qcom_glink_native.c 40 KB

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