1 /* SPDX-License-Identifier: GPL-2.0-only
2  *
3  * Copyright (C) 2020-21 Intel Corporation.
4  */
5 
6 #ifndef IOSM_IPC_MUX_H
7 #define IOSM_IPC_MUX_H
8 
9 #include "iosm_ipc_protocol.h"
10 
11 #define IPC_MEM_MAX_UL_DG_ENTRIES	100
12 #define IPC_MEM_MAX_TDS_MUX_AGGR_UL	60
13 #define IPC_MEM_MAX_TDS_MUX_AGGR_DL	60
14 
15 #define IPC_MEM_MAX_ADB_BUF_SIZE (16 * 1024)
16 #define IPC_MEM_MAX_UL_ADB_BUF_SIZE IPC_MEM_MAX_ADB_BUF_SIZE
17 #define IPC_MEM_MAX_DL_ADB_BUF_SIZE IPC_MEM_MAX_ADB_BUF_SIZE
18 
19 /* Size of the buffer for the IP MUX Lite data buffer. */
20 #define IPC_MEM_MAX_DL_MUX_LITE_BUF_SIZE (2 * 1024)
21 
22 /* TD counts for IP MUX Lite */
23 #define IPC_MEM_MAX_TDS_MUX_LITE_UL 800
24 #define IPC_MEM_MAX_TDS_MUX_LITE_DL 1200
25 
26 /* open session request (AP->CP) */
27 #define MUX_CMD_OPEN_SESSION 1
28 
29 /* response to open session request (CP->AP) */
30 #define MUX_CMD_OPEN_SESSION_RESP 2
31 
32 /* close session request (AP->CP) */
33 #define MUX_CMD_CLOSE_SESSION 3
34 
35 /* response to close session request (CP->AP) */
36 #define MUX_CMD_CLOSE_SESSION_RESP 4
37 
38 /* Flow control command with mask of the flow per queue/flow. */
39 #define MUX_LITE_CMD_FLOW_CTL 5
40 
41 /* ACK the flow control command. Shall have the same Transaction ID as the
42  * matching FLOW_CTL command.
43  */
44 #define MUX_LITE_CMD_FLOW_CTL_ACK 6
45 
46 /* Command for report packet indicating link quality metrics. */
47 #define MUX_LITE_CMD_LINK_STATUS_REPORT 7
48 
49 /* Response to a report packet */
50 #define MUX_LITE_CMD_LINK_STATUS_REPORT_RESP 8
51 
52 /* Used to reset a command/response state. */
53 #define MUX_CMD_INVALID 255
54 
55 /* command response : command processed successfully */
56 #define MUX_CMD_RESP_SUCCESS 0
57 
58 /* MUX for route link devices */
59 #define IPC_MEM_WWAN_MUX BIT(0)
60 
61 /* Initiated actions to change the state of the MUX object. */
62 enum mux_event {
63 	MUX_E_INACTIVE, /* No initiated actions. */
64 	MUX_E_MUX_SESSION_OPEN, /* Create the MUX channel and a session. */
65 	MUX_E_MUX_SESSION_CLOSE, /* Release a session. */
66 	MUX_E_MUX_CHANNEL_CLOSE, /* Release the MUX channel. */
67 	MUX_E_NO_ORDERS, /* No MUX order. */
68 	MUX_E_NOT_APPLICABLE, /* Defect IP MUX. */
69 };
70 
71 /* MUX session open command. */
72 struct mux_session_open {
73 	enum mux_event event;
74 	__le32 if_id;
75 };
76 
77 /* MUX session close command. */
78 struct mux_session_close {
79 	enum mux_event event;
80 	__le32 if_id;
81 };
82 
83 /* MUX channel close command. */
84 struct mux_channel_close {
85 	enum mux_event event;
86 };
87 
88 /* Default message type to find out the right message type. */
89 struct mux_common {
90 	enum mux_event event;
91 };
92 
93 /* List of ops in MUX mode. */
94 union mux_msg {
95 	struct mux_session_open session_open;
96 	struct mux_session_close session_close;
97 	struct mux_channel_close channel_close;
98 	struct mux_common common;
99 };
100 
101 /* Parameter definition of the open session command. */
102 struct mux_cmd_open_session {
103 	u8 flow_ctrl; /* 0: Flow control disabled (flow allowed). */
104 	/* 1: Flow control enabled (flow not allowed)*/
105 	u8 ipv4v6_hints; /* 0: IPv4/IPv6 hints not supported.*/
106 	/* 1: IPv4/IPv6 hints supported*/
107 	__le16 reserved2; /* Reserved. Set to zero. */
108 	__le32 dl_head_pad_len; /* Maximum length supported */
109 	/* for DL head padding on a datagram. */
110 };
111 
112 /* Parameter definition of the open session response. */
113 struct mux_cmd_open_session_resp {
114 	__le32 response; /* Response code */
115 	u8 flow_ctrl; /* 0: Flow control disabled (flow allowed). */
116 	/* 1: Flow control enabled (flow not allowed) */
117 	u8 ipv4v6_hints; /* 0: IPv4/IPv6 hints not supported */
118 	/* 1: IPv4/IPv6 hints supported */
119 	__le16 reserved2; /* Reserved. Set to zero. */
120 	__le32 ul_head_pad_len; /* Actual length supported for */
121 	/* UL head padding on adatagram.*/
122 };
123 
124 /* Parameter definition of the close session response code */
125 struct mux_cmd_close_session_resp {
126 	__le32 response;
127 };
128 
129 /* Parameter definition of the flow control command. */
130 struct mux_cmd_flow_ctl {
131 	__le32 mask; /* indicating the desired flow control */
132 	/* state for various flows/queues */
133 };
134 
135 /* Parameter definition of the link status report code*/
136 struct mux_cmd_link_status_report {
137 	u8 payload;
138 };
139 
140 /* Parameter definition of the link status report response code. */
141 struct mux_cmd_link_status_report_resp {
142 	__le32 response;
143 };
144 
145 /**
146  * union mux_cmd_param - Union-definition of the command parameters.
147  * @open_session:	Inband command for open session
148  * @open_session_resp:	Inband command for open session response
149  * @close_session_resp:	Inband command for close session response
150  * @flow_ctl:		In-band flow control on the opened interfaces
151  * @link_status:	In-band Link Status Report
152  * @link_status_resp:	In-band command for link status report response
153  */
154 union mux_cmd_param {
155 	struct mux_cmd_open_session open_session;
156 	struct mux_cmd_open_session_resp open_session_resp;
157 	struct mux_cmd_close_session_resp close_session_resp;
158 	struct mux_cmd_flow_ctl flow_ctl;
159 	struct mux_cmd_link_status_report link_status;
160 	struct mux_cmd_link_status_report_resp link_status_resp;
161 };
162 
163 /* States of the MUX object.. */
164 enum mux_state {
165 	MUX_S_INACTIVE, /* IP MUX is unused. */
166 	MUX_S_ACTIVE, /* IP MUX channel is available. */
167 	MUX_S_ERROR, /* Defect IP MUX. */
168 };
169 
170 /* Supported MUX protocols. */
171 enum ipc_mux_protocol {
172 	MUX_UNKNOWN,
173 	MUX_LITE,
174 	MUX_AGGREGATION,
175 };
176 
177 /* Supported UL data transfer methods. */
178 enum ipc_mux_ul_flow {
179 	MUX_UL_UNKNOWN,
180 	MUX_UL, /* Normal UL data transfer */
181 	MUX_UL_ON_CREDITS, /* UL data transfer will be based on credits */
182 };
183 
184 /* List of the MUX session. */
185 struct mux_session {
186 	struct iosm_wwan *wwan; /*Network i/f used for communication*/
187 	int if_id; /* i/f id for session open message.*/
188 	u32 flags;
189 	u32 ul_head_pad_len; /* Nr of bytes for UL head padding. */
190 	u32 dl_head_pad_len; /* Nr of bytes for DL head padding. */
191 	struct sk_buff_head ul_list; /* skb entries for an ADT. */
192 	u32 flow_ctl_mask; /* UL flow control */
193 	u32 flow_ctl_en_cnt; /* Flow control Enable cmd count */
194 	u32 flow_ctl_dis_cnt; /* Flow Control Disable cmd count */
195 	int ul_flow_credits; /* UL flow credits */
196 	u8 net_tx_stop:1,
197 	   flush:1; /* flush net interface ? */
198 };
199 
200 /**
201  * struct mux_adth_dg - Structure of the datagram in the Aggregated Datagram
202  *			Table Header.
203  * @datagram_index :	Index (in bytes) to the k-th datagram in the table.
204  *			Index shall count from the start of the block including
205  *			the 16-byte header. This value shall be non-zero.
206  * @datagram_length:	Length of the k-th datagram including the head padding.
207  *			This value shall be non-zero.
208  * @service_class:	Service class identifier for the datagram.
209  * @reserved:		Reserved bytes. Set to zero
210  */
211 struct mux_adth_dg {
212 	__le32 datagram_index;
213 	__le16 datagram_length;
214 	u8 service_class;
215 	u8 reserved;
216 };
217 
218 /**
219  * struct mux_qlth_ql - Structure of the queue level in the Aggregated
220  *			Datagram Queue Level Table Header.
221  * @nr_of_bytes:	Number of bytes available to transmit in the queue.
222  */
223 struct mux_qlth_ql {
224 	__le32 nr_of_bytes;
225 };
226 
227 /**
228  * struct mux_qlth -    Structure of Aggregated Datagram Queue Level Table
229  *			Header.
230  * @signature:          Signature of the Queue Level Table Header
231  *                      Value: 0x48544C51 (ASCII characters: 'Q' 'L' 'T' 'H')
232  * @table_length:       Length (in bytes) of the datagram table. This length
233  *                      shall include the queue level table header size.
234  *                      Minimum value:0x10
235  * @if_id:              ID of the interface the queue levels in the table
236  *                      belong to.
237  * @reserved:           Reserved byte. Set to zero.
238  * @next_table_index:   Index (in bytes) to the next table in the buffer. Index
239  *                      shall count from the start of the block including the
240  *                      16-byte header. Value of zero indicates end of the list.
241  * @reserved2:          Reserved bytes. Set to zero
242  * @ql:                 Queue level table with variable length
243  */
244 struct mux_qlth {
245 	__le32 signature;
246 	__le16 table_length;
247 	u8 if_id;
248 	u8 reserved;
249 	__le32 next_table_index;
250 	__le32 reserved2;
251 	struct mux_qlth_ql ql;
252 };
253 
254 /**
255  * struct mux_adb - Structure of State of a single UL data block.
256  * @dest_skb:		Current UL skb for the data block.
257  * @buf:		ADB memory
258  * @adgh:		ADGH pointer
259  * @qlth_skb:		QLTH pointer
260  * @next_table_index:	Pointer to next table index.
261  * @free_list:		List of alloc. ADB for the UL sess.
262  * @size:		Size of the ADB memory.
263  * @if_cnt:		Statistic counter
264  * @dg_cnt_total:	Datagram count total
265  * @payload_size:	Payload Size
266  * @dg:			Datagram table.
267  * @pp_qlt:		Pointers to hold Queue Level Tables of session
268  * @adbh:		ADBH pointer
269  * @qlt_updated:	Queue level table updated
270  * @dg_count:		Datagram count
271  */
272 struct mux_adb {
273 	struct sk_buff *dest_skb;
274 	u8 *buf;
275 	struct mux_adgh *adgh;
276 	struct sk_buff *qlth_skb;
277 	u32 *next_table_index;
278 	struct sk_buff_head free_list;
279 	int size;
280 	u32 if_cnt;
281 	u32 dg_cnt_total;
282 	u32 payload_size;
283 	struct mux_adth_dg
284 		dg[IPC_MEM_MUX_IP_SESSION_ENTRIES][IPC_MEM_MAX_UL_DG_ENTRIES];
285 	struct mux_qlth *pp_qlt[IPC_MEM_MUX_IP_SESSION_ENTRIES];
286 	struct mux_adbh *adbh;
287 	u32 qlt_updated[IPC_MEM_MUX_IP_SESSION_ENTRIES];
288 	u32 dg_count[IPC_MEM_MUX_IP_SESSION_ENTRIES];
289 };
290 
291 /**
292  * struct mux_acb - Structure of Temporary ACB state.
293  * @skb:		Used UL skb.
294  * @if_id:		Session id.
295  * @buf_p:		Command buffer.
296  * @wanted_response:	Wanted Response
297  * @got_response:	Got response
298  * @cmd:		command
299  * @got_param:		Received command/response parameter
300  */
301 struct mux_acb {
302 	struct sk_buff *skb; /* Used UL skb. */
303 	int if_id; /* Session id. */
304 	u8 *buf_p;
305 	u32 wanted_response;
306 	u32 got_response;
307 	u32 cmd;
308 	union mux_cmd_param got_param; /* Received command/response parameter */
309 };
310 
311 /**
312  * struct iosm_mux - Structure of the data multiplexing over an IP channel.
313  * @dev:		Pointer to device structure
314  * @session:		Array of the MUX sessions.
315  * @channel:		Reference to the IP MUX channel
316  * @pcie:		Pointer to iosm_pcie struct
317  * @imem:		Pointer to iosm_imem
318  * @wwan:		Poinetr to iosm_wwan
319  * @ipc_protocol:	Pointer to iosm_protocol
320  * @channel_id:		Channel ID for MUX
321  * @protocol:		Type of the MUX protocol
322  * @ul_flow:		UL Flow type
323  * @nr_sessions:	Number of sessions
324  * @instance_id:	Instance ID
325  * @state:		States of the MUX object
326  * @event:		Initiated actions to change the state of the MUX object
327  * @tx_transaction_id:	Transaction id for the ACB command.
328  * @rr_next_session:	Next session number for round robin.
329  * @ul_adb:		State of the UL ADB/ADGH.
330  * @size_needed:	Variable to store the size needed during ADB preparation
331  * @ul_data_pend_bytes:	Pending UL data to be processed in bytes
332  * @acb:		Temporary ACB state
333  * @wwan_q_offset:	This will hold the offset of the given instance
334  *			Useful while passing or receiving packets from
335  *			wwan/imem layer.
336  * @adb_finish_timer:	Timer for forcefully finishing the ADB
337  * @acb_tx_sequence_nr: Sequence number for the ACB header.
338  * @params:		user configurable parameters
339  * @adb_tx_sequence_nr: Sequence number for ADB header
340  * @acc_adb_size:       Statistic data for logging
341  * @acc_payload_size:   Statistic data for logging
342  * @initialized:	MUX object is initialized
343  * @ev_mux_net_transmit_pending:
344  *			0 means inform the IPC tasklet to pass the
345  *			accumulated uplink ADB to CP.
346  * @adb_prep_ongoing:	Flag for ADB preparation status
347  */
348 struct iosm_mux {
349 	struct device *dev;
350 	struct mux_session session[IPC_MEM_MUX_IP_SESSION_ENTRIES];
351 	struct ipc_mem_channel *channel;
352 	struct iosm_pcie *pcie;
353 	struct iosm_imem *imem;
354 	struct iosm_wwan *wwan;
355 	struct iosm_protocol *ipc_protocol;
356 	int channel_id;
357 	enum ipc_mux_protocol protocol;
358 	enum ipc_mux_ul_flow ul_flow;
359 	int nr_sessions;
360 	int instance_id;
361 	enum mux_state state;
362 	enum mux_event event;
363 	u32 tx_transaction_id;
364 	int rr_next_session;
365 	struct mux_adb ul_adb;
366 	int size_needed;
367 	long long ul_data_pend_bytes;
368 	struct mux_acb acb;
369 	int wwan_q_offset;
370 	struct hrtimer adb_finish_timer;
371 	u16 acb_tx_sequence_nr;
372 	struct ipc_params *params;
373 	u16 adb_tx_sequence_nr;
374 	unsigned long long acc_adb_size;
375 	unsigned long long acc_payload_size;
376 	u8 initialized:1,
377 	   ev_mux_net_transmit_pending:1,
378 	   adb_prep_ongoing;
379 } __packed;
380 
381 /* MUX configuration structure */
382 struct ipc_mux_config {
383 	enum ipc_mux_protocol protocol;
384 	enum ipc_mux_ul_flow ul_flow;
385 	int instance_id;
386 };
387 
388 /**
389  * ipc_mux_init - Allocates and Init MUX instance
390  * @mux_cfg:	Pointer to MUX configuration structure
391  * @ipc_imem:	Pointer to imem data-struct
392  *
393  * Returns: Initialized mux pointer on success else NULL
394  */
395 struct iosm_mux *ipc_mux_init(struct ipc_mux_config *mux_cfg,
396 			      struct iosm_imem *ipc_imem);
397 
398 /**
399  * ipc_mux_deinit - Deallocates MUX instance
400  * @ipc_mux:	Pointer to the MUX instance.
401  */
402 void ipc_mux_deinit(struct iosm_mux *ipc_mux);
403 
404 /**
405  * ipc_mux_check_n_restart_tx - Checks for pending UL date bytes and then
406  *				it restarts the net interface tx queue if
407  *				device has set flow control as off.
408  * @ipc_mux:	Pointer to MUX data-struct
409  */
410 void ipc_mux_check_n_restart_tx(struct iosm_mux *ipc_mux);
411 
412 /**
413  * ipc_mux_get_active_protocol - Returns the active MUX protocol type.
414  * @ipc_mux:	Pointer to MUX data-struct
415  *
416  * Returns: enum of type ipc_mux_protocol
417  */
418 enum ipc_mux_protocol ipc_mux_get_active_protocol(struct iosm_mux *ipc_mux);
419 
420 /**
421  * ipc_mux_open_session - Opens a MUX session for IP traffic.
422  * @ipc_mux:	Pointer to MUX data-struct
423  * @session_nr:	Interface ID or session number
424  *
425  * Returns: channel id on success, failure value on error
426  */
427 int ipc_mux_open_session(struct iosm_mux *ipc_mux, int session_nr);
428 
429 /**
430  * ipc_mux_close_session - Closes a MUX session.
431  * @ipc_mux:	Pointer to MUX data-struct
432  * @session_nr:	Interface ID or session number
433  *
434  * Returns: channel id on success, failure value on error
435  */
436 int ipc_mux_close_session(struct iosm_mux *ipc_mux, int session_nr);
437 
438 /**
439  * ipc_mux_get_max_sessions - Retuns the maximum sessions supported on the
440  *			      provided MUX instance..
441  * @ipc_mux:	Pointer to MUX data-struct
442  *
443  * Returns: Number of sessions supported on Success and failure value on error
444  */
445 int ipc_mux_get_max_sessions(struct iosm_mux *ipc_mux);
446 #endif
447