diff options
author | Srinivas Kandagatla <srinivas.kandagatla@linaro.org> | 2016-07-20 11:56:01 +0100 |
---|---|---|
committer | Srinivas Kandagatla <srinivas.kandagatla@linaro.org> | 2016-07-20 11:56:01 +0100 |
commit | f4b0ba1a038d9be34a60ee86a134f96c3e035106 (patch) | |
tree | a415a1742af6ffc08ac644119aff7120fa2ac3f5 | |
parent | f0c85745724efff263faf8b297f3bb9cc2791040 (diff) | |
parent | 3918c4eea36ac4e7cc3795b6cbfcacb4703fca7a (diff) |
Merge branch 'tracking-qcomlt-glink' into integration-linux-qcomltqcomlt-v4.7-rc7
-rw-r--r-- | drivers/soc/qcom/Kconfig | 19 | ||||
-rw-r--r-- | drivers/soc/qcom/Makefile | 2 | ||||
-rw-r--r-- | drivers/soc/qcom/glink.c | 5797 | ||||
-rw-r--r-- | drivers/soc/qcom/glink_core_if.h | 213 | ||||
-rw-r--r-- | drivers/soc/qcom/glink_debugfs.c | 783 | ||||
-rw-r--r-- | drivers/soc/qcom/glink_private.h | 1044 | ||||
-rw-r--r-- | drivers/soc/qcom/glink_smem_native_xprt.c | 2500 | ||||
-rw-r--r-- | drivers/soc/qcom/glink_xprt_if.h | 201 | ||||
-rw-r--r-- | drivers/soc/qcom/smd-rpm.c | 103 | ||||
-rw-r--r-- | drivers/soc/qcom/smd.c | 177 | ||||
-rw-r--r-- | include/linux/ipc_logging.h | 290 | ||||
-rw-r--r-- | include/linux/soc/qcom/smd.h | 12 | ||||
-rw-r--r-- | include/soc/qcom/glink.h | 443 | ||||
-rw-r--r-- | include/soc/qcom/glink_rpm_xprt.h | 78 | ||||
-rw-r--r-- | include/soc/qcom/rpm-notifier.h | 63 | ||||
-rw-r--r-- | include/soc/qcom/rpm-smd.h | 309 | ||||
-rw-r--r-- | include/soc/qcom/tracer_pkt.h | 130 |
17 files changed, 12058 insertions, 106 deletions
diff --git a/drivers/soc/qcom/Kconfig b/drivers/soc/qcom/Kconfig index b77e2a8bad9b..d2c8783d32a7 100644 --- a/drivers/soc/qcom/Kconfig +++ b/drivers/soc/qcom/Kconfig @@ -49,6 +49,15 @@ config QCOM_SMD_RPM frequencies controlled by the RPM on these devices. Say M here if you want to include support for the Qualcomm RPM as a + +config MSM_GLINK + bool "Generic Link (G-Link)" + help + G-Link is a generic link transport that replaces SMD. It is used + within a System-on-Chip (SoC) for communication between both internal + processors and external peripherals. The actual physical transport + is handled by transport plug-ins that can be individually enabled and + configured separately. module. This will build a module called "qcom-smd-rpm". config QCOM_SMEM_STATE @@ -108,3 +117,13 @@ config BUS_TOPOLOGY_ADHOC directionality of connections by explicitly listing device connections thus avoiding illegal routes. +config MSM_GLINK_SMEM_NATIVE_XPRT + depends on QCOM_SMEM + depends on MSM_GLINK + bool "Generic Link (G-Link) SMEM Native Transport" + help + G-Link SMEM Native Transport is a G-Link Transport plug-in. It allows + G-Link communication to remote entities through a shared memory + physical transport. The nature of shared memory limits this G-Link + transport to only connecting with entities internal to the + System-on-Chip. diff --git a/drivers/soc/qcom/Makefile b/drivers/soc/qcom/Makefile index 844221134688..a335a2b00b92 100644 --- a/drivers/soc/qcom/Makefile +++ b/drivers/soc/qcom/Makefile @@ -10,3 +10,5 @@ obj-$(CONFIG_QCOM_WCNSS_CTRL) += wcnss_ctrl.o obj-$(CONFIG_MSM_BUS_SCALING) += msm_bus/ obj-$(CONFIG_BUS_TOPOLOGY_ADHOC) += msm_bus/ +obj-$(CONFIG_MSM_GLINK) += glink.o glink_debugfs.o +obj-$(CONFIG_MSM_GLINK_SMEM_NATIVE_XPRT) += glink_smem_native_xprt.o diff --git a/drivers/soc/qcom/glink.c b/drivers/soc/qcom/glink.c new file mode 100644 index 000000000000..ed92d8680acc --- /dev/null +++ b/drivers/soc/qcom/glink.c @@ -0,0 +1,5797 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <asm/arch_timer.h> +#include <linux/err.h> +#include <linux/ipc_logging.h> +#include <linux/list.h> +#include <linux/spinlock.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/workqueue.h> +#include <linux/rwsem.h> +#include <linux/pm_qos.h> +#include <soc/qcom/glink.h> +#include <soc/qcom/tracer_pkt.h> +#include "glink_core_if.h" +#include "glink_private.h" +#include "glink_xprt_if.h" + +/* Number of internal IPC Logging log pages */ +#define NUM_LOG_PAGES 10 +#define GLINK_PM_QOS_HOLDOFF_MS 10 +#define GLINK_QOS_DEF_NUM_TOKENS 10 +#define GLINK_QOS_DEF_NUM_PRIORITY 1 +#define GLINK_QOS_DEF_MTU 2048 + +/** + * struct glink_qos_priority_bin - Packet Scheduler's priority bucket + * @max_rate_kBps: Maximum rate supported by the priority bucket. + * @power_state: Transport power state for this priority bin. + * @tx_ready: List of channels ready for tx in the priority bucket. + * @active_ch_cnt: Active channels of this priority. + */ +struct glink_qos_priority_bin { + unsigned long max_rate_kBps; + uint32_t power_state; + struct list_head tx_ready; + uint32_t active_ch_cnt; +}; + +/** + * struct glink_core_xprt_ctx - transport representation structure + * @xprt_state_lhb0: controls read/write access to transport state + * @list_node: used to chain this transport in a global + * transport list + * @name: name of this transport + * @edge: what this transport connects to + * @id: the id to use for channel migration + * @versions: array of transport versions this implementation + * supports + * @versions_entries: number of entries in @versions + * @local_version_idx: local version index into @versions this + * transport is currently running + * @remote_version_idx: remote version index into @versions this + * transport is currently running + * @l_features: Features negotiated by the local side + * @capabilities: Capabilities of underlying transport + * @ops: transport defined implementation of common + * operations + * @local_state: value from local_channel_state_e representing + * the local state of this transport + * @remote_neg_completed: is the version negotiation with the remote end + * completed + * @xprt_ctx_lock_lhb1 lock to protect @next_lcid and @channels + * @next_lcid: logical channel identifier to assign to the next + * created channel + * @max_cid: maximum number of channel identifiers supported + * @max_iid: maximum number of intent identifiers supported + * @tx_work: work item to process @tx_ready + * @tx_wq: workqueue to run @tx_work + * @channels: list of all existing channels on this transport + * @mtu: MTU supported by this transport. + * @token_count: Number of tokens to be assigned per assignment. + * @curr_qos_rate_kBps: Aggregate of currently supported QoS requests. + * @threshold_rate_kBps: Maximum Rate allocated for QoS traffic. + * @num_priority: Number of priority buckets in the transport. + * @tx_ready_lock_lhb2: lock to protect @tx_ready + * @active_high_prio: Highest priority of active channels. + * @prio_bin: Pointer to priority buckets. + * @pm_qos_req: power management QoS request for TX path + * @qos_req_active: a vote is active with the PM QoS system + * @tx_path_activity: transmit activity has occurred + * @pm_qos_work: removes PM QoS vote due to inactivity + * @xprt_dbgfs_lock_lhb3: debugfs channel structure lock + * @log_ctx: IPC logging context for this transport. + */ +struct glink_core_xprt_ctx { + struct rwref_lock xprt_state_lhb0; + struct list_head list_node; + char name[GLINK_NAME_SIZE]; + char edge[GLINK_NAME_SIZE]; + uint16_t id; + const struct glink_core_version *versions; + size_t versions_entries; + uint32_t local_version_idx; + uint32_t remote_version_idx; + uint32_t l_features; + uint32_t capabilities; + struct glink_transport_if *ops; + enum transport_state_e local_state; + bool remote_neg_completed; + + spinlock_t xprt_ctx_lock_lhb1; + struct list_head channels; + uint32_t next_lcid; + struct list_head free_lcid_list; + + uint32_t max_cid; + uint32_t max_iid; + struct work_struct tx_work; + struct workqueue_struct *tx_wq; + + size_t mtu; + uint32_t token_count; + unsigned long curr_qos_rate_kBps; + unsigned long threshold_rate_kBps; + uint32_t num_priority; + spinlock_t tx_ready_lock_lhb2; + uint32_t active_high_prio; + struct glink_qos_priority_bin *prio_bin; + + struct pm_qos_request pm_qos_req; + bool qos_req_active; + bool tx_path_activity; + struct delayed_work pm_qos_work; + + struct mutex xprt_dbgfs_lock_lhb3; + void *log_ctx; +}; + +/** + * Channel Context + * @xprt_state_lhb0: controls read/write access to channel state + * @port_list_node: channel list node used by transport "channels" list + * @tx_ready_list_node: channels that have data ready to transmit + * @name: name of the channel + * + * @user_priv: user opaque data type passed into glink_open() + * @notify_rx: RX notification function + * @notify_tx_done: TX-done notification function (remote side is done) + * @notify_state: Channel state (connected / disconnected) notifications + * @notify_rx_intent_req: Request from remote side for an intent + * @notify_rxv: RX notification function (for io buffer chain) + * @notify_rx_sigs: RX signal change notification + * @notify_rx_abort: Channel close RX Intent aborted + * @notify_tx_abort: Channel close TX aborted + * @notify_rx_tracer_pkt: Receive notification for tracer packet + * @notify_remote_rx_intent: Receive notification for remote-queued RX intent + * + * @transport_ptr: Transport this channel uses + * @lcid: Local channel ID + * @rcid: Remote channel ID + * @local_open_state: Local channel state + * @remote_opened: Remote channel state (opened or closed) + * @int_req_ack: Remote side intent request ACK state + * @int_req_ack_complete: Intent tracking completion - received remote ACK + * @int_req_complete: Intent tracking completion - received intent + * @rx_intent_req_timeout_jiffies: Timeout for requesting an RX intent, in + * jiffies; if set to 0, timeout is infinite + * + * @local_rx_intent_lst_lock_lhc1: RX intent list lock + * @local_rx_intent_list: Active RX Intents queued by client + * @local_rx_intent_ntfy_list: Client notified, waiting for rx_done() + * @local_rx_intent_free_list: Available intent container structure + * + * @rmt_rx_intent_lst_lock_lhc2: Remote RX intent list lock + * @rmt_rx_intent_list: Remote RX intent list + * + * @max_used_liid: Maximum Local Intent ID used + * @dummy_riid: Dummy remote intent ID + * + * @tx_lists_lock_lhc3: TX list lock + * @tx_active: Ready to transmit + * + * @tx_pending_rmt_done_lock_lhc4: Remote-done list lock + * @tx_pending_remote_done: Transmitted, waiting for remote done + * @lsigs: Local signals + * @rsigs: Remote signals + * @pending_delete: waiting for channel to be deleted + * @no_migrate: The local client does not want to + * migrate transports + * @local_xprt_req: The transport the local side requested + * @local_xprt_resp: The response to @local_xprt_req + * @remote_xprt_req: The transport the remote side requested + * @remote_xprt_resp: The response to @remote_xprt_req + * @curr_priority: Channel's current priority. + * @initial_priority: Channel's initial priority. + * @token_count: Tokens for consumption by packet. + * @txd_len: Transmitted data size in the current + * token assignment cycle. + * @token_start_time: Time at which tokens are assigned. + * @req_rate_kBps: Current QoS request by the channel. + * @tx_intent_cnt: Intent count to transmit soon in future. + * @tx_cnt: Packets to be picked by tx scheduler. + */ +struct channel_ctx { + struct rwref_lock ch_state_lhc0; + struct list_head port_list_node; + struct list_head tx_ready_list_node; + char name[GLINK_NAME_SIZE]; + + /* user info */ + void *user_priv; + int (*notify_rx)(void *handle, const void *data, size_t size); + void (*notify_tx_done)(void *handle, const void *priv, + const void *pkt_priv, const void *ptr); + void (*notify_state)(void *handle, const void *priv, unsigned event); + bool (*notify_rx_intent_req)(void *handle, const void *priv, + size_t req_size); + void (*notify_rxv)(void *handle, const void *priv, const void *pkt_priv, + void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, + size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, + size_t *size)); + void (*notify_rx_sigs)(void *handle, const void *priv, + uint32_t old_sigs, uint32_t new_sigs); + void (*notify_rx_abort)(void *handle, const void *priv, + const void *pkt_priv); + void (*notify_tx_abort)(void *handle, const void *priv, + const void *pkt_priv); + void (*notify_rx_tracer_pkt)(void *handle, const void *priv, + const void *pkt_priv, const void *ptr, size_t size); + void (*notify_remote_rx_intent)(void *handle, const void *priv, + size_t size); + + /* internal port state */ + struct glink_core_xprt_ctx *transport_ptr; + uint32_t lcid; + uint32_t rcid; + enum local_channel_state_e local_open_state; + bool remote_opened; + bool int_req_ack; + struct completion int_req_ack_complete; + struct completion int_req_complete; + unsigned long rx_intent_req_timeout_jiffies; + + spinlock_t local_rx_intent_lst_lock_lhc1; + struct list_head local_rx_intent_list; + struct list_head local_rx_intent_ntfy_list; + struct list_head local_rx_intent_free_list; + + spinlock_t rmt_rx_intent_lst_lock_lhc2; + struct list_head rmt_rx_intent_list; + + uint32_t max_used_liid; + uint32_t dummy_riid; + + spinlock_t tx_lists_lock_lhc3; + struct list_head tx_active; + + spinlock_t tx_pending_rmt_done_lock_lhc4; + struct list_head tx_pending_remote_done; + + uint32_t lsigs; + uint32_t rsigs; + bool pending_delete; + + bool no_migrate; + uint16_t local_xprt_req; + uint16_t local_xprt_resp; + uint16_t remote_xprt_req; + uint16_t remote_xprt_resp; + + uint32_t curr_priority; + uint32_t initial_priority; + uint32_t token_count; + size_t txd_len; + unsigned long token_start_time; + unsigned long req_rate_kBps; + uint32_t tx_intent_cnt; + uint32_t tx_cnt; + void *drvdata; +}; + +static struct glink_core_if core_impl; +static void *log_ctx; +static unsigned glink_debug_mask = QCOM_GLINK_INFO; +module_param_named(debug_mask, glink_debug_mask, + uint, S_IRUGO | S_IWUSR | S_IWGRP); + +static unsigned glink_pm_qos; +module_param_named(pm_qos_enable, glink_pm_qos, + uint, S_IRUGO | S_IWUSR | S_IWGRP); + + +static LIST_HEAD(transport_list); + +/* + * Used while notifying the clients about link state events. Since the clients + * need to store the callback information temporarily and since all the + * existing accesses to transport list are in non-IRQ context, defining the + * transport_list_lock as a mutex. + */ +static DEFINE_MUTEX(transport_list_lock_lha0); + +struct link_state_notifier_info { + struct list_head list; + char transport[GLINK_NAME_SIZE]; + char edge[GLINK_NAME_SIZE]; + void (*glink_link_state_notif_cb)( + struct glink_link_state_cb_info *cb_info, void *priv); + void *priv; +}; +static LIST_HEAD(link_state_notifier_list); +static DEFINE_MUTEX(link_state_notifier_lock_lha1); + +static struct glink_core_xprt_ctx *find_open_transport(const char *edge, + const char *name, + bool initial_xprt, + uint16_t *best_id); + +static bool xprt_is_fully_opened(struct glink_core_xprt_ctx *xprt); + +static struct channel_ctx *xprt_lcid_to_ch_ctx_get( + struct glink_core_xprt_ctx *xprt_ctx, + uint32_t lcid); + +static struct channel_ctx *xprt_rcid_to_ch_ctx_get( + struct glink_core_xprt_ctx *xprt_ctx, + uint32_t rcid); + +static void xprt_schedule_tx(struct glink_core_xprt_ctx *xprt_ptr, + struct channel_ctx *ch_ptr, + struct glink_core_tx_pkt *tx_info); + +static int xprt_single_threaded_tx(struct glink_core_xprt_ctx *xprt_ptr, + struct channel_ctx *ch_ptr, + struct glink_core_tx_pkt *tx_info); + +static void tx_work_func(struct work_struct *work); + +static struct channel_ctx *ch_name_to_ch_ctx_create( + struct glink_core_xprt_ctx *xprt_ctx, + const char *name); + +static void ch_push_remote_rx_intent(struct channel_ctx *ctx, size_t size, + uint32_t riid); + +static int ch_pop_remote_rx_intent(struct channel_ctx *ctx, size_t size, + uint32_t *riid_ptr, size_t *intent_size); + +static struct glink_core_rx_intent *ch_push_local_rx_intent( + struct channel_ctx *ctx, const void *pkt_priv, size_t size); + +static void ch_remove_local_rx_intent(struct channel_ctx *ctx, uint32_t liid); + +static struct glink_core_rx_intent *ch_get_local_rx_intent( + struct channel_ctx *ctx, uint32_t liid); + +static void ch_set_local_rx_intent_notified(struct channel_ctx *ctx, + struct glink_core_rx_intent *intent_ptr); + +static struct glink_core_rx_intent *ch_get_local_rx_intent_notified( + struct channel_ctx *ctx, const void *ptr); + +static void ch_remove_local_rx_intent_notified(struct channel_ctx *ctx, + struct glink_core_rx_intent *liid_ptr, bool reuse); + +static struct glink_core_rx_intent *ch_get_free_local_rx_intent( + struct channel_ctx *ctx); + +static void ch_purge_intent_lists(struct channel_ctx *ctx); + +static void ch_add_rcid(struct glink_core_xprt_ctx *xprt_ctx, + struct channel_ctx *ctx, + uint32_t rcid); + +static bool ch_is_fully_opened(struct channel_ctx *ctx); +static bool ch_is_fully_closed(struct channel_ctx *ctx); + +struct glink_core_tx_pkt *ch_get_tx_pending_remote_done(struct channel_ctx *ctx, + uint32_t riid); + +static void ch_remove_tx_pending_remote_done(struct channel_ctx *ctx, + struct glink_core_tx_pkt *tx_pkt); + +static void glink_core_rx_cmd_rx_intent_req_ack(struct glink_transport_if + *if_ptr, uint32_t rcid, bool granted); + +static bool glink_core_remote_close_common(struct channel_ctx *ctx); + +static void check_link_notifier_and_notify(struct glink_core_xprt_ctx *xprt_ptr, + enum glink_link_state link_state); + +static void glink_core_channel_cleanup(struct glink_core_xprt_ctx *xprt_ptr); +static void glink_pm_qos_vote(struct glink_core_xprt_ctx *xprt_ptr); +static void glink_pm_qos_unvote(struct glink_core_xprt_ctx *xprt_ptr); +static void glink_pm_qos_cancel_worker(struct work_struct *work); +static bool ch_update_local_state(struct channel_ctx *ctx, + enum local_channel_state_e lstate); +static bool ch_update_rmt_state(struct channel_ctx *ctx, bool rstate); +static void glink_core_deinit_xprt_qos_cfg( + struct glink_core_xprt_ctx *xprt_ptr); + +#define glink_prio_to_power_state(xprt_ctx, priority) \ + ((xprt_ctx)->prio_bin[priority].power_state) + +#define GLINK_GET_CH_TX_STATE(ctx) \ + ((ctx)->tx_intent_cnt || (ctx)->tx_cnt) + +/** + * glink_ssr() - Clean up locally for SSR by simulating remote close + * @subsystem: The name of the subsystem being restarted + * + * Call into the transport using the ssr(if_ptr) function to allow it to + * clean up any necessary structures, then simulate a remote close from + * subsystem for all channels on that edge. + * + * Return: Standard error codes. + */ +int glink_ssr(const char *subsystem) +{ + int ret = 0; + bool transport_found = false; + struct glink_core_xprt_ctx *xprt_ctx = NULL; + struct channel_ctx *ch_ctx, *temp_ch_ctx; + uint32_t i; + unsigned long flags; + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt_ctx, &transport_list, list_node) { + if (!strcmp(subsystem, xprt_ctx->edge) && + xprt_is_fully_opened(xprt_ctx)) { + GLINK_INFO_XPRT(xprt_ctx, "%s: SSR\n", __func__); + spin_lock_irqsave(&xprt_ctx->tx_ready_lock_lhb2, + flags); + for (i = 0; i < xprt_ctx->num_priority; i++) + list_for_each_entry_safe(ch_ctx, temp_ch_ctx, + &xprt_ctx->prio_bin[i].tx_ready, + tx_ready_list_node) + list_del_init( + &ch_ctx->tx_ready_list_node); + spin_unlock_irqrestore(&xprt_ctx->tx_ready_lock_lhb2, + flags); + + xprt_ctx->ops->ssr(xprt_ctx->ops); + transport_found = true; + } + } + mutex_unlock(&transport_list_lock_lha0); + + if (!transport_found) + ret = -ENODEV; + + return ret; +} +EXPORT_SYMBOL(glink_ssr); + +/** + * glink_core_ch_close_ack_common() - handles the common operations during + * close ack. + * @ctx: Pointer to channel instance. + * + * Return: True if the channel is fully closed after the state change, + * false otherwise. + */ +static bool glink_core_ch_close_ack_common(struct channel_ctx *ctx) +{ + bool is_fully_closed; + + if (ctx == NULL) + return false; + is_fully_closed = ch_update_local_state(ctx, GLINK_CHANNEL_CLOSED); + GLINK_INFO_PERF_CH(ctx, + "%s: local:GLINK_CHANNEL_CLOSING->GLINK_CHANNEL_CLOSED\n", + __func__); + + if (ctx->notify_state) { + ctx->notify_state(ctx, ctx->user_priv, + GLINK_LOCAL_DISCONNECTED); + ch_purge_intent_lists(ctx); + GLINK_INFO_PERF_CH(ctx, + "%s: notify state: GLINK_LOCAL_DISCONNECTED\n", + __func__); + } + + return is_fully_closed; +} + +/** + * glink_core_remote_close_common() - Handles the common operations during + * a remote close. + * @ctx: Pointer to channel instance. + * + * Return: True if the channel is fully closed after the state change, + * false otherwise. + */ +static bool glink_core_remote_close_common(struct channel_ctx *ctx) +{ + bool is_fully_closed; + + if (ctx == NULL) + return false; + is_fully_closed = ch_update_rmt_state(ctx, false); + ctx->rcid = 0; + + if (ctx->local_open_state != GLINK_CHANNEL_CLOSED && + ctx->local_open_state != GLINK_CHANNEL_CLOSING) { + if (ctx->notify_state) + ctx->notify_state(ctx, ctx->user_priv, + GLINK_REMOTE_DISCONNECTED); + GLINK_INFO_CH(ctx, + "%s: %s: GLINK_REMOTE_DISCONNECTED\n", + __func__, "notify state"); + } + + if (ctx->local_open_state == GLINK_CHANNEL_CLOSED) + GLINK_INFO_CH(ctx, + "%s: %s, %s\n", __func__, + "Did not send GLINK_REMOTE_DISCONNECTED", + "local state is already CLOSED"); + + ctx->int_req_ack = false; + complete_all(&ctx->int_req_ack_complete); + complete_all(&ctx->int_req_complete); + ch_purge_intent_lists(ctx); + + return is_fully_closed; +} + +/** + * glink_qos_calc_rate_kBps() - Calculate the transmit rate in kBps + * @pkt_size: Worst case packet size per transmission. + * @interval_us: Packet transmit interval in us. + * + * This function is used to calculate the rate of transmission rate of + * a channel in kBps. + * + * Return: Transmission rate in kBps. + */ +static unsigned long glink_qos_calc_rate_kBps(size_t pkt_size, + unsigned long interval_us) +{ + unsigned long rate_kBps, rem; + + rate_kBps = pkt_size * USEC_PER_SEC; + rem = do_div(rate_kBps, (interval_us * 1024)); + return rate_kBps; +} + +/** + * glink_qos_check_feasibility() - Feasibility test on a QoS Request + * @xprt_ctx: Transport in which the QoS request is made. + * @req_rate_kBps: QoS Request. + * + * This function is used to perform the schedulability test on a QoS request + * over a specific transport. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_check_feasibility(struct glink_core_xprt_ctx *xprt_ctx, + unsigned long req_rate_kBps) +{ + unsigned long new_rate_kBps; + + if (xprt_ctx->num_priority == GLINK_QOS_DEF_NUM_PRIORITY) + return -EOPNOTSUPP; + + new_rate_kBps = xprt_ctx->curr_qos_rate_kBps + req_rate_kBps; + if (new_rate_kBps > xprt_ctx->threshold_rate_kBps) { + GLINK_ERR_XPRT(xprt_ctx, + "New_rate(%lu + %lu) > threshold_rate(%lu)\n", + xprt_ctx->curr_qos_rate_kBps, req_rate_kBps, + xprt_ctx->threshold_rate_kBps); + return -EBUSY; + } + return 0; +} + +/** + * glink_qos_update_ch_prio() - Update the channel priority + * @ctx: Channel context whose priority is updated. + * @new_priority: New priority of the channel. + * + * This function is called to update the channel priority during QoS request, + * QoS Cancel or Priority evaluation by packet scheduler. This function must + * be called with transport's tx_ready_lock_lhb2 lock and channel's + * tx_lists_lock_lhc3 locked. + */ +static void glink_qos_update_ch_prio(struct channel_ctx *ctx, + uint32_t new_priority) +{ + uint32_t old_priority; + + if (unlikely(!ctx)) + return; + + old_priority = ctx->curr_priority; + if (!list_empty(&ctx->tx_ready_list_node)) { + ctx->transport_ptr->prio_bin[old_priority].active_ch_cnt--; + list_move(&ctx->tx_ready_list_node, + &ctx->transport_ptr->prio_bin[new_priority].tx_ready); + ctx->transport_ptr->prio_bin[new_priority].active_ch_cnt++; + } + ctx->curr_priority = new_priority; +} + +/** + * glink_qos_assign_priority() - Assign priority to a channel + * @ctx: Channel for which the priority has to be assigned. + * @req_rate_kBps: QoS request by the channel. + * + * This function is used to assign a priority to the channel depending on its + * QoS Request. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_assign_priority(struct channel_ctx *ctx, + unsigned long req_rate_kBps) +{ + int ret; + uint32_t i; + unsigned long flags; + + spin_lock_irqsave(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + if (ctx->req_rate_kBps) { + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, + flags); + GLINK_ERR_CH(ctx, "%s: QoS Request already exists\n", __func__); + return -EINVAL; + } + + ret = glink_qos_check_feasibility(ctx->transport_ptr, req_rate_kBps); + if (ret < 0) { + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, + flags); + return ret; + } + + spin_lock(&ctx->tx_lists_lock_lhc3); + i = ctx->transport_ptr->num_priority - 1; + while (i > 0 && + ctx->transport_ptr->prio_bin[i-1].max_rate_kBps >= req_rate_kBps) + i--; + + ctx->initial_priority = i; + glink_qos_update_ch_prio(ctx, i); + ctx->req_rate_kBps = req_rate_kBps; + if (i > 0) { + ctx->transport_ptr->curr_qos_rate_kBps += req_rate_kBps; + ctx->token_count = ctx->transport_ptr->token_count; + ctx->txd_len = 0; + ctx->token_start_time = arch_counter_get_cntpct(); + } + spin_unlock(&ctx->tx_lists_lock_lhc3); + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + return 0; +} + +/** + * glink_qos_reset_priority() - Reset the channel priority + * @ctx: Channel for which the priority is reset. + * + * This function is used to reset the channel priority when the QoS request + * is cancelled by the channel. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_reset_priority(struct channel_ctx *ctx) +{ + unsigned long flags; + + spin_lock_irqsave(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + spin_lock(&ctx->tx_lists_lock_lhc3); + if (ctx->initial_priority > 0) { + ctx->initial_priority = 0; + glink_qos_update_ch_prio(ctx, 0); + ctx->transport_ptr->curr_qos_rate_kBps -= ctx->req_rate_kBps; + ctx->txd_len = 0; + ctx->req_rate_kBps = 0; + } + spin_unlock(&ctx->tx_lists_lock_lhc3); + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + return 0; +} + +/** + * glink_qos_ch_vote_xprt() - Vote the transport that channel is active + * @ctx: Channel context which is active. + * + * This function is called to vote for the transport either when the channel + * is transmitting or when it shows an intention to transmit sooner. This + * function must be called with transport's tx_ready_lock_lhb2 lock and + * channel's tx_lists_lock_lhc3 locked. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_ch_vote_xprt(struct channel_ctx *ctx) +{ + uint32_t prio; + + if (unlikely(!ctx || !ctx->transport_ptr)) + return -EINVAL; + + prio = ctx->curr_priority; + ctx->transport_ptr->prio_bin[prio].active_ch_cnt++; + + if (ctx->transport_ptr->prio_bin[prio].active_ch_cnt == 1 && + ctx->transport_ptr->active_high_prio < prio) { + /* + * One active channel in this priority and this is the + * highest active priority bucket + */ + ctx->transport_ptr->active_high_prio = prio; + return ctx->transport_ptr->ops->power_vote( + ctx->transport_ptr->ops, + glink_prio_to_power_state(ctx->transport_ptr, + prio)); + } + return 0; +} + +/** + * glink_qos_ch_unvote_xprt() - Unvote the transport when channel is inactive + * @ctx: Channel context which is inactive. + * + * This function is called to unvote for the transport either when all the + * packets queued by the channel are transmitted by the scheduler. This + * function must be called with transport's tx_ready_lock_lhb2 lock and + * channel's tx_lists_lock_lhc3 locked. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_ch_unvote_xprt(struct channel_ctx *ctx) +{ + uint32_t prio; + + if (unlikely(!ctx || !ctx->transport_ptr)) + return -EINVAL; + + prio = ctx->curr_priority; + ctx->transport_ptr->prio_bin[prio].active_ch_cnt--; + + if (ctx->transport_ptr->prio_bin[prio].active_ch_cnt || + ctx->transport_ptr->active_high_prio > prio) + return 0; + + /* + * No active channel in this priority and this is the + * highest active priority bucket + */ + while (prio > 0) { + prio--; + if (!ctx->transport_ptr->prio_bin[prio].active_ch_cnt) + continue; + + ctx->transport_ptr->active_high_prio = prio; + return ctx->transport_ptr->ops->power_vote( + ctx->transport_ptr->ops, + glink_prio_to_power_state(ctx->transport_ptr, + prio)); + } + return ctx->transport_ptr->ops->power_unvote(ctx->transport_ptr->ops); +} + +/** + * glink_qos_add_ch_tx_intent() - Add the channel's intention to transmit soon + * @ctx: Channel context which is going to be active. + * + * This function is called to update the channel state when it is intending to + * transmit sooner. This function must be called with transport's + * tx_ready_lock_lhb2 lock and channel's tx_lists_lock_lhc3 locked. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_add_ch_tx_intent(struct channel_ctx *ctx) +{ + bool active_tx; + + if (unlikely(!ctx)) + return -EINVAL; + + active_tx = GLINK_GET_CH_TX_STATE(ctx); + ctx->tx_intent_cnt++; + if (!active_tx) + glink_qos_ch_vote_xprt(ctx); + return 0; +} + +/** + * glink_qos_do_ch_tx() - Update the channel's state that it is transmitting + * @ctx: Channel context which is transmitting. + * + * This function is called to update the channel state when it is queueing a + * packet to transmit. This function must be called with transport's + * tx_ready_lock_lhb2 lock and channel's tx_lists_lock_lhc3 locked. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_do_ch_tx(struct channel_ctx *ctx) +{ + bool active_tx; + + if (unlikely(!ctx)) + return -EINVAL; + + active_tx = GLINK_GET_CH_TX_STATE(ctx); + ctx->tx_cnt++; + if (ctx->tx_intent_cnt) + ctx->tx_intent_cnt--; + if (!active_tx) + glink_qos_ch_vote_xprt(ctx); + return 0; +} + +/** + * glink_qos_done_ch_tx() - Update the channel's state when transmission is done + * @ctx: Channel context for which all packets are transmitted. + * + * This function is called to update the channel state when all packets in its + * transmit queue are successfully transmitted. This function must be called + * with transport's tx_ready_lock_lhb2 lock and channel's tx_lists_lock_lhc3 + * locked. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_qos_done_ch_tx(struct channel_ctx *ctx) +{ + bool active_tx; + + if (unlikely(!ctx)) + return -EINVAL; + + WARN_ON(ctx->tx_cnt == 0); + ctx->tx_cnt = 0; + active_tx = GLINK_GET_CH_TX_STATE(ctx); + if (!active_tx) + glink_qos_ch_unvote_xprt(ctx); + return 0; +} + +/** + * tx_linear_vbuf_provider() - Virtual Buffer Provider for linear buffers + * @iovec: Pointer to the beginning of the linear buffer. + * @offset: Offset into the buffer whose address is needed. + * @size: Pointer to hold the length of the contiguous buffer space. + * + * This function is used when a linear buffer is transmitted. + * + * Return: Address of the buffer which is at offset "offset" from the beginning + * of the buffer. + */ +static void *tx_linear_vbuf_provider(void *iovec, size_t offset, size_t *size) +{ + struct glink_core_tx_pkt *tx_info = (struct glink_core_tx_pkt *)iovec; + + if (unlikely(!iovec || !size)) + return NULL; + + if (offset >= tx_info->size) + return NULL; + + if (unlikely(OVERFLOW_ADD_UNSIGNED(void *, tx_info->data, offset))) + return NULL; + + *size = tx_info->size - offset; + + return (void *)tx_info->data + offset; +} + +/** + * linearize_vector() - Linearize the vector buffer + * @iovec: Pointer to the vector buffer. + * @size: Size of data in the vector buffer. + * vbuf_provider: Virtual address-space Buffer Provider for the vector. + * pbuf_provider: Physical address-space Buffer Provider for the vector. + * + * This function is used to linearize the vector buffer provided by the + * transport when the client has registered to receive only the vector + * buffer. + * + * Return: address of the linear buffer on success, NULL on failure. + */ +static void *linearize_vector(void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, size_t *buf_size), + void * (*pbuf_provider)(void *iovec, size_t offset, size_t *buf_size)) +{ + void *bounce_buf; + void *pdata; + void *vdata; + size_t data_size; + size_t offset = 0; + + bounce_buf = kmalloc(size, GFP_KERNEL); + if (!bounce_buf) + return ERR_PTR(-ENOMEM); + + do { + if (vbuf_provider) { + vdata = vbuf_provider(iovec, offset, &data_size); + } else { + pdata = pbuf_provider(iovec, offset, &data_size); + vdata = phys_to_virt((unsigned long)pdata); + } + + if (!vdata) + break; + + if (OVERFLOW_ADD_UNSIGNED(size_t, data_size, offset)) { + GLINK_ERR("%s: overflow data_size %zu + offset %zu\n", + __func__, data_size, offset); + goto err; + } + + memcpy(bounce_buf + offset, vdata, data_size); + offset += data_size; + } while (offset < size); + + if (offset != size) { + GLINK_ERR("%s: Error size_copied %zu != total_size %zu\n", + __func__, offset, size); + goto err; + } + return bounce_buf; + +err: + kfree(bounce_buf); + return NULL; +} + +/** + * xprt_lcid_to_ch_ctx_get() - lookup a channel by local id + * @xprt_ctx: Transport to search for a matching channel. + * @lcid: Local channel identifier corresponding to the desired channel. + * + * If the channel is found, the reference count is incremented to ensure the + * lifetime of the channel context. The caller must call rwref_put() when done. + * + * Return: The channel corresponding to @lcid or NULL if a matching channel + * is not found. + */ +static struct channel_ctx *xprt_lcid_to_ch_ctx_get( + struct glink_core_xprt_ctx *xprt_ctx, + uint32_t lcid) +{ + struct channel_ctx *entry; + unsigned long flags; + + spin_lock_irqsave(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + list_for_each_entry(entry, &xprt_ctx->channels, port_list_node) + if (entry->lcid == lcid) { + rwref_get(&entry->ch_state_lhc0); + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, + flags); + return entry; + } + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + + return NULL; +} + +/** + * xprt_rcid_to_ch_ctx_get() - lookup a channel by remote id + * @xprt_ctx: Transport to search for a matching channel. + * @rcid: Remote channel identifier corresponding to the desired channel. + * + * If the channel is found, the reference count is incremented to ensure the + * lifetime of the channel context. The caller must call rwref_put() when done. + * + * Return: The channel corresponding to @rcid or NULL if a matching channel + * is not found. + */ +static struct channel_ctx *xprt_rcid_to_ch_ctx_get( + struct glink_core_xprt_ctx *xprt_ctx, + uint32_t rcid) +{ + struct channel_ctx *entry; + unsigned long flags; + + spin_lock_irqsave(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + list_for_each_entry(entry, &xprt_ctx->channels, port_list_node) + if (entry->rcid == rcid) { + rwref_get(&entry->ch_state_lhc0); + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, + flags); + return entry; + } + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + + return NULL; +} + +/** + * ch_check_duplicate_riid() - Checks for duplicate riid + * @ctx: Local channel context + * @riid: Remote intent ID + * + * This functions check the riid is present in the remote_rx_list or not + */ +bool ch_check_duplicate_riid(struct channel_ctx *ctx, int riid) +{ + struct glink_core_rx_intent *intent; + unsigned long flags; + + spin_lock_irqsave(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + list_for_each_entry(intent, &ctx->rmt_rx_intent_list, list) { + if (riid == intent->id) { + spin_unlock_irqrestore( + &ctx->rmt_rx_intent_lst_lock_lhc2, flags); + return true; + } + } + spin_unlock_irqrestore(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + return false; +} + +/** + * ch_pop_remote_rx_intent() - Finds a matching RX intent + * @ctx: Local channel context + * @size: Size of Intent + * @riid_ptr: Pointer to return value of remote intent ID + * + * This functions searches for an RX intent that is >= to the requested size. + */ +int ch_pop_remote_rx_intent(struct channel_ctx *ctx, size_t size, + uint32_t *riid_ptr, size_t *intent_size) +{ + struct glink_core_rx_intent *intent; + struct glink_core_rx_intent *intent_tmp; + unsigned long flags; + + if (GLINK_MAX_PKT_SIZE < size) { + GLINK_ERR_CH(ctx, "%s: R[]:%zu Invalid size.\n", __func__, + size); + return -EINVAL; + } + + if (riid_ptr == NULL) + return -EINVAL; + + *riid_ptr = 0; + spin_lock_irqsave(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + if (ctx->transport_ptr->capabilities & GCAP_INTENTLESS) { + *riid_ptr = ++ctx->dummy_riid; + spin_unlock_irqrestore(&ctx->rmt_rx_intent_lst_lock_lhc2, + flags); + return 0; + } + list_for_each_entry_safe(intent, intent_tmp, &ctx->rmt_rx_intent_list, + list) { + if (intent->intent_size >= size) { + list_del(&intent->list); + GLINK_DBG_CH(ctx, + "%s: R[%u]:%zu Removed remote intent\n", + __func__, + intent->id, + intent->intent_size); + *riid_ptr = intent->id; + *intent_size = intent->intent_size; + kfree(intent); + spin_unlock_irqrestore( + &ctx->rmt_rx_intent_lst_lock_lhc2, flags); + return 0; + } + } + spin_unlock_irqrestore(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + return -EAGAIN; +} + +/** + * ch_push_remote_rx_intent() - Registers a remote RX intent + * @ctx: Local channel context + * @size: Size of Intent + * @riid: Remote intent ID + * + * This functions adds a remote RX intent to the remote RX intent list. + */ +void ch_push_remote_rx_intent(struct channel_ctx *ctx, size_t size, + uint32_t riid) +{ + struct glink_core_rx_intent *intent; + unsigned long flags; + gfp_t gfp_flag; + + if (GLINK_MAX_PKT_SIZE < size) { + GLINK_ERR_CH(ctx, "%s: R[%u]:%zu Invalid size.\n", __func__, + riid, size); + return; + } + + if (ch_check_duplicate_riid(ctx, riid)) { + GLINK_ERR_CH(ctx, "%s: R[%d]:%zu Duplicate RIID found\n", + __func__, riid, size); + return; + } + + gfp_flag = (ctx->transport_ptr->capabilities & GCAP_AUTO_QUEUE_RX_INT) ? + GFP_ATOMIC : GFP_KERNEL; + intent = kzalloc(sizeof(struct glink_core_rx_intent), gfp_flag); + if (!intent) { + GLINK_ERR_CH(ctx, + "%s: R[%u]:%zu Memory allocation for intent failed\n", + __func__, riid, size); + return; + } + intent->id = riid; + intent->intent_size = size; + + spin_lock_irqsave(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + list_add_tail(&intent->list, &ctx->rmt_rx_intent_list); + + complete_all(&ctx->int_req_complete); + if (ctx->notify_remote_rx_intent) + ctx->notify_remote_rx_intent(ctx, ctx->user_priv, size); + spin_unlock_irqrestore(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + + GLINK_DBG_CH(ctx, "%s: R[%u]:%zu Pushed remote intent\n", __func__, + intent->id, + intent->intent_size); +} + +/** + * ch_push_local_rx_intent() - Create an rx_intent + * @ctx: Local channel context + * @pkt_priv: Opaque private pointer provided by client to be returned later + * @size: Size of intent + * + * This functions creates a local intent and adds it to the local + * intent list. + */ +struct glink_core_rx_intent *ch_push_local_rx_intent(struct channel_ctx *ctx, + const void *pkt_priv, size_t size) +{ + struct glink_core_rx_intent *intent; + unsigned long flags; + int ret; + + if (GLINK_MAX_PKT_SIZE < size) { + GLINK_ERR_CH(ctx, + "%s: L[]:%zu Invalid size\n", __func__, size); + return NULL; + } + + intent = ch_get_free_local_rx_intent(ctx); + if (!intent) { + if (ctx->max_used_liid >= ctx->transport_ptr->max_iid) { + GLINK_ERR_CH(ctx, + "%s: All intents are in USE max_iid[%d]", + __func__, ctx->transport_ptr->max_iid); + return NULL; + } + + intent = kzalloc(sizeof(struct glink_core_rx_intent), + GFP_KERNEL); + if (!intent) { + GLINK_ERR_CH(ctx, + "%s: Memory Allocation for local rx_intent failed", + __func__); + return NULL; + } + intent->id = ++ctx->max_used_liid; + } + + /* transport is responsible for allocating/reserving for the intent */ + ret = ctx->transport_ptr->ops->allocate_rx_intent( + ctx->transport_ptr->ops, size, intent); + if (ret < 0) { + /* intent data allocation failure */ + GLINK_ERR_CH(ctx, "%s: unable to allocate intent sz[%zu] %d", + __func__, size, ret); + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_add_tail(&intent->list, + &ctx->local_rx_intent_free_list); + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, + flags); + return NULL; + } + + intent->pkt_priv = pkt_priv; + intent->intent_size = size; + intent->write_offset = 0; + intent->pkt_size = 0; + intent->bounce_buf = NULL; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_add_tail(&intent->list, &ctx->local_rx_intent_list); + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_DBG_CH(ctx, "%s: L[%u]:%zu Pushed intent\n", __func__, + intent->id, + intent->intent_size); + return intent; +} + +/** + * ch_remove_local_rx_intent() - Find and remove RX Intent from list + * @ctx: Local channel context + * @liid: Local channel Intent ID + * + * This functions parses the local intent list for a specific channel + * and checks for the intent using the intent ID. If found, the intent + * is deleted from the list. + */ +void ch_remove_local_rx_intent(struct channel_ctx *ctx, uint32_t liid) +{ + struct glink_core_rx_intent *intent, *tmp_intent; + unsigned long flags; + + if (ctx->transport_ptr->max_iid < liid) { + GLINK_ERR_CH(ctx, "%s: L[%u] Invalid ID.\n", __func__, + liid); + return; + } + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry_safe(intent, tmp_intent, &ctx->local_rx_intent_list, + list) { + if (liid == intent->id) { + list_del(&intent->list); + list_add_tail(&intent->list, + &ctx->local_rx_intent_free_list); + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, + flags); + GLINK_DBG_CH(ctx, + "%s: L[%u]:%zu moved intent to Free/unused list\n", + __func__, + intent->id, + intent->intent_size); + return; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_ERR_CH(ctx, "%s: L[%u] Intent not found.\n", __func__, + liid); +} + +/** + * ch_get_dummy_rx_intent() - Get a dummy rx_intent + * @ctx: Local channel context + * @liid: Local channel Intent ID + * + * This functions parses the local intent list for a specific channel and + * returns either a matching intent or allocates a dummy one if no matching + * intents can be found. + * + * Return: Pointer to the intent if intent is found else NULL + */ +struct glink_core_rx_intent *ch_get_dummy_rx_intent(struct channel_ctx *ctx, + uint32_t liid) +{ + struct glink_core_rx_intent *intent; + unsigned long flags; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + if (!list_empty(&ctx->local_rx_intent_list)) { + intent = list_first_entry(&ctx->local_rx_intent_list, + struct glink_core_rx_intent, list); + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, + flags); + return intent; + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + + intent = ch_get_free_local_rx_intent(ctx); + if (!intent) { + intent = kzalloc(sizeof(struct glink_core_rx_intent), + GFP_ATOMIC); + if (!intent) { + GLINK_ERR_CH(ctx, + "%s: Memory Allocation for local rx_intent failed", + __func__); + return NULL; + } + intent->id = ++ctx->max_used_liid; + } + intent->intent_size = 0; + intent->write_offset = 0; + intent->pkt_size = 0; + intent->bounce_buf = NULL; + intent->pkt_priv = NULL; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_add_tail(&intent->list, &ctx->local_rx_intent_list); + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_DBG_CH(ctx, "%s: L[%u]:%zu Pushed intent\n", __func__, + intent->id, + intent->intent_size); + return intent; +} + +/** + * ch_get_local_rx_intent() - Search for an rx_intent + * @ctx: Local channel context + * @liid: Local channel Intent ID + * + * This functions parses the local intent list for a specific channel + * and checks for the intent using the intent ID. If found, pointer to + * the intent is returned. + * + * Return: Pointer to the intent if intent is found else NULL + */ +struct glink_core_rx_intent *ch_get_local_rx_intent(struct channel_ctx *ctx, + uint32_t liid) +{ + struct glink_core_rx_intent *intent; + unsigned long flags; + + if (ctx->transport_ptr->max_iid < liid) { + GLINK_ERR_CH(ctx, "%s: L[%u] Invalid ID.\n", __func__, + liid); + return NULL; + } + + if (ctx->transport_ptr->capabilities & GCAP_INTENTLESS) + return ch_get_dummy_rx_intent(ctx, liid); + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry(intent, &ctx->local_rx_intent_list, list) { + if (liid == intent->id) { + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, flags); + return intent; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_ERR_CH(ctx, "%s: L[%u] Intent not found.\n", __func__, + liid); + return NULL; +} + +/** + * ch_set_local_rx_intent_notified() - Add a rx intent to local intent + * notified list + * @ctx: Local channel context + * @intent_ptr: Pointer to the local intent + * + * This functions parses the local intent list for a specific channel + * and checks for the intent. If found, the function deletes the intent + * from local_rx_intent list and adds it to local_rx_intent_notified list. + */ +void ch_set_local_rx_intent_notified(struct channel_ctx *ctx, + struct glink_core_rx_intent *intent_ptr) +{ + struct glink_core_rx_intent *tmp_intent, *intent; + unsigned long flags; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry_safe(intent, tmp_intent, &ctx->local_rx_intent_list, + list) { + if (intent == intent_ptr) { + list_del(&intent->list); + list_add_tail(&intent->list, + &ctx->local_rx_intent_ntfy_list); + GLINK_DBG_CH(ctx, + "%s: L[%u]:%zu Moved intent %s", + __func__, + intent_ptr->id, + intent_ptr->intent_size, + "from local to notify list\n"); + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, + flags); + return; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_ERR_CH(ctx, "%s: L[%u] Intent not found.\n", __func__, + intent_ptr->id); +} + +/** + * ch_get_local_rx_intent_notified() - Find rx intent in local notified list + * @ctx: Local channel context + * @ptr: Pointer to the rx intent + * + * This functions parses the local intent notify list for a specific channel + * and checks for the intent. + * + * Return: Pointer to the intent if intent is found else NULL. + */ +struct glink_core_rx_intent *ch_get_local_rx_intent_notified( + struct channel_ctx *ctx, const void *ptr) +{ + struct glink_core_rx_intent *ptr_intent; + unsigned long flags; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry(ptr_intent, &ctx->local_rx_intent_ntfy_list, + list) { + if (ptr_intent->data == ptr || ptr_intent->iovec == ptr || + ptr_intent->bounce_buf == ptr) { + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, + flags); + return ptr_intent; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_ERR_CH(ctx, "%s: Local intent not found\n", __func__); + return NULL; +} + +/** + * ch_remove_local_rx_intent_notified() - Remove a rx intent in local intent + * notified list + * @ctx: Local channel context + * @ptr: Pointer to the rx intent + * @reuse: Reuse the rx intent + * + * This functions parses the local intent notify list for a specific channel + * and checks for the intent. If found, the function deletes the intent + * from local_rx_intent_notified list and adds it to local_rx_intent_free list. + */ +void ch_remove_local_rx_intent_notified(struct channel_ctx *ctx, + struct glink_core_rx_intent *liid_ptr, bool reuse) +{ + struct glink_core_rx_intent *ptr_intent, *tmp_intent; + unsigned long flags; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry_safe(ptr_intent, tmp_intent, + &ctx->local_rx_intent_ntfy_list, list) { + if (ptr_intent == liid_ptr) { + list_del(&ptr_intent->list); + GLINK_DBG_CH(ctx, + "%s: L[%u]:%zu Removed intent from notify list\n", + __func__, + ptr_intent->id, + ptr_intent->intent_size); + kfree(ptr_intent->bounce_buf); + ptr_intent->bounce_buf = NULL; + ptr_intent->write_offset = 0; + ptr_intent->pkt_size = 0; + if (reuse) + list_add_tail(&ptr_intent->list, + &ctx->local_rx_intent_list); + else + list_add_tail(&ptr_intent->list, + &ctx->local_rx_intent_free_list); + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, + flags); + return; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + GLINK_ERR_CH(ctx, "%s: L[%u] Intent not found.\n", __func__, + liid_ptr->id); +} + +/** + * ch_get_free_local_rx_intent() - Return a rx intent in local intent + * free list + * @ctx: Local channel context + * + * This functions parses the local_rx_intent_free list for a specific channel + * and checks for the free unused intent. If found, the function returns + * the free intent pointer else NULL pointer. + */ +struct glink_core_rx_intent *ch_get_free_local_rx_intent( + struct channel_ctx *ctx) +{ + struct glink_core_rx_intent *ptr_intent = NULL; + unsigned long flags; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + if (!list_empty(&ctx->local_rx_intent_free_list)) { + ptr_intent = list_first_entry(&ctx->local_rx_intent_free_list, + struct glink_core_rx_intent, + list); + list_del(&ptr_intent->list); + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + return ptr_intent; +} + +/** + * ch_purge_intent_lists() - Remove all intents for a channel + * + * @ctx: Local channel context + * + * This functions parses the local intent lists for a specific channel and + * removes and frees all intents. + */ +void ch_purge_intent_lists(struct channel_ctx *ctx) +{ + struct glink_core_rx_intent *ptr_intent, *tmp_intent; + struct glink_core_tx_pkt *tx_info, *tx_info_temp; + unsigned long flags; + + spin_lock_irqsave(&ctx->tx_lists_lock_lhc3, flags); + list_for_each_entry_safe(tx_info, tx_info_temp, &ctx->tx_active, + list_node) { + ctx->notify_tx_abort(ctx, ctx->user_priv, + tx_info->pkt_priv); + rwref_put(&tx_info->pkt_ref); + } + spin_unlock_irqrestore(&ctx->tx_lists_lock_lhc3, flags); + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry_safe(ptr_intent, tmp_intent, + &ctx->local_rx_intent_list, list) { + ctx->notify_rx_abort(ctx, ctx->user_priv, + ptr_intent->pkt_priv); + list_del(&ptr_intent->list); + kfree(ptr_intent); + } + + if (!list_empty(&ctx->local_rx_intent_ntfy_list)) + /* + * The client is still processing an rx_notify() call and has + * not yet called glink_rx_done() to return the pointer to us. + * glink_rx_done() will do the appropriate cleanup when this + * call occurs, but log a message here just for internal state + * tracking. + */ + GLINK_INFO_CH(ctx, "%s: waiting on glink_rx_done()\n", + __func__); + + list_for_each_entry_safe(ptr_intent, tmp_intent, + &ctx->local_rx_intent_free_list, list) { + list_del(&ptr_intent->list); + kfree(ptr_intent); + } + ctx->max_used_liid = 0; + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + + spin_lock_irqsave(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); + list_for_each_entry_safe(ptr_intent, tmp_intent, + &ctx->rmt_rx_intent_list, list) { + list_del(&ptr_intent->list); + kfree(ptr_intent); + } + spin_unlock_irqrestore(&ctx->rmt_rx_intent_lst_lock_lhc2, flags); +} + +/** + * ch_get_tx_pending_remote_done() - Lookup for a packet that is waiting for + * the remote-done notification. + * @ctx: Pointer to the channel context + * @riid: riid of transmit packet + * + * This function adds a packet to the tx_pending_remote_done list. + * + * The tx_lists_lock_lhc3 lock needs to be held while calling this function. + * + * Return: Pointer to the tx packet + */ +struct glink_core_tx_pkt *ch_get_tx_pending_remote_done( + struct channel_ctx *ctx, uint32_t riid) +{ + struct glink_core_tx_pkt *tx_pkt; + unsigned long flags; + + if (!ctx) { + GLINK_ERR("%s: Invalid context pointer", __func__); + return NULL; + } + + spin_lock_irqsave(&ctx->tx_pending_rmt_done_lock_lhc4, flags); + list_for_each_entry(tx_pkt, &ctx->tx_pending_remote_done, list_done) { + if (tx_pkt->riid == riid) { + if (tx_pkt->size_remaining) { + GLINK_ERR_CH(ctx, "%s: R[%u] TX not complete", + __func__, riid); + tx_pkt = NULL; + } + spin_unlock_irqrestore( + &ctx->tx_pending_rmt_done_lock_lhc4, flags); + return tx_pkt; + } + } + spin_unlock_irqrestore(&ctx->tx_pending_rmt_done_lock_lhc4, flags); + + GLINK_ERR_CH(ctx, "%s: R[%u] Tx packet for intent not found.\n", + __func__, riid); + return NULL; +} + +/** + * ch_remove_tx_pending_remote_done() - Removes a packet transmit context for a + * packet that is waiting for the remote-done notification + * @ctx: Pointer to the channel context + * @tx_pkt: Pointer to the transmit packet + * + * This function parses through tx_pending_remote_done and removes a + * packet that matches with the tx_pkt. + */ +void ch_remove_tx_pending_remote_done(struct channel_ctx *ctx, + struct glink_core_tx_pkt *tx_pkt) +{ + struct glink_core_tx_pkt *local_tx_pkt, *tmp_tx_pkt; + unsigned long flags; + + if (!ctx || !tx_pkt) { + GLINK_ERR("%s: Invalid input", __func__); + return; + } + + spin_lock_irqsave(&ctx->tx_pending_rmt_done_lock_lhc4, flags); + list_for_each_entry_safe(local_tx_pkt, tmp_tx_pkt, + &ctx->tx_pending_remote_done, list_done) { + if (tx_pkt == local_tx_pkt) { + list_del_init(&tx_pkt->list_done); + GLINK_DBG_CH(ctx, + "%s: R[%u] Removed Tx packet for intent\n", + __func__, + tx_pkt->riid); + rwref_put(&tx_pkt->pkt_ref); + spin_unlock_irqrestore( + &ctx->tx_pending_rmt_done_lock_lhc4, flags); + return; + } + } + spin_unlock_irqrestore(&ctx->tx_pending_rmt_done_lock_lhc4, flags); + + GLINK_ERR_CH(ctx, "%s: R[%u] Tx packet for intent not found", __func__, + tx_pkt->riid); +} + +/** + * glink_add_free_lcid_list() - adds the lcid of a to be deleted channel to + * available lcid list + * @ctx: Pointer to channel context. + */ +static void glink_add_free_lcid_list(struct channel_ctx *ctx) +{ + struct channel_lcid *free_lcid; + unsigned long flags; + + free_lcid = kzalloc(sizeof(*free_lcid), GFP_KERNEL); + if (!free_lcid) { + GLINK_ERR( + "%s: allocation failed on xprt:edge [%s:%s] for lcid [%d]\n", + __func__, ctx->transport_ptr->name, + ctx->transport_ptr->edge, ctx->lcid); + return; + } + free_lcid->lcid = ctx->lcid; + spin_lock_irqsave(&ctx->transport_ptr->xprt_ctx_lock_lhb1, flags); + list_add_tail(&free_lcid->list_node, + &ctx->transport_ptr->free_lcid_list); + spin_unlock_irqrestore(&ctx->transport_ptr->xprt_ctx_lock_lhb1, + flags); +} + +/** + * glink_ch_ctx_release - Free the channel context + * @ch_st_lock: handle to the rwref_lock associated with the chanel + * + * This should only be called when the reference count associated with the + * channel goes to zero. + */ +static void glink_ch_ctx_release(struct rwref_lock *ch_st_lock) +{ + struct channel_ctx *ctx = container_of(ch_st_lock, struct channel_ctx, + ch_state_lhc0); + ctx->transport_ptr = NULL; + kfree(ctx); + GLINK_INFO("%s: freed the channel ctx in pid [%d]\n", __func__, + current->pid); + ctx = NULL; +} + +/** + * ch_name_to_ch_ctx_create() - lookup a channel by name, create the channel if + * it is not found. + * @xprt_ctx: Transport to search for a matching channel. + * @name: Name of the desired channel. + * + * Return: The channel corresponding to @name, NULL if a matching channel was + * not found AND a new channel could not be created. + */ +static struct channel_ctx *ch_name_to_ch_ctx_create( + struct glink_core_xprt_ctx *xprt_ctx, + const char *name) +{ + struct channel_ctx *entry; + struct channel_ctx *ctx; + struct channel_ctx *temp; + unsigned long flags; + struct channel_lcid *flcid; + + ctx = kzalloc(sizeof(struct channel_ctx), GFP_KERNEL); + if (!ctx) { + GLINK_ERR_XPRT(xprt_ctx, "%s: Failed to allocated ctx, %s", + "checking if there is one existing\n", + __func__); + goto check_ctx; + } + + ctx->local_open_state = GLINK_CHANNEL_CLOSED; + strlcpy(ctx->name, name, GLINK_NAME_SIZE); + rwref_lock_init(&ctx->ch_state_lhc0, glink_ch_ctx_release); + INIT_LIST_HEAD(&ctx->tx_ready_list_node); + init_completion(&ctx->int_req_ack_complete); + init_completion(&ctx->int_req_complete); + INIT_LIST_HEAD(&ctx->local_rx_intent_list); + INIT_LIST_HEAD(&ctx->local_rx_intent_ntfy_list); + INIT_LIST_HEAD(&ctx->local_rx_intent_free_list); + spin_lock_init(&ctx->local_rx_intent_lst_lock_lhc1); + INIT_LIST_HEAD(&ctx->rmt_rx_intent_list); + spin_lock_init(&ctx->rmt_rx_intent_lst_lock_lhc2); + INIT_LIST_HEAD(&ctx->tx_active); + spin_lock_init(&ctx->tx_pending_rmt_done_lock_lhc4); + INIT_LIST_HEAD(&ctx->tx_pending_remote_done); + spin_lock_init(&ctx->tx_lists_lock_lhc3); + +check_ctx: + rwref_write_get(&xprt_ctx->xprt_state_lhb0); + if (xprt_ctx->local_state != GLINK_XPRT_OPENED) { + kfree(ctx); + rwref_write_put(&xprt_ctx->xprt_state_lhb0); + return NULL; + } + spin_lock_irqsave(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + list_for_each_entry_safe(entry, temp, &xprt_ctx->channels, + port_list_node) + if (!strcmp(entry->name, name) && !entry->pending_delete) { + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, + flags); + kfree(ctx); + rwref_write_put(&xprt_ctx->xprt_state_lhb0); + return entry; + } + + if (ctx) { + if (list_empty(&xprt_ctx->free_lcid_list)) { + if (xprt_ctx->next_lcid > xprt_ctx->max_cid) { + /* no more channels available */ + GLINK_ERR_XPRT(xprt_ctx, + "%s: unable to exceed %u channels\n", + __func__, xprt_ctx->max_cid); + spin_unlock_irqrestore( + &xprt_ctx->xprt_ctx_lock_lhb1, + flags); + kfree(ctx); + rwref_write_put(&xprt_ctx->xprt_state_lhb0); + return NULL; + } else { + ctx->lcid = xprt_ctx->next_lcid++; + } + } else { + flcid = list_first_entry(&xprt_ctx->free_lcid_list, + struct channel_lcid, list_node); + ctx->lcid = flcid->lcid; + list_del(&flcid->list_node); + kfree(flcid); + } + + list_add_tail(&ctx->port_list_node, &xprt_ctx->channels); + + GLINK_INFO_PERF_CH_XPRT(ctx, xprt_ctx, + "%s: local:GLINK_CHANNEL_CLOSED\n", + __func__); + } + spin_unlock_irqrestore(&xprt_ctx->xprt_ctx_lock_lhb1, flags); + rwref_write_put(&xprt_ctx->xprt_state_lhb0); + mutex_lock(&xprt_ctx->xprt_dbgfs_lock_lhb3); + if (ctx != NULL) + glink_debugfs_add_channel(ctx, xprt_ctx); + mutex_unlock(&xprt_ctx->xprt_dbgfs_lock_lhb3); + return ctx; +} + +/** + * ch_add_rcid() - add a remote channel identifier to an existing channel + * @xprt_ctx: Transport the channel resides on. + * @ctx: Channel receiving the identifier. + * @rcid: The remote channel identifier. + */ +static void ch_add_rcid(struct glink_core_xprt_ctx *xprt_ctx, + struct channel_ctx *ctx, + uint32_t rcid) +{ + ctx->rcid = rcid; +} + +/** + * ch_update_local_state() - Update the local channel state + * @ctx: Pointer to channel context. + * @lstate: Local channel state. + * + * Return: True if the channel is fully closed as a result of this update, + * false otherwise. + */ +static bool ch_update_local_state(struct channel_ctx *ctx, + enum local_channel_state_e lstate) +{ + bool is_fully_closed; + + rwref_write_get(&ctx->ch_state_lhc0); + ctx->local_open_state = lstate; + is_fully_closed = ch_is_fully_closed(ctx); + rwref_write_put(&ctx->ch_state_lhc0); + + return is_fully_closed; +} + +/** + * ch_update_local_state() - Update the local channel state + * @ctx: Pointer to channel context. + * @rstate: Remote Channel state. + * + * Return: True if the channel is fully closed as result of this update, + * false otherwise. + */ +static bool ch_update_rmt_state(struct channel_ctx *ctx, bool rstate) +{ + bool is_fully_closed; + + rwref_write_get(&ctx->ch_state_lhc0); + ctx->remote_opened = rstate; + is_fully_closed = ch_is_fully_closed(ctx); + rwref_write_put(&ctx->ch_state_lhc0); + + return is_fully_closed; +} + +/* + * ch_is_fully_opened() - Verify if a channel is open + * ctx: Pointer to channel context + * + * Return: True if open, else flase + */ +static bool ch_is_fully_opened(struct channel_ctx *ctx) +{ + if (ctx->remote_opened && ctx->local_open_state == GLINK_CHANNEL_OPENED) + return true; + + return false; +} + +/* + * ch_is_fully_closed() - Verify if a channel is closed on both sides + * @ctx: Pointer to channel context + * @returns: True if open, else flase + */ +static bool ch_is_fully_closed(struct channel_ctx *ctx) +{ + if (!ctx->remote_opened && + ctx->local_open_state == GLINK_CHANNEL_CLOSED) + return true; + + return false; +} + +/** + * find_open_transport() - find a specific open transport + * @edge: Edge the transport is on. + * @name: Name of the transport (or NULL if no preference) + * @initial_xprt: The specified transport is the start for migration + * @best_id: The best transport found for this connection + * + * Find an open transport corresponding to the specified @name and @edge. @edge + * is expected to be valid. @name is expected to be NULL (unspecified) or + * valid. If @name is not specified, then the best transport found on the + * specified edge will be returned. + * + * Return: Transport with the specified name on the specified edge, if open. + * NULL if the transport exists, but is not fully open. ENODEV if no such + * transport exists. + */ +static struct glink_core_xprt_ctx *find_open_transport(const char *edge, + const char *name, + bool initial_xprt, + uint16_t *best_id) +{ + struct glink_core_xprt_ctx *xprt; + struct glink_core_xprt_ctx *best_xprt = NULL; + struct glink_core_xprt_ctx *ret; + bool first = true; + + ret = (struct glink_core_xprt_ctx *)ERR_PTR(-ENODEV); + *best_id = USHRT_MAX; + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt, &transport_list, list_node) { + if (strcmp(edge, xprt->edge)) + continue; + if (first) { + first = false; + ret = NULL; + } + if (!xprt_is_fully_opened(xprt)) + continue; + + if (xprt->id < *best_id) { + *best_id = xprt->id; + best_xprt = xprt; + } + + /* + * Braces are required in this instacne because the else will + * attach to the wrong if otherwise. + */ + if (name) { + if (!strcmp(name, xprt->name)) + ret = xprt; + } else { + ret = best_xprt; + } + } + + mutex_unlock(&transport_list_lock_lha0); + + if (IS_ERR_OR_NULL(ret)) + return ret; + if (!initial_xprt) + *best_id = ret->id; + + return ret; +} + +/** + * xprt_is_fully_opened() - check the open status of a transport + * @xprt: Transport being checked. + * + * Return: True if the transport is fully opened, false otherwise. + */ +static bool xprt_is_fully_opened(struct glink_core_xprt_ctx *xprt) +{ + if (xprt->remote_neg_completed && + xprt->local_state == GLINK_XPRT_OPENED) + return true; + + return false; +} + +/** + * glink_dummy_notify_rx_intent_req() - Dummy RX Request + * + * @handle: Channel handle (ignored) + * @priv: Private data pointer (ignored) + * @req_size: Requested size (ignored) + * + * Dummy RX intent request if client does not implement the optional callback + * function. + * + * Return: False + */ +static bool glink_dummy_notify_rx_intent_req(void *handle, const void *priv, + size_t req_size) +{ + return false; +} + +/** + * glink_dummy_notify_rx_sigs() - Dummy signal callback + * + * @handle: Channel handle (ignored) + * @priv: Private data pointer (ignored) + * @req_size: Requested size (ignored) + * + * Dummy signal callback if client does not implement the optional callback + * function. + * + * Return: False + */ +static void glink_dummy_notify_rx_sigs(void *handle, const void *priv, + uint32_t old_sigs, uint32_t new_sigs) +{ + /* intentionally left blank */ +} + +/** + * glink_dummy_rx_abort() - Dummy rx abort callback + * + * handle: Channel handle (ignored) + * priv: Private data pointer (ignored) + * pkt_priv: Private intent data pointer (ignored) + * + * Dummy rx abort callback if client does not implement the optional callback + * function. + */ +static void glink_dummy_notify_rx_abort(void *handle, const void *priv, + const void *pkt_priv) +{ + /* intentionally left blank */ +} + +/** + * glink_dummy_tx_abort() - Dummy tx abort callback + * + * @handle: Channel handle (ignored) + * @priv: Private data pointer (ignored) + * @pkt_priv: Private intent data pointer (ignored) + * + * Dummy tx abort callback if client does not implement the optional callback + * function. + */ +static void glink_dummy_notify_tx_abort(void *handle, const void *priv, + const void *pkt_priv) +{ + /* intentionally left blank */ +} + +/** + * dummy_poll() - a dummy poll() for transports that don't define one + * @if_ptr: The transport interface handle for this transport. + * @lcid: The channel to poll. + * + * Return: An error to indicate that this operation is unsupported. + */ +static int dummy_poll(struct glink_transport_if *if_ptr, uint32_t lcid) +{ + return -EOPNOTSUPP; +} + +/** + * dummy_reuse_rx_intent() - a dummy reuse_rx_intent() for transports that + * don't define one + * @if_ptr: The transport interface handle for this transport. + * @intent: The intent to reuse. + * + * Return: Success. + */ +static int dummy_reuse_rx_intent(struct glink_transport_if *if_ptr, + struct glink_core_rx_intent *intent) +{ + return 0; +} + +/** + * dummy_mask_rx_irq() - a dummy mask_rx_irq() for transports that don't define + * one + * @if_ptr: The transport interface handle for this transport. + * @lcid: The local channel id for this channel. + * @mask: True to mask the irq, false to unmask. + * @pstruct: Platform defined structure with data necessary for masking. + * + * Return: An error to indicate that this operation is unsupported. + */ +static int dummy_mask_rx_irq(struct glink_transport_if *if_ptr, uint32_t lcid, + bool mask, void *pstruct) +{ + return -EOPNOTSUPP; +} + +/** + * dummy_wait_link_down() - a dummy wait_link_down() for transports that don't + * define one + * @if_ptr: The transport interface handle for this transport. + * + * Return: An error to indicate that this operation is unsupported. + */ +static int dummy_wait_link_down(struct glink_transport_if *if_ptr) +{ + return -EOPNOTSUPP; +} + +/** + * dummy_allocate_rx_intent() - a dummy RX intent allocation function that does + * not allocate anything + * @if_ptr: The transport the intent is associated with. + * @size: Size of intent. + * @intent: Pointer to the intent structure. + * + * Return: Success. + */ +static int dummy_allocate_rx_intent(struct glink_transport_if *if_ptr, + size_t size, struct glink_core_rx_intent *intent) +{ + return 0; +} + +/** + * dummy_tx_cmd_tracer_pkt() - a dummy tracer packet tx cmd for transports + * that don't define one + * @if_ptr: The transport interface handle for this transport. + * @lcid: The channel in which the tracer packet is transmitted. + * @pctx: Context of the packet to be transmitted. + * + * Return: 0. + */ +static int dummy_tx_cmd_tracer_pkt(struct glink_transport_if *if_ptr, + uint32_t lcid, struct glink_core_tx_pkt *pctx) +{ + pctx->size_remaining = 0; + return 0; +} + +/** + * dummy_deallocate_rx_intent() - a dummy rx intent deallocation function that + * does not deallocate anything + * @if_ptr: The transport the intent is associated with. + * @intent: Pointer to the intent structure. + * + * Return: Success. + */ +static int dummy_deallocate_rx_intent(struct glink_transport_if *if_ptr, + struct glink_core_rx_intent *intent) +{ + return 0; +} + +/** + * dummy_tx_cmd_local_rx_intent() - dummy local rx intent request + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @size: The intent size to encode. + * @liid: The local intent id to encode. + * + * Return: Success. + */ +static int dummy_tx_cmd_local_rx_intent(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size, uint32_t liid) +{ + return 0; +} + +/** + * dummy_tx_cmd_local_rx_done() - dummy rx done command + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @liid: The local intent id to encode. + * @reuse: Reuse the consumed intent. + */ +static void dummy_tx_cmd_local_rx_done(struct glink_transport_if *if_ptr, + uint32_t lcid, uint32_t liid, bool reuse) +{ + /* intentionally left blank */ +} + +/** + * dummy_tx() - dummy tx() that does not send anything + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @pctx: The data to encode. + * + * Return: Number of bytes written i.e. zero. + */ +static int dummy_tx(struct glink_transport_if *if_ptr, uint32_t lcid, + struct glink_core_tx_pkt *pctx) +{ + return 0; +} + +/** + * dummy_tx_cmd_rx_intent_req() - dummy rx intent request functon + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @size: The requested intent size to encode. + * + * Return: Success. + */ +static int dummy_tx_cmd_rx_intent_req(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size) +{ + return 0; +} + +/** + * dummy_tx_cmd_rx_intent_req_ack() - dummy rx intent request ack + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @granted: The request response to encode. + * + * Return: Success. + */ +static int dummy_tx_cmd_remote_rx_intent_req_ack( + struct glink_transport_if *if_ptr, + uint32_t lcid, bool granted) +{ + return 0; +} + +/** + * dummy_tx_cmd_set_sigs() - dummy signals ack transmit function + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @sigs: The signals to encode. + * + * Return: Success. + */ +static int dummy_tx_cmd_set_sigs(struct glink_transport_if *if_ptr, + uint32_t lcid, uint32_t sigs) +{ + return 0; +} + +/** + * dummy_tx_cmd_ch_close() - dummy channel close transmit function + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * + * Return: Success. + */ +static int dummy_tx_cmd_ch_close(struct glink_transport_if *if_ptr, + uint32_t lcid) +{ + return 0; +} + +/** + * dummy_tx_cmd_ch_remote_close_ack() - dummy channel close ack sending function + * @if_ptr: The transport to transmit on. + * @rcid: The remote channel id to encode. + */ +static void dummy_tx_cmd_ch_remote_close_ack(struct glink_transport_if *if_ptr, + uint32_t rcid) +{ + /* intentionally left blank */ +} + +/** + * dummy_get_power_vote_ramp_time() - Dummy Power vote ramp time + * @if_ptr: The transport to transmit on. + * @state: The power state being requested from the transport. + */ +static unsigned long dummy_get_power_vote_ramp_time( + struct glink_transport_if *if_ptr, uint32_t state) +{ + return (unsigned long)-EOPNOTSUPP; +} + +/** + * dummy_power_vote() - Dummy Power vote operation + * @if_ptr: The transport to transmit on. + * @state: The power state being requested from the transport. + */ +static int dummy_power_vote(struct glink_transport_if *if_ptr, + uint32_t state) +{ + return -EOPNOTSUPP; +} + +/** + * dummy_power_unvote() - Dummy Power unvote operation + * @if_ptr: The transport to transmit on. + */ +static int dummy_power_unvote(struct glink_transport_if *if_ptr) +{ + return -EOPNOTSUPP; +} + +/** + * notif_if_up_all_xprts() - Check and notify existing transport state if up + * @notif_info: Data structure containing transport information to be notified. + * + * This function is called when the client registers a notifier to know about + * the state of a transport. This function matches the existing transports with + * the transport in the "notif_info" parameter. When a matching transport is + * found, the callback function in the "notif_info" parameter is called with + * the state of the matching transport. + * + * If an edge or transport is not defined, then all edges and/or transports + * will be matched and will receive up notifications. + */ +static void notif_if_up_all_xprts( + struct link_state_notifier_info *notif_info) +{ + struct glink_core_xprt_ctx *xprt_ptr; + struct glink_link_state_cb_info cb_info; + + cb_info.link_state = GLINK_LINK_STATE_UP; + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt_ptr, &transport_list, list_node) { + if (strlen(notif_info->edge) && + strcmp(notif_info->edge, xprt_ptr->edge)) + continue; + + if (strlen(notif_info->transport) && + strcmp(notif_info->transport, xprt_ptr->name)) + continue; + + if (!xprt_is_fully_opened(xprt_ptr)) + continue; + + cb_info.transport = xprt_ptr->name; + cb_info.edge = xprt_ptr->edge; + notif_info->glink_link_state_notif_cb(&cb_info, + notif_info->priv); + } + mutex_unlock(&transport_list_lock_lha0); +} + +/** + * check_link_notifier_and_notify() - Check and notify clients about link state + * @xprt_ptr: Transport whose state to be notified. + * @link_state: State of the transport to be notified. + * + * This function is called when the state of the transport changes. This + * function matches the transport with the clients that have registered to + * be notified about the state changes. When a matching client notifier is + * found, the callback function in the client notifier is called with the + * new state of the transport. + */ +static void check_link_notifier_and_notify(struct glink_core_xprt_ctx *xprt_ptr, + enum glink_link_state link_state) +{ + struct link_state_notifier_info *notif_info; + struct glink_link_state_cb_info cb_info; + + cb_info.link_state = link_state; + mutex_lock(&link_state_notifier_lock_lha1); + list_for_each_entry(notif_info, &link_state_notifier_list, list) { + if (strlen(notif_info->edge) && + strcmp(notif_info->edge, xprt_ptr->edge)) + continue; + + if (strlen(notif_info->transport) && + strcmp(notif_info->transport, xprt_ptr->name)) + continue; + + cb_info.transport = xprt_ptr->name; + cb_info.edge = xprt_ptr->edge; + notif_info->glink_link_state_notif_cb(&cb_info, + notif_info->priv); + } + mutex_unlock(&link_state_notifier_lock_lha1); +} + +/** + * Open GLINK channel. + * + * @cfg_ptr: Open configuration structure (the structure is copied before + * glink_open returns). All unused fields should be zero-filled. + * + * This should not be called from link state callback context by clients. + * It is recommended that client should invoke this function from their own + * thread. + * + * Return: Pointer to channel on success, PTR_ERR() with standard Linux + * error code on failure. + */ +void *glink_open(const struct glink_open_config *cfg) +{ + struct channel_ctx *ctx = NULL; + struct glink_core_xprt_ctx *transport_ptr; + size_t len; + int ret; + uint16_t best_id; + + if (!cfg->edge || !cfg->name) { + GLINK_ERR("%s: !cfg->edge || !cfg->name\n", __func__); + return ERR_PTR(-EINVAL); + } + + len = strlen(cfg->edge); + if (len == 0 || len >= GLINK_NAME_SIZE) { + GLINK_ERR("%s: [EDGE] len == 0 || len >= GLINK_NAME_SIZE\n", + __func__); + return ERR_PTR(-EINVAL); + } + + len = strlen(cfg->name); + if (len == 0 || len >= GLINK_NAME_SIZE) { + GLINK_ERR("%s: [NAME] len == 0 || len >= GLINK_NAME_SIZE\n", + __func__); + return ERR_PTR(-EINVAL); + } + + if (cfg->transport) { + len = strlen(cfg->transport); + if (len == 0 || len >= GLINK_NAME_SIZE) { + GLINK_ERR("%s: [TRANSPORT] len == 0 || %s\n", + __func__, + "len >= GLINK_NAME_SIZE"); + return ERR_PTR(-EINVAL); + } + } + + /* confirm required notification parameters */ + if (!(cfg->notify_rx || cfg->notify_rxv) || !cfg->notify_tx_done + || !cfg->notify_state + || ((cfg->options & GLINK_OPT_RX_INTENT_NOTIF) + && !cfg->notify_remote_rx_intent)) { + GLINK_ERR("%s: Incorrect notification parameters\n", __func__); + return ERR_PTR(-EINVAL); + } + + /* find transport */ + transport_ptr = find_open_transport(cfg->edge, cfg->transport, + cfg->options & GLINK_OPT_INITIAL_XPORT, + &best_id); + if (IS_ERR_OR_NULL(transport_ptr)) { + GLINK_ERR("%s:%s %s: Error %d - unable to find transport\n", + cfg->transport, cfg->edge, __func__, + (unsigned)PTR_ERR(transport_ptr)); + return ERR_PTR(-ENODEV); + } + + /* + * look for an existing port structure which can occur in + * reopen and remote-open-first cases + */ + ctx = ch_name_to_ch_ctx_create(transport_ptr, cfg->name); + if (ctx == NULL) { + GLINK_ERR("%s:%s %s: Error - unable to allocate new channel\n", + cfg->transport, cfg->edge, __func__); + return ERR_PTR(-ENOMEM); + } + + /* port already exists */ + if (ctx->local_open_state != GLINK_CHANNEL_CLOSED) { + /* not ready to be re-opened */ + GLINK_INFO_CH_XPRT(ctx, transport_ptr, + "%s: Channel not ready to be re-opened. State: %u\n", + __func__, ctx->local_open_state); + return ERR_PTR(-EBUSY); + } + + /* initialize port structure */ + ctx->user_priv = cfg->priv; + ctx->rx_intent_req_timeout_jiffies = + msecs_to_jiffies(cfg->rx_intent_req_timeout_ms); + ctx->notify_rx = cfg->notify_rx; + ctx->notify_tx_done = cfg->notify_tx_done; + ctx->notify_state = cfg->notify_state; + ctx->notify_rx_intent_req = cfg->notify_rx_intent_req; + ctx->notify_rxv = cfg->notify_rxv; + ctx->notify_rx_sigs = cfg->notify_rx_sigs; + ctx->notify_rx_abort = cfg->notify_rx_abort; + ctx->notify_tx_abort = cfg->notify_tx_abort; + ctx->notify_rx_tracer_pkt = cfg->notify_rx_tracer_pkt; + ctx->notify_remote_rx_intent = cfg->notify_remote_rx_intent; + + if (!ctx->notify_rx_intent_req) + ctx->notify_rx_intent_req = glink_dummy_notify_rx_intent_req; + if (!ctx->notify_rx_sigs) + ctx->notify_rx_sigs = glink_dummy_notify_rx_sigs; + if (!ctx->notify_rx_abort) + ctx->notify_rx_abort = glink_dummy_notify_rx_abort; + if (!ctx->notify_tx_abort) + ctx->notify_tx_abort = glink_dummy_notify_tx_abort; + + if (!ctx->rx_intent_req_timeout_jiffies) + ctx->rx_intent_req_timeout_jiffies = MAX_SCHEDULE_TIMEOUT; + + ctx->local_xprt_req = best_id; + ctx->no_migrate = cfg->transport && + !(cfg->options & GLINK_OPT_INITIAL_XPORT); + ctx->transport_ptr = transport_ptr; + ctx->local_open_state = GLINK_CHANNEL_OPENING; + GLINK_INFO_PERF_CH(ctx, + "%s: local:GLINK_CHANNEL_CLOSED->GLINK_CHANNEL_OPENING\n", + __func__); + + /* start local-open sequence */ + ret = ctx->transport_ptr->ops->tx_cmd_ch_open(ctx->transport_ptr->ops, + ctx->lcid, cfg->name, best_id); + if (ret) { + /* failure to send open command (transport failure) */ + ctx->local_open_state = GLINK_CHANNEL_CLOSED; + GLINK_ERR_CH(ctx, "%s: Unable to send open command %d\n", + __func__, ret); + return ERR_PTR(ret); + } + + GLINK_INFO_CH(ctx, "%s: Created channel, sent OPEN command. ctx %p\n", + __func__, ctx); + + return ctx; +} +EXPORT_SYMBOL(glink_open); + +/** + * glink_get_channel_id_for_handle() - Get logical channel ID + * + * @handle: handle of channel + * + * Used internally by G-Link debugfs. + * + * Return: Logical Channel ID or standard Linux error code + */ +int glink_get_channel_id_for_handle(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + if (ctx == NULL) + return -EINVAL; + + return ctx->lcid; +} +EXPORT_SYMBOL(glink_get_channel_id_for_handle); + +/** + * glink_get_channel_name_for_handle() - return channel name + * + * @handle: handle of channel + * + * Used internally by G-Link debugfs. + * + * Return: Channel name or NULL + */ +char *glink_get_channel_name_for_handle(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + if (ctx == NULL) + return NULL; + + return ctx->name; +} +EXPORT_SYMBOL(glink_get_channel_name_for_handle); + +/** + * glink_delete_ch_from_list() - delete the channel from the list + * @ctx: Pointer to channel context. + * @add_flcid: Boolean value to decide whether the lcid should be added or not. + * + * This function deletes the channel from the list along with the debugfs + * information associated with it. It also adds the channel lcid to the free + * lcid list except if the channel is deleted in case of ssr/unregister case. + * It can only called when channel is fully closed. + */ +static void glink_delete_ch_from_list(struct channel_ctx *ctx, bool add_flcid) +{ + unsigned long flags; + spin_lock_irqsave(&ctx->transport_ptr->xprt_ctx_lock_lhb1, + flags); + if (!list_empty(&ctx->port_list_node)) + list_del_init(&ctx->port_list_node); + spin_unlock_irqrestore( + &ctx->transport_ptr->xprt_ctx_lock_lhb1, + flags); + if (add_flcid) + glink_add_free_lcid_list(ctx); + mutex_lock(&ctx->transport_ptr->xprt_dbgfs_lock_lhb3); + glink_debugfs_remove_channel(ctx, ctx->transport_ptr); + mutex_unlock(&ctx->transport_ptr->xprt_dbgfs_lock_lhb3); + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_close() - Close a previously opened channel. + * + * @handle: handle to close + * + * Once the closing process has been completed, the GLINK_LOCAL_DISCONNECTED + * state event will be sent and the channel can be reopened. + * + * Return: 0 on success; -EINVAL for invalid handle, -EBUSY is close is + * already in progress, standard Linux Error code otherwise. + */ +int glink_close(void *handle) +{ + struct glink_core_xprt_ctx *xprt_ctx = NULL; + struct channel_ctx *ctx = (struct channel_ctx *)handle; + int ret = 0; + unsigned long flags; + + if (!ctx) + return -EINVAL; + + GLINK_INFO_CH(ctx, "%s: Closing channel, ctx: %p\n", __func__, ctx); + if (ctx->local_open_state == GLINK_CHANNEL_CLOSED) + return 0; + + if (ctx->local_open_state == GLINK_CHANNEL_CLOSING) { + /* close already pending */ + return -EBUSY; + } + + /* Set the channel state before removing it from xprt's list(s) */ + GLINK_INFO_PERF_CH(ctx, + "%s: local:%u->GLINK_CHANNEL_CLOSING\n", + __func__, ctx->local_open_state); + ctx->local_open_state = GLINK_CHANNEL_CLOSING; + + ctx->pending_delete = true; + ctx->int_req_ack = false; + complete_all(&ctx->int_req_ack_complete); + complete_all(&ctx->int_req_complete); + + spin_lock_irqsave(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + if (!list_empty(&ctx->tx_ready_list_node)) + list_del_init(&ctx->tx_ready_list_node); + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + + if (ctx->transport_ptr->local_state != GLINK_XPRT_DOWN) { + glink_qos_reset_priority(ctx); + ret = ctx->transport_ptr->ops->tx_cmd_ch_close( + ctx->transport_ptr->ops, + ctx->lcid); + } else if (!strcmp(ctx->transport_ptr->name, "dummy")) { + /* + * This check will avoid any race condition when clients call + * glink_close before the dummy xprt swapping happens in link + * down scenario. + */ + ret = 0; + xprt_ctx = ctx->transport_ptr; + rwref_write_get(&xprt_ctx->xprt_state_lhb0); + glink_core_ch_close_ack_common(ctx); + if (ch_is_fully_closed(ctx)) { + glink_delete_ch_from_list(ctx, false); + rwref_put(&xprt_ctx->xprt_state_lhb0); + if (list_empty(&xprt_ctx->channels)) + /* For the xprt reference */ + rwref_put(&xprt_ctx->xprt_state_lhb0); + } else { + GLINK_ERR_CH(ctx, + "channel Not closed yet local state [%d] remote_state [%d]\n", + ctx->local_open_state, ctx->remote_opened); + } + rwref_write_put(&xprt_ctx->xprt_state_lhb0); + } + + return ret; +} +EXPORT_SYMBOL(glink_close); + +/** + * glink_tx_pkt_release() - Release a packet's transmit information + * @tx_pkt_ref: Packet information which needs to be released. + * + * This function is called when all the references to a packet information + * is dropped. + */ +static void glink_tx_pkt_release(struct rwref_lock *tx_pkt_ref) +{ + struct glink_core_tx_pkt *tx_info = container_of(tx_pkt_ref, + struct glink_core_tx_pkt, + pkt_ref); + if (!list_empty(&tx_info->list_done)) + list_del_init(&tx_info->list_done); + if (!list_empty(&tx_info->list_node)) + list_del_init(&tx_info->list_node); + kfree(tx_info); +} + +/** + * glink_tx_common() - Common TX implementation + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data value that will be returned to client with + * notify_tx_done notification + * @data: pointer to the data + * @size: size of data + * @vbuf_provider: Virtual Address-space Buffer Provider for the tx buffer. + * @vbuf_provider: Physical Address-space Buffer Provider for the tx buffer. + * @tx_flags: Flags to indicate transmit options + * + * Return: -EINVAL for invalid handle; -EBUSY if channel isn't ready for + * transmit operation (not fully opened); -EAGAIN if remote side + * has not provided a receive intent that is big enough. + */ +static int glink_tx_common(void *handle, void *pkt_priv, + void *data, void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, size_t *size), + uint32_t tx_flags) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + uint32_t riid; + int ret = 0; + struct glink_core_tx_pkt *tx_info; + size_t intent_size; + bool is_atomic = + tx_flags & (GLINK_TX_SINGLE_THREADED | GLINK_TX_ATOMIC); + unsigned long flags; + + if (!size) + return -EINVAL; + + if (!ctx) + return -EINVAL; + + rwref_get(&ctx->ch_state_lhc0); + if (!(vbuf_provider || pbuf_provider)) { + rwref_put(&ctx->ch_state_lhc0); + return -EINVAL; + } + + if (!ch_is_fully_opened(ctx)) { + rwref_put(&ctx->ch_state_lhc0); + return -EBUSY; + } + + if (size > GLINK_MAX_PKT_SIZE) { + rwref_put(&ctx->ch_state_lhc0); + return -EINVAL; + } + + if (unlikely(tx_flags & GLINK_TX_TRACER_PKT)) { + if (!(ctx->transport_ptr->capabilities & GCAP_TRACER_PKT)) { + rwref_put(&ctx->ch_state_lhc0); + return -EOPNOTSUPP; + } + tracer_pkt_log_event(data, GLINK_CORE_TX); + } + + /* find matching rx intent (first-fit algorithm for now) */ + if (ch_pop_remote_rx_intent(ctx, size, &riid, &intent_size)) { + if (!(tx_flags & GLINK_TX_REQ_INTENT)) { + /* no rx intent available */ + GLINK_ERR_CH(ctx, + "%s: R[%u]:%zu Intent not present for lcid\n", + __func__, riid, size); + rwref_put(&ctx->ch_state_lhc0); + return -EAGAIN; + } + if (is_atomic && !(ctx->transport_ptr->capabilities & + GCAP_AUTO_QUEUE_RX_INT)) { + GLINK_ERR_CH(ctx, + "%s: Cannot request intent in atomic context\n", + __func__); + rwref_put(&ctx->ch_state_lhc0); + return -EINVAL; + } + + /* request intent of correct size */ + reinit_completion(&ctx->int_req_ack_complete); + ret = ctx->transport_ptr->ops->tx_cmd_rx_intent_req( + ctx->transport_ptr->ops, ctx->lcid, size); + if (ret) { + GLINK_ERR_CH(ctx, "%s: Request intent failed %d\n", + __func__, ret); + rwref_put(&ctx->ch_state_lhc0); + return ret; + } + + while (ch_pop_remote_rx_intent(ctx, size, &riid, + &intent_size)) { + if (is_atomic) { + GLINK_ERR_CH(ctx, + "%s Intent of size %zu not ready\n", + __func__, size); + rwref_put(&ctx->ch_state_lhc0); + return -EAGAIN; + } + + if (ctx->transport_ptr->local_state == GLINK_XPRT_DOWN + || !ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, + "%s: Channel closed while waiting for intent\n", + __func__); + rwref_put(&ctx->ch_state_lhc0); + return -EBUSY; + } + + /* wait for the remote intent req ack */ + if (!wait_for_completion_timeout( + &ctx->int_req_ack_complete, + ctx->rx_intent_req_timeout_jiffies)) { + GLINK_ERR_CH(ctx, + "%s: Intent request ack with size: %zu not granted for lcid\n", + __func__, size); + rwref_put(&ctx->ch_state_lhc0); + return -ETIMEDOUT; + } + + if (!ctx->int_req_ack) { + GLINK_ERR_CH(ctx, + "%s: Intent Request with size: %zu %s", + __func__, size, + "not granted for lcid\n"); + rwref_put(&ctx->ch_state_lhc0); + return -EAGAIN; + } + + /* wait for the rx_intent from remote side */ + if (!wait_for_completion_timeout( + &ctx->int_req_complete, + ctx->rx_intent_req_timeout_jiffies)) { + GLINK_ERR_CH(ctx, + "%s: Intent request with size: %zu not granted for lcid\n", + __func__, size); + rwref_put(&ctx->ch_state_lhc0); + return -ETIMEDOUT; + } + + reinit_completion(&ctx->int_req_complete); + } + } + + if (!is_atomic) { + spin_lock_irqsave(&ctx->transport_ptr->tx_ready_lock_lhb2, + flags); + glink_pm_qos_vote(ctx->transport_ptr); + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, + flags); + } + + GLINK_INFO_PERF_CH(ctx, "%s: R[%u]:%zu data[%p], size[%zu]. TID %u\n", + __func__, riid, intent_size, + data ? data : iovec, size, current->pid); + tx_info = kzalloc(sizeof(struct glink_core_tx_pkt), + is_atomic ? GFP_ATOMIC : GFP_KERNEL); + if (!tx_info) { + GLINK_ERR_CH(ctx, "%s: No memory for allocation\n", __func__); + ch_push_remote_rx_intent(ctx, intent_size, riid); + rwref_put(&ctx->ch_state_lhc0); + return -ENOMEM; + } + rwref_lock_init(&tx_info->pkt_ref, glink_tx_pkt_release); + INIT_LIST_HEAD(&tx_info->list_done); + INIT_LIST_HEAD(&tx_info->list_node); + tx_info->pkt_priv = pkt_priv; + tx_info->data = data; + tx_info->riid = riid; + tx_info->rcid = ctx->rcid; + tx_info->size = size; + tx_info->size_remaining = size; + tx_info->tracer_pkt = tx_flags & GLINK_TX_TRACER_PKT ? true : false; + tx_info->iovec = iovec ? iovec : (void *)tx_info; + tx_info->vprovider = vbuf_provider; + tx_info->pprovider = pbuf_provider; + tx_info->intent_size = intent_size; + + /* schedule packet for transmit */ + if ((tx_flags & GLINK_TX_SINGLE_THREADED) && + (ctx->transport_ptr->capabilities & GCAP_INTENTLESS)) + ret = xprt_single_threaded_tx(ctx->transport_ptr, + ctx, tx_info); + else + xprt_schedule_tx(ctx->transport_ptr, ctx, tx_info); + + rwref_put(&ctx->ch_state_lhc0); + return ret; +} + +/** + * glink_tx() - Transmit packet. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data value that will be returned to client with + * notify_tx_done notification + * @data: pointer to the data + * @size: size of data + * @tx_flags: Flags to specify transmit specific options + * + * Return: -EINVAL for invalid handle; -EBUSY if channel isn't ready for + * transmit operation (not fully opened); -EAGAIN if remote side + * has not provided a receive intent that is big enough. + */ +int glink_tx(void *handle, void *pkt_priv, void *data, size_t size, + uint32_t tx_flags) +{ + return glink_tx_common(handle, pkt_priv, data, NULL, size, + tx_linear_vbuf_provider, NULL, tx_flags); +} +EXPORT_SYMBOL(glink_tx); + +/** + * glink_queue_rx_intent() - Register an intent to receive data. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data type that is returned when a packet is received + * size: maximum size of data to receive + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_queue_rx_intent(void *handle, const void *pkt_priv, size_t size) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + struct glink_core_rx_intent *intent_ptr; + int ret = 0; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + /* Can only queue rx intents if channel is fully opened */ + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + intent_ptr = ch_push_local_rx_intent(ctx, pkt_priv, size); + if (!intent_ptr) { + GLINK_ERR_CH(ctx, + "%s: Intent pointer allocation failed size[%zu]\n", + __func__, size); + return -ENOMEM; + } + GLINK_DBG_CH(ctx, "%s: L[%u]:%zu\n", __func__, intent_ptr->id, + intent_ptr->intent_size); + + if (ctx->transport_ptr->capabilities & GCAP_INTENTLESS) + return ret; + + /* notify remote side of rx intent */ + ret = ctx->transport_ptr->ops->tx_cmd_local_rx_intent( + ctx->transport_ptr->ops, ctx->lcid, size, intent_ptr->id); + if (ret) + /* unable to transmit, dequeue intent */ + ch_remove_local_rx_intent(ctx, intent_ptr->id); + + return ret; +} +EXPORT_SYMBOL(glink_queue_rx_intent); + +/** + * glink_rx_intent_exists() - Check if an intent exists. + * + * @handle: handle returned by glink_open() + * @size: size of an intent to check or 0 for any intent + * + * Return: TRUE if an intent exists with greater than or equal to the size + * else FALSE + */ +bool glink_rx_intent_exists(void *handle, size_t size) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + struct glink_core_rx_intent *intent; + unsigned long flags; + + if (!ctx || !ch_is_fully_opened(ctx)) + return false; + + spin_lock_irqsave(&ctx->local_rx_intent_lst_lock_lhc1, flags); + list_for_each_entry(intent, &ctx->local_rx_intent_list, list) { + if (size <= intent->intent_size) { + spin_unlock_irqrestore( + &ctx->local_rx_intent_lst_lock_lhc1, flags); + return true; + } + } + spin_unlock_irqrestore(&ctx->local_rx_intent_lst_lock_lhc1, flags); + + return false; +} +EXPORT_SYMBOL(glink_rx_intent_exists); + +/** + * glink_rx_done() - Return receive buffer to remote side. + * + * @handle: handle returned by glink_open() + * @ptr: data pointer provided in the notify_rx() call + * @reuse: if true, receive intent is re-used + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_rx_done(void *handle, const void *ptr, bool reuse) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + struct glink_core_rx_intent *liid_ptr; + uint32_t id; + int ret = 0; + + liid_ptr = ch_get_local_rx_intent_notified(ctx, ptr); + + if (IS_ERR_OR_NULL(liid_ptr)) { + /* invalid pointer */ + GLINK_ERR_CH(ctx, "%s: Invalid pointer %p\n", __func__, ptr); + return -EINVAL; + } + + GLINK_INFO_PERF_CH(ctx, "%s: L[%u]: data[%p]. TID %u\n", + __func__, liid_ptr->id, ptr, current->pid); + id = liid_ptr->id; + if (reuse) { + ret = ctx->transport_ptr->ops->reuse_rx_intent( + ctx->transport_ptr->ops, liid_ptr); + if (ret) { + GLINK_ERR_CH(ctx, "%s: Intent reuse err %d for %p\n", + __func__, ret, ptr); + ret = -ENOBUFS; + reuse = false; + ctx->transport_ptr->ops->deallocate_rx_intent( + ctx->transport_ptr->ops, liid_ptr); + } + } else { + ctx->transport_ptr->ops->deallocate_rx_intent( + ctx->transport_ptr->ops, liid_ptr); + } + ch_remove_local_rx_intent_notified(ctx, liid_ptr, reuse); + /* send rx done */ + ctx->transport_ptr->ops->tx_cmd_local_rx_done(ctx->transport_ptr->ops, + ctx->lcid, id, reuse); + + return ret; +} +EXPORT_SYMBOL(glink_rx_done); + +/** + * glink_txv() - Transmit a packet in vector form. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data value that will be returned to client with + * notify_tx_done notification + * @iovec: pointer to the vector (must remain valid until notify_tx_done + * notification) + * @size: size of data/vector + * @vbuf_provider: Client provided helper function to iterate the vector + * in physical address space + * @pbuf_provider: Client provided helper function to iterate the vector + * in virtual address space + * @tx_flags: Flags to specify transmit specific options + * + * Return: -EINVAL for invalid handle; -EBUSY if channel isn't ready for + * transmit operation (not fully opened); -EAGAIN if remote side has + * not provided a receive intent that is big enough. + */ +int glink_txv(void *handle, void *pkt_priv, + void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, size_t *size), + uint32_t tx_flags) +{ + return glink_tx_common(handle, pkt_priv, NULL, iovec, size, + vbuf_provider, pbuf_provider, tx_flags); +} +EXPORT_SYMBOL(glink_txv); + +/** + * glink_sigs_set() - Set the local signals for the GLINK channel + * + * @handle: handle returned by glink_open() + * @sigs: modified signal value + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_set(void *handle, uint32_t sigs) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + int ret; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + ctx->lsigs = sigs; + + ret = ctx->transport_ptr->ops->tx_cmd_set_sigs(ctx->transport_ptr->ops, + ctx->lcid, ctx->lsigs); + GLINK_INFO_CH(ctx, "%s: Sent SIGNAL SET command\n", __func__); + + return ret; +} +EXPORT_SYMBOL(glink_sigs_set); + +/** + * glink_sigs_local_get() - Get the local signals for the GLINK channel + * + * handle: handle returned by glink_open() + * sigs: Pointer to hold the signals + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_local_get(void *handle, uint32_t *sigs) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx || !sigs) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + *sigs = ctx->lsigs; + return 0; +} +EXPORT_SYMBOL(glink_sigs_local_get); + +/** + * glink_sigs_remote_get() - Get the Remote signals for the GLINK channel + * + * handle: handle returned by glink_open() + * sigs: Pointer to hold the signals + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_remote_get(void *handle, uint32_t *sigs) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx || !sigs) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + *sigs = ctx->rsigs; + return 0; +} +EXPORT_SYMBOL(glink_sigs_remote_get); + +/** + * glink_register_link_state_cb() - Register for link state notification + * @link_info: Data structure containing the link identification and callback. + * @priv: Private information to be passed with the callback. + * + * This function is used to register a notifier to receive the updates about a + * link's/transport's state. This notifier needs to be registered first before + * an attempt to open a channel. + * + * Return: a reference to the notifier handle. + */ +void *glink_register_link_state_cb(struct glink_link_info *link_info, + void *priv) +{ + struct link_state_notifier_info *notif_info; + + if (!link_info || !link_info->glink_link_state_notif_cb) + return ERR_PTR(-EINVAL); + + notif_info = kzalloc(sizeof(*notif_info), GFP_KERNEL); + if (!notif_info) { + GLINK_ERR("%s: Error allocating link state notifier info\n", + __func__); + return ERR_PTR(-ENOMEM); + } + if (link_info->transport) + strlcpy(notif_info->transport, link_info->transport, + GLINK_NAME_SIZE); + + if (link_info->edge) + strlcpy(notif_info->edge, link_info->edge, GLINK_NAME_SIZE); + notif_info->priv = priv; + notif_info->glink_link_state_notif_cb = + link_info->glink_link_state_notif_cb; + + mutex_lock(&link_state_notifier_lock_lha1); + list_add_tail(¬if_info->list, &link_state_notifier_list); + mutex_unlock(&link_state_notifier_lock_lha1); + + notif_if_up_all_xprts(notif_info); + return notif_info; +} +EXPORT_SYMBOL(glink_register_link_state_cb); + +/** + * glink_unregister_link_state_cb() - Unregister the link state notification + * notif_handle: Handle to be unregistered. + * + * This function is used to unregister a notifier to stop receiving the updates + * about a link's/ transport's state. + */ +void glink_unregister_link_state_cb(void *notif_handle) +{ + struct link_state_notifier_info *notif_info, *tmp_notif_info; + + if (IS_ERR_OR_NULL(notif_handle)) + return; + + mutex_lock(&link_state_notifier_lock_lha1); + list_for_each_entry_safe(notif_info, tmp_notif_info, + &link_state_notifier_list, list) { + if (notif_info == notif_handle) { + list_del(¬if_info->list); + mutex_unlock(&link_state_notifier_lock_lha1); + kfree(notif_info); + return; + } + } + mutex_unlock(&link_state_notifier_lock_lha1); + return; +} +EXPORT_SYMBOL(glink_unregister_link_state_cb); + +/** + * glink_qos_latency() - Register the latency QoS requirement + * @handle: Channel handle in which the latency is required. + * @latency_us: Latency requirement in units of micro-seconds. + * @pkt_size: Worst case packet size for which the latency is required. + * + * This function is used to register the latency requirement for a channel + * and ensures that the latency requirement for this channel is met without + * impacting the existing latency requirements of other channels. + * + * Return: 0 if QoS request is achievable, standard Linux error codes on error + */ +int glink_qos_latency(void *handle, unsigned long latency_us, size_t pkt_size) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + int ret; + unsigned long req_rate_kBps; + + if (!ctx || !latency_us || !pkt_size) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + req_rate_kBps = glink_qos_calc_rate_kBps(pkt_size, latency_us); + + ret = glink_qos_assign_priority(ctx, req_rate_kBps); + if (ret < 0) + GLINK_ERR_CH(ctx, "%s: QoS %lu:%zu cannot be met\n", + __func__, latency_us, pkt_size); + + return ret; +} +EXPORT_SYMBOL(glink_qos_latency); + +/** + * glink_qos_cancel() - Cancel or unregister the QoS request + * @handle: Channel handle for which the QoS request is cancelled. + * + * This function is used to cancel/unregister the QoS requests for a channel. + * + * Return: 0 on success, standard Linux error codes on failure + */ +int glink_qos_cancel(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + int ret; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + ret = glink_qos_reset_priority(ctx); + return ret; +} +EXPORT_SYMBOL(glink_qos_cancel); + +/** + * glink_qos_start() - Start of the transmission requiring QoS + * @handle: Channel handle in which the transmit activity is performed. + * + * This function is called by the clients to indicate G-Link regarding the + * start of the transmission which requires a certain QoS. The clients + * must account for the QoS ramp time to ensure meeting the QoS. + * + * Return: 0 on success, standard Linux error codes on failure + */ +int glink_qos_start(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + int ret; + unsigned long flags; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return -EBUSY; + } + + spin_lock_irqsave(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + spin_lock(&ctx->tx_lists_lock_lhc3); + ret = glink_qos_add_ch_tx_intent(ctx); + spin_unlock(&ctx->tx_lists_lock_lhc3); + spin_unlock_irqrestore(&ctx->transport_ptr->tx_ready_lock_lhb2, flags); + return ret; +} +EXPORT_SYMBOL(glink_qos_start); + +/** + * glink_qos_get_ramp_time() - Get the QoS ramp time + * @handle: Channel handle for which the QoS ramp time is required. + * @pkt_size: Worst case packet size. + * + * This function is called by the clients to obtain the ramp time required + * to meet the QoS requirements. + * + * Return: QoS ramp time is returned in units of micro-seconds on success, + * standard Linux error codes cast to unsigned long on error. + */ +unsigned long glink_qos_get_ramp_time(void *handle, size_t pkt_size) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx) + return (unsigned long)-EINVAL; + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + return (unsigned long)-EBUSY; + } + + return ctx->transport_ptr->ops->get_power_vote_ramp_time( + ctx->transport_ptr->ops, + glink_prio_to_power_state(ctx->transport_ptr, + ctx->initial_priority)); +} +EXPORT_SYMBOL(glink_qos_get_ramp_time); + +/** + * glink_rpm_rx_poll() - Poll and receive any available events + * @handle: Channel handle in which this operation is performed. + * + * This function is used to poll and receive events and packets while the + * receive interrupt from RPM is disabled. + * + * Note that even if a return value > 0 is returned indicating that some events + * were processed, clients should only use the notification functions passed + * into glink_open() to determine if an entire packet has been received since + * some events may be internal details that are not visible to clients. + * + * Return: 0 for no packets available; > 0 for events available; standard + * Linux error codes on failure. + */ +int glink_rpm_rx_poll(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) + return -EBUSY; + + if (!ctx->transport_ptr || + !(ctx->transport_ptr->capabilities & GCAP_INTENTLESS)) + return -EOPNOTSUPP; + + return ctx->transport_ptr->ops->poll(ctx->transport_ptr->ops, + ctx->lcid); +} +EXPORT_SYMBOL(glink_rpm_rx_poll); + +/** + * glink_rpm_mask_rx_interrupt() - Mask or unmask the RPM receive interrupt + * @handle: Channel handle in which this operation is performed. + * @mask: Flag to mask or unmask the interrupt. + * @pstruct: Pointer to any platform specific data. + * + * This function is used to mask or unmask the receive interrupt from RPM. + * "mask" set to true indicates masking the interrupt and when set to false + * indicates unmasking the interrupt. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int glink_rpm_mask_rx_interrupt(void *handle, bool mask, void *pstruct) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx) + return -EINVAL; + + if (!ch_is_fully_opened(ctx)) + return -EBUSY; + + if (!ctx->transport_ptr || + !(ctx->transport_ptr->capabilities & GCAP_INTENTLESS)) + return -EOPNOTSUPP; + + return ctx->transport_ptr->ops->mask_rx_irq(ctx->transport_ptr->ops, + ctx->lcid, mask, pstruct); + +} +EXPORT_SYMBOL(glink_rpm_mask_rx_interrupt); + +/** + * glink_wait_link_down() - Get status of link + * @handle: Channel handle in which this operation is performed + * + * This function will query the transport for its status, to allow clients to + * proceed in cleanup operations. + */ +int glink_wait_link_down(void *handle) +{ + struct channel_ctx *ctx = (struct channel_ctx *)handle; + + if (!ctx) + return -EINVAL; + if (!ctx->transport_ptr) + return -EOPNOTSUPP; + + return ctx->transport_ptr->ops->wait_link_down(ctx->transport_ptr->ops); +} +EXPORT_SYMBOL(glink_wait_link_down); + +/** + * glink_xprt_ctx_release - Free the transport context + * @ch_st_lock: handle to the rwref_lock associated with the transport + * + * This should only be called when the reference count associated with the + * transport goes to zero. + */ +void glink_xprt_ctx_release(struct rwref_lock *xprt_st_lock) +{ + struct glink_dbgfs xprt_rm_dbgfs; + struct glink_core_xprt_ctx *xprt_ctx = container_of(xprt_st_lock, + struct glink_core_xprt_ctx, xprt_state_lhb0); + GLINK_INFO("%s: freeing transport [%s->%s]context\n", __func__, + xprt_ctx->name, + xprt_ctx->edge); + xprt_rm_dbgfs.curr_name = xprt_ctx->name; + xprt_rm_dbgfs.par_name = "xprt"; + glink_debugfs_remove_recur(&xprt_rm_dbgfs); + GLINK_INFO("%s: xprt debugfs removec\n", __func__); + destroy_workqueue(xprt_ctx->tx_wq); + glink_core_deinit_xprt_qos_cfg(xprt_ctx); + kfree(xprt_ctx); + xprt_ctx = NULL; +} + +/** + * glink_dummy_xprt_ctx_release - free the dummy transport context + * @xprt_st_lock: Handle to the rwref_lock associated with the transport. + * + * The release function is called when all the channels on this dummy + * transport are closed and the reference count goes to zero. + */ +static void glink_dummy_xprt_ctx_release(struct rwref_lock *xprt_st_lock) +{ + struct glink_core_xprt_ctx *xprt_ctx = container_of(xprt_st_lock, + struct glink_core_xprt_ctx, xprt_state_lhb0); + GLINK_INFO("%s: freeing transport [%s->%s]context\n", __func__, + xprt_ctx->name, + xprt_ctx->edge); + kfree(xprt_ctx); +} + +/** + * glink_xprt_name_to_id() - convert transport name to id + * @name: Name of the transport. + * @id: Assigned id. + * + * Return: 0 on success or standard Linux error code. + */ +int glink_xprt_name_to_id(const char *name, uint16_t *id) +{ + if (!strcmp(name, "smem")) { + *id = SMEM_XPRT_ID; + return 0; + } + if (!strcmp(name, "mailbox")) { + *id = SMEM_XPRT_ID; + return 0; + } + if (!strcmp(name, "smd_trans")) { + *id = SMD_TRANS_XPRT_ID; + return 0; + } + if (!strcmp(name, "lloop")) { + *id = LLOOP_XPRT_ID; + return 0; + } + if (!strcmp(name, "mock")) { + *id = MOCK_XPRT_ID; + return 0; + } + if (!strcmp(name, "mock_low")) { + *id = MOCK_XPRT_LOW_ID; + return 0; + } + if (!strcmp(name, "mock_high")) { + *id = MOCK_XPRT_HIGH_ID; + return 0; + } + return -ENODEV; +} +EXPORT_SYMBOL(glink_xprt_name_to_id); + +/** + * of_get_glink_core_qos_cfg() - Parse the qos related dt entries + * @phandle: The handle to the qos related node in DT. + * @cfg: The transport configuration to be filled. + * + * Return: 0 on Success, standard Linux error otherwise. + */ +int of_get_glink_core_qos_cfg(struct device_node *phandle, + struct glink_core_transport_cfg *cfg) +{ + int rc, i; + char *key; + uint32_t num_flows; + uint32_t *arr32; + + if (!phandle) { + GLINK_ERR("%s: phandle is NULL\n", __func__); + return -EINVAL; + } + + key = "qcom,mtu-size"; + rc = of_property_read_u32(phandle, key, (uint32_t *)&cfg->mtu); + if (rc) { + GLINK_ERR("%s: missing key %s\n", __func__, key); + return -ENODEV; + } + + key = "qcom,tput-stats-cycle"; + rc = of_property_read_u32(phandle, key, &cfg->token_count); + if (rc) { + GLINK_ERR("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto error; + } + + key = "qcom,flow-info"; + if (!of_find_property(phandle, key, &num_flows)) { + GLINK_ERR("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto error; + } + + num_flows /= sizeof(uint32_t); + if (num_flows % 2) { + GLINK_ERR("%s: Invalid flow info length\n", __func__); + rc = -EINVAL; + goto error; + } + + num_flows /= 2; + cfg->num_flows = num_flows; + + cfg->flow_info = kmalloc_array(num_flows, sizeof(*(cfg->flow_info)), + GFP_KERNEL); + if (!cfg->flow_info) { + GLINK_ERR("%s: Memory allocation for flow info failed\n", + __func__); + rc = -ENOMEM; + goto error; + } + arr32 = kmalloc_array(num_flows * 2, sizeof(uint32_t), GFP_KERNEL); + if (!arr32) { + GLINK_ERR("%s: Memory allocation for temporary array failed\n", + __func__); + rc = -ENOMEM; + goto temp_mem_alloc_fail; + } + + of_property_read_u32_array(phandle, key, arr32, num_flows * 2); + + for (i = 0; i < num_flows; i++) { + cfg->flow_info[i].mtu_tx_time_us = arr32[2 * i]; + cfg->flow_info[i].power_state = arr32[2 * i + 1]; + } + + kfree(arr32); + of_node_put(phandle); + return 0; + +temp_mem_alloc_fail: + kfree(cfg->flow_info); +error: + cfg->mtu = 0; + cfg->token_count = 0; + cfg->num_flows = 0; + cfg->flow_info = NULL; + return rc; +} +EXPORT_SYMBOL(of_get_glink_core_qos_cfg); + +/** + * glink_core_init_xprt_qos_cfg() - Initialize a transport's QoS configuration + * @xprt_ptr: Transport to be initialized with QoS configuration. + * @cfg: Data structure containing QoS configuration. + * + * This function is used during the transport registration to initialize it + * with QoS configuration. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +static int glink_core_init_xprt_qos_cfg(struct glink_core_xprt_ctx *xprt_ptr, + struct glink_core_transport_cfg *cfg) +{ + int i; + + xprt_ptr->mtu = cfg->mtu ? cfg->mtu : GLINK_QOS_DEF_MTU; + xprt_ptr->num_priority = cfg->num_flows ? cfg->num_flows : + GLINK_QOS_DEF_NUM_PRIORITY; + xprt_ptr->token_count = cfg->token_count ? cfg->token_count : + GLINK_QOS_DEF_NUM_TOKENS; + + xprt_ptr->prio_bin = kzalloc(xprt_ptr->num_priority * + sizeof(struct glink_qos_priority_bin), + GFP_KERNEL); + if (!xprt_ptr->prio_bin) { + GLINK_ERR("%s: unable to allocate priority bins\n", __func__); + return -ENOMEM; + } + for (i = 1; i < xprt_ptr->num_priority; i++) { + xprt_ptr->prio_bin[i].max_rate_kBps = + glink_qos_calc_rate_kBps(xprt_ptr->mtu, + cfg->flow_info[i].mtu_tx_time_us); + xprt_ptr->prio_bin[i].power_state = + cfg->flow_info[i].power_state; + INIT_LIST_HEAD(&xprt_ptr->prio_bin[i].tx_ready); + } + xprt_ptr->prio_bin[0].max_rate_kBps = 0; + if (cfg->flow_info) + xprt_ptr->prio_bin[0].power_state = + cfg->flow_info[0].power_state; + INIT_LIST_HEAD(&xprt_ptr->prio_bin[0].tx_ready); + xprt_ptr->threshold_rate_kBps = + xprt_ptr->prio_bin[xprt_ptr->num_priority - 1].max_rate_kBps; + + return 0; +} + +/** + * glink_core_deinit_xprt_qos_cfg() - Reset a transport's QoS configuration + * @xprt_ptr: Transport to be deinitialized. + * + * This function is used during the time of transport unregistration to + * de-initialize the QoS configuration from a transport. + */ +static void glink_core_deinit_xprt_qos_cfg(struct glink_core_xprt_ctx *xprt_ptr) +{ + kfree(xprt_ptr->prio_bin); + xprt_ptr->prio_bin = NULL; + xprt_ptr->mtu = 0; + xprt_ptr->num_priority = 0; + xprt_ptr->token_count = 0; + xprt_ptr->threshold_rate_kBps = 0; +} + +/** + * glink_core_register_transport() - register a new transport + * @if_ptr: The interface to the transport. + * @cfg: Description and configuration of the transport. + * + * Return: 0 on success, EINVAL for invalid input. + */ +int glink_core_register_transport(struct glink_transport_if *if_ptr, + struct glink_core_transport_cfg *cfg) +{ + struct glink_core_xprt_ctx *xprt_ptr; + size_t len; + uint16_t id; + int ret; + char log_name[GLINK_NAME_SIZE*2+2] = {0}; + + if (!if_ptr || !cfg || !cfg->name || !cfg->edge) + return -EINVAL; + + len = strlen(cfg->name); + if (len == 0 || len >= GLINK_NAME_SIZE) + return -EINVAL; + + len = strlen(cfg->edge); + if (len == 0 || len >= GLINK_NAME_SIZE) + return -EINVAL; + + if (cfg->versions_entries < 1) + return -EINVAL; + + ret = glink_xprt_name_to_id(cfg->name, &id); + if (ret) + return ret; + + xprt_ptr = kzalloc(sizeof(struct glink_core_xprt_ctx), GFP_KERNEL); + if (xprt_ptr == NULL) + return -ENOMEM; + + xprt_ptr->id = id; + rwref_lock_init(&xprt_ptr->xprt_state_lhb0, + glink_xprt_ctx_release); + strlcpy(xprt_ptr->name, cfg->name, GLINK_NAME_SIZE); + strlcpy(xprt_ptr->edge, cfg->edge, GLINK_NAME_SIZE); + xprt_ptr->versions = cfg->versions; + xprt_ptr->versions_entries = cfg->versions_entries; + xprt_ptr->local_version_idx = cfg->versions_entries - 1; + xprt_ptr->remote_version_idx = cfg->versions_entries - 1; + xprt_ptr->l_features = + cfg->versions[cfg->versions_entries - 1].features; + if (!if_ptr->poll) + if_ptr->poll = dummy_poll; + if (!if_ptr->mask_rx_irq) + if_ptr->mask_rx_irq = dummy_mask_rx_irq; + if (!if_ptr->reuse_rx_intent) + if_ptr->reuse_rx_intent = dummy_reuse_rx_intent; + if (!if_ptr->wait_link_down) + if_ptr->wait_link_down = dummy_wait_link_down; + if (!if_ptr->tx_cmd_tracer_pkt) + if_ptr->tx_cmd_tracer_pkt = dummy_tx_cmd_tracer_pkt; + if (!if_ptr->get_power_vote_ramp_time) + if_ptr->get_power_vote_ramp_time = + dummy_get_power_vote_ramp_time; + if (!if_ptr->power_vote) + if_ptr->power_vote = dummy_power_vote; + if (!if_ptr->power_unvote) + if_ptr->power_unvote = dummy_power_unvote; + xprt_ptr->capabilities = 0; + xprt_ptr->ops = if_ptr; + spin_lock_init(&xprt_ptr->xprt_ctx_lock_lhb1); + xprt_ptr->next_lcid = 1; /* 0 reserved for default unconfigured */ + INIT_LIST_HEAD(&xprt_ptr->free_lcid_list); + xprt_ptr->max_cid = cfg->max_cid; + xprt_ptr->max_iid = cfg->max_iid; + xprt_ptr->local_state = GLINK_XPRT_DOWN; + xprt_ptr->remote_neg_completed = false; + INIT_LIST_HEAD(&xprt_ptr->channels); + ret = glink_core_init_xprt_qos_cfg(xprt_ptr, cfg); + if (ret < 0) { + kfree(xprt_ptr); + return ret; + } + spin_lock_init(&xprt_ptr->tx_ready_lock_lhb2); + mutex_init(&xprt_ptr->xprt_dbgfs_lock_lhb3); + INIT_WORK(&xprt_ptr->tx_work, tx_work_func); + xprt_ptr->tx_wq = create_singlethread_workqueue("glink_tx"); + if (IS_ERR_OR_NULL(xprt_ptr->tx_wq)) { + GLINK_ERR("%s: unable to allocate workqueue\n", __func__); + glink_core_deinit_xprt_qos_cfg(xprt_ptr); + kfree(xprt_ptr); + return -ENOMEM; + } + INIT_DELAYED_WORK(&xprt_ptr->pm_qos_work, glink_pm_qos_cancel_worker); + pm_qos_add_request(&xprt_ptr->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, + PM_QOS_DEFAULT_VALUE); + + if_ptr->glink_core_priv = xprt_ptr; + if_ptr->glink_core_if_ptr = &core_impl; + + mutex_lock(&transport_list_lock_lha0); + list_add_tail(&xprt_ptr->list_node, &transport_list); + mutex_unlock(&transport_list_lock_lha0); + glink_debugfs_add_xprt(xprt_ptr); + snprintf(log_name, sizeof(log_name), "%s_%s", + xprt_ptr->edge, xprt_ptr->name); + xprt_ptr->log_ctx = ipc_log_context_create(NUM_LOG_PAGES, log_name, 0); + if (!xprt_ptr->log_ctx) + GLINK_ERR("%s: unable to create log context for [%s:%s]\n", + __func__, xprt_ptr->edge, xprt_ptr->name); + + return 0; +} +EXPORT_SYMBOL(glink_core_register_transport); + +/** + * glink_core_unregister_transport() - unregister a transport + * + * @if_ptr: The interface to the transport. + */ +void glink_core_unregister_transport(struct glink_transport_if *if_ptr) +{ + struct glink_core_xprt_ctx *xprt_ptr = if_ptr->glink_core_priv; + + GLINK_DBG_XPRT(xprt_ptr, "%s: destroying transport\n", __func__); + if (xprt_ptr->local_state != GLINK_XPRT_DOWN) { + GLINK_ERR_XPRT(xprt_ptr, + "%s: link_down should have been called before this\n", + __func__); + return; + } + + mutex_lock(&transport_list_lock_lha0); + list_del(&xprt_ptr->list_node); + mutex_unlock(&transport_list_lock_lha0); + flush_delayed_work(&xprt_ptr->pm_qos_work); + pm_qos_remove_request(&xprt_ptr->pm_qos_req); + ipc_log_context_destroy(xprt_ptr->log_ctx); + xprt_ptr->log_ctx = NULL; + rwref_put(&xprt_ptr->xprt_state_lhb0); +} +EXPORT_SYMBOL(glink_core_unregister_transport); + +/** + * glink_core_link_up() - transport link-up notification + * + * @if_ptr: pointer to transport interface + */ +static void glink_core_link_up(struct glink_transport_if *if_ptr) +{ + struct glink_core_xprt_ctx *xprt_ptr = if_ptr->glink_core_priv; + + /* start local negotiation */ + xprt_ptr->local_state = GLINK_XPRT_NEGOTIATING; + xprt_ptr->local_version_idx = xprt_ptr->versions_entries - 1; + xprt_ptr->l_features = + xprt_ptr->versions[xprt_ptr->local_version_idx].features; + if_ptr->tx_cmd_version(if_ptr, + xprt_ptr->versions[xprt_ptr->local_version_idx].version, + xprt_ptr->versions[xprt_ptr->local_version_idx].features); + +} + +/** + * glink_core_link_down() - transport link-down notification + * + * @if_ptr: pointer to transport interface + */ +static void glink_core_link_down(struct glink_transport_if *if_ptr) +{ + struct glink_core_xprt_ctx *xprt_ptr = if_ptr->glink_core_priv; + + rwref_write_get(&xprt_ptr->xprt_state_lhb0); + xprt_ptr->next_lcid = 1; + xprt_ptr->local_state = GLINK_XPRT_DOWN; + xprt_ptr->local_version_idx = xprt_ptr->versions_entries - 1; + xprt_ptr->remote_version_idx = xprt_ptr->versions_entries - 1; + xprt_ptr->l_features = + xprt_ptr->versions[xprt_ptr->local_version_idx].features; + xprt_ptr->remote_neg_completed = false; + rwref_write_put(&xprt_ptr->xprt_state_lhb0); + GLINK_DBG_XPRT(xprt_ptr, + "%s: Flushing work from tx_wq. Thread: %u\n", __func__, + current->pid); + flush_workqueue(xprt_ptr->tx_wq); + glink_core_channel_cleanup(xprt_ptr); + check_link_notifier_and_notify(xprt_ptr, GLINK_LINK_STATE_DOWN); +} + +/** + * glink_create_dummy_xprt_ctx() - create a dummy transport that replaces all + * the transport interface functions with a dummy + * @orig_xprt_ctx: Pointer to the original transport context. + * + * The dummy transport is used only when it is swapped with the actual transport + * pointer in ssr/unregister case. + * + * Return: Pointer to dummy transport context. + */ +static struct glink_core_xprt_ctx *glink_create_dummy_xprt_ctx( + struct glink_core_xprt_ctx *orig_xprt_ctx) +{ + + struct glink_core_xprt_ctx *xprt_ptr; + struct glink_transport_if *if_ptr; + + xprt_ptr = kzalloc(sizeof(*xprt_ptr), GFP_KERNEL); + if (!xprt_ptr) + return ERR_PTR(-ENOMEM); + if_ptr = kmalloc(sizeof(*if_ptr), GFP_KERNEL); + if (!if_ptr) { + kfree(xprt_ptr); + return ERR_PTR(-ENOMEM); + } + rwref_lock_init(&xprt_ptr->xprt_state_lhb0, + glink_dummy_xprt_ctx_release); + + strlcpy(xprt_ptr->name, "dummy", GLINK_NAME_SIZE); + strlcpy(xprt_ptr->edge, orig_xprt_ctx->edge, GLINK_NAME_SIZE); + if_ptr->poll = dummy_poll; + if_ptr->mask_rx_irq = dummy_mask_rx_irq; + if_ptr->reuse_rx_intent = dummy_reuse_rx_intent; + if_ptr->wait_link_down = dummy_wait_link_down; + if_ptr->allocate_rx_intent = dummy_allocate_rx_intent; + if_ptr->deallocate_rx_intent = dummy_deallocate_rx_intent; + if_ptr->tx_cmd_local_rx_intent = dummy_tx_cmd_local_rx_intent; + if_ptr->tx_cmd_local_rx_done = dummy_tx_cmd_local_rx_done; + if_ptr->tx = dummy_tx; + if_ptr->tx_cmd_rx_intent_req = dummy_tx_cmd_rx_intent_req; + if_ptr->tx_cmd_remote_rx_intent_req_ack = + dummy_tx_cmd_remote_rx_intent_req_ack; + if_ptr->tx_cmd_set_sigs = dummy_tx_cmd_set_sigs; + if_ptr->tx_cmd_ch_close = dummy_tx_cmd_ch_close; + if_ptr->tx_cmd_ch_remote_close_ack = dummy_tx_cmd_ch_remote_close_ack; + + xprt_ptr->ops = if_ptr; + xprt_ptr->log_ctx = log_ctx; + spin_lock_init(&xprt_ptr->xprt_ctx_lock_lhb1); + INIT_LIST_HEAD(&xprt_ptr->free_lcid_list); + xprt_ptr->local_state = GLINK_XPRT_DOWN; + xprt_ptr->remote_neg_completed = false; + INIT_LIST_HEAD(&xprt_ptr->channels); + spin_lock_init(&xprt_ptr->tx_ready_lock_lhb2); + mutex_init(&xprt_ptr->xprt_dbgfs_lock_lhb3); + return xprt_ptr; +} + +/** + * glink_core_channel_cleanup() - cleanup all channels for the transport + * + * @xprt_ptr: pointer to transport context + * + * This function should be called either from link_down or ssr + */ +static void glink_core_channel_cleanup(struct glink_core_xprt_ctx *xprt_ptr) +{ + unsigned long flags, d_flags; + struct channel_ctx *ctx, *tmp_ctx; + struct channel_lcid *temp_lcid, *temp_lcid1; + struct glink_core_xprt_ctx *dummy_xprt_ctx; + + dummy_xprt_ctx = glink_create_dummy_xprt_ctx(xprt_ptr); + if (IS_ERR_OR_NULL(dummy_xprt_ctx)) { + GLINK_ERR("%s: Dummy Transport creation failed\n", __func__); + return; + } + + rwref_get(&dummy_xprt_ctx->xprt_state_lhb0); + spin_lock_irqsave(&xprt_ptr->xprt_ctx_lock_lhb1, flags); + list_for_each_entry_safe(ctx, tmp_ctx, &xprt_ptr->channels, + port_list_node) { + rwref_get(&ctx->ch_state_lhc0); + if (ctx->local_open_state == GLINK_CHANNEL_OPENED || + ctx->local_open_state == GLINK_CHANNEL_OPENING) { + rwref_get(&dummy_xprt_ctx->xprt_state_lhb0); + spin_lock_irqsave(&dummy_xprt_ctx->xprt_ctx_lock_lhb1, + d_flags); + list_move_tail(&ctx->port_list_node, + &dummy_xprt_ctx->channels); + spin_unlock_irqrestore( + &dummy_xprt_ctx->xprt_ctx_lock_lhb1, d_flags); + ctx->transport_ptr = dummy_xprt_ctx; + } else { + /* local state is in either CLOSED or CLOSING */ + spin_unlock_irqrestore(&xprt_ptr->xprt_ctx_lock_lhb1, + flags); + glink_core_remote_close_common(ctx); + if (ctx->local_open_state == GLINK_CHANNEL_CLOSING) + glink_core_ch_close_ack_common(ctx); + /* Channel should be fully closed now. Delete here */ + if (ch_is_fully_closed(ctx)) + glink_delete_ch_from_list(ctx, false); + spin_lock_irqsave(&xprt_ptr->xprt_ctx_lock_lhb1, flags); + } + rwref_put(&ctx->ch_state_lhc0); + } + list_for_each_entry_safe(temp_lcid, temp_lcid1, + &xprt_ptr->free_lcid_list, list_node) { + list_del(&temp_lcid->list_node); + kfree(&temp_lcid->list_node); + } + spin_unlock_irqrestore(&xprt_ptr->xprt_ctx_lock_lhb1, flags); + + spin_lock_irqsave(&dummy_xprt_ctx->xprt_ctx_lock_lhb1, d_flags); + list_for_each_entry_safe(ctx, tmp_ctx, &dummy_xprt_ctx->channels, + port_list_node) { + rwref_get(&ctx->ch_state_lhc0); + spin_unlock_irqrestore(&dummy_xprt_ctx->xprt_ctx_lock_lhb1, + d_flags); + glink_core_remote_close_common(ctx); + spin_lock_irqsave(&dummy_xprt_ctx->xprt_ctx_lock_lhb1, + d_flags); + rwref_put(&ctx->ch_state_lhc0); + } + spin_unlock_irqrestore(&dummy_xprt_ctx->xprt_ctx_lock_lhb1, d_flags); + rwref_put(&dummy_xprt_ctx->xprt_state_lhb0); +} +/** + * glink_core_rx_cmd_version() - receive version/features from remote system + * + * @if_ptr: pointer to transport interface + * @r_version: remote version + * @r_features: remote features + * + * This function is called in response to a remote-initiated version/feature + * negotiation sequence. + */ +static void glink_core_rx_cmd_version(struct glink_transport_if *if_ptr, + uint32_t r_version, uint32_t r_features) +{ + struct glink_core_xprt_ctx *xprt_ptr = if_ptr->glink_core_priv; + const struct glink_core_version *versions = xprt_ptr->versions; + bool neg_complete = false; + uint32_t l_version; + + if (xprt_is_fully_opened(xprt_ptr)) { + GLINK_ERR_XPRT(xprt_ptr, + "%s: Negotiation already complete\n", __func__); + return; + } + + l_version = versions[xprt_ptr->remote_version_idx].version; + + GLINK_INFO_XPRT(xprt_ptr, + "%s: [local]%x:%08x [remote]%x:%08x\n", __func__, + l_version, xprt_ptr->l_features, r_version, r_features); + + if (l_version > r_version) { + /* Find matching version */ + while (true) { + uint32_t rver_idx; + + if (xprt_ptr->remote_version_idx == 0) { + /* version negotiation failed */ + GLINK_ERR_XPRT(xprt_ptr, + "%s: Transport negotiation failed\n", + __func__); + l_version = 0; + xprt_ptr->l_features = 0; + break; + } + --xprt_ptr->remote_version_idx; + rver_idx = xprt_ptr->remote_version_idx; + + if (versions[rver_idx].version <= r_version) { + /* found a potential match */ + l_version = versions[rver_idx].version; + xprt_ptr->l_features = + versions[rver_idx].features; + break; + } + } + } + + if (l_version == r_version) { + GLINK_INFO_XPRT(xprt_ptr, + "%s: Remote and local version are matched %x:%08x\n", + __func__, r_version, r_features); + if (xprt_ptr->l_features != r_features) { + uint32_t rver_idx = xprt_ptr->remote_version_idx; + + xprt_ptr->l_features = versions[rver_idx] + .negotiate_features(if_ptr, + &xprt_ptr->versions[rver_idx], + r_features); + GLINK_INFO_XPRT(xprt_ptr, + "%s: negotiate features %x:%08x\n", + __func__, l_version, xprt_ptr->l_features); + } + neg_complete = true; + } + if_ptr->tx_cmd_version_ack(if_ptr, l_version, xprt_ptr->l_features); + + if (neg_complete) { + GLINK_INFO_XPRT(xprt_ptr, + "%s: Remote negotiation complete %x:%08x\n", __func__, + l_version, xprt_ptr->l_features); + + if (xprt_ptr->local_state == GLINK_XPRT_OPENED) { + xprt_ptr->capabilities = if_ptr->set_version(if_ptr, + l_version, + xprt_ptr->l_features); + } + if_ptr->glink_core_priv->remote_neg_completed = true; + if (xprt_is_fully_opened(xprt_ptr)) + check_link_notifier_and_notify(xprt_ptr, + GLINK_LINK_STATE_UP); + } +} + +/** + * glink_core_rx_cmd_version_ack() - receive negotiation ack from remote system + * + * @if_ptr: pointer to transport interface + * @r_version: remote version response + * @r_features: remote features response + * + * This function is called in response to a local-initiated version/feature + * negotiation sequence and is the counter-offer from the remote side based + * upon the initial version and feature set requested. + */ +static void glink_core_rx_cmd_version_ack(struct glink_transport_if *if_ptr, + uint32_t r_version, uint32_t r_features) +{ + struct glink_core_xprt_ctx *xprt_ptr = if_ptr->glink_core_priv; + const struct glink_core_version *versions = xprt_ptr->versions; + uint32_t l_version; + bool neg_complete = false; + + if (xprt_is_fully_opened(xprt_ptr)) { + GLINK_ERR_XPRT(xprt_ptr, + "%s: Negotiation already complete\n", __func__); + return; + } + + l_version = versions[xprt_ptr->local_version_idx].version; + + GLINK_INFO_XPRT(xprt_ptr, + "%s: [local]%x:%08x [remote]%x:%08x\n", __func__, + l_version, xprt_ptr->l_features, r_version, r_features); + + if (l_version > r_version) { + /* find matching version */ + while (true) { + uint32_t lver_idx = xprt_ptr->local_version_idx; + + if (xprt_ptr->local_version_idx == 0) { + /* version negotiation failed */ + xprt_ptr->local_state = GLINK_XPRT_FAILED; + GLINK_ERR_XPRT(xprt_ptr, + "%s: Transport negotiation failed\n", + __func__); + l_version = 0; + xprt_ptr->l_features = 0; + break; + } + --xprt_ptr->local_version_idx; + lver_idx = xprt_ptr->local_version_idx; + + if (versions[lver_idx].version <= r_version) { + /* found a potential match */ + l_version = versions[lver_idx].version; + xprt_ptr->l_features = + versions[lver_idx].features; + break; + } + } + } else if (l_version == r_version) { + if (xprt_ptr->l_features != r_features) { + /* version matches, negotiate features */ + uint32_t lver_idx = xprt_ptr->local_version_idx; + + xprt_ptr->l_features = versions[lver_idx] + .negotiate_features(if_ptr, + &versions[lver_idx], + r_features); + GLINK_INFO_XPRT(xprt_ptr, + "%s: negotiation features %x:%08x\n", + __func__, l_version, xprt_ptr->l_features); + } else { + neg_complete = true; + } + } else { + /* + * r_version > l_version + * + * Remote responded with a version greater than what we + * requested which is invalid and is treated as failure of the + * negotiation algorithm. + */ + GLINK_ERR_XPRT(xprt_ptr, + "%s: [local]%x:%08x [remote]%x:%08x neg failure\n", + __func__, l_version, xprt_ptr->l_features, r_version, + r_features); + xprt_ptr->local_state = GLINK_XPRT_FAILED; + l_version = 0; + xprt_ptr->l_features = 0; + } + + if (neg_complete) { + /* negotiation complete */ + GLINK_INFO_XPRT(xprt_ptr, + "%s: Local negotiation complete %x:%08x\n", + __func__, l_version, xprt_ptr->l_features); + + if (xprt_ptr->remote_neg_completed) { + xprt_ptr->capabilities = if_ptr->set_version(if_ptr, + l_version, + xprt_ptr->l_features); + } + + xprt_ptr->local_state = GLINK_XPRT_OPENED; + if (xprt_is_fully_opened(xprt_ptr)) + check_link_notifier_and_notify(xprt_ptr, + GLINK_LINK_STATE_UP); + } else { + if_ptr->tx_cmd_version(if_ptr, l_version, xprt_ptr->l_features); + } +} + +/** + * find_l_ctx_get() - find a local channel context based on a remote one + * @r_ctx: The remote channel to use as a lookup key. + * + * If the channel is found, the reference count is incremented to ensure the + * lifetime of the channel context. The caller must call rwref_put() when done. + * + * Return: The corresponding local ctx or NULL is not found. + */ +static struct channel_ctx *find_l_ctx_get(struct channel_ctx *r_ctx) +{ + struct glink_core_xprt_ctx *xprt; + struct channel_ctx *ctx; + unsigned long flags; + struct channel_ctx *l_ctx = NULL; + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt, &transport_list, list_node) + if (!strcmp(r_ctx->transport_ptr->edge, xprt->edge)) { + rwref_write_get(&xprt->xprt_state_lhb0); + if (xprt->local_state != GLINK_XPRT_OPENED) { + rwref_write_put(&xprt->xprt_state_lhb0); + continue; + } + spin_lock_irqsave(&xprt->xprt_ctx_lock_lhb1, flags); + list_for_each_entry(ctx, &xprt->channels, + port_list_node) + if (!strcmp(ctx->name, r_ctx->name) && + ctx->local_xprt_req && + ctx->local_xprt_resp) { + l_ctx = ctx; + rwref_get(&l_ctx->ch_state_lhc0); + } + spin_unlock_irqrestore(&xprt->xprt_ctx_lock_lhb1, + flags); + rwref_write_put(&xprt->xprt_state_lhb0); + } + mutex_unlock(&transport_list_lock_lha0); + + return l_ctx; +} + +/** + * find_r_ctx_get() - find a remote channel context based on a local one + * @l_ctx: The local channel to use as a lookup key. + * + * If the channel is found, the reference count is incremented to ensure the + * lifetime of the channel context. The caller must call rwref_put() when done. + * + * Return: The corresponding remote ctx or NULL is not found. + */ +static struct channel_ctx *find_r_ctx_get(struct channel_ctx *l_ctx) +{ + struct glink_core_xprt_ctx *xprt; + struct channel_ctx *ctx; + unsigned long flags; + struct channel_ctx *r_ctx = NULL; + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt, &transport_list, list_node) + if (!strcmp(l_ctx->transport_ptr->edge, xprt->edge)) { + rwref_write_get(&xprt->xprt_state_lhb0); + if (xprt->local_state != GLINK_XPRT_OPENED) { + rwref_write_put(&xprt->xprt_state_lhb0); + continue; + } + spin_lock_irqsave(&xprt->xprt_ctx_lock_lhb1, flags); + list_for_each_entry(ctx, &xprt->channels, + port_list_node) + if (!strcmp(ctx->name, l_ctx->name) && + ctx->remote_xprt_req && + ctx->remote_xprt_resp) { + r_ctx = ctx; + rwref_get(&r_ctx->ch_state_lhc0); + } + spin_unlock_irqrestore(&xprt->xprt_ctx_lock_lhb1, + flags); + rwref_write_put(&xprt->xprt_state_lhb0); + } + mutex_unlock(&transport_list_lock_lha0); + + return r_ctx; +} + +/** + * will_migrate() - will a channel migrate to a different transport + * @l_ctx: The local channel to migrate. + * @r_ctx: The remote channel to migrate. + * + * One of the channel contexts can be NULL if not known, but at least one ctx + * must be provided. + * + * Return: Bool indicating if migration will occur. + */ +static bool will_migrate(struct channel_ctx *l_ctx, struct channel_ctx *r_ctx) +{ + uint16_t new_xprt; + bool migrate = false; + + if (!r_ctx) + r_ctx = find_r_ctx_get(l_ctx); + else + rwref_get(&r_ctx->ch_state_lhc0); + if (!r_ctx) + return migrate; + + if (!l_ctx) + l_ctx = find_l_ctx_get(r_ctx); + else + rwref_get(&l_ctx->ch_state_lhc0); + if (!l_ctx) + goto exit; + + if (l_ctx->local_xprt_req == r_ctx->remote_xprt_req && + l_ctx->local_xprt_req == l_ctx->transport_ptr->id) + goto exit; + if (l_ctx->no_migrate) + goto exit; + + if (l_ctx->local_xprt_req > r_ctx->transport_ptr->id) + l_ctx->local_xprt_req = r_ctx->transport_ptr->id; + + new_xprt = max(l_ctx->local_xprt_req, r_ctx->remote_xprt_req); + + if (new_xprt == l_ctx->transport_ptr->id) + goto exit; + + migrate = true; +exit: + if (l_ctx) + rwref_put(&l_ctx->ch_state_lhc0); + if (r_ctx) + rwref_put(&r_ctx->ch_state_lhc0); + + return migrate; +} + +/** + * ch_migrate() - migrate a channel to a different transport + * @l_ctx: The local channel to migrate. + * @r_ctx: The remote channel to migrate. + * + * One of the channel contexts can be NULL if not known, but at least one ctx + * must be provided. + * + * Return: Bool indicating if migration occurred. + */ +static bool ch_migrate(struct channel_ctx *l_ctx, struct channel_ctx *r_ctx) +{ + uint16_t new_xprt; + struct glink_core_xprt_ctx *xprt; + unsigned long flags; + struct channel_lcid *flcid; + uint16_t best_xprt = USHRT_MAX; + struct channel_ctx *ctx_clone; + bool migrated = false; + + if (!r_ctx) + r_ctx = find_r_ctx_get(l_ctx); + else + rwref_get(&r_ctx->ch_state_lhc0); + if (!r_ctx) + return migrated; + + if (!l_ctx) + l_ctx = find_l_ctx_get(r_ctx); + else + rwref_get(&l_ctx->ch_state_lhc0); + if (!l_ctx) { + rwref_put(&r_ctx->ch_state_lhc0); + return migrated; + } + + if (l_ctx->local_xprt_req == r_ctx->remote_xprt_req && + l_ctx->local_xprt_req == l_ctx->transport_ptr->id) + goto exit; + if (l_ctx->no_migrate) + goto exit; + + if (l_ctx->local_xprt_req > r_ctx->transport_ptr->id) + l_ctx->local_xprt_req = r_ctx->transport_ptr->id; + + new_xprt = max(l_ctx->local_xprt_req, r_ctx->remote_xprt_req); + + if (new_xprt == l_ctx->transport_ptr->id) + goto exit; + + ctx_clone = kmalloc(sizeof(*ctx_clone), GFP_KERNEL); + if (!ctx_clone) + goto exit; + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt, &transport_list, list_node) + if (!strcmp(l_ctx->transport_ptr->edge, xprt->edge)) + if (xprt->id == new_xprt) + break; + mutex_unlock(&transport_list_lock_lha0); + + spin_lock_irqsave(&l_ctx->transport_ptr->xprt_ctx_lock_lhb1, flags); + list_del_init(&l_ctx->port_list_node); + spin_unlock_irqrestore(&l_ctx->transport_ptr->xprt_ctx_lock_lhb1, + flags); + + memcpy(ctx_clone, l_ctx, sizeof(*ctx_clone)); + ctx_clone->local_xprt_req = 0; + ctx_clone->local_xprt_resp = 0; + ctx_clone->remote_xprt_req = 0; + ctx_clone->remote_xprt_resp = 0; + ctx_clone->notify_state = NULL; + ctx_clone->local_open_state = GLINK_CHANNEL_CLOSING; + rwref_lock_init(&ctx_clone->ch_state_lhc0, glink_ch_ctx_release); + init_completion(&ctx_clone->int_req_ack_complete); + init_completion(&ctx_clone->int_req_complete); + spin_lock_init(&ctx_clone->local_rx_intent_lst_lock_lhc1); + spin_lock_init(&ctx_clone->rmt_rx_intent_lst_lock_lhc2); + INIT_LIST_HEAD(&ctx_clone->tx_ready_list_node); + INIT_LIST_HEAD(&ctx_clone->local_rx_intent_list); + INIT_LIST_HEAD(&ctx_clone->local_rx_intent_ntfy_list); + INIT_LIST_HEAD(&ctx_clone->local_rx_intent_free_list); + INIT_LIST_HEAD(&ctx_clone->rmt_rx_intent_list); + INIT_LIST_HEAD(&ctx_clone->tx_active); + spin_lock_init(&ctx_clone->tx_pending_rmt_done_lock_lhc4); + INIT_LIST_HEAD(&ctx_clone->tx_pending_remote_done); + spin_lock_init(&ctx_clone->tx_lists_lock_lhc3); + spin_lock_irqsave(&l_ctx->transport_ptr->xprt_ctx_lock_lhb1, flags); + list_add_tail(&ctx_clone->port_list_node, + &l_ctx->transport_ptr->channels); + spin_unlock_irqrestore(&l_ctx->transport_ptr->xprt_ctx_lock_lhb1, + flags); + + l_ctx->transport_ptr->ops->tx_cmd_ch_close(l_ctx->transport_ptr->ops, + l_ctx->lcid); + + l_ctx->transport_ptr = xprt; + l_ctx->local_xprt_req = 0; + l_ctx->local_xprt_resp = 0; + if (new_xprt != r_ctx->transport_ptr->id) { + r_ctx->local_xprt_req = 0; + r_ctx->local_xprt_resp = 0; + r_ctx->remote_xprt_req = 0; + r_ctx->remote_xprt_resp = 0; + + l_ctx->remote_xprt_req = 0; + l_ctx->remote_xprt_resp = 0; + l_ctx->remote_opened = false; + + rwref_write_get(&xprt->xprt_state_lhb0); + spin_lock_irqsave(&xprt->xprt_ctx_lock_lhb1, flags); + if (list_empty(&xprt->free_lcid_list)) { + l_ctx->lcid = xprt->next_lcid++; + } else { + flcid = list_first_entry(&xprt->free_lcid_list, + struct channel_lcid, list_node); + l_ctx->lcid = flcid->lcid; + list_del(&flcid->list_node); + kfree(flcid); + } + list_add_tail(&l_ctx->port_list_node, &xprt->channels); + spin_unlock_irqrestore(&xprt->xprt_ctx_lock_lhb1, flags); + rwref_write_put(&xprt->xprt_state_lhb0); + } else { + l_ctx->lcid = r_ctx->lcid; + l_ctx->rcid = r_ctx->rcid; + l_ctx->remote_opened = r_ctx->remote_opened; + l_ctx->remote_xprt_req = r_ctx->remote_xprt_req; + l_ctx->remote_xprt_resp = r_ctx->remote_xprt_resp; + glink_delete_ch_from_list(r_ctx, false); + + spin_lock_irqsave(&xprt->xprt_ctx_lock_lhb1, flags); + list_add_tail(&l_ctx->port_list_node, &xprt->channels); + spin_unlock_irqrestore(&xprt->xprt_ctx_lock_lhb1, flags); + } + + + mutex_lock(&transport_list_lock_lha0); + list_for_each_entry(xprt, &transport_list, list_node) + if (!strcmp(l_ctx->transport_ptr->edge, xprt->edge)) + if (xprt->id < best_xprt) + best_xprt = xprt->id; + mutex_unlock(&transport_list_lock_lha0); + l_ctx->local_open_state = GLINK_CHANNEL_OPENING; + l_ctx->local_xprt_req = best_xprt; + l_ctx->transport_ptr->ops->tx_cmd_ch_open(l_ctx->transport_ptr->ops, + l_ctx->lcid, l_ctx->name, best_xprt); + + migrated = true; +exit: + rwref_put(&l_ctx->ch_state_lhc0); + rwref_put(&r_ctx->ch_state_lhc0); + + return migrated; +} + +/** + * calculate_xprt_resp() - calculate the response to a remote xprt request + * @r_ctx: The channel the remote xprt request is for. + * + * Return: The calculated response. + */ +static uint16_t calculate_xprt_resp(struct channel_ctx *r_ctx) +{ + struct channel_ctx *l_ctx; + + l_ctx = find_l_ctx_get(r_ctx); + if (!l_ctx) { + r_ctx->remote_xprt_resp = r_ctx->transport_ptr->id; + } else if (r_ctx->remote_xprt_req == r_ctx->transport_ptr->id) { + r_ctx->remote_xprt_resp = r_ctx->remote_xprt_req; + } else { + if (!l_ctx->local_xprt_req) + r_ctx->remote_xprt_resp = r_ctx->remote_xprt_req; + else if (l_ctx->no_migrate) + r_ctx->remote_xprt_resp = l_ctx->local_xprt_req; + else + r_ctx->remote_xprt_resp = max(l_ctx->local_xprt_req, + r_ctx->remote_xprt_req); + } + + if (l_ctx) + rwref_put(&l_ctx->ch_state_lhc0); + + return r_ctx->remote_xprt_resp; +} + +/** + * glink_core_rx_cmd_ch_remote_open() - Remote-initiated open command + * + * @if_ptr: Pointer to transport instance + * @rcid: Remote Channel ID + * @name: Channel name + * @req_xprt: Requested transport to migrate to + */ +static void glink_core_rx_cmd_ch_remote_open(struct glink_transport_if *if_ptr, + uint32_t rcid, const char *name, uint16_t req_xprt) +{ + struct channel_ctx *ctx; + uint16_t xprt_resp; + bool do_migrate; + + ctx = ch_name_to_ch_ctx_create(if_ptr->glink_core_priv, name); + if (ctx == NULL) { + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid %u received, name '%s'\n", + __func__, rcid, name); + return; + } + + /* port already exists */ + if (ctx->remote_opened) { + GLINK_ERR_CH(ctx, + "%s: Duplicate remote open for rcid %u, name '%s'\n", + __func__, rcid, name); + return; + } + + ctx->remote_opened = true; + ch_add_rcid(if_ptr->glink_core_priv, ctx, rcid); + ctx->transport_ptr = if_ptr->glink_core_priv; + + ctx->remote_xprt_req = req_xprt; + xprt_resp = calculate_xprt_resp(ctx); + + do_migrate = will_migrate(NULL, ctx); + GLINK_INFO_CH(ctx, "%s: remote: CLOSED->OPENED ; xprt req:resp %u:%u\n", + __func__, req_xprt, xprt_resp); + + if_ptr->tx_cmd_ch_remote_open_ack(if_ptr, rcid, xprt_resp); + if (!do_migrate && ch_is_fully_opened(ctx)) + ctx->notify_state(ctx, ctx->user_priv, GLINK_CONNECTED); + + + if (do_migrate) + ch_migrate(NULL, ctx); +} + +/** + * glink_core_rx_cmd_ch_open_ack() - Receive ack to previously sent open request + * + * if_ptr: Pointer to transport instance + * lcid: Local Channel ID + * @xprt_resp: Response to the transport migration request + */ +static void glink_core_rx_cmd_ch_open_ack(struct glink_transport_if *if_ptr, + uint32_t lcid, uint16_t xprt_resp) +{ + struct channel_ctx *ctx; + + ctx = xprt_lcid_to_ch_ctx_get(if_ptr->glink_core_priv, lcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid lcid %u received\n", __func__, + (unsigned)lcid); + return; + } + + if (ctx->local_open_state != GLINK_CHANNEL_OPENING) { + GLINK_ERR_CH(ctx, + "%s: unexpected open ack receive for lcid. Current state: %u. Thread: %u\n", + __func__, ctx->local_open_state, current->pid); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + ctx->local_xprt_resp = xprt_resp; + if (!ch_migrate(ctx, NULL)) { + ctx->local_open_state = GLINK_CHANNEL_OPENED; + GLINK_INFO_PERF_CH(ctx, + "%s: local:GLINK_CHANNEL_OPENING_WAIT->GLINK_CHANNEL_OPENED\n", + __func__); + + if (ch_is_fully_opened(ctx)) { + ctx->notify_state(ctx, ctx->user_priv, GLINK_CONNECTED); + GLINK_INFO_PERF_CH(ctx, + "%s: notify state: GLINK_CONNECTED\n", + __func__); + } + } + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_cmd_ch_remote_close() - Receive remote close command + * + * if_ptr: Pointer to transport instance + * rcid: Remote Channel ID + */ +static void glink_core_rx_cmd_ch_remote_close( + struct glink_transport_if *if_ptr, uint32_t rcid) +{ + struct channel_ctx *ctx; + bool is_ch_fully_closed; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid %u received\n", __func__, + (unsigned)rcid); + return; + } + + if (!ctx->remote_opened) { + GLINK_ERR_CH(ctx, + "%s: unexpected remote close receive for rcid %u\n", + __func__, (unsigned)rcid); + rwref_put(&ctx->ch_state_lhc0); + return; + } + GLINK_INFO_CH(ctx, "%s: remote: OPENED->CLOSED\n", __func__); + + is_ch_fully_closed = glink_core_remote_close_common(ctx); + + ctx->pending_delete = true; + if_ptr->tx_cmd_ch_remote_close_ack(if_ptr, rcid); + + if (is_ch_fully_closed) { + glink_delete_ch_from_list(ctx, true); + flush_workqueue(ctx->transport_ptr->tx_wq); + } + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_cmd_ch_close_ack() - Receive locally-request close ack + * + * if_ptr: Pointer to transport instance + * lcid: Local Channel ID + */ +static void glink_core_rx_cmd_ch_close_ack(struct glink_transport_if *if_ptr, + uint32_t lcid) +{ + struct channel_ctx *ctx; + bool is_ch_fully_closed; + + ctx = xprt_lcid_to_ch_ctx_get(if_ptr->glink_core_priv, lcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid lcid %u received\n", __func__, + (unsigned)lcid); + return; + } + + if (ctx->local_open_state != GLINK_CHANNEL_CLOSING) { + GLINK_ERR_CH(ctx, + "%s: unexpected close ack receive for lcid %u\n", + __func__, (unsigned)lcid); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + is_ch_fully_closed = glink_core_ch_close_ack_common(ctx); + if (is_ch_fully_closed) { + glink_delete_ch_from_list(ctx, true); + flush_workqueue(ctx->transport_ptr->tx_wq); + } + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_remote_rx_intent_put() - Receive remove intent + * + * @if_ptr: Pointer to transport instance + * @rcid: Remote Channel ID + * @riid: Remote Intent ID + * @size: Size of the remote intent ID + */ +static void glink_core_remote_rx_intent_put(struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t riid, size_t size) +{ + struct channel_ctx *ctx; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown rcid received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid received %u\n", __func__, + (unsigned)rcid); + return; + } + + ch_push_remote_rx_intent(ctx, size, riid); + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_cmd_remote_rx_intent_req() - Receive a request for rx_intent + * from remote side + * if_ptr: Pointer to the transport interface + * rcid: Remote channel ID + * size: size of the intent + * + * The function searches for the local channel to which the request for + * rx_intent has arrived and informs this request to the local channel through + * notify_rx_intent_req callback registered by the local channel. + */ +static void glink_core_rx_cmd_remote_rx_intent_req( + struct glink_transport_if *if_ptr, uint32_t rcid, size_t size) +{ + struct channel_ctx *ctx; + bool cb_ret; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid received %u\n", __func__, + (unsigned)rcid); + return; + } + if (!ctx->notify_rx_intent_req) { + GLINK_ERR_CH(ctx, + "%s: Notify function not defined for local channel", + __func__); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + cb_ret = ctx->notify_rx_intent_req(ctx, ctx->user_priv, size); + if_ptr->tx_cmd_remote_rx_intent_req_ack(if_ptr, ctx->lcid, cb_ret); + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_cmd_remote_rx_intent_req_ack()- Receive ack from remote side + * for a local rx_intent request + * if_ptr: Pointer to the transport interface + * rcid: Remote channel ID + * size: size of the intent + * + * This function receives the ack for rx_intent request from local channel. + */ +static void glink_core_rx_cmd_rx_intent_req_ack(struct glink_transport_if + *if_ptr, uint32_t rcid, bool granted) +{ + struct channel_ctx *ctx; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: Invalid rcid received %u\n", __func__, + (unsigned)rcid); + return; + } + ctx->int_req_ack = granted; + complete_all(&ctx->int_req_ack_complete); + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_get_pkt_ctx() - lookup RX intent structure + * + * if_ptr: Pointer to the transport interface + * rcid: Remote channel ID + * liid: Local RX Intent ID + * + * Note that this function is designed to always be followed by a call to + * glink_core_rx_put_pkt_ctx() to complete an RX operation by the transport. + * + * Return: Pointer to RX intent structure (or NULL if none found) + */ +static struct glink_core_rx_intent *glink_core_rx_get_pkt_ctx( + struct glink_transport_if *if_ptr, uint32_t rcid, uint32_t liid) +{ + struct channel_ctx *ctx; + struct glink_core_rx_intent *intent_ptr; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid received %u\n", __func__, + (unsigned)rcid); + return NULL; + } + + /* match pending intent */ + intent_ptr = ch_get_local_rx_intent(ctx, liid); + if (intent_ptr == NULL) { + GLINK_ERR_CH(ctx, + "%s: L[%u]: No matching rx intent\n", + __func__, liid); + rwref_put(&ctx->ch_state_lhc0); + return NULL; + } + + rwref_put(&ctx->ch_state_lhc0); + return intent_ptr; +} + +/** + * glink_core_rx_put_pkt_ctx() - lookup RX intent structure + * + * if_ptr: Pointer to the transport interface + * rcid: Remote channel ID + * intent_ptr: Pointer to the RX intent + * complete: Packet has been completely received + * + * Note that this function should always be preceded by a call to + * glink_core_rx_get_pkt_ctx(). + */ +void glink_core_rx_put_pkt_ctx(struct glink_transport_if *if_ptr, + uint32_t rcid, struct glink_core_rx_intent *intent_ptr, bool complete) +{ + struct channel_ctx *ctx; + + if (!complete) { + GLINK_DBG_XPRT(if_ptr->glink_core_priv, + "%s: rcid[%u] liid[%u] pkt_size[%zu] write_offset[%zu] Fragment received\n", + __func__, rcid, intent_ptr->id, + intent_ptr->pkt_size, + intent_ptr->write_offset); + return; + } + + /* packet complete */ + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid received %u\n", __func__, + (unsigned)rcid); + return; + } + + if (unlikely(intent_ptr->tracer_pkt)) { + tracer_pkt_log_event(intent_ptr->data, GLINK_CORE_RX); + ch_set_local_rx_intent_notified(ctx, intent_ptr); + if (ctx->notify_rx_tracer_pkt) + ctx->notify_rx_tracer_pkt(ctx, ctx->user_priv, + intent_ptr->pkt_priv, intent_ptr->data, + intent_ptr->pkt_size); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + GLINK_PERF_CH(ctx, "%s: L[%u]: data[%p] size[%zu]\n", + __func__, intent_ptr->id, + intent_ptr->data ? intent_ptr->data : intent_ptr->iovec, + intent_ptr->write_offset); + if (!intent_ptr->data && !ctx->notify_rxv) { + /* Received a vector, but client can't handle a vector */ + intent_ptr->bounce_buf = linearize_vector(intent_ptr->iovec, + intent_ptr->pkt_size, + intent_ptr->vprovider, + intent_ptr->pprovider); + if (IS_ERR_OR_NULL(intent_ptr->bounce_buf)) { + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: Error %ld linearizing vector\n", __func__, + PTR_ERR(intent_ptr->bounce_buf)); + BUG(); + rwref_put(&ctx->ch_state_lhc0); + return; + } + } + + ch_set_local_rx_intent_notified(ctx, intent_ptr); + if (ctx->notify_rx && (intent_ptr->data || intent_ptr->bounce_buf)) { + ctx->notify_rx(ctx, intent_ptr->data ? + intent_ptr->data : intent_ptr->bounce_buf, + intent_ptr->pkt_size); + } else if (ctx->notify_rxv) { + ctx->notify_rxv(ctx, ctx->user_priv, intent_ptr->pkt_priv, + intent_ptr->iovec, intent_ptr->pkt_size, + intent_ptr->vprovider, intent_ptr->pprovider); + } else { + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: Unable to process rx data\n", __func__); + BUG(); + } + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * glink_core_rx_cmd_tx_done() - Receive Transmit Done Command + * @xprt_ptr: Transport to send packet on. + * @rcid: Remote channel ID + * @riid: Remote intent ID + * @reuse: Reuse the consumed intent + */ +void glink_core_rx_cmd_tx_done(struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t riid, bool reuse) +{ + struct channel_ctx *ctx; + struct glink_core_tx_pkt *tx_pkt; + unsigned long flags; + size_t intent_size; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown RCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid %u received\n", __func__, + rcid); + return; + } + + spin_lock_irqsave(&ctx->tx_lists_lock_lhc3, flags); + tx_pkt = ch_get_tx_pending_remote_done(ctx, riid); + if (IS_ERR_OR_NULL(tx_pkt)) { + /* + * FUTURE - in the case of a zero-copy transport, this is a + * fatal protocol failure since memory corruption could occur + * in this case. Prevent this by adding code in glink_close() + * to recall any buffers in flight / wait for them to be + * returned. + */ + GLINK_ERR_CH(ctx, "%s: R[%u]: No matching tx\n", + __func__, + (unsigned)riid); + spin_unlock_irqrestore(&ctx->tx_lists_lock_lhc3, flags); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + /* notify client */ + ctx->notify_tx_done(ctx, ctx->user_priv, tx_pkt->pkt_priv, + tx_pkt->data ? tx_pkt->data : tx_pkt->iovec); + intent_size = tx_pkt->intent_size; + ch_remove_tx_pending_remote_done(ctx, tx_pkt); + spin_unlock_irqrestore(&ctx->tx_lists_lock_lhc3, flags); + + if (reuse) + ch_push_remote_rx_intent(ctx, intent_size, riid); + rwref_put(&ctx->ch_state_lhc0); +} + +/** + * xprt_schedule_tx() - Schedules packet for transmit. + * @xprt_ptr: Transport to send packet on. + * @ch_ptr: Channel to send packet on. + * @tx_info: Packet to transmit. + */ +static void xprt_schedule_tx(struct glink_core_xprt_ctx *xprt_ptr, + struct channel_ctx *ch_ptr, + struct glink_core_tx_pkt *tx_info) +{ + unsigned long flags; + + if (unlikely(xprt_ptr->local_state == GLINK_XPRT_DOWN)) { + GLINK_ERR_CH(ch_ptr, "%s: Error XPRT is down\n", __func__); + kfree(tx_info); + return; + } + + spin_lock_irqsave(&xprt_ptr->tx_ready_lock_lhb2, flags); + if (unlikely(!ch_is_fully_opened(ch_ptr))) { + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, flags); + GLINK_ERR_CH(ch_ptr, "%s: Channel closed before tx\n", + __func__); + kfree(tx_info); + return; + } + if (list_empty(&ch_ptr->tx_ready_list_node)) + list_add_tail(&ch_ptr->tx_ready_list_node, + &xprt_ptr->prio_bin[ch_ptr->curr_priority].tx_ready); + + spin_lock(&ch_ptr->tx_lists_lock_lhc3); + list_add_tail(&tx_info->list_node, &ch_ptr->tx_active); + glink_qos_do_ch_tx(ch_ptr); + if (unlikely(tx_info->tracer_pkt)) + tracer_pkt_log_event((void *)(tx_info->data), + GLINK_QUEUE_TO_SCHEDULER); + + spin_unlock(&ch_ptr->tx_lists_lock_lhc3); + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, flags); + + queue_work(xprt_ptr->tx_wq, &xprt_ptr->tx_work); +} + +/** + * xprt_single_threaded_tx() - Transmit in the context of sender. + * @xprt_ptr: Transport to send packet on. + * @ch_ptr: Channel to send packet on. + * @tx_info: Packet to transmit. + */ +static int xprt_single_threaded_tx(struct glink_core_xprt_ctx *xprt_ptr, + struct channel_ctx *ch_ptr, + struct glink_core_tx_pkt *tx_info) +{ + int ret; + unsigned long flags; + + spin_lock_irqsave(&ch_ptr->tx_pending_rmt_done_lock_lhc4, flags); + do { + ret = xprt_ptr->ops->tx(ch_ptr->transport_ptr->ops, + ch_ptr->lcid, tx_info); + } while (ret == -EAGAIN); + if (ret < 0 || tx_info->size_remaining) { + GLINK_ERR_CH(ch_ptr, "%s: Error %d writing data\n", + __func__, ret); + kfree(tx_info); + } else { + list_add_tail(&tx_info->list_done, + &ch_ptr->tx_pending_remote_done); + ret = 0; + } + spin_unlock_irqrestore(&ch_ptr->tx_pending_rmt_done_lock_lhc4, flags); + return ret; +} + +/** + * glink_scheduler_eval_prio() - Evaluate the channel priority + * @ctx: Channel whose priority is evaluated. + * @xprt_ctx: Transport in which the channel is part of. + * + * This function is called by the packet scheduler to measure the traffic + * rate observed in the channel and compare it against the traffic rate + * requested by the channel. The comparison result is used to evaluate the + * priority of the channel. + */ +static void glink_scheduler_eval_prio(struct channel_ctx *ctx, + struct glink_core_xprt_ctx *xprt_ctx) +{ + unsigned long token_end_time; + unsigned long token_consume_time, rem; + unsigned long obs_rate_kBps; + + if (ctx->initial_priority == 0) + return; + + if (ctx->token_count) + return; + + token_end_time = arch_counter_get_cntpct(); + + token_consume_time = NSEC_PER_SEC; + rem = do_div(token_consume_time, arch_timer_get_rate()); + token_consume_time = (token_end_time - ctx->token_start_time) * + token_consume_time; + rem = do_div(token_consume_time, 1000); + obs_rate_kBps = glink_qos_calc_rate_kBps(ctx->txd_len, + token_consume_time); + if (obs_rate_kBps > ctx->req_rate_kBps) { + GLINK_INFO_CH(ctx, "%s: Obs. Rate (%lu) > Req. Rate (%lu)\n", + __func__, obs_rate_kBps, ctx->req_rate_kBps); + glink_qos_update_ch_prio(ctx, 0); + } else { + glink_qos_update_ch_prio(ctx, ctx->initial_priority); + } + + ctx->token_count = xprt_ctx->token_count; + ctx->txd_len = 0; + ctx->token_start_time = arch_counter_get_cntpct(); +} + +/** + * glink_scheduler_tx() - Transmit operation by the scheduler + * @ctx: Channel which is scheduled for transmission. + * @xprt_ctx: Transport context in which the transmission is performed. + * + * This function is called by the scheduler after scheduling a channel for + * transmission over the transport. + * + * Return: return value as returned by the transport on success, + * standard Linux error codes on failure. + */ +static int glink_scheduler_tx(struct channel_ctx *ctx, + struct glink_core_xprt_ctx *xprt_ctx) +{ + unsigned long flags; + struct glink_core_tx_pkt *tx_info; + size_t txd_len = 0; + size_t tx_len = 0; + uint32_t num_pkts = 0; + int ret; + + spin_lock_irqsave(&ctx->tx_lists_lock_lhc3, flags); + while (txd_len < xprt_ctx->mtu && + !list_empty(&ctx->tx_active)) { + tx_info = list_first_entry(&ctx->tx_active, + struct glink_core_tx_pkt, list_node); + rwref_get(&tx_info->pkt_ref); + + spin_lock(&ctx->tx_pending_rmt_done_lock_lhc4); + if (list_empty(&tx_info->list_done)) + list_add(&tx_info->list_done, + &ctx->tx_pending_remote_done); + spin_unlock(&ctx->tx_pending_rmt_done_lock_lhc4); + spin_unlock_irqrestore(&ctx->tx_lists_lock_lhc3, flags); + + if (unlikely(tx_info->tracer_pkt)) { + tracer_pkt_log_event((void *)(tx_info->data), + GLINK_SCHEDULER_TX); + ret = xprt_ctx->ops->tx_cmd_tracer_pkt(xprt_ctx->ops, + ctx->lcid, tx_info); + } else { + tx_len = tx_info->size_remaining < + (xprt_ctx->mtu - txd_len) ? + tx_info->size_remaining : + (xprt_ctx->mtu - txd_len); + tx_info->tx_len = tx_len; + ret = xprt_ctx->ops->tx(xprt_ctx->ops, + ctx->lcid, tx_info); + } + spin_lock_irqsave(&ctx->tx_lists_lock_lhc3, flags); + if (ret == -EAGAIN) { + /* + * transport unable to send at the moment and will call + * tx_resume() when it can send again. + */ + rwref_put(&tx_info->pkt_ref); + break; + } else if (ret < 0) { + /* + * General failure code that indicates that the + * transport is unable to recover. In this case, the + * communication failure will be detected at a higher + * level and a subsystem restart of the affected system + * will be triggered. + */ + GLINK_ERR_XPRT(xprt_ctx, + "%s: unrecoverable xprt failure %d\n", + __func__, ret); + rwref_put(&tx_info->pkt_ref); + break; + } else if (!ret && tx_info->size_remaining) { + /* + * Transport unable to send any data on this channel. + * Break out of the loop so that the scheduler can + * continue with the next channel. + */ + break; + } else { + txd_len += tx_len; + } + + if (!tx_info->size_remaining) { + num_pkts++; + list_del_init(&tx_info->list_node); + rwref_put(&tx_info->pkt_ref); + } + } + + ctx->txd_len += txd_len; + if (txd_len) { + if (num_pkts >= ctx->token_count) + ctx->token_count = 0; + else if (num_pkts) + ctx->token_count -= num_pkts; + else + ctx->token_count--; + } + spin_unlock_irqrestore(&ctx->tx_lists_lock_lhc3, flags); + + return ret; +} + +/** + * tx_work_func() - Transmit worker + * @work: Linux work structure + */ +static void tx_work_func(struct work_struct *work) +{ + struct glink_core_xprt_ctx *xprt_ptr = + container_of(work, struct glink_core_xprt_ctx, tx_work); + struct channel_ctx *ch_ptr; + uint32_t prio; + uint32_t tx_ready_head_prio = 0; + struct channel_ctx *tx_ready_head = NULL; + bool transmitted_successfully = true; + unsigned long flags; + int ret = 0; + + GLINK_PERF("%s: worker starting\n", __func__); + + while (1) { + prio = xprt_ptr->num_priority - 1; + spin_lock_irqsave(&xprt_ptr->tx_ready_lock_lhb2, flags); + while (list_empty(&xprt_ptr->prio_bin[prio].tx_ready)) { + if (prio == 0) { + spin_unlock_irqrestore( + &xprt_ptr->tx_ready_lock_lhb2, flags); + return; + } + prio--; + } + glink_pm_qos_vote(xprt_ptr); + ch_ptr = list_first_entry(&xprt_ptr->prio_bin[prio].tx_ready, + struct channel_ctx, tx_ready_list_node); + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, flags); + + if (tx_ready_head == NULL || tx_ready_head_prio < prio) { + tx_ready_head = ch_ptr; + tx_ready_head_prio = prio; + } + + if (ch_ptr == tx_ready_head && !transmitted_successfully) { + GLINK_ERR_XPRT(xprt_ptr, + "%s: Unable to send data on this transport.\n", + __func__); + break; + } + transmitted_successfully = false; + + ret = glink_scheduler_tx(ch_ptr, xprt_ptr); + if (ret == -EAGAIN) { + /* + * transport unable to send at the moment and will call + * tx_resume() when it can send again. + */ + break; + } else if (ret < 0) { + /* + * General failure code that indicates that the + * transport is unable to recover. In this case, the + * communication failure will be detected at a higher + * level and a subsystem restart of the affected system + * will be triggered. + */ + GLINK_ERR_XPRT(xprt_ptr, + "%s: unrecoverable xprt failure %d\n", + __func__, ret); + break; + } else if (!ret) { + /* + * Transport unable to send any data on this channel, + * but didn't return an error. Move to the next channel + * and continue. + */ + spin_lock_irqsave(&xprt_ptr->tx_ready_lock_lhb2, flags); + list_rotate_left(&xprt_ptr->prio_bin[prio].tx_ready); + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, + flags); + continue; + } + + spin_lock_irqsave(&xprt_ptr->tx_ready_lock_lhb2, flags); + spin_lock(&ch_ptr->tx_lists_lock_lhc3); + + glink_scheduler_eval_prio(ch_ptr, xprt_ptr); + if (list_empty(&ch_ptr->tx_active)) { + list_del_init(&ch_ptr->tx_ready_list_node); + glink_qos_done_ch_tx(ch_ptr); + } + + spin_unlock(&ch_ptr->tx_lists_lock_lhc3); + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, flags); + + tx_ready_head = NULL; + transmitted_successfully = true; + } + glink_pm_qos_unvote(xprt_ptr); + GLINK_PERF("%s: worker exiting\n", __func__); +} + +static void glink_core_tx_resume(struct glink_transport_if *if_ptr) +{ + queue_work(if_ptr->glink_core_priv->tx_wq, + &if_ptr->glink_core_priv->tx_work); +} + +/** + * glink_pm_qos_vote() - Add Power Management QoS Vote + * @xprt_ptr: Transport for power vote + * + * Note - must be called with tx_ready_lock_lhb2 locked. + */ +static void glink_pm_qos_vote(struct glink_core_xprt_ctx *xprt_ptr) +{ + if (glink_pm_qos && !xprt_ptr->qos_req_active) { + GLINK_PERF("%s: qos vote %u us\n", __func__, glink_pm_qos); + pm_qos_update_request(&xprt_ptr->pm_qos_req, glink_pm_qos); + xprt_ptr->qos_req_active = true; + } + xprt_ptr->tx_path_activity = true; +} + +/** + * glink_pm_qos_unvote() - Schedule Power Management QoS Vote Removal + * @xprt_ptr: Transport for power vote removal + * + * Note - must be called with tx_ready_lock_lhb2 locked. + */ +static void glink_pm_qos_unvote(struct glink_core_xprt_ctx *xprt_ptr) +{ + xprt_ptr->tx_path_activity = false; + if (xprt_ptr->qos_req_active) { + GLINK_PERF("%s: qos unvote\n", __func__); + schedule_delayed_work(&xprt_ptr->pm_qos_work, + msecs_to_jiffies(GLINK_PM_QOS_HOLDOFF_MS)); + } +} + +/** + * glink_pm_qos_cancel_worker() - Remove Power Management QoS Vote + * @work: Delayed work structure + * + * Removes PM QoS vote if no additional transmit activity has occurred between + * the unvote and when this worker runs. + */ +static void glink_pm_qos_cancel_worker(struct work_struct *work) +{ + struct glink_core_xprt_ctx *xprt_ptr; + unsigned long flags; + + xprt_ptr = container_of(to_delayed_work(work), + struct glink_core_xprt_ctx, pm_qos_work); + + spin_lock_irqsave(&xprt_ptr->tx_ready_lock_lhb2, flags); + if (!xprt_ptr->tx_path_activity) { + /* no more tx activity */ + GLINK_PERF("%s: qos off\n", __func__); + pm_qos_update_request(&xprt_ptr->pm_qos_req, + PM_QOS_DEFAULT_VALUE); + xprt_ptr->qos_req_active = false; + } + xprt_ptr->tx_path_activity = false; + spin_unlock_irqrestore(&xprt_ptr->tx_ready_lock_lhb2, flags); +} + +/** + * glink_core_rx_cmd_remote_sigs() - Receive remote channel signal command + * + * if_ptr: Pointer to transport instance + * rcid: Remote Channel ID + */ +static void glink_core_rx_cmd_remote_sigs(struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t sigs) +{ + struct channel_ctx *ctx; + uint32_t old_sigs; + + ctx = xprt_rcid_to_ch_ctx_get(if_ptr->glink_core_priv, rcid); + if (!ctx) { + /* unknown LCID received - this shouldn't happen */ + GLINK_ERR_XPRT(if_ptr->glink_core_priv, + "%s: invalid rcid %u received\n", __func__, + (unsigned)rcid); + return; + } + + if (!ch_is_fully_opened(ctx)) { + GLINK_ERR_CH(ctx, "%s: Channel is not fully opened\n", + __func__); + rwref_put(&ctx->ch_state_lhc0); + return; + } + + old_sigs = ctx->rsigs; + ctx->rsigs = sigs; + if (ctx->notify_rx_sigs) { + ctx->notify_rx_sigs(ctx, ctx->user_priv, old_sigs, ctx->rsigs); + GLINK_INFO_CH(ctx, "%s: notify rx sigs old:0x%x new:0x%x\n", + __func__, old_sigs, ctx->rsigs); + } + rwref_put(&ctx->ch_state_lhc0); +} + +static struct glink_core_if core_impl = { + .link_up = glink_core_link_up, + .link_down = glink_core_link_down, + .rx_cmd_version = glink_core_rx_cmd_version, + .rx_cmd_version_ack = glink_core_rx_cmd_version_ack, + .rx_cmd_ch_remote_open = glink_core_rx_cmd_ch_remote_open, + .rx_cmd_ch_open_ack = glink_core_rx_cmd_ch_open_ack, + .rx_cmd_ch_remote_close = glink_core_rx_cmd_ch_remote_close, + .rx_cmd_ch_close_ack = glink_core_rx_cmd_ch_close_ack, + .rx_get_pkt_ctx = glink_core_rx_get_pkt_ctx, + .rx_put_pkt_ctx = glink_core_rx_put_pkt_ctx, + .rx_cmd_remote_rx_intent_put = glink_core_remote_rx_intent_put, + .rx_cmd_remote_rx_intent_req = glink_core_rx_cmd_remote_rx_intent_req, + .rx_cmd_rx_intent_req_ack = glink_core_rx_cmd_rx_intent_req_ack, + .rx_cmd_tx_done = glink_core_rx_cmd_tx_done, + .tx_resume = glink_core_tx_resume, + .rx_cmd_remote_sigs = glink_core_rx_cmd_remote_sigs, +}; + +/** + * glink_xprt_ctx_iterator_init() - Initializes the transport context list iterator + * @xprt_i: pointer to the transport context iterator. + * + * This function acquires the transport context lock which must then be + * released by glink_xprt_ctx_iterator_end() + */ +void glink_xprt_ctx_iterator_init(struct xprt_ctx_iterator *xprt_i) +{ + if (xprt_i == NULL) + return; + + mutex_lock(&transport_list_lock_lha0); + xprt_i->xprt_list = &transport_list; + xprt_i->i_curr = list_entry(&transport_list, + struct glink_core_xprt_ctx, list_node); +} +EXPORT_SYMBOL(glink_xprt_ctx_iterator_init); + +/** + * glink_xprt_ctx_iterator_end() - Ends the transport context list iteration + * @xprt_i: pointer to the transport context iterator. + */ +void glink_xprt_ctx_iterator_end(struct xprt_ctx_iterator *xprt_i) +{ + if (xprt_i == NULL) + return; + + xprt_i->xprt_list = NULL; + xprt_i->i_curr = NULL; + mutex_unlock(&transport_list_lock_lha0); +} +EXPORT_SYMBOL(glink_xprt_ctx_iterator_end); + +/** + * glink_xprt_ctx_iterator_next() - iterates element by element in transport context list + * @xprt_i: pointer to the transport context iterator. + * + * Return: pointer to the transport context structure + */ +struct glink_core_xprt_ctx *glink_xprt_ctx_iterator_next( + struct xprt_ctx_iterator *xprt_i) +{ + struct glink_core_xprt_ctx *xprt_ctx = NULL; + + if (xprt_i == NULL) + return xprt_ctx; + + if (list_empty(xprt_i->xprt_list)) + return xprt_ctx; + + list_for_each_entry_continue(xprt_i->i_curr, + xprt_i->xprt_list, list_node) { + xprt_ctx = xprt_i->i_curr; + break; + } + return xprt_ctx; +} +EXPORT_SYMBOL(glink_xprt_ctx_iterator_next); + +/** + * glink_get_xprt_name() - get the transport name + * @xprt_ctx: pointer to the transport context. + * + * Return: name of the transport + */ +char *glink_get_xprt_name(struct glink_core_xprt_ctx *xprt_ctx) +{ + if (xprt_ctx == NULL) + return NULL; + + return xprt_ctx->name; +} +EXPORT_SYMBOL(glink_get_xprt_name); + +/** + * glink_get_xprt_name() - get the name of the remote processor/edge + * of the transport + * @xprt_ctx: pointer to the transport context. + * + * Return: Name of the remote processor/edge + */ +char *glink_get_xprt_edge_name(struct glink_core_xprt_ctx *xprt_ctx) +{ + if (xprt_ctx == NULL) + return NULL; + return xprt_ctx->edge; +} +EXPORT_SYMBOL(glink_get_xprt_edge_name); + +/** + * glink_get_xprt_state() - get the state of the transport + * @xprt_ctx: pointer to the transport context. + * + * Return: Name of the transport state, NULL in case of invalid input + */ +const char *glink_get_xprt_state(struct glink_core_xprt_ctx *xprt_ctx) +{ + if (xprt_ctx == NULL) + return NULL; + + return glink_get_xprt_state_string(xprt_ctx->local_state); +} +EXPORT_SYMBOL(glink_get_xprt_state); + +/** + * glink_get_xprt_version_features() - get the version and feature set + * of local transport in glink + * @xprt_ctx: pointer to the transport context. + * + * Return: pointer to the glink_core_version + */ +const struct glink_core_version *glink_get_xprt_version_features( + struct glink_core_xprt_ctx *xprt_ctx) +{ + const struct glink_core_version *ver = NULL; + if (xprt_ctx == NULL) + return ver; + + ver = &xprt_ctx->versions[xprt_ctx->local_version_idx]; + return ver; +} +EXPORT_SYMBOL(glink_get_xprt_version_features); + +/** + * glink_ch_ctx_iterator_init() - Initializes the channel context list iterator + * @ch_iter: pointer to the channel context iterator. + * xprt: pointer to the transport context that holds the channel list + * + * This function acquires the channel context lock which must then be + * released by glink_ch_ctx_iterator_end() + */ +void glink_ch_ctx_iterator_init(struct ch_ctx_iterator *ch_iter, + struct glink_core_xprt_ctx *xprt) +{ + unsigned long flags; + + if (ch_iter == NULL || xprt == NULL) + return; + + spin_lock_irqsave(&xprt->xprt_ctx_lock_lhb1, flags); + ch_iter->ch_list = &(xprt->channels); + ch_iter->i_curr = list_entry(&(xprt->channels), + struct channel_ctx, port_list_node); + ch_iter->ch_list_flags = flags; +} +EXPORT_SYMBOL(glink_ch_ctx_iterator_init); + +/** + * glink_ch_ctx_iterator_end() - Ends the channel context list iteration + * @ch_iter: pointer to the channel context iterator. + */ +void glink_ch_ctx_iterator_end(struct ch_ctx_iterator *ch_iter, + struct glink_core_xprt_ctx *xprt) +{ + if (ch_iter == NULL || xprt == NULL) + return; + + spin_unlock_irqrestore(&xprt->xprt_ctx_lock_lhb1, + ch_iter->ch_list_flags); + ch_iter->ch_list = NULL; + ch_iter->i_curr = NULL; +} +EXPORT_SYMBOL(glink_ch_ctx_iterator_end); + +/** + * glink_ch_ctx_iterator_next() - iterates element by element in channel context list + * @c_i: pointer to the channel context iterator. + * + * Return: pointer to the channel context structure + */ +struct channel_ctx *glink_ch_ctx_iterator_next(struct ch_ctx_iterator *c_i) +{ + struct channel_ctx *ch_ctx = NULL; + + if (c_i == NULL) + return ch_ctx; + + if (list_empty(c_i->ch_list)) + return ch_ctx; + + list_for_each_entry_continue(c_i->i_curr, + c_i->ch_list, port_list_node) { + ch_ctx = c_i->i_curr; + break; + } + return ch_ctx; +} +EXPORT_SYMBOL(glink_ch_ctx_iterator_next); + +/** + * glink_get_ch_name() - get the channel name + * @ch_ctx: pointer to the channel context. + * + * Return: name of the channel, NULL in case of invalid input + */ +char *glink_get_ch_name(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return NULL; + + return ch_ctx->name; +} +EXPORT_SYMBOL(glink_get_ch_name); + +/** + * glink_get_ch_edge_name() - get the edge on whcih channel is created + * @ch_ctx: pointer to the channel context. + * + * Return: name of the edge, NULL in case of invalid input + */ +char *glink_get_ch_edge_name(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return NULL; + + return ch_ctx->transport_ptr->edge; +} +EXPORT_SYMBOL(glink_get_ch_edge_name); + +/** + * glink_get_ch_lcid() - get the local channel ID + * @c_i: pointer to the channel context. + * + * Return: local channel id, -EINVAL in case of invalid input + */ +int glink_get_ch_lcid(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return -EINVAL; + + return ch_ctx->lcid; +} +EXPORT_SYMBOL(glink_get_ch_lcid); + +/** + * glink_get_ch_rcid() - get the remote channel ID + * @ch_ctx: pointer to the channel context. + * + * Return: remote channel id, -EINVAL in case of invalid input + */ +int glink_get_ch_rcid(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return -EINVAL; + + return ch_ctx->rcid; +} +EXPORT_SYMBOL(glink_get_ch_rcid); + +/** + * glink_get_ch_lstate() - get the local channel state + * @ch_ctx: pointer to the channel context. + * + * Return: Name of the local channel state, NUll in case of invalid input + */ +const char *glink_get_ch_lstate(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return NULL; + + return glink_get_ch_state_string(ch_ctx->local_open_state); +} +EXPORT_SYMBOL(glink_get_ch_lstate); + +/** + * glink_get_ch_rstate() - get the remote channel state + * @ch_ctx: pointer to the channel context. + * + * Return: true if remote side is opened false otherwise + */ +bool glink_get_ch_rstate(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return NULL; + + return ch_ctx->remote_opened; +} +EXPORT_SYMBOL(glink_get_ch_rstate); + +/** + * glink_get_ch_xprt_name() - get the name of the transport to which + * the channel belongs + * @ch_ctx: pointer to the channel context. + * + * Return: name of the export, NULL in case of invalid input + */ +char *glink_get_ch_xprt_name(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return NULL; + + return ch_ctx->transport_ptr->name; +} +EXPORT_SYMBOL(glink_get_ch_xprt_name); + +/** + * glink_get_tx_pkt_count() - get the total number of packets sent + * through this channel + * @ch_ctx: pointer to the channel context. + * + * Return: number of packets transmitted, -EINVAL in case of invalid input + */ +int glink_get_ch_tx_pkt_count(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return -EINVAL; + + /* FUTURE: packet stats not yet implemented */ + + return -ENOSYS; +} +EXPORT_SYMBOL(glink_get_ch_tx_pkt_count); + +/** + * glink_get_ch_rx_pkt_count() - get the total number of packets + * recieved at this channel + * @ch_ctx: pointer to the channel context. + * + * Return: number of packets recieved, -EINVAL in case of invalid input + */ +int glink_get_ch_rx_pkt_count(struct channel_ctx *ch_ctx) +{ + if (ch_ctx == NULL) + return -EINVAL; + + /* FUTURE: packet stats not yet implemented */ + + return -ENOSYS; +} +EXPORT_SYMBOL(glink_get_ch_rx_pkt_count); + +/** + * glink_get_ch_lintents_queued() - get the total number of intents queued + * at local side + * @ch_ctx: pointer to the channel context. + * + * Return: number of intents queued, -EINVAL in case of invalid input + */ +int glink_get_ch_lintents_queued(struct channel_ctx *ch_ctx) +{ + struct glink_core_rx_intent *intent; + int ilrx_count = 0; + + if (ch_ctx == NULL) + return -EINVAL; + + list_for_each_entry(intent, &ch_ctx->local_rx_intent_list, list) + ilrx_count++; + + return ilrx_count; +} +EXPORT_SYMBOL(glink_get_ch_lintents_queued); + +/** + * glink_get_ch_rintents_queued() - get the total number of intents queued + * from remote side + * @ch_ctx: pointer to the channel context. + * + * Return: number of intents queued, -EINVAL in case of invalid input + */ +int glink_get_ch_rintents_queued(struct channel_ctx *ch_ctx) +{ + struct glink_core_rx_intent *intent; + int irrx_count = 0; + + if (ch_ctx == NULL) + return -EINVAL; + + list_for_each_entry(intent, &ch_ctx->rmt_rx_intent_list, list) + irrx_count++; + + return irrx_count; +} +EXPORT_SYMBOL(glink_get_ch_rintents_queued); + +/** + * glink_get_ch_intent_info() - get the intent details of a channel + * @ch_ctx: pointer to the channel context. + * ch_ctx_i: pointer to a structure that will contain intent details + * + * This function is used to get all the channel intent details including locks. + */ +void glink_get_ch_intent_info(struct channel_ctx *ch_ctx, + struct glink_ch_intent_info *ch_ctx_i) +{ + if (ch_ctx == NULL || ch_ctx_i == NULL) + return; + + ch_ctx_i->li_lst_lock = &ch_ctx->local_rx_intent_lst_lock_lhc1; + ch_ctx_i->li_avail_list = &ch_ctx->local_rx_intent_list; + ch_ctx_i->li_used_list = &ch_ctx->local_rx_intent_ntfy_list; + ch_ctx_i->ri_lst_lock = &ch_ctx->rmt_rx_intent_lst_lock_lhc2; + ch_ctx_i->ri_list = &ch_ctx->rmt_rx_intent_list; +} +EXPORT_SYMBOL(glink_get_ch_intent_info); + +/** + * glink_get_debug_mask() - Return debug mask attribute + * + * Return: debug mask attribute + */ +unsigned glink_get_debug_mask(void) +{ + return glink_debug_mask; +} +EXPORT_SYMBOL(glink_get_debug_mask); + +/** + * glink_get_log_ctx() - Return log context for other GLINK modules. + * + * Return: Log context or NULL if none. + */ +void *glink_get_log_ctx(void) +{ + return log_ctx; +} +EXPORT_SYMBOL(glink_get_log_ctx); + +/** + * glink_get_xprt_log_ctx() - Return log context for GLINK xprts. + * + * Return: Log context or NULL if none. + */ +void *glink_get_xprt_log_ctx(struct glink_core_xprt_ctx *xprt) +{ + if (xprt) + return xprt->log_ctx; + else + return NULL; +} +EXPORT_SYMBOL(glink_get_xprt_log_ctx); + +void *qcom_glink_get_drvdata(void *ch) +{ + struct channel_ctx *channel = ch; + + return channel->drvdata; +} +EXPORT_SYMBOL(qcom_glink_get_drvdata); + +void qcom_glink_set_drvdata(void *ch, void *data) +{ + struct channel_ctx *channel = ch; + + channel->drvdata = data; +} +EXPORT_SYMBOL(qcom_glink_set_drvdata); + +static int glink_init(void) +{ + log_ctx = ipc_log_context_create(NUM_LOG_PAGES, "glink", 0); + if (!log_ctx) + GLINK_ERR("%s: unable to create log context\n", __func__); + glink_debugfs_init(); + + return 0; +} +arch_initcall(glink_init); + +MODULE_DESCRIPTION("MSM Generic Link (G-Link) Transport"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/soc/qcom/glink_core_if.h b/drivers/soc/qcom/glink_core_if.h new file mode 100644 index 000000000000..93c59d9c4aa1 --- /dev/null +++ b/drivers/soc/qcom/glink_core_if.h @@ -0,0 +1,213 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _SOC_QCOM_GLINK_CORE_IF_H_ +#define _SOC_QCOM_GLINK_CORE_IF_H_ + +#include <linux/of.h> +#include <linux/types.h> +#include "glink_private.h" + +/* Local Channel state */ +enum local_channel_state_e { + GLINK_CHANNEL_CLOSED = 0, + GLINK_CHANNEL_OPENING, + GLINK_CHANNEL_OPENED, + GLINK_CHANNEL_CLOSING, +}; + +/* Transport Negotiation State */ +enum transport_state_e { + GLINK_XPRT_DOWN, + GLINK_XPRT_NEGOTIATING, + GLINK_XPRT_OPENED, + GLINK_XPRT_FAILED, +}; + +struct channel_ctx; +struct glink_core_xprt_ctx; +struct glink_transport_if; +struct glink_core_version; + +/** + * struct glink_core_version - Individual version element + * + * version: supported version + * features: all supported features for version + */ +struct glink_core_version { + uint32_t version; + uint32_t features; + + uint32_t (*negotiate_features)(struct glink_transport_if *if_ptr, + const struct glink_core_version *version_ptr, + uint32_t features); +}; + +/** + * RX intent + * + * data: pointer to the data (may be NULL for zero-copy) + * id: remote or local intent ID + * pkt_size: total size of packet + * write_offset: next write offset (initially 0) + * intent_size: size of the original intent (do not modify) + * tracer_pkt: Flag to indicate if the data is a tracer packet + * iovec: Pointer to vector buffer if the transport passes a vector buffer + * vprovider: Virtual address-space buffer provider for a vector buffer + * pprovider: Physical address-space buffer provider for a vector buffer + * pkt_priv: G-Link core owned packet-private data + * list: G-Link core owned list node + * bounce_buf: Pointer to the temporary/internal bounce buffer + */ +struct glink_core_rx_intent { + void *data; + uint32_t id; + size_t pkt_size; + size_t write_offset; + size_t intent_size; + bool tracer_pkt; + void *iovec; + void * (*vprovider)(void *iovec, size_t offset, size_t *size); + void * (*pprovider)(void *iovec, size_t offset, size_t *size); + + /* G-Link-Core-owned elements - please ignore */ + struct list_head list; + const void *pkt_priv; + void *bounce_buf; +}; + +/** + * struct glink_core_flow_info - Flow specific Information + * @mtu_tx_time_us: Time to transmit an MTU in microseconds. + * @power_state: Power state associated with the traffic flow. + */ +struct glink_core_flow_info { + unsigned long mtu_tx_time_us; + uint32_t power_state; +}; + +/** + * struct glink_core_transport_cfg - configuration of a new transport + * @name: Name of the transport. + * @edge: Subsystem the transport connects to. + * @versions: Array of transport versions supported. + * @versions_entries: Number of entries in @versions. + * @max_cid: Maximum number of channel identifiers supported. + * @max_iid: Maximum number of intent identifiers supported. + * @mtu: MTU supported by this transport. + * @num_flows: Number of traffic flows/priority buckets. + * @flow_info: Information about each flow/priority. + * @token_count: Number of tokens per assignment. + */ +struct glink_core_transport_cfg { + const char *name; + const char *edge; + const struct glink_core_version *versions; + size_t versions_entries; + uint32_t max_cid; + uint32_t max_iid; + + size_t mtu; + uint32_t num_flows; + struct glink_core_flow_info *flow_info; + uint32_t token_count; +}; + +struct glink_core_if { + /* Negotiation */ + void (*link_up)(struct glink_transport_if *if_ptr); + void (*link_down)(struct glink_transport_if *if_ptr); + void (*rx_cmd_version)(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features); + void (*rx_cmd_version_ack)(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features); + + /* channel management */ + void (*rx_cmd_ch_remote_open)(struct glink_transport_if *if_ptr, + uint32_t rcid, const char *name, uint16_t req_xprt); + void (*rx_cmd_ch_open_ack)(struct glink_transport_if *if_ptr, + uint32_t lcid, uint16_t xprt_resp); + void (*rx_cmd_ch_remote_close)(struct glink_transport_if *if_ptr, + uint32_t rcid); + void (*rx_cmd_ch_close_ack)(struct glink_transport_if *if_ptr, + uint32_t lcid); + + /* channel data */ + struct glink_core_rx_intent * (*rx_get_pkt_ctx)( + struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t liid); + void (*rx_put_pkt_ctx)(struct glink_transport_if *if_ptr, uint32_t rcid, + struct glink_core_rx_intent *intent_ptr, bool complete); + void (*rx_cmd_remote_rx_intent_put)(struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t riid, size_t size); + void (*rx_cmd_tx_done)(struct glink_transport_if *if_ptr, uint32_t rcid, + uint32_t riid, bool reuse); + void (*rx_cmd_remote_rx_intent_req)(struct glink_transport_if *if_ptr, + uint32_t rcid, size_t size); + void (*rx_cmd_rx_intent_req_ack)(struct glink_transport_if *if_ptr, + uint32_t rcid, bool granted); + void (*rx_cmd_remote_sigs)(struct glink_transport_if *if_ptr, + uint32_t rcid, uint32_t sigs); + + /* channel scheduling */ + void (*tx_resume)(struct glink_transport_if *if_ptr); +}; + +int glink_core_register_transport(struct glink_transport_if *if_ptr, + struct glink_core_transport_cfg *cfg); + +void glink_core_unregister_transport(struct glink_transport_if *if_ptr); + +/** + * of_get_glink_core_qos_cfg() - Parse the qos related dt entries + * @phandle: The handle to the qos related node in DT. + * @cfg: The transport configuration to be filled. + * + * Return: 0 on Success, standard Linux error otherwise. + */ +int of_get_glink_core_qos_cfg(struct device_node *phandle, + struct glink_core_transport_cfg *cfg); + +/** + * rx_linear_vbuf_provider() - Virtual Buffer Provider for linear buffers + * iovec: Pointer to the beginning of the linear buffer. + * offset: Offset into the buffer whose address is needed. + * size: Pointer to hold the length of the contiguous buffer space. + * + * This function is used when a linear buffer is received while the client has + * registered to receive vector buffers. + * + * Return: Address of the buffer which is at offset "offset" from the beginning + * of the buffer. + */ +static inline void *rx_linear_vbuf_provider(void *iovec, size_t offset, + size_t *size) +{ + struct glink_core_rx_intent *rx_info = + (struct glink_core_rx_intent *)iovec; + + if (unlikely(!iovec || !size)) + return NULL; + + if (unlikely(offset >= rx_info->pkt_size)) + return NULL; + + if (unlikely(OVERFLOW_ADD_UNSIGNED(void *, rx_info->data, offset))) + return NULL; + + *size = rx_info->pkt_size - offset; + return rx_info->data + offset; +} + +#endif /* _SOC_QCOM_GLINK_CORE_IF_H_ */ diff --git a/drivers/soc/qcom/glink_debugfs.c b/drivers/soc/qcom/glink_debugfs.c new file mode 100644 index 000000000000..8e65e4ac9b8e --- /dev/null +++ b/drivers/soc/qcom/glink_debugfs.c @@ -0,0 +1,783 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/debugfs.h> +#include <linux/err.h> +#include <linux/ipc_logging.h> +#include <linux/list.h> +#include <linux/slab.h> +#include <soc/qcom/glink.h> +#include "glink_private.h" +#include "glink_core_if.h" + + +static const char * const ss_string[] = { + [GLINK_DBGFS_MPSS] = "mpss", + [GLINK_DBGFS_APSS] = "apss", + [GLINK_DBGFS_LPASS] = "lpass", + [GLINK_DBGFS_DSPS] = "dsps", + [GLINK_DBGFS_RPM] = "rpm", + [GLINK_DBGFS_WCNSS] = "wcnss", + [GLINK_DBGFS_LLOOP] = "lloop", + [GLINK_DBGFS_MOCK] = "mock" +}; + +static const char * const xprt_string[] = { + [GLINK_DBGFS_SMEM] = "smem", + [GLINK_DBGFS_SMD] = "smd", + [GLINK_DBGFS_XLLOOP] = "lloop", + [GLINK_DBGFS_XMOCK] = "mock", + [GLINK_DBGFS_XMOCK_LOW] = "mock_low", + [GLINK_DBGFS_XMOCK_HIGH] = "mock_high" +}; + +static const char * const ch_st_string[] = { + [GLINK_CHANNEL_CLOSED] = "CLOSED", + [GLINK_CHANNEL_OPENING] = "OPENING", + [GLINK_CHANNEL_OPENED] = "OPENED", + [GLINK_CHANNEL_CLOSING] = "CLOSING", +}; + +static const char * const xprt_st_string[] = { + [GLINK_XPRT_DOWN] = "DOWN", + [GLINK_XPRT_NEGOTIATING] = "NEGOT", + [GLINK_XPRT_OPENED] = "OPENED", + [GLINK_XPRT_FAILED] = "FAILED" +}; + +#if defined(CONFIG_DEBUG_FS) +#define GLINK_DBGFS_NAME_SIZE (2 * GLINK_NAME_SIZE + 1) + +struct glink_dbgfs_dent { + struct list_head list_node; + char par_name[GLINK_DBGFS_NAME_SIZE]; + char self_name[GLINK_DBGFS_NAME_SIZE]; + struct dentry *parent; + struct dentry *self; + spinlock_t file_list_lock_lhb0; + struct list_head file_list; +}; + +static struct dentry *dent; +static LIST_HEAD(dent_list); +static DEFINE_MUTEX(dent_list_lock_lha0); + +static int debugfs_show(struct seq_file *s, void *data) +{ + struct glink_dbgfs_data *dfs_d; + dfs_d = s->private; + dfs_d->o_func(s); + return 0; +} + +static int debug_open(struct inode *inode, struct file *file) +{ + return single_open(file, debugfs_show, inode->i_private); +} + +static const struct file_operations debug_ops = { + .open = debug_open, + .release = single_release, + .read = seq_read, + .llseek = seq_lseek, +}; +#endif + +/** + * glink_get_ss_enum_string() - get the name of the subsystem based on enum value + * @enum_id: enum id of a specific subsystem. + * + * Return: name of the subsystem, NULL in case of invalid input + */ +const char *glink_get_ss_enum_string(unsigned int enum_id) +{ + if (enum_id >= ARRAY_SIZE(ss_string)) + return NULL; + + return ss_string[enum_id]; +} +EXPORT_SYMBOL(glink_get_ss_enum_string); + +/** + * glink_get_xprt_enum_string() - get the name of the transport based on enum value + * @enum_id: enum id of a specific transport. + * + * Return: name of the transport, NULL in case of invalid input + */ +const char *glink_get_xprt_enum_string(unsigned int enum_id) +{ + if (enum_id >= ARRAY_SIZE(xprt_string)) + return NULL; + return xprt_string[enum_id]; +} +EXPORT_SYMBOL(glink_get_xprt_enum_string); + +/** + * glink_get_xprt_state_string() - get the name of the transport based on enum value + * @enum_id: enum id of the state of the transport. + * + * Return: name of the transport state, NULL in case of invalid input + */ +const char *glink_get_xprt_state_string( + enum transport_state_e enum_id) +{ + if (enum_id >= ARRAY_SIZE(xprt_st_string)) + return NULL; + + return xprt_st_string[enum_id]; +} +EXPORT_SYMBOL(glink_get_xprt_state_string); + +/** + * glink_get_ch_state_string() - get the name of the transport based on enum value + * @enum_id: enum id of a specific state of the channel. + * + * Return: name of the channel state, NULL in case of invalid input + */ +const char *glink_get_ch_state_string( + enum local_channel_state_e enum_id) +{ + if (enum_id >= ARRAY_SIZE(ch_st_string)) + return NULL; + + return ch_st_string[enum_id]; +} +EXPORT_SYMBOL(glink_get_ch_state_string); + +#if defined(CONFIG_DEBUG_FS) +/** + * glink_dfs_create_file() - create the debugfs file + * @name: debugfs file name + * @parent: pointer to the parent dentry structure + * @show: pointer to the actual function which will be invoked upon + * opening this file. + * + * Return: pointer to the allocated glink_dbgfs_data structure or + * NULL in case of an error. + * + * This function actually create a debugfs file under the parent directory + */ +static struct glink_dbgfs_data *glink_dfs_create_file(const char *name, + struct dentry *parent, void (*show)(struct seq_file *s), + void *dbgfs_data, bool b_free_req) +{ + struct dentry *file; + struct glink_dbgfs_data *dfs_d; + + dfs_d = kzalloc(sizeof(struct glink_dbgfs_data), GFP_KERNEL); + if (dfs_d == NULL) + return NULL; + + dfs_d->o_func = show; + if (dbgfs_data != NULL) { + dfs_d->priv_data = dbgfs_data; + dfs_d->b_priv_free_req = b_free_req; + } + file = debugfs_create_file(name, 0400, parent, dfs_d, &debug_ops); + if (!file) + GLINK_DBG("%s: unable to create file '%s'\n", __func__, + name); + dfs_d->dent = file; + return dfs_d; +} + +/** + * write_ch_intent() - write channel intent details + * @s: pointer to the sequential file + * @intent: pointer glink core intent structure + * @i_type: type of intent + * @count: serial number of the intent. + * + * This function is a helper function of glink_dfs_update_ch_intents() + * that prints out details of any specific intent. + */ +static void write_ch_intent(struct seq_file *s, + struct glink_core_rx_intent *intent, + char *i_type, unsigned int count) +{ + char *intent_type; + /* + * formatted, human readable channel state output, ie: + * TYPE |SN |ID |PKT_SIZE|W_OFFSET|INT_SIZE| + * -------------------------------------------------------------- + * LOCAL_LIST|#2 |1 |0 |0 |8 | + */ + if (count == 1) { + intent_type = i_type; + seq_puts(s, + "\n--------------------------------------------------------\n"); + } else { + intent_type = ""; + } + seq_printf(s, "%-20s|#%-5d|%-6u|%-10zu|%-10zu|%-10zu|\n", + intent_type, + count, + intent->id, + intent->pkt_size, + intent->write_offset, + intent->intent_size); +} + +/** + * glink_dfs_update_ch_intent() - writes the intent details of a specific + * channel to the corresponding debugfs file + * @s: pointer to the sequential file + * + * This function extracts the intent details of a channel & prints them to + * corrseponding debugfs file of that channel. + */ +static void glink_dfs_update_ch_intent(struct seq_file *s) +{ + struct glink_dbgfs_data *dfs_d; + struct channel_ctx *ch_ctx; + struct glink_core_rx_intent *intent; + struct glink_core_rx_intent *intent_temp; + struct glink_ch_intent_info ch_intent_info; + unsigned long flags; + unsigned int count = 0; + + dfs_d = s->private; + ch_ctx = dfs_d->priv_data; + if (ch_ctx != NULL) { + glink_get_ch_intent_info(ch_ctx, &ch_intent_info); + seq_puts(s, + "---------------------------------------------------------------\n"); + seq_printf(s, "%-20s|%-6s|%-6s|%-10s|%-10s|%-10s|\n", + "INTENT TYPE", + "SN", + "ID", + "PKT_SIZE", + "W_OFFSET", + "INT_SIZE"); + seq_puts(s, + "---------------------------------------------------------------\n"); + spin_lock_irqsave(ch_intent_info.li_lst_lock, flags); + list_for_each_entry_safe(intent, intent_temp, + ch_intent_info.li_avail_list, list) { + count++; + write_ch_intent(s, intent, "LOCAL_AVAIL_LIST", count); + } + + count = 0; + list_for_each_entry_safe(intent, intent_temp, + ch_intent_info.li_used_list, list) { + count++; + write_ch_intent(s, intent, "LOCAL_USED_LIST", count); + } + spin_unlock_irqrestore(ch_intent_info.li_lst_lock, flags); + + count = 0; + spin_lock_irqsave(ch_intent_info.ri_lst_lock, flags); + list_for_each_entry_safe(intent, intent_temp, + ch_intent_info.ri_list, list) { + count++; + write_ch_intent(s, intent, "REMOTE_LIST", count); + } + spin_unlock_irqrestore(ch_intent_info.ri_lst_lock, + flags); + seq_puts(s, + "---------------------------------------------------------------\n"); + } +} + +/** + * glink_dfs_update_ch_stats() - writes statistics of a specific + * channel to the corresponding debugfs file + * @s: pointer to the sequential file + * + * This function extracts other statistics of a channel & prints them to + * corrseponding debugfs file of that channel + */ +static void glink_dfs_update_ch_stats(struct seq_file *s) +{ + /* FUTURE: add channel statistics */ + seq_puts(s, "not yet implemented\n"); +} + +/** + * glink_debugfs_remove_channel() - remove all channel specifc files & folder in + * debugfs when channel is fully closed + * @ch_ctx: pointer to the channel_contenxt + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when any channel is fully closed. It removes the + * folders & other files in debugfs for that channel. + */ +void glink_debugfs_remove_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx){ + + struct glink_dbgfs ch_rm_dbgfs; + char *edge_name; + char curr_dir_name[GLINK_DBGFS_NAME_SIZE]; + char *xprt_name; + + ch_rm_dbgfs.curr_name = glink_get_ch_name(ch_ctx); + edge_name = glink_get_xprt_edge_name(xprt_ctx); + xprt_name = glink_get_xprt_name(xprt_ctx); + if (!xprt_name || !edge_name) { + GLINK_ERR("%s: Invalid xprt_name or edge_name for ch '%s'\n", + __func__, ch_rm_dbgfs.curr_name); + return; + } + snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s", + edge_name, xprt_name); + ch_rm_dbgfs.par_name = curr_dir_name; + glink_debugfs_remove_recur(&ch_rm_dbgfs); +} +EXPORT_SYMBOL(glink_debugfs_remove_channel); + +/** + * glink_debugfs_add_channel() - create channel specifc files & folder in + * debugfs when channel is added + * @ch_ctx: pointer to the channel_contenxt + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when a new channel is created. It creates the + * folders & other files in debugfs for that channel + */ +void glink_debugfs_add_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx) +{ + struct glink_dbgfs ch_dbgfs; + char *ch_name; + char *edge_name; + char *xprt_name; + char curr_dir_name[GLINK_DBGFS_NAME_SIZE]; + + if (ch_ctx == NULL) { + GLINK_ERR("%s: Channel Context is NULL\n", __func__); + return; + } + + ch_name = glink_get_ch_name(ch_ctx); + edge_name = glink_get_xprt_edge_name(xprt_ctx); + xprt_name = glink_get_xprt_name(xprt_ctx); + if (!xprt_name || !edge_name) { + GLINK_ERR("%s: Invalid xprt_name or edge_name for ch '%s'\n", + __func__, ch_name); + return; + } + snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s", + edge_name, xprt_name); + + ch_dbgfs.curr_name = curr_dir_name; + ch_dbgfs.par_name = "channel"; + ch_dbgfs.b_dir_create = true; + glink_debugfs_create(ch_name, NULL, &ch_dbgfs, NULL, false); + + ch_dbgfs.par_name = ch_dbgfs.curr_name; + ch_dbgfs.curr_name = ch_name; + ch_dbgfs.b_dir_create = false; + glink_debugfs_create("stats", glink_dfs_update_ch_stats, + &ch_dbgfs, (void *)ch_ctx, false); + glink_debugfs_create("intents", glink_dfs_update_ch_intent, + &ch_dbgfs, (void *)ch_ctx, false); +} +EXPORT_SYMBOL(glink_debugfs_add_channel); + +/** + * glink_debugfs_add_xprt() - create transport specifc files & folder in + * debugfs when new transport is registerd + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when a new transport is registered. It creates the + * folders & other files in debugfs for that transport + */ +void glink_debugfs_add_xprt(struct glink_core_xprt_ctx *xprt_ctx) +{ + struct glink_dbgfs xprt_dbgfs; + char *xprt_name; + char *edge_name; + char curr_dir_name[GLINK_DBGFS_NAME_SIZE]; + + if (xprt_ctx == NULL) + GLINK_ERR("%s: Transport Context is NULL\n", __func__); + xprt_name = glink_get_xprt_name(xprt_ctx); + edge_name = glink_get_xprt_edge_name(xprt_ctx); + if (!xprt_name || !edge_name) { + GLINK_ERR("%s: xprt name or edge name is NULL\n", __func__); + return; + } + snprintf(curr_dir_name, sizeof(curr_dir_name), "%s_%s", + edge_name, xprt_name); + xprt_dbgfs.par_name = "glink"; + xprt_dbgfs.curr_name = "xprt"; + xprt_dbgfs.b_dir_create = true; + glink_debugfs_create(curr_dir_name, NULL, &xprt_dbgfs, NULL, false); + xprt_dbgfs.curr_name = "channel"; + glink_debugfs_create(curr_dir_name, NULL, &xprt_dbgfs, NULL, false); +} +EXPORT_SYMBOL(glink_debugfs_add_xprt); + +/** + * glink_dfs_create_channel_list() - create & update the channel details + * s: pointer to seq_file + * + * This function updates channel details in debugfs + * file present in /glink/channel/channels + */ +static void glink_dfs_create_channel_list(struct seq_file *s) +{ + struct xprt_ctx_iterator xprt_iter; + struct ch_ctx_iterator ch_iter; + + struct glink_core_xprt_ctx *xprt_ctx; + struct channel_ctx *ch_ctx; + int count = 0; + /* + * formatted, human readable channel state output, ie: + * NAME |LCID|RCID|XPRT|EDGE|LSTATE |RSTATE|LINT-Q|RINT-Q| + * -------------------------------------------------------------------- + * LOCAL_LOOPBACK_CLNT|2 |1 |lloop|local|OPENED|OPENED|5 |6 | + * N.B. Number of TX & RX Packets not implemented yet. -ENOSYS is printed + */ + seq_printf(s, "%-20s|%-4s|%-4s|%-10s|%-6s|%-7s|%-7s|%-5s|%-5s|\n", + "NAME", + "LCID", + "RCID", + "XPRT", + "EDGE", + "LSTATE", + "RSTATE", + "LINTQ", + "RINTQ"); + seq_puts(s, + "-------------------------------------------------------------------------------\n"); + glink_xprt_ctx_iterator_init(&xprt_iter); + xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter); + + while (xprt_ctx != NULL) { + glink_ch_ctx_iterator_init(&ch_iter, xprt_ctx); + ch_ctx = glink_ch_ctx_iterator_next(&ch_iter); + while (ch_ctx != NULL) { + count++; + seq_printf(s, "%-20s|%-4i|%-4i|%-10s|%-6s|%-7s|", + glink_get_ch_name(ch_ctx), + glink_get_ch_lcid(ch_ctx), + glink_get_ch_rcid(ch_ctx), + glink_get_ch_xprt_name(ch_ctx), + glink_get_ch_edge_name(ch_ctx), + glink_get_ch_lstate(ch_ctx)); + seq_printf(s, "%-7s|%-5i|%-5i|\n", + (glink_get_ch_rstate(ch_ctx) ? "OPENED" : "CLOSED"), + glink_get_ch_lintents_queued(ch_ctx), + glink_get_ch_rintents_queued(ch_ctx)); + + ch_ctx = glink_ch_ctx_iterator_next(&ch_iter); + } + glink_ch_ctx_iterator_end(&ch_iter, xprt_ctx); + xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter); + } + + glink_xprt_ctx_iterator_end(&xprt_iter); +} + +/** + * glink_dfs_create_xprt_list() - create & update the transport details + * @s: pointer to seq_file + * + * This function updates channel details in debugfs file present + * in /glink/xprt/xprts + */ +static void glink_dfs_create_xprt_list(struct seq_file *s) +{ + struct xprt_ctx_iterator xprt_iter; + struct glink_core_xprt_ctx *xprt_ctx; + const struct glink_core_version *gver; + uint32_t version; + uint32_t features; + int count = 0; + /* + * formatted, human readable channel state output, ie: + * XPRT_NAME|REMOTE |STATE|VERSION |FEATURES| + * --------------------------------------------- + * smd_trans|lpass |2 |0 |1 | + * smem |mpss |0 |0 |0 | + */ + seq_printf(s, "%-20s|%-20s|%-6s|%-8s|%-8s|\n", + "XPRT_NAME", + "REMOTE", + "STATE", + "VERSION", + "FEATURES"); + seq_puts(s, + "-------------------------------------------------------------------------------\n"); + glink_xprt_ctx_iterator_init(&xprt_iter); + xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter); + + while (xprt_ctx != NULL) { + count++; + seq_printf(s, "%-20s|%-20s|", + glink_get_xprt_name(xprt_ctx), + glink_get_xprt_edge_name(xprt_ctx)); + gver = glink_get_xprt_version_features(xprt_ctx); + if (gver != NULL) { + version = gver->version; + features = gver->features; + seq_printf(s, "%-6s|%-8i|%-8i|\n", + glink_get_xprt_state(xprt_ctx), + version, + features); + } else { + seq_printf(s, "%-6s|%-8i|%-8i|\n", + glink_get_xprt_state(xprt_ctx), + -ENODATA, + -ENODATA); + } + xprt_ctx = glink_xprt_ctx_iterator_next(&xprt_iter); + + } + + glink_xprt_ctx_iterator_end(&xprt_iter); +} + +/** + * glink_dfs_update_list() - update the internally maintained dentry linked list + * @curr_dent: pointer to the current dentry object + * @parent: pointer to the parent dentry object + * @curr: current directory name + * @par_dir: parent directory name + */ +void glink_dfs_update_list(struct dentry *curr_dent, struct dentry *parent, + const char *curr, const char *par_dir) +{ + struct glink_dbgfs_dent *dbgfs_dent_s; + if (curr_dent != NULL) { + dbgfs_dent_s = kzalloc(sizeof(struct glink_dbgfs_dent), + GFP_KERNEL); + if (dbgfs_dent_s != NULL) { + INIT_LIST_HEAD(&dbgfs_dent_s->file_list); + spin_lock_init(&dbgfs_dent_s->file_list_lock_lhb0); + dbgfs_dent_s->parent = parent; + dbgfs_dent_s->self = curr_dent; + strlcpy(dbgfs_dent_s->self_name, + curr, strlen(curr) + 1); + strlcpy(dbgfs_dent_s->par_name, par_dir, + strlen(par_dir) + 1); + mutex_lock(&dent_list_lock_lha0); + list_add_tail(&dbgfs_dent_s->list_node, &dent_list); + mutex_unlock(&dent_list_lock_lha0); + } + } else { + GLINK_DBG("%s:create directory failed for par:curr [%s:%s]\n", + __func__, par_dir, curr); + } + return; +} + +/** + * glink_remove_dfs_entry() - remove the the entries from dent_list + * @entry: pointer to the glink_dbgfs_dent structure + * + * This function removes the removes the entries from internally maintained + * linked list of dentries. It also deletes the file list and associated memory + * if present. + */ +void glink_remove_dfs_entry(struct glink_dbgfs_dent *entry) +{ + struct glink_dbgfs_data *fentry, *fentry_temp; + unsigned long flags; + + if (entry == NULL) + return; + if (!list_empty(&entry->file_list)) { + spin_lock_irqsave(&entry->file_list_lock_lhb0, flags); + list_for_each_entry_safe(fentry, fentry_temp, + &entry->file_list, flist) { + if (fentry->b_priv_free_req) + kfree(fentry->priv_data); + list_del(&fentry->flist); + kfree(fentry); + fentry = NULL; + } + spin_unlock_irqrestore(&entry->file_list_lock_lhb0, flags); + } + list_del(&entry->list_node); + kfree(entry); + entry = NULL; +} + +/** + * glink_debugfs_remove_recur() - remove the the directory & files recursively + * @rm_dfs: pointer to the structure glink_dbgfs + * + * This function removes the files & directories below the given directory. + * This also takes care of freeing any memory associated with the debugfs file. + */ +void glink_debugfs_remove_recur(struct glink_dbgfs *rm_dfs) +{ + const char *c_dir_name; + const char *p_dir_name; + struct glink_dbgfs_dent *entry, *entry_temp; + struct dentry *par_dent = NULL; + + if (rm_dfs == NULL) + return; + + c_dir_name = rm_dfs->curr_name; + p_dir_name = rm_dfs->par_name; + + mutex_lock(&dent_list_lock_lha0); + list_for_each_entry_safe(entry, entry_temp, &dent_list, list_node) { + if (!strcmp(entry->par_name, c_dir_name)) { + glink_remove_dfs_entry(entry); + } else if (!strcmp(entry->self_name, c_dir_name) + && !strcmp(entry->par_name, p_dir_name)) { + par_dent = entry->self; + glink_remove_dfs_entry(entry); + } + } + mutex_unlock(&dent_list_lock_lha0); + if (par_dent != NULL) + debugfs_remove_recursive(par_dent); +} +EXPORT_SYMBOL(glink_debugfs_remove_recur); + +/** + * glink_debugfs_create() - create the debugfs file + * @name: debugfs file name + * @show: pointer to the actual function which will be invoked upon + * opening this file. + * @dir: pointer to a structure debugfs_dir + * dbgfs_data: pointer to any private data need to be associated with debugfs + * b_free_req: boolean value to decide to free the memory associated with + * @dbgfs_data during deletion of the file + * + * Return: pointer to the file/directory created, NULL in case of error + * + * This function checks which directory will be used to create the debugfs file + * and calls glink_dfs_create_file. Anybody who intend to allocate some memory + * for the dbgfs_data and required to free it in deletion, need to set + * b_free_req to true. Otherwise, there will be a memory leak. + */ +struct dentry *glink_debugfs_create(const char *name, + void (*show)(struct seq_file *), + struct glink_dbgfs *dir, void *dbgfs_data, bool b_free_req) +{ + struct dentry *parent = NULL; + struct dentry *dent = NULL; + struct glink_dbgfs_dent *entry; + struct glink_dbgfs_data *file_data; + const char *c_dir_name; + const char *p_dir_name; + unsigned long flags; + + if (dir == NULL) { + GLINK_ERR("%s: debugfs_dir strucutre is null\n", __func__); + return NULL; + } + c_dir_name = dir->curr_name; + p_dir_name = dir->par_name; + + mutex_lock(&dent_list_lock_lha0); + list_for_each_entry(entry, &dent_list, list_node) + if (!strcmp(entry->par_name, p_dir_name) + && !strcmp(entry->self_name, c_dir_name)) { + parent = entry->self; + break; + } + mutex_unlock(&dent_list_lock_lha0); + p_dir_name = c_dir_name; + c_dir_name = name; + if (parent != NULL) { + if (dir->b_dir_create) { + dent = debugfs_create_dir(name, parent); + if (dent != NULL) + glink_dfs_update_list(dent, parent, + c_dir_name, p_dir_name); + } else { + file_data = glink_dfs_create_file(name, parent, show, + dbgfs_data, b_free_req); + spin_lock_irqsave(&entry->file_list_lock_lhb0, flags); + if (file_data != NULL) + list_add_tail(&file_data->flist, + &entry->file_list); + spin_unlock_irqrestore(&entry->file_list_lock_lhb0, + flags); + } + } else { + GLINK_DBG("%s: parent dentry is null for [%s]\n", + __func__, name); + } + return dent; +} +EXPORT_SYMBOL(glink_debugfs_create); + +/** + * glink_debugfs_init() - initialize the glink debugfs directory structure + * + * Return: 0 in success otherwise appropriate error code + * + * This function initializes the debugfs directory for glink + */ +int glink_debugfs_init(void) +{ + struct glink_dbgfs dbgfs; + + /* fake parent name */ + dent = debugfs_create_dir("glink", NULL); + if (IS_ERR_OR_NULL(dent)) + return PTR_ERR(dent); + + glink_dfs_update_list(dent, NULL, "glink", "root"); + + dbgfs.b_dir_create = true; + dbgfs.curr_name = "glink"; + dbgfs.par_name = "root"; + glink_debugfs_create("xprt", NULL, &dbgfs, NULL, false); + glink_debugfs_create("channel", NULL, &dbgfs, NULL, false); + + dbgfs.curr_name = "channel"; + dbgfs.par_name = "glink"; + dbgfs.b_dir_create = false; + glink_debugfs_create("channels", glink_dfs_create_channel_list, + &dbgfs, NULL, false); + dbgfs.curr_name = "xprt"; + glink_debugfs_create("xprts", glink_dfs_create_xprt_list, + &dbgfs, NULL, false); + + return 0; +} +EXPORT_SYMBOL(glink_debugfs_init); + +/** + * glink_debugfs_exit() - removes the glink debugfs directory + * + * This function recursively remove all the debugfs directories + * starting from dent + */ +void glink_debugfs_exit(void) +{ + if (dent != NULL) + debugfs_remove_recursive(dent); +} +EXPORT_SYMBOL(glink_debugfs_exit); +#else +void glink_debugfs_remove_recur(struct glink_dbgfs *dfs) { } +EXPORT_SYMBOL(glink_debugfs_remove_recur); + +void glink_debugfs_remove_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx) { } +EXPORT_SYMBOL(glink_debugfs_remove_channel); + +void glink_debugfs_add_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx) { } +EXPORT_SYMBOL(glink_debugfs_add_channel); + +void glink_debugfs_add_xprt(struct glink_core_xprt_ctx *xprt_ctx) { } +EXPORT_SYMBOL(glink_debugfs_add_xprt); + +int glink_debugfs_init(void) { return 0; } +EXPORT_SYMBOL(glink_debugfs_init); + +void glink_debugfs_exit(void) { } +EXPORT_SYMBOL(glink_debugfs_exit); +#endif /* CONFIG_DEBUG_FS */ diff --git a/drivers/soc/qcom/glink_private.h b/drivers/soc/qcom/glink_private.h new file mode 100644 index 000000000000..d79893227cd2 --- /dev/null +++ b/drivers/soc/qcom/glink_private.h @@ -0,0 +1,1044 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _SOC_QCOM_GLINK_PRIVATE_H_ +#define _SOC_QCOM_GLINK_PRIVATE_H_ + +#include <linux/bug.h> +#include <linux/completion.h> +#include <linux/dcache.h> +#include <linux/ipc_logging.h> +#include <linux/kernel.h> +#include <linux/kref.h> +#include <linux/ratelimit.h> +#include <linux/seq_file.h> +#include <linux/spinlock.h> +#include <linux/types.h> +#include <soc/qcom/glink.h> + +struct glink_core_xprt_ctx; +struct channel_ctx; +enum transport_state_e; +enum local_channel_state_e; + +/* Logging Macros */ +enum { + QCOM_GLINK_INFO = 1U << 0, + QCOM_GLINK_DEBUG = 1U << 1, + QCOM_GLINK_GPIO = 1U << 2, + QCOM_GLINK_PERF = 1U << 3, +}; + +enum glink_dbgfs_ss { + GLINK_DBGFS_MPSS, + GLINK_DBGFS_APSS, + GLINK_DBGFS_LPASS, + GLINK_DBGFS_DSPS, + GLINK_DBGFS_RPM, + GLINK_DBGFS_WCNSS, + GLINK_DBGFS_LLOOP, + GLINK_DBGFS_MOCK, + GLINK_DBGFS_MAX_NUM_SUBS +}; + +enum glink_dbgfs_xprt { + GLINK_DBGFS_SMEM, + GLINK_DBGFS_SMD, + GLINK_DBGFS_XLLOOP, + GLINK_DBGFS_XMOCK, + GLINK_DBGFS_XMOCK_LOW, + GLINK_DBGFS_XMOCK_HIGH, + GLINK_DBGFS_MAX_NUM_XPRTS +}; + +struct glink_dbgfs { + const char *curr_name; + const char *par_name; + bool b_dir_create; +}; + +struct glink_dbgfs_data { + struct list_head flist; + struct dentry *dent; + void (*o_func)(struct seq_file *s); + void *priv_data; + bool b_priv_free_req; +}; + +struct xprt_ctx_iterator { + struct list_head *xprt_list; + struct glink_core_xprt_ctx *i_curr; + unsigned long xprt_list_flags; +}; + +struct ch_ctx_iterator { + struct list_head *ch_list; + struct channel_ctx *i_curr; + unsigned long ch_list_flags; +}; + +struct glink_ch_intent_info { + spinlock_t *li_lst_lock; + struct list_head *li_avail_list; + struct list_head *li_used_list; + spinlock_t *ri_lst_lock; + struct list_head *ri_list; +}; + +/* Tracer Packet Event IDs for G-Link */ +enum glink_tracer_pkt_events { + GLINK_CORE_TX = 1, + GLINK_QUEUE_TO_SCHEDULER = 2, + GLINK_SCHEDULER_TX = 3, + GLINK_XPRT_TX = 4, + GLINK_XPRT_RX = 5, + GLINK_CORE_RX = 6, +}; + +/** + * glink_get_ss_enum_string() - get the name of the subsystem based on enum value + * @enum_id: enum id of a specific subsystem. + * + * Return: name of the subsystem, NULL in case of invalid input + */ +const char *glink_get_ss_enum_string(unsigned int enum_id); + +/** + * glink_get_xprt_enum_string() - get the name of the transport based on enum value + * @enum_id: enum id of a specific transport. + * + * Return: name of the transport, NULL in case of invalid input + */ +const char *glink_get_xprt_enum_string(unsigned int enum_id); + +/** + * glink_get_xprt_state_string() - get the name of the transport based on enum value + * @enum_id: enum id of the state of the transport. + * + * Return: name of the transport state, NULL in case of invalid input + */ +const char *glink_get_xprt_state_string(enum transport_state_e enum_id); + +/** + * glink_get_ch_state_string() - get the name of the transport based on enum value + * @enum_id: enum id of a specific state of the channel. + * + * Return: name of the channel state, NULL in case of invalid input + */ +const char *glink_get_ch_state_string(enum local_channel_state_e enum_id); + +#define GLINK_IPC_LOG_STR(x...) do { \ + if (glink_get_log_ctx()) \ + ipc_log_string(glink_get_log_ctx(), x); \ +} while (0) + +#define GLINK_DBG(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_IPC_LOG_STR(x); \ +} while (0) + +#define GLINK_INFO(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_IPC_LOG_STR(x); \ +} while (0) + +#define GLINK_INFO_PERF(x...) do { \ + if (glink_get_debug_mask() & (QCOM_GLINK_INFO | QCOM_GLINK_PERF)) \ + GLINK_IPC_LOG_STR(x); \ +} while (0) + +#define GLINK_PERF(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_PERF) \ + GLINK_IPC_LOG_STR("<PERF> " x); \ +} while (0) + +#define GLINK_UT_ERR(x...) do { \ + if (!(glink_get_debug_mask() & QCOM_GLINK_PERF)) \ + pr_err("<UT> " x); \ + GLINK_IPC_LOG_STR("<UT> " x); \ +} while (0) + +#define GLINK_UT_DBG(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_IPC_LOG_STR("<UT> " x); \ +} while (0) + +#define GLINK_UT_INFO(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_IPC_LOG_STR("<UT> " x); \ +} while (0) + +#define GLINK_UT_INFO_PERF(x...) do { \ + if (glink_get_debug_mask() & (QCOM_GLINK_INFO | QCOM_GLINK_PERF)) \ + GLINK_IPC_LOG_STR("<UT> " x); \ +} while (0) + +#define GLINK_UT_PERF(x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_PERF) \ + GLINK_IPC_LOG_STR("<PERF> " x); \ +} while (0) + +#define GLINK_XPRT_IPC_LOG_STR(xprt, x...) do { \ + if (glink_get_xprt_log_ctx(xprt)) \ + ipc_log_string(glink_get_xprt_log_ctx(xprt), x); \ +} while (0) + +#define GLINK_XPRT_IF_INFO(xprt_if, x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_XPRT_IPC_LOG_STR(xprt_if.glink_core_priv, "<XPRT> " x); \ +} while (0) + +#define GLINK_XPRT_IF_DBG(xprt_if, x...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_XPRT_IPC_LOG_STR(xprt_if.glink_core_priv, "<XPRT> " x); \ +} while (0) + +#define GLINK_XPRT_IF_ERR(xprt_if, x...) do { \ + pr_err("<XPRT> " x); \ + GLINK_XPRT_IPC_LOG_STR(xprt_if.glink_core_priv, "<XPRT> " x); \ +} while (0) + +#define GLINK_PERF_XPRT(xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_PERF) \ + GLINK_XPRT_IPC_LOG_STR(xprt, "<PERF> %s:%s " fmt, \ + xprt->name, xprt->edge, args); \ +} while (0) + +#define GLINK_PERF_CH(ctx, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_PERF) \ + GLINK_XPRT_IPC_LOG_STR(ctx->transport_ptr, \ + "<PERF> %s:%s:%s[%u:%u] " fmt, \ + ctx->transport_ptr->name, \ + ctx->transport_ptr->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_PERF_CH_XPRT(ctx, xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_PERF) \ + GLINK_XPRT_IPC_LOG_STR(xprt, \ + "<PERF> %s:%s:%s[%u:%u] " fmt, \ + xprt->name, \ + xprt->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_INFO_PERF_XPRT(xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & (QCOM_GLINK_INFO | QCOM_GLINK_PERF)) \ + GLINK_XPRT_IPC_LOG_STR(xprt, "<CORE> %s:%s " fmt, \ + xprt->name, xprt->edge, args); \ +} while (0) + +#define GLINK_INFO_PERF_CH(ctx, fmt, args...) do { \ + if (glink_get_debug_mask() & (QCOM_GLINK_INFO | QCOM_GLINK_PERF)) \ + GLINK_XPRT_IPC_LOG_STR(ctx->transport_ptr, \ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + ctx->transport_ptr->name, \ + ctx->transport_ptr->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_INFO_PERF_CH_XPRT(ctx, xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & (QCOM_GLINK_INFO | QCOM_GLINK_PERF)) \ + GLINK_XPRT_IPC_LOG_STR(xprt,\ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + xprt->name, \ + xprt->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_INFO_XPRT(xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_XPRT_IPC_LOG_STR(xprt, "<CORE> %s:%s " fmt, \ + xprt->name, xprt->edge, args); \ +} while (0) + +#define GLINK_INFO_CH(ctx, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_XPRT_IPC_LOG_STR(ctx->transport_ptr, \ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + ctx->transport_ptr->name, \ + ctx->transport_ptr->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_INFO_CH_XPRT(ctx, xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_INFO) \ + GLINK_XPRT_IPC_LOG_STR(xprt, \ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + xprt->name, \ + xprt->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_DBG_XPRT(xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_XPRT_IPC_LOG_STR(xprt, "<CORE> %s:%s " fmt, \ + xprt->name, xprt->edge, args); \ +} while (0) + +#define GLINK_DBG_CH(ctx, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_XPRT_IPC_LOG_STR(ctx->transport_ptr, \ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + ctx->transport_ptr->name, \ + ctx->transport_ptr->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_DBG_CH_XPRT(ctx, xprt, fmt, args...) do { \ + if (glink_get_debug_mask() & QCOM_GLINK_DEBUG) \ + GLINK_XPRT_IPC_LOG_STR(xprt, \ + "<CORE> %s:%s:%s[%u:%u] " fmt, \ + xprt->name, \ + xprt->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ +} while (0) + +#define GLINK_ERR(x...) do { \ + pr_err_ratelimited("<CORE> " x); \ + GLINK_IPC_LOG_STR("<CORE> " x); \ +} while (0) + +#define GLINK_ERR_XPRT(xprt, fmt, args...) do { \ + pr_err_ratelimited("<CORE> %s:%s " fmt, \ + xprt->name, xprt->edge, args); \ + GLINK_INFO_XPRT(xprt, fmt, args); \ +} while (0) + +#define GLINK_ERR_CH(ctx, fmt, args...) do { \ + pr_err_ratelimited("<CORE> %s:%s:%s[%u:%u] " fmt, \ + ctx->transport_ptr->name, \ + ctx->transport_ptr->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ + GLINK_INFO_CH(ctx, fmt, args); \ +} while (0) + +#define GLINK_ERR_CH_XPRT(ctx, xprt, fmt, args...) do { \ + pr_err_ratelimited("<CORE> %s:%s:%s[%u:%u] " fmt, \ + xprt->name, \ + xprt->edge, \ + ctx->name, \ + ctx->lcid, \ + ctx->rcid, args); \ + GLINK_INFO_CH_XPRT(ctx, xprt, fmt, args); \ +} while (0) + +/** + * OVERFLOW_ADD_UNSIGNED() - check for unsigned overflow + * + * type: type to check for overflow + * a: left value to use + * b: right value to use + * returns: true if a + b will result in overflow; false otherwise + */ +#define OVERFLOW_ADD_UNSIGNED(type, a, b) \ + (((type)~0 - (a)) < (b) ? true : false) + +/** + * glink_get_debug_mask() - Return debug mask attribute + * + * Return: debug mask attribute + */ +unsigned glink_get_debug_mask(void); + +/** + * glink_get_log_ctx() - Return log context for other GLINK modules. + * + * Return: Log context or NULL if none. + */ +void *glink_get_log_ctx(void); + +/** + * glink_get_xprt_log_ctx() - Return log context for other GLINK modules. + * + * Return: Log context or NULL if none. + */ +void *glink_get_xprt_log_ctx(struct glink_core_xprt_ctx *xprt); + +/** + * glink_get_channel_id_for_handle() - Get logical channel ID + * + * @handle: handle of channel + * + * Used internally by G-Link debugfs. + * + * Return: Logical Channel ID or standard Linux error code + */ +int glink_get_channel_id_for_handle(void *handle); + +/** + * glink_get_channel_name_for_handle() - return channel name + * + * @handle: handle of channel + * + * Used internally by G-Link debugfs. + * + * Return: Channel name or NULL + */ +char *glink_get_channel_name_for_handle(void *handle); + +/** + * glink_debugfs_init() - initialize glink debugfs directory + * + * Return: error code or success. + */ +int glink_debugfs_init(void); + +/** + * glink_debugfs_exit() - removes glink debugfs directory + */ +void glink_debugfs_exit(void); + +/** + * glink_debugfs_create() - create the debugfs file + * @name: debugfs file name + * @show: pointer to the actual function which will be invoked upon + * opening this file. + * @dir: pointer to a structure debugfs_dir + * @dbgfs_data: pointer to any private data need to be associated with debugfs + * @b_free_req: boolean value to decide to free the memory associated with + * @dbgfs_data during deletion of the file + * + * Return: pointer to the file/directory created, NULL in case of error + * + * This function checks which directory will be used to create the debugfs file + * and calls glink_dfs_create_file. Anybody who intend to allocate some memory + * for the dbgfs_data and required to free it in deletion, need to set + * b_free_req to true. Otherwise, there will be a memory leak. + */ +struct dentry *glink_debugfs_create(const char *name, + void (*show)(struct seq_file *), + struct glink_dbgfs *dir, void *dbgfs_data, bool b_free_req); + +/** + * glink_debugfs_remove_recur() - remove the the directory & files recursively + * @rm_dfs: pointer to the structure glink_dbgfs + * + * This function removes the files & directories. This also takes care of + * freeing any memory associated with the debugfs file. + */ +void glink_debugfs_remove_recur(struct glink_dbgfs *dfs); + +/** + * glink_debugfs_remove_channel() - remove all channel specifc files & folder in + * debugfs when channel is fully closed + * @ch_ctx: pointer to the channel_contenxt + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when any channel is fully closed. It removes the + * folders & other files in debugfs for that channel. + */ +void glink_debugfs_remove_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_debugfs_add_channel() - create channel specifc files & folder in + * debugfs when channel is added + * @ch_ctx: pointer to the channel_contenxt + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when a new channel is created. It creates the + * folders & other files in debugfs for that channel + */ +void glink_debugfs_add_channel(struct channel_ctx *ch_ctx, + struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_debugfs_add_xprt() - create transport specifc files & folder in + * debugfs when new transport is registerd + * @xprt_ctx: pointer to the transport_context + * + * This function is invoked when a new transport is registered. It creates the + * folders & other files in debugfs for that transport + */ +void glink_debugfs_add_xprt(struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_xprt_ctx_iterator_init() - Initializes the transport context list iterator + * @xprt_i: pointer to the transport context iterator. + * + * Return: None + * + * This function acquires the transport context lock which must then be + * released by glink_xprt_ctx_iterator_end() + */ +void glink_xprt_ctx_iterator_init(struct xprt_ctx_iterator *xprt_i); + +/** + * glink_xprt_ctx_iterator_end() - Ends the transport context list iteration + * @xprt_i: pointer to the transport context iterator. + * + * Return: None + */ +void glink_xprt_ctx_iterator_end(struct xprt_ctx_iterator *xprt_i); + +/** + * glink_xprt_ctx_iterator_next() - iterates element by element in transport context list + * @xprt_i: pointer to the transport context iterator. + * + * Return: pointer to the transport context structure + */ +struct glink_core_xprt_ctx *glink_xprt_ctx_iterator_next( + struct xprt_ctx_iterator *xprt_i); + +/** + * glink_get_xprt_name() - get the transport name + * @xprt_ctx: pointer to the transport context. + * + * Return: name of the transport + */ +char *glink_get_xprt_name(struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_get_xprt_edge_name() - get the name of the remote processor/edge + * of the transport + * @xprt_ctx: pointer to the transport context. + * + * Return: name of the remote processor/edge + */ +char *glink_get_xprt_edge_name(struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_get_xprt_state() - get the state of the transport + * @xprt_ctx: pointer to the transport context. + * + * Return: name of the transport state, NULL in case of invalid input + */ +const char *glink_get_xprt_state(struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_get_xprt_version_features() - get the version and feature set + * of local transport in glink + * @xprt_ctx: pointer to the transport context. + * + * Return: pointer to the glink_core_version + */ +const struct glink_core_version *glink_get_xprt_version_features( + struct glink_core_xprt_ctx *xprt_ctx); + +/** + * glink_ch_ctx_iterator_init() - Initializes the channel context list iterator + * @ch_iter: pointer to the channel context iterator. + * @xprt: pointer to the transport context that holds the channel list + * + * This function acquires the channel context lock which must then be + * released by glink_ch_ctx_iterator_end() + */ +void glink_ch_ctx_iterator_init(struct ch_ctx_iterator *ch_iter, + struct glink_core_xprt_ctx *xprt); + +/** + * glink_ch_ctx_iterator_end() - Ends the channel context list iteration + * @ch_iter: pointer to the channel context iterator. + * + */ +void glink_ch_ctx_iterator_end(struct ch_ctx_iterator *ch_iter, + struct glink_core_xprt_ctx *xprt); + +/** + * glink_ch_ctx_iterator_next() - iterates element by element in channel context list + * @c_i: pointer to the channel context iterator. + * + * Return: pointer to the channel context structure + */ +struct channel_ctx *glink_ch_ctx_iterator_next(struct ch_ctx_iterator *ch_iter); + +/** + * glink_get_ch_name() - get the channel name + * @ch_ctx: pointer to the channel context. + * + * Return: name of the channel, NULL in case of invalid input + */ +char *glink_get_ch_name(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_edge_name() - get the name of the remote processor/edge + * of the channel + * @xprt_ctx: pointer to the channel context. + * + * Return: name of the remote processor/edge + */ +char *glink_get_ch_edge_name(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_rcid() - get the remote channel ID + * @ch_ctx: pointer to the channel context. + * + * Return: remote channel id, -EINVAL in case of invalid input + */ +int glink_get_ch_lcid(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_rcid() - get the remote channel ID + * @ch_ctx: pointer to the channel context. + * + * Return: remote channel id, -EINVAL in case of invalid input + */ +int glink_get_ch_rcid(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_lstate() - get the local channel state + * @ch_ctx: pointer to the channel context. + * + * Return: name of the local channel state, NULL in case of invalid input + */ +const char *glink_get_ch_lstate(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_rstate() - get the remote channel state + * @ch_ctx: pointer to the channel context. + * + * Return: true if remote side is opened false otherwise + */ +bool glink_get_ch_rstate(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_xprt_name() - get the name of the transport to which + * the channel belongs + * @ch_ctx: pointer to the channel context. + * + * Return: name of the export, NULL in case of invalid input + */ +char *glink_get_ch_xprt_name(struct channel_ctx *ch_ctx); + +/** + * glink_get_tx_pkt_count() - get the total number of packets sent + * through this channel + * @ch_ctx: pointer to the channel context. + * + * Return: number of packets transmitted, -EINVAL in case of invalid input + */ +int glink_get_ch_tx_pkt_count(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_rx_pkt_count() - get the total number of packets + * recieved at this channel + * @ch_ctx: pointer to the channel context. + * + * Return: number of packets recieved, -EINVAL in case of invalid input + */ +int glink_get_ch_rx_pkt_count(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_lintents_queued() - get the total number of intents queued + * at local side + * @ch_ctx: pointer to the channel context. + * + * Return: number of intents queued, -EINVAL in case of invalid input + */ +int glink_get_ch_lintents_queued(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_rintents_queued() - get the total number of intents queued + * from remote side + * @ch_ctx: pointer to the channel context. + * + * Return: number of intents queued + */ +int glink_get_ch_rintents_queued(struct channel_ctx *ch_ctx); + +/** + * glink_get_ch_intent_info() - get the intent details of a channel + * @ch_ctx: pointer to the channel context. + * @ch_ctx_i: pointer to a structure that will contain intent details + * + * This funcion is used to get all the channel intent details including locks. + */ +void glink_get_ch_intent_info(struct channel_ctx *ch_ctx, + struct glink_ch_intent_info *ch_ctx_i); + +/** + * enum ssr_command - G-Link SSR protocol commands + */ +enum ssr_command { + GLINK_SSR_DO_CLEANUP, + GLINK_SSR_CLEANUP_DONE, +}; + +/** + * struct ssr_notify_data - Contains private data used for client notifications + * from G-Link. + * tx_done: Indicates whether or not the tx_done notification has + * been received. + * event: The state notification event received. + * responded: Indicates whether or not a cleanup_done response was + * received. + * edge: The G-Link edge name for the channel associated with + * this callback data + * do_cleanup_data: Structure containing the G-Link SSR do_cleanup message. + */ +struct ssr_notify_data { + bool tx_done; + unsigned event; + bool responded; + const char *edge; + struct do_cleanup_msg *do_cleanup_data; +}; + +/** + * struct subsys_info - Subsystem info structure + * ssr_name: name of the subsystem recognized by the SSR framework + * edge: name of the G-Link edge + * xprt: name of the G-Link transport + * handle: glink_ssr channel used for this subsystem + * link_state_handle: link state handle for this edge, used to unregister + * from receiving link state callbacks + * link_info: Transport info used in link state callback registration + * cb_data: Private callback data structure for notification + * functions + * subsystem_list_node: used to chain this structure in a list of subsystem + * info structures + * notify_list: list of subsys_info_leaf structures, containing the + * subsystems to notify if this subsystem undergoes SSR + * notify_list_len: length of notify_list + * link_up: Flag indicating whether transport is up or not + * link_up_lock: Lock for protecting the link_up flag + */ +struct subsys_info { + const char *ssr_name; + const char *edge; + const char *xprt; + void *handle; + void *link_state_handle; + struct glink_link_info *link_info; + struct ssr_notify_data *cb_data; + struct list_head subsystem_list_node; + struct list_head notify_list; + int notify_list_len; + bool link_up; + spinlock_t link_up_lock; +}; + +/** + * struct subsys_info_leaf - Subsystem info leaf structure (a subsystem on the + * notify list of a subsys_info structure) + * ssr_name: Name of the subsystem recognized by the SSR framework + * edge: Name of the G-Link edge + * xprt: Name of the G-Link transport + * restarted: Indicates whether a restart has been triggered for this edge + * cb_data: Private callback data structure for notification functions + * notify_list_node: used to chain this structure in the notify list + */ +struct subsys_info_leaf { + const char *ssr_name; + const char *edge; + const char *xprt; + bool restarted; + struct ssr_notify_data *cb_data; + struct list_head notify_list_node; +}; + +/** + * struct do_cleanup_msg - The data structure for an SSR do_cleanup message + * version: The G-Link SSR protocol version + * command: The G-Link SSR command - do_cleanup + * seq_num: Sequence number + * name_len: Length of the name of the subsystem being restarted + * name: G-Link edge name of the subsystem being restarted + */ +struct do_cleanup_msg { + uint32_t version; + uint32_t command; + uint32_t seq_num; + uint32_t name_len; + char name[32]; +}; + +/** + * struct cleanup_done_msg - The data structure for an SSR cleanup_done message + * version: The G-Link SSR protocol version + * response: The G-Link SSR response to a do_cleanup command, cleanup_done + * seq_num: Sequence number + */ +struct cleanup_done_msg { + uint32_t version; + uint32_t response; + uint32_t seq_num; +}; + +/** + * get_info_for_subsystem() - Retrieve information about a subsystem from the + * global subsystem_info_list + * @subsystem: The name of the subsystem recognized by the SSR + * framework + * + * Return: subsys_info structure containing info for the requested subsystem; + * NULL if no structure can be found for the requested subsystem + */ +struct subsys_info *get_info_for_subsystem(const char *subsystem); + +/** + * get_info_for_edge() - Retrieve information about a subsystem from the + * global subsystem_info_list + * @edge: The name of the edge recognized by G-Link + * + * Return: subsys_info structure containing info for the requested subsystem; + * NULL if no structure can be found for the requested subsystem + */ +struct subsys_info *get_info_for_edge(const char *edge); + +/** + * glink_ssr_get_seq_num() - Get the current SSR sequence number + * + * Return: The current SSR sequence number + */ +uint32_t glink_ssr_get_seq_num(void); + +/* + * glink_ssr() - SSR cleanup function. + * + * Return: Standard error code. + */ +int glink_ssr(const char *subsystem); + +/** + * notify for subsystem() - Notify other subsystems that a subsystem is being + * restarted + * @ss_info: Subsystem info structure for the subsystem being restarted + * + * This function sends notifications to affected subsystems that the subsystem + * in ss_info is being restarted, and waits for the cleanup done response from + * all of those subsystems. It also initiates any local cleanup that is + * necessary. + * + * Return: 0 on success, standard error codes otherwise + */ +int notify_for_subsystem(struct subsys_info *ss_info); + +/** + * glink_ssr_wait_cleanup_done() - Get the value of the + * notifications_successful flag in glink_ssr. + * @timeout_multiplier: timeout multiplier for waiting on all processors + * + * + * Return: True if cleanup_done received from all processors, false otherwise + */ +bool glink_ssr_wait_cleanup_done(unsigned ssr_timeout_multiplier); + +struct channel_lcid { + struct list_head list_node; + uint32_t lcid; +}; + +/** + * struct rwref_lock - Read/Write Reference Lock + * + * kref: reference count + * read_count: number of readers that own the lock + * write_count: number of writers (max 1) that own the lock + * count_zero: used for internal signaling for non-atomic locks + * + * A Read/Write Reference Lock is a combination of a read/write spinlock and a + * refence count. The main difference is that no locks are held in the + * critical section and the lifetime of the object is guaranteed. + * + * Read Locking + * Multiple readers may access the lock at any given time and a read lock will + * also ensure that the object exists for the life of the lock. + * + * rwref_read_get() + * use resource in "critical section" (no locks are held) + * rwref_read_put() + * + * Write Locking + * A single writer may access the lock at any given time and a write lock will + * also ensure that the object exists for the life of the lock. + * + * rwref_write_get() + * use resource in "critical section" (no locks are held) + * rwref_write_put() + * + * Reference Lock + * To ensure the lifetime of the lock (and not affect the read or write lock), + * a simple reference can be done. By default, rwref_lock_init() will set the + * reference count to 1. + * + * rwref_lock_init() Reference count is 1 + * rwref_get() Reference count is 2 + * rwref_put() Reference count is 1 + * rwref_put() Reference count goes to 0 and object is destroyed + */ +struct rwref_lock { + struct kref kref; + unsigned read_count; + unsigned write_count; + spinlock_t lock; + struct completion count_zero; + + void (*release)(struct rwref_lock *); +}; + +/** + * rwref_lock_release() - Initialize rwref_lock + * lock_ptr: pointer to lock structure + */ +static inline void rwref_lock_release(struct kref *kref_ptr) +{ + struct rwref_lock *lock_ptr; + + BUG_ON(kref_ptr == NULL); + + lock_ptr = container_of(kref_ptr, struct rwref_lock, kref); + if (lock_ptr->release) + lock_ptr->release(lock_ptr); +} + +/** + * rwref_lock_init() - Initialize rwref_lock + * lock_ptr: pointer to lock structure + * release: release function called when reference count goes to 0 + */ +static inline void rwref_lock_init(struct rwref_lock *lock_ptr, + void (*release)(struct rwref_lock *)) +{ + BUG_ON(lock_ptr == NULL); + + kref_init(&lock_ptr->kref); + lock_ptr->read_count = 0; + lock_ptr->write_count = 0; + spin_lock_init(&lock_ptr->lock); + init_completion(&lock_ptr->count_zero); + lock_ptr->release = release; +} + +/** + * rwref_get() - gains a reference count for the object + * lock_ptr: pointer to lock structure + */ +static inline void rwref_get(struct rwref_lock *lock_ptr) +{ + BUG_ON(lock_ptr == NULL); + + kref_get(&lock_ptr->kref); +} + +/** + * rwref_put() - puts a reference count for the object + * lock_ptr: pointer to lock structure + * + * If the reference count goes to zero, the release function is called. + */ +static inline void rwref_put(struct rwref_lock *lock_ptr) +{ + BUG_ON(lock_ptr == NULL); + + kref_put(&lock_ptr->kref, rwref_lock_release); +} + +/** + * rwref_read_get() - gains a reference count for a read operation + * lock_ptr: pointer to lock structure + * + * Multiple readers may acquire the lock as long as the write count is zero. + */ +static inline void rwref_read_get(struct rwref_lock *lock_ptr) +{ + unsigned long flags; + + BUG_ON(lock_ptr == NULL); + + kref_get(&lock_ptr->kref); + while (1) { + spin_lock_irqsave(&lock_ptr->lock, flags); + if (lock_ptr->write_count == 0) { + lock_ptr->read_count++; + spin_unlock_irqrestore(&lock_ptr->lock, flags); + break; + } + spin_unlock_irqrestore(&lock_ptr->lock, flags); + wait_for_completion(&lock_ptr->count_zero); + } +} + +/** + * rwref_read_put() - returns a reference count for a read operation + * lock_ptr: pointer to lock structure + * + * Must be preceded by a call to rwref_read_get(). + */ +static inline void rwref_read_put(struct rwref_lock *lock_ptr) +{ + unsigned long flags; + + BUG_ON(lock_ptr == NULL); + + spin_lock_irqsave(&lock_ptr->lock, flags); + BUG_ON(lock_ptr->read_count == 0); + if (--lock_ptr->read_count == 0) + complete(&lock_ptr->count_zero); + spin_unlock_irqrestore(&lock_ptr->lock, flags); + kref_put(&lock_ptr->kref, rwref_lock_release); +} + +/** + * rwref_write_get() - gains a reference count for a write operation + * lock_ptr: pointer to lock structure + * + * Only one writer may acquire the lock as long as the reader count is zero. + */ +static inline void rwref_write_get(struct rwref_lock *lock_ptr) +{ + unsigned long flags; + + BUG_ON(lock_ptr == NULL); + + kref_get(&lock_ptr->kref); + while (1) { + spin_lock_irqsave(&lock_ptr->lock, flags); + if (lock_ptr->read_count == 0 && lock_ptr->write_count == 0) { + lock_ptr->write_count++; + spin_unlock_irqrestore(&lock_ptr->lock, flags); + break; + } + spin_unlock_irqrestore(&lock_ptr->lock, flags); + wait_for_completion(&lock_ptr->count_zero); + } +} + +/** + * rwref_write_put() - returns a reference count for a write operation + * lock_ptr: pointer to lock structure + * + * Must be preceded by a call to rwref_write_get(). + */ +static inline void rwref_write_put(struct rwref_lock *lock_ptr) +{ + unsigned long flags; + + BUG_ON(lock_ptr == NULL); + + spin_lock_irqsave(&lock_ptr->lock, flags); + BUG_ON(lock_ptr->write_count != 1); + if (--lock_ptr->write_count == 0) + complete(&lock_ptr->count_zero); + spin_unlock_irqrestore(&lock_ptr->lock, flags); + kref_put(&lock_ptr->kref, rwref_lock_release); +} + +#endif /* _SOC_QCOM_GLINK_PRIVATE_H_ */ diff --git a/drivers/soc/qcom/glink_smem_native_xprt.c b/drivers/soc/qcom/glink_smem_native_xprt.c new file mode 100644 index 000000000000..2796493dfee2 --- /dev/null +++ b/drivers/soc/qcom/glink_smem_native_xprt.c @@ -0,0 +1,2500 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#include <linux/debugfs.h> +#include <linux/err.h> +#include <linux/fs.h> +#include <linux/gfp.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/ipc_logging.h> +#include <linux/irq.h> +#include <linux/kernel.h> +#include <linux/kthread.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/mutex.h> +#include <linux/of.h> +#include <linux/of_address.h> +#include <linux/of_device.h> +#include <linux/of_irq.h> +#include <linux/platform_device.h> +#include <linux/printk.h> +#include <linux/sched.h> +#include <linux/seq_file.h> +#include <linux/sizes.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/srcu.h> +#include <linux/wait.h> +#include <soc/qcom/tracer_pkt.h> +#include "glink_core_if.h" +#include "glink_private.h" +#include "glink_xprt_if.h" +#include <linux/soc/qcom/smd.h> +#include <soc/qcom/glink.h> + +#define XPRT_NAME "smem" +#define FIFO_FULL_RESERVE 8 +#define FIFO_ALIGNMENT 8 +#define TX_BLOCKED_CMD_RESERVE 8 /* size of struct read_notif_request */ +#define RPM_TOC_ID 0x67727430 +#define RPM_TX_FIFO_ID 0x61703272 +#define RPM_RX_FIFO_ID 0x72326170 +#define RPM_TOC_SIZE 256 +#define RPM_MAX_TOC_ENTRIES 20 +#define RPM_FIFO_ADDR_ALIGN_BYTES 3 +#define TRACER_PKT_FEATURE BIT(2) + +static struct device *glink_dev; +static struct completion glink_ack; +static struct bus_type *ipc_bus; + +#define GLINK_RPM_REQUEST_TIMEOUT 5*HZ + +/** + * enum command_types - definition of the types of commands sent/received + * @VERSION_CMD: Version and feature set supported + * @VERSION_ACK_CMD: Response for @VERSION_CMD + * @OPEN_CMD: Open a channel + * @CLOSE_CMD: Close a channel + * @OPEN_ACK_CMD: Response to @OPEN_CMD + * @RX_INTENT_CMD: RX intent for a channel was queued + * @RX_DONE_CMD: Use of RX intent for a channel is complete + * @RX_INTENT_REQ_CMD: Request to have RX intent queued + * @RX_INTENT_REQ_ACK_CMD: Response for @RX_INTENT_REQ_CMD + * @TX_DATA_CMD: Start of a data transfer + * @ZERO_COPY_TX_DATA_CMD: Start of a data transfer with zero copy + * @CLOSE_ACK_CMD: Response for @CLOSE_CMD + * @TX_DATA_CONT_CMD: Continuation or end of a data transfer + * @READ_NOTIF_CMD: Request for a notification when this cmd is read + * @RX_DONE_W_REUSE_CMD: Same as @RX_DONE but also reuse the used intent + * @SIGNALS_CMD: Sideband signals + * @TRACER_PKT_CMD: Start of a Tracer Packet Command + * @TRACER_PKT_CONT_CMD: Continuation or end of a Tracer Packet Command + */ +enum command_types { + VERSION_CMD, + VERSION_ACK_CMD, + OPEN_CMD, + CLOSE_CMD, + OPEN_ACK_CMD, + RX_INTENT_CMD, + RX_DONE_CMD, + RX_INTENT_REQ_CMD, + RX_INTENT_REQ_ACK_CMD, + TX_DATA_CMD, + ZERO_COPY_TX_DATA_CMD, + CLOSE_ACK_CMD, + TX_DATA_CONT_CMD, + READ_NOTIF_CMD, + RX_DONE_W_REUSE_CMD, + SIGNALS_CMD, + TRACER_PKT_CMD, + TRACER_PKT_CONT_CMD, +}; + +/** + * struct channel_desc - description of a channel fifo with a remote entity + * @read_index: The read index for the fifo where data should be + * consumed from. + * @write_index: The write index for the fifo where data should produced + * to. + * + * This structure resides in SMEM and contains the control information for the + * fifo data pipes of the channel. There is one physical channel between us + * and a remote entity. + */ +struct channel_desc { + uint32_t read_index; + uint32_t write_index; +}; + +/** + * struct edge_info - local information for managing a single complete edge + * @xprt_if: The transport interface registered with the + * glink core associated with this edge. + * @xprt_cfg: The transport configuration for the glink core + * assocaited with this edge. + * @intentless: True if this edge runs in intentless mode. + * @irq_disabled: Flag indicating the whether interrupt is enabled + * or disabled. + * @rx_reset_reg: Reference to the register to reset the rx irq + * line, if applicable. + * @out_irq_reg: Reference to the register to send an irq to the + * remote side. + * @out_irq_mask: Mask written to @out_irq_reg to trigger the + * correct irq. + * @irq_line: The incoming interrupt line. + * @tx_irq_count: Number of interrupts triggered. + * @rx_irq_count: Number of interrupts received. + * @tx_ch_desc: Reference to the channel description structure + * for tx in SMEM for this edge. + * @rx_ch_desc: Reference to the channel description structure + * for rx in SMEM for this edge. + * @tx_fifo: Reference to the transmit fifo in SMEM. + * @rx_fifo: Reference to the receive fifo in SMEM. + * @tx_fifo_size: Total size of @tx_fifo. + * @rx_fifo_size: Total size of @rx_fifo. + * @read_from_fifo: Memcpy for this edge. + * @write_to_fifo: Memcpy for this edge. + * @write_lock: Lock to serialize access to @tx_fifo. + * @tx_blocked_queue: Queue of entities waiting for the remote side to + * signal @tx_fifo has flushed and is now empty. + * @tx_resume_needed: A tx resume signal needs to be sent to the glink + * core once the remote side indicates @tx_fifo has + * flushed. + * @tx_blocked_signal_sent: Flag to indicate the flush signal has already + * been sent, and a response is pending from the + * remote side. Protected by @write_lock. + * @kwork: Work to be executed when an irq is received. + * @kworker: Handle to the entity processing @kwork. + * @task: Handle to the task context used to run @kworker. + * @use_ref: Active uses of this transport use this to grab + * a reference. Used for ssr synchronization. + * @rx_lock: Used to serialize concurrent instances of rx + * processing. + * @deferred_cmds: List of deferred commands that need to be + * processed in process context. + * @num_pw_states: Size of @ramp_time_us. + * @ramp_time_us: Array of ramp times in microseconds where array + * index position represents a power state. + */ +struct edge_info { + struct glink_transport_if xprt_if; + struct glink_core_transport_cfg xprt_cfg; + bool intentless; + bool irq_disabled; + void __iomem *rx_reset_reg; + void __iomem *out_irq_reg; + uint32_t out_irq_mask; + uint32_t irq_line; + uint32_t tx_irq_count; + uint32_t rx_irq_count; + struct channel_desc *tx_ch_desc; + struct channel_desc *rx_ch_desc; + void __iomem *tx_fifo; + void __iomem *rx_fifo; + uint32_t tx_fifo_size; + uint32_t rx_fifo_size; + void * (*read_from_fifo)(void *dest, const void *src, size_t num_bytes); + void * (*write_to_fifo)(void *dest, const void *src, size_t num_bytes); + spinlock_t write_lock; + wait_queue_head_t tx_blocked_queue; + bool tx_resume_needed; + bool tx_blocked_signal_sent; + struct kthread_work kwork; + struct kthread_worker kworker; + struct task_struct *task; + struct srcu_struct use_ref; + spinlock_t rx_lock; + struct list_head deferred_cmds; + uint32_t num_pw_states; + unsigned long *ramp_time_us; +}; + +/** + * struct deferred_cmd - description of a command to be processed later + * @list_node: Used to put this command on a list in the edge. + * @id: ID of the command. + * @param1: Parameter one of the command. + * @param2: Parameter two of the command. + * @data: Extra data associated with the command, if applicable. + * + * This structure stores the relevant information of a command that was removed + * from the fifo but needs to be processed at a later time. + */ +struct deferred_cmd { + struct list_head list_node; + uint16_t id; + uint16_t param1; + uint32_t param2; + void *data; +}; + +static uint32_t negotiate_features_v1(struct glink_transport_if *if_ptr, + const struct glink_core_version *version, + uint32_t features); +static void register_debugfs_info(struct edge_info *einfo); + +static DEFINE_MUTEX(probe_lock); +static struct glink_core_version versions[] = { + {1, TRACER_PKT_FEATURE, negotiate_features_v1}, +}; + +/** + * send_irq() - send an irq to a remote entity as an event signal + * @einfo: Which remote entity that should receive the irq. + */ +static void send_irq(struct edge_info *einfo) +{ + /* + * Any data associated with this event must be visable to the remote + * before the interrupt is triggered + */ + wmb(); + writel_relaxed(einfo->out_irq_mask, einfo->out_irq_reg); + einfo->tx_irq_count++; +} + +/** + * memcpy32_toio() - memcpy to word access only memory + * @dest: Destination address. + * @src: Source address. + * @num_bytes: Number of bytes to copy. + * + * Return: Destination address. + */ +static void *memcpy32_toio(void *dest, const void *src, size_t num_bytes) +{ + uint32_t *dest_local = (uint32_t *)dest; + uint32_t *src_local = (uint32_t *)src; + + BUG_ON(num_bytes & RPM_FIFO_ADDR_ALIGN_BYTES); + BUG_ON(!dest_local || + ((uintptr_t)dest_local & RPM_FIFO_ADDR_ALIGN_BYTES)); + BUG_ON(!src_local || + ((uintptr_t)src_local & RPM_FIFO_ADDR_ALIGN_BYTES)); + num_bytes /= sizeof(uint32_t); + + while (num_bytes--) + __raw_writel(*src_local++, dest_local++); + + return dest; +} + +/** + * memcpy32_fromio() - memcpy from word access only memory + * @dest: Destination address. + * @src: Source address. + * @num_bytes: Number of bytes to copy. + * + * Return: Destination address. + */ +static void *memcpy32_fromio(void *dest, const void *src, size_t num_bytes) +{ + uint32_t *dest_local = (uint32_t *)dest; + uint32_t *src_local = (uint32_t *)src; + + BUG_ON(num_bytes & RPM_FIFO_ADDR_ALIGN_BYTES); + BUG_ON(!dest_local || + ((uintptr_t)dest_local & RPM_FIFO_ADDR_ALIGN_BYTES)); + BUG_ON(!src_local || + ((uintptr_t)src_local & RPM_FIFO_ADDR_ALIGN_BYTES)); + num_bytes /= sizeof(uint32_t); + + while (num_bytes--) + *dest_local++ = __raw_readl(src_local++); + + return dest; +} + +/** + * fifo_read_avail() - how many bytes are available to be read from an edge + * @einfo: The concerned edge to query. + * + * Return: The number of bytes available to be read from edge. + */ +static uint32_t fifo_read_avail(struct edge_info *einfo) +{ + uint32_t read_index = einfo->rx_ch_desc->read_index; + uint32_t write_index = einfo->rx_ch_desc->write_index; + uint32_t fifo_size = einfo->rx_fifo_size; + uint32_t bytes_avail; + + bytes_avail = write_index - read_index; + if (write_index < read_index) + /* + * Case: W < R - Write has wrapped + * -------------------------------- + * In this case, the write operation has wrapped past the end + * of the FIFO which means that now calculating the amount of + * data in the FIFO results in a negative number. This can be + * easily fixed by adding the fifo_size to the value. Even + * though the values are unsigned, subtraction is always done + * using 2's complement which means that the result will still + * be correct once the FIFO size has been added to the negative + * result. + * + * Example: + * '-' = data in fifo + * '.' = empty + * + * 0 1 + * 0123456789012345 + * |-----w.....r----| + * 0 N + * + * write = 5 = 101b + * read = 11 = 1011b + * Data in FIFO + * (write - read) + fifo_size = (101b - 1011b) + 10000b + * = 11111010b + 10000b = 1010b = 10 + */ + bytes_avail += fifo_size; + + return bytes_avail; +} + +/** + * fifo_write_avail() - how many bytes can be written to the edge + * @einfo: The concerned edge to query. + * + * Calculates the number of bytes that can be transmitted at this time. + * Automatically reserves some space to maintain alignment when the fifo is + * completely full, and reserves space so that the flush command can always be + * transmitted when needed. + * + * Return: The number of bytes available to be read from edge. + */ +static uint32_t fifo_write_avail(struct edge_info *einfo) +{ + uint32_t read_index = einfo->tx_ch_desc->read_index; + uint32_t write_index = einfo->tx_ch_desc->write_index; + uint32_t fifo_size = einfo->tx_fifo_size; + uint32_t bytes_avail = read_index - write_index; + + if (read_index <= write_index) + bytes_avail += fifo_size; + if (bytes_avail < FIFO_FULL_RESERVE + TX_BLOCKED_CMD_RESERVE) + bytes_avail = 0; + else + bytes_avail -= FIFO_FULL_RESERVE + TX_BLOCKED_CMD_RESERVE; + + return bytes_avail; +} + +/** + * fifo_read() - read data from an edge + * @einfo: The concerned edge to read from. + * @_data: Buffer to copy the read data into. + * @len: The ammount of data to read in bytes. + * + * Return: The number of bytes read. + */ +static int fifo_read(struct edge_info *einfo, void *_data, int len) +{ + void *ptr; + void *data = _data; + int orig_len = len; + uint32_t read_index = einfo->rx_ch_desc->read_index; + uint32_t write_index = einfo->rx_ch_desc->write_index; + uint32_t fifo_size = einfo->rx_fifo_size; + uint32_t n; + + while (len) { + ptr = einfo->rx_fifo + read_index; + if (read_index <= write_index) + n = write_index - read_index; + else + n = fifo_size - read_index; + + if (n == 0) + break; + if (n > len) + n = len; + + einfo->read_from_fifo(data, ptr, n); + + data += n; + len -= n; + read_index += n; + if (read_index >= fifo_size) + read_index -= fifo_size; + } + einfo->rx_ch_desc->read_index = read_index; + + return orig_len - len; +} + +/** + * fifo_write_body() - Copy transmit data into an edge + * @einfo: The concerned edge to copy into. + * @_data: Buffer of data to copy from. + * @len: Size of data to copy in bytes. + * @write_index: Index into the channel where the data should be copied. + * + * Return: Number of bytes remaining to be copied into the edge. + */ +static uint32_t fifo_write_body(struct edge_info *einfo, const void *_data, + int len, uint32_t *write_index) +{ + void *ptr; + const void *data = _data; + uint32_t read_index = einfo->tx_ch_desc->read_index; + uint32_t fifo_size = einfo->tx_fifo_size; + uint32_t n; + + while (len) { + ptr = einfo->tx_fifo + *write_index; + if (*write_index < read_index) { + n = read_index - *write_index - FIFO_FULL_RESERVE; + } else { + if (read_index < FIFO_FULL_RESERVE) + n = fifo_size + read_index - *write_index - + FIFO_FULL_RESERVE; + else + n = fifo_size - *write_index; + } + + if (n == 0) + break; + if (n > len) + n = len; + + einfo->write_to_fifo(ptr, data, n); + + data += n; + len -= n; + *write_index += n; + if (*write_index >= fifo_size) + *write_index -= fifo_size; + } + return len; +} + +/** + * fifo_write() - Write data into an edge + * @einfo: The concerned edge to write to. + * @data: Buffer of data to write. + * @len: Length of data to write, in bytes. + * + * Wrapper around fifo_write_body() to manage additional details that are + * necessary for a complete write event. Does not manage concurrency. Clients + * should use fifo_write_avail() to check if there is sufficent space before + * calling fifo_write(). + * + * Return: Number of bytes written to the edge. + */ +static int fifo_write(struct edge_info *einfo, const void *data, int len) +{ + int orig_len = len; + uint32_t write_index = einfo->tx_ch_desc->write_index; + + len = fifo_write_body(einfo, data, len, &write_index); + einfo->tx_ch_desc->write_index = write_index; + send_irq(einfo); + + return orig_len - len; +} + +/** + * fifo_write_complex() - writes a transaction of multiple buffers to an edge + * @einfo: The concerned edge to write to. + * @data1: The first buffer of data to write. + * @len1: The length of the first buffer in bytes. + * @data2: The second buffer of data to write. + * @len2: The length of the second buffer in bytes. + * @data3: The thirs buffer of data to write. + * @len3: The length of the third buffer in bytes. + * + * A variant of fifo_write() which optimizes the usecase found in tx(). The + * remote side expects all or none of the transmitted data to be available. + * This prevents the tx() usecase from calling fifo_write() multiple times. The + * alternative would be an allocation and additional memcpy to create a buffer + * to copy all the data segments into one location before calling fifo_write(). + * + * Return: Number of bytes written to the edge. + */ +static int fifo_write_complex(struct edge_info *einfo, + const void *data1, int len1, + const void *data2, int len2, + const void *data3, int len3) +{ + int orig_len = len1 + len2 + len3; + uint32_t write_index = einfo->tx_ch_desc->write_index; + + len1 = fifo_write_body(einfo, data1, len1, &write_index); + len2 = fifo_write_body(einfo, data2, len2, &write_index); + len3 = fifo_write_body(einfo, data3, len3, &write_index); + einfo->tx_ch_desc->write_index = write_index; + send_irq(einfo); + + return orig_len - len1 - len2 - len3; +} + +/** + * send_tx_blocked_signal() - send the flush command as we are blocked from tx + * @einfo: The concerned edge which is blocked. + * + * Used to send a signal to the remote side that we have no more space to + * transmit data and therefore need the remote side to signal us when they have + * cleared some space by reading some data. This function relies upon the + * assumption that fifo_write_avail() will reserve some space so that the flush + * signal command can always be put into the transmit fifo, even when "everyone" + * else thinks that the transmit fifo is truely full. This function assumes + * that it is called with the write_lock already locked. + */ +static void send_tx_blocked_signal(struct edge_info *einfo) +{ + struct read_notif_request { + uint16_t cmd; + uint16_t reserved; + uint32_t reserved2; + }; + struct read_notif_request read_notif_req; + + read_notif_req.cmd = READ_NOTIF_CMD; + read_notif_req.reserved = 0; + read_notif_req.reserved2 = 0; + + if (!einfo->tx_blocked_signal_sent) { + einfo->tx_blocked_signal_sent = true; + fifo_write(einfo, &read_notif_req, sizeof(read_notif_req)); + } +} + +/** + * fifo_tx() - transmit data on an edge + * @einfo: The concerned edge to transmit on. + * @data: Buffer of data to transmit. + * @len: Length of data to transmit in bytes. + * + * This helper function is the preferred interface to fifo_write() and should + * be used in the normal case for transmitting entities. fifo_tx() will block + * until there is sufficent room to transmit the requested ammount of data. + * fifo_tx() will manage any concurrency between multiple transmitters on a + * channel. + * + * Return: Number of bytes transmitted. + */ +static int fifo_tx(struct edge_info *einfo, const void *data, int len) +{ + unsigned long flags; + int ret; + + DEFINE_WAIT(wait); + + spin_lock_irqsave(&einfo->write_lock, flags); + while (fifo_write_avail(einfo) < len) { + send_tx_blocked_signal(einfo); + spin_unlock_irqrestore(&einfo->write_lock, flags); + prepare_to_wait(&einfo->tx_blocked_queue, &wait, + TASK_UNINTERRUPTIBLE); + if (fifo_write_avail(einfo) < len) + schedule(); + finish_wait(&einfo->tx_blocked_queue, &wait); + spin_lock_irqsave(&einfo->write_lock, flags); + } + ret = fifo_write(einfo, data, len); + spin_unlock_irqrestore(&einfo->write_lock, flags); + + return ret; +} + +/** + * process_rx_data() - process received data from an edge + * @einfo: The edge the data was received on. + * @cmd_id: ID to specify the type of data. + * @rcid: The remote channel id associated with the data. + * @intend_id: The intent the data should be put in. + */ +static void process_rx_data(struct edge_info *einfo, uint16_t cmd_id, + uint32_t rcid, uint32_t intent_id) +{ + struct command { + uint32_t frag_size; + uint32_t size_remaining; + }; + struct command cmd; + struct glink_core_rx_intent *intent; + char trash[FIFO_ALIGNMENT]; + int alignment; + bool err = false; + + fifo_read(einfo, &cmd, sizeof(cmd)); + + intent = einfo->xprt_if.glink_core_if_ptr->rx_get_pkt_ctx( + &einfo->xprt_if, rcid, intent_id); + if (intent == NULL) { + GLINK_ERR("%s: no intent for ch %d liid %d\n", __func__, rcid, + intent_id); + err = true; + } else if (intent->data == NULL) { + if (einfo->intentless) { + intent->data = kmalloc(cmd.frag_size, GFP_ATOMIC); + if (!intent->data) + err = true; + else + intent->intent_size = cmd.frag_size; + } else { + GLINK_ERR( + "%s: intent for ch %d liid %d has no data buff\n", + __func__, rcid, intent_id); + err = true; + } + } + + if (!err && + (intent->intent_size - intent->write_offset < cmd.frag_size || + intent->write_offset + cmd.size_remaining > intent->intent_size)) { + GLINK_ERR("%s: rx data size:%d and remaining:%d %s %d %s:%d\n", + __func__, + cmd.frag_size, + cmd.size_remaining, + "will overflow ch", + rcid, + "intent", + intent_id); + err = true; + } + + if (err) { + alignment = ALIGN(cmd.frag_size, FIFO_ALIGNMENT); + alignment -= cmd.frag_size; + while (cmd.frag_size) { + if (cmd.frag_size > FIFO_ALIGNMENT) { + fifo_read(einfo, trash, FIFO_ALIGNMENT); + cmd.frag_size -= FIFO_ALIGNMENT; + } else { + fifo_read(einfo, trash, cmd.frag_size); + cmd.frag_size = 0; + } + } + if (alignment) + fifo_read(einfo, trash, alignment); + return; + } + fifo_read(einfo, intent->data + intent->write_offset, cmd.frag_size); + intent->write_offset += cmd.frag_size; + intent->pkt_size += cmd.frag_size; + + alignment = ALIGN(cmd.frag_size, FIFO_ALIGNMENT); + alignment -= cmd.frag_size; + if (alignment) + fifo_read(einfo, trash, alignment); + + if (unlikely((cmd_id == TRACER_PKT_CMD || + cmd_id == TRACER_PKT_CONT_CMD) && !cmd.size_remaining)) { + tracer_pkt_log_event(intent->data, GLINK_XPRT_RX); + intent->tracer_pkt = true; + } + + einfo->xprt_if.glink_core_if_ptr->rx_put_pkt_ctx(&einfo->xprt_if, + rcid, + intent, + cmd.size_remaining ? + false : true); +} + +/** + * queue_cmd() - queue a deferred command for later processing + * @einfo: Edge to queue commands on. + * @cmd: Command to queue. + * @data: Command specific data to queue with the command. + * + * Return: True if queuing was successful, false otherwise. + */ +static bool queue_cmd(struct edge_info *einfo, void *cmd, void *data) +{ + struct command { + uint16_t id; + uint16_t param1; + uint32_t param2; + }; + struct command *_cmd = cmd; + struct deferred_cmd *d_cmd; + + d_cmd = kmalloc(sizeof(*d_cmd), GFP_ATOMIC); + if (!d_cmd) { + GLINK_ERR("%s: Discarding cmd %d\n", __func__, _cmd->id); + return false; + } + d_cmd->id = _cmd->id; + d_cmd->param1 = _cmd->param1; + d_cmd->param2 = _cmd->param2; + d_cmd->data = data; + list_add_tail(&d_cmd->list_node, &einfo->deferred_cmds); + queue_kthread_work(&einfo->kworker, &einfo->kwork); + return true; +} + +/** + * get_rx_fifo() - Find the rx fifo for an edge + * @einfo: Edge to find the fifo for. + * + * Return: True if fifo was found, false otherwise. + */ +static bool get_rx_fifo(struct edge_info *einfo) +{ + return true; +} + +/** + * __rx_worker() - process received commands on a specific edge + * @einfo: Edge to process commands on. + * @atomic_ctx: Indicates if the caller is in atomic context and requires any + * non-atomic operations to be deferred. + */ +static void __rx_worker(struct edge_info *einfo, bool atomic_ctx) +{ + struct command { + uint16_t id; + uint16_t param1; + uint32_t param2; + }; + struct intent_desc { + uint32_t size; + uint32_t id; + }; + struct command cmd; + struct intent_desc intent; + struct intent_desc *intents; + int i; + bool granted; + unsigned long flags; + bool trigger_wakeup = false; + int rcu_id; + uint16_t rcid; + uint32_t name_len; + uint32_t len; + char *name; + char trash[FIFO_ALIGNMENT]; + struct deferred_cmd *d_cmd; + void *cmd_data; + + rcu_id = srcu_read_lock(&einfo->use_ref); + + if (unlikely(!einfo->rx_fifo)) { + if (!get_rx_fifo(einfo)) { + srcu_read_unlock(&einfo->use_ref, rcu_id); + return; + } + einfo->xprt_if.glink_core_if_ptr->link_up(&einfo->xprt_if); + } + + if (!atomic_ctx) { + if (einfo->tx_resume_needed && fifo_write_avail(einfo)) { + einfo->tx_resume_needed = false; + einfo->xprt_if.glink_core_if_ptr->tx_resume( + &einfo->xprt_if); + } + spin_lock_irqsave(&einfo->write_lock, flags); + if (waitqueue_active(&einfo->tx_blocked_queue)) { + einfo->tx_blocked_signal_sent = false; + trigger_wakeup = true; + } + spin_unlock_irqrestore(&einfo->write_lock, flags); + if (trigger_wakeup) + wake_up_all(&einfo->tx_blocked_queue); + } + + + /* + * Access to the fifo needs to be synchronized, however only the calls + * into the core from process_rx_data() are compatible with an atomic + * processing context. For everything else, we need to do all the fifo + * processing, then unlock the lock for the call into the core. Data + * in the fifo is allowed to be processed immediately instead of being + * ordered with the commands because the channel open process prevents + * intents from being queued (which prevents data from being sent) until + * all the channel open commands are processed by the core, thus + * eliminating a race. + */ + spin_lock_irqsave(&einfo->rx_lock, flags); + while (fifo_read_avail(einfo) || + (!atomic_ctx && !list_empty(&einfo->deferred_cmds))) { + + if (!atomic_ctx && !list_empty(&einfo->deferred_cmds)) { + d_cmd = list_first_entry(&einfo->deferred_cmds, + struct deferred_cmd, list_node); + list_del(&d_cmd->list_node); + cmd.id = d_cmd->id; + cmd.param1 = d_cmd->param1; + cmd.param2 = d_cmd->param2; + cmd_data = d_cmd->data; + kfree(d_cmd); + } else { + fifo_read(einfo, &cmd, sizeof(cmd)); + cmd_data = NULL; + } + + switch (cmd.id) { + case VERSION_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_version( + &einfo->xprt_if, + cmd.param1, + cmd.param2); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case VERSION_ACK_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_version_ack( + &einfo->xprt_if, + cmd.param1, + cmd.param2); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case OPEN_CMD: + rcid = cmd.param1; + name_len = cmd.param2; + + if (cmd_data) { + name = cmd_data; + } else { + len = ALIGN(name_len, FIFO_ALIGNMENT); + name = kmalloc(len, GFP_ATOMIC); + if (!name) { + pr_err("No memory available to rx ch open cmd name. Discarding cmd.\n"); + while (len) { + fifo_read(einfo, trash, + FIFO_ALIGNMENT); + len -= FIFO_ALIGNMENT; + } + break; + } + fifo_read(einfo, name, len); + } + if (atomic_ctx) { + if (!queue_cmd(einfo, &cmd, name)) + kfree(name); + break; + } + + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_ch_remote_open( + &einfo->xprt_if, + rcid, + name, + SMEM_XPRT_ID); + kfree(name); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case CLOSE_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr-> + rx_cmd_ch_remote_close( + &einfo->xprt_if, + cmd.param1); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case OPEN_ACK_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_ch_open_ack( + &einfo->xprt_if, + cmd.param1, + SMEM_XPRT_ID); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case RX_INTENT_CMD: + /* + * One intent listed with this command. This is the + * expected case and can be optimized over the general + * case of an array of intents. + */ + if (cmd.param2 == 1) { + if (cmd_data) { + intent.id = ((struct intent_desc *) + cmd_data)->id; + intent.size = ((struct intent_desc *) + cmd_data)->size; + kfree(cmd_data); + } else { + fifo_read(einfo, &intent, + sizeof(intent)); + } + if (atomic_ctx) { + cmd_data = kmalloc(sizeof(intent), + GFP_ATOMIC); + if (!cmd_data) { + pr_err("%s: dropping cmd %d\n", + __func__, + cmd.id); + break; + } + ((struct intent_desc *)cmd_data)->id = + intent.id; + ((struct intent_desc *)cmd_data)->size = + intent.size; + if (!queue_cmd(einfo, &cmd, cmd_data)) + kfree(cmd_data); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr-> + rx_cmd_remote_rx_intent_put( + &einfo->xprt_if, + cmd.param1, + intent.id, + intent.size); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + } + + /* Array of intents to process */ + if (cmd_data) { + intents = cmd_data; + } else { + intents = kmalloc(sizeof(*intents) * cmd.param2, + GFP_ATOMIC); + if (!intents) { + for (i = 0; i < cmd.param2; ++i) + fifo_read(einfo, &intent, + sizeof(intent)); + break; + } + fifo_read(einfo, intents, + sizeof(*intents) * cmd.param2); + } + if (atomic_ctx) { + if (!queue_cmd(einfo, &cmd, intents)) + kfree(intents); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + for (i = 0; i < cmd.param2; ++i) { + einfo->xprt_if.glink_core_if_ptr-> + rx_cmd_remote_rx_intent_put( + &einfo->xprt_if, + cmd.param1, + intents[i].id, + intents[i].size); + } + kfree(intents); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case RX_DONE_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_tx_done( + &einfo->xprt_if, + cmd.param1, + cmd.param2, + false); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case RX_INTENT_REQ_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr-> + rx_cmd_remote_rx_intent_req( + &einfo->xprt_if, + cmd.param1, + cmd.param2); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case RX_INTENT_REQ_ACK_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + granted = false; + if (cmd.param2 == 1) + granted = true; + einfo->xprt_if.glink_core_if_ptr-> + rx_cmd_rx_intent_req_ack( + &einfo->xprt_if, + cmd.param1, + granted); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case TX_DATA_CMD: + case TX_DATA_CONT_CMD: + case TRACER_PKT_CMD: + case TRACER_PKT_CONT_CMD: + process_rx_data(einfo, cmd.id, cmd.param1, cmd.param2); + break; + case CLOSE_ACK_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_ch_close_ack( + &einfo->xprt_if, + cmd.param1); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case READ_NOTIF_CMD: + send_irq(einfo); + break; + case SIGNALS_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_remote_sigs( + &einfo->xprt_if, + cmd.param1, + cmd.param2); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + case RX_DONE_W_REUSE_CMD: + if (atomic_ctx) { + queue_cmd(einfo, &cmd, NULL); + break; + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + einfo->xprt_if.glink_core_if_ptr->rx_cmd_tx_done( + &einfo->xprt_if, + cmd.param1, + cmd.param2, + true); + spin_lock_irqsave(&einfo->rx_lock, flags); + break; + default: + pr_err("Unrecognized command: %d\n", cmd.id); + break; + } + } + spin_unlock_irqrestore(&einfo->rx_lock, flags); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * rx_worker() - worker function to process received commands + * @work: kwork associated with the edge to process commands on. + */ +static void rx_worker(struct kthread_work *work) +{ + struct edge_info *einfo; + + einfo = container_of(work, struct edge_info, kwork); + __rx_worker(einfo, false); +} + +irqreturn_t irq_handler(int irq, void *priv) +{ + struct edge_info *einfo = (struct edge_info *)priv; + + if (einfo->rx_reset_reg) + writel_relaxed(einfo->out_irq_mask, einfo->rx_reset_reg); + + queue_kthread_work(&einfo->kworker, &einfo->kwork); + einfo->rx_irq_count++; + + return IRQ_HANDLED; +} + +/** + * tx_cmd_version() - convert a version cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @version: The version number to encode. + * @features: The features information to encode. + */ +static void tx_cmd_version(struct glink_transport_if *if_ptr, uint32_t version, + uint32_t features) +{ + struct command { + uint16_t id; + uint16_t version; + uint32_t features; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = VERSION_CMD; + cmd.version = version; + cmd.features = features; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * tx_cmd_version_ack() - convert a version ack cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @version: The version number to encode. + * @features: The features information to encode. + */ +static void tx_cmd_version_ack(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features) +{ + struct command { + uint16_t id; + uint16_t version; + uint32_t features; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = VERSION_ACK_CMD; + cmd.version = version; + cmd.features = features; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * set_version() - activate a negotiated version and feature set + * @if_ptr: The transport to configure. + * @version: The version to use. + * @features: The features to use. + * + * Return: The supported capabilities of the transport. + */ +static uint32_t set_version(struct glink_transport_if *if_ptr, uint32_t version, + uint32_t features) +{ + struct edge_info *einfo; + uint32_t ret; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + ret = einfo->intentless ? + GCAP_INTENTLESS | GCAP_SIGNALS : GCAP_SIGNALS; + + if (features & TRACER_PKT_FEATURE) + ret |= GCAP_TRACER_PKT; + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return ret; +} + +/** + * tx_cmd_ch_open() - convert a channel open cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @name: The channel name to encode. + * @req_xprt: The transport the core would like to migrate this channel to. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_ch_open(struct glink_transport_if *if_ptr, uint32_t lcid, + const char *name, uint16_t req_xprt) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t length; + }; + struct command cmd; + struct edge_info *einfo; + uint32_t buf_size; + void *buf; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = OPEN_CMD; + cmd.lcid = lcid; + cmd.length = strlen(name) + 1; + + buf_size = ALIGN(sizeof(cmd) + cmd.length, FIFO_ALIGNMENT); + + buf = kzalloc(buf_size, GFP_KERNEL); + if (!buf) { + pr_err("%s: malloc fail for %d size buf\n", __func__, buf_size); + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -ENOMEM; + } + + memcpy(buf, &cmd, sizeof(cmd)); + memcpy(buf + sizeof(cmd), name, cmd.length); + + fifo_tx(einfo, buf, buf_size); + + kfree(buf); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_cmd_ch_close() - convert a channel close cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_ch_close(struct glink_transport_if *if_ptr, uint32_t lcid) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t reserved; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = CLOSE_CMD; + cmd.lcid = lcid; + cmd.reserved = 0; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_cmd_ch_remote_open_ack() - convert a channel open ack cmd to wire format + * and transmit + * @if_ptr: The transport to transmit on. + * @rcid: The remote channel id to encode. + * @xprt_resp: The response to a transport migration request. + */ +static void tx_cmd_ch_remote_open_ack(struct glink_transport_if *if_ptr, + uint32_t rcid, uint16_t xprt_resp) +{ + struct command { + uint16_t id; + uint16_t rcid; + uint32_t reserved; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = OPEN_ACK_CMD; + cmd.rcid = rcid; + cmd.reserved = 0; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * tx_cmd_ch_remote_close_ack() - convert a channel close ack cmd to wire format + * and transmit + * @if_ptr: The transport to transmit on. + * @rcid: The remote channel id to encode. + */ +static void tx_cmd_ch_remote_close_ack(struct glink_transport_if *if_ptr, + uint32_t rcid) +{ + struct command { + uint16_t id; + uint16_t rcid; + uint32_t reserved; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = CLOSE_ACK_CMD; + cmd.rcid = rcid; + cmd.reserved = 0; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * int wait_link_down() - Check status of read/write indices + * @if_ptr: The transport to check + * + * Return: 1 if indices are all zero, 0 otherwise + */ +int wait_link_down(struct glink_transport_if *if_ptr) +{ + struct edge_info *einfo; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (einfo->tx_ch_desc->write_index == 0 && + einfo->tx_ch_desc->read_index == 0 && + einfo->rx_ch_desc->write_index == 0 && + einfo->rx_ch_desc->read_index == 0) + return 1; + else + return 0; +} + +/** + * allocate_rx_intent() - allocate/reserve space for RX Intent + * @if_ptr: The transport the intent is associated with. + * @size: size of intent. + * @intent: Pointer to the intent structure. + * + * Assign "data" with the buffer created, since the transport creates + * a linear buffer and "iovec" with the "intent" itself, so that + * the data can be passed to a client that receives only vector buffer. + * Note that returning NULL for the pointer is valid (it means that space has + * been reserved, but the actual pointer will be provided later). + * + * Return: 0 on success or standard Linux error code. + */ +static int allocate_rx_intent(struct glink_transport_if *if_ptr, size_t size, + struct glink_core_rx_intent *intent) +{ + void *t; + + t = kmalloc(size, GFP_KERNEL); + if (!t) + return -ENOMEM; + + intent->data = t; + intent->iovec = (void *)intent; + intent->vprovider = rx_linear_vbuf_provider; + intent->pprovider = NULL; + return 0; +} + +/** + * deallocate_rx_intent() - Deallocate space created for RX Intent + * @if_ptr: The transport the intent is associated with. + * @intent: Pointer to the intent structure. + * + * Return: 0 on success or standard Linux error code. + */ +static int deallocate_rx_intent(struct glink_transport_if *if_ptr, + struct glink_core_rx_intent *intent) +{ + if (!intent || !intent->data) + return -EINVAL; + + kfree(intent->data); + intent->data = NULL; + intent->iovec = NULL; + intent->vprovider = NULL; + return 0; +} + +/** + * tx_cmd_local_rx_intent() - convert an rx intent cmd to wire format and + * transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @size: The intent size to encode. + * @liid: The local intent id to encode. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_local_rx_intent(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size, uint32_t liid) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t count; + uint32_t size; + uint32_t liid; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + if (size > UINT_MAX) { + pr_err("%s: size %zu is too large to encode\n", __func__, size); + return -EMSGSIZE; + } + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (einfo->intentless) + return -EOPNOTSUPP; + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = RX_INTENT_CMD; + cmd.lcid = lcid; + cmd.count = 1; + cmd.size = size; + cmd.liid = liid; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_cmd_local_rx_done() - convert an rx done cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @liid: The local intent id to encode. + * @reuse: Reuse the consumed intent. + */ +static void tx_cmd_local_rx_done(struct glink_transport_if *if_ptr, + uint32_t lcid, uint32_t liid, bool reuse) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t liid; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (einfo->intentless) + return; + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = reuse ? RX_DONE_W_REUSE_CMD : RX_DONE_CMD; + cmd.lcid = lcid; + cmd.liid = liid; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + srcu_read_unlock(&einfo->use_ref, rcu_id); +} + +/** + * tx_cmd_rx_intent_req() - convert an rx intent request cmd to wire format and + * transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @size: The requested intent size to encode. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_rx_intent_req(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t size; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + if (size > UINT_MAX) { + pr_err("%s: size %zu is too large to encode\n", __func__, size); + return -EMSGSIZE; + } + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (einfo->intentless) + return -EOPNOTSUPP; + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = RX_INTENT_REQ_CMD, + cmd.lcid = lcid; + cmd.size = size; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_cmd_rx_intent_req_ack() - convert an rx intent request ack cmd to wire + * format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @granted: The request response to encode. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_remote_rx_intent_req_ack(struct glink_transport_if *if_ptr, + uint32_t lcid, bool granted) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t response; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (einfo->intentless) + return -EOPNOTSUPP; + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = RX_INTENT_REQ_ACK_CMD, + cmd.lcid = lcid; + if (granted) + cmd.response = 1; + else + cmd.response = 0; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_cmd_set_sigs() - convert a signals ack cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @sigs: The signals to encode. + * + * Return: 0 on success or standard Linux error code. + */ +static int tx_cmd_set_sigs(struct glink_transport_if *if_ptr, uint32_t lcid, + uint32_t sigs) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t sigs; + }; + struct command cmd; + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + cmd.id = SIGNALS_CMD, + cmd.lcid = lcid; + cmd.sigs = sigs; + + fifo_tx(einfo, &cmd, sizeof(cmd)); + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * poll() - poll for data on a channel + * @if_ptr: The transport the channel exists on. + * @lcid: The local channel id. + * + * Return: 0 if no data available, 1 if data available. + */ +static int poll(struct glink_transport_if *if_ptr, uint32_t lcid) +{ + struct edge_info *einfo; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + if (fifo_read_avail(einfo)) { + __rx_worker(einfo, true); + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 1; + } + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * mask_rx_irq() - mask the receive irq for a channel + * @if_ptr: The transport the channel exists on. + * @lcid: The local channel id for the channel. + * @mask: True to mask the irq, false to unmask. + * @pstruct: Platform defined structure for handling the masking. + * + * Return: 0 on success or standard Linux error code. + */ +static int mask_rx_irq(struct glink_transport_if *if_ptr, uint32_t lcid, + bool mask, void *pstruct) +{ + struct edge_info *einfo; + struct irq_chip *irq_chip; + struct irq_data *irq_data; + int rcu_id; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + irq_chip = irq_get_chip(einfo->irq_line); + if (!irq_chip) { + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -ENODEV; + } + + irq_data = irq_get_irq_data(einfo->irq_line); + if (!irq_data) { + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -ENODEV; + } + + if (mask) { + irq_chip->irq_mask(irq_data); + einfo->irq_disabled = true; + if (pstruct) + irq_set_affinity(einfo->irq_line, pstruct); + } else { + irq_chip->irq_unmask(irq_data); + einfo->irq_disabled = false; + } + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return 0; +} + +/** + * tx_data() - convert a data/tracer_pkt to wire format and transmit + * @if_ptr: The transport to transmit on. + * @cmd_id: The command ID to transmit. + * @lcid: The local channel id to encode. + * @pctx: The data to encode. + * + * Return: Number of bytes written or standard Linux error code. + */ +static int tx_data(struct glink_transport_if *if_ptr, uint16_t cmd_id, + uint32_t lcid, struct glink_core_tx_pkt *pctx) +{ + struct command { + uint16_t id; + uint16_t lcid; + uint32_t riid; + uint32_t size; + uint32_t size_left; + }; + struct command cmd; + struct edge_info *einfo; + uint32_t size; + uint32_t zeros_size; + const void *data_start; + char zeros[FIFO_ALIGNMENT] = { 0 }; + unsigned long flags; + size_t tx_size = 0; + int rcu_id; + + if (pctx->size < pctx->size_remaining) { + GLINK_ERR("%s: size remaining exceeds size. Resetting.\n", + __func__); + pctx->size_remaining = pctx->size; + } + if (!pctx->size_remaining) + return 0; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + rcu_id = srcu_read_lock(&einfo->use_ref); + + if (einfo->intentless && + (pctx->size_remaining != pctx->size || cmd_id == TRACER_PKT_CMD)) { + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -EINVAL; + } + + if (cmd_id == TX_DATA_CMD) { + if (pctx->size_remaining == pctx->size) + cmd.id = TX_DATA_CMD; + else + cmd.id = TX_DATA_CONT_CMD; + } else { + if (pctx->size_remaining == pctx->size) + cmd.id = TRACER_PKT_CMD; + else + cmd.id = TRACER_PKT_CONT_CMD; + } + cmd.lcid = lcid; + cmd.riid = pctx->riid; + data_start = get_tx_vaddr(pctx, pctx->size - pctx->size_remaining, + &tx_size); + if (!data_start) { + GLINK_ERR("%s: invalid data_start\n", __func__); + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -EINVAL; + } + + spin_lock_irqsave(&einfo->write_lock, flags); + size = fifo_write_avail(einfo); + + /* Intentless clients expect a complete commit or instant failure */ + if (einfo->intentless && size < sizeof(cmd) + pctx->size) { + spin_unlock_irqrestore(&einfo->write_lock, flags); + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -ENOSPC; + } + + /* Need enough space to write the command and some data */ + if (size <= sizeof(cmd)) { + einfo->tx_resume_needed = true; + spin_unlock_irqrestore(&einfo->write_lock, flags); + srcu_read_unlock(&einfo->use_ref, rcu_id); + return -EAGAIN; + } + size -= sizeof(cmd); + if (size > tx_size) + size = tx_size; + + cmd.size = size; + pctx->size_remaining -= size; + cmd.size_left = pctx->size_remaining; + zeros_size = ALIGN(size, FIFO_ALIGNMENT) - cmd.size; + if (cmd.id == TRACER_PKT_CMD) + tracer_pkt_log_event((void *)(pctx->data), GLINK_XPRT_TX); + + fifo_write_complex(einfo, &cmd, sizeof(cmd), data_start, size, zeros, + zeros_size); + GLINK_DBG("%s %s: lcid[%u] riid[%u] cmd[%d], size[%d], size_left[%d]\n", + "<SMEM>", __func__, cmd.lcid, cmd.riid, cmd.id, cmd.size, + cmd.size_left); + spin_unlock_irqrestore(&einfo->write_lock, flags); + + /* Fake tx_done for intentless since its not supported over the wire */ + if (einfo->intentless) { + spin_lock_irqsave(&einfo->rx_lock, flags); + cmd.id = RX_DONE_CMD; + cmd.lcid = pctx->rcid; + queue_cmd(einfo, &cmd, NULL); + spin_unlock_irqrestore(&einfo->rx_lock, flags); + } + + srcu_read_unlock(&einfo->use_ref, rcu_id); + return cmd.size; +} + +/** + * tx() - convert a data transmit cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @pctx: The data to encode. + * + * Return: Number of bytes written or standard Linux error code. + */ +static int tx(struct glink_transport_if *if_ptr, uint32_t lcid, + struct glink_core_tx_pkt *pctx) +{ + return tx_data(if_ptr, TX_DATA_CMD, lcid, pctx); +} + +/** + * tx_cmd_tracer_pkt() - convert a tracer packet cmd to wire format and transmit + * @if_ptr: The transport to transmit on. + * @lcid: The local channel id to encode. + * @pctx: The data to encode. + * + * Return: Number of bytes written or standard Linux error code. + */ +static int tx_cmd_tracer_pkt(struct glink_transport_if *if_ptr, uint32_t lcid, + struct glink_core_tx_pkt *pctx) +{ + return tx_data(if_ptr, TRACER_PKT_CMD, lcid, pctx); +} + +/** + * get_power_vote_ramp_time() - Get the ramp time required for the power + * votes to be applied + * @if_ptr: The transport interface on which power voting is requested. + * @state: The power state for which ramp time is required. + * + * Return: The ramp time specific to the power state, standard error otherwise. + */ +static unsigned long get_power_vote_ramp_time( + struct glink_transport_if *if_ptr, + uint32_t state) +{ + struct edge_info *einfo; + + einfo = container_of(if_ptr, struct edge_info, xprt_if); + + if (state >= einfo->num_pw_states || !(einfo->ramp_time_us)) + return (unsigned long)ERR_PTR(-EINVAL); + + return einfo->ramp_time_us[state]; +} + +/** + * power_vote() - Update the power votes to meet qos requirement + * @if_ptr: The transport interface on which power voting is requested. + * @state: The power state for which the voting should be done. + * + * Return: 0 on Success, standard error otherwise. + */ +static int power_vote(struct glink_transport_if *if_ptr, uint32_t state) +{ + return 0; +} + +/** + * power_unvote() - Remove the all the power votes + * @if_ptr: The transport interface on which power voting is requested. + * + * Return: 0 on Success, standard error otherwise. + */ +static int power_unvote(struct glink_transport_if *if_ptr) +{ + return 0; +} + +/** + * negotiate_features_v1() - determine what features of a version can be used + * @if_ptr: The transport for which features are negotiated for. + * @version: The version negotiated. + * @features: The set of requested features. + * + * Return: What set of the requested features can be supported. + */ +static uint32_t negotiate_features_v1(struct glink_transport_if *if_ptr, + const struct glink_core_version *version, + uint32_t features) +{ + return features & version->features; +} + +/** + * init_xprt_if() - initialize the xprt_if for an edge + * @einfo: The edge to initialize. + */ +static void init_xprt_if(struct edge_info *einfo) +{ + einfo->xprt_if.tx_cmd_version = tx_cmd_version; + einfo->xprt_if.tx_cmd_version_ack = tx_cmd_version_ack; + einfo->xprt_if.set_version = set_version; + einfo->xprt_if.tx_cmd_ch_open = tx_cmd_ch_open; + einfo->xprt_if.tx_cmd_ch_close = tx_cmd_ch_close; + einfo->xprt_if.tx_cmd_ch_remote_open_ack = tx_cmd_ch_remote_open_ack; + einfo->xprt_if.tx_cmd_ch_remote_close_ack = tx_cmd_ch_remote_close_ack; + einfo->xprt_if.allocate_rx_intent = allocate_rx_intent; + einfo->xprt_if.deallocate_rx_intent = deallocate_rx_intent; + einfo->xprt_if.tx_cmd_local_rx_intent = tx_cmd_local_rx_intent; + einfo->xprt_if.tx_cmd_local_rx_done = tx_cmd_local_rx_done; + einfo->xprt_if.tx = tx; + einfo->xprt_if.tx_cmd_rx_intent_req = tx_cmd_rx_intent_req; + einfo->xprt_if.tx_cmd_remote_rx_intent_req_ack = + tx_cmd_remote_rx_intent_req_ack; + einfo->xprt_if.tx_cmd_set_sigs = tx_cmd_set_sigs; + einfo->xprt_if.poll = poll; + einfo->xprt_if.mask_rx_irq = mask_rx_irq; + einfo->xprt_if.wait_link_down = wait_link_down; + einfo->xprt_if.tx_cmd_tracer_pkt = tx_cmd_tracer_pkt; + einfo->xprt_if.get_power_vote_ramp_time = get_power_vote_ramp_time; + einfo->xprt_if.power_vote = power_vote; + einfo->xprt_if.power_unvote = power_unvote; +} + +static struct qcom_smd_device *to_ipc_device(struct device *dev) +{ + return container_of(dev, struct qcom_smd_device, dev); +} + +static struct qcom_smd_driver *to_ipc_driver(struct device *dev) +{ + struct qcom_smd_device *qidev = to_ipc_device(dev); + + return container_of(qidev->dev.driver, struct qcom_smd_driver, driver); +} + +static int qcom_ipc_dev_match(struct device *dev, struct device_driver *drv) +{ + return of_driver_match_device(dev, drv); +} + +static void msm_rpm_trans_notify_tx_done(void *handle, const void *priv, + const void *pkt_priv, const void *ptr) +{ + return; +} + +static void msm_rpm_trans_notify_state(void *handle, const void *priv, + unsigned event) +{ + switch (event) { + case GLINK_CONNECTED: + if (IS_ERR_OR_NULL(handle)) { + pr_err("glink_handle %d\n", + (int)PTR_ERR(handle)); + BUG_ON(1); + } + complete(&glink_ack); + break; + default: + break; + } +} + +/* + * Probe the ipc client. + */ +static int qcom_ipc_dev_probe(struct device *dev) +{ + struct qcom_smd_device *qidev = to_ipc_device(dev); + struct qcom_smd_driver *qidrv = to_ipc_driver(dev); + struct glink_open_config *open_config; + const char *channel_name, *key; + int ret; + + key = "qcom,glink-channels"; + ret = of_property_read_string(dev->of_node, key, + &channel_name); + + if (ret) { + pr_err("Failed to read node: %s, key=%s\n", + dev->of_node->full_name, key); + return ret; + } + + open_config = kzalloc(sizeof(*open_config), GFP_KERNEL); + + /* open a glink channel */ + open_config->name = channel_name; + open_config->priv = qidev; + open_config->edge = dev_get_drvdata(dev); + open_config->notify_rx = qidrv->callback; + open_config->notify_tx_done = msm_rpm_trans_notify_tx_done; + open_config->notify_state = msm_rpm_trans_notify_state; + + qidev->channel = glink_open(open_config); + ret = wait_for_completion_timeout(&glink_ack, GLINK_RPM_REQUEST_TIMEOUT); + if (!ret) + return -ETIMEDOUT; + + ret = qidrv->probe(qidev); + if (ret) + goto err; + + return 0; + +err: + dev_err(&qidev->dev, "probe failed\n"); + return ret; +} + +static int qcom_ipc_dev_remove(struct device *dev) +{ + struct qcom_smd_device *qidev = to_ipc_device(dev); + struct qcom_smd_driver *qidrv = to_ipc_driver(dev); + int ret; + + ret = glink_close(qidev->channel); + if (ret) + dev_err(&qidev->dev, "glink_close failed"); + + qidrv->remove(qidev); + + return ret; +} + +static struct bus_type qcom_ipc_bus = { + .name = "qcom_ipc", + .match = qcom_ipc_dev_match, + .probe = qcom_ipc_dev_probe, + .remove = qcom_ipc_dev_remove, +}; + +/* + * Release function for the qcom_smd_device object. + */ +static void qcom_ipc_release_device(struct device *dev) +{ + struct qcom_smd_device *qidev = to_ipc_device(dev); + + kfree(qidev); +} + +/* + * Create a ipc client device for channel that is being opened. + */ +static int qcom_ipc_create_device(struct device_node *node, + const void *edge_name) +{ + struct qcom_smd_device *qidev; + const char *name = edge_name; + int ret; + + qidev = kzalloc(sizeof(*qidev), GFP_KERNEL); + if (!qidev) + return -ENOMEM; + + dev_set_name(&qidev->dev, "%s.%s", name, node->name); + qidev->dev.parent = glink_dev; + qidev->dev.bus = &qcom_ipc_bus; + qidev->dev.release = qcom_ipc_release_device; + qidev->dev.of_node = node; + + dev_set_drvdata(&qidev->dev, (void *)edge_name); + + ret = device_register(&qidev->dev); + if (ret) { + dev_err(&qidev->dev, "device_register failed: %d\n", ret); + put_device(&qidev->dev); + } + + return ret; +} + +/** + * init_xprt_cfg() - initialize the xprt_cfg for an edge + * @einfo: The edge to initialize. + * @name: The name of the remote side this edge communicates to. + */ +static void init_xprt_cfg(struct edge_info *einfo, const char *name) +{ + einfo->xprt_cfg.name = XPRT_NAME; + einfo->xprt_cfg.edge = name; + einfo->xprt_cfg.versions = versions; + einfo->xprt_cfg.versions_entries = ARRAY_SIZE(versions); + einfo->xprt_cfg.max_cid = SZ_64K; + einfo->xprt_cfg.max_iid = SZ_2G; +} + +static int glink_edge_parse(struct device_node *node, const char *edge_name) +{ + struct device_node *child_node; + struct edge_info *einfo; + int rc; + char *key; + const char *subsys_name; + uint32_t irq_line; + uint32_t irq_mask; + struct resource irq_r; + struct resource msgram_r; + void __iomem *msgram; + char toc[RPM_TOC_SIZE]; + uint32_t *tocp; + uint32_t num_toc_entries; + + einfo = kzalloc(sizeof(*einfo), GFP_KERNEL); + if (!einfo) { + pr_err("%s: edge_info allocation failed\n", __func__); + rc = -ENOMEM; + goto edge_info_alloc_fail; + } + + subsys_name = edge_name; + + key = "interrupts"; + irq_line = irq_of_parse_and_map(node, 0); + if (!irq_line) { + pr_err("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto missing_key; + } + + key = "qcom,irq-mask"; + rc = of_property_read_u32(node, key, &irq_mask); + if (rc) { + pr_err("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto missing_key; + } + + rc = of_address_to_resource(node, 1, &irq_r); + if (rc || !irq_r.start) { + pr_err("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto missing_key; + } + + rc = of_address_to_resource(node, 0, &msgram_r); + if (rc || !msgram_r.start) { + pr_err("%s: missing key %s\n", __func__, key); + rc = -ENODEV; + goto missing_key; + } + + init_xprt_cfg(einfo, subsys_name); + init_xprt_if(einfo); + spin_lock_init(&einfo->write_lock); + init_waitqueue_head(&einfo->tx_blocked_queue); + init_kthread_work(&einfo->kwork, rx_worker); + init_kthread_worker(&einfo->kworker); + einfo->intentless = true; + einfo->read_from_fifo = memcpy32_fromio; + einfo->write_to_fifo = memcpy32_toio; + init_srcu_struct(&einfo->use_ref); + spin_lock_init(&einfo->rx_lock); + INIT_LIST_HEAD(&einfo->deferred_cmds); + + einfo->out_irq_mask = irq_mask; + einfo->out_irq_reg = ioremap_nocache(irq_r.start, + resource_size(&irq_r)); + + if (!einfo->out_irq_reg) { + pr_err("%s: unable to map irq reg\n", __func__); + rc = -ENOMEM; + goto irq_ioremap_fail; + } + + msgram = ioremap_nocache(msgram_r.start, resource_size(&msgram_r)); + if (!msgram) { + pr_err("%s: unable to map msgram\n", __func__); + rc = -ENOMEM; + goto msgram_ioremap_fail; + } + + einfo->task = kthread_run(kthread_worker_fn, &einfo->kworker, + "smem_native_%s", subsys_name); + if (IS_ERR(einfo->task)) { + rc = PTR_ERR(einfo->task); + pr_err("%s: kthread_run failed %d\n", __func__, rc); + goto kthread_fail; + } + + memcpy32_fromio(toc, msgram + resource_size(&msgram_r) - RPM_TOC_SIZE, + RPM_TOC_SIZE); + tocp = (uint32_t *)toc; + if (*tocp != RPM_TOC_ID) { + rc = -ENODEV; + pr_err("%s: TOC id %d is not valid\n", __func__, *tocp); + goto toc_init_fail; + } + ++tocp; + num_toc_entries = *tocp; + if (num_toc_entries > RPM_MAX_TOC_ENTRIES) { + rc = -ENODEV; + pr_err("%s: %d is too many toc entries\n", __func__, + num_toc_entries); + goto toc_init_fail; + } + ++tocp; + + for (rc = 0; rc < num_toc_entries; ++rc) { + if (*tocp != RPM_TX_FIFO_ID) { + tocp += 3; + continue; + } + ++tocp; + einfo->tx_ch_desc = msgram + *tocp; + einfo->tx_fifo = einfo->tx_ch_desc + 1; + if ((uintptr_t)einfo->tx_fifo > + (uintptr_t)(msgram + resource_size(&msgram_r))) { + pr_err("%s: invalid tx fifo address\n", __func__); + einfo->tx_fifo = NULL; + break; + } + ++tocp; + einfo->tx_fifo_size = *tocp; + if (einfo->tx_fifo_size > resource_size(&msgram_r) || + (uintptr_t)(einfo->tx_fifo + einfo->tx_fifo_size) > + (uintptr_t)(msgram + resource_size(&msgram_r))) { + pr_err("%s: invalid tx fifo size\n", __func__); + einfo->tx_fifo = NULL; + break; + } + break; + } + if (!einfo->tx_fifo) { + rc = -ENODEV; + pr_err("%s: tx fifo not found\n", __func__); + goto toc_init_fail; + } + + tocp = (uint32_t *)toc; + tocp += 2; + for (rc = 0; rc < num_toc_entries; ++rc) { + if (*tocp != RPM_RX_FIFO_ID) { + tocp += 3; + continue; + } + ++tocp; + einfo->rx_ch_desc = msgram + *tocp; + einfo->rx_fifo = einfo->rx_ch_desc + 1; + if ((uintptr_t)einfo->rx_fifo > + (uintptr_t)(msgram + resource_size(&msgram_r))) { + pr_err("%s: invalid rx fifo address\n", __func__); + einfo->rx_fifo = NULL; + break; + } + ++tocp; + einfo->rx_fifo_size = *tocp; + if (einfo->rx_fifo_size > resource_size(&msgram_r) || + (uintptr_t)(einfo->rx_fifo + einfo->rx_fifo_size) > + (uintptr_t)(msgram + resource_size(&msgram_r))) { + pr_err("%s: invalid rx fifo size\n", __func__); + einfo->rx_fifo = NULL; + break; + } + break; + } + if (!einfo->rx_fifo) { + rc = -ENODEV; + pr_err("%s: rx fifo not found\n", __func__); + goto toc_init_fail; + } + + einfo->tx_ch_desc->write_index = 0; + einfo->rx_ch_desc->read_index = 0; + + rc = glink_core_register_transport(&einfo->xprt_if, &einfo->xprt_cfg); + if (rc == -EPROBE_DEFER) + goto reg_xprt_fail; + if (rc) { + pr_err("%s: glink core register transport failed: %d\n", + __func__, rc); + goto reg_xprt_fail; + } + + einfo->irq_line = irq_line; + rc = request_irq(irq_line, irq_handler, + IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND | IRQF_SHARED, + node->name, einfo); + if (rc < 0) { + pr_err("%s: request_irq on %d failed: %d\n", __func__, irq_line, + rc); + goto request_irq_fail; + } + rc = enable_irq_wake(irq_line); + if (rc < 0) + pr_err("%s: enable_irq_wake() failed on %d\n", __func__, + irq_line); + + register_debugfs_info(einfo); + einfo->xprt_if.glink_core_if_ptr->link_up(&einfo->xprt_if); + + /* scan through all the edges available channels */ + for_each_available_child_of_node(node, child_node) + qcom_ipc_create_device(child_node, edge_name); + return 0; + +request_irq_fail: + glink_core_unregister_transport(&einfo->xprt_if); +reg_xprt_fail: +toc_init_fail: + flush_kthread_worker(&einfo->kworker); + kthread_stop(einfo->task); + einfo->task = NULL; +kthread_fail: + iounmap(msgram); +msgram_ioremap_fail: + iounmap(einfo->out_irq_reg); +irq_ioremap_fail: +missing_key: + kfree(einfo); +edge_info_alloc_fail: + return rc; +} + +static int glink_native_probe(struct platform_device *pdev) +{ + struct device_node *node; + const char *edge_name, *key; + int ret; + + glink_dev = &pdev->dev; + + init_completion(&glink_ack); + + for_each_available_child_of_node(pdev->dev.of_node, node) { + key = "qcom,glink-edge"; + ret = of_property_read_string(node, key, &edge_name); + if (ret) { + dev_err(&pdev->dev, "edge missing %s property\n", key); + return -EINVAL; + } + + glink_edge_parse(node, edge_name); + } + + return 0; +} + +#if defined(CONFIG_DEBUG_FS) +/** + * debug_edge() - generates formatted text output displaying current edge state + * @s: File to send the output to. + */ +static void debug_edge(struct seq_file *s) +{ + struct edge_info *einfo; + struct glink_dbgfs_data *dfs_d; + + dfs_d = s->private; + einfo = dfs_d->priv_data; + +/* + * formatted, human readable edge state output, ie: + * TX/RX fifo information: +ID|EDGE |TX READ |TX WRITE |TX SIZE |RX READ |RX WRITE |RX SIZE +------------------------------------------------------------------------------- +01|mpss |0x00000128|0x00000128|0x00000800|0x00000256|0x00000256|0x00001000 + * + * Interrupt information: + * EDGE |TX INT |RX INT + * -------------------------------- + * mpss |0x00000006|0x00000008 + */ + seq_puts(s, "TX/RX fifo information:\n"); + seq_printf(s, "%2s|%-10s|%-10s|%-10s|%-10s|%-10s|%-10s|%-10s\n", + "ID", + "EDGE", + "TX READ", + "TX WRITE", + "TX SIZE", + "RX READ", + "RX WRITE", + "RX SIZE"); + seq_puts(s, + "-------------------------------------------------------------------------------\n"); + if (!einfo) + return; + + if (!einfo->rx_fifo) + seq_puts(s, "Link Not Up\n"); + else + seq_printf(s, "0x%08X|0x%08X|0x%08X|0x%08X|0x%08X|0x%08X\n", + einfo->tx_ch_desc->read_index, + einfo->tx_ch_desc->write_index, + einfo->tx_fifo_size, + einfo->rx_ch_desc->read_index, + einfo->rx_ch_desc->write_index, + einfo->rx_fifo_size); + + seq_puts(s, "\nInterrupt information:\n"); + seq_printf(s, "%-10s|%-10s|%-10s\n", "EDGE", "TX INT", "RX INT"); + seq_puts(s, "--------------------------------\n"); + seq_printf(s, "%-10s|0x%08X|0x%08X\n", einfo->xprt_cfg.edge, + einfo->tx_irq_count, + einfo->rx_irq_count); +} + +/** + * register_debugfs_info() - initialize debugfs device entries + * @einfo: Pointer to specific edge_info for which register is called. + */ +static void register_debugfs_info(struct edge_info *einfo) +{ + struct glink_dbgfs dfs; + char *curr_dir_name; + int dir_name_len; + + dir_name_len = strlen(einfo->xprt_cfg.edge) + + strlen(einfo->xprt_cfg.name) + 2; + curr_dir_name = kmalloc(dir_name_len, GFP_KERNEL); + if (!curr_dir_name) { + GLINK_ERR("%s: Memory allocation failed\n", __func__); + return; + } + + snprintf(curr_dir_name, dir_name_len, "%s_%s", + einfo->xprt_cfg.edge, einfo->xprt_cfg.name); + dfs.curr_name = curr_dir_name; + dfs.par_name = "xprt"; + dfs.b_dir_create = false; + glink_debugfs_create("XPRT_INFO", debug_edge, + &dfs, einfo, false); + kfree(curr_dir_name); +} + +#else +static void register_debugfs_info(struct edge_info *einfo) +{ +} +#endif /* CONFIG_DEBUG_FS */ + +static struct of_device_id glink_match_table[] = { + { .compatible = "qcom,glink" }, + {}, +}; + +static struct platform_driver glink_rpm_native_driver = { + .probe = glink_native_probe, + .driver = { + .name = "qcom_glink", + .owner = THIS_MODULE, + .of_match_table = glink_match_table, + }, +}; + +static const struct of_device_id glink_of_device_ids[] __initconst = { + { .compatible = "qcom,glink" }, + {} +}; + +static int __init glink_smem_native_xprt_init(void) +{ + int rc = 0; + struct device_node *np; + + np = of_find_matching_node(NULL, glink_of_device_ids); + if (np) { + rc = bus_register(&qcom_ipc_bus); + if (rc) { + pr_err("failed to register smd bus: %d\n", rc); + return rc; + } + ipc_bus = &qcom_ipc_bus; + + rc = platform_driver_register(&glink_rpm_native_driver); + if (rc) { + pr_err("%s: glink_rpm_native_driver register failed %d\n", + __func__, rc); + return rc; + } + } + + return 0; +} +postcore_initcall(glink_smem_native_xprt_init); + +/** + * qcom_smd_driver_register - register a smd driver + * @qidrv: qcom_smd_driver struct + */ +int qcom_glink_driver_register(void *drv) +{ + struct qcom_smd_driver *qidrv = drv; + + if (!ipc_bus) + return 0; + + qidrv->driver.bus = ipc_bus; + return driver_register(&qidrv->driver); +} +EXPORT_SYMBOL(qcom_glink_driver_register); + +/** + * qcom_smd_driver_register - register a smd driver + * @qidrv: qcom_smd_driver struct + */ +void qcom_glink_driver_unregister(void *drv) +{ + struct qcom_smd_driver *qidrv = drv; + + if (!ipc_bus) + return; + + driver_unregister(&qidrv->driver); +} +EXPORT_SYMBOL(qcom_glink_driver_unregister); +MODULE_DESCRIPTION("MSM G-Link SMEM Native Transport"); +MODULE_LICENSE("GPL v2"); diff --git a/drivers/soc/qcom/glink_xprt_if.h b/drivers/soc/qcom/glink_xprt_if.h new file mode 100644 index 000000000000..6242e867fe72 --- /dev/null +++ b/drivers/soc/qcom/glink_xprt_if.h @@ -0,0 +1,201 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _SOC_QCOM_GLINK_XPRT_IF_H_ +#define _SOC_QCOM_GLINK_XPRT_IF_H_ + +#include <linux/bitops.h> +#include <linux/list.h> +#include <linux/types.h> + +struct glink_core_xprt_ctx; +struct glink_core_if; +struct channel_ctx; +struct glink_core_rx_intent; + +enum buf_type { + LINEAR = 0, + VECTOR, +}; + +enum xprt_ids { + SMEM_XPRT_ID = 100, + SMD_TRANS_XPRT_ID = 200, + LLOOP_XPRT_ID = 300, + MOCK_XPRT_HIGH_ID = 390, + MOCK_XPRT_ID = 400, + MOCK_XPRT_LOW_ID = 410, +}; + +#define GCAP_SIGNALS BIT(0) +#define GCAP_INTENTLESS BIT(1) +#define GCAP_TRACER_PKT BIT(2) +#define GCAP_AUTO_QUEUE_RX_INT BIT(3) + +/** + * struct glink_core_tx_pkt - Transmit Packet information + * @list_done: Index to the channel's transmit queue. + * @list_done: Index to the channel's acknowledgment queue. + * @pkt_priv: Private information specific to the packet. + * @data: Pointer to the buffer containing the data. + * @riid: Remote receive intent used to transmit the packet. + * @rcid: Remote channel receiving the packet. + * @size: Total size of the data in the packet. + * @tx_len: Data length to transmit in the current transmit slot. + * @size_remaining: Remaining size of the data in the packet. + * @intent_size: Receive intent size queued by the remote side. + * @tracer_pkt: Flag to indicate if the packet is a tracer packet. + * @iovec: Pointer to the vector buffer packet. + * @vprovider: Packet-specific virtual buffer provider function. + * @pprovider: Packet-specific physical buffer provider function. + * @pkt_ref: Active references to the packet. + */ +struct glink_core_tx_pkt { + struct list_head list_node; + struct list_head list_done; + const void *pkt_priv; + const void *data; + uint32_t riid; + uint32_t rcid; + uint32_t size; + uint32_t tx_len; + uint32_t size_remaining; + size_t intent_size; + bool tracer_pkt; + void *iovec; + void * (*vprovider)(void *iovec, size_t offset, size_t *size); + void * (*pprovider)(void *iovec, size_t offset, size_t *size); + struct rwref_lock pkt_ref; +}; + +/** + * Note - each call to register the interface must pass a unique + * instance of this data. + */ +struct glink_transport_if { + /* Negotiation */ + void (*tx_cmd_version)(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features); + void (*tx_cmd_version_ack)(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features); + uint32_t (*set_version)(struct glink_transport_if *if_ptr, + uint32_t version, + uint32_t features); + + /* channel state */ + int (*tx_cmd_ch_open)(struct glink_transport_if *if_ptr, uint32_t lcid, + const char *name, uint16_t req_xprt); + int (*tx_cmd_ch_close)(struct glink_transport_if *if_ptr, + uint32_t lcid); + void (*tx_cmd_ch_remote_open_ack)(struct glink_transport_if *if_ptr, + uint32_t rcid, uint16_t xprt_resp); + void (*tx_cmd_ch_remote_close_ack)(struct glink_transport_if *if_ptr, + uint32_t rcid); + int (*ssr)(struct glink_transport_if *if_ptr); + + /* channel data */ + int (*allocate_rx_intent)(struct glink_transport_if *if_ptr, + size_t size, + struct glink_core_rx_intent *intent); + int (*deallocate_rx_intent)(struct glink_transport_if *if_ptr, + struct glink_core_rx_intent *intent); + /* Optional */ + int (*reuse_rx_intent)(struct glink_transport_if *if_ptr, + struct glink_core_rx_intent *intent); + + int (*tx_cmd_local_rx_intent)(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size, uint32_t liid); + void (*tx_cmd_local_rx_done)(struct glink_transport_if *if_ptr, + uint32_t lcid, uint32_t liid, bool reuse); + int (*tx)(struct glink_transport_if *if_ptr, uint32_t lcid, + struct glink_core_tx_pkt *pctx); + int (*tx_cmd_rx_intent_req)(struct glink_transport_if *if_ptr, + uint32_t lcid, size_t size); + int (*tx_cmd_remote_rx_intent_req_ack)( + struct glink_transport_if *if_ptr, + uint32_t lcid, bool granted); + int (*tx_cmd_set_sigs)(struct glink_transport_if *if_ptr, + uint32_t lcid, uint32_t sigs); + + /* Optional. If NULL at xprt registration, dummies will be used */ + int (*poll)(struct glink_transport_if *if_ptr, uint32_t lcid); + int (*mask_rx_irq)(struct glink_transport_if *if_ptr, uint32_t lcid, + bool mask, void *pstruct); + int (*wait_link_down)(struct glink_transport_if *if_ptr); + int (*tx_cmd_tracer_pkt)(struct glink_transport_if *if_ptr, + uint32_t lcid, struct glink_core_tx_pkt *pctx); + unsigned long (*get_power_vote_ramp_time)( + struct glink_transport_if *if_ptr, uint32_t state); + int (*power_vote)(struct glink_transport_if *if_ptr, uint32_t state); + int (*power_unvote)(struct glink_transport_if *if_ptr); + /* + * Keep data pointers at the end of the structure after all function + * pointer to allow for in-place initialization. + */ + + /* private pointer for core */ + struct glink_core_xprt_ctx *glink_core_priv; + + /* core pointer (set during transport registration) */ + struct glink_core_if *glink_core_if_ptr; +}; + +#ifdef CONFIG_MSM_GLINK + +/** + * get_tx_vaddr() - Get the virtual address from which the tx has to be done + * @pctx: transmit packet context. + * @offset: offset into the packet. + * @tx_size: pointer to hold the length of the contiguous buffer + * space. + * + * Return: Address from which the tx has to be done. + */ +static inline void *get_tx_vaddr(struct glink_core_tx_pkt *pctx, size_t offset, + size_t *tx_size) +{ + void *pdata; + + if (pctx->vprovider) { + return pctx->vprovider((void *)pctx->iovec, offset, tx_size); + } else if (pctx->pprovider) { + pdata = pctx->pprovider((void *)pctx->iovec, offset, tx_size); + return phys_to_virt((unsigned long)pdata); + } + return NULL; +} + +/** + * glink_xprt_name_to_id() - convert transport name to id + * @name: Name of the transport. + * @id: Assigned id. + * + * Return: 0 on success or standard Linux error code. + */ +int glink_xprt_name_to_id(const char *name, uint16_t *id); + + +#else /* CONFIG_MSM_GLINK */ +static inline void *get_tx_vaddr(struct glink_core_tx_pkt *pctx, size_t offset, + size_t *tx_size) +{ + return NULL; +} + +static inline int glink_xprt_name_to_id(const char *name, uint16_t *id) +{ + return -ENODEV; +} + +#endif /* CONFIG_MSM_GLINK */ +#endif /* _SOC_QCOM_GLINK_XPRT_IF_H_ */ diff --git a/drivers/soc/qcom/smd-rpm.c b/drivers/soc/qcom/smd-rpm.c index abb32001ff3a..5296e5189a1a 100644 --- a/drivers/soc/qcom/smd-rpm.c +++ b/drivers/soc/qcom/smd-rpm.c @@ -20,24 +20,29 @@ #include <linux/slab.h> #include <linux/soc/qcom/smd.h> +#include <soc/qcom/glink.h> #include <linux/soc/qcom/smd-rpm.h> #define RPM_REQUEST_TIMEOUT (5 * HZ) +#define SMD_RPM BIT(0) +#define GLINK_RPM BIT(1) + /** * struct qcom_smd_rpm - state of the rpm device driver - * @rpm_channel: reference to the smd channel + * @smd_channel: reference to the smd channel * @ack: completion for acks * @lock: mutual exclusion around the send/complete pair * @ack_status: result of the rpm request */ struct qcom_smd_rpm { - struct qcom_smd_channel *rpm_channel; struct device *dev; - + struct qcom_smd_channel *smd_channel; + void *glink_channel; struct completion ack; struct mutex lock; int ack_status; + u8 flag; }; /** @@ -133,7 +138,12 @@ int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm, pkt->req.data_len = cpu_to_le32(count); memcpy(pkt->payload, buf, count); - ret = qcom_smd_send(rpm->rpm_channel, pkt, size); + if (rpm->flag & SMD_RPM) { + ret = qcom_smd_send(rpm->smd_channel, pkt, size); + } else { + ret = glink_tx(rpm->glink_channel, pkt, pkt, size, + GLINK_TX_SINGLE_THREADED); + } if (ret) goto out; @@ -150,14 +160,13 @@ out: } EXPORT_SYMBOL(qcom_rpm_smd_write); -static int qcom_smd_rpm_callback(struct qcom_smd_channel *channel, +static int qcom_ipc_rpm_callback(struct qcom_smd_rpm *rpm, const void *data, size_t count) { const struct qcom_rpm_header *hdr = data; size_t hdr_length = le32_to_cpu(hdr->length); const struct qcom_rpm_message *msg; - struct qcom_smd_rpm *rpm = qcom_smd_get_drvdata(channel); const u8 *buf = data + sizeof(struct qcom_rpm_header); const u8 *end = buf + hdr_length; char msgbuf[32]; @@ -173,6 +182,7 @@ static int qcom_smd_rpm_callback(struct qcom_smd_channel *channel, while (buf < end) { msg = (struct qcom_rpm_message *)buf; msg_length = le32_to_cpu(msg->length); + switch (le32_to_cpu(msg->msg_type)) { case RPM_MSG_TYPE_MSG_ID: break; @@ -192,33 +202,57 @@ static int qcom_smd_rpm_callback(struct qcom_smd_channel *channel, } rpm->ack_status = status; + complete(&rpm->ack); return 0; } -static int qcom_smd_rpm_probe(struct qcom_smd_device *sdev) +static int qcom_smd_rpm_callback(void *dev, + const void *data, + size_t count) +{ + struct qcom_smd_rpm *rpm = qcom_smd_get_drvdata(dev); + return qcom_ipc_rpm_callback(rpm, data, count); +} + +static int qcom_glink_rpm_callback(void *dev, + const void *data, + size_t count) +{ + struct qcom_smd_rpm *rpm = qcom_glink_get_drvdata(dev); + return qcom_ipc_rpm_callback(rpm, data, count); +} + +static int qcom_ipc_rpm_probe(struct qcom_smd_device *idev) { struct qcom_smd_rpm *rpm; - rpm = devm_kzalloc(&sdev->dev, sizeof(*rpm), GFP_KERNEL); + rpm = devm_kzalloc(&idev->dev, sizeof(*rpm), GFP_KERNEL); if (!rpm) return -ENOMEM; + if (of_device_is_compatible(idev->dev.of_node, "qcom,rpm-msm8996")) { + rpm->flag |= GLINK_RPM; + rpm->glink_channel = idev->channel; + qcom_glink_set_drvdata(rpm->glink_channel, rpm); + } else { /* default behaviour */ + rpm->flag |= SMD_RPM; + rpm->smd_channel = idev->channel; + qcom_smd_set_drvdata(rpm->smd_channel, rpm); + } + mutex_init(&rpm->lock); init_completion(&rpm->ack); - rpm->dev = &sdev->dev; - rpm->rpm_channel = sdev->channel; - qcom_smd_set_drvdata(sdev->channel, rpm); + rpm->dev = &idev->dev; + dev_set_drvdata(&idev->dev, rpm); - dev_set_drvdata(&sdev->dev, rpm); - - return of_platform_populate(sdev->dev.of_node, NULL, NULL, &sdev->dev); + return of_platform_populate(idev->dev.of_node, NULL, NULL, &idev->dev); } -static void qcom_smd_rpm_remove(struct qcom_smd_device *sdev) +static void qcom_ipc_rpm_remove(struct qcom_smd_device *idev) { - of_platform_depopulate(&sdev->dev); + of_platform_depopulate(&idev->dev); } static const struct of_device_id qcom_smd_rpm_of_match[] = { @@ -230,29 +264,52 @@ static const struct of_device_id qcom_smd_rpm_of_match[] = { }; MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match); +static const struct of_device_id qcom_glink_rpm_of_match[] = { + { .compatible = "qcom,rpm-msm8996" }, + {} +}; + static struct qcom_smd_driver qcom_smd_rpm_driver = { - .probe = qcom_smd_rpm_probe, - .remove = qcom_smd_rpm_remove, + .probe = qcom_ipc_rpm_probe, + .remove = qcom_ipc_rpm_remove, .callback = qcom_smd_rpm_callback, .driver = { - .name = "qcom_smd_rpm", + .name = "qcom_ipc_rpm", .owner = THIS_MODULE, .of_match_table = qcom_smd_rpm_of_match, }, }; +static struct qcom_smd_driver qcom_glink_rpm_driver = { + .probe = qcom_ipc_rpm_probe, + .remove = qcom_ipc_rpm_remove, + .callback = qcom_glink_rpm_callback, + .driver = { + .name = "qcom_ipc_rpm", + .owner = THIS_MODULE, + .of_match_table = qcom_glink_rpm_of_match, + }, +}; + static int __init qcom_smd_rpm_init(void) { return qcom_smd_driver_register(&qcom_smd_rpm_driver); } -arch_initcall(qcom_smd_rpm_init); +subsys_initcall(qcom_smd_rpm_init); + +static int __init qcom_glink_rpm_init(void) +{ + return qcom_glink_driver_register(&qcom_glink_rpm_driver); +} +subsys_initcall(qcom_glink_rpm_init); -static void __exit qcom_smd_rpm_exit(void) +static void __exit qcom_ipc_rpm_exit(void) { qcom_smd_driver_unregister(&qcom_smd_rpm_driver); + qcom_glink_driver_unregister(&qcom_glink_rpm_driver); } -module_exit(qcom_smd_rpm_exit); +module_exit(qcom_ipc_rpm_exit); MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>"); -MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver"); +MODULE_DESCRIPTION("Qualcomm SMD/GLINK backed RPM driver"); MODULE_LICENSE("GPL v2"); diff --git a/drivers/soc/qcom/smd.c b/drivers/soc/qcom/smd.c index ac1957dfdf24..ba90e9512fc0 100644 --- a/drivers/soc/qcom/smd.c +++ b/drivers/soc/qcom/smd.c @@ -69,6 +69,8 @@ struct smd_channel_info_pair; struct smd_channel_info_word; struct smd_channel_info_word_pair; +static struct bus_type *ipc_bus; + #define SMD_ALLOC_TBL_COUNT 2 #define SMD_ALLOC_TBL_SIZE 64 @@ -171,7 +173,7 @@ enum smd_channel_state { struct qcom_smd_channel { struct qcom_smd_edge *edge; - struct qcom_smd_device *qsdev; + struct qcom_smd_device *qidev; char *name; enum smd_channel_state state; @@ -535,6 +537,7 @@ static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel) } ret = channel->cb(channel, ptr, len); + if (ret < 0) return ret; @@ -781,24 +784,24 @@ out: } EXPORT_SYMBOL(qcom_smd_send); -static struct qcom_smd_device *to_smd_device(struct device *dev) +static struct qcom_smd_device *to_ipc_device(struct device *dev) { return container_of(dev, struct qcom_smd_device, dev); } -static struct qcom_smd_driver *to_smd_driver(struct device *dev) +static struct qcom_smd_driver *to_ipc_driver(struct device *dev) { - struct qcom_smd_device *qsdev = to_smd_device(dev); + struct qcom_smd_device *qidev = to_ipc_device(dev); - return container_of(qsdev->dev.driver, struct qcom_smd_driver, driver); + return container_of(qidev->dev.driver, struct qcom_smd_driver, driver); } -static int qcom_smd_dev_match(struct device *dev, struct device_driver *drv) +static int qcom_ipc_dev_match(struct device *dev, struct device_driver *drv) { - struct qcom_smd_device *qsdev = to_smd_device(dev); - struct qcom_smd_driver *qsdrv = container_of(drv, struct qcom_smd_driver, driver); - const struct qcom_smd_id *match = qsdrv->smd_match_table; - const char *name = qsdev->channel->name; + struct qcom_smd_device *qidev = to_ipc_device(dev); + struct qcom_smd_driver *qidrv = container_of(drv, struct qcom_smd_driver, driver); + const struct qcom_smd_id *match = qidrv->smd_match_table; + const char *name = qidev->channel->name; if (match) { while (match->name[0]) { @@ -854,18 +857,18 @@ static void qcom_smd_channel_close(struct qcom_smd_channel *channel) * The remote side have indicated that it want the channel to be opened, so * complete the state handshake and probe our client driver. */ -static int qcom_smd_dev_probe(struct device *dev) +static int qcom_ipc_dev_probe(struct device *dev) { - struct qcom_smd_device *qsdev = to_smd_device(dev); - struct qcom_smd_driver *qsdrv = to_smd_driver(dev); - struct qcom_smd_channel *channel = qsdev->channel; + struct qcom_smd_device *qidev = to_ipc_device(dev); + struct qcom_smd_driver *qidrv = to_ipc_driver(dev); + struct qcom_smd_channel *channel = qidev->channel; int ret; - ret = qcom_smd_channel_open(channel, qsdrv->callback); + ret = qcom_smd_channel_open(channel, qidrv->callback); if (ret) return ret; - ret = qsdrv->probe(qsdev); + ret = qidrv->probe(qidev); if (ret) goto err; @@ -874,7 +877,7 @@ static int qcom_smd_dev_probe(struct device *dev) return 0; err: - dev_err(&qsdev->dev, "probe failed\n"); + dev_err(&qidev->dev, "probe failed\n"); qcom_smd_channel_close(channel); return ret; @@ -886,11 +889,11 @@ err: * The channel is going away, for some reason, so remove the smd client and * reset the channel state. */ -static int qcom_smd_dev_remove(struct device *dev) +static int qcom_ipc_dev_remove(struct device *dev) { - struct qcom_smd_device *qsdev = to_smd_device(dev); - struct qcom_smd_driver *qsdrv = to_smd_driver(dev); - struct qcom_smd_channel *channel = qsdev->channel; + struct qcom_smd_device *qidev = to_ipc_device(dev); + struct qcom_smd_driver *qidrv = to_ipc_driver(dev); + struct qcom_smd_channel *channel = qidev->channel; struct qcom_smd_channel *tmp; struct qcom_smd_channel *ch; @@ -908,8 +911,8 @@ static int qcom_smd_dev_remove(struct device *dev) * We expect that the client might block in remove() waiting for any * outstanding calls to qcom_smd_send() to wake up and finish. */ - if (qsdrv->remove) - qsdrv->remove(qsdev); + if (qidrv->remove) + qidrv->remove(qidev); /* * The client is now gone, close and release all channels associated @@ -918,27 +921,27 @@ static int qcom_smd_dev_remove(struct device *dev) list_for_each_entry_safe(ch, tmp, &channel->dev_list, dev_list) { qcom_smd_channel_close(ch); list_del(&ch->dev_list); - ch->qsdev = NULL; + ch->qidev = NULL; } return 0; } -static struct bus_type qcom_smd_bus = { +static struct bus_type qcom_ipc_bus = { .name = "qcom_smd", - .match = qcom_smd_dev_match, - .probe = qcom_smd_dev_probe, - .remove = qcom_smd_dev_remove, + .match = qcom_ipc_dev_match, + .probe = qcom_ipc_dev_probe, + .remove = qcom_ipc_dev_remove, }; /* * Release function for the qcom_smd_device object. */ -static void qcom_smd_release_device(struct device *dev) +static void qcom_ipc_release_device(struct device *dev) { - struct qcom_smd_device *qsdev = to_smd_device(dev); + struct qcom_smd_device *qidev = to_ipc_device(dev); - kfree(qsdev); + kfree(qidev); } /* @@ -968,41 +971,40 @@ static struct device_node *qcom_smd_match_channel(struct device_node *edge_node, /* * Create a smd client device for channel that is being opened. */ -static int qcom_smd_create_device(struct qcom_smd_channel *channel) +static int qcom_ipc_create_device(struct qcom_smd_channel *channel) { - struct qcom_smd_device *qsdev; + struct qcom_smd_device *qidev; struct qcom_smd_edge *edge = channel->edge; struct device_node *node; struct qcom_smd *smd = edge->smd; int ret; - if (channel->qsdev) + if (channel->qidev) return -EEXIST; dev_dbg(smd->dev, "registering '%s'\n", channel->name); - qsdev = kzalloc(sizeof(*qsdev), GFP_KERNEL); - if (!qsdev) + qidev = kzalloc(sizeof(*qidev), GFP_KERNEL); + if (!qidev) return -ENOMEM; node = qcom_smd_match_channel(edge->of_node, channel->name); - dev_set_name(&qsdev->dev, "%s.%s", + dev_set_name(&qidev->dev, "%s.%s", edge->of_node->name, node ? node->name : channel->name); - qsdev->dev.parent = smd->dev; - qsdev->dev.bus = &qcom_smd_bus; - qsdev->dev.release = qcom_smd_release_device; - qsdev->dev.of_node = node; - - qsdev->channel = channel; + qidev->dev.parent = smd->dev; + qidev->dev.bus = &qcom_ipc_bus; + qidev->dev.release = qcom_ipc_release_device; + qidev->dev.of_node = node; - channel->qsdev = qsdev; + qidev->channel = channel; + channel->qidev = qidev; - ret = device_register(&qsdev->dev); + ret = device_register(&qidev->dev); if (ret) { dev_err(smd->dev, "device_register failed: %d\n", ret); - put_device(&qsdev->dev); + put_device(&qidev->dev); } return ret; @@ -1011,13 +1013,13 @@ static int qcom_smd_create_device(struct qcom_smd_channel *channel) /* * Destroy a smd client device for a channel that's going away. */ -static void qcom_smd_destroy_device(struct qcom_smd_channel *channel) +static void qcom_ipc_destroy_device(struct qcom_smd_channel *channel) { struct device *dev; - BUG_ON(!channel->qsdev); + BUG_ON(!channel->qidev); - dev = &channel->qsdev->dev; + dev = &channel->qidev->dev; device_unregister(dev); of_node_put(dev->of_node); @@ -1026,34 +1028,42 @@ static void qcom_smd_destroy_device(struct qcom_smd_channel *channel) /** * qcom_smd_driver_register - register a smd driver - * @qsdrv: qcom_smd_driver struct + * @qidrv: qcom_smd_driver struct */ -int qcom_smd_driver_register(struct qcom_smd_driver *qsdrv) +int qcom_smd_driver_register(struct qcom_smd_driver *qidrv) { - qsdrv->driver.bus = &qcom_smd_bus; - return driver_register(&qsdrv->driver); + if (!ipc_bus) + return 0; + + qidrv->driver.bus = ipc_bus; + return driver_register(&qidrv->driver); } EXPORT_SYMBOL(qcom_smd_driver_register); -void *qcom_smd_get_drvdata(struct qcom_smd_channel *channel) +void *qcom_smd_get_drvdata(void *ch) { + struct qcom_smd_channel *channel = ch; return channel->drvdata; } EXPORT_SYMBOL(qcom_smd_get_drvdata); -void qcom_smd_set_drvdata(struct qcom_smd_channel *channel, void *data) +void qcom_smd_set_drvdata(void *ch, void *data) { + struct qcom_smd_channel *channel = ch; channel->drvdata = data; } EXPORT_SYMBOL(qcom_smd_set_drvdata); /** * qcom_smd_driver_unregister - unregister a smd driver - * @qsdrv: qcom_smd_driver struct + * @qidrv: qcom_smd_driver struct */ -void qcom_smd_driver_unregister(struct qcom_smd_driver *qsdrv) +void qcom_smd_driver_unregister(struct qcom_smd_driver *qidrv) { - driver_unregister(&qsdrv->driver); + if (!ipc_bus) + return; + + driver_unregister(&qidrv->driver); } EXPORT_SYMBOL(qcom_smd_driver_unregister); @@ -1097,7 +1107,7 @@ struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_channel *parent, qcom_smd_cb_t cb) { struct qcom_smd_channel *channel; - struct qcom_smd_device *sdev = parent->qsdev; + struct qcom_smd_device *sdev = parent->qidev; struct qcom_smd_edge *edge = parent->edge; int ret; @@ -1113,10 +1123,10 @@ struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_channel *parent, return ERR_PTR(-EBUSY); } - channel->qsdev = sdev; + channel->qidev = sdev; ret = qcom_smd_channel_open(channel, cb); if (ret) { - channel->qsdev = NULL; + channel->qidev = NULL; return ERR_PTR(ret); } @@ -1305,7 +1315,7 @@ static void qcom_channel_state_worker(struct work_struct *work) continue; spin_unlock_irqrestore(&edge->channels_lock, flags); - qcom_smd_create_device(channel); + qcom_ipc_create_device(channel); spin_lock_irqsave(&edge->channels_lock, flags); } @@ -1324,7 +1334,7 @@ static void qcom_channel_state_worker(struct work_struct *work) continue; spin_unlock_irqrestore(&edge->channels_lock, flags); - qcom_smd_destroy_device(channel); + qcom_ipc_destroy_device(channel); spin_lock_irqsave(&edge->channels_lock, flags); } spin_unlock_irqrestore(&edge->channels_lock, flags); @@ -1464,10 +1474,10 @@ static int qcom_smd_remove(struct platform_device *pdev) /* No need to lock here, because the writer is gone */ list_for_each_entry(channel, &edge->channels, list) { - if (!channel->qsdev) + if (!channel->qidev) continue; - qcom_smd_destroy_device(channel); + qcom_ipc_destroy_device(channel); } } @@ -1489,27 +1499,38 @@ static struct platform_driver qcom_smd_driver = { }, }; -static int __init qcom_smd_init(void) -{ - int ret; +static const struct of_device_id smd_of_device_ids[] __initconst = { + { .compatible = "qcom,smd" }, + {} +}; - ret = bus_register(&qcom_smd_bus); - if (ret) { - pr_err("failed to register smd bus: %d\n", ret); - return ret; +static int __init qcom_ipc_init(void) +{ + struct device_node *np; + int ret = 0; + + np = of_find_matching_node(NULL, smd_of_device_ids); + if (np) { + ret = bus_register(&qcom_ipc_bus); + if (ret) { + pr_err("failed to register smd bus: %d\n", ret); + return ret; + } + ipc_bus = &qcom_ipc_bus; + return platform_driver_register(&qcom_smd_driver); } - return platform_driver_register(&qcom_smd_driver); + return ret; } -postcore_initcall(qcom_smd_init); +postcore_initcall(qcom_ipc_init); -static void __exit qcom_smd_exit(void) +static void __exit qcom_ipc_exit(void) { platform_driver_unregister(&qcom_smd_driver); - bus_unregister(&qcom_smd_bus); + bus_unregister(&qcom_ipc_bus); } -module_exit(qcom_smd_exit); +module_exit(qcom_ipc_exit); MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>"); -MODULE_DESCRIPTION("Qualcomm Shared Memory Driver"); +MODULE_DESCRIPTION("Qualcomm SMD ipc Driver"); MODULE_LICENSE("GPL v2"); diff --git a/include/linux/ipc_logging.h b/include/linux/ipc_logging.h new file mode 100644 index 000000000000..780a82d2f1b4 --- /dev/null +++ b/include/linux/ipc_logging.h @@ -0,0 +1,290 @@ +/* Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _IPC_LOGGING_H +#define _IPC_LOGGING_H + +#include <linux/types.h> + +#define MAX_MSG_SIZE 255 + +enum { + TSV_TYPE_MSG_START = 1, + TSV_TYPE_SKB = TSV_TYPE_MSG_START, + TSV_TYPE_STRING, + TSV_TYPE_MSG_END = TSV_TYPE_STRING, +}; + +struct tsv_header { + unsigned char type; + unsigned char size; /* size of data field */ +}; + +struct encode_context { + struct tsv_header hdr; + char buff[MAX_MSG_SIZE]; + int offset; +}; + +struct decode_context { + int output_format; /* 0 = debugfs */ + char *buff; /* output buffer */ + int size; /* size of output buffer */ +}; + +#if defined(CONFIG_IPC_LOGGING) +/* + * ipc_log_context_create: Create a debug log context + * Should not be called from atomic context + * + * @max_num_pages: Number of pages of logging space required (max. 10) + * @mod_name : Name of the directory entry under DEBUGFS + * @user_version : Version number of user-defined message formats + * + * returns context id on success, NULL on failure + */ +void *ipc_log_context_create(int max_num_pages, const char *modname, + uint16_t user_version); + +/* + * msg_encode_start: Start encoding a log message + * + * @ectxt: Temporary storage to hold the encoded message + * @type: Root event type defined by the module which is logging + */ +void msg_encode_start(struct encode_context *ectxt, uint32_t type); + +/* + * tsv_timestamp_write: Writes the current timestamp count + * + * @ectxt: Context initialized by calling msg_encode_start() + */ +int tsv_timestamp_write(struct encode_context *ectxt); + +/* + * tsv_qtimer_write: Writes the current QTimer timestamp count + * + * @ectxt: Context initialized by calling msg_encode_start() + */ +int tsv_qtimer_write(struct encode_context *ectxt); + +/* + * tsv_pointer_write: Writes a data pointer + * + * @ectxt: Context initialized by calling msg_encode_start() + * @pointer: Pointer value to write + */ +int tsv_pointer_write(struct encode_context *ectxt, void *pointer); + +/* + * tsv_int32_write: Writes a 32-bit integer value + * + * @ectxt: Context initialized by calling msg_encode_start() + * @n: Integer to write + */ +int tsv_int32_write(struct encode_context *ectxt, int32_t n); + +/* + * tsv_int32_write: Writes a 32-bit integer value + * + * @ectxt: Context initialized by calling msg_encode_start() + * @n: Integer to write + */ +int tsv_byte_array_write(struct encode_context *ectxt, + void *data, int data_size); + +/* + * msg_encode_end: Complete the message encode process + * + * @ectxt: Temporary storage which holds the encoded message + */ +void msg_encode_end(struct encode_context *ectxt); + +/* + * msg_encode_end: Complete the message encode process + * + * @ectxt: Temporary storage which holds the encoded message + */ +void ipc_log_write(void *ctxt, struct encode_context *ectxt); + +/* + * ipc_log_string: Helper function to log a string + * + * @ilctxt: Debug Log Context created using ipc_log_context_create() + * @fmt: Data specified using format specifiers + */ +int ipc_log_string(void *ilctxt, const char *fmt, ...) __printf(2, 3); + +/** + * ipc_log_extract - Reads and deserializes log + * + * @ilctxt: logging context + * @buff: buffer to receive the data + * @size: size of the buffer + * @returns: 0 if no data read; >0 number of bytes read; < 0 error + * + * If no data is available to be read, then the ilctxt::read_avail + * completion is reinitialized. This allows clients to block + * until new log data is save. + */ +int ipc_log_extract(void *ilctxt, char *buff, int size); + +/* + * Print a string to decode context. + * @dctxt Decode context + * @args printf args + */ +#define IPC_SPRINTF_DECODE(dctxt, args...) \ +do { \ + int i; \ + i = scnprintf(dctxt->buff, dctxt->size, args); \ + dctxt->buff += i; \ + dctxt->size -= i; \ +} while (0) + +/* + * tsv_timestamp_read: Reads a timestamp + * + * @ectxt: Context retrieved by reading from log space + * @dctxt: Temporary storage to hold the decoded message + * @format: Output format while dumping through DEBUGFS + */ +void tsv_timestamp_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format); + +/* + * tsv_qtimer_read: Reads a QTimer timestamp + * + * @ectxt: Context retrieved by reading from log space + * @dctxt: Temporary storage to hold the decoded message + * @format: Output format while dumping through DEBUGFS + */ +void tsv_qtimer_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format); + +/* + * tsv_pointer_read: Reads a data pointer + * + * @ectxt: Context retrieved by reading from log space + * @dctxt: Temporary storage to hold the decoded message + * @format: Output format while dumping through DEBUGFS + */ +void tsv_pointer_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format); + +/* + * tsv_int32_read: Reads a 32-bit integer value + * + * @ectxt: Context retrieved by reading from log space + * @dctxt: Temporary storage to hold the decoded message + * @format: Output format while dumping through DEBUGFS + */ +int32_t tsv_int32_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format); + +/* + * tsv_int32_read: Reads a 32-bit integer value + * + * @ectxt: Context retrieved by reading from log space + * @dctxt: Temporary storage to hold the decoded message + * @format: Output format while dumping through DEBUGFS + */ +void tsv_byte_array_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format); + +/* + * add_deserialization_func: Register a deserialization function to + * to unpack the subevents of a main event + * + * @ctxt: Debug log context to which the deserialization function has + * to be registered + * @type: Main/Root event, defined by the module which is logging, to + * which this deserialization function has to be registered. + * @dfune: Deserialization function to be registered + * + * return 0 on success, -ve value on FAILURE + */ +int add_deserialization_func(void *ctxt, int type, + void (*dfunc)(struct encode_context *, + struct decode_context *)); + +/* + * ipc_log_context_destroy: Destroy debug log context + * + * @ctxt: debug log context created by calling ipc_log_context_create API. + */ +int ipc_log_context_destroy(void *ctxt); + +#else + +static inline void *ipc_log_context_create(int max_num_pages, + const char *modname, uint16_t user_version) +{ return NULL; } + +static inline void msg_encode_start(struct encode_context *ectxt, + uint32_t type) { } + +static inline int tsv_timestamp_write(struct encode_context *ectxt) +{ return -EINVAL; } + +static inline int tsv_qtimer_write(struct encode_context *ectxt) +{ return -EINVAL; } + +static inline int tsv_pointer_write(struct encode_context *ectxt, void *pointer) +{ return -EINVAL; } + +static inline int tsv_int32_write(struct encode_context *ectxt, int32_t n) +{ return -EINVAL; } + +static inline int tsv_byte_array_write(struct encode_context *ectxt, + void *data, int data_size) +{ return -EINVAL; } + +static inline void msg_encode_end(struct encode_context *ectxt) { } + +static inline void ipc_log_write(void *ctxt, struct encode_context *ectxt) { } + +static inline int ipc_log_string(void *ilctxt, const char *fmt, ...) +{ return -EINVAL; } + +static inline int ipc_log_extract(void *ilctxt, char *buff, int size) +{ return -EINVAL; } + +#define IPC_SPRINTF_DECODE(dctxt, args...) do { } while (0) + +static inline void tsv_timestamp_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format) { } + +static inline void tsv_qtimer_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format) { } + +static inline void tsv_pointer_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format) { } + +static inline int32_t tsv_int32_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format) +{ return 0; } + +static inline void tsv_byte_array_read(struct encode_context *ectxt, + struct decode_context *dctxt, const char *format) { } + +static inline int add_deserialization_func(void *ctxt, int type, + void (*dfunc)(struct encode_context *, + struct decode_context *)) +{ return 0; } + +static inline int ipc_log_context_destroy(void *ctxt) +{ return 0; } + +#endif + +#endif diff --git a/include/linux/soc/qcom/smd.h b/include/linux/soc/qcom/smd.h index cbb0f06c41b2..26182e85633a 100644 --- a/include/linux/soc/qcom/smd.h +++ b/include/linux/soc/qcom/smd.h @@ -7,6 +7,7 @@ struct qcom_smd; struct qcom_smd_channel; struct qcom_smd_lookup; +struct qcom_smd_device; /** * struct qcom_smd_id - struct used for matching a smd device @@ -26,7 +27,7 @@ struct qcom_smd_device { struct qcom_smd_channel *channel; }; -typedef int (*qcom_smd_cb_t)(struct qcom_smd_channel *, const void *, size_t); +typedef int (*qcom_smd_cb_t)(void *, const void *, size_t); /** * struct qcom_smd_driver - smd driver struct @@ -51,12 +52,13 @@ struct qcom_smd_driver { int qcom_smd_driver_register(struct qcom_smd_driver *drv); void qcom_smd_driver_unregister(struct qcom_smd_driver *drv); +void qcom_ipc_bus_register(struct bus_type *bus); struct qcom_smd_channel *qcom_smd_open_channel(struct qcom_smd_channel *channel, const char *name, qcom_smd_cb_t cb); -void *qcom_smd_get_drvdata(struct qcom_smd_channel *channel); -void qcom_smd_set_drvdata(struct qcom_smd_channel *channel, void *data); +void *qcom_smd_get_drvdata(void *ch); +void qcom_smd_set_drvdata(void *ch, void *data); int qcom_smd_send(struct qcom_smd_channel *channel, const void *data, int len); @@ -106,8 +108,8 @@ static inline int qcom_smd_send(struct qcom_smd_channel *channel, #endif -#define module_qcom_smd_driver(__smd_driver) \ - module_driver(__smd_driver, qcom_smd_driver_register, \ +#define module_qcom_smd_driver(__ipc_driver) \ + module_driver(__ipc_driver, qcom_smd_driver_register, \ qcom_smd_driver_unregister) diff --git a/include/soc/qcom/glink.h b/include/soc/qcom/glink.h new file mode 100644 index 000000000000..9c20e44972e2 --- /dev/null +++ b/include/soc/qcom/glink.h @@ -0,0 +1,443 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _SOC_QCOM_GLINK_H_ +#define _SOC_QCOM_GLINK_H_ + +#include <linux/types.h> + +/* Maximum size (including null) for channel, edge, or transport names */ +#define GLINK_NAME_SIZE 32 + +/* Maximum packet size for TX and RX */ +#define GLINK_MAX_PKT_SIZE SZ_1M + +/** + * G-Link Port State Notification Values + */ +enum { + GLINK_CONNECTED, + GLINK_LOCAL_DISCONNECTED, + GLINK_REMOTE_DISCONNECTED, +}; + +/** + * G-Link Open Options + * + * Used to define the glink_open_config::options field which is passed into + * glink_open(). + */ +enum { + GLINK_OPT_INITIAL_XPORT = BIT(0), + GLINK_OPT_RX_INTENT_NOTIF = BIT(1), +}; + +/** + * Open configuration. + * + * priv: Private data passed into user callbacks + * options: Open option flags + * rx_intent_req_timeout_ms: Timeout for requesting an RX intent, in + * milliseconds; if set to 0, timeout is infinite + * notify_rx: Receive notification function (required) + * notify_tx_done: Transmit-done notification function (required) + * notify_state: State-change notification (required) + * notify_rx_intent_req: Receive intent request (optional) + * notify_rxv: Receive notification function for vector buffers + * (required if notify_rx is not provided) + * notify_sig: Signal-change notification (optional) + * notify_rx_tracer_pkt: Receive notification for tracer packet + * notify_remote_rx_intent: Receive notification for remote-queued RX intent + * + * This structure is passed into the glink_open() call to setup + * configuration handles. All unused fields should be set to 0. + * + * The structure is copied internally before the call to glink_open() returns. + */ +struct glink_open_config { + void *priv; + uint32_t options; + + const char *transport; + const char *edge; + const char *name; + unsigned int rx_intent_req_timeout_ms; + + int (*notify_rx)(void *handle, const void *ptr, size_t size); + void (*notify_tx_done)(void *handle, const void *priv, + const void *pkt_priv, const void *ptr); + void (*notify_state)(void *handle, const void *priv, unsigned event); + bool (*notify_rx_intent_req)(void *handle, const void *priv, + size_t req_size); + void (*notify_rxv)(void *handle, const void *priv, const void *pkt_priv, + void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, + size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, + size_t *size)); + void (*notify_rx_sigs)(void *handle, const void *priv, + uint32_t old_sigs, uint32_t new_sigs); + void (*notify_rx_abort)(void *handle, const void *priv, + const void *pkt_priv); + void (*notify_tx_abort)(void *handle, const void *priv, + const void *pkt_priv); + void (*notify_rx_tracer_pkt)(void *handle, const void *priv, + const void *pkt_priv, const void *ptr, size_t size); + void (*notify_remote_rx_intent)(void *handle, const void *priv, + size_t size); +}; + +enum glink_link_state { + GLINK_LINK_STATE_UP, + GLINK_LINK_STATE_DOWN, +}; + +/** + * Data structure containing information during Link State callback + * transport: String identifying the transport. + * edge: String identifying the edge. + * link_state: Link state(UP?DOWN). + */ +struct glink_link_state_cb_info { + const char *transport; + const char *edge; + enum glink_link_state link_state; +}; + +/** + * Data structure containing information for link state registration + * transport: String identifying the transport. + * edge: String identifying the edge. + * glink_link_state_notif_cb: Callback function used to pass the event. + */ +struct glink_link_info { + const char *transport; + const char *edge; + void (*glink_link_state_notif_cb)( + struct glink_link_state_cb_info *cb_info, + void *priv); +}; + +enum tx_flags { + GLINK_TX_REQ_INTENT = 0x1, + GLINK_TX_SINGLE_THREADED = 0x2, + GLINK_TX_TRACER_PKT = 0x4, + GLINK_TX_ATOMIC = 0x8, +}; + +#ifdef CONFIG_MSM_GLINK +/** + * Open GLINK channel. + * + * @cfg_ptr: Open configuration structure (the structure is copied before + * glink_open returns). All unused fields should be zero-filled. + * + * This should not be called from link state callback context by clients. + * It is recommended that client should invoke this function from their own + * thread. + * + * Return: Pointer to channel on success, PTR_ERR() with standard Linux + * error code on failure. + */ +void *glink_open(const struct glink_open_config *cfg_ptr); + +/** + * glink_close() - Close a previously opened channel. + * + * @handle: handle to close + * + * Once the closing process has been completed, the GLINK_LOCAL_DISCONNECTED + * state event will be sent and the channel can be reopened. + * + * Return: 0 on success; -EINVAL for invalid handle, -EBUSY is close is + * already in progress, standard Linux Error code otherwise. + */ +int glink_close(void *handle); + +/** + * glink_tx() - Transmit packet. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data value that will be returned to client with + * notify_tx_done notification + * @data: pointer to the data + * @size: size of data + * @tx_flags: Flags to specify transmit specific options + * + * Return: -EINVAL for invalid handle; -EBUSY if channel isn't ready for + * transmit operation (not fully opened); -EAGAIN if remote side + * has not provided a receive intent that is big enough. + */ +int glink_tx(void *handle, void *pkt_priv, void *data, size_t size, + uint32_t tx_flags); + +/** + * glink_queue_rx_intent() - Register an intent to receive data. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data type that is returned when a packet is received + * size: maximum size of data to receive + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_queue_rx_intent(void *handle, const void *pkt_priv, size_t size); + +/** + * glink_rx_intent_exists() - Check if an intent of size exists. + * + * @handle: handle returned by glink_open() + * @size: size of an intent to check or 0 for any intent + * + * Return: TRUE if an intent exists with greater than or equal to the size + * else FALSE + */ +bool glink_rx_intent_exists(void *handle, size_t size); + +/** + * glink_rx_done() - Return receive buffer to remote side. + * + * @handle: handle returned by glink_open() + * @ptr: data pointer provided in the notify_rx() call + * @reuse: if true, receive intent is re-used + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_rx_done(void *handle, const void *ptr, bool reuse); + +/** + * glink_txv() - Transmit a packet in vector form. + * + * @handle: handle returned by glink_open() + * @pkt_priv: opaque data value that will be returned to client with + * notify_tx_done notification + * @iovec: pointer to the vector (must remain valid until notify_tx_done + * notification) + * @size: size of data/vector + * @vbuf_provider: Client provided helper function to iterate the vector + * in physical address space + * @pbuf_provider: Client provided helper function to iterate the vector + * in virtual address space + * @tx_flags: Flags to specify transmit specific options + * + * Return: -EINVAL for invalid handle; -EBUSY if channel isn't ready for + * transmit operation (not fully opened); -EAGAIN if remote side has + * not provided a receive intent that is big enough. + */ +int glink_txv(void *handle, void *pkt_priv, + void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, size_t *size), + uint32_t tx_flags); + +/** + * glink_sigs_set() - Set the local signals for the GLINK channel + * + * @handle: handle returned by glink_open() + * @sigs: modified signal value + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_set(void *handle, uint32_t sigs); + +/** + * glink_sigs_local_get() - Get the local signals for the GLINK channel + * + * handle: handle returned by glink_open() + * sigs: Pointer to hold the signals + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_local_get(void *handle, uint32_t *sigs); + +/** + * glink_sigs_remote_get() - Get the Remote signals for the GLINK channel + * + * handle: handle returned by glink_open() + * sigs: Pointer to hold the signals + * + * Return: 0 for success; standard Linux error code for failure case + */ +int glink_sigs_remote_get(void *handle, uint32_t *sigs); + +/** + * glink_register_link_state_cb() - Register for link state notification + * @link_info: Data structure containing the link identification and callback. + * @priv: Private information to be passed with the callback. + * + * This function is used to register a notifier to receive the updates about a + * link's/transport's state. This notifier needs to be registered first before + * an attempt to open a channel. + * + * Return: a reference to the notifier handle. + */ +void *glink_register_link_state_cb(struct glink_link_info *link_info, + void *priv); + +/** + * glink_unregister_link_state_cb() - Unregister the link state notification + * notif_handle: Handle to be unregistered. + * + * This function is used to unregister a notifier to stop receiving the updates + * about a link's/transport's state. + */ +void glink_unregister_link_state_cb(void *notif_handle); + +/** + * glink_qos_latency() - Register the latency QoS requirement + * @handle: Channel handle in which the latency is required. + * @latency_us: Latency requirement in units of micro-seconds. + * @pkt_size: Worst case packet size for which the latency is required. + * + * This function is used to register the latency requirement for a channel + * and ensures that the latency requirement for this channel is met without + * impacting the existing latency requirements of other channels. + * + * Return: 0 if QoS request is achievable, standard Linux error codes on error + */ +int glink_qos_latency(void *handle, unsigned long latency_us, size_t pkt_size); + +/** + * glink_qos_cancel() - Cancel or unregister the QoS request + * @handle: Channel handle for which the QoS request is cancelled. + * + * This function is used to cancel/unregister the QoS requests for a channel. + * + * Return: 0 on success, standard Linux error codes on failure + */ +int glink_qos_cancel(void *handle); + +/** + * glink_qos_start() - Start of the transmission requiring QoS + * @handle: Channel handle in which the transmit activity is performed. + * + * This function is called by the clients to indicate G-Link regarding the + * start of the transmission which requires a certain QoS. The clients + * must account for the QoS ramp time to ensure meeting the QoS. + * + * Return: 0 on success, standard Linux error codes on failure + */ +int glink_qos_start(void *handle); + +/** + * glink_qos_get_ramp_time() - Get the QoS ramp time + * @handle: Channel handle for which the QoS ramp time is required. + * @pkt_size: Worst case packet size. + * + * This function is called by the clients to obtain the ramp time required + * to meet the QoS requirements. + * + * Return: QoS ramp time is returned in units of micro-seconds + */ +unsigned long glink_qos_get_ramp_time(void *handle, size_t pkt_size); + +void *qcom_glink_get_drvdata(void *ch); +void qcom_glink_set_drvdata(void *ch, void *data); +int qcom_glink_driver_register(void *drv); +void qcom_glink_driver_unregister(void *drv); +#else /* CONFIG_MSM_GLINK */ +static inline void *glink_open(const struct glink_open_config *cfg_ptr) +{ + return NULL; +} + +static inline int glink_close(void *handle) +{ + return -ENODEV; +} + +static inline int glink_tx(void *handle, void *pkt_priv, void *data, + size_t size, uint32_t tx_flags) +{ + return -ENODEV; +} + +static inline int glink_queue_rx_intent(void *handle, const void *pkt_priv, + size_t size) +{ + return -ENODEV; +} + +static inline bool glink_rx_intent_exists(void *handle, size_t size) +{ + return -ENODEV; +} + +static inline int glink_rx_done(void *handle, const void *ptr, bool reuse) +{ + return -ENODEV; +} + +static inline int glink_txv(void *handle, void *pkt_priv, + void *iovec, size_t size, + void * (*vbuf_provider)(void *iovec, size_t offset, size_t *size), + void * (*pbuf_provider)(void *iovec, size_t offset, size_t *size), + uint32_t tx_flags) +{ + return -ENODEV; +} + +static inline int glink_sigs_set(void *handle, uint32_t sigs) +{ + return -ENODEV; +} + +static inline int glink_sigs_local_get(void *handle, uint32_t *sigs) +{ + return -ENODEV; +} + +static inline int glink_sigs_remote_get(void *handle, uint32_t *sigs) +{ + return -ENODEV; +} + +static inline void *glink_register_link_state_cb( + struct glink_link_info *link_info, void *priv) +{ + return NULL; +} + +static inline void glink_unregister_link_state_cb(void *notif_handle) +{ +} + +static inline int glink_qos_latency(void *handle, unsigned long latency_us, + size_t pkt_size) +{ + return -ENODEV; +} + +static inline int glink_qos_cancel(void *handle) +{ + return -ENODEV; +} + +static inline int glink_qos_start(void *handle) +{ + return -ENODEV; +} + +static inline unsigned long glink_qos_get_ramp_time(void *handle, + size_t pkt_size) +{ + return 0; +} + +void *qcom_glink_get_drvdata(void *ch) +{ + return NULL; +} + +void qcom_glink_set_drvdata(void *ch, void *data) {} +int qcom_glink_driver_register(void *drv) {} +void qcom_glink_driver_unregister(void *drv) {} +#endif /* CONFIG_MSM_GLINK */ +#endif /* _SOC_QCOM_GLINK_H_ */ diff --git a/include/soc/qcom/glink_rpm_xprt.h b/include/soc/qcom/glink_rpm_xprt.h new file mode 100644 index 000000000000..8dfd43783e57 --- /dev/null +++ b/include/soc/qcom/glink_rpm_xprt.h @@ -0,0 +1,78 @@ +/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _SOC_QCOM_GLINK_RPM_XPRT_H_ +#define _SOC_QCOM_GLINK_RPM_XPRT_H_ + +#include <linux/types.h> + +#ifdef CONFIG_MSM_GLINK + +/** + * glink_rpm_rx_poll() - Poll and receive any available events + * @handle: Channel handle in which this operation is performed. + * + * This function is used to poll and receive events and packets while the + * receive interrupt from RPM is disabled. + * + * Note that even if a return value > 0 is returned indicating that some events + * were processed, clients should only use the notification functions passed + * into glink_open() to determine if an entire packet has been received since + * some events may be internal details that are not visible to clients. + * + * Return: 0 for no packets available; > 0 for events available; standard + * Linux error codes on failure. + */ +int glink_rpm_rx_poll(void *handle); + +/** + * glink_rpm_mask_rx_interrupt() - Mask or unmask the RPM receive interrupt + * @handle: Channel handle in which this operation is performed. + * @mask: Flag to mask or unmask the interrupt. + * @pstruct: Pointer to any platform specific data. + * + * This function is used to mask or unmask the receive interrupt from RPM. + * "mask" set to true indicates masking the interrupt and when set to false + * indicates unmasking the interrupt. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int glink_rpm_mask_rx_interrupt(void *handle, bool mask, void *pstruct); + +/** + * glink_wait_link_down() - Return whether read/write indices in FIFO are all 0. + * @handle: Channel handle in which this operation is performed. + * + * This function returns the status of the read/write indices in the FIFO. + * + * Return: 1 if the indices are all 0, 0 otherwise. + */ +int glink_wait_link_down(void *handle); + +#else +static inline int glink_rpm_rx_poll(void *handle) +{ + return -ENODEV; +} + +static inline int glink_rpm_mask_rx_interrupt(void *handle, bool mask, + void *pstruct) +{ + return -ENODEV; +} +static inline int glink_wait_link_down(void *handle) +{ + return -ENODEV; +} + +#endif /* CONFIG_MSM_GLINK */ + +#endif /* _SOC_QCOM_GLINK_RPM_XPRT_H_ */ diff --git a/include/soc/qcom/rpm-notifier.h b/include/soc/qcom/rpm-notifier.h new file mode 100644 index 000000000000..ea6d95e313a8 --- /dev/null +++ b/include/soc/qcom/rpm-notifier.h @@ -0,0 +1,63 @@ +/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ +#ifndef __ARCH_ARM_MACH_MSM_RPM_NOTIF_H +#define __ARCH_ARM_MACH_MSM_RPM_NOTIF_H + +struct msm_rpm_notifier_data { + uint32_t rsc_type; + uint32_t rsc_id; + uint32_t key; + uint32_t size; + uint8_t *value; +}; +/** + * msm_rpm_register_notifier - Register for sleep set notifications + * + * @nb - notifier block to register + * + * return 0 on success, errno on failure. + */ +int msm_rpm_register_notifier(struct notifier_block *nb); + +/** + * msm_rpm_unregister_notifier - Unregister previously registered notifications + * + * @nb - notifier block to unregister + * + * return 0 on success, errno on failure. + */ +int msm_rpm_unregister_notifier(struct notifier_block *nb); + +/** + * msm_rpm_enter_sleep - Notify RPM driver to prepare for entering sleep + * + * @bool - flag to enable print contents of sleep buffer. + * @cpumask - cpumask of next wakeup cpu + * + * return 0 on success errno on failure. + */ +int msm_rpm_enter_sleep(bool print, const struct cpumask *cpumask); + +/** + * msm_rpm_exit_sleep - Notify RPM driver about resuming from power collapse + */ +void msm_rpm_exit_sleep(void); + +/** + * msm_rpm_waiting_for_ack - Indicate if there is RPM message + * pending acknowledgement. + * returns true for pending messages and false otherwise + */ +bool msm_rpm_waiting_for_ack(void); + +#endif /*__ARCH_ARM_MACH_MSM_RPM_NOTIF_H */ diff --git a/include/soc/qcom/rpm-smd.h b/include/soc/qcom/rpm-smd.h new file mode 100644 index 000000000000..020ffe308284 --- /dev/null +++ b/include/soc/qcom/rpm-smd.h @@ -0,0 +1,309 @@ +/* Copyright (c) 2012, 2014-2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#ifndef __ARCH_ARM_MACH_MSM_RPM_SMD_H +#define __ARCH_ARM_MACH_MSM_RPM_SMD_H + +/** + * enum msm_rpm_set - RPM enumerations for sleep/active set + * %MSM_RPM_CTX_SET_0: Set resource parameters for active mode. + * %MSM_RPM_CTX_SET_SLEEP: Set resource parameters for sleep. + */ +enum msm_rpm_set { + MSM_RPM_CTX_ACTIVE_SET, + MSM_RPM_CTX_SLEEP_SET, +}; + +struct msm_rpm_request; + +struct msm_rpm_kvp { + uint32_t key; + uint32_t length; + uint8_t *data; +}; +#ifdef CONFIG_MSM_RPM_SMD +/** + * msm_rpm_request() - Creates a parent element to identify the + * resource on the RPM, that stores the KVPs for different fields modified + * for a hardware resource + * + * @set: if the device is setting the active/sleep set parameter + * for the resource + * @rsc_type: unsigned 32 bit integer that identifies the type of the resource + * @rsc_id: unsigned 32 bit that uniquely identifies a resource within a type + * @num_elements: number of KVPs pairs associated with the resource + * + * returns pointer to a msm_rpm_request on success, NULL on error + */ +struct msm_rpm_request *msm_rpm_create_request( + enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, int num_elements); + +/** + * msm_rpm_request_noirq() - Creates a parent element to identify the + * resource on the RPM, that stores the KVPs for different fields modified + * for a hardware resource. This function is similar to msm_rpm_create_request + * except that it has to be called with interrupts masked. + * + * @set: if the device is setting the active/sleep set parameter + * for the resource + * @rsc_type: unsigned 32 bit integer that identifies the type of the resource + * @rsc_id: unsigned 32 bit that uniquely identifies a resource within a type + * @num_elements: number of KVPs pairs associated with the resource + * + * returns pointer to a msm_rpm_request on success, NULL on error + */ +struct msm_rpm_request *msm_rpm_create_request_noirq( + enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, int num_elements); + +/** + * msm_rpm_add_kvp_data() - Adds a Key value pair to a existing RPM resource. + * + * @handle: RPM resource handle to which the data should be appended + * @key: unsigned integer identify the parameter modified + * @data: byte array that contains the value corresponding to key. + * @size: size of data in bytes. + * + * returns 0 on success or errno + */ +int msm_rpm_add_kvp_data(struct msm_rpm_request *handle, + uint32_t key, const uint8_t *data, int size); + +/** + * msm_rpm_add_kvp_data_noirq() - Adds a Key value pair to a existing RPM + * resource. This function is similar to msm_rpm_add_kvp_data except that it + * has to be called with interrupts masked. + * + * @handle: RPM resource handle to which the data should be appended + * @key: unsigned integer identify the parameter modified + * @data: byte array that contains the value corresponding to key. + * @size: size of data in bytes. + * + * returns 0 on success or errno + */ +int msm_rpm_add_kvp_data_noirq(struct msm_rpm_request *handle, + uint32_t key, const uint8_t *data, int size); + +/** msm_rpm_free_request() - clean up the RPM request handle created with + * msm_rpm_create_request + * + * @handle: RPM resource handle to be cleared. + */ + +void msm_rpm_free_request(struct msm_rpm_request *handle); + +/** + * msm_rpm_send_request() - Send the RPM messages using SMD. The function + * assigns a message id before sending the data out to the RPM. RPM hardware + * uses the message id to acknowledge the messages. + * + * @handle: pointer to the msm_rpm_request for the resource being modified. + * + * returns non-zero message id on success and zero on a failed transaction. + * The drivers use message id to wait for ACK from RPM. + */ +int msm_rpm_send_request(struct msm_rpm_request *handle); + +/** + * msm_rpm_send_request_noack() - Send the RPM messages using SMD. The function + * assigns a message id before sending the data out to the RPM. RPM hardware + * uses the message id to acknowledge the messages, but this API does not wait + * on the ACK for this message id and it does not add the message id to the wait + * list. + * + * @handle: pointer to the msm_rpm_request for the resource being modified. + * + * returns NULL on success and PTR_ERR on a failed transaction. + */ +void *msm_rpm_send_request_noack(struct msm_rpm_request *handle); + +/** + * msm_rpm_send_request_noirq() - Send the RPM messages using SMD. The + * function assigns a message id before sending the data out to the RPM. + * RPM hardware uses the message id to acknowledge the messages. This function + * is similar to msm_rpm_send_request except that it has to be called with + * interrupts masked. + * + * @handle: pointer to the msm_rpm_request for the resource being modified. + * + * returns non-zero message id on success and zero on a failed transaction. + * The drivers use message id to wait for ACK from RPM. + */ +int msm_rpm_send_request_noirq(struct msm_rpm_request *handle); + +/** + * msm_rpm_wait_for_ack() - A blocking call that waits for acknowledgment of + * a message from RPM. + * + * @msg_id: the return from msm_rpm_send_requests + * + * returns 0 on success or errno + */ +int msm_rpm_wait_for_ack(uint32_t msg_id); + +/** + * msm_rpm_wait_for_ack_noirq() - A blocking call that waits for acknowledgment + * of a message from RPM. This function is similar to msm_rpm_wait_for_ack + * except that it has to be called with interrupts masked. + * + * @msg_id: the return from msm_rpm_send_request + * + * returns 0 on success or errno + */ +int msm_rpm_wait_for_ack_noirq(uint32_t msg_id); + +/** + * msm_rpm_send_message() -Wrapper function for clients to send data given an + * array of key value pairs. + * + * @set: if the device is setting the active/sleep set parameter + * for the resource + * @rsc_type: unsigned 32 bit integer that identifies the type of the resource + * @rsc_id: unsigned 32 bit that uniquely identifies a resource within a type + * @kvp: array of KVP data. + * @nelem: number of KVPs pairs associated with the message. + * + * returns 0 on success and errno on failure. + */ +int msm_rpm_send_message(enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems); + +/** + * msm_rpm_send_message_noack() -Wrapper function for clients to send data + * given an array of key value pairs without waiting for ack. + * + * @set: if the device is setting the active/sleep set parameter + * for the resource + * @rsc_type: unsigned 32 bit integer that identifies the type of the resource + * @rsc_id: unsigned 32 bit that uniquely identifies a resource within a type + * @kvp: array of KVP data. + * @nelem: number of KVPs pairs associated with the message. + * + * returns NULL on success and PTR_ERR(errno) on failure. + */ +void *msm_rpm_send_message_noack(enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems); + +/** + * msm_rpm_send_message_noirq() -Wrapper function for clients to send data + * given an array of key value pairs. This function is similar to the + * msm_rpm_send_message() except that it has to be called with interrupts + * disabled. Clients should choose the irq version when possible for system + * performance. + * + * @set: if the device is setting the active/sleep set parameter + * for the resource + * @rsc_type: unsigned 32 bit integer that identifies the type of the resource + * @rsc_id: unsigned 32 bit that uniquely identifies a resource within a type + * @kvp: array of KVP data. + * @nelem: number of KVPs pairs associated with the message. + * + * returns 0 on success and errno on failure. + */ +int msm_rpm_send_message_noirq(enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems); + +/** + * msm_rpm_driver_init() - Initialization function that registers for a + * rpm platform driver. + * + * returns 0 on success. + */ +int __init msm_rpm_driver_init(void); + +#else + +static inline struct msm_rpm_request *msm_rpm_create_request( + enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, int num_elements) +{ + return NULL; +} + +static inline struct msm_rpm_request *msm_rpm_create_request_noirq( + enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, int num_elements) +{ + return NULL; + +} +static inline uint32_t msm_rpm_add_kvp_data(struct msm_rpm_request *handle, + uint32_t key, const uint8_t *data, int count) +{ + return 0; +} +static inline uint32_t msm_rpm_add_kvp_data_noirq( + struct msm_rpm_request *handle, uint32_t key, + const uint8_t *data, int count) +{ + return 0; +} + +static inline void msm_rpm_free_request(struct msm_rpm_request *handle) +{ + return; +} + +static inline int msm_rpm_send_request(struct msm_rpm_request *handle) +{ + return 0; +} + +static inline int msm_rpm_send_request_noirq(struct msm_rpm_request *handle) +{ + return 0; + +} + +static inline void *msm_rpm_send_request_noack(struct msm_rpm_request *handle) +{ + return NULL; +} + +static inline int msm_rpm_send_message(enum msm_rpm_set set, uint32_t rsc_type, + uint32_t rsc_id, struct msm_rpm_kvp *kvp, int nelems) +{ + return 0; +} + +static inline int msm_rpm_send_message_noirq(enum msm_rpm_set set, + uint32_t rsc_type, uint32_t rsc_id, struct msm_rpm_kvp *kvp, + int nelems) +{ + return 0; +} + +static inline void *msm_rpm_send_message_noack(enum msm_rpm_set set, + uint32_t rsc_type, uint32_t rsc_id, struct msm_rpm_kvp *kvp, + int nelems) +{ + return NULL; +} + +static inline int msm_rpm_wait_for_ack(uint32_t msg_id) +{ + return 0; + +} +static inline int msm_rpm_wait_for_ack_noirq(uint32_t msg_id) +{ + return 0; +} + +static inline int __init msm_rpm_driver_init(void) +{ + return 0; +} +#endif +#endif /*__ARCH_ARM_MACH_MSM_RPM_SMD_H*/ diff --git a/include/soc/qcom/tracer_pkt.h b/include/soc/qcom/tracer_pkt.h new file mode 100644 index 000000000000..2657b79b1ed6 --- /dev/null +++ b/include/soc/qcom/tracer_pkt.h @@ -0,0 +1,130 @@ +/* Copyright (c) 2015, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ +#ifndef _TRACER_PKT_H_ +#define _TRACER_PKT_H_ + +#include <linux/err.h> +#include <linux/types.h> + +#ifdef CONFIG_TRACER_PKT + +/** + * tracer_pkt_init() - initialize the tracer packet + * @data: Pointer to the buffer to be initialized with a tracer + * packet. + * @data_len: Length of the buffer. + * @client_event_cfg: Client-specific event configuration mask. + * @glink_event_cfg: G-Link-specific event configuration mask. + * @pkt_priv: Private/Cookie information to be added to the tracer + * packet. + * @pkt_priv_len: Length of the private data. + * + * This function is used to initialize a buffer with the tracer packet header. + * The tracer packet header includes the data as passed by the elements in the + * parameters. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int tracer_pkt_init(void *data, size_t data_len, + uint16_t client_event_cfg, uint32_t glink_event_cfg, + void *pkt_priv, size_t pkt_priv_len); + +/** + * tracer_pkt_set_event_cfg() - set the event configuration mask in the tracer + * packet + * @data: Pointer to the buffer to be initialized with event + * configuration mask. + * @client_event_cfg: Client-specific event configuration mask. + * @glink_event_cfg: G-Link-specific event configuration mask. + * + * This function is used to initialize a buffer with the event configuration + * mask as passed by the elements in the parameters. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int tracer_pkt_set_event_cfg(void *data, uint16_t client_event_cfg, + uint32_t glink_event_cfg); + +/** + * tracer_pkt_log_event() - log an event specific to the tracer packet + * @data: Pointer to the buffer containing tracer packet. + * @event_id: Event ID to be logged. + * + * This function is used to log an event specific to the tracer packet. + * The event is logged either into the tracer packet itself or a different + * tracing mechanism as configured. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int tracer_pkt_log_event(void *data, uint32_t event_id); + +/** + * tracer_pkt_calc_hex_dump_size() - calculate the hex dump size of a tracer + * packet + * @data: Pointer to the buffer containing tracer packet. + * @data_len: Length of the tracer packet buffer. + * + * This function is used to calculate the length of the buffer required to + * hold the hex dump of the tracer packet. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +size_t tracer_pkt_calc_hex_dump_size(void *data, size_t data_len); + +/** + * tracer_pkt_hex_dump() - hex dump the tracer packet into a buffer + * @buf: Buffer to contain the hex dump of the tracer packet. + * @buf_len: Length of the hex dump buffer. + * @data: Buffer containing the tracer packet. + * @data_len: Length of the buffer containing the tracer packet. + * + * This function is used to dump the contents of the tracer packet into + * a buffer in a specific hexadecimal format. The hex dump buffer can then + * be dumped through debugfs. + * + * Return: 0 on success, standard Linux error codes on failure. + */ +int tracer_pkt_hex_dump(void *buf, size_t buf_len, void *data, size_t data_len); + +#else + +static inline int tracer_pkt_init(void *data, size_t data_len, + uint16_t client_event_cfg, uint32_t glink_event_cfg, + void *pkt_priv, size_t pkt_priv_len) +{ + return -EOPNOTSUPP; +} + +static inline int tracer_pkt_set_event_cfg(uint16_t client_event_cfg, + uint32_t glink_event_cfg) +{ + return -EOPNOTSUPP; +} + +static inline int tracer_pkt_log_event(void *data, uint32_t event_id) +{ + return -EOPNOTSUPP; +} + +static inline size_t tracer_pkt_calc_hex_dump_size(void *data, size_t data_len) +{ + return -EOPNOTSUPP; +} + +static inline int tracer_pkt_hex_dump(void *buf, size_t buf_len, void *data, + size_t data_len) +{ + return -EOPNOTSUPP; +} + +#endif /* CONFIG_TRACER_PKT */ +#endif /* _TRACER_PKT_H_ */ |