1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Intel(R) Ethernet Switch Host Interface Driver
3  * Copyright(c) 2013 - 2016 Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in
15  * the file called "COPYING".
16  *
17  * Contact Information:
18  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
19  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
20  */
21 
22 #ifndef _FM10K_MBX_H_
23 #define _FM10K_MBX_H_
24 
25 /* forward declaration */
26 struct fm10k_mbx_info;
27 
28 #include "fm10k_type.h"
29 #include "fm10k_tlv.h"
30 
31 /* PF Mailbox Registers */
32 #define FM10K_MBMEM(_n)		((_n) + 0x18000)
33 #define FM10K_MBMEM_VF(_n, _m)	(((_n) * 0x10) + (_m) + 0x18000)
34 #define FM10K_MBMEM_SM(_n)	((_n) + 0x18400)
35 #define FM10K_MBMEM_PF(_n)	((_n) + 0x18600)
36 /* XOR provides means of switching from Tx to Rx FIFO */
37 #define FM10K_MBMEM_PF_XOR	(FM10K_MBMEM_SM(0) ^ FM10K_MBMEM_PF(0))
38 #define FM10K_MBX(_n)		((_n) + 0x18800)
39 #define FM10K_MBX_REQ				0x00000002
40 #define FM10K_MBX_ACK				0x00000004
41 #define FM10K_MBX_REQ_INTERRUPT			0x00000008
42 #define FM10K_MBX_ACK_INTERRUPT			0x00000010
43 #define FM10K_MBX_INTERRUPT_ENABLE		0x00000020
44 #define FM10K_MBX_INTERRUPT_DISABLE		0x00000040
45 #define FM10K_MBX_GLOBAL_REQ_INTERRUPT		0x00000200
46 #define FM10K_MBX_GLOBAL_ACK_INTERRUPT		0x00000400
47 #define FM10K_MBICR(_n)		((_n) + 0x18840)
48 #define FM10K_GMBX		0x18842
49 
50 /* VF Mailbox Registers */
51 #define FM10K_VFMBX		0x00010
52 #define FM10K_VFMBMEM(_n)	((_n) + 0x00020)
53 #define FM10K_VFMBMEM_LEN	16
54 #define FM10K_VFMBMEM_VF_XOR	(FM10K_VFMBMEM_LEN / 2)
55 
56 /* Delays/timeouts */
57 #define FM10K_MBX_DISCONNECT_TIMEOUT		500
58 #define FM10K_MBX_POLL_DELAY			19
59 #define FM10K_MBX_INT_DELAY			20
60 
61 /* PF/VF Mailbox state machine
62  *
63  * +----------+	    connect()	+----------+
64  * |  CLOSED  | --------------> |  CONNECT |
65  * +----------+			+----------+
66  *   ^				  ^	 |
67  *   | rcv:	      rcv:	  |	 | rcv:
68  *   |  Connect	       Disconnect |	 |  Connect
69  *   |  Disconnect     Error	  |	 |  Data
70  *   |				  |	 |
71  *   |				  |	 V
72  * +----------+   disconnect()	+----------+
73  * |DISCONNECT| <-------------- |   OPEN   |
74  * +----------+			+----------+
75  *
76  * The diagram above describes the PF/VF mailbox state machine.  There
77  * are four main states to this machine.
78  * Closed: This state represents a mailbox that is in a standby state
79  *	   with interrupts disabled.  In this state the mailbox should not
80  *	   read the mailbox or write any data.  The only means of exiting
81  *	   this state is for the system to make the connect() call for the
82  *	   mailbox, it will then transition to the connect state.
83  * Connect: In this state the mailbox is seeking a connection.  It will
84  *	    post a connect message with no specified destination and will
85  *	    wait for a reply from the other side of the mailbox.  This state
86  *	    is exited when either a connect with the local mailbox as the
87  *	    destination is received or when a data message is received with
88  *	    a valid sequence number.
89  * Open: In this state the mailbox is able to transfer data between the local
90  *       entity and the remote.  It will fall back to connect in the event of
91  *       receiving either an error message, or a disconnect message.  It will
92  *       transition to disconnect on a call to disconnect();
93  * Disconnect: In this state the mailbox is attempting to gracefully terminate
94  *	       the connection.  It will do so at the first point where it knows
95  *	       that the remote endpoint is either done sending, or when the
96  *	       remote endpoint has fallen back into connect.
97  */
98 enum fm10k_mbx_state {
99 	FM10K_STATE_CLOSED,
100 	FM10K_STATE_CONNECT,
101 	FM10K_STATE_OPEN,
102 	FM10K_STATE_DISCONNECT,
103 };
104 
105 /* PF/VF Mailbox header format
106  *    3			  2		      1			  0
107  *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
108  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109  * |        Size/Err_no/CRC        | Rsvd0 | Head  | Tail  | Type  |
110  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111  *
112  * The layout above describes the format for the header used in the PF/VF
113  * mailbox.  The header is broken out into the following fields:
114  * Type: There are 4 supported message types
115  *		0x8: Data header - used to transport message data
116  *		0xC: Connect header - used to establish connection
117  *		0xD: Disconnect header - used to tear down a connection
118  *		0xE: Error header - used to address message exceptions
119  * Tail: Tail index for local FIFO
120  *		Tail index actually consists of two parts.  The MSB of
121  *		the head is a loop tracker, it is 0 on an even numbered
122  *		loop through the FIFO, and 1 on the odd numbered loops.
123  *		To get the actual mailbox offset based on the tail it
124  *		is necessary to add bit 3 to bit 0 and clear bit 3.  This
125  *		gives us a valid range of 0x1 - 0xE.
126  * Head: Head index for remote FIFO
127  *		Head index follows the same format as the tail index.
128  * Rsvd0: Reserved 0 portion of the mailbox header
129  * CRC: Running CRC for all data since connect plus current message header
130  * Size: Maximum message size - Applies only to connect headers
131  *		The maximum message size is provided during connect to avoid
132  *		jamming the mailbox with messages that do not fit.
133  * Err_no: Error number - Applies only to error headers
134  *		The error number provides an indication of the type of error
135  *		experienced.
136  */
137 
138 /* macros for retrieving and setting header values */
139 #define FM10K_MSG_HDR_MASK(name) \
140 	((0x1u << FM10K_MSG_##name##_SIZE) - 1)
141 #define FM10K_MSG_HDR_FIELD_SET(value, name) \
142 	(((u32)(value) & FM10K_MSG_HDR_MASK(name)) << FM10K_MSG_##name##_SHIFT)
143 #define FM10K_MSG_HDR_FIELD_GET(value, name) \
144 	((u16)((value) >> FM10K_MSG_##name##_SHIFT) & FM10K_MSG_HDR_MASK(name))
145 
146 /* offsets shared between all headers */
147 #define FM10K_MSG_TYPE_SHIFT			0
148 #define FM10K_MSG_TYPE_SIZE			4
149 #define FM10K_MSG_TAIL_SHIFT			4
150 #define FM10K_MSG_TAIL_SIZE			4
151 #define FM10K_MSG_HEAD_SHIFT			8
152 #define FM10K_MSG_HEAD_SIZE			4
153 #define FM10K_MSG_RSVD0_SHIFT			12
154 #define FM10K_MSG_RSVD0_SIZE			4
155 
156 /* offsets for data/disconnect headers */
157 #define FM10K_MSG_CRC_SHIFT			16
158 #define FM10K_MSG_CRC_SIZE			16
159 
160 /* offsets for connect headers */
161 #define FM10K_MSG_CONNECT_SIZE_SHIFT		16
162 #define FM10K_MSG_CONNECT_SIZE_SIZE		16
163 
164 /* offsets for error headers */
165 #define FM10K_MSG_ERR_NO_SHIFT			16
166 #define FM10K_MSG_ERR_NO_SIZE			16
167 
168 enum fm10k_msg_type {
169 	FM10K_MSG_DATA			= 0x8,
170 	FM10K_MSG_CONNECT		= 0xC,
171 	FM10K_MSG_DISCONNECT		= 0xD,
172 	FM10K_MSG_ERROR			= 0xE,
173 };
174 
175 /* HNI/SM Mailbox FIFO format
176  *    3                   2                   1                   0
177  *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
178  * +-------+-----------------------+-------+-----------------------+
179  * | Error |      Remote Head      |Version|      Local Tail       |
180  * +-------+-----------------------+-------+-----------------------+
181  * |                                                               |
182  * .                        Local FIFO Data                        .
183  * .                                                               .
184  * +-------+-----------------------+-------+-----------------------+
185  *
186  * The layout above describes the format for the FIFOs used by the host
187  * network interface and the switch manager to communicate messages back
188  * and forth.  Both the HNI and the switch maintain one such FIFO.  The
189  * layout in memory has the switch manager FIFO followed immediately by
190  * the HNI FIFO.  For this reason I am using just the pointer to the
191  * HNI FIFO in the mailbox ops as the offset between the two is fixed.
192  *
193  * The header for the FIFO is broken out into the following fields:
194  * Local Tail:  Offset into FIFO region for next DWORD to write.
195  * Version:  Version info for mailbox, only values of 0/1 are supported.
196  * Remote Head:  Offset into remote FIFO to indicate how much we have read.
197  * Error: Error indication, values TBD.
198  */
199 
200 /* version number for switch manager mailboxes */
201 #define FM10K_SM_MBX_VERSION		1
202 #define FM10K_SM_MBX_FIFO_LEN		(FM10K_MBMEM_PF_XOR - 1)
203 
204 /* offsets shared between all SM FIFO headers */
205 #define FM10K_MSG_SM_TAIL_SHIFT			0
206 #define FM10K_MSG_SM_TAIL_SIZE			12
207 #define FM10K_MSG_SM_VER_SHIFT			12
208 #define FM10K_MSG_SM_VER_SIZE			4
209 #define FM10K_MSG_SM_HEAD_SHIFT			16
210 #define FM10K_MSG_SM_HEAD_SIZE			12
211 #define FM10K_MSG_SM_ERR_SHIFT			28
212 #define FM10K_MSG_SM_ERR_SIZE			4
213 
214 /* All error messages returned by mailbox functions
215  * The value -511 is 0xFE01 in hex.  The idea is to order the errors
216  * from 0xFE01 - 0xFEFF so error codes are easily visible in the mailbox
217  * messages.  This also helps to avoid error number collisions as Linux
218  * doesn't appear to use error numbers 256 - 511.
219  */
220 #define FM10K_MBX_ERR(_n) ((_n) - 512)
221 #define FM10K_MBX_ERR_NO_MBX		FM10K_MBX_ERR(0x01)
222 #define FM10K_MBX_ERR_NO_SPACE		FM10K_MBX_ERR(0x03)
223 #define FM10K_MBX_ERR_TAIL		FM10K_MBX_ERR(0x05)
224 #define FM10K_MBX_ERR_HEAD		FM10K_MBX_ERR(0x06)
225 #define FM10K_MBX_ERR_SRC		FM10K_MBX_ERR(0x08)
226 #define FM10K_MBX_ERR_TYPE		FM10K_MBX_ERR(0x09)
227 #define FM10K_MBX_ERR_SIZE		FM10K_MBX_ERR(0x0B)
228 #define FM10K_MBX_ERR_BUSY		FM10K_MBX_ERR(0x0C)
229 #define FM10K_MBX_ERR_RSVD0		FM10K_MBX_ERR(0x0E)
230 #define FM10K_MBX_ERR_CRC		FM10K_MBX_ERR(0x0F)
231 
232 #define FM10K_MBX_CRC_SEED		0xFFFF
233 
234 struct fm10k_mbx_ops {
235 	s32 (*connect)(struct fm10k_hw *, struct fm10k_mbx_info *);
236 	void (*disconnect)(struct fm10k_hw *, struct fm10k_mbx_info *);
237 	bool (*rx_ready)(struct fm10k_mbx_info *);
238 	bool (*tx_ready)(struct fm10k_mbx_info *, u16);
239 	bool (*tx_complete)(struct fm10k_mbx_info *);
240 	s32 (*enqueue_tx)(struct fm10k_hw *, struct fm10k_mbx_info *,
241 			  const u32 *);
242 	s32 (*process)(struct fm10k_hw *, struct fm10k_mbx_info *);
243 	s32 (*register_handlers)(struct fm10k_mbx_info *,
244 				 const struct fm10k_msg_data *);
245 };
246 
247 struct fm10k_mbx_fifo {
248 	u32 *buffer;
249 	u16 head;
250 	u16 tail;
251 	u16 size;
252 };
253 
254 /* size of buffer to be stored in mailbox for FIFOs */
255 #define FM10K_MBX_TX_BUFFER_SIZE	512
256 #define FM10K_MBX_RX_BUFFER_SIZE	128
257 #define FM10K_MBX_BUFFER_SIZE \
258 	(FM10K_MBX_TX_BUFFER_SIZE + FM10K_MBX_RX_BUFFER_SIZE)
259 
260 /* minimum and maximum message size in dwords */
261 #define FM10K_MBX_MSG_MAX_SIZE \
262 	((FM10K_MBX_TX_BUFFER_SIZE - 1) & (FM10K_MBX_RX_BUFFER_SIZE - 1))
263 #define FM10K_VFMBX_MSG_MTU	((FM10K_VFMBMEM_LEN / 2) - 1)
264 
265 #define FM10K_MBX_INIT_TIMEOUT	2000 /* number of retries on mailbox */
266 #define FM10K_MBX_INIT_DELAY	500  /* microseconds between retries */
267 
268 struct fm10k_mbx_info {
269 	/* function pointers for mailbox operations */
270 	struct fm10k_mbx_ops ops;
271 	const struct fm10k_msg_data *msg_data;
272 
273 	/* message FIFOs */
274 	struct fm10k_mbx_fifo rx;
275 	struct fm10k_mbx_fifo tx;
276 
277 	/* delay for handling timeouts */
278 	u32 timeout;
279 	u32 udelay;
280 
281 	/* mailbox state info */
282 	u32 mbx_reg, mbmem_reg, mbx_lock, mbx_hdr;
283 	u16 max_size, mbmem_len;
284 	u16 tail, tail_len, pulled;
285 	u16 head, head_len, pushed;
286 	u16 local, remote;
287 	enum fm10k_mbx_state state;
288 
289 	/* result of last mailbox test */
290 	s32 test_result;
291 
292 	/* statistics */
293 	u64 tx_busy;
294 	u64 tx_dropped;
295 	u64 tx_messages;
296 	u64 tx_dwords;
297 	u64 tx_mbmem_pulled;
298 	u64 rx_messages;
299 	u64 rx_dwords;
300 	u64 rx_mbmem_pushed;
301 	u64 rx_parse_err;
302 
303 	/* Buffer to store messages */
304 	u32 buffer[FM10K_MBX_BUFFER_SIZE];
305 };
306 
307 s32 fm10k_pfvf_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
308 			const struct fm10k_msg_data *, u8);
309 s32 fm10k_sm_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
310 		      const struct fm10k_msg_data *);
311 
312 #endif /* _FM10K_MBX_H_ */
313