qcom_glink_native.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*
  2. * Copyright (c) 2016-2017, Linaro Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/idr.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/list.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/rpmsg.h>
  25. #include <linux/sizes.h>
  26. #include <linux/slab.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/mailbox_client.h>
  29. #include "rpmsg_internal.h"
  30. #include "qcom_glink_native.h"
  31. #define GLINK_NAME_SIZE 32
  32. #define GLINK_VERSION_1 1
  33. #define RPM_GLINK_CID_MIN 1
  34. #define RPM_GLINK_CID_MAX 65536
  35. struct glink_msg {
  36. __le16 cmd;
  37. __le16 param1;
  38. __le32 param2;
  39. u8 data[];
  40. } __packed;
  41. /**
  42. * struct glink_defer_cmd - deferred incoming control message
  43. * @node: list node
  44. * @msg: message header
  45. * data: payload of the message
  46. *
  47. * Copy of a received control message, to be added to @rx_queue and processed
  48. * by @rx_work of @qcom_glink.
  49. */
  50. struct glink_defer_cmd {
  51. struct list_head node;
  52. struct glink_msg msg;
  53. u8 data[];
  54. };
  55. /**
  56. * struct glink_core_rx_intent - RX intent
  57. * RX intent
  58. *
  59. * data: pointer to the data (may be NULL for zero-copy)
  60. * id: remote or local intent ID
  61. * size: size of the original intent (do not modify)
  62. * reuse: To mark if the intent can be reused after first use
  63. * in_use: To mark if intent is already in use for the channel
  64. * offset: next write offset (initially 0)
  65. */
  66. struct glink_core_rx_intent {
  67. void *data;
  68. u32 id;
  69. size_t size;
  70. bool reuse;
  71. bool in_use;
  72. u32 offset;
  73. struct list_head node;
  74. };
  75. /**
  76. * struct qcom_glink - driver context, relates to one remote subsystem
  77. * @dev: reference to the associated struct device
  78. * @mbox_client: mailbox client
  79. * @mbox_chan: mailbox channel
  80. * @rx_pipe: pipe object for receive FIFO
  81. * @tx_pipe: pipe object for transmit FIFO
  82. * @irq: IRQ for signaling incoming events
  83. * @rx_work: worker for handling received control messages
  84. * @rx_lock: protects the @rx_queue
  85. * @rx_queue: queue of received control messages to be processed in @rx_work
  86. * @tx_lock: synchronizes operations on the tx fifo
  87. * @idr_lock: synchronizes @lcids and @rcids modifications
  88. * @lcids: idr of all channels with a known local channel id
  89. * @rcids: idr of all channels with a known remote channel id
  90. */
  91. struct qcom_glink {
  92. struct device *dev;
  93. struct mbox_client mbox_client;
  94. struct mbox_chan *mbox_chan;
  95. struct qcom_glink_pipe *rx_pipe;
  96. struct qcom_glink_pipe *tx_pipe;
  97. int irq;
  98. struct work_struct rx_work;
  99. spinlock_t rx_lock;
  100. struct list_head rx_queue;
  101. struct mutex tx_lock;
  102. spinlock_t idr_lock;
  103. struct idr lcids;
  104. struct idr rcids;
  105. unsigned long features;
  106. bool intentless;
  107. };
  108. enum {
  109. GLINK_STATE_CLOSED,
  110. GLINK_STATE_OPENING,
  111. GLINK_STATE_OPEN,
  112. GLINK_STATE_CLOSING,
  113. };
  114. /**
  115. * struct glink_channel - internal representation of a channel
  116. * @rpdev: rpdev reference, only used for primary endpoints
  117. * @ept: rpmsg endpoint this channel is associated with
  118. * @glink: qcom_glink context handle
  119. * @refcount: refcount for the channel object
  120. * @recv_lock: guard for @ept.cb
  121. * @name: unique channel name/identifier
  122. * @lcid: channel id, in local space
  123. * @rcid: channel id, in remote space
  124. * @intent_lock: lock for protection of @liids, @riids
  125. * @liids: idr of all local intents
  126. * @riids: idr of all remote intents
  127. * @intent_work: worker responsible for transmitting rx_done packets
  128. * @done_intents: list of intents that needs to be announced rx_done
  129. * @buf: receive buffer, for gathering fragments
  130. * @buf_offset: write offset in @buf
  131. * @buf_size: size of current @buf
  132. * @open_ack: completed once remote has acked the open-request
  133. * @open_req: completed once open-request has been received
  134. * @intent_req_lock: Synchronises multiple intent requests
  135. * @intent_req_result: Result of intent request
  136. * @intent_req_comp: Completion for intent_req signalling
  137. */
  138. struct glink_channel {
  139. struct rpmsg_endpoint ept;
  140. struct rpmsg_device *rpdev;
  141. struct qcom_glink *glink;
  142. struct kref refcount;
  143. spinlock_t recv_lock;
  144. char *name;
  145. unsigned int lcid;
  146. unsigned int rcid;
  147. spinlock_t intent_lock;
  148. struct idr liids;
  149. struct idr riids;
  150. struct work_struct intent_work;
  151. struct list_head done_intents;
  152. struct glink_core_rx_intent *buf;
  153. int buf_offset;
  154. int buf_size;
  155. struct completion open_ack;
  156. struct completion open_req;
  157. struct mutex intent_req_lock;
  158. bool intent_req_result;
  159. struct completion intent_req_comp;
  160. };
  161. #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
  162. static const struct rpmsg_endpoint_ops glink_endpoint_ops;
  163. #define RPM_CMD_VERSION 0
  164. #define RPM_CMD_VERSION_ACK 1
  165. #define RPM_CMD_OPEN 2
  166. #define RPM_CMD_CLOSE 3
  167. #define RPM_CMD_OPEN_ACK 4
  168. #define RPM_CMD_INTENT 5
  169. #define RPM_CMD_RX_DONE 6
  170. #define RPM_CMD_RX_INTENT_REQ 7
  171. #define RPM_CMD_RX_INTENT_REQ_ACK 8
  172. #define RPM_CMD_TX_DATA 9
  173. #define RPM_CMD_CLOSE_ACK 11
  174. #define RPM_CMD_TX_DATA_CONT 12
  175. #define RPM_CMD_READ_NOTIF 13
  176. #define RPM_CMD_RX_DONE_W_REUSE 14
  177. #define GLINK_FEATURE_INTENTLESS BIT(1)
  178. static void qcom_glink_rx_done_work(struct work_struct *work);
  179. static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
  180. const char *name)
  181. {
  182. struct glink_channel *channel;
  183. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  184. if (!channel)
  185. return ERR_PTR(-ENOMEM);
  186. /* Setup glink internal glink_channel data */
  187. spin_lock_init(&channel->recv_lock);
  188. spin_lock_init(&channel->intent_lock);
  189. channel->glink = glink;
  190. channel->name = kstrdup(name, GFP_KERNEL);
  191. init_completion(&channel->open_req);
  192. init_completion(&channel->open_ack);
  193. INIT_LIST_HEAD(&channel->done_intents);
  194. INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
  195. idr_init(&channel->liids);
  196. idr_init(&channel->riids);
  197. kref_init(&channel->refcount);
  198. return channel;
  199. }
  200. static void qcom_glink_channel_release(struct kref *ref)
  201. {
  202. struct glink_channel *channel = container_of(ref, struct glink_channel,
  203. refcount);
  204. unsigned long flags;
  205. spin_lock_irqsave(&channel->intent_lock, flags);
  206. idr_destroy(&channel->liids);
  207. idr_destroy(&channel->riids);
  208. spin_unlock_irqrestore(&channel->intent_lock, flags);
  209. kfree(channel->name);
  210. kfree(channel);
  211. }
  212. static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
  213. {
  214. return glink->rx_pipe->avail(glink->rx_pipe);
  215. }
  216. static void qcom_glink_rx_peak(struct qcom_glink *glink,
  217. void *data, unsigned int offset, size_t count)
  218. {
  219. glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
  220. }
  221. static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
  222. {
  223. glink->rx_pipe->advance(glink->rx_pipe, count);
  224. }
  225. static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
  226. {
  227. return glink->tx_pipe->avail(glink->tx_pipe);
  228. }
  229. static void qcom_glink_tx_write(struct qcom_glink *glink,
  230. const void *hdr, size_t hlen,
  231. const void *data, size_t dlen)
  232. {
  233. glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
  234. }
  235. static int qcom_glink_tx(struct qcom_glink *glink,
  236. const void *hdr, size_t hlen,
  237. const void *data, size_t dlen, bool wait)
  238. {
  239. unsigned int tlen = hlen + dlen;
  240. int ret;
  241. /* Reject packets that are too big */
  242. if (tlen >= glink->tx_pipe->length)
  243. return -EINVAL;
  244. ret = mutex_lock_interruptible(&glink->tx_lock);
  245. if (ret)
  246. return ret;
  247. while (qcom_glink_tx_avail(glink) < tlen) {
  248. if (!wait) {
  249. ret = -EAGAIN;
  250. goto out;
  251. }
  252. usleep_range(10000, 15000);
  253. }
  254. qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
  255. mbox_send_message(glink->mbox_chan, NULL);
  256. mbox_client_txdone(glink->mbox_chan, 0);
  257. out:
  258. mutex_unlock(&glink->tx_lock);
  259. return ret;
  260. }
  261. static int qcom_glink_send_version(struct qcom_glink *glink)
  262. {
  263. struct glink_msg msg;
  264. msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
  265. msg.param1 = cpu_to_le16(GLINK_VERSION_1);
  266. msg.param2 = cpu_to_le32(glink->features);
  267. return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
  268. }
  269. static void qcom_glink_send_version_ack(struct qcom_glink *glink)
  270. {
  271. struct glink_msg msg;
  272. msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
  273. msg.param1 = cpu_to_le16(GLINK_VERSION_1);
  274. msg.param2 = cpu_to_le32(glink->features);
  275. qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
  276. }
  277. static void qcom_glink_send_open_ack(struct qcom_glink *glink,
  278. struct glink_channel *channel)
  279. {
  280. struct glink_msg msg;
  281. msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
  282. msg.param1 = cpu_to_le16(channel->rcid);
  283. msg.param2 = cpu_to_le32(0);
  284. qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
  285. }
  286. static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
  287. unsigned int cid, bool granted)
  288. {
  289. struct glink_channel *channel;
  290. unsigned long flags;
  291. spin_lock_irqsave(&glink->idr_lock, flags);
  292. channel = idr_find(&glink->rcids, cid);
  293. spin_unlock_irqrestore(&glink->idr_lock, flags);
  294. if (!channel) {
  295. dev_err(glink->dev, "unable to find channel\n");
  296. return;
  297. }
  298. channel->intent_req_result = granted;
  299. complete(&channel->intent_req_comp);
  300. }
  301. /**
  302. * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
  303. * @glink: Ptr to the glink edge
  304. * @channel: Ptr to the channel that the open req is sent
  305. *
  306. * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
  307. * Will return with refcount held, regardless of outcome.
  308. *
  309. * Returns 0 on success, negative errno otherwise.
  310. */
  311. static int qcom_glink_send_open_req(struct qcom_glink *glink,
  312. struct glink_channel *channel)
  313. {
  314. struct {
  315. struct glink_msg msg;
  316. u8 name[GLINK_NAME_SIZE];
  317. } __packed req;
  318. int name_len = strlen(channel->name) + 1;
  319. int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
  320. int ret;
  321. unsigned long flags;
  322. kref_get(&channel->refcount);
  323. spin_lock_irqsave(&glink->idr_lock, flags);
  324. ret = idr_alloc_cyclic(&glink->lcids, channel,
  325. RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
  326. GFP_ATOMIC);
  327. spin_unlock_irqrestore(&glink->idr_lock, flags);
  328. if (ret < 0)
  329. return ret;
  330. channel->lcid = ret;
  331. req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
  332. req.msg.param1 = cpu_to_le16(channel->lcid);
  333. req.msg.param2 = cpu_to_le32(name_len);
  334. strcpy(req.name, channel->name);
  335. ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
  336. if (ret)
  337. goto remove_idr;
  338. return 0;
  339. remove_idr:
  340. spin_lock_irqsave(&glink->idr_lock, flags);
  341. idr_remove(&glink->lcids, channel->lcid);
  342. channel->lcid = 0;
  343. spin_unlock_irqrestore(&glink->idr_lock, flags);
  344. return ret;
  345. }
  346. static void qcom_glink_send_close_req(struct qcom_glink *glink,
  347. struct glink_channel *channel)
  348. {
  349. struct glink_msg req;
  350. req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
  351. req.param1 = cpu_to_le16(channel->lcid);
  352. req.param2 = 0;
  353. qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
  354. }
  355. static void qcom_glink_send_close_ack(struct qcom_glink *glink,
  356. unsigned int rcid)
  357. {
  358. struct glink_msg req;
  359. req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
  360. req.param1 = cpu_to_le16(rcid);
  361. req.param2 = 0;
  362. qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
  363. }
  364. static void qcom_glink_rx_done_work(struct work_struct *work)
  365. {
  366. struct glink_channel *channel = container_of(work, struct glink_channel,
  367. intent_work);
  368. struct qcom_glink *glink = channel->glink;
  369. struct glink_core_rx_intent *intent, *tmp;
  370. struct {
  371. u16 id;
  372. u16 lcid;
  373. u32 liid;
  374. } __packed cmd;
  375. unsigned int cid = channel->lcid;
  376. unsigned int iid;
  377. bool reuse;
  378. unsigned long flags;
  379. spin_lock_irqsave(&channel->intent_lock, flags);
  380. list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
  381. list_del(&intent->node);
  382. spin_unlock_irqrestore(&channel->intent_lock, flags);
  383. iid = intent->id;
  384. reuse = intent->reuse;
  385. cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
  386. cmd.lcid = cid;
  387. cmd.liid = iid;
  388. qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
  389. if (!reuse) {
  390. kfree(intent->data);
  391. kfree(intent);
  392. }
  393. spin_lock_irqsave(&channel->intent_lock, flags);
  394. }
  395. spin_unlock_irqrestore(&channel->intent_lock, flags);
  396. }
  397. static void qcom_glink_rx_done(struct qcom_glink *glink,
  398. struct glink_channel *channel,
  399. struct glink_core_rx_intent *intent)
  400. {
  401. /* We don't send RX_DONE to intentless systems */
  402. if (glink->intentless) {
  403. kfree(intent->data);
  404. kfree(intent);
  405. return;
  406. }
  407. /* Take it off the tree of receive intents */
  408. if (!intent->reuse) {
  409. spin_lock(&channel->intent_lock);
  410. idr_remove(&channel->liids, intent->id);
  411. spin_unlock(&channel->intent_lock);
  412. }
  413. /* Schedule the sending of a rx_done indication */
  414. spin_lock(&channel->intent_lock);
  415. list_add_tail(&intent->node, &channel->done_intents);
  416. spin_unlock(&channel->intent_lock);
  417. schedule_work(&channel->intent_work);
  418. }
  419. /**
  420. * qcom_glink_receive_version() - receive version/features from remote system
  421. *
  422. * @glink: pointer to transport interface
  423. * @r_version: remote version
  424. * @r_features: remote features
  425. *
  426. * This function is called in response to a remote-initiated version/feature
  427. * negotiation sequence.
  428. */
  429. static void qcom_glink_receive_version(struct qcom_glink *glink,
  430. u32 version,
  431. u32 features)
  432. {
  433. switch (version) {
  434. case 0:
  435. break;
  436. case GLINK_VERSION_1:
  437. glink->features &= features;
  438. /* FALLTHROUGH */
  439. default:
  440. qcom_glink_send_version_ack(glink);
  441. break;
  442. }
  443. }
  444. /**
  445. * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
  446. *
  447. * @glink: pointer to transport interface
  448. * @r_version: remote version response
  449. * @r_features: remote features response
  450. *
  451. * This function is called in response to a local-initiated version/feature
  452. * negotiation sequence and is the counter-offer from the remote side based
  453. * upon the initial version and feature set requested.
  454. */
  455. static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
  456. u32 version,
  457. u32 features)
  458. {
  459. switch (version) {
  460. case 0:
  461. /* Version negotiation failed */
  462. break;
  463. case GLINK_VERSION_1:
  464. if (features == glink->features)
  465. break;
  466. glink->features &= features;
  467. /* FALLTHROUGH */
  468. default:
  469. qcom_glink_send_version(glink);
  470. break;
  471. }
  472. }
  473. /**
  474. * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
  475. wire format and transmit
  476. * @glink: The transport to transmit on.
  477. * @channel: The glink channel
  478. * @granted: The request response to encode.
  479. *
  480. * Return: 0 on success or standard Linux error code.
  481. */
  482. static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
  483. struct glink_channel *channel,
  484. bool granted)
  485. {
  486. struct glink_msg msg;
  487. msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
  488. msg.param1 = cpu_to_le16(channel->lcid);
  489. msg.param2 = cpu_to_le32(granted);
  490. qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
  491. return 0;
  492. }
  493. /**
  494. * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
  495. * transmit
  496. * @glink: The transport to transmit on.
  497. * @channel: The local channel
  498. * @size: The intent to pass on to remote.
  499. *
  500. * Return: 0 on success or standard Linux error code.
  501. */
  502. static int qcom_glink_advertise_intent(struct qcom_glink *glink,
  503. struct glink_channel *channel,
  504. struct glink_core_rx_intent *intent)
  505. {
  506. struct command {
  507. u16 id;
  508. u16 lcid;
  509. u32 count;
  510. u32 size;
  511. u32 liid;
  512. } __packed;
  513. struct command cmd;
  514. cmd.id = cpu_to_le16(RPM_CMD_INTENT);
  515. cmd.lcid = cpu_to_le16(channel->lcid);
  516. cmd.count = cpu_to_le32(1);
  517. cmd.size = cpu_to_le32(intent->size);
  518. cmd.liid = cpu_to_le32(intent->id);
  519. qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
  520. return 0;
  521. }
  522. static struct glink_core_rx_intent *
  523. qcom_glink_alloc_intent(struct qcom_glink *glink,
  524. struct glink_channel *channel,
  525. size_t size,
  526. bool reuseable)
  527. {
  528. struct glink_core_rx_intent *intent;
  529. int ret;
  530. unsigned long flags;
  531. intent = kzalloc(sizeof(*intent), GFP_KERNEL);
  532. if (!intent)
  533. return NULL;
  534. intent->data = kzalloc(size, GFP_KERNEL);
  535. if (!intent->data)
  536. return NULL;
  537. spin_lock_irqsave(&channel->intent_lock, flags);
  538. ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
  539. if (ret < 0) {
  540. spin_unlock_irqrestore(&channel->intent_lock, flags);
  541. return NULL;
  542. }
  543. spin_unlock_irqrestore(&channel->intent_lock, flags);
  544. intent->id = ret;
  545. intent->size = size;
  546. intent->reuse = reuseable;
  547. return intent;
  548. }
  549. static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
  550. u32 cid, uint32_t iid,
  551. bool reuse)
  552. {
  553. struct glink_core_rx_intent *intent;
  554. struct glink_channel *channel;
  555. unsigned long flags;
  556. spin_lock_irqsave(&glink->idr_lock, flags);
  557. channel = idr_find(&glink->rcids, cid);
  558. spin_unlock_irqrestore(&glink->idr_lock, flags);
  559. if (!channel) {
  560. dev_err(glink->dev, "invalid channel id received\n");
  561. return;
  562. }
  563. spin_lock_irqsave(&channel->intent_lock, flags);
  564. intent = idr_find(&channel->riids, iid);
  565. if (!intent) {
  566. spin_unlock_irqrestore(&channel->intent_lock, flags);
  567. dev_err(glink->dev, "invalid intent id received\n");
  568. return;
  569. }
  570. intent->in_use = false;
  571. if (!reuse) {
  572. idr_remove(&channel->riids, intent->id);
  573. kfree(intent);
  574. }
  575. spin_unlock_irqrestore(&channel->intent_lock, flags);
  576. }
  577. /**
  578. * qcom_glink_handle_intent_req() - Receive a request for rx_intent
  579. * from remote side
  580. * if_ptr: Pointer to the transport interface
  581. * rcid: Remote channel ID
  582. * size: size of the intent
  583. *
  584. * The function searches for the local channel to which the request for
  585. * rx_intent has arrived and allocates and notifies the remote back
  586. */
  587. static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
  588. u32 cid, size_t size)
  589. {
  590. struct glink_core_rx_intent *intent;
  591. struct glink_channel *channel;
  592. unsigned long flags;
  593. spin_lock_irqsave(&glink->idr_lock, flags);
  594. channel = idr_find(&glink->rcids, cid);
  595. spin_unlock_irqrestore(&glink->idr_lock, flags);
  596. if (!channel) {
  597. pr_err("%s channel not found for cid %d\n", __func__, cid);
  598. return;
  599. }
  600. intent = qcom_glink_alloc_intent(glink, channel, size, false);
  601. if (intent)
  602. qcom_glink_advertise_intent(glink, channel, intent);
  603. qcom_glink_send_intent_req_ack(glink, channel, !!intent);
  604. }
  605. static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
  606. {
  607. struct glink_defer_cmd *dcmd;
  608. extra = ALIGN(extra, 8);
  609. if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
  610. dev_dbg(glink->dev, "Insufficient data in rx fifo");
  611. return -ENXIO;
  612. }
  613. dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
  614. if (!dcmd)
  615. return -ENOMEM;
  616. INIT_LIST_HEAD(&dcmd->node);
  617. qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
  618. spin_lock(&glink->rx_lock);
  619. list_add_tail(&dcmd->node, &glink->rx_queue);
  620. spin_unlock(&glink->rx_lock);
  621. schedule_work(&glink->rx_work);
  622. qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
  623. return 0;
  624. }
  625. static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
  626. {
  627. struct glink_core_rx_intent *intent;
  628. struct glink_channel *channel;
  629. struct {
  630. struct glink_msg msg;
  631. __le32 chunk_size;
  632. __le32 left_size;
  633. } __packed hdr;
  634. unsigned int chunk_size;
  635. unsigned int left_size;
  636. unsigned int rcid;
  637. unsigned int liid;
  638. int ret = 0;
  639. unsigned long flags;
  640. if (avail < sizeof(hdr)) {
  641. dev_dbg(glink->dev, "Not enough data in fifo\n");
  642. return -EAGAIN;
  643. }
  644. qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
  645. chunk_size = le32_to_cpu(hdr.chunk_size);
  646. left_size = le32_to_cpu(hdr.left_size);
  647. if (avail < sizeof(hdr) + chunk_size) {
  648. dev_dbg(glink->dev, "Payload not yet in fifo\n");
  649. return -EAGAIN;
  650. }
  651. if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
  652. return -EINVAL;
  653. rcid = le16_to_cpu(hdr.msg.param1);
  654. spin_lock_irqsave(&glink->idr_lock, flags);
  655. channel = idr_find(&glink->rcids, rcid);
  656. spin_unlock_irqrestore(&glink->idr_lock, flags);
  657. if (!channel) {
  658. dev_dbg(glink->dev, "Data on non-existing channel\n");
  659. /* Drop the message */
  660. goto advance_rx;
  661. }
  662. if (glink->intentless) {
  663. /* Might have an ongoing, fragmented, message to append */
  664. if (!channel->buf) {
  665. intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
  666. if (!intent)
  667. return -ENOMEM;
  668. intent->data = kmalloc(chunk_size + left_size,
  669. GFP_ATOMIC);
  670. if (!intent->data) {
  671. kfree(intent);
  672. return -ENOMEM;
  673. }
  674. intent->id = 0xbabababa;
  675. intent->size = chunk_size + left_size;
  676. intent->offset = 0;
  677. channel->buf = intent;
  678. } else {
  679. intent = channel->buf;
  680. }
  681. } else {
  682. liid = le32_to_cpu(hdr.msg.param2);
  683. spin_lock_irqsave(&channel->intent_lock, flags);
  684. intent = idr_find(&channel->liids, liid);
  685. spin_unlock_irqrestore(&channel->intent_lock, flags);
  686. if (!intent) {
  687. dev_err(glink->dev,
  688. "no intent found for channel %s intent %d",
  689. channel->name, liid);
  690. goto advance_rx;
  691. }
  692. }
  693. if (intent->size - intent->offset < chunk_size) {
  694. dev_err(glink->dev, "Insufficient space in intent\n");
  695. /* The packet header lied, drop payload */
  696. goto advance_rx;
  697. }
  698. qcom_glink_rx_peak(glink, intent->data + intent->offset,
  699. sizeof(hdr), chunk_size);
  700. intent->offset += chunk_size;
  701. /* Handle message when no fragments remain to be received */
  702. if (!left_size) {
  703. spin_lock(&channel->recv_lock);
  704. if (channel->ept.cb) {
  705. channel->ept.cb(channel->ept.rpdev,
  706. intent->data,
  707. intent->offset,
  708. channel->ept.priv,
  709. RPMSG_ADDR_ANY);
  710. }
  711. spin_unlock(&channel->recv_lock);
  712. intent->offset = 0;
  713. channel->buf = NULL;
  714. qcom_glink_rx_done(glink, channel, intent);
  715. }
  716. advance_rx:
  717. qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
  718. return ret;
  719. }
  720. static void qcom_glink_handle_intent(struct qcom_glink *glink,
  721. unsigned int cid,
  722. unsigned int count,
  723. size_t avail)
  724. {
  725. struct glink_core_rx_intent *intent;
  726. struct glink_channel *channel;
  727. struct intent_pair {
  728. __le32 size;
  729. __le32 iid;
  730. };
  731. struct {
  732. struct glink_msg msg;
  733. struct intent_pair intents[];
  734. } __packed * msg;
  735. const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
  736. int ret;
  737. int i;
  738. unsigned long flags;
  739. if (avail < msglen) {
  740. dev_dbg(glink->dev, "Not enough data in fifo\n");
  741. return;
  742. }
  743. spin_lock_irqsave(&glink->idr_lock, flags);
  744. channel = idr_find(&glink->rcids, cid);
  745. spin_unlock_irqrestore(&glink->idr_lock, flags);
  746. if (!channel) {
  747. dev_err(glink->dev, "intents for non-existing channel\n");
  748. return;
  749. }
  750. msg = kmalloc(msglen, GFP_ATOMIC);
  751. if (!msg)
  752. return;
  753. qcom_glink_rx_peak(glink, msg, 0, msglen);
  754. for (i = 0; i < count; ++i) {
  755. intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
  756. if (!intent)
  757. break;
  758. intent->id = le32_to_cpu(msg->intents[i].iid);
  759. intent->size = le32_to_cpu(msg->intents[i].size);
  760. spin_lock_irqsave(&channel->intent_lock, flags);
  761. ret = idr_alloc(&channel->riids, intent,
  762. intent->id, intent->id + 1, GFP_ATOMIC);
  763. spin_unlock_irqrestore(&channel->intent_lock, flags);
  764. if (ret < 0)
  765. dev_err(glink->dev, "failed to store remote intent\n");
  766. }
  767. kfree(msg);
  768. qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
  769. }
  770. static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
  771. {
  772. struct glink_channel *channel;
  773. spin_lock(&glink->idr_lock);
  774. channel = idr_find(&glink->lcids, lcid);
  775. spin_unlock(&glink->idr_lock);
  776. if (!channel) {
  777. dev_err(glink->dev, "Invalid open ack packet\n");
  778. return -EINVAL;
  779. }
  780. complete(&channel->open_ack);
  781. return 0;
  782. }
  783. static irqreturn_t qcom_glink_native_intr(int irq, void *data)
  784. {
  785. struct qcom_glink *glink = data;
  786. struct glink_msg msg;
  787. unsigned int param1;
  788. unsigned int param2;
  789. unsigned int avail;
  790. unsigned int cmd;
  791. int ret = 0;
  792. for (;;) {
  793. avail = qcom_glink_rx_avail(glink);
  794. if (avail < sizeof(msg))
  795. break;
  796. qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
  797. cmd = le16_to_cpu(msg.cmd);
  798. param1 = le16_to_cpu(msg.param1);
  799. param2 = le32_to_cpu(msg.param2);
  800. switch (cmd) {
  801. case RPM_CMD_VERSION:
  802. case RPM_CMD_VERSION_ACK:
  803. case RPM_CMD_CLOSE:
  804. case RPM_CMD_CLOSE_ACK:
  805. case RPM_CMD_RX_INTENT_REQ:
  806. ret = qcom_glink_rx_defer(glink, 0);
  807. break;
  808. case RPM_CMD_OPEN_ACK:
  809. ret = qcom_glink_rx_open_ack(glink, param1);
  810. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  811. break;
  812. case RPM_CMD_OPEN:
  813. ret = qcom_glink_rx_defer(glink, param2);
  814. break;
  815. case RPM_CMD_TX_DATA:
  816. case RPM_CMD_TX_DATA_CONT:
  817. ret = qcom_glink_rx_data(glink, avail);
  818. break;
  819. case RPM_CMD_READ_NOTIF:
  820. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  821. mbox_send_message(glink->mbox_chan, NULL);
  822. mbox_client_txdone(glink->mbox_chan, 0);
  823. break;
  824. case RPM_CMD_INTENT:
  825. qcom_glink_handle_intent(glink, param1, param2, avail);
  826. break;
  827. case RPM_CMD_RX_DONE:
  828. qcom_glink_handle_rx_done(glink, param1, param2, false);
  829. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  830. break;
  831. case RPM_CMD_RX_DONE_W_REUSE:
  832. qcom_glink_handle_rx_done(glink, param1, param2, true);
  833. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  834. break;
  835. case RPM_CMD_RX_INTENT_REQ_ACK:
  836. qcom_glink_handle_intent_req_ack(glink, param1, param2);
  837. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  838. break;
  839. default:
  840. dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
  841. ret = -EINVAL;
  842. break;
  843. }
  844. if (ret)
  845. break;
  846. }
  847. return IRQ_HANDLED;
  848. }
  849. /* Locally initiated rpmsg_create_ept */
  850. static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
  851. const char *name)
  852. {
  853. struct glink_channel *channel;
  854. int ret;
  855. unsigned long flags;
  856. channel = qcom_glink_alloc_channel(glink, name);
  857. if (IS_ERR(channel))
  858. return ERR_CAST(channel);
  859. ret = qcom_glink_send_open_req(glink, channel);
  860. if (ret)
  861. goto release_channel;
  862. ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
  863. if (!ret)
  864. goto err_timeout;
  865. ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
  866. if (!ret)
  867. goto err_timeout;
  868. qcom_glink_send_open_ack(glink, channel);
  869. return channel;
  870. err_timeout:
  871. /* qcom_glink_send_open_req() did register the channel in lcids*/
  872. spin_lock_irqsave(&glink->idr_lock, flags);
  873. idr_remove(&glink->lcids, channel->lcid);
  874. spin_unlock_irqrestore(&glink->idr_lock, flags);
  875. release_channel:
  876. /* Release qcom_glink_send_open_req() reference */
  877. kref_put(&channel->refcount, qcom_glink_channel_release);
  878. /* Release qcom_glink_alloc_channel() reference */
  879. kref_put(&channel->refcount, qcom_glink_channel_release);
  880. return ERR_PTR(-ETIMEDOUT);
  881. }
  882. /* Remote initiated rpmsg_create_ept */
  883. static int qcom_glink_create_remote(struct qcom_glink *glink,
  884. struct glink_channel *channel)
  885. {
  886. int ret;
  887. qcom_glink_send_open_ack(glink, channel);
  888. ret = qcom_glink_send_open_req(glink, channel);
  889. if (ret)
  890. goto close_link;
  891. ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
  892. if (!ret) {
  893. ret = -ETIMEDOUT;
  894. goto close_link;
  895. }
  896. return 0;
  897. close_link:
  898. /*
  899. * Send a close request to "undo" our open-ack. The close-ack will
  900. * release the last reference.
  901. */
  902. qcom_glink_send_close_req(glink, channel);
  903. /* Release qcom_glink_send_open_req() reference */
  904. kref_put(&channel->refcount, qcom_glink_channel_release);
  905. return ret;
  906. }
  907. static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
  908. rpmsg_rx_cb_t cb,
  909. void *priv,
  910. struct rpmsg_channel_info
  911. chinfo)
  912. {
  913. struct glink_channel *parent = to_glink_channel(rpdev->ept);
  914. struct glink_channel *channel;
  915. struct qcom_glink *glink = parent->glink;
  916. struct rpmsg_endpoint *ept;
  917. const char *name = chinfo.name;
  918. int cid;
  919. int ret;
  920. unsigned long flags;
  921. spin_lock_irqsave(&glink->idr_lock, flags);
  922. idr_for_each_entry(&glink->rcids, channel, cid) {
  923. if (!strcmp(channel->name, name))
  924. break;
  925. }
  926. spin_unlock_irqrestore(&glink->idr_lock, flags);
  927. if (!channel) {
  928. channel = qcom_glink_create_local(glink, name);
  929. if (IS_ERR(channel))
  930. return NULL;
  931. } else {
  932. ret = qcom_glink_create_remote(glink, channel);
  933. if (ret)
  934. return NULL;
  935. }
  936. ept = &channel->ept;
  937. ept->rpdev = rpdev;
  938. ept->cb = cb;
  939. ept->priv = priv;
  940. ept->ops = &glink_endpoint_ops;
  941. return ept;
  942. }
  943. static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
  944. {
  945. struct glink_channel *channel = to_glink_channel(rpdev->ept);
  946. struct glink_core_rx_intent *intent;
  947. struct qcom_glink *glink = channel->glink;
  948. int num_intents = glink->intentless ? 0 : 5;
  949. /* Channel is now open, advertise base set of intents */
  950. while (num_intents--) {
  951. intent = qcom_glink_alloc_intent(glink, channel, SZ_1K, true);
  952. if (!intent)
  953. break;
  954. qcom_glink_advertise_intent(glink, channel, intent);
  955. }
  956. return 0;
  957. }
  958. static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
  959. {
  960. struct glink_channel *channel = to_glink_channel(ept);
  961. struct qcom_glink *glink = channel->glink;
  962. unsigned long flags;
  963. spin_lock_irqsave(&channel->recv_lock, flags);
  964. channel->ept.cb = NULL;
  965. spin_unlock_irqrestore(&channel->recv_lock, flags);
  966. /* Decouple the potential rpdev from the channel */
  967. channel->rpdev = NULL;
  968. qcom_glink_send_close_req(glink, channel);
  969. }
  970. static int qcom_glink_request_intent(struct qcom_glink *glink,
  971. struct glink_channel *channel,
  972. size_t size)
  973. {
  974. struct {
  975. u16 id;
  976. u16 cid;
  977. u32 size;
  978. } __packed cmd;
  979. int ret;
  980. mutex_lock(&channel->intent_req_lock);
  981. reinit_completion(&channel->intent_req_comp);
  982. cmd.id = RPM_CMD_RX_INTENT_REQ;
  983. cmd.cid = channel->lcid;
  984. cmd.size = size;
  985. ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
  986. if (ret)
  987. return ret;
  988. ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
  989. if (!ret) {
  990. dev_err(glink->dev, "intent request timed out\n");
  991. ret = -ETIMEDOUT;
  992. } else {
  993. ret = channel->intent_req_result ? 0 : -ECANCELED;
  994. }
  995. mutex_unlock(&channel->intent_req_lock);
  996. return ret;
  997. }
  998. static int __qcom_glink_send(struct glink_channel *channel,
  999. void *data, int len, bool wait)
  1000. {
  1001. struct qcom_glink *glink = channel->glink;
  1002. struct glink_core_rx_intent *intent = NULL;
  1003. struct glink_core_rx_intent *tmp;
  1004. int iid = 0;
  1005. struct {
  1006. struct glink_msg msg;
  1007. __le32 chunk_size;
  1008. __le32 left_size;
  1009. } __packed req;
  1010. int ret;
  1011. unsigned long flags;
  1012. if (!glink->intentless) {
  1013. while (!intent) {
  1014. spin_lock_irqsave(&channel->intent_lock, flags);
  1015. idr_for_each_entry(&channel->riids, tmp, iid) {
  1016. if (tmp->size >= len && !tmp->in_use) {
  1017. tmp->in_use = true;
  1018. intent = tmp;
  1019. break;
  1020. }
  1021. }
  1022. spin_unlock_irqrestore(&channel->intent_lock, flags);
  1023. /* We found an available intent */
  1024. if (intent)
  1025. break;
  1026. if (!wait)
  1027. return -EBUSY;
  1028. ret = qcom_glink_request_intent(glink, channel, len);
  1029. if (ret < 0)
  1030. return ret;
  1031. }
  1032. iid = intent->id;
  1033. }
  1034. req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
  1035. req.msg.param1 = cpu_to_le16(channel->lcid);
  1036. req.msg.param2 = cpu_to_le32(iid);
  1037. req.chunk_size = cpu_to_le32(len);
  1038. req.left_size = cpu_to_le32(0);
  1039. ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
  1040. /* Mark intent available if we failed */
  1041. if (ret && intent)
  1042. intent->in_use = false;
  1043. return ret;
  1044. }
  1045. static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
  1046. {
  1047. struct glink_channel *channel = to_glink_channel(ept);
  1048. return __qcom_glink_send(channel, data, len, true);
  1049. }
  1050. static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  1051. {
  1052. struct glink_channel *channel = to_glink_channel(ept);
  1053. return __qcom_glink_send(channel, data, len, false);
  1054. }
  1055. /*
  1056. * Finds the device_node for the glink child interested in this channel.
  1057. */
  1058. static struct device_node *qcom_glink_match_channel(struct device_node *node,
  1059. const char *channel)
  1060. {
  1061. struct device_node *child;
  1062. const char *name;
  1063. const char *key;
  1064. int ret;
  1065. for_each_available_child_of_node(node, child) {
  1066. key = "qcom,glink-channels";
  1067. ret = of_property_read_string(child, key, &name);
  1068. if (ret)
  1069. continue;
  1070. if (strcmp(name, channel) == 0)
  1071. return child;
  1072. }
  1073. return NULL;
  1074. }
  1075. static const struct rpmsg_device_ops glink_device_ops = {
  1076. .create_ept = qcom_glink_create_ept,
  1077. .announce_create = qcom_glink_announce_create,
  1078. };
  1079. static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
  1080. .destroy_ept = qcom_glink_destroy_ept,
  1081. .send = qcom_glink_send,
  1082. .trysend = qcom_glink_trysend,
  1083. };
  1084. static void qcom_glink_rpdev_release(struct device *dev)
  1085. {
  1086. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  1087. struct glink_channel *channel = to_glink_channel(rpdev->ept);
  1088. channel->rpdev = NULL;
  1089. kfree(rpdev);
  1090. }
  1091. static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
  1092. char *name)
  1093. {
  1094. struct glink_channel *channel;
  1095. struct rpmsg_device *rpdev;
  1096. bool create_device = false;
  1097. struct device_node *node;
  1098. int lcid;
  1099. int ret;
  1100. unsigned long flags;
  1101. spin_lock_irqsave(&glink->idr_lock, flags);
  1102. idr_for_each_entry(&glink->lcids, channel, lcid) {
  1103. if (!strcmp(channel->name, name))
  1104. break;
  1105. }
  1106. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1107. if (!channel) {
  1108. channel = qcom_glink_alloc_channel(glink, name);
  1109. if (IS_ERR(channel))
  1110. return PTR_ERR(channel);
  1111. /* The opening dance was initiated by the remote */
  1112. create_device = true;
  1113. }
  1114. spin_lock_irqsave(&glink->idr_lock, flags);
  1115. ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
  1116. if (ret < 0) {
  1117. dev_err(glink->dev, "Unable to insert channel into rcid list\n");
  1118. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1119. goto free_channel;
  1120. }
  1121. channel->rcid = ret;
  1122. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1123. complete(&channel->open_req);
  1124. if (create_device) {
  1125. rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
  1126. if (!rpdev) {
  1127. ret = -ENOMEM;
  1128. goto rcid_remove;
  1129. }
  1130. rpdev->ept = &channel->ept;
  1131. strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
  1132. rpdev->src = RPMSG_ADDR_ANY;
  1133. rpdev->dst = RPMSG_ADDR_ANY;
  1134. rpdev->ops = &glink_device_ops;
  1135. node = qcom_glink_match_channel(glink->dev->of_node, name);
  1136. rpdev->dev.of_node = node;
  1137. rpdev->dev.parent = glink->dev;
  1138. rpdev->dev.release = qcom_glink_rpdev_release;
  1139. ret = rpmsg_register_device(rpdev);
  1140. if (ret)
  1141. goto free_rpdev;
  1142. channel->rpdev = rpdev;
  1143. }
  1144. return 0;
  1145. free_rpdev:
  1146. kfree(rpdev);
  1147. rcid_remove:
  1148. spin_lock_irqsave(&glink->idr_lock, flags);
  1149. idr_remove(&glink->rcids, channel->rcid);
  1150. channel->rcid = 0;
  1151. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1152. free_channel:
  1153. /* Release the reference, iff we took it */
  1154. if (create_device)
  1155. kref_put(&channel->refcount, qcom_glink_channel_release);
  1156. return ret;
  1157. }
  1158. static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
  1159. {
  1160. struct rpmsg_channel_info chinfo;
  1161. struct glink_channel *channel;
  1162. unsigned long flags;
  1163. spin_lock_irqsave(&glink->idr_lock, flags);
  1164. channel = idr_find(&glink->rcids, rcid);
  1165. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1166. if (WARN(!channel, "close request on unknown channel\n"))
  1167. return;
  1168. /* cancel pending rx_done work */
  1169. cancel_work_sync(&channel->intent_work);
  1170. if (channel->rpdev) {
  1171. strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
  1172. chinfo.src = RPMSG_ADDR_ANY;
  1173. chinfo.dst = RPMSG_ADDR_ANY;
  1174. rpmsg_unregister_device(glink->dev, &chinfo);
  1175. }
  1176. qcom_glink_send_close_ack(glink, channel->rcid);
  1177. spin_lock_irqsave(&glink->idr_lock, flags);
  1178. idr_remove(&glink->rcids, channel->rcid);
  1179. channel->rcid = 0;
  1180. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1181. kref_put(&channel->refcount, qcom_glink_channel_release);
  1182. }
  1183. static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
  1184. {
  1185. struct glink_channel *channel;
  1186. unsigned long flags;
  1187. spin_lock_irqsave(&glink->idr_lock, flags);
  1188. channel = idr_find(&glink->lcids, lcid);
  1189. if (WARN(!channel, "close ack on unknown channel\n")) {
  1190. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1191. return;
  1192. }
  1193. idr_remove(&glink->lcids, channel->lcid);
  1194. channel->lcid = 0;
  1195. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1196. kref_put(&channel->refcount, qcom_glink_channel_release);
  1197. }
  1198. static void qcom_glink_work(struct work_struct *work)
  1199. {
  1200. struct qcom_glink *glink = container_of(work, struct qcom_glink,
  1201. rx_work);
  1202. struct glink_defer_cmd *dcmd;
  1203. struct glink_msg *msg;
  1204. unsigned long flags;
  1205. unsigned int param1;
  1206. unsigned int param2;
  1207. unsigned int cmd;
  1208. for (;;) {
  1209. spin_lock_irqsave(&glink->rx_lock, flags);
  1210. if (list_empty(&glink->rx_queue)) {
  1211. spin_unlock_irqrestore(&glink->rx_lock, flags);
  1212. break;
  1213. }
  1214. dcmd = list_first_entry(&glink->rx_queue,
  1215. struct glink_defer_cmd, node);
  1216. list_del(&dcmd->node);
  1217. spin_unlock_irqrestore(&glink->rx_lock, flags);
  1218. msg = &dcmd->msg;
  1219. cmd = le16_to_cpu(msg->cmd);
  1220. param1 = le16_to_cpu(msg->param1);
  1221. param2 = le32_to_cpu(msg->param2);
  1222. switch (cmd) {
  1223. case RPM_CMD_VERSION:
  1224. qcom_glink_receive_version(glink, param1, param2);
  1225. break;
  1226. case RPM_CMD_VERSION_ACK:
  1227. qcom_glink_receive_version_ack(glink, param1, param2);
  1228. break;
  1229. case RPM_CMD_OPEN:
  1230. qcom_glink_rx_open(glink, param1, msg->data);
  1231. break;
  1232. case RPM_CMD_CLOSE:
  1233. qcom_glink_rx_close(glink, param1);
  1234. break;
  1235. case RPM_CMD_CLOSE_ACK:
  1236. qcom_glink_rx_close_ack(glink, param1);
  1237. break;
  1238. case RPM_CMD_RX_INTENT_REQ:
  1239. qcom_glink_handle_intent_req(glink, param1, param2);
  1240. break;
  1241. default:
  1242. WARN(1, "Unknown defer object %d\n", cmd);
  1243. break;
  1244. }
  1245. kfree(dcmd);
  1246. }
  1247. }
  1248. struct qcom_glink *qcom_glink_native_probe(struct device *dev,
  1249. unsigned long features,
  1250. struct qcom_glink_pipe *rx,
  1251. struct qcom_glink_pipe *tx,
  1252. bool intentless)
  1253. {
  1254. int irq;
  1255. int ret;
  1256. struct qcom_glink *glink;
  1257. glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
  1258. if (!glink)
  1259. return ERR_PTR(-ENOMEM);
  1260. glink->dev = dev;
  1261. glink->tx_pipe = tx;
  1262. glink->rx_pipe = rx;
  1263. glink->features = features;
  1264. glink->intentless = intentless;
  1265. mutex_init(&glink->tx_lock);
  1266. spin_lock_init(&glink->rx_lock);
  1267. INIT_LIST_HEAD(&glink->rx_queue);
  1268. INIT_WORK(&glink->rx_work, qcom_glink_work);
  1269. spin_lock_init(&glink->idr_lock);
  1270. idr_init(&glink->lcids);
  1271. idr_init(&glink->rcids);
  1272. glink->mbox_client.dev = dev;
  1273. glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
  1274. if (IS_ERR(glink->mbox_chan)) {
  1275. if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
  1276. dev_err(dev, "failed to acquire IPC channel\n");
  1277. return ERR_CAST(glink->mbox_chan);
  1278. }
  1279. irq = of_irq_get(dev->of_node, 0);
  1280. ret = devm_request_irq(dev, irq,
  1281. qcom_glink_native_intr,
  1282. IRQF_NO_SUSPEND | IRQF_SHARED,
  1283. "glink-native", glink);
  1284. if (ret) {
  1285. dev_err(dev, "failed to request IRQ\n");
  1286. return ERR_PTR(ret);
  1287. }
  1288. glink->irq = irq;
  1289. ret = qcom_glink_send_version(glink);
  1290. if (ret)
  1291. return ERR_PTR(ret);
  1292. return glink;
  1293. }
  1294. EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
  1295. static int qcom_glink_remove_device(struct device *dev, void *data)
  1296. {
  1297. device_unregister(dev);
  1298. return 0;
  1299. }
  1300. void qcom_glink_native_remove(struct qcom_glink *glink)
  1301. {
  1302. struct glink_channel *channel;
  1303. int cid;
  1304. int ret;
  1305. unsigned long flags;
  1306. disable_irq(glink->irq);
  1307. cancel_work_sync(&glink->rx_work);
  1308. ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
  1309. if (ret)
  1310. dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
  1311. spin_lock_irqsave(&glink->idr_lock, flags);
  1312. /* Release any defunct local channels, waiting for close-ack */
  1313. idr_for_each_entry(&glink->lcids, channel, cid)
  1314. kref_put(&channel->refcount, qcom_glink_channel_release);
  1315. idr_destroy(&glink->lcids);
  1316. idr_destroy(&glink->rcids);
  1317. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1318. mbox_free_channel(glink->mbox_chan);
  1319. }
  1320. EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
  1321. void qcom_glink_native_unregister(struct qcom_glink *glink)
  1322. {
  1323. device_unregister(glink->dev);
  1324. }
  1325. EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);