xref: /linux/drivers/net/wwan/iosm/iosm_ipc_mux_codec.c (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-21 Intel Corporation.
4  */
5 
6 #include <linux/nospec.h>
7 
8 #include "iosm_ipc_imem_ops.h"
9 #include "iosm_ipc_mux_codec.h"
10 #include "iosm_ipc_task_queue.h"
11 
12 /* Test the link power state and send a MUX command in blocking mode. */
ipc_mux_tq_cmd_send(struct iosm_imem * ipc_imem,int arg,void * msg,size_t size)13 static int ipc_mux_tq_cmd_send(struct iosm_imem *ipc_imem, int arg, void *msg,
14 			       size_t size)
15 {
16 	struct iosm_mux *ipc_mux = ipc_imem->mux;
17 	const struct mux_acb *acb = msg;
18 
19 	skb_queue_tail(&ipc_mux->channel->ul_list, acb->skb);
20 	ipc_imem_ul_send(ipc_mux->imem);
21 
22 	return 0;
23 }
24 
ipc_mux_acb_send(struct iosm_mux * ipc_mux,bool blocking)25 static int ipc_mux_acb_send(struct iosm_mux *ipc_mux, bool blocking)
26 {
27 	struct completion *completion = &ipc_mux->channel->ul_sem;
28 	int ret = ipc_task_queue_send_task(ipc_mux->imem, ipc_mux_tq_cmd_send,
29 					   0, &ipc_mux->acb,
30 					   sizeof(ipc_mux->acb), false);
31 	if (ret) {
32 		dev_err(ipc_mux->dev, "unable to send mux command");
33 		return ret;
34 	}
35 
36 	/* if blocking, suspend the app and wait for irq in the flash or
37 	 * crash phase. return false on timeout to indicate failure.
38 	 */
39 	if (blocking) {
40 		u32 wait_time_milliseconds = IPC_MUX_CMD_RUN_DEFAULT_TIMEOUT;
41 
42 		reinit_completion(completion);
43 
44 		if (wait_for_completion_interruptible_timeout
45 		   (completion, msecs_to_jiffies(wait_time_milliseconds)) ==
46 		   0) {
47 			dev_err(ipc_mux->dev, "ch[%d] timeout",
48 				ipc_mux->channel_id);
49 			ipc_uevent_send(ipc_mux->imem->dev, UEVENT_MDM_TIMEOUT);
50 			return -ETIMEDOUT;
51 		}
52 	}
53 
54 	return 0;
55 }
56 
57 /* Initialize the command header. */
ipc_mux_acb_init(struct iosm_mux * ipc_mux)58 static void ipc_mux_acb_init(struct iosm_mux *ipc_mux)
59 {
60 	struct mux_acb *acb = &ipc_mux->acb;
61 	struct mux_acbh *header;
62 
63 	header = (struct mux_acbh *)(acb->skb)->data;
64 	header->block_length = cpu_to_le32(sizeof(struct mux_acbh));
65 	header->first_cmd_index = header->block_length;
66 	header->signature = cpu_to_le32(IOSM_AGGR_MUX_SIG_ACBH);
67 	header->sequence_nr = cpu_to_le16(ipc_mux->acb_tx_sequence_nr++);
68 }
69 
70 /* Add a command to the ACB. */
ipc_mux_acb_add_cmd(struct iosm_mux * ipc_mux,u32 cmd,void * param,u32 param_size)71 static struct mux_cmdh *ipc_mux_acb_add_cmd(struct iosm_mux *ipc_mux, u32 cmd,
72 					    void *param, u32 param_size)
73 {
74 	struct mux_acbh *header;
75 	struct mux_cmdh *cmdh;
76 	struct mux_acb *acb;
77 
78 	acb = &ipc_mux->acb;
79 	header = (struct mux_acbh *)(acb->skb)->data;
80 	cmdh = (struct mux_cmdh *)
81 		((acb->skb)->data + le32_to_cpu(header->block_length));
82 
83 	cmdh->signature = cpu_to_le32(MUX_SIG_CMDH);
84 	cmdh->command_type = cpu_to_le32(cmd);
85 	cmdh->if_id = acb->if_id;
86 
87 	acb->cmd = cmd;
88 	cmdh->cmd_len = cpu_to_le16(offsetof(struct mux_cmdh, param) +
89 				    param_size);
90 	cmdh->transaction_id = cpu_to_le32(ipc_mux->tx_transaction_id++);
91 	if (param)
92 		memcpy(&cmdh->param, param, param_size);
93 
94 	skb_put(acb->skb, le32_to_cpu(header->block_length) +
95 					le16_to_cpu(cmdh->cmd_len));
96 
97 	return cmdh;
98 }
99 
100 /* Prepare mux Command */
ipc_mux_lite_add_cmd(struct iosm_mux * ipc_mux,u32 cmd,struct mux_acb * acb,void * param,u32 param_size)101 static struct mux_lite_cmdh *ipc_mux_lite_add_cmd(struct iosm_mux *ipc_mux,
102 						  u32 cmd, struct mux_acb *acb,
103 						  void *param, u32 param_size)
104 {
105 	struct mux_lite_cmdh *cmdh = (struct mux_lite_cmdh *)acb->skb->data;
106 
107 	cmdh->signature = cpu_to_le32(MUX_SIG_CMDH);
108 	cmdh->command_type = cpu_to_le32(cmd);
109 	cmdh->if_id = acb->if_id;
110 
111 	acb->cmd = cmd;
112 
113 	cmdh->cmd_len = cpu_to_le16(offsetof(struct mux_lite_cmdh, param) +
114 				    param_size);
115 	cmdh->transaction_id = cpu_to_le32(ipc_mux->tx_transaction_id++);
116 
117 	if (param)
118 		memcpy(&cmdh->param, param, param_size);
119 
120 	skb_put(acb->skb, le16_to_cpu(cmdh->cmd_len));
121 
122 	return cmdh;
123 }
124 
ipc_mux_acb_alloc(struct iosm_mux * ipc_mux)125 static int ipc_mux_acb_alloc(struct iosm_mux *ipc_mux)
126 {
127 	struct mux_acb *acb = &ipc_mux->acb;
128 	struct sk_buff *skb;
129 	dma_addr_t mapping;
130 
131 	/* Allocate skb memory for the uplink buffer. */
132 	skb = ipc_pcie_alloc_skb(ipc_mux->pcie, MUX_MAX_UL_ACB_BUF_SIZE,
133 				 GFP_ATOMIC, &mapping, DMA_TO_DEVICE, 0);
134 	if (!skb)
135 		return -ENOMEM;
136 
137 	/* Save the skb address. */
138 	acb->skb = skb;
139 
140 	memset(skb->data, 0, MUX_MAX_UL_ACB_BUF_SIZE);
141 
142 	return 0;
143 }
144 
ipc_mux_dl_acb_send_cmds(struct iosm_mux * ipc_mux,u32 cmd_type,u8 if_id,u32 transaction_id,union mux_cmd_param * param,size_t res_size,bool blocking,bool respond)145 int ipc_mux_dl_acb_send_cmds(struct iosm_mux *ipc_mux, u32 cmd_type, u8 if_id,
146 			     u32 transaction_id, union mux_cmd_param *param,
147 			     size_t res_size, bool blocking, bool respond)
148 {
149 	struct mux_acb *acb = &ipc_mux->acb;
150 	union mux_type_cmdh cmdh;
151 	int ret = 0;
152 
153 	acb->if_id = if_id;
154 	ret = ipc_mux_acb_alloc(ipc_mux);
155 	if (ret)
156 		return ret;
157 
158 	if (ipc_mux->protocol == MUX_LITE) {
159 		cmdh.ack_lite = ipc_mux_lite_add_cmd(ipc_mux, cmd_type, acb,
160 						     param, res_size);
161 
162 		if (respond)
163 			cmdh.ack_lite->transaction_id =
164 					cpu_to_le32(transaction_id);
165 	} else {
166 		/* Initialize the ACB header. */
167 		ipc_mux_acb_init(ipc_mux);
168 		cmdh.ack_aggr = ipc_mux_acb_add_cmd(ipc_mux, cmd_type, param,
169 						    res_size);
170 
171 		if (respond)
172 			cmdh.ack_aggr->transaction_id =
173 					cpu_to_le32(transaction_id);
174 	}
175 	ret = ipc_mux_acb_send(ipc_mux, blocking);
176 
177 	return ret;
178 }
179 
ipc_mux_netif_tx_flowctrl(struct mux_session * session,int idx,bool on)180 void ipc_mux_netif_tx_flowctrl(struct mux_session *session, int idx, bool on)
181 {
182 	/* Inform the network interface to start/stop flow ctrl */
183 	ipc_wwan_tx_flowctrl(session->wwan, idx, on);
184 }
185 
ipc_mux_dl_cmdresps_decode_process(struct iosm_mux * ipc_mux,union mux_cmd_param param,__le32 command_type,u8 if_id,__le32 transaction_id)186 static int ipc_mux_dl_cmdresps_decode_process(struct iosm_mux *ipc_mux,
187 					      union mux_cmd_param param,
188 					      __le32 command_type, u8 if_id,
189 					      __le32 transaction_id)
190 {
191 	struct mux_acb *acb = &ipc_mux->acb;
192 
193 	switch (le32_to_cpu(command_type)) {
194 	case MUX_CMD_OPEN_SESSION_RESP:
195 	case MUX_CMD_CLOSE_SESSION_RESP:
196 		/* Resume the control application. */
197 		acb->got_param = param;
198 		break;
199 
200 	case MUX_LITE_CMD_FLOW_CTL_ACK:
201 		/* This command type is not expected as response for
202 		 * Aggregation version of the protocol. So return non-zero.
203 		 */
204 		if (ipc_mux->protocol != MUX_LITE)
205 			return -EINVAL;
206 
207 		dev_dbg(ipc_mux->dev, "if_id %u FLOW_CTL_ACK %u received",
208 			if_id, le32_to_cpu(transaction_id));
209 		break;
210 
211 	case IOSM_AGGR_MUX_CMD_FLOW_CTL_ACK:
212 		/* This command type is not expected as response for
213 		 * Lite version of the protocol. So return non-zero.
214 		 */
215 		if (ipc_mux->protocol == MUX_LITE)
216 			return -EINVAL;
217 		break;
218 
219 	default:
220 		return -EINVAL;
221 	}
222 
223 	acb->wanted_response = MUX_CMD_INVALID;
224 	acb->got_response = le32_to_cpu(command_type);
225 	complete(&ipc_mux->channel->ul_sem);
226 
227 	return 0;
228 }
229 
ipc_mux_dl_cmds_decode_process(struct iosm_mux * ipc_mux,union mux_cmd_param * param,__le32 command_type,u8 if_id,__le16 cmd_len,int size)230 static int ipc_mux_dl_cmds_decode_process(struct iosm_mux *ipc_mux,
231 					  union mux_cmd_param *param,
232 					  __le32 command_type, u8 if_id,
233 					  __le16 cmd_len, int size)
234 {
235 	struct mux_session *session;
236 	struct hrtimer *adb_timer;
237 
238 	dev_dbg(ipc_mux->dev, "if_id[%d]: dlcmds decode process %d",
239 		if_id, le32_to_cpu(command_type));
240 
241 	switch (le32_to_cpu(command_type)) {
242 	case MUX_LITE_CMD_FLOW_CTL:
243 	case IOSM_AGGR_MUX_CMD_FLOW_CTL_DISABLE:
244 
245 		if (if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
246 			dev_err(ipc_mux->dev, "if_id [%d] not valid",
247 				if_id);
248 			return -EINVAL; /* No session interface id. */
249 		}
250 
251 		session = &ipc_mux->session[if_id];
252 		adb_timer = &ipc_mux->imem->adb_timer;
253 
254 		if (param->flow_ctl.mask == cpu_to_le32(0xFFFFFFFF)) {
255 			/* Backward Compatibility */
256 			if (cmd_len == cpu_to_le16(size))
257 				session->flow_ctl_mask =
258 					le32_to_cpu(param->flow_ctl.mask);
259 			else
260 				session->flow_ctl_mask = ~0;
261 			/* if CP asks for FLOW CTRL Enable
262 			 * then set our internal flow control Tx flag
263 			 * to limit uplink session queueing
264 			 */
265 			session->net_tx_stop = true;
266 
267 			/* We have to call Finish ADB here.
268 			 * Otherwise any already queued data
269 			 * will be sent to CP when ADB is full
270 			 * for some other sessions.
271 			 */
272 			if (ipc_mux->protocol == MUX_AGGREGATION) {
273 				ipc_mux_ul_adb_finish(ipc_mux);
274 				ipc_imem_hrtimer_stop(adb_timer);
275 			}
276 			/* Update the stats */
277 			session->flow_ctl_en_cnt++;
278 		} else if (param->flow_ctl.mask == 0) {
279 			/* Just reset the Flow control mask and let
280 			 * mux_flow_ctrl_low_thre_b take control on
281 			 * our internal Tx flag and enabling kernel
282 			 * flow control
283 			 */
284 			dev_dbg(ipc_mux->dev, "if_id[%u] flow_ctl mask 0x%08X",
285 				if_id, le32_to_cpu(param->flow_ctl.mask));
286 			/* Backward Compatibility */
287 			if (cmd_len == cpu_to_le16(size))
288 				session->flow_ctl_mask =
289 					le32_to_cpu(param->flow_ctl.mask);
290 			else
291 				session->flow_ctl_mask = 0;
292 			/* Update the stats */
293 			session->flow_ctl_dis_cnt++;
294 		} else {
295 			break;
296 		}
297 
298 		ipc_mux->acc_adb_size = 0;
299 		ipc_mux->acc_payload_size = 0;
300 
301 		dev_dbg(ipc_mux->dev, "if_id[%u] FLOW CTRL 0x%08X", if_id,
302 			le32_to_cpu(param->flow_ctl.mask));
303 		break;
304 
305 	case MUX_LITE_CMD_LINK_STATUS_REPORT:
306 		break;
307 
308 	default:
309 		return -EINVAL;
310 	}
311 	return 0;
312 }
313 
314 /* Decode and Send appropriate response to a command block. */
ipc_mux_dl_cmd_decode(struct iosm_mux * ipc_mux,struct sk_buff * skb)315 static void ipc_mux_dl_cmd_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
316 {
317 	struct mux_lite_cmdh *cmdh = (struct mux_lite_cmdh *)skb->data;
318 	__le32 trans_id = cmdh->transaction_id;
319 	int size;
320 
321 	if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, cmdh->param,
322 					       cmdh->command_type, cmdh->if_id,
323 					       cmdh->transaction_id)) {
324 		/* Unable to decode command response indicates the cmd_type
325 		 * may be a command instead of response. So try to decoding it.
326 		 */
327 		size = offsetof(struct mux_lite_cmdh, param) +
328 				sizeof(cmdh->param.flow_ctl);
329 		if (!ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param,
330 						    cmdh->command_type,
331 						    cmdh->if_id,
332 						    cmdh->cmd_len, size)) {
333 			/* Decoded command may need a response. Give the
334 			 * response according to the command type.
335 			 */
336 			union mux_cmd_param *mux_cmd = NULL;
337 			size_t size = 0;
338 			u32 cmd = MUX_LITE_CMD_LINK_STATUS_REPORT_RESP;
339 
340 			if (cmdh->command_type ==
341 			    cpu_to_le32(MUX_LITE_CMD_LINK_STATUS_REPORT)) {
342 				mux_cmd = &cmdh->param;
343 				mux_cmd->link_status_resp.response =
344 					cpu_to_le32(MUX_CMD_RESP_SUCCESS);
345 				/* response field is u32 */
346 				size = sizeof(u32);
347 			} else if (cmdh->command_type ==
348 				   cpu_to_le32(MUX_LITE_CMD_FLOW_CTL)) {
349 				cmd = MUX_LITE_CMD_FLOW_CTL_ACK;
350 			} else {
351 				return;
352 			}
353 
354 			if (ipc_mux_dl_acb_send_cmds(ipc_mux, cmd, cmdh->if_id,
355 						     le32_to_cpu(trans_id),
356 						     mux_cmd, size, false,
357 						     true))
358 				dev_err(ipc_mux->dev,
359 					"if_id %d: cmd send failed",
360 					cmdh->if_id);
361 		}
362 	}
363 }
364 
365 /* Pass the DL packet to the netif layer. */
ipc_mux_net_receive(struct iosm_mux * ipc_mux,int if_id,struct iosm_wwan * wwan,u32 offset,u8 service_class,struct sk_buff * skb,u32 pkt_len)366 static int ipc_mux_net_receive(struct iosm_mux *ipc_mux, int if_id,
367 			       struct iosm_wwan *wwan, u32 offset,
368 			       u8 service_class, struct sk_buff *skb,
369 			       u32 pkt_len)
370 {
371 	struct sk_buff *dest_skb = skb_clone(skb, GFP_ATOMIC);
372 
373 	if (!dest_skb)
374 		return -ENOMEM;
375 
376 	skb_pull(dest_skb, offset);
377 	skb_trim(dest_skb, pkt_len);
378 	/* Pass the packet to the netif layer. */
379 	dest_skb->priority = service_class;
380 
381 	return ipc_wwan_receive(wwan, dest_skb, false, if_id);
382 }
383 
384 /* Decode Flow Credit Table in the block */
ipc_mux_dl_fcth_decode(struct iosm_mux * ipc_mux,unsigned char * block)385 static void ipc_mux_dl_fcth_decode(struct iosm_mux *ipc_mux,
386 				   unsigned char *block)
387 {
388 	struct ipc_mem_lite_gen_tbl *fct = (struct ipc_mem_lite_gen_tbl *)block;
389 	struct iosm_wwan *wwan;
390 	int ul_credits;
391 	int if_id;
392 
393 	if (fct->vfl_length != sizeof(fct->vfl.nr_of_bytes)) {
394 		dev_err(ipc_mux->dev, "unexpected FCT length: %d",
395 			fct->vfl_length);
396 		return;
397 	}
398 
399 	if_id = fct->if_id;
400 	if (if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
401 		dev_err(ipc_mux->dev, "not supported if_id: %d", if_id);
402 		return;
403 	}
404 
405 	/* Is the session active ? */
406 	if_id = array_index_nospec(if_id, IPC_MEM_MUX_IP_SESSION_ENTRIES);
407 	wwan = ipc_mux->session[if_id].wwan;
408 	if (!wwan) {
409 		dev_err(ipc_mux->dev, "session Net ID is NULL");
410 		return;
411 	}
412 
413 	ul_credits = le32_to_cpu(fct->vfl.nr_of_bytes);
414 
415 	dev_dbg(ipc_mux->dev, "Flow_Credit:: if_id[%d] Old: %d Grants: %d",
416 		if_id, ipc_mux->session[if_id].ul_flow_credits, ul_credits);
417 
418 	/* Update the Flow Credit information from ADB */
419 	ipc_mux->session[if_id].ul_flow_credits += ul_credits;
420 
421 	/* Check whether the TX can be started */
422 	if (ipc_mux->session[if_id].ul_flow_credits > 0) {
423 		ipc_mux->session[if_id].net_tx_stop = false;
424 		ipc_mux_netif_tx_flowctrl(&ipc_mux->session[if_id],
425 					  ipc_mux->session[if_id].if_id, false);
426 	}
427 }
428 
429 /* Decode non-aggregated datagram */
ipc_mux_dl_adgh_decode(struct iosm_mux * ipc_mux,struct sk_buff * skb)430 static void ipc_mux_dl_adgh_decode(struct iosm_mux *ipc_mux,
431 				   struct sk_buff *skb)
432 {
433 	u32 pad_len, packet_offset, adgh_len;
434 	struct iosm_wwan *wwan;
435 	struct mux_adgh *adgh;
436 	u8 *block = skb->data;
437 	int rc = 0;
438 	u8 if_id;
439 
440 	adgh = (struct mux_adgh *)block;
441 
442 	if (adgh->signature != cpu_to_le32(IOSM_AGGR_MUX_SIG_ADGH)) {
443 		dev_err(ipc_mux->dev, "invalid ADGH signature received");
444 		return;
445 	}
446 
447 	if_id = adgh->if_id;
448 	if (if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES) {
449 		dev_err(ipc_mux->dev, "invalid if_id while decoding %d", if_id);
450 		return;
451 	}
452 
453 	/* Is the session active ? */
454 	if_id = array_index_nospec(if_id, IPC_MEM_MUX_IP_SESSION_ENTRIES);
455 	wwan = ipc_mux->session[if_id].wwan;
456 	if (!wwan) {
457 		dev_err(ipc_mux->dev, "session Net ID is NULL");
458 		return;
459 	}
460 
461 	/* Store the pad len for the corresponding session
462 	 * Pad bytes as negotiated in the open session less the header size
463 	 * (see session management chapter for details).
464 	 * If resulting padding is zero or less, the additional head padding is
465 	 * omitted. For e.g., if HEAD_PAD_LEN = 16 or less, this field is
466 	 * omitted if HEAD_PAD_LEN = 20, then this field will have 4 bytes
467 	 * set to zero
468 	 */
469 	pad_len =
470 		ipc_mux->session[if_id].dl_head_pad_len - IPC_MEM_DL_ETH_OFFSET;
471 	packet_offset = sizeof(*adgh) + pad_len;
472 
473 	if_id += ipc_mux->wwan_q_offset;
474 	adgh_len = le16_to_cpu(adgh->length);
475 
476 	/* Pass the packet to the netif layer */
477 	rc = ipc_mux_net_receive(ipc_mux, if_id, wwan, packet_offset,
478 				 adgh->service_class, skb,
479 				 adgh_len - packet_offset);
480 	if (rc) {
481 		dev_err(ipc_mux->dev, "mux adgh decoding error");
482 		return;
483 	}
484 	ipc_mux->session[if_id].flush = 1;
485 }
486 
ipc_mux_dl_acbcmd_decode(struct iosm_mux * ipc_mux,struct mux_cmdh * cmdh,int size)487 static void ipc_mux_dl_acbcmd_decode(struct iosm_mux *ipc_mux,
488 				     struct mux_cmdh *cmdh, int size)
489 {
490 	u32 link_st  = IOSM_AGGR_MUX_CMD_LINK_STATUS_REPORT_RESP;
491 	u32 fctl_dis = IOSM_AGGR_MUX_CMD_FLOW_CTL_DISABLE;
492 	u32 fctl_ena = IOSM_AGGR_MUX_CMD_FLOW_CTL_ENABLE;
493 	u32 fctl_ack = IOSM_AGGR_MUX_CMD_FLOW_CTL_ACK;
494 	union mux_cmd_param *cmd_p = NULL;
495 	u32 cmd = link_st;
496 	u32 trans_id;
497 
498 	if (!ipc_mux_dl_cmds_decode_process(ipc_mux, &cmdh->param,
499 					    cmdh->command_type, cmdh->if_id,
500 					    cmdh->cmd_len, size)) {
501 		size = 0;
502 		if (cmdh->command_type == cpu_to_le32(link_st)) {
503 			cmd_p = &cmdh->param;
504 			cmd_p->link_status_resp.response = MUX_CMD_RESP_SUCCESS;
505 		} else if ((cmdh->command_type == cpu_to_le32(fctl_ena)) ||
506 				(cmdh->command_type == cpu_to_le32(fctl_dis))) {
507 			cmd = fctl_ack;
508 		} else {
509 			return;
510 			}
511 		trans_id = le32_to_cpu(cmdh->transaction_id);
512 		ipc_mux_dl_acb_send_cmds(ipc_mux, cmd, cmdh->if_id,
513 					 trans_id, cmd_p, size, false, true);
514 	}
515 }
516 
517 /* Decode an aggregated command block. */
ipc_mux_dl_acb_decode(struct iosm_mux * ipc_mux,struct sk_buff * skb)518 static void ipc_mux_dl_acb_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
519 {
520 	struct mux_acbh *acbh;
521 	struct mux_cmdh *cmdh;
522 	u32 next_cmd_index;
523 	u8 *block;
524 	int size;
525 
526 	acbh = (struct mux_acbh *)(skb->data);
527 	block = (u8 *)(skb->data);
528 
529 	next_cmd_index = le32_to_cpu(acbh->first_cmd_index);
530 	next_cmd_index = array_index_nospec(next_cmd_index,
531 					    sizeof(struct mux_cmdh));
532 
533 	while (next_cmd_index != 0) {
534 		cmdh = (struct mux_cmdh *)&block[next_cmd_index];
535 		next_cmd_index = le32_to_cpu(cmdh->next_cmd_index);
536 		if (ipc_mux_dl_cmdresps_decode_process(ipc_mux, cmdh->param,
537 						       cmdh->command_type,
538 						       cmdh->if_id,
539 						       cmdh->transaction_id)) {
540 			size = offsetof(struct mux_cmdh, param) +
541 				sizeof(cmdh->param.flow_ctl);
542 			ipc_mux_dl_acbcmd_decode(ipc_mux, cmdh, size);
543 		}
544 	}
545 }
546 
547 /* process datagram */
mux_dl_process_dg(struct iosm_mux * ipc_mux,struct mux_adbh * adbh,struct mux_adth_dg * dg,struct sk_buff * skb,int if_id,int nr_of_dg)548 static int mux_dl_process_dg(struct iosm_mux *ipc_mux, struct mux_adbh *adbh,
549 			     struct mux_adth_dg *dg, struct sk_buff *skb,
550 			     int if_id, int nr_of_dg)
551 {
552 	u32 dl_head_pad_len = ipc_mux->session[if_id].dl_head_pad_len;
553 	u32 packet_offset, i, rc, dg_len;
554 
555 	for (i = 0; i < nr_of_dg; i++, dg++) {
556 		u32 dg_index = le32_to_cpu(dg->datagram_index);
557 
558 		dg_len = le16_to_cpu(dg->datagram_length);
559 
560 		if (dg_index < sizeof(struct mux_adbh))
561 			goto dg_error;
562 
563 		/* Is the packet inside of the ADB and the received skb ? */
564 		if (dg_index >= le32_to_cpu(adbh->block_length) ||
565 		    dg_index >= skb->len ||
566 		    dg_len > skb->len - dg_index ||
567 		    dl_head_pad_len >= dg_len) {
568 			goto dg_error;
569 		} else {
570 			packet_offset = dg_index + dl_head_pad_len;
571 			/* Pass the packet to the netif layer. */
572 			rc = ipc_mux_net_receive(ipc_mux, if_id, ipc_mux->wwan,
573 						 packet_offset,
574 						 dg->service_class, skb,
575 						 dg_len - dl_head_pad_len);
576 			if (rc)
577 				goto dg_error;
578 		}
579 	}
580 	return 0;
581 dg_error:
582 	return -1;
583 }
584 
585 /* Decode an aggregated data block. */
mux_dl_adb_decode(struct iosm_mux * ipc_mux,struct sk_buff * skb)586 static void mux_dl_adb_decode(struct iosm_mux *ipc_mux,
587 			      struct sk_buff *skb)
588 {
589 	struct mux_adth_dg *dg;
590 	struct iosm_wwan *wwan;
591 	struct mux_adbh *adbh;
592 	struct mux_adth *adth;
593 	int nr_of_dg, if_id;
594 	u32 adth_index, prev_index = 0;
595 	u8 *block;
596 
597 	block = skb->data;
598 	adbh = (struct mux_adbh *)block;
599 
600 	/* The block header itself must fit in the received skb. */
601 	if (skb->len < sizeof(struct mux_adbh))
602 		goto adb_decode_err;
603 
604 	/* Process the aggregated datagram tables. */
605 	adth_index = le32_to_cpu(adbh->first_table_index);
606 
607 	/* Has CP sent an empty ADB ? */
608 	if (adth_index < 1) {
609 		dev_err(ipc_mux->dev, "unexpected empty ADB");
610 		goto adb_decode_err;
611 	}
612 
613 	/* Loop through mixed session tables. */
614 	while (adth_index) {
615 		/* The table header must lie within the received skb, and the
616 		 * chain must move forward so a modem cannot make the loop
617 		 * cycle between two tables.
618 		 */
619 		if (adth_index <= prev_index ||
620 		    adth_index < sizeof(struct mux_adbh) ||
621 		    adth_index > skb->len - sizeof(struct mux_adth))
622 			goto adb_decode_err;
623 		prev_index = adth_index;
624 
625 		/* Get the reference to the table header. */
626 		adth = (struct mux_adth *)(block + adth_index);
627 
628 		/* Get the interface id and map it to the netif id. */
629 		if_id = adth->if_id;
630 		if (if_id >= IPC_MEM_MUX_IP_SESSION_ENTRIES)
631 			goto adb_decode_err;
632 
633 		if_id = array_index_nospec(if_id,
634 					   IPC_MEM_MUX_IP_SESSION_ENTRIES);
635 
636 		/* Is the session active ? */
637 		wwan = ipc_mux->session[if_id].wwan;
638 		if (!wwan)
639 			goto adb_decode_err;
640 
641 		/* Consistency checks for aggregated datagram table. */
642 		if (adth->signature != cpu_to_le32(IOSM_AGGR_MUX_SIG_ADTH))
643 			goto adb_decode_err;
644 
645 		if (le16_to_cpu(adth->table_length) < sizeof(struct mux_adth))
646 			goto adb_decode_err;
647 
648 		/* The whole datagram table must fit in the received skb. */
649 		if (le16_to_cpu(adth->table_length) > skb->len - adth_index)
650 			goto adb_decode_err;
651 
652 		/* Calculate the number of datagrams. */
653 		nr_of_dg = (le16_to_cpu(adth->table_length) -
654 					sizeof(struct mux_adth)) /
655 					sizeof(struct mux_adth_dg);
656 
657 		/* Is the datagram table empty ? */
658 		if (nr_of_dg < 1) {
659 			dev_err(ipc_mux->dev,
660 				"adthidx=%u,nr_of_dg=%d,next_tblidx=%u",
661 				adth_index, nr_of_dg,
662 				le32_to_cpu(adth->next_table_index));
663 
664 			/* Move to the next aggregated datagram table. */
665 			adth_index = le32_to_cpu(adth->next_table_index);
666 			continue;
667 		}
668 
669 		/* New aggregated datagram table. */
670 		dg = adth->dg;
671 		if (mux_dl_process_dg(ipc_mux, adbh, dg, skb, if_id,
672 				      nr_of_dg) < 0)
673 			goto adb_decode_err;
674 
675 		/* mark session for final flush */
676 		ipc_mux->session[if_id].flush = 1;
677 
678 		/* Move to the next aggregated datagram table. */
679 		adth_index = le32_to_cpu(adth->next_table_index);
680 	}
681 
682 adb_decode_err:
683 	return;
684 }
685 
686 /**
687  * ipc_mux_dl_decode -  Route the DL packet through the IP MUX layer
688  *                      depending on Header.
689  * @ipc_mux:            Pointer to MUX data-struct
690  * @skb:                Pointer to ipc_skb.
691  */
ipc_mux_dl_decode(struct iosm_mux * ipc_mux,struct sk_buff * skb)692 void ipc_mux_dl_decode(struct iosm_mux *ipc_mux, struct sk_buff *skb)
693 {
694 	u32 signature;
695 
696 	if (!skb->data)
697 		return;
698 
699 	/* Decode the MUX header type. */
700 	signature = le32_to_cpup((__le32 *)skb->data);
701 
702 	switch (signature) {
703 	case IOSM_AGGR_MUX_SIG_ADBH:	/* Aggregated Data Block Header */
704 		mux_dl_adb_decode(ipc_mux, skb);
705 		break;
706 	case IOSM_AGGR_MUX_SIG_ADGH:
707 		ipc_mux_dl_adgh_decode(ipc_mux, skb);
708 		break;
709 	case MUX_SIG_FCTH:
710 		ipc_mux_dl_fcth_decode(ipc_mux, skb->data);
711 		break;
712 	case IOSM_AGGR_MUX_SIG_ACBH:	/* Aggregated Command Block Header */
713 		ipc_mux_dl_acb_decode(ipc_mux, skb);
714 		break;
715 	case MUX_SIG_CMDH:
716 		ipc_mux_dl_cmd_decode(ipc_mux, skb);
717 		break;
718 
719 	default:
720 		dev_err(ipc_mux->dev, "invalid ABH signature");
721 	}
722 
723 	ipc_pcie_kfree_skb(ipc_mux->pcie, skb);
724 }
725 
ipc_mux_ul_skb_alloc(struct iosm_mux * ipc_mux,struct mux_adb * ul_adb,u32 type)726 static int ipc_mux_ul_skb_alloc(struct iosm_mux *ipc_mux,
727 				struct mux_adb *ul_adb, u32 type)
728 {
729 	/* Take the first element of the free list. */
730 	struct sk_buff *skb = skb_dequeue(&ul_adb->free_list);
731 	u32 no_if = IPC_MEM_MUX_IP_SESSION_ENTRIES;
732 	u32 *next_tb_id;
733 	int qlt_size;
734 	u32 if_id;
735 
736 	if (!skb)
737 		return -EBUSY; /* Wait for a free ADB skb. */
738 
739 	/* Mark it as UL ADB to select the right free operation. */
740 	IPC_CB(skb)->op_type = (u8)UL_MUX_OP_ADB;
741 
742 	switch (type) {
743 	case IOSM_AGGR_MUX_SIG_ADBH:
744 		/* Save the ADB memory settings. */
745 		ul_adb->dest_skb = skb;
746 		ul_adb->buf = skb->data;
747 		ul_adb->size = IPC_MEM_MAX_ADB_BUF_SIZE;
748 
749 		/* reset statistic counter */
750 		ul_adb->if_cnt = 0;
751 		ul_adb->payload_size = 0;
752 		ul_adb->dg_cnt_total = 0;
753 
754 		/* Initialize the ADBH. */
755 		ul_adb->adbh = (struct mux_adbh *)ul_adb->buf;
756 		memset(ul_adb->adbh, 0, sizeof(struct mux_adbh));
757 		ul_adb->adbh->signature = cpu_to_le32(IOSM_AGGR_MUX_SIG_ADBH);
758 		ul_adb->adbh->block_length =
759 					cpu_to_le32(sizeof(struct mux_adbh));
760 		next_tb_id = (unsigned int *)&ul_adb->adbh->first_table_index;
761 		ul_adb->next_table_index = next_tb_id;
762 
763 		/* Clear the local copy of DGs for new ADB */
764 		memset(ul_adb->dg, 0, sizeof(ul_adb->dg));
765 
766 		/* Clear the DG count and QLT updated status for new ADB */
767 		for (if_id = 0; if_id < no_if; if_id++) {
768 			ul_adb->dg_count[if_id] = 0;
769 			ul_adb->qlt_updated[if_id] = 0;
770 		}
771 		break;
772 
773 	case IOSM_AGGR_MUX_SIG_ADGH:
774 		/* Save the ADB memory settings. */
775 		ul_adb->dest_skb = skb;
776 		ul_adb->buf = skb->data;
777 		ul_adb->size = IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE;
778 		/* reset statistic counter */
779 		ul_adb->if_cnt = 0;
780 		ul_adb->payload_size = 0;
781 		ul_adb->dg_cnt_total = 0;
782 
783 		ul_adb->adgh = (struct mux_adgh *)skb->data;
784 		memset(ul_adb->adgh, 0, sizeof(struct mux_adgh));
785 		break;
786 
787 	case MUX_SIG_QLTH:
788 		qlt_size = offsetof(struct ipc_mem_lite_gen_tbl, vfl) +
789 			   (MUX_QUEUE_LEVEL * sizeof(struct mux_lite_vfl));
790 
791 		if (qlt_size > IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE) {
792 			dev_err(ipc_mux->dev,
793 				"can't support. QLT size:%d SKB size: %d",
794 				qlt_size, IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE);
795 			return -ERANGE;
796 		}
797 
798 		ul_adb->qlth_skb = skb;
799 		memset((ul_adb->qlth_skb)->data, 0, qlt_size);
800 		skb_put(skb, qlt_size);
801 		break;
802 	}
803 
804 	return 0;
805 }
806 
ipc_mux_ul_adgh_finish(struct iosm_mux * ipc_mux)807 static void ipc_mux_ul_adgh_finish(struct iosm_mux *ipc_mux)
808 {
809 	struct mux_adb *ul_adb = &ipc_mux->ul_adb;
810 	u16 adgh_len;
811 	long long bytes;
812 	char *str;
813 
814 	if (!ul_adb->dest_skb) {
815 		dev_err(ipc_mux->dev, "no dest skb");
816 		return;
817 	}
818 
819 	adgh_len = le16_to_cpu(ul_adb->adgh->length);
820 	skb_put(ul_adb->dest_skb, adgh_len);
821 	skb_queue_tail(&ipc_mux->channel->ul_list, ul_adb->dest_skb);
822 	ul_adb->dest_skb = NULL;
823 
824 	if (ipc_mux->ul_flow == MUX_UL_ON_CREDITS) {
825 		struct mux_session *session;
826 
827 		session = &ipc_mux->session[ul_adb->adgh->if_id];
828 		str = "available_credits";
829 		bytes = (long long)session->ul_flow_credits;
830 
831 	} else {
832 		str = "pend_bytes";
833 		bytes = ipc_mux->ul_data_pend_bytes;
834 		ipc_mux->ul_data_pend_bytes = ipc_mux->ul_data_pend_bytes +
835 					      adgh_len;
836 	}
837 
838 	dev_dbg(ipc_mux->dev, "UL ADGH: size=%u, if_id=%d, payload=%d, %s=%lld",
839 		adgh_len, ul_adb->adgh->if_id, ul_adb->payload_size,
840 		str, bytes);
841 }
842 
ipc_mux_ul_encode_adth(struct iosm_mux * ipc_mux,struct mux_adb * ul_adb,int * out_offset)843 static void ipc_mux_ul_encode_adth(struct iosm_mux *ipc_mux,
844 				   struct mux_adb *ul_adb, int *out_offset)
845 {
846 	int i, qlt_size, offset = *out_offset;
847 	struct mux_qlth *p_adb_qlt;
848 	struct mux_adth_dg *dg;
849 	struct mux_adth *adth;
850 	u16 adth_dg_size;
851 	u32 *next_tb_id;
852 
853 	qlt_size = offsetof(struct mux_qlth, ql) +
854 			MUX_QUEUE_LEVEL * sizeof(struct mux_qlth_ql);
855 
856 	for (i = 0; i < ipc_mux->nr_sessions; i++) {
857 		if (ul_adb->dg_count[i] > 0) {
858 			adth_dg_size = offsetof(struct mux_adth, dg) +
859 					ul_adb->dg_count[i] * sizeof(*dg);
860 
861 			*ul_adb->next_table_index = offset;
862 			adth = (struct mux_adth *)&ul_adb->buf[offset];
863 			next_tb_id = (unsigned int *)&adth->next_table_index;
864 			ul_adb->next_table_index = next_tb_id;
865 			offset += adth_dg_size;
866 			adth->signature = cpu_to_le32(IOSM_AGGR_MUX_SIG_ADTH);
867 			adth->if_id = i;
868 			adth->table_length = cpu_to_le16(adth_dg_size);
869 			adth_dg_size -= offsetof(struct mux_adth, dg);
870 			memcpy(adth->dg, ul_adb->dg[i], adth_dg_size);
871 			ul_adb->if_cnt++;
872 		}
873 
874 		if (ul_adb->qlt_updated[i]) {
875 			*ul_adb->next_table_index = offset;
876 			p_adb_qlt = (struct mux_qlth *)&ul_adb->buf[offset];
877 			ul_adb->next_table_index =
878 				(u32 *)&p_adb_qlt->next_table_index;
879 			memcpy(p_adb_qlt, ul_adb->pp_qlt[i], qlt_size);
880 			offset += qlt_size;
881 		}
882 	}
883 	*out_offset = offset;
884 }
885 
886 /**
887  * ipc_mux_ul_adb_finish - Add the TD of the aggregated session packets to TDR.
888  * @ipc_mux:               Pointer to MUX data-struct.
889  */
ipc_mux_ul_adb_finish(struct iosm_mux * ipc_mux)890 void ipc_mux_ul_adb_finish(struct iosm_mux *ipc_mux)
891 {
892 	bool ul_data_pend = false;
893 	struct mux_adb *ul_adb;
894 	unsigned long flags;
895 	int offset;
896 
897 	ul_adb = &ipc_mux->ul_adb;
898 	if (!ul_adb->dest_skb)
899 		return;
900 
901 	offset = *ul_adb->next_table_index;
902 	ipc_mux_ul_encode_adth(ipc_mux, ul_adb, &offset);
903 	ul_adb->adbh->block_length = cpu_to_le32(offset);
904 
905 	if (le32_to_cpu(ul_adb->adbh->block_length) > ul_adb->size) {
906 		ul_adb->dest_skb = NULL;
907 		return;
908 	}
909 
910 	*ul_adb->next_table_index = 0;
911 	ul_adb->adbh->sequence_nr = cpu_to_le16(ipc_mux->adb_tx_sequence_nr++);
912 	skb_put(ul_adb->dest_skb, le32_to_cpu(ul_adb->adbh->block_length));
913 
914 	spin_lock_irqsave(&(&ipc_mux->channel->ul_list)->lock, flags);
915 	__skb_queue_tail(&ipc_mux->channel->ul_list,  ul_adb->dest_skb);
916 	spin_unlock_irqrestore(&(&ipc_mux->channel->ul_list)->lock, flags);
917 
918 	ul_adb->dest_skb = NULL;
919 	/* Updates the TDs with ul_list */
920 	ul_data_pend = ipc_imem_ul_write_td(ipc_mux->imem);
921 
922 	/* Delay the doorbell irq */
923 	if (ul_data_pend)
924 		ipc_imem_td_update_timer_start(ipc_mux->imem);
925 
926 	ipc_mux->acc_adb_size +=  le32_to_cpu(ul_adb->adbh->block_length);
927 	ipc_mux->acc_payload_size += ul_adb->payload_size;
928 	ipc_mux->ul_data_pend_bytes += ul_adb->payload_size;
929 }
930 
931 /* Allocates an ADB from the free list and initializes it with ADBH  */
ipc_mux_ul_adb_allocate(struct iosm_mux * ipc_mux,struct mux_adb * adb,int * size_needed,u32 type)932 static bool ipc_mux_ul_adb_allocate(struct iosm_mux *ipc_mux,
933 				    struct mux_adb *adb, int *size_needed,
934 				    u32 type)
935 {
936 	bool ret_val = false;
937 	int status;
938 
939 	if (!adb->dest_skb) {
940 		/* Allocate memory for the ADB including of the
941 		 * datagram table header.
942 		 */
943 		status = ipc_mux_ul_skb_alloc(ipc_mux, adb, type);
944 		if (status)
945 			/* Is a pending ADB available ? */
946 			ret_val = true; /* None. */
947 
948 		/* Update size need to zero only for new ADB memory */
949 		*size_needed = 0;
950 	}
951 
952 	return ret_val;
953 }
954 
955 /* Informs the network stack to stop sending further packets for all opened
956  * sessions
957  */
ipc_mux_stop_tx_for_all_sessions(struct iosm_mux * ipc_mux)958 static void ipc_mux_stop_tx_for_all_sessions(struct iosm_mux *ipc_mux)
959 {
960 	struct mux_session *session;
961 	int idx;
962 
963 	for (idx = 0; idx < IPC_MEM_MUX_IP_SESSION_ENTRIES; idx++) {
964 		session = &ipc_mux->session[idx];
965 
966 		if (!session->wwan)
967 			continue;
968 
969 		session->net_tx_stop = true;
970 	}
971 }
972 
973 /* Sends Queue Level Table of all opened sessions */
ipc_mux_lite_send_qlt(struct iosm_mux * ipc_mux)974 static bool ipc_mux_lite_send_qlt(struct iosm_mux *ipc_mux)
975 {
976 	struct ipc_mem_lite_gen_tbl *qlt;
977 	struct mux_session *session;
978 	bool qlt_updated = false;
979 	int i;
980 	int qlt_size;
981 
982 	if (!ipc_mux->initialized || ipc_mux->state != MUX_S_ACTIVE)
983 		return qlt_updated;
984 
985 	qlt_size = offsetof(struct ipc_mem_lite_gen_tbl, vfl) +
986 		   MUX_QUEUE_LEVEL * sizeof(struct mux_lite_vfl);
987 
988 	for (i = 0; i < IPC_MEM_MUX_IP_SESSION_ENTRIES; i++) {
989 		session = &ipc_mux->session[i];
990 
991 		if (!session->wwan || session->flow_ctl_mask)
992 			continue;
993 
994 		if (ipc_mux_ul_skb_alloc(ipc_mux, &ipc_mux->ul_adb,
995 					 MUX_SIG_QLTH)) {
996 			dev_err(ipc_mux->dev,
997 				"no reserved mem to send QLT of if_id: %d", i);
998 			break;
999 		}
1000 
1001 		/* Prepare QLT */
1002 		qlt = (struct ipc_mem_lite_gen_tbl *)(ipc_mux->ul_adb.qlth_skb)
1003 			      ->data;
1004 		qlt->signature = cpu_to_le32(MUX_SIG_QLTH);
1005 		qlt->length = cpu_to_le16(qlt_size);
1006 		qlt->if_id = i;
1007 		qlt->vfl_length = MUX_QUEUE_LEVEL * sizeof(struct mux_lite_vfl);
1008 		qlt->reserved[0] = 0;
1009 		qlt->reserved[1] = 0;
1010 
1011 		qlt->vfl.nr_of_bytes = cpu_to_le32(session->ul_list.qlen);
1012 
1013 		/* Add QLT to the transfer list. */
1014 		skb_queue_tail(&ipc_mux->channel->ul_list,
1015 			       ipc_mux->ul_adb.qlth_skb);
1016 
1017 		qlt_updated = true;
1018 		ipc_mux->ul_adb.qlth_skb = NULL;
1019 	}
1020 
1021 	if (qlt_updated)
1022 		/* Updates the TDs with ul_list */
1023 		(void)ipc_imem_ul_write_td(ipc_mux->imem);
1024 
1025 	return qlt_updated;
1026 }
1027 
1028 /* Checks the available credits for the specified session and returns
1029  * number of packets for which credits are available.
1030  */
ipc_mux_ul_bytes_credits_check(struct iosm_mux * ipc_mux,struct mux_session * session,struct sk_buff_head * ul_list,int max_nr_of_pkts)1031 static int ipc_mux_ul_bytes_credits_check(struct iosm_mux *ipc_mux,
1032 					  struct mux_session *session,
1033 					  struct sk_buff_head *ul_list,
1034 					  int max_nr_of_pkts)
1035 {
1036 	int pkts_to_send = 0;
1037 	struct sk_buff *skb;
1038 	int credits = 0;
1039 
1040 	if (ipc_mux->ul_flow == MUX_UL_ON_CREDITS) {
1041 		credits = session->ul_flow_credits;
1042 		if (credits <= 0) {
1043 			dev_dbg(ipc_mux->dev,
1044 				"FC::if_id[%d] Insuff.Credits/Qlen:%d/%u",
1045 				session->if_id, session->ul_flow_credits,
1046 				session->ul_list.qlen); /* nr_of_bytes */
1047 			return 0;
1048 		}
1049 	} else {
1050 		credits = IPC_MEM_MUX_UL_FLOWCTRL_HIGH_B -
1051 			  ipc_mux->ul_data_pend_bytes;
1052 		if (credits <= 0) {
1053 			ipc_mux_stop_tx_for_all_sessions(ipc_mux);
1054 
1055 			dev_dbg(ipc_mux->dev,
1056 				"if_id[%d] encod. fail Bytes: %llu, thresh: %d",
1057 				session->if_id, ipc_mux->ul_data_pend_bytes,
1058 				IPC_MEM_MUX_UL_FLOWCTRL_HIGH_B);
1059 			return 0;
1060 		}
1061 	}
1062 
1063 	/* Check if there are enough credits/bytes available to send the
1064 	 * requested max_nr_of_pkts. Otherwise restrict the nr_of_pkts
1065 	 * depending on available credits.
1066 	 */
1067 	skb_queue_walk(ul_list, skb)
1068 	{
1069 		if (!(credits >= skb->len && pkts_to_send < max_nr_of_pkts))
1070 			break;
1071 		credits -= skb->len;
1072 		pkts_to_send++;
1073 	}
1074 
1075 	return pkts_to_send;
1076 }
1077 
1078 /* Encode the UL IP packet according to Lite spec. */
ipc_mux_ul_adgh_encode(struct iosm_mux * ipc_mux,int session_id,struct mux_session * session,struct sk_buff_head * ul_list,struct mux_adb * adb,int nr_of_pkts)1079 static int ipc_mux_ul_adgh_encode(struct iosm_mux *ipc_mux, int session_id,
1080 				  struct mux_session *session,
1081 				  struct sk_buff_head *ul_list,
1082 				  struct mux_adb *adb, int nr_of_pkts)
1083 {
1084 	int offset = sizeof(struct mux_adgh);
1085 	int adb_updated = -EINVAL;
1086 	struct sk_buff *src_skb;
1087 	int aligned_size = 0;
1088 	int nr_of_skb = 0;
1089 	u32 pad_len = 0;
1090 
1091 	/* Re-calculate the number of packets depending on number of bytes to be
1092 	 * processed/available credits.
1093 	 */
1094 	nr_of_pkts = ipc_mux_ul_bytes_credits_check(ipc_mux, session, ul_list,
1095 						    nr_of_pkts);
1096 
1097 	/* If calculated nr_of_pkts from available credits is <= 0
1098 	 * then nothing to do.
1099 	 */
1100 	if (nr_of_pkts <= 0)
1101 		return 0;
1102 
1103 	/* Read configured UL head_pad_length for session.*/
1104 	if (session->ul_head_pad_len > IPC_MEM_DL_ETH_OFFSET)
1105 		pad_len = session->ul_head_pad_len - IPC_MEM_DL_ETH_OFFSET;
1106 
1107 	/* Process all pending UL packets for this session
1108 	 * depending on the allocated datagram table size.
1109 	 */
1110 	while (nr_of_pkts > 0) {
1111 		/* get destination skb allocated */
1112 		if (ipc_mux_ul_adb_allocate(ipc_mux, adb, &ipc_mux->size_needed,
1113 					    IOSM_AGGR_MUX_SIG_ADGH)) {
1114 			dev_err(ipc_mux->dev, "no reserved memory for ADGH");
1115 			return -ENOMEM;
1116 		}
1117 
1118 		/* Peek at the head of the list. */
1119 		src_skb = skb_peek(ul_list);
1120 		if (!src_skb) {
1121 			dev_err(ipc_mux->dev,
1122 				"skb peek return NULL with count : %d",
1123 				nr_of_pkts);
1124 			break;
1125 		}
1126 
1127 		/* Calculate the memory value. */
1128 		aligned_size = ALIGN((pad_len + src_skb->len), 4);
1129 
1130 		ipc_mux->size_needed = sizeof(struct mux_adgh) + aligned_size;
1131 
1132 		if (ipc_mux->size_needed > adb->size) {
1133 			dev_dbg(ipc_mux->dev, "size needed %d, adgh size %d",
1134 				ipc_mux->size_needed, adb->size);
1135 			/* Return 1 if any IP packet is added to the transfer
1136 			 * list.
1137 			 */
1138 			return nr_of_skb ? 1 : 0;
1139 		}
1140 
1141 		/* Add buffer (without head padding to next pending transfer) */
1142 		memcpy(adb->buf + offset + pad_len, src_skb->data,
1143 		       src_skb->len);
1144 
1145 		adb->adgh->signature = cpu_to_le32(IOSM_AGGR_MUX_SIG_ADGH);
1146 		adb->adgh->if_id = session_id;
1147 		adb->adgh->length =
1148 			cpu_to_le16(sizeof(struct mux_adgh) + pad_len +
1149 				    src_skb->len);
1150 		adb->adgh->service_class = src_skb->priority;
1151 		adb->adgh->next_count = --nr_of_pkts;
1152 		adb->dg_cnt_total++;
1153 		adb->payload_size += src_skb->len;
1154 
1155 		if (ipc_mux->ul_flow == MUX_UL_ON_CREDITS)
1156 			/* Decrement the credit value as we are processing the
1157 			 * datagram from the UL list.
1158 			 */
1159 			session->ul_flow_credits -= src_skb->len;
1160 
1161 		/* Remove the processed elements and free it. */
1162 		src_skb = skb_dequeue(ul_list);
1163 		dev_kfree_skb(src_skb);
1164 		nr_of_skb++;
1165 
1166 		ipc_mux_ul_adgh_finish(ipc_mux);
1167 	}
1168 
1169 	if (nr_of_skb) {
1170 		/* Send QLT info to modem if pending bytes > high watermark
1171 		 * in case of mux lite
1172 		 */
1173 		if (ipc_mux->ul_flow == MUX_UL_ON_CREDITS ||
1174 		    ipc_mux->ul_data_pend_bytes >=
1175 			    IPC_MEM_MUX_UL_FLOWCTRL_LOW_B)
1176 			adb_updated = ipc_mux_lite_send_qlt(ipc_mux);
1177 		else
1178 			adb_updated = 1;
1179 
1180 		/* Updates the TDs with ul_list */
1181 		(void)ipc_imem_ul_write_td(ipc_mux->imem);
1182 	}
1183 
1184 	return adb_updated;
1185 }
1186 
1187 /**
1188  * ipc_mux_ul_adb_update_ql - Adds Queue Level Table and Queue Level to ADB
1189  * @ipc_mux:            pointer to MUX instance data
1190  * @p_adb:              pointer to UL aggegated data block
1191  * @session_id:         session id
1192  * @qlth_n_ql_size:     Length (in bytes) of the datagram table
1193  * @ul_list:            pointer to skb buffer head
1194  */
ipc_mux_ul_adb_update_ql(struct iosm_mux * ipc_mux,struct mux_adb * p_adb,int session_id,int qlth_n_ql_size,struct sk_buff_head * ul_list)1195 void ipc_mux_ul_adb_update_ql(struct iosm_mux *ipc_mux, struct mux_adb *p_adb,
1196 			      int session_id, int qlth_n_ql_size,
1197 			      struct sk_buff_head *ul_list)
1198 {
1199 	int qlevel = ul_list->qlen;
1200 	struct mux_qlth *p_qlt;
1201 
1202 	p_qlt = (struct mux_qlth *)p_adb->pp_qlt[session_id];
1203 
1204 	/* Initialize QLTH if not been done */
1205 	if (p_adb->qlt_updated[session_id] == 0) {
1206 		p_qlt->signature = cpu_to_le32(MUX_SIG_QLTH);
1207 		p_qlt->if_id = session_id;
1208 		p_qlt->table_length = cpu_to_le16(qlth_n_ql_size);
1209 		p_qlt->reserved = 0;
1210 		p_qlt->reserved2 = 0;
1211 	}
1212 
1213 	/* Update Queue Level information always */
1214 	p_qlt->ql.nr_of_bytes = cpu_to_le32(qlevel);
1215 	p_adb->qlt_updated[session_id] = 1;
1216 }
1217 
1218 /* Update the next table index. */
mux_ul_dg_update_tbl_index(struct iosm_mux * ipc_mux,int session_id,struct sk_buff_head * ul_list,struct mux_adth_dg * dg,int aligned_size,u32 qlth_n_ql_size,struct mux_adb * adb,struct sk_buff * src_skb)1219 static int mux_ul_dg_update_tbl_index(struct iosm_mux *ipc_mux,
1220 				      int session_id,
1221 				      struct sk_buff_head *ul_list,
1222 				      struct mux_adth_dg *dg,
1223 				      int aligned_size,
1224 				      u32 qlth_n_ql_size,
1225 				      struct mux_adb *adb,
1226 				      struct sk_buff *src_skb)
1227 {
1228 	ipc_mux_ul_adb_update_ql(ipc_mux, adb, session_id,
1229 				 qlth_n_ql_size, ul_list);
1230 	ipc_mux_ul_adb_finish(ipc_mux);
1231 	if (ipc_mux_ul_adb_allocate(ipc_mux, adb, &ipc_mux->size_needed,
1232 				    IOSM_AGGR_MUX_SIG_ADBH))
1233 		return -ENOMEM;
1234 
1235 	ipc_mux->size_needed = le32_to_cpu(adb->adbh->block_length);
1236 
1237 	ipc_mux->size_needed += offsetof(struct mux_adth, dg);
1238 	ipc_mux->size_needed += qlth_n_ql_size;
1239 	ipc_mux->size_needed += sizeof(*dg) + aligned_size;
1240 	return 0;
1241 }
1242 
1243 /* Process encode session UL data. */
mux_ul_dg_encode(struct iosm_mux * ipc_mux,struct mux_adb * adb,struct mux_adth_dg * dg,struct sk_buff_head * ul_list,struct sk_buff * src_skb,int session_id,int pkt_to_send,u32 qlth_n_ql_size,int * out_offset,int head_pad_len)1244 static int mux_ul_dg_encode(struct iosm_mux *ipc_mux, struct mux_adb *adb,
1245 			    struct mux_adth_dg *dg,
1246 			    struct sk_buff_head *ul_list,
1247 			    struct sk_buff *src_skb, int session_id,
1248 			    int pkt_to_send, u32 qlth_n_ql_size,
1249 			    int *out_offset, int head_pad_len)
1250 {
1251 	int aligned_size;
1252 	int offset = *out_offset;
1253 	unsigned long flags;
1254 	int nr_of_skb = 0;
1255 
1256 	while (pkt_to_send > 0) {
1257 		/* Peek at the head of the list. */
1258 		src_skb = skb_peek(ul_list);
1259 		if (!src_skb) {
1260 			dev_err(ipc_mux->dev,
1261 				"skb peek return NULL with count : %d",
1262 				pkt_to_send);
1263 			return -1;
1264 		}
1265 		aligned_size = ALIGN((head_pad_len + src_skb->len), 4);
1266 		ipc_mux->size_needed += sizeof(*dg) + aligned_size;
1267 
1268 		if (ipc_mux->size_needed > adb->size ||
1269 		    ((ipc_mux->size_needed + ipc_mux->ul_data_pend_bytes) >=
1270 		      IPC_MEM_MUX_UL_FLOWCTRL_HIGH_B)) {
1271 			*adb->next_table_index = offset;
1272 			if (mux_ul_dg_update_tbl_index(ipc_mux, session_id,
1273 						       ul_list, dg,
1274 						       aligned_size,
1275 						       qlth_n_ql_size, adb,
1276 						       src_skb) < 0)
1277 				return -ENOMEM;
1278 			nr_of_skb = 0;
1279 			offset = le32_to_cpu(adb->adbh->block_length);
1280 			/* Load pointer to next available datagram entry */
1281 			dg = adb->dg[session_id] + adb->dg_count[session_id];
1282 		}
1283 		/* Add buffer without head padding to next pending transfer. */
1284 		memcpy(adb->buf + offset + head_pad_len,
1285 		       src_skb->data, src_skb->len);
1286 		/* Setup datagram entry. */
1287 		dg->datagram_index = cpu_to_le32(offset);
1288 		dg->datagram_length = cpu_to_le16(src_skb->len + head_pad_len);
1289 		dg->service_class = (((struct sk_buff *)src_skb)->priority);
1290 		dg->reserved = 0;
1291 		adb->dg_cnt_total++;
1292 		adb->payload_size += le16_to_cpu(dg->datagram_length);
1293 		dg++;
1294 		adb->dg_count[session_id]++;
1295 		offset += aligned_size;
1296 		/* Remove the processed elements and free it. */
1297 		spin_lock_irqsave(&ul_list->lock, flags);
1298 		src_skb = __skb_dequeue(ul_list);
1299 		spin_unlock_irqrestore(&ul_list->lock, flags);
1300 
1301 		dev_kfree_skb(src_skb);
1302 		nr_of_skb++;
1303 		pkt_to_send--;
1304 	}
1305 	*out_offset = offset;
1306 	return nr_of_skb;
1307 }
1308 
1309 /* Process encode session UL data to ADB. */
mux_ul_adb_encode(struct iosm_mux * ipc_mux,int session_id,struct mux_session * session,struct sk_buff_head * ul_list,struct mux_adb * adb,int pkt_to_send)1310 static int mux_ul_adb_encode(struct iosm_mux *ipc_mux, int session_id,
1311 			     struct mux_session *session,
1312 			     struct sk_buff_head *ul_list, struct mux_adb *adb,
1313 			     int pkt_to_send)
1314 {
1315 	int adb_updated = -EINVAL;
1316 	int head_pad_len, offset;
1317 	struct sk_buff *src_skb = NULL;
1318 	struct mux_adth_dg *dg;
1319 	u32 qlth_n_ql_size;
1320 
1321 	/* If any of the opened session has set Flow Control ON then limit the
1322 	 * UL data to mux_flow_ctrl_high_thresh_b bytes
1323 	 */
1324 	if (ipc_mux->ul_data_pend_bytes >=
1325 		IPC_MEM_MUX_UL_FLOWCTRL_HIGH_B) {
1326 		ipc_mux_stop_tx_for_all_sessions(ipc_mux);
1327 		return adb_updated;
1328 	}
1329 
1330 	qlth_n_ql_size = offsetof(struct mux_qlth, ql) +
1331 			 MUX_QUEUE_LEVEL * sizeof(struct mux_qlth_ql);
1332 	head_pad_len = session->ul_head_pad_len;
1333 
1334 	if (session->ul_head_pad_len > IPC_MEM_DL_ETH_OFFSET)
1335 		head_pad_len = session->ul_head_pad_len - IPC_MEM_DL_ETH_OFFSET;
1336 
1337 	if (ipc_mux_ul_adb_allocate(ipc_mux, adb, &ipc_mux->size_needed,
1338 				    IOSM_AGGR_MUX_SIG_ADBH))
1339 		return -ENOMEM;
1340 
1341 	offset = le32_to_cpu(adb->adbh->block_length);
1342 
1343 	if (ipc_mux->size_needed == 0)
1344 		ipc_mux->size_needed = offset;
1345 
1346 	/* Calculate the size needed for ADTH, QLTH and QL*/
1347 	if (adb->dg_count[session_id] == 0) {
1348 		ipc_mux->size_needed += offsetof(struct mux_adth, dg);
1349 		ipc_mux->size_needed += qlth_n_ql_size;
1350 	}
1351 
1352 	dg = adb->dg[session_id] + adb->dg_count[session_id];
1353 
1354 	if (mux_ul_dg_encode(ipc_mux, adb, dg, ul_list, src_skb,
1355 			     session_id, pkt_to_send, qlth_n_ql_size, &offset,
1356 			     head_pad_len) > 0) {
1357 		adb_updated = 1;
1358 		*adb->next_table_index = offset;
1359 		ipc_mux_ul_adb_update_ql(ipc_mux, adb, session_id,
1360 					 qlth_n_ql_size, ul_list);
1361 		adb->adbh->block_length = cpu_to_le32(offset);
1362 	}
1363 
1364 	return adb_updated;
1365 }
1366 
ipc_mux_ul_data_encode(struct iosm_mux * ipc_mux)1367 bool ipc_mux_ul_data_encode(struct iosm_mux *ipc_mux)
1368 {
1369 	struct sk_buff_head *ul_list;
1370 	struct mux_session *session;
1371 	int updated = 0;
1372 	int session_id;
1373 	int dg_n;
1374 	int i;
1375 
1376 	if (!ipc_mux || ipc_mux->state != MUX_S_ACTIVE ||
1377 	    ipc_mux->adb_prep_ongoing)
1378 		return false;
1379 
1380 	ipc_mux->adb_prep_ongoing = true;
1381 
1382 	for (i = 0; i < IPC_MEM_MUX_IP_SESSION_ENTRIES; i++) {
1383 		session_id = ipc_mux->rr_next_session;
1384 		session = &ipc_mux->session[session_id];
1385 
1386 		/* Go to next handle rr_next_session overflow */
1387 		ipc_mux->rr_next_session++;
1388 		if (ipc_mux->rr_next_session >= IPC_MEM_MUX_IP_SESSION_ENTRIES)
1389 			ipc_mux->rr_next_session = 0;
1390 
1391 		if (!session->wwan || session->flow_ctl_mask ||
1392 		    session->net_tx_stop)
1393 			continue;
1394 
1395 		ul_list = &session->ul_list;
1396 
1397 		/* Is something pending in UL and flow ctrl off */
1398 		dg_n = skb_queue_len(ul_list);
1399 		if (dg_n > MUX_MAX_UL_DG_ENTRIES)
1400 			dg_n = MUX_MAX_UL_DG_ENTRIES;
1401 
1402 		if (dg_n == 0)
1403 			/* Nothing to do for ipc_mux session
1404 			 * -> try next session id.
1405 			 */
1406 			continue;
1407 		if (ipc_mux->protocol == MUX_LITE)
1408 			updated = ipc_mux_ul_adgh_encode(ipc_mux, session_id,
1409 							 session, ul_list,
1410 							 &ipc_mux->ul_adb,
1411 							 dg_n);
1412 		else
1413 			updated = mux_ul_adb_encode(ipc_mux, session_id,
1414 						    session, ul_list,
1415 						    &ipc_mux->ul_adb,
1416 						    dg_n);
1417 	}
1418 
1419 	ipc_mux->adb_prep_ongoing = false;
1420 	return updated == 1;
1421 }
1422 
1423 /* Calculates the Payload from any given ADB. */
ipc_mux_get_payload_from_adb(struct iosm_mux * ipc_mux,struct mux_adbh * p_adbh)1424 static int ipc_mux_get_payload_from_adb(struct iosm_mux *ipc_mux,
1425 					struct mux_adbh *p_adbh)
1426 {
1427 	struct mux_adth_dg *dg;
1428 	struct mux_adth *adth;
1429 	u32 payload_size = 0;
1430 	u32 next_table_idx;
1431 	int nr_of_dg, i;
1432 
1433 	/* Process the aggregated datagram tables. */
1434 	next_table_idx = le32_to_cpu(p_adbh->first_table_index);
1435 
1436 	if (next_table_idx < sizeof(struct mux_adbh)) {
1437 		dev_err(ipc_mux->dev, "unexpected empty ADB");
1438 		return payload_size;
1439 	}
1440 
1441 	while (next_table_idx != 0) {
1442 		/* Get the reference to the table header. */
1443 		adth = (struct mux_adth *)((u8 *)p_adbh + next_table_idx);
1444 
1445 		if (adth->signature == cpu_to_le32(IOSM_AGGR_MUX_SIG_ADTH)) {
1446 			nr_of_dg = (le16_to_cpu(adth->table_length) -
1447 					sizeof(struct mux_adth)) /
1448 					sizeof(struct mux_adth_dg);
1449 
1450 			if (nr_of_dg <= 0)
1451 				return payload_size;
1452 
1453 			dg = adth->dg;
1454 
1455 			for (i = 0; i < nr_of_dg; i++, dg++) {
1456 				if (le32_to_cpu(dg->datagram_index) <
1457 					sizeof(struct mux_adbh)) {
1458 					return payload_size;
1459 				}
1460 				payload_size +=
1461 					le16_to_cpu(dg->datagram_length);
1462 			}
1463 		}
1464 		next_table_idx = le32_to_cpu(adth->next_table_index);
1465 	}
1466 
1467 	return payload_size;
1468 }
1469 
ipc_mux_ul_encoded_process(struct iosm_mux * ipc_mux,struct sk_buff * skb)1470 void ipc_mux_ul_encoded_process(struct iosm_mux *ipc_mux, struct sk_buff *skb)
1471 {
1472 	union mux_type_header hr;
1473 	u16 adgh_len;
1474 	int payload;
1475 
1476 	if (ipc_mux->protocol == MUX_LITE) {
1477 		hr.adgh = (struct mux_adgh *)skb->data;
1478 		adgh_len = le16_to_cpu(hr.adgh->length);
1479 		if (hr.adgh->signature == cpu_to_le32(IOSM_AGGR_MUX_SIG_ADGH) &&
1480 		    ipc_mux->ul_flow == MUX_UL)
1481 			ipc_mux->ul_data_pend_bytes =
1482 					ipc_mux->ul_data_pend_bytes - adgh_len;
1483 	} else {
1484 		hr.adbh = (struct mux_adbh *)(skb->data);
1485 		payload = ipc_mux_get_payload_from_adb(ipc_mux, hr.adbh);
1486 		ipc_mux->ul_data_pend_bytes -= payload;
1487 	}
1488 
1489 	if (ipc_mux->ul_flow == MUX_UL)
1490 		dev_dbg(ipc_mux->dev, "ul_data_pend_bytes: %lld",
1491 			ipc_mux->ul_data_pend_bytes);
1492 
1493 	/* Reset the skb settings. */
1494 	skb_trim(skb, 0);
1495 
1496 	/* Add the consumed ADB to the free list. */
1497 	skb_queue_tail((&ipc_mux->ul_adb.free_list), skb);
1498 }
1499 
1500 /* Start the NETIF uplink send transfer in MUX mode. */
ipc_mux_tq_ul_trigger_encode(struct iosm_imem * ipc_imem,int arg,void * msg,size_t size)1501 static int ipc_mux_tq_ul_trigger_encode(struct iosm_imem *ipc_imem, int arg,
1502 					void *msg, size_t size)
1503 {
1504 	struct iosm_mux *ipc_mux = ipc_imem->mux;
1505 	bool ul_data_pend = false;
1506 
1507 	/* Add session UL data to a ADB and ADGH */
1508 	ul_data_pend = ipc_mux_ul_data_encode(ipc_mux);
1509 	if (ul_data_pend) {
1510 		if (ipc_mux->protocol == MUX_AGGREGATION)
1511 			ipc_imem_adb_timer_start(ipc_mux->imem);
1512 
1513 		/* Delay the doorbell irq */
1514 		ipc_imem_td_update_timer_start(ipc_mux->imem);
1515 	}
1516 	/* reset the debounce flag */
1517 	ipc_mux->ev_mux_net_transmit_pending = false;
1518 
1519 	return 0;
1520 }
1521 
ipc_mux_ul_trigger_encode(struct iosm_mux * ipc_mux,int if_id,struct sk_buff * skb)1522 int ipc_mux_ul_trigger_encode(struct iosm_mux *ipc_mux, int if_id,
1523 			      struct sk_buff *skb)
1524 {
1525 	struct mux_session *session = &ipc_mux->session[if_id];
1526 	int ret = -EINVAL;
1527 
1528 	if (ipc_mux->channel &&
1529 	    ipc_mux->channel->state != IMEM_CHANNEL_ACTIVE) {
1530 		dev_err(ipc_mux->dev,
1531 			"channel state is not IMEM_CHANNEL_ACTIVE");
1532 		goto out;
1533 	}
1534 
1535 	if (!session->wwan) {
1536 		dev_err(ipc_mux->dev, "session net ID is NULL");
1537 		ret = -EFAULT;
1538 		goto out;
1539 	}
1540 
1541 	/* Session is under flow control.
1542 	 * Check if packet can be queued in session list, if not
1543 	 * suspend net tx
1544 	 */
1545 	if (skb_queue_len(&session->ul_list) >=
1546 	    (session->net_tx_stop ?
1547 		     IPC_MEM_MUX_UL_SESS_FCON_THRESHOLD :
1548 		     (IPC_MEM_MUX_UL_SESS_FCON_THRESHOLD *
1549 		      IPC_MEM_MUX_UL_SESS_FCOFF_THRESHOLD_FACTOR))) {
1550 		ipc_mux_netif_tx_flowctrl(session, session->if_id, true);
1551 		ret = -EBUSY;
1552 		goto out;
1553 	}
1554 
1555 	/* Add skb to the uplink skb accumulator. */
1556 	skb_queue_tail(&session->ul_list, skb);
1557 
1558 	/* Inform the IPC kthread to pass uplink IP packets to CP. */
1559 	if (!ipc_mux->ev_mux_net_transmit_pending) {
1560 		ipc_mux->ev_mux_net_transmit_pending = true;
1561 		ret = ipc_task_queue_send_task(ipc_mux->imem,
1562 					       ipc_mux_tq_ul_trigger_encode, 0,
1563 					       NULL, 0, false);
1564 		if (ret)
1565 			goto out;
1566 	}
1567 	dev_dbg(ipc_mux->dev, "mux ul if[%d] qlen=%d/%u, len=%d/%d, prio=%d",
1568 		if_id, skb_queue_len(&session->ul_list), session->ul_list.qlen,
1569 		skb->len, skb->truesize, skb->priority);
1570 	ret = 0;
1571 out:
1572 	return ret;
1573 }
1574