1ae06c70bSJeff Kirsher /* SPDX-License-Identifier: GPL-2.0 */ 2*51dce24bSJeff Kirsher /* Copyright(c) 2013 - 2018 Intel Corporation. */ 36b1f201fSAlexander Duyck 46b1f201fSAlexander Duyck #ifndef _FM10K_MBX_H_ 56b1f201fSAlexander Duyck #define _FM10K_MBX_H_ 66b1f201fSAlexander Duyck 76b1f201fSAlexander Duyck /* forward declaration */ 86b1f201fSAlexander Duyck struct fm10k_mbx_info; 96b1f201fSAlexander Duyck 106b1f201fSAlexander Duyck #include "fm10k_type.h" 116b1f201fSAlexander Duyck #include "fm10k_tlv.h" 126b1f201fSAlexander Duyck 136b1f201fSAlexander Duyck /* PF Mailbox Registers */ 146b1f201fSAlexander Duyck #define FM10K_MBMEM(_n) ((_n) + 0x18000) 156b1f201fSAlexander Duyck #define FM10K_MBMEM_VF(_n, _m) (((_n) * 0x10) + (_m) + 0x18000) 166b1f201fSAlexander Duyck #define FM10K_MBMEM_SM(_n) ((_n) + 0x18400) 176b1f201fSAlexander Duyck #define FM10K_MBMEM_PF(_n) ((_n) + 0x18600) 186b1f201fSAlexander Duyck /* XOR provides means of switching from Tx to Rx FIFO */ 196b1f201fSAlexander Duyck #define FM10K_MBMEM_PF_XOR (FM10K_MBMEM_SM(0) ^ FM10K_MBMEM_PF(0)) 206b1f201fSAlexander Duyck #define FM10K_MBX(_n) ((_n) + 0x18800) 216b1f201fSAlexander Duyck #define FM10K_MBX_REQ 0x00000002 226b1f201fSAlexander Duyck #define FM10K_MBX_ACK 0x00000004 236b1f201fSAlexander Duyck #define FM10K_MBX_REQ_INTERRUPT 0x00000008 246b1f201fSAlexander Duyck #define FM10K_MBX_ACK_INTERRUPT 0x00000010 256b1f201fSAlexander Duyck #define FM10K_MBX_INTERRUPT_ENABLE 0x00000020 266b1f201fSAlexander Duyck #define FM10K_MBX_INTERRUPT_DISABLE 0x00000040 275e93cbadSNgai-Mint Kwan #define FM10K_MBX_GLOBAL_REQ_INTERRUPT 0x00000200 285e93cbadSNgai-Mint Kwan #define FM10K_MBX_GLOBAL_ACK_INTERRUPT 0x00000400 296b1f201fSAlexander Duyck #define FM10K_MBICR(_n) ((_n) + 0x18840) 306b1f201fSAlexander Duyck #define FM10K_GMBX 0x18842 316b1f201fSAlexander Duyck 326b1f201fSAlexander Duyck /* VF Mailbox Registers */ 336b1f201fSAlexander Duyck #define FM10K_VFMBX 0x00010 346b1f201fSAlexander Duyck #define FM10K_VFMBMEM(_n) ((_n) + 0x00020) 356b1f201fSAlexander Duyck #define FM10K_VFMBMEM_LEN 16 366b1f201fSAlexander Duyck #define FM10K_VFMBMEM_VF_XOR (FM10K_VFMBMEM_LEN / 2) 376b1f201fSAlexander Duyck 386b1f201fSAlexander Duyck /* Delays/timeouts */ 396b1f201fSAlexander Duyck #define FM10K_MBX_DISCONNECT_TIMEOUT 500 406b1f201fSAlexander Duyck #define FM10K_MBX_POLL_DELAY 19 416b1f201fSAlexander Duyck #define FM10K_MBX_INT_DELAY 20 426b1f201fSAlexander Duyck 436b1f201fSAlexander Duyck /* PF/VF Mailbox state machine 446b1f201fSAlexander Duyck * 456b1f201fSAlexander Duyck * +----------+ connect() +----------+ 466b1f201fSAlexander Duyck * | CLOSED | --------------> | CONNECT | 476b1f201fSAlexander Duyck * +----------+ +----------+ 486b1f201fSAlexander Duyck * ^ ^ | 496b1f201fSAlexander Duyck * | rcv: rcv: | | rcv: 506b1f201fSAlexander Duyck * | Connect Disconnect | | Connect 516b1f201fSAlexander Duyck * | Disconnect Error | | Data 526b1f201fSAlexander Duyck * | | | 536b1f201fSAlexander Duyck * | | V 546b1f201fSAlexander Duyck * +----------+ disconnect() +----------+ 556b1f201fSAlexander Duyck * |DISCONNECT| <-------------- | OPEN | 566b1f201fSAlexander Duyck * +----------+ +----------+ 576b1f201fSAlexander Duyck * 586b1f201fSAlexander Duyck * The diagram above describes the PF/VF mailbox state machine. There 596b1f201fSAlexander Duyck * are four main states to this machine. 606b1f201fSAlexander Duyck * Closed: This state represents a mailbox that is in a standby state 616b1f201fSAlexander Duyck * with interrupts disabled. In this state the mailbox should not 626b1f201fSAlexander Duyck * read the mailbox or write any data. The only means of exiting 636b1f201fSAlexander Duyck * this state is for the system to make the connect() call for the 646b1f201fSAlexander Duyck * mailbox, it will then transition to the connect state. 656b1f201fSAlexander Duyck * Connect: In this state the mailbox is seeking a connection. It will 666b1f201fSAlexander Duyck * post a connect message with no specified destination and will 676b1f201fSAlexander Duyck * wait for a reply from the other side of the mailbox. This state 686b1f201fSAlexander Duyck * is exited when either a connect with the local mailbox as the 696b1f201fSAlexander Duyck * destination is received or when a data message is received with 706b1f201fSAlexander Duyck * a valid sequence number. 716b1f201fSAlexander Duyck * Open: In this state the mailbox is able to transfer data between the local 726b1f201fSAlexander Duyck * entity and the remote. It will fall back to connect in the event of 736b1f201fSAlexander Duyck * receiving either an error message, or a disconnect message. It will 746b1f201fSAlexander Duyck * transition to disconnect on a call to disconnect(); 756b1f201fSAlexander Duyck * Disconnect: In this state the mailbox is attempting to gracefully terminate 766b1f201fSAlexander Duyck * the connection. It will do so at the first point where it knows 776b1f201fSAlexander Duyck * that the remote endpoint is either done sending, or when the 786b1f201fSAlexander Duyck * remote endpoint has fallen back into connect. 796b1f201fSAlexander Duyck */ 806b1f201fSAlexander Duyck enum fm10k_mbx_state { 816b1f201fSAlexander Duyck FM10K_STATE_CLOSED, 826b1f201fSAlexander Duyck FM10K_STATE_CONNECT, 836b1f201fSAlexander Duyck FM10K_STATE_OPEN, 846b1f201fSAlexander Duyck FM10K_STATE_DISCONNECT, 856b1f201fSAlexander Duyck }; 866b1f201fSAlexander Duyck 87b651957cSAlexander Duyck /* PF/VF Mailbox header format 88b651957cSAlexander Duyck * 3 2 1 0 89b651957cSAlexander Duyck * 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 90b651957cSAlexander Duyck * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 91b651957cSAlexander Duyck * | Size/Err_no/CRC | Rsvd0 | Head | Tail | Type | 92b651957cSAlexander Duyck * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 93b651957cSAlexander Duyck * 94b651957cSAlexander Duyck * The layout above describes the format for the header used in the PF/VF 95b651957cSAlexander Duyck * mailbox. The header is broken out into the following fields: 96b651957cSAlexander Duyck * Type: There are 4 supported message types 97b651957cSAlexander Duyck * 0x8: Data header - used to transport message data 98b651957cSAlexander Duyck * 0xC: Connect header - used to establish connection 99b651957cSAlexander Duyck * 0xD: Disconnect header - used to tear down a connection 100b651957cSAlexander Duyck * 0xE: Error header - used to address message exceptions 101b651957cSAlexander Duyck * Tail: Tail index for local FIFO 102b651957cSAlexander Duyck * Tail index actually consists of two parts. The MSB of 103b651957cSAlexander Duyck * the head is a loop tracker, it is 0 on an even numbered 104b651957cSAlexander Duyck * loop through the FIFO, and 1 on the odd numbered loops. 105b651957cSAlexander Duyck * To get the actual mailbox offset based on the tail it 106b651957cSAlexander Duyck * is necessary to add bit 3 to bit 0 and clear bit 3. This 107b651957cSAlexander Duyck * gives us a valid range of 0x1 - 0xE. 108b651957cSAlexander Duyck * Head: Head index for remote FIFO 109b651957cSAlexander Duyck * Head index follows the same format as the tail index. 110b651957cSAlexander Duyck * Rsvd0: Reserved 0 portion of the mailbox header 111b651957cSAlexander Duyck * CRC: Running CRC for all data since connect plus current message header 112b651957cSAlexander Duyck * Size: Maximum message size - Applies only to connect headers 113b651957cSAlexander Duyck * The maximum message size is provided during connect to avoid 114b651957cSAlexander Duyck * jamming the mailbox with messages that do not fit. 115b651957cSAlexander Duyck * Err_no: Error number - Applies only to error headers 116f632fed3SBruce Allan * The error number provides an indication of the type of error 117b651957cSAlexander Duyck * experienced. 118b651957cSAlexander Duyck */ 119b651957cSAlexander Duyck 120f632fed3SBruce Allan /* macros for retrieving and setting header values */ 1216b1f201fSAlexander Duyck #define FM10K_MSG_HDR_MASK(name) \ 1226b1f201fSAlexander Duyck ((0x1u << FM10K_MSG_##name##_SIZE) - 1) 1236b1f201fSAlexander Duyck #define FM10K_MSG_HDR_FIELD_SET(value, name) \ 1246b1f201fSAlexander Duyck (((u32)(value) & FM10K_MSG_HDR_MASK(name)) << FM10K_MSG_##name##_SHIFT) 1256b1f201fSAlexander Duyck #define FM10K_MSG_HDR_FIELD_GET(value, name) \ 1266b1f201fSAlexander Duyck ((u16)((value) >> FM10K_MSG_##name##_SHIFT) & FM10K_MSG_HDR_MASK(name)) 1276b1f201fSAlexander Duyck 128b651957cSAlexander Duyck /* offsets shared between all headers */ 129b651957cSAlexander Duyck #define FM10K_MSG_TYPE_SHIFT 0 130b651957cSAlexander Duyck #define FM10K_MSG_TYPE_SIZE 4 131b651957cSAlexander Duyck #define FM10K_MSG_TAIL_SHIFT 4 132b651957cSAlexander Duyck #define FM10K_MSG_TAIL_SIZE 4 133b651957cSAlexander Duyck #define FM10K_MSG_HEAD_SHIFT 8 134b651957cSAlexander Duyck #define FM10K_MSG_HEAD_SIZE 4 135b651957cSAlexander Duyck #define FM10K_MSG_RSVD0_SHIFT 12 136b651957cSAlexander Duyck #define FM10K_MSG_RSVD0_SIZE 4 137b651957cSAlexander Duyck 138b651957cSAlexander Duyck /* offsets for data/disconnect headers */ 139b651957cSAlexander Duyck #define FM10K_MSG_CRC_SHIFT 16 140b651957cSAlexander Duyck #define FM10K_MSG_CRC_SIZE 16 141b651957cSAlexander Duyck 142b651957cSAlexander Duyck /* offsets for connect headers */ 143b651957cSAlexander Duyck #define FM10K_MSG_CONNECT_SIZE_SHIFT 16 144b651957cSAlexander Duyck #define FM10K_MSG_CONNECT_SIZE_SIZE 16 145b651957cSAlexander Duyck 146b651957cSAlexander Duyck /* offsets for error headers */ 147b651957cSAlexander Duyck #define FM10K_MSG_ERR_NO_SHIFT 16 148b651957cSAlexander Duyck #define FM10K_MSG_ERR_NO_SIZE 16 149b651957cSAlexander Duyck 150b651957cSAlexander Duyck enum fm10k_msg_type { 151b651957cSAlexander Duyck FM10K_MSG_DATA = 0x8, 152b651957cSAlexander Duyck FM10K_MSG_CONNECT = 0xC, 153b651957cSAlexander Duyck FM10K_MSG_DISCONNECT = 0xD, 154b651957cSAlexander Duyck FM10K_MSG_ERROR = 0xE, 155b651957cSAlexander Duyck }; 156b651957cSAlexander Duyck 1576b1f201fSAlexander Duyck /* HNI/SM Mailbox FIFO format 1586b1f201fSAlexander Duyck * 3 2 1 0 1596b1f201fSAlexander Duyck * 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 1606b1f201fSAlexander Duyck * +-------+-----------------------+-------+-----------------------+ 1616b1f201fSAlexander Duyck * | Error | Remote Head |Version| Local Tail | 1626b1f201fSAlexander Duyck * +-------+-----------------------+-------+-----------------------+ 1636b1f201fSAlexander Duyck * | | 1646b1f201fSAlexander Duyck * . Local FIFO Data . 1656b1f201fSAlexander Duyck * . . 1666b1f201fSAlexander Duyck * +-------+-----------------------+-------+-----------------------+ 1676b1f201fSAlexander Duyck * 1686b1f201fSAlexander Duyck * The layout above describes the format for the FIFOs used by the host 1696b1f201fSAlexander Duyck * network interface and the switch manager to communicate messages back 1706b1f201fSAlexander Duyck * and forth. Both the HNI and the switch maintain one such FIFO. The 1716b1f201fSAlexander Duyck * layout in memory has the switch manager FIFO followed immediately by 1726b1f201fSAlexander Duyck * the HNI FIFO. For this reason I am using just the pointer to the 1736b1f201fSAlexander Duyck * HNI FIFO in the mailbox ops as the offset between the two is fixed. 1746b1f201fSAlexander Duyck * 1756b1f201fSAlexander Duyck * The header for the FIFO is broken out into the following fields: 1766b1f201fSAlexander Duyck * Local Tail: Offset into FIFO region for next DWORD to write. 1776b1f201fSAlexander Duyck * Version: Version info for mailbox, only values of 0/1 are supported. 1786b1f201fSAlexander Duyck * Remote Head: Offset into remote FIFO to indicate how much we have read. 1796b1f201fSAlexander Duyck * Error: Error indication, values TBD. 1806b1f201fSAlexander Duyck */ 1816b1f201fSAlexander Duyck 1826b1f201fSAlexander Duyck /* version number for switch manager mailboxes */ 1836b1f201fSAlexander Duyck #define FM10K_SM_MBX_VERSION 1 1846b1f201fSAlexander Duyck #define FM10K_SM_MBX_FIFO_LEN (FM10K_MBMEM_PF_XOR - 1) 1856b1f201fSAlexander Duyck 1866b1f201fSAlexander Duyck /* offsets shared between all SM FIFO headers */ 1876b1f201fSAlexander Duyck #define FM10K_MSG_SM_TAIL_SHIFT 0 1886b1f201fSAlexander Duyck #define FM10K_MSG_SM_TAIL_SIZE 12 1896b1f201fSAlexander Duyck #define FM10K_MSG_SM_VER_SHIFT 12 1906b1f201fSAlexander Duyck #define FM10K_MSG_SM_VER_SIZE 4 1916b1f201fSAlexander Duyck #define FM10K_MSG_SM_HEAD_SHIFT 16 1926b1f201fSAlexander Duyck #define FM10K_MSG_SM_HEAD_SIZE 12 1936b1f201fSAlexander Duyck #define FM10K_MSG_SM_ERR_SHIFT 28 1946b1f201fSAlexander Duyck #define FM10K_MSG_SM_ERR_SIZE 4 1956b1f201fSAlexander Duyck 1966b1f201fSAlexander Duyck /* All error messages returned by mailbox functions 1976b1f201fSAlexander Duyck * The value -511 is 0xFE01 in hex. The idea is to order the errors 1986b1f201fSAlexander Duyck * from 0xFE01 - 0xFEFF so error codes are easily visible in the mailbox 1996b1f201fSAlexander Duyck * messages. This also helps to avoid error number collisions as Linux 2006b1f201fSAlexander Duyck * doesn't appear to use error numbers 256 - 511. 2016b1f201fSAlexander Duyck */ 2026b1f201fSAlexander Duyck #define FM10K_MBX_ERR(_n) ((_n) - 512) 2036b1f201fSAlexander Duyck #define FM10K_MBX_ERR_NO_MBX FM10K_MBX_ERR(0x01) 2046b1f201fSAlexander Duyck #define FM10K_MBX_ERR_NO_SPACE FM10K_MBX_ERR(0x03) 2056b1f201fSAlexander Duyck #define FM10K_MBX_ERR_TAIL FM10K_MBX_ERR(0x05) 2066b1f201fSAlexander Duyck #define FM10K_MBX_ERR_HEAD FM10K_MBX_ERR(0x06) 2076b1f201fSAlexander Duyck #define FM10K_MBX_ERR_SRC FM10K_MBX_ERR(0x08) 2086b1f201fSAlexander Duyck #define FM10K_MBX_ERR_TYPE FM10K_MBX_ERR(0x09) 2096b1f201fSAlexander Duyck #define FM10K_MBX_ERR_SIZE FM10K_MBX_ERR(0x0B) 2106b1f201fSAlexander Duyck #define FM10K_MBX_ERR_BUSY FM10K_MBX_ERR(0x0C) 2116b1f201fSAlexander Duyck #define FM10K_MBX_ERR_RSVD0 FM10K_MBX_ERR(0x0E) 2126b1f201fSAlexander Duyck #define FM10K_MBX_ERR_CRC FM10K_MBX_ERR(0x0F) 2136b1f201fSAlexander Duyck 2146b1f201fSAlexander Duyck #define FM10K_MBX_CRC_SEED 0xFFFF 2156b1f201fSAlexander Duyck 2166b1f201fSAlexander Duyck struct fm10k_mbx_ops { 2176b1f201fSAlexander Duyck s32 (*connect)(struct fm10k_hw *, struct fm10k_mbx_info *); 2186b1f201fSAlexander Duyck void (*disconnect)(struct fm10k_hw *, struct fm10k_mbx_info *); 2196b1f201fSAlexander Duyck bool (*rx_ready)(struct fm10k_mbx_info *); 2206b1f201fSAlexander Duyck bool (*tx_ready)(struct fm10k_mbx_info *, u16); 2216b1f201fSAlexander Duyck bool (*tx_complete)(struct fm10k_mbx_info *); 2226b1f201fSAlexander Duyck s32 (*enqueue_tx)(struct fm10k_hw *, struct fm10k_mbx_info *, 2236b1f201fSAlexander Duyck const u32 *); 2246b1f201fSAlexander Duyck s32 (*process)(struct fm10k_hw *, struct fm10k_mbx_info *); 2256b1f201fSAlexander Duyck s32 (*register_handlers)(struct fm10k_mbx_info *, 2266b1f201fSAlexander Duyck const struct fm10k_msg_data *); 2276b1f201fSAlexander Duyck }; 2286b1f201fSAlexander Duyck 2296b1f201fSAlexander Duyck struct fm10k_mbx_fifo { 2306b1f201fSAlexander Duyck u32 *buffer; 2316b1f201fSAlexander Duyck u16 head; 2326b1f201fSAlexander Duyck u16 tail; 2336b1f201fSAlexander Duyck u16 size; 2346b1f201fSAlexander Duyck }; 2356b1f201fSAlexander Duyck 2366b1f201fSAlexander Duyck /* size of buffer to be stored in mailbox for FIFOs */ 2376b1f201fSAlexander Duyck #define FM10K_MBX_TX_BUFFER_SIZE 512 2386b1f201fSAlexander Duyck #define FM10K_MBX_RX_BUFFER_SIZE 128 2396b1f201fSAlexander Duyck #define FM10K_MBX_BUFFER_SIZE \ 2406b1f201fSAlexander Duyck (FM10K_MBX_TX_BUFFER_SIZE + FM10K_MBX_RX_BUFFER_SIZE) 2416b1f201fSAlexander Duyck 2426b1f201fSAlexander Duyck /* minimum and maximum message size in dwords */ 2436b1f201fSAlexander Duyck #define FM10K_MBX_MSG_MAX_SIZE \ 2446b1f201fSAlexander Duyck ((FM10K_MBX_TX_BUFFER_SIZE - 1) & (FM10K_MBX_RX_BUFFER_SIZE - 1)) 2456b1f201fSAlexander Duyck #define FM10K_VFMBX_MSG_MTU ((FM10K_VFMBMEM_LEN / 2) - 1) 2466b1f201fSAlexander Duyck 2476b1f201fSAlexander Duyck #define FM10K_MBX_INIT_TIMEOUT 2000 /* number of retries on mailbox */ 2486b1f201fSAlexander Duyck #define FM10K_MBX_INIT_DELAY 500 /* microseconds between retries */ 2496b1f201fSAlexander Duyck 2506b1f201fSAlexander Duyck struct fm10k_mbx_info { 2516b1f201fSAlexander Duyck /* function pointers for mailbox operations */ 2526b1f201fSAlexander Duyck struct fm10k_mbx_ops ops; 2536b1f201fSAlexander Duyck const struct fm10k_msg_data *msg_data; 2546b1f201fSAlexander Duyck 2556b1f201fSAlexander Duyck /* message FIFOs */ 2566b1f201fSAlexander Duyck struct fm10k_mbx_fifo rx; 2576b1f201fSAlexander Duyck struct fm10k_mbx_fifo tx; 2586b1f201fSAlexander Duyck 2596b1f201fSAlexander Duyck /* delay for handling timeouts */ 2606b1f201fSAlexander Duyck u32 timeout; 2616b1f201fSAlexander Duyck u32 udelay; 2626b1f201fSAlexander Duyck 2636b1f201fSAlexander Duyck /* mailbox state info */ 2646b1f201fSAlexander Duyck u32 mbx_reg, mbmem_reg, mbx_lock, mbx_hdr; 2656b1f201fSAlexander Duyck u16 max_size, mbmem_len; 2666b1f201fSAlexander Duyck u16 tail, tail_len, pulled; 2676b1f201fSAlexander Duyck u16 head, head_len, pushed; 2686b1f201fSAlexander Duyck u16 local, remote; 2696b1f201fSAlexander Duyck enum fm10k_mbx_state state; 2706b1f201fSAlexander Duyck 2716b1f201fSAlexander Duyck /* result of last mailbox test */ 2726b1f201fSAlexander Duyck s32 test_result; 2736b1f201fSAlexander Duyck 2746b1f201fSAlexander Duyck /* statistics */ 2756b1f201fSAlexander Duyck u64 tx_busy; 2766b1f201fSAlexander Duyck u64 tx_dropped; 2776b1f201fSAlexander Duyck u64 tx_messages; 2786b1f201fSAlexander Duyck u64 tx_dwords; 27917d39facSJacob Keller u64 tx_mbmem_pulled; 2806b1f201fSAlexander Duyck u64 rx_messages; 2816b1f201fSAlexander Duyck u64 rx_dwords; 28217d39facSJacob Keller u64 rx_mbmem_pushed; 2836b1f201fSAlexander Duyck u64 rx_parse_err; 2846b1f201fSAlexander Duyck 2856b1f201fSAlexander Duyck /* Buffer to store messages */ 2866b1f201fSAlexander Duyck u32 buffer[FM10K_MBX_BUFFER_SIZE]; 2876b1f201fSAlexander Duyck }; 2886b1f201fSAlexander Duyck 289b651957cSAlexander Duyck s32 fm10k_pfvf_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *, 290b651957cSAlexander Duyck const struct fm10k_msg_data *, u8); 2911337e6b9SAlexander Duyck s32 fm10k_sm_mbx_init(struct fm10k_hw *, struct fm10k_mbx_info *, 2921337e6b9SAlexander Duyck const struct fm10k_msg_data *); 2931337e6b9SAlexander Duyck 2946b1f201fSAlexander Duyck #endif /* _FM10K_MBX_H_ */ 295