blob: 7ff54923355e4a36e026ca47e4a40e5a4fc81339 [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 Meer6a309de2013-07-12 16:16:30 +05301848 release_pkt(pkt);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301849 return;
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001850 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001851 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001852 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06001853 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001854 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001855 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001856 return;
1857
1858fail_data:
1859 release_pkt(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 pr_err("ipc_router has died\n");
1861}
1862
1863int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr,
1864 struct msm_ipc_addr *name)
1865{
1866 struct msm_ipc_server *server;
1867 unsigned long flags;
1868 union rr_control_msg ctl;
1869
1870 if (!port_ptr || !name)
1871 return -EINVAL;
1872
1873 if (name->addrtype != MSM_IPC_ADDR_NAME)
1874 return -EINVAL;
1875
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001876 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877 server = msm_ipc_router_lookup_server(name->addr.port_name.service,
1878 name->addr.port_name.instance,
1879 IPC_ROUTER_NID_LOCAL,
1880 port_ptr->this_port.port_id);
1881 if (server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001882 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 pr_err("%s: Server already present\n", __func__);
1884 return -EINVAL;
1885 }
1886
1887 server = msm_ipc_router_create_server(name->addr.port_name.service,
1888 name->addr.port_name.instance,
1889 IPC_ROUTER_NID_LOCAL,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001890 port_ptr->this_port.port_id,
1891 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001893 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 pr_err("%s: Server Creation failed\n", __func__);
1895 return -EINVAL;
1896 }
1897
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001898 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001899 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1900 ctl.srv.service = server->name.service;
1901 ctl.srv.instance = server->name.instance;
1902 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1903 ctl.srv.port_id = port_ptr->this_port.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001904 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 broadcast_ctl_msg(&ctl);
Karthikeyan Ramasubramanianfa807952013-08-06 18:04:12 -06001906 broadcast_ctl_msg_locally(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001907 spin_lock_irqsave(&port_ptr->port_lock, flags);
1908 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001909 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 port_ptr->port_name.service = server->name.service;
1911 port_ptr->port_name.instance = server->name.instance;
1912 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1913 return 0;
1914}
1915
1916int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1917{
1918 struct msm_ipc_server *server;
1919 unsigned long flags;
1920 union rr_control_msg ctl;
1921
1922 if (!port_ptr)
1923 return -EINVAL;
1924
1925 if (port_ptr->type != SERVER_PORT) {
1926 pr_err("%s: Trying to unregister a non-server port\n",
1927 __func__);
1928 return -EINVAL;
1929 }
1930
1931 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1932 pr_err("%s: Trying to unregister a remote server locally\n",
1933 __func__);
1934 return -EINVAL;
1935 }
1936
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001937 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001938 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1939 port_ptr->port_name.instance,
1940 port_ptr->this_port.node_id,
1941 port_ptr->this_port.port_id);
1942 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001943 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 pr_err("%s: Server lookup failed\n", __func__);
1945 return -ENODEV;
1946 }
1947
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001948 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1950 ctl.srv.service = server->name.service;
1951 ctl.srv.instance = server->name.instance;
1952 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1953 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1955 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001956 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06001957 broadcast_ctl_msg(&ctl);
Karthikeyan Ramasubramanianfa807952013-08-06 18:04:12 -06001958 broadcast_ctl_msg_locally(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 spin_lock_irqsave(&port_ptr->port_lock, flags);
1960 port_ptr->type = CLIENT_PORT;
1961 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1962 return 0;
1963}
1964
1965static int loopback_data(struct msm_ipc_port *src,
1966 uint32_t port_id,
1967 struct sk_buff_head *data)
1968{
1969 struct sk_buff *head_skb;
1970 struct rr_header *hdr;
1971 struct msm_ipc_port *port_ptr;
1972 struct rr_packet *pkt;
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07001973 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974
1975 if (!data) {
1976 pr_err("%s: Invalid pkt pointer\n", __func__);
1977 return -EINVAL;
1978 }
1979
1980 pkt = create_pkt(data);
1981 if (!pkt) {
1982 pr_err("%s: New pkt create failed\n", __func__);
1983 return -ENOMEM;
1984 }
1985
1986 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001987 if (!head_skb) {
1988 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik0e83d3b2013-05-01 16:25:00 -06001989 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001990 return -EINVAL;
1991 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001992 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
1993 if (!hdr) {
1994 pr_err("%s: Prepend Header failed\n", __func__);
1995 release_pkt(pkt);
1996 return -ENOMEM;
1997 }
1998 hdr->version = IPC_ROUTER_VERSION;
1999 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2000 hdr->src_node_id = src->this_port.node_id;
2001 hdr->src_port_id = src->this_port.port_id;
2002 hdr->size = pkt->length;
2003 hdr->confirm_rx = 0;
2004 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2005 hdr->dst_port_id = port_id;
2006 pkt->length += IPC_ROUTER_HDR_SIZE;
2007
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002008 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2010 if (!port_ptr) {
2011 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002012 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002013 release_pkt(pkt);
2014 return -ENODEV;
2015 }
2016
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002017 ret_len = pkt->length;
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06002018 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002019 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002020 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002022 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023}
2024
2025static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2026 struct msm_ipc_router_remote_port *rport_ptr,
2027 struct rr_packet *pkt)
2028{
2029 struct sk_buff *head_skb;
2030 struct rr_header *hdr;
2031 struct msm_ipc_router_xprt_info *xprt_info;
2032 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302033 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035
2036 if (!rport_ptr || !src || !pkt)
2037 return -EINVAL;
2038
2039 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002040 if (!head_skb) {
2041 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2042 return -EINVAL;
2043 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2045 if (!hdr) {
2046 pr_err("%s: Prepend Header failed\n", __func__);
2047 return -ENOMEM;
2048 }
2049 hdr->version = IPC_ROUTER_VERSION;
2050 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2051 hdr->src_node_id = src->this_port.node_id;
2052 hdr->src_port_id = src->this_port.port_id;
2053 hdr->size = pkt->length;
2054 hdr->confirm_rx = 0;
2055 hdr->dst_node_id = rport_ptr->node_id;
2056 hdr->dst_port_id = rport_ptr->port_id;
2057 pkt->length += IPC_ROUTER_HDR_SIZE;
2058
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002059 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302060 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2061 if (msm_ipc_router_lookup_resume_tx_port(
2062 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002063 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302064 return -EAGAIN;
2065 }
2066 resume_tx_port =
2067 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2068 GFP_KERNEL);
2069 if (!resume_tx_port) {
2070 pr_err("%s: Resume_Tx port allocation failed\n",
2071 __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002072 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302073 return -ENOMEM;
2074 }
2075 INIT_LIST_HEAD(&resume_tx_port->list);
2076 resume_tx_port->port_id = src->this_port.port_id;
2077 resume_tx_port->node_id = src->this_port.node_id;
2078 list_add_tail(&resume_tx_port->list,
2079 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002080 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302081 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082 }
2083 rport_ptr->tx_quota_cnt++;
2084 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2085 hdr->confirm_rx = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002086 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 rt_entry = lookup_routing_table(hdr->dst_node_id);
2089 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090 pr_err("%s: Remote node %d not up\n",
2091 __func__, hdr->dst_node_id);
2092 return -ENODEV;
2093 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002094 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002095 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002096 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002097 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002098 mutex_unlock(&xprt_info->tx_lock_lhb2);
2099 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100
2101 if (ret < 0) {
2102 pr_err("%s: Write on XPRT failed\n", __func__);
2103 return ret;
2104 }
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002105 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106
2107 RAW_HDR("[w rr_h] "
2108 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2109 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2110 hdr->version, type_to_str(hdr->type),
2111 hdr->src_node_id, hdr->src_port_id,
2112 hdr->confirm_rx, hdr->size,
2113 hdr->dst_node_id, hdr->dst_port_id);
2114
2115#if defined(CONFIG_MSM_SMD_LOGGING)
2116#if defined(DEBUG)
2117 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2118 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05302119 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002120 IPC_ROUTER_LOG_EVENT_TX),
2121 (hdr->src_node_id << 24) |
2122 (hdr->src_port_id & 0xffffff),
2123 (hdr->dst_node_id << 24) |
2124 (hdr->dst_port_id & 0xffffff),
2125 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2126 (hdr->size & 0xffff));
2127 }
2128#endif
2129#endif
2130
2131 return pkt->length;
2132}
2133
2134int msm_ipc_router_send_to(struct msm_ipc_port *src,
2135 struct sk_buff_head *data,
2136 struct msm_ipc_addr *dest)
2137{
2138 uint32_t dst_node_id = 0, dst_port_id = 0;
2139 struct msm_ipc_server *server;
2140 struct msm_ipc_server_port *server_port;
2141 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2142 struct rr_packet *pkt;
2143 int ret;
2144
2145 if (!src || !data || !dest) {
2146 pr_err("%s: Invalid Parameters\n", __func__);
2147 return -EINVAL;
2148 }
2149
2150 /* Resolve Address*/
2151 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2152 dst_node_id = dest->addr.port_addr.node_id;
2153 dst_port_id = dest->addr.port_addr.port_id;
2154 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002155 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002156 server = msm_ipc_router_lookup_server(
2157 dest->addr.port_name.service,
2158 dest->addr.port_name.instance,
2159 0, 0);
2160 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002161 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002162 pr_err("%s: Destination not reachable\n", __func__);
2163 return -ENODEV;
2164 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 server_port = list_first_entry(&server->server_port_list,
2166 struct msm_ipc_server_port,
2167 list);
2168 dst_node_id = server_port->server_addr.node_id;
2169 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002170 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171 }
2172 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2173 ret = loopback_data(src, dst_port_id, data);
2174 return ret;
2175 }
2176
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002177 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2179 dst_port_id);
2180 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002181 up_read(&routing_table_lock_lha3);
Zaheerulla Meer2c515312013-05-10 15:51:28 +05302182 pr_err("%s: Remote port not found\n", __func__);
2183 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184 }
2185
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002186 if (src->check_send_permissions) {
2187 ret = src->check_send_permissions(rport_ptr->sec_rule);
2188 if (ret <= 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002189 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002190 pr_err("%s: permission failure for %s\n",
2191 __func__, current->comm);
2192 return -EPERM;
2193 }
2194 }
2195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002196 pkt = create_pkt(data);
2197 if (!pkt) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002198 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199 pr_err("%s: Pkt creation failed\n", __func__);
2200 return -ENOMEM;
2201 }
2202
2203 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002204 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 release_pkt(pkt);
2206
2207 return ret;
2208}
2209
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002210int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2211 struct msm_ipc_addr *dest,
2212 void *data, unsigned int data_len)
2213{
2214 struct sk_buff_head *out_skb_head;
2215 int ret;
2216
2217 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2218 if (!out_skb_head) {
2219 pr_err("%s: SKB conversion failed\n", __func__);
2220 return -EFAULT;
2221 }
2222
2223 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meerc8400402013-05-08 19:27:27 +05302224 if (ret == -EAGAIN)
2225 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002226 if (ret < 0) {
2227 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2228 __func__, ret);
2229 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meerfc59a8a2013-06-26 22:39:00 +05302230 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002231 }
2232 return 0;
2233}
2234
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302235/**
2236 * msm_ipc_router_send_resume_tx() - Send Resume_Tx message
2237 * @data: Pointer to received data packet that has confirm_rx bit set
2238 *
2239 * @return: On success, number of bytes transferred is returned, else
2240 * standard linux error code is returned.
2241 *
2242 * This function sends the Resume_Tx event to the remote node that
2243 * sent the data with confirm_rx field set. In case of a multi-hop
2244 * scenario also, this function makes sure that the destination node_id
2245 * to which the resume_tx event should reach is right.
2246 */
2247static int msm_ipc_router_send_resume_tx(void *data)
2248{
2249 union rr_control_msg msg;
2250 struct rr_header *hdr = (struct rr_header *)data;
2251 struct msm_ipc_routing_table_entry *rt_entry;
2252 int ret;
2253
2254 memset(&msg, 0, sizeof(msg));
2255 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
2256 msg.cli.node_id = hdr->dst_node_id;
2257 msg.cli.port_id = hdr->dst_port_id;
2258 down_read(&routing_table_lock_lha3);
2259 rt_entry = lookup_routing_table(hdr->src_node_id);
2260 if (!rt_entry) {
2261 pr_err("%s: %d Node is not present",
2262 __func__, hdr->src_node_id);
2263 up_read(&routing_table_lock_lha3);
2264 return -ENODEV;
2265 }
2266 RR("x RESUME_TX id=%d:%08x\n",
2267 msg.cli.node_id, msg.cli.port_id);
2268 ret = msm_ipc_router_send_control_msg(rt_entry->xprt_info, &msg,
2269 hdr->src_node_id);
2270 up_read(&routing_table_lock_lha3);
2271 if (ret < 0)
2272 pr_err("%s: Send Resume_Tx Failed SRC_NODE: %d SRC_PORT: %d DEST_NODE: %d",
2273 __func__, hdr->dst_node_id, hdr->dst_port_id,
2274 hdr->src_node_id);
2275
2276 return ret;
2277}
2278
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2280 struct sk_buff_head **data,
2281 size_t buf_len)
2282{
2283 struct rr_packet *pkt;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302284 struct sk_buff *head_skb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002285 int ret;
2286
2287 if (!port_ptr || !data)
2288 return -EINVAL;
2289
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002290 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002292 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002293 return -EAGAIN;
2294 }
2295
2296 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2297 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002298 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299 return -ETOOSMALL;
2300 }
2301 list_del(&pkt->list);
2302 if (list_empty(&port_ptr->port_rx_q))
2303 wake_unlock(&port_ptr->port_rx_wake_lock);
2304 *data = pkt->pkt_fragment_q;
2305 ret = pkt->length;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002306 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302307 kfree(pkt);
2308 head_skb = skb_peek(*data);
2309 if (!head_skb) {
2310 pr_err("%s: Socket Buffer not found", __func__);
2311 return -EFAULT;
2312 }
2313 if (((struct rr_header *)(head_skb->data))->confirm_rx)
2314 msm_ipc_router_send_resume_tx((void *)(head_skb->data));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002315
2316 return ret;
2317}
2318
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302319/**
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302320 * msm_ipc_router_rx_data_wait() - Wait for new message destined to a local port.
2321 * @port_ptr: Pointer to the local port
2322 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2323 * > 0 timeout indicates the wait time.
2324 * 0 indicates that we do not wait.
2325 * @return: 0 if there are pending messages to read,
2326 * standard Linux error code otherwise.
2327 *
2328 * Checks for the availability of messages that are destined to a local port.
2329 * If no messages are present then waits as per @timeout.
2330 */
2331int msm_ipc_router_rx_data_wait(struct msm_ipc_port *port_ptr, long timeout)
2332{
2333 int ret = 0;
2334
2335 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2336 while (list_empty(&port_ptr->port_rx_q)) {
2337 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2338 if (timeout < 0) {
2339 ret = wait_event_interruptible(
2340 port_ptr->port_rx_wait_q,
2341 !list_empty(&port_ptr->port_rx_q));
2342 if (ret)
2343 return ret;
2344 } else if (timeout > 0) {
2345 timeout = wait_event_interruptible_timeout(
2346 port_ptr->port_rx_wait_q,
2347 !list_empty(&port_ptr->port_rx_q),
2348 timeout);
2349 if (timeout < 0)
2350 return -EFAULT;
2351 }
2352 if (timeout == 0)
2353 return -ENOMSG;
2354 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2355 }
2356 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2357
2358 return ret;
2359}
2360
2361/**
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302362 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2363 * @port_ptr: Pointer to the local port
2364 * @data : Pointer to the socket buffer head
2365 * @src: Pointer to local port address
2366 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2367 * > 0 timeout indicates the wait time.
2368 * 0 indicates that we do not wait.
2369 * @return: = Number of bytes read(On successful read operation).
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302370 * = -ENOMSG (If there are no pending messages and timeout is 0).
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302371 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2372 * = -EFAULT (If there are no pending messages when timeout is > 0
2373 * and the wait_event_interruptible_timeout has returned value > 0)
2374 * = -ERESTARTSYS (If there are no pending messages when timeout
2375 * is < 0 and wait_event_interruptible was interrupted by a signal)
2376 *
2377 * This function reads the messages that are destined for a local port. It
2378 * is used by modules that exist with-in the kernel and use IPC Router for
2379 * transport. The function checks if there are any messages that are already
2380 * received. If yes, it reads them, else it waits as per the timeout value.
2381 * On a successful read, the return value of the function indicates the number
2382 * of bytes that are read.
2383 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2385 struct sk_buff_head **data,
2386 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002387 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002388{
2389 int ret, data_len, align_size;
2390 struct sk_buff *temp_skb;
2391 struct rr_header *hdr = NULL;
2392
2393 if (!port_ptr || !data) {
2394 pr_err("%s: Invalid pointers being passed\n", __func__);
2395 return -EINVAL;
2396 }
2397
2398 *data = NULL;
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302399
2400 ret = msm_ipc_router_rx_data_wait(port_ptr, timeout);
2401 if (ret)
2402 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002403
2404 ret = msm_ipc_router_read(port_ptr, data, 0);
2405 if (ret <= 0 || !(*data))
2406 return ret;
2407
2408 temp_skb = skb_peek(*data);
2409 hdr = (struct rr_header *)(temp_skb->data);
2410 if (src) {
2411 src->addrtype = MSM_IPC_ADDR_ID;
2412 src->addr.port_addr.node_id = hdr->src_node_id;
2413 src->addr.port_addr.port_id = hdr->src_port_id;
2414 }
2415
2416 data_len = hdr->size;
2417 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2418 align_size = ALIGN_SIZE(data_len);
2419 if (align_size) {
2420 temp_skb = skb_peek_tail(*data);
2421 skb_trim(temp_skb, (temp_skb->len - align_size));
2422 }
2423 return data_len;
2424}
2425
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002426int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2427 struct msm_ipc_addr *src,
2428 unsigned char **data,
2429 unsigned int *len)
2430{
2431 struct sk_buff_head *in_skb_head;
2432 int ret;
2433
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302434 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2435
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002436 if (ret < 0) {
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302437 if (ret != -ENOMSG)
2438 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2439 __func__, ret);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002440 return ret;
2441 }
2442
2443 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2444 if (!(*data))
2445 pr_err("%s: Buf conversion failed\n", __func__);
2446
2447 *len = ret;
2448 msm_ipc_router_free_skb(in_skb_head);
2449 return 0;
2450}
2451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002453 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002454 void *priv)
2455{
2456 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002457 int ret;
2458
2459 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2460 if (ret < 0) {
2461 pr_err("%s: Error waiting for local router\n", __func__);
2462 return NULL;
2463 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464
2465 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2466 if (!port_ptr)
2467 pr_err("%s: port_ptr alloc failed\n", __func__);
2468
2469 return port_ptr;
2470}
2471
2472int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2473{
2474 union rr_control_msg msg;
2475 struct rr_packet *pkt, *temp_pkt;
2476 struct msm_ipc_server *server;
2477
2478 if (!port_ptr)
2479 return -EINVAL;
2480
2481 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002482 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002483 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002484 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002485
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06002487 memset(&msg, 0, sizeof(msg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2489 msg.srv.service = port_ptr->port_name.service;
2490 msg.srv.instance = port_ptr->port_name.instance;
2491 msg.srv.node_id = port_ptr->this_port.node_id;
2492 msg.srv.port_id = port_ptr->this_port.port_id;
2493 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2494 msg.srv.service, msg.srv.instance,
2495 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002496 broadcast_ctl_msg(&msg);
2497 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498 }
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002499
2500 /*
2501 * Server port could have been a client port earlier.
2502 * Send REMOVE_CLIENT message in either case.
2503 */
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002504 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002505 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2506 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2507 port_ptr->this_port.node_id,
2508 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002509 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002510 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002511 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002512 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002513 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002514 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002515 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002516 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002517 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002518 }
2519
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002520 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2522 list_del(&pkt->list);
2523 release_pkt(pkt);
2524 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002525 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002528 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 server = msm_ipc_router_lookup_server(
2530 port_ptr->port_name.service,
2531 port_ptr->port_name.instance,
2532 port_ptr->this_port.node_id,
2533 port_ptr->this_port.port_id);
2534 if (server)
2535 msm_ipc_router_destroy_server(server,
2536 port_ptr->this_port.node_id,
2537 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002538 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539 }
2540
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002541 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002542 kfree(port_ptr);
2543 return 0;
2544}
2545
2546int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2547{
2548 struct rr_packet *pkt;
2549 int rc = 0;
2550
2551 if (!port_ptr)
2552 return -EINVAL;
2553
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002554 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002555 if (!list_empty(&port_ptr->port_rx_q)) {
2556 pkt = list_first_entry(&port_ptr->port_rx_q,
2557 struct rr_packet, list);
2558 rc = pkt->length;
2559 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002560 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002561
2562 return rc;
2563}
2564
2565int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2566{
2567 if (!port_ptr)
2568 return -EINVAL;
2569
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002570 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002571 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002572 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002574 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002575 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002576 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577
2578 return 0;
2579}
2580
2581int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002582 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002583 int num_entries_in_array,
2584 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585{
2586 struct msm_ipc_server *server;
2587 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002588 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002589
2590 if (!srv_name) {
2591 pr_err("%s: Invalid srv_name\n", __func__);
2592 return -EINVAL;
2593 }
2594
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002595 if (num_entries_in_array && !srv_info) {
2596 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002597 return -EINVAL;
2598 }
2599
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002600 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002601 if (!lookup_mask)
2602 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002603 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2604 list_for_each_entry(server, &server_list[key], list) {
2605 if ((server->name.service != srv_name->service) ||
2606 ((server->name.instance & lookup_mask) !=
2607 srv_name->instance))
2608 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002609
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002610 list_for_each_entry(server_port,
2611 &server->server_port_list, list) {
2612 if (i < num_entries_in_array) {
2613 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002614 server_port->server_addr.node_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002615 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002616 server_port->server_addr.port_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002617 srv_info[i].service = server->name.service;
2618 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002619 }
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002620 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002622 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002623 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002624
2625 return i;
2626}
2627
2628int msm_ipc_router_close(void)
2629{
2630 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2631
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002632 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2634 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002635 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 list_del(&xprt_info->list);
2637 kfree(xprt_info);
2638 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002639 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002640 return 0;
2641}
2642
2643#if defined(CONFIG_DEBUG_FS)
2644static int dump_routing_table(char *buf, int max)
2645{
2646 int i = 0, j;
2647 struct msm_ipc_routing_table_entry *rt_entry;
2648
2649 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002650 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002651 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002652 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002653 i += scnprintf(buf + i, max - i,
2654 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianc1a4e3a2012-09-10 16:10:24 -06002655 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 i += scnprintf(buf + i, max - i,
2657 "XPRT Name: Loopback\n");
2658 i += scnprintf(buf + i, max - i,
2659 "Next Hop: %d\n", rt_entry->node_id);
2660 } else {
2661 i += scnprintf(buf + i, max - i,
2662 "XPRT Name: %s\n",
2663 rt_entry->xprt_info->xprt->name);
2664 i += scnprintf(buf + i, max - i,
2665 "Next Hop: 0x%08x\n",
2666 rt_entry->xprt_info->remote_node_id);
2667 }
2668 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002669 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002671 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002672 }
2673
2674 return i;
2675}
2676
2677static int dump_xprt_info(char *buf, int max)
2678{
2679 int i = 0;
2680 struct msm_ipc_router_xprt_info *xprt_info;
2681
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002682 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002683 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2684 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2685 xprt_info->xprt->name);
2686 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2687 xprt_info->xprt->link_id);
2688 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2689 (xprt_info->initialized ? "Y" : "N"));
2690 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2691 xprt_info->remote_node_id);
2692 i += scnprintf(buf + i, max - i, "\n");
2693 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002694 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002695
2696 return i;
2697}
2698
2699static int dump_servers(char *buf, int max)
2700{
2701 int i = 0, j;
2702 struct msm_ipc_server *server;
2703 struct msm_ipc_server_port *server_port;
2704
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002705 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002706 for (j = 0; j < SRV_HASH_SIZE; j++) {
2707 list_for_each_entry(server, &server_list[j], list) {
2708 list_for_each_entry(server_port,
2709 &server->server_port_list,
2710 list) {
2711 i += scnprintf(buf + i, max - i, "Service: "
2712 "0x%08x\n", server->name.service);
2713 i += scnprintf(buf + i, max - i, "Instance: "
2714 "0x%08x\n", server->name.instance);
2715 i += scnprintf(buf + i, max - i,
2716 "Node_id: 0x%08x\n",
2717 server_port->server_addr.node_id);
2718 i += scnprintf(buf + i, max - i,
2719 "Port_id: 0x%08x\n",
2720 server_port->server_addr.port_id);
2721 i += scnprintf(buf + i, max - i, "\n");
2722 }
2723 }
2724 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002725 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002726
2727 return i;
2728}
2729
2730static int dump_remote_ports(char *buf, int max)
2731{
2732 int i = 0, j, k;
2733 struct msm_ipc_router_remote_port *rport_ptr;
2734 struct msm_ipc_routing_table_entry *rt_entry;
2735
2736 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002737 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002738 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002739 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002740 for (k = 0; k < RP_HASH_SIZE; k++) {
2741 list_for_each_entry(rport_ptr,
2742 &rt_entry->remote_port_list[k],
2743 list) {
2744 i += scnprintf(buf + i, max - i,
2745 "Node_id: 0x%08x\n",
2746 rport_ptr->node_id);
2747 i += scnprintf(buf + i, max - i,
2748 "Port_id: 0x%08x\n",
2749 rport_ptr->port_id);
2750 i += scnprintf(buf + i, max - i,
2751 "Quota_cnt: %d\n",
2752 rport_ptr->tx_quota_cnt);
2753 i += scnprintf(buf + i, max - i, "\n");
2754 }
2755 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002756 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002758 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002759 }
2760
2761 return i;
2762}
2763
2764static int dump_control_ports(char *buf, int max)
2765{
2766 int i = 0;
2767 struct msm_ipc_port *port_ptr;
2768
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002769 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002770 list_for_each_entry(port_ptr, &control_ports, list) {
2771 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2772 port_ptr->this_port.node_id);
2773 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2774 port_ptr->this_port.port_id);
2775 i += scnprintf(buf + i, max - i, "\n");
2776 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002777 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002778
2779 return i;
2780}
2781
2782static int dump_local_ports(char *buf, int max)
2783{
2784 int i = 0, j;
2785 unsigned long flags;
2786 struct msm_ipc_port *port_ptr;
2787
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002788 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002789 for (j = 0; j < LP_HASH_SIZE; j++) {
2790 list_for_each_entry(port_ptr, &local_ports[j], list) {
2791 spin_lock_irqsave(&port_ptr->port_lock, flags);
2792 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2793 port_ptr->this_port.node_id);
2794 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2795 port_ptr->this_port.port_id);
2796 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2797 port_ptr->num_tx);
2798 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2799 port_ptr->num_rx);
2800 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2801 port_ptr->num_tx_bytes);
2802 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2803 port_ptr->num_rx_bytes);
2804 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2805 i += scnprintf(buf + i, max - i, "\n");
2806 }
2807 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002808 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002809
2810 return i;
2811}
2812
2813#define DEBUG_BUFMAX 4096
2814static char debug_buffer[DEBUG_BUFMAX];
2815
2816static ssize_t debug_read(struct file *file, char __user *buf,
2817 size_t count, loff_t *ppos)
2818{
2819 int (*fill)(char *buf, int max) = file->private_data;
2820 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2821 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2822}
2823
2824static int debug_open(struct inode *inode, struct file *file)
2825{
2826 file->private_data = inode->i_private;
2827 return 0;
2828}
2829
2830static const struct file_operations debug_ops = {
2831 .read = debug_read,
2832 .open = debug_open,
2833};
2834
2835static void debug_create(const char *name, mode_t mode,
2836 struct dentry *dent,
2837 int (*fill)(char *buf, int max))
2838{
2839 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2840}
2841
2842static void debugfs_init(void)
2843{
2844 struct dentry *dent;
2845
2846 dent = debugfs_create_dir("msm_ipc_router", 0);
2847 if (IS_ERR(dent))
2848 return;
2849
2850 debug_create("dump_local_ports", 0444, dent,
2851 dump_local_ports);
2852 debug_create("dump_remote_ports", 0444, dent,
2853 dump_remote_ports);
2854 debug_create("dump_control_ports", 0444, dent,
2855 dump_control_ports);
2856 debug_create("dump_servers", 0444, dent,
2857 dump_servers);
2858 debug_create("dump_xprt_info", 0444, dent,
2859 dump_xprt_info);
2860 debug_create("dump_routing_table", 0444, dent,
2861 dump_routing_table);
2862}
2863
2864#else
2865static void debugfs_init(void) {}
2866#endif
2867
2868static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2869{
2870 struct msm_ipc_router_xprt_info *xprt_info;
2871 struct msm_ipc_routing_table_entry *rt_entry;
2872
2873 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2874 GFP_KERNEL);
2875 if (!xprt_info)
2876 return -ENOMEM;
2877
2878 xprt_info->xprt = xprt;
2879 xprt_info->initialized = 0;
2880 xprt_info->remote_node_id = -1;
2881 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002882 mutex_init(&xprt_info->rx_lock_lhb2);
2883 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002884 wake_lock_init(&xprt_info->wakelock,
2885 WAKE_LOCK_SUSPEND, xprt->name);
2886 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002887 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002888 INIT_WORK(&xprt_info->read_data, do_read_data);
2889 INIT_LIST_HEAD(&xprt_info->list);
2890
2891 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2892 if (!xprt_info->workqueue) {
2893 kfree(xprt_info);
2894 return -ENOMEM;
2895 }
2896
2897 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2898 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2899 xprt_info->initialized = 1;
2900 }
2901
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002902 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002903 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002904 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002905
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002906 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002907 if (!routing_table_inited) {
2908 init_routing_table();
2909 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2910 add_routing_table_entry(rt_entry);
2911 routing_table_inited = 1;
2912 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002913 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002914
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002915 xprt->priv = xprt_info;
2916
2917 return 0;
2918}
2919
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002920static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2921{
2922 struct msm_ipc_router_xprt_info *xprt_info;
2923
2924 if (xprt && xprt->priv) {
2925 xprt_info = xprt->priv;
2926
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002927 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002928 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002929 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002930
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002931 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002932 list_del(&xprt_info->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002933 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002934
2935 flush_workqueue(xprt_info->workqueue);
2936 destroy_workqueue(xprt_info->workqueue);
2937 wake_lock_destroy(&xprt_info->wakelock);
2938
2939 xprt->priv = 0;
2940 kfree(xprt_info);
2941 }
2942}
2943
2944
2945struct msm_ipc_router_xprt_work {
2946 struct msm_ipc_router_xprt *xprt;
2947 struct work_struct work;
2948};
2949
2950static void xprt_open_worker(struct work_struct *work)
2951{
2952 struct msm_ipc_router_xprt_work *xprt_work =
2953 container_of(work, struct msm_ipc_router_xprt_work, work);
2954
2955 msm_ipc_router_add_xprt(xprt_work->xprt);
2956 kfree(xprt_work);
2957}
2958
2959static void xprt_close_worker(struct work_struct *work)
2960{
2961 struct msm_ipc_router_xprt_work *xprt_work =
2962 container_of(work, struct msm_ipc_router_xprt_work, work);
2963
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002964 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002965 msm_ipc_router_remove_xprt(xprt_work->xprt);
Zaheerulla Meer35893a62013-06-19 16:54:44 +05302966 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002967 kfree(xprt_work);
2968}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002969
2970void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2971 unsigned event,
2972 void *data)
2973{
2974 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002975 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002976 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002977 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002978
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002979 if (!msm_ipc_router_workqueue) {
2980 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2981 IPC_ROUTER_INIT_TIMEOUT);
2982 if (!ret || !msm_ipc_router_workqueue) {
2983 pr_err("%s: IPC Router not initialized\n", __func__);
2984 return;
2985 }
2986 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002987
2988 switch (event) {
2989 case IPC_ROUTER_XPRT_EVENT_OPEN:
2990 D("open event for '%s'\n", xprt->name);
2991 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2992 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06002993 if (xprt_work) {
2994 xprt_work->xprt = xprt;
2995 INIT_WORK(&xprt_work->work, xprt_open_worker);
2996 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2997 } else {
2998 pr_err("%s: malloc failure - Couldn't notify OPEN event",
2999 __func__);
3000 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003001 break;
3002
3003 case IPC_ROUTER_XPRT_EVENT_CLOSE:
3004 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003005 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
3006 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06003007 if (xprt_work) {
3008 xprt_work->xprt = xprt;
3009 INIT_WORK(&xprt_work->work, xprt_close_worker);
3010 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
3011 } else {
3012 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
3013 __func__);
3014 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003015 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003016 }
3017
3018 if (!data)
3019 return;
3020
3021 while (!xprt_info) {
3022 msleep(100);
3023 xprt_info = xprt->priv;
3024 }
3025
3026 pkt = clone_pkt((struct rr_packet *)data);
3027 if (!pkt)
3028 return;
3029
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003030 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003031 list_add_tail(&pkt->list, &xprt_info->pkt_list);
3032 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003033 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06003034 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003035}
3036
3037static int __init msm_ipc_router_init(void)
3038{
3039 int i, ret;
3040 struct msm_ipc_routing_table_entry *rt_entry;
3041
3042 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06003043 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
3044 "ipc_router");
3045 if (!ipc_rtr_log_ctxt)
3046 pr_err("%s: Unable to create IPC logging for IPC RTR",
3047 __func__);
3048
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003049 msm_ipc_router_workqueue =
3050 create_singlethread_workqueue("msm_ipc_router");
3051 if (!msm_ipc_router_workqueue)
3052 return -ENOMEM;
3053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003054 debugfs_init();
3055
3056 for (i = 0; i < SRV_HASH_SIZE; i++)
3057 INIT_LIST_HEAD(&server_list[i]);
3058
3059 for (i = 0; i < LP_HASH_SIZE; i++)
3060 INIT_LIST_HEAD(&local_ports[i]);
3061
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003062 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003063 if (!routing_table_inited) {
3064 init_routing_table();
3065 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
3066 add_routing_table_entry(rt_entry);
3067 routing_table_inited = 1;
3068 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003069 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003070
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003071 ret = msm_ipc_router_init_sockets();
3072 if (ret < 0)
3073 pr_err("%s: Init sockets failed\n", __func__);
3074
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06003075 ret = msm_ipc_router_security_init();
3076 if (ret < 0)
3077 pr_err("%s: Security Init failed\n", __func__);
3078
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003079 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003080 return ret;
3081}
3082
3083module_init(msm_ipc_router_init);
3084MODULE_DESCRIPTION("MSM IPC Router");
3085MODULE_LICENSE("GPL v2");