ipa_intf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* Copyright (c) 2013-2019, 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. #include <linux/fs.h>
  13. #include <linux/sched.h>
  14. #include "ipa_i.h"
  15. struct ipa_intf {
  16. char name[IPA_RESOURCE_NAME_MAX];
  17. struct list_head link;
  18. u32 num_tx_props;
  19. u32 num_rx_props;
  20. struct ipa_ioc_tx_intf_prop *tx;
  21. struct ipa_ioc_rx_intf_prop *rx;
  22. };
  23. struct ipa_push_msg {
  24. struct ipa_msg_meta meta;
  25. ipa_msg_free_fn callback;
  26. void *buff;
  27. struct list_head link;
  28. };
  29. struct ipa_pull_msg {
  30. struct ipa_msg_meta meta;
  31. ipa_msg_pull_fn callback;
  32. struct list_head link;
  33. };
  34. /**
  35. * ipa_register_intf() - register "logical" interface
  36. * @name: [in] interface name
  37. * @tx: [in] TX properties of the interface
  38. * @rx: [in] RX properties of the interface
  39. *
  40. * Register an interface and its tx and rx properties, this allows
  41. * configuration of rules from user-space
  42. *
  43. * Returns: 0 on success, negative on failure
  44. *
  45. * Note: Should not be called from atomic context
  46. */
  47. int ipa_register_intf(const char *name, const struct ipa_tx_intf *tx,
  48. const struct ipa_rx_intf *rx)
  49. {
  50. struct ipa_intf *intf;
  51. u32 len;
  52. if (name == NULL || (tx == NULL && rx == NULL)) {
  53. IPAERR("invalid params name=%p tx=%p rx=%p\n", name, tx, rx);
  54. return -EINVAL;
  55. }
  56. if (tx && tx->num_props > IPA_NUM_PROPS_MAX) {
  57. IPAERR("invalid tx num_props=%d max=%d\n", tx->num_props,
  58. IPA_NUM_PROPS_MAX);
  59. return -EINVAL;
  60. }
  61. if (rx && rx->num_props > IPA_NUM_PROPS_MAX) {
  62. IPAERR("invalid rx num_props=%d max=%d\n", rx->num_props,
  63. IPA_NUM_PROPS_MAX);
  64. return -EINVAL;
  65. }
  66. len = sizeof(struct ipa_intf);
  67. intf = kzalloc(len, GFP_KERNEL);
  68. if (intf == NULL) {
  69. IPAERR("fail to alloc 0x%x bytes\n", len);
  70. return -ENOMEM;
  71. }
  72. strlcpy(intf->name, name, IPA_RESOURCE_NAME_MAX);
  73. if (tx) {
  74. intf->num_tx_props = tx->num_props;
  75. len = tx->num_props * sizeof(struct ipa_ioc_tx_intf_prop);
  76. intf->tx = kzalloc(len, GFP_KERNEL);
  77. if (intf->tx == NULL) {
  78. IPAERR("fail to alloc 0x%x bytes\n", len);
  79. kfree(intf);
  80. return -ENOMEM;
  81. }
  82. memcpy(intf->tx, tx->prop, len);
  83. }
  84. if (rx) {
  85. intf->num_rx_props = rx->num_props;
  86. len = rx->num_props * sizeof(struct ipa_ioc_rx_intf_prop);
  87. intf->rx = kzalloc(len, GFP_KERNEL);
  88. if (intf->rx == NULL) {
  89. IPAERR("fail to alloc 0x%x bytes\n", len);
  90. kfree(intf->tx);
  91. kfree(intf);
  92. return -ENOMEM;
  93. }
  94. memcpy(intf->rx, rx->prop, len);
  95. }
  96. mutex_lock(&ipa_ctx->lock);
  97. list_add_tail(&intf->link, &ipa_ctx->intf_list);
  98. mutex_unlock(&ipa_ctx->lock);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(ipa_register_intf);
  102. /**
  103. * ipa_deregister_intf() - de-register previously registered logical interface
  104. * @name: [in] interface name
  105. *
  106. * De-register a previously registered interface
  107. *
  108. * Returns: 0 on success, negative on failure
  109. *
  110. * Note: Should not be called from atomic context
  111. */
  112. int ipa_deregister_intf(const char *name)
  113. {
  114. struct ipa_intf *entry;
  115. struct ipa_intf *next;
  116. int result = -EINVAL;
  117. if (name == NULL) {
  118. IPAERR("invalid param name=%p\n", name);
  119. return result;
  120. }
  121. mutex_lock(&ipa_ctx->lock);
  122. list_for_each_entry_safe(entry, next, &ipa_ctx->intf_list, link) {
  123. if (!strncmp(entry->name, name, IPA_RESOURCE_NAME_MAX)) {
  124. list_del(&entry->link);
  125. kfree(entry->rx);
  126. kfree(entry->tx);
  127. kfree(entry);
  128. result = 0;
  129. break;
  130. }
  131. }
  132. mutex_unlock(&ipa_ctx->lock);
  133. return result;
  134. }
  135. EXPORT_SYMBOL(ipa_deregister_intf);
  136. /**
  137. * ipa_query_intf() - query logical interface properties
  138. * @lookup: [inout] interface name and number of properties
  139. *
  140. * Obtain the handle and number of tx and rx properties for the named
  141. * interface, used as part of querying the tx and rx properties for
  142. * configuration of various rules from user-space
  143. *
  144. * Returns: 0 on success, negative on failure
  145. *
  146. * Note: Should not be called from atomic context
  147. */
  148. int ipa_query_intf(struct ipa_ioc_query_intf *lookup)
  149. {
  150. struct ipa_intf *entry;
  151. int result = -EINVAL;
  152. if (lookup == NULL) {
  153. IPAERR("invalid param lookup=%p\n", lookup);
  154. return result;
  155. }
  156. mutex_lock(&ipa_ctx->lock);
  157. list_for_each_entry(entry, &ipa_ctx->intf_list, link) {
  158. if (!strncmp(entry->name, lookup->name,
  159. IPA_RESOURCE_NAME_MAX)) {
  160. lookup->num_tx_props = entry->num_tx_props;
  161. lookup->num_rx_props = entry->num_rx_props;
  162. result = 0;
  163. break;
  164. }
  165. }
  166. mutex_unlock(&ipa_ctx->lock);
  167. return result;
  168. }
  169. /**
  170. * ipa_query_intf_tx_props() - qeury TX props of an interface
  171. * @tx: [inout] interface tx attributes
  172. *
  173. * Obtain the tx properties for the specifed interface
  174. *
  175. * Returns: 0 on success, negative on failure
  176. *
  177. * Note: Should not be called from atomic context
  178. */
  179. int ipa_query_intf_tx_props(struct ipa_ioc_query_intf_tx_props *tx)
  180. {
  181. struct ipa_intf *entry;
  182. int result = -EINVAL;
  183. if (tx == NULL) {
  184. IPAERR("invalid param tx=%p\n", tx);
  185. return result;
  186. }
  187. mutex_lock(&ipa_ctx->lock);
  188. list_for_each_entry(entry, &ipa_ctx->intf_list, link) {
  189. if (!strncmp(entry->name, tx->name, IPA_RESOURCE_NAME_MAX)) {
  190. /* add the entry check */
  191. if (entry->num_tx_props != tx->num_tx_props) {
  192. IPAERR("invalid entry number(%u %u)\n",
  193. entry->num_tx_props,
  194. tx->num_tx_props);
  195. mutex_unlock(&ipa_ctx->lock);
  196. return result;
  197. }
  198. memcpy(tx->tx, entry->tx, entry->num_tx_props *
  199. sizeof(struct ipa_ioc_tx_intf_prop));
  200. result = 0;
  201. break;
  202. }
  203. }
  204. mutex_unlock(&ipa_ctx->lock);
  205. return result;
  206. }
  207. /**
  208. * ipa_query_intf_rx_props() - qeury RX props of an interface
  209. * @rx: [inout] interface rx attributes
  210. *
  211. * Obtain the rx properties for the specifed interface
  212. *
  213. * Returns: 0 on success, negative on failure
  214. *
  215. * Note: Should not be called from atomic context
  216. */
  217. int ipa_query_intf_rx_props(struct ipa_ioc_query_intf_rx_props *rx)
  218. {
  219. struct ipa_intf *entry;
  220. int result = -EINVAL;
  221. if (rx == NULL) {
  222. IPAERR("invalid param rx=%p\n", rx);
  223. return result;
  224. }
  225. mutex_lock(&ipa_ctx->lock);
  226. list_for_each_entry(entry, &ipa_ctx->intf_list, link) {
  227. if (!strncmp(entry->name, rx->name, IPA_RESOURCE_NAME_MAX)) {
  228. /* add the entry check */
  229. if (entry->num_rx_props != rx->num_rx_props) {
  230. IPAERR("invalid entry number(%u %u)\n",
  231. entry->num_rx_props,
  232. rx->num_rx_props);
  233. mutex_unlock(&ipa_ctx->lock);
  234. return result;
  235. }
  236. memcpy(rx->rx, entry->rx, entry->num_rx_props *
  237. sizeof(struct ipa_ioc_rx_intf_prop));
  238. result = 0;
  239. break;
  240. }
  241. }
  242. mutex_unlock(&ipa_ctx->lock);
  243. return result;
  244. }
  245. /**
  246. * ipa_send_msg() - Send "message" from kernel client to IPA driver
  247. * @meta: [in] message meta-data
  248. * @buff: [in] the payload for message
  249. * @callback: [in] free callback
  250. *
  251. * Client supplies the message meta-data and payload which IPA driver buffers
  252. * till read by user-space. After read from user space IPA driver invokes the
  253. * callback supplied to free the message payload. Client must not touch/free
  254. * the message payload after calling this API.
  255. *
  256. * Returns: 0 on success, negative on failure
  257. *
  258. * Note: Should not be called from atomic context
  259. */
  260. int ipa_send_msg(struct ipa_msg_meta *meta, void *buff,
  261. ipa_msg_free_fn callback)
  262. {
  263. struct ipa_push_msg *msg;
  264. if (meta == NULL || (buff == NULL && callback != NULL) ||
  265. (buff != NULL && callback == NULL)) {
  266. IPAERR("invalid param meta=%p buff=%p, callback=%p\n",
  267. meta, buff, callback);
  268. return -EINVAL;
  269. }
  270. if (meta->msg_type >= IPA_EVENT_MAX) {
  271. IPAERR("unsupported message type %d\n", meta->msg_type);
  272. return -EINVAL;
  273. }
  274. msg = kzalloc(sizeof(struct ipa_push_msg), GFP_KERNEL);
  275. if (msg == NULL) {
  276. IPAERR("fail to alloc ipa_msg container\n");
  277. return -ENOMEM;
  278. }
  279. msg->meta = *meta;
  280. msg->buff = buff;
  281. msg->callback = callback;
  282. mutex_lock(&ipa_ctx->msg_lock);
  283. list_add_tail(&msg->link, &ipa_ctx->msg_list);
  284. mutex_unlock(&ipa_ctx->msg_lock);
  285. IPA_STATS_INC_CNT(ipa_ctx->stats.msg_w[meta->msg_type]);
  286. wake_up(&ipa_ctx->msg_waitq);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(ipa_send_msg);
  290. /**
  291. * ipa_register_pull_msg() - register pull message type
  292. * @meta: [in] message meta-data
  293. * @callback: [in] pull callback
  294. *
  295. * Register message callback by kernel client with IPA driver for IPA driver to
  296. * pull message on-demand.
  297. *
  298. * Returns: 0 on success, negative on failure
  299. *
  300. * Note: Should not be called from atomic context
  301. */
  302. int ipa_register_pull_msg(struct ipa_msg_meta *meta, ipa_msg_pull_fn callback)
  303. {
  304. struct ipa_pull_msg *msg;
  305. if (meta == NULL || callback == NULL) {
  306. IPAERR("invalid param meta=%p callback=%p\n", meta, callback);
  307. return -EINVAL;
  308. }
  309. msg = kzalloc(sizeof(struct ipa_pull_msg), GFP_KERNEL);
  310. if (msg == NULL) {
  311. IPAERR("fail to alloc ipa_msg container\n");
  312. return -ENOMEM;
  313. }
  314. msg->meta = *meta;
  315. msg->callback = callback;
  316. mutex_lock(&ipa_ctx->msg_lock);
  317. list_add_tail(&msg->link, &ipa_ctx->pull_msg_list);
  318. mutex_unlock(&ipa_ctx->msg_lock);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL(ipa_register_pull_msg);
  322. /**
  323. * ipa_deregister_pull_msg() - De-register pull message type
  324. * @meta: [in] message meta-data
  325. *
  326. * De-register "message" by kernel client from IPA driver
  327. *
  328. * Returns: 0 on success, negative on failure
  329. *
  330. * Note: Should not be called from atomic context
  331. */
  332. int ipa_deregister_pull_msg(struct ipa_msg_meta *meta)
  333. {
  334. struct ipa_pull_msg *entry;
  335. struct ipa_pull_msg *next;
  336. int result = -EINVAL;
  337. if (meta == NULL) {
  338. IPAERR("invalid param name=%p\n", meta);
  339. return result;
  340. }
  341. mutex_lock(&ipa_ctx->msg_lock);
  342. list_for_each_entry_safe(entry, next, &ipa_ctx->pull_msg_list, link) {
  343. if (entry->meta.msg_len == meta->msg_len &&
  344. entry->meta.msg_type == meta->msg_type) {
  345. list_del(&entry->link);
  346. kfree(entry);
  347. result = 0;
  348. break;
  349. }
  350. }
  351. mutex_unlock(&ipa_ctx->msg_lock);
  352. return result;
  353. }
  354. EXPORT_SYMBOL(ipa_deregister_pull_msg);
  355. /**
  356. * ipa_read() - read message from IPA device
  357. * @filp: [in] file pointer
  358. * @buf: [out] buffer to read into
  359. * @count: [in] size of above buffer
  360. * @f_pos: [inout] file position
  361. *
  362. * Uer-space should continually read from /dev/ipa, read wll block when there
  363. * are no messages to read. Upon return, user-space should read the ipa_msg_meta
  364. * from the start of the buffer to know what type of message was read and its
  365. * length in the remainder of the buffer. Buffer supplied must be big enough to
  366. * hold the message meta-data and the largest defined message type
  367. *
  368. * Returns: how many bytes copied to buffer
  369. *
  370. * Note: Should not be called from atomic context
  371. */
  372. ssize_t ipa_read(struct file *filp, char __user *buf, size_t count,
  373. loff_t *f_pos)
  374. {
  375. char __user *start;
  376. struct ipa_push_msg *msg = NULL;
  377. int ret;
  378. DEFINE_WAIT(wait);
  379. int locked;
  380. start = buf;
  381. while (1) {
  382. prepare_to_wait(&ipa_ctx->msg_waitq, &wait, TASK_INTERRUPTIBLE);
  383. mutex_lock(&ipa_ctx->msg_lock);
  384. locked = 1;
  385. if (!list_empty(&ipa_ctx->msg_list)) {
  386. msg = list_first_entry(&ipa_ctx->msg_list,
  387. struct ipa_push_msg, link);
  388. list_del(&msg->link);
  389. }
  390. IPADBG("msg=%p\n", msg);
  391. if (msg) {
  392. locked = 0;
  393. mutex_unlock(&ipa_ctx->msg_lock);
  394. if (count < sizeof(struct ipa_msg_meta)) {
  395. kfree(msg);
  396. msg = NULL;
  397. ret = -EFAULT;
  398. break;
  399. }
  400. if (copy_to_user(buf, &msg->meta,
  401. sizeof(struct ipa_msg_meta))) {
  402. kfree(msg);
  403. msg = NULL;
  404. ret = -EFAULT;
  405. break;
  406. }
  407. buf += sizeof(struct ipa_msg_meta);
  408. count -= sizeof(struct ipa_msg_meta);
  409. if (msg->buff) {
  410. if (count >= msg->meta.msg_len) {
  411. if (copy_to_user(buf, msg->buff,
  412. msg->meta.msg_len)) {
  413. kfree(msg);
  414. msg = NULL;
  415. ret = -EFAULT;
  416. break;
  417. }
  418. } else {
  419. kfree(msg);
  420. msg = NULL;
  421. ret = -EFAULT;
  422. break;
  423. }
  424. buf += msg->meta.msg_len;
  425. count -= msg->meta.msg_len;
  426. msg->callback(msg->buff, msg->meta.msg_len,
  427. msg->meta.msg_type);
  428. }
  429. IPA_STATS_INC_CNT(
  430. ipa_ctx->stats.msg_r[msg->meta.msg_type]);
  431. kfree(msg);
  432. }
  433. ret = -EAGAIN;
  434. if (filp->f_flags & O_NONBLOCK)
  435. break;
  436. ret = -EINTR;
  437. if (signal_pending(current))
  438. break;
  439. if (start != buf)
  440. break;
  441. locked = 0;
  442. mutex_unlock(&ipa_ctx->msg_lock);
  443. schedule();
  444. }
  445. finish_wait(&ipa_ctx->msg_waitq, &wait);
  446. if (start != buf && ret != -EFAULT)
  447. ret = buf - start;
  448. if (locked)
  449. mutex_unlock(&ipa_ctx->msg_lock);
  450. return ret;
  451. }
  452. /**
  453. * ipa_pull_msg() - pull the specified message from client
  454. * @meta: [in] message meta-data
  455. * @buf: [out] buffer to read into
  456. * @count: [in] size of above buffer
  457. *
  458. * Populate the supplied buffer with the pull message which is fetched
  459. * from client, the message must have previously been registered with
  460. * the IPA driver
  461. *
  462. * Returns: how many bytes copied to buffer
  463. *
  464. * Note: Should not be called from atomic context
  465. */
  466. int ipa_pull_msg(struct ipa_msg_meta *meta, char *buff, size_t count)
  467. {
  468. struct ipa_pull_msg *entry;
  469. int result = -EINVAL;
  470. if (meta == NULL || buff == NULL || !count) {
  471. IPAERR("invalid param name=%p buff=%p count=%zu\n",
  472. meta, buff, count);
  473. return result;
  474. }
  475. mutex_lock(&ipa_ctx->msg_lock);
  476. list_for_each_entry(entry, &ipa_ctx->pull_msg_list, link) {
  477. if (entry->meta.msg_len == meta->msg_len &&
  478. entry->meta.msg_type == meta->msg_type) {
  479. result = entry->callback(buff, count, meta->msg_type);
  480. break;
  481. }
  482. }
  483. mutex_unlock(&ipa_ctx->msg_lock);
  484. return result;
  485. }