Karthikeyan Ramasubramanian | 25e9187 | 2013-01-10 17:09:13 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 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> |
| 30 | |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/byteorder.h> |
| 33 | |
| 34 | #include <mach/smem_log.h> |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 35 | #include <mach/subsystem_notif.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | |
| 37 | #include "ipc_router.h" |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 38 | #include "modem_notifier.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | |
| 40 | enum { |
| 41 | SMEM_LOG = 1U << 0, |
| 42 | RTR_DBG = 1U << 1, |
| 43 | R2R_MSG = 1U << 2, |
| 44 | R2R_RAW = 1U << 3, |
| 45 | NTFY_MSG = 1U << 4, |
| 46 | R2R_RAW_HDR = 1U << 5, |
| 47 | }; |
| 48 | |
| 49 | static int msm_ipc_router_debug_mask; |
| 50 | module_param_named(debug_mask, msm_ipc_router_debug_mask, |
| 51 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 52 | |
| 53 | #define DIAG(x...) pr_info("[RR] ERROR " x) |
| 54 | |
| 55 | #if defined(DEBUG) |
| 56 | #define D(x...) do { \ |
| 57 | if (msm_ipc_router_debug_mask & RTR_DBG) \ |
| 58 | pr_info(x); \ |
| 59 | } while (0) |
| 60 | |
| 61 | #define RR(x...) do { \ |
| 62 | if (msm_ipc_router_debug_mask & R2R_MSG) \ |
| 63 | pr_info("[RR] "x); \ |
| 64 | } while (0) |
| 65 | |
| 66 | #define RAW(x...) do { \ |
| 67 | if (msm_ipc_router_debug_mask & R2R_RAW) \ |
| 68 | pr_info("[RAW] "x); \ |
| 69 | } while (0) |
| 70 | |
| 71 | #define NTFY(x...) do { \ |
| 72 | if (msm_ipc_router_debug_mask & NTFY_MSG) \ |
| 73 | pr_info("[NOTIFY] "x); \ |
| 74 | } while (0) |
| 75 | |
| 76 | #define RAW_HDR(x...) do { \ |
| 77 | if (msm_ipc_router_debug_mask & R2R_RAW_HDR) \ |
| 78 | pr_info("[HDR] "x); \ |
| 79 | } while (0) |
| 80 | #else |
| 81 | #define D(x...) do { } while (0) |
| 82 | #define RR(x...) do { } while (0) |
| 83 | #define RAW(x...) do { } while (0) |
| 84 | #define RAW_HDR(x...) do { } while (0) |
| 85 | #define NTFY(x...) do { } while (0) |
| 86 | #endif |
| 87 | |
| 88 | #define IPC_ROUTER_LOG_EVENT_ERROR 0x10 |
| 89 | #define IPC_ROUTER_LOG_EVENT_TX 0x11 |
| 90 | #define IPC_ROUTER_LOG_EVENT_RX 0x12 |
| 91 | |
| 92 | static LIST_HEAD(control_ports); |
| 93 | static DEFINE_MUTEX(control_ports_lock); |
| 94 | |
| 95 | #define LP_HASH_SIZE 32 |
| 96 | static struct list_head local_ports[LP_HASH_SIZE]; |
| 97 | static DEFINE_MUTEX(local_ports_lock); |
| 98 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 99 | /* |
| 100 | * Server info is organized as a hash table. The server's service ID is |
| 101 | * used to index into the hash table. The instance ID of most of the servers |
| 102 | * are 1 or 2. The service IDs are well distributed compared to the instance |
| 103 | * IDs and hence choosing service ID to index into this hash table optimizes |
| 104 | * the hash table operations like add, lookup, destroy. |
| 105 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 106 | #define SRV_HASH_SIZE 32 |
| 107 | static struct list_head server_list[SRV_HASH_SIZE]; |
| 108 | static DEFINE_MUTEX(server_list_lock); |
| 109 | static wait_queue_head_t newserver_wait; |
| 110 | |
| 111 | struct msm_ipc_server { |
| 112 | struct list_head list; |
| 113 | struct msm_ipc_port_name name; |
| 114 | struct list_head server_port_list; |
| 115 | }; |
| 116 | |
| 117 | struct msm_ipc_server_port { |
| 118 | struct list_head list; |
| 119 | struct msm_ipc_port_addr server_addr; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 120 | struct msm_ipc_router_xprt_info *xprt_info; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | #define RP_HASH_SIZE 32 |
| 124 | struct msm_ipc_router_remote_port { |
| 125 | struct list_head list; |
| 126 | uint32_t node_id; |
| 127 | uint32_t port_id; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 128 | uint32_t restart_state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | wait_queue_head_t quota_wait; |
| 130 | uint32_t tx_quota_cnt; |
| 131 | struct mutex quota_lock; |
| 132 | }; |
| 133 | |
| 134 | struct msm_ipc_router_xprt_info { |
| 135 | struct list_head list; |
| 136 | struct msm_ipc_router_xprt *xprt; |
| 137 | uint32_t remote_node_id; |
| 138 | uint32_t initialized; |
| 139 | struct list_head pkt_list; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | struct wake_lock wakelock; |
| 141 | struct mutex rx_lock; |
| 142 | struct mutex tx_lock; |
| 143 | uint32_t need_len; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 144 | uint32_t abort_data_read; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 145 | struct work_struct read_data; |
| 146 | struct workqueue_struct *workqueue; |
| 147 | }; |
| 148 | |
| 149 | #define RT_HASH_SIZE 4 |
| 150 | struct msm_ipc_routing_table_entry { |
| 151 | struct list_head list; |
| 152 | uint32_t node_id; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 153 | uint32_t neighbor_node_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | struct list_head remote_port_list[RP_HASH_SIZE]; |
| 155 | struct msm_ipc_router_xprt_info *xprt_info; |
| 156 | struct mutex lock; |
| 157 | unsigned long num_tx_bytes; |
| 158 | unsigned long num_rx_bytes; |
| 159 | }; |
| 160 | |
| 161 | static struct list_head routing_table[RT_HASH_SIZE]; |
| 162 | static DEFINE_MUTEX(routing_table_lock); |
| 163 | static int routing_table_inited; |
| 164 | |
| 165 | static LIST_HEAD(msm_ipc_board_dev_list); |
| 166 | static DEFINE_MUTEX(msm_ipc_board_dev_list_lock); |
| 167 | |
| 168 | static void do_read_data(struct work_struct *work); |
| 169 | |
| 170 | #define RR_STATE_IDLE 0 |
| 171 | #define RR_STATE_HEADER 1 |
| 172 | #define RR_STATE_BODY 2 |
| 173 | #define RR_STATE_ERROR 3 |
| 174 | |
| 175 | #define RESTART_NORMAL 0 |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 176 | #define RESTART_PEND 1 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 177 | |
| 178 | /* State for remote ep following restart */ |
| 179 | #define RESTART_QUOTA_ABORT 1 |
| 180 | |
| 181 | static LIST_HEAD(xprt_info_list); |
| 182 | static DEFINE_MUTEX(xprt_info_list_lock); |
| 183 | |
Karthikeyan Ramasubramanian | 4af9f7c | 2011-09-30 15:55:18 -0600 | [diff] [blame] | 184 | static DECLARE_COMPLETION(msm_ipc_local_router_up); |
| 185 | #define IPC_ROUTER_INIT_TIMEOUT (10 * HZ) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | |
| 187 | static uint32_t next_port_id; |
| 188 | static DEFINE_MUTEX(next_port_id_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 189 | static atomic_t pending_close_count = ATOMIC_INIT(0); |
| 190 | static wait_queue_head_t subsystem_restart_wait; |
| 191 | static struct workqueue_struct *msm_ipc_router_workqueue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 192 | |
| 193 | enum { |
| 194 | CLIENT_PORT, |
| 195 | SERVER_PORT, |
| 196 | CONTROL_PORT, |
| 197 | }; |
| 198 | |
| 199 | enum { |
| 200 | DOWN, |
| 201 | UP, |
| 202 | }; |
| 203 | |
| 204 | static void init_routing_table(void) |
| 205 | { |
| 206 | int i; |
| 207 | for (i = 0; i < RT_HASH_SIZE; i++) |
| 208 | INIT_LIST_HEAD(&routing_table[i]); |
| 209 | } |
| 210 | |
| 211 | static struct msm_ipc_routing_table_entry *alloc_routing_table_entry( |
| 212 | uint32_t node_id) |
| 213 | { |
| 214 | int i; |
| 215 | struct msm_ipc_routing_table_entry *rt_entry; |
| 216 | |
| 217 | rt_entry = kmalloc(sizeof(struct msm_ipc_routing_table_entry), |
| 218 | GFP_KERNEL); |
| 219 | if (!rt_entry) { |
| 220 | pr_err("%s: rt_entry allocation failed for %d\n", |
| 221 | __func__, node_id); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | for (i = 0; i < RP_HASH_SIZE; i++) |
| 226 | INIT_LIST_HEAD(&rt_entry->remote_port_list[i]); |
| 227 | |
| 228 | mutex_init(&rt_entry->lock); |
| 229 | rt_entry->node_id = node_id; |
| 230 | rt_entry->xprt_info = NULL; |
| 231 | return rt_entry; |
| 232 | } |
| 233 | |
| 234 | /*Please take routing_table_lock before calling this function*/ |
| 235 | static int add_routing_table_entry( |
| 236 | struct msm_ipc_routing_table_entry *rt_entry) |
| 237 | { |
| 238 | uint32_t key; |
| 239 | |
| 240 | if (!rt_entry) |
| 241 | return -EINVAL; |
| 242 | |
| 243 | key = (rt_entry->node_id % RT_HASH_SIZE); |
| 244 | list_add_tail(&rt_entry->list, &routing_table[key]); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /*Please take routing_table_lock before calling this function*/ |
| 249 | static struct msm_ipc_routing_table_entry *lookup_routing_table( |
| 250 | uint32_t node_id) |
| 251 | { |
| 252 | uint32_t key = (node_id % RT_HASH_SIZE); |
| 253 | struct msm_ipc_routing_table_entry *rt_entry; |
| 254 | |
| 255 | list_for_each_entry(rt_entry, &routing_table[key], list) { |
| 256 | if (rt_entry->node_id == node_id) |
| 257 | return rt_entry; |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | struct rr_packet *rr_read(struct msm_ipc_router_xprt_info *xprt_info) |
| 263 | { |
| 264 | struct rr_packet *temp_pkt; |
| 265 | |
| 266 | if (!xprt_info) |
| 267 | return NULL; |
| 268 | |
| 269 | mutex_lock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 270 | if (xprt_info->abort_data_read) { |
| 271 | mutex_unlock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 272 | pr_err("%s detected SSR & exiting now\n", |
| 273 | xprt_info->xprt->name); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | if (list_empty(&xprt_info->pkt_list)) { |
| 278 | mutex_unlock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 279 | return NULL; |
| 280 | } |
| 281 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 282 | temp_pkt = list_first_entry(&xprt_info->pkt_list, |
| 283 | struct rr_packet, list); |
| 284 | list_del(&temp_pkt->list); |
| 285 | if (list_empty(&xprt_info->pkt_list)) |
| 286 | wake_unlock(&xprt_info->wakelock); |
| 287 | mutex_unlock(&xprt_info->rx_lock); |
| 288 | return temp_pkt; |
| 289 | } |
| 290 | |
| 291 | struct rr_packet *clone_pkt(struct rr_packet *pkt) |
| 292 | { |
| 293 | struct rr_packet *cloned_pkt; |
| 294 | struct sk_buff *temp_skb, *cloned_skb; |
| 295 | struct sk_buff_head *pkt_fragment_q; |
| 296 | |
| 297 | cloned_pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL); |
| 298 | if (!cloned_pkt) { |
| 299 | pr_err("%s: failure\n", __func__); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL); |
| 304 | if (!pkt_fragment_q) { |
| 305 | pr_err("%s: pkt_frag_q alloc failure\n", __func__); |
| 306 | kfree(cloned_pkt); |
| 307 | return NULL; |
| 308 | } |
| 309 | skb_queue_head_init(pkt_fragment_q); |
| 310 | |
| 311 | skb_queue_walk(pkt->pkt_fragment_q, temp_skb) { |
| 312 | cloned_skb = skb_clone(temp_skb, GFP_KERNEL); |
| 313 | if (!cloned_skb) |
| 314 | goto fail_clone; |
| 315 | skb_queue_tail(pkt_fragment_q, cloned_skb); |
| 316 | } |
| 317 | cloned_pkt->pkt_fragment_q = pkt_fragment_q; |
| 318 | cloned_pkt->length = pkt->length; |
| 319 | return cloned_pkt; |
| 320 | |
| 321 | fail_clone: |
| 322 | while (!skb_queue_empty(pkt_fragment_q)) { |
| 323 | temp_skb = skb_dequeue(pkt_fragment_q); |
| 324 | kfree_skb(temp_skb); |
| 325 | } |
| 326 | kfree(pkt_fragment_q); |
| 327 | kfree(cloned_pkt); |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | struct rr_packet *create_pkt(struct sk_buff_head *data) |
| 332 | { |
| 333 | struct rr_packet *pkt; |
| 334 | struct sk_buff *temp_skb; |
| 335 | |
| 336 | pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL); |
| 337 | if (!pkt) { |
| 338 | pr_err("%s: failure\n", __func__); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | pkt->pkt_fragment_q = data; |
| 343 | skb_queue_walk(pkt->pkt_fragment_q, temp_skb) |
| 344 | pkt->length += temp_skb->len; |
| 345 | return pkt; |
| 346 | } |
| 347 | |
| 348 | void release_pkt(struct rr_packet *pkt) |
| 349 | { |
| 350 | struct sk_buff *temp_skb; |
| 351 | |
| 352 | if (!pkt) |
| 353 | return; |
| 354 | |
| 355 | if (!pkt->pkt_fragment_q) { |
| 356 | kfree(pkt); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | while (!skb_queue_empty(pkt->pkt_fragment_q)) { |
| 361 | temp_skb = skb_dequeue(pkt->pkt_fragment_q); |
| 362 | kfree_skb(temp_skb); |
| 363 | } |
| 364 | kfree(pkt->pkt_fragment_q); |
| 365 | kfree(pkt); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | static int post_control_ports(struct rr_packet *pkt) |
| 370 | { |
| 371 | struct msm_ipc_port *port_ptr; |
| 372 | struct rr_packet *cloned_pkt; |
| 373 | |
| 374 | if (!pkt) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | mutex_lock(&control_ports_lock); |
| 378 | list_for_each_entry(port_ptr, &control_ports, list) { |
| 379 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 380 | cloned_pkt = clone_pkt(pkt); |
| 381 | wake_lock(&port_ptr->port_rx_wake_lock); |
| 382 | list_add_tail(&cloned_pkt->list, &port_ptr->port_rx_q); |
| 383 | wake_up(&port_ptr->port_rx_wait_q); |
| 384 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 385 | } |
| 386 | mutex_unlock(&control_ports_lock); |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static uint32_t allocate_port_id(void) |
| 391 | { |
| 392 | uint32_t port_id = 0, prev_port_id, key; |
| 393 | struct msm_ipc_port *port_ptr; |
| 394 | |
| 395 | mutex_lock(&next_port_id_lock); |
| 396 | prev_port_id = next_port_id; |
| 397 | mutex_lock(&local_ports_lock); |
| 398 | do { |
| 399 | next_port_id++; |
| 400 | if ((next_port_id & 0xFFFFFFFE) == 0xFFFFFFFE) |
| 401 | next_port_id = 1; |
| 402 | |
| 403 | key = (next_port_id & (LP_HASH_SIZE - 1)); |
| 404 | if (list_empty(&local_ports[key])) { |
| 405 | port_id = next_port_id; |
| 406 | break; |
| 407 | } |
| 408 | list_for_each_entry(port_ptr, &local_ports[key], list) { |
| 409 | if (port_ptr->this_port.port_id == next_port_id) { |
| 410 | port_id = next_port_id; |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | if (!port_id) { |
| 415 | port_id = next_port_id; |
| 416 | break; |
| 417 | } |
| 418 | port_id = 0; |
| 419 | } while (next_port_id != prev_port_id); |
| 420 | mutex_unlock(&local_ports_lock); |
| 421 | mutex_unlock(&next_port_id_lock); |
| 422 | |
| 423 | return port_id; |
| 424 | } |
| 425 | |
| 426 | void msm_ipc_router_add_local_port(struct msm_ipc_port *port_ptr) |
| 427 | { |
| 428 | uint32_t key; |
| 429 | |
| 430 | if (!port_ptr) |
| 431 | return; |
| 432 | |
| 433 | key = (port_ptr->this_port.port_id & (LP_HASH_SIZE - 1)); |
| 434 | mutex_lock(&local_ports_lock); |
| 435 | list_add_tail(&port_ptr->list, &local_ports[key]); |
| 436 | mutex_unlock(&local_ports_lock); |
| 437 | } |
| 438 | |
| 439 | struct msm_ipc_port *msm_ipc_router_create_raw_port(void *endpoint, |
| 440 | void (*notify)(unsigned event, void *data, |
| 441 | void *addr, void *priv), |
| 442 | void *priv) |
| 443 | { |
| 444 | struct msm_ipc_port *port_ptr; |
| 445 | |
| 446 | port_ptr = kzalloc(sizeof(struct msm_ipc_port), GFP_KERNEL); |
| 447 | if (!port_ptr) |
| 448 | return NULL; |
| 449 | |
| 450 | port_ptr->this_port.node_id = IPC_ROUTER_NID_LOCAL; |
| 451 | port_ptr->this_port.port_id = allocate_port_id(); |
| 452 | if (!port_ptr->this_port.port_id) { |
| 453 | pr_err("%s: All port ids are in use\n", __func__); |
| 454 | kfree(port_ptr); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | spin_lock_init(&port_ptr->port_lock); |
| 459 | INIT_LIST_HEAD(&port_ptr->incomplete); |
| 460 | mutex_init(&port_ptr->incomplete_lock); |
| 461 | INIT_LIST_HEAD(&port_ptr->port_rx_q); |
| 462 | mutex_init(&port_ptr->port_rx_q_lock); |
| 463 | init_waitqueue_head(&port_ptr->port_rx_wait_q); |
Karthikeyan Ramasubramanian | b4aeeb9 | 2012-03-13 12:31:43 -0600 | [diff] [blame] | 464 | snprintf(port_ptr->rx_wakelock_name, MAX_WAKELOCK_NAME_SZ, |
| 465 | "msm_ipc_read%08x:%08x", |
| 466 | port_ptr->this_port.node_id, |
| 467 | port_ptr->this_port.port_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 468 | wake_lock_init(&port_ptr->port_rx_wake_lock, |
Karthikeyan Ramasubramanian | b4aeeb9 | 2012-03-13 12:31:43 -0600 | [diff] [blame] | 469 | WAKE_LOCK_SUSPEND, port_ptr->rx_wakelock_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 470 | |
| 471 | port_ptr->endpoint = endpoint; |
| 472 | port_ptr->notify = notify; |
| 473 | port_ptr->priv = priv; |
| 474 | |
| 475 | msm_ipc_router_add_local_port(port_ptr); |
| 476 | return port_ptr; |
| 477 | } |
| 478 | |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 479 | /* |
| 480 | * Should be called with local_ports_lock locked |
| 481 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 482 | static struct msm_ipc_port *msm_ipc_router_lookup_local_port(uint32_t port_id) |
| 483 | { |
| 484 | int key = (port_id & (LP_HASH_SIZE - 1)); |
| 485 | struct msm_ipc_port *port_ptr; |
| 486 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 487 | list_for_each_entry(port_ptr, &local_ports[key], list) { |
| 488 | if (port_ptr->this_port.port_id == port_id) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 489 | return port_ptr; |
| 490 | } |
| 491 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | static struct msm_ipc_router_remote_port *msm_ipc_router_lookup_remote_port( |
| 496 | uint32_t node_id, |
| 497 | uint32_t port_id) |
| 498 | { |
| 499 | struct msm_ipc_router_remote_port *rport_ptr; |
| 500 | struct msm_ipc_routing_table_entry *rt_entry; |
| 501 | int key = (port_id & (RP_HASH_SIZE - 1)); |
| 502 | |
| 503 | mutex_lock(&routing_table_lock); |
| 504 | rt_entry = lookup_routing_table(node_id); |
| 505 | if (!rt_entry) { |
| 506 | mutex_unlock(&routing_table_lock); |
| 507 | pr_err("%s: Node is not up\n", __func__); |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | mutex_lock(&rt_entry->lock); |
| 512 | list_for_each_entry(rport_ptr, |
| 513 | &rt_entry->remote_port_list[key], list) { |
| 514 | if (rport_ptr->port_id == port_id) { |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 515 | if (rport_ptr->restart_state != RESTART_NORMAL) |
| 516 | rport_ptr = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 517 | mutex_unlock(&rt_entry->lock); |
| 518 | mutex_unlock(&routing_table_lock); |
| 519 | return rport_ptr; |
| 520 | } |
| 521 | } |
| 522 | mutex_unlock(&rt_entry->lock); |
| 523 | mutex_unlock(&routing_table_lock); |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | static struct msm_ipc_router_remote_port *msm_ipc_router_create_remote_port( |
| 528 | uint32_t node_id, |
| 529 | uint32_t port_id) |
| 530 | { |
| 531 | struct msm_ipc_router_remote_port *rport_ptr; |
| 532 | struct msm_ipc_routing_table_entry *rt_entry; |
| 533 | int key = (port_id & (RP_HASH_SIZE - 1)); |
| 534 | |
| 535 | mutex_lock(&routing_table_lock); |
| 536 | rt_entry = lookup_routing_table(node_id); |
| 537 | if (!rt_entry) { |
| 538 | mutex_unlock(&routing_table_lock); |
| 539 | pr_err("%s: Node is not up\n", __func__); |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | mutex_lock(&rt_entry->lock); |
| 544 | rport_ptr = kmalloc(sizeof(struct msm_ipc_router_remote_port), |
| 545 | GFP_KERNEL); |
| 546 | if (!rport_ptr) { |
| 547 | mutex_unlock(&rt_entry->lock); |
| 548 | mutex_unlock(&routing_table_lock); |
| 549 | pr_err("%s: Remote port alloc failed\n", __func__); |
| 550 | return NULL; |
| 551 | } |
| 552 | rport_ptr->port_id = port_id; |
| 553 | rport_ptr->node_id = node_id; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 554 | rport_ptr->restart_state = RESTART_NORMAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | rport_ptr->tx_quota_cnt = 0; |
| 556 | init_waitqueue_head(&rport_ptr->quota_wait); |
| 557 | mutex_init(&rport_ptr->quota_lock); |
| 558 | list_add_tail(&rport_ptr->list, |
| 559 | &rt_entry->remote_port_list[key]); |
| 560 | mutex_unlock(&rt_entry->lock); |
| 561 | mutex_unlock(&routing_table_lock); |
| 562 | return rport_ptr; |
| 563 | } |
| 564 | |
| 565 | static void msm_ipc_router_destroy_remote_port( |
| 566 | struct msm_ipc_router_remote_port *rport_ptr) |
| 567 | { |
| 568 | uint32_t node_id; |
| 569 | struct msm_ipc_routing_table_entry *rt_entry; |
| 570 | |
| 571 | if (!rport_ptr) |
| 572 | return; |
| 573 | |
| 574 | node_id = rport_ptr->node_id; |
| 575 | mutex_lock(&routing_table_lock); |
| 576 | rt_entry = lookup_routing_table(node_id); |
| 577 | if (!rt_entry) { |
| 578 | mutex_unlock(&routing_table_lock); |
| 579 | pr_err("%s: Node %d is not up\n", __func__, node_id); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | mutex_lock(&rt_entry->lock); |
| 584 | list_del(&rport_ptr->list); |
| 585 | kfree(rport_ptr); |
| 586 | mutex_unlock(&rt_entry->lock); |
| 587 | mutex_unlock(&routing_table_lock); |
| 588 | return; |
| 589 | } |
| 590 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 591 | /** |
| 592 | * msm_ipc_router_lookup_server() - Lookup server information |
| 593 | * @service: Service ID of the server info to be looked up. |
| 594 | * @instance: Instance ID of the server info to be looked up. |
| 595 | * @node_id: Node/Processor ID in which the server is hosted. |
| 596 | * @port_id: Port ID within the node in which the server is hosted. |
| 597 | * |
| 598 | * @return: If found Pointer to server structure, else NULL. |
| 599 | * |
| 600 | * Note1: Lock the server_list_lock before accessing this function. |
| 601 | * Note2: If the <node_id:port_id> are <0:0>, then the lookup is restricted |
| 602 | * to <service:instance>. Used only when a client wants to send a |
| 603 | * message to any QMI server. |
| 604 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 605 | static struct msm_ipc_server *msm_ipc_router_lookup_server( |
| 606 | uint32_t service, |
| 607 | uint32_t instance, |
| 608 | uint32_t node_id, |
| 609 | uint32_t port_id) |
| 610 | { |
| 611 | struct msm_ipc_server *server; |
| 612 | struct msm_ipc_server_port *server_port; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 613 | int key = (service & (SRV_HASH_SIZE - 1)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 614 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | list_for_each_entry(server, &server_list[key], list) { |
| 616 | if ((server->name.service != service) || |
| 617 | (server->name.instance != instance)) |
| 618 | continue; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 619 | if ((node_id == 0) && (port_id == 0)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 620 | return server; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 621 | list_for_each_entry(server_port, &server->server_port_list, |
| 622 | list) { |
| 623 | if ((server_port->server_addr.node_id == node_id) && |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 624 | (server_port->server_addr.port_id == port_id)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | return server; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 626 | } |
| 627 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 628 | return NULL; |
| 629 | } |
| 630 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 631 | /** |
| 632 | * msm_ipc_router_create_server() - Add server info to hash table |
| 633 | * @service: Service ID of the server info to be created. |
| 634 | * @instance: Instance ID of the server info to be created. |
| 635 | * @node_id: Node/Processor ID in which the server is hosted. |
| 636 | * @port_id: Port ID within the node in which the server is hosted. |
| 637 | * @xprt_info: XPRT through which the node hosting the server is reached. |
| 638 | * |
| 639 | * @return: Pointer to server structure on success, else NULL. |
| 640 | * |
| 641 | * This function adds the server info to the hash table. If the same |
| 642 | * server(i.e. <service_id:instance_id>) is hosted in different nodes, |
| 643 | * they are maintained as list of "server_port" under "server" structure. |
| 644 | * Note: Lock the server_list_lock before accessing this function. |
| 645 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 646 | static struct msm_ipc_server *msm_ipc_router_create_server( |
| 647 | uint32_t service, |
| 648 | uint32_t instance, |
| 649 | uint32_t node_id, |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 650 | uint32_t port_id, |
| 651 | struct msm_ipc_router_xprt_info *xprt_info) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | { |
| 653 | struct msm_ipc_server *server = NULL; |
| 654 | struct msm_ipc_server_port *server_port; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 655 | int key = (service & (SRV_HASH_SIZE - 1)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 656 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 657 | list_for_each_entry(server, &server_list[key], list) { |
| 658 | if ((server->name.service == service) && |
| 659 | (server->name.instance == instance)) |
| 660 | goto create_srv_port; |
| 661 | } |
| 662 | |
| 663 | server = kmalloc(sizeof(struct msm_ipc_server), GFP_KERNEL); |
| 664 | if (!server) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 665 | pr_err("%s: Server allocation failed\n", __func__); |
| 666 | return NULL; |
| 667 | } |
| 668 | server->name.service = service; |
| 669 | server->name.instance = instance; |
| 670 | INIT_LIST_HEAD(&server->server_port_list); |
| 671 | list_add_tail(&server->list, &server_list[key]); |
| 672 | |
| 673 | create_srv_port: |
| 674 | server_port = kmalloc(sizeof(struct msm_ipc_server_port), GFP_KERNEL); |
| 675 | if (!server_port) { |
| 676 | if (list_empty(&server->server_port_list)) { |
| 677 | list_del(&server->list); |
| 678 | kfree(server); |
| 679 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 680 | pr_err("%s: Server Port allocation failed\n", __func__); |
| 681 | return NULL; |
| 682 | } |
| 683 | server_port->server_addr.node_id = node_id; |
| 684 | server_port->server_addr.port_id = port_id; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 685 | server_port->xprt_info = xprt_info; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 686 | list_add_tail(&server_port->list, &server->server_port_list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 687 | |
| 688 | return server; |
| 689 | } |
| 690 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 691 | /** |
| 692 | * msm_ipc_router_destroy_server() - Remove server info from hash table |
| 693 | * @server: Server info to be removed. |
| 694 | * @node_id: Node/Processor ID in which the server is hosted. |
| 695 | * @port_id: Port ID within the node in which the server is hosted. |
| 696 | * |
| 697 | * This function removes the server_port identified using <node_id:port_id> |
| 698 | * from the server structure. If the server_port list under server structure |
| 699 | * is empty after removal, then remove the server structure from the server |
| 700 | * hash table. |
| 701 | * Note: Lock the server_list_lock before accessing this function. |
| 702 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 703 | static void msm_ipc_router_destroy_server(struct msm_ipc_server *server, |
| 704 | uint32_t node_id, uint32_t port_id) |
| 705 | { |
| 706 | struct msm_ipc_server_port *server_port; |
| 707 | |
| 708 | if (!server) |
| 709 | return; |
| 710 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 711 | list_for_each_entry(server_port, &server->server_port_list, list) { |
| 712 | if ((server_port->server_addr.node_id == node_id) && |
| 713 | (server_port->server_addr.port_id == port_id)) |
| 714 | break; |
| 715 | } |
| 716 | if (server_port) { |
| 717 | list_del(&server_port->list); |
| 718 | kfree(server_port); |
| 719 | } |
| 720 | if (list_empty(&server->server_port_list)) { |
| 721 | list_del(&server->list); |
| 722 | kfree(server); |
| 723 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
| 727 | static int msm_ipc_router_send_control_msg( |
| 728 | struct msm_ipc_router_xprt_info *xprt_info, |
| 729 | union rr_control_msg *msg) |
| 730 | { |
| 731 | struct rr_packet *pkt; |
| 732 | struct sk_buff *ipc_rtr_pkt; |
| 733 | struct rr_header *hdr; |
| 734 | int pkt_size; |
| 735 | void *data; |
| 736 | struct sk_buff_head *pkt_fragment_q; |
| 737 | int ret; |
| 738 | |
| 739 | if (!xprt_info || ((msg->cmd != IPC_ROUTER_CTRL_CMD_HELLO) && |
| 740 | !xprt_info->initialized)) { |
| 741 | pr_err("%s: xprt_info not initialized\n", __func__); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
| 745 | if (xprt_info->remote_node_id == IPC_ROUTER_NID_LOCAL) |
| 746 | return 0; |
| 747 | |
| 748 | pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL); |
| 749 | if (!pkt) { |
| 750 | pr_err("%s: pkt alloc failed\n", __func__); |
| 751 | return -ENOMEM; |
| 752 | } |
| 753 | |
| 754 | pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL); |
| 755 | if (!pkt_fragment_q) { |
| 756 | pr_err("%s: pkt_fragment_q alloc failed\n", __func__); |
| 757 | kfree(pkt); |
| 758 | return -ENOMEM; |
| 759 | } |
| 760 | skb_queue_head_init(pkt_fragment_q); |
| 761 | |
| 762 | pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg); |
| 763 | ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL); |
| 764 | if (!ipc_rtr_pkt) { |
| 765 | pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__); |
| 766 | kfree(pkt_fragment_q); |
| 767 | kfree(pkt); |
| 768 | return -ENOMEM; |
| 769 | } |
| 770 | |
| 771 | skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE); |
| 772 | data = skb_put(ipc_rtr_pkt, sizeof(*msg)); |
| 773 | memcpy(data, msg, sizeof(*msg)); |
| 774 | hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE); |
| 775 | if (!hdr) { |
| 776 | pr_err("%s: skb_push failed\n", __func__); |
| 777 | kfree_skb(ipc_rtr_pkt); |
| 778 | kfree(pkt_fragment_q); |
| 779 | kfree(pkt); |
| 780 | return -ENOMEM; |
| 781 | } |
| 782 | |
| 783 | hdr->version = IPC_ROUTER_VERSION; |
| 784 | hdr->type = msg->cmd; |
| 785 | hdr->src_node_id = IPC_ROUTER_NID_LOCAL; |
| 786 | hdr->src_port_id = IPC_ROUTER_ADDRESS; |
| 787 | hdr->confirm_rx = 0; |
| 788 | hdr->size = sizeof(*msg); |
| 789 | hdr->dst_node_id = xprt_info->remote_node_id; |
| 790 | hdr->dst_port_id = IPC_ROUTER_ADDRESS; |
| 791 | skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt); |
| 792 | pkt->pkt_fragment_q = pkt_fragment_q; |
| 793 | pkt->length = pkt_size; |
| 794 | |
| 795 | mutex_lock(&xprt_info->tx_lock); |
Karthikeyan Ramasubramanian | 8cec592 | 2012-02-16 17:41:58 -0700 | [diff] [blame] | 796 | ret = xprt_info->xprt->write(pkt, pkt_size, xprt_info->xprt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | mutex_unlock(&xprt_info->tx_lock); |
| 798 | |
| 799 | release_pkt(pkt); |
| 800 | return ret; |
| 801 | } |
| 802 | |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 803 | static int msm_ipc_router_send_server_list(uint32_t node_id, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 804 | struct msm_ipc_router_xprt_info *xprt_info) |
| 805 | { |
| 806 | union rr_control_msg ctl; |
| 807 | struct msm_ipc_server *server; |
| 808 | struct msm_ipc_server_port *server_port; |
| 809 | int i; |
| 810 | |
| 811 | if (!xprt_info || !xprt_info->initialized) { |
| 812 | pr_err("%s: Xprt info not initialized\n", __func__); |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER; |
| 817 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 818 | for (i = 0; i < SRV_HASH_SIZE; i++) { |
| 819 | list_for_each_entry(server, &server_list[i], list) { |
| 820 | ctl.srv.service = server->name.service; |
| 821 | ctl.srv.instance = server->name.instance; |
| 822 | list_for_each_entry(server_port, |
| 823 | &server->server_port_list, list) { |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 824 | if (server_port->server_addr.node_id != |
| 825 | node_id) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 826 | continue; |
| 827 | |
| 828 | ctl.srv.node_id = |
| 829 | server_port->server_addr.node_id; |
| 830 | ctl.srv.port_id = |
| 831 | server_port->server_addr.port_id; |
| 832 | msm_ipc_router_send_control_msg(xprt_info, |
| 833 | &ctl); |
| 834 | } |
| 835 | } |
| 836 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | #if defined(DEBUG) |
| 842 | static char *type_to_str(int i) |
| 843 | { |
| 844 | switch (i) { |
| 845 | case IPC_ROUTER_CTRL_CMD_DATA: |
| 846 | return "data "; |
| 847 | case IPC_ROUTER_CTRL_CMD_HELLO: |
| 848 | return "hello "; |
| 849 | case IPC_ROUTER_CTRL_CMD_BYE: |
| 850 | return "bye "; |
| 851 | case IPC_ROUTER_CTRL_CMD_NEW_SERVER: |
| 852 | return "new_srvr"; |
| 853 | case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER: |
| 854 | return "rmv_srvr"; |
| 855 | case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT: |
| 856 | return "rmv_clnt"; |
| 857 | case IPC_ROUTER_CTRL_CMD_RESUME_TX: |
| 858 | return "resum_tx"; |
| 859 | case IPC_ROUTER_CTRL_CMD_EXIT: |
| 860 | return "cmd_exit"; |
| 861 | default: |
| 862 | return "invalid"; |
| 863 | } |
| 864 | } |
| 865 | #endif |
| 866 | |
| 867 | static int broadcast_ctl_msg_locally(union rr_control_msg *msg) |
| 868 | { |
| 869 | struct rr_packet *pkt; |
| 870 | struct sk_buff *ipc_rtr_pkt; |
| 871 | struct rr_header *hdr; |
| 872 | int pkt_size; |
| 873 | void *data; |
| 874 | struct sk_buff_head *pkt_fragment_q; |
| 875 | int ret; |
| 876 | |
| 877 | pkt = kzalloc(sizeof(struct rr_packet), GFP_KERNEL); |
| 878 | if (!pkt) { |
| 879 | pr_err("%s: pkt alloc failed\n", __func__); |
| 880 | return -ENOMEM; |
| 881 | } |
| 882 | |
| 883 | pkt_fragment_q = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL); |
| 884 | if (!pkt_fragment_q) { |
| 885 | pr_err("%s: pkt_fragment_q alloc failed\n", __func__); |
| 886 | kfree(pkt); |
| 887 | return -ENOMEM; |
| 888 | } |
| 889 | skb_queue_head_init(pkt_fragment_q); |
| 890 | |
| 891 | pkt_size = IPC_ROUTER_HDR_SIZE + sizeof(*msg); |
| 892 | ipc_rtr_pkt = alloc_skb(pkt_size, GFP_KERNEL); |
| 893 | if (!ipc_rtr_pkt) { |
| 894 | pr_err("%s: ipc_rtr_pkt alloc failed\n", __func__); |
| 895 | kfree(pkt_fragment_q); |
| 896 | kfree(pkt); |
| 897 | return -ENOMEM; |
| 898 | } |
| 899 | |
| 900 | skb_reserve(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE); |
| 901 | data = skb_put(ipc_rtr_pkt, sizeof(*msg)); |
| 902 | memcpy(data, msg, sizeof(*msg)); |
| 903 | hdr = (struct rr_header *)skb_push(ipc_rtr_pkt, IPC_ROUTER_HDR_SIZE); |
| 904 | if (!hdr) { |
| 905 | pr_err("%s: skb_push failed\n", __func__); |
| 906 | kfree_skb(ipc_rtr_pkt); |
| 907 | kfree(pkt_fragment_q); |
| 908 | kfree(pkt); |
| 909 | return -ENOMEM; |
| 910 | } |
| 911 | hdr->version = IPC_ROUTER_VERSION; |
| 912 | hdr->type = msg->cmd; |
| 913 | hdr->src_node_id = IPC_ROUTER_NID_LOCAL; |
| 914 | hdr->src_port_id = IPC_ROUTER_ADDRESS; |
| 915 | hdr->confirm_rx = 0; |
| 916 | hdr->size = sizeof(*msg); |
| 917 | hdr->dst_node_id = IPC_ROUTER_NID_LOCAL; |
| 918 | hdr->dst_port_id = IPC_ROUTER_ADDRESS; |
| 919 | skb_queue_tail(pkt_fragment_q, ipc_rtr_pkt); |
| 920 | pkt->pkt_fragment_q = pkt_fragment_q; |
| 921 | pkt->length = pkt_size; |
| 922 | |
| 923 | ret = post_control_ports(pkt); |
| 924 | release_pkt(pkt); |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | static int broadcast_ctl_msg(union rr_control_msg *ctl) |
| 929 | { |
| 930 | struct msm_ipc_router_xprt_info *xprt_info; |
| 931 | |
| 932 | mutex_lock(&xprt_info_list_lock); |
| 933 | list_for_each_entry(xprt_info, &xprt_info_list, list) { |
| 934 | msm_ipc_router_send_control_msg(xprt_info, ctl); |
| 935 | } |
| 936 | mutex_unlock(&xprt_info_list_lock); |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 941 | static int relay_ctl_msg(struct msm_ipc_router_xprt_info *xprt_info, |
| 942 | union rr_control_msg *ctl) |
| 943 | { |
| 944 | struct msm_ipc_router_xprt_info *fwd_xprt_info; |
| 945 | |
| 946 | if (!xprt_info || !ctl) |
| 947 | return -EINVAL; |
| 948 | |
| 949 | mutex_lock(&xprt_info_list_lock); |
| 950 | list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) { |
| 951 | if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id) |
| 952 | msm_ipc_router_send_control_msg(fwd_xprt_info, ctl); |
| 953 | } |
| 954 | mutex_unlock(&xprt_info_list_lock); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 959 | static int relay_msg(struct msm_ipc_router_xprt_info *xprt_info, |
| 960 | struct rr_packet *pkt) |
| 961 | { |
| 962 | struct msm_ipc_router_xprt_info *fwd_xprt_info; |
| 963 | |
| 964 | if (!xprt_info || !pkt) |
| 965 | return -EINVAL; |
| 966 | |
| 967 | mutex_lock(&xprt_info_list_lock); |
| 968 | list_for_each_entry(fwd_xprt_info, &xprt_info_list, list) { |
| 969 | mutex_lock(&fwd_xprt_info->tx_lock); |
| 970 | if (xprt_info->xprt->link_id != fwd_xprt_info->xprt->link_id) |
Karthikeyan Ramasubramanian | 8cec592 | 2012-02-16 17:41:58 -0700 | [diff] [blame] | 971 | fwd_xprt_info->xprt->write(pkt, pkt->length, |
| 972 | fwd_xprt_info->xprt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 973 | mutex_unlock(&fwd_xprt_info->tx_lock); |
| 974 | } |
| 975 | mutex_unlock(&xprt_info_list_lock); |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | static int forward_msg(struct msm_ipc_router_xprt_info *xprt_info, |
| 980 | struct rr_packet *pkt) |
| 981 | { |
| 982 | uint32_t dst_node_id; |
| 983 | struct sk_buff *head_pkt; |
| 984 | struct rr_header *hdr; |
| 985 | struct msm_ipc_router_xprt_info *fwd_xprt_info; |
| 986 | struct msm_ipc_routing_table_entry *rt_entry; |
| 987 | |
| 988 | if (!xprt_info || !pkt) |
| 989 | return -EINVAL; |
| 990 | |
| 991 | head_pkt = skb_peek(pkt->pkt_fragment_q); |
| 992 | if (!head_pkt) |
| 993 | return -EINVAL; |
| 994 | |
| 995 | hdr = (struct rr_header *)head_pkt->data; |
| 996 | dst_node_id = hdr->dst_node_id; |
| 997 | mutex_lock(&routing_table_lock); |
| 998 | rt_entry = lookup_routing_table(dst_node_id); |
| 999 | if (!(rt_entry) || !(rt_entry->xprt_info)) { |
| 1000 | mutex_unlock(&routing_table_lock); |
| 1001 | pr_err("%s: Routing table not initialized\n", __func__); |
| 1002 | return -ENODEV; |
| 1003 | } |
| 1004 | |
| 1005 | mutex_lock(&rt_entry->lock); |
| 1006 | fwd_xprt_info = rt_entry->xprt_info; |
| 1007 | mutex_lock(&fwd_xprt_info->tx_lock); |
| 1008 | if (xprt_info->remote_node_id == fwd_xprt_info->remote_node_id) { |
| 1009 | mutex_unlock(&fwd_xprt_info->tx_lock); |
| 1010 | mutex_unlock(&rt_entry->lock); |
| 1011 | mutex_unlock(&routing_table_lock); |
| 1012 | pr_err("%s: Discarding Command to route back\n", __func__); |
| 1013 | return -EINVAL; |
| 1014 | } |
| 1015 | |
| 1016 | if (xprt_info->xprt->link_id == fwd_xprt_info->xprt->link_id) { |
| 1017 | mutex_unlock(&fwd_xprt_info->tx_lock); |
| 1018 | mutex_unlock(&rt_entry->lock); |
| 1019 | mutex_unlock(&routing_table_lock); |
| 1020 | pr_err("%s: DST in the same cluster\n", __func__); |
| 1021 | return 0; |
| 1022 | } |
Karthikeyan Ramasubramanian | 8cec592 | 2012-02-16 17:41:58 -0700 | [diff] [blame] | 1023 | fwd_xprt_info->xprt->write(pkt, pkt->length, fwd_xprt_info->xprt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1024 | mutex_unlock(&fwd_xprt_info->tx_lock); |
| 1025 | mutex_unlock(&rt_entry->lock); |
| 1026 | mutex_unlock(&routing_table_lock); |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1031 | static void reset_remote_port_info(uint32_t node_id, uint32_t port_id) |
| 1032 | { |
| 1033 | struct msm_ipc_router_remote_port *rport_ptr; |
| 1034 | |
| 1035 | rport_ptr = msm_ipc_router_lookup_remote_port(node_id, port_id); |
| 1036 | if (!rport_ptr) { |
| 1037 | pr_err("%s: No such remote port %08x:%08x\n", |
| 1038 | __func__, node_id, port_id); |
| 1039 | return; |
| 1040 | } |
| 1041 | mutex_lock(&rport_ptr->quota_lock); |
| 1042 | rport_ptr->restart_state = RESTART_PEND; |
| 1043 | wake_up(&rport_ptr->quota_wait); |
| 1044 | mutex_unlock(&rport_ptr->quota_lock); |
| 1045 | return; |
| 1046 | } |
| 1047 | |
| 1048 | static void msm_ipc_cleanup_remote_server_info( |
| 1049 | struct msm_ipc_router_xprt_info *xprt_info) |
| 1050 | { |
| 1051 | struct msm_ipc_server *svr, *tmp_svr; |
| 1052 | struct msm_ipc_server_port *svr_port, *tmp_svr_port; |
| 1053 | int i; |
| 1054 | union rr_control_msg ctl; |
| 1055 | |
| 1056 | if (!xprt_info) { |
| 1057 | pr_err("%s: Invalid xprt_info\n", __func__); |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER; |
| 1062 | mutex_lock(&server_list_lock); |
| 1063 | for (i = 0; i < SRV_HASH_SIZE; i++) { |
| 1064 | list_for_each_entry_safe(svr, tmp_svr, &server_list[i], list) { |
| 1065 | ctl.srv.service = svr->name.service; |
| 1066 | ctl.srv.instance = svr->name.instance; |
| 1067 | list_for_each_entry_safe(svr_port, tmp_svr_port, |
| 1068 | &svr->server_port_list, list) { |
| 1069 | if (svr_port->xprt_info != xprt_info) |
| 1070 | continue; |
| 1071 | D("Remove server %08x:%08x - %08x:%08x", |
| 1072 | ctl.srv.service, ctl.srv.instance, |
| 1073 | svr_port->server_addr.node_id, |
| 1074 | svr_port->server_addr.port_id); |
| 1075 | reset_remote_port_info( |
| 1076 | svr_port->server_addr.node_id, |
| 1077 | svr_port->server_addr.port_id); |
| 1078 | ctl.srv.node_id = svr_port->server_addr.node_id; |
| 1079 | ctl.srv.port_id = svr_port->server_addr.port_id; |
| 1080 | relay_ctl_msg(xprt_info, &ctl); |
| 1081 | broadcast_ctl_msg_locally(&ctl); |
| 1082 | list_del(&svr_port->list); |
| 1083 | kfree(svr_port); |
| 1084 | } |
| 1085 | if (list_empty(&svr->server_port_list)) { |
| 1086 | list_del(&svr->list); |
| 1087 | kfree(svr); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | mutex_unlock(&server_list_lock); |
| 1092 | } |
| 1093 | |
| 1094 | static void msm_ipc_cleanup_remote_client_info( |
| 1095 | struct msm_ipc_router_xprt_info *xprt_info) |
| 1096 | { |
| 1097 | struct msm_ipc_routing_table_entry *rt_entry; |
| 1098 | struct msm_ipc_router_remote_port *rport_ptr; |
| 1099 | int i, j; |
| 1100 | union rr_control_msg ctl; |
| 1101 | |
| 1102 | if (!xprt_info) { |
| 1103 | pr_err("%s: Invalid xprt_info\n", __func__); |
| 1104 | return; |
| 1105 | } |
| 1106 | |
| 1107 | ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT; |
| 1108 | mutex_lock(&routing_table_lock); |
| 1109 | for (i = 0; i < RT_HASH_SIZE; i++) { |
| 1110 | list_for_each_entry(rt_entry, &routing_table[i], list) { |
| 1111 | mutex_lock(&rt_entry->lock); |
| 1112 | if (rt_entry->xprt_info != xprt_info) { |
| 1113 | mutex_unlock(&rt_entry->lock); |
| 1114 | continue; |
| 1115 | } |
| 1116 | for (j = 0; j < RP_HASH_SIZE; j++) { |
| 1117 | list_for_each_entry(rport_ptr, |
| 1118 | &rt_entry->remote_port_list[j], list) { |
| 1119 | if (rport_ptr->restart_state == |
| 1120 | RESTART_PEND) |
| 1121 | continue; |
| 1122 | mutex_lock(&rport_ptr->quota_lock); |
| 1123 | rport_ptr->restart_state = RESTART_PEND; |
| 1124 | wake_up(&rport_ptr->quota_wait); |
| 1125 | mutex_unlock(&rport_ptr->quota_lock); |
| 1126 | ctl.cli.node_id = rport_ptr->node_id; |
| 1127 | ctl.cli.port_id = rport_ptr->port_id; |
| 1128 | broadcast_ctl_msg_locally(&ctl); |
| 1129 | } |
| 1130 | } |
| 1131 | mutex_unlock(&rt_entry->lock); |
| 1132 | } |
| 1133 | } |
| 1134 | mutex_unlock(&routing_table_lock); |
| 1135 | } |
| 1136 | |
| 1137 | static void msm_ipc_cleanup_remote_port_info(uint32_t node_id) |
| 1138 | { |
| 1139 | struct msm_ipc_routing_table_entry *rt_entry, *tmp_rt_entry; |
| 1140 | struct msm_ipc_router_remote_port *rport_ptr, *tmp_rport_ptr; |
| 1141 | int i, j; |
| 1142 | |
| 1143 | mutex_lock(&routing_table_lock); |
| 1144 | for (i = 0; i < RT_HASH_SIZE; i++) { |
| 1145 | list_for_each_entry_safe(rt_entry, tmp_rt_entry, |
| 1146 | &routing_table[i], list) { |
| 1147 | mutex_lock(&rt_entry->lock); |
| 1148 | if (rt_entry->neighbor_node_id != node_id) { |
| 1149 | mutex_unlock(&rt_entry->lock); |
| 1150 | continue; |
| 1151 | } |
| 1152 | for (j = 0; j < RP_HASH_SIZE; j++) { |
| 1153 | list_for_each_entry_safe(rport_ptr, |
| 1154 | tmp_rport_ptr, |
| 1155 | &rt_entry->remote_port_list[j], list) { |
| 1156 | list_del(&rport_ptr->list); |
| 1157 | kfree(rport_ptr); |
| 1158 | } |
| 1159 | } |
| 1160 | mutex_unlock(&rt_entry->lock); |
| 1161 | } |
| 1162 | } |
| 1163 | mutex_unlock(&routing_table_lock); |
| 1164 | } |
| 1165 | |
| 1166 | static void msm_ipc_cleanup_routing_table( |
| 1167 | struct msm_ipc_router_xprt_info *xprt_info) |
| 1168 | { |
| 1169 | int i; |
| 1170 | struct msm_ipc_routing_table_entry *rt_entry; |
| 1171 | |
| 1172 | if (!xprt_info) { |
| 1173 | pr_err("%s: Invalid xprt_info\n", __func__); |
| 1174 | return; |
| 1175 | } |
| 1176 | |
| 1177 | mutex_lock(&routing_table_lock); |
| 1178 | for (i = 0; i < RT_HASH_SIZE; i++) { |
| 1179 | list_for_each_entry(rt_entry, &routing_table[i], list) { |
| 1180 | mutex_lock(&rt_entry->lock); |
| 1181 | if (rt_entry->xprt_info == xprt_info) |
| 1182 | rt_entry->xprt_info = NULL; |
| 1183 | mutex_unlock(&rt_entry->lock); |
| 1184 | } |
| 1185 | } |
| 1186 | mutex_unlock(&routing_table_lock); |
| 1187 | } |
| 1188 | |
| 1189 | static void modem_reset_cleanup(struct msm_ipc_router_xprt_info *xprt_info) |
| 1190 | { |
| 1191 | |
| 1192 | if (!xprt_info) { |
| 1193 | pr_err("%s: Invalid xprt_info\n", __func__); |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | msm_ipc_cleanup_remote_server_info(xprt_info); |
| 1198 | msm_ipc_cleanup_remote_client_info(xprt_info); |
| 1199 | msm_ipc_cleanup_routing_table(xprt_info); |
| 1200 | } |
| 1201 | |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1202 | static int process_hello_msg(struct msm_ipc_router_xprt_info *xprt_info, |
| 1203 | struct rr_header *hdr) |
| 1204 | { |
| 1205 | int i, rc = 0; |
| 1206 | union rr_control_msg ctl; |
| 1207 | struct msm_ipc_routing_table_entry *rt_entry; |
| 1208 | |
| 1209 | if (!hdr) |
| 1210 | return -EINVAL; |
| 1211 | |
| 1212 | RR("o HELLO NID %d\n", hdr->src_node_id); |
| 1213 | |
| 1214 | xprt_info->remote_node_id = hdr->src_node_id; |
| 1215 | /* |
| 1216 | * Find the entry from Routing Table corresponding to Node ID. |
| 1217 | * Under SSR, an entry will be found. When the system boots up |
| 1218 | * for the 1st time, an entry will not be found and hence allocate |
| 1219 | * an entry. Update the entry with the Node ID that it corresponds |
| 1220 | * to and the XPRT through which it can be reached. |
| 1221 | */ |
| 1222 | mutex_lock(&routing_table_lock); |
| 1223 | rt_entry = lookup_routing_table(hdr->src_node_id); |
| 1224 | if (!rt_entry) { |
| 1225 | rt_entry = alloc_routing_table_entry(hdr->src_node_id); |
| 1226 | if (!rt_entry) { |
| 1227 | mutex_unlock(&routing_table_lock); |
| 1228 | pr_err("%s: rt_entry allocation failed\n", __func__); |
| 1229 | return -ENOMEM; |
| 1230 | } |
| 1231 | add_routing_table_entry(rt_entry); |
| 1232 | } |
| 1233 | mutex_lock(&rt_entry->lock); |
| 1234 | rt_entry->neighbor_node_id = xprt_info->remote_node_id; |
| 1235 | rt_entry->xprt_info = xprt_info; |
| 1236 | mutex_unlock(&rt_entry->lock); |
| 1237 | mutex_unlock(&routing_table_lock); |
| 1238 | |
| 1239 | /* Cleanup any remote ports, if the node is coming out of reset */ |
| 1240 | msm_ipc_cleanup_remote_port_info(xprt_info->remote_node_id); |
| 1241 | |
| 1242 | /* Send a reply HELLO message */ |
| 1243 | memset(&ctl, 0, sizeof(ctl)); |
| 1244 | ctl.cmd = IPC_ROUTER_CTRL_CMD_HELLO; |
| 1245 | rc = msm_ipc_router_send_control_msg(xprt_info, &ctl); |
| 1246 | if (rc < 0) { |
| 1247 | pr_err("%s: Error sending reply HELLO message\n", __func__); |
| 1248 | return rc; |
| 1249 | } |
| 1250 | xprt_info->initialized = 1; |
| 1251 | |
| 1252 | /* |
| 1253 | * Send list of servers from the local node and from nodes |
| 1254 | * outside the mesh network in which this XPRT is part of. |
| 1255 | */ |
Karthikeyan Ramasubramanian | e8ad535 | 2012-10-31 15:12:08 -0600 | [diff] [blame] | 1256 | mutex_lock(&server_list_lock); |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1257 | mutex_lock(&routing_table_lock); |
| 1258 | for (i = 0; i < RT_HASH_SIZE; i++) { |
| 1259 | list_for_each_entry(rt_entry, &routing_table[i], list) { |
| 1260 | if ((rt_entry->node_id != IPC_ROUTER_NID_LOCAL) && |
| 1261 | (rt_entry->xprt_info->xprt->link_id == |
| 1262 | xprt_info->xprt->link_id)) |
| 1263 | continue; |
| 1264 | rc = msm_ipc_router_send_server_list(rt_entry->node_id, |
| 1265 | xprt_info); |
| 1266 | if (rc < 0) { |
| 1267 | mutex_unlock(&routing_table_lock); |
Karthikeyan Ramasubramanian | e8ad535 | 2012-10-31 15:12:08 -0600 | [diff] [blame] | 1268 | mutex_unlock(&server_list_lock); |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1269 | return rc; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | mutex_unlock(&routing_table_lock); |
Karthikeyan Ramasubramanian | e8ad535 | 2012-10-31 15:12:08 -0600 | [diff] [blame] | 1274 | mutex_unlock(&server_list_lock); |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1275 | RR("HELLO message processed\n"); |
| 1276 | return rc; |
| 1277 | } |
| 1278 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1279 | static int process_control_msg(struct msm_ipc_router_xprt_info *xprt_info, |
| 1280 | struct rr_packet *pkt) |
| 1281 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1282 | union rr_control_msg *msg; |
| 1283 | struct msm_ipc_router_remote_port *rport_ptr; |
| 1284 | int rc = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1285 | struct sk_buff *temp_ptr; |
| 1286 | struct rr_header *hdr; |
| 1287 | struct msm_ipc_server *server; |
| 1288 | struct msm_ipc_routing_table_entry *rt_entry; |
| 1289 | |
| 1290 | if (pkt->length != (IPC_ROUTER_HDR_SIZE + sizeof(*msg))) { |
| 1291 | pr_err("%s: r2r msg size %d != %d\n", __func__, pkt->length, |
| 1292 | (IPC_ROUTER_HDR_SIZE + sizeof(*msg))); |
| 1293 | return -EINVAL; |
| 1294 | } |
| 1295 | |
| 1296 | temp_ptr = skb_peek(pkt->pkt_fragment_q); |
Karthikeyan Ramasubramanian | 9024dd8 | 2011-12-19 18:44:19 -0700 | [diff] [blame] | 1297 | if (!temp_ptr) { |
| 1298 | pr_err("%s: pkt_fragment_q is empty\n", __func__); |
| 1299 | return -EINVAL; |
| 1300 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1301 | hdr = (struct rr_header *)temp_ptr->data; |
Karthikeyan Ramasubramanian | 9024dd8 | 2011-12-19 18:44:19 -0700 | [diff] [blame] | 1302 | if (!hdr) { |
| 1303 | pr_err("%s: No data inside the skb\n", __func__); |
| 1304 | return -EINVAL; |
| 1305 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1306 | msg = (union rr_control_msg *)((char *)hdr + IPC_ROUTER_HDR_SIZE); |
| 1307 | |
| 1308 | switch (msg->cmd) { |
| 1309 | case IPC_ROUTER_CTRL_CMD_HELLO: |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1310 | rc = process_hello_msg(xprt_info, hdr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1311 | break; |
Karthikeyan Ramasubramanian | b234c24 | 2012-10-23 13:12:44 -0600 | [diff] [blame] | 1312 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1313 | case IPC_ROUTER_CTRL_CMD_RESUME_TX: |
| 1314 | RR("o RESUME_TX id=%d:%08x\n", |
| 1315 | msg->cli.node_id, msg->cli.port_id); |
| 1316 | |
| 1317 | rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id, |
| 1318 | msg->cli.port_id); |
| 1319 | if (!rport_ptr) { |
| 1320 | pr_err("%s: Unable to resume client\n", __func__); |
| 1321 | break; |
| 1322 | } |
| 1323 | mutex_lock(&rport_ptr->quota_lock); |
| 1324 | rport_ptr->tx_quota_cnt = 0; |
| 1325 | mutex_unlock(&rport_ptr->quota_lock); |
| 1326 | wake_up(&rport_ptr->quota_wait); |
| 1327 | break; |
| 1328 | |
| 1329 | case IPC_ROUTER_CTRL_CMD_NEW_SERVER: |
| 1330 | if (msg->srv.instance == 0) { |
| 1331 | pr_err( |
| 1332 | "rpcrouter: Server create rejected, version = 0, " |
| 1333 | "service = %08x\n", msg->srv.service); |
| 1334 | break; |
| 1335 | } |
| 1336 | |
| 1337 | RR("o NEW_SERVER id=%d:%08x service=%08x:%08x\n", |
| 1338 | msg->srv.node_id, msg->srv.port_id, |
| 1339 | msg->srv.service, msg->srv.instance); |
| 1340 | |
| 1341 | mutex_lock(&routing_table_lock); |
| 1342 | rt_entry = lookup_routing_table(msg->srv.node_id); |
| 1343 | if (!rt_entry) { |
| 1344 | rt_entry = alloc_routing_table_entry(msg->srv.node_id); |
| 1345 | if (!rt_entry) { |
| 1346 | mutex_unlock(&routing_table_lock); |
| 1347 | pr_err("%s: rt_entry allocation failed\n", |
| 1348 | __func__); |
| 1349 | return -ENOMEM; |
| 1350 | } |
| 1351 | mutex_lock(&rt_entry->lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1352 | rt_entry->neighbor_node_id = xprt_info->remote_node_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1353 | rt_entry->xprt_info = xprt_info; |
| 1354 | mutex_unlock(&rt_entry->lock); |
| 1355 | add_routing_table_entry(rt_entry); |
| 1356 | } |
| 1357 | mutex_unlock(&routing_table_lock); |
| 1358 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1359 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1360 | server = msm_ipc_router_lookup_server(msg->srv.service, |
| 1361 | msg->srv.instance, |
| 1362 | msg->srv.node_id, |
| 1363 | msg->srv.port_id); |
| 1364 | if (!server) { |
| 1365 | server = msm_ipc_router_create_server( |
| 1366 | msg->srv.service, msg->srv.instance, |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1367 | msg->srv.node_id, msg->srv.port_id, xprt_info); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1368 | if (!server) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1369 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1370 | pr_err("%s: Server Create failed\n", __func__); |
| 1371 | return -ENOMEM; |
| 1372 | } |
| 1373 | |
| 1374 | if (!msm_ipc_router_lookup_remote_port( |
| 1375 | msg->srv.node_id, msg->srv.port_id)) { |
| 1376 | rport_ptr = msm_ipc_router_create_remote_port( |
| 1377 | msg->srv.node_id, msg->srv.port_id); |
| 1378 | if (!rport_ptr) |
| 1379 | pr_err("%s: Remote port create " |
| 1380 | "failed\n", __func__); |
| 1381 | } |
| 1382 | wake_up(&newserver_wait); |
| 1383 | } |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1384 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1385 | |
| 1386 | relay_msg(xprt_info, pkt); |
| 1387 | post_control_ports(pkt); |
| 1388 | break; |
| 1389 | case IPC_ROUTER_CTRL_CMD_REMOVE_SERVER: |
| 1390 | RR("o REMOVE_SERVER service=%08x:%d\n", |
| 1391 | msg->srv.service, msg->srv.instance); |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1392 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1393 | server = msm_ipc_router_lookup_server(msg->srv.service, |
| 1394 | msg->srv.instance, |
| 1395 | msg->srv.node_id, |
| 1396 | msg->srv.port_id); |
| 1397 | if (server) { |
| 1398 | msm_ipc_router_destroy_server(server, |
| 1399 | msg->srv.node_id, |
| 1400 | msg->srv.port_id); |
| 1401 | relay_msg(xprt_info, pkt); |
| 1402 | post_control_ports(pkt); |
| 1403 | } |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1404 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1405 | break; |
| 1406 | case IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT: |
| 1407 | RR("o REMOVE_CLIENT id=%d:%08x\n", |
| 1408 | msg->cli.node_id, msg->cli.port_id); |
| 1409 | rport_ptr = msm_ipc_router_lookup_remote_port(msg->cli.node_id, |
| 1410 | msg->cli.port_id); |
| 1411 | if (rport_ptr) |
| 1412 | msm_ipc_router_destroy_remote_port(rport_ptr); |
| 1413 | |
| 1414 | relay_msg(xprt_info, pkt); |
| 1415 | post_control_ports(pkt); |
| 1416 | break; |
| 1417 | case IPC_ROUTER_CTRL_CMD_PING: |
| 1418 | /* No action needed for ping messages received */ |
| 1419 | RR("o PING\n"); |
| 1420 | break; |
| 1421 | default: |
| 1422 | RR("o UNKNOWN(%08x)\n", msg->cmd); |
| 1423 | rc = -ENOSYS; |
| 1424 | } |
| 1425 | |
| 1426 | return rc; |
| 1427 | } |
| 1428 | |
| 1429 | static void do_read_data(struct work_struct *work) |
| 1430 | { |
| 1431 | struct rr_header *hdr; |
| 1432 | struct rr_packet *pkt = NULL; |
| 1433 | struct msm_ipc_port *port_ptr; |
| 1434 | struct sk_buff *head_skb; |
| 1435 | struct msm_ipc_port_addr *src_addr; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1436 | struct msm_ipc_router_remote_port *rport_ptr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1437 | uint32_t resume_tx, resume_tx_node_id, resume_tx_port_id; |
| 1438 | |
| 1439 | struct msm_ipc_router_xprt_info *xprt_info = |
| 1440 | container_of(work, |
| 1441 | struct msm_ipc_router_xprt_info, |
| 1442 | read_data); |
| 1443 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1444 | while ((pkt = rr_read(xprt_info)) != NULL) { |
| 1445 | if (pkt->length < IPC_ROUTER_HDR_SIZE || |
| 1446 | pkt->length > MAX_IPC_PKT_SIZE) { |
| 1447 | pr_err("%s: Invalid pkt length %d\n", |
| 1448 | __func__, pkt->length); |
| 1449 | goto fail_data; |
| 1450 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1451 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1452 | head_skb = skb_peek(pkt->pkt_fragment_q); |
| 1453 | if (!head_skb) { |
| 1454 | pr_err("%s: head_skb is invalid\n", __func__); |
| 1455 | goto fail_data; |
| 1456 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1457 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1458 | hdr = (struct rr_header *)(head_skb->data); |
| 1459 | RR("- ver=%d type=%d src=%d:%08x crx=%d siz=%d dst=%d:%08x\n", |
| 1460 | hdr->version, hdr->type, hdr->src_node_id, hdr->src_port_id, |
| 1461 | hdr->confirm_rx, hdr->size, hdr->dst_node_id, |
| 1462 | hdr->dst_port_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1463 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1464 | if (hdr->version != IPC_ROUTER_VERSION) { |
| 1465 | pr_err("version %d != %d\n", |
| 1466 | hdr->version, IPC_ROUTER_VERSION); |
| 1467 | goto fail_data; |
| 1468 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1469 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1470 | if ((hdr->dst_node_id != IPC_ROUTER_NID_LOCAL) && |
| 1471 | ((hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX) || |
| 1472 | (hdr->type == IPC_ROUTER_CTRL_CMD_DATA))) { |
| 1473 | forward_msg(xprt_info, pkt); |
| 1474 | release_pkt(pkt); |
| 1475 | continue; |
| 1476 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1477 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1478 | if ((hdr->dst_port_id == IPC_ROUTER_ADDRESS) || |
| 1479 | (hdr->type == IPC_ROUTER_CTRL_CMD_HELLO)) { |
| 1480 | process_control_msg(xprt_info, pkt); |
| 1481 | release_pkt(pkt); |
| 1482 | continue; |
| 1483 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1484 | #if defined(CONFIG_MSM_SMD_LOGGING) |
| 1485 | #if defined(DEBUG) |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1486 | if (msm_ipc_router_debug_mask & SMEM_LOG) { |
| 1487 | smem_log_event((SMEM_LOG_PROC_ID_APPS | |
| 1488 | SMEM_LOG_RPC_ROUTER_EVENT_BASE | |
| 1489 | IPC_ROUTER_LOG_EVENT_RX), |
| 1490 | (hdr->src_node_id << 24) | |
| 1491 | (hdr->src_port_id & 0xffffff), |
| 1492 | (hdr->dst_node_id << 24) | |
| 1493 | (hdr->dst_port_id & 0xffffff), |
| 1494 | (hdr->type << 24) | (hdr->confirm_rx << 16) | |
| 1495 | (hdr->size & 0xffff)); |
| 1496 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1497 | #endif |
| 1498 | #endif |
| 1499 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1500 | resume_tx = hdr->confirm_rx; |
| 1501 | resume_tx_node_id = hdr->dst_node_id; |
| 1502 | resume_tx_port_id = hdr->dst_port_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1503 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1504 | rport_ptr = msm_ipc_router_lookup_remote_port(hdr->src_node_id, |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 1505 | hdr->src_port_id); |
| 1506 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1507 | mutex_lock(&local_ports_lock); |
| 1508 | port_ptr = msm_ipc_router_lookup_local_port(hdr->dst_port_id); |
| 1509 | if (!port_ptr) { |
| 1510 | pr_err("%s: No local port id %08x\n", __func__, |
| 1511 | hdr->dst_port_id); |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 1512 | mutex_unlock(&local_ports_lock); |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1513 | release_pkt(pkt); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1514 | goto process_done; |
| 1515 | } |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1516 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1517 | if (!rport_ptr) { |
| 1518 | rport_ptr = msm_ipc_router_create_remote_port( |
| 1519 | hdr->src_node_id, |
| 1520 | hdr->src_port_id); |
| 1521 | if (!rport_ptr) { |
| 1522 | pr_err("%s: Rmt Prt %08x:%08x create failed\n", |
| 1523 | __func__, hdr->src_node_id, |
| 1524 | hdr->src_port_id); |
| 1525 | mutex_unlock(&local_ports_lock); |
| 1526 | goto process_done; |
| 1527 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1528 | } |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1529 | |
| 1530 | if (!port_ptr->notify) { |
| 1531 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1532 | wake_lock(&port_ptr->port_rx_wake_lock); |
| 1533 | list_add_tail(&pkt->list, &port_ptr->port_rx_q); |
| 1534 | wake_up(&port_ptr->port_rx_wait_q); |
| 1535 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1536 | mutex_unlock(&local_ports_lock); |
| 1537 | } else { |
| 1538 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1539 | src_addr = kmalloc(sizeof(struct msm_ipc_port_addr), |
| 1540 | GFP_KERNEL); |
| 1541 | if (src_addr) { |
| 1542 | src_addr->node_id = hdr->src_node_id; |
| 1543 | src_addr->port_id = hdr->src_port_id; |
| 1544 | } |
| 1545 | skb_pull(head_skb, IPC_ROUTER_HDR_SIZE); |
| 1546 | mutex_unlock(&local_ports_lock); |
| 1547 | port_ptr->notify(MSM_IPC_ROUTER_READ_CB, |
| 1548 | pkt->pkt_fragment_q, src_addr, port_ptr->priv); |
| 1549 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1550 | pkt->pkt_fragment_q = NULL; |
| 1551 | src_addr = NULL; |
| 1552 | release_pkt(pkt); |
| 1553 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1554 | |
| 1555 | process_done: |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1556 | if (resume_tx) { |
| 1557 | union rr_control_msg msg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1558 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1559 | msg.cmd = IPC_ROUTER_CTRL_CMD_RESUME_TX; |
| 1560 | msg.cli.node_id = resume_tx_node_id; |
| 1561 | msg.cli.port_id = resume_tx_port_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1562 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 1563 | RR("x RESUME_TX id=%d:%08x\n", |
| 1564 | msg.cli.node_id, msg.cli.port_id); |
| 1565 | msm_ipc_router_send_control_msg(xprt_info, &msg); |
| 1566 | } |
| 1567 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1568 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1569 | return; |
| 1570 | |
| 1571 | fail_data: |
| 1572 | release_pkt(pkt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1573 | pr_err("ipc_router has died\n"); |
| 1574 | } |
| 1575 | |
| 1576 | int msm_ipc_router_register_server(struct msm_ipc_port *port_ptr, |
| 1577 | struct msm_ipc_addr *name) |
| 1578 | { |
| 1579 | struct msm_ipc_server *server; |
| 1580 | unsigned long flags; |
| 1581 | union rr_control_msg ctl; |
| 1582 | |
| 1583 | if (!port_ptr || !name) |
| 1584 | return -EINVAL; |
| 1585 | |
| 1586 | if (name->addrtype != MSM_IPC_ADDR_NAME) |
| 1587 | return -EINVAL; |
| 1588 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1589 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1590 | server = msm_ipc_router_lookup_server(name->addr.port_name.service, |
| 1591 | name->addr.port_name.instance, |
| 1592 | IPC_ROUTER_NID_LOCAL, |
| 1593 | port_ptr->this_port.port_id); |
| 1594 | if (server) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1595 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1596 | pr_err("%s: Server already present\n", __func__); |
| 1597 | return -EINVAL; |
| 1598 | } |
| 1599 | |
| 1600 | server = msm_ipc_router_create_server(name->addr.port_name.service, |
| 1601 | name->addr.port_name.instance, |
| 1602 | IPC_ROUTER_NID_LOCAL, |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1603 | port_ptr->this_port.port_id, |
| 1604 | NULL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1605 | if (!server) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1606 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1607 | pr_err("%s: Server Creation failed\n", __func__); |
| 1608 | return -EINVAL; |
| 1609 | } |
| 1610 | |
| 1611 | ctl.cmd = IPC_ROUTER_CTRL_CMD_NEW_SERVER; |
| 1612 | ctl.srv.service = server->name.service; |
| 1613 | ctl.srv.instance = server->name.instance; |
| 1614 | ctl.srv.node_id = IPC_ROUTER_NID_LOCAL; |
| 1615 | ctl.srv.port_id = port_ptr->this_port.port_id; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1616 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1617 | broadcast_ctl_msg(&ctl); |
| 1618 | spin_lock_irqsave(&port_ptr->port_lock, flags); |
| 1619 | port_ptr->type = SERVER_PORT; |
| 1620 | port_ptr->port_name.service = server->name.service; |
| 1621 | port_ptr->port_name.instance = server->name.instance; |
| 1622 | spin_unlock_irqrestore(&port_ptr->port_lock, flags); |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | int msm_ipc_router_unregister_server(struct msm_ipc_port *port_ptr) |
| 1627 | { |
| 1628 | struct msm_ipc_server *server; |
| 1629 | unsigned long flags; |
| 1630 | union rr_control_msg ctl; |
| 1631 | |
| 1632 | if (!port_ptr) |
| 1633 | return -EINVAL; |
| 1634 | |
| 1635 | if (port_ptr->type != SERVER_PORT) { |
| 1636 | pr_err("%s: Trying to unregister a non-server port\n", |
| 1637 | __func__); |
| 1638 | return -EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | if (port_ptr->this_port.node_id != IPC_ROUTER_NID_LOCAL) { |
| 1642 | pr_err("%s: Trying to unregister a remote server locally\n", |
| 1643 | __func__); |
| 1644 | return -EINVAL; |
| 1645 | } |
| 1646 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1647 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1648 | server = msm_ipc_router_lookup_server(port_ptr->port_name.service, |
| 1649 | port_ptr->port_name.instance, |
| 1650 | port_ptr->this_port.node_id, |
| 1651 | port_ptr->this_port.port_id); |
| 1652 | if (!server) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1653 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1654 | pr_err("%s: Server lookup failed\n", __func__); |
| 1655 | return -ENODEV; |
| 1656 | } |
| 1657 | |
| 1658 | ctl.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER; |
| 1659 | ctl.srv.service = server->name.service; |
| 1660 | ctl.srv.instance = server->name.instance; |
| 1661 | ctl.srv.node_id = IPC_ROUTER_NID_LOCAL; |
| 1662 | ctl.srv.port_id = port_ptr->this_port.port_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1663 | msm_ipc_router_destroy_server(server, port_ptr->this_port.node_id, |
| 1664 | port_ptr->this_port.port_id); |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1665 | mutex_unlock(&server_list_lock); |
| 1666 | broadcast_ctl_msg(&ctl); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1667 | spin_lock_irqsave(&port_ptr->port_lock, flags); |
| 1668 | port_ptr->type = CLIENT_PORT; |
| 1669 | spin_unlock_irqrestore(&port_ptr->port_lock, flags); |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static int loopback_data(struct msm_ipc_port *src, |
| 1674 | uint32_t port_id, |
| 1675 | struct sk_buff_head *data) |
| 1676 | { |
| 1677 | struct sk_buff *head_skb; |
| 1678 | struct rr_header *hdr; |
| 1679 | struct msm_ipc_port *port_ptr; |
| 1680 | struct rr_packet *pkt; |
| 1681 | |
| 1682 | if (!data) { |
| 1683 | pr_err("%s: Invalid pkt pointer\n", __func__); |
| 1684 | return -EINVAL; |
| 1685 | } |
| 1686 | |
| 1687 | pkt = create_pkt(data); |
| 1688 | if (!pkt) { |
| 1689 | pr_err("%s: New pkt create failed\n", __func__); |
| 1690 | return -ENOMEM; |
| 1691 | } |
| 1692 | |
| 1693 | head_skb = skb_peek(pkt->pkt_fragment_q); |
Karthikeyan Ramasubramanian | 9024dd8 | 2011-12-19 18:44:19 -0700 | [diff] [blame] | 1694 | if (!head_skb) { |
| 1695 | pr_err("%s: pkt_fragment_q is empty\n", __func__); |
| 1696 | return -EINVAL; |
| 1697 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1698 | hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE); |
| 1699 | if (!hdr) { |
| 1700 | pr_err("%s: Prepend Header failed\n", __func__); |
| 1701 | release_pkt(pkt); |
| 1702 | return -ENOMEM; |
| 1703 | } |
| 1704 | hdr->version = IPC_ROUTER_VERSION; |
| 1705 | hdr->type = IPC_ROUTER_CTRL_CMD_DATA; |
| 1706 | hdr->src_node_id = src->this_port.node_id; |
| 1707 | hdr->src_port_id = src->this_port.port_id; |
| 1708 | hdr->size = pkt->length; |
| 1709 | hdr->confirm_rx = 0; |
| 1710 | hdr->dst_node_id = IPC_ROUTER_NID_LOCAL; |
| 1711 | hdr->dst_port_id = port_id; |
| 1712 | pkt->length += IPC_ROUTER_HDR_SIZE; |
| 1713 | |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 1714 | mutex_lock(&local_ports_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1715 | port_ptr = msm_ipc_router_lookup_local_port(port_id); |
| 1716 | if (!port_ptr) { |
| 1717 | pr_err("%s: Local port %d not present\n", __func__, port_id); |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 1718 | mutex_unlock(&local_ports_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1719 | release_pkt(pkt); |
| 1720 | return -ENODEV; |
| 1721 | } |
| 1722 | |
| 1723 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1724 | wake_lock(&port_ptr->port_rx_wake_lock); |
| 1725 | list_add_tail(&pkt->list, &port_ptr->port_rx_q); |
| 1726 | wake_up(&port_ptr->port_rx_wait_q); |
| 1727 | mutex_unlock(&port_ptr->port_rx_q_lock); |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 1728 | mutex_unlock(&local_ports_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1729 | |
| 1730 | return pkt->length; |
| 1731 | } |
| 1732 | |
| 1733 | static int msm_ipc_router_write_pkt(struct msm_ipc_port *src, |
| 1734 | struct msm_ipc_router_remote_port *rport_ptr, |
| 1735 | struct rr_packet *pkt) |
| 1736 | { |
| 1737 | struct sk_buff *head_skb; |
| 1738 | struct rr_header *hdr; |
| 1739 | struct msm_ipc_router_xprt_info *xprt_info; |
| 1740 | struct msm_ipc_routing_table_entry *rt_entry; |
| 1741 | int ret; |
| 1742 | DEFINE_WAIT(__wait); |
| 1743 | |
| 1744 | if (!rport_ptr || !src || !pkt) |
| 1745 | return -EINVAL; |
| 1746 | |
| 1747 | head_skb = skb_peek(pkt->pkt_fragment_q); |
Karthikeyan Ramasubramanian | 9024dd8 | 2011-12-19 18:44:19 -0700 | [diff] [blame] | 1748 | if (!head_skb) { |
| 1749 | pr_err("%s: pkt_fragment_q is empty\n", __func__); |
| 1750 | return -EINVAL; |
| 1751 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1752 | hdr = (struct rr_header *)skb_push(head_skb, IPC_ROUTER_HDR_SIZE); |
| 1753 | if (!hdr) { |
| 1754 | pr_err("%s: Prepend Header failed\n", __func__); |
| 1755 | return -ENOMEM; |
| 1756 | } |
| 1757 | hdr->version = IPC_ROUTER_VERSION; |
| 1758 | hdr->type = IPC_ROUTER_CTRL_CMD_DATA; |
| 1759 | hdr->src_node_id = src->this_port.node_id; |
| 1760 | hdr->src_port_id = src->this_port.port_id; |
| 1761 | hdr->size = pkt->length; |
| 1762 | hdr->confirm_rx = 0; |
| 1763 | hdr->dst_node_id = rport_ptr->node_id; |
| 1764 | hdr->dst_port_id = rport_ptr->port_id; |
| 1765 | pkt->length += IPC_ROUTER_HDR_SIZE; |
| 1766 | |
| 1767 | for (;;) { |
| 1768 | prepare_to_wait(&rport_ptr->quota_wait, &__wait, |
| 1769 | TASK_INTERRUPTIBLE); |
| 1770 | mutex_lock(&rport_ptr->quota_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1771 | if (rport_ptr->restart_state != RESTART_NORMAL) |
| 1772 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1773 | if (rport_ptr->tx_quota_cnt < |
| 1774 | IPC_ROUTER_DEFAULT_RX_QUOTA) |
| 1775 | break; |
| 1776 | if (signal_pending(current)) |
| 1777 | break; |
| 1778 | mutex_unlock(&rport_ptr->quota_lock); |
| 1779 | schedule(); |
| 1780 | } |
| 1781 | finish_wait(&rport_ptr->quota_wait, &__wait); |
| 1782 | |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1783 | if (rport_ptr->restart_state != RESTART_NORMAL) { |
| 1784 | mutex_unlock(&rport_ptr->quota_lock); |
| 1785 | return -ENETRESET; |
| 1786 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1787 | if (signal_pending(current)) { |
| 1788 | mutex_unlock(&rport_ptr->quota_lock); |
| 1789 | return -ERESTARTSYS; |
| 1790 | } |
| 1791 | rport_ptr->tx_quota_cnt++; |
| 1792 | if (rport_ptr->tx_quota_cnt == IPC_ROUTER_DEFAULT_RX_QUOTA) |
| 1793 | hdr->confirm_rx = 1; |
| 1794 | mutex_unlock(&rport_ptr->quota_lock); |
| 1795 | |
| 1796 | mutex_lock(&routing_table_lock); |
| 1797 | rt_entry = lookup_routing_table(hdr->dst_node_id); |
| 1798 | if (!rt_entry || !rt_entry->xprt_info) { |
| 1799 | mutex_unlock(&routing_table_lock); |
| 1800 | pr_err("%s: Remote node %d not up\n", |
| 1801 | __func__, hdr->dst_node_id); |
| 1802 | return -ENODEV; |
| 1803 | } |
| 1804 | mutex_lock(&rt_entry->lock); |
| 1805 | xprt_info = rt_entry->xprt_info; |
| 1806 | mutex_lock(&xprt_info->tx_lock); |
Karthikeyan Ramasubramanian | 8cec592 | 2012-02-16 17:41:58 -0700 | [diff] [blame] | 1807 | ret = xprt_info->xprt->write(pkt, pkt->length, xprt_info->xprt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1808 | mutex_unlock(&xprt_info->tx_lock); |
| 1809 | mutex_unlock(&rt_entry->lock); |
| 1810 | mutex_unlock(&routing_table_lock); |
| 1811 | |
| 1812 | if (ret < 0) { |
| 1813 | pr_err("%s: Write on XPRT failed\n", __func__); |
| 1814 | return ret; |
| 1815 | } |
| 1816 | |
| 1817 | RAW_HDR("[w rr_h] " |
| 1818 | "ver=%i,type=%s,src_nid=%08x,src_port_id=%08x," |
| 1819 | "confirm_rx=%i,size=%3i,dst_pid=%08x,dst_cid=%08x\n", |
| 1820 | hdr->version, type_to_str(hdr->type), |
| 1821 | hdr->src_node_id, hdr->src_port_id, |
| 1822 | hdr->confirm_rx, hdr->size, |
| 1823 | hdr->dst_node_id, hdr->dst_port_id); |
| 1824 | |
| 1825 | #if defined(CONFIG_MSM_SMD_LOGGING) |
| 1826 | #if defined(DEBUG) |
| 1827 | if (msm_ipc_router_debug_mask & SMEM_LOG) { |
| 1828 | smem_log_event((SMEM_LOG_PROC_ID_APPS | |
| 1829 | SMEM_LOG_RPC_ROUTER_EVENT_BASE | |
| 1830 | IPC_ROUTER_LOG_EVENT_TX), |
| 1831 | (hdr->src_node_id << 24) | |
| 1832 | (hdr->src_port_id & 0xffffff), |
| 1833 | (hdr->dst_node_id << 24) | |
| 1834 | (hdr->dst_port_id & 0xffffff), |
| 1835 | (hdr->type << 24) | (hdr->confirm_rx << 16) | |
| 1836 | (hdr->size & 0xffff)); |
| 1837 | } |
| 1838 | #endif |
| 1839 | #endif |
| 1840 | |
| 1841 | return pkt->length; |
| 1842 | } |
| 1843 | |
| 1844 | int msm_ipc_router_send_to(struct msm_ipc_port *src, |
| 1845 | struct sk_buff_head *data, |
| 1846 | struct msm_ipc_addr *dest) |
| 1847 | { |
| 1848 | uint32_t dst_node_id = 0, dst_port_id = 0; |
| 1849 | struct msm_ipc_server *server; |
| 1850 | struct msm_ipc_server_port *server_port; |
| 1851 | struct msm_ipc_router_remote_port *rport_ptr = NULL; |
| 1852 | struct rr_packet *pkt; |
| 1853 | int ret; |
| 1854 | |
| 1855 | if (!src || !data || !dest) { |
| 1856 | pr_err("%s: Invalid Parameters\n", __func__); |
| 1857 | return -EINVAL; |
| 1858 | } |
| 1859 | |
| 1860 | /* Resolve Address*/ |
| 1861 | if (dest->addrtype == MSM_IPC_ADDR_ID) { |
| 1862 | dst_node_id = dest->addr.port_addr.node_id; |
| 1863 | dst_port_id = dest->addr.port_addr.port_id; |
| 1864 | } else if (dest->addrtype == MSM_IPC_ADDR_NAME) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1865 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1866 | server = msm_ipc_router_lookup_server( |
| 1867 | dest->addr.port_name.service, |
| 1868 | dest->addr.port_name.instance, |
| 1869 | 0, 0); |
| 1870 | if (!server) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 1871 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1872 | pr_err("%s: Destination not reachable\n", __func__); |
| 1873 | return -ENODEV; |
| 1874 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1875 | server_port = list_first_entry(&server->server_port_list, |
| 1876 | struct msm_ipc_server_port, |
| 1877 | list); |
| 1878 | dst_node_id = server_port->server_addr.node_id; |
| 1879 | dst_port_id = server_port->server_addr.port_id; |
| 1880 | mutex_unlock(&server_list_lock); |
| 1881 | } |
| 1882 | if (dst_node_id == IPC_ROUTER_NID_LOCAL) { |
| 1883 | ret = loopback_data(src, dst_port_id, data); |
| 1884 | return ret; |
| 1885 | } |
| 1886 | |
| 1887 | /* Achieve Flow control */ |
| 1888 | rport_ptr = msm_ipc_router_lookup_remote_port(dst_node_id, |
| 1889 | dst_port_id); |
| 1890 | if (!rport_ptr) { |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 1891 | pr_err("%s: Could not create remote port\n", __func__); |
| 1892 | return -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | pkt = create_pkt(data); |
| 1896 | if (!pkt) { |
| 1897 | pr_err("%s: Pkt creation failed\n", __func__); |
| 1898 | return -ENOMEM; |
| 1899 | } |
| 1900 | |
| 1901 | ret = msm_ipc_router_write_pkt(src, rport_ptr, pkt); |
| 1902 | release_pkt(pkt); |
| 1903 | |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | int msm_ipc_router_read(struct msm_ipc_port *port_ptr, |
| 1908 | struct sk_buff_head **data, |
| 1909 | size_t buf_len) |
| 1910 | { |
| 1911 | struct rr_packet *pkt; |
| 1912 | int ret; |
| 1913 | |
| 1914 | if (!port_ptr || !data) |
| 1915 | return -EINVAL; |
| 1916 | |
| 1917 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1918 | if (list_empty(&port_ptr->port_rx_q)) { |
| 1919 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1920 | return -EAGAIN; |
| 1921 | } |
| 1922 | |
| 1923 | pkt = list_first_entry(&port_ptr->port_rx_q, struct rr_packet, list); |
| 1924 | if ((buf_len) && ((pkt->length - IPC_ROUTER_HDR_SIZE) > buf_len)) { |
| 1925 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1926 | return -ETOOSMALL; |
| 1927 | } |
| 1928 | list_del(&pkt->list); |
| 1929 | if (list_empty(&port_ptr->port_rx_q)) |
| 1930 | wake_unlock(&port_ptr->port_rx_wake_lock); |
| 1931 | *data = pkt->pkt_fragment_q; |
| 1932 | ret = pkt->length; |
| 1933 | kfree(pkt); |
| 1934 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1935 | |
| 1936 | return ret; |
| 1937 | } |
| 1938 | |
| 1939 | int msm_ipc_router_recv_from(struct msm_ipc_port *port_ptr, |
| 1940 | struct sk_buff_head **data, |
| 1941 | struct msm_ipc_addr *src, |
| 1942 | unsigned long timeout) |
| 1943 | { |
| 1944 | int ret, data_len, align_size; |
| 1945 | struct sk_buff *temp_skb; |
| 1946 | struct rr_header *hdr = NULL; |
| 1947 | |
| 1948 | if (!port_ptr || !data) { |
| 1949 | pr_err("%s: Invalid pointers being passed\n", __func__); |
| 1950 | return -EINVAL; |
| 1951 | } |
| 1952 | |
| 1953 | *data = NULL; |
| 1954 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1955 | while (list_empty(&port_ptr->port_rx_q)) { |
| 1956 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1957 | if (timeout < 0) { |
| 1958 | ret = wait_event_interruptible( |
| 1959 | port_ptr->port_rx_wait_q, |
| 1960 | !list_empty(&port_ptr->port_rx_q)); |
| 1961 | if (ret) |
| 1962 | return ret; |
| 1963 | } else if (timeout > 0) { |
| 1964 | timeout = wait_event_interruptible_timeout( |
| 1965 | port_ptr->port_rx_wait_q, |
| 1966 | !list_empty(&port_ptr->port_rx_q), |
| 1967 | timeout); |
| 1968 | if (timeout < 0) |
| 1969 | return -EFAULT; |
| 1970 | } |
| 1971 | if (timeout == 0) |
| 1972 | return -ETIMEDOUT; |
| 1973 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 1974 | } |
| 1975 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 1976 | |
| 1977 | ret = msm_ipc_router_read(port_ptr, data, 0); |
| 1978 | if (ret <= 0 || !(*data)) |
| 1979 | return ret; |
| 1980 | |
| 1981 | temp_skb = skb_peek(*data); |
| 1982 | hdr = (struct rr_header *)(temp_skb->data); |
| 1983 | if (src) { |
| 1984 | src->addrtype = MSM_IPC_ADDR_ID; |
| 1985 | src->addr.port_addr.node_id = hdr->src_node_id; |
| 1986 | src->addr.port_addr.port_id = hdr->src_port_id; |
| 1987 | } |
| 1988 | |
| 1989 | data_len = hdr->size; |
| 1990 | skb_pull(temp_skb, IPC_ROUTER_HDR_SIZE); |
| 1991 | align_size = ALIGN_SIZE(data_len); |
| 1992 | if (align_size) { |
| 1993 | temp_skb = skb_peek_tail(*data); |
| 1994 | skb_trim(temp_skb, (temp_skb->len - align_size)); |
| 1995 | } |
| 1996 | return data_len; |
| 1997 | } |
| 1998 | |
| 1999 | struct msm_ipc_port *msm_ipc_router_create_port( |
| 2000 | void (*notify)(unsigned event, void *data, void *addr, void *priv), |
| 2001 | void *priv) |
| 2002 | { |
| 2003 | struct msm_ipc_port *port_ptr; |
| 2004 | |
| 2005 | port_ptr = msm_ipc_router_create_raw_port(NULL, notify, priv); |
| 2006 | if (!port_ptr) |
| 2007 | pr_err("%s: port_ptr alloc failed\n", __func__); |
| 2008 | |
| 2009 | return port_ptr; |
| 2010 | } |
| 2011 | |
| 2012 | int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr) |
| 2013 | { |
| 2014 | union rr_control_msg msg; |
| 2015 | struct rr_packet *pkt, *temp_pkt; |
| 2016 | struct msm_ipc_server *server; |
| 2017 | |
| 2018 | if (!port_ptr) |
| 2019 | return -EINVAL; |
| 2020 | |
| 2021 | if (port_ptr->type == SERVER_PORT || port_ptr->type == CLIENT_PORT) { |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 2022 | mutex_lock(&local_ports_lock); |
| 2023 | list_del(&port_ptr->list); |
| 2024 | mutex_unlock(&local_ports_lock); |
| 2025 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2026 | if (port_ptr->type == SERVER_PORT) { |
| 2027 | msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_SERVER; |
| 2028 | msg.srv.service = port_ptr->port_name.service; |
| 2029 | msg.srv.instance = port_ptr->port_name.instance; |
| 2030 | msg.srv.node_id = port_ptr->this_port.node_id; |
| 2031 | msg.srv.port_id = port_ptr->this_port.port_id; |
| 2032 | RR("x REMOVE_SERVER Name=%d:%08x Id=%d:%08x\n", |
| 2033 | msg.srv.service, msg.srv.instance, |
| 2034 | msg.srv.node_id, msg.srv.port_id); |
Karthikeyan Ramasubramanian | 25e9187 | 2013-01-10 17:09:13 -0700 | [diff] [blame^] | 2035 | broadcast_ctl_msg(&msg); |
| 2036 | broadcast_ctl_msg_locally(&msg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2037 | } |
Karthikeyan Ramasubramanian | 25e9187 | 2013-01-10 17:09:13 -0700 | [diff] [blame^] | 2038 | |
| 2039 | /* |
| 2040 | * Server port could have been a client port earlier. |
| 2041 | * Send REMOVE_CLIENT message in either case. |
| 2042 | */ |
| 2043 | msg.cmd = IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT; |
| 2044 | msg.cli.node_id = port_ptr->this_port.node_id; |
| 2045 | msg.cli.port_id = port_ptr->this_port.port_id; |
| 2046 | RR("x REMOVE_CLIENT id=%d:%08x\n", |
| 2047 | msg.cli.node_id, msg.cli.port_id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2048 | broadcast_ctl_msg(&msg); |
| 2049 | broadcast_ctl_msg_locally(&msg); |
Karthikeyan Ramasubramanian | be9954b | 2012-06-13 12:59:54 -0600 | [diff] [blame] | 2050 | } else if (port_ptr->type == CONTROL_PORT) { |
| 2051 | mutex_lock(&control_ports_lock); |
| 2052 | list_del(&port_ptr->list); |
| 2053 | mutex_unlock(&control_ports_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 2057 | list_for_each_entry_safe(pkt, temp_pkt, &port_ptr->port_rx_q, list) { |
| 2058 | list_del(&pkt->list); |
| 2059 | release_pkt(pkt); |
| 2060 | } |
| 2061 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 2062 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2063 | if (port_ptr->type == SERVER_PORT) { |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2064 | mutex_lock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2065 | server = msm_ipc_router_lookup_server( |
| 2066 | port_ptr->port_name.service, |
| 2067 | port_ptr->port_name.instance, |
| 2068 | port_ptr->this_port.node_id, |
| 2069 | port_ptr->this_port.port_id); |
| 2070 | if (server) |
| 2071 | msm_ipc_router_destroy_server(server, |
| 2072 | port_ptr->this_port.node_id, |
| 2073 | port_ptr->this_port.port_id); |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2074 | mutex_unlock(&server_list_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Karthikeyan Ramasubramanian | dd8c3b5 | 2011-11-30 16:26:12 -0700 | [diff] [blame] | 2077 | wake_lock_destroy(&port_ptr->port_rx_wake_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2078 | kfree(port_ptr); |
| 2079 | return 0; |
| 2080 | } |
| 2081 | |
| 2082 | int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr) |
| 2083 | { |
| 2084 | struct rr_packet *pkt; |
| 2085 | int rc = 0; |
| 2086 | |
| 2087 | if (!port_ptr) |
| 2088 | return -EINVAL; |
| 2089 | |
| 2090 | mutex_lock(&port_ptr->port_rx_q_lock); |
| 2091 | if (!list_empty(&port_ptr->port_rx_q)) { |
| 2092 | pkt = list_first_entry(&port_ptr->port_rx_q, |
| 2093 | struct rr_packet, list); |
| 2094 | rc = pkt->length; |
| 2095 | } |
| 2096 | mutex_unlock(&port_ptr->port_rx_q_lock); |
| 2097 | |
| 2098 | return rc; |
| 2099 | } |
| 2100 | |
| 2101 | int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr) |
| 2102 | { |
| 2103 | if (!port_ptr) |
| 2104 | return -EINVAL; |
| 2105 | |
| 2106 | mutex_lock(&local_ports_lock); |
| 2107 | list_del(&port_ptr->list); |
| 2108 | mutex_unlock(&local_ports_lock); |
| 2109 | port_ptr->type = CONTROL_PORT; |
| 2110 | mutex_lock(&control_ports_lock); |
| 2111 | list_add_tail(&port_ptr->list, &control_ports); |
| 2112 | mutex_unlock(&control_ports_lock); |
| 2113 | |
| 2114 | return 0; |
| 2115 | } |
| 2116 | |
| 2117 | int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name, |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 2118 | struct msm_ipc_server_info *srv_info, |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2119 | int num_entries_in_array, |
| 2120 | uint32_t lookup_mask) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2121 | { |
| 2122 | struct msm_ipc_server *server; |
| 2123 | struct msm_ipc_server_port *server_port; |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2124 | int key, i = 0; /*num_entries_found*/ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2125 | |
| 2126 | if (!srv_name) { |
| 2127 | pr_err("%s: Invalid srv_name\n", __func__); |
| 2128 | return -EINVAL; |
| 2129 | } |
| 2130 | |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 2131 | if (num_entries_in_array && !srv_info) { |
| 2132 | pr_err("%s: srv_info NULL\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2133 | return -EINVAL; |
| 2134 | } |
| 2135 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2136 | mutex_lock(&server_list_lock); |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2137 | if (!lookup_mask) |
| 2138 | lookup_mask = 0xFFFFFFFF; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2139 | key = (srv_name->service & (SRV_HASH_SIZE - 1)); |
| 2140 | list_for_each_entry(server, &server_list[key], list) { |
| 2141 | if ((server->name.service != srv_name->service) || |
| 2142 | ((server->name.instance & lookup_mask) != |
| 2143 | srv_name->instance)) |
| 2144 | continue; |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2145 | |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2146 | list_for_each_entry(server_port, |
| 2147 | &server->server_port_list, list) { |
| 2148 | if (i < num_entries_in_array) { |
| 2149 | srv_info[i].node_id = |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2150 | server_port->server_addr.node_id; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2151 | srv_info[i].port_id = |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2152 | server_port->server_addr.port_id; |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2153 | srv_info[i].service = server->name.service; |
| 2154 | srv_info[i].instance = server->name.instance; |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 2155 | } |
Karthikeyan Ramasubramanian | a85d09f | 2012-10-25 15:40:45 -0600 | [diff] [blame] | 2156 | i++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2157 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2158 | } |
| 2159 | mutex_unlock(&server_list_lock); |
| 2160 | |
| 2161 | return i; |
| 2162 | } |
| 2163 | |
| 2164 | int msm_ipc_router_close(void) |
| 2165 | { |
| 2166 | struct msm_ipc_router_xprt_info *xprt_info, *tmp_xprt_info; |
| 2167 | |
| 2168 | mutex_lock(&xprt_info_list_lock); |
| 2169 | list_for_each_entry_safe(xprt_info, tmp_xprt_info, |
| 2170 | &xprt_info_list, list) { |
Karthikeyan Ramasubramanian | 8cec592 | 2012-02-16 17:41:58 -0700 | [diff] [blame] | 2171 | xprt_info->xprt->close(xprt_info->xprt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2172 | list_del(&xprt_info->list); |
| 2173 | kfree(xprt_info); |
| 2174 | } |
| 2175 | mutex_unlock(&xprt_info_list_lock); |
| 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | #if defined(CONFIG_DEBUG_FS) |
| 2180 | static int dump_routing_table(char *buf, int max) |
| 2181 | { |
| 2182 | int i = 0, j; |
| 2183 | struct msm_ipc_routing_table_entry *rt_entry; |
| 2184 | |
| 2185 | for (j = 0; j < RT_HASH_SIZE; j++) { |
| 2186 | mutex_lock(&routing_table_lock); |
| 2187 | list_for_each_entry(rt_entry, &routing_table[j], list) { |
| 2188 | mutex_lock(&rt_entry->lock); |
| 2189 | i += scnprintf(buf + i, max - i, |
| 2190 | "Node Id: 0x%08x\n", rt_entry->node_id); |
Karthikeyan Ramasubramanian | c1a4e3a | 2012-09-10 16:10:24 -0600 | [diff] [blame] | 2191 | if (rt_entry->node_id == IPC_ROUTER_NID_LOCAL) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2192 | i += scnprintf(buf + i, max - i, |
| 2193 | "XPRT Name: Loopback\n"); |
| 2194 | i += scnprintf(buf + i, max - i, |
| 2195 | "Next Hop: %d\n", rt_entry->node_id); |
| 2196 | } else { |
| 2197 | i += scnprintf(buf + i, max - i, |
| 2198 | "XPRT Name: %s\n", |
| 2199 | rt_entry->xprt_info->xprt->name); |
| 2200 | i += scnprintf(buf + i, max - i, |
| 2201 | "Next Hop: 0x%08x\n", |
| 2202 | rt_entry->xprt_info->remote_node_id); |
| 2203 | } |
| 2204 | i += scnprintf(buf + i, max - i, "\n"); |
| 2205 | mutex_unlock(&rt_entry->lock); |
| 2206 | } |
| 2207 | mutex_unlock(&routing_table_lock); |
| 2208 | } |
| 2209 | |
| 2210 | return i; |
| 2211 | } |
| 2212 | |
| 2213 | static int dump_xprt_info(char *buf, int max) |
| 2214 | { |
| 2215 | int i = 0; |
| 2216 | struct msm_ipc_router_xprt_info *xprt_info; |
| 2217 | |
| 2218 | mutex_lock(&xprt_info_list_lock); |
| 2219 | list_for_each_entry(xprt_info, &xprt_info_list, list) { |
| 2220 | i += scnprintf(buf + i, max - i, "XPRT Name: %s\n", |
| 2221 | xprt_info->xprt->name); |
| 2222 | i += scnprintf(buf + i, max - i, "Link Id: %d\n", |
| 2223 | xprt_info->xprt->link_id); |
| 2224 | i += scnprintf(buf + i, max - i, "Initialized: %s\n", |
| 2225 | (xprt_info->initialized ? "Y" : "N")); |
| 2226 | i += scnprintf(buf + i, max - i, "Remote Node Id: 0x%08x\n", |
| 2227 | xprt_info->remote_node_id); |
| 2228 | i += scnprintf(buf + i, max - i, "\n"); |
| 2229 | } |
| 2230 | mutex_unlock(&xprt_info_list_lock); |
| 2231 | |
| 2232 | return i; |
| 2233 | } |
| 2234 | |
| 2235 | static int dump_servers(char *buf, int max) |
| 2236 | { |
| 2237 | int i = 0, j; |
| 2238 | struct msm_ipc_server *server; |
| 2239 | struct msm_ipc_server_port *server_port; |
| 2240 | |
| 2241 | mutex_lock(&server_list_lock); |
| 2242 | for (j = 0; j < SRV_HASH_SIZE; j++) { |
| 2243 | list_for_each_entry(server, &server_list[j], list) { |
| 2244 | list_for_each_entry(server_port, |
| 2245 | &server->server_port_list, |
| 2246 | list) { |
| 2247 | i += scnprintf(buf + i, max - i, "Service: " |
| 2248 | "0x%08x\n", server->name.service); |
| 2249 | i += scnprintf(buf + i, max - i, "Instance: " |
| 2250 | "0x%08x\n", server->name.instance); |
| 2251 | i += scnprintf(buf + i, max - i, |
| 2252 | "Node_id: 0x%08x\n", |
| 2253 | server_port->server_addr.node_id); |
| 2254 | i += scnprintf(buf + i, max - i, |
| 2255 | "Port_id: 0x%08x\n", |
| 2256 | server_port->server_addr.port_id); |
| 2257 | i += scnprintf(buf + i, max - i, "\n"); |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | mutex_unlock(&server_list_lock); |
| 2262 | |
| 2263 | return i; |
| 2264 | } |
| 2265 | |
| 2266 | static int dump_remote_ports(char *buf, int max) |
| 2267 | { |
| 2268 | int i = 0, j, k; |
| 2269 | struct msm_ipc_router_remote_port *rport_ptr; |
| 2270 | struct msm_ipc_routing_table_entry *rt_entry; |
| 2271 | |
| 2272 | for (j = 0; j < RT_HASH_SIZE; j++) { |
| 2273 | mutex_lock(&routing_table_lock); |
| 2274 | list_for_each_entry(rt_entry, &routing_table[j], list) { |
| 2275 | mutex_lock(&rt_entry->lock); |
| 2276 | for (k = 0; k < RP_HASH_SIZE; k++) { |
| 2277 | list_for_each_entry(rport_ptr, |
| 2278 | &rt_entry->remote_port_list[k], |
| 2279 | list) { |
| 2280 | i += scnprintf(buf + i, max - i, |
| 2281 | "Node_id: 0x%08x\n", |
| 2282 | rport_ptr->node_id); |
| 2283 | i += scnprintf(buf + i, max - i, |
| 2284 | "Port_id: 0x%08x\n", |
| 2285 | rport_ptr->port_id); |
| 2286 | i += scnprintf(buf + i, max - i, |
| 2287 | "Quota_cnt: %d\n", |
| 2288 | rport_ptr->tx_quota_cnt); |
| 2289 | i += scnprintf(buf + i, max - i, "\n"); |
| 2290 | } |
| 2291 | } |
| 2292 | mutex_unlock(&rt_entry->lock); |
| 2293 | } |
| 2294 | mutex_unlock(&routing_table_lock); |
| 2295 | } |
| 2296 | |
| 2297 | return i; |
| 2298 | } |
| 2299 | |
| 2300 | static int dump_control_ports(char *buf, int max) |
| 2301 | { |
| 2302 | int i = 0; |
| 2303 | struct msm_ipc_port *port_ptr; |
| 2304 | |
| 2305 | mutex_lock(&control_ports_lock); |
| 2306 | list_for_each_entry(port_ptr, &control_ports, list) { |
| 2307 | i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n", |
| 2308 | port_ptr->this_port.node_id); |
| 2309 | i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n", |
| 2310 | port_ptr->this_port.port_id); |
| 2311 | i += scnprintf(buf + i, max - i, "\n"); |
| 2312 | } |
| 2313 | mutex_unlock(&control_ports_lock); |
| 2314 | |
| 2315 | return i; |
| 2316 | } |
| 2317 | |
| 2318 | static int dump_local_ports(char *buf, int max) |
| 2319 | { |
| 2320 | int i = 0, j; |
| 2321 | unsigned long flags; |
| 2322 | struct msm_ipc_port *port_ptr; |
| 2323 | |
| 2324 | mutex_lock(&local_ports_lock); |
| 2325 | for (j = 0; j < LP_HASH_SIZE; j++) { |
| 2326 | list_for_each_entry(port_ptr, &local_ports[j], list) { |
| 2327 | spin_lock_irqsave(&port_ptr->port_lock, flags); |
| 2328 | i += scnprintf(buf + i, max - i, "Node_id: 0x%08x\n", |
| 2329 | port_ptr->this_port.node_id); |
| 2330 | i += scnprintf(buf + i, max - i, "Port_id: 0x%08x\n", |
| 2331 | port_ptr->this_port.port_id); |
| 2332 | i += scnprintf(buf + i, max - i, "# pkts tx'd %d\n", |
| 2333 | port_ptr->num_tx); |
| 2334 | i += scnprintf(buf + i, max - i, "# pkts rx'd %d\n", |
| 2335 | port_ptr->num_rx); |
| 2336 | i += scnprintf(buf + i, max - i, "# bytes tx'd %ld\n", |
| 2337 | port_ptr->num_tx_bytes); |
| 2338 | i += scnprintf(buf + i, max - i, "# bytes rx'd %ld\n", |
| 2339 | port_ptr->num_rx_bytes); |
| 2340 | spin_unlock_irqrestore(&port_ptr->port_lock, flags); |
| 2341 | i += scnprintf(buf + i, max - i, "\n"); |
| 2342 | } |
| 2343 | } |
| 2344 | mutex_unlock(&local_ports_lock); |
| 2345 | |
| 2346 | return i; |
| 2347 | } |
| 2348 | |
| 2349 | #define DEBUG_BUFMAX 4096 |
| 2350 | static char debug_buffer[DEBUG_BUFMAX]; |
| 2351 | |
| 2352 | static ssize_t debug_read(struct file *file, char __user *buf, |
| 2353 | size_t count, loff_t *ppos) |
| 2354 | { |
| 2355 | int (*fill)(char *buf, int max) = file->private_data; |
| 2356 | int bsize = fill(debug_buffer, DEBUG_BUFMAX); |
| 2357 | return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize); |
| 2358 | } |
| 2359 | |
| 2360 | static int debug_open(struct inode *inode, struct file *file) |
| 2361 | { |
| 2362 | file->private_data = inode->i_private; |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | static const struct file_operations debug_ops = { |
| 2367 | .read = debug_read, |
| 2368 | .open = debug_open, |
| 2369 | }; |
| 2370 | |
| 2371 | static void debug_create(const char *name, mode_t mode, |
| 2372 | struct dentry *dent, |
| 2373 | int (*fill)(char *buf, int max)) |
| 2374 | { |
| 2375 | debugfs_create_file(name, mode, dent, fill, &debug_ops); |
| 2376 | } |
| 2377 | |
| 2378 | static void debugfs_init(void) |
| 2379 | { |
| 2380 | struct dentry *dent; |
| 2381 | |
| 2382 | dent = debugfs_create_dir("msm_ipc_router", 0); |
| 2383 | if (IS_ERR(dent)) |
| 2384 | return; |
| 2385 | |
| 2386 | debug_create("dump_local_ports", 0444, dent, |
| 2387 | dump_local_ports); |
| 2388 | debug_create("dump_remote_ports", 0444, dent, |
| 2389 | dump_remote_ports); |
| 2390 | debug_create("dump_control_ports", 0444, dent, |
| 2391 | dump_control_ports); |
| 2392 | debug_create("dump_servers", 0444, dent, |
| 2393 | dump_servers); |
| 2394 | debug_create("dump_xprt_info", 0444, dent, |
| 2395 | dump_xprt_info); |
| 2396 | debug_create("dump_routing_table", 0444, dent, |
| 2397 | dump_routing_table); |
| 2398 | } |
| 2399 | |
| 2400 | #else |
| 2401 | static void debugfs_init(void) {} |
| 2402 | #endif |
| 2403 | |
| 2404 | static int msm_ipc_router_add_xprt(struct msm_ipc_router_xprt *xprt) |
| 2405 | { |
| 2406 | struct msm_ipc_router_xprt_info *xprt_info; |
| 2407 | struct msm_ipc_routing_table_entry *rt_entry; |
| 2408 | |
| 2409 | xprt_info = kmalloc(sizeof(struct msm_ipc_router_xprt_info), |
| 2410 | GFP_KERNEL); |
| 2411 | if (!xprt_info) |
| 2412 | return -ENOMEM; |
| 2413 | |
| 2414 | xprt_info->xprt = xprt; |
| 2415 | xprt_info->initialized = 0; |
| 2416 | xprt_info->remote_node_id = -1; |
| 2417 | INIT_LIST_HEAD(&xprt_info->pkt_list); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2418 | mutex_init(&xprt_info->rx_lock); |
| 2419 | mutex_init(&xprt_info->tx_lock); |
| 2420 | wake_lock_init(&xprt_info->wakelock, |
| 2421 | WAKE_LOCK_SUSPEND, xprt->name); |
| 2422 | xprt_info->need_len = 0; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2423 | xprt_info->abort_data_read = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2424 | INIT_WORK(&xprt_info->read_data, do_read_data); |
| 2425 | INIT_LIST_HEAD(&xprt_info->list); |
| 2426 | |
| 2427 | xprt_info->workqueue = create_singlethread_workqueue(xprt->name); |
| 2428 | if (!xprt_info->workqueue) { |
| 2429 | kfree(xprt_info); |
| 2430 | return -ENOMEM; |
| 2431 | } |
| 2432 | |
| 2433 | if (!strcmp(xprt->name, "msm_ipc_router_loopback_xprt")) { |
| 2434 | xprt_info->remote_node_id = IPC_ROUTER_NID_LOCAL; |
| 2435 | xprt_info->initialized = 1; |
| 2436 | } |
| 2437 | |
| 2438 | mutex_lock(&xprt_info_list_lock); |
| 2439 | list_add_tail(&xprt_info->list, &xprt_info_list); |
| 2440 | mutex_unlock(&xprt_info_list_lock); |
| 2441 | |
| 2442 | mutex_lock(&routing_table_lock); |
| 2443 | if (!routing_table_inited) { |
| 2444 | init_routing_table(); |
| 2445 | rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL); |
| 2446 | add_routing_table_entry(rt_entry); |
| 2447 | routing_table_inited = 1; |
| 2448 | } |
| 2449 | mutex_unlock(&routing_table_lock); |
| 2450 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2451 | xprt->priv = xprt_info; |
| 2452 | |
| 2453 | return 0; |
| 2454 | } |
| 2455 | |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2456 | static void msm_ipc_router_remove_xprt(struct msm_ipc_router_xprt *xprt) |
| 2457 | { |
| 2458 | struct msm_ipc_router_xprt_info *xprt_info; |
| 2459 | |
| 2460 | if (xprt && xprt->priv) { |
| 2461 | xprt_info = xprt->priv; |
| 2462 | |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 2463 | mutex_lock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2464 | xprt_info->abort_data_read = 1; |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 2465 | mutex_unlock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2466 | |
| 2467 | mutex_lock(&xprt_info_list_lock); |
| 2468 | list_del(&xprt_info->list); |
| 2469 | mutex_unlock(&xprt_info_list_lock); |
| 2470 | |
| 2471 | flush_workqueue(xprt_info->workqueue); |
| 2472 | destroy_workqueue(xprt_info->workqueue); |
| 2473 | wake_lock_destroy(&xprt_info->wakelock); |
| 2474 | |
| 2475 | xprt->priv = 0; |
| 2476 | kfree(xprt_info); |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | |
| 2481 | struct msm_ipc_router_xprt_work { |
| 2482 | struct msm_ipc_router_xprt *xprt; |
| 2483 | struct work_struct work; |
| 2484 | }; |
| 2485 | |
| 2486 | static void xprt_open_worker(struct work_struct *work) |
| 2487 | { |
| 2488 | struct msm_ipc_router_xprt_work *xprt_work = |
| 2489 | container_of(work, struct msm_ipc_router_xprt_work, work); |
| 2490 | |
| 2491 | msm_ipc_router_add_xprt(xprt_work->xprt); |
| 2492 | kfree(xprt_work); |
| 2493 | } |
| 2494 | |
| 2495 | static void xprt_close_worker(struct work_struct *work) |
| 2496 | { |
| 2497 | struct msm_ipc_router_xprt_work *xprt_work = |
| 2498 | container_of(work, struct msm_ipc_router_xprt_work, work); |
| 2499 | |
| 2500 | modem_reset_cleanup(xprt_work->xprt->priv); |
| 2501 | msm_ipc_router_remove_xprt(xprt_work->xprt); |
| 2502 | |
| 2503 | if (atomic_dec_return(&pending_close_count) == 0) |
| 2504 | wake_up(&subsystem_restart_wait); |
| 2505 | |
| 2506 | kfree(xprt_work); |
| 2507 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2508 | |
| 2509 | void msm_ipc_router_xprt_notify(struct msm_ipc_router_xprt *xprt, |
| 2510 | unsigned event, |
| 2511 | void *data) |
| 2512 | { |
| 2513 | struct msm_ipc_router_xprt_info *xprt_info = xprt->priv; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2514 | struct msm_ipc_router_xprt_work *xprt_work; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2515 | struct rr_packet *pkt; |
Karthikeyan Ramasubramanian | 4af9f7c | 2011-09-30 15:55:18 -0600 | [diff] [blame] | 2516 | unsigned long ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2517 | |
Karthikeyan Ramasubramanian | 4af9f7c | 2011-09-30 15:55:18 -0600 | [diff] [blame] | 2518 | if (!msm_ipc_router_workqueue) { |
| 2519 | ret = wait_for_completion_timeout(&msm_ipc_local_router_up, |
| 2520 | IPC_ROUTER_INIT_TIMEOUT); |
| 2521 | if (!ret || !msm_ipc_router_workqueue) { |
| 2522 | pr_err("%s: IPC Router not initialized\n", __func__); |
| 2523 | return; |
| 2524 | } |
| 2525 | } |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2526 | |
| 2527 | switch (event) { |
| 2528 | case IPC_ROUTER_XPRT_EVENT_OPEN: |
| 2529 | D("open event for '%s'\n", xprt->name); |
| 2530 | xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work), |
| 2531 | GFP_ATOMIC); |
| 2532 | xprt_work->xprt = xprt; |
| 2533 | INIT_WORK(&xprt_work->work, xprt_open_worker); |
| 2534 | queue_work(msm_ipc_router_workqueue, &xprt_work->work); |
| 2535 | break; |
| 2536 | |
| 2537 | case IPC_ROUTER_XPRT_EVENT_CLOSE: |
| 2538 | D("close event for '%s'\n", xprt->name); |
| 2539 | atomic_inc(&pending_close_count); |
| 2540 | xprt_work = kmalloc(sizeof(struct msm_ipc_router_xprt_work), |
| 2541 | GFP_ATOMIC); |
| 2542 | xprt_work->xprt = xprt; |
| 2543 | INIT_WORK(&xprt_work->work, xprt_close_worker); |
| 2544 | queue_work(msm_ipc_router_workqueue, &xprt_work->work); |
| 2545 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | if (!data) |
| 2549 | return; |
| 2550 | |
| 2551 | while (!xprt_info) { |
| 2552 | msleep(100); |
| 2553 | xprt_info = xprt->priv; |
| 2554 | } |
| 2555 | |
| 2556 | pkt = clone_pkt((struct rr_packet *)data); |
| 2557 | if (!pkt) |
| 2558 | return; |
| 2559 | |
| 2560 | mutex_lock(&xprt_info->rx_lock); |
| 2561 | list_add_tail(&pkt->list, &xprt_info->pkt_list); |
| 2562 | wake_lock(&xprt_info->wakelock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2563 | mutex_unlock(&xprt_info->rx_lock); |
Karthikeyan Ramasubramanian | 872ecd8 | 2012-07-25 11:07:48 -0600 | [diff] [blame] | 2564 | queue_work(xprt_info->workqueue, &xprt_info->read_data); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2565 | } |
| 2566 | |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2567 | static int modem_restart_notifier_cb(struct notifier_block *this, |
| 2568 | unsigned long code, |
| 2569 | void *data); |
| 2570 | static struct notifier_block msm_ipc_router_nb = { |
| 2571 | .notifier_call = modem_restart_notifier_cb, |
| 2572 | }; |
| 2573 | |
| 2574 | static int modem_restart_notifier_cb(struct notifier_block *this, |
| 2575 | unsigned long code, |
| 2576 | void *data) |
| 2577 | { |
| 2578 | switch (code) { |
| 2579 | case SUBSYS_BEFORE_SHUTDOWN: |
| 2580 | D("%s: SUBSYS_BEFORE_SHUTDOWN\n", __func__); |
| 2581 | break; |
| 2582 | |
| 2583 | case SUBSYS_BEFORE_POWERUP: |
| 2584 | D("%s: waiting for RPC restart to complete\n", __func__); |
| 2585 | wait_event(subsystem_restart_wait, |
| 2586 | atomic_read(&pending_close_count) == 0); |
| 2587 | D("%s: finished restart wait\n", __func__); |
| 2588 | break; |
| 2589 | |
| 2590 | default: |
| 2591 | break; |
| 2592 | } |
| 2593 | |
| 2594 | return NOTIFY_DONE; |
| 2595 | } |
| 2596 | |
| 2597 | static void *restart_notifier_handle; |
| 2598 | static __init int msm_ipc_router_modem_restart_late_init(void) |
| 2599 | { |
| 2600 | restart_notifier_handle = subsys_notif_register_notifier("modem", |
| 2601 | &msm_ipc_router_nb); |
| 2602 | return 0; |
| 2603 | } |
| 2604 | late_initcall(msm_ipc_router_modem_restart_late_init); |
| 2605 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2606 | static int __init msm_ipc_router_init(void) |
| 2607 | { |
| 2608 | int i, ret; |
| 2609 | struct msm_ipc_routing_table_entry *rt_entry; |
| 2610 | |
| 2611 | msm_ipc_router_debug_mask |= SMEM_LOG; |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2612 | msm_ipc_router_workqueue = |
| 2613 | create_singlethread_workqueue("msm_ipc_router"); |
| 2614 | if (!msm_ipc_router_workqueue) |
| 2615 | return -ENOMEM; |
| 2616 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2617 | debugfs_init(); |
| 2618 | |
| 2619 | for (i = 0; i < SRV_HASH_SIZE; i++) |
| 2620 | INIT_LIST_HEAD(&server_list[i]); |
| 2621 | |
| 2622 | for (i = 0; i < LP_HASH_SIZE; i++) |
| 2623 | INIT_LIST_HEAD(&local_ports[i]); |
| 2624 | |
| 2625 | mutex_lock(&routing_table_lock); |
| 2626 | if (!routing_table_inited) { |
| 2627 | init_routing_table(); |
| 2628 | rt_entry = alloc_routing_table_entry(IPC_ROUTER_NID_LOCAL); |
| 2629 | add_routing_table_entry(rt_entry); |
| 2630 | routing_table_inited = 1; |
| 2631 | } |
| 2632 | mutex_unlock(&routing_table_lock); |
| 2633 | |
| 2634 | init_waitqueue_head(&newserver_wait); |
Karthikeyan Ramasubramanian | ff6fbae | 2011-06-09 11:13:19 -0600 | [diff] [blame] | 2635 | init_waitqueue_head(&subsystem_restart_wait); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2636 | ret = msm_ipc_router_init_sockets(); |
| 2637 | if (ret < 0) |
| 2638 | pr_err("%s: Init sockets failed\n", __func__); |
| 2639 | |
Karthikeyan Ramasubramanian | 4af9f7c | 2011-09-30 15:55:18 -0600 | [diff] [blame] | 2640 | complete_all(&msm_ipc_local_router_up); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | module_init(msm_ipc_router_init); |
| 2645 | MODULE_DESCRIPTION("MSM IPC Router"); |
| 2646 | MODULE_LICENSE("GPL v2"); |