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. ret = -ENOENT;
  712. goto advance_rx;
  713. }
  714. }
  715. if (intent->size - intent->offset < chunk_size) {
  716. dev_err(glink->dev, "Insufficient space in intent\n");
  717. /* The packet header lied, drop payload */
  718. goto advance_rx;
  719. }
  720. qcom_glink_rx_peak(glink, intent->data + intent->offset,
  721. sizeof(hdr), chunk_size);
  722. intent->offset += chunk_size;
  723. /* Handle message when no fragments remain to be received */
  724. if (!left_size) {
  725. spin_lock(&channel->recv_lock);
  726. if (channel->ept.cb) {
  727. channel->ept.cb(channel->ept.rpdev,
  728. intent->data,
  729. intent->offset,
  730. channel->ept.priv,
  731. RPMSG_ADDR_ANY);
  732. }
  733. spin_unlock(&channel->recv_lock);
  734. intent->offset = 0;
  735. channel->buf = NULL;
  736. qcom_glink_rx_done(glink, channel, intent);
  737. }
  738. advance_rx:
  739. qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
  740. return ret;
  741. }
  742. static void qcom_glink_handle_intent(struct qcom_glink *glink,
  743. unsigned int cid,
  744. unsigned int count,
  745. size_t avail)
  746. {
  747. struct glink_core_rx_intent *intent;
  748. struct glink_channel *channel;
  749. struct intent_pair {
  750. __le32 size;
  751. __le32 iid;
  752. };
  753. struct {
  754. struct glink_msg msg;
  755. struct intent_pair intents[];
  756. } __packed * msg;
  757. const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
  758. int ret;
  759. int i;
  760. unsigned long flags;
  761. if (avail < msglen) {
  762. dev_dbg(glink->dev, "Not enough data in fifo\n");
  763. return;
  764. }
  765. spin_lock_irqsave(&glink->idr_lock, flags);
  766. channel = idr_find(&glink->rcids, cid);
  767. spin_unlock_irqrestore(&glink->idr_lock, flags);
  768. if (!channel) {
  769. dev_err(glink->dev, "intents for non-existing channel\n");
  770. return;
  771. }
  772. msg = kmalloc(msglen, GFP_ATOMIC);
  773. if (!msg)
  774. return;
  775. qcom_glink_rx_peak(glink, msg, 0, msglen);
  776. for (i = 0; i < count; ++i) {
  777. intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
  778. if (!intent)
  779. break;
  780. intent->id = le32_to_cpu(msg->intents[i].iid);
  781. intent->size = le32_to_cpu(msg->intents[i].size);
  782. spin_lock_irqsave(&channel->intent_lock, flags);
  783. ret = idr_alloc(&channel->riids, intent,
  784. intent->id, intent->id + 1, GFP_ATOMIC);
  785. spin_unlock_irqrestore(&channel->intent_lock, flags);
  786. if (ret < 0)
  787. dev_err(glink->dev, "failed to store remote intent\n");
  788. }
  789. kfree(msg);
  790. qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
  791. }
  792. static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
  793. {
  794. struct glink_channel *channel;
  795. spin_lock(&glink->idr_lock);
  796. channel = idr_find(&glink->lcids, lcid);
  797. spin_unlock(&glink->idr_lock);
  798. if (!channel) {
  799. dev_err(glink->dev, "Invalid open ack packet\n");
  800. return -EINVAL;
  801. }
  802. complete_all(&channel->open_ack);
  803. return 0;
  804. }
  805. static irqreturn_t qcom_glink_native_intr(int irq, void *data)
  806. {
  807. struct qcom_glink *glink = data;
  808. struct glink_msg msg;
  809. unsigned int param1;
  810. unsigned int param2;
  811. unsigned int avail;
  812. unsigned int cmd;
  813. int ret = 0;
  814. for (;;) {
  815. avail = qcom_glink_rx_avail(glink);
  816. if (avail < sizeof(msg))
  817. break;
  818. qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
  819. cmd = le16_to_cpu(msg.cmd);
  820. param1 = le16_to_cpu(msg.param1);
  821. param2 = le32_to_cpu(msg.param2);
  822. switch (cmd) {
  823. case RPM_CMD_VERSION:
  824. case RPM_CMD_VERSION_ACK:
  825. case RPM_CMD_CLOSE:
  826. case RPM_CMD_CLOSE_ACK:
  827. case RPM_CMD_RX_INTENT_REQ:
  828. ret = qcom_glink_rx_defer(glink, 0);
  829. break;
  830. case RPM_CMD_OPEN_ACK:
  831. ret = qcom_glink_rx_open_ack(glink, param1);
  832. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  833. break;
  834. case RPM_CMD_OPEN:
  835. ret = qcom_glink_rx_defer(glink, param2);
  836. break;
  837. case RPM_CMD_TX_DATA:
  838. case RPM_CMD_TX_DATA_CONT:
  839. ret = qcom_glink_rx_data(glink, avail);
  840. break;
  841. case RPM_CMD_READ_NOTIF:
  842. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  843. mbox_send_message(glink->mbox_chan, NULL);
  844. mbox_client_txdone(glink->mbox_chan, 0);
  845. break;
  846. case RPM_CMD_INTENT:
  847. qcom_glink_handle_intent(glink, param1, param2, avail);
  848. break;
  849. case RPM_CMD_RX_DONE:
  850. qcom_glink_handle_rx_done(glink, param1, param2, false);
  851. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  852. break;
  853. case RPM_CMD_RX_DONE_W_REUSE:
  854. qcom_glink_handle_rx_done(glink, param1, param2, true);
  855. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  856. break;
  857. case RPM_CMD_RX_INTENT_REQ_ACK:
  858. qcom_glink_handle_intent_req_ack(glink, param1, param2);
  859. qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
  860. break;
  861. default:
  862. dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
  863. ret = -EINVAL;
  864. break;
  865. }
  866. if (ret)
  867. break;
  868. }
  869. return IRQ_HANDLED;
  870. }
  871. /* Locally initiated rpmsg_create_ept */
  872. static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
  873. const char *name)
  874. {
  875. struct glink_channel *channel;
  876. int ret;
  877. unsigned long flags;
  878. channel = qcom_glink_alloc_channel(glink, name);
  879. if (IS_ERR(channel))
  880. return ERR_CAST(channel);
  881. ret = qcom_glink_send_open_req(glink, channel);
  882. if (ret)
  883. goto release_channel;
  884. ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
  885. if (!ret)
  886. goto err_timeout;
  887. ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
  888. if (!ret)
  889. goto err_timeout;
  890. qcom_glink_send_open_ack(glink, channel);
  891. return channel;
  892. err_timeout:
  893. /* qcom_glink_send_open_req() did register the channel in lcids*/
  894. spin_lock_irqsave(&glink->idr_lock, flags);
  895. idr_remove(&glink->lcids, channel->lcid);
  896. spin_unlock_irqrestore(&glink->idr_lock, flags);
  897. release_channel:
  898. /* Release qcom_glink_send_open_req() reference */
  899. kref_put(&channel->refcount, qcom_glink_channel_release);
  900. /* Release qcom_glink_alloc_channel() reference */
  901. kref_put(&channel->refcount, qcom_glink_channel_release);
  902. return ERR_PTR(-ETIMEDOUT);
  903. }
  904. /* Remote initiated rpmsg_create_ept */
  905. static int qcom_glink_create_remote(struct qcom_glink *glink,
  906. struct glink_channel *channel)
  907. {
  908. int ret;
  909. qcom_glink_send_open_ack(glink, channel);
  910. ret = qcom_glink_send_open_req(glink, channel);
  911. if (ret)
  912. goto close_link;
  913. ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
  914. if (!ret) {
  915. ret = -ETIMEDOUT;
  916. goto close_link;
  917. }
  918. return 0;
  919. close_link:
  920. /*
  921. * Send a close request to "undo" our open-ack. The close-ack will
  922. * release qcom_glink_send_open_req() reference and the last reference
  923. * will be relesed after receiving remote_close or transport unregister
  924. * by calling qcom_glink_native_remove().
  925. */
  926. qcom_glink_send_close_req(glink, channel);
  927. return ret;
  928. }
  929. static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
  930. rpmsg_rx_cb_t cb,
  931. void *priv,
  932. struct rpmsg_channel_info
  933. chinfo)
  934. {
  935. struct glink_channel *parent = to_glink_channel(rpdev->ept);
  936. struct glink_channel *channel;
  937. struct qcom_glink *glink = parent->glink;
  938. struct rpmsg_endpoint *ept;
  939. const char *name = chinfo.name;
  940. int cid;
  941. int ret;
  942. unsigned long flags;
  943. spin_lock_irqsave(&glink->idr_lock, flags);
  944. idr_for_each_entry(&glink->rcids, channel, cid) {
  945. if (!strcmp(channel->name, name))
  946. break;
  947. }
  948. spin_unlock_irqrestore(&glink->idr_lock, flags);
  949. if (!channel) {
  950. channel = qcom_glink_create_local(glink, name);
  951. if (IS_ERR(channel))
  952. return NULL;
  953. } else {
  954. ret = qcom_glink_create_remote(glink, channel);
  955. if (ret)
  956. return NULL;
  957. }
  958. ept = &channel->ept;
  959. ept->rpdev = rpdev;
  960. ept->cb = cb;
  961. ept->priv = priv;
  962. ept->ops = &glink_endpoint_ops;
  963. return ept;
  964. }
  965. static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
  966. {
  967. struct glink_channel *channel = to_glink_channel(rpdev->ept);
  968. struct device_node *np = rpdev->dev.of_node;
  969. struct qcom_glink *glink = channel->glink;
  970. struct glink_core_rx_intent *intent;
  971. const struct property *prop = NULL;
  972. __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
  973. int num_intents;
  974. int num_groups = 1;
  975. __be32 *val = defaults;
  976. int size;
  977. if (glink->intentless || !completion_done(&channel->open_ack))
  978. return 0;
  979. prop = of_find_property(np, "qcom,intents", NULL);
  980. if (prop) {
  981. val = prop->value;
  982. num_groups = prop->length / sizeof(u32) / 2;
  983. }
  984. /* Channel is now open, advertise base set of intents */
  985. while (num_groups--) {
  986. size = be32_to_cpup(val++);
  987. num_intents = be32_to_cpup(val++);
  988. while (num_intents--) {
  989. intent = qcom_glink_alloc_intent(glink, channel, size,
  990. true);
  991. if (!intent)
  992. break;
  993. qcom_glink_advertise_intent(glink, channel, intent);
  994. }
  995. }
  996. return 0;
  997. }
  998. static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
  999. {
  1000. struct glink_channel *channel = to_glink_channel(ept);
  1001. struct qcom_glink *glink = channel->glink;
  1002. unsigned long flags;
  1003. spin_lock_irqsave(&channel->recv_lock, flags);
  1004. channel->ept.cb = NULL;
  1005. spin_unlock_irqrestore(&channel->recv_lock, flags);
  1006. /* Decouple the potential rpdev from the channel */
  1007. channel->rpdev = NULL;
  1008. qcom_glink_send_close_req(glink, channel);
  1009. }
  1010. static int qcom_glink_request_intent(struct qcom_glink *glink,
  1011. struct glink_channel *channel,
  1012. size_t size)
  1013. {
  1014. struct {
  1015. u16 id;
  1016. u16 cid;
  1017. u32 size;
  1018. } __packed cmd;
  1019. int ret;
  1020. mutex_lock(&channel->intent_req_lock);
  1021. reinit_completion(&channel->intent_req_comp);
  1022. cmd.id = RPM_CMD_RX_INTENT_REQ;
  1023. cmd.cid = channel->lcid;
  1024. cmd.size = size;
  1025. ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
  1026. if (ret)
  1027. goto unlock;
  1028. ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
  1029. if (!ret) {
  1030. dev_err(glink->dev, "intent request timed out\n");
  1031. ret = -ETIMEDOUT;
  1032. } else {
  1033. ret = channel->intent_req_result ? 0 : -ECANCELED;
  1034. }
  1035. unlock:
  1036. mutex_unlock(&channel->intent_req_lock);
  1037. return ret;
  1038. }
  1039. static int __qcom_glink_send(struct glink_channel *channel,
  1040. void *data, int len, bool wait)
  1041. {
  1042. struct qcom_glink *glink = channel->glink;
  1043. struct glink_core_rx_intent *intent = NULL;
  1044. struct glink_core_rx_intent *tmp;
  1045. int iid = 0;
  1046. struct {
  1047. struct glink_msg msg;
  1048. __le32 chunk_size;
  1049. __le32 left_size;
  1050. } __packed req;
  1051. int ret;
  1052. unsigned long flags;
  1053. if (!glink->intentless) {
  1054. while (!intent) {
  1055. spin_lock_irqsave(&channel->intent_lock, flags);
  1056. idr_for_each_entry(&channel->riids, tmp, iid) {
  1057. if (tmp->size >= len && !tmp->in_use) {
  1058. if (!intent)
  1059. intent = tmp;
  1060. else if (intent->size > tmp->size)
  1061. intent = tmp;
  1062. if (intent->size == len)
  1063. break;
  1064. }
  1065. }
  1066. if (intent)
  1067. intent->in_use = true;
  1068. spin_unlock_irqrestore(&channel->intent_lock, flags);
  1069. /* We found an available intent */
  1070. if (intent)
  1071. break;
  1072. if (!wait)
  1073. return -EBUSY;
  1074. ret = qcom_glink_request_intent(glink, channel, len);
  1075. if (ret < 0)
  1076. return ret;
  1077. }
  1078. iid = intent->id;
  1079. }
  1080. req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
  1081. req.msg.param1 = cpu_to_le16(channel->lcid);
  1082. req.msg.param2 = cpu_to_le32(iid);
  1083. req.chunk_size = cpu_to_le32(len);
  1084. req.left_size = cpu_to_le32(0);
  1085. ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
  1086. /* Mark intent available if we failed */
  1087. if (ret && intent)
  1088. intent->in_use = false;
  1089. return ret;
  1090. }
  1091. static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
  1092. {
  1093. struct glink_channel *channel = to_glink_channel(ept);
  1094. return __qcom_glink_send(channel, data, len, true);
  1095. }
  1096. static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
  1097. {
  1098. struct glink_channel *channel = to_glink_channel(ept);
  1099. return __qcom_glink_send(channel, data, len, false);
  1100. }
  1101. /*
  1102. * Finds the device_node for the glink child interested in this channel.
  1103. */
  1104. static struct device_node *qcom_glink_match_channel(struct device_node *node,
  1105. const char *channel)
  1106. {
  1107. struct device_node *child;
  1108. const char *name;
  1109. const char *key;
  1110. int ret;
  1111. for_each_available_child_of_node(node, child) {
  1112. key = "qcom,glink-channels";
  1113. ret = of_property_read_string(child, key, &name);
  1114. if (ret)
  1115. continue;
  1116. if (strcmp(name, channel) == 0)
  1117. return child;
  1118. }
  1119. return NULL;
  1120. }
  1121. static const struct rpmsg_device_ops glink_device_ops = {
  1122. .create_ept = qcom_glink_create_ept,
  1123. .announce_create = qcom_glink_announce_create,
  1124. };
  1125. static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
  1126. .destroy_ept = qcom_glink_destroy_ept,
  1127. .send = qcom_glink_send,
  1128. .trysend = qcom_glink_trysend,
  1129. };
  1130. static void qcom_glink_rpdev_release(struct device *dev)
  1131. {
  1132. struct rpmsg_device *rpdev = to_rpmsg_device(dev);
  1133. struct glink_channel *channel = to_glink_channel(rpdev->ept);
  1134. channel->rpdev = NULL;
  1135. kfree(rpdev);
  1136. }
  1137. static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
  1138. char *name)
  1139. {
  1140. struct glink_channel *channel;
  1141. struct rpmsg_device *rpdev;
  1142. bool create_device = false;
  1143. struct device_node *node;
  1144. int lcid;
  1145. int ret;
  1146. unsigned long flags;
  1147. spin_lock_irqsave(&glink->idr_lock, flags);
  1148. idr_for_each_entry(&glink->lcids, channel, lcid) {
  1149. if (!strcmp(channel->name, name))
  1150. break;
  1151. }
  1152. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1153. if (!channel) {
  1154. channel = qcom_glink_alloc_channel(glink, name);
  1155. if (IS_ERR(channel))
  1156. return PTR_ERR(channel);
  1157. /* The opening dance was initiated by the remote */
  1158. create_device = true;
  1159. }
  1160. spin_lock_irqsave(&glink->idr_lock, flags);
  1161. ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
  1162. if (ret < 0) {
  1163. dev_err(glink->dev, "Unable to insert channel into rcid list\n");
  1164. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1165. goto free_channel;
  1166. }
  1167. channel->rcid = ret;
  1168. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1169. complete_all(&channel->open_req);
  1170. if (create_device) {
  1171. rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
  1172. if (!rpdev) {
  1173. ret = -ENOMEM;
  1174. goto rcid_remove;
  1175. }
  1176. rpdev->ept = &channel->ept;
  1177. strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
  1178. rpdev->src = RPMSG_ADDR_ANY;
  1179. rpdev->dst = RPMSG_ADDR_ANY;
  1180. rpdev->ops = &glink_device_ops;
  1181. node = qcom_glink_match_channel(glink->dev->of_node, name);
  1182. rpdev->dev.of_node = node;
  1183. rpdev->dev.parent = glink->dev;
  1184. rpdev->dev.release = qcom_glink_rpdev_release;
  1185. ret = rpmsg_register_device(rpdev);
  1186. if (ret)
  1187. goto rcid_remove;
  1188. channel->rpdev = rpdev;
  1189. }
  1190. return 0;
  1191. rcid_remove:
  1192. spin_lock_irqsave(&glink->idr_lock, flags);
  1193. idr_remove(&glink->rcids, channel->rcid);
  1194. channel->rcid = 0;
  1195. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1196. free_channel:
  1197. /* Release the reference, iff we took it */
  1198. if (create_device)
  1199. kref_put(&channel->refcount, qcom_glink_channel_release);
  1200. return ret;
  1201. }
  1202. static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
  1203. {
  1204. struct rpmsg_channel_info chinfo;
  1205. struct glink_channel *channel;
  1206. unsigned long flags;
  1207. spin_lock_irqsave(&glink->idr_lock, flags);
  1208. channel = idr_find(&glink->rcids, rcid);
  1209. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1210. if (WARN(!channel, "close request on unknown channel\n"))
  1211. return;
  1212. /* cancel pending rx_done work */
  1213. cancel_work_sync(&channel->intent_work);
  1214. if (channel->rpdev) {
  1215. strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
  1216. chinfo.src = RPMSG_ADDR_ANY;
  1217. chinfo.dst = RPMSG_ADDR_ANY;
  1218. rpmsg_unregister_device(glink->dev, &chinfo);
  1219. }
  1220. qcom_glink_send_close_ack(glink, channel->rcid);
  1221. spin_lock_irqsave(&glink->idr_lock, flags);
  1222. idr_remove(&glink->rcids, channel->rcid);
  1223. channel->rcid = 0;
  1224. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1225. kref_put(&channel->refcount, qcom_glink_channel_release);
  1226. }
  1227. static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
  1228. {
  1229. struct glink_channel *channel;
  1230. unsigned long flags;
  1231. spin_lock_irqsave(&glink->idr_lock, flags);
  1232. channel = idr_find(&glink->lcids, lcid);
  1233. if (WARN(!channel, "close ack on unknown channel\n")) {
  1234. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1235. return;
  1236. }
  1237. idr_remove(&glink->lcids, channel->lcid);
  1238. channel->lcid = 0;
  1239. spin_unlock_irqrestore(&glink->idr_lock, flags);
  1240. kref_put(&channel->refcount, qcom_glink_channel_release);
  1241. }
  1242. static void qcom_glink_work(struct work_struct *work)
  1243. {
  1244. struct qcom_glink *glink = container_of(work, struct qcom_glink,
  1245. rx_work);
  1246. struct glink_defer_cmd *dcmd;
  1247. struct glink_msg *msg;
  1248. unsigned long flags;
  1249. unsigned int param1;
  1250. unsigned int param2;
  1251. unsigned int cmd;
  1252. for (;;) {
  1253. spin_lock_irqsave(&glink->rx_lock, flags);
  1254. if (list_empty(&glink->rx_queue)) {
  1255. spin_unlock_irqrestore(&glink->rx_lock, flags);
  1256. break;
  1257. }
  1258. dcmd = list_first_entry(&glink->rx_queue,
  1259. struct glink_defer_cmd, node);
  1260. list_del(&dcmd->node);
  1261. spin_unlock_irqrestore(&glink->rx_lock, flags);
  1262. msg = &dcmd->msg;
  1263. cmd = le16_to_cpu(msg->cmd);
  1264. param1 = le16_to_cpu(msg->param1);
  1265. param2 = le32_to_cpu(msg->param2);
  1266. switch (cmd) {
  1267. case RPM_CMD_VERSION:
  1268. qcom_glink_receive_version(glink, param1, param2);
  1269. break;
  1270. case RPM_CMD_VERSION_ACK:
  1271. qcom_glink_receive_version_ack(glink, param1, param2);
  1272. break;
  1273. case RPM_CMD_OPEN:
  1274. qcom_glink_rx_open(glink, param1, msg->data);
  1275. break;
  1276. case RPM_CMD_CLOSE:
  1277. qcom_glink_rx_close(glink, param1);
  1278. break;
  1279. case RPM_CMD_CLOSE_ACK:
  1280. qcom_glink_rx_close_ack(glink, param1);
  1281. break;
  1282. case RPM_CMD_RX_INTENT_REQ:
  1283. qcom_glink_handle_intent_req(glink, param1, param2);
  1284. break;
  1285. default:
  1286. WARN(1, "Unknown defer object %d\n", cmd);
  1287. break;
  1288. }
  1289. kfree(dcmd);
  1290. }
  1291. }
  1292. static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
  1293. {
  1294. struct glink_defer_cmd *dcmd;
  1295. struct glink_defer_cmd *tmp;
  1296. /* cancel any pending deferred rx_work */
  1297. cancel_work_sync(&glink->rx_work);
  1298. list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
  1299. kfree(dcmd);
  1300. }
  1301. struct qcom_glink *qcom_glink_native_probe(struct device *dev,
  1302. unsigned long features,
  1303. struct qcom_glink_pipe *rx,
  1304. struct qcom_glink_pipe *tx,
  1305. bool intentless)
  1306. {
  1307. int irq;
  1308. int ret;
  1309. struct qcom_glink *glink;
  1310. glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
  1311. if (!glink)
  1312. return ERR_PTR(-ENOMEM);
  1313. glink->dev = dev;
  1314. glink->tx_pipe = tx;
  1315. glink->rx_pipe = rx;
  1316. glink->features = features;
  1317. glink->intentless = intentless;
  1318. spin_lock_init(&glink->tx_lock);
  1319. spin_lock_init(&glink->rx_lock);
  1320. INIT_LIST_HEAD(&glink->rx_queue);
  1321. INIT_WORK(&glink->rx_work, qcom_glink_work);
  1322. spin_lock_init(&glink->idr_lock);
  1323. idr_init(&glink->lcids);
  1324. idr_init(&glink->rcids);
  1325. ret = of_property_read_string(dev->of_node, "label", &glink->name);
  1326. if (ret < 0)
  1327. glink->name = dev->of_node->name;
  1328. glink->mbox_client.dev = dev;
  1329. glink->mbox_client.knows_txdone = true;
  1330. glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
  1331. if (IS_ERR(glink->mbox_chan)) {
  1332. if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
  1333. dev_err(dev, "failed to acquire IPC channel\n");
  1334. return ERR_CAST(glink->mbox_chan);
  1335. }
  1336. irq = of_irq_get(dev->of_node, 0);
  1337. ret = devm_request_irq(dev, irq,
  1338. qcom_glink_native_intr,
  1339. IRQF_NO_SUSPEND | IRQF_SHARED,
  1340. "glink-native", glink);
  1341. if (ret) {
  1342. dev_err(dev, "failed to request IRQ\n");
  1343. return ERR_PTR(ret);
  1344. }
  1345. glink->irq = irq;
  1346. ret = qcom_glink_send_version(glink);
  1347. if (ret)
  1348. return ERR_PTR(ret);
  1349. return glink;
  1350. }
  1351. EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
  1352. static int qcom_glink_remove_device(struct device *dev, void *data)
  1353. {
  1354. device_unregister(dev);
  1355. return 0;
  1356. }
  1357. void qcom_glink_native_remove(struct qcom_glink *glink)
  1358. {
  1359. struct glink_channel *channel;
  1360. int cid;
  1361. int ret;
  1362. disable_irq(glink->irq);
  1363. qcom_glink_cancel_rx_work(glink);
  1364. ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
  1365. if (ret)
  1366. dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
  1367. /* Release any defunct local channels, waiting for close-ack */
  1368. idr_for_each_entry(&glink->lcids, channel, cid)
  1369. kref_put(&channel->refcount, qcom_glink_channel_release);
  1370. /* Release any defunct local channels, waiting for close-req */
  1371. idr_for_each_entry(&glink->rcids, channel, cid)
  1372. kref_put(&channel->refcount, qcom_glink_channel_release);
  1373. idr_destroy(&glink->lcids);
  1374. idr_destroy(&glink->rcids);
  1375. mbox_free_channel(glink->mbox_chan);
  1376. }
  1377. EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
  1378. void qcom_glink_native_unregister(struct qcom_glink *glink)
  1379. {
  1380. device_unregister(glink->dev);
  1381. }
  1382. EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
  1383. MODULE_DESCRIPTION("Qualcomm GLINK driver");
  1384. MODULE_LICENSE("GPL v2");