qcom_glink_native.c 42 KB

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