1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-21 Intel Corporation.
4  */
5 
6 #include "iosm_ipc_mux_codec.h"
7 
8 /* At the begin of the runtime phase the IP MUX channel shall created. */
9 static int ipc_mux_channel_create(struct iosm_mux *ipc_mux)
10 {
11 	int channel_id;
12 
13 	channel_id = ipc_imem_channel_alloc(ipc_mux->imem, ipc_mux->instance_id,
14 					    IPC_CTYPE_WWAN);
15 
16 	if (channel_id < 0) {
17 		dev_err(ipc_mux->dev,
18 			"allocation of the MUX channel id failed");
19 		ipc_mux->state = MUX_S_ERROR;
20 		ipc_mux->event = MUX_E_NOT_APPLICABLE;
21 		goto no_channel;
22 	}
23 
24 	/* Establish the MUX channel in blocking mode. */
25 	ipc_mux->channel = ipc_imem_channel_open(ipc_mux->imem, channel_id,
26 						 IPC_HP_NET_CHANNEL_INIT);
27 
28 	if (!ipc_mux->channel) {
29 		dev_err(ipc_mux->dev, "ipc_imem_channel_open failed");
30 		ipc_mux->state = MUX_S_ERROR;
31 		ipc_mux->event = MUX_E_NOT_APPLICABLE;
32 		return -ENODEV; /* MUX channel is not available. */
33 	}
34 
35 	/* Define the MUX active state properties. */
36 	ipc_mux->state = MUX_S_ACTIVE;
37 	ipc_mux->event = MUX_E_NO_ORDERS;
38 
39 no_channel:
40 	return channel_id;
41 }
42 
43 /* Reset the session/if id state. */
44 static void ipc_mux_session_free(struct iosm_mux *ipc_mux, int if_id)
45 {
46 	struct mux_session *if_entry;
47 
48 	if_entry = &ipc_mux->session[if_id];
49 	/* Reset the session state. */
50 	if_entry->wwan = NULL;
51 }
52 
53 /* Create and send the session open command. */
54 static struct mux_cmd_open_session_resp *
55 ipc_mux_session_open_send(struct iosm_mux *ipc_mux, int if_id)
56 {
57 	struct mux_cmd_open_session_resp *open_session_resp;
58 	struct mux_acb *acb = &ipc_mux->acb;
59 	union mux_cmd_param param;
60 
61 	/* open_session commands to one ACB and start transmission. */
62 	param.open_session.flow_ctrl = 0;
63 	param.open_session.ipv4v6_hints = 0;
64 	param.open_session.reserved2 = 0;
65 	param.open_session.dl_head_pad_len = cpu_to_le32(IPC_MEM_DL_ETH_OFFSET);
66 
67 	/* Finish and transfer ACB. The user thread is suspended.
68 	 * It is a blocking function call, until CP responds or timeout.
69 	 */
70 	acb->wanted_response = MUX_CMD_OPEN_SESSION_RESP;
71 	if (ipc_mux_dl_acb_send_cmds(ipc_mux, MUX_CMD_OPEN_SESSION, if_id, 0,
72 				     &param, sizeof(param.open_session), true,
73 				 false) ||
74 	    acb->got_response != MUX_CMD_OPEN_SESSION_RESP) {
75 		dev_err(ipc_mux->dev, "if_id %d: OPEN_SESSION send failed",
76 			if_id);
77 		return NULL;
78 	}
79 
80 	open_session_resp = &ipc_mux->acb.got_param.open_session_resp;
81 	if (open_session_resp->response != cpu_to_le32(MUX_CMD_RESP_SUCCESS)) {
82 		dev_err(ipc_mux->dev,
83 			"if_id %d,session open failed,response=%d", if_id,
84 			open_session_resp->response);
85 		return NULL;
86 	}
87 
88 	return open_session_resp;
89 }
90 
91 /* Open the first IP session. */
92 static bool ipc_mux_session_open(struct iosm_mux *ipc_mux,
93 				 struct mux_session_open *session_open)
94 {
95 	struct mux_cmd_open_session_resp *open_session_resp;
96 	int if_id;
97 
98 	/* Search for a free session interface id. */
99 	if_id = le32_to_cpu(session_open->if_id);
100 	if (if_id < 0 || if_id >= ipc_mux->nr_sessions) {
101 		dev_err(ipc_mux->dev, "invalid interface id=%d", if_id);
102 		return false;
103 	}
104 
105 	/* Create and send the session open command.
106 	 * It is a blocking function call, until CP responds or timeout.
107 	 */
108 	open_session_resp = ipc_mux_session_open_send(ipc_mux, if_id);
109 	if (!open_session_resp) {
110 		ipc_mux_session_free(ipc_mux, if_id);
111 		session_open->if_id = cpu_to_le32(-1);
112 		return false;
113 	}
114 
115 	/* Initialize the uplink skb accumulator. */
116 	skb_queue_head_init(&ipc_mux->session[if_id].ul_list);
117 
118 	ipc_mux->session[if_id].dl_head_pad_len = IPC_MEM_DL_ETH_OFFSET;
119 	ipc_mux->session[if_id].ul_head_pad_len =
120 		le32_to_cpu(open_session_resp->ul_head_pad_len);
121 	ipc_mux->session[if_id].wwan = ipc_mux->wwan;
122 
123 	/* Reset the flow ctrl stats of the session */
124 	ipc_mux->session[if_id].flow_ctl_en_cnt = 0;
125 	ipc_mux->session[if_id].flow_ctl_dis_cnt = 0;
126 	ipc_mux->session[if_id].ul_flow_credits = 0;
127 	ipc_mux->session[if_id].net_tx_stop = false;
128 	ipc_mux->session[if_id].flow_ctl_mask = 0;
129 
130 	/* Save and return the assigned if id. */
131 	session_open->if_id = cpu_to_le32(if_id);
132 
133 	return true;
134 }
135 
136 /* Free pending session UL packet. */
137 static void ipc_mux_session_reset(struct iosm_mux *ipc_mux, int if_id)
138 {
139 	/* Reset the session/if id state. */
140 	ipc_mux_session_free(ipc_mux, if_id);
141 
142 	/* Empty the uplink skb accumulator. */
143 	skb_queue_purge(&ipc_mux->session[if_id].ul_list);
144 }
145 
146 static void ipc_mux_session_close(struct iosm_mux *ipc_mux,
147 				  struct mux_session_close *msg)
148 {
149 	int if_id;
150 
151 	/* Copy the session interface id. */
152 	if_id = le32_to_cpu(msg->if_id);
153 
154 	if (if_id < 0 || if_id >= ipc_mux->nr_sessions) {
155 		dev_err(ipc_mux->dev, "invalid session id %d", if_id);
156 		return;
157 	}
158 
159 	/* Create and send the session close command.
160 	 * It is a blocking function call, until CP responds or timeout.
161 	 */
162 	if (ipc_mux_dl_acb_send_cmds(ipc_mux, MUX_CMD_CLOSE_SESSION, if_id, 0,
163 				     NULL, 0, true, false))
164 		dev_err(ipc_mux->dev, "if_id %d: CLOSE_SESSION send failed",
165 			if_id);
166 
167 	/* Reset the flow ctrl stats of the session */
168 	ipc_mux->session[if_id].flow_ctl_en_cnt = 0;
169 	ipc_mux->session[if_id].flow_ctl_dis_cnt = 0;
170 	ipc_mux->session[if_id].flow_ctl_mask = 0;
171 
172 	ipc_mux_session_reset(ipc_mux, if_id);
173 }
174 
175 static void ipc_mux_channel_close(struct iosm_mux *ipc_mux,
176 				  struct mux_channel_close *channel_close_p)
177 {
178 	int i;
179 
180 	/* Free pending session UL packet. */
181 	for (i = 0; i < ipc_mux->nr_sessions; i++)
182 		if (ipc_mux->session[i].wwan)
183 			ipc_mux_session_reset(ipc_mux, i);
184 
185 	ipc_imem_channel_close(ipc_mux->imem, ipc_mux->channel_id);
186 
187 	/* Reset the MUX object. */
188 	ipc_mux->state = MUX_S_INACTIVE;
189 	ipc_mux->event = MUX_E_INACTIVE;
190 }
191 
192 /* CP has interrupted AP. If AP is in IP MUX mode, execute the pending ops. */
193 static int ipc_mux_schedule(struct iosm_mux *ipc_mux, union mux_msg *msg)
194 {
195 	enum mux_event order;
196 	bool success;
197 	int ret = -EIO;
198 
199 	if (!ipc_mux->initialized) {
200 		ret = -EAGAIN;
201 		goto out;
202 	}
203 
204 	order = msg->common.event;
205 
206 	switch (ipc_mux->state) {
207 	case MUX_S_INACTIVE:
208 		if (order != MUX_E_MUX_SESSION_OPEN)
209 			goto out; /* Wait for the request to open a session */
210 
211 		if (ipc_mux->event == MUX_E_INACTIVE)
212 			/* Establish the MUX channel and the new state. */
213 			ipc_mux->channel_id = ipc_mux_channel_create(ipc_mux);
214 
215 		if (ipc_mux->state != MUX_S_ACTIVE) {
216 			ret = ipc_mux->channel_id; /* Missing the MUX channel */
217 			goto out;
218 		}
219 
220 		/* Disable the TD update timer and open the first IP session. */
221 		ipc_imem_td_update_timer_suspend(ipc_mux->imem, true);
222 		ipc_mux->event = MUX_E_MUX_SESSION_OPEN;
223 		success = ipc_mux_session_open(ipc_mux, &msg->session_open);
224 
225 		ipc_imem_td_update_timer_suspend(ipc_mux->imem, false);
226 		if (success)
227 			ret = ipc_mux->channel_id;
228 		goto out;
229 
230 	case MUX_S_ACTIVE:
231 		switch (order) {
232 		case MUX_E_MUX_SESSION_OPEN:
233 			/* Disable the TD update timer and open a session */
234 			ipc_imem_td_update_timer_suspend(ipc_mux->imem, true);
235 			ipc_mux->event = MUX_E_MUX_SESSION_OPEN;
236 			success = ipc_mux_session_open(ipc_mux,
237 						       &msg->session_open);
238 			ipc_imem_td_update_timer_suspend(ipc_mux->imem, false);
239 			if (success)
240 				ret = ipc_mux->channel_id;
241 			goto out;
242 
243 		case MUX_E_MUX_SESSION_CLOSE:
244 			/* Release an IP session. */
245 			ipc_mux->event = MUX_E_MUX_SESSION_CLOSE;
246 			ipc_mux_session_close(ipc_mux, &msg->session_close);
247 			ret = ipc_mux->channel_id;
248 			goto out;
249 
250 		case MUX_E_MUX_CHANNEL_CLOSE:
251 			/* Close the MUX channel pipes. */
252 			ipc_mux->event = MUX_E_MUX_CHANNEL_CLOSE;
253 			ipc_mux_channel_close(ipc_mux, &msg->channel_close);
254 			ret = ipc_mux->channel_id;
255 			goto out;
256 
257 		default:
258 			/* Invalid order. */
259 			goto out;
260 		}
261 
262 	default:
263 		dev_err(ipc_mux->dev,
264 			"unexpected MUX transition: state=%d, event=%d",
265 			ipc_mux->state, ipc_mux->event);
266 	}
267 out:
268 	return ret;
269 }
270 
271 struct iosm_mux *ipc_mux_init(struct ipc_mux_config *mux_cfg,
272 			      struct iosm_imem *imem)
273 {
274 	struct iosm_mux *ipc_mux = kzalloc(sizeof(*ipc_mux), GFP_KERNEL);
275 	int i, ul_tds, ul_td_size;
276 	struct sk_buff_head *free_list;
277 	struct sk_buff *skb;
278 
279 	if (!ipc_mux)
280 		return NULL;
281 
282 	ipc_mux->protocol = mux_cfg->protocol;
283 	ipc_mux->ul_flow = mux_cfg->ul_flow;
284 	ipc_mux->nr_sessions = mux_cfg->nr_sessions;
285 	ipc_mux->instance_id = mux_cfg->instance_id;
286 	ipc_mux->wwan_q_offset = 0;
287 
288 	ipc_mux->pcie = imem->pcie;
289 	ipc_mux->imem = imem;
290 	ipc_mux->ipc_protocol = imem->ipc_protocol;
291 	ipc_mux->dev = imem->dev;
292 	ipc_mux->wwan = imem->wwan;
293 
294 	/* Get the reference to the UL ADB list. */
295 	free_list = &ipc_mux->ul_adb.free_list;
296 
297 	/* Initialize the list with free ADB. */
298 	skb_queue_head_init(free_list);
299 
300 	ul_td_size = IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE;
301 
302 	ul_tds = IPC_MEM_MAX_TDS_MUX_LITE_UL;
303 
304 	ipc_mux->ul_adb.dest_skb = NULL;
305 
306 	ipc_mux->initialized = true;
307 	ipc_mux->adb_prep_ongoing = false;
308 	ipc_mux->size_needed = 0;
309 	ipc_mux->ul_data_pend_bytes = 0;
310 	ipc_mux->state = MUX_S_INACTIVE;
311 	ipc_mux->ev_mux_net_transmit_pending = false;
312 	ipc_mux->tx_transaction_id = 0;
313 	ipc_mux->rr_next_session = 0;
314 	ipc_mux->event = MUX_E_INACTIVE;
315 	ipc_mux->channel_id = -1;
316 	ipc_mux->channel = NULL;
317 
318 	/* Allocate the list of UL ADB. */
319 	for (i = 0; i < ul_tds; i++) {
320 		dma_addr_t mapping;
321 
322 		skb = ipc_pcie_alloc_skb(ipc_mux->pcie, ul_td_size, GFP_ATOMIC,
323 					 &mapping, DMA_TO_DEVICE, 0);
324 		if (!skb) {
325 			ipc_mux_deinit(ipc_mux);
326 			return NULL;
327 		}
328 		/* Extend the UL ADB list. */
329 		skb_queue_tail(free_list, skb);
330 	}
331 
332 	return ipc_mux;
333 }
334 
335 /* Informs the network stack to restart transmission for all opened session if
336  * Flow Control is not ON for that session.
337  */
338 static void ipc_mux_restart_tx_for_all_sessions(struct iosm_mux *ipc_mux)
339 {
340 	struct mux_session *session;
341 	int idx;
342 
343 	for (idx = 0; idx < ipc_mux->nr_sessions; idx++) {
344 		session = &ipc_mux->session[idx];
345 
346 		if (!session->wwan)
347 			continue;
348 
349 		/* If flow control of the session is OFF and if there was tx
350 		 * stop then restart. Inform the network interface to restart
351 		 * sending data.
352 		 */
353 		if (session->flow_ctl_mask == 0) {
354 			session->net_tx_stop = false;
355 			ipc_mux_netif_tx_flowctrl(session, idx, false);
356 		}
357 	}
358 }
359 
360 /* Informs the network stack to stop sending further pkt for all opened
361  * sessions
362  */
363 static void ipc_mux_stop_netif_for_all_sessions(struct iosm_mux *ipc_mux)
364 {
365 	struct mux_session *session;
366 	int idx;
367 
368 	for (idx = 0; idx < ipc_mux->nr_sessions; idx++) {
369 		session = &ipc_mux->session[idx];
370 
371 		if (!session->wwan)
372 			continue;
373 
374 		ipc_mux_netif_tx_flowctrl(session, session->if_id, true);
375 	}
376 }
377 
378 void ipc_mux_check_n_restart_tx(struct iosm_mux *ipc_mux)
379 {
380 	if (ipc_mux->ul_flow == MUX_UL) {
381 		int low_thresh = IPC_MEM_MUX_UL_FLOWCTRL_LOW_B;
382 
383 		if (ipc_mux->ul_data_pend_bytes < low_thresh)
384 			ipc_mux_restart_tx_for_all_sessions(ipc_mux);
385 	}
386 }
387 
388 int ipc_mux_get_max_sessions(struct iosm_mux *ipc_mux)
389 {
390 	return ipc_mux ? ipc_mux->nr_sessions : -EFAULT;
391 }
392 
393 enum ipc_mux_protocol ipc_mux_get_active_protocol(struct iosm_mux *ipc_mux)
394 {
395 	return ipc_mux ? ipc_mux->protocol : MUX_UNKNOWN;
396 }
397 
398 int ipc_mux_open_session(struct iosm_mux *ipc_mux, int session_nr)
399 {
400 	struct mux_session_open *session_open;
401 	union mux_msg mux_msg;
402 
403 	session_open = &mux_msg.session_open;
404 	session_open->event = MUX_E_MUX_SESSION_OPEN;
405 
406 	session_open->if_id = cpu_to_le32(session_nr);
407 	ipc_mux->session[session_nr].flags |= IPC_MEM_WWAN_MUX;
408 	return ipc_mux_schedule(ipc_mux, &mux_msg);
409 }
410 
411 int ipc_mux_close_session(struct iosm_mux *ipc_mux, int session_nr)
412 {
413 	struct mux_session_close *session_close;
414 	union mux_msg mux_msg;
415 	int ret_val;
416 
417 	session_close = &mux_msg.session_close;
418 	session_close->event = MUX_E_MUX_SESSION_CLOSE;
419 
420 	session_close->if_id = cpu_to_le32(session_nr);
421 	ret_val = ipc_mux_schedule(ipc_mux, &mux_msg);
422 	ipc_mux->session[session_nr].flags &= ~IPC_MEM_WWAN_MUX;
423 
424 	return ret_val;
425 }
426 
427 void ipc_mux_deinit(struct iosm_mux *ipc_mux)
428 {
429 	struct mux_channel_close *channel_close;
430 	struct sk_buff_head *free_list;
431 	union mux_msg mux_msg;
432 	struct sk_buff *skb;
433 
434 	if (!ipc_mux->initialized)
435 		return;
436 	ipc_mux_stop_netif_for_all_sessions(ipc_mux);
437 
438 	channel_close = &mux_msg.channel_close;
439 	channel_close->event = MUX_E_MUX_CHANNEL_CLOSE;
440 	ipc_mux_schedule(ipc_mux, &mux_msg);
441 
442 	/* Empty the ADB free list. */
443 	free_list = &ipc_mux->ul_adb.free_list;
444 
445 	/* Remove from the head of the downlink queue. */
446 	while ((skb = skb_dequeue(free_list)))
447 		ipc_pcie_kfree_skb(ipc_mux->pcie, skb);
448 
449 	if (ipc_mux->channel) {
450 		ipc_mux->channel->ul_pipe.is_open = false;
451 		ipc_mux->channel->dl_pipe.is_open = false;
452 	}
453 
454 	kfree(ipc_mux);
455 }
456