blob: 7b83b9ebfab837486543b6c818b8b9a14b92c161 [file] [log] [blame]
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define DEBUG
14
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/types.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/sched.h>
25#include <linux/poll.h>
26#include <linux/wakelock.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/debugfs.h>
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -060030#include <linux/rwsem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include <asm/uaccess.h>
33#include <asm/byteorder.h>
34
35#include <mach/smem_log.h>
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060036#include <mach/subsystem_notif.h>
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -060037#include <mach/msm_ipc_router.h>
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060038#include <mach/msm_ipc_logging.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "ipc_router.h"
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060041#include "modem_notifier.h"
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -060042#include "msm_ipc_router_security.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44enum {
45 SMEM_LOG = 1U << 0,
46 RTR_DBG = 1U << 1,
47 R2R_MSG = 1U << 2,
48 R2R_RAW = 1U << 3,
49 NTFY_MSG = 1U << 4,
50 R2R_RAW_HDR = 1U << 5,
51};
52
53static int msm_ipc_router_debug_mask;
54module_param_named(debug_mask, msm_ipc_router_debug_mask,
55 int, S_IRUGO | S_IWUSR | S_IWGRP);
56
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060057static void *ipc_rtr_log_ctxt;
58#define IPC_RTR_LOG_PAGES 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#define DIAG(x...) pr_info("[RR] ERROR " x)
60
61#if defined(DEBUG)
62#define D(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060063if (ipc_rtr_log_ctxt) \
64 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065if (msm_ipc_router_debug_mask & RTR_DBG) \
66 pr_info(x); \
67} while (0)
68
69#define RR(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060070if (ipc_rtr_log_ctxt) \
71 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072if (msm_ipc_router_debug_mask & R2R_MSG) \
73 pr_info("[RR] "x); \
74} while (0)
75
76#define RAW(x...) do { \
77if (msm_ipc_router_debug_mask & R2R_RAW) \
78 pr_info("[RAW] "x); \
79} while (0)
80
81#define NTFY(x...) do { \
82if (msm_ipc_router_debug_mask & NTFY_MSG) \
83 pr_info("[NOTIFY] "x); \
84} while (0)
85
86#define RAW_HDR(x...) do { \
87if (msm_ipc_router_debug_mask & R2R_RAW_HDR) \
88 pr_info("[HDR] "x); \
89} while (0)
90#else
91#define D(x...) do { } while (0)
92#define RR(x...) do { } while (0)
93#define RAW(x...) do { } while (0)
94#define RAW_HDR(x...) do { } while (0)
95#define NTFY(x...) do { } while (0)
96#endif
97
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +053098#define IPC_ROUTER_LOG_EVENT_ERROR 0x00
99#define IPC_ROUTER_LOG_EVENT_TX 0x01
100#define IPC_ROUTER_LOG_EVENT_RX 0x02
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530101#define IPC_ROUTER_DUMMY_DEST_NODE 0xFFFFFFFF
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102
103static LIST_HEAD(control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600104static DECLARE_RWSEM(control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105
106#define LP_HASH_SIZE 32
107static struct list_head local_ports[LP_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600108static DECLARE_RWSEM(local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600110/*
111 * Server info is organized as a hash table. The server's service ID is
112 * used to index into the hash table. The instance ID of most of the servers
113 * are 1 or 2. The service IDs are well distributed compared to the instance
114 * IDs and hence choosing service ID to index into this hash table optimizes
115 * the hash table operations like add, lookup, destroy.
116 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117#define SRV_HASH_SIZE 32
118static struct list_head server_list[SRV_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600119static DECLARE_RWSEM(server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
121struct msm_ipc_server {
122 struct list_head list;
123 struct msm_ipc_port_name name;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600124 int synced_sec_rule;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600125 char pdev_name[32];
126 int next_pdev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 struct list_head server_port_list;
128};
129
130struct msm_ipc_server_port {
131 struct list_head list;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600132 struct platform_device pdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 struct msm_ipc_port_addr server_addr;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600134 struct msm_ipc_router_xprt_info *xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135};
136
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530137struct msm_ipc_resume_tx_port {
138 struct list_head list;
139 uint32_t port_id;
140 uint32_t node_id;
141};
142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143#define RP_HASH_SIZE 32
144struct msm_ipc_router_remote_port {
145 struct list_head list;
146 uint32_t node_id;
147 uint32_t port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 uint32_t tx_quota_cnt;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600149 struct mutex quota_lock_lhb2;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530150 struct list_head resume_tx_port_list;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600151 void *sec_rule;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600152 struct msm_ipc_server *server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153};
154
155struct msm_ipc_router_xprt_info {
156 struct list_head list;
157 struct msm_ipc_router_xprt *xprt;
158 uint32_t remote_node_id;
159 uint32_t initialized;
160 struct list_head pkt_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 struct wake_lock wakelock;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600162 struct mutex rx_lock_lhb2;
163 struct mutex tx_lock_lhb2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 uint32_t need_len;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600165 uint32_t abort_data_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 struct work_struct read_data;
167 struct workqueue_struct *workqueue;
168};
169
170#define RT_HASH_SIZE 4
171struct msm_ipc_routing_table_entry {
172 struct list_head list;
173 uint32_t node_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600174 uint32_t neighbor_node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175 struct list_head remote_port_list[RP_HASH_SIZE];
176 struct msm_ipc_router_xprt_info *xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600177 struct rw_semaphore lock_lha4;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 unsigned long num_tx_bytes;
179 unsigned long num_rx_bytes;
180};
181
182static struct list_head routing_table[RT_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600183static DECLARE_RWSEM(routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184static int routing_table_inited;
185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186static void do_read_data(struct work_struct *work);
187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188static LIST_HEAD(xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600189static DECLARE_RWSEM(xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -0600191static DECLARE_COMPLETION(msm_ipc_local_router_up);
192#define IPC_ROUTER_INIT_TIMEOUT (10 * HZ)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193
194static uint32_t next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600195static DEFINE_MUTEX(next_port_id_lock_lha1);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600196static struct workqueue_struct *msm_ipc_router_workqueue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197
198enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 DOWN,
200 UP,
201};
202
203static void init_routing_table(void)
204{
205 int i;
206 for (i = 0; i < RT_HASH_SIZE; i++)
207 INIT_LIST_HEAD(&routing_table[i]);
208}
209
210static struct msm_ipc_routing_table_entry *alloc_routing_table_entry(
211 uint32_t node_id)
212{
213 int i;
214 struct msm_ipc_routing_table_entry *rt_entry;
215
216 rt_entry = kmalloc(sizeof(struct msm_ipc_routing_table_entry),
217 GFP_KERNEL);
218 if (!rt_entry) {
219 pr_err("%s: rt_entry allocation failed for %d\n",
220 __func__, node_id);
221 return NULL;
222 }
223
224 for (i = 0; i < RP_HASH_SIZE; i++)
225 INIT_LIST_HEAD(&rt_entry->remote_port_list[i]);
226
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600227 init_rwsem(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 rt_entry->node_id = node_id;
229 rt_entry->xprt_info = NULL;
230 return rt_entry;
231}
232
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600233/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234static int add_routing_table_entry(
235 struct msm_ipc_routing_table_entry *rt_entry)
236{
237 uint32_t key;
238
239 if (!rt_entry)
240 return -EINVAL;
241
242 key = (rt_entry->node_id % RT_HASH_SIZE);
243 list_add_tail(&rt_entry->list, &routing_table[key]);
244 return 0;
245}
246
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600247/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248static struct msm_ipc_routing_table_entry *lookup_routing_table(
249 uint32_t node_id)
250{
251 uint32_t key = (node_id % RT_HASH_SIZE);
252 struct msm_ipc_routing_table_entry *rt_entry;
253
254 list_for_each_entry(rt_entry, &routing_table[key], list) {
255 if (rt_entry->node_id == node_id)
256 return rt_entry;
257 }
258 return NULL;
259}
260
261struct rr_packet *rr_read(struct msm_ipc_router_xprt_info *xprt_info)
262{
263 struct rr_packet *temp_pkt;
264
265 if (!xprt_info)
266 return NULL;
267
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600268 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600269 if (xprt_info->abort_data_read) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600270 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -0600271 pr_err("%s detected SSR & exiting now\n",
272 xprt_info->xprt->name);
273 return NULL;
274 }
275
276 if (list_empty(&xprt_info->pkt_list)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600277 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600278 return NULL;
279 }
280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 temp_pkt = list_first_entry(&xprt_info->pkt_list,
282 struct rr_packet, list);
283 list_del(&temp_pkt->list);
284 if (list_empty(&xprt_info->pkt_list))
285 wake_unlock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600286 mutex_unlock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return temp_pkt;
288}
289
290struct rr_packet *clone_pkt(struct rr_packet *pkt)
291{
292 struct rr_packet *cloned_pkt;
293 struct sk_buff *temp_skb, *cloned_skb;
294 struct sk_buff_head *pkt_fragment_q;
295
296 cloned_pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
297 if (!cloned_pkt) {
298 pr_err("%s: failure\n", __func__);
299 return NULL;
300 }
301
302 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
303 if (!pkt_fragment_q) {
304 pr_err("%s: pkt_frag_q alloc failure\n", __func__);
305 kfree(cloned_pkt);
306 return NULL;
307 }
308 skb_queue_head_init(pkt_fragment_q);
309
310 skb_queue_walk(pkt->pkt_fragment_q, temp_skb) {
311 cloned_skb = skb_clone(temp_skb, GFP_KERNEL);
312 if (!cloned_skb)
313 goto fail_clone;
314 skb_queue_tail(pkt_fragment_q, cloned_skb);
315 }
316 cloned_pkt->pkt_fragment_q = pkt_fragment_q;
317 cloned_pkt->length = pkt->length;
318 return cloned_pkt;
319
320fail_clone:
321 while (!skb_queue_empty(pkt_fragment_q)) {
322 temp_skb = skb_dequeue(pkt_fragment_q);
323 kfree_skb(temp_skb);
324 }
325 kfree(pkt_fragment_q);
326 kfree(cloned_pkt);
327 return NULL;
328}
329
330struct rr_packet *create_pkt(struct sk_buff_head *data)
331{
332 struct rr_packet *pkt;
333 struct sk_buff *temp_skb;
334
335 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
336 if (!pkt) {
337 pr_err("%s: failure\n", __func__);
338 return NULL;
339 }
340
341 pkt->pkt_fragment_q = data;
342 skb_queue_walk(pkt->pkt_fragment_q, temp_skb)
343 pkt->length += temp_skb->len;
344 return pkt;
345}
346
347void release_pkt(struct rr_packet *pkt)
348{
349 struct sk_buff *temp_skb;
350
351 if (!pkt)
352 return;
353
354 if (!pkt->pkt_fragment_q) {
355 kfree(pkt);
356 return;
357 }
358
359 while (!skb_queue_empty(pkt->pkt_fragment_q)) {
360 temp_skb = skb_dequeue(pkt->pkt_fragment_q);
361 kfree_skb(temp_skb);
362 }
363 kfree(pkt->pkt_fragment_q);
364 kfree(pkt);
365 return;
366}
367
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600368static struct sk_buff_head *msm_ipc_router_buf_to_skb(void *buf,
369 unsigned int buf_len)
370{
371 struct sk_buff_head *skb_head;
372 struct sk_buff *skb;
373 int first = 1, offset = 0;
374 int skb_size, data_size;
375 void *data;
376
377 skb_head = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
378 if (!skb_head) {
379 pr_err("%s: Couldnot allocate skb_head\n", __func__);
380 return NULL;
381 }
382 skb_queue_head_init(skb_head);
383
384 data_size = buf_len;
385 while (offset != buf_len) {
386 skb_size = data_size;
387 if (first)
388 skb_size += IPC_ROUTER_HDR_SIZE;
389
390 skb = alloc_skb(skb_size, GFP_KERNEL);
391 if (!skb) {
392 if (skb_size <= (PAGE_SIZE/2)) {
393 pr_err("%s: cannot allocate skb\n", __func__);
394 goto buf_to_skb_error;
395 }
396 data_size = data_size / 2;
397 continue;
398 }
399
400 if (first) {
401 skb_reserve(skb, IPC_ROUTER_HDR_SIZE);
402 first = 0;
403 }
404
405 data = skb_put(skb, data_size);
406 memcpy(skb->data, buf + offset, data_size);
407 skb_queue_tail(skb_head, skb);
408 offset += data_size;
409 data_size = buf_len - offset;
410 }
411 return skb_head;
412
413buf_to_skb_error:
414 while (!skb_queue_empty(skb_head)) {
415 skb = skb_dequeue(skb_head);
416 kfree_skb(skb);
417 }
418 kfree(skb_head);
419 return NULL;
420}
421
422static void *msm_ipc_router_skb_to_buf(struct sk_buff_head *skb_head,
423 unsigned int len)
424{
425 struct sk_buff *temp;
426 int offset = 0, buf_len = 0, copy_len;
427 void *buf;
428
429 if (!skb_head) {
430 pr_err("%s: NULL skb_head\n", __func__);
431 return NULL;
432 }
433
434 temp = skb_peek(skb_head);
435 buf_len = len;
436 buf = kmalloc(buf_len, GFP_KERNEL);
437 if (!buf) {
438 pr_err("%s: cannot allocate buf\n", __func__);
439 return NULL;
440 }
441 skb_queue_walk(skb_head, temp) {
442 copy_len = buf_len < temp->len ? buf_len : temp->len;
443 memcpy(buf + offset, temp->data, copy_len);
444 offset += copy_len;
445 buf_len -= copy_len;
446 }
447 return buf;
448}
449
450static void msm_ipc_router_free_skb(struct sk_buff_head *skb_head)
451{
452 struct sk_buff *temp_skb;
453
454 if (!skb_head)
455 return;
456
457 while (!skb_queue_empty(skb_head)) {
458 temp_skb = skb_dequeue(skb_head);
459 kfree_skb(temp_skb);
460 }
461 kfree(skb_head);
462}
463
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600464static int post_pkt_to_port(struct msm_ipc_port *port_ptr,
465 struct rr_packet *pkt, int clone)
466{
467 struct rr_packet *temp_pkt = pkt;
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530468 void (*notify)(unsigned event, void *priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600469
470 if (unlikely(!port_ptr || !pkt))
471 return -EINVAL;
472
473 if (clone) {
474 temp_pkt = clone_pkt(pkt);
475 if (!temp_pkt) {
476 pr_err("%s: Error cloning packet for port %08x:%08x\n",
477 __func__, port_ptr->this_port.node_id,
478 port_ptr->this_port.port_id);
479 return -ENOMEM;
480 }
481 }
482
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600483 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600484 wake_lock(&port_ptr->port_rx_wake_lock);
485 list_add_tail(&temp_pkt->list, &port_ptr->port_rx_q);
486 wake_up(&port_ptr->port_rx_wait_q);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530487 notify = port_ptr->notify;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600488 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530489 if (notify)
490 notify(MSM_IPC_ROUTER_READ_CB, port_ptr->priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600491 return 0;
492}
493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494static int post_control_ports(struct rr_packet *pkt)
495{
496 struct msm_ipc_port *port_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497
498 if (!pkt)
499 return -EINVAL;
500
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600501 down_read(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600502 list_for_each_entry(port_ptr, &control_ports, list)
503 post_pkt_to_port(port_ptr, pkt, 1);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600504 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 return 0;
506}
507
508static uint32_t allocate_port_id(void)
509{
510 uint32_t port_id = 0, prev_port_id, key;
511 struct msm_ipc_port *port_ptr;
512
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600513 mutex_lock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 prev_port_id = next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600515 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 do {
517 next_port_id++;
518 if ((next_port_id & 0xFFFFFFFE) == 0xFFFFFFFE)
519 next_port_id = 1;
520
521 key = (next_port_id & (LP_HASH_SIZE - 1));
522 if (list_empty(&local_ports[key])) {
523 port_id = next_port_id;
524 break;
525 }
526 list_for_each_entry(port_ptr, &local_ports[key], list) {
527 if (port_ptr->this_port.port_id == next_port_id) {
528 port_id = next_port_id;
529 break;
530 }
531 }
532 if (!port_id) {
533 port_id = next_port_id;
534 break;
535 }
536 port_id = 0;
537 } while (next_port_id != prev_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600538 up_read(&local_ports_lock_lha2);
539 mutex_unlock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540
541 return port_id;
542}
543
544void msm_ipc_router_add_local_port(struct msm_ipc_port *port_ptr)
545{
546 uint32_t key;
547
548 if (!port_ptr)
549 return;
550
551 key = (port_ptr->this_port.port_id & (LP_HASH_SIZE - 1));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600552 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 list_add_tail(&port_ptr->list, &local_ports[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600554 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
556
557struct msm_ipc_port *msm_ipc_router_create_raw_port(void *endpoint,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600558 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 void *priv)
560{
561 struct msm_ipc_port *port_ptr;
562
563 port_ptr = kzalloc(sizeof(struct msm_ipc_port), GFP_KERNEL);
564 if (!port_ptr)
565 return NULL;
566
567 port_ptr->this_port.node_id = IPC_ROUTER_NID_LOCAL;
568 port_ptr->this_port.port_id = allocate_port_id();
569 if (!port_ptr->this_port.port_id) {
570 pr_err("%s: All port ids are in use\n", __func__);
571 kfree(port_ptr);
572 return NULL;
573 }
574
575 spin_lock_init(&port_ptr->port_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 INIT_LIST_HEAD(&port_ptr->port_rx_q);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600577 mutex_init(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 init_waitqueue_head(&port_ptr->port_rx_wait_q);
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600579 snprintf(port_ptr->rx_wakelock_name, MAX_WAKELOCK_NAME_SZ,
Karthikeyan Ramasubramanian090486e2013-02-14 13:53:20 -0700580 "ipc%08x_%s",
581 port_ptr->this_port.port_id,
582 current->comm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 wake_lock_init(&port_ptr->port_rx_wake_lock,
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600584 WAKE_LOCK_SUSPEND, port_ptr->rx_wakelock_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 port_ptr->endpoint = endpoint;
587 port_ptr->notify = notify;
588 port_ptr->priv = priv;
589
590 msm_ipc_router_add_local_port(port_ptr);
591 return port_ptr;
592}
593
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600594/* Must be called with local_ports_lock_lha2 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595static struct msm_ipc_port *msm_ipc_router_lookup_local_port(uint32_t port_id)
596{
597 int key = (port_id & (LP_HASH_SIZE - 1));
598 struct msm_ipc_port *port_ptr;
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 list_for_each_entry(port_ptr, &local_ports[key], list) {
601 if (port_ptr->this_port.port_id == port_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 return port_ptr;
603 }
604 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 return NULL;
606}
607
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600608/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609static struct msm_ipc_router_remote_port *msm_ipc_router_lookup_remote_port(
610 uint32_t node_id,
611 uint32_t port_id)
612{
613 struct msm_ipc_router_remote_port *rport_ptr;
614 struct msm_ipc_routing_table_entry *rt_entry;
615 int key = (port_id & (RP_HASH_SIZE - 1));
616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 rt_entry = lookup_routing_table(node_id);
618 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 pr_err("%s: Node is not up\n", __func__);
620 return NULL;
621 }
622
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600623 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 list_for_each_entry(rport_ptr,
625 &rt_entry->remote_port_list[key], list) {
626 if (rport_ptr->port_id == port_id) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600627 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 return rport_ptr;
629 }
630 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600631 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 return NULL;
633}
634
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600635/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636static struct msm_ipc_router_remote_port *msm_ipc_router_create_remote_port(
637 uint32_t node_id,
638 uint32_t port_id)
639{
640 struct msm_ipc_router_remote_port *rport_ptr;
641 struct msm_ipc_routing_table_entry *rt_entry;
642 int key = (port_id & (RP_HASH_SIZE - 1));
643
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 rt_entry = lookup_routing_table(node_id);
645 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 pr_err("%s: Node is not up\n", __func__);
647 return NULL;
648 }
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 rport_ptr = kmalloc(sizeof(struct msm_ipc_router_remote_port),
651 GFP_KERNEL);
652 if (!rport_ptr) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 pr_err("%s: Remote port alloc failed\n", __func__);
654 return NULL;
655 }
656 rport_ptr->port_id = port_id;
657 rport_ptr->node_id = node_id;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600658 rport_ptr->sec_rule = NULL;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600659 rport_ptr->server = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 rport_ptr->tx_quota_cnt = 0;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600661 mutex_init(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530662 INIT_LIST_HEAD(&rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600663 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 list_add_tail(&rport_ptr->list,
665 &rt_entry->remote_port_list[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600666 up_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 return rport_ptr;
668}
669
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530670/**
671 * msm_ipc_router_free_resume_tx_port() - Free the resume_tx ports
672 * @rport_ptr: Pointer to the remote port.
673 *
674 * This function deletes all the resume_tx ports associated with a remote port
675 * and frees the memory allocated to each resume_tx port.
676 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600677 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530678 */
679static void msm_ipc_router_free_resume_tx_port(
680 struct msm_ipc_router_remote_port *rport_ptr)
681{
682 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
683
684 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
685 &rport_ptr->resume_tx_port_list, list) {
686 list_del(&rtx_port->list);
687 kfree(rtx_port);
688 }
689}
690
691/**
692 * msm_ipc_router_lookup_resume_tx_port() - Lookup resume_tx port list
693 * @rport_ptr: Remote port whose resume_tx port list needs to be looked.
694 * @port_id: Port ID which needs to be looked from the list.
695 *
696 * return 1 if the port_id is found in the list, else 0.
697 *
698 * This function is used to lookup the existence of a local port in
699 * remote port's resume_tx list. This function is used to ensure that
700 * the same port is not added to the remote_port's resume_tx list repeatedly.
701 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600702 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530703 */
704static int msm_ipc_router_lookup_resume_tx_port(
705 struct msm_ipc_router_remote_port *rport_ptr, uint32_t port_id)
706{
707 struct msm_ipc_resume_tx_port *rtx_port;
708
709 list_for_each_entry(rtx_port, &rport_ptr->resume_tx_port_list, list) {
710 if (port_id == rtx_port->port_id)
711 return 1;
712 }
713 return 0;
714}
715
716/**
717 * post_resume_tx() - Post the resume_tx event
718 * @rport_ptr: Pointer to the remote port
719 * @pkt : The data packet that is received on a resume_tx event
720 *
721 * This function informs about the reception of the resume_tx message from a
722 * remote port pointed by rport_ptr to all the local ports that are in the
723 * resume_tx_ports_list of this remote port. On posting the information, this
724 * function sequentially deletes each entry in the resume_tx_port_list of the
725 * remote port.
726 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600727 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530728 */
729static void post_resume_tx(struct msm_ipc_router_remote_port *rport_ptr,
730 struct rr_packet *pkt)
731{
732 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
733 struct msm_ipc_port *local_port;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530734
735 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
736 &rport_ptr->resume_tx_port_list, list) {
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530737 local_port =
738 msm_ipc_router_lookup_local_port(rtx_port->port_id);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530739 if (local_port && local_port->notify)
740 local_port->notify(MSM_IPC_ROUTER_RESUME_TX,
741 local_port->priv);
742 else if (local_port)
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600743 post_pkt_to_port(local_port, pkt, 1);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530744 else
745 pr_err("%s: Local Port %d not Found",
746 __func__, rtx_port->port_id);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530747 list_del(&rtx_port->list);
748 kfree(rtx_port);
749 }
750}
751
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600752/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753static void msm_ipc_router_destroy_remote_port(
754 struct msm_ipc_router_remote_port *rport_ptr)
755{
756 uint32_t node_id;
757 struct msm_ipc_routing_table_entry *rt_entry;
758
759 if (!rport_ptr)
760 return;
761
762 node_id = rport_ptr->node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 rt_entry = lookup_routing_table(node_id);
764 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 pr_err("%s: Node %d is not up\n", __func__, node_id);
766 return;
767 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600768 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 list_del(&rport_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600770 up_write(&rt_entry->lock_lha4);
771 mutex_lock(&rport_ptr->quota_lock_lhb2);
772 msm_ipc_router_free_resume_tx_port(rport_ptr);
773 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 kfree(rport_ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 return;
776}
777
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600778/**
779 * msm_ipc_router_lookup_server() - Lookup server information
780 * @service: Service ID of the server info to be looked up.
781 * @instance: Instance ID of the server info to be looked up.
782 * @node_id: Node/Processor ID in which the server is hosted.
783 * @port_id: Port ID within the node in which the server is hosted.
784 *
785 * @return: If found Pointer to server structure, else NULL.
786 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600787 * Note1: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600788 * Note2: If the <node_id:port_id> are <0:0>, then the lookup is restricted
789 * to <service:instance>. Used only when a client wants to send a
790 * message to any QMI server.
791 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792static struct msm_ipc_server *msm_ipc_router_lookup_server(
793 uint32_t service,
794 uint32_t instance,
795 uint32_t node_id,
796 uint32_t port_id)
797{
798 struct msm_ipc_server *server;
799 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600800 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 list_for_each_entry(server, &server_list[key], list) {
803 if ((server->name.service != service) ||
804 (server->name.instance != instance))
805 continue;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600806 if ((node_id == 0) && (port_id == 0))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 list_for_each_entry(server_port, &server->server_port_list,
809 list) {
810 if ((server_port->server_addr.node_id == node_id) &&
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600811 (server_port->server_addr.port_id == port_id))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 }
814 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 return NULL;
816}
817
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600818static void dummy_release(struct device *dev)
819{
820}
821
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600822/**
823 * msm_ipc_router_create_server() - Add server info to hash table
824 * @service: Service ID of the server info to be created.
825 * @instance: Instance ID of the server info to be created.
826 * @node_id: Node/Processor ID in which the server is hosted.
827 * @port_id: Port ID within the node in which the server is hosted.
828 * @xprt_info: XPRT through which the node hosting the server is reached.
829 *
830 * @return: Pointer to server structure on success, else NULL.
831 *
832 * This function adds the server info to the hash table. If the same
833 * server(i.e. <service_id:instance_id>) is hosted in different nodes,
834 * they are maintained as list of "server_port" under "server" structure.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600835 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600836 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837static struct msm_ipc_server *msm_ipc_router_create_server(
838 uint32_t service,
839 uint32_t instance,
840 uint32_t node_id,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600841 uint32_t port_id,
842 struct msm_ipc_router_xprt_info *xprt_info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843{
844 struct msm_ipc_server *server = NULL;
845 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600846 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 list_for_each_entry(server, &server_list[key], list) {
849 if ((server->name.service == service) &&
850 (server->name.instance == instance))
851 goto create_srv_port;
852 }
853
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600854 server = kzalloc(sizeof(struct msm_ipc_server), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 if (!server) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 pr_err("%s: Server allocation failed\n", __func__);
857 return NULL;
858 }
859 server->name.service = service;
860 server->name.instance = instance;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600861 server->synced_sec_rule = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 INIT_LIST_HEAD(&server->server_port_list);
863 list_add_tail(&server->list, &server_list[key]);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600864 scnprintf(server->pdev_name, sizeof(server->pdev_name),
865 "QMI%08x:%08x", service, instance);
866 server->next_pdev_id = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867
868create_srv_port:
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600869 server_port = kzalloc(sizeof(struct msm_ipc_server_port), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 if (!server_port) {
871 if (list_empty(&server->server_port_list)) {
872 list_del(&server->list);
873 kfree(server);
874 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 pr_err("%s: Server Port allocation failed\n", __func__);
876 return NULL;
877 }
878 server_port->server_addr.node_id = node_id;
879 server_port->server_addr.port_id = port_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600880 server_port->xprt_info = xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 list_add_tail(&server_port->list, &server->server_port_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600883 server_port->pdev.name = server->pdev_name;
884 server_port->pdev.id = server->next_pdev_id++;
885 server_port->pdev.dev.release = dummy_release;
886 platform_device_register(&server_port->pdev);
887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 return server;
889}
890
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600891/**
892 * msm_ipc_router_destroy_server() - Remove server info from hash table
893 * @server: Server info to be removed.
894 * @node_id: Node/Processor ID in which the server is hosted.
895 * @port_id: Port ID within the node in which the server is hosted.
896 *
897 * This function removes the server_port identified using <node_id:port_id>
898 * from the server structure. If the server_port list under server structure
899 * is empty after removal, then remove the server structure from the server
900 * hash table.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600901 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600902 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903static void msm_ipc_router_destroy_server(struct msm_ipc_server *server,
904 uint32_t node_id, uint32_t port_id)
905{
906 struct msm_ipc_server_port *server_port;
907
908 if (!server)
909 return;
910
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 list_for_each_entry(server_port, &server->server_port_list, list) {
912 if ((server_port->server_addr.node_id == node_id) &&
913 (server_port->server_addr.port_id == port_id))
914 break;
915 }
916 if (server_port) {
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600917 platform_device_unregister(&server_port->pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 list_del(&server_port->list);
919 kfree(server_port);
920 }
921 if (list_empty(&server->server_port_list)) {
922 list_del(&server->list);
923 kfree(server);
924 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 return;
926}
927
928static int msm_ipc_router_send_control_msg(
929 struct msm_ipc_router_xprt_info *xprt_info,
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530930 union rr_control_msg *msg,
931 uint32_t dst_node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932{
933 struct rr_packet *pkt;
934 struct sk_buff *ipc_rtr_pkt;
935 struct rr_header *hdr;
936 int pkt_size;
937 void *data;
938 struct sk_buff_head *pkt_fragment_q;
939 int ret;
940
941 if (!xprt_info || ((msg->cmd != IPC_ROUTER_CTRL_CMD_HELLO) &&
942 !xprt_info->initialized)) {
943 pr_err("%s: xprt_info not initialized\n", __func__);
944 return -EINVAL;
945 }
946
947 if (xprt_info->remote_node_id == IPC_ROUTER_NID_LOCAL)
948 return 0;
949
950 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
951 if (!pkt) {
952 pr_err("%s: pkt alloc failed\n", __func__);
953 return -ENOMEM;
954 }
955
956 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
957 if (!pkt_fragment_q) {
958 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
959 kfree(pkt);
960 return -ENOMEM;
961 }
962 skb_queue_head_init(pkt_fragment_q);
963
964 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
965 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
966 if (!ipc_rtr_pkt) {
967 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
968 kfree(pkt_fragment_q);
969 kfree(pkt);
970 return -ENOMEM;
971 }
972
973 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
974 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
975 memcpy(data, msg, sizeof(*msg));
976 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
977 if (!hdr) {
978 pr_err("%s: skb_push failed\n", __func__);
979 kfree_skb(ipc_rtr_pkt);
980 kfree(pkt_fragment_q);
981 kfree(pkt);
982 return -ENOMEM;
983 }
984
985 hdr->version = IPC_ROUTER_VERSION;
986 hdr->type = msg->cmd;
987 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
988 hdr->src_port_id = IPC_ROUTER_ADDRESS;
989 hdr->confirm_rx = 0;
990 hdr->size = sizeof(*msg);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530991 if (hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX)
992 hdr->dst_node_id = dst_node_id;
993 else
994 hdr->dst_node_id = xprt_info->remote_node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
996 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
997 pkt->pkt_fragment_q = pkt_fragment_q;
998 pkt->length = pkt_size;
999
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001000 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001001 ret = xprt_info->xprt->write(pkt, pkt_size, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001002 mutex_unlock(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
1004 release_pkt(pkt);
1005 return ret;
1006}
1007
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001008static int msm_ipc_router_send_server_list(uint32_t node_id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 struct msm_ipc_router_xprt_info *xprt_info)
1010{
1011 union rr_control_msg ctl;
1012 struct msm_ipc_server *server;
1013 struct msm_ipc_server_port *server_port;
1014 int i;
1015
1016 if (!xprt_info || !xprt_info->initialized) {
1017 pr_err("%s: Xprt info not initialized\n", __func__);
1018 return -EINVAL;
1019 }
1020
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001021 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 for (i = 0; i < SRV_HASH_SIZE; i++) {
1025 list_for_each_entry(server, &server_list[i], list) {
1026 ctl.srv.service = server->name.service;
1027 ctl.srv.instance = server->name.instance;
1028 list_for_each_entry(server_port,
1029 &server->server_port_list, list) {
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001030 if (server_port->server_addr.node_id !=
1031 node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 continue;
1033
1034 ctl.srv.node_id =
1035 server_port->server_addr.node_id;
1036 ctl.srv.port_id =
1037 server_port->server_addr.port_id;
1038 msm_ipc_router_send_control_msg(xprt_info,
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301039 &ctl, IPC_ROUTER_DUMMY_DEST_NODE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 }
1041 }
1042 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043
1044 return 0;
1045}
1046
1047#if defined(DEBUG)
1048static char *type_to_str(int i)
1049{
1050 switch (i) {
1051 case IPC_ROUTER_CTRL_CMD_DATA:
1052 return "data ";
1053 case IPC_ROUTER_CTRL_CMD_HELLO:
1054 return "hello ";
1055 case IPC_ROUTER_CTRL_CMD_BYE:
1056 return "bye ";
1057 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
1058 return "new_srvr";
1059 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
1060 return "rmv_srvr";
1061 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
1062 return "rmv_clnt";
1063 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
1064 return "resum_tx";
1065 case IPC_ROUTER_CTRL_CMD_EXIT:
1066 return "cmd_exit";
1067 default:
1068 return "invalid";
1069 }
1070}
1071#endif
1072
1073static int broadcast_ctl_msg_locally(union rr_control_msg *msg)
1074{
1075 struct rr_packet *pkt;
1076 struct sk_buff *ipc_rtr_pkt;
1077 struct rr_header *hdr;
1078 int pkt_size;
1079 void *data;
1080 struct sk_buff_head *pkt_fragment_q;
1081 int ret;
1082
1083 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
1084 if (!pkt) {
1085 pr_err("%s: pkt alloc failed\n", __func__);
1086 return -ENOMEM;
1087 }
1088
1089 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
1090 if (!pkt_fragment_q) {
1091 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
1092 kfree(pkt);
1093 return -ENOMEM;
1094 }
1095 skb_queue_head_init(pkt_fragment_q);
1096
1097 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
1098 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
1099 if (!ipc_rtr_pkt) {
1100 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
1101 kfree(pkt_fragment_q);
1102 kfree(pkt);
1103 return -ENOMEM;
1104 }
1105
1106 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1107 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
1108 memcpy(data, msg, sizeof(*msg));
1109 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1110 if (!hdr) {
1111 pr_err("%s: skb_push failed\n", __func__);
1112 kfree_skb(ipc_rtr_pkt);
1113 kfree(pkt_fragment_q);
1114 kfree(pkt);
1115 return -ENOMEM;
1116 }
1117 hdr->version = IPC_ROUTER_VERSION;
1118 hdr->type = msg->cmd;
1119 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
1120 hdr->src_port_id = IPC_ROUTER_ADDRESS;
1121 hdr->confirm_rx = 0;
1122 hdr->size = sizeof(*msg);
1123 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
1124 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
1125 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
1126 pkt->pkt_fragment_q = pkt_fragment_q;
1127 pkt->length = pkt_size;
1128
1129 ret = post_control_ports(pkt);
1130 release_pkt(pkt);
1131 return ret;
1132}
1133
1134static int broadcast_ctl_msg(union rr_control_msg *ctl)
1135{
1136 struct msm_ipc_router_xprt_info *xprt_info;
1137
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001138 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 list_for_each_entry(xprt_info, &xprt_info_list, list) {
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301140 msm_ipc_router_send_control_msg(xprt_info, ctl,
1141 IPC_ROUTER_DUMMY_DEST_NODE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001143 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144
1145 return 0;
1146}
1147
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001148static int relay_ctl_msg(struct msm_ipc_router_xprt_info *xprt_info,
1149 union rr_control_msg *ctl)
1150{
1151 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1152
1153 if (!xprt_info || !ctl)
1154 return -EINVAL;
1155
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001156 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001157 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
1158 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301159 msm_ipc_router_send_control_msg(fwd_xprt_info, ctl,
1160 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001161 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001162 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001163
1164 return 0;
1165}
1166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167static int relay_msg(struct msm_ipc_router_xprt_info *xprt_info,
1168 struct rr_packet *pkt)
1169{
1170 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1171
1172 if (!xprt_info || !pkt)
1173 return -EINVAL;
1174
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001175 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001177 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001179 fwd_xprt_info->xprt->write(pkt, pkt->length,
1180 fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001181 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001183 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 return 0;
1185}
1186
1187static int forward_msg(struct msm_ipc_router_xprt_info *xprt_info,
1188 struct rr_packet *pkt)
1189{
1190 uint32_t dst_node_id;
1191 struct sk_buff *head_pkt;
1192 struct rr_header *hdr;
1193 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1194 struct msm_ipc_routing_table_entry *rt_entry;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001195 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196
1197 if (!xprt_info || !pkt)
1198 return -EINVAL;
1199
1200 head_pkt = skb_peek(pkt->pkt_fragment_q);
1201 if (!head_pkt)
1202 return -EINVAL;
1203
1204 hdr = (struct rr_header *)head_pkt->data;
1205 dst_node_id = hdr->dst_node_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001206 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 rt_entry = lookup_routing_table(dst_node_id);
1208 if (!(rt_entry) || !(rt_entry->xprt_info)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001209 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 pr_err("%s: Routing table not initialized\n", __func__);
1211 return -ENODEV;
1212 }
1213
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001214 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 fwd_xprt_info = rt_entry->xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 if (xprt_info->remote_node_id == fwd_xprt_info->remote_node_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 pr_err("%s: Discarding Command to route back\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001218 ret = -EINVAL;
1219 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 }
1221
1222 if (xprt_info->xprt->link_id == fwd_xprt_info->xprt->link_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 pr_err("%s: DST in the same cluster\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001224 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001226 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001227 fwd_xprt_info->xprt->write(pkt, pkt->length, fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001228 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
1229fwd_msg_out:
1230 up_read(&rt_entry->lock_lha4);
1231 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001233 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234}
1235
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001236static int msm_ipc_router_send_remove_client(struct comm_mode_info *mode_info,
1237 uint32_t node_id, uint32_t port_id)
1238{
1239 union rr_control_msg msg;
1240 struct msm_ipc_router_xprt_info *tmp_xprt_info;
1241 int mode;
1242 void *xprt_info;
1243 int rc = 0;
1244
1245 if (!mode_info) {
1246 pr_err("%s: NULL mode_info\n", __func__);
1247 return -EINVAL;
1248 }
1249 mode = mode_info->mode;
1250 xprt_info = mode_info->xprt_info;
1251
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001252 memset(&msg, 0, sizeof(msg));
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001253 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1254 msg.cli.node_id = node_id;
1255 msg.cli.port_id = port_id;
1256
1257 if ((mode == SINGLE_LINK_MODE) && xprt_info) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001258 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001259 list_for_each_entry(tmp_xprt_info, &xprt_info_list, list) {
1260 if (tmp_xprt_info != xprt_info)
1261 continue;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301262 msm_ipc_router_send_control_msg(tmp_xprt_info, &msg,
1263 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001264 break;
1265 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001266 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001267 } else if ((mode == SINGLE_LINK_MODE) && !xprt_info) {
1268 broadcast_ctl_msg_locally(&msg);
1269 } else if (mode == MULTI_LINK_MODE) {
1270 broadcast_ctl_msg(&msg);
1271 broadcast_ctl_msg_locally(&msg);
1272 } else if (mode != NULL_MODE) {
1273 pr_err("%s: Invalid mode(%d) + xprt_inf(%p) for %08x:%08x\n",
1274 __func__, mode, xprt_info, node_id, port_id);
1275 rc = -EINVAL;
1276 }
1277 return rc;
1278}
1279
1280static void update_comm_mode_info(struct comm_mode_info *mode_info,
1281 struct msm_ipc_router_xprt_info *xprt_info)
1282{
1283 if (!mode_info) {
1284 pr_err("%s: NULL mode_info\n", __func__);
1285 return;
1286 }
1287
1288 if (mode_info->mode == NULL_MODE) {
1289 mode_info->xprt_info = xprt_info;
1290 mode_info->mode = SINGLE_LINK_MODE;
1291 } else if (mode_info->mode == SINGLE_LINK_MODE &&
1292 mode_info->xprt_info != xprt_info) {
1293 mode_info->mode = MULTI_LINK_MODE;
1294 }
1295
1296 return;
1297}
1298
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001299static void cleanup_rmt_server(struct msm_ipc_router_xprt_info *xprt_info,
1300 struct msm_ipc_router_remote_port *rport_ptr)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001301{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001302 union rr_control_msg ctl;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001303 struct msm_ipc_server *server = rport_ptr->server;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001304
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001305 D("Remove server %08x:%08x - %08x:%08x",
1306 server->name.service, server->name.instance,
1307 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001308 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001309 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001310 ctl.srv.service = server->name.service;
1311 ctl.srv.instance = server->name.instance;
1312 ctl.srv.node_id = rport_ptr->node_id;
1313 ctl.srv.port_id = rport_ptr->port_id;
1314 relay_ctl_msg(xprt_info, &ctl);
1315 broadcast_ctl_msg_locally(&ctl);
1316 msm_ipc_router_destroy_server(server,
1317 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001318}
1319
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001320static void cleanup_rmt_ports(struct msm_ipc_router_xprt_info *xprt_info,
1321 struct msm_ipc_routing_table_entry *rt_entry)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001322{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001323 struct msm_ipc_router_remote_port *rport_ptr, *tmp_rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001324 union rr_control_msg ctl;
1325 int j;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001326
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001327 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001328 for (j = 0; j < RP_HASH_SIZE; j++) {
1329 list_for_each_entry_safe(rport_ptr, tmp_rport_ptr,
1330 &rt_entry->remote_port_list[j], list) {
1331 list_del(&rport_ptr->list);
1332 mutex_lock(&rport_ptr->quota_lock_lhb2);
1333 msm_ipc_router_free_resume_tx_port(rport_ptr);
1334 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1335
1336 if (rport_ptr->server)
1337 cleanup_rmt_server(xprt_info, rport_ptr);
1338
1339 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1340 ctl.cli.node_id = rport_ptr->node_id;
1341 ctl.cli.port_id = rport_ptr->port_id;
1342 relay_ctl_msg(xprt_info, &ctl);
1343 broadcast_ctl_msg_locally(&ctl);
1344 kfree(rport_ptr);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001345 }
1346 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001347}
1348
1349static void msm_ipc_cleanup_routing_table(
1350 struct msm_ipc_router_xprt_info *xprt_info)
1351{
1352 int i;
1353 struct msm_ipc_routing_table_entry *rt_entry;
1354
1355 if (!xprt_info) {
1356 pr_err("%s: Invalid xprt_info\n", __func__);
1357 return;
1358 }
1359
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001360 down_write(&server_list_lock_lha2);
1361 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001362 for (i = 0; i < RT_HASH_SIZE; i++) {
1363 list_for_each_entry(rt_entry, &routing_table[i], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001364 down_write(&rt_entry->lock_lha4);
1365 if (rt_entry->xprt_info != xprt_info) {
1366 up_write(&rt_entry->lock_lha4);
1367 continue;
1368 }
1369 cleanup_rmt_ports(xprt_info, rt_entry);
1370 rt_entry->xprt_info = NULL;
1371 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001372 }
1373 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001374 up_write(&routing_table_lock_lha3);
1375 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001376}
1377
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001378/**
1379 * sync_sec_rule() - Synchrnoize the security rule into the server structure
1380 * @server: Server structure where the rule has to be synchronized.
1381 * @rule: Security tule to be synchronized.
1382 *
1383 * This function is used to update the server structure with the security
1384 * rule configured for the <service:instance> corresponding to that server.
1385 */
1386static void sync_sec_rule(struct msm_ipc_server *server, void *rule)
1387{
1388 struct msm_ipc_server_port *server_port;
1389 struct msm_ipc_router_remote_port *rport_ptr = NULL;
1390
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001391 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001392 list_for_each_entry(server_port, &server->server_port_list, list) {
1393 rport_ptr = msm_ipc_router_lookup_remote_port(
1394 server_port->server_addr.node_id,
1395 server_port->server_addr.port_id);
1396 if (!rport_ptr)
1397 continue;
1398 rport_ptr->sec_rule = rule;
1399 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001400 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001401 server->synced_sec_rule = 1;
1402}
1403
1404/**
1405 * msm_ipc_sync_sec_rule() - Sync the security rule to the service
1406 * @service: Service for which the rule has to be synchronized.
1407 * @instance: Instance for which the rule has to be synchronized.
1408 * @rule: Security rule to be synchronized.
1409 *
1410 * This function is used to syncrhonize the security rule with the server
1411 * hash table, if the user-space script configures the rule after the service
1412 * has come up. This function is used to synchronize the security rule to a
1413 * specific service and optionally a specific instance.
1414 */
1415void msm_ipc_sync_sec_rule(uint32_t service, uint32_t instance, void *rule)
1416{
1417 int key = (service & (SRV_HASH_SIZE - 1));
1418 struct msm_ipc_server *server;
1419
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001420 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001421 list_for_each_entry(server, &server_list[key], list) {
1422 if (server->name.service != service)
1423 continue;
1424
1425 if (server->name.instance != instance &&
1426 instance != ALL_INSTANCE)
1427 continue;
1428
1429 /*
1430 * If the rule applies to all instances and if the specific
1431 * instance of a service has a rule synchronized already,
1432 * do not apply the rule for that specific instance.
1433 */
1434 if (instance == ALL_INSTANCE && server->synced_sec_rule)
1435 continue;
1436
1437 sync_sec_rule(server, rule);
1438 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001439 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001440}
1441
1442/**
1443 * msm_ipc_sync_default_sec_rule() - Default security rule to all services
1444 * @rule: Security rule to be synchronized.
1445 *
1446 * This function is used to syncrhonize the security rule with the server
1447 * hash table, if the user-space script configures the rule after the service
1448 * has come up. This function is used to synchronize the security rule that
1449 * applies to all services, if the concerned service do not have any rule
1450 * defined.
1451 */
1452void msm_ipc_sync_default_sec_rule(void *rule)
1453{
1454 int key;
1455 struct msm_ipc_server *server;
1456
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001457 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001458 for (key = 0; key < SRV_HASH_SIZE; key++) {
1459 list_for_each_entry(server, &server_list[key], list) {
1460 if (server->synced_sec_rule)
1461 continue;
1462
1463 sync_sec_rule(server, rule);
1464 }
1465 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001466 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001467}
1468
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001469static int process_hello_msg(struct msm_ipc_router_xprt_info *xprt_info,
1470 struct rr_header *hdr)
1471{
1472 int i, rc = 0;
1473 union rr_control_msg ctl;
1474 struct msm_ipc_routing_table_entry *rt_entry;
1475
1476 if (!hdr)
1477 return -EINVAL;
1478
1479 RR("o HELLO NID %d\n", hdr->src_node_id);
1480
1481 xprt_info->remote_node_id = hdr->src_node_id;
1482 /*
1483 * Find the entry from Routing Table corresponding to Node ID.
1484 * Under SSR, an entry will be found. When the system boots up
1485 * for the 1st time, an entry will not be found and hence allocate
1486 * an entry. Update the entry with the Node ID that it corresponds
1487 * to and the XPRT through which it can be reached.
1488 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001489 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001490 rt_entry = lookup_routing_table(hdr->src_node_id);
1491 if (!rt_entry) {
1492 rt_entry = alloc_routing_table_entry(hdr->src_node_id);
1493 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001494 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001495 pr_err("%s: rt_entry allocation failed\n", __func__);
1496 return -ENOMEM;
1497 }
1498 add_routing_table_entry(rt_entry);
1499 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001500 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001501 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1502 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001503 up_write(&rt_entry->lock_lha4);
1504 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001505
1506 /* Send a reply HELLO message */
1507 memset(&ctl, 0, sizeof(ctl));
1508 ctl.cmd = IPC_ROUTER_CTRL_CMD_HELLO;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301509 rc = msm_ipc_router_send_control_msg(xprt_info, &ctl,
1510 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001511 if (rc < 0) {
1512 pr_err("%s: Error sending reply HELLO message\n", __func__);
1513 return rc;
1514 }
1515 xprt_info->initialized = 1;
1516
1517 /*
1518 * Send list of servers from the local node and from nodes
1519 * outside the mesh network in which this XPRT is part of.
1520 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001521 down_read(&server_list_lock_lha2);
1522 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001523 for (i = 0; i < RT_HASH_SIZE; i++) {
1524 list_for_each_entry(rt_entry, &routing_table[i], list) {
1525 if ((rt_entry->node_id != IPC_ROUTER_NID_LOCAL) &&
Karthikeyan Ramasubramanian72ad5792013-01-30 14:17:57 -07001526 (!rt_entry->xprt_info ||
1527 (rt_entry->xprt_info->xprt->link_id ==
1528 xprt_info->xprt->link_id)))
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001529 continue;
1530 rc = msm_ipc_router_send_server_list(rt_entry->node_id,
1531 xprt_info);
1532 if (rc < 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001533 up_read(&routing_table_lock_lha3);
1534 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001535 return rc;
1536 }
1537 }
1538 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001539 up_read(&routing_table_lock_lha3);
1540 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001541 RR("HELLO message processed\n");
1542 return rc;
1543}
1544
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001545static int process_resume_tx_msg(union rr_control_msg *msg,
1546 struct rr_packet *pkt)
1547{
1548 struct msm_ipc_router_remote_port *rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001549 int ret = 0;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001550
1551 RR("o RESUME_TX id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
1552
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001553 down_read(&local_ports_lock_lha2);
1554 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001555 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1556 msg->cli.port_id);
1557 if (!rport_ptr) {
1558 pr_err("%s: Unable to resume client\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001559 ret = -ENODEV;
1560 goto prtm_out;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001561 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001562 mutex_lock(&rport_ptr->quota_lock_lhb2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001563 rport_ptr->tx_quota_cnt = 0;
1564 post_resume_tx(rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001565 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1566prtm_out:
1567 up_read(&routing_table_lock_lha3);
1568 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001569 return 0;
1570}
1571
1572static int process_new_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1573 union rr_control_msg *msg, struct rr_packet *pkt)
1574{
1575 struct msm_ipc_routing_table_entry *rt_entry;
1576 struct msm_ipc_server *server;
1577 struct msm_ipc_router_remote_port *rport_ptr;
1578
1579 if (msg->srv.instance == 0) {
1580 pr_err("%s: Server %08x create rejected, version = 0\n",
1581 __func__, msg->srv.service);
1582 return -EINVAL;
1583 }
1584
1585 RR("o NEW_SERVER id=%d:%08x service=%08x:%08x\n", msg->srv.node_id,
1586 msg->srv.port_id, msg->srv.service, msg->srv.instance);
1587 /*
1588 * Find the entry from Routing Table corresponding to Node ID.
1589 * Under SSR, an entry will be found. When the subsystem hosting
1590 * service is not adjacent, an entry will not be found and hence
1591 * allocate an entry. Update the entry with the Node ID that it
1592 * corresponds to and the XPRT through which it can be reached.
1593 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001594 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001595 rt_entry = lookup_routing_table(msg->srv.node_id);
1596 if (!rt_entry) {
1597 rt_entry = alloc_routing_table_entry(msg->srv.node_id);
1598 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001599 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001600 pr_err("%s: rt_entry allocation failed\n", __func__);
1601 return -ENOMEM;
1602 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001603 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001604 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1605 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001606 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001607 add_routing_table_entry(rt_entry);
1608 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001609 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001610
1611 /*
1612 * If the service does not exist already in the database, create and
1613 * store the service info. Create a remote port structure in which
1614 * the service is hosted and cache the security rule for the service
1615 * in that remote port structure.
1616 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001617 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001618 server = msm_ipc_router_lookup_server(msg->srv.service,
1619 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1620 if (!server) {
1621 server = msm_ipc_router_create_server(
1622 msg->srv.service, msg->srv.instance,
1623 msg->srv.node_id, msg->srv.port_id, xprt_info);
1624 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001625 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001626 pr_err("%s: Server Create failed\n", __func__);
1627 return -ENOMEM;
1628 }
1629
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001630 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001631 if (!msm_ipc_router_lookup_remote_port(
1632 msg->srv.node_id, msg->srv.port_id)) {
1633 rport_ptr = msm_ipc_router_create_remote_port(
1634 msg->srv.node_id, msg->srv.port_id);
1635 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001636 up_read(&routing_table_lock_lha3);
1637 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001638 return -ENOMEM;
1639 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001640 rport_ptr->server = server;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001641 rport_ptr->sec_rule = msm_ipc_get_security_rule(
1642 msg->srv.service,
1643 msg->srv.instance);
1644 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001645 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001646 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001647 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001648
1649 /*
1650 * Relay the new server message to other subsystems that do not belong
1651 * to the cluster from which this message is received. Notify the
1652 * local clients waiting for this service.
1653 */
1654 relay_msg(xprt_info, pkt);
1655 post_control_ports(pkt);
1656 return 0;
1657}
1658
1659static int process_rmv_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1660 union rr_control_msg *msg, struct rr_packet *pkt)
1661{
1662 struct msm_ipc_server *server;
1663
1664 RR("o REMOVE_SERVER service=%08x:%d\n",
1665 msg->srv.service, msg->srv.instance);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001666 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001667 server = msm_ipc_router_lookup_server(msg->srv.service,
1668 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1669 if (server) {
1670 msm_ipc_router_destroy_server(server, msg->srv.node_id,
1671 msg->srv.port_id);
1672 /*
1673 * Relay the new server message to other subsystems that do not
1674 * belong to the cluster from which this message is received.
1675 * Notify the local clients communicating with the service.
1676 */
1677 relay_msg(xprt_info, pkt);
1678 post_control_ports(pkt);
1679 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001680 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001681 return 0;
1682}
1683
1684static int process_rmv_client_msg(struct msm_ipc_router_xprt_info *xprt_info,
1685 union rr_control_msg *msg, struct rr_packet *pkt)
1686{
1687 struct msm_ipc_router_remote_port *rport_ptr;
1688
1689 RR("o REMOVE_CLIENT id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001690 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001691 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1692 msg->cli.port_id);
1693 if (rport_ptr)
1694 msm_ipc_router_destroy_remote_port(rport_ptr);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001695 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001696
1697 relay_msg(xprt_info, pkt);
1698 post_control_ports(pkt);
1699 return 0;
1700}
1701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001702static int process_control_msg(struct msm_ipc_router_xprt_info *xprt_info,
1703 struct rr_packet *pkt)
1704{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705 union rr_control_msg *msg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001706 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001707 struct sk_buff *temp_ptr;
1708 struct rr_header *hdr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709
1710 if (pkt->length != (IPC_ROUTER_HDR_SIZE + sizeof(*msg))) {
1711 pr_err("%s: r2r msg size %d != %d\n", __func__, pkt->length,
1712 (IPC_ROUTER_HDR_SIZE + sizeof(*msg)));
1713 return -EINVAL;
1714 }
1715
1716 temp_ptr = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001717 if (!temp_ptr) {
1718 pr_err("%s: pkt_fragment_q is empty\n", __func__);
1719 return -EINVAL;
1720 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 hdr = (struct rr_header *)temp_ptr->data;
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001722 if (!hdr) {
1723 pr_err("%s: No data inside the skb\n", __func__);
1724 return -EINVAL;
1725 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 msg = (union rr_control_msg *)((char *)hdr + IPC_ROUTER_HDR_SIZE);
1727
1728 switch (msg->cmd) {
1729 case IPC_ROUTER_CTRL_CMD_HELLO:
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001730 rc = process_hello_msg(xprt_info, hdr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 break;
1732 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001733 rc = process_resume_tx_msg(msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001736 rc = process_new_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 break;
1738 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001739 rc = process_rmv_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 break;
1741 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001742 rc = process_rmv_client_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 break;
1744 case IPC_ROUTER_CTRL_CMD_PING:
1745 /* No action needed for ping messages received */
1746 RR("o PING\n");
1747 break;
1748 default:
1749 RR("o UNKNOWN(%08x)\n", msg->cmd);
1750 rc = -ENOSYS;
1751 }
1752
1753 return rc;
1754}
1755
1756static void do_read_data(struct work_struct *work)
1757{
1758 struct rr_header *hdr;
1759 struct rr_packet *pkt = NULL;
1760 struct msm_ipc_port *port_ptr;
1761 struct sk_buff *head_skb;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001762 struct msm_ipc_router_remote_port *rport_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001763
1764 struct msm_ipc_router_xprt_info *xprt_info =
1765 container_of(work,
1766 struct msm_ipc_router_xprt_info,
1767 read_data);
1768
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001769 while ((pkt = rr_read(xprt_info)) != NULL) {
1770 if (pkt->length < IPC_ROUTER_HDR_SIZE ||
1771 pkt->length > MAX_IPC_PKT_SIZE) {
1772 pr_err("%s: Invalid pkt length %d\n",
1773 __func__, pkt->length);
1774 goto fail_data;
1775 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001777 head_skb = skb_peek(pkt->pkt_fragment_q);
1778 if (!head_skb) {
1779 pr_err("%s: head_skb is invalid\n", __func__);
1780 goto fail_data;
1781 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001783 hdr = (struct rr_header *)(head_skb->data);
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06001784 RAW("ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n",
1785 hdr->version, hdr->type, hdr->src_node_id,
1786 hdr->src_port_id, hdr->confirm_rx, hdr->size,
1787 hdr->dst_node_id, hdr->dst_port_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001788
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001789 if (hdr->version != IPC_ROUTER_VERSION) {
1790 pr_err("version %d != %d\n",
1791 hdr->version, IPC_ROUTER_VERSION);
1792 goto fail_data;
1793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001795 if ((hdr->dst_node_id != IPC_ROUTER_NID_LOCAL) &&
1796 ((hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX) ||
1797 (hdr->type == IPC_ROUTER_CTRL_CMD_DATA))) {
1798 forward_msg(xprt_info, pkt);
1799 release_pkt(pkt);
1800 continue;
1801 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001803 if ((hdr->dst_port_id == IPC_ROUTER_ADDRESS) ||
1804 (hdr->type == IPC_ROUTER_CTRL_CMD_HELLO)) {
1805 process_control_msg(xprt_info, pkt);
1806 release_pkt(pkt);
1807 continue;
1808 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809#if defined(CONFIG_MSM_SMD_LOGGING)
1810#if defined(DEBUG)
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001811 if (msm_ipc_router_debug_mask & SMEM_LOG) {
1812 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05301813 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001814 IPC_ROUTER_LOG_EVENT_RX),
1815 (hdr->src_node_id << 24) |
1816 (hdr->src_port_id & 0xffffff),
1817 (hdr->dst_node_id << 24) |
1818 (hdr->dst_port_id & 0xffffff),
1819 (hdr->type << 24) | (hdr->confirm_rx << 16) |
1820 (hdr->size & 0xffff));
1821 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001822#endif
1823#endif
1824
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001825 down_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001826 port_ptr = msm_ipc_router_lookup_local_port(hdr->dst_port_id);
1827 if (!port_ptr) {
1828 pr_err("%s: No local port id %08x\n", __func__,
1829 hdr->dst_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001830 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001831 release_pkt(pkt);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301832 return;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001833 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001834
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001835 down_read(&routing_table_lock_lha3);
1836 rport_ptr = msm_ipc_router_lookup_remote_port(hdr->src_node_id,
1837 hdr->src_port_id);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001838 if (!rport_ptr) {
1839 rport_ptr = msm_ipc_router_create_remote_port(
1840 hdr->src_node_id,
1841 hdr->src_port_id);
1842 if (!rport_ptr) {
1843 pr_err("%s: Rmt Prt %08x:%08x create failed\n",
1844 __func__, hdr->src_node_id,
1845 hdr->src_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001846 up_read(&routing_table_lock_lha3);
1847 up_read(&local_ports_lock_lha2);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301848 return;
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001849 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001851 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06001852 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001853 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001855 return;
1856
1857fail_data:
1858 release_pkt(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001859 pr_err("ipc_router has died\n");
1860}
1861
1862int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr,
1863 struct msm_ipc_addr *name)
1864{
1865 struct msm_ipc_server *server;
1866 unsigned long flags;
1867 union rr_control_msg ctl;
1868
1869 if (!port_ptr || !name)
1870 return -EINVAL;
1871
1872 if (name->addrtype != MSM_IPC_ADDR_NAME)
1873 return -EINVAL;
1874
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001875 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001876 server = msm_ipc_router_lookup_server(name->addr.port_name.service,
1877 name->addr.port_name.instance,
1878 IPC_ROUTER_NID_LOCAL,
1879 port_ptr->this_port.port_id);
1880 if (server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001881 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 pr_err("%s: Server already present\n", __func__);
1883 return -EINVAL;
1884 }
1885
1886 server = msm_ipc_router_create_server(name->addr.port_name.service,
1887 name->addr.port_name.instance,
1888 IPC_ROUTER_NID_LOCAL,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001889 port_ptr->this_port.port_id,
1890 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001892 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 pr_err("%s: Server Creation failed\n", __func__);
1894 return -EINVAL;
1895 }
1896
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001897 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1899 ctl.srv.service = server->name.service;
1900 ctl.srv.instance = server->name.instance;
1901 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1902 ctl.srv.port_id = port_ptr->this_port.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001903 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 broadcast_ctl_msg(&ctl);
1905 spin_lock_irqsave(&port_ptr->port_lock, flags);
1906 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001907 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 port_ptr->port_name.service = server->name.service;
1909 port_ptr->port_name.instance = server->name.instance;
1910 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1911 return 0;
1912}
1913
1914int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1915{
1916 struct msm_ipc_server *server;
1917 unsigned long flags;
1918 union rr_control_msg ctl;
1919
1920 if (!port_ptr)
1921 return -EINVAL;
1922
1923 if (port_ptr->type != SERVER_PORT) {
1924 pr_err("%s: Trying to unregister a non-server port\n",
1925 __func__);
1926 return -EINVAL;
1927 }
1928
1929 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1930 pr_err("%s: Trying to unregister a remote server locally\n",
1931 __func__);
1932 return -EINVAL;
1933 }
1934
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001935 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001936 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1937 port_ptr->port_name.instance,
1938 port_ptr->this_port.node_id,
1939 port_ptr->this_port.port_id);
1940 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001941 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001942 pr_err("%s: Server lookup failed\n", __func__);
1943 return -ENODEV;
1944 }
1945
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001946 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1948 ctl.srv.service = server->name.service;
1949 ctl.srv.instance = server->name.instance;
1950 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1951 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1953 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001954 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06001955 broadcast_ctl_msg(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 spin_lock_irqsave(&port_ptr->port_lock, flags);
1957 port_ptr->type = CLIENT_PORT;
1958 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1959 return 0;
1960}
1961
1962static int loopback_data(struct msm_ipc_port *src,
1963 uint32_t port_id,
1964 struct sk_buff_head *data)
1965{
1966 struct sk_buff *head_skb;
1967 struct rr_header *hdr;
1968 struct msm_ipc_port *port_ptr;
1969 struct rr_packet *pkt;
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07001970 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001971
1972 if (!data) {
1973 pr_err("%s: Invalid pkt pointer\n", __func__);
1974 return -EINVAL;
1975 }
1976
1977 pkt = create_pkt(data);
1978 if (!pkt) {
1979 pr_err("%s: New pkt create failed\n", __func__);
1980 return -ENOMEM;
1981 }
1982
1983 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001984 if (!head_skb) {
1985 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik0e83d3b2013-05-01 16:25:00 -06001986 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001987 return -EINVAL;
1988 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
1990 if (!hdr) {
1991 pr_err("%s: Prepend Header failed\n", __func__);
1992 release_pkt(pkt);
1993 return -ENOMEM;
1994 }
1995 hdr->version = IPC_ROUTER_VERSION;
1996 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
1997 hdr->src_node_id = src->this_port.node_id;
1998 hdr->src_port_id = src->this_port.port_id;
1999 hdr->size = pkt->length;
2000 hdr->confirm_rx = 0;
2001 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2002 hdr->dst_port_id = port_id;
2003 pkt->length += IPC_ROUTER_HDR_SIZE;
2004
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002005 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002006 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2007 if (!port_ptr) {
2008 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002009 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 release_pkt(pkt);
2011 return -ENODEV;
2012 }
2013
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002014 ret_len = pkt->length;
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06002015 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002016 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002017 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002019 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002020}
2021
2022static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2023 struct msm_ipc_router_remote_port *rport_ptr,
2024 struct rr_packet *pkt)
2025{
2026 struct sk_buff *head_skb;
2027 struct rr_header *hdr;
2028 struct msm_ipc_router_xprt_info *xprt_info;
2029 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302030 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032
2033 if (!rport_ptr || !src || !pkt)
2034 return -EINVAL;
2035
2036 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002037 if (!head_skb) {
2038 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2039 return -EINVAL;
2040 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002041 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2042 if (!hdr) {
2043 pr_err("%s: Prepend Header failed\n", __func__);
2044 return -ENOMEM;
2045 }
2046 hdr->version = IPC_ROUTER_VERSION;
2047 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2048 hdr->src_node_id = src->this_port.node_id;
2049 hdr->src_port_id = src->this_port.port_id;
2050 hdr->size = pkt->length;
2051 hdr->confirm_rx = 0;
2052 hdr->dst_node_id = rport_ptr->node_id;
2053 hdr->dst_port_id = rport_ptr->port_id;
2054 pkt->length += IPC_ROUTER_HDR_SIZE;
2055
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002056 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302057 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2058 if (msm_ipc_router_lookup_resume_tx_port(
2059 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002060 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302061 return -EAGAIN;
2062 }
2063 resume_tx_port =
2064 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2065 GFP_KERNEL);
2066 if (!resume_tx_port) {
2067 pr_err("%s: Resume_Tx port allocation failed\n",
2068 __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002069 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302070 return -ENOMEM;
2071 }
2072 INIT_LIST_HEAD(&resume_tx_port->list);
2073 resume_tx_port->port_id = src->this_port.port_id;
2074 resume_tx_port->node_id = src->this_port.node_id;
2075 list_add_tail(&resume_tx_port->list,
2076 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002077 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302078 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079 }
2080 rport_ptr->tx_quota_cnt++;
2081 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2082 hdr->confirm_rx = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002083 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 rt_entry = lookup_routing_table(hdr->dst_node_id);
2086 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087 pr_err("%s: Remote node %d not up\n",
2088 __func__, hdr->dst_node_id);
2089 return -ENODEV;
2090 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002091 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002092 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002093 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002094 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002095 mutex_unlock(&xprt_info->tx_lock_lhb2);
2096 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002097
2098 if (ret < 0) {
2099 pr_err("%s: Write on XPRT failed\n", __func__);
2100 return ret;
2101 }
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002102 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002103
2104 RAW_HDR("[w rr_h] "
2105 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2106 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2107 hdr->version, type_to_str(hdr->type),
2108 hdr->src_node_id, hdr->src_port_id,
2109 hdr->confirm_rx, hdr->size,
2110 hdr->dst_node_id, hdr->dst_port_id);
2111
2112#if defined(CONFIG_MSM_SMD_LOGGING)
2113#if defined(DEBUG)
2114 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2115 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05302116 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002117 IPC_ROUTER_LOG_EVENT_TX),
2118 (hdr->src_node_id << 24) |
2119 (hdr->src_port_id & 0xffffff),
2120 (hdr->dst_node_id << 24) |
2121 (hdr->dst_port_id & 0xffffff),
2122 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2123 (hdr->size & 0xffff));
2124 }
2125#endif
2126#endif
2127
2128 return pkt->length;
2129}
2130
2131int msm_ipc_router_send_to(struct msm_ipc_port *src,
2132 struct sk_buff_head *data,
2133 struct msm_ipc_addr *dest)
2134{
2135 uint32_t dst_node_id = 0, dst_port_id = 0;
2136 struct msm_ipc_server *server;
2137 struct msm_ipc_server_port *server_port;
2138 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2139 struct rr_packet *pkt;
2140 int ret;
2141
2142 if (!src || !data || !dest) {
2143 pr_err("%s: Invalid Parameters\n", __func__);
2144 return -EINVAL;
2145 }
2146
2147 /* Resolve Address*/
2148 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2149 dst_node_id = dest->addr.port_addr.node_id;
2150 dst_port_id = dest->addr.port_addr.port_id;
2151 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002152 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153 server = msm_ipc_router_lookup_server(
2154 dest->addr.port_name.service,
2155 dest->addr.port_name.instance,
2156 0, 0);
2157 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002158 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 pr_err("%s: Destination not reachable\n", __func__);
2160 return -ENODEV;
2161 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 server_port = list_first_entry(&server->server_port_list,
2163 struct msm_ipc_server_port,
2164 list);
2165 dst_node_id = server_port->server_addr.node_id;
2166 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002167 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 }
2169 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2170 ret = loopback_data(src, dst_port_id, data);
2171 return ret;
2172 }
2173
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002174 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002175 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2176 dst_port_id);
2177 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002178 up_read(&routing_table_lock_lha3);
Zaheerulla Meer2c515312013-05-10 15:51:28 +05302179 pr_err("%s: Remote port not found\n", __func__);
2180 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 }
2182
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002183 if (src->check_send_permissions) {
2184 ret = src->check_send_permissions(rport_ptr->sec_rule);
2185 if (ret <= 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002186 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002187 pr_err("%s: permission failure for %s\n",
2188 __func__, current->comm);
2189 return -EPERM;
2190 }
2191 }
2192
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193 pkt = create_pkt(data);
2194 if (!pkt) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002195 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 pr_err("%s: Pkt creation failed\n", __func__);
2197 return -ENOMEM;
2198 }
2199
2200 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002201 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202 release_pkt(pkt);
2203
2204 return ret;
2205}
2206
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002207int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2208 struct msm_ipc_addr *dest,
2209 void *data, unsigned int data_len)
2210{
2211 struct sk_buff_head *out_skb_head;
2212 int ret;
2213
2214 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2215 if (!out_skb_head) {
2216 pr_err("%s: SKB conversion failed\n", __func__);
2217 return -EFAULT;
2218 }
2219
2220 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meerc8400402013-05-08 19:27:27 +05302221 if (ret == -EAGAIN)
2222 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002223 if (ret < 0) {
2224 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2225 __func__, ret);
2226 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meerfc59a8a2013-06-26 22:39:00 +05302227 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002228 }
2229 return 0;
2230}
2231
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302232/**
2233 * msm_ipc_router_send_resume_tx() - Send Resume_Tx message
2234 * @data: Pointer to received data packet that has confirm_rx bit set
2235 *
2236 * @return: On success, number of bytes transferred is returned, else
2237 * standard linux error code is returned.
2238 *
2239 * This function sends the Resume_Tx event to the remote node that
2240 * sent the data with confirm_rx field set. In case of a multi-hop
2241 * scenario also, this function makes sure that the destination node_id
2242 * to which the resume_tx event should reach is right.
2243 */
2244static int msm_ipc_router_send_resume_tx(void *data)
2245{
2246 union rr_control_msg msg;
2247 struct rr_header *hdr = (struct rr_header *)data;
2248 struct msm_ipc_routing_table_entry *rt_entry;
2249 int ret;
2250
2251 memset(&msg, 0, sizeof(msg));
2252 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
2253 msg.cli.node_id = hdr->dst_node_id;
2254 msg.cli.port_id = hdr->dst_port_id;
2255 down_read(&routing_table_lock_lha3);
2256 rt_entry = lookup_routing_table(hdr->src_node_id);
2257 if (!rt_entry) {
2258 pr_err("%s: %d Node is not present",
2259 __func__, hdr->src_node_id);
2260 up_read(&routing_table_lock_lha3);
2261 return -ENODEV;
2262 }
2263 RR("x RESUME_TX id=%d:%08x\n",
2264 msg.cli.node_id, msg.cli.port_id);
2265 ret = msm_ipc_router_send_control_msg(rt_entry->xprt_info, &msg,
2266 hdr->src_node_id);
2267 up_read(&routing_table_lock_lha3);
2268 if (ret < 0)
2269 pr_err("%s: Send Resume_Tx Failed SRC_NODE: %d SRC_PORT: %d DEST_NODE: %d",
2270 __func__, hdr->dst_node_id, hdr->dst_port_id,
2271 hdr->src_node_id);
2272
2273 return ret;
2274}
2275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2277 struct sk_buff_head **data,
2278 size_t buf_len)
2279{
2280 struct rr_packet *pkt;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302281 struct sk_buff *head_skb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282 int ret;
2283
2284 if (!port_ptr || !data)
2285 return -EINVAL;
2286
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002287 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002288 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002289 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290 return -EAGAIN;
2291 }
2292
2293 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2294 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002295 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 return -ETOOSMALL;
2297 }
2298 list_del(&pkt->list);
2299 if (list_empty(&port_ptr->port_rx_q))
2300 wake_unlock(&port_ptr->port_rx_wake_lock);
2301 *data = pkt->pkt_fragment_q;
2302 ret = pkt->length;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002303 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302304 kfree(pkt);
2305 head_skb = skb_peek(*data);
2306 if (!head_skb) {
2307 pr_err("%s: Socket Buffer not found", __func__);
2308 return -EFAULT;
2309 }
2310 if (((struct rr_header *)(head_skb->data))->confirm_rx)
2311 msm_ipc_router_send_resume_tx((void *)(head_skb->data));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002312
2313 return ret;
2314}
2315
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302316/**
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302317 * msm_ipc_router_rx_data_wait() - Wait for new message destined to a local port.
2318 * @port_ptr: Pointer to the local port
2319 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2320 * > 0 timeout indicates the wait time.
2321 * 0 indicates that we do not wait.
2322 * @return: 0 if there are pending messages to read,
2323 * standard Linux error code otherwise.
2324 *
2325 * Checks for the availability of messages that are destined to a local port.
2326 * If no messages are present then waits as per @timeout.
2327 */
2328int msm_ipc_router_rx_data_wait(struct msm_ipc_port *port_ptr, long timeout)
2329{
2330 int ret = 0;
2331
2332 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2333 while (list_empty(&port_ptr->port_rx_q)) {
2334 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2335 if (timeout < 0) {
2336 ret = wait_event_interruptible(
2337 port_ptr->port_rx_wait_q,
2338 !list_empty(&port_ptr->port_rx_q));
2339 if (ret)
2340 return ret;
2341 } else if (timeout > 0) {
2342 timeout = wait_event_interruptible_timeout(
2343 port_ptr->port_rx_wait_q,
2344 !list_empty(&port_ptr->port_rx_q),
2345 timeout);
2346 if (timeout < 0)
2347 return -EFAULT;
2348 }
2349 if (timeout == 0)
2350 return -ENOMSG;
2351 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2352 }
2353 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2354
2355 return ret;
2356}
2357
2358/**
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302359 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2360 * @port_ptr: Pointer to the local port
2361 * @data : Pointer to the socket buffer head
2362 * @src: Pointer to local port address
2363 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2364 * > 0 timeout indicates the wait time.
2365 * 0 indicates that we do not wait.
2366 * @return: = Number of bytes read(On successful read operation).
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302367 * = -ENOMSG (If there are no pending messages and timeout is 0).
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302368 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2369 * = -EFAULT (If there are no pending messages when timeout is > 0
2370 * and the wait_event_interruptible_timeout has returned value > 0)
2371 * = -ERESTARTSYS (If there are no pending messages when timeout
2372 * is < 0 and wait_event_interruptible was interrupted by a signal)
2373 *
2374 * This function reads the messages that are destined for a local port. It
2375 * is used by modules that exist with-in the kernel and use IPC Router for
2376 * transport. The function checks if there are any messages that are already
2377 * received. If yes, it reads them, else it waits as per the timeout value.
2378 * On a successful read, the return value of the function indicates the number
2379 * of bytes that are read.
2380 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002381int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2382 struct sk_buff_head **data,
2383 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002384 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385{
2386 int ret, data_len, align_size;
2387 struct sk_buff *temp_skb;
2388 struct rr_header *hdr = NULL;
2389
2390 if (!port_ptr || !data) {
2391 pr_err("%s: Invalid pointers being passed\n", __func__);
2392 return -EINVAL;
2393 }
2394
2395 *data = NULL;
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302396
2397 ret = msm_ipc_router_rx_data_wait(port_ptr, timeout);
2398 if (ret)
2399 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400
2401 ret = msm_ipc_router_read(port_ptr, data, 0);
2402 if (ret <= 0 || !(*data))
2403 return ret;
2404
2405 temp_skb = skb_peek(*data);
2406 hdr = (struct rr_header *)(temp_skb->data);
2407 if (src) {
2408 src->addrtype = MSM_IPC_ADDR_ID;
2409 src->addr.port_addr.node_id = hdr->src_node_id;
2410 src->addr.port_addr.port_id = hdr->src_port_id;
2411 }
2412
2413 data_len = hdr->size;
2414 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2415 align_size = ALIGN_SIZE(data_len);
2416 if (align_size) {
2417 temp_skb = skb_peek_tail(*data);
2418 skb_trim(temp_skb, (temp_skb->len - align_size));
2419 }
2420 return data_len;
2421}
2422
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002423int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2424 struct msm_ipc_addr *src,
2425 unsigned char **data,
2426 unsigned int *len)
2427{
2428 struct sk_buff_head *in_skb_head;
2429 int ret;
2430
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302431 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2432
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002433 if (ret < 0) {
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302434 if (ret != -ENOMSG)
2435 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2436 __func__, ret);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002437 return ret;
2438 }
2439
2440 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2441 if (!(*data))
2442 pr_err("%s: Buf conversion failed\n", __func__);
2443
2444 *len = ret;
2445 msm_ipc_router_free_skb(in_skb_head);
2446 return 0;
2447}
2448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002449struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002450 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451 void *priv)
2452{
2453 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002454 int ret;
2455
2456 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2457 if (ret < 0) {
2458 pr_err("%s: Error waiting for local router\n", __func__);
2459 return NULL;
2460 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461
2462 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2463 if (!port_ptr)
2464 pr_err("%s: port_ptr alloc failed\n", __func__);
2465
2466 return port_ptr;
2467}
2468
2469int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2470{
2471 union rr_control_msg msg;
2472 struct rr_packet *pkt, *temp_pkt;
2473 struct msm_ipc_server *server;
2474
2475 if (!port_ptr)
2476 return -EINVAL;
2477
2478 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002479 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002480 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002481 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002482
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002483 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06002484 memset(&msg, 0, sizeof(msg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002485 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2486 msg.srv.service = port_ptr->port_name.service;
2487 msg.srv.instance = port_ptr->port_name.instance;
2488 msg.srv.node_id = port_ptr->this_port.node_id;
2489 msg.srv.port_id = port_ptr->this_port.port_id;
2490 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2491 msg.srv.service, msg.srv.instance,
2492 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002493 broadcast_ctl_msg(&msg);
2494 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495 }
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002496
2497 /*
2498 * Server port could have been a client port earlier.
2499 * Send REMOVE_CLIENT message in either case.
2500 */
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002501 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002502 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2503 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2504 port_ptr->this_port.node_id,
2505 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002506 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002507 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002508 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002509 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002510 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002511 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002512 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002513 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002514 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515 }
2516
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002517 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002518 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2519 list_del(&pkt->list);
2520 release_pkt(pkt);
2521 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002522 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002525 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526 server = msm_ipc_router_lookup_server(
2527 port_ptr->port_name.service,
2528 port_ptr->port_name.instance,
2529 port_ptr->this_port.node_id,
2530 port_ptr->this_port.port_id);
2531 if (server)
2532 msm_ipc_router_destroy_server(server,
2533 port_ptr->this_port.node_id,
2534 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002535 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002536 }
2537
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002538 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539 kfree(port_ptr);
2540 return 0;
2541}
2542
2543int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2544{
2545 struct rr_packet *pkt;
2546 int rc = 0;
2547
2548 if (!port_ptr)
2549 return -EINVAL;
2550
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002551 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002552 if (!list_empty(&port_ptr->port_rx_q)) {
2553 pkt = list_first_entry(&port_ptr->port_rx_q,
2554 struct rr_packet, list);
2555 rc = pkt->length;
2556 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002557 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558
2559 return rc;
2560}
2561
2562int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2563{
2564 if (!port_ptr)
2565 return -EINVAL;
2566
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002567 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002568 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002569 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002570 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002571 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002572 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002573 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574
2575 return 0;
2576}
2577
2578int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002579 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002580 int num_entries_in_array,
2581 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002582{
2583 struct msm_ipc_server *server;
2584 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002585 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002586
2587 if (!srv_name) {
2588 pr_err("%s: Invalid srv_name\n", __func__);
2589 return -EINVAL;
2590 }
2591
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002592 if (num_entries_in_array && !srv_info) {
2593 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002594 return -EINVAL;
2595 }
2596
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002597 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002598 if (!lookup_mask)
2599 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002600 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2601 list_for_each_entry(server, &server_list[key], list) {
2602 if ((server->name.service != srv_name->service) ||
2603 ((server->name.instance & lookup_mask) !=
2604 srv_name->instance))
2605 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002606
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002607 list_for_each_entry(server_port,
2608 &server->server_port_list, list) {
2609 if (i < num_entries_in_array) {
2610 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002611 server_port->server_addr.node_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002612 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002613 server_port->server_addr.port_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002614 srv_info[i].service = server->name.service;
2615 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002616 }
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002617 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002618 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002619 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002620 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621
2622 return i;
2623}
2624
2625int msm_ipc_router_close(void)
2626{
2627 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2628
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002629 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2631 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002632 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 list_del(&xprt_info->list);
2634 kfree(xprt_info);
2635 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002636 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002637 return 0;
2638}
2639
2640#if defined(CONFIG_DEBUG_FS)
2641static int dump_routing_table(char *buf, int max)
2642{
2643 int i = 0, j;
2644 struct msm_ipc_routing_table_entry *rt_entry;
2645
2646 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002647 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002648 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002649 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002650 i += scnprintf(buf + i, max - i,
2651 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianc1a4e3a2012-09-10 16:10:24 -06002652 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002653 i += scnprintf(buf + i, max - i,
2654 "XPRT Name: Loopback\n");
2655 i += scnprintf(buf + i, max - i,
2656 "Next Hop: %d\n", rt_entry->node_id);
2657 } else {
2658 i += scnprintf(buf + i, max - i,
2659 "XPRT Name: %s\n",
2660 rt_entry->xprt_info->xprt->name);
2661 i += scnprintf(buf + i, max - i,
2662 "Next Hop: 0x%08x\n",
2663 rt_entry->xprt_info->remote_node_id);
2664 }
2665 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002666 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002667 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002668 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002669 }
2670
2671 return i;
2672}
2673
2674static int dump_xprt_info(char *buf, int max)
2675{
2676 int i = 0;
2677 struct msm_ipc_router_xprt_info *xprt_info;
2678
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002679 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2681 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2682 xprt_info->xprt->name);
2683 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2684 xprt_info->xprt->link_id);
2685 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2686 (xprt_info->initialized ? "Y" : "N"));
2687 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2688 xprt_info->remote_node_id);
2689 i += scnprintf(buf + i, max - i, "\n");
2690 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002691 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002692
2693 return i;
2694}
2695
2696static int dump_servers(char *buf, int max)
2697{
2698 int i = 0, j;
2699 struct msm_ipc_server *server;
2700 struct msm_ipc_server_port *server_port;
2701
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002702 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002703 for (j = 0; j < SRV_HASH_SIZE; j++) {
2704 list_for_each_entry(server, &server_list[j], list) {
2705 list_for_each_entry(server_port,
2706 &server->server_port_list,
2707 list) {
2708 i += scnprintf(buf + i, max - i, "Service: "
2709 "0x%08x\n", server->name.service);
2710 i += scnprintf(buf + i, max - i, "Instance: "
2711 "0x%08x\n", server->name.instance);
2712 i += scnprintf(buf + i, max - i,
2713 "Node_id: 0x%08x\n",
2714 server_port->server_addr.node_id);
2715 i += scnprintf(buf + i, max - i,
2716 "Port_id: 0x%08x\n",
2717 server_port->server_addr.port_id);
2718 i += scnprintf(buf + i, max - i, "\n");
2719 }
2720 }
2721 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002722 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002723
2724 return i;
2725}
2726
2727static int dump_remote_ports(char *buf, int max)
2728{
2729 int i = 0, j, k;
2730 struct msm_ipc_router_remote_port *rport_ptr;
2731 struct msm_ipc_routing_table_entry *rt_entry;
2732
2733 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002734 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002735 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002736 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002737 for (k = 0; k < RP_HASH_SIZE; k++) {
2738 list_for_each_entry(rport_ptr,
2739 &rt_entry->remote_port_list[k],
2740 list) {
2741 i += scnprintf(buf + i, max - i,
2742 "Node_id: 0x%08x\n",
2743 rport_ptr->node_id);
2744 i += scnprintf(buf + i, max - i,
2745 "Port_id: 0x%08x\n",
2746 rport_ptr->port_id);
2747 i += scnprintf(buf + i, max - i,
2748 "Quota_cnt: %d\n",
2749 rport_ptr->tx_quota_cnt);
2750 i += scnprintf(buf + i, max - i, "\n");
2751 }
2752 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002753 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002754 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002755 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002756 }
2757
2758 return i;
2759}
2760
2761static int dump_control_ports(char *buf, int max)
2762{
2763 int i = 0;
2764 struct msm_ipc_port *port_ptr;
2765
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002766 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767 list_for_each_entry(port_ptr, &control_ports, list) {
2768 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2769 port_ptr->this_port.node_id);
2770 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2771 port_ptr->this_port.port_id);
2772 i += scnprintf(buf + i, max - i, "\n");
2773 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002774 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002775
2776 return i;
2777}
2778
2779static int dump_local_ports(char *buf, int max)
2780{
2781 int i = 0, j;
2782 unsigned long flags;
2783 struct msm_ipc_port *port_ptr;
2784
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002785 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002786 for (j = 0; j < LP_HASH_SIZE; j++) {
2787 list_for_each_entry(port_ptr, &local_ports[j], list) {
2788 spin_lock_irqsave(&port_ptr->port_lock, flags);
2789 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2790 port_ptr->this_port.node_id);
2791 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2792 port_ptr->this_port.port_id);
2793 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2794 port_ptr->num_tx);
2795 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2796 port_ptr->num_rx);
2797 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2798 port_ptr->num_tx_bytes);
2799 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2800 port_ptr->num_rx_bytes);
2801 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2802 i += scnprintf(buf + i, max - i, "\n");
2803 }
2804 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002805 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002806
2807 return i;
2808}
2809
2810#define DEBUG_BUFMAX 4096
2811static char debug_buffer[DEBUG_BUFMAX];
2812
2813static ssize_t debug_read(struct file *file, char __user *buf,
2814 size_t count, loff_t *ppos)
2815{
2816 int (*fill)(char *buf, int max) = file->private_data;
2817 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2818 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2819}
2820
2821static int debug_open(struct inode *inode, struct file *file)
2822{
2823 file->private_data = inode->i_private;
2824 return 0;
2825}
2826
2827static const struct file_operations debug_ops = {
2828 .read = debug_read,
2829 .open = debug_open,
2830};
2831
2832static void debug_create(const char *name, mode_t mode,
2833 struct dentry *dent,
2834 int (*fill)(char *buf, int max))
2835{
2836 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2837}
2838
2839static void debugfs_init(void)
2840{
2841 struct dentry *dent;
2842
2843 dent = debugfs_create_dir("msm_ipc_router", 0);
2844 if (IS_ERR(dent))
2845 return;
2846
2847 debug_create("dump_local_ports", 0444, dent,
2848 dump_local_ports);
2849 debug_create("dump_remote_ports", 0444, dent,
2850 dump_remote_ports);
2851 debug_create("dump_control_ports", 0444, dent,
2852 dump_control_ports);
2853 debug_create("dump_servers", 0444, dent,
2854 dump_servers);
2855 debug_create("dump_xprt_info", 0444, dent,
2856 dump_xprt_info);
2857 debug_create("dump_routing_table", 0444, dent,
2858 dump_routing_table);
2859}
2860
2861#else
2862static void debugfs_init(void) {}
2863#endif
2864
2865static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2866{
2867 struct msm_ipc_router_xprt_info *xprt_info;
2868 struct msm_ipc_routing_table_entry *rt_entry;
2869
2870 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2871 GFP_KERNEL);
2872 if (!xprt_info)
2873 return -ENOMEM;
2874
2875 xprt_info->xprt = xprt;
2876 xprt_info->initialized = 0;
2877 xprt_info->remote_node_id = -1;
2878 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002879 mutex_init(&xprt_info->rx_lock_lhb2);
2880 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002881 wake_lock_init(&xprt_info->wakelock,
2882 WAKE_LOCK_SUSPEND, xprt->name);
2883 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002884 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002885 INIT_WORK(&xprt_info->read_data, do_read_data);
2886 INIT_LIST_HEAD(&xprt_info->list);
2887
2888 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2889 if (!xprt_info->workqueue) {
2890 kfree(xprt_info);
2891 return -ENOMEM;
2892 }
2893
2894 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2895 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2896 xprt_info->initialized = 1;
2897 }
2898
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002899 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002900 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002901 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002902
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002903 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002904 if (!routing_table_inited) {
2905 init_routing_table();
2906 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2907 add_routing_table_entry(rt_entry);
2908 routing_table_inited = 1;
2909 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002910 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002911
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912 xprt->priv = xprt_info;
2913
2914 return 0;
2915}
2916
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002917static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2918{
2919 struct msm_ipc_router_xprt_info *xprt_info;
2920
2921 if (xprt && xprt->priv) {
2922 xprt_info = xprt->priv;
2923
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002924 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002925 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002926 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002927
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002928 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002929 list_del(&xprt_info->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002930 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002931
2932 flush_workqueue(xprt_info->workqueue);
2933 destroy_workqueue(xprt_info->workqueue);
2934 wake_lock_destroy(&xprt_info->wakelock);
2935
2936 xprt->priv = 0;
2937 kfree(xprt_info);
2938 }
2939}
2940
2941
2942struct msm_ipc_router_xprt_work {
2943 struct msm_ipc_router_xprt *xprt;
2944 struct work_struct work;
2945};
2946
2947static void xprt_open_worker(struct work_struct *work)
2948{
2949 struct msm_ipc_router_xprt_work *xprt_work =
2950 container_of(work, struct msm_ipc_router_xprt_work, work);
2951
2952 msm_ipc_router_add_xprt(xprt_work->xprt);
2953 kfree(xprt_work);
2954}
2955
2956static void xprt_close_worker(struct work_struct *work)
2957{
2958 struct msm_ipc_router_xprt_work *xprt_work =
2959 container_of(work, struct msm_ipc_router_xprt_work, work);
2960
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002961 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002962 msm_ipc_router_remove_xprt(xprt_work->xprt);
Zaheerulla Meer35893a62013-06-19 16:54:44 +05302963 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002964 kfree(xprt_work);
2965}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002966
2967void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2968 unsigned event,
2969 void *data)
2970{
2971 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002972 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002973 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002974 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002975
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002976 if (!msm_ipc_router_workqueue) {
2977 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2978 IPC_ROUTER_INIT_TIMEOUT);
2979 if (!ret || !msm_ipc_router_workqueue) {
2980 pr_err("%s: IPC Router not initialized\n", __func__);
2981 return;
2982 }
2983 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002984
2985 switch (event) {
2986 case IPC_ROUTER_XPRT_EVENT_OPEN:
2987 D("open event for '%s'\n", xprt->name);
2988 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2989 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06002990 if (xprt_work) {
2991 xprt_work->xprt = xprt;
2992 INIT_WORK(&xprt_work->work, xprt_open_worker);
2993 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2994 } else {
2995 pr_err("%s: malloc failure - Couldn't notify OPEN event",
2996 __func__);
2997 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002998 break;
2999
3000 case IPC_ROUTER_XPRT_EVENT_CLOSE:
3001 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003002 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
3003 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06003004 if (xprt_work) {
3005 xprt_work->xprt = xprt;
3006 INIT_WORK(&xprt_work->work, xprt_close_worker);
3007 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
3008 } else {
3009 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
3010 __func__);
3011 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003012 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003013 }
3014
3015 if (!data)
3016 return;
3017
3018 while (!xprt_info) {
3019 msleep(100);
3020 xprt_info = xprt->priv;
3021 }
3022
3023 pkt = clone_pkt((struct rr_packet *)data);
3024 if (!pkt)
3025 return;
3026
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003027 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003028 list_add_tail(&pkt->list, &xprt_info->pkt_list);
3029 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003030 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06003031 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003032}
3033
3034static int __init msm_ipc_router_init(void)
3035{
3036 int i, ret;
3037 struct msm_ipc_routing_table_entry *rt_entry;
3038
3039 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06003040 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
3041 "ipc_router");
3042 if (!ipc_rtr_log_ctxt)
3043 pr_err("%s: Unable to create IPC logging for IPC RTR",
3044 __func__);
3045
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003046 msm_ipc_router_workqueue =
3047 create_singlethread_workqueue("msm_ipc_router");
3048 if (!msm_ipc_router_workqueue)
3049 return -ENOMEM;
3050
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003051 debugfs_init();
3052
3053 for (i = 0; i < SRV_HASH_SIZE; i++)
3054 INIT_LIST_HEAD(&server_list[i]);
3055
3056 for (i = 0; i < LP_HASH_SIZE; i++)
3057 INIT_LIST_HEAD(&local_ports[i]);
3058
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003059 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003060 if (!routing_table_inited) {
3061 init_routing_table();
3062 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
3063 add_routing_table_entry(rt_entry);
3064 routing_table_inited = 1;
3065 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003066 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003068 ret = msm_ipc_router_init_sockets();
3069 if (ret < 0)
3070 pr_err("%s: Init sockets failed\n", __func__);
3071
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06003072 ret = msm_ipc_router_security_init();
3073 if (ret < 0)
3074 pr_err("%s: Security Init failed\n", __func__);
3075
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003076 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003077 return ret;
3078}
3079
3080module_init(msm_ipc_router_init);
3081MODULE_DESCRIPTION("MSM IPC Router");
3082MODULE_LICENSE("GPL v2");