blob: 7263832c668e738c6c3b6e53e04d043e978cdf1a [file] [log] [blame]
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define DEBUG
14
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/types.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/sched.h>
25#include <linux/poll.h>
26#include <linux/wakelock.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
29#include <linux/debugfs.h>
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -060030#include <linux/rwsem.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031
32#include <asm/uaccess.h>
33#include <asm/byteorder.h>
34
35#include <mach/smem_log.h>
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060036#include <mach/subsystem_notif.h>
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -060037#include <mach/msm_ipc_router.h>
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060038#include <mach/msm_ipc_logging.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#include "ipc_router.h"
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -060041#include "modem_notifier.h"
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -060042#include "msm_ipc_router_security.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
44enum {
45 SMEM_LOG = 1U << 0,
46 RTR_DBG = 1U << 1,
47 R2R_MSG = 1U << 2,
48 R2R_RAW = 1U << 3,
49 NTFY_MSG = 1U << 4,
50 R2R_RAW_HDR = 1U << 5,
51};
52
53static int msm_ipc_router_debug_mask;
54module_param_named(debug_mask, msm_ipc_router_debug_mask,
55 int, S_IRUGO | S_IWUSR | S_IWGRP);
56
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060057static void *ipc_rtr_log_ctxt;
58#define IPC_RTR_LOG_PAGES 5
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059#define DIAG(x...) pr_info("[RR] ERROR " x)
60
61#if defined(DEBUG)
62#define D(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060063if (ipc_rtr_log_ctxt) \
64 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065if (msm_ipc_router_debug_mask & RTR_DBG) \
66 pr_info(x); \
67} while (0)
68
69#define RR(x...) do { \
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -060070if (ipc_rtr_log_ctxt) \
71 ipc_log_string(ipc_rtr_log_ctxt, x); \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072if (msm_ipc_router_debug_mask & R2R_MSG) \
73 pr_info("[RR] "x); \
74} while (0)
75
76#define RAW(x...) do { \
77if (msm_ipc_router_debug_mask & R2R_RAW) \
78 pr_info("[RAW] "x); \
79} while (0)
80
81#define NTFY(x...) do { \
82if (msm_ipc_router_debug_mask & NTFY_MSG) \
83 pr_info("[NOTIFY] "x); \
84} while (0)
85
86#define RAW_HDR(x...) do { \
87if (msm_ipc_router_debug_mask & R2R_RAW_HDR) \
88 pr_info("[HDR] "x); \
89} while (0)
90#else
91#define D(x...) do { } while (0)
92#define RR(x...) do { } while (0)
93#define RAW(x...) do { } while (0)
94#define RAW_HDR(x...) do { } while (0)
95#define NTFY(x...) do { } while (0)
96#endif
97
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +053098#define IPC_ROUTER_LOG_EVENT_ERROR 0x00
99#define IPC_ROUTER_LOG_EVENT_TX 0x01
100#define IPC_ROUTER_LOG_EVENT_RX 0x02
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530101#define IPC_ROUTER_DUMMY_DEST_NODE 0xFFFFFFFF
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102
103static LIST_HEAD(control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600104static DECLARE_RWSEM(control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105
106#define LP_HASH_SIZE 32
107static struct list_head local_ports[LP_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600108static DECLARE_RWSEM(local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600110/*
111 * Server info is organized as a hash table. The server's service ID is
112 * used to index into the hash table. The instance ID of most of the servers
113 * are 1 or 2. The service IDs are well distributed compared to the instance
114 * IDs and hence choosing service ID to index into this hash table optimizes
115 * the hash table operations like add, lookup, destroy.
116 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117#define SRV_HASH_SIZE 32
118static struct list_head server_list[SRV_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600119static DECLARE_RWSEM(server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120
121struct msm_ipc_server {
122 struct list_head list;
123 struct msm_ipc_port_name name;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600124 int synced_sec_rule;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600125 char pdev_name[32];
126 int next_pdev_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 struct list_head server_port_list;
128};
129
130struct msm_ipc_server_port {
131 struct list_head list;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600132 struct platform_device pdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 struct msm_ipc_port_addr server_addr;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600134 struct msm_ipc_router_xprt_info *xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135};
136
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530137struct msm_ipc_resume_tx_port {
138 struct list_head list;
139 uint32_t port_id;
140 uint32_t node_id;
141};
142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143#define RP_HASH_SIZE 32
144struct msm_ipc_router_remote_port {
145 struct list_head list;
146 uint32_t node_id;
147 uint32_t port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 uint32_t tx_quota_cnt;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600149 struct mutex quota_lock_lhb2;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530150 struct list_head resume_tx_port_list;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600151 void *sec_rule;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600152 struct msm_ipc_server *server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153};
154
155struct msm_ipc_router_xprt_info {
156 struct list_head list;
157 struct msm_ipc_router_xprt *xprt;
158 uint32_t remote_node_id;
159 uint32_t initialized;
160 struct list_head pkt_list;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 struct wake_lock wakelock;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600162 struct mutex rx_lock_lhb2;
163 struct mutex tx_lock_lhb2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 uint32_t need_len;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600165 uint32_t abort_data_read;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 struct work_struct read_data;
167 struct workqueue_struct *workqueue;
168};
169
170#define RT_HASH_SIZE 4
171struct msm_ipc_routing_table_entry {
172 struct list_head list;
173 uint32_t node_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600174 uint32_t neighbor_node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175 struct list_head remote_port_list[RP_HASH_SIZE];
176 struct msm_ipc_router_xprt_info *xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600177 struct rw_semaphore lock_lha4;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 unsigned long num_tx_bytes;
179 unsigned long num_rx_bytes;
180};
181
182static struct list_head routing_table[RT_HASH_SIZE];
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600183static DECLARE_RWSEM(routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184static int routing_table_inited;
185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186static void do_read_data(struct work_struct *work);
187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188static LIST_HEAD(xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600189static DECLARE_RWSEM(xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -0600191static DECLARE_COMPLETION(msm_ipc_local_router_up);
192#define IPC_ROUTER_INIT_TIMEOUT (10 * HZ)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193
194static uint32_t next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600195static DEFINE_MUTEX(next_port_id_lock_lha1);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600196static struct workqueue_struct *msm_ipc_router_workqueue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197
198enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 DOWN,
200 UP,
201};
202
203static void init_routing_table(void)
204{
205 int i;
206 for (i = 0; i < RT_HASH_SIZE; i++)
207 INIT_LIST_HEAD(&routing_table[i]);
208}
209
210static struct msm_ipc_routing_table_entry *alloc_routing_table_entry(
211 uint32_t node_id)
212{
213 int i;
214 struct msm_ipc_routing_table_entry *rt_entry;
215
216 rt_entry = kmalloc(sizeof(struct msm_ipc_routing_table_entry),
217 GFP_KERNEL);
218 if (!rt_entry) {
219 pr_err("%s: rt_entry allocation failed for %d\n",
220 __func__, node_id);
221 return NULL;
222 }
223
224 for (i = 0; i < RP_HASH_SIZE; i++)
225 INIT_LIST_HEAD(&rt_entry->remote_port_list[i]);
226
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600227 init_rwsem(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 rt_entry->node_id = node_id;
229 rt_entry->xprt_info = NULL;
230 return rt_entry;
231}
232
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600233/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234static int add_routing_table_entry(
235 struct msm_ipc_routing_table_entry *rt_entry)
236{
237 uint32_t key;
238
239 if (!rt_entry)
240 return -EINVAL;
241
242 key = (rt_entry->node_id % RT_HASH_SIZE);
243 list_add_tail(&rt_entry->list, &routing_table[key]);
244 return 0;
245}
246
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600247/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248static struct msm_ipc_routing_table_entry *lookup_routing_table(
249 uint32_t node_id)
250{
251 uint32_t key = (node_id % RT_HASH_SIZE);
252 struct msm_ipc_routing_table_entry *rt_entry;
253
254 list_for_each_entry(rt_entry, &routing_table[key], list) {
255 if (rt_entry->node_id == node_id)
256 return rt_entry;
257 }
258 return NULL;
259}
260
261struct rr_packet *rr_read(struct msm_ipc_router_xprt_info *xprt_info)
262{
263 struct rr_packet *temp_pkt;
264
265 if (!xprt_info)
266 return NULL;
267
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600268 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600269 if (xprt_info->abort_data_read) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600270 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -0600271 pr_err("%s detected SSR & exiting now\n",
272 xprt_info->xprt->name);
273 return NULL;
274 }
275
276 if (list_empty(&xprt_info->pkt_list)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600277 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600278 return NULL;
279 }
280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700281 temp_pkt = list_first_entry(&xprt_info->pkt_list,
282 struct rr_packet, list);
283 list_del(&temp_pkt->list);
284 if (list_empty(&xprt_info->pkt_list))
285 wake_unlock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600286 mutex_unlock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 return temp_pkt;
288}
289
290struct rr_packet *clone_pkt(struct rr_packet *pkt)
291{
292 struct rr_packet *cloned_pkt;
293 struct sk_buff *temp_skb, *cloned_skb;
294 struct sk_buff_head *pkt_fragment_q;
295
296 cloned_pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
297 if (!cloned_pkt) {
298 pr_err("%s: failure\n", __func__);
299 return NULL;
300 }
301
302 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
303 if (!pkt_fragment_q) {
304 pr_err("%s: pkt_frag_q alloc failure\n", __func__);
305 kfree(cloned_pkt);
306 return NULL;
307 }
308 skb_queue_head_init(pkt_fragment_q);
309
310 skb_queue_walk(pkt->pkt_fragment_q, temp_skb) {
311 cloned_skb = skb_clone(temp_skb, GFP_KERNEL);
312 if (!cloned_skb)
313 goto fail_clone;
314 skb_queue_tail(pkt_fragment_q, cloned_skb);
315 }
316 cloned_pkt->pkt_fragment_q = pkt_fragment_q;
317 cloned_pkt->length = pkt->length;
318 return cloned_pkt;
319
320fail_clone:
321 while (!skb_queue_empty(pkt_fragment_q)) {
322 temp_skb = skb_dequeue(pkt_fragment_q);
323 kfree_skb(temp_skb);
324 }
325 kfree(pkt_fragment_q);
326 kfree(cloned_pkt);
327 return NULL;
328}
329
330struct rr_packet *create_pkt(struct sk_buff_head *data)
331{
332 struct rr_packet *pkt;
333 struct sk_buff *temp_skb;
334
335 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
336 if (!pkt) {
337 pr_err("%s: failure\n", __func__);
338 return NULL;
339 }
340
341 pkt->pkt_fragment_q = data;
342 skb_queue_walk(pkt->pkt_fragment_q, temp_skb)
343 pkt->length += temp_skb->len;
344 return pkt;
345}
346
347void release_pkt(struct rr_packet *pkt)
348{
349 struct sk_buff *temp_skb;
350
351 if (!pkt)
352 return;
353
354 if (!pkt->pkt_fragment_q) {
355 kfree(pkt);
356 return;
357 }
358
359 while (!skb_queue_empty(pkt->pkt_fragment_q)) {
360 temp_skb = skb_dequeue(pkt->pkt_fragment_q);
361 kfree_skb(temp_skb);
362 }
363 kfree(pkt->pkt_fragment_q);
364 kfree(pkt);
365 return;
366}
367
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600368static struct sk_buff_head *msm_ipc_router_buf_to_skb(void *buf,
369 unsigned int buf_len)
370{
371 struct sk_buff_head *skb_head;
372 struct sk_buff *skb;
373 int first = 1, offset = 0;
374 int skb_size, data_size;
375 void *data;
376
377 skb_head = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
378 if (!skb_head) {
379 pr_err("%s: Couldnot allocate skb_head\n", __func__);
380 return NULL;
381 }
382 skb_queue_head_init(skb_head);
383
384 data_size = buf_len;
385 while (offset != buf_len) {
386 skb_size = data_size;
387 if (first)
388 skb_size += IPC_ROUTER_HDR_SIZE;
389
390 skb = alloc_skb(skb_size, GFP_KERNEL);
391 if (!skb) {
392 if (skb_size <= (PAGE_SIZE/2)) {
393 pr_err("%s: cannot allocate skb\n", __func__);
394 goto buf_to_skb_error;
395 }
396 data_size = data_size / 2;
397 continue;
398 }
399
400 if (first) {
401 skb_reserve(skb, IPC_ROUTER_HDR_SIZE);
402 first = 0;
403 }
404
405 data = skb_put(skb, data_size);
406 memcpy(skb->data, buf + offset, data_size);
407 skb_queue_tail(skb_head, skb);
408 offset += data_size;
409 data_size = buf_len - offset;
410 }
411 return skb_head;
412
413buf_to_skb_error:
414 while (!skb_queue_empty(skb_head)) {
415 skb = skb_dequeue(skb_head);
416 kfree_skb(skb);
417 }
418 kfree(skb_head);
419 return NULL;
420}
421
422static void *msm_ipc_router_skb_to_buf(struct sk_buff_head *skb_head,
423 unsigned int len)
424{
425 struct sk_buff *temp;
426 int offset = 0, buf_len = 0, copy_len;
427 void *buf;
428
429 if (!skb_head) {
430 pr_err("%s: NULL skb_head\n", __func__);
431 return NULL;
432 }
433
434 temp = skb_peek(skb_head);
435 buf_len = len;
436 buf = kmalloc(buf_len, GFP_KERNEL);
437 if (!buf) {
438 pr_err("%s: cannot allocate buf\n", __func__);
439 return NULL;
440 }
441 skb_queue_walk(skb_head, temp) {
442 copy_len = buf_len < temp->len ? buf_len : temp->len;
443 memcpy(buf + offset, temp->data, copy_len);
444 offset += copy_len;
445 buf_len -= copy_len;
446 }
447 return buf;
448}
449
450static void msm_ipc_router_free_skb(struct sk_buff_head *skb_head)
451{
452 struct sk_buff *temp_skb;
453
454 if (!skb_head)
455 return;
456
457 while (!skb_queue_empty(skb_head)) {
458 temp_skb = skb_dequeue(skb_head);
459 kfree_skb(temp_skb);
460 }
461 kfree(skb_head);
462}
463
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600464static int post_pkt_to_port(struct msm_ipc_port *port_ptr,
465 struct rr_packet *pkt, int clone)
466{
467 struct rr_packet *temp_pkt = pkt;
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530468 void (*notify)(unsigned event, void *priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600469
470 if (unlikely(!port_ptr || !pkt))
471 return -EINVAL;
472
473 if (clone) {
474 temp_pkt = clone_pkt(pkt);
475 if (!temp_pkt) {
476 pr_err("%s: Error cloning packet for port %08x:%08x\n",
477 __func__, port_ptr->this_port.node_id,
478 port_ptr->this_port.port_id);
479 return -ENOMEM;
480 }
481 }
482
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600483 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600484 wake_lock(&port_ptr->port_rx_wake_lock);
485 list_add_tail(&temp_pkt->list, &port_ptr->port_rx_q);
486 wake_up(&port_ptr->port_rx_wait_q);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530487 notify = port_ptr->notify;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600488 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer29302cf2013-05-24 21:19:18 +0530489 if (notify)
490 notify(MSM_IPC_ROUTER_READ_CB, port_ptr->priv);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600491 return 0;
492}
493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494static int post_control_ports(struct rr_packet *pkt)
495{
496 struct msm_ipc_port *port_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497
498 if (!pkt)
499 return -EINVAL;
500
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600501 down_read(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600502 list_for_each_entry(port_ptr, &control_ports, list)
503 post_pkt_to_port(port_ptr, pkt, 1);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600504 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505 return 0;
506}
507
508static uint32_t allocate_port_id(void)
509{
510 uint32_t port_id = 0, prev_port_id, key;
511 struct msm_ipc_port *port_ptr;
512
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600513 mutex_lock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514 prev_port_id = next_port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600515 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700516 do {
517 next_port_id++;
518 if ((next_port_id & 0xFFFFFFFE) == 0xFFFFFFFE)
519 next_port_id = 1;
520
521 key = (next_port_id & (LP_HASH_SIZE - 1));
522 if (list_empty(&local_ports[key])) {
523 port_id = next_port_id;
524 break;
525 }
526 list_for_each_entry(port_ptr, &local_ports[key], list) {
527 if (port_ptr->this_port.port_id == next_port_id) {
528 port_id = next_port_id;
529 break;
530 }
531 }
532 if (!port_id) {
533 port_id = next_port_id;
534 break;
535 }
536 port_id = 0;
537 } while (next_port_id != prev_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600538 up_read(&local_ports_lock_lha2);
539 mutex_unlock(&next_port_id_lock_lha1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700540
541 return port_id;
542}
543
544void msm_ipc_router_add_local_port(struct msm_ipc_port *port_ptr)
545{
546 uint32_t key;
547
548 if (!port_ptr)
549 return;
550
551 key = (port_ptr->this_port.port_id & (LP_HASH_SIZE - 1));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600552 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700553 list_add_tail(&port_ptr->list, &local_ports[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600554 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555}
556
557struct msm_ipc_port *msm_ipc_router_create_raw_port(void *endpoint,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600558 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 void *priv)
560{
561 struct msm_ipc_port *port_ptr;
562
563 port_ptr = kzalloc(sizeof(struct msm_ipc_port), GFP_KERNEL);
564 if (!port_ptr)
565 return NULL;
566
567 port_ptr->this_port.node_id = IPC_ROUTER_NID_LOCAL;
568 port_ptr->this_port.port_id = allocate_port_id();
569 if (!port_ptr->this_port.port_id) {
570 pr_err("%s: All port ids are in use\n", __func__);
571 kfree(port_ptr);
572 return NULL;
573 }
574
575 spin_lock_init(&port_ptr->port_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 INIT_LIST_HEAD(&port_ptr->port_rx_q);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600577 mutex_init(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578 init_waitqueue_head(&port_ptr->port_rx_wait_q);
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600579 snprintf(port_ptr->rx_wakelock_name, MAX_WAKELOCK_NAME_SZ,
Karthikeyan Ramasubramanian090486e2013-02-14 13:53:20 -0700580 "ipc%08x_%s",
581 port_ptr->this_port.port_id,
582 current->comm);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583 wake_lock_init(&port_ptr->port_rx_wake_lock,
Karthikeyan Ramasubramanianb4aeeb92012-03-13 12:31:43 -0600584 WAKE_LOCK_SUSPEND, port_ptr->rx_wakelock_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585
586 port_ptr->endpoint = endpoint;
587 port_ptr->notify = notify;
588 port_ptr->priv = priv;
589
590 msm_ipc_router_add_local_port(port_ptr);
591 return port_ptr;
592}
593
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600594/* Must be called with local_ports_lock_lha2 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595static struct msm_ipc_port *msm_ipc_router_lookup_local_port(uint32_t port_id)
596{
597 int key = (port_id & (LP_HASH_SIZE - 1));
598 struct msm_ipc_port *port_ptr;
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 list_for_each_entry(port_ptr, &local_ports[key], list) {
601 if (port_ptr->this_port.port_id == port_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 return port_ptr;
603 }
604 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605 return NULL;
606}
607
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600608/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609static struct msm_ipc_router_remote_port *msm_ipc_router_lookup_remote_port(
610 uint32_t node_id,
611 uint32_t port_id)
612{
613 struct msm_ipc_router_remote_port *rport_ptr;
614 struct msm_ipc_routing_table_entry *rt_entry;
615 int key = (port_id & (RP_HASH_SIZE - 1));
616
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 rt_entry = lookup_routing_table(node_id);
618 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 pr_err("%s: Node is not up\n", __func__);
620 return NULL;
621 }
622
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600623 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 list_for_each_entry(rport_ptr,
625 &rt_entry->remote_port_list[key], list) {
626 if (rport_ptr->port_id == port_id) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600627 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 return rport_ptr;
629 }
630 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600631 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 return NULL;
633}
634
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600635/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636static struct msm_ipc_router_remote_port *msm_ipc_router_create_remote_port(
637 uint32_t node_id,
638 uint32_t port_id)
639{
640 struct msm_ipc_router_remote_port *rport_ptr;
641 struct msm_ipc_routing_table_entry *rt_entry;
642 int key = (port_id & (RP_HASH_SIZE - 1));
643
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 rt_entry = lookup_routing_table(node_id);
645 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 pr_err("%s: Node is not up\n", __func__);
647 return NULL;
648 }
649
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 rport_ptr = kmalloc(sizeof(struct msm_ipc_router_remote_port),
651 GFP_KERNEL);
652 if (!rport_ptr) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 pr_err("%s: Remote port alloc failed\n", __func__);
654 return NULL;
655 }
656 rport_ptr->port_id = port_id;
657 rport_ptr->node_id = node_id;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600658 rport_ptr->sec_rule = NULL;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600659 rport_ptr->server = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 rport_ptr->tx_quota_cnt = 0;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600661 mutex_init(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530662 INIT_LIST_HEAD(&rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600663 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 list_add_tail(&rport_ptr->list,
665 &rt_entry->remote_port_list[key]);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600666 up_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 return rport_ptr;
668}
669
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530670/**
671 * msm_ipc_router_free_resume_tx_port() - Free the resume_tx ports
672 * @rport_ptr: Pointer to the remote port.
673 *
674 * This function deletes all the resume_tx ports associated with a remote port
675 * and frees the memory allocated to each resume_tx port.
676 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600677 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530678 */
679static void msm_ipc_router_free_resume_tx_port(
680 struct msm_ipc_router_remote_port *rport_ptr)
681{
682 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
683
684 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
685 &rport_ptr->resume_tx_port_list, list) {
686 list_del(&rtx_port->list);
687 kfree(rtx_port);
688 }
689}
690
691/**
692 * msm_ipc_router_lookup_resume_tx_port() - Lookup resume_tx port list
693 * @rport_ptr: Remote port whose resume_tx port list needs to be looked.
694 * @port_id: Port ID which needs to be looked from the list.
695 *
696 * return 1 if the port_id is found in the list, else 0.
697 *
698 * This function is used to lookup the existence of a local port in
699 * remote port's resume_tx list. This function is used to ensure that
700 * the same port is not added to the remote_port's resume_tx list repeatedly.
701 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600702 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530703 */
704static int msm_ipc_router_lookup_resume_tx_port(
705 struct msm_ipc_router_remote_port *rport_ptr, uint32_t port_id)
706{
707 struct msm_ipc_resume_tx_port *rtx_port;
708
709 list_for_each_entry(rtx_port, &rport_ptr->resume_tx_port_list, list) {
710 if (port_id == rtx_port->port_id)
711 return 1;
712 }
713 return 0;
714}
715
716/**
717 * post_resume_tx() - Post the resume_tx event
718 * @rport_ptr: Pointer to the remote port
719 * @pkt : The data packet that is received on a resume_tx event
720 *
721 * This function informs about the reception of the resume_tx message from a
722 * remote port pointed by rport_ptr to all the local ports that are in the
723 * resume_tx_ports_list of this remote port. On posting the information, this
724 * function sequentially deletes each entry in the resume_tx_port_list of the
725 * remote port.
726 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600727 * Must be called with rport_ptr->quota_lock_lhb2 locked.
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530728 */
729static void post_resume_tx(struct msm_ipc_router_remote_port *rport_ptr,
730 struct rr_packet *pkt)
731{
732 struct msm_ipc_resume_tx_port *rtx_port, *tmp_rtx_port;
733 struct msm_ipc_port *local_port;
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530734
735 list_for_each_entry_safe(rtx_port, tmp_rtx_port,
736 &rport_ptr->resume_tx_port_list, list) {
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530737 local_port =
738 msm_ipc_router_lookup_local_port(rtx_port->port_id);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530739 if (local_port && local_port->notify)
740 local_port->notify(MSM_IPC_ROUTER_RESUME_TX,
741 local_port->priv);
742 else if (local_port)
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -0600743 post_pkt_to_port(local_port, pkt, 1);
Zaheerulla Meerc8400402013-05-08 19:27:27 +0530744 else
745 pr_err("%s: Local Port %d not Found",
746 __func__, rtx_port->port_id);
Zaheerulla Meera34fc662013-04-17 01:16:47 +0530747 list_del(&rtx_port->list);
748 kfree(rtx_port);
749 }
750}
751
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600752/* Must be called with routing_table_lock_lha3 locked. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753static void msm_ipc_router_destroy_remote_port(
754 struct msm_ipc_router_remote_port *rport_ptr)
755{
756 uint32_t node_id;
757 struct msm_ipc_routing_table_entry *rt_entry;
758
759 if (!rport_ptr)
760 return;
761
762 node_id = rport_ptr->node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 rt_entry = lookup_routing_table(node_id);
764 if (!rt_entry) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 pr_err("%s: Node %d is not up\n", __func__, node_id);
766 return;
767 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600768 down_write(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 list_del(&rport_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600770 up_write(&rt_entry->lock_lha4);
771 mutex_lock(&rport_ptr->quota_lock_lhb2);
772 msm_ipc_router_free_resume_tx_port(rport_ptr);
773 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 kfree(rport_ptr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 return;
776}
777
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600778/**
779 * msm_ipc_router_lookup_server() - Lookup server information
780 * @service: Service ID of the server info to be looked up.
781 * @instance: Instance ID of the server info to be looked up.
782 * @node_id: Node/Processor ID in which the server is hosted.
783 * @port_id: Port ID within the node in which the server is hosted.
784 *
785 * @return: If found Pointer to server structure, else NULL.
786 *
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600787 * Note1: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600788 * Note2: If the <node_id:port_id> are <0:0>, then the lookup is restricted
789 * to <service:instance>. Used only when a client wants to send a
790 * message to any QMI server.
791 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792static struct msm_ipc_server *msm_ipc_router_lookup_server(
793 uint32_t service,
794 uint32_t instance,
795 uint32_t node_id,
796 uint32_t port_id)
797{
798 struct msm_ipc_server *server;
799 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600800 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 list_for_each_entry(server, &server_list[key], list) {
803 if ((server->name.service != service) ||
804 (server->name.instance != instance))
805 continue;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600806 if ((node_id == 0) && (port_id == 0))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700807 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 list_for_each_entry(server_port, &server->server_port_list,
809 list) {
810 if ((server_port->server_addr.node_id == node_id) &&
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600811 (server_port->server_addr.port_id == port_id))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 return server;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 }
814 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 return NULL;
816}
817
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600818static void dummy_release(struct device *dev)
819{
820}
821
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600822/**
823 * msm_ipc_router_create_server() - Add server info to hash table
824 * @service: Service ID of the server info to be created.
825 * @instance: Instance ID of the server info to be created.
826 * @node_id: Node/Processor ID in which the server is hosted.
827 * @port_id: Port ID within the node in which the server is hosted.
828 * @xprt_info: XPRT through which the node hosting the server is reached.
829 *
830 * @return: Pointer to server structure on success, else NULL.
831 *
832 * This function adds the server info to the hash table. If the same
833 * server(i.e. <service_id:instance_id>) is hosted in different nodes,
834 * they are maintained as list of "server_port" under "server" structure.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600835 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600836 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837static struct msm_ipc_server *msm_ipc_router_create_server(
838 uint32_t service,
839 uint32_t instance,
840 uint32_t node_id,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600841 uint32_t port_id,
842 struct msm_ipc_router_xprt_info *xprt_info)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843{
844 struct msm_ipc_server *server = NULL;
845 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600846 int key = (service & (SRV_HASH_SIZE - 1));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 list_for_each_entry(server, &server_list[key], list) {
849 if ((server->name.service == service) &&
850 (server->name.instance == instance))
851 goto create_srv_port;
852 }
853
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600854 server = kzalloc(sizeof(struct msm_ipc_server), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 if (!server) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 pr_err("%s: Server allocation failed\n", __func__);
857 return NULL;
858 }
859 server->name.service = service;
860 server->name.instance = instance;
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -0600861 server->synced_sec_rule = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862 INIT_LIST_HEAD(&server->server_port_list);
863 list_add_tail(&server->list, &server_list[key]);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600864 scnprintf(server->pdev_name, sizeof(server->pdev_name),
865 "QMI%08x:%08x", service, instance);
866 server->next_pdev_id = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867
868create_srv_port:
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600869 server_port = kzalloc(sizeof(struct msm_ipc_server_port), GFP_KERNEL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870 if (!server_port) {
871 if (list_empty(&server->server_port_list)) {
872 list_del(&server->list);
873 kfree(server);
874 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 pr_err("%s: Server Port allocation failed\n", __func__);
876 return NULL;
877 }
878 server_port->server_addr.node_id = node_id;
879 server_port->server_addr.port_id = port_id;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -0600880 server_port->xprt_info = xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 list_add_tail(&server_port->list, &server->server_port_list);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600883 server_port->pdev.name = server->pdev_name;
884 server_port->pdev.id = server->next_pdev_id++;
885 server_port->pdev.dev.release = dummy_release;
886 platform_device_register(&server_port->pdev);
887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 return server;
889}
890
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600891/**
892 * msm_ipc_router_destroy_server() - Remove server info from hash table
893 * @server: Server info to be removed.
894 * @node_id: Node/Processor ID in which the server is hosted.
895 * @port_id: Port ID within the node in which the server is hosted.
896 *
897 * This function removes the server_port identified using <node_id:port_id>
898 * from the server structure. If the server_port list under server structure
899 * is empty after removal, then remove the server structure from the server
900 * hash table.
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -0600901 * Note: Lock the server_list_lock_lha2 before accessing this function.
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -0600902 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903static void msm_ipc_router_destroy_server(struct msm_ipc_server *server,
904 uint32_t node_id, uint32_t port_id)
905{
906 struct msm_ipc_server_port *server_port;
907
908 if (!server)
909 return;
910
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 list_for_each_entry(server_port, &server->server_port_list, list) {
912 if ((server_port->server_addr.node_id == node_id) &&
913 (server_port->server_addr.port_id == port_id))
914 break;
915 }
916 if (server_port) {
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -0600917 platform_device_unregister(&server_port->pdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700918 list_del(&server_port->list);
919 kfree(server_port);
920 }
921 if (list_empty(&server->server_port_list)) {
922 list_del(&server->list);
923 kfree(server);
924 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 return;
926}
927
928static int msm_ipc_router_send_control_msg(
929 struct msm_ipc_router_xprt_info *xprt_info,
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530930 union rr_control_msg *msg,
931 uint32_t dst_node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932{
933 struct rr_packet *pkt;
934 struct sk_buff *ipc_rtr_pkt;
935 struct rr_header *hdr;
936 int pkt_size;
937 void *data;
938 struct sk_buff_head *pkt_fragment_q;
939 int ret;
940
941 if (!xprt_info || ((msg->cmd != IPC_ROUTER_CTRL_CMD_HELLO) &&
942 !xprt_info->initialized)) {
943 pr_err("%s: xprt_info not initialized\n", __func__);
944 return -EINVAL;
945 }
946
947 if (xprt_info->remote_node_id == IPC_ROUTER_NID_LOCAL)
948 return 0;
949
950 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
951 if (!pkt) {
952 pr_err("%s: pkt alloc failed\n", __func__);
953 return -ENOMEM;
954 }
955
956 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
957 if (!pkt_fragment_q) {
958 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
959 kfree(pkt);
960 return -ENOMEM;
961 }
962 skb_queue_head_init(pkt_fragment_q);
963
964 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
965 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
966 if (!ipc_rtr_pkt) {
967 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
968 kfree(pkt_fragment_q);
969 kfree(pkt);
970 return -ENOMEM;
971 }
972
973 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
974 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
975 memcpy(data, msg, sizeof(*msg));
976 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
977 if (!hdr) {
978 pr_err("%s: skb_push failed\n", __func__);
979 kfree_skb(ipc_rtr_pkt);
980 kfree(pkt_fragment_q);
981 kfree(pkt);
982 return -ENOMEM;
983 }
984
985 hdr->version = IPC_ROUTER_VERSION;
986 hdr->type = msg->cmd;
987 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
988 hdr->src_port_id = IPC_ROUTER_ADDRESS;
989 hdr->confirm_rx = 0;
990 hdr->size = sizeof(*msg);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +0530991 if (hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX)
992 hdr->dst_node_id = dst_node_id;
993 else
994 hdr->dst_node_id = xprt_info->remote_node_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
996 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
997 pkt->pkt_fragment_q = pkt_fragment_q;
998 pkt->length = pkt_size;
999
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001000 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001001 ret = xprt_info->xprt->write(pkt, pkt_size, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001002 mutex_unlock(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003
1004 release_pkt(pkt);
1005 return ret;
1006}
1007
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001008static int msm_ipc_router_send_server_list(uint32_t node_id,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 struct msm_ipc_router_xprt_info *xprt_info)
1010{
1011 union rr_control_msg ctl;
1012 struct msm_ipc_server *server;
1013 struct msm_ipc_server_port *server_port;
1014 int i;
1015
1016 if (!xprt_info || !xprt_info->initialized) {
1017 pr_err("%s: Xprt info not initialized\n", __func__);
1018 return -EINVAL;
1019 }
1020
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001021 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001022 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 for (i = 0; i < SRV_HASH_SIZE; i++) {
1025 list_for_each_entry(server, &server_list[i], list) {
1026 ctl.srv.service = server->name.service;
1027 ctl.srv.instance = server->name.instance;
1028 list_for_each_entry(server_port,
1029 &server->server_port_list, list) {
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001030 if (server_port->server_addr.node_id !=
1031 node_id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 continue;
1033
1034 ctl.srv.node_id =
1035 server_port->server_addr.node_id;
1036 ctl.srv.port_id =
1037 server_port->server_addr.port_id;
1038 msm_ipc_router_send_control_msg(xprt_info,
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301039 &ctl, IPC_ROUTER_DUMMY_DEST_NODE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 }
1041 }
1042 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043
1044 return 0;
1045}
1046
1047#if defined(DEBUG)
1048static char *type_to_str(int i)
1049{
1050 switch (i) {
1051 case IPC_ROUTER_CTRL_CMD_DATA:
1052 return "data ";
1053 case IPC_ROUTER_CTRL_CMD_HELLO:
1054 return "hello ";
1055 case IPC_ROUTER_CTRL_CMD_BYE:
1056 return "bye ";
1057 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
1058 return "new_srvr";
1059 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
1060 return "rmv_srvr";
1061 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
1062 return "rmv_clnt";
1063 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
1064 return "resum_tx";
1065 case IPC_ROUTER_CTRL_CMD_EXIT:
1066 return "cmd_exit";
1067 default:
1068 return "invalid";
1069 }
1070}
1071#endif
1072
1073static int broadcast_ctl_msg_locally(union rr_control_msg *msg)
1074{
1075 struct rr_packet *pkt;
1076 struct sk_buff *ipc_rtr_pkt;
1077 struct rr_header *hdr;
1078 int pkt_size;
1079 void *data;
1080 struct sk_buff_head *pkt_fragment_q;
1081 int ret;
1082
1083 pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL);
1084 if (!pkt) {
1085 pr_err("%s: pkt alloc failed\n", __func__);
1086 return -ENOMEM;
1087 }
1088
1089 pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL);
1090 if (!pkt_fragment_q) {
1091 pr_err("%s: pkt_fragment_q alloc failed\n", __func__);
1092 kfree(pkt);
1093 return -ENOMEM;
1094 }
1095 skb_queue_head_init(pkt_fragment_q);
1096
1097 pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg);
1098 ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL);
1099 if (!ipc_rtr_pkt) {
1100 pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__);
1101 kfree(pkt_fragment_q);
1102 kfree(pkt);
1103 return -ENOMEM;
1104 }
1105
1106 skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1107 data = skb_put(ipc_rtr_pkt, sizeof(*msg));
1108 memcpy(data, msg, sizeof(*msg));
1109 hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE);
1110 if (!hdr) {
1111 pr_err("%s: skb_push failed\n", __func__);
1112 kfree_skb(ipc_rtr_pkt);
1113 kfree(pkt_fragment_q);
1114 kfree(pkt);
1115 return -ENOMEM;
1116 }
1117 hdr->version = IPC_ROUTER_VERSION;
1118 hdr->type = msg->cmd;
1119 hdr->src_node_id = IPC_ROUTER_NID_LOCAL;
1120 hdr->src_port_id = IPC_ROUTER_ADDRESS;
1121 hdr->confirm_rx = 0;
1122 hdr->size = sizeof(*msg);
1123 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
1124 hdr->dst_port_id = IPC_ROUTER_ADDRESS;
1125 skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt);
1126 pkt->pkt_fragment_q = pkt_fragment_q;
1127 pkt->length = pkt_size;
1128
1129 ret = post_control_ports(pkt);
1130 release_pkt(pkt);
1131 return ret;
1132}
1133
1134static int broadcast_ctl_msg(union rr_control_msg *ctl)
1135{
1136 struct msm_ipc_router_xprt_info *xprt_info;
1137
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001138 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 list_for_each_entry(xprt_info, &xprt_info_list, list) {
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301140 msm_ipc_router_send_control_msg(xprt_info, ctl,
1141 IPC_ROUTER_DUMMY_DEST_NODE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001143 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144
1145 return 0;
1146}
1147
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001148static int relay_ctl_msg(struct msm_ipc_router_xprt_info *xprt_info,
1149 union rr_control_msg *ctl)
1150{
1151 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1152
1153 if (!xprt_info || !ctl)
1154 return -EINVAL;
1155
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001156 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001157 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
1158 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301159 msm_ipc_router_send_control_msg(fwd_xprt_info, ctl,
1160 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001161 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001162 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001163
1164 return 0;
1165}
1166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167static int relay_msg(struct msm_ipc_router_xprt_info *xprt_info,
1168 struct rr_packet *pkt)
1169{
1170 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1171
1172 if (!xprt_info || !pkt)
1173 return -EINVAL;
1174
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001175 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001177 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id)
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001179 fwd_xprt_info->xprt->write(pkt, pkt->length,
1180 fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001181 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001183 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001184 return 0;
1185}
1186
1187static int forward_msg(struct msm_ipc_router_xprt_info *xprt_info,
1188 struct rr_packet *pkt)
1189{
1190 uint32_t dst_node_id;
1191 struct sk_buff *head_pkt;
1192 struct rr_header *hdr;
1193 struct msm_ipc_router_xprt_info *fwd_xprt_info;
1194 struct msm_ipc_routing_table_entry *rt_entry;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001195 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196
1197 if (!xprt_info || !pkt)
1198 return -EINVAL;
1199
1200 head_pkt = skb_peek(pkt->pkt_fragment_q);
1201 if (!head_pkt)
1202 return -EINVAL;
1203
1204 hdr = (struct rr_header *)head_pkt->data;
1205 dst_node_id = hdr->dst_node_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001206 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 rt_entry = lookup_routing_table(dst_node_id);
1208 if (!(rt_entry) || !(rt_entry->xprt_info)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001209 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210 pr_err("%s: Routing table not initialized\n", __func__);
1211 return -ENODEV;
1212 }
1213
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001214 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 fwd_xprt_info = rt_entry->xprt_info;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 if (xprt_info->remote_node_id == fwd_xprt_info->remote_node_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 pr_err("%s: Discarding Command to route back\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001218 ret = -EINVAL;
1219 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 }
1221
1222 if (xprt_info->xprt->link_id == fwd_xprt_info->xprt->link_id) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 pr_err("%s: DST in the same cluster\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001224 goto fwd_msg_out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001226 mutex_lock(&fwd_xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07001227 fwd_xprt_info->xprt->write(pkt, pkt->length, fwd_xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001228 mutex_unlock(&fwd_xprt_info->tx_lock_lhb2);
1229fwd_msg_out:
1230 up_read(&rt_entry->lock_lha4);
1231 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001233 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001234}
1235
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001236static int msm_ipc_router_send_remove_client(struct comm_mode_info *mode_info,
1237 uint32_t node_id, uint32_t port_id)
1238{
1239 union rr_control_msg msg;
1240 struct msm_ipc_router_xprt_info *tmp_xprt_info;
1241 int mode;
1242 void *xprt_info;
1243 int rc = 0;
1244
1245 if (!mode_info) {
1246 pr_err("%s: NULL mode_info\n", __func__);
1247 return -EINVAL;
1248 }
1249 mode = mode_info->mode;
1250 xprt_info = mode_info->xprt_info;
1251
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001252 memset(&msg, 0, sizeof(msg));
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001253 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1254 msg.cli.node_id = node_id;
1255 msg.cli.port_id = port_id;
1256
1257 if ((mode == SINGLE_LINK_MODE) && xprt_info) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001258 down_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001259 list_for_each_entry(tmp_xprt_info, &xprt_info_list, list) {
1260 if (tmp_xprt_info != xprt_info)
1261 continue;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301262 msm_ipc_router_send_control_msg(tmp_xprt_info, &msg,
1263 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001264 break;
1265 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001266 up_read(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001267 } else if ((mode == SINGLE_LINK_MODE) && !xprt_info) {
1268 broadcast_ctl_msg_locally(&msg);
1269 } else if (mode == MULTI_LINK_MODE) {
1270 broadcast_ctl_msg(&msg);
1271 broadcast_ctl_msg_locally(&msg);
1272 } else if (mode != NULL_MODE) {
1273 pr_err("%s: Invalid mode(%d) + xprt_inf(%p) for %08x:%08x\n",
1274 __func__, mode, xprt_info, node_id, port_id);
1275 rc = -EINVAL;
1276 }
1277 return rc;
1278}
1279
1280static void update_comm_mode_info(struct comm_mode_info *mode_info,
1281 struct msm_ipc_router_xprt_info *xprt_info)
1282{
1283 if (!mode_info) {
1284 pr_err("%s: NULL mode_info\n", __func__);
1285 return;
1286 }
1287
1288 if (mode_info->mode == NULL_MODE) {
1289 mode_info->xprt_info = xprt_info;
1290 mode_info->mode = SINGLE_LINK_MODE;
1291 } else if (mode_info->mode == SINGLE_LINK_MODE &&
1292 mode_info->xprt_info != xprt_info) {
1293 mode_info->mode = MULTI_LINK_MODE;
1294 }
1295
1296 return;
1297}
1298
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001299static void cleanup_rmt_server(struct msm_ipc_router_xprt_info *xprt_info,
1300 struct msm_ipc_router_remote_port *rport_ptr)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001301{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001302 union rr_control_msg ctl;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001303 struct msm_ipc_server *server = rport_ptr->server;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001304
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001305 D("Remove server %08x:%08x - %08x:%08x",
1306 server->name.service, server->name.instance,
1307 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001308 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001309 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001310 ctl.srv.service = server->name.service;
1311 ctl.srv.instance = server->name.instance;
1312 ctl.srv.node_id = rport_ptr->node_id;
1313 ctl.srv.port_id = rport_ptr->port_id;
1314 relay_ctl_msg(xprt_info, &ctl);
1315 broadcast_ctl_msg_locally(&ctl);
1316 msm_ipc_router_destroy_server(server,
1317 rport_ptr->node_id, rport_ptr->port_id);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001318}
1319
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001320static void cleanup_rmt_ports(struct msm_ipc_router_xprt_info *xprt_info,
1321 struct msm_ipc_routing_table_entry *rt_entry)
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001322{
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001323 struct msm_ipc_router_remote_port *rport_ptr, *tmp_rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001324 union rr_control_msg ctl;
1325 int j;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001326
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001327 memset(&ctl, 0, sizeof(ctl));
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001328 for (j = 0; j < RP_HASH_SIZE; j++) {
1329 list_for_each_entry_safe(rport_ptr, tmp_rport_ptr,
1330 &rt_entry->remote_port_list[j], list) {
1331 list_del(&rport_ptr->list);
1332 mutex_lock(&rport_ptr->quota_lock_lhb2);
1333 msm_ipc_router_free_resume_tx_port(rport_ptr);
1334 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1335
1336 if (rport_ptr->server)
1337 cleanup_rmt_server(xprt_info, rport_ptr);
1338
1339 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT;
1340 ctl.cli.node_id = rport_ptr->node_id;
1341 ctl.cli.port_id = rport_ptr->port_id;
1342 relay_ctl_msg(xprt_info, &ctl);
1343 broadcast_ctl_msg_locally(&ctl);
1344 kfree(rport_ptr);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001345 }
1346 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001347}
1348
1349static void msm_ipc_cleanup_routing_table(
1350 struct msm_ipc_router_xprt_info *xprt_info)
1351{
1352 int i;
Karthikeyan Ramasubramanian97eec852013-08-06 18:15:52 -06001353 struct msm_ipc_routing_table_entry *rt_entry, *tmp_rt_entry;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001354
1355 if (!xprt_info) {
1356 pr_err("%s: Invalid xprt_info\n", __func__);
1357 return;
1358 }
1359
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001360 down_write(&server_list_lock_lha2);
1361 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001362 for (i = 0; i < RT_HASH_SIZE; i++) {
Karthikeyan Ramasubramanian97eec852013-08-06 18:15:52 -06001363 list_for_each_entry_safe(rt_entry, tmp_rt_entry,
1364 &routing_table[i], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001365 down_write(&rt_entry->lock_lha4);
1366 if (rt_entry->xprt_info != xprt_info) {
1367 up_write(&rt_entry->lock_lha4);
1368 continue;
1369 }
1370 cleanup_rmt_ports(xprt_info, rt_entry);
1371 rt_entry->xprt_info = NULL;
1372 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian97eec852013-08-06 18:15:52 -06001373 list_del(&rt_entry->list);
1374 kfree(rt_entry);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001375 }
1376 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001377 up_write(&routing_table_lock_lha3);
1378 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001379}
1380
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001381/**
1382 * sync_sec_rule() - Synchrnoize the security rule into the server structure
1383 * @server: Server structure where the rule has to be synchronized.
1384 * @rule: Security tule to be synchronized.
1385 *
1386 * This function is used to update the server structure with the security
1387 * rule configured for the <service:instance> corresponding to that server.
1388 */
1389static void sync_sec_rule(struct msm_ipc_server *server, void *rule)
1390{
1391 struct msm_ipc_server_port *server_port;
1392 struct msm_ipc_router_remote_port *rport_ptr = NULL;
1393
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001394 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001395 list_for_each_entry(server_port, &server->server_port_list, list) {
1396 rport_ptr = msm_ipc_router_lookup_remote_port(
1397 server_port->server_addr.node_id,
1398 server_port->server_addr.port_id);
1399 if (!rport_ptr)
1400 continue;
1401 rport_ptr->sec_rule = rule;
1402 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001403 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001404 server->synced_sec_rule = 1;
1405}
1406
1407/**
1408 * msm_ipc_sync_sec_rule() - Sync the security rule to the service
1409 * @service: Service for which the rule has to be synchronized.
1410 * @instance: Instance for which the rule has to be synchronized.
1411 * @rule: Security rule to be synchronized.
1412 *
1413 * This function is used to syncrhonize the security rule with the server
1414 * hash table, if the user-space script configures the rule after the service
1415 * has come up. This function is used to synchronize the security rule to a
1416 * specific service and optionally a specific instance.
1417 */
1418void msm_ipc_sync_sec_rule(uint32_t service, uint32_t instance, void *rule)
1419{
1420 int key = (service & (SRV_HASH_SIZE - 1));
1421 struct msm_ipc_server *server;
1422
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001423 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001424 list_for_each_entry(server, &server_list[key], list) {
1425 if (server->name.service != service)
1426 continue;
1427
1428 if (server->name.instance != instance &&
1429 instance != ALL_INSTANCE)
1430 continue;
1431
1432 /*
1433 * If the rule applies to all instances and if the specific
1434 * instance of a service has a rule synchronized already,
1435 * do not apply the rule for that specific instance.
1436 */
1437 if (instance == ALL_INSTANCE && server->synced_sec_rule)
1438 continue;
1439
1440 sync_sec_rule(server, rule);
1441 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001442 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001443}
1444
1445/**
1446 * msm_ipc_sync_default_sec_rule() - Default security rule to all services
1447 * @rule: Security rule to be synchronized.
1448 *
1449 * This function is used to syncrhonize the security rule with the server
1450 * hash table, if the user-space script configures the rule after the service
1451 * has come up. This function is used to synchronize the security rule that
1452 * applies to all services, if the concerned service do not have any rule
1453 * defined.
1454 */
1455void msm_ipc_sync_default_sec_rule(void *rule)
1456{
1457 int key;
1458 struct msm_ipc_server *server;
1459
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001460 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001461 for (key = 0; key < SRV_HASH_SIZE; key++) {
1462 list_for_each_entry(server, &server_list[key], list) {
1463 if (server->synced_sec_rule)
1464 continue;
1465
1466 sync_sec_rule(server, rule);
1467 }
1468 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001469 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06001470}
1471
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001472static int process_hello_msg(struct msm_ipc_router_xprt_info *xprt_info,
1473 struct rr_header *hdr)
1474{
1475 int i, rc = 0;
1476 union rr_control_msg ctl;
1477 struct msm_ipc_routing_table_entry *rt_entry;
1478
1479 if (!hdr)
1480 return -EINVAL;
1481
1482 RR("o HELLO NID %d\n", hdr->src_node_id);
1483
1484 xprt_info->remote_node_id = hdr->src_node_id;
1485 /*
1486 * Find the entry from Routing Table corresponding to Node ID.
1487 * Under SSR, an entry will be found. When the system boots up
1488 * for the 1st time, an entry will not be found and hence allocate
1489 * an entry. Update the entry with the Node ID that it corresponds
1490 * to and the XPRT through which it can be reached.
1491 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001492 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001493 rt_entry = lookup_routing_table(hdr->src_node_id);
1494 if (!rt_entry) {
1495 rt_entry = alloc_routing_table_entry(hdr->src_node_id);
1496 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001497 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001498 pr_err("%s: rt_entry allocation failed\n", __func__);
1499 return -ENOMEM;
1500 }
1501 add_routing_table_entry(rt_entry);
1502 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001503 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001504 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1505 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001506 up_write(&rt_entry->lock_lha4);
1507 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001508
1509 /* Send a reply HELLO message */
1510 memset(&ctl, 0, sizeof(ctl));
1511 ctl.cmd = IPC_ROUTER_CTRL_CMD_HELLO;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301512 rc = msm_ipc_router_send_control_msg(xprt_info, &ctl,
1513 IPC_ROUTER_DUMMY_DEST_NODE);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001514 if (rc < 0) {
1515 pr_err("%s: Error sending reply HELLO message\n", __func__);
1516 return rc;
1517 }
1518 xprt_info->initialized = 1;
1519
1520 /*
1521 * Send list of servers from the local node and from nodes
1522 * outside the mesh network in which this XPRT is part of.
1523 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001524 down_read(&server_list_lock_lha2);
1525 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001526 for (i = 0; i < RT_HASH_SIZE; i++) {
1527 list_for_each_entry(rt_entry, &routing_table[i], list) {
1528 if ((rt_entry->node_id != IPC_ROUTER_NID_LOCAL) &&
Karthikeyan Ramasubramanian72ad5792013-01-30 14:17:57 -07001529 (!rt_entry->xprt_info ||
1530 (rt_entry->xprt_info->xprt->link_id ==
1531 xprt_info->xprt->link_id)))
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001532 continue;
1533 rc = msm_ipc_router_send_server_list(rt_entry->node_id,
1534 xprt_info);
1535 if (rc < 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001536 up_read(&routing_table_lock_lha3);
1537 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001538 return rc;
1539 }
1540 }
1541 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001542 up_read(&routing_table_lock_lha3);
1543 up_read(&server_list_lock_lha2);
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001544 RR("HELLO message processed\n");
1545 return rc;
1546}
1547
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001548static int process_resume_tx_msg(union rr_control_msg *msg,
1549 struct rr_packet *pkt)
1550{
1551 struct msm_ipc_router_remote_port *rport_ptr;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001552 int ret = 0;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001553
1554 RR("o RESUME_TX id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
1555
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001556 down_read(&local_ports_lock_lha2);
1557 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001558 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1559 msg->cli.port_id);
1560 if (!rport_ptr) {
1561 pr_err("%s: Unable to resume client\n", __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001562 ret = -ENODEV;
1563 goto prtm_out;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001564 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001565 mutex_lock(&rport_ptr->quota_lock_lhb2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001566 rport_ptr->tx_quota_cnt = 0;
1567 post_resume_tx(rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001568 mutex_unlock(&rport_ptr->quota_lock_lhb2);
1569prtm_out:
1570 up_read(&routing_table_lock_lha3);
1571 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001572 return 0;
1573}
1574
1575static int process_new_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1576 union rr_control_msg *msg, struct rr_packet *pkt)
1577{
1578 struct msm_ipc_routing_table_entry *rt_entry;
1579 struct msm_ipc_server *server;
1580 struct msm_ipc_router_remote_port *rport_ptr;
1581
1582 if (msg->srv.instance == 0) {
1583 pr_err("%s: Server %08x create rejected, version = 0\n",
1584 __func__, msg->srv.service);
1585 return -EINVAL;
1586 }
1587
1588 RR("o NEW_SERVER id=%d:%08x service=%08x:%08x\n", msg->srv.node_id,
1589 msg->srv.port_id, msg->srv.service, msg->srv.instance);
1590 /*
1591 * Find the entry from Routing Table corresponding to Node ID.
1592 * Under SSR, an entry will be found. When the subsystem hosting
1593 * service is not adjacent, an entry will not be found and hence
1594 * allocate an entry. Update the entry with the Node ID that it
1595 * corresponds to and the XPRT through which it can be reached.
1596 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001597 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001598 rt_entry = lookup_routing_table(msg->srv.node_id);
1599 if (!rt_entry) {
1600 rt_entry = alloc_routing_table_entry(msg->srv.node_id);
1601 if (!rt_entry) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001602 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001603 pr_err("%s: rt_entry allocation failed\n", __func__);
1604 return -ENOMEM;
1605 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001606 down_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001607 rt_entry->neighbor_node_id = xprt_info->remote_node_id;
1608 rt_entry->xprt_info = xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001609 up_write(&rt_entry->lock_lha4);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001610 add_routing_table_entry(rt_entry);
1611 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001612 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001613
1614 /*
1615 * If the service does not exist already in the database, create and
1616 * store the service info. Create a remote port structure in which
1617 * the service is hosted and cache the security rule for the service
1618 * in that remote port structure.
1619 */
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001620 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001621 server = msm_ipc_router_lookup_server(msg->srv.service,
1622 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1623 if (!server) {
1624 server = msm_ipc_router_create_server(
1625 msg->srv.service, msg->srv.instance,
1626 msg->srv.node_id, msg->srv.port_id, xprt_info);
1627 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001628 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001629 pr_err("%s: Server Create failed\n", __func__);
1630 return -ENOMEM;
1631 }
1632
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001633 down_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001634 if (!msm_ipc_router_lookup_remote_port(
1635 msg->srv.node_id, msg->srv.port_id)) {
1636 rport_ptr = msm_ipc_router_create_remote_port(
1637 msg->srv.node_id, msg->srv.port_id);
1638 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001639 up_read(&routing_table_lock_lha3);
1640 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001641 return -ENOMEM;
1642 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001643 rport_ptr->server = server;
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001644 rport_ptr->sec_rule = msm_ipc_get_security_rule(
1645 msg->srv.service,
1646 msg->srv.instance);
1647 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001648 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001649 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001650 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001651
1652 /*
1653 * Relay the new server message to other subsystems that do not belong
1654 * to the cluster from which this message is received. Notify the
1655 * local clients waiting for this service.
1656 */
1657 relay_msg(xprt_info, pkt);
1658 post_control_ports(pkt);
1659 return 0;
1660}
1661
1662static int process_rmv_server_msg(struct msm_ipc_router_xprt_info *xprt_info,
1663 union rr_control_msg *msg, struct rr_packet *pkt)
1664{
1665 struct msm_ipc_server *server;
1666
1667 RR("o REMOVE_SERVER service=%08x:%d\n",
1668 msg->srv.service, msg->srv.instance);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001669 down_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001670 server = msm_ipc_router_lookup_server(msg->srv.service,
1671 msg->srv.instance, msg->srv.node_id, msg->srv.port_id);
1672 if (server) {
1673 msm_ipc_router_destroy_server(server, msg->srv.node_id,
1674 msg->srv.port_id);
1675 /*
1676 * Relay the new server message to other subsystems that do not
1677 * belong to the cluster from which this message is received.
1678 * Notify the local clients communicating with the service.
1679 */
1680 relay_msg(xprt_info, pkt);
1681 post_control_ports(pkt);
1682 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001683 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001684 return 0;
1685}
1686
1687static int process_rmv_client_msg(struct msm_ipc_router_xprt_info *xprt_info,
1688 union rr_control_msg *msg, struct rr_packet *pkt)
1689{
1690 struct msm_ipc_router_remote_port *rport_ptr;
1691
1692 RR("o REMOVE_CLIENT id=%d:%08x\n", msg->cli.node_id, msg->cli.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001693 down_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001694 rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id,
1695 msg->cli.port_id);
1696 if (rport_ptr)
1697 msm_ipc_router_destroy_remote_port(rport_ptr);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001698 up_write(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001699
1700 relay_msg(xprt_info, pkt);
1701 post_control_ports(pkt);
1702 return 0;
1703}
1704
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705static int process_control_msg(struct msm_ipc_router_xprt_info *xprt_info,
1706 struct rr_packet *pkt)
1707{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 union rr_control_msg *msg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710 struct sk_buff *temp_ptr;
1711 struct rr_header *hdr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001712
1713 if (pkt->length != (IPC_ROUTER_HDR_SIZE + sizeof(*msg))) {
1714 pr_err("%s: r2r msg size %d != %d\n", __func__, pkt->length,
1715 (IPC_ROUTER_HDR_SIZE + sizeof(*msg)));
1716 return -EINVAL;
1717 }
1718
1719 temp_ptr = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001720 if (!temp_ptr) {
1721 pr_err("%s: pkt_fragment_q is empty\n", __func__);
1722 return -EINVAL;
1723 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 hdr = (struct rr_header *)temp_ptr->data;
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001725 if (!hdr) {
1726 pr_err("%s: No data inside the skb\n", __func__);
1727 return -EINVAL;
1728 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 msg = (union rr_control_msg *)((char *)hdr + IPC_ROUTER_HDR_SIZE);
1730
1731 switch (msg->cmd) {
1732 case IPC_ROUTER_CTRL_CMD_HELLO:
Karthikeyan Ramasubramanianb234c242012-10-23 13:12:44 -06001733 rc = process_hello_msg(xprt_info, hdr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 break;
1735 case IPC_ROUTER_CTRL_CMD_RESUME_TX:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001736 rc = process_resume_tx_msg(msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738 case IPC_ROUTER_CTRL_CMD_NEW_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001739 rc = process_new_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 break;
1741 case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001742 rc = process_rmv_server_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 break;
1744 case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT:
Karthikeyan Ramasubramanian4b4120e2013-05-17 18:18:20 -06001745 rc = process_rmv_client_msg(xprt_info, msg, pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 break;
1747 case IPC_ROUTER_CTRL_CMD_PING:
1748 /* No action needed for ping messages received */
1749 RR("o PING\n");
1750 break;
1751 default:
1752 RR("o UNKNOWN(%08x)\n", msg->cmd);
1753 rc = -ENOSYS;
1754 }
1755
1756 return rc;
1757}
1758
1759static void do_read_data(struct work_struct *work)
1760{
1761 struct rr_header *hdr;
1762 struct rr_packet *pkt = NULL;
1763 struct msm_ipc_port *port_ptr;
1764 struct sk_buff *head_skb;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001765 struct msm_ipc_router_remote_port *rport_ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001766
1767 struct msm_ipc_router_xprt_info *xprt_info =
1768 container_of(work,
1769 struct msm_ipc_router_xprt_info,
1770 read_data);
1771
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001772 while ((pkt = rr_read(xprt_info)) != NULL) {
1773 if (pkt->length < IPC_ROUTER_HDR_SIZE ||
1774 pkt->length > MAX_IPC_PKT_SIZE) {
1775 pr_err("%s: Invalid pkt length %d\n",
1776 __func__, pkt->length);
1777 goto fail_data;
1778 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001780 head_skb = skb_peek(pkt->pkt_fragment_q);
1781 if (!head_skb) {
1782 pr_err("%s: head_skb is invalid\n", __func__);
1783 goto fail_data;
1784 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001786 hdr = (struct rr_header *)(head_skb->data);
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06001787 RAW("ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n",
1788 hdr->version, hdr->type, hdr->src_node_id,
1789 hdr->src_port_id, hdr->confirm_rx, hdr->size,
1790 hdr->dst_node_id, hdr->dst_port_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001792 if (hdr->version != IPC_ROUTER_VERSION) {
1793 pr_err("version %d != %d\n",
1794 hdr->version, IPC_ROUTER_VERSION);
1795 goto fail_data;
1796 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001798 if ((hdr->dst_node_id != IPC_ROUTER_NID_LOCAL) &&
1799 ((hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX) ||
1800 (hdr->type == IPC_ROUTER_CTRL_CMD_DATA))) {
1801 forward_msg(xprt_info, pkt);
1802 release_pkt(pkt);
1803 continue;
1804 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001805
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001806 if ((hdr->dst_port_id == IPC_ROUTER_ADDRESS) ||
1807 (hdr->type == IPC_ROUTER_CTRL_CMD_HELLO)) {
1808 process_control_msg(xprt_info, pkt);
1809 release_pkt(pkt);
1810 continue;
1811 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812#if defined(CONFIG_MSM_SMD_LOGGING)
1813#if defined(DEBUG)
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001814 if (msm_ipc_router_debug_mask & SMEM_LOG) {
1815 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05301816 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001817 IPC_ROUTER_LOG_EVENT_RX),
1818 (hdr->src_node_id << 24) |
1819 (hdr->src_port_id & 0xffffff),
1820 (hdr->dst_node_id << 24) |
1821 (hdr->dst_port_id & 0xffffff),
1822 (hdr->type << 24) | (hdr->confirm_rx << 16) |
1823 (hdr->size & 0xffff));
1824 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825#endif
1826#endif
1827
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001828 down_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001829 port_ptr = msm_ipc_router_lookup_local_port(hdr->dst_port_id);
1830 if (!port_ptr) {
1831 pr_err("%s: No local port id %08x\n", __func__,
1832 hdr->dst_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001833 up_read(&local_ports_lock_lha2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001834 release_pkt(pkt);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301835 return;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001836 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001837
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001838 down_read(&routing_table_lock_lha3);
1839 rport_ptr = msm_ipc_router_lookup_remote_port(hdr->src_node_id,
1840 hdr->src_port_id);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001841 if (!rport_ptr) {
1842 rport_ptr = msm_ipc_router_create_remote_port(
1843 hdr->src_node_id,
1844 hdr->src_port_id);
1845 if (!rport_ptr) {
1846 pr_err("%s: Rmt Prt %08x:%08x create failed\n",
1847 __func__, hdr->src_node_id,
1848 hdr->src_port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001849 up_read(&routing_table_lock_lha3);
1850 up_read(&local_ports_lock_lha2);
Zaheerulla Meer6a309de2013-07-12 16:16:30 +05301851 release_pkt(pkt);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05301852 return;
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06001853 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001855 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06001856 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001857 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001859 return;
1860
1861fail_data:
1862 release_pkt(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 pr_err("ipc_router has died\n");
1864}
1865
1866int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr,
1867 struct msm_ipc_addr *name)
1868{
1869 struct msm_ipc_server *server;
1870 unsigned long flags;
1871 union rr_control_msg ctl;
1872
1873 if (!port_ptr || !name)
1874 return -EINVAL;
1875
1876 if (name->addrtype != MSM_IPC_ADDR_NAME)
1877 return -EINVAL;
1878
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001879 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 server = msm_ipc_router_lookup_server(name->addr.port_name.service,
1881 name->addr.port_name.instance,
1882 IPC_ROUTER_NID_LOCAL,
1883 port_ptr->this_port.port_id);
1884 if (server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001885 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 pr_err("%s: Server already present\n", __func__);
1887 return -EINVAL;
1888 }
1889
1890 server = msm_ipc_router_create_server(name->addr.port_name.service,
1891 name->addr.port_name.instance,
1892 IPC_ROUTER_NID_LOCAL,
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06001893 port_ptr->this_port.port_id,
1894 NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001896 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 pr_err("%s: Server Creation failed\n", __func__);
1898 return -EINVAL;
1899 }
1900
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001901 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER;
1903 ctl.srv.service = server->name.service;
1904 ctl.srv.instance = server->name.instance;
1905 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1906 ctl.srv.port_id = port_ptr->this_port.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001907 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 broadcast_ctl_msg(&ctl);
Karthikeyan Ramasubramanianfa807952013-08-06 18:04:12 -06001909 broadcast_ctl_msg_locally(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 spin_lock_irqsave(&port_ptr->port_lock, flags);
1911 port_ptr->type = SERVER_PORT;
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06001912 port_ptr->mode_info.mode = MULTI_LINK_MODE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 port_ptr->port_name.service = server->name.service;
1914 port_ptr->port_name.instance = server->name.instance;
1915 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1916 return 0;
1917}
1918
1919int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr)
1920{
1921 struct msm_ipc_server *server;
1922 unsigned long flags;
1923 union rr_control_msg ctl;
1924
1925 if (!port_ptr)
1926 return -EINVAL;
1927
1928 if (port_ptr->type != SERVER_PORT) {
1929 pr_err("%s: Trying to unregister a non-server port\n",
1930 __func__);
1931 return -EINVAL;
1932 }
1933
1934 if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) {
1935 pr_err("%s: Trying to unregister a remote server locally\n",
1936 __func__);
1937 return -EINVAL;
1938 }
1939
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001940 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941 server = msm_ipc_router_lookup_server(port_ptr->port_name.service,
1942 port_ptr->port_name.instance,
1943 port_ptr->this_port.node_id,
1944 port_ptr->this_port.port_id);
1945 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001946 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947 pr_err("%s: Server lookup failed\n", __func__);
1948 return -ENODEV;
1949 }
1950
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06001951 memset(&ctl, 0, sizeof(ctl));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
1953 ctl.srv.service = server->name.service;
1954 ctl.srv.instance = server->name.instance;
1955 ctl.srv.node_id = IPC_ROUTER_NID_LOCAL;
1956 ctl.srv.port_id = port_ptr->this_port.port_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001957 msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id,
1958 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06001959 up_write(&server_list_lock_lha2);
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06001960 broadcast_ctl_msg(&ctl);
Karthikeyan Ramasubramanianfa807952013-08-06 18:04:12 -06001961 broadcast_ctl_msg_locally(&ctl);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962 spin_lock_irqsave(&port_ptr->port_lock, flags);
1963 port_ptr->type = CLIENT_PORT;
1964 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
1965 return 0;
1966}
1967
1968static int loopback_data(struct msm_ipc_port *src,
1969 uint32_t port_id,
1970 struct sk_buff_head *data)
1971{
1972 struct sk_buff *head_skb;
1973 struct rr_header *hdr;
1974 struct msm_ipc_port *port_ptr;
1975 struct rr_packet *pkt;
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07001976 int ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977
1978 if (!data) {
1979 pr_err("%s: Invalid pkt pointer\n", __func__);
1980 return -EINVAL;
1981 }
1982
1983 pkt = create_pkt(data);
1984 if (!pkt) {
1985 pr_err("%s: New pkt create failed\n", __func__);
1986 return -ENOMEM;
1987 }
1988
1989 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001990 if (!head_skb) {
1991 pr_err("%s: pkt_fragment_q is empty\n", __func__);
Brent Hronik0e83d3b2013-05-01 16:25:00 -06001992 release_pkt(pkt);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07001993 return -EINVAL;
1994 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001995 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
1996 if (!hdr) {
1997 pr_err("%s: Prepend Header failed\n", __func__);
1998 release_pkt(pkt);
1999 return -ENOMEM;
2000 }
2001 hdr->version = IPC_ROUTER_VERSION;
2002 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2003 hdr->src_node_id = src->this_port.node_id;
2004 hdr->src_port_id = src->this_port.port_id;
2005 hdr->size = pkt->length;
2006 hdr->confirm_rx = 0;
2007 hdr->dst_node_id = IPC_ROUTER_NID_LOCAL;
2008 hdr->dst_port_id = port_id;
2009 pkt->length += IPC_ROUTER_HDR_SIZE;
2010
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002011 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002012 port_ptr = msm_ipc_router_lookup_local_port(port_id);
2013 if (!port_ptr) {
2014 pr_err("%s: Local port %d not present\n", __func__, port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002015 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 release_pkt(pkt);
2017 return -ENODEV;
2018 }
2019
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002020 ret_len = pkt->length;
Karthikeyan Ramasubramaniand1af6252013-05-17 15:19:56 -06002021 post_pkt_to_port(port_ptr, pkt, 0);
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002022 update_comm_mode_info(&src->mode_info, NULL);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002023 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024
Karthikeyan Ramasubramanian0a801c12013-02-08 13:07:42 -07002025 return ret_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002026}
2027
2028static int msm_ipc_router_write_pkt(struct msm_ipc_port *src,
2029 struct msm_ipc_router_remote_port *rport_ptr,
2030 struct rr_packet *pkt)
2031{
2032 struct sk_buff *head_skb;
2033 struct rr_header *hdr;
2034 struct msm_ipc_router_xprt_info *xprt_info;
2035 struct msm_ipc_routing_table_entry *rt_entry;
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302036 struct msm_ipc_resume_tx_port *resume_tx_port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002038
2039 if (!rport_ptr || !src || !pkt)
2040 return -EINVAL;
2041
2042 head_skb = skb_peek(pkt->pkt_fragment_q);
Karthikeyan Ramasubramanian9024dd82011-12-19 18:44:19 -07002043 if (!head_skb) {
2044 pr_err("%s: pkt_fragment_q is empty\n", __func__);
2045 return -EINVAL;
2046 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE);
2048 if (!hdr) {
2049 pr_err("%s: Prepend Header failed\n", __func__);
2050 return -ENOMEM;
2051 }
2052 hdr->version = IPC_ROUTER_VERSION;
2053 hdr->type = IPC_ROUTER_CTRL_CMD_DATA;
2054 hdr->src_node_id = src->this_port.node_id;
2055 hdr->src_port_id = src->this_port.port_id;
2056 hdr->size = pkt->length;
2057 hdr->confirm_rx = 0;
2058 hdr->dst_node_id = rport_ptr->node_id;
2059 hdr->dst_port_id = rport_ptr->port_id;
2060 pkt->length += IPC_ROUTER_HDR_SIZE;
2061
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002062 mutex_lock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302063 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) {
2064 if (msm_ipc_router_lookup_resume_tx_port(
2065 rport_ptr, src->this_port.port_id)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002066 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302067 return -EAGAIN;
2068 }
2069 resume_tx_port =
2070 kzalloc(sizeof(struct msm_ipc_resume_tx_port),
2071 GFP_KERNEL);
2072 if (!resume_tx_port) {
2073 pr_err("%s: Resume_Tx port allocation failed\n",
2074 __func__);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002075 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302076 return -ENOMEM;
2077 }
2078 INIT_LIST_HEAD(&resume_tx_port->list);
2079 resume_tx_port->port_id = src->this_port.port_id;
2080 resume_tx_port->node_id = src->this_port.node_id;
2081 list_add_tail(&resume_tx_port->list,
2082 &rport_ptr->resume_tx_port_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002083 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Zaheerulla Meera34fc662013-04-17 01:16:47 +05302084 return -EAGAIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 }
2086 rport_ptr->tx_quota_cnt++;
2087 if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA)
2088 hdr->confirm_rx = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002089 mutex_unlock(&rport_ptr->quota_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002090
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 rt_entry = lookup_routing_table(hdr->dst_node_id);
2092 if (!rt_entry || !rt_entry->xprt_info) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 pr_err("%s: Remote node %d not up\n",
2094 __func__, hdr->dst_node_id);
2095 return -ENODEV;
2096 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002097 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098 xprt_info = rt_entry->xprt_info;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002099 mutex_lock(&xprt_info->tx_lock_lhb2);
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002100 ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002101 mutex_unlock(&xprt_info->tx_lock_lhb2);
2102 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002103
2104 if (ret < 0) {
2105 pr_err("%s: Write on XPRT failed\n", __func__);
2106 return ret;
2107 }
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002108 update_comm_mode_info(&src->mode_info, xprt_info);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002109
2110 RAW_HDR("[w rr_h] "
2111 "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x,"
2112 "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n",
2113 hdr->version, type_to_str(hdr->type),
2114 hdr->src_node_id, hdr->src_port_id,
2115 hdr->confirm_rx, hdr->size,
2116 hdr->dst_node_id, hdr->dst_port_id);
2117
2118#if defined(CONFIG_MSM_SMD_LOGGING)
2119#if defined(DEBUG)
2120 if (msm_ipc_router_debug_mask & SMEM_LOG) {
2121 smem_log_event((SMEM_LOG_PROC_ID_APPS |
Zaheerulla Meer1ee914e2013-06-19 16:31:17 +05302122 SMEM_LOG_IPC_ROUTER_EVENT_BASE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002123 IPC_ROUTER_LOG_EVENT_TX),
2124 (hdr->src_node_id << 24) |
2125 (hdr->src_port_id & 0xffffff),
2126 (hdr->dst_node_id << 24) |
2127 (hdr->dst_port_id & 0xffffff),
2128 (hdr->type << 24) | (hdr->confirm_rx << 16) |
2129 (hdr->size & 0xffff));
2130 }
2131#endif
2132#endif
2133
2134 return pkt->length;
2135}
2136
2137int msm_ipc_router_send_to(struct msm_ipc_port *src,
2138 struct sk_buff_head *data,
2139 struct msm_ipc_addr *dest)
2140{
2141 uint32_t dst_node_id = 0, dst_port_id = 0;
2142 struct msm_ipc_server *server;
2143 struct msm_ipc_server_port *server_port;
2144 struct msm_ipc_router_remote_port *rport_ptr = NULL;
2145 struct rr_packet *pkt;
2146 int ret;
2147
2148 if (!src || !data || !dest) {
2149 pr_err("%s: Invalid Parameters\n", __func__);
2150 return -EINVAL;
2151 }
2152
2153 /* Resolve Address*/
2154 if (dest->addrtype == MSM_IPC_ADDR_ID) {
2155 dst_node_id = dest->addr.port_addr.node_id;
2156 dst_port_id = dest->addr.port_addr.port_id;
2157 } else if (dest->addrtype == MSM_IPC_ADDR_NAME) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002158 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002159 server = msm_ipc_router_lookup_server(
2160 dest->addr.port_name.service,
2161 dest->addr.port_name.instance,
2162 0, 0);
2163 if (!server) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002164 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 pr_err("%s: Destination not reachable\n", __func__);
2166 return -ENODEV;
2167 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 server_port = list_first_entry(&server->server_port_list,
2169 struct msm_ipc_server_port,
2170 list);
2171 dst_node_id = server_port->server_addr.node_id;
2172 dst_port_id = server_port->server_addr.port_id;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002173 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002174 }
2175 if (dst_node_id == IPC_ROUTER_NID_LOCAL) {
2176 ret = loopback_data(src, dst_port_id, data);
2177 return ret;
2178 }
2179
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002180 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id,
2182 dst_port_id);
2183 if (!rport_ptr) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002184 up_read(&routing_table_lock_lha3);
Zaheerulla Meer2c515312013-05-10 15:51:28 +05302185 pr_err("%s: Remote port not found\n", __func__);
2186 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 }
2188
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002189 if (src->check_send_permissions) {
2190 ret = src->check_send_permissions(rport_ptr->sec_rule);
2191 if (ret <= 0) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002192 up_read(&routing_table_lock_lha3);
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06002193 pr_err("%s: permission failure for %s\n",
2194 __func__, current->comm);
2195 return -EPERM;
2196 }
2197 }
2198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199 pkt = create_pkt(data);
2200 if (!pkt) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002201 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202 pr_err("%s: Pkt creation failed\n", __func__);
2203 return -ENOMEM;
2204 }
2205
2206 ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002207 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 release_pkt(pkt);
2209
2210 return ret;
2211}
2212
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002213int msm_ipc_router_send_msg(struct msm_ipc_port *src,
2214 struct msm_ipc_addr *dest,
2215 void *data, unsigned int data_len)
2216{
2217 struct sk_buff_head *out_skb_head;
2218 int ret;
2219
2220 out_skb_head = msm_ipc_router_buf_to_skb(data, data_len);
2221 if (!out_skb_head) {
2222 pr_err("%s: SKB conversion failed\n", __func__);
2223 return -EFAULT;
2224 }
2225
2226 ret = msm_ipc_router_send_to(src, out_skb_head, dest);
Zaheerulla Meerc8400402013-05-08 19:27:27 +05302227 if (ret == -EAGAIN)
2228 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002229 if (ret < 0) {
2230 pr_err("%s: msm_ipc_router_send_to failed - ret: %d\n",
2231 __func__, ret);
2232 msm_ipc_router_free_skb(out_skb_head);
Zaheerulla Meerfc59a8a2013-06-26 22:39:00 +05302233 return ret;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002234 }
2235 return 0;
2236}
2237
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302238/**
2239 * msm_ipc_router_send_resume_tx() - Send Resume_Tx message
2240 * @data: Pointer to received data packet that has confirm_rx bit set
2241 *
2242 * @return: On success, number of bytes transferred is returned, else
2243 * standard linux error code is returned.
2244 *
2245 * This function sends the Resume_Tx event to the remote node that
2246 * sent the data with confirm_rx field set. In case of a multi-hop
2247 * scenario also, this function makes sure that the destination node_id
2248 * to which the resume_tx event should reach is right.
2249 */
2250static int msm_ipc_router_send_resume_tx(void *data)
2251{
2252 union rr_control_msg msg;
2253 struct rr_header *hdr = (struct rr_header *)data;
2254 struct msm_ipc_routing_table_entry *rt_entry;
2255 int ret;
2256
2257 memset(&msg, 0, sizeof(msg));
2258 msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX;
2259 msg.cli.node_id = hdr->dst_node_id;
2260 msg.cli.port_id = hdr->dst_port_id;
2261 down_read(&routing_table_lock_lha3);
2262 rt_entry = lookup_routing_table(hdr->src_node_id);
2263 if (!rt_entry) {
2264 pr_err("%s: %d Node is not present",
2265 __func__, hdr->src_node_id);
2266 up_read(&routing_table_lock_lha3);
2267 return -ENODEV;
2268 }
2269 RR("x RESUME_TX id=%d:%08x\n",
2270 msg.cli.node_id, msg.cli.port_id);
2271 ret = msm_ipc_router_send_control_msg(rt_entry->xprt_info, &msg,
2272 hdr->src_node_id);
2273 up_read(&routing_table_lock_lha3);
2274 if (ret < 0)
2275 pr_err("%s: Send Resume_Tx Failed SRC_NODE: %d SRC_PORT: %d DEST_NODE: %d",
2276 __func__, hdr->dst_node_id, hdr->dst_port_id,
2277 hdr->src_node_id);
2278
2279 return ret;
2280}
2281
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282int msm_ipc_router_read(struct msm_ipc_port *port_ptr,
2283 struct sk_buff_head **data,
2284 size_t buf_len)
2285{
2286 struct rr_packet *pkt;
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302287 struct sk_buff *head_skb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002288 int ret;
2289
2290 if (!port_ptr || !data)
2291 return -EINVAL;
2292
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002293 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002294 if (list_empty(&port_ptr->port_rx_q)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002295 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 return -EAGAIN;
2297 }
2298
2299 pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list);
2300 if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002301 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002302 return -ETOOSMALL;
2303 }
2304 list_del(&pkt->list);
2305 if (list_empty(&port_ptr->port_rx_q))
2306 wake_unlock(&port_ptr->port_rx_wake_lock);
2307 *data = pkt->pkt_fragment_q;
2308 ret = pkt->length;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002309 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Zaheerulla Meer3ce2dfb2013-07-11 19:24:40 +05302310 kfree(pkt);
2311 head_skb = skb_peek(*data);
2312 if (!head_skb) {
2313 pr_err("%s: Socket Buffer not found", __func__);
2314 return -EFAULT;
2315 }
2316 if (((struct rr_header *)(head_skb->data))->confirm_rx)
2317 msm_ipc_router_send_resume_tx((void *)(head_skb->data));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002318
2319 return ret;
2320}
2321
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302322/**
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302323 * msm_ipc_router_rx_data_wait() - Wait for new message destined to a local port.
2324 * @port_ptr: Pointer to the local port
2325 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2326 * > 0 timeout indicates the wait time.
2327 * 0 indicates that we do not wait.
2328 * @return: 0 if there are pending messages to read,
2329 * standard Linux error code otherwise.
2330 *
2331 * Checks for the availability of messages that are destined to a local port.
2332 * If no messages are present then waits as per @timeout.
2333 */
2334int msm_ipc_router_rx_data_wait(struct msm_ipc_port *port_ptr, long timeout)
2335{
2336 int ret = 0;
2337
2338 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2339 while (list_empty(&port_ptr->port_rx_q)) {
2340 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2341 if (timeout < 0) {
2342 ret = wait_event_interruptible(
2343 port_ptr->port_rx_wait_q,
2344 !list_empty(&port_ptr->port_rx_q));
2345 if (ret)
2346 return ret;
2347 } else if (timeout > 0) {
2348 timeout = wait_event_interruptible_timeout(
2349 port_ptr->port_rx_wait_q,
2350 !list_empty(&port_ptr->port_rx_q),
2351 timeout);
2352 if (timeout < 0)
2353 return -EFAULT;
2354 }
2355 if (timeout == 0)
2356 return -ENOMSG;
2357 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
2358 }
2359 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
2360
2361 return ret;
2362}
2363
2364/**
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302365 * msm_ipc_router_recv_from() - Recieve messages destined to a local port.
2366 * @port_ptr: Pointer to the local port
2367 * @data : Pointer to the socket buffer head
2368 * @src: Pointer to local port address
2369 * @timeout: < 0 timeout indicates infinite wait till a message arrives.
2370 * > 0 timeout indicates the wait time.
2371 * 0 indicates that we do not wait.
2372 * @return: = Number of bytes read(On successful read operation).
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302373 * = -ENOMSG (If there are no pending messages and timeout is 0).
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302374 * = -EINVAL (If either of the arguments, port_ptr or data is invalid)
2375 * = -EFAULT (If there are no pending messages when timeout is > 0
2376 * and the wait_event_interruptible_timeout has returned value > 0)
2377 * = -ERESTARTSYS (If there are no pending messages when timeout
2378 * is < 0 and wait_event_interruptible was interrupted by a signal)
2379 *
2380 * This function reads the messages that are destined for a local port. It
2381 * is used by modules that exist with-in the kernel and use IPC Router for
2382 * transport. The function checks if there are any messages that are already
2383 * received. If yes, it reads them, else it waits as per the timeout value.
2384 * On a successful read, the return value of the function indicates the number
2385 * of bytes that are read.
2386 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr,
2388 struct sk_buff_head **data,
2389 struct msm_ipc_addr *src,
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002390 long timeout)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002391{
2392 int ret, data_len, align_size;
2393 struct sk_buff *temp_skb;
2394 struct rr_header *hdr = NULL;
2395
2396 if (!port_ptr || !data) {
2397 pr_err("%s: Invalid pointers being passed\n", __func__);
2398 return -EINVAL;
2399 }
2400
2401 *data = NULL;
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302402
2403 ret = msm_ipc_router_rx_data_wait(port_ptr, timeout);
2404 if (ret)
2405 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002406
2407 ret = msm_ipc_router_read(port_ptr, data, 0);
2408 if (ret <= 0 || !(*data))
2409 return ret;
2410
2411 temp_skb = skb_peek(*data);
2412 hdr = (struct rr_header *)(temp_skb->data);
2413 if (src) {
2414 src->addrtype = MSM_IPC_ADDR_ID;
2415 src->addr.port_addr.node_id = hdr->src_node_id;
2416 src->addr.port_addr.port_id = hdr->src_port_id;
2417 }
2418
2419 data_len = hdr->size;
2420 skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE);
2421 align_size = ALIGN_SIZE(data_len);
2422 if (align_size) {
2423 temp_skb = skb_peek_tail(*data);
2424 skb_trim(temp_skb, (temp_skb->len - align_size));
2425 }
2426 return data_len;
2427}
2428
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002429int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
2430 struct msm_ipc_addr *src,
2431 unsigned char **data,
2432 unsigned int *len)
2433{
2434 struct sk_buff_head *in_skb_head;
2435 int ret;
2436
Zaheerulla Meer20e84262013-05-22 23:31:59 +05302437 ret = msm_ipc_router_recv_from(port_ptr, &in_skb_head, src, 0);
2438
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002439 if (ret < 0) {
Arun Kumar Neelakantam0f73f432013-06-21 17:57:18 +05302440 if (ret != -ENOMSG)
2441 pr_err("%s: msm_ipc_router_recv_from failed - ret: %d\n",
2442 __func__, ret);
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002443 return ret;
2444 }
2445
2446 *data = msm_ipc_router_skb_to_buf(in_skb_head, ret);
2447 if (!(*data))
2448 pr_err("%s: Buf conversion failed\n", __func__);
2449
2450 *len = ret;
2451 msm_ipc_router_free_skb(in_skb_head);
2452 return 0;
2453}
2454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002455struct msm_ipc_port *msm_ipc_router_create_port(
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002456 void (*notify)(unsigned event, void *priv),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457 void *priv)
2458{
2459 struct msm_ipc_port *port_ptr;
Karthikeyan Ramasubramanian70444192012-07-12 10:25:49 -06002460 int ret;
2461
2462 ret = wait_for_completion_interruptible(&msm_ipc_local_router_up);
2463 if (ret < 0) {
2464 pr_err("%s: Error waiting for local router\n", __func__);
2465 return NULL;
2466 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467
2468 port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv);
2469 if (!port_ptr)
2470 pr_err("%s: port_ptr alloc failed\n", __func__);
2471
2472 return port_ptr;
2473}
2474
2475int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
2476{
2477 union rr_control_msg msg;
2478 struct rr_packet *pkt, *temp_pkt;
2479 struct msm_ipc_server *server;
2480
2481 if (!port_ptr)
2482 return -EINVAL;
2483
2484 if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002485 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002486 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002487 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002489 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramanian45008a62013-07-19 15:58:38 -06002490 memset(&msg, 0, sizeof(msg));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491 msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER;
2492 msg.srv.service = port_ptr->port_name.service;
2493 msg.srv.instance = port_ptr->port_name.instance;
2494 msg.srv.node_id = port_ptr->this_port.node_id;
2495 msg.srv.port_id = port_ptr->this_port.port_id;
2496 RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n",
2497 msg.srv.service, msg.srv.instance,
2498 msg.srv.node_id, msg.srv.port_id);
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002499 broadcast_ctl_msg(&msg);
2500 broadcast_ctl_msg_locally(&msg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501 }
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002502
2503 /*
2504 * Server port could have been a client port earlier.
2505 * Send REMOVE_CLIENT message in either case.
2506 */
Karthikeyan Ramasubramanian25e91872013-01-10 17:09:13 -07002507 RR("x REMOVE_CLIENT id=%d:%08x\n",
Karthikeyan Ramasubramaniandc9c4442013-05-02 17:25:54 -06002508 port_ptr->this_port.node_id, port_ptr->this_port.port_id);
2509 msm_ipc_router_send_remove_client(&port_ptr->mode_info,
2510 port_ptr->this_port.node_id,
2511 port_ptr->this_port.port_id);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002512 } else if (port_ptr->type == CONTROL_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002513 down_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramanianbe9954b2012-06-13 12:59:54 -06002514 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002515 up_write(&control_ports_lock_lha5);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002516 } else if (port_ptr->type == IRSC_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002517 down_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002518 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002519 up_write(&local_ports_lock_lha2);
Karthikeyan Ramasubramaniand6dbfae2013-01-16 09:00:28 -07002520 signal_irsc_completion();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 }
2522
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002523 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524 list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) {
2525 list_del(&pkt->list);
2526 release_pkt(pkt);
2527 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002528 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530 if (port_ptr->type == SERVER_PORT) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002531 down_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 server = msm_ipc_router_lookup_server(
2533 port_ptr->port_name.service,
2534 port_ptr->port_name.instance,
2535 port_ptr->this_port.node_id,
2536 port_ptr->this_port.port_id);
2537 if (server)
2538 msm_ipc_router_destroy_server(server,
2539 port_ptr->this_port.node_id,
2540 port_ptr->this_port.port_id);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002541 up_write(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002542 }
2543
Karthikeyan Ramasubramaniandd8c3b52011-11-30 16:26:12 -07002544 wake_lock_destroy(&port_ptr->port_rx_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545 kfree(port_ptr);
2546 return 0;
2547}
2548
2549int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
2550{
2551 struct rr_packet *pkt;
2552 int rc = 0;
2553
2554 if (!port_ptr)
2555 return -EINVAL;
2556
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002557 mutex_lock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558 if (!list_empty(&port_ptr->port_rx_q)) {
2559 pkt = list_first_entry(&port_ptr->port_rx_q,
2560 struct rr_packet, list);
2561 rc = pkt->length;
2562 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002563 mutex_unlock(&port_ptr->port_rx_q_lock_lhb3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002564
2565 return rc;
2566}
2567
2568int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr)
2569{
2570 if (!port_ptr)
2571 return -EINVAL;
2572
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002573 down_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574 list_del(&port_ptr->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002575 up_write(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576 port_ptr->type = CONTROL_PORT;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002577 down_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002578 list_add_tail(&port_ptr->list, &control_ports);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002579 up_write(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580
2581 return 0;
2582}
2583
2584int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002585 struct msm_ipc_server_info *srv_info,
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002586 int num_entries_in_array,
2587 uint32_t lookup_mask)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588{
2589 struct msm_ipc_server *server;
2590 struct msm_ipc_server_port *server_port;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002591 int key, i = 0; /*num_entries_found*/
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592
2593 if (!srv_name) {
2594 pr_err("%s: Invalid srv_name\n", __func__);
2595 return -EINVAL;
2596 }
2597
Karthikeyan Ramasubramaniandfde01b2012-06-12 14:25:13 -06002598 if (num_entries_in_array && !srv_info) {
2599 pr_err("%s: srv_info NULL\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 return -EINVAL;
2601 }
2602
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002603 down_read(&server_list_lock_lha2);
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002604 if (!lookup_mask)
2605 lookup_mask = 0xFFFFFFFF;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002606 key = (srv_name->service & (SRV_HASH_SIZE - 1));
2607 list_for_each_entry(server, &server_list[key], list) {
2608 if ((server->name.service != srv_name->service) ||
2609 ((server->name.instance & lookup_mask) !=
2610 srv_name->instance))
2611 continue;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002612
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002613 list_for_each_entry(server_port,
2614 &server->server_port_list, list) {
2615 if (i < num_entries_in_array) {
2616 srv_info[i].node_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002617 server_port->server_addr.node_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002618 srv_info[i].port_id =
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002619 server_port->server_addr.port_id;
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002620 srv_info[i].service = server->name.service;
2621 srv_info[i].instance = server->name.instance;
Karthikeyan Ramasubramaniancc450c92011-07-27 14:38:15 -06002622 }
Karthikeyan Ramasubramaniana85d09f2012-10-25 15:40:45 -06002623 i++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002624 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002626 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627
2628 return i;
2629}
2630
2631int msm_ipc_router_close(void)
2632{
2633 struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info;
2634
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002635 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 list_for_each_entry_safe(xprt_info, tmp_xprt_info,
2637 &xprt_info_list, list) {
Karthikeyan Ramasubramanian8cec5922012-02-16 17:41:58 -07002638 xprt_info->xprt->close(xprt_info->xprt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002639 list_del(&xprt_info->list);
2640 kfree(xprt_info);
2641 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002642 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002643 return 0;
2644}
2645
2646#if defined(CONFIG_DEBUG_FS)
2647static int dump_routing_table(char *buf, int max)
2648{
2649 int i = 0, j;
2650 struct msm_ipc_routing_table_entry *rt_entry;
2651
2652 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002653 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002655 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 i += scnprintf(buf + i, max - i,
2657 "Node Id: 0x%08x\n", rt_entry->node_id);
Karthikeyan Ramasubramanianc1a4e3a2012-09-10 16:10:24 -06002658 if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659 i += scnprintf(buf + i, max - i,
2660 "XPRT Name: Loopback\n");
2661 i += scnprintf(buf + i, max - i,
2662 "Next Hop: %d\n", rt_entry->node_id);
2663 } else {
2664 i += scnprintf(buf + i, max - i,
2665 "XPRT Name: %s\n",
2666 rt_entry->xprt_info->xprt->name);
2667 i += scnprintf(buf + i, max - i,
2668 "Next Hop: 0x%08x\n",
2669 rt_entry->xprt_info->remote_node_id);
2670 }
2671 i += scnprintf(buf + i, max - i, "\n");
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002672 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002673 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002674 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002675 }
2676
2677 return i;
2678}
2679
2680static int dump_xprt_info(char *buf, int max)
2681{
2682 int i = 0;
2683 struct msm_ipc_router_xprt_info *xprt_info;
2684
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002685 down_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002686 list_for_each_entry(xprt_info, &xprt_info_list, list) {
2687 i += scnprintf(buf + i, max - i, "XPRT Name: %s\n",
2688 xprt_info->xprt->name);
2689 i += scnprintf(buf + i, max - i, "Link Id: %d\n",
2690 xprt_info->xprt->link_id);
2691 i += scnprintf(buf + i, max - i, "Initialized: %s\n",
2692 (xprt_info->initialized ? "Y" : "N"));
2693 i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n",
2694 xprt_info->remote_node_id);
2695 i += scnprintf(buf + i, max - i, "\n");
2696 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002697 up_read(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002698
2699 return i;
2700}
2701
2702static int dump_servers(char *buf, int max)
2703{
2704 int i = 0, j;
2705 struct msm_ipc_server *server;
2706 struct msm_ipc_server_port *server_port;
2707
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002708 down_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002709 for (j = 0; j < SRV_HASH_SIZE; j++) {
2710 list_for_each_entry(server, &server_list[j], list) {
2711 list_for_each_entry(server_port,
2712 &server->server_port_list,
2713 list) {
2714 i += scnprintf(buf + i, max - i, "Service: "
2715 "0x%08x\n", server->name.service);
2716 i += scnprintf(buf + i, max - i, "Instance: "
2717 "0x%08x\n", server->name.instance);
2718 i += scnprintf(buf + i, max - i,
2719 "Node_id: 0x%08x\n",
2720 server_port->server_addr.node_id);
2721 i += scnprintf(buf + i, max - i,
2722 "Port_id: 0x%08x\n",
2723 server_port->server_addr.port_id);
2724 i += scnprintf(buf + i, max - i, "\n");
2725 }
2726 }
2727 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002728 up_read(&server_list_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002729
2730 return i;
2731}
2732
2733static int dump_remote_ports(char *buf, int max)
2734{
2735 int i = 0, j, k;
2736 struct msm_ipc_router_remote_port *rport_ptr;
2737 struct msm_ipc_routing_table_entry *rt_entry;
2738
2739 for (j = 0; j < RT_HASH_SIZE; j++) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002740 down_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002741 list_for_each_entry(rt_entry, &routing_table[j], list) {
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002742 down_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002743 for (k = 0; k < RP_HASH_SIZE; k++) {
2744 list_for_each_entry(rport_ptr,
2745 &rt_entry->remote_port_list[k],
2746 list) {
2747 i += scnprintf(buf + i, max - i,
2748 "Node_id: 0x%08x\n",
2749 rport_ptr->node_id);
2750 i += scnprintf(buf + i, max - i,
2751 "Port_id: 0x%08x\n",
2752 rport_ptr->port_id);
2753 i += scnprintf(buf + i, max - i,
2754 "Quota_cnt: %d\n",
2755 rport_ptr->tx_quota_cnt);
2756 i += scnprintf(buf + i, max - i, "\n");
2757 }
2758 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002759 up_read(&rt_entry->lock_lha4);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002760 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002761 up_read(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002762 }
2763
2764 return i;
2765}
2766
2767static int dump_control_ports(char *buf, int max)
2768{
2769 int i = 0;
2770 struct msm_ipc_port *port_ptr;
2771
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002772 down_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002773 list_for_each_entry(port_ptr, &control_ports, list) {
2774 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2775 port_ptr->this_port.node_id);
2776 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2777 port_ptr->this_port.port_id);
2778 i += scnprintf(buf + i, max - i, "\n");
2779 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002780 up_read(&control_ports_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002781
2782 return i;
2783}
2784
2785static int dump_local_ports(char *buf, int max)
2786{
2787 int i = 0, j;
2788 unsigned long flags;
2789 struct msm_ipc_port *port_ptr;
2790
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002791 down_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002792 for (j = 0; j < LP_HASH_SIZE; j++) {
2793 list_for_each_entry(port_ptr, &local_ports[j], list) {
2794 spin_lock_irqsave(&port_ptr->port_lock, flags);
2795 i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n",
2796 port_ptr->this_port.node_id);
2797 i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n",
2798 port_ptr->this_port.port_id);
2799 i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n",
2800 port_ptr->num_tx);
2801 i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n",
2802 port_ptr->num_rx);
2803 i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n",
2804 port_ptr->num_tx_bytes);
2805 i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n",
2806 port_ptr->num_rx_bytes);
2807 spin_unlock_irqrestore(&port_ptr->port_lock, flags);
2808 i += scnprintf(buf + i, max - i, "\n");
2809 }
2810 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002811 up_read(&local_ports_lock_lha2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002812
2813 return i;
2814}
2815
2816#define DEBUG_BUFMAX 4096
2817static char debug_buffer[DEBUG_BUFMAX];
2818
2819static ssize_t debug_read(struct file *file, char __user *buf,
2820 size_t count, loff_t *ppos)
2821{
2822 int (*fill)(char *buf, int max) = file->private_data;
2823 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
2824 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
2825}
2826
2827static int debug_open(struct inode *inode, struct file *file)
2828{
2829 file->private_data = inode->i_private;
2830 return 0;
2831}
2832
2833static const struct file_operations debug_ops = {
2834 .read = debug_read,
2835 .open = debug_open,
2836};
2837
2838static void debug_create(const char *name, mode_t mode,
2839 struct dentry *dent,
2840 int (*fill)(char *buf, int max))
2841{
2842 debugfs_create_file(name, mode, dent, fill, &debug_ops);
2843}
2844
2845static void debugfs_init(void)
2846{
2847 struct dentry *dent;
2848
2849 dent = debugfs_create_dir("msm_ipc_router", 0);
2850 if (IS_ERR(dent))
2851 return;
2852
2853 debug_create("dump_local_ports", 0444, dent,
2854 dump_local_ports);
2855 debug_create("dump_remote_ports", 0444, dent,
2856 dump_remote_ports);
2857 debug_create("dump_control_ports", 0444, dent,
2858 dump_control_ports);
2859 debug_create("dump_servers", 0444, dent,
2860 dump_servers);
2861 debug_create("dump_xprt_info", 0444, dent,
2862 dump_xprt_info);
2863 debug_create("dump_routing_table", 0444, dent,
2864 dump_routing_table);
2865}
2866
2867#else
2868static void debugfs_init(void) {}
2869#endif
2870
2871static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt)
2872{
2873 struct msm_ipc_router_xprt_info *xprt_info;
2874 struct msm_ipc_routing_table_entry *rt_entry;
2875
2876 xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info),
2877 GFP_KERNEL);
2878 if (!xprt_info)
2879 return -ENOMEM;
2880
2881 xprt_info->xprt = xprt;
2882 xprt_info->initialized = 0;
2883 xprt_info->remote_node_id = -1;
2884 INIT_LIST_HEAD(&xprt_info->pkt_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002885 mutex_init(&xprt_info->rx_lock_lhb2);
2886 mutex_init(&xprt_info->tx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887 wake_lock_init(&xprt_info->wakelock,
2888 WAKE_LOCK_SUSPEND, xprt->name);
2889 xprt_info->need_len = 0;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002890 xprt_info->abort_data_read = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002891 INIT_WORK(&xprt_info->read_data, do_read_data);
2892 INIT_LIST_HEAD(&xprt_info->list);
2893
2894 xprt_info->workqueue = create_singlethread_workqueue(xprt->name);
2895 if (!xprt_info->workqueue) {
2896 kfree(xprt_info);
2897 return -ENOMEM;
2898 }
2899
2900 if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) {
2901 xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL;
2902 xprt_info->initialized = 1;
2903 }
2904
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002905 down_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002906 list_add_tail(&xprt_info->list, &xprt_info_list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002907 up_write(&xprt_info_list_lock_lha5);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002908
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002909 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002910 if (!routing_table_inited) {
2911 init_routing_table();
2912 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
2913 add_routing_table_entry(rt_entry);
2914 routing_table_inited = 1;
2915 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002916 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002918 xprt->priv = xprt_info;
2919
2920 return 0;
2921}
2922
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002923static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt)
2924{
2925 struct msm_ipc_router_xprt_info *xprt_info;
2926
2927 if (xprt && xprt->priv) {
2928 xprt_info = xprt->priv;
2929
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002930 mutex_lock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002931 xprt_info->abort_data_read = 1;
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002932 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002933
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002934 down_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002935 list_del(&xprt_info->list);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002936 up_write(&xprt_info_list_lock_lha5);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002937
2938 flush_workqueue(xprt_info->workqueue);
2939 destroy_workqueue(xprt_info->workqueue);
2940 wake_lock_destroy(&xprt_info->wakelock);
2941
2942 xprt->priv = 0;
2943 kfree(xprt_info);
2944 }
2945}
2946
2947
2948struct msm_ipc_router_xprt_work {
2949 struct msm_ipc_router_xprt *xprt;
2950 struct work_struct work;
2951};
2952
2953static void xprt_open_worker(struct work_struct *work)
2954{
2955 struct msm_ipc_router_xprt_work *xprt_work =
2956 container_of(work, struct msm_ipc_router_xprt_work, work);
2957
2958 msm_ipc_router_add_xprt(xprt_work->xprt);
2959 kfree(xprt_work);
2960}
2961
2962static void xprt_close_worker(struct work_struct *work)
2963{
2964 struct msm_ipc_router_xprt_work *xprt_work =
2965 container_of(work, struct msm_ipc_router_xprt_work, work);
2966
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06002967 msm_ipc_cleanup_routing_table(xprt_work->xprt->priv);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002968 msm_ipc_router_remove_xprt(xprt_work->xprt);
Zaheerulla Meer35893a62013-06-19 16:54:44 +05302969 xprt_work->xprt->sft_close_done(xprt_work->xprt);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002970 kfree(xprt_work);
2971}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002972
2973void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt,
2974 unsigned event,
2975 void *data)
2976{
2977 struct msm_ipc_router_xprt_info *xprt_info = xprt->priv;
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002978 struct msm_ipc_router_xprt_work *xprt_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002979 struct rr_packet *pkt;
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002980 unsigned long ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002981
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06002982 if (!msm_ipc_router_workqueue) {
2983 ret = wait_for_completion_timeout(&msm_ipc_local_router_up,
2984 IPC_ROUTER_INIT_TIMEOUT);
2985 if (!ret || !msm_ipc_router_workqueue) {
2986 pr_err("%s: IPC Router not initialized\n", __func__);
2987 return;
2988 }
2989 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06002990
2991 switch (event) {
2992 case IPC_ROUTER_XPRT_EVENT_OPEN:
2993 D("open event for '%s'\n", xprt->name);
2994 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
2995 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06002996 if (xprt_work) {
2997 xprt_work->xprt = xprt;
2998 INIT_WORK(&xprt_work->work, xprt_open_worker);
2999 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
3000 } else {
3001 pr_err("%s: malloc failure - Couldn't notify OPEN event",
3002 __func__);
3003 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003004 break;
3005
3006 case IPC_ROUTER_XPRT_EVENT_CLOSE:
3007 D("close event for '%s'\n", xprt->name);
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003008 xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work),
3009 GFP_ATOMIC);
Karthikeyan Ramasubramanianc51456c2013-05-16 15:51:29 -06003010 if (xprt_work) {
3011 xprt_work->xprt = xprt;
3012 INIT_WORK(&xprt_work->work, xprt_close_worker);
3013 queue_work(msm_ipc_router_workqueue, &xprt_work->work);
3014 } else {
3015 pr_err("%s: malloc failure - Couldn't notify CLOSE event",
3016 __func__);
3017 }
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003018 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003019 }
3020
3021 if (!data)
3022 return;
3023
3024 while (!xprt_info) {
3025 msleep(100);
3026 xprt_info = xprt->priv;
3027 }
3028
3029 pkt = clone_pkt((struct rr_packet *)data);
3030 if (!pkt)
3031 return;
3032
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003033 mutex_lock(&xprt_info->rx_lock_lhb2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003034 list_add_tail(&pkt->list, &xprt_info->pkt_list);
3035 wake_lock(&xprt_info->wakelock);
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003036 mutex_unlock(&xprt_info->rx_lock_lhb2);
Karthikeyan Ramasubramanian872ecd82012-07-25 11:07:48 -06003037 queue_work(xprt_info->workqueue, &xprt_info->read_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038}
3039
3040static int __init msm_ipc_router_init(void)
3041{
3042 int i, ret;
3043 struct msm_ipc_routing_table_entry *rt_entry;
3044
3045 msm_ipc_router_debug_mask |= SMEM_LOG;
Karthikeyan Ramasubramanian682ebcc2013-03-22 10:47:20 -06003046 ipc_rtr_log_ctxt = ipc_log_context_create(IPC_RTR_LOG_PAGES,
3047 "ipc_router");
3048 if (!ipc_rtr_log_ctxt)
3049 pr_err("%s: Unable to create IPC logging for IPC RTR",
3050 __func__);
3051
Karthikeyan Ramasubramanianff6fbae2011-06-09 11:13:19 -06003052 msm_ipc_router_workqueue =
3053 create_singlethread_workqueue("msm_ipc_router");
3054 if (!msm_ipc_router_workqueue)
3055 return -ENOMEM;
3056
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003057 debugfs_init();
3058
3059 for (i = 0; i < SRV_HASH_SIZE; i++)
3060 INIT_LIST_HEAD(&server_list[i]);
3061
3062 for (i = 0; i < LP_HASH_SIZE; i++)
3063 INIT_LIST_HEAD(&local_ports[i]);
3064
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003065 down_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003066 if (!routing_table_inited) {
3067 init_routing_table();
3068 rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL);
3069 add_routing_table_entry(rt_entry);
3070 routing_table_inited = 1;
3071 }
Karthikeyan Ramasubramaniand06fe072013-05-21 11:49:21 -06003072 up_write(&routing_table_lock_lha3);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003073
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003074 ret = msm_ipc_router_init_sockets();
3075 if (ret < 0)
3076 pr_err("%s: Init sockets failed\n", __func__);
3077
Karthikeyan Ramasubramanian1668cc62012-09-23 22:23:36 -06003078 ret = msm_ipc_router_security_init();
3079 if (ret < 0)
3080 pr_err("%s: Security Init failed\n", __func__);
3081
Karthikeyan Ramasubramanian4af9f7c2011-09-30 15:55:18 -06003082 complete_all(&msm_ipc_local_router_up);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083 return ret;
3084}
3085
3086module_init(msm_ipc_router_init);
3087MODULE_DESCRIPTION("MSM IPC Router");
3088MODULE_LICENSE("GPL v2");