qcom_glink_native.c 41 KB

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