qcom_glink_native.c 40 KB

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