qcom_glink_native.c 50 KB

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