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