qcom_glink_native.c 40 KB

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