63325845d2ff16932c208c84912b234006407413
[dpdk.git] / lib / librte_pmd_fm10k / base / fm10k_mbx.h
1 /*******************************************************************************
2
3 Copyright (c) 2013 - 2015, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #ifndef _FM10K_MBX_H_
35 #define _FM10K_MBX_H_
36
37 /* forward declaration */
38 struct fm10k_mbx_info;
39
40 #include "fm10k_type.h"
41 #include "fm10k_tlv.h"
42
43 /* PF Mailbox Registers */
44 #define FM10K_MBMEM(_n)         ((_n) + 0x18000)
45 #define FM10K_MBMEM_VF(_n, _m)  (((_n) * 0x10) + (_m) + 0x18000)
46 #define FM10K_MBMEM_SM(_n)      ((_n) + 0x18400)
47 #define FM10K_MBMEM_PF(_n)      ((_n) + 0x18600)
48 /* XOR provides means of switching from Tx to Rx FIFO */
49 #define FM10K_MBMEM_PF_XOR      (FM10K_MBMEM_SM(0) ^ FM10K_MBMEM_PF(0))
50 #define FM10K_MBX(_n)           ((_n) + 0x18800)
51 #define FM10K_MBX_OWNER                         0x00000001
52 #define FM10K_MBX_REQ                           0x00000002
53 #define FM10K_MBX_ACK                           0x00000004
54 #define FM10K_MBX_REQ_INTERRUPT                 0x00000008
55 #define FM10K_MBX_ACK_INTERRUPT                 0x00000010
56 #define FM10K_MBX_INTERRUPT_ENABLE              0x00000020
57 #define FM10K_MBX_INTERRUPT_DISABLE             0x00000040
58 #define FM10K_MBICR(_n)         ((_n) + 0x18840)
59 #define FM10K_GMBX              0x18842
60
61 /* VF Mailbox Registers */
62 #define FM10K_VFMBX             0x00010
63 #define FM10K_VFMBMEM(_n)       ((_n) + 0x00020)
64 #define FM10K_VFMBMEM_LEN       16
65 #define FM10K_VFMBMEM_VF_XOR    (FM10K_VFMBMEM_LEN / 2)
66
67 /* Delays/timeouts */
68 #define FM10K_MBX_DISCONNECT_TIMEOUT            500
69 #define FM10K_MBX_POLL_DELAY                    19
70 #define FM10K_MBX_INT_DELAY                     20
71
72 #define FM10K_WRITE_MBX(hw, reg, value) FM10K_WRITE_REG(hw, reg, value)
73
74 /* PF/VF Mailbox state machine
75  *
76  * +----------+     connect()   +----------+
77  * |  CLOSED  | --------------> |  CONNECT |
78  * +----------+                 +----------+
79  *   ^                            ^      |
80  *   | rcv:           rcv:        |      | rcv:
81  *   |  Connect        Disconnect |      |  Connect
82  *   |  Disconnect     Error      |      |  Data
83  *   |                            |      |
84  *   |                            |      V
85  * +----------+   disconnect()  +----------+
86  * |DISCONNECT| <-------------- |   OPEN   |
87  * +----------+                 +----------+
88  *
89  * The diagram above describes the PF/VF mailbox state machine.  There
90  * are four main states to this machine.
91  * Closed: This state represents a mailbox that is in a standby state
92  *         with interrupts disabled.  In this state the mailbox should not
93  *         read the mailbox or write any data.  The only means of exiting
94  *         this state is for the system to make the connect() call for the
95  *         mailbox, it will then transition to the connect state.
96  * Connect: In this state the mailbox is seeking a connection.  It will
97  *          post a connect message with no specified destination and will
98  *          wait for a reply from the other side of the mailbox.  This state
99  *          is exited when either a connect with the local mailbox as the
100  *          destination is received or when a data message is received with
101  *          a valid sequence number.
102  * Open: In this state the mailbox is able to transfer data between the local
103  *       entity and the remote.  It will fall back to connect in the event of
104  *       receiving either an error message, or a disconnect message.  It will
105  *       transition to disconnect on a call to disconnect();
106  * Disconnect: In this state the mailbox is attempting to gracefully terminate
107  *             the connection.  It will do so at the first point where it knows
108  *             that the remote endpoint is either done sending, or when the
109  *             remote endpoint has fallen back into connect.
110  */
111 enum fm10k_mbx_state {
112         FM10K_STATE_CLOSED,
113         FM10K_STATE_CONNECT,
114         FM10K_STATE_OPEN,
115         FM10K_STATE_DISCONNECT,
116 };
117
118 /* PF/VF Mailbox header format
119  *    3                   2                   1                   0
120  *  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
121  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122  * |        Size/Err_no/CRC        | Rsvd0 | Head  | Tail  | Type  |
123  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124  *
125  * The layout above describes the format for the header used in the PF/VF
126  * mailbox.  The header is broken out into the following fields:
127  * Type: There are 4 supported message types
128  *              0x8: Data header - used to transport message data
129  *              0xC: Connect header - used to establish connection
130  *              0xD: Disconnect header - used to tear down a connection
131  *              0xE: Error header - used to address message exceptions
132  * Tail: Tail index for local FIFO
133  *              Tail index actually consists of two parts.  The MSB of
134  *              the head is a loop tracker, it is 0 on an even numbered
135  *              loop through the FIFO, and 1 on the odd numbered loops.
136  *              To get the actual mailbox offset based on the tail it
137  *              is necessary to add bit 3 to bit 0 and clear bit 3.  This
138  *              gives us a valid range of 0x1 - 0xE.
139  * Head: Head index for remote FIFO
140  *              Head index follows the same format as the tail index.
141  * Rsvd0: Reserved 0 portion of the mailbox header
142  * CRC: Running CRC for all data since connect plus current message header
143  * Size: Maximum message size - Applies only to connect headers
144  *              The maximum message size is provided during connect to avoid
145  *              jamming the mailbox with messages that do not fit.
146  * Err_no: Error number - Applies only to error headers
147  *              The error number provides a indication of the type of error
148  *              experienced.
149  */
150
151 /* macros for retriving and setting header values */
152 #define FM10K_MSG_HDR_MASK(name) \
153         ((0x1u << FM10K_MSG_##name##_SIZE) - 1)
154 #define FM10K_MSG_HDR_FIELD_SET(value, name) \
155         (((u32)(value) & FM10K_MSG_HDR_MASK(name)) << FM10K_MSG_##name##_SHIFT)
156 #define FM10K_MSG_HDR_FIELD_GET(value, name) \
157         ((u16)((value) >> FM10K_MSG_##name##_SHIFT) & FM10K_MSG_HDR_MASK(name))
158
159 /* offsets shared between all headers */
160 #define FM10K_MSG_TYPE_SHIFT                    0
161 #define FM10K_MSG_TYPE_SIZE                     4
162 #define FM10K_MSG_TAIL_SHIFT                    4
163 #define FM10K_MSG_TAIL_SIZE                     4
164 #define FM10K_MSG_HEAD_SHIFT                    8
165 #define FM10K_MSG_HEAD_SIZE                     4
166 #define FM10K_MSG_RSVD0_SHIFT                   12
167 #define FM10K_MSG_RSVD0_SIZE                    4
168
169 /* offsets for data/disconnect headers */
170 #define FM10K_MSG_CRC_SHIFT                     16
171 #define FM10K_MSG_CRC_SIZE                      16
172
173 /* offsets for connect headers */
174 #define FM10K_MSG_CONNECT_SIZE_SHIFT            16
175 #define FM10K_MSG_CONNECT_SIZE_SIZE             16
176
177 /* offsets for error headers */
178 #define FM10K_MSG_ERR_NO_SHIFT                  16
179 #define FM10K_MSG_ERR_NO_SIZE                   16
180
181 enum fm10k_msg_type {
182         FM10K_MSG_DATA                  = 0x8,
183         FM10K_MSG_CONNECT               = 0xC,
184         FM10K_MSG_DISCONNECT            = 0xD,
185         FM10K_MSG_ERROR                 = 0xE,
186 };
187
188 /* HNI/SM Mailbox FIFO format
189  *    3                   2                   1                   0
190  *  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
191  * +-------+-----------------------+-------+-----------------------+
192  * | Error |      Remote Head      |Version|      Local Tail       |
193  * +-------+-----------------------+-------+-----------------------+
194  * |                                                               |
195  * .                        Local FIFO Data                        .
196  * .                                                               .
197  * +-------+-----------------------+-------+-----------------------+
198  *
199  * The layout above describes the format for the FIFOs used by the host
200  * network interface and the switch manager to communicate messages back
201  * and forth.  Both the HNI and the switch maintain one such FIFO.  The
202  * layout in memory has the switch manager FIFO followed immediately by
203  * the HNI FIFO.  For this reason I am using just the pointer to the
204  * HNI FIFO in the mailbox ops as the offset between the two is fixed.
205  *
206  * The header for the FIFO is broken out into the following fields:
207  * Local Tail:  Offset into FIFO region for next DWORD to write.
208  * Version:  Version info for mailbox, only values of 0/1 are supported.
209  * Remote Head:  Offset into remote FIFO to indicate how much we have read.
210  * Error: Error indication, values TBD.
211  */
212
213 /* version number for switch manager mailboxes */
214 #define FM10K_SM_MBX_VERSION            1
215 #define FM10K_SM_MBX_FIFO_LEN           (FM10K_MBMEM_PF_XOR - 1)
216 #define FM10K_SM_MBX_FIFO_HDR_LEN       1
217
218 /* offsets shared between all SM FIFO headers */
219 #define FM10K_MSG_SM_TAIL_SHIFT                 0
220 #define FM10K_MSG_SM_TAIL_SIZE                  12
221 #define FM10K_MSG_SM_VER_SHIFT                  12
222 #define FM10K_MSG_SM_VER_SIZE                   4
223 #define FM10K_MSG_SM_HEAD_SHIFT                 16
224 #define FM10K_MSG_SM_HEAD_SIZE                  12
225 #define FM10K_MSG_SM_ERR_SHIFT                  28
226 #define FM10K_MSG_SM_ERR_SIZE                   4
227
228 /* All error messages returned by mailbox functions
229  * The value -511 is 0xFE01 in hex.  The idea is to order the errors
230  * from 0xFE01 - 0xFEFF so error codes are easily visible in the mailbox
231  * messages.  This also helps to avoid error number collisions as Linux
232  * doesn't appear to use error numbers 256 - 511.
233  */
234 #define FM10K_MBX_ERR(_n) ((_n) - 512)
235 #define FM10K_MBX_ERR_NO_MBX            FM10K_MBX_ERR(0x01)
236 #define FM10K_MBX_ERR_NO_MSG            FM10K_MBX_ERR(0x02)
237 #define FM10K_MBX_ERR_NO_SPACE          FM10K_MBX_ERR(0x03)
238 #define FM10K_MBX_ERR_LOCK              FM10K_MBX_ERR(0x04)
239 #define FM10K_MBX_ERR_TAIL              FM10K_MBX_ERR(0x05)
240 #define FM10K_MBX_ERR_HEAD              FM10K_MBX_ERR(0x06)
241 #define FM10K_MBX_ERR_DST               FM10K_MBX_ERR(0x07)
242 #define FM10K_MBX_ERR_SRC               FM10K_MBX_ERR(0x08)
243 #define FM10K_MBX_ERR_TYPE              FM10K_MBX_ERR(0x09)
244 #define FM10K_MBX_ERR_LEN               FM10K_MBX_ERR(0x0A)
245 #define FM10K_MBX_ERR_SIZE              FM10K_MBX_ERR(0x0B)
246 #define FM10K_MBX_ERR_BUSY              FM10K_MBX_ERR(0x0C)
247 #define FM10K_MBX_ERR_VALUE             FM10K_MBX_ERR(0x0D)
248 #define FM10K_MBX_ERR_RSVD0             FM10K_MBX_ERR(0x0E)
249 #define FM10K_MBX_ERR_CRC               FM10K_MBX_ERR(0x0F)
250
251 #define FM10K_MBX_CRC_SEED              0xFFFF
252
253 struct fm10k_mbx_ops {
254         s32 (*connect)(struct fm10k_hw *, struct fm10k_mbx_info *);
255         void (*disconnect)(struct fm10k_hw *, struct fm10k_mbx_info *);
256         bool (*rx_ready)(struct fm10k_mbx_info *);
257         bool (*tx_ready)(struct fm10k_mbx_info *, u16);
258         bool (*tx_complete)(struct fm10k_mbx_info *);
259         s32 (*enqueue_tx)(struct fm10k_hw *, struct fm10k_mbx_info *,
260                           const u32 *);
261         s32 (*process)(struct fm10k_hw *, struct fm10k_mbx_info *);
262         s32 (*register_handlers)(struct fm10k_mbx_info *,
263                                  const struct fm10k_msg_data *);
264 };
265
266 struct fm10k_mbx_fifo {
267         u32 *buffer;
268         u16 head;
269         u16 tail;
270         u16 size;
271 };
272
273 /* size of buffer to be stored in mailbox for FIFOs */
274 #define FM10K_MBX_TX_BUFFER_SIZE        512
275 #define FM10K_MBX_RX_BUFFER_SIZE        128
276 #define FM10K_MBX_BUFFER_SIZE \
277         (FM10K_MBX_TX_BUFFER_SIZE + FM10K_MBX_RX_BUFFER_SIZE)
278
279 /* minimum and maximum message size in dwords */
280 #define FM10K_MBX_MSG_MAX_SIZE \
281         ((FM10K_MBX_TX_BUFFER_SIZE - 1) & (FM10K_MBX_RX_BUFFER_SIZE - 1))
282 #define FM10K_VFMBX_MSG_MTU     ((FM10K_VFMBMEM_LEN / 2) - 1)
283
284 #define FM10K_MBX_INIT_TIMEOUT  2000 /* number of retries on mailbox */
285 #define FM10K_MBX_INIT_DELAY    500  /* microseconds between retries */
286
287 struct fm10k_mbx_info {
288         /* function pointers for mailbox operations */
289         struct fm10k_mbx_ops ops;
290         const struct fm10k_msg_data *msg_data;
291
292         /* message FIFOs */
293         struct fm10k_mbx_fifo rx;
294         struct fm10k_mbx_fifo tx;
295
296         /* delay for handling timeouts */
297         u32 timeout;
298         u32 usec_delay;
299
300         /* mailbox state info */
301         u32 mbx_reg, mbmem_reg, mbx_lock, mbx_hdr;
302         u16 max_size, mbmem_len;
303         u16 tail, tail_len, pulled;
304         u16 head, head_len, pushed;
305         u16 local, remote;
306         enum fm10k_mbx_state state;
307
308         /* result of last mailbox test */
309         s32 test_result;
310
311         /* statistics */
312         u64 tx_busy;
313         u64 tx_dropped;
314         u64 tx_messages;
315         u64 tx_dwords;
316         u64 rx_messages;
317         u64 rx_dwords;
318         u64 rx_parse_err;
319
320         /* Buffer to store messages */
321         u32 buffer[FM10K_MBX_BUFFER_SIZE];
322 };
323
324 s32 fm10k_pfvf_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
325                         const struct fm10k_msg_data *, u8);
326 s32 fm10k_sm_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
327                       const struct fm10k_msg_data *);
328
329 #endif /* _FM10K_MBX_H_ */