1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2013 - 2015 Intel Corporation 3 */ 4 5 #ifndef _FM10K_MBX_H_ 6 #define _FM10K_MBX_H_ 7 8 /* forward declaration */ 9 struct fm10k_mbx_info; 10 11 #include "fm10k_type.h" 12 #include "fm10k_tlv.h" 13 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 32 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) 38 39 /* Delays/timeouts */ 40 #define FM10K_MBX_DISCONNECT_TIMEOUT 500 41 #define FM10K_MBX_POLL_DELAY 19 42 #define FM10K_MBX_INT_DELAY 20 43 44 #define FM10K_WRITE_MBX(hw, reg, value) FM10K_WRITE_REG(hw, reg, value) 45 46 /* PF/VF Mailbox state machine 47 * 48 * +----------+ connect() +----------+ 49 * | CLOSED | --------------> | CONNECT | 50 * +----------+ +----------+ 51 * ^ ^ | 52 * | rcv: rcv: | | rcv: 53 * | Connect Disconnect | | Connect 54 * | Disconnect Error | | Data 55 * | | | 56 * | | V 57 * +----------+ disconnect() +----------+ 58 * |DISCONNECT| <-------------- | OPEN | 59 * +----------+ +----------+ 60 * 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. 82 */ 83 enum fm10k_mbx_state { 84 FM10K_STATE_CLOSED, 85 FM10K_STATE_CONNECT, 86 FM10K_STATE_OPEN, 87 FM10K_STATE_DISCONNECT, 88 }; 89 90 /* PF/VF Mailbox header format 91 * 3 2 1 0 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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 96 * 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 120 * experienced. 121 */ 122 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)) 130 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 140 141 /* offsets for data/disconnect headers */ 142 #define FM10K_MSG_CRC_SHIFT 16 143 #define FM10K_MSG_CRC_SIZE 16 144 145 /* offsets for connect headers */ 146 #define FM10K_MSG_CONNECT_SIZE_SHIFT 16 147 #define FM10K_MSG_CONNECT_SIZE_SIZE 16 148 149 /* offsets for error headers */ 150 #define FM10K_MSG_ERR_NO_SHIFT 16 151 #define FM10K_MSG_ERR_NO_SIZE 16 152 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, 158 }; 159 160 /* HNI/SM Mailbox FIFO format 161 * 3 2 1 0 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 * +-------+-----------------------+-------+-----------------------+ 166 * | | 167 * . Local FIFO Data . 168 * . . 169 * +-------+-----------------------+-------+-----------------------+ 170 * 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. 177 * 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. 183 */ 184 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) 188 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 198 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. 204 */ 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) 216 217 #define FM10K_MBX_CRC_SEED 0xFFFF 218 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 *, 226 const u32 *); 227 s32 (*process)(struct fm10k_hw *, struct fm10k_mbx_info *); 228 s32 (*register_handlers)(struct fm10k_mbx_info *, 229 const struct fm10k_msg_data *); 230 }; 231 232 struct fm10k_mbx_fifo { 233 u32 *buffer; 234 u16 head; 235 u16 tail; 236 u16 size; 237 }; 238 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) 244 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) 249 250 #define FM10K_MBX_INIT_TIMEOUT 2000 /* number of retries on mailbox */ 251 #define FM10K_MBX_INIT_DELAY 500 /* microseconds between retries */ 252 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; 257 258 /* message FIFOs */ 259 struct fm10k_mbx_fifo rx; 260 struct fm10k_mbx_fifo tx; 261 262 /* delay for handling timeouts */ 263 u32 timeout; 264 u32 usec_delay; 265 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; 271 u16 local, remote; 272 enum fm10k_mbx_state state; 273 274 /* result of last mailbox test */ 275 s32 test_result; 276 277 /* statistics */ 278 u64 tx_busy; 279 u64 tx_dropped; 280 u64 tx_messages; 281 u64 tx_dwords; 282 u64 tx_mbmem_pulled; 283 u64 rx_messages; 284 u64 rx_dwords; 285 u64 rx_mbmem_pushed; 286 u64 rx_parse_err; 287 288 /* Buffer to store messages */ 289 u32 buffer[FM10K_MBX_BUFFER_SIZE]; 290 }; 291 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 *); 296 297 #endif /* _FM10K_MBX_H_ */ 298