xref: /openbmc/linux/drivers/net/wwan/iosm/iosm_ipc_mux.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
151c45fa9SM Chetan Kumar // SPDX-License-Identifier: GPL-2.0-only
251c45fa9SM Chetan Kumar /*
351c45fa9SM Chetan Kumar  * Copyright (C) 2020-21 Intel Corporation.
451c45fa9SM Chetan Kumar  */
551c45fa9SM Chetan Kumar 
651c45fa9SM Chetan Kumar #include "iosm_ipc_mux_codec.h"
751c45fa9SM Chetan Kumar 
851c45fa9SM Chetan Kumar /* At the begin of the runtime phase the IP MUX channel shall created. */
ipc_mux_channel_create(struct iosm_mux * ipc_mux)951c45fa9SM Chetan Kumar static int ipc_mux_channel_create(struct iosm_mux *ipc_mux)
1051c45fa9SM Chetan Kumar {
1151c45fa9SM Chetan Kumar 	int channel_id;
1251c45fa9SM Chetan Kumar 
1351c45fa9SM Chetan Kumar 	channel_id = ipc_imem_channel_alloc(ipc_mux->imem, ipc_mux->instance_id,
1451c45fa9SM Chetan Kumar 					    IPC_CTYPE_WWAN);
1551c45fa9SM Chetan Kumar 
1651c45fa9SM Chetan Kumar 	if (channel_id < 0) {
1751c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev,
1851c45fa9SM Chetan Kumar 			"allocation of the MUX channel id failed");
1951c45fa9SM Chetan Kumar 		ipc_mux->state = MUX_S_ERROR;
2051c45fa9SM Chetan Kumar 		ipc_mux->event = MUX_E_NOT_APPLICABLE;
2151c45fa9SM Chetan Kumar 		goto no_channel;
2251c45fa9SM Chetan Kumar 	}
2351c45fa9SM Chetan Kumar 
2451c45fa9SM Chetan Kumar 	/* Establish the MUX channel in blocking mode. */
2551c45fa9SM Chetan Kumar 	ipc_mux->channel = ipc_imem_channel_open(ipc_mux->imem, channel_id,
2651c45fa9SM Chetan Kumar 						 IPC_HP_NET_CHANNEL_INIT);
2751c45fa9SM Chetan Kumar 
2851c45fa9SM Chetan Kumar 	if (!ipc_mux->channel) {
2951c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev, "ipc_imem_channel_open failed");
3051c45fa9SM Chetan Kumar 		ipc_mux->state = MUX_S_ERROR;
3151c45fa9SM Chetan Kumar 		ipc_mux->event = MUX_E_NOT_APPLICABLE;
3251c45fa9SM Chetan Kumar 		return -ENODEV; /* MUX channel is not available. */
3351c45fa9SM Chetan Kumar 	}
3451c45fa9SM Chetan Kumar 
3551c45fa9SM Chetan Kumar 	/* Define the MUX active state properties. */
3651c45fa9SM Chetan Kumar 	ipc_mux->state = MUX_S_ACTIVE;
3751c45fa9SM Chetan Kumar 	ipc_mux->event = MUX_E_NO_ORDERS;
3851c45fa9SM Chetan Kumar 
3951c45fa9SM Chetan Kumar no_channel:
4051c45fa9SM Chetan Kumar 	return channel_id;
4151c45fa9SM Chetan Kumar }
4251c45fa9SM Chetan Kumar 
4351c45fa9SM Chetan Kumar /* Reset the session/if id state. */
ipc_mux_session_free(struct iosm_mux * ipc_mux,int if_id)4451c45fa9SM Chetan Kumar static void ipc_mux_session_free(struct iosm_mux *ipc_mux, int if_id)
4551c45fa9SM Chetan Kumar {
4651c45fa9SM Chetan Kumar 	struct mux_session *if_entry;
4751c45fa9SM Chetan Kumar 
4851c45fa9SM Chetan Kumar 	if_entry = &ipc_mux->session[if_id];
4951c45fa9SM Chetan Kumar 	/* Reset the session state. */
5051c45fa9SM Chetan Kumar 	if_entry->wwan = NULL;
5151c45fa9SM Chetan Kumar }
5251c45fa9SM Chetan Kumar 
5351c45fa9SM Chetan Kumar /* Create and send the session open command. */
5451c45fa9SM Chetan Kumar static struct mux_cmd_open_session_resp *
ipc_mux_session_open_send(struct iosm_mux * ipc_mux,int if_id)5551c45fa9SM Chetan Kumar ipc_mux_session_open_send(struct iosm_mux *ipc_mux, int if_id)
5651c45fa9SM Chetan Kumar {
5751c45fa9SM Chetan Kumar 	struct mux_cmd_open_session_resp *open_session_resp;
5851c45fa9SM Chetan Kumar 	struct mux_acb *acb = &ipc_mux->acb;
5951c45fa9SM Chetan Kumar 	union mux_cmd_param param;
6051c45fa9SM Chetan Kumar 
6151c45fa9SM Chetan Kumar 	/* open_session commands to one ACB and start transmission. */
6251c45fa9SM Chetan Kumar 	param.open_session.flow_ctrl = 0;
6351c45fa9SM Chetan Kumar 	param.open_session.ipv4v6_hints = 0;
6451c45fa9SM Chetan Kumar 	param.open_session.reserved2 = 0;
6551c45fa9SM Chetan Kumar 	param.open_session.dl_head_pad_len = cpu_to_le32(IPC_MEM_DL_ETH_OFFSET);
6651c45fa9SM Chetan Kumar 
6751c45fa9SM Chetan Kumar 	/* Finish and transfer ACB. The user thread is suspended.
6851c45fa9SM Chetan Kumar 	 * It is a blocking function call, until CP responds or timeout.
6951c45fa9SM Chetan Kumar 	 */
7051c45fa9SM Chetan Kumar 	acb->wanted_response = MUX_CMD_OPEN_SESSION_RESP;
7151c45fa9SM Chetan Kumar 	if (ipc_mux_dl_acb_send_cmds(ipc_mux, MUX_CMD_OPEN_SESSION, if_id, 0,
7251c45fa9SM Chetan Kumar 				     &param, sizeof(param.open_session), true,
7351c45fa9SM Chetan Kumar 				 false) ||
7451c45fa9SM Chetan Kumar 	    acb->got_response != MUX_CMD_OPEN_SESSION_RESP) {
7551c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev, "if_id %d: OPEN_SESSION send failed",
7651c45fa9SM Chetan Kumar 			if_id);
7751c45fa9SM Chetan Kumar 		return NULL;
7851c45fa9SM Chetan Kumar 	}
7951c45fa9SM Chetan Kumar 
8051c45fa9SM Chetan Kumar 	open_session_resp = &ipc_mux->acb.got_param.open_session_resp;
8151c45fa9SM Chetan Kumar 	if (open_session_resp->response != cpu_to_le32(MUX_CMD_RESP_SUCCESS)) {
8251c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev,
8351c45fa9SM Chetan Kumar 			"if_id %d,session open failed,response=%d", if_id,
8451c45fa9SM Chetan Kumar 			open_session_resp->response);
8551c45fa9SM Chetan Kumar 		return NULL;
8651c45fa9SM Chetan Kumar 	}
8751c45fa9SM Chetan Kumar 
8851c45fa9SM Chetan Kumar 	return open_session_resp;
8951c45fa9SM Chetan Kumar }
9051c45fa9SM Chetan Kumar 
9151c45fa9SM Chetan Kumar /* Open the first IP session. */
ipc_mux_session_open(struct iosm_mux * ipc_mux,struct mux_session_open * session_open)9251c45fa9SM Chetan Kumar static bool ipc_mux_session_open(struct iosm_mux *ipc_mux,
9351c45fa9SM Chetan Kumar 				 struct mux_session_open *session_open)
9451c45fa9SM Chetan Kumar {
9551c45fa9SM Chetan Kumar 	struct mux_cmd_open_session_resp *open_session_resp;
9651c45fa9SM Chetan Kumar 	int if_id;
9751c45fa9SM Chetan Kumar 
9851c45fa9SM Chetan Kumar 	/* Search for a free session interface id. */
9951c45fa9SM Chetan Kumar 	if_id = le32_to_cpu(session_open->if_id);
100da633aa3SM Chetan Kumar 	if (if_id < 0 || if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
10151c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev, "invalid interface id=%d", if_id);
10251c45fa9SM Chetan Kumar 		return false;
10351c45fa9SM Chetan Kumar 	}
10451c45fa9SM Chetan Kumar 
10551c45fa9SM Chetan Kumar 	/* Create and send the session open command.
10651c45fa9SM Chetan Kumar 	 * It is a blocking function call, until CP responds or timeout.
10751c45fa9SM Chetan Kumar 	 */
10851c45fa9SM Chetan Kumar 	open_session_resp = ipc_mux_session_open_send(ipc_mux, if_id);
10951c45fa9SM Chetan Kumar 	if (!open_session_resp) {
11051c45fa9SM Chetan Kumar 		ipc_mux_session_free(ipc_mux, if_id);
11151c45fa9SM Chetan Kumar 		session_open->if_id = cpu_to_le32(-1);
11251c45fa9SM Chetan Kumar 		return false;
11351c45fa9SM Chetan Kumar 	}
11451c45fa9SM Chetan Kumar 
11551c45fa9SM Chetan Kumar 	/* Initialize the uplink skb accumulator. */
11651c45fa9SM Chetan Kumar 	skb_queue_head_init(&ipc_mux->session[if_id].ul_list);
11751c45fa9SM Chetan Kumar 
11851c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].dl_head_pad_len = IPC_MEM_DL_ETH_OFFSET;
11951c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].ul_head_pad_len =
12051c45fa9SM Chetan Kumar 		le32_to_cpu(open_session_resp->ul_head_pad_len);
12151c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].wwan = ipc_mux->wwan;
12251c45fa9SM Chetan Kumar 
12351c45fa9SM Chetan Kumar 	/* Reset the flow ctrl stats of the session */
12451c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_en_cnt = 0;
12551c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_dis_cnt = 0;
12651c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].ul_flow_credits = 0;
12751c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].net_tx_stop = false;
12851c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_mask = 0;
12951c45fa9SM Chetan Kumar 
13051c45fa9SM Chetan Kumar 	/* Save and return the assigned if id. */
13151c45fa9SM Chetan Kumar 	session_open->if_id = cpu_to_le32(if_id);
132da633aa3SM Chetan Kumar 	ipc_mux->nr_sessions++;
13351c45fa9SM Chetan Kumar 
13451c45fa9SM Chetan Kumar 	return true;
13551c45fa9SM Chetan Kumar }
13651c45fa9SM Chetan Kumar 
13751c45fa9SM Chetan Kumar /* Free pending session UL packet. */
ipc_mux_session_reset(struct iosm_mux * ipc_mux,int if_id)13851c45fa9SM Chetan Kumar static void ipc_mux_session_reset(struct iosm_mux *ipc_mux, int if_id)
13951c45fa9SM Chetan Kumar {
14051c45fa9SM Chetan Kumar 	/* Reset the session/if id state. */
14151c45fa9SM Chetan Kumar 	ipc_mux_session_free(ipc_mux, if_id);
14251c45fa9SM Chetan Kumar 
14351c45fa9SM Chetan Kumar 	/* Empty the uplink skb accumulator. */
14451c45fa9SM Chetan Kumar 	skb_queue_purge(&ipc_mux->session[if_id].ul_list);
14551c45fa9SM Chetan Kumar }
14651c45fa9SM Chetan Kumar 
ipc_mux_session_close(struct iosm_mux * ipc_mux,struct mux_session_close * msg)14751c45fa9SM Chetan Kumar static void ipc_mux_session_close(struct iosm_mux *ipc_mux,
14851c45fa9SM Chetan Kumar 				  struct mux_session_close *msg)
14951c45fa9SM Chetan Kumar {
15051c45fa9SM Chetan Kumar 	int if_id;
15151c45fa9SM Chetan Kumar 
15251c45fa9SM Chetan Kumar 	/* Copy the session interface id. */
15351c45fa9SM Chetan Kumar 	if_id = le32_to_cpu(msg->if_id);
15451c45fa9SM Chetan Kumar 
155da633aa3SM Chetan Kumar 	if (if_id < 0 || if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
15651c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev, "invalid session id %d", if_id);
15751c45fa9SM Chetan Kumar 		return;
15851c45fa9SM Chetan Kumar 	}
15951c45fa9SM Chetan Kumar 
16051c45fa9SM Chetan Kumar 	/* Create and send the session close command.
16151c45fa9SM Chetan Kumar 	 * It is a blocking function call, until CP responds or timeout.
16251c45fa9SM Chetan Kumar 	 */
16351c45fa9SM Chetan Kumar 	if (ipc_mux_dl_acb_send_cmds(ipc_mux, MUX_CMD_CLOSE_SESSION, if_id, 0,
16451c45fa9SM Chetan Kumar 				     NULL, 0, true, false))
16551c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev, "if_id %d: CLOSE_SESSION send failed",
16651c45fa9SM Chetan Kumar 			if_id);
16751c45fa9SM Chetan Kumar 
16851c45fa9SM Chetan Kumar 	/* Reset the flow ctrl stats of the session */
16951c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_en_cnt = 0;
17051c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_dis_cnt = 0;
17151c45fa9SM Chetan Kumar 	ipc_mux->session[if_id].flow_ctl_mask = 0;
17251c45fa9SM Chetan Kumar 
17351c45fa9SM Chetan Kumar 	ipc_mux_session_reset(ipc_mux, if_id);
174da633aa3SM Chetan Kumar 	ipc_mux->nr_sessions--;
17551c45fa9SM Chetan Kumar }
17651c45fa9SM Chetan Kumar 
ipc_mux_channel_close(struct iosm_mux * ipc_mux,struct mux_channel_close * channel_close_p)17751c45fa9SM Chetan Kumar static void ipc_mux_channel_close(struct iosm_mux *ipc_mux,
17851c45fa9SM Chetan Kumar 				  struct mux_channel_close *channel_close_p)
17951c45fa9SM Chetan Kumar {
18051c45fa9SM Chetan Kumar 	int i;
18151c45fa9SM Chetan Kumar 
18251c45fa9SM Chetan Kumar 	/* Free pending session UL packet. */
183da633aa3SM Chetan Kumar 	for (i = 0; i < IPC_MEM_MUX_IP_SESSION_ENTRIES; i++)
18451c45fa9SM Chetan Kumar 		if (ipc_mux->session[i].wwan)
18551c45fa9SM Chetan Kumar 			ipc_mux_session_reset(ipc_mux, i);
18651c45fa9SM Chetan Kumar 
18751c45fa9SM Chetan Kumar 	ipc_imem_channel_close(ipc_mux->imem, ipc_mux->channel_id);
18851c45fa9SM Chetan Kumar 
18951c45fa9SM Chetan Kumar 	/* Reset the MUX object. */
19051c45fa9SM Chetan Kumar 	ipc_mux->state = MUX_S_INACTIVE;
19151c45fa9SM Chetan Kumar 	ipc_mux->event = MUX_E_INACTIVE;
19251c45fa9SM Chetan Kumar }
19351c45fa9SM Chetan Kumar 
19451c45fa9SM Chetan Kumar /* CP has interrupted AP. If AP is in IP MUX mode, execute the pending ops. */
ipc_mux_schedule(struct iosm_mux * ipc_mux,union mux_msg * msg)19551c45fa9SM Chetan Kumar static int ipc_mux_schedule(struct iosm_mux *ipc_mux, union mux_msg *msg)
19651c45fa9SM Chetan Kumar {
19751c45fa9SM Chetan Kumar 	enum mux_event order;
19851c45fa9SM Chetan Kumar 	bool success;
19951c45fa9SM Chetan Kumar 	int ret = -EIO;
20051c45fa9SM Chetan Kumar 
20151c45fa9SM Chetan Kumar 	if (!ipc_mux->initialized) {
20251c45fa9SM Chetan Kumar 		ret = -EAGAIN;
20351c45fa9SM Chetan Kumar 		goto out;
20451c45fa9SM Chetan Kumar 	}
20551c45fa9SM Chetan Kumar 
20651c45fa9SM Chetan Kumar 	order = msg->common.event;
20751c45fa9SM Chetan Kumar 
20851c45fa9SM Chetan Kumar 	switch (ipc_mux->state) {
20951c45fa9SM Chetan Kumar 	case MUX_S_INACTIVE:
21051c45fa9SM Chetan Kumar 		if (order != MUX_E_MUX_SESSION_OPEN)
21151c45fa9SM Chetan Kumar 			goto out; /* Wait for the request to open a session */
21251c45fa9SM Chetan Kumar 
21351c45fa9SM Chetan Kumar 		if (ipc_mux->event == MUX_E_INACTIVE)
21451c45fa9SM Chetan Kumar 			/* Establish the MUX channel and the new state. */
21551c45fa9SM Chetan Kumar 			ipc_mux->channel_id = ipc_mux_channel_create(ipc_mux);
21651c45fa9SM Chetan Kumar 
21751c45fa9SM Chetan Kumar 		if (ipc_mux->state != MUX_S_ACTIVE) {
21851c45fa9SM Chetan Kumar 			ret = ipc_mux->channel_id; /* Missing the MUX channel */
21951c45fa9SM Chetan Kumar 			goto out;
22051c45fa9SM Chetan Kumar 		}
22151c45fa9SM Chetan Kumar 
22251c45fa9SM Chetan Kumar 		/* Disable the TD update timer and open the first IP session. */
22351c45fa9SM Chetan Kumar 		ipc_imem_td_update_timer_suspend(ipc_mux->imem, true);
22451c45fa9SM Chetan Kumar 		ipc_mux->event = MUX_E_MUX_SESSION_OPEN;
22551c45fa9SM Chetan Kumar 		success = ipc_mux_session_open(ipc_mux, &msg->session_open);
22651c45fa9SM Chetan Kumar 
22751c45fa9SM Chetan Kumar 		ipc_imem_td_update_timer_suspend(ipc_mux->imem, false);
22851c45fa9SM Chetan Kumar 		if (success)
22951c45fa9SM Chetan Kumar 			ret = ipc_mux->channel_id;
23051c45fa9SM Chetan Kumar 		goto out;
23151c45fa9SM Chetan Kumar 
23251c45fa9SM Chetan Kumar 	case MUX_S_ACTIVE:
23351c45fa9SM Chetan Kumar 		switch (order) {
23451c45fa9SM Chetan Kumar 		case MUX_E_MUX_SESSION_OPEN:
23551c45fa9SM Chetan Kumar 			/* Disable the TD update timer and open a session */
23651c45fa9SM Chetan Kumar 			ipc_imem_td_update_timer_suspend(ipc_mux->imem, true);
23751c45fa9SM Chetan Kumar 			ipc_mux->event = MUX_E_MUX_SESSION_OPEN;
23851c45fa9SM Chetan Kumar 			success = ipc_mux_session_open(ipc_mux,
23951c45fa9SM Chetan Kumar 						       &msg->session_open);
24051c45fa9SM Chetan Kumar 			ipc_imem_td_update_timer_suspend(ipc_mux->imem, false);
24151c45fa9SM Chetan Kumar 			if (success)
24251c45fa9SM Chetan Kumar 				ret = ipc_mux->channel_id;
24351c45fa9SM Chetan Kumar 			goto out;
24451c45fa9SM Chetan Kumar 
24551c45fa9SM Chetan Kumar 		case MUX_E_MUX_SESSION_CLOSE:
24651c45fa9SM Chetan Kumar 			/* Release an IP session. */
24751c45fa9SM Chetan Kumar 			ipc_mux->event = MUX_E_MUX_SESSION_CLOSE;
24851c45fa9SM Chetan Kumar 			ipc_mux_session_close(ipc_mux, &msg->session_close);
249da633aa3SM Chetan Kumar 			if (!ipc_mux->nr_sessions) {
250da633aa3SM Chetan Kumar 				ipc_mux->event = MUX_E_MUX_CHANNEL_CLOSE;
251da633aa3SM Chetan Kumar 				ipc_mux_channel_close(ipc_mux,
252da633aa3SM Chetan Kumar 						      &msg->channel_close);
253da633aa3SM Chetan Kumar 			}
25451c45fa9SM Chetan Kumar 			ret = ipc_mux->channel_id;
25551c45fa9SM Chetan Kumar 			goto out;
25651c45fa9SM Chetan Kumar 
25751c45fa9SM Chetan Kumar 		case MUX_E_MUX_CHANNEL_CLOSE:
25851c45fa9SM Chetan Kumar 			/* Close the MUX channel pipes. */
25951c45fa9SM Chetan Kumar 			ipc_mux->event = MUX_E_MUX_CHANNEL_CLOSE;
26051c45fa9SM Chetan Kumar 			ipc_mux_channel_close(ipc_mux, &msg->channel_close);
26151c45fa9SM Chetan Kumar 			ret = ipc_mux->channel_id;
26251c45fa9SM Chetan Kumar 			goto out;
26351c45fa9SM Chetan Kumar 
26451c45fa9SM Chetan Kumar 		default:
26551c45fa9SM Chetan Kumar 			/* Invalid order. */
26651c45fa9SM Chetan Kumar 			goto out;
26751c45fa9SM Chetan Kumar 		}
26851c45fa9SM Chetan Kumar 
26951c45fa9SM Chetan Kumar 	default:
27051c45fa9SM Chetan Kumar 		dev_err(ipc_mux->dev,
27151c45fa9SM Chetan Kumar 			"unexpected MUX transition: state=%d, event=%d",
27251c45fa9SM Chetan Kumar 			ipc_mux->state, ipc_mux->event);
27351c45fa9SM Chetan Kumar 	}
27451c45fa9SM Chetan Kumar out:
27551c45fa9SM Chetan Kumar 	return ret;
27651c45fa9SM Chetan Kumar }
27751c45fa9SM Chetan Kumar 
ipc_mux_init(struct ipc_mux_config * mux_cfg,struct iosm_imem * imem)27851c45fa9SM Chetan Kumar struct iosm_mux *ipc_mux_init(struct ipc_mux_config *mux_cfg,
27951c45fa9SM Chetan Kumar 			      struct iosm_imem *imem)
28051c45fa9SM Chetan Kumar {
28151c45fa9SM Chetan Kumar 	struct iosm_mux *ipc_mux = kzalloc(sizeof(*ipc_mux), GFP_KERNEL);
2821f52d7b6SM Chetan Kumar 	int i, j, ul_tds, ul_td_size;
28351c45fa9SM Chetan Kumar 	struct sk_buff_head *free_list;
28451c45fa9SM Chetan Kumar 	struct sk_buff *skb;
2851f52d7b6SM Chetan Kumar 	int qlt_size;
28651c45fa9SM Chetan Kumar 
28751c45fa9SM Chetan Kumar 	if (!ipc_mux)
28851c45fa9SM Chetan Kumar 		return NULL;
28951c45fa9SM Chetan Kumar 
29051c45fa9SM Chetan Kumar 	ipc_mux->protocol = mux_cfg->protocol;
29151c45fa9SM Chetan Kumar 	ipc_mux->ul_flow = mux_cfg->ul_flow;
29251c45fa9SM Chetan Kumar 	ipc_mux->instance_id = mux_cfg->instance_id;
29351c45fa9SM Chetan Kumar 	ipc_mux->wwan_q_offset = 0;
29451c45fa9SM Chetan Kumar 
29551c45fa9SM Chetan Kumar 	ipc_mux->pcie = imem->pcie;
29651c45fa9SM Chetan Kumar 	ipc_mux->imem = imem;
29751c45fa9SM Chetan Kumar 	ipc_mux->ipc_protocol = imem->ipc_protocol;
29851c45fa9SM Chetan Kumar 	ipc_mux->dev = imem->dev;
29951c45fa9SM Chetan Kumar 	ipc_mux->wwan = imem->wwan;
30051c45fa9SM Chetan Kumar 
30151c45fa9SM Chetan Kumar 	/* Get the reference to the UL ADB list. */
30251c45fa9SM Chetan Kumar 	free_list = &ipc_mux->ul_adb.free_list;
30351c45fa9SM Chetan Kumar 
30451c45fa9SM Chetan Kumar 	/* Initialize the list with free ADB. */
30551c45fa9SM Chetan Kumar 	skb_queue_head_init(free_list);
30651c45fa9SM Chetan Kumar 
30751c45fa9SM Chetan Kumar 	ul_td_size = IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE;
30851c45fa9SM Chetan Kumar 
30951c45fa9SM Chetan Kumar 	ul_tds = IPC_MEM_MAX_TDS_MUX_LITE_UL;
31051c45fa9SM Chetan Kumar 
31151c45fa9SM Chetan Kumar 	ipc_mux->ul_adb.dest_skb = NULL;
31251c45fa9SM Chetan Kumar 
31351c45fa9SM Chetan Kumar 	ipc_mux->initialized = true;
31451c45fa9SM Chetan Kumar 	ipc_mux->adb_prep_ongoing = false;
31551c45fa9SM Chetan Kumar 	ipc_mux->size_needed = 0;
31651c45fa9SM Chetan Kumar 	ipc_mux->ul_data_pend_bytes = 0;
31751c45fa9SM Chetan Kumar 	ipc_mux->state = MUX_S_INACTIVE;
31851c45fa9SM Chetan Kumar 	ipc_mux->ev_mux_net_transmit_pending = false;
31951c45fa9SM Chetan Kumar 	ipc_mux->tx_transaction_id = 0;
32051c45fa9SM Chetan Kumar 	ipc_mux->rr_next_session = 0;
32151c45fa9SM Chetan Kumar 	ipc_mux->event = MUX_E_INACTIVE;
32251c45fa9SM Chetan Kumar 	ipc_mux->channel_id = -1;
32351c45fa9SM Chetan Kumar 	ipc_mux->channel = NULL;
32451c45fa9SM Chetan Kumar 
3251f52d7b6SM Chetan Kumar 	if (ipc_mux->protocol != MUX_LITE) {
3261f52d7b6SM Chetan Kumar 		qlt_size = offsetof(struct mux_qlth, ql) +
3271f52d7b6SM Chetan Kumar 				MUX_QUEUE_LEVEL * sizeof(struct mux_qlth_ql);
3281f52d7b6SM Chetan Kumar 
3291f52d7b6SM Chetan Kumar 		for (i = 0; i < IPC_MEM_MUX_IP_SESSION_ENTRIES; i++) {
3301f52d7b6SM Chetan Kumar 			ipc_mux->ul_adb.pp_qlt[i] = kzalloc(qlt_size,
3311f52d7b6SM Chetan Kumar 							    GFP_ATOMIC);
3321f52d7b6SM Chetan Kumar 			if (!ipc_mux->ul_adb.pp_qlt[i]) {
3331f52d7b6SM Chetan Kumar 				for (j = i - 1; j >= 0; j--)
3341f52d7b6SM Chetan Kumar 					kfree(ipc_mux->ul_adb.pp_qlt[j]);
335*23353efcSZhengchao Shao 				kfree(ipc_mux);
3361f52d7b6SM Chetan Kumar 				return NULL;
3371f52d7b6SM Chetan Kumar 			}
3381f52d7b6SM Chetan Kumar 		}
3391f52d7b6SM Chetan Kumar 
3401f52d7b6SM Chetan Kumar 		ul_td_size = IPC_MEM_MAX_UL_ADB_BUF_SIZE;
3411f52d7b6SM Chetan Kumar 		ul_tds = IPC_MEM_MAX_TDS_MUX_AGGR_UL;
3421f52d7b6SM Chetan Kumar 	}
3431f52d7b6SM Chetan Kumar 
34451c45fa9SM Chetan Kumar 	/* Allocate the list of UL ADB. */
34551c45fa9SM Chetan Kumar 	for (i = 0; i < ul_tds; i++) {
34651c45fa9SM Chetan Kumar 		dma_addr_t mapping;
34751c45fa9SM Chetan Kumar 
34851c45fa9SM Chetan Kumar 		skb = ipc_pcie_alloc_skb(ipc_mux->pcie, ul_td_size, GFP_ATOMIC,
34951c45fa9SM Chetan Kumar 					 &mapping, DMA_TO_DEVICE, 0);
35051c45fa9SM Chetan Kumar 		if (!skb) {
35151c45fa9SM Chetan Kumar 			ipc_mux_deinit(ipc_mux);
35251c45fa9SM Chetan Kumar 			return NULL;
35351c45fa9SM Chetan Kumar 		}
35451c45fa9SM Chetan Kumar 		/* Extend the UL ADB list. */
35551c45fa9SM Chetan Kumar 		skb_queue_tail(free_list, skb);
35651c45fa9SM Chetan Kumar 	}
35751c45fa9SM Chetan Kumar 
35851c45fa9SM Chetan Kumar 	return ipc_mux;
35951c45fa9SM Chetan Kumar }
36051c45fa9SM Chetan Kumar 
36151c45fa9SM Chetan Kumar /* Informs the network stack to restart transmission for all opened session if
36251c45fa9SM Chetan Kumar  * Flow Control is not ON for that session.
36351c45fa9SM Chetan Kumar  */
ipc_mux_restart_tx_for_all_sessions(struct iosm_mux * ipc_mux)36451c45fa9SM Chetan Kumar static void ipc_mux_restart_tx_for_all_sessions(struct iosm_mux *ipc_mux)
36551c45fa9SM Chetan Kumar {
36651c45fa9SM Chetan Kumar 	struct mux_session *session;
36751c45fa9SM Chetan Kumar 	int idx;
36851c45fa9SM Chetan Kumar 
369da633aa3SM Chetan Kumar 	for (idx = 0; idx < IPC_MEM_MUX_IP_SESSION_ENTRIES; idx++) {
37051c45fa9SM Chetan Kumar 		session = &ipc_mux->session[idx];
37151c45fa9SM Chetan Kumar 
37251c45fa9SM Chetan Kumar 		if (!session->wwan)
37351c45fa9SM Chetan Kumar 			continue;
37451c45fa9SM Chetan Kumar 
37551c45fa9SM Chetan Kumar 		/* If flow control of the session is OFF and if there was tx
37651c45fa9SM Chetan Kumar 		 * stop then restart. Inform the network interface to restart
37751c45fa9SM Chetan Kumar 		 * sending data.
37851c45fa9SM Chetan Kumar 		 */
37951c45fa9SM Chetan Kumar 		if (session->flow_ctl_mask == 0) {
38051c45fa9SM Chetan Kumar 			session->net_tx_stop = false;
38151c45fa9SM Chetan Kumar 			ipc_mux_netif_tx_flowctrl(session, idx, false);
38251c45fa9SM Chetan Kumar 		}
38351c45fa9SM Chetan Kumar 	}
38451c45fa9SM Chetan Kumar }
38551c45fa9SM Chetan Kumar 
38651c45fa9SM Chetan Kumar /* Informs the network stack to stop sending further pkt for all opened
38751c45fa9SM Chetan Kumar  * sessions
38851c45fa9SM Chetan Kumar  */
ipc_mux_stop_netif_for_all_sessions(struct iosm_mux * ipc_mux)38951c45fa9SM Chetan Kumar static void ipc_mux_stop_netif_for_all_sessions(struct iosm_mux *ipc_mux)
39051c45fa9SM Chetan Kumar {
39151c45fa9SM Chetan Kumar 	struct mux_session *session;
39251c45fa9SM Chetan Kumar 	int idx;
39351c45fa9SM Chetan Kumar 
394da633aa3SM Chetan Kumar 	for (idx = 0; idx < IPC_MEM_MUX_IP_SESSION_ENTRIES; idx++) {
39551c45fa9SM Chetan Kumar 		session = &ipc_mux->session[idx];
39651c45fa9SM Chetan Kumar 
39751c45fa9SM Chetan Kumar 		if (!session->wwan)
39851c45fa9SM Chetan Kumar 			continue;
39951c45fa9SM Chetan Kumar 
40051c45fa9SM Chetan Kumar 		ipc_mux_netif_tx_flowctrl(session, session->if_id, true);
40151c45fa9SM Chetan Kumar 	}
40251c45fa9SM Chetan Kumar }
40351c45fa9SM Chetan Kumar 
ipc_mux_check_n_restart_tx(struct iosm_mux * ipc_mux)40451c45fa9SM Chetan Kumar void ipc_mux_check_n_restart_tx(struct iosm_mux *ipc_mux)
40551c45fa9SM Chetan Kumar {
40651c45fa9SM Chetan Kumar 	if (ipc_mux->ul_flow == MUX_UL) {
40751c45fa9SM Chetan Kumar 		int low_thresh = IPC_MEM_MUX_UL_FLOWCTRL_LOW_B;
40851c45fa9SM Chetan Kumar 
40951c45fa9SM Chetan Kumar 		if (ipc_mux->ul_data_pend_bytes < low_thresh)
41051c45fa9SM Chetan Kumar 			ipc_mux_restart_tx_for_all_sessions(ipc_mux);
41151c45fa9SM Chetan Kumar 	}
41251c45fa9SM Chetan Kumar }
41351c45fa9SM Chetan Kumar 
ipc_mux_get_max_sessions(struct iosm_mux * ipc_mux)41451c45fa9SM Chetan Kumar int ipc_mux_get_max_sessions(struct iosm_mux *ipc_mux)
41551c45fa9SM Chetan Kumar {
416da633aa3SM Chetan Kumar 	return ipc_mux ? IPC_MEM_MUX_IP_SESSION_ENTRIES : -EFAULT;
41751c45fa9SM Chetan Kumar }
41851c45fa9SM Chetan Kumar 
ipc_mux_get_active_protocol(struct iosm_mux * ipc_mux)41951c45fa9SM Chetan Kumar enum ipc_mux_protocol ipc_mux_get_active_protocol(struct iosm_mux *ipc_mux)
42051c45fa9SM Chetan Kumar {
42151c45fa9SM Chetan Kumar 	return ipc_mux ? ipc_mux->protocol : MUX_UNKNOWN;
42251c45fa9SM Chetan Kumar }
42351c45fa9SM Chetan Kumar 
ipc_mux_open_session(struct iosm_mux * ipc_mux,int session_nr)42451c45fa9SM Chetan Kumar int ipc_mux_open_session(struct iosm_mux *ipc_mux, int session_nr)
42551c45fa9SM Chetan Kumar {
42651c45fa9SM Chetan Kumar 	struct mux_session_open *session_open;
42751c45fa9SM Chetan Kumar 	union mux_msg mux_msg;
42851c45fa9SM Chetan Kumar 
42951c45fa9SM Chetan Kumar 	session_open = &mux_msg.session_open;
43051c45fa9SM Chetan Kumar 	session_open->event = MUX_E_MUX_SESSION_OPEN;
43151c45fa9SM Chetan Kumar 
43251c45fa9SM Chetan Kumar 	session_open->if_id = cpu_to_le32(session_nr);
43351c45fa9SM Chetan Kumar 	ipc_mux->session[session_nr].flags |= IPC_MEM_WWAN_MUX;
43451c45fa9SM Chetan Kumar 	return ipc_mux_schedule(ipc_mux, &mux_msg);
43551c45fa9SM Chetan Kumar }
43651c45fa9SM Chetan Kumar 
ipc_mux_close_session(struct iosm_mux * ipc_mux,int session_nr)43751c45fa9SM Chetan Kumar int ipc_mux_close_session(struct iosm_mux *ipc_mux, int session_nr)
43851c45fa9SM Chetan Kumar {
43951c45fa9SM Chetan Kumar 	struct mux_session_close *session_close;
44051c45fa9SM Chetan Kumar 	union mux_msg mux_msg;
44151c45fa9SM Chetan Kumar 	int ret_val;
44251c45fa9SM Chetan Kumar 
44351c45fa9SM Chetan Kumar 	session_close = &mux_msg.session_close;
44451c45fa9SM Chetan Kumar 	session_close->event = MUX_E_MUX_SESSION_CLOSE;
44551c45fa9SM Chetan Kumar 
44651c45fa9SM Chetan Kumar 	session_close->if_id = cpu_to_le32(session_nr);
44751c45fa9SM Chetan Kumar 	ret_val = ipc_mux_schedule(ipc_mux, &mux_msg);
44851c45fa9SM Chetan Kumar 	ipc_mux->session[session_nr].flags &= ~IPC_MEM_WWAN_MUX;
44951c45fa9SM Chetan Kumar 
45051c45fa9SM Chetan Kumar 	return ret_val;
45151c45fa9SM Chetan Kumar }
45251c45fa9SM Chetan Kumar 
ipc_mux_deinit(struct iosm_mux * ipc_mux)45351c45fa9SM Chetan Kumar void ipc_mux_deinit(struct iosm_mux *ipc_mux)
45451c45fa9SM Chetan Kumar {
45551c45fa9SM Chetan Kumar 	struct mux_channel_close *channel_close;
45651c45fa9SM Chetan Kumar 	struct sk_buff_head *free_list;
45751c45fa9SM Chetan Kumar 	union mux_msg mux_msg;
45851c45fa9SM Chetan Kumar 	struct sk_buff *skb;
45951c45fa9SM Chetan Kumar 
46051c45fa9SM Chetan Kumar 	if (!ipc_mux->initialized)
46151c45fa9SM Chetan Kumar 		return;
46251c45fa9SM Chetan Kumar 	ipc_mux_stop_netif_for_all_sessions(ipc_mux);
46351c45fa9SM Chetan Kumar 
464da633aa3SM Chetan Kumar 	if (ipc_mux->state == MUX_S_ACTIVE) {
46551c45fa9SM Chetan Kumar 		channel_close = &mux_msg.channel_close;
46651c45fa9SM Chetan Kumar 		channel_close->event = MUX_E_MUX_CHANNEL_CLOSE;
46751c45fa9SM Chetan Kumar 		ipc_mux_schedule(ipc_mux, &mux_msg);
468da633aa3SM Chetan Kumar 	}
46951c45fa9SM Chetan Kumar 
47051c45fa9SM Chetan Kumar 	/* Empty the ADB free list. */
47151c45fa9SM Chetan Kumar 	free_list = &ipc_mux->ul_adb.free_list;
47251c45fa9SM Chetan Kumar 
47351c45fa9SM Chetan Kumar 	/* Remove from the head of the downlink queue. */
47451c45fa9SM Chetan Kumar 	while ((skb = skb_dequeue(free_list)))
47551c45fa9SM Chetan Kumar 		ipc_pcie_kfree_skb(ipc_mux->pcie, skb);
47651c45fa9SM Chetan Kumar 
47751c45fa9SM Chetan Kumar 	if (ipc_mux->channel) {
47851c45fa9SM Chetan Kumar 		ipc_mux->channel->ul_pipe.is_open = false;
47951c45fa9SM Chetan Kumar 		ipc_mux->channel->dl_pipe.is_open = false;
48051c45fa9SM Chetan Kumar 	}
48151c45fa9SM Chetan Kumar 
48251c45fa9SM Chetan Kumar 	kfree(ipc_mux);
48351c45fa9SM Chetan Kumar }
484