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