rpmsg_rpc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Remote Processor Procedure Call Driver
  4. *
  5. * Copyright (C) 2012-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Erik Rainey <erik.rainey@ti.com>
  7. * Suman Anna <s-anna@ti.com>
  8. */
  9. #define pr_fmt(fmt) "%s: " fmt, __func__
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/idr.h>
  14. #include <linux/poll.h>
  15. #include <linux/mutex.h>
  16. #include <linux/sched.h>
  17. #include <linux/fdtable.h>
  18. #include <linux/remoteproc.h>
  19. #include <linux/rpmsg.h>
  20. #include <linux/rpmsg_rpc.h>
  21. #include <linux/rpmsg/virtio_rpmsg.h>
  22. #include <linux/sched/signal.h>
  23. #include "rpmsg_rpc_internal.h"
  24. #define RPPC_MAX_DEVICES (8)
  25. #define RPPC_MAX_REG_FDS (10)
  26. #define RPPC_SIG_NUM_PARAM(sig) ((sig).num_param - 1)
  27. /* TODO: remove these fields */
  28. #define RPPC_JOBID_DISCRETE (0)
  29. #define RPPC_POOLID_DEFAULT (0x8000)
  30. static struct class *rppc_class;
  31. static dev_t rppc_dev;
  32. /* store all remote rpc connection services (usually one per remoteproc) */
  33. static DEFINE_IDR(rppc_devices);
  34. static DEFINE_MUTEX(rppc_devices_lock);
  35. /*
  36. * Retrieve the rproc instance so that it can be used for performing
  37. * address translations
  38. */
  39. static inline struct rproc *rpdev_to_rproc(struct rpmsg_device *rpdev)
  40. {
  41. return rproc_get_by_child(&rpdev->dev);
  42. }
  43. /*
  44. * A wrapper function to translate local physical addresses to the remote core
  45. * device addresses (virtual addresses that a code on remote processor can use
  46. * directly.
  47. *
  48. * XXX: Fix this to return negative values on errors to follow normal kernel
  49. * conventions, and since 0 can also be a valid remote processor address
  50. *
  51. * Returns a remote processor device address on success, 0 otherwise
  52. */
  53. dev_addr_t rppc_local_to_remote_da(struct rppc_instance *rpc, phys_addr_t pa)
  54. {
  55. int ret;
  56. struct rproc *rproc;
  57. u64 da = 0;
  58. dev_addr_t rda;
  59. struct device *dev = rpc->dev;
  60. if (mutex_lock_interruptible(&rpc->rppcdev->lock))
  61. return 0;
  62. rproc = rpdev_to_rproc(rpc->rppcdev->rpdev);
  63. if (!rproc) {
  64. dev_err(dev, "error getting rproc for rpdev 0x%x\n",
  65. (u32)rpc->rppcdev->rpdev);
  66. } else {
  67. ret = rproc_pa_to_da(rproc, pa, &da);
  68. if (ret) {
  69. dev_err(dev, "error from rproc_pa_to_da, rproc = %p, pa = %pa ret = %d\n",
  70. rproc, &pa, ret);
  71. }
  72. }
  73. rda = (dev_addr_t)da;
  74. mutex_unlock(&rpc->rppcdev->lock);
  75. return rda;
  76. }
  77. static void rppc_print_msg(struct rppc_instance *rpc, char *prefix,
  78. char buffer[512])
  79. {
  80. struct rppc_msg_header *hdr = (struct rppc_msg_header *)buffer;
  81. struct rppc_instance_handle *hdl = NULL;
  82. struct rppc_query_function *info = NULL;
  83. struct rppc_packet *packet = NULL;
  84. struct rppc_param_data *param = NULL;
  85. struct device *dev = rpc->dev;
  86. u32 i = 0, paramsz = sizeof(*param);
  87. dev_dbg(dev, "%s HDR: msg_type = %d msg_len = %d\n",
  88. prefix, hdr->msg_type, hdr->msg_len);
  89. switch (hdr->msg_type) {
  90. case RPPC_MSGTYPE_CREATE_RESP:
  91. case RPPC_MSGTYPE_DELETE_RESP:
  92. hdl = RPPC_PAYLOAD(buffer, rppc_instance_handle);
  93. dev_dbg(dev, "%s endpoint = %d status = %d\n",
  94. prefix, hdl->endpoint_address, hdl->status);
  95. break;
  96. case RPPC_MSGTYPE_FUNCTION_INFO:
  97. info = RPPC_PAYLOAD(buffer, rppc_query_function);
  98. dev_dbg(dev, "%s (info not yet implemented)\n", prefix);
  99. break;
  100. case RPPC_MSGTYPE_FUNCTION_CALL:
  101. packet = RPPC_PAYLOAD(buffer, rppc_packet);
  102. dev_dbg(dev, "%s PACKET: desc = %04x msg_id = %04x flags = %08x func = 0x%08x result = %d size = %u\n",
  103. prefix, packet->desc, packet->msg_id,
  104. packet->flags, packet->fxn_id,
  105. packet->result, packet->data_size);
  106. param = (struct rppc_param_data *)packet->data;
  107. for (i = 0; i < (packet->data_size / paramsz); i++) {
  108. dev_dbg(dev, "%s param[%u] size = %zu data = %zu (0x%08x)",
  109. prefix, i, param[i].size, param[i].data,
  110. param[i].data);
  111. }
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. /* free any outstanding function calls */
  118. static void rppc_delete_fxns(struct rppc_instance *rpc)
  119. {
  120. struct rppc_function_list *pos, *n;
  121. if (!list_empty(&rpc->fxn_list)) {
  122. mutex_lock(&rpc->lock);
  123. list_for_each_entry_safe(pos, n, &rpc->fxn_list, list) {
  124. list_del(&pos->list);
  125. kfree(pos->function);
  126. kfree(pos);
  127. }
  128. mutex_unlock(&rpc->lock);
  129. }
  130. }
  131. static
  132. struct rppc_function *rppc_find_fxn(struct rppc_instance *rpc, u16 msg_id)
  133. {
  134. struct rppc_function *function = NULL;
  135. struct rppc_function_list *pos, *n;
  136. struct device *dev = rpc->dev;
  137. mutex_lock(&rpc->lock);
  138. list_for_each_entry_safe(pos, n, &rpc->fxn_list, list) {
  139. dev_dbg(dev, "looking for msg %u, found msg %u\n",
  140. msg_id, pos->msg_id);
  141. if (pos->msg_id == msg_id) {
  142. function = pos->function;
  143. list_del(&pos->list);
  144. kfree(pos);
  145. break;
  146. }
  147. }
  148. mutex_unlock(&rpc->lock);
  149. return function;
  150. }
  151. static int rppc_add_fxn(struct rppc_instance *rpc,
  152. struct rppc_function *function, u16 msg_id)
  153. {
  154. struct rppc_function_list *fxn = NULL;
  155. struct device *dev = rpc->dev;
  156. fxn = kzalloc(sizeof(*fxn), GFP_KERNEL);
  157. if (!fxn)
  158. return -ENOMEM;
  159. fxn->function = function;
  160. fxn->msg_id = msg_id;
  161. mutex_lock(&rpc->lock);
  162. list_add(&fxn->list, &rpc->fxn_list);
  163. mutex_unlock(&rpc->lock);
  164. dev_dbg(dev, "added msg id %u to list", msg_id);
  165. return 0;
  166. }
  167. static
  168. void rppc_handle_create_resp(struct rppc_instance *rpc, char *data, int len)
  169. {
  170. struct device *dev = rpc->dev;
  171. struct rppc_msg_header *hdr = (struct rppc_msg_header *)data;
  172. struct rppc_instance_handle *hdl;
  173. u32 exp_len = sizeof(*hdl) + sizeof(*hdr);
  174. if (len != exp_len) {
  175. dev_err(dev, "invalid response message length %d (expected %d bytes)",
  176. len, exp_len);
  177. rpc->state = RPPC_STATE_STALE;
  178. return;
  179. }
  180. hdl = RPPC_PAYLOAD(data, rppc_instance_handle);
  181. mutex_lock(&rpc->lock);
  182. if (rpc->state != RPPC_STATE_STALE && hdl->status == 0) {
  183. rpc->dst = hdl->endpoint_address;
  184. rpc->state = RPPC_STATE_CONNECTED;
  185. } else {
  186. rpc->state = RPPC_STATE_STALE;
  187. }
  188. rpc->in_transition = 0;
  189. dev_dbg(dev, "creation response: status %d addr 0x%x\n",
  190. hdl->status, hdl->endpoint_address);
  191. complete(&rpc->reply_arrived);
  192. mutex_unlock(&rpc->lock);
  193. }
  194. static
  195. void rppc_handle_delete_resp(struct rppc_instance *rpc, char *data, int len)
  196. {
  197. struct device *dev = rpc->dev;
  198. struct rppc_msg_header *hdr = (struct rppc_msg_header *)data;
  199. struct rppc_instance_handle *hdl;
  200. u32 exp_len = sizeof(*hdl) + sizeof(*hdr);
  201. if (len != exp_len) {
  202. dev_err(dev, "invalid response message length %d (expected %d bytes)",
  203. len, exp_len);
  204. rpc->state = RPPC_STATE_STALE;
  205. return;
  206. }
  207. if (hdr->msg_len != sizeof(*hdl)) {
  208. dev_err(dev, "disconnect message was incorrect size!\n");
  209. rpc->state = RPPC_STATE_STALE;
  210. return;
  211. }
  212. hdl = RPPC_PAYLOAD(data, rppc_instance_handle);
  213. dev_dbg(dev, "deletion response: status %d addr 0x%x\n",
  214. hdl->status, hdl->endpoint_address);
  215. mutex_lock(&rpc->lock);
  216. rpc->dst = 0;
  217. rpc->state = RPPC_STATE_DISCONNECTED;
  218. rpc->in_transition = 0;
  219. complete(&rpc->reply_arrived);
  220. mutex_unlock(&rpc->lock);
  221. }
  222. /*
  223. * store the received message and wake up any blocking processes,
  224. * waiting for new data. The allocated buffer would be freed after
  225. * the user-space reads the packet.
  226. */
  227. static void rppc_handle_fxn_resp(struct rppc_instance *rpc, char *data, int len)
  228. {
  229. struct rppc_msg_header *hdr = (struct rppc_msg_header *)data;
  230. struct sk_buff *skb;
  231. char *skbdata;
  232. /* TODO: need to check the response length? */
  233. skb = alloc_skb(hdr->msg_len, GFP_KERNEL);
  234. if (!skb)
  235. return;
  236. skbdata = skb_put(skb, hdr->msg_len);
  237. memcpy(skbdata, hdr->msg_data, hdr->msg_len);
  238. mutex_lock(&rpc->lock);
  239. skb_queue_tail(&rpc->queue, skb);
  240. mutex_unlock(&rpc->lock);
  241. wake_up_interruptible(&rpc->readq);
  242. }
  243. /*
  244. * callback function for processing the different responses
  245. * from the remote processor on a particular rpmsg channel
  246. * instance.
  247. */
  248. static int rppc_cb(struct rpmsg_device *rpdev,
  249. void *data, int len, void *priv, u32 src)
  250. {
  251. struct rppc_msg_header *hdr = data;
  252. struct rppc_instance *rpc = priv;
  253. struct device *dev = rpc->dev;
  254. char *buf = (char *)data;
  255. dev_dbg(dev, "<== incoming msg src %d len %d msg_type %d msg_len %d\n",
  256. src, len, hdr->msg_type, hdr->msg_len);
  257. rppc_print_msg(rpc, "RX:", buf);
  258. if (len <= sizeof(*hdr)) {
  259. dev_err(dev, "message truncated\n");
  260. rpc->state = RPPC_STATE_STALE;
  261. return -EINVAL;
  262. }
  263. switch (hdr->msg_type) {
  264. case RPPC_MSGTYPE_CREATE_RESP:
  265. rppc_handle_create_resp(rpc, data, len);
  266. break;
  267. case RPPC_MSGTYPE_DELETE_RESP:
  268. rppc_handle_delete_resp(rpc, data, len);
  269. break;
  270. case RPPC_MSGTYPE_FUNCTION_CALL:
  271. case RPPC_MSGTYPE_FUNCTION_RET:
  272. rppc_handle_fxn_resp(rpc, data, len);
  273. break;
  274. default:
  275. dev_warn(dev, "unexpected msg type: %d\n", hdr->msg_type);
  276. break;
  277. }
  278. return 0;
  279. }
  280. /*
  281. * send a connection request to the remote rpc connection service. Use
  282. * the new local address created during .open for this instance as the
  283. * source address to complete the connection.
  284. */
  285. static int rppc_connect(struct rppc_instance *rpc,
  286. struct rppc_create_instance *connect)
  287. {
  288. int ret = 0;
  289. u32 len = 0;
  290. char kbuf[512];
  291. struct rppc_device *rppcdev = rpc->rppcdev;
  292. struct rppc_msg_header *hdr = (struct rppc_msg_header *)&kbuf[0];
  293. if (rpc->state == RPPC_STATE_CONNECTED) {
  294. dev_dbg(rpc->dev, "endpoint already connected\n");
  295. return -EISCONN;
  296. }
  297. hdr->msg_type = RPPC_MSGTYPE_CREATE_REQ;
  298. hdr->msg_len = sizeof(*connect);
  299. memcpy(hdr->msg_data, connect, hdr->msg_len);
  300. len = sizeof(struct rppc_msg_header) + hdr->msg_len;
  301. init_completion(&rpc->reply_arrived);
  302. rpc->in_transition = 1;
  303. ret = rpmsg_send_offchannel(rppcdev->rpdev->ept, rpc->ept->addr,
  304. rppcdev->rpdev->dst, (char *)kbuf, len);
  305. if (ret > 0) {
  306. dev_err(rpc->dev, "rpmsg_send failed: %d\n", ret);
  307. return ret;
  308. }
  309. ret = wait_for_completion_interruptible_timeout(&rpc->reply_arrived,
  310. msecs_to_jiffies(5000));
  311. if (rpc->state == RPPC_STATE_CONNECTED)
  312. return 0;
  313. if (rpc->state == RPPC_STATE_STALE)
  314. return -ENXIO;
  315. if (ret > 0) {
  316. dev_err(rpc->dev, "premature wakeup: %d\n", ret);
  317. return -EIO;
  318. }
  319. return -ETIMEDOUT;
  320. }
  321. static void rppc_disconnect(struct rppc_instance *rpc)
  322. {
  323. int ret;
  324. size_t len;
  325. char kbuf[512];
  326. struct rppc_device *rppcdev = rpc->rppcdev;
  327. struct rppc_msg_header *hdr = (struct rppc_msg_header *)&kbuf[0];
  328. struct rppc_instance_handle *handle =
  329. RPPC_PAYLOAD(kbuf, rppc_instance_handle);
  330. if (rpc->state != RPPC_STATE_CONNECTED)
  331. return;
  332. hdr->msg_type = RPPC_MSGTYPE_DELETE_REQ;
  333. hdr->msg_len = sizeof(u32);
  334. handle->endpoint_address = rpc->dst;
  335. handle->status = 0;
  336. len = sizeof(struct rppc_msg_header) + hdr->msg_len;
  337. dev_dbg(rpc->dev, "disconnecting from RPC service at %d\n",
  338. rpc->dst);
  339. ret = rpmsg_send_offchannel(rppcdev->rpdev->ept, rpc->ept->addr,
  340. rppcdev->rpdev->dst, kbuf, len);
  341. if (ret)
  342. dev_err(rpc->dev, "rpmsg_send failed: %d\n", ret);
  343. /*
  344. * TODO: should we wait for a message to come back?
  345. * For now, no.
  346. */
  347. wait_for_completion_interruptible(&rpc->reply_arrived);
  348. }
  349. static int rppc_register_buffers(struct rppc_instance *rpc,
  350. unsigned long arg)
  351. {
  352. struct rppc_buf_fds data;
  353. int *fds = NULL;
  354. struct rppc_dma_buf **bufs = NULL;
  355. struct rppc_dma_buf *tmp;
  356. int i = 0, ret = 0;
  357. if (copy_from_user(&data, (char __user *)arg, sizeof(data)))
  358. return -EFAULT;
  359. /* impose a maximum number of buffers for now */
  360. if (data.num > RPPC_MAX_REG_FDS)
  361. return -EINVAL;
  362. fds = kcalloc(data.num, sizeof(*fds), GFP_KERNEL);
  363. if (!fds)
  364. return -ENOMEM;
  365. if (copy_from_user(fds, (char __user *)data.fds,
  366. sizeof(*fds) * data.num)) {
  367. ret = -EFAULT;
  368. goto free_fds;
  369. }
  370. for (i = 0; i < data.num; i++) {
  371. rcu_read_lock();
  372. if (!fcheck(fds[i])) {
  373. rcu_read_unlock();
  374. ret = -EBADF;
  375. goto free_fds;
  376. }
  377. rcu_read_unlock();
  378. tmp = rppc_find_dmabuf(rpc, fds[i]);
  379. if (!IS_ERR_OR_NULL(tmp)) {
  380. ret = -EEXIST;
  381. goto free_fds;
  382. }
  383. }
  384. bufs = kcalloc(data.num, sizeof(*bufs), GFP_KERNEL);
  385. if (!bufs) {
  386. ret = -ENOMEM;
  387. goto free_fds;
  388. }
  389. for (i = 0; i < data.num; i++) {
  390. bufs[i] = rppc_alloc_dmabuf(rpc, fds[i], false);
  391. if (IS_ERR(bufs[i])) {
  392. ret = PTR_ERR(bufs[i]);
  393. break;
  394. }
  395. }
  396. if (i == data.num)
  397. goto free_bufs;
  398. for (i -= 1; i >= 0; i--)
  399. rppc_free_dmabuf(bufs[i]->id, bufs[i], rpc);
  400. free_bufs:
  401. kfree(bufs);
  402. free_fds:
  403. kfree(fds);
  404. return ret;
  405. }
  406. static int rppc_unregister_buffers(struct rppc_instance *rpc,
  407. unsigned long arg)
  408. {
  409. struct rppc_buf_fds data;
  410. int *fds = NULL;
  411. struct rppc_dma_buf **bufs = NULL;
  412. int i = 0, ret = 0;
  413. if (copy_from_user(&data, (char __user *)arg, sizeof(data)))
  414. return -EFAULT;
  415. /* impose a maximum number of buffers for now */
  416. if (data.num > RPPC_MAX_REG_FDS)
  417. return -EINVAL;
  418. fds = kcalloc(data.num, sizeof(*fds), GFP_KERNEL);
  419. if (!fds)
  420. return -ENOMEM;
  421. if (copy_from_user(fds, (char __user *)data.fds,
  422. sizeof(*fds) * data.num)) {
  423. ret = -EFAULT;
  424. goto free_fds;
  425. }
  426. bufs = kcalloc(data.num, sizeof(*bufs), GFP_KERNEL);
  427. if (!bufs) {
  428. ret = -ENOMEM;
  429. goto free_fds;
  430. }
  431. for (i = 0; i < data.num; i++) {
  432. rcu_read_lock();
  433. if (!fcheck(fds[i])) {
  434. rcu_read_unlock();
  435. ret = -EBADF;
  436. goto free_bufs;
  437. }
  438. rcu_read_unlock();
  439. bufs[i] = rppc_find_dmabuf(rpc, fds[i]);
  440. if (IS_ERR_OR_NULL(bufs[i])) {
  441. ret = -EEXIST;
  442. goto free_bufs;
  443. }
  444. }
  445. for (i = 0; i < data.num; i++)
  446. rppc_free_dmabuf(bufs[i]->id, bufs[i], rpc);
  447. free_bufs:
  448. kfree(bufs);
  449. free_fds:
  450. kfree(fds);
  451. return ret;
  452. }
  453. /*
  454. * create a new rpc instance that a user-space client can use to invoke
  455. * remote functions. A new local address would be created and tied with
  456. * this instance for uniquely identifying the messages communicated by
  457. * this instance with the remote side.
  458. *
  459. * The function is blocking if there is no underlying connection manager
  460. * channel, unless the device is opened with non-blocking flags specifically.
  461. */
  462. static int rppc_open(struct inode *inode, struct file *filp)
  463. {
  464. struct rppc_device *rppcdev;
  465. struct rppc_instance *rpc;
  466. struct rpmsg_channel_info chinfo = {};
  467. rppcdev = container_of(inode->i_cdev, struct rppc_device, cdev);
  468. if (!rppcdev->rpdev)
  469. if ((filp->f_flags & O_NONBLOCK) ||
  470. wait_for_completion_interruptible(&rppcdev->comp))
  471. return -EBUSY;
  472. rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
  473. if (!rpc)
  474. return -ENOMEM;
  475. mutex_init(&rpc->lock);
  476. skb_queue_head_init(&rpc->queue);
  477. init_waitqueue_head(&rpc->readq);
  478. INIT_LIST_HEAD(&rpc->fxn_list);
  479. idr_init(&rpc->dma_idr);
  480. rpc->in_transition = 0;
  481. rpc->msg_id = 0;
  482. rpc->state = RPPC_STATE_DISCONNECTED;
  483. rpc->rppcdev = rppcdev;
  484. rpc->dev = get_device(rppcdev->dev);
  485. chinfo.src = RPMSG_ADDR_ANY;
  486. chinfo.dst = RPMSG_ADDR_ANY;
  487. rpc->ept = rpmsg_create_ept(rppcdev->rpdev, rppc_cb, rpc, chinfo);
  488. if (!rpc->ept) {
  489. dev_err(rpc->dev, "create ept failed\n");
  490. put_device(rpc->dev);
  491. kfree(rpc);
  492. return -ENOMEM;
  493. }
  494. filp->private_data = rpc;
  495. mutex_lock(&rppcdev->lock);
  496. list_add(&rpc->list, &rppcdev->instances);
  497. mutex_unlock(&rppcdev->lock);
  498. dev_dbg(rpc->dev, "local addr assigned: 0x%x\n", rpc->ept->addr);
  499. return 0;
  500. }
  501. /*
  502. * release and free all the resources associated with a particular rpc
  503. * instance. This includes the data structures maintaining the current
  504. * outstanding function invocations, and all the buffers registered for
  505. * use with this instance. Send a disconnect message and cleanup the
  506. * local end-point only if the instance is in a normal state, with the
  507. * remote connection manager functional.
  508. */
  509. static int rppc_release(struct inode *inode, struct file *filp)
  510. {
  511. struct rppc_instance *rpc = filp->private_data;
  512. struct rppc_device *rppcdev = rpc->rppcdev;
  513. struct sk_buff *skb = NULL;
  514. dev_dbg(rpc->dev, "releasing Instance %p, in state %d\n", rpc,
  515. rpc->state);
  516. if (rpc->state != RPPC_STATE_STALE) {
  517. if (rpc->ept) {
  518. rppc_disconnect(rpc);
  519. rpmsg_destroy_ept(rpc->ept);
  520. rpc->ept = NULL;
  521. }
  522. }
  523. rppc_delete_fxns(rpc);
  524. while (!skb_queue_empty(&rpc->queue)) {
  525. skb = skb_dequeue(&rpc->queue);
  526. kfree_skb(skb);
  527. }
  528. mutex_lock(&rpc->lock);
  529. idr_for_each(&rpc->dma_idr, rppc_free_dmabuf, rpc);
  530. idr_destroy(&rpc->dma_idr);
  531. mutex_unlock(&rpc->lock);
  532. mutex_lock(&rppcdev->lock);
  533. list_del(&rpc->list);
  534. mutex_unlock(&rppcdev->lock);
  535. dev_dbg(rpc->dev, "instance %p has been deleted!\n", rpc);
  536. if (list_empty(&rppcdev->instances))
  537. dev_dbg(rpc->dev, "all instances have been removed!\n");
  538. put_device(rpc->dev);
  539. kfree(rpc);
  540. return 0;
  541. }
  542. static long rppc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  543. {
  544. struct rppc_instance *rpc = filp->private_data;
  545. struct rppc_create_instance connect;
  546. int ret = 0;
  547. dev_dbg(rpc->dev, "%s: cmd %d, arg 0x%lx\n", __func__, cmd, arg);
  548. if (_IOC_TYPE(cmd) != RPPC_IOC_MAGIC)
  549. return -ENOTTY;
  550. if (_IOC_NR(cmd) > RPPC_IOC_MAXNR)
  551. return -ENOTTY;
  552. switch (cmd) {
  553. case RPPC_IOC_CREATE:
  554. ret = copy_from_user(&connect, (char __user *)arg,
  555. sizeof(connect));
  556. if (ret) {
  557. dev_err(rpc->dev, "%s: %d: copy_from_user fail: %d\n",
  558. __func__, _IOC_NR(cmd), ret);
  559. ret = -EFAULT;
  560. } else {
  561. connect.name[sizeof(connect.name) - 1] = '\0';
  562. ret = rppc_connect(rpc, &connect);
  563. }
  564. break;
  565. case RPPC_IOC_BUFREGISTER:
  566. ret = rppc_register_buffers(rpc, arg);
  567. break;
  568. case RPPC_IOC_BUFUNREGISTER:
  569. ret = rppc_unregister_buffers(rpc, arg);
  570. break;
  571. default:
  572. dev_err(rpc->dev, "unhandled ioctl cmd: %d\n", cmd);
  573. break;
  574. }
  575. return ret;
  576. }
  577. static ssize_t rppc_read(struct file *filp, char __user *buf, size_t len,
  578. loff_t *offp)
  579. {
  580. struct rppc_instance *rpc = filp->private_data;
  581. struct rppc_packet *packet = NULL;
  582. struct rppc_param_data *parameters = NULL;
  583. struct rppc_function *function = NULL;
  584. struct rppc_function_return returned;
  585. struct sk_buff *skb = NULL;
  586. int ret = 0;
  587. int use = sizeof(returned);
  588. DEFINE_WAIT(wait);
  589. if (mutex_lock_interruptible(&rpc->lock))
  590. return -ERESTARTSYS;
  591. /* instance is invalid */
  592. if (rpc->state == RPPC_STATE_STALE) {
  593. mutex_unlock(&rpc->lock);
  594. return -ENXIO;
  595. }
  596. /* not yet connected to the remote side */
  597. if (rpc->state == RPPC_STATE_DISCONNECTED) {
  598. mutex_unlock(&rpc->lock);
  599. return -ENOTCONN;
  600. }
  601. if (len > use) {
  602. mutex_unlock(&rpc->lock);
  603. return -EOVERFLOW;
  604. }
  605. if (len < use) {
  606. mutex_unlock(&rpc->lock);
  607. return -EINVAL;
  608. }
  609. /* TODO: Use the much simpler wait_event_interruptible API */
  610. while (skb_queue_empty(&rpc->queue)) {
  611. mutex_unlock(&rpc->lock);
  612. /* non-blocking requested ? return now */
  613. if (filp->f_flags & O_NONBLOCK)
  614. return -EAGAIN;
  615. prepare_to_wait_exclusive(&rpc->readq, &wait,
  616. TASK_INTERRUPTIBLE);
  617. if (skb_queue_empty(&rpc->queue) &&
  618. rpc->state != RPPC_STATE_STALE)
  619. schedule();
  620. finish_wait(&rpc->readq, &wait);
  621. if (signal_pending(current))
  622. return -ERESTARTSYS;
  623. ret = mutex_lock_interruptible(&rpc->lock);
  624. if (ret < 0)
  625. return -ERESTARTSYS;
  626. if (rpc->state == RPPC_STATE_STALE) {
  627. mutex_unlock(&rpc->lock);
  628. return -ENXIO;
  629. }
  630. /* make sure state is sane while we waited */
  631. if (rpc->state != RPPC_STATE_CONNECTED) {
  632. mutex_unlock(&rpc->lock);
  633. ret = -EIO;
  634. goto out;
  635. }
  636. }
  637. skb = skb_dequeue(&rpc->queue);
  638. if (WARN_ON(!skb)) {
  639. mutex_unlock(&rpc->lock);
  640. ret = -EIO;
  641. goto out;
  642. }
  643. mutex_unlock(&rpc->lock);
  644. packet = (struct rppc_packet *)skb->data;
  645. parameters = (struct rppc_param_data *)packet->data;
  646. /*
  647. * pull the function memory from the list and untranslate
  648. * the remote device address pointers in the packet back
  649. * to MPU pointers.
  650. */
  651. function = rppc_find_fxn(rpc, packet->msg_id);
  652. if (function && function->num_translations > 0) {
  653. ret = rppc_xlate_buffers(rpc, function, RPPC_RPA_TO_UVA);
  654. if (ret < 0) {
  655. dev_err(rpc->dev, "failed to translate back pointers from remote core!\n");
  656. goto failure;
  657. }
  658. }
  659. returned.fxn_id = RPPC_FXN_MASK(packet->fxn_id);
  660. returned.status = packet->result;
  661. if (copy_to_user(buf, &returned, use)) {
  662. dev_err(rpc->dev, "%s: copy_to_user fail\n", __func__);
  663. ret = -EFAULT;
  664. } else {
  665. ret = use;
  666. }
  667. failure:
  668. kfree(function);
  669. kfree_skb(skb);
  670. out:
  671. return ret;
  672. }
  673. static ssize_t rppc_write(struct file *filp, const char __user *ubuf,
  674. size_t len, loff_t *offp)
  675. {
  676. struct rppc_instance *rpc = filp->private_data;
  677. struct rppc_device *rppcdev = rpc->rppcdev;
  678. struct device *dev = rpc->dev;
  679. struct rppc_msg_header *hdr = NULL;
  680. struct rppc_function *function = NULL;
  681. struct rppc_packet *packet = NULL;
  682. struct rppc_param_data *parameters = NULL;
  683. char kbuf[512];
  684. int use = 0, ret = 0, param = 0;
  685. u32 sig_idx = 0;
  686. u32 sig_prm = 0;
  687. static u32 rppc_atomic_size[RPPC_PARAM_ATOMIC_MAX] = {
  688. 0, /* RPPC_PARAM_VOID */
  689. 1, /* RPPC_PARAM_S08 */
  690. 1, /* RPPC_PARAM_U08 */
  691. 2, /* RPPC_PARAM_S16 */
  692. 2, /* RPPC_PARAM_U16 */
  693. 4, /* RPPC_PARAM_S32 */
  694. 4, /* RPPC_PARAM_U32 */
  695. 8, /* RPPC_PARAM_S64 */
  696. 8 /* RPPC_PARAM_U64 */
  697. };
  698. if (len < sizeof(*function)) {
  699. ret = -ENOTSUPP;
  700. goto failure;
  701. }
  702. if (len > (sizeof(*function) + RPPC_MAX_TRANSLATIONS *
  703. sizeof(struct rppc_param_translation))) {
  704. ret = -ENOTSUPP;
  705. goto failure;
  706. }
  707. if (rpc->state != RPPC_STATE_CONNECTED) {
  708. ret = -ENOTCONN;
  709. goto failure;
  710. }
  711. function = kzalloc(len, GFP_KERNEL);
  712. if (!function) {
  713. ret = -ENOMEM;
  714. goto failure;
  715. }
  716. if (copy_from_user(function, ubuf, len)) {
  717. ret = -EMSGSIZE;
  718. goto failure;
  719. }
  720. /* increment the message id and wrap if needed */
  721. rpc->msg_id = (rpc->msg_id + 1) & 0xFFFF;
  722. memset(kbuf, 0, sizeof(kbuf));
  723. sig_idx = function->fxn_id + 1;
  724. hdr = (struct rppc_msg_header *)kbuf;
  725. hdr->msg_type = RPPC_MSGTYPE_FUNCTION_CALL;
  726. hdr->msg_len = sizeof(*packet);
  727. packet = RPPC_PAYLOAD(kbuf, rppc_packet);
  728. packet->desc = RPPC_DESC_EXEC_SYNC;
  729. packet->msg_id = rpc->msg_id;
  730. packet->flags = (RPPC_JOBID_DISCRETE << 16) | RPPC_POOLID_DEFAULT;
  731. packet->fxn_id = RPPC_SET_FXN_IDX(function->fxn_id);
  732. packet->result = 0;
  733. packet->data_size = sizeof(*parameters) * function->num_params;
  734. /* check the signatures against what were published */
  735. if (RPPC_SIG_NUM_PARAM(rppcdev->signatures[sig_idx]) !=
  736. function->num_params) {
  737. dev_err(dev, "number of parameters mismatch! params = %u expected = %u\n",
  738. function->num_params,
  739. RPPC_SIG_NUM_PARAM(rppcdev->signatures[sig_idx]));
  740. ret = -EINVAL;
  741. goto failure;
  742. }
  743. /*
  744. * compute the parameter pointer changes last since this will cause the
  745. * cache operations
  746. */
  747. parameters = (struct rppc_param_data *)packet->data;
  748. for (param = 0; param < function->num_params; param++) {
  749. u32 param_type;
  750. sig_prm = param + 1;
  751. param_type = rppcdev->signatures[sig_idx].params[sig_prm].type;
  752. /*
  753. * check to make sure the parameter description matches the
  754. * signature published from the other side.
  755. */
  756. if (function->params[param].type == RPPC_PARAM_TYPE_PTR &&
  757. !RPPC_IS_PTR(param_type)) {
  758. dev_err(dev, "parameter %u Pointer Type Mismatch sig type:%x func %u\n",
  759. param, param_type, sig_idx);
  760. ret = -EINVAL;
  761. goto failure;
  762. } else if (param > 0 && function->params[param].type ==
  763. RPPC_PARAM_TYPE_ATOMIC) {
  764. if (!RPPC_IS_ATOMIC(param_type)) {
  765. dev_err(dev, "parameter Atomic Type Mismatch\n");
  766. ret = -EINVAL;
  767. goto failure;
  768. } else {
  769. if (rppc_atomic_size[param_type] !=
  770. function->params[param].size) {
  771. dev_err(dev, "size mismatch! u:%u sig:%u\n",
  772. function->params[param].size,
  773. rppc_atomic_size[param_type]);
  774. ret = -EINVAL;
  775. goto failure;
  776. }
  777. }
  778. }
  779. parameters[param].size = function->params[param].size;
  780. /* check the type and lookup if it's a pointer */
  781. if (function->params[param].type == RPPC_PARAM_TYPE_PTR) {
  782. /*
  783. * internally the buffer translations takes care of the
  784. * offsets.
  785. */
  786. int fd = function->params[param].fd;
  787. parameters[param].data = (size_t)rppc_buffer_lookup(rpc,
  788. (virt_addr_t)function->params[param].data,
  789. (virt_addr_t)function->params[param].base, fd);
  790. } else if (function->params[param].type ==
  791. RPPC_PARAM_TYPE_ATOMIC) {
  792. parameters[param].data = function->params[param].data;
  793. } else {
  794. ret = -ENOTSUPP;
  795. goto failure;
  796. }
  797. }
  798. /* compute the size of the rpmsg packet */
  799. use = sizeof(*hdr) + hdr->msg_len + packet->data_size;
  800. /* failed to provide the translation data */
  801. if (function->num_translations > 0 &&
  802. len < (sizeof(*function) + (function->num_translations *
  803. sizeof(struct rppc_param_translation)))) {
  804. ret = -EINVAL;
  805. goto failure;
  806. }
  807. /*
  808. * if there are pointers to translate for the user, do so now.
  809. * alter our copy of function and the user's parameters so that
  810. * the proper pointers can be sent to remote cores
  811. */
  812. if (function->num_translations > 0) {
  813. ret = rppc_xlate_buffers(rpc, function, RPPC_UVA_TO_RPA);
  814. if (ret < 0) {
  815. dev_err(dev, "failed to translate all pointers for remote core!\n");
  816. goto failure;
  817. }
  818. }
  819. ret = rppc_add_fxn(rpc, function, rpc->msg_id);
  820. if (ret < 0) {
  821. rppc_xlate_buffers(rpc, function, RPPC_RPA_TO_UVA);
  822. goto failure;
  823. }
  824. rppc_print_msg(rpc, "TX:", kbuf);
  825. ret = rpmsg_send_offchannel(rppcdev->rpdev->ept, rpc->ept->addr,
  826. rpc->dst, kbuf, use);
  827. if (ret) {
  828. dev_err(dev, "rpmsg_send failed: %d\n", ret);
  829. rppc_find_fxn(rpc, rpc->msg_id);
  830. rppc_xlate_buffers(rpc, function, RPPC_RPA_TO_UVA);
  831. goto failure;
  832. }
  833. dev_dbg(dev, "==> sent msg to remote endpoint %u\n", rpc->dst);
  834. failure:
  835. if (ret >= 0)
  836. ret = len;
  837. else
  838. kfree(function);
  839. return ret;
  840. }
  841. static __poll_t rppc_poll(struct file *filp, struct poll_table_struct *wait)
  842. {
  843. struct rppc_instance *rpc = filp->private_data;
  844. __poll_t mask = 0;
  845. poll_wait(filp, &rpc->readq, wait);
  846. if (rpc->state == RPPC_STATE_STALE) {
  847. mask = EPOLLERR;
  848. goto out;
  849. }
  850. /* if the queue is not empty set the poll bit correctly */
  851. if (!skb_queue_empty(&rpc->queue))
  852. mask |= (EPOLLIN | EPOLLRDNORM);
  853. /* TODO: writes are deemed to be successful always, fix this later */
  854. if (true)
  855. mask |= EPOLLOUT | EPOLLWRNORM;
  856. out:
  857. return mask;
  858. }
  859. static const struct file_operations rppc_fops = {
  860. .owner = THIS_MODULE,
  861. .open = rppc_open,
  862. .release = rppc_release,
  863. .unlocked_ioctl = rppc_ioctl,
  864. .read = rppc_read,
  865. .write = rppc_write,
  866. .poll = rppc_poll,
  867. };
  868. /*
  869. * send a function query message, the sysfs entry will be created
  870. * during the processing of the response message
  871. */
  872. static int rppc_query_function(struct rpmsg_device *rpdev)
  873. {
  874. int ret = 0;
  875. u32 len = 0;
  876. char kbuf[512];
  877. struct rppc_device *rppcdev = dev_get_drvdata(&rpdev->dev);
  878. struct rppc_msg_header *hdr = (struct rppc_msg_header *)&kbuf[0];
  879. struct rppc_query_function *fxn_info =
  880. (struct rppc_query_function *)hdr->msg_data;
  881. if (rppcdev->cur_func >= rppcdev->num_funcs)
  882. return -EINVAL;
  883. hdr->msg_type = RPPC_MSGTYPE_FUNCTION_QUERY;
  884. hdr->msg_len = sizeof(*fxn_info);
  885. len = sizeof(*hdr) + hdr->msg_len;
  886. fxn_info->info_type = RPPC_INFOTYPE_FUNC_SIGNATURE;
  887. fxn_info->fxn_id = rppcdev->cur_func++;
  888. dev_dbg(&rpdev->dev, "sending function query type %u for function %u\n",
  889. fxn_info->info_type, fxn_info->fxn_id);
  890. ret = rpmsg_send(rpdev->ept, (char *)kbuf, len);
  891. if (ret) {
  892. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  893. return ret;
  894. }
  895. return 0;
  896. }
  897. static void
  898. rppc_handle_devinfo_resp(struct rpmsg_device *rpdev, char *data, int len)
  899. {
  900. struct rppc_device *rppcdev = dev_get_drvdata(&rpdev->dev);
  901. struct rppc_device_info *info;
  902. u32 exp_len = sizeof(*info) + sizeof(struct rppc_msg_header);
  903. if (len != exp_len) {
  904. dev_err(&rpdev->dev, "invalid message length %d (expected %d bytes)",
  905. len, exp_len);
  906. return;
  907. }
  908. info = RPPC_PAYLOAD(data, rppc_device_info);
  909. if (info->num_funcs > RPPC_MAX_NUM_FUNCS) {
  910. rppcdev->num_funcs = 0;
  911. dev_err(&rpdev->dev, "number of functions (%d) exceeds the limit supported(%d)\n",
  912. info->num_funcs, RPPC_MAX_NUM_FUNCS);
  913. return;
  914. }
  915. rppcdev->num_funcs = info->num_funcs;
  916. rppcdev->signatures = kcalloc(rppcdev->num_funcs,
  917. sizeof(struct rppc_func_signature),
  918. GFP_KERNEL);
  919. if (!rppcdev->signatures)
  920. return;
  921. dev_info(&rpdev->dev, "published functions = %u\n", info->num_funcs);
  922. /* send the function query for first function */
  923. if (rppc_query_function(rpdev) == -EINVAL)
  924. dev_err(&rpdev->dev, "failed to get a reasonable number of functions!\n");
  925. }
  926. static void
  927. rppc_handle_fxninfo_resp(struct rpmsg_device *rpdev, char *data, int len)
  928. {
  929. struct rppc_device *rppcdev = dev_get_drvdata(&rpdev->dev);
  930. struct rppc_query_function *fxn_info;
  931. struct rppc_func_signature *signature;
  932. u32 exp_len = sizeof(*fxn_info) + sizeof(struct rppc_msg_header);
  933. int i;
  934. if (len != exp_len) {
  935. dev_err(&rpdev->dev, "invalid message length %d (expected %d bytes)",
  936. len, exp_len);
  937. return;
  938. }
  939. fxn_info = RPPC_PAYLOAD(data, rppc_query_function);
  940. dev_dbg(&rpdev->dev, "response for function query of type %u\n",
  941. fxn_info->info_type);
  942. switch (fxn_info->info_type) {
  943. case RPPC_INFOTYPE_FUNC_SIGNATURE:
  944. if (fxn_info->fxn_id >= rppcdev->num_funcs) {
  945. dev_err(&rpdev->dev, "function(%d) is out of range!\n",
  946. fxn_info->fxn_id);
  947. break;
  948. }
  949. memcpy(&rppcdev->signatures[fxn_info->fxn_id],
  950. &fxn_info->info.signature, sizeof(*signature));
  951. /* TODO: delete these debug prints later */
  952. dev_dbg(&rpdev->dev, "received info for func(%d); name = %s #params = %u\n",
  953. fxn_info->fxn_id, fxn_info->info.signature.name,
  954. fxn_info->info.signature.num_param);
  955. signature = &rppcdev->signatures[fxn_info->fxn_id];
  956. for (i = 0; i < signature->num_param; i++) {
  957. dev_dbg(&rpdev->dev, "param[%u] type = %x dir = %u\n",
  958. i, signature->params[i].type,
  959. signature->params[i].direction);
  960. }
  961. /* query again until we've hit our limit */
  962. if (rppc_query_function(rpdev) == -EINVAL) {
  963. dev_dbg(&rpdev->dev, "reached end of function list!\n");
  964. rppc_create_sysfs(rppcdev);
  965. }
  966. break;
  967. default:
  968. dev_err(&rpdev->dev, "unrecognized fxn query response %u\n",
  969. fxn_info->info_type);
  970. break;
  971. }
  972. }
  973. static int rppc_driver_cb(struct rpmsg_device *rpdev, void *data, int len,
  974. void *priv, u32 src)
  975. {
  976. struct rppc_msg_header *hdr = data;
  977. char *buf = (char *)data;
  978. dev_dbg(&rpdev->dev, "<== incoming drv msg src %d len %d msg_type %d msg_len %d\n",
  979. src, len, hdr->msg_type, hdr->msg_len);
  980. if (len <= sizeof(*hdr)) {
  981. dev_err(&rpdev->dev, "message truncated\n");
  982. return -EINVAL;
  983. }
  984. switch (hdr->msg_type) {
  985. case RPPC_MSGTYPE_DEVINFO_RESP:
  986. rppc_handle_devinfo_resp(rpdev, buf, len);
  987. break;
  988. case RPPC_MSGTYPE_FUNCTION_INFO:
  989. rppc_handle_fxninfo_resp(rpdev, buf, len);
  990. break;
  991. default:
  992. dev_err(&rpdev->dev, "unrecognized message type %u\n",
  993. hdr->msg_type);
  994. break;
  995. }
  996. return 0;
  997. }
  998. static int find_rpccdev_by_name(int id, void *p, void *data)
  999. {
  1000. struct rppc_device *rppcdev = p;
  1001. return strcmp(rppcdev->desc, data) ? 0 : (int)p;
  1002. }
  1003. /*
  1004. * send a device info query message, the device will be created
  1005. * during the processing of the response message
  1006. */
  1007. static int rppc_device_create(struct rpmsg_device *rpdev)
  1008. {
  1009. int ret;
  1010. u32 len;
  1011. char kbuf[512];
  1012. struct rppc_msg_header *hdr = (struct rppc_msg_header *)&kbuf[0];
  1013. hdr->msg_type = RPPC_MSGTYPE_DEVINFO_REQ;
  1014. hdr->msg_len = 0;
  1015. len = sizeof(*hdr);
  1016. ret = rpmsg_send(rpdev->ept, (char *)kbuf, len);
  1017. if (ret) {
  1018. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  1019. return ret;
  1020. }
  1021. return 0;
  1022. }
  1023. static int rppc_probe(struct rpmsg_device *rpdev)
  1024. {
  1025. int ret, minor;
  1026. int major = MAJOR(rppc_dev);
  1027. struct rppc_device *rppcdev = NULL;
  1028. dev_t dev;
  1029. char namedesc[RPMSG_NAME_SIZE];
  1030. dev_info(&rpdev->dev, "probing service %s with src %u dst %u\n",
  1031. rpdev->desc, rpdev->src, rpdev->dst);
  1032. mutex_lock(&rppc_devices_lock);
  1033. snprintf(namedesc, sizeof(namedesc), "%s", rpdev->desc);
  1034. rppcdev = (struct rppc_device *)idr_for_each(&rppc_devices,
  1035. find_rpccdev_by_name, namedesc);
  1036. if (rppcdev) {
  1037. rppcdev->rpdev = rpdev;
  1038. dev_set_drvdata(&rpdev->dev, rppcdev);
  1039. goto serv_up;
  1040. }
  1041. rppcdev = kzalloc(sizeof(*rppcdev), GFP_KERNEL);
  1042. if (!rppcdev) {
  1043. ret = -ENOMEM;
  1044. goto exit;
  1045. }
  1046. minor = idr_alloc(&rppc_devices, rppcdev, 0, 0, GFP_KERNEL);
  1047. if (minor < 0) {
  1048. ret = minor;
  1049. dev_err(&rpdev->dev, "failed to get a minor number: %d\n", ret);
  1050. goto free_rppcdev;
  1051. }
  1052. INIT_LIST_HEAD(&rppcdev->instances);
  1053. mutex_init(&rppcdev->lock);
  1054. init_completion(&rppcdev->comp);
  1055. rppcdev->minor = minor;
  1056. rppcdev->rpdev = rpdev;
  1057. strncpy(rppcdev->desc, namedesc, RPMSG_NAME_SIZE);
  1058. dev_set_drvdata(&rpdev->dev, rppcdev);
  1059. cdev_init(&rppcdev->cdev, &rppc_fops);
  1060. rppcdev->cdev.owner = THIS_MODULE;
  1061. dev = MKDEV(major, minor);
  1062. ret = cdev_add(&rppcdev->cdev, dev, 1);
  1063. if (ret) {
  1064. dev_err(&rpdev->dev, "cdev_add failed: %d\n", ret);
  1065. goto free_id;
  1066. }
  1067. serv_up:
  1068. rppcdev->dev = device_create(rppc_class, &rpdev->dev,
  1069. MKDEV(major, rppcdev->minor), NULL,
  1070. namedesc);
  1071. if (IS_ERR(rppcdev->dev)) {
  1072. int ret = PTR_ERR(rppcdev->dev);
  1073. dev_err(&rpdev->dev, "device_create failed: %d\n", ret);
  1074. goto free_cdev;
  1075. }
  1076. dev_set_drvdata(rppcdev->dev, rppcdev);
  1077. ret = rppc_device_create(rpdev);
  1078. if (ret) {
  1079. dev_err(&rpdev->dev, "failed to query channel info: %d\n", ret);
  1080. dev = MKDEV(MAJOR(rppc_dev), rppcdev->minor);
  1081. goto free_dev;
  1082. }
  1083. complete_all(&rppcdev->comp);
  1084. dev_dbg(&rpdev->dev, "new RPPC connection srv channel: %u -> %u!\n",
  1085. rpdev->src, rpdev->dst);
  1086. mutex_unlock(&rppc_devices_lock);
  1087. return 0;
  1088. free_dev:
  1089. device_destroy(rppc_class, dev);
  1090. free_cdev:
  1091. cdev_del(&rppcdev->cdev);
  1092. free_id:
  1093. idr_remove(&rppc_devices, rppcdev->minor);
  1094. free_rppcdev:
  1095. kfree(rppcdev);
  1096. exit:
  1097. mutex_unlock(&rppc_devices_lock);
  1098. return ret;
  1099. }
  1100. static void rppc_remove(struct rpmsg_device *rpdev)
  1101. {
  1102. struct rppc_device *rppcdev = dev_get_drvdata(&rpdev->dev);
  1103. struct rppc_instance *rpc = NULL;
  1104. int major = MAJOR(rppc_dev);
  1105. dev_dbg(&rpdev->dev, "removing rpmsg-rpc device %u.%u\n",
  1106. major, rppcdev->minor);
  1107. mutex_lock(&rppc_devices_lock);
  1108. rppc_remove_sysfs(rppcdev);
  1109. rppcdev->cur_func = 0;
  1110. kfree(rppcdev->signatures);
  1111. /* if there are no instances in the list, just teardown */
  1112. if (list_empty(&rppcdev->instances)) {
  1113. dev_dbg(&rpdev->dev, "no instances, removing device!\n");
  1114. device_destroy(rppc_class, MKDEV(major, rppcdev->minor));
  1115. cdev_del(&rppcdev->cdev);
  1116. idr_remove(&rppc_devices, rppcdev->minor);
  1117. kfree(rppcdev);
  1118. mutex_unlock(&rppc_devices_lock);
  1119. return;
  1120. }
  1121. /*
  1122. * if there are rpc instances that means that this is a recovery
  1123. * operation. Don't clean the rppcdev, and retain it for reuse.
  1124. * mark each instance as invalid, and complete any on-going transactions
  1125. */
  1126. init_completion(&rppcdev->comp);
  1127. mutex_lock(&rppcdev->lock);
  1128. list_for_each_entry(rpc, &rppcdev->instances, list) {
  1129. dev_dbg(&rpdev->dev, "instance %p in state %d\n",
  1130. rpc, rpc->state);
  1131. if (rpc->state == RPPC_STATE_CONNECTED && rpc->in_transition)
  1132. complete_all(&rpc->reply_arrived);
  1133. rpc->state = RPPC_STATE_STALE;
  1134. if (rpc->ept) {
  1135. rpmsg_destroy_ept(rpc->ept);
  1136. rpc->ept = NULL;
  1137. }
  1138. wake_up_interruptible(&rpc->readq);
  1139. }
  1140. device_destroy(rppc_class, MKDEV(major, rppcdev->minor));
  1141. rppcdev->dev = NULL;
  1142. rppcdev->rpdev = NULL;
  1143. mutex_unlock(&rppcdev->lock);
  1144. mutex_unlock(&rppc_devices_lock);
  1145. dev_dbg(&rpdev->dev, "removed rpmsg rpmsg-rpc service %s\n",
  1146. rpdev->desc);
  1147. }
  1148. static struct rpmsg_device_id rppc_id_table[] = {
  1149. {.name = "rpmsg-rpc"},
  1150. {},
  1151. };
  1152. static struct rpmsg_driver rppc_driver = {
  1153. .drv.name = KBUILD_MODNAME,
  1154. .id_table = rppc_id_table,
  1155. .probe = rppc_probe,
  1156. .remove = rppc_remove,
  1157. .callback = rppc_driver_cb,
  1158. };
  1159. static int __init rppc_init(void)
  1160. {
  1161. int ret;
  1162. ret = alloc_chrdev_region(&rppc_dev, 0, RPPC_MAX_DEVICES,
  1163. KBUILD_MODNAME);
  1164. if (ret) {
  1165. pr_err("alloc_chrdev_region failed: %d\n", ret);
  1166. goto out;
  1167. }
  1168. rppc_class = class_create(THIS_MODULE, KBUILD_MODNAME);
  1169. if (IS_ERR(rppc_class)) {
  1170. ret = PTR_ERR(rppc_class);
  1171. pr_err("class_create failed: %d\n", ret);
  1172. goto unreg_region;
  1173. }
  1174. ret = register_rpmsg_driver(&rppc_driver);
  1175. if (ret) {
  1176. pr_err("register_rpmsg_driver failed: %d\n", ret);
  1177. goto destroy_class;
  1178. }
  1179. return 0;
  1180. destroy_class:
  1181. class_destroy(rppc_class);
  1182. unreg_region:
  1183. unregister_chrdev_region(rppc_dev, RPPC_MAX_DEVICES);
  1184. out:
  1185. return ret;
  1186. }
  1187. static void __exit rppc_exit(void)
  1188. {
  1189. unregister_rpmsg_driver(&rppc_driver);
  1190. class_destroy(rppc_class);
  1191. unregister_chrdev_region(rppc_dev, RPPC_MAX_DEVICES);
  1192. }
  1193. module_init(rppc_init);
  1194. module_exit(rppc_exit);
  1195. MODULE_DEVICE_TABLE(rpmsg, rppc_id_table);
  1196. MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");
  1197. MODULE_AUTHOR("Erik Rainey <erik.rainey@ti.com>");
  1198. MODULE_DESCRIPTION("Remote Processor Procedure Call Driver");
  1199. MODULE_ALIAS("rpmsg:rpmsg-rpc");
  1200. MODULE_LICENSE("GPL v2");