Karthikeyan Ramasubramanian | 6a116d6 | 2016-09-16 16:05:32 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved. |
| 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 | #ifndef _IPC_ROUTER_H |
| 14 | #define _IPC_ROUTER_H |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/socket.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/pm.h> |
| 22 | #include <linux/msm_ipc.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/kref.h> |
| 25 | |
| 26 | /* Maximum Wakeup Source Name Size */ |
| 27 | #define MAX_WS_NAME_SZ 32 |
| 28 | |
| 29 | #define IPC_RTR_ERR(buf, ...) \ |
| 30 | pr_err("IPC_RTR: " buf, __VA_ARGS__) |
| 31 | |
| 32 | /** |
| 33 | * enum msm_ipc_router_event - Events that will be generated by IPC Router |
| 34 | */ |
| 35 | enum msm_ipc_router_event { |
| 36 | IPC_ROUTER_CTRL_CMD_DATA = 1, |
| 37 | IPC_ROUTER_CTRL_CMD_HELLO, |
| 38 | IPC_ROUTER_CTRL_CMD_BYE, |
| 39 | IPC_ROUTER_CTRL_CMD_NEW_SERVER, |
| 40 | IPC_ROUTER_CTRL_CMD_REMOVE_SERVER, |
| 41 | IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT, |
| 42 | IPC_ROUTER_CTRL_CMD_RESUME_TX, |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * rr_control_msg - Control message structure |
| 47 | * @cmd: Command identifier for HELLO message in Version 1. |
| 48 | * @hello: Message structure for HELLO message in Version 2. |
| 49 | * @srv: Message structure for NEW_SERVER/REMOVE_SERVER events. |
| 50 | * @cli: Message structure for REMOVE_CLIENT event. |
| 51 | */ |
| 52 | union rr_control_msg { |
| 53 | uint32_t cmd; |
| 54 | struct { |
| 55 | uint32_t cmd; |
| 56 | uint32_t checksum; |
| 57 | uint32_t versions; |
| 58 | uint32_t capability; |
| 59 | uint32_t reserved; |
| 60 | } hello; |
| 61 | struct { |
| 62 | uint32_t cmd; |
| 63 | uint32_t service; |
| 64 | uint32_t instance; |
| 65 | uint32_t node_id; |
| 66 | uint32_t port_id; |
| 67 | } srv; |
| 68 | struct { |
| 69 | uint32_t cmd; |
| 70 | uint32_t node_id; |
| 71 | uint32_t port_id; |
| 72 | } cli; |
| 73 | }; |
| 74 | |
| 75 | struct comm_mode_info { |
| 76 | int mode; |
| 77 | void *xprt_info; |
| 78 | }; |
| 79 | |
| 80 | enum ipc_rtr_af_event_type { |
| 81 | IPCRTR_AF_INIT = 1, |
| 82 | IPCRTR_AF_DEINIT, |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * msm_ipc_port - Definition of IPC Router port |
| 87 | * @list: List(local/control ports) in which this port is present. |
| 88 | * @ref: Reference count for this port. |
| 89 | * @this_port: Contains port's node_id and port_id information. |
| 90 | * @port_name: Contains service & instance info if the port hosts a service. |
| 91 | * @type: Type of the port - Client, Service, Control or Security Config. |
| 92 | * @flags: Flags to identify the port state. |
| 93 | * @port_lock_lhc3: Lock to protect access to the port information. |
| 94 | * @mode_info: Communication mode of the port owner. |
| 95 | * @port_rx_q: Receive queue where incoming messages are queued. |
| 96 | * @port_rx_q_lock_lhc3: Lock to protect access to the port's rx_q. |
| 97 | * @rx_ws_name: Name of the receive wakeup source. |
| 98 | * @port_rx_ws: Wakeup source to prevent suspend until the rx_q is empty. |
| 99 | * @port_rx_wait_q: Wait queue to wait for the incoming messages. |
| 100 | * @restart_state: Flag to hold the restart state information. |
| 101 | * @restart_lock: Lock to protect access to the restart_state. |
| 102 | * @restart_wait: Wait Queue to wait for any restart events. |
| 103 | * @endpoint: Contains the information related to user-space interface. |
| 104 | * @notify: Function to notify the incoming events on the port. |
| 105 | * @check_send_permissions: Function to check access control from this port. |
| 106 | * @num_tx: Number of packets transmitted. |
| 107 | * @num_rx: Number of packets received. |
| 108 | * @num_tx_bytes: Number of bytes transmitted. |
| 109 | * @num_rx_bytes: Number of bytes received. |
| 110 | * @priv: Private information registered by the port owner. |
| 111 | */ |
| 112 | struct msm_ipc_port { |
| 113 | struct list_head list; |
| 114 | struct kref ref; |
| 115 | |
| 116 | struct msm_ipc_port_addr this_port; |
| 117 | struct msm_ipc_port_name port_name; |
| 118 | uint32_t type; |
| 119 | unsigned int flags; |
| 120 | struct mutex port_lock_lhc3; |
| 121 | struct comm_mode_info mode_info; |
| 122 | |
| 123 | struct msm_ipc_port_addr dest_addr; |
| 124 | int conn_status; |
| 125 | |
| 126 | struct list_head port_rx_q; |
| 127 | struct mutex port_rx_q_lock_lhc3; |
| 128 | char rx_ws_name[MAX_WS_NAME_SZ]; |
| 129 | struct wakeup_source *port_rx_ws; |
| 130 | wait_queue_head_t port_rx_wait_q; |
| 131 | wait_queue_head_t port_tx_wait_q; |
| 132 | |
| 133 | int restart_state; |
| 134 | spinlock_t restart_lock; |
| 135 | wait_queue_head_t restart_wait; |
| 136 | |
| 137 | void *rport_info; |
| 138 | void *endpoint; |
| 139 | void (*notify)(unsigned int event, void *oob_data, |
| 140 | size_t oob_data_len, void *priv); |
| 141 | int (*check_send_permissions)(void *data); |
| 142 | |
| 143 | uint32_t num_tx; |
| 144 | uint32_t num_rx; |
| 145 | unsigned long num_tx_bytes; |
| 146 | unsigned long num_rx_bytes; |
| 147 | void *priv; |
| 148 | }; |
| 149 | |
| 150 | #ifdef CONFIG_IPC_ROUTER |
| 151 | /** |
| 152 | * msm_ipc_router_create_port() - Create a IPC Router port/endpoint |
| 153 | * @notify: Callback function to notify any event on the port. |
| 154 | * @event: Event ID to be handled. |
| 155 | * @oob_data: Any out-of-band data associated with the event. |
| 156 | * @oob_data_len: Size of the out-of-band data, if valid. |
| 157 | * @priv: Private data registered during the port creation. |
| 158 | * @priv: Private info to be passed while the notification is generated. |
| 159 | * |
| 160 | * @return: Pointer to the port on success, NULL on error. |
| 161 | */ |
| 162 | struct msm_ipc_port *msm_ipc_router_create_port( |
| 163 | void (*notify)(unsigned int event, void *oob_data, |
| 164 | size_t oob_data_len, void *priv), |
| 165 | void *priv); |
| 166 | |
| 167 | /** |
| 168 | * msm_ipc_router_bind_control_port() - Bind a port as a control port |
| 169 | * @port_ptr: Port which needs to be marked as a control port. |
| 170 | * |
| 171 | * @return: 0 on success, standard Linux error codes on error. |
| 172 | */ |
| 173 | int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr); |
| 174 | |
| 175 | /** |
| 176 | * msm_ipc_router_lookup_server_name() - Resolve server address |
| 177 | * @srv_name: Name<service:instance> of the server to be resolved. |
| 178 | * @srv_info: Buffer to hold the resolved address. |
| 179 | * @num_entries_in_array: Number of server info the buffer can hold. |
| 180 | * @lookup_mask: Mask to specify the range of instances to be resolved. |
| 181 | * |
| 182 | * @return: Number of server addresses resolved on success, < 0 on error. |
| 183 | */ |
| 184 | int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name, |
| 185 | struct msm_ipc_server_info *srv_info, |
| 186 | int num_entries_in_array, |
| 187 | uint32_t lookup_mask); |
| 188 | |
| 189 | /** |
| 190 | * msm_ipc_router_send_msg() - Send a message/packet |
| 191 | * @src: Sender's address/port. |
| 192 | * @dest: Destination address. |
| 193 | * @data: Pointer to the data to be sent. |
| 194 | * @data_len: Length of the data to be sent. |
| 195 | * |
| 196 | * @return: 0 on success, < 0 on error. |
| 197 | */ |
| 198 | int msm_ipc_router_send_msg(struct msm_ipc_port *src, |
| 199 | struct msm_ipc_addr *dest, |
| 200 | void *data, unsigned int data_len); |
| 201 | |
| 202 | /** |
| 203 | * msm_ipc_router_get_curr_pkt_size() - Get the packet size of the first |
| 204 | * packet in the rx queue |
| 205 | * @port_ptr: Port which owns the rx queue. |
| 206 | * |
| 207 | * @return: Returns the size of the first packet, if available. |
| 208 | * 0 if no packets available, < 0 on error. |
| 209 | */ |
| 210 | int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr); |
| 211 | |
| 212 | /** |
| 213 | * msm_ipc_router_read_msg() - Read a message/packet |
| 214 | * @port_ptr: Receiver's port/address. |
| 215 | * @data: Pointer containing the address of the received data. |
| 216 | * @src: Address of the sender/source. |
| 217 | * @len: Length of the data being read. |
| 218 | * |
| 219 | * @return: 0 on success, < 0 on error. |
| 220 | */ |
| 221 | int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr, |
| 222 | struct msm_ipc_addr *src, |
| 223 | unsigned char **data, |
| 224 | unsigned int *len); |
| 225 | |
| 226 | /** |
| 227 | * msm_ipc_router_close_port() - Close the port |
| 228 | * @port_ptr: Pointer to the port to be closed. |
| 229 | * |
| 230 | * @return: 0 on success, < 0 on error. |
| 231 | */ |
| 232 | int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr); |
| 233 | |
| 234 | /** |
| 235 | * msm_ipc_router_register_server() - Register a service on a port |
| 236 | * @server_port: IPC Router port with which a service is registered. |
| 237 | * @name: Service name <service_id:instance_id> that gets registered. |
| 238 | * |
| 239 | * @return: 0 on success, standard Linux error codes on error. |
| 240 | */ |
| 241 | int msm_ipc_router_register_server(struct msm_ipc_port *server_port, |
| 242 | struct msm_ipc_addr *name); |
| 243 | |
| 244 | /** |
| 245 | * msm_ipc_router_unregister_server() - Unregister a service from a port |
| 246 | * @server_port: Port with with a service is already registered. |
| 247 | * |
| 248 | * @return: 0 on success, standard Linux error codes on error. |
| 249 | */ |
| 250 | int msm_ipc_router_unregister_server(struct msm_ipc_port *server_port); |
| 251 | |
| 252 | /** |
| 253 | * register_ipcrtr_af_init_notifier() - Register for ipc router socket |
| 254 | * address family initialization callback |
| 255 | * @nb: Notifier block which will be notified once address family is |
| 256 | * initialized. |
| 257 | * |
| 258 | * Return: 0 on success, standard error code otherwise. |
| 259 | */ |
| 260 | int register_ipcrtr_af_init_notifier(struct notifier_block *nb); |
| 261 | |
| 262 | /** |
| 263 | * unregister_ipcrtr_af_init_notifier() - Unregister for ipc router socket |
| 264 | * address family initialization callback |
| 265 | * @nb: Notifier block which will be notified once address family is |
| 266 | * initialized. |
| 267 | * |
| 268 | * Return: 0 on success, standard error code otherwise. |
| 269 | */ |
| 270 | int unregister_ipcrtr_af_init_notifier(struct notifier_block *nb); |
| 271 | |
| 272 | #else |
| 273 | |
| 274 | struct msm_ipc_port *msm_ipc_router_create_port( |
| 275 | void (*notify)(unsigned int event, void *oob_data, |
| 276 | size_t oob_data_len, void *priv), |
| 277 | void *priv) |
| 278 | { |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | static inline int msm_ipc_router_bind_control_port( |
| 283 | struct msm_ipc_port *port_ptr) |
| 284 | { |
| 285 | return -ENODEV; |
| 286 | } |
| 287 | |
| 288 | int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name, |
| 289 | struct msm_ipc_server_info *srv_info, |
| 290 | int num_entries_in_array, |
| 291 | uint32_t lookup_mask) |
| 292 | { |
| 293 | return -ENODEV; |
| 294 | } |
| 295 | |
| 296 | int msm_ipc_router_send_msg(struct msm_ipc_port *src, |
| 297 | struct msm_ipc_addr *dest, |
| 298 | void *data, unsigned int data_len) |
| 299 | { |
| 300 | return -ENODEV; |
| 301 | } |
| 302 | |
| 303 | int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr) |
| 304 | { |
| 305 | return -ENODEV; |
| 306 | } |
| 307 | |
| 308 | int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr, |
| 309 | struct msm_ipc_addr *src, |
| 310 | unsigned char **data, |
| 311 | unsigned int *len) |
| 312 | { |
| 313 | return -ENODEV; |
| 314 | } |
| 315 | |
| 316 | int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr) |
| 317 | { |
| 318 | return -ENODEV; |
| 319 | } |
| 320 | |
| 321 | static inline int msm_ipc_router_register_server( |
| 322 | struct msm_ipc_port *server_port, |
| 323 | struct msm_ipc_addr *name) |
| 324 | { |
| 325 | return -ENODEV; |
| 326 | } |
| 327 | |
| 328 | static inline int msm_ipc_router_unregister_server( |
| 329 | struct msm_ipc_port *server_port) |
| 330 | { |
| 331 | return -ENODEV; |
| 332 | } |
| 333 | |
| 334 | int register_ipcrtr_af_init_notifier(struct notifier_block *nb) |
| 335 | { |
| 336 | return -ENODEV; |
| 337 | } |
| 338 | |
| 339 | int unregister_ipcrtr_af_init_notifier(struct notifier_block *nb) |
| 340 | { |
| 341 | return -ENODEV; |
| 342 | } |
| 343 | |
| 344 | #endif |
| 345 | |
| 346 | #endif |