blob: 863a2d7a89abad8960c28a35099961f4f60c47e0 [file] [log] [blame]
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define DEBUG
14
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/types.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/sched.h>
25#include <linux/poll.h>
26#include <linux/wakelock.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/debugfs.h>
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -060030#include <linux/rwsem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include <asm/uaccess.h>
33#include <asm/byteorder.h>
34
35#include <mach/smem_log.h>
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060036#include <mach/subsystem_notif.h>
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -060037#include <mach/msm_ipc_router.h>
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -060038#include <mach/msm_ipc_logging.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "ipc_router.h"
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060041#include "modem_notifier.h"
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -060042#include "msm_ipc_router_security.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44enum {
45 SMEM_LOG = 1U << 0,
46 RTR_DBG = 1U << 1,
47 R2R_MSG = 1U << 2,
48 R2R_RAW = 1U << 3,
49 NTFY_MSG = 1U << 4,
50 R2R_RAW_HDR = 1U << 5,
51};
52
53static int msm_ipc_router_debug_mask;
54module_param_named(debug_mask, msm_ipc_router_debug_mask,
55 int, S_IRUGO | S_IWUSR | S_IWGRP);
56
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -060057static void *ipc_rtr_log_ctxt;
58#define IPC_RTR_LOG_PAGES 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#define DIAG(x...) pr_info("[RR] ERROR " x)
60
61#if defined(DEBUG)
62#define D(x...) do { \
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -060063if (ipc_rtr_log_ctxt) \
64 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065if (msm_ipc_router_debug_mask & RTR_DBG) \
66 pr_info(x); \
67} while (0)
68
69#define RR(x...) do { \
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -060070if (ipc_rtr_log_ctxt) \
71 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072if (msm_ipc_router_debug_mask & R2R_MSG) \
73 pr_info("[RR] "x); \
74} while (0)
75
76#define RAW(x...) do { \
77if (msm_ipc_router_debug_mask & R2R_RAW) \
78 pr_info("[RAW] "x); \
79} while (0)
80
81#define NTFY(x...) do { \
82if (msm_ipc_router_debug_mask & NTFY_MSG) \
83 pr_info("[NOTIFY] "x); \
84} while (0)
85
86#define RAW_HDR(x...) do { \
87if (msm_ipc_router_debug_mask & R2R_RAW_HDR) \
88 pr_info("[HDR] "x); \
89} while (0)
90#else
91#define D(x...) do { } while (0)
92#define RR(x...) do { } while (0)
93#define RAW(x...) do { } while (0)
94#define RAW_HDR(x...) do { } while (0)
95#define NTFY(x...) do { } while (0)
96#endif
97
Zaheerulla Meer83e6cbb2013-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 Meerfbc5f902013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600108static DECLARE_RWSEM(local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-07-12 10:25:49 -0600124 char pdev_name[32];
125 int next_pdev_id;
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -0600126 int synced_sec_rule;
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 Ramasubramanianefc493b2012-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 Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600149 struct mutex quota_lock_lhb2;
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530150 struct list_head resume_tx_port_list;
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -0600151 void *sec_rule;
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-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 Ramasubramanian6dbb00f2013-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 Meer84d0bd562013-05-24 21:19:18 +0530468 void (*notify)(unsigned event, void *priv);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600483 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Karthikeyan Ramasubramanian6dbb00f2013-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 Meer84d0bd562013-05-24 21:19:18 +0530487 notify = port_ptr->notify;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600488 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer84d0bd562013-05-24 21:19:18 +0530489 if (notify)
490 notify(MSM_IPC_ROUTER_READ_CB, port_ptr->priv);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600501 down_read(&control_ports_lock_lha5);
Karthikeyan Ramasubramanian6dbb00f2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianefc493b2012-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian6396bdd2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian5b502d3642012-09-23 22:23:36 -0600658 rport_ptr->sec_rule = NULL;
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600661 mutex_init(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530662 INIT_LIST_HEAD(&rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600677 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600702 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600727 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meere3f6c3a2013-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 Meere3f6c3a2013-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 Meere3f6c3a2013-04-17 01:16:47 +0530737 local_port =
738 msm_ipc_router_lookup_local_port(rtx_port->port_id);
Zaheerulla Meer51dc3a12013-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 Ramasubramanian6dbb00f2013-05-17 15:19:56 -0600743 post_pkt_to_port(local_port, pkt, 1);
Zaheerulla Meer51dc3a12013-05-08 19:27:27 +0530744 else
745 pr_err("%s: Local Port %d not Found",
746 __func__, rtx_port->port_id);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +0530747 list_del(&rtx_port->list);
748 kfree(rtx_port);
749 }
750}
751
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600787 * Note1: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-07-12 10:25:49 -0600818static void dummy_release(struct device *dev)
819{
820}
821
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600835 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanian5b502d3642012-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 Ramasubramanianefc493b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanianefc493b2012-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 Ramasubramanianbb8306b2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -0600901 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramanianbb8306b2012-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 Ramasubramanianefc493b2012-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 Meerfbc5f902013-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 Meerfbc5f902013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramaniana2b7fdb2012-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 Ramasubramanian972c2d22013-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 Ramasubramaniana2b7fdb2012-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 Meerfbc5f902013-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 Ramasubramanian4b88e5d2013-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 Meerfbc5f902013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Meerfbc5f902013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001181 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 }
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001224 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 }
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001233 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234}
1235
Karthikeyan Ramasubramanian96cced72013-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 Ramasubramanian972c2d22013-07-19 15:58:38 -06001252 memset(&msg, 0, sizeof(msg));
Karthikeyan Ramasubramanian96cced72013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001258 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanian96cced72013-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 Meerfbc5f902013-07-11 19:24:40 +05301262 msm_ipc_router_send_control_msg(tmp_xprt_info, &msg,
1263 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001264 break;
1265 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001266 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanian96cced72013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001303 struct msm_ipc_server *server = rport_ptr->server;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001304
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian972c2d22013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001324 union rr_control_msg ctl;
1325 int j;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001326
Karthikeyan Ramasubramanian972c2d22013-07-19 15:58:38 -06001327 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001391 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001400 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001420 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001439 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001457 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001466 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06001467}
1468
Karthikeyan Ramasubramaniana2b7fdb2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001489 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001494 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001500 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001501 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1502 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001503 up_write(&rt_entry->lock_lha4);
1504 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-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 Meerfbc5f902013-07-11 19:24:40 +05301509 rc = msm_ipc_router_send_control_msg(xprt_info, &ctl,
1510 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramaniana2b7fdb2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001521 down_read(&server_list_lock_lha2);
1522 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniana2b7fdb2012-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 Ramasubramanianc4c0aaa2013-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 Ramasubramaniana2b7fdb2012-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001533 up_read(&routing_table_lock_lha3);
1534 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001535 return rc;
1536 }
1537 }
1538 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001539 up_read(&routing_table_lock_lha3);
1540 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana2b7fdb2012-10-23 13:12:44 -06001541 RR("HELLO message processed\n");
1542 return rc;
1543}
1544
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001549 int ret = 0;
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001553 down_read(&local_ports_lock_lha2);
1554 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001559 ret = -ENODEV;
1560 goto prtm_out;
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001561 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001562 mutex_lock(&rport_ptr->quota_lock_lhb2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001563 rport_ptr->tx_quota_cnt = 0;
1564 post_resume_tx(rport_ptr, pkt);
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001594 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001599 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001600 pr_err("%s: rt_entry allocation failed\n", __func__);
1601 return -ENOMEM;
1602 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001603 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001604 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1605 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001606 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001607 add_routing_table_entry(rt_entry);
1608 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001609 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001617 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001625 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001626 pr_err("%s: Server Create failed\n", __func__);
1627 return -ENOMEM;
1628 }
1629
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001630 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001636 up_read(&routing_table_lock_lha3);
1637 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001638 return -ENOMEM;
1639 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001640 rport_ptr->server = server;
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001645 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-05-17 18:18:20 -06001646 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001647 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001666 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001680 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001690 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001695 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniane2e15fa2013-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 Ramasubramaniana2b7fdb2012-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 Ramasubramaniane2e15fa2013-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 Ramasubramaniane2e15fa2013-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 Ramasubramaniane2e15fa2013-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 Ramasubramaniane2e15fa2013-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 Ramasubramanian2bfe8ec2013-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 Meer83e6cbb2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001830 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001831 release_pkt(pkt);
Zaheerulla Meerfbc5f902013-07-11 19:24:40 +05301832 return;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001833 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001834
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001846 up_read(&routing_table_lock_lha3);
1847 up_read(&local_ports_lock_lha2);
Zaheerulla Meer34274332013-07-12 16:16:30 +05301848 release_pkt(pkt);
Zaheerulla Meerfbc5f902013-07-11 19:24:40 +05301849 return;
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001850 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001851 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001852 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian6dbb00f2013-05-17 15:19:56 -06001853 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian4b88e5d2013-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 Ramasubramanian972c2d22013-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 Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001904 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 broadcast_ctl_msg(&ctl);
1906 spin_lock_irqsave(&port_ptr->port_lock, flags);
1907 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06001908 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001909 port_ptr->port_name.service = server->name.service;
1910 port_ptr->port_name.instance = server->name.instance;
1911 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1912 return 0;
1913}
1914
1915int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1916{
1917 struct msm_ipc_server *server;
1918 unsigned long flags;
1919 union rr_control_msg ctl;
1920
1921 if (!port_ptr)
1922 return -EINVAL;
1923
1924 if (port_ptr->type != SERVER_PORT) {
1925 pr_err("%s: Trying to unregister a non-server port\n",
1926 __func__);
1927 return -EINVAL;
1928 }
1929
1930 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1931 pr_err("%s: Trying to unregister a remote server locally\n",
1932 __func__);
1933 return -EINVAL;
1934 }
1935
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001936 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1938 port_ptr->port_name.instance,
1939 port_ptr->this_port.node_id,
1940 port_ptr->this_port.port_id);
1941 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001942 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 pr_err("%s: Server lookup failed\n", __func__);
1944 return -ENODEV;
1945 }
1946
Karthikeyan Ramasubramanian972c2d22013-07-19 15:58:38 -06001947 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001948 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1949 ctl.srv.service = server->name.service;
1950 ctl.srv.instance = server->name.instance;
1951 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1952 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1954 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06001955 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06001956 broadcast_ctl_msg(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 spin_lock_irqsave(&port_ptr->port_lock, flags);
1958 port_ptr->type = CLIENT_PORT;
1959 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1960 return 0;
1961}
1962
1963static int loopback_data(struct msm_ipc_port *src,
1964 uint32_t port_id,
1965 struct sk_buff_head *data)
1966{
1967 struct sk_buff *head_skb;
1968 struct rr_header *hdr;
1969 struct msm_ipc_port *port_ptr;
1970 struct rr_packet *pkt;
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07001971 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001972
1973 if (!data) {
1974 pr_err("%s: Invalid pkt pointer\n", __func__);
1975 return -EINVAL;
1976 }
1977
1978 pkt = create_pkt(data);
1979 if (!pkt) {
1980 pr_err("%s: New pkt create failed\n", __func__);
1981 return -ENOMEM;
1982 }
1983
1984 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001985 if (!head_skb) {
1986 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik1c9a3d42013-04-17 15:10:40 -06001987 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001988 return -EINVAL;
1989 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001990 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
1991 if (!hdr) {
1992 pr_err("%s: Prepend Header failed\n", __func__);
1993 release_pkt(pkt);
1994 return -ENOMEM;
1995 }
1996 hdr->version = IPC_ROUTER_VERSION;
1997 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
1998 hdr->src_node_id = src->this_port.node_id;
1999 hdr->src_port_id = src->this_port.port_id;
2000 hdr->size = pkt->length;
2001 hdr->confirm_rx = 0;
2002 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2003 hdr->dst_port_id = port_id;
2004 pkt->length += IPC_ROUTER_HDR_SIZE;
2005
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002006 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002007 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2008 if (!port_ptr) {
2009 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002010 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011 release_pkt(pkt);
2012 return -ENODEV;
2013 }
2014
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07002015 ret_len = pkt->length;
Karthikeyan Ramasubramanian6dbb00f2013-05-17 15:19:56 -06002016 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002017 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002018 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002019
Karthikeyan Ramasubramanianb0e23c52013-02-08 13:07:42 -07002020 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021}
2022
2023static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2024 struct msm_ipc_router_remote_port *rport_ptr,
2025 struct rr_packet *pkt)
2026{
2027 struct sk_buff *head_skb;
2028 struct rr_header *hdr;
2029 struct msm_ipc_router_xprt_info *xprt_info;
2030 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302031 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033
2034 if (!rport_ptr || !src || !pkt)
2035 return -EINVAL;
2036
2037 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002038 if (!head_skb) {
2039 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2040 return -EINVAL;
2041 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2043 if (!hdr) {
2044 pr_err("%s: Prepend Header failed\n", __func__);
2045 return -ENOMEM;
2046 }
2047 hdr->version = IPC_ROUTER_VERSION;
2048 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2049 hdr->src_node_id = src->this_port.node_id;
2050 hdr->src_port_id = src->this_port.port_id;
2051 hdr->size = pkt->length;
2052 hdr->confirm_rx = 0;
2053 hdr->dst_node_id = rport_ptr->node_id;
2054 hdr->dst_port_id = rport_ptr->port_id;
2055 pkt->length += IPC_ROUTER_HDR_SIZE;
2056
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002057 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302058 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2059 if (msm_ipc_router_lookup_resume_tx_port(
2060 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002061 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302062 return -EAGAIN;
2063 }
2064 resume_tx_port =
2065 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2066 GFP_KERNEL);
2067 if (!resume_tx_port) {
2068 pr_err("%s: Resume_Tx port allocation failed\n",
2069 __func__);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002070 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302071 return -ENOMEM;
2072 }
2073 INIT_LIST_HEAD(&resume_tx_port->list);
2074 resume_tx_port->port_id = src->this_port.port_id;
2075 resume_tx_port->node_id = src->this_port.node_id;
2076 list_add_tail(&resume_tx_port->list,
2077 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002078 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meere3f6c3a2013-04-17 01:16:47 +05302079 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002080 }
2081 rport_ptr->tx_quota_cnt++;
2082 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2083 hdr->confirm_rx = 1;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002084 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086 rt_entry = lookup_routing_table(hdr->dst_node_id);
2087 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002088 pr_err("%s: Remote node %d not up\n",
2089 __func__, hdr->dst_node_id);
2090 return -ENODEV;
2091 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002092 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002094 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002095 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002096 mutex_unlock(&xprt_info->tx_lock_lhb2);
2097 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098
2099 if (ret < 0) {
2100 pr_err("%s: Write on XPRT failed\n", __func__);
2101 return ret;
2102 }
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002103 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104
2105 RAW_HDR("[w rr_h] "
2106 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2107 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2108 hdr->version, type_to_str(hdr->type),
2109 hdr->src_node_id, hdr->src_port_id,
2110 hdr->confirm_rx, hdr->size,
2111 hdr->dst_node_id, hdr->dst_port_id);
2112
2113#if defined(CONFIG_MSM_SMD_LOGGING)
2114#if defined(DEBUG)
2115 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2116 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer83e6cbb2013-06-19 16:31:17 +05302117 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002118 IPC_ROUTER_LOG_EVENT_TX),
2119 (hdr->src_node_id << 24) |
2120 (hdr->src_port_id & 0xffffff),
2121 (hdr->dst_node_id << 24) |
2122 (hdr->dst_port_id & 0xffffff),
2123 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2124 (hdr->size & 0xffff));
2125 }
2126#endif
2127#endif
2128
2129 return pkt->length;
2130}
2131
2132int msm_ipc_router_send_to(struct msm_ipc_port *src,
2133 struct sk_buff_head *data,
2134 struct msm_ipc_addr *dest)
2135{
2136 uint32_t dst_node_id = 0, dst_port_id = 0;
2137 struct msm_ipc_server *server;
2138 struct msm_ipc_server_port *server_port;
2139 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2140 struct rr_packet *pkt;
2141 int ret;
2142
2143 if (!src || !data || !dest) {
2144 pr_err("%s: Invalid Parameters\n", __func__);
2145 return -EINVAL;
2146 }
2147
2148 /* Resolve Address*/
2149 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2150 dst_node_id = dest->addr.port_addr.node_id;
2151 dst_port_id = dest->addr.port_addr.port_id;
2152 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002153 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002154 server = msm_ipc_router_lookup_server(
2155 dest->addr.port_name.service,
2156 dest->addr.port_name.instance,
2157 0, 0);
2158 if (!server) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002159 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160 pr_err("%s: Destination not reachable\n", __func__);
2161 return -ENODEV;
2162 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002163 server_port = list_first_entry(&server->server_port_list,
2164 struct msm_ipc_server_port,
2165 list);
2166 dst_node_id = server_port->server_addr.node_id;
2167 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002168 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169 }
2170 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2171 ret = loopback_data(src, dst_port_id, data);
2172 return ret;
2173 }
2174
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002175 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2177 dst_port_id);
2178 if (!rport_ptr) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002179 up_read(&routing_table_lock_lha3);
Zaheerulla Meer3d8b0a02013-05-10 15:51:28 +05302180 pr_err("%s: Remote port not found\n", __func__);
2181 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002182 }
2183
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06002184 if (src->check_send_permissions) {
2185 ret = src->check_send_permissions(rport_ptr->sec_rule);
2186 if (ret <= 0) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002187 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06002188 pr_err("%s: permission failure for %s\n",
2189 __func__, current->comm);
2190 return -EPERM;
2191 }
2192 }
2193
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194 pkt = create_pkt(data);
2195 if (!pkt) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002196 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 pr_err("%s: Pkt creation failed\n", __func__);
2198 return -ENOMEM;
2199 }
2200
2201 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002202 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 release_pkt(pkt);
2204
2205 return ret;
2206}
2207
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002208int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2209 struct msm_ipc_addr *dest,
2210 void *data, unsigned int data_len)
2211{
2212 struct sk_buff_head *out_skb_head;
2213 int ret;
2214
2215 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2216 if (!out_skb_head) {
2217 pr_err("%s: SKB conversion failed\n", __func__);
2218 return -EFAULT;
2219 }
2220
2221 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meer51dc3a12013-05-08 19:27:27 +05302222 if (ret == -EAGAIN)
2223 return ret;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002224 if (ret < 0) {
2225 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2226 __func__, ret);
2227 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meer2f1eb072013-06-26 22:39:00 +05302228 return ret;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002229 }
2230 return 0;
2231}
2232
Zaheerulla Meerfbc5f902013-07-11 19:24:40 +05302233/**
2234 * msm_ipc_router_send_resume_tx() - Send Resume_Tx message
2235 * @data: Pointer to received data packet that has confirm_rx bit set
2236 *
2237 * @return: On success, number of bytes transferred is returned, else
2238 * standard linux error code is returned.
2239 *
2240 * This function sends the Resume_Tx event to the remote node that
2241 * sent the data with confirm_rx field set. In case of a multi-hop
2242 * scenario also, this function makes sure that the destination node_id
2243 * to which the resume_tx event should reach is right.
2244 */
2245static int msm_ipc_router_send_resume_tx(void *data)
2246{
2247 union rr_control_msg msg;
2248 struct rr_header *hdr = (struct rr_header *)data;
2249 struct msm_ipc_routing_table_entry *rt_entry;
2250 int ret;
2251
2252 memset(&msg, 0, sizeof(msg));
2253 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
2254 msg.cli.node_id = hdr->dst_node_id;
2255 msg.cli.port_id = hdr->dst_port_id;
2256 down_read(&routing_table_lock_lha3);
2257 rt_entry = lookup_routing_table(hdr->src_node_id);
2258 if (!rt_entry) {
2259 pr_err("%s: %d Node is not present",
2260 __func__, hdr->src_node_id);
2261 up_read(&routing_table_lock_lha3);
2262 return -ENODEV;
2263 }
2264 RR("x RESUME_TX id=%d:%08x\n",
2265 msg.cli.node_id, msg.cli.port_id);
2266 ret = msm_ipc_router_send_control_msg(rt_entry->xprt_info, &msg,
2267 hdr->src_node_id);
2268 up_read(&routing_table_lock_lha3);
2269 if (ret < 0)
2270 pr_err("%s: Send Resume_Tx Failed SRC_NODE: %d SRC_PORT: %d DEST_NODE: %d",
2271 __func__, hdr->dst_node_id, hdr->dst_port_id,
2272 hdr->src_node_id);
2273
2274 return ret;
2275}
2276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002277int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2278 struct sk_buff_head **data,
2279 size_t buf_len)
2280{
2281 struct rr_packet *pkt;
Zaheerulla Meerfbc5f902013-07-11 19:24:40 +05302282 struct sk_buff *head_skb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002283 int ret;
2284
2285 if (!port_ptr || !data)
2286 return -EINVAL;
2287
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002288 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002290 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291 return -EAGAIN;
2292 }
2293
2294 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2295 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002296 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002297 return -ETOOSMALL;
2298 }
2299 list_del(&pkt->list);
2300 if (list_empty(&port_ptr->port_rx_q))
2301 wake_unlock(&port_ptr->port_rx_wake_lock);
2302 *data = pkt->pkt_fragment_q;
2303 ret = pkt->length;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002304 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meerfbc5f902013-07-11 19:24:40 +05302305 kfree(pkt);
2306 head_skb = skb_peek(*data);
2307 if (!head_skb) {
2308 pr_err("%s: Socket Buffer not found", __func__);
2309 return -EFAULT;
2310 }
2311 if (((struct rr_header *)(head_skb->data))->confirm_rx)
2312 msm_ipc_router_send_resume_tx((void *)(head_skb->data));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002313
2314 return ret;
2315}
2316
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302317/**
Arun Kumar Neelakantam0af96762013-06-21 17:57:18 +05302318 * msm_ipc_router_rx_data_wait() - Wait for new message destined to a local port.
2319 * @port_ptr: Pointer to the local port
2320 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2321 * > 0 timeout indicates the wait time.
2322 * 0 indicates that we do not wait.
2323 * @return: 0 if there are pending messages to read,
2324 * standard Linux error code otherwise.
2325 *
2326 * Checks for the availability of messages that are destined to a local port.
2327 * If no messages are present then waits as per @timeout.
2328 */
2329int msm_ipc_router_rx_data_wait(struct msm_ipc_port *port_ptr, long timeout)
2330{
2331 int ret = 0;
2332
2333 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2334 while (list_empty(&port_ptr->port_rx_q)) {
2335 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2336 if (timeout < 0) {
2337 ret = wait_event_interruptible(
2338 port_ptr->port_rx_wait_q,
2339 !list_empty(&port_ptr->port_rx_q));
2340 if (ret)
2341 return ret;
2342 } else if (timeout > 0) {
2343 timeout = wait_event_interruptible_timeout(
2344 port_ptr->port_rx_wait_q,
2345 !list_empty(&port_ptr->port_rx_q),
2346 timeout);
2347 if (timeout < 0)
2348 return -EFAULT;
2349 }
2350 if (timeout == 0)
2351 return -ENOMSG;
2352 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2353 }
2354 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2355
2356 return ret;
2357}
2358
2359/**
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302360 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2361 * @port_ptr: Pointer to the local port
2362 * @data : Pointer to the socket buffer head
2363 * @src: Pointer to local port address
2364 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2365 * > 0 timeout indicates the wait time.
2366 * 0 indicates that we do not wait.
2367 * @return: = Number of bytes read(On successful read operation).
Arun Kumar Neelakantam0af96762013-06-21 17:57:18 +05302368 * = -ENOMSG (If there are no pending messages and timeout is 0).
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302369 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2370 * = -EFAULT (If there are no pending messages when timeout is > 0
2371 * and the wait_event_interruptible_timeout has returned value > 0)
2372 * = -ERESTARTSYS (If there are no pending messages when timeout
2373 * is < 0 and wait_event_interruptible was interrupted by a signal)
2374 *
2375 * This function reads the messages that are destined for a local port. It
2376 * is used by modules that exist with-in the kernel and use IPC Router for
2377 * transport. The function checks if there are any messages that are already
2378 * received. If yes, it reads them, else it waits as per the timeout value.
2379 * On a successful read, the return value of the function indicates the number
2380 * of bytes that are read.
2381 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002382int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2383 struct sk_buff_head **data,
2384 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002385 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002386{
2387 int ret, data_len, align_size;
2388 struct sk_buff *temp_skb;
2389 struct rr_header *hdr = NULL;
2390
2391 if (!port_ptr || !data) {
2392 pr_err("%s: Invalid pointers being passed\n", __func__);
2393 return -EINVAL;
2394 }
2395
2396 *data = NULL;
Arun Kumar Neelakantam0af96762013-06-21 17:57:18 +05302397
2398 ret = msm_ipc_router_rx_data_wait(port_ptr, timeout);
2399 if (ret)
2400 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401
2402 ret = msm_ipc_router_read(port_ptr, data, 0);
2403 if (ret <= 0 || !(*data))
2404 return ret;
2405
2406 temp_skb = skb_peek(*data);
2407 hdr = (struct rr_header *)(temp_skb->data);
2408 if (src) {
2409 src->addrtype = MSM_IPC_ADDR_ID;
2410 src->addr.port_addr.node_id = hdr->src_node_id;
2411 src->addr.port_addr.port_id = hdr->src_port_id;
2412 }
2413
2414 data_len = hdr->size;
2415 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2416 align_size = ALIGN_SIZE(data_len);
2417 if (align_size) {
2418 temp_skb = skb_peek_tail(*data);
2419 skb_trim(temp_skb, (temp_skb->len - align_size));
2420 }
2421 return data_len;
2422}
2423
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002424int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2425 struct msm_ipc_addr *src,
2426 unsigned char **data,
2427 unsigned int *len)
2428{
2429 struct sk_buff_head *in_skb_head;
2430 int ret;
2431
Zaheerulla Meerabb0eba2013-05-22 23:31:59 +05302432 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2433
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002434 if (ret < 0) {
Arun Kumar Neelakantam0af96762013-06-21 17:57:18 +05302435 if (ret != -ENOMSG)
2436 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2437 __func__, ret);
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002438 return ret;
2439 }
2440
2441 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2442 if (!(*data))
2443 pr_err("%s: Buf conversion failed\n", __func__);
2444
2445 *len = ret;
2446 msm_ipc_router_free_skb(in_skb_head);
2447 return 0;
2448}
2449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002451 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452 void *priv)
2453{
2454 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanianefc493b2012-07-12 10:25:49 -06002455 int ret;
2456
2457 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2458 if (ret < 0) {
2459 pr_err("%s: Error waiting for local router\n", __func__);
2460 return NULL;
2461 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462
2463 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2464 if (!port_ptr)
2465 pr_err("%s: port_ptr alloc failed\n", __func__);
2466
2467 return port_ptr;
2468}
2469
2470int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2471{
2472 union rr_control_msg msg;
2473 struct rr_packet *pkt, *temp_pkt;
2474 struct msm_ipc_server *server;
2475
2476 if (!port_ptr)
2477 return -EINVAL;
2478
2479 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002480 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002481 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002482 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002483
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian972c2d22013-07-19 15:58:38 -06002485 memset(&msg, 0, sizeof(msg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2487 msg.srv.service = port_ptr->port_name.service;
2488 msg.srv.instance = port_ptr->port_name.instance;
2489 msg.srv.node_id = port_ptr->this_port.node_id;
2490 msg.srv.port_id = port_ptr->this_port.port_id;
2491 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2492 msg.srv.service, msg.srv.instance,
2493 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002494 broadcast_ctl_msg(&msg);
2495 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002496 }
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002497
2498 /*
2499 * Server port could have been a client port earlier.
2500 * Send REMOVE_CLIENT message in either case.
2501 */
Karthikeyan Ramasubramanian5c568fb2013-01-10 17:09:13 -07002502 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramanian96cced72013-05-02 17:25:54 -06002503 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2504 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2505 port_ptr->this_port.node_id,
2506 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002507 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002508 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002509 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002510 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002511 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002512 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002513 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002514 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianf7a4b6e2013-01-16 09:00:28 -07002515 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516 }
2517
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002518 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2520 list_del(&pkt->list);
2521 release_pkt(pkt);
2522 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002523 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002526 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002527 server = msm_ipc_router_lookup_server(
2528 port_ptr->port_name.service,
2529 port_ptr->port_name.instance,
2530 port_ptr->this_port.node_id,
2531 port_ptr->this_port.port_id);
2532 if (server)
2533 msm_ipc_router_destroy_server(server,
2534 port_ptr->this_port.node_id,
2535 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002536 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002537 }
2538
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002539 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540 kfree(port_ptr);
2541 return 0;
2542}
2543
2544int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2545{
2546 struct rr_packet *pkt;
2547 int rc = 0;
2548
2549 if (!port_ptr)
2550 return -EINVAL;
2551
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002552 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002553 if (!list_empty(&port_ptr->port_rx_q)) {
2554 pkt = list_first_entry(&port_ptr->port_rx_q,
2555 struct rr_packet, list);
2556 rc = pkt->length;
2557 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002558 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002559
2560 return rc;
2561}
2562
2563int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2564{
2565 if (!port_ptr)
2566 return -EINVAL;
2567
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002568 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002569 list_del(&port_ptr->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002570 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002571 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002572 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002574 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002575
2576 return 0;
2577}
2578
2579int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002580 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002581 int num_entries_in_array,
2582 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002583{
2584 struct msm_ipc_server *server;
2585 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002586 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587
2588 if (!srv_name) {
2589 pr_err("%s: Invalid srv_name\n", __func__);
2590 return -EINVAL;
2591 }
2592
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002593 if (num_entries_in_array && !srv_info) {
2594 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 return -EINVAL;
2596 }
2597
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002598 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002599 if (!lookup_mask)
2600 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002601 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2602 list_for_each_entry(server, &server_list[key], list) {
2603 if ((server->name.service != srv_name->service) ||
2604 ((server->name.instance & lookup_mask) !=
2605 srv_name->instance))
2606 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002607
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002608 list_for_each_entry(server_port,
2609 &server->server_port_list, list) {
2610 if (i < num_entries_in_array) {
2611 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002612 server_port->server_addr.node_id;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002613 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002614 server_port->server_addr.port_id;
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002615 srv_info[i].service = server->name.service;
2616 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002617 }
Karthikeyan Ramasubramanianbb8306b2012-10-25 15:40:45 -06002618 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002619 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002620 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002621 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002622
2623 return i;
2624}
2625
2626int msm_ipc_router_close(void)
2627{
2628 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2629
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002630 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002631 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2632 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002633 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634 list_del(&xprt_info->list);
2635 kfree(xprt_info);
2636 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002637 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638 return 0;
2639}
2640
2641#if defined(CONFIG_DEBUG_FS)
2642static int dump_routing_table(char *buf, int max)
2643{
2644 int i = 0, j;
2645 struct msm_ipc_routing_table_entry *rt_entry;
2646
2647 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002648 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002650 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002651 i += scnprintf(buf + i, max - i,
2652 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianbddeec72012-09-10 16:10:24 -06002653 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654 i += scnprintf(buf + i, max - i,
2655 "XPRT Name: Loopback\n");
2656 i += scnprintf(buf + i, max - i,
2657 "Next Hop: %d\n", rt_entry->node_id);
2658 } else {
2659 i += scnprintf(buf + i, max - i,
2660 "XPRT Name: %s\n",
2661 rt_entry->xprt_info->xprt->name);
2662 i += scnprintf(buf + i, max - i,
2663 "Next Hop: 0x%08x\n",
2664 rt_entry->xprt_info->remote_node_id);
2665 }
2666 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002667 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002669 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670 }
2671
2672 return i;
2673}
2674
2675static int dump_xprt_info(char *buf, int max)
2676{
2677 int i = 0;
2678 struct msm_ipc_router_xprt_info *xprt_info;
2679
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002680 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002681 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2682 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2683 xprt_info->xprt->name);
2684 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2685 xprt_info->xprt->link_id);
2686 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2687 (xprt_info->initialized ? "Y" : "N"));
2688 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2689 xprt_info->remote_node_id);
2690 i += scnprintf(buf + i, max - i, "\n");
2691 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002692 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002693
2694 return i;
2695}
2696
2697static int dump_servers(char *buf, int max)
2698{
2699 int i = 0, j;
2700 struct msm_ipc_server *server;
2701 struct msm_ipc_server_port *server_port;
2702
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002703 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002704 for (j = 0; j < SRV_HASH_SIZE; j++) {
2705 list_for_each_entry(server, &server_list[j], list) {
2706 list_for_each_entry(server_port,
2707 &server->server_port_list,
2708 list) {
2709 i += scnprintf(buf + i, max - i, "Service: "
2710 "0x%08x\n", server->name.service);
2711 i += scnprintf(buf + i, max - i, "Instance: "
2712 "0x%08x\n", server->name.instance);
2713 i += scnprintf(buf + i, max - i,
2714 "Node_id: 0x%08x\n",
2715 server_port->server_addr.node_id);
2716 i += scnprintf(buf + i, max - i,
2717 "Port_id: 0x%08x\n",
2718 server_port->server_addr.port_id);
2719 i += scnprintf(buf + i, max - i, "\n");
2720 }
2721 }
2722 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002723 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002724
2725 return i;
2726}
2727
2728static int dump_remote_ports(char *buf, int max)
2729{
2730 int i = 0, j, k;
2731 struct msm_ipc_router_remote_port *rport_ptr;
2732 struct msm_ipc_routing_table_entry *rt_entry;
2733
2734 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002735 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002736 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002737 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002738 for (k = 0; k < RP_HASH_SIZE; k++) {
2739 list_for_each_entry(rport_ptr,
2740 &rt_entry->remote_port_list[k],
2741 list) {
2742 i += scnprintf(buf + i, max - i,
2743 "Node_id: 0x%08x\n",
2744 rport_ptr->node_id);
2745 i += scnprintf(buf + i, max - i,
2746 "Port_id: 0x%08x\n",
2747 rport_ptr->port_id);
2748 i += scnprintf(buf + i, max - i,
2749 "Quota_cnt: %d\n",
2750 rport_ptr->tx_quota_cnt);
2751 i += scnprintf(buf + i, max - i, "\n");
2752 }
2753 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002754 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002755 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002756 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 }
2758
2759 return i;
2760}
2761
2762static int dump_control_ports(char *buf, int max)
2763{
2764 int i = 0;
2765 struct msm_ipc_port *port_ptr;
2766
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002767 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002768 list_for_each_entry(port_ptr, &control_ports, list) {
2769 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2770 port_ptr->this_port.node_id);
2771 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2772 port_ptr->this_port.port_id);
2773 i += scnprintf(buf + i, max - i, "\n");
2774 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002775 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002776
2777 return i;
2778}
2779
2780static int dump_local_ports(char *buf, int max)
2781{
2782 int i = 0, j;
2783 unsigned long flags;
2784 struct msm_ipc_port *port_ptr;
2785
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002786 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002787 for (j = 0; j < LP_HASH_SIZE; j++) {
2788 list_for_each_entry(port_ptr, &local_ports[j], list) {
2789 spin_lock_irqsave(&port_ptr->port_lock, flags);
2790 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2791 port_ptr->this_port.node_id);
2792 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2793 port_ptr->this_port.port_id);
2794 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2795 port_ptr->num_tx);
2796 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2797 port_ptr->num_rx);
2798 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2799 port_ptr->num_tx_bytes);
2800 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2801 port_ptr->num_rx_bytes);
2802 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2803 i += scnprintf(buf + i, max - i, "\n");
2804 }
2805 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002806 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002807
2808 return i;
2809}
2810
2811#define DEBUG_BUFMAX 4096
2812static char debug_buffer[DEBUG_BUFMAX];
2813
2814static ssize_t debug_read(struct file *file, char __user *buf,
2815 size_t count, loff_t *ppos)
2816{
2817 int (*fill)(char *buf, int max) = file->private_data;
2818 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2819 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2820}
2821
2822static int debug_open(struct inode *inode, struct file *file)
2823{
2824 file->private_data = inode->i_private;
2825 return 0;
2826}
2827
2828static const struct file_operations debug_ops = {
2829 .read = debug_read,
2830 .open = debug_open,
2831};
2832
2833static void debug_create(const char *name, mode_t mode,
2834 struct dentry *dent,
2835 int (*fill)(char *buf, int max))
2836{
2837 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2838}
2839
2840static void debugfs_init(void)
2841{
2842 struct dentry *dent;
2843
2844 dent = debugfs_create_dir("msm_ipc_router", 0);
2845 if (IS_ERR(dent))
2846 return;
2847
2848 debug_create("dump_local_ports", 0444, dent,
2849 dump_local_ports);
2850 debug_create("dump_remote_ports", 0444, dent,
2851 dump_remote_ports);
2852 debug_create("dump_control_ports", 0444, dent,
2853 dump_control_ports);
2854 debug_create("dump_servers", 0444, dent,
2855 dump_servers);
2856 debug_create("dump_xprt_info", 0444, dent,
2857 dump_xprt_info);
2858 debug_create("dump_routing_table", 0444, dent,
2859 dump_routing_table);
2860}
2861
2862#else
2863static void debugfs_init(void) {}
2864#endif
2865
2866static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2867{
2868 struct msm_ipc_router_xprt_info *xprt_info;
2869 struct msm_ipc_routing_table_entry *rt_entry;
2870
2871 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2872 GFP_KERNEL);
2873 if (!xprt_info)
2874 return -ENOMEM;
2875
2876 xprt_info->xprt = xprt;
2877 xprt_info->initialized = 0;
2878 xprt_info->remote_node_id = -1;
2879 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002880 mutex_init(&xprt_info->rx_lock_lhb2);
2881 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 wake_lock_init(&xprt_info->wakelock,
2883 WAKE_LOCK_SUSPEND, xprt->name);
2884 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002885 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886 INIT_WORK(&xprt_info->read_data, do_read_data);
2887 INIT_LIST_HEAD(&xprt_info->list);
2888
2889 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2890 if (!xprt_info->workqueue) {
2891 kfree(xprt_info);
2892 return -ENOMEM;
2893 }
2894
2895 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2896 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2897 xprt_info->initialized = 1;
2898 }
2899
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002900 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002901 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002902 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002903
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002904 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002905 if (!routing_table_inited) {
2906 init_routing_table();
2907 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2908 add_routing_table_entry(rt_entry);
2909 routing_table_inited = 1;
2910 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002911 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002912
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002913 xprt->priv = xprt_info;
2914
2915 return 0;
2916}
2917
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002918static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2919{
2920 struct msm_ipc_router_xprt_info *xprt_info;
2921
2922 if (xprt && xprt->priv) {
2923 xprt_info = xprt->priv;
2924
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002925 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002926 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002927 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002928
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002929 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002930 list_del(&xprt_info->list);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002931 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002932
2933 flush_workqueue(xprt_info->workqueue);
2934 destroy_workqueue(xprt_info->workqueue);
2935 wake_lock_destroy(&xprt_info->wakelock);
2936
2937 xprt->priv = 0;
2938 kfree(xprt_info);
2939 }
2940}
2941
2942
2943struct msm_ipc_router_xprt_work {
2944 struct msm_ipc_router_xprt *xprt;
2945 struct work_struct work;
2946};
2947
2948static void xprt_open_worker(struct work_struct *work)
2949{
2950 struct msm_ipc_router_xprt_work *xprt_work =
2951 container_of(work, struct msm_ipc_router_xprt_work, work);
2952
2953 msm_ipc_router_add_xprt(xprt_work->xprt);
2954 kfree(xprt_work);
2955}
2956
2957static void xprt_close_worker(struct work_struct *work)
2958{
2959 struct msm_ipc_router_xprt_work *xprt_work =
2960 container_of(work, struct msm_ipc_router_xprt_work, work);
2961
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06002962 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002963 msm_ipc_router_remove_xprt(xprt_work->xprt);
Karthikeyan Ramasubramaniane6ef0a22013-06-12 11:49:26 -06002964 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002965 kfree(xprt_work);
2966}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002967
2968void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2969 unsigned event,
2970 void *data)
2971{
2972 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002973 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002974 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002975 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002976
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002977 if (!msm_ipc_router_workqueue) {
2978 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2979 IPC_ROUTER_INIT_TIMEOUT);
2980 if (!ret || !msm_ipc_router_workqueue) {
2981 pr_err("%s: IPC Router not initialized\n", __func__);
2982 return;
2983 }
2984 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002985
2986 switch (event) {
2987 case IPC_ROUTER_XPRT_EVENT_OPEN:
2988 D("open event for '%s'\n", xprt->name);
2989 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2990 GFP_ATOMIC);
Karthikeyan Ramasubramanian840bed12013-05-16 15:51:29 -06002991 if (xprt_work) {
2992 xprt_work->xprt = xprt;
2993 INIT_WORK(&xprt_work->work, xprt_open_worker);
2994 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
2995 } else {
2996 pr_err("%s: malloc failure - Couldn't notify OPEN event",
2997 __func__);
2998 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002999 break;
3000
3001 case IPC_ROUTER_XPRT_EVENT_CLOSE:
3002 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003003 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
3004 GFP_ATOMIC);
Karthikeyan Ramasubramanian840bed12013-05-16 15:51:29 -06003005 if (xprt_work) {
3006 xprt_work->xprt = xprt;
3007 INIT_WORK(&xprt_work->work, xprt_close_worker);
3008 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
3009 } else {
3010 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
3011 __func__);
3012 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003013 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003014 }
3015
3016 if (!data)
3017 return;
3018
3019 while (!xprt_info) {
3020 msleep(100);
3021 xprt_info = xprt->priv;
3022 }
3023
3024 pkt = clone_pkt((struct rr_packet *)data);
3025 if (!pkt)
3026 return;
3027
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06003028 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003029 list_add_tail(&pkt->list, &xprt_info->pkt_list);
3030 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06003031 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06003032 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003033}
3034
3035static int __init msm_ipc_router_init(void)
3036{
3037 int i, ret;
3038 struct msm_ipc_routing_table_entry *rt_entry;
3039
3040 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian2bfe8ec2013-03-22 10:47:20 -06003041 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
3042 "ipc_router");
3043 if (!ipc_rtr_log_ctxt)
3044 pr_err("%s: Unable to create IPC logging for IPC RTR",
3045 __func__);
3046
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003047 msm_ipc_router_workqueue =
3048 create_singlethread_workqueue("msm_ipc_router");
3049 if (!msm_ipc_router_workqueue)
3050 return -ENOMEM;
3051
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003052 debugfs_init();
3053
3054 for (i = 0; i < SRV_HASH_SIZE; i++)
3055 INIT_LIST_HEAD(&server_list[i]);
3056
3057 for (i = 0; i < LP_HASH_SIZE; i++)
3058 INIT_LIST_HEAD(&local_ports[i]);
3059
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06003060 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003061 if (!routing_table_inited) {
3062 init_routing_table();
3063 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
3064 add_routing_table_entry(rt_entry);
3065 routing_table_inited = 1;
3066 }
Karthikeyan Ramasubramanian4b88e5d2013-05-21 11:49:21 -06003067 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003068
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003069 ret = msm_ipc_router_init_sockets();
3070 if (ret < 0)
3071 pr_err("%s: Init sockets failed\n", __func__);
3072
Karthikeyan Ramasubramanian5b502d3642012-09-23 22:23:36 -06003073 ret = msm_ipc_router_security_init();
3074 if (ret < 0)
3075 pr_err("%s: Security Init failed\n", __func__);
3076
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003077 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003078 return ret;
3079}
3080
3081module_init(msm_ipc_router_init);
3082MODULE_DESCRIPTION("MSM IPC Router");
3083MODULE_LICENSE("GPL v2");