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