Utkarsh Saxena | 9619488 | 2017-04-03 13:21:58 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved. |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [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 | #include <linux/fs.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include "ipa_i.h" |
| 16 | |
| 17 | struct ipa3_intf { |
| 18 | char name[IPA_RESOURCE_NAME_MAX]; |
| 19 | struct list_head link; |
| 20 | u32 num_tx_props; |
| 21 | u32 num_rx_props; |
| 22 | u32 num_ext_props; |
| 23 | struct ipa_ioc_tx_intf_prop *tx; |
| 24 | struct ipa_ioc_rx_intf_prop *rx; |
| 25 | struct ipa_ioc_ext_intf_prop *ext; |
| 26 | enum ipa_client_type excp_pipe; |
| 27 | }; |
| 28 | |
| 29 | struct ipa3_push_msg { |
| 30 | struct ipa_msg_meta meta; |
| 31 | ipa_msg_free_fn callback; |
| 32 | void *buff; |
| 33 | struct list_head link; |
| 34 | }; |
| 35 | |
| 36 | struct ipa3_pull_msg { |
| 37 | struct ipa_msg_meta meta; |
| 38 | ipa_msg_pull_fn callback; |
| 39 | struct list_head link; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * ipa3_register_intf() - register "logical" interface |
| 44 | * @name: [in] interface name |
| 45 | * @tx: [in] TX properties of the interface |
| 46 | * @rx: [in] RX properties of the interface |
| 47 | * |
| 48 | * Register an interface and its tx and rx properties, this allows |
| 49 | * configuration of rules from user-space |
| 50 | * |
| 51 | * Returns: 0 on success, negative on failure |
| 52 | * |
| 53 | * Note: Should not be called from atomic context |
| 54 | */ |
| 55 | int ipa3_register_intf(const char *name, const struct ipa_tx_intf *tx, |
| 56 | const struct ipa_rx_intf *rx) |
| 57 | { |
| 58 | return ipa3_register_intf_ext(name, tx, rx, NULL); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * ipa3_register_intf_ext() - register "logical" interface which has only |
| 63 | * extended properties |
| 64 | * @name: [in] interface name |
| 65 | * @tx: [in] TX properties of the interface |
| 66 | * @rx: [in] RX properties of the interface |
| 67 | * @ext: [in] EXT properties of the interface |
| 68 | * |
| 69 | * Register an interface and its tx, rx and ext properties, this allows |
| 70 | * configuration of rules from user-space |
| 71 | * |
| 72 | * Returns: 0 on success, negative on failure |
| 73 | * |
| 74 | * Note: Should not be called from atomic context |
| 75 | */ |
| 76 | int ipa3_register_intf_ext(const char *name, const struct ipa_tx_intf *tx, |
| 77 | const struct ipa_rx_intf *rx, |
| 78 | const struct ipa_ext_intf *ext) |
| 79 | { |
| 80 | struct ipa3_intf *intf; |
| 81 | u32 len; |
| 82 | |
| 83 | if (name == NULL || (tx == NULL && rx == NULL && ext == NULL)) { |
| 84 | IPAERR("invalid params name=%p tx=%p rx=%p ext=%p\n", name, |
| 85 | tx, rx, ext); |
| 86 | return -EINVAL; |
| 87 | } |
| 88 | |
| 89 | if (tx && tx->num_props > IPA_NUM_PROPS_MAX) { |
| 90 | IPAERR("invalid tx num_props=%d max=%d\n", tx->num_props, |
| 91 | IPA_NUM_PROPS_MAX); |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
| 95 | if (rx && rx->num_props > IPA_NUM_PROPS_MAX) { |
| 96 | IPAERR("invalid rx num_props=%d max=%d\n", rx->num_props, |
| 97 | IPA_NUM_PROPS_MAX); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | if (ext && ext->num_props > IPA_NUM_PROPS_MAX) { |
| 102 | IPAERR("invalid ext num_props=%d max=%d\n", ext->num_props, |
| 103 | IPA_NUM_PROPS_MAX); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | len = sizeof(struct ipa3_intf); |
| 108 | intf = kzalloc(len, GFP_KERNEL); |
| 109 | if (intf == NULL) { |
| 110 | IPAERR("fail to alloc 0x%x bytes\n", len); |
| 111 | return -ENOMEM; |
| 112 | } |
| 113 | |
| 114 | strlcpy(intf->name, name, IPA_RESOURCE_NAME_MAX); |
| 115 | |
| 116 | if (tx) { |
| 117 | intf->num_tx_props = tx->num_props; |
| 118 | len = tx->num_props * sizeof(struct ipa_ioc_tx_intf_prop); |
| 119 | intf->tx = kzalloc(len, GFP_KERNEL); |
| 120 | if (intf->tx == NULL) { |
| 121 | IPAERR("fail to alloc 0x%x bytes\n", len); |
| 122 | kfree(intf); |
| 123 | return -ENOMEM; |
| 124 | } |
| 125 | memcpy(intf->tx, tx->prop, len); |
| 126 | } |
| 127 | |
| 128 | if (rx) { |
| 129 | intf->num_rx_props = rx->num_props; |
| 130 | len = rx->num_props * sizeof(struct ipa_ioc_rx_intf_prop); |
| 131 | intf->rx = kzalloc(len, GFP_KERNEL); |
| 132 | if (intf->rx == NULL) { |
| 133 | IPAERR("fail to alloc 0x%x bytes\n", len); |
| 134 | kfree(intf->tx); |
| 135 | kfree(intf); |
| 136 | return -ENOMEM; |
| 137 | } |
| 138 | memcpy(intf->rx, rx->prop, len); |
| 139 | } |
| 140 | |
| 141 | if (ext) { |
| 142 | intf->num_ext_props = ext->num_props; |
| 143 | len = ext->num_props * sizeof(struct ipa_ioc_ext_intf_prop); |
| 144 | intf->ext = kzalloc(len, GFP_KERNEL); |
| 145 | if (intf->ext == NULL) { |
| 146 | IPAERR("fail to alloc 0x%x bytes\n", len); |
| 147 | kfree(intf->rx); |
| 148 | kfree(intf->tx); |
| 149 | kfree(intf); |
| 150 | return -ENOMEM; |
| 151 | } |
| 152 | memcpy(intf->ext, ext->prop, len); |
| 153 | } |
| 154 | |
| 155 | if (ext && ext->excp_pipe_valid) |
| 156 | intf->excp_pipe = ext->excp_pipe; |
| 157 | else |
| 158 | intf->excp_pipe = IPA_CLIENT_APPS_LAN_CONS; |
| 159 | |
| 160 | mutex_lock(&ipa3_ctx->lock); |
| 161 | list_add_tail(&intf->link, &ipa3_ctx->intf_list); |
| 162 | mutex_unlock(&ipa3_ctx->lock); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * ipa3_deregister_intf() - de-register previously registered logical interface |
| 169 | * @name: [in] interface name |
| 170 | * |
| 171 | * De-register a previously registered interface |
| 172 | * |
| 173 | * Returns: 0 on success, negative on failure |
| 174 | * |
| 175 | * Note: Should not be called from atomic context |
| 176 | */ |
| 177 | int ipa3_deregister_intf(const char *name) |
| 178 | { |
| 179 | struct ipa3_intf *entry; |
| 180 | struct ipa3_intf *next; |
| 181 | int result = -EINVAL; |
| 182 | |
| 183 | if ((name == NULL) || |
| 184 | (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX)) { |
| 185 | IPAERR("invalid param name=%s\n", name); |
| 186 | return result; |
| 187 | } |
| 188 | |
| 189 | mutex_lock(&ipa3_ctx->lock); |
| 190 | list_for_each_entry_safe(entry, next, &ipa3_ctx->intf_list, link) { |
| 191 | if (!strcmp(entry->name, name)) { |
| 192 | list_del(&entry->link); |
| 193 | kfree(entry->ext); |
| 194 | kfree(entry->rx); |
| 195 | kfree(entry->tx); |
| 196 | kfree(entry); |
| 197 | result = 0; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | mutex_unlock(&ipa3_ctx->lock); |
| 202 | |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * ipa3_query_intf() - query logical interface properties |
| 208 | * @lookup: [inout] interface name and number of properties |
| 209 | * |
| 210 | * Obtain the handle and number of tx and rx properties for the named |
| 211 | * interface, used as part of querying the tx and rx properties for |
| 212 | * configuration of various rules from user-space |
| 213 | * |
| 214 | * Returns: 0 on success, negative on failure |
| 215 | * |
| 216 | * Note: Should not be called from atomic context |
| 217 | */ |
| 218 | int ipa3_query_intf(struct ipa_ioc_query_intf *lookup) |
| 219 | { |
| 220 | struct ipa3_intf *entry; |
| 221 | int result = -EINVAL; |
| 222 | |
| 223 | if (lookup == NULL) { |
| 224 | IPAERR("invalid param lookup=%p\n", lookup); |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | if (strnlen(lookup->name, IPA_RESOURCE_NAME_MAX) == |
| 229 | IPA_RESOURCE_NAME_MAX) { |
| 230 | IPAERR("Interface name too long. (%s)\n", lookup->name); |
| 231 | return result; |
| 232 | } |
| 233 | |
| 234 | mutex_lock(&ipa3_ctx->lock); |
| 235 | list_for_each_entry(entry, &ipa3_ctx->intf_list, link) { |
| 236 | if (!strcmp(entry->name, lookup->name)) { |
| 237 | lookup->num_tx_props = entry->num_tx_props; |
| 238 | lookup->num_rx_props = entry->num_rx_props; |
| 239 | lookup->num_ext_props = entry->num_ext_props; |
| 240 | lookup->excp_pipe = entry->excp_pipe; |
| 241 | result = 0; |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | mutex_unlock(&ipa3_ctx->lock); |
| 246 | |
| 247 | return result; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * ipa3_query_intf_tx_props() - qeury TX props of an interface |
| 252 | * @tx: [inout] interface tx attributes |
| 253 | * |
| 254 | * Obtain the tx properties for the specified interface |
| 255 | * |
| 256 | * Returns: 0 on success, negative on failure |
| 257 | * |
| 258 | * Note: Should not be called from atomic context |
| 259 | */ |
| 260 | int ipa3_query_intf_tx_props(struct ipa_ioc_query_intf_tx_props *tx) |
| 261 | { |
| 262 | struct ipa3_intf *entry; |
| 263 | int result = -EINVAL; |
| 264 | |
| 265 | if (tx == NULL) { |
| 266 | IPAERR("invalid param tx=%p\n", tx); |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | if (strnlen(tx->name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) { |
| 271 | IPAERR("Interface name too long. (%s)\n", tx->name); |
| 272 | return result; |
| 273 | } |
| 274 | |
| 275 | mutex_lock(&ipa3_ctx->lock); |
| 276 | list_for_each_entry(entry, &ipa3_ctx->intf_list, link) { |
| 277 | if (!strcmp(entry->name, tx->name)) { |
Skylar Chang | 1b59810 | 2017-03-01 16:08:27 -0800 | [diff] [blame^] | 278 | /* add the entry check */ |
| 279 | if (entry->num_tx_props != tx->num_tx_props) { |
| 280 | IPAERR("invalid entry number(%u %u)\n", |
| 281 | entry->num_tx_props, |
| 282 | tx->num_tx_props); |
| 283 | mutex_unlock(&ipa3_ctx->lock); |
| 284 | return result; |
| 285 | } |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 286 | memcpy(tx->tx, entry->tx, entry->num_tx_props * |
| 287 | sizeof(struct ipa_ioc_tx_intf_prop)); |
| 288 | result = 0; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | mutex_unlock(&ipa3_ctx->lock); |
| 293 | |
| 294 | return result; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * ipa3_query_intf_rx_props() - qeury RX props of an interface |
| 299 | * @rx: [inout] interface rx attributes |
| 300 | * |
| 301 | * Obtain the rx properties for the specified interface |
| 302 | * |
| 303 | * Returns: 0 on success, negative on failure |
| 304 | * |
| 305 | * Note: Should not be called from atomic context |
| 306 | */ |
| 307 | int ipa3_query_intf_rx_props(struct ipa_ioc_query_intf_rx_props *rx) |
| 308 | { |
| 309 | struct ipa3_intf *entry; |
| 310 | int result = -EINVAL; |
| 311 | |
| 312 | if (rx == NULL) { |
| 313 | IPAERR("invalid param rx=%p\n", rx); |
| 314 | return result; |
| 315 | } |
| 316 | |
| 317 | if (strnlen(rx->name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) { |
| 318 | IPAERR("Interface name too long. (%s)\n", rx->name); |
| 319 | return result; |
| 320 | } |
| 321 | |
| 322 | mutex_lock(&ipa3_ctx->lock); |
| 323 | list_for_each_entry(entry, &ipa3_ctx->intf_list, link) { |
| 324 | if (!strcmp(entry->name, rx->name)) { |
Skylar Chang | 1b59810 | 2017-03-01 16:08:27 -0800 | [diff] [blame^] | 325 | /* add the entry check */ |
| 326 | if (entry->num_rx_props != rx->num_rx_props) { |
| 327 | IPAERR("invalid entry number(%u %u)\n", |
| 328 | entry->num_rx_props, |
| 329 | rx->num_rx_props); |
| 330 | mutex_unlock(&ipa3_ctx->lock); |
| 331 | return result; |
| 332 | } |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 333 | memcpy(rx->rx, entry->rx, entry->num_rx_props * |
| 334 | sizeof(struct ipa_ioc_rx_intf_prop)); |
| 335 | result = 0; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | mutex_unlock(&ipa3_ctx->lock); |
| 340 | |
| 341 | return result; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * ipa3_query_intf_ext_props() - qeury EXT props of an interface |
| 346 | * @ext: [inout] interface ext attributes |
| 347 | * |
| 348 | * Obtain the ext properties for the specified interface |
| 349 | * |
| 350 | * Returns: 0 on success, negative on failure |
| 351 | * |
| 352 | * Note: Should not be called from atomic context |
| 353 | */ |
| 354 | int ipa3_query_intf_ext_props(struct ipa_ioc_query_intf_ext_props *ext) |
| 355 | { |
| 356 | struct ipa3_intf *entry; |
| 357 | int result = -EINVAL; |
| 358 | |
| 359 | if (ext == NULL) { |
| 360 | IPAERR("invalid param ext=%p\n", ext); |
| 361 | return result; |
| 362 | } |
| 363 | |
| 364 | mutex_lock(&ipa3_ctx->lock); |
| 365 | list_for_each_entry(entry, &ipa3_ctx->intf_list, link) { |
| 366 | if (!strcmp(entry->name, ext->name)) { |
Skylar Chang | 1b59810 | 2017-03-01 16:08:27 -0800 | [diff] [blame^] | 367 | /* add the entry check */ |
| 368 | if (entry->num_ext_props != ext->num_ext_props) { |
| 369 | IPAERR("invalid entry number(%u %u)\n", |
| 370 | entry->num_ext_props, |
| 371 | ext->num_ext_props); |
| 372 | mutex_unlock(&ipa3_ctx->lock); |
| 373 | return result; |
| 374 | } |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 375 | memcpy(ext->ext, entry->ext, entry->num_ext_props * |
| 376 | sizeof(struct ipa_ioc_ext_intf_prop)); |
| 377 | result = 0; |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | mutex_unlock(&ipa3_ctx->lock); |
| 382 | return result; |
| 383 | } |
| 384 | |
Gidon Studinski | 3021a6f | 2016-11-10 12:48:48 +0200 | [diff] [blame] | 385 | static void ipa3_send_msg_free(void *buff, u32 len, u32 type) |
| 386 | { |
| 387 | kfree(buff); |
| 388 | } |
| 389 | |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 390 | /** |
| 391 | * ipa3_send_msg() - Send "message" from kernel client to IPA driver |
| 392 | * @meta: [in] message meta-data |
| 393 | * @buff: [in] the payload for message |
| 394 | * @callback: [in] free callback |
| 395 | * |
| 396 | * Client supplies the message meta-data and payload which IPA driver buffers |
| 397 | * till read by user-space. After read from user space IPA driver invokes the |
| 398 | * callback supplied to free the message payload. Client must not touch/free |
| 399 | * the message payload after calling this API. |
| 400 | * |
| 401 | * Returns: 0 on success, negative on failure |
| 402 | * |
| 403 | * Note: Should not be called from atomic context |
| 404 | */ |
| 405 | int ipa3_send_msg(struct ipa_msg_meta *meta, void *buff, |
| 406 | ipa_msg_free_fn callback) |
| 407 | { |
| 408 | struct ipa3_push_msg *msg; |
Gidon Studinski | 3021a6f | 2016-11-10 12:48:48 +0200 | [diff] [blame] | 409 | void *data = NULL; |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 410 | |
| 411 | if (meta == NULL || (buff == NULL && callback != NULL) || |
| 412 | (buff != NULL && callback == NULL)) { |
| 413 | IPAERR("invalid param meta=%p buff=%p, callback=%p\n", |
| 414 | meta, buff, callback); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | if (meta->msg_type >= IPA_EVENT_MAX_NUM) { |
| 419 | IPAERR("unsupported message type %d\n", meta->msg_type); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | msg = kzalloc(sizeof(struct ipa3_push_msg), GFP_KERNEL); |
| 424 | if (msg == NULL) { |
| 425 | IPAERR("fail to alloc ipa_msg container\n"); |
| 426 | return -ENOMEM; |
| 427 | } |
| 428 | |
| 429 | msg->meta = *meta; |
Gidon Studinski | 3021a6f | 2016-11-10 12:48:48 +0200 | [diff] [blame] | 430 | if (meta->msg_len > 0 && buff) { |
| 431 | data = kmalloc(meta->msg_len, GFP_KERNEL); |
| 432 | if (data == NULL) { |
| 433 | IPAERR("fail to alloc data container\n"); |
| 434 | kfree(msg); |
| 435 | return -ENOMEM; |
| 436 | } |
| 437 | memcpy(data, buff, meta->msg_len); |
| 438 | msg->buff = data; |
| 439 | msg->callback = ipa3_send_msg_free; |
| 440 | } |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 441 | |
| 442 | mutex_lock(&ipa3_ctx->msg_lock); |
| 443 | list_add_tail(&msg->link, &ipa3_ctx->msg_list); |
| 444 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 445 | IPA_STATS_INC_CNT(ipa3_ctx->stats.msg_w[meta->msg_type]); |
| 446 | |
| 447 | wake_up(&ipa3_ctx->msg_waitq); |
Gidon Studinski | 3021a6f | 2016-11-10 12:48:48 +0200 | [diff] [blame] | 448 | if (buff) |
| 449 | callback(buff, meta->msg_len, meta->msg_type); |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * ipa3_register_pull_msg() - register pull message type |
| 456 | * @meta: [in] message meta-data |
| 457 | * @callback: [in] pull callback |
| 458 | * |
| 459 | * Register message callback by kernel client with IPA driver for IPA driver to |
| 460 | * pull message on-demand. |
| 461 | * |
| 462 | * Returns: 0 on success, negative on failure |
| 463 | * |
| 464 | * Note: Should not be called from atomic context |
| 465 | */ |
| 466 | int ipa3_register_pull_msg(struct ipa_msg_meta *meta, ipa_msg_pull_fn callback) |
| 467 | { |
| 468 | struct ipa3_pull_msg *msg; |
| 469 | |
| 470 | if (meta == NULL || callback == NULL) { |
| 471 | IPAERR("invalid param meta=%p callback=%p\n", meta, callback); |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | msg = kzalloc(sizeof(struct ipa3_pull_msg), GFP_KERNEL); |
| 476 | if (msg == NULL) { |
| 477 | IPAERR("fail to alloc ipa_msg container\n"); |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
| 481 | msg->meta = *meta; |
| 482 | msg->callback = callback; |
| 483 | |
| 484 | mutex_lock(&ipa3_ctx->msg_lock); |
| 485 | list_add_tail(&msg->link, &ipa3_ctx->pull_msg_list); |
| 486 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * ipa3_deregister_pull_msg() - De-register pull message type |
| 493 | * @meta: [in] message meta-data |
| 494 | * |
| 495 | * De-register "message" by kernel client from IPA driver |
| 496 | * |
| 497 | * Returns: 0 on success, negative on failure |
| 498 | * |
| 499 | * Note: Should not be called from atomic context |
| 500 | */ |
| 501 | int ipa3_deregister_pull_msg(struct ipa_msg_meta *meta) |
| 502 | { |
| 503 | struct ipa3_pull_msg *entry; |
| 504 | struct ipa3_pull_msg *next; |
| 505 | int result = -EINVAL; |
| 506 | |
| 507 | if (meta == NULL) { |
| 508 | IPAERR("invalid param name=%p\n", meta); |
| 509 | return result; |
| 510 | } |
| 511 | |
| 512 | mutex_lock(&ipa3_ctx->msg_lock); |
| 513 | list_for_each_entry_safe(entry, next, &ipa3_ctx->pull_msg_list, link) { |
| 514 | if (entry->meta.msg_len == meta->msg_len && |
| 515 | entry->meta.msg_type == meta->msg_type) { |
| 516 | list_del(&entry->link); |
| 517 | kfree(entry); |
| 518 | result = 0; |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 523 | return result; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * ipa3_read() - read message from IPA device |
| 528 | * @filp: [in] file pointer |
| 529 | * @buf: [out] buffer to read into |
| 530 | * @count: [in] size of above buffer |
| 531 | * @f_pos: [inout] file position |
| 532 | * |
| 533 | * Uer-space should continually read from /dev/ipa, read wll block when there |
| 534 | * are no messages to read. Upon return, user-space should read the ipa_msg_meta |
| 535 | * from the start of the buffer to know what type of message was read and its |
| 536 | * length in the remainder of the buffer. Buffer supplied must be big enough to |
| 537 | * hold the message meta-data and the largest defined message type |
| 538 | * |
| 539 | * Returns: how many bytes copied to buffer |
| 540 | * |
| 541 | * Note: Should not be called from atomic context |
| 542 | */ |
| 543 | ssize_t ipa3_read(struct file *filp, char __user *buf, size_t count, |
| 544 | loff_t *f_pos) |
| 545 | { |
| 546 | char __user *start; |
| 547 | struct ipa3_push_msg *msg = NULL; |
| 548 | int ret; |
| 549 | DEFINE_WAIT(wait); |
| 550 | int locked; |
| 551 | |
| 552 | start = buf; |
| 553 | |
| 554 | while (1) { |
Gidon Studinski | 3021a6f | 2016-11-10 12:48:48 +0200 | [diff] [blame] | 555 | mutex_lock(&ipa3_ctx->msg_lock); |
| 556 | locked = 1; |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 557 | prepare_to_wait(&ipa3_ctx->msg_waitq, |
| 558 | &wait, |
| 559 | TASK_INTERRUPTIBLE); |
| 560 | |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 561 | if (!list_empty(&ipa3_ctx->msg_list)) { |
| 562 | msg = list_first_entry(&ipa3_ctx->msg_list, |
| 563 | struct ipa3_push_msg, link); |
| 564 | list_del(&msg->link); |
| 565 | } |
| 566 | |
| 567 | IPADBG_LOW("msg=%p\n", msg); |
| 568 | |
| 569 | if (msg) { |
| 570 | locked = 0; |
| 571 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 572 | if (copy_to_user(buf, &msg->meta, |
| 573 | sizeof(struct ipa_msg_meta))) { |
| 574 | ret = -EFAULT; |
Utkarsh Saxena | 9619488 | 2017-04-03 13:21:58 +0530 | [diff] [blame] | 575 | kfree(msg); |
| 576 | msg = NULL; |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | buf += sizeof(struct ipa_msg_meta); |
| 580 | count -= sizeof(struct ipa_msg_meta); |
| 581 | if (msg->buff) { |
| 582 | if (copy_to_user(buf, msg->buff, |
| 583 | msg->meta.msg_len)) { |
| 584 | ret = -EFAULT; |
Utkarsh Saxena | 9619488 | 2017-04-03 13:21:58 +0530 | [diff] [blame] | 585 | kfree(msg); |
| 586 | msg = NULL; |
Amir Levy | 9659e59 | 2016-10-27 18:08:27 +0300 | [diff] [blame] | 587 | break; |
| 588 | } |
| 589 | buf += msg->meta.msg_len; |
| 590 | count -= msg->meta.msg_len; |
| 591 | msg->callback(msg->buff, msg->meta.msg_len, |
| 592 | msg->meta.msg_type); |
| 593 | } |
| 594 | IPA_STATS_INC_CNT( |
| 595 | ipa3_ctx->stats.msg_r[msg->meta.msg_type]); |
| 596 | kfree(msg); |
| 597 | } |
| 598 | |
| 599 | ret = -EAGAIN; |
| 600 | if (filp->f_flags & O_NONBLOCK) |
| 601 | break; |
| 602 | |
| 603 | ret = -EINTR; |
| 604 | if (signal_pending(current)) |
| 605 | break; |
| 606 | |
| 607 | if (start != buf) |
| 608 | break; |
| 609 | |
| 610 | locked = 0; |
| 611 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 612 | schedule(); |
| 613 | } |
| 614 | |
| 615 | finish_wait(&ipa3_ctx->msg_waitq, &wait); |
| 616 | if (start != buf && ret != -EFAULT) |
| 617 | ret = buf - start; |
| 618 | |
| 619 | if (locked) |
| 620 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 621 | |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * ipa3_pull_msg() - pull the specified message from client |
| 627 | * @meta: [in] message meta-data |
| 628 | * @buf: [out] buffer to read into |
| 629 | * @count: [in] size of above buffer |
| 630 | * |
| 631 | * Populate the supplied buffer with the pull message which is fetched |
| 632 | * from client, the message must have previously been registered with |
| 633 | * the IPA driver |
| 634 | * |
| 635 | * Returns: how many bytes copied to buffer |
| 636 | * |
| 637 | * Note: Should not be called from atomic context |
| 638 | */ |
| 639 | int ipa3_pull_msg(struct ipa_msg_meta *meta, char *buff, size_t count) |
| 640 | { |
| 641 | struct ipa3_pull_msg *entry; |
| 642 | int result = -EINVAL; |
| 643 | |
| 644 | if (meta == NULL || buff == NULL || !count) { |
| 645 | IPAERR("invalid param name=%p buff=%p count=%zu\n", |
| 646 | meta, buff, count); |
| 647 | return result; |
| 648 | } |
| 649 | |
| 650 | mutex_lock(&ipa3_ctx->msg_lock); |
| 651 | list_for_each_entry(entry, &ipa3_ctx->pull_msg_list, link) { |
| 652 | if (entry->meta.msg_len == meta->msg_len && |
| 653 | entry->meta.msg_type == meta->msg_type) { |
| 654 | result = entry->callback(buff, count, meta->msg_type); |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | mutex_unlock(&ipa3_ctx->msg_lock); |
| 659 | return result; |
| 660 | } |