1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2013 - 2015 Intel Corporation
8 /* forward declaration */
11 #include "fm10k_type.h"
12 #include "fm10k_tlv.h"
14 /* PF Mailbox Registers */
15 #define FM10K_MBMEM(_n) ((_n) + 0x18000)
16 #define FM10K_MBMEM_VF(_n, _m) (((_n) * 0x10) + (_m) + 0x18000)
17 #define FM10K_MBMEM_SM(_n) ((_n) + 0x18400)
18 #define FM10K_MBMEM_PF(_n) ((_n) + 0x18600)
19 /* XOR provides means of switching from Tx to Rx FIFO */
20 #define FM10K_MBMEM_PF_XOR (FM10K_MBMEM_SM(0) ^ FM10K_MBMEM_PF(0))
21 #define FM10K_MBX(_n) ((_n) + 0x18800)
22 #define FM10K_MBX_REQ 0x00000002
23 #define FM10K_MBX_ACK 0x00000004
24 #define FM10K_MBX_REQ_INTERRUPT 0x00000008
25 #define FM10K_MBX_ACK_INTERRUPT 0x00000010
26 #define FM10K_MBX_INTERRUPT_ENABLE 0x00000020
27 #define FM10K_MBX_INTERRUPT_DISABLE 0x00000040
28 #define FM10K_MBX_GLOBAL_REQ_INTERRUPT 0x00000200
29 #define FM10K_MBX_GLOBAL_ACK_INTERRUPT 0x00000400
30 #define FM10K_MBICR(_n) ((_n) + 0x18840)
31 #define FM10K_GMBX 0x18842
33 /* VF Mailbox Registers */
34 #define FM10K_VFMBX 0x00010
35 #define FM10K_VFMBMEM(_n) ((_n) + 0x00020)
36 #define FM10K_VFMBMEM_LEN 16
37 #define FM10K_VFMBMEM_VF_XOR (FM10K_VFMBMEM_LEN / 2)
40 #define FM10K_MBX_DISCONNECT_TIMEOUT 500
41 #define FM10K_MBX_POLL_DELAY 19
42 #define FM10K_MBX_INT_DELAY 20
44 #define FM10K_WRITE_MBX(hw, reg, value) FM10K_WRITE_REG(hw, reg, value)
46 /* PF/VF Mailbox state machine
48 * +----------+ connect() +----------+
49 * | CLOSED | --------------> | CONNECT |
50 * +----------+ +----------+
52 * | rcv: rcv: | | rcv:
53 * | Connect Disconnect | | Connect
54 * | Disconnect Error | | Data
57 * +----------+ disconnect() +----------+
58 * |DISCONNECT| <-------------- | OPEN |
59 * +----------+ +----------+
61 * The diagram above describes the PF/VF mailbox state machine. There
62 * are four main states to this machine.
63 * Closed: This state represents a mailbox that is in a standby state
64 * with interrupts disabled. In this state the mailbox should not
65 * read the mailbox or write any data. The only means of exiting
66 * this state is for the system to make the connect() call for the
67 * mailbox, it will then transition to the connect state.
68 * Connect: In this state the mailbox is seeking a connection. It will
69 * post a connect message with no specified destination and will
70 * wait for a reply from the other side of the mailbox. This state
71 * is exited when either a connect with the local mailbox as the
72 * destination is received or when a data message is received with
73 * a valid sequence number.
74 * Open: In this state the mailbox is able to transfer data between the local
75 * entity and the remote. It will fall back to connect in the event of
76 * receiving either an error message, or a disconnect message. It will
77 * transition to disconnect on a call to disconnect();
78 * Disconnect: In this state the mailbox is attempting to gracefully terminate
79 * the connection. It will do so at the first point where it knows
80 * that the remote endpoint is either done sending, or when the
81 * remote endpoint has fallen back into connect.
83 enum fm10k_mbx_state {
87 FM10K_STATE_DISCONNECT,
90 /* PF/VF Mailbox header format
92 * 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
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | Size/Err_no/CRC | Rsvd0 | Head | Tail | Type |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 * The layout above describes the format for the header used in the PF/VF
98 * mailbox. The header is broken out into the following fields:
99 * Type: There are 4 supported message types
100 * 0x8: Data header - used to transport message data
101 * 0xC: Connect header - used to establish connection
102 * 0xD: Disconnect header - used to tear down a connection
103 * 0xE: Error header - used to address message exceptions
104 * Tail: Tail index for local FIFO
105 * Tail index actually consists of two parts. The MSB of
106 * the head is a loop tracker, it is 0 on an even numbered
107 * loop through the FIFO, and 1 on the odd numbered loops.
108 * To get the actual mailbox offset based on the tail it
109 * is necessary to add bit 3 to bit 0 and clear bit 3. This
110 * gives us a valid range of 0x1 - 0xE.
111 * Head: Head index for remote FIFO
112 * Head index follows the same format as the tail index.
113 * Rsvd0: Reserved 0 portion of the mailbox header
114 * CRC: Running CRC for all data since connect plus current message header
115 * Size: Maximum message size - Applies only to connect headers
116 * The maximum message size is provided during connect to avoid
117 * jamming the mailbox with messages that do not fit.
118 * Err_no: Error number - Applies only to error headers
119 * The error number provides an indication of the type of error
123 /* macros for retrieving and setting header values */
124 #define FM10K_MSG_HDR_MASK(name) \
125 ((0x1u << FM10K_MSG_##name##_SIZE) - 1)
126 #define FM10K_MSG_HDR_FIELD_SET(value, name) \
127 (((u32)(value) & FM10K_MSG_HDR_MASK(name)) << FM10K_MSG_##name##_SHIFT)
128 #define FM10K_MSG_HDR_FIELD_GET(value, name) \
129 ((u16)((value) >> FM10K_MSG_##name##_SHIFT) & FM10K_MSG_HDR_MASK(name))
131 /* offsets shared between all headers */
132 #define FM10K_MSG_TYPE_SHIFT 0
133 #define FM10K_MSG_TYPE_SIZE 4
134 #define FM10K_MSG_TAIL_SHIFT 4
135 #define FM10K_MSG_TAIL_SIZE 4
136 #define FM10K_MSG_HEAD_SHIFT 8
137 #define FM10K_MSG_HEAD_SIZE 4
138 #define FM10K_MSG_RSVD0_SHIFT 12
139 #define FM10K_MSG_RSVD0_SIZE 4
141 /* offsets for data/disconnect headers */
142 #define FM10K_MSG_CRC_SHIFT 16
143 #define FM10K_MSG_CRC_SIZE 16
145 /* offsets for connect headers */
146 #define FM10K_MSG_CONNECT_SIZE_SHIFT 16
147 #define FM10K_MSG_CONNECT_SIZE_SIZE 16
149 /* offsets for error headers */
150 #define FM10K_MSG_ERR_NO_SHIFT 16
151 #define FM10K_MSG_ERR_NO_SIZE 16
153 enum fm10k_msg_type {
154 FM10K_MSG_DATA = 0x8,
155 FM10K_MSG_CONNECT = 0xC,
156 FM10K_MSG_DISCONNECT = 0xD,
157 FM10K_MSG_ERROR = 0xE,
160 /* HNI/SM Mailbox FIFO format
162 * 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
163 * +-------+-----------------------+-------+-----------------------+
164 * | Error | Remote Head |Version| Local Tail |
165 * +-------+-----------------------+-------+-----------------------+
167 * . Local FIFO Data .
169 * +-------+-----------------------+-------+-----------------------+
171 * The layout above describes the format for the FIFOs used by the host
172 * network interface and the switch manager to communicate messages back
173 * and forth. Both the HNI and the switch maintain one such FIFO. The
174 * layout in memory has the switch manager FIFO followed immediately by
175 * the HNI FIFO. For this reason I am using just the pointer to the
176 * HNI FIFO in the mailbox ops as the offset between the two is fixed.
178 * The header for the FIFO is broken out into the following fields:
179 * Local Tail: Offset into FIFO region for next DWORD to write.
180 * Version: Version info for mailbox, only values of 0/1 are supported.
181 * Remote Head: Offset into remote FIFO to indicate how much we have read.
182 * Error: Error indication, values TBD.
185 /* version number for switch manager mailboxes */
186 #define FM10K_SM_MBX_VERSION 1
187 #define FM10K_SM_MBX_FIFO_LEN (FM10K_MBMEM_PF_XOR - 1)
189 /* offsets shared between all SM FIFO headers */
190 #define FM10K_MSG_SM_TAIL_SHIFT 0
191 #define FM10K_MSG_SM_TAIL_SIZE 12
192 #define FM10K_MSG_SM_VER_SHIFT 12
193 #define FM10K_MSG_SM_VER_SIZE 4
194 #define FM10K_MSG_SM_HEAD_SHIFT 16
195 #define FM10K_MSG_SM_HEAD_SIZE 12
196 #define FM10K_MSG_SM_ERR_SHIFT 28
197 #define FM10K_MSG_SM_ERR_SIZE 4
199 /* All error messages returned by mailbox functions
200 * The value -511 is 0xFE01 in hex. The idea is to order the errors
201 * from 0xFE01 - 0xFEFF so error codes are easily visible in the mailbox
202 * messages. This also helps to avoid error number collisions as Linux
203 * doesn't appear to use error numbers 256 - 511.
205 #define FM10K_MBX_ERR(_n) ((_n) - 512)
206 #define FM10K_MBX_ERR_NO_MBX FM10K_MBX_ERR(0x01)
207 #define FM10K_MBX_ERR_NO_SPACE FM10K_MBX_ERR(0x03)
208 #define FM10K_MBX_ERR_TAIL FM10K_MBX_ERR(0x05)
209 #define FM10K_MBX_ERR_HEAD FM10K_MBX_ERR(0x06)
210 #define FM10K_MBX_ERR_SRC FM10K_MBX_ERR(0x08)
211 #define FM10K_MBX_ERR_TYPE FM10K_MBX_ERR(0x09)
212 #define FM10K_MBX_ERR_SIZE FM10K_MBX_ERR(0x0B)
213 #define FM10K_MBX_ERR_BUSY FM10K_MBX_ERR(0x0C)
214 #define FM10K_MBX_ERR_RSVD0 FM10K_MBX_ERR(0x0E)
215 #define FM10K_MBX_ERR_CRC FM10K_MBX_ERR(0x0F)
217 #define FM10K_MBX_CRC_SEED 0xFFFF
219 struct fm10k_mbx_ops {
220 s32 (*connect)(struct fm10k_hw *, struct fm10k_mbx_info *);
221 void (*disconnect)(struct fm10k_hw *, struct fm10k_mbx_info *);
222 bool (*rx_ready)(struct fm10k_mbx_info *);
223 bool (*tx_ready)(struct fm10k_mbx_info *, u16);
224 bool (*tx_complete)(struct fm10k_mbx_info *);
225 s32 (*enqueue_tx)(struct fm10k_hw *, struct fm10k_mbx_info *,
227 s32 (*process)(struct fm10k_hw *, struct fm10k_mbx_info *);
228 s32 (*register_handlers)(struct fm10k_mbx_info *,
229 const struct fm10k_msg_data *);
232 struct fm10k_mbx_fifo {
239 /* size of buffer to be stored in mailbox for FIFOs */
240 #define FM10K_MBX_TX_BUFFER_SIZE 512
241 #define FM10K_MBX_RX_BUFFER_SIZE 128
242 #define FM10K_MBX_BUFFER_SIZE \
243 (FM10K_MBX_TX_BUFFER_SIZE + FM10K_MBX_RX_BUFFER_SIZE)
245 /* minimum and maximum message size in dwords */
246 #define FM10K_MBX_MSG_MAX_SIZE \
247 ((FM10K_MBX_TX_BUFFER_SIZE - 1) & (FM10K_MBX_RX_BUFFER_SIZE - 1))
248 #define FM10K_VFMBX_MSG_MTU ((FM10K_VFMBMEM_LEN / 2) - 1)
250 #define FM10K_MBX_INIT_TIMEOUT 2000 /* number of retries on mailbox */
251 #define FM10K_MBX_INIT_DELAY 500 /* microseconds between retries */
253 struct fm10k_mbx_info {
254 /* function pointers for mailbox operations */
255 struct fm10k_mbx_ops ops;
256 const struct fm10k_msg_data *msg_data;
259 struct fm10k_mbx_fifo rx;
260 struct fm10k_mbx_fifo tx;
262 /* delay for handling timeouts */
266 /* mailbox state info */
267 u32 mbx_reg, mbmem_reg, mbx_lock, mbx_hdr;
268 u16 max_size, mbmem_len;
269 u16 tail, tail_len, pulled;
270 u16 head, head_len, pushed;
272 enum fm10k_mbx_state state;
274 /* result of last mailbox test */
288 /* Buffer to store messages */
289 u32 buffer[FM10K_MBX_BUFFER_SIZE];
292 s32 fm10k_pfvf_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
293 const struct fm10k_msg_data *, u8);
294 s32 fm10k_sm_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *,
295 const struct fm10k_msg_data *);
297 #endif /* _FM10K_MBX_H_ */