162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 2013 - 2019 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "fm10k_common.h"
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/**
762306a36Sopenharmony_ci *  fm10k_fifo_init - Initialize a message FIFO
862306a36Sopenharmony_ci *  @fifo: pointer to FIFO
962306a36Sopenharmony_ci *  @buffer: pointer to memory to be used to store FIFO
1062306a36Sopenharmony_ci *  @size: maximum message size to store in FIFO, must be 2^n - 1
1162306a36Sopenharmony_ci **/
1262306a36Sopenharmony_cistatic void fm10k_fifo_init(struct fm10k_mbx_fifo *fifo, u32 *buffer, u16 size)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	fifo->buffer = buffer;
1562306a36Sopenharmony_ci	fifo->size = size;
1662306a36Sopenharmony_ci	fifo->head = 0;
1762306a36Sopenharmony_ci	fifo->tail = 0;
1862306a36Sopenharmony_ci}
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/**
2162306a36Sopenharmony_ci *  fm10k_fifo_used - Retrieve used space in FIFO
2262306a36Sopenharmony_ci *  @fifo: pointer to FIFO
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci *  This function returns the number of DWORDs used in the FIFO
2562306a36Sopenharmony_ci **/
2662306a36Sopenharmony_cistatic u16 fm10k_fifo_used(struct fm10k_mbx_fifo *fifo)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	return fifo->tail - fifo->head;
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci *  fm10k_fifo_unused - Retrieve unused space in FIFO
3362306a36Sopenharmony_ci *  @fifo: pointer to FIFO
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci *  This function returns the number of unused DWORDs in the FIFO
3662306a36Sopenharmony_ci **/
3762306a36Sopenharmony_cistatic u16 fm10k_fifo_unused(struct fm10k_mbx_fifo *fifo)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	return fifo->size + fifo->head - fifo->tail;
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/**
4362306a36Sopenharmony_ci *  fm10k_fifo_empty - Test to verify if FIFO is empty
4462306a36Sopenharmony_ci *  @fifo: pointer to FIFO
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci *  This function returns true if the FIFO is empty, else false
4762306a36Sopenharmony_ci **/
4862306a36Sopenharmony_cistatic bool fm10k_fifo_empty(struct fm10k_mbx_fifo *fifo)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	return fifo->head == fifo->tail;
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/**
5462306a36Sopenharmony_ci *  fm10k_fifo_head_offset - returns indices of head with given offset
5562306a36Sopenharmony_ci *  @fifo: pointer to FIFO
5662306a36Sopenharmony_ci *  @offset: offset to add to head
5762306a36Sopenharmony_ci *
5862306a36Sopenharmony_ci *  This function returns the indices into the FIFO based on head + offset
5962306a36Sopenharmony_ci **/
6062306a36Sopenharmony_cistatic u16 fm10k_fifo_head_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	return (fifo->head + offset) & (fifo->size - 1);
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/**
6662306a36Sopenharmony_ci *  fm10k_fifo_tail_offset - returns indices of tail with given offset
6762306a36Sopenharmony_ci *  @fifo: pointer to FIFO
6862306a36Sopenharmony_ci *  @offset: offset to add to tail
6962306a36Sopenharmony_ci *
7062306a36Sopenharmony_ci *  This function returns the indices into the FIFO based on tail + offset
7162306a36Sopenharmony_ci **/
7262306a36Sopenharmony_cistatic u16 fm10k_fifo_tail_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	return (fifo->tail + offset) & (fifo->size - 1);
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/**
7862306a36Sopenharmony_ci *  fm10k_fifo_head_len - Retrieve length of first message in FIFO
7962306a36Sopenharmony_ci *  @fifo: pointer to FIFO
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci *  This function returns the size of the first message in the FIFO
8262306a36Sopenharmony_ci **/
8362306a36Sopenharmony_cistatic u16 fm10k_fifo_head_len(struct fm10k_mbx_fifo *fifo)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	u32 *head = fifo->buffer + fm10k_fifo_head_offset(fifo, 0);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* verify there is at least 1 DWORD in the fifo so *head is valid */
8862306a36Sopenharmony_ci	if (fm10k_fifo_empty(fifo))
8962306a36Sopenharmony_ci		return 0;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/* retieve the message length */
9262306a36Sopenharmony_ci	return FM10K_TLV_DWORD_LEN(*head);
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/**
9662306a36Sopenharmony_ci *  fm10k_fifo_head_drop - Drop the first message in FIFO
9762306a36Sopenharmony_ci *  @fifo: pointer to FIFO
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci *  This function returns the size of the message dropped from the FIFO
10062306a36Sopenharmony_ci **/
10162306a36Sopenharmony_cistatic u16 fm10k_fifo_head_drop(struct fm10k_mbx_fifo *fifo)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	u16 len = fm10k_fifo_head_len(fifo);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* update head so it is at the start of next frame */
10662306a36Sopenharmony_ci	fifo->head += len;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	return len;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci *  fm10k_fifo_drop_all - Drop all messages in FIFO
11362306a36Sopenharmony_ci *  @fifo: pointer to FIFO
11462306a36Sopenharmony_ci *
11562306a36Sopenharmony_ci *  This function resets the head pointer to drop all messages in the FIFO and
11662306a36Sopenharmony_ci *  ensure the FIFO is empty.
11762306a36Sopenharmony_ci **/
11862306a36Sopenharmony_cistatic void fm10k_fifo_drop_all(struct fm10k_mbx_fifo *fifo)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	fifo->head = fifo->tail;
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci/**
12462306a36Sopenharmony_ci *  fm10k_mbx_index_len - Convert a head/tail index into a length value
12562306a36Sopenharmony_ci *  @mbx: pointer to mailbox
12662306a36Sopenharmony_ci *  @head: head index
12762306a36Sopenharmony_ci *  @tail: head index
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci *  This function takes the head and tail index and determines the length
13062306a36Sopenharmony_ci *  of the data indicated by this pair.
13162306a36Sopenharmony_ci **/
13262306a36Sopenharmony_cistatic u16 fm10k_mbx_index_len(struct fm10k_mbx_info *mbx, u16 head, u16 tail)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	u16 len = tail - head;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/* we wrapped so subtract 2, one for index 0, one for all 1s index */
13762306a36Sopenharmony_ci	if (len > tail)
13862306a36Sopenharmony_ci		len -= 2;
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci	return len & ((mbx->mbmem_len << 1) - 1);
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/**
14462306a36Sopenharmony_ci *  fm10k_mbx_tail_add - Determine new tail value with added offset
14562306a36Sopenharmony_ci *  @mbx: pointer to mailbox
14662306a36Sopenharmony_ci *  @offset: length to add to tail offset
14762306a36Sopenharmony_ci *
14862306a36Sopenharmony_ci *  This function takes the local tail index and recomputes it for
14962306a36Sopenharmony_ci *  a given length added as an offset.
15062306a36Sopenharmony_ci **/
15162306a36Sopenharmony_cistatic u16 fm10k_mbx_tail_add(struct fm10k_mbx_info *mbx, u16 offset)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	u16 tail = (mbx->tail + offset + 1) & ((mbx->mbmem_len << 1) - 1);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	/* add/sub 1 because we cannot have offset 0 or all 1s */
15662306a36Sopenharmony_ci	return (tail > mbx->tail) ? --tail : ++tail;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/**
16062306a36Sopenharmony_ci *  fm10k_mbx_tail_sub - Determine new tail value with subtracted offset
16162306a36Sopenharmony_ci *  @mbx: pointer to mailbox
16262306a36Sopenharmony_ci *  @offset: length to add to tail offset
16362306a36Sopenharmony_ci *
16462306a36Sopenharmony_ci *  This function takes the local tail index and recomputes it for
16562306a36Sopenharmony_ci *  a given length added as an offset.
16662306a36Sopenharmony_ci **/
16762306a36Sopenharmony_cistatic u16 fm10k_mbx_tail_sub(struct fm10k_mbx_info *mbx, u16 offset)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	u16 tail = (mbx->tail - offset - 1) & ((mbx->mbmem_len << 1) - 1);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	/* sub/add 1 because we cannot have offset 0 or all 1s */
17262306a36Sopenharmony_ci	return (tail < mbx->tail) ? ++tail : --tail;
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci/**
17662306a36Sopenharmony_ci *  fm10k_mbx_head_add - Determine new head value with added offset
17762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
17862306a36Sopenharmony_ci *  @offset: length to add to head offset
17962306a36Sopenharmony_ci *
18062306a36Sopenharmony_ci *  This function takes the local head index and recomputes it for
18162306a36Sopenharmony_ci *  a given length added as an offset.
18262306a36Sopenharmony_ci **/
18362306a36Sopenharmony_cistatic u16 fm10k_mbx_head_add(struct fm10k_mbx_info *mbx, u16 offset)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	u16 head = (mbx->head + offset + 1) & ((mbx->mbmem_len << 1) - 1);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/* add/sub 1 because we cannot have offset 0 or all 1s */
18862306a36Sopenharmony_ci	return (head > mbx->head) ? --head : ++head;
18962306a36Sopenharmony_ci}
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci/**
19262306a36Sopenharmony_ci *  fm10k_mbx_head_sub - Determine new head value with subtracted offset
19362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
19462306a36Sopenharmony_ci *  @offset: length to add to head offset
19562306a36Sopenharmony_ci *
19662306a36Sopenharmony_ci *  This function takes the local head index and recomputes it for
19762306a36Sopenharmony_ci *  a given length added as an offset.
19862306a36Sopenharmony_ci **/
19962306a36Sopenharmony_cistatic u16 fm10k_mbx_head_sub(struct fm10k_mbx_info *mbx, u16 offset)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	u16 head = (mbx->head - offset - 1) & ((mbx->mbmem_len << 1) - 1);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	/* sub/add 1 because we cannot have offset 0 or all 1s */
20462306a36Sopenharmony_ci	return (head < mbx->head) ? ++head : --head;
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/**
20862306a36Sopenharmony_ci *  fm10k_mbx_pushed_tail_len - Retrieve the length of message being pushed
20962306a36Sopenharmony_ci *  @mbx: pointer to mailbox
21062306a36Sopenharmony_ci *
21162306a36Sopenharmony_ci *  This function will return the length of the message currently being
21262306a36Sopenharmony_ci *  pushed onto the tail of the Rx queue.
21362306a36Sopenharmony_ci **/
21462306a36Sopenharmony_cistatic u16 fm10k_mbx_pushed_tail_len(struct fm10k_mbx_info *mbx)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	u32 *tail = mbx->rx.buffer + fm10k_fifo_tail_offset(&mbx->rx, 0);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	/* pushed tail is only valid if pushed is set */
21962306a36Sopenharmony_ci	if (!mbx->pushed)
22062306a36Sopenharmony_ci		return 0;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	return FM10K_TLV_DWORD_LEN(*tail);
22362306a36Sopenharmony_ci}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci/**
22662306a36Sopenharmony_ci *  fm10k_fifo_write_copy - pulls data off of msg and places it in FIFO
22762306a36Sopenharmony_ci *  @fifo: pointer to FIFO
22862306a36Sopenharmony_ci *  @msg: message array to populate
22962306a36Sopenharmony_ci *  @tail_offset: additional offset to add to tail pointer
23062306a36Sopenharmony_ci *  @len: length of FIFO to copy into message header
23162306a36Sopenharmony_ci *
23262306a36Sopenharmony_ci *  This function will take a message and copy it into a section of the
23362306a36Sopenharmony_ci *  FIFO.  In order to get something into a location other than just
23462306a36Sopenharmony_ci *  the tail you can use tail_offset to adjust the pointer.
23562306a36Sopenharmony_ci **/
23662306a36Sopenharmony_cistatic void fm10k_fifo_write_copy(struct fm10k_mbx_fifo *fifo,
23762306a36Sopenharmony_ci				  const u32 *msg, u16 tail_offset, u16 len)
23862306a36Sopenharmony_ci{
23962306a36Sopenharmony_ci	u16 end = fm10k_fifo_tail_offset(fifo, tail_offset);
24062306a36Sopenharmony_ci	u32 *tail = fifo->buffer + end;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	/* track when we should cross the end of the FIFO */
24362306a36Sopenharmony_ci	end = fifo->size - end;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* copy end of message before start of message */
24662306a36Sopenharmony_ci	if (end < len)
24762306a36Sopenharmony_ci		memcpy(fifo->buffer, msg + end, (len - end) << 2);
24862306a36Sopenharmony_ci	else
24962306a36Sopenharmony_ci		end = len;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/* Copy remaining message into Tx FIFO */
25262306a36Sopenharmony_ci	memcpy(tail, msg, end << 2);
25362306a36Sopenharmony_ci}
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci/**
25662306a36Sopenharmony_ci *  fm10k_fifo_enqueue - Enqueues the message to the tail of the FIFO
25762306a36Sopenharmony_ci *  @fifo: pointer to FIFO
25862306a36Sopenharmony_ci *  @msg: message array to read
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci *  This function enqueues a message up to the size specified by the length
26162306a36Sopenharmony_ci *  contained in the first DWORD of the message and will place at the tail
26262306a36Sopenharmony_ci *  of the FIFO.  It will return 0 on success, or a negative value on error.
26362306a36Sopenharmony_ci **/
26462306a36Sopenharmony_cistatic s32 fm10k_fifo_enqueue(struct fm10k_mbx_fifo *fifo, const u32 *msg)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	u16 len = FM10K_TLV_DWORD_LEN(*msg);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	/* verify parameters */
26962306a36Sopenharmony_ci	if (len > fifo->size)
27062306a36Sopenharmony_ci		return FM10K_MBX_ERR_SIZE;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* verify there is room for the message */
27362306a36Sopenharmony_ci	if (len > fm10k_fifo_unused(fifo))
27462306a36Sopenharmony_ci		return FM10K_MBX_ERR_NO_SPACE;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	/* Copy message into FIFO */
27762306a36Sopenharmony_ci	fm10k_fifo_write_copy(fifo, msg, 0, len);
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	/* memory barrier to guarantee FIFO is written before tail update */
28062306a36Sopenharmony_ci	wmb();
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	/* Update Tx FIFO tail */
28362306a36Sopenharmony_ci	fifo->tail += len;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	return 0;
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci/**
28962306a36Sopenharmony_ci *  fm10k_mbx_validate_msg_size - Validate incoming message based on size
29062306a36Sopenharmony_ci *  @mbx: pointer to mailbox
29162306a36Sopenharmony_ci *  @len: length of data pushed onto buffer
29262306a36Sopenharmony_ci *
29362306a36Sopenharmony_ci *  This function analyzes the frame and will return a non-zero value when
29462306a36Sopenharmony_ci *  the start of a message larger than the mailbox is detected.
29562306a36Sopenharmony_ci **/
29662306a36Sopenharmony_cistatic u16 fm10k_mbx_validate_msg_size(struct fm10k_mbx_info *mbx, u16 len)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->rx;
29962306a36Sopenharmony_ci	u16 total_len = 0, msg_len;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	/* length should include previous amounts pushed */
30262306a36Sopenharmony_ci	len += mbx->pushed;
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	/* offset in message is based off of current message size */
30562306a36Sopenharmony_ci	do {
30662306a36Sopenharmony_ci		u32 *msg;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci		msg = fifo->buffer + fm10k_fifo_tail_offset(fifo, total_len);
30962306a36Sopenharmony_ci		msg_len = FM10K_TLV_DWORD_LEN(*msg);
31062306a36Sopenharmony_ci		total_len += msg_len;
31162306a36Sopenharmony_ci	} while (total_len < len);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	/* message extends out of pushed section, but fits in FIFO */
31462306a36Sopenharmony_ci	if ((len < total_len) && (msg_len <= mbx->max_size))
31562306a36Sopenharmony_ci		return 0;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	/* return length of invalid section */
31862306a36Sopenharmony_ci	return (len < total_len) ? len : (len - total_len);
31962306a36Sopenharmony_ci}
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci/**
32262306a36Sopenharmony_ci *  fm10k_mbx_write_copy - pulls data off of Tx FIFO and places it in mbmem
32362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
32462306a36Sopenharmony_ci *  @mbx: pointer to mailbox
32562306a36Sopenharmony_ci *
32662306a36Sopenharmony_ci *  This function will take a section of the Tx FIFO and copy it into the
32762306a36Sopenharmony_ci *  mailbox memory.  The offset in mbmem is based on the lower bits of the
32862306a36Sopenharmony_ci *  tail and len determines the length to copy.
32962306a36Sopenharmony_ci **/
33062306a36Sopenharmony_cistatic void fm10k_mbx_write_copy(struct fm10k_hw *hw,
33162306a36Sopenharmony_ci				 struct fm10k_mbx_info *mbx)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->tx;
33462306a36Sopenharmony_ci	u32 mbmem = mbx->mbmem_reg;
33562306a36Sopenharmony_ci	u32 *head = fifo->buffer;
33662306a36Sopenharmony_ci	u16 end, len, tail, mask;
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	if (!mbx->tail_len)
33962306a36Sopenharmony_ci		return;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	/* determine data length and mbmem tail index */
34262306a36Sopenharmony_ci	mask = mbx->mbmem_len - 1;
34362306a36Sopenharmony_ci	len = mbx->tail_len;
34462306a36Sopenharmony_ci	tail = fm10k_mbx_tail_sub(mbx, len);
34562306a36Sopenharmony_ci	if (tail > mask)
34662306a36Sopenharmony_ci		tail++;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	/* determine offset in the ring */
34962306a36Sopenharmony_ci	end = fm10k_fifo_head_offset(fifo, mbx->pulled);
35062306a36Sopenharmony_ci	head += end;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	/* memory barrier to guarantee data is ready to be read */
35362306a36Sopenharmony_ci	rmb();
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	/* Copy message from Tx FIFO */
35662306a36Sopenharmony_ci	for (end = fifo->size - end; len; head = fifo->buffer) {
35762306a36Sopenharmony_ci		do {
35862306a36Sopenharmony_ci			/* adjust tail to match offset for FIFO */
35962306a36Sopenharmony_ci			tail &= mask;
36062306a36Sopenharmony_ci			if (!tail)
36162306a36Sopenharmony_ci				tail++;
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci			mbx->tx_mbmem_pulled++;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci			/* write message to hardware FIFO */
36662306a36Sopenharmony_ci			fm10k_write_reg(hw, mbmem + tail++, *(head++));
36762306a36Sopenharmony_ci		} while (--len && --end);
36862306a36Sopenharmony_ci	}
36962306a36Sopenharmony_ci}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci/**
37262306a36Sopenharmony_ci *  fm10k_mbx_pull_head - Pulls data off of head of Tx FIFO
37362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
37462306a36Sopenharmony_ci *  @mbx: pointer to mailbox
37562306a36Sopenharmony_ci *  @head: acknowledgement number last received
37662306a36Sopenharmony_ci *
37762306a36Sopenharmony_ci *  This function will push the tail index forward based on the remote
37862306a36Sopenharmony_ci *  head index.  It will then pull up to mbmem_len DWORDs off of the
37962306a36Sopenharmony_ci *  head of the FIFO and will place it in the MBMEM registers
38062306a36Sopenharmony_ci *  associated with the mailbox.
38162306a36Sopenharmony_ci **/
38262306a36Sopenharmony_cistatic void fm10k_mbx_pull_head(struct fm10k_hw *hw,
38362306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx, u16 head)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	u16 mbmem_len, len, ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
38662306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->tx;
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	/* update number of bytes pulled and update bytes in transit */
38962306a36Sopenharmony_ci	mbx->pulled += mbx->tail_len - ack;
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	/* determine length of data to pull, reserve space for mbmem header */
39262306a36Sopenharmony_ci	mbmem_len = mbx->mbmem_len - 1;
39362306a36Sopenharmony_ci	len = fm10k_fifo_used(fifo) - mbx->pulled;
39462306a36Sopenharmony_ci	if (len > mbmem_len)
39562306a36Sopenharmony_ci		len = mbmem_len;
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	/* update tail and record number of bytes in transit */
39862306a36Sopenharmony_ci	mbx->tail = fm10k_mbx_tail_add(mbx, len - ack);
39962306a36Sopenharmony_ci	mbx->tail_len = len;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	/* drop pulled messages from the FIFO */
40262306a36Sopenharmony_ci	for (len = fm10k_fifo_head_len(fifo);
40362306a36Sopenharmony_ci	     len && (mbx->pulled >= len);
40462306a36Sopenharmony_ci	     len = fm10k_fifo_head_len(fifo)) {
40562306a36Sopenharmony_ci		mbx->pulled -= fm10k_fifo_head_drop(fifo);
40662306a36Sopenharmony_ci		mbx->tx_messages++;
40762306a36Sopenharmony_ci		mbx->tx_dwords += len;
40862306a36Sopenharmony_ci	}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	/* Copy message out from the Tx FIFO */
41162306a36Sopenharmony_ci	fm10k_mbx_write_copy(hw, mbx);
41262306a36Sopenharmony_ci}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci/**
41562306a36Sopenharmony_ci *  fm10k_mbx_read_copy - pulls data off of mbmem and places it in Rx FIFO
41662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
41762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
41862306a36Sopenharmony_ci *
41962306a36Sopenharmony_ci *  This function will take a section of the mailbox memory and copy it
42062306a36Sopenharmony_ci *  into the Rx FIFO.  The offset is based on the lower bits of the
42162306a36Sopenharmony_ci *  head and len determines the length to copy.
42262306a36Sopenharmony_ci **/
42362306a36Sopenharmony_cistatic void fm10k_mbx_read_copy(struct fm10k_hw *hw,
42462306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx)
42562306a36Sopenharmony_ci{
42662306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->rx;
42762306a36Sopenharmony_ci	u32 mbmem = mbx->mbmem_reg ^ mbx->mbmem_len;
42862306a36Sopenharmony_ci	u32 *tail = fifo->buffer;
42962306a36Sopenharmony_ci	u16 end, len, head;
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	/* determine data length and mbmem head index */
43262306a36Sopenharmony_ci	len = mbx->head_len;
43362306a36Sopenharmony_ci	head = fm10k_mbx_head_sub(mbx, len);
43462306a36Sopenharmony_ci	if (head >= mbx->mbmem_len)
43562306a36Sopenharmony_ci		head++;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	/* determine offset in the ring */
43862306a36Sopenharmony_ci	end = fm10k_fifo_tail_offset(fifo, mbx->pushed);
43962306a36Sopenharmony_ci	tail += end;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	/* Copy message into Rx FIFO */
44262306a36Sopenharmony_ci	for (end = fifo->size - end; len; tail = fifo->buffer) {
44362306a36Sopenharmony_ci		do {
44462306a36Sopenharmony_ci			/* adjust head to match offset for FIFO */
44562306a36Sopenharmony_ci			head &= mbx->mbmem_len - 1;
44662306a36Sopenharmony_ci			if (!head)
44762306a36Sopenharmony_ci				head++;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci			mbx->rx_mbmem_pushed++;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci			/* read message from hardware FIFO */
45262306a36Sopenharmony_ci			*(tail++) = fm10k_read_reg(hw, mbmem + head++);
45362306a36Sopenharmony_ci		} while (--len && --end);
45462306a36Sopenharmony_ci	}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	/* memory barrier to guarantee FIFO is written before tail update */
45762306a36Sopenharmony_ci	wmb();
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/**
46162306a36Sopenharmony_ci *  fm10k_mbx_push_tail - Pushes up to 15 DWORDs on to tail of FIFO
46262306a36Sopenharmony_ci *  @hw: pointer to hardware structure
46362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
46462306a36Sopenharmony_ci *  @tail: tail index of message
46562306a36Sopenharmony_ci *
46662306a36Sopenharmony_ci *  This function will first validate the tail index and size for the
46762306a36Sopenharmony_ci *  incoming message.  It then updates the acknowledgment number and
46862306a36Sopenharmony_ci *  copies the data into the FIFO.  It will return the number of messages
46962306a36Sopenharmony_ci *  dequeued on success and a negative value on error.
47062306a36Sopenharmony_ci **/
47162306a36Sopenharmony_cistatic s32 fm10k_mbx_push_tail(struct fm10k_hw *hw,
47262306a36Sopenharmony_ci			       struct fm10k_mbx_info *mbx,
47362306a36Sopenharmony_ci			       u16 tail)
47462306a36Sopenharmony_ci{
47562306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->rx;
47662306a36Sopenharmony_ci	u16 len, seq = fm10k_mbx_index_len(mbx, mbx->head, tail);
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	/* determine length of data to push */
47962306a36Sopenharmony_ci	len = fm10k_fifo_unused(fifo) - mbx->pushed;
48062306a36Sopenharmony_ci	if (len > seq)
48162306a36Sopenharmony_ci		len = seq;
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci	/* update head and record bytes received */
48462306a36Sopenharmony_ci	mbx->head = fm10k_mbx_head_add(mbx, len);
48562306a36Sopenharmony_ci	mbx->head_len = len;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	/* nothing to do if there is no data */
48862306a36Sopenharmony_ci	if (!len)
48962306a36Sopenharmony_ci		return 0;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	/* Copy msg into Rx FIFO */
49262306a36Sopenharmony_ci	fm10k_mbx_read_copy(hw, mbx);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	/* determine if there are any invalid lengths in message */
49562306a36Sopenharmony_ci	if (fm10k_mbx_validate_msg_size(mbx, len))
49662306a36Sopenharmony_ci		return FM10K_MBX_ERR_SIZE;
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	/* Update pushed */
49962306a36Sopenharmony_ci	mbx->pushed += len;
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	/* flush any completed messages */
50262306a36Sopenharmony_ci	for (len = fm10k_mbx_pushed_tail_len(mbx);
50362306a36Sopenharmony_ci	     len && (mbx->pushed >= len);
50462306a36Sopenharmony_ci	     len = fm10k_mbx_pushed_tail_len(mbx)) {
50562306a36Sopenharmony_ci		fifo->tail += len;
50662306a36Sopenharmony_ci		mbx->pushed -= len;
50762306a36Sopenharmony_ci		mbx->rx_messages++;
50862306a36Sopenharmony_ci		mbx->rx_dwords += len;
50962306a36Sopenharmony_ci	}
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	return 0;
51262306a36Sopenharmony_ci}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci/* pre-generated data for generating the CRC based on the poly 0xAC9A. */
51562306a36Sopenharmony_cistatic const u16 fm10k_crc_16b_table[256] = {
51662306a36Sopenharmony_ci	0x0000, 0x7956, 0xF2AC, 0x8BFA, 0xBC6D, 0xC53B, 0x4EC1, 0x3797,
51762306a36Sopenharmony_ci	0x21EF, 0x58B9, 0xD343, 0xAA15, 0x9D82, 0xE4D4, 0x6F2E, 0x1678,
51862306a36Sopenharmony_ci	0x43DE, 0x3A88, 0xB172, 0xC824, 0xFFB3, 0x86E5, 0x0D1F, 0x7449,
51962306a36Sopenharmony_ci	0x6231, 0x1B67, 0x909D, 0xE9CB, 0xDE5C, 0xA70A, 0x2CF0, 0x55A6,
52062306a36Sopenharmony_ci	0x87BC, 0xFEEA, 0x7510, 0x0C46, 0x3BD1, 0x4287, 0xC97D, 0xB02B,
52162306a36Sopenharmony_ci	0xA653, 0xDF05, 0x54FF, 0x2DA9, 0x1A3E, 0x6368, 0xE892, 0x91C4,
52262306a36Sopenharmony_ci	0xC462, 0xBD34, 0x36CE, 0x4F98, 0x780F, 0x0159, 0x8AA3, 0xF3F5,
52362306a36Sopenharmony_ci	0xE58D, 0x9CDB, 0x1721, 0x6E77, 0x59E0, 0x20B6, 0xAB4C, 0xD21A,
52462306a36Sopenharmony_ci	0x564D, 0x2F1B, 0xA4E1, 0xDDB7, 0xEA20, 0x9376, 0x188C, 0x61DA,
52562306a36Sopenharmony_ci	0x77A2, 0x0EF4, 0x850E, 0xFC58, 0xCBCF, 0xB299, 0x3963, 0x4035,
52662306a36Sopenharmony_ci	0x1593, 0x6CC5, 0xE73F, 0x9E69, 0xA9FE, 0xD0A8, 0x5B52, 0x2204,
52762306a36Sopenharmony_ci	0x347C, 0x4D2A, 0xC6D0, 0xBF86, 0x8811, 0xF147, 0x7ABD, 0x03EB,
52862306a36Sopenharmony_ci	0xD1F1, 0xA8A7, 0x235D, 0x5A0B, 0x6D9C, 0x14CA, 0x9F30, 0xE666,
52962306a36Sopenharmony_ci	0xF01E, 0x8948, 0x02B2, 0x7BE4, 0x4C73, 0x3525, 0xBEDF, 0xC789,
53062306a36Sopenharmony_ci	0x922F, 0xEB79, 0x6083, 0x19D5, 0x2E42, 0x5714, 0xDCEE, 0xA5B8,
53162306a36Sopenharmony_ci	0xB3C0, 0xCA96, 0x416C, 0x383A, 0x0FAD, 0x76FB, 0xFD01, 0x8457,
53262306a36Sopenharmony_ci	0xAC9A, 0xD5CC, 0x5E36, 0x2760, 0x10F7, 0x69A1, 0xE25B, 0x9B0D,
53362306a36Sopenharmony_ci	0x8D75, 0xF423, 0x7FD9, 0x068F, 0x3118, 0x484E, 0xC3B4, 0xBAE2,
53462306a36Sopenharmony_ci	0xEF44, 0x9612, 0x1DE8, 0x64BE, 0x5329, 0x2A7F, 0xA185, 0xD8D3,
53562306a36Sopenharmony_ci	0xCEAB, 0xB7FD, 0x3C07, 0x4551, 0x72C6, 0x0B90, 0x806A, 0xF93C,
53662306a36Sopenharmony_ci	0x2B26, 0x5270, 0xD98A, 0xA0DC, 0x974B, 0xEE1D, 0x65E7, 0x1CB1,
53762306a36Sopenharmony_ci	0x0AC9, 0x739F, 0xF865, 0x8133, 0xB6A4, 0xCFF2, 0x4408, 0x3D5E,
53862306a36Sopenharmony_ci	0x68F8, 0x11AE, 0x9A54, 0xE302, 0xD495, 0xADC3, 0x2639, 0x5F6F,
53962306a36Sopenharmony_ci	0x4917, 0x3041, 0xBBBB, 0xC2ED, 0xF57A, 0x8C2C, 0x07D6, 0x7E80,
54062306a36Sopenharmony_ci	0xFAD7, 0x8381, 0x087B, 0x712D, 0x46BA, 0x3FEC, 0xB416, 0xCD40,
54162306a36Sopenharmony_ci	0xDB38, 0xA26E, 0x2994, 0x50C2, 0x6755, 0x1E03, 0x95F9, 0xECAF,
54262306a36Sopenharmony_ci	0xB909, 0xC05F, 0x4BA5, 0x32F3, 0x0564, 0x7C32, 0xF7C8, 0x8E9E,
54362306a36Sopenharmony_ci	0x98E6, 0xE1B0, 0x6A4A, 0x131C, 0x248B, 0x5DDD, 0xD627, 0xAF71,
54462306a36Sopenharmony_ci	0x7D6B, 0x043D, 0x8FC7, 0xF691, 0xC106, 0xB850, 0x33AA, 0x4AFC,
54562306a36Sopenharmony_ci	0x5C84, 0x25D2, 0xAE28, 0xD77E, 0xE0E9, 0x99BF, 0x1245, 0x6B13,
54662306a36Sopenharmony_ci	0x3EB5, 0x47E3, 0xCC19, 0xB54F, 0x82D8, 0xFB8E, 0x7074, 0x0922,
54762306a36Sopenharmony_ci	0x1F5A, 0x660C, 0xEDF6, 0x94A0, 0xA337, 0xDA61, 0x519B, 0x28CD };
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci/**
55062306a36Sopenharmony_ci *  fm10k_crc_16b - Generate a 16 bit CRC for a region of 16 bit data
55162306a36Sopenharmony_ci *  @data: pointer to data to process
55262306a36Sopenharmony_ci *  @seed: seed value for CRC
55362306a36Sopenharmony_ci *  @len: length measured in 16 bits words
55462306a36Sopenharmony_ci *
55562306a36Sopenharmony_ci *  This function will generate a CRC based on the polynomial 0xAC9A and
55662306a36Sopenharmony_ci *  whatever value is stored in the seed variable.  Note that this
55762306a36Sopenharmony_ci *  value inverts the local seed and the result in order to capture all
55862306a36Sopenharmony_ci *  leading and trailing zeros.
55962306a36Sopenharmony_ci */
56062306a36Sopenharmony_cistatic u16 fm10k_crc_16b(const u32 *data, u16 seed, u16 len)
56162306a36Sopenharmony_ci{
56262306a36Sopenharmony_ci	u32 result = seed;
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	while (len--) {
56562306a36Sopenharmony_ci		result ^= *(data++);
56662306a36Sopenharmony_ci		result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
56762306a36Sopenharmony_ci		result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci		if (!(len--))
57062306a36Sopenharmony_ci			break;
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci		result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
57362306a36Sopenharmony_ci		result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
57462306a36Sopenharmony_ci	}
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	return (u16)result;
57762306a36Sopenharmony_ci}
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci/**
58062306a36Sopenharmony_ci *  fm10k_fifo_crc - generate a CRC based off of FIFO data
58162306a36Sopenharmony_ci *  @fifo: pointer to FIFO
58262306a36Sopenharmony_ci *  @offset: offset point for start of FIFO
58362306a36Sopenharmony_ci *  @len: number of DWORDS words to process
58462306a36Sopenharmony_ci *  @seed: seed value for CRC
58562306a36Sopenharmony_ci *
58662306a36Sopenharmony_ci *  This function generates a CRC for some region of the FIFO
58762306a36Sopenharmony_ci **/
58862306a36Sopenharmony_cistatic u16 fm10k_fifo_crc(struct fm10k_mbx_fifo *fifo, u16 offset,
58962306a36Sopenharmony_ci			  u16 len, u16 seed)
59062306a36Sopenharmony_ci{
59162306a36Sopenharmony_ci	u32 *data = fifo->buffer + offset;
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci	/* track when we should cross the end of the FIFO */
59462306a36Sopenharmony_ci	offset = fifo->size - offset;
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	/* if we are in 2 blocks process the end of the FIFO first */
59762306a36Sopenharmony_ci	if (offset < len) {
59862306a36Sopenharmony_ci		seed = fm10k_crc_16b(data, seed, offset * 2);
59962306a36Sopenharmony_ci		data = fifo->buffer;
60062306a36Sopenharmony_ci		len -= offset;
60162306a36Sopenharmony_ci	}
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	/* process any remaining bits */
60462306a36Sopenharmony_ci	return fm10k_crc_16b(data, seed, len * 2);
60562306a36Sopenharmony_ci}
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci/**
60862306a36Sopenharmony_ci *  fm10k_mbx_update_local_crc - Update the local CRC for outgoing data
60962306a36Sopenharmony_ci *  @mbx: pointer to mailbox
61062306a36Sopenharmony_ci *  @head: head index provided by remote mailbox
61162306a36Sopenharmony_ci *
61262306a36Sopenharmony_ci *  This function will generate the CRC for all data from the end of the
61362306a36Sopenharmony_ci *  last head update to the current one.  It uses the result of the
61462306a36Sopenharmony_ci *  previous CRC as the seed for this update.  The result is stored in
61562306a36Sopenharmony_ci *  mbx->local.
61662306a36Sopenharmony_ci **/
61762306a36Sopenharmony_cistatic void fm10k_mbx_update_local_crc(struct fm10k_mbx_info *mbx, u16 head)
61862306a36Sopenharmony_ci{
61962306a36Sopenharmony_ci	u16 len = mbx->tail_len - fm10k_mbx_index_len(mbx, head, mbx->tail);
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci	/* determine the offset for the start of the region to be pulled */
62262306a36Sopenharmony_ci	head = fm10k_fifo_head_offset(&mbx->tx, mbx->pulled);
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci	/* update local CRC to include all of the pulled data */
62562306a36Sopenharmony_ci	mbx->local = fm10k_fifo_crc(&mbx->tx, head, len, mbx->local);
62662306a36Sopenharmony_ci}
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci/**
62962306a36Sopenharmony_ci *  fm10k_mbx_verify_remote_crc - Verify the CRC is correct for current data
63062306a36Sopenharmony_ci *  @mbx: pointer to mailbox
63162306a36Sopenharmony_ci *
63262306a36Sopenharmony_ci *  This function will take all data that has been provided from the remote
63362306a36Sopenharmony_ci *  end and generate a CRC for it.  This is stored in mbx->remote.  The
63462306a36Sopenharmony_ci *  CRC for the header is then computed and if the result is non-zero this
63562306a36Sopenharmony_ci *  is an error and we signal an error dropping all data and resetting the
63662306a36Sopenharmony_ci *  connection.
63762306a36Sopenharmony_ci */
63862306a36Sopenharmony_cistatic s32 fm10k_mbx_verify_remote_crc(struct fm10k_mbx_info *mbx)
63962306a36Sopenharmony_ci{
64062306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->rx;
64162306a36Sopenharmony_ci	u16 len = mbx->head_len;
64262306a36Sopenharmony_ci	u16 offset = fm10k_fifo_tail_offset(fifo, mbx->pushed) - len;
64362306a36Sopenharmony_ci	u16 crc;
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	/* update the remote CRC if new data has been received */
64662306a36Sopenharmony_ci	if (len)
64762306a36Sopenharmony_ci		mbx->remote = fm10k_fifo_crc(fifo, offset, len, mbx->remote);
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	/* process the full header as we have to validate the CRC */
65062306a36Sopenharmony_ci	crc = fm10k_crc_16b(&mbx->mbx_hdr, mbx->remote, 1);
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci	/* notify other end if we have a problem */
65362306a36Sopenharmony_ci	return crc ? FM10K_MBX_ERR_CRC : 0;
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci/**
65762306a36Sopenharmony_ci *  fm10k_mbx_rx_ready - Indicates that a message is ready in the Rx FIFO
65862306a36Sopenharmony_ci *  @mbx: pointer to mailbox
65962306a36Sopenharmony_ci *
66062306a36Sopenharmony_ci *  This function returns true if there is a message in the Rx FIFO to dequeue.
66162306a36Sopenharmony_ci **/
66262306a36Sopenharmony_cistatic bool fm10k_mbx_rx_ready(struct fm10k_mbx_info *mbx)
66362306a36Sopenharmony_ci{
66462306a36Sopenharmony_ci	u16 msg_size = fm10k_fifo_head_len(&mbx->rx);
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci	return msg_size && (fm10k_fifo_used(&mbx->rx) >= msg_size);
66762306a36Sopenharmony_ci}
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci/**
67062306a36Sopenharmony_ci *  fm10k_mbx_tx_ready - Indicates that the mailbox is in state ready for Tx
67162306a36Sopenharmony_ci *  @mbx: pointer to mailbox
67262306a36Sopenharmony_ci *  @len: verify free space is >= this value
67362306a36Sopenharmony_ci *
67462306a36Sopenharmony_ci *  This function returns true if the mailbox is in a state ready to transmit.
67562306a36Sopenharmony_ci **/
67662306a36Sopenharmony_cistatic bool fm10k_mbx_tx_ready(struct fm10k_mbx_info *mbx, u16 len)
67762306a36Sopenharmony_ci{
67862306a36Sopenharmony_ci	u16 fifo_unused = fm10k_fifo_unused(&mbx->tx);
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci	return (mbx->state == FM10K_STATE_OPEN) && (fifo_unused >= len);
68162306a36Sopenharmony_ci}
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci/**
68462306a36Sopenharmony_ci *  fm10k_mbx_tx_complete - Indicates that the Tx FIFO has been emptied
68562306a36Sopenharmony_ci *  @mbx: pointer to mailbox
68662306a36Sopenharmony_ci *
68762306a36Sopenharmony_ci *  This function returns true if the Tx FIFO is empty.
68862306a36Sopenharmony_ci **/
68962306a36Sopenharmony_cistatic bool fm10k_mbx_tx_complete(struct fm10k_mbx_info *mbx)
69062306a36Sopenharmony_ci{
69162306a36Sopenharmony_ci	return fm10k_fifo_empty(&mbx->tx);
69262306a36Sopenharmony_ci}
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_ci/**
69562306a36Sopenharmony_ci *  fm10k_mbx_dequeue_rx - Dequeues the message from the head in the Rx FIFO
69662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
69762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
69862306a36Sopenharmony_ci *
69962306a36Sopenharmony_ci *  This function dequeues messages and hands them off to the TLV parser.
70062306a36Sopenharmony_ci *  It will return the number of messages processed when called.
70162306a36Sopenharmony_ci **/
70262306a36Sopenharmony_cistatic u16 fm10k_mbx_dequeue_rx(struct fm10k_hw *hw,
70362306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx)
70462306a36Sopenharmony_ci{
70562306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->rx;
70662306a36Sopenharmony_ci	s32 err;
70762306a36Sopenharmony_ci	u16 cnt;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	/* parse Rx messages out of the Rx FIFO to empty it */
71062306a36Sopenharmony_ci	for (cnt = 0; !fm10k_fifo_empty(fifo); cnt++) {
71162306a36Sopenharmony_ci		err = fm10k_tlv_msg_parse(hw, fifo->buffer + fifo->head,
71262306a36Sopenharmony_ci					  mbx, mbx->msg_data);
71362306a36Sopenharmony_ci		if (err < 0)
71462306a36Sopenharmony_ci			mbx->rx_parse_err++;
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci		fm10k_fifo_head_drop(fifo);
71762306a36Sopenharmony_ci	}
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci	/* shift remaining bytes back to start of FIFO */
72062306a36Sopenharmony_ci	memmove(fifo->buffer, fifo->buffer + fifo->tail, mbx->pushed << 2);
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	/* shift head and tail based on the memory we moved */
72362306a36Sopenharmony_ci	fifo->tail -= fifo->head;
72462306a36Sopenharmony_ci	fifo->head = 0;
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci	return cnt;
72762306a36Sopenharmony_ci}
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci/**
73062306a36Sopenharmony_ci *  fm10k_mbx_enqueue_tx - Enqueues the message to the tail of the Tx FIFO
73162306a36Sopenharmony_ci *  @hw: pointer to hardware structure
73262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
73362306a36Sopenharmony_ci *  @msg: message array to read
73462306a36Sopenharmony_ci *
73562306a36Sopenharmony_ci *  This function enqueues a message up to the size specified by the length
73662306a36Sopenharmony_ci *  contained in the first DWORD of the message and will place at the tail
73762306a36Sopenharmony_ci *  of the FIFO.  It will return 0 on success, or a negative value on error.
73862306a36Sopenharmony_ci **/
73962306a36Sopenharmony_cistatic s32 fm10k_mbx_enqueue_tx(struct fm10k_hw *hw,
74062306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx, const u32 *msg)
74162306a36Sopenharmony_ci{
74262306a36Sopenharmony_ci	u32 countdown = mbx->timeout;
74362306a36Sopenharmony_ci	s32 err;
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	switch (mbx->state) {
74662306a36Sopenharmony_ci	case FM10K_STATE_CLOSED:
74762306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
74862306a36Sopenharmony_ci		return FM10K_MBX_ERR_NO_MBX;
74962306a36Sopenharmony_ci	default:
75062306a36Sopenharmony_ci		break;
75162306a36Sopenharmony_ci	}
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci	/* enqueue the message on the Tx FIFO */
75462306a36Sopenharmony_ci	err = fm10k_fifo_enqueue(&mbx->tx, msg);
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_ci	/* if it failed give the FIFO a chance to drain */
75762306a36Sopenharmony_ci	while (err && countdown) {
75862306a36Sopenharmony_ci		countdown--;
75962306a36Sopenharmony_ci		udelay(mbx->udelay);
76062306a36Sopenharmony_ci		mbx->ops.process(hw, mbx);
76162306a36Sopenharmony_ci		err = fm10k_fifo_enqueue(&mbx->tx, msg);
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	/* if we failed treat the error */
76562306a36Sopenharmony_ci	if (err) {
76662306a36Sopenharmony_ci		mbx->timeout = 0;
76762306a36Sopenharmony_ci		mbx->tx_busy++;
76862306a36Sopenharmony_ci	}
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci	/* begin processing message, ignore errors as this is just meant
77162306a36Sopenharmony_ci	 * to start the mailbox flow so we are not concerned if there
77262306a36Sopenharmony_ci	 * is a bad error, or the mailbox is already busy with a request
77362306a36Sopenharmony_ci	 */
77462306a36Sopenharmony_ci	if (!mbx->tail_len)
77562306a36Sopenharmony_ci		mbx->ops.process(hw, mbx);
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci	return 0;
77862306a36Sopenharmony_ci}
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci/**
78162306a36Sopenharmony_ci *  fm10k_mbx_read - Copies the mbmem to local message buffer
78262306a36Sopenharmony_ci *  @hw: pointer to hardware structure
78362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
78462306a36Sopenharmony_ci *
78562306a36Sopenharmony_ci *  This function copies the message from the mbmem to the message array
78662306a36Sopenharmony_ci **/
78762306a36Sopenharmony_cistatic s32 fm10k_mbx_read(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
78862306a36Sopenharmony_ci{
78962306a36Sopenharmony_ci	/* only allow one reader in here at a time */
79062306a36Sopenharmony_ci	if (mbx->mbx_hdr)
79162306a36Sopenharmony_ci		return FM10K_MBX_ERR_BUSY;
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci	/* read to capture initial interrupt bits */
79462306a36Sopenharmony_ci	if (fm10k_read_reg(hw, mbx->mbx_reg) & FM10K_MBX_REQ_INTERRUPT)
79562306a36Sopenharmony_ci		mbx->mbx_lock = FM10K_MBX_ACK;
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ci	/* write back interrupt bits to clear */
79862306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbx_reg,
79962306a36Sopenharmony_ci			FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT);
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	/* read remote header */
80262306a36Sopenharmony_ci	mbx->mbx_hdr = fm10k_read_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len);
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci	return 0;
80562306a36Sopenharmony_ci}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci/**
80862306a36Sopenharmony_ci *  fm10k_mbx_write - Copies the local message buffer to mbmem
80962306a36Sopenharmony_ci *  @hw: pointer to hardware structure
81062306a36Sopenharmony_ci *  @mbx: pointer to mailbox
81162306a36Sopenharmony_ci *
81262306a36Sopenharmony_ci *  This function copies the message from the message array to mbmem
81362306a36Sopenharmony_ci **/
81462306a36Sopenharmony_cistatic void fm10k_mbx_write(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
81562306a36Sopenharmony_ci{
81662306a36Sopenharmony_ci	u32 mbmem = mbx->mbmem_reg;
81762306a36Sopenharmony_ci
81862306a36Sopenharmony_ci	/* write new msg header to notify recipient of change */
81962306a36Sopenharmony_ci	fm10k_write_reg(hw, mbmem, mbx->mbx_hdr);
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci	/* write mailbox to send interrupt */
82262306a36Sopenharmony_ci	if (mbx->mbx_lock)
82362306a36Sopenharmony_ci		fm10k_write_reg(hw, mbx->mbx_reg, mbx->mbx_lock);
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_ci	/* we no longer are using the header so free it */
82662306a36Sopenharmony_ci	mbx->mbx_hdr = 0;
82762306a36Sopenharmony_ci	mbx->mbx_lock = 0;
82862306a36Sopenharmony_ci}
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci/**
83162306a36Sopenharmony_ci *  fm10k_mbx_create_connect_hdr - Generate a connect mailbox header
83262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
83362306a36Sopenharmony_ci *
83462306a36Sopenharmony_ci *  This function returns a connection mailbox header
83562306a36Sopenharmony_ci **/
83662306a36Sopenharmony_cistatic void fm10k_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx)
83762306a36Sopenharmony_ci{
83862306a36Sopenharmony_ci	mbx->mbx_lock |= FM10K_MBX_REQ;
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_ci	mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_CONNECT, TYPE) |
84162306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD) |
84262306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->rx.size - 1, CONNECT_SIZE);
84362306a36Sopenharmony_ci}
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci/**
84662306a36Sopenharmony_ci *  fm10k_mbx_create_data_hdr - Generate a data mailbox header
84762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
84862306a36Sopenharmony_ci *
84962306a36Sopenharmony_ci *  This function returns a data mailbox header
85062306a36Sopenharmony_ci **/
85162306a36Sopenharmony_cistatic void fm10k_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
85262306a36Sopenharmony_ci{
85362306a36Sopenharmony_ci	u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DATA, TYPE) |
85462306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
85562306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
85662306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->tx;
85762306a36Sopenharmony_ci	u16 crc;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci	if (mbx->tail_len)
86062306a36Sopenharmony_ci		mbx->mbx_lock |= FM10K_MBX_REQ;
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci	/* generate CRC for data in flight and header */
86362306a36Sopenharmony_ci	crc = fm10k_fifo_crc(fifo, fm10k_fifo_head_offset(fifo, mbx->pulled),
86462306a36Sopenharmony_ci			     mbx->tail_len, mbx->local);
86562306a36Sopenharmony_ci	crc = fm10k_crc_16b(&hdr, crc, 1);
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci	/* load header to memory to be written */
86862306a36Sopenharmony_ci	mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
86962306a36Sopenharmony_ci}
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci/**
87262306a36Sopenharmony_ci *  fm10k_mbx_create_disconnect_hdr - Generate a disconnect mailbox header
87362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
87462306a36Sopenharmony_ci *
87562306a36Sopenharmony_ci *  This function returns a disconnect mailbox header
87662306a36Sopenharmony_ci **/
87762306a36Sopenharmony_cistatic void fm10k_mbx_create_disconnect_hdr(struct fm10k_mbx_info *mbx)
87862306a36Sopenharmony_ci{
87962306a36Sopenharmony_ci	u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
88062306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
88162306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
88262306a36Sopenharmony_ci	u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci	mbx->mbx_lock |= FM10K_MBX_ACK;
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci	/* load header to memory to be written */
88762306a36Sopenharmony_ci	mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
88862306a36Sopenharmony_ci}
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci/**
89162306a36Sopenharmony_ci *  fm10k_mbx_create_fake_disconnect_hdr - Generate a false disconnect mbox hdr
89262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
89362306a36Sopenharmony_ci *
89462306a36Sopenharmony_ci *  This function creates a fake disconnect header for loading into remote
89562306a36Sopenharmony_ci *  mailbox header. The primary purpose is to prevent errors on immediate
89662306a36Sopenharmony_ci *  start up after mbx->connect.
89762306a36Sopenharmony_ci **/
89862306a36Sopenharmony_cistatic void fm10k_mbx_create_fake_disconnect_hdr(struct fm10k_mbx_info *mbx)
89962306a36Sopenharmony_ci{
90062306a36Sopenharmony_ci	u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
90162306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->head, TAIL) |
90262306a36Sopenharmony_ci		  FM10K_MSG_HDR_FIELD_SET(mbx->tail, HEAD);
90362306a36Sopenharmony_ci	u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci	mbx->mbx_lock |= FM10K_MBX_ACK;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	/* load header to memory to be written */
90862306a36Sopenharmony_ci	mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
90962306a36Sopenharmony_ci}
91062306a36Sopenharmony_ci
91162306a36Sopenharmony_ci/**
91262306a36Sopenharmony_ci *  fm10k_mbx_create_error_msg - Generate an error message
91362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
91462306a36Sopenharmony_ci *  @err: local error encountered
91562306a36Sopenharmony_ci *
91662306a36Sopenharmony_ci *  This function will interpret the error provided by err, and based on
91762306a36Sopenharmony_ci *  that it may shift the message by 1 DWORD and then place an error header
91862306a36Sopenharmony_ci *  at the start of the message.
91962306a36Sopenharmony_ci **/
92062306a36Sopenharmony_cistatic void fm10k_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
92162306a36Sopenharmony_ci{
92262306a36Sopenharmony_ci	/* only generate an error message for these types */
92362306a36Sopenharmony_ci	switch (err) {
92462306a36Sopenharmony_ci	case FM10K_MBX_ERR_TAIL:
92562306a36Sopenharmony_ci	case FM10K_MBX_ERR_HEAD:
92662306a36Sopenharmony_ci	case FM10K_MBX_ERR_TYPE:
92762306a36Sopenharmony_ci	case FM10K_MBX_ERR_SIZE:
92862306a36Sopenharmony_ci	case FM10K_MBX_ERR_RSVD0:
92962306a36Sopenharmony_ci	case FM10K_MBX_ERR_CRC:
93062306a36Sopenharmony_ci		break;
93162306a36Sopenharmony_ci	default:
93262306a36Sopenharmony_ci		return;
93362306a36Sopenharmony_ci	}
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci	mbx->mbx_lock |= FM10K_MBX_REQ;
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci	mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_ERROR, TYPE) |
93862306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(err, ERR_NO) |
93962306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
94062306a36Sopenharmony_ci}
94162306a36Sopenharmony_ci
94262306a36Sopenharmony_ci/**
94362306a36Sopenharmony_ci *  fm10k_mbx_validate_msg_hdr - Validate common fields in the message header
94462306a36Sopenharmony_ci *  @mbx: pointer to mailbox
94562306a36Sopenharmony_ci *
94662306a36Sopenharmony_ci *  This function will parse up the fields in the mailbox header and return
94762306a36Sopenharmony_ci *  an error if the header contains any of a number of invalid configurations
94862306a36Sopenharmony_ci *  including unrecognized type, invalid route, or a malformed message.
94962306a36Sopenharmony_ci **/
95062306a36Sopenharmony_cistatic s32 fm10k_mbx_validate_msg_hdr(struct fm10k_mbx_info *mbx)
95162306a36Sopenharmony_ci{
95262306a36Sopenharmony_ci	u16 type, rsvd0, head, tail, size;
95362306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci	type = FM10K_MSG_HDR_FIELD_GET(*hdr, TYPE);
95662306a36Sopenharmony_ci	rsvd0 = FM10K_MSG_HDR_FIELD_GET(*hdr, RSVD0);
95762306a36Sopenharmony_ci	tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
95862306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
95962306a36Sopenharmony_ci	size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci	if (rsvd0)
96262306a36Sopenharmony_ci		return FM10K_MBX_ERR_RSVD0;
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	switch (type) {
96562306a36Sopenharmony_ci	case FM10K_MSG_DISCONNECT:
96662306a36Sopenharmony_ci		/* validate that all data has been received */
96762306a36Sopenharmony_ci		if (tail != mbx->head)
96862306a36Sopenharmony_ci			return FM10K_MBX_ERR_TAIL;
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci		fallthrough;
97162306a36Sopenharmony_ci	case FM10K_MSG_DATA:
97262306a36Sopenharmony_ci		/* validate that head is moving correctly */
97362306a36Sopenharmony_ci		if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
97462306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
97562306a36Sopenharmony_ci		if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
97662306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci		/* validate that tail is moving correctly */
97962306a36Sopenharmony_ci		if (!tail || (tail == FM10K_MSG_HDR_MASK(TAIL)))
98062306a36Sopenharmony_ci			return FM10K_MBX_ERR_TAIL;
98162306a36Sopenharmony_ci		if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
98262306a36Sopenharmony_ci			break;
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci		return FM10K_MBX_ERR_TAIL;
98562306a36Sopenharmony_ci	case FM10K_MSG_CONNECT:
98662306a36Sopenharmony_ci		/* validate size is in range and is power of 2 mask */
98762306a36Sopenharmony_ci		if ((size < FM10K_VFMBX_MSG_MTU) || (size & (size + 1)))
98862306a36Sopenharmony_ci			return FM10K_MBX_ERR_SIZE;
98962306a36Sopenharmony_ci
99062306a36Sopenharmony_ci		fallthrough;
99162306a36Sopenharmony_ci	case FM10K_MSG_ERROR:
99262306a36Sopenharmony_ci		if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
99362306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
99462306a36Sopenharmony_ci		/* neither create nor error include a tail offset */
99562306a36Sopenharmony_ci		if (tail)
99662306a36Sopenharmony_ci			return FM10K_MBX_ERR_TAIL;
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci		break;
99962306a36Sopenharmony_ci	default:
100062306a36Sopenharmony_ci		return FM10K_MBX_ERR_TYPE;
100162306a36Sopenharmony_ci	}
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci	return 0;
100462306a36Sopenharmony_ci}
100562306a36Sopenharmony_ci
100662306a36Sopenharmony_ci/**
100762306a36Sopenharmony_ci *  fm10k_mbx_create_reply - Generate reply based on state and remote head
100862306a36Sopenharmony_ci *  @hw: pointer to hardware structure
100962306a36Sopenharmony_ci *  @mbx: pointer to mailbox
101062306a36Sopenharmony_ci *  @head: acknowledgement number
101162306a36Sopenharmony_ci *
101262306a36Sopenharmony_ci *  This function will generate an outgoing message based on the current
101362306a36Sopenharmony_ci *  mailbox state and the remote FIFO head.  It will return the length
101462306a36Sopenharmony_ci *  of the outgoing message excluding header on success, and a negative value
101562306a36Sopenharmony_ci *  on error.
101662306a36Sopenharmony_ci **/
101762306a36Sopenharmony_cistatic s32 fm10k_mbx_create_reply(struct fm10k_hw *hw,
101862306a36Sopenharmony_ci				  struct fm10k_mbx_info *mbx, u16 head)
101962306a36Sopenharmony_ci{
102062306a36Sopenharmony_ci	switch (mbx->state) {
102162306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
102262306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
102362306a36Sopenharmony_ci		/* update our checksum for the outgoing data */
102462306a36Sopenharmony_ci		fm10k_mbx_update_local_crc(mbx, head);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci		/* as long as other end recognizes us keep sending data */
102762306a36Sopenharmony_ci		fm10k_mbx_pull_head(hw, mbx, head);
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci		/* generate new header based on data */
103062306a36Sopenharmony_ci		if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN))
103162306a36Sopenharmony_ci			fm10k_mbx_create_data_hdr(mbx);
103262306a36Sopenharmony_ci		else
103362306a36Sopenharmony_ci			fm10k_mbx_create_disconnect_hdr(mbx);
103462306a36Sopenharmony_ci		break;
103562306a36Sopenharmony_ci	case FM10K_STATE_CONNECT:
103662306a36Sopenharmony_ci		/* send disconnect even if we aren't connected */
103762306a36Sopenharmony_ci		fm10k_mbx_create_connect_hdr(mbx);
103862306a36Sopenharmony_ci		break;
103962306a36Sopenharmony_ci	case FM10K_STATE_CLOSED:
104062306a36Sopenharmony_ci		/* generate new header based on data */
104162306a36Sopenharmony_ci		fm10k_mbx_create_disconnect_hdr(mbx);
104262306a36Sopenharmony_ci		break;
104362306a36Sopenharmony_ci	default:
104462306a36Sopenharmony_ci		break;
104562306a36Sopenharmony_ci	}
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci	return 0;
104862306a36Sopenharmony_ci}
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci/**
105162306a36Sopenharmony_ci *  fm10k_mbx_reset_work- Reset internal pointers for any pending work
105262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
105362306a36Sopenharmony_ci *
105462306a36Sopenharmony_ci *  This function will reset all internal pointers so any work in progress
105562306a36Sopenharmony_ci *  is dropped.  This call should occur every time we transition from the
105662306a36Sopenharmony_ci *  open state to the connect state.
105762306a36Sopenharmony_ci **/
105862306a36Sopenharmony_cistatic void fm10k_mbx_reset_work(struct fm10k_mbx_info *mbx)
105962306a36Sopenharmony_ci{
106062306a36Sopenharmony_ci	u16 len, head, ack;
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci	/* reset our outgoing max size back to Rx limits */
106362306a36Sopenharmony_ci	mbx->max_size = mbx->rx.size - 1;
106462306a36Sopenharmony_ci
106562306a36Sopenharmony_ci	/* update mbx->pulled to account for tail_len and ack */
106662306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, HEAD);
106762306a36Sopenharmony_ci	ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
106862306a36Sopenharmony_ci	mbx->pulled += mbx->tail_len - ack;
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_ci	/* now drop any messages which have started or finished transmitting */
107162306a36Sopenharmony_ci	while (fm10k_fifo_head_len(&mbx->tx) && mbx->pulled) {
107262306a36Sopenharmony_ci		len = fm10k_fifo_head_drop(&mbx->tx);
107362306a36Sopenharmony_ci		mbx->tx_dropped++;
107462306a36Sopenharmony_ci		if (mbx->pulled >= len)
107562306a36Sopenharmony_ci			mbx->pulled -= len;
107662306a36Sopenharmony_ci		else
107762306a36Sopenharmony_ci			mbx->pulled = 0;
107862306a36Sopenharmony_ci	}
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci	/* just do a quick resysnc to start of message */
108162306a36Sopenharmony_ci	mbx->pushed = 0;
108262306a36Sopenharmony_ci	mbx->pulled = 0;
108362306a36Sopenharmony_ci	mbx->tail_len = 0;
108462306a36Sopenharmony_ci	mbx->head_len = 0;
108562306a36Sopenharmony_ci	mbx->rx.tail = 0;
108662306a36Sopenharmony_ci	mbx->rx.head = 0;
108762306a36Sopenharmony_ci}
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci/**
109062306a36Sopenharmony_ci *  fm10k_mbx_update_max_size - Update the max_size and drop any large messages
109162306a36Sopenharmony_ci *  @mbx: pointer to mailbox
109262306a36Sopenharmony_ci *  @size: new value for max_size
109362306a36Sopenharmony_ci *
109462306a36Sopenharmony_ci *  This function updates the max_size value and drops any outgoing messages
109562306a36Sopenharmony_ci *  at the head of the Tx FIFO if they are larger than max_size. It does not
109662306a36Sopenharmony_ci *  drop all messages, as this is too difficult to parse and remove them from
109762306a36Sopenharmony_ci *  the FIFO. Instead, rely on the checking to ensure that messages larger
109862306a36Sopenharmony_ci *  than max_size aren't pushed into the memory buffer.
109962306a36Sopenharmony_ci **/
110062306a36Sopenharmony_cistatic void fm10k_mbx_update_max_size(struct fm10k_mbx_info *mbx, u16 size)
110162306a36Sopenharmony_ci{
110262306a36Sopenharmony_ci	u16 len;
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci	mbx->max_size = size;
110562306a36Sopenharmony_ci
110662306a36Sopenharmony_ci	/* flush any oversized messages from the queue */
110762306a36Sopenharmony_ci	for (len = fm10k_fifo_head_len(&mbx->tx);
110862306a36Sopenharmony_ci	     len > size;
110962306a36Sopenharmony_ci	     len = fm10k_fifo_head_len(&mbx->tx)) {
111062306a36Sopenharmony_ci		fm10k_fifo_head_drop(&mbx->tx);
111162306a36Sopenharmony_ci		mbx->tx_dropped++;
111262306a36Sopenharmony_ci	}
111362306a36Sopenharmony_ci}
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci/**
111662306a36Sopenharmony_ci *  fm10k_mbx_connect_reset - Reset following request for reset
111762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
111862306a36Sopenharmony_ci *
111962306a36Sopenharmony_ci *  This function resets the mailbox to either a disconnected state
112062306a36Sopenharmony_ci *  or a connect state depending on the current mailbox state
112162306a36Sopenharmony_ci **/
112262306a36Sopenharmony_cistatic void fm10k_mbx_connect_reset(struct fm10k_mbx_info *mbx)
112362306a36Sopenharmony_ci{
112462306a36Sopenharmony_ci	/* just do a quick resysnc to start of frame */
112562306a36Sopenharmony_ci	fm10k_mbx_reset_work(mbx);
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci	/* reset CRC seeds */
112862306a36Sopenharmony_ci	mbx->local = FM10K_MBX_CRC_SEED;
112962306a36Sopenharmony_ci	mbx->remote = FM10K_MBX_CRC_SEED;
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci	/* we cannot exit connect until the size is good */
113262306a36Sopenharmony_ci	if (mbx->state == FM10K_STATE_OPEN)
113362306a36Sopenharmony_ci		mbx->state = FM10K_STATE_CONNECT;
113462306a36Sopenharmony_ci	else
113562306a36Sopenharmony_ci		mbx->state = FM10K_STATE_CLOSED;
113662306a36Sopenharmony_ci}
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci/**
113962306a36Sopenharmony_ci *  fm10k_mbx_process_connect - Process connect header
114062306a36Sopenharmony_ci *  @hw: pointer to hardware structure
114162306a36Sopenharmony_ci *  @mbx: pointer to mailbox
114262306a36Sopenharmony_ci *
114362306a36Sopenharmony_ci *  This function will read an incoming connect header and reply with the
114462306a36Sopenharmony_ci *  appropriate message.  It will return a value indicating the number of
114562306a36Sopenharmony_ci *  data DWORDs on success, or will return a negative value on failure.
114662306a36Sopenharmony_ci **/
114762306a36Sopenharmony_cistatic s32 fm10k_mbx_process_connect(struct fm10k_hw *hw,
114862306a36Sopenharmony_ci				     struct fm10k_mbx_info *mbx)
114962306a36Sopenharmony_ci{
115062306a36Sopenharmony_ci	const enum fm10k_mbx_state state = mbx->state;
115162306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
115262306a36Sopenharmony_ci	u16 size, head;
115362306a36Sopenharmony_ci
115462306a36Sopenharmony_ci	/* we will need to pull all of the fields for verification */
115562306a36Sopenharmony_ci	size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
115662306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	switch (state) {
115962306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
116062306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
116162306a36Sopenharmony_ci		/* reset any in-progress work */
116262306a36Sopenharmony_ci		fm10k_mbx_connect_reset(mbx);
116362306a36Sopenharmony_ci		break;
116462306a36Sopenharmony_ci	case FM10K_STATE_CONNECT:
116562306a36Sopenharmony_ci		/* we cannot exit connect until the size is good */
116662306a36Sopenharmony_ci		if (size > mbx->rx.size) {
116762306a36Sopenharmony_ci			mbx->max_size = mbx->rx.size - 1;
116862306a36Sopenharmony_ci		} else {
116962306a36Sopenharmony_ci			/* record the remote system requesting connection */
117062306a36Sopenharmony_ci			mbx->state = FM10K_STATE_OPEN;
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci			fm10k_mbx_update_max_size(mbx, size);
117362306a36Sopenharmony_ci		}
117462306a36Sopenharmony_ci		break;
117562306a36Sopenharmony_ci	default:
117662306a36Sopenharmony_ci		break;
117762306a36Sopenharmony_ci	}
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_ci	/* align our tail index to remote head index */
118062306a36Sopenharmony_ci	mbx->tail = head;
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci	return fm10k_mbx_create_reply(hw, mbx, head);
118362306a36Sopenharmony_ci}
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci/**
118662306a36Sopenharmony_ci *  fm10k_mbx_process_data - Process data header
118762306a36Sopenharmony_ci *  @hw: pointer to hardware structure
118862306a36Sopenharmony_ci *  @mbx: pointer to mailbox
118962306a36Sopenharmony_ci *
119062306a36Sopenharmony_ci *  This function will read an incoming data header and reply with the
119162306a36Sopenharmony_ci *  appropriate message.  It will return a value indicating the number of
119262306a36Sopenharmony_ci *  data DWORDs on success, or will return a negative value on failure.
119362306a36Sopenharmony_ci **/
119462306a36Sopenharmony_cistatic s32 fm10k_mbx_process_data(struct fm10k_hw *hw,
119562306a36Sopenharmony_ci				  struct fm10k_mbx_info *mbx)
119662306a36Sopenharmony_ci{
119762306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
119862306a36Sopenharmony_ci	u16 head, tail;
119962306a36Sopenharmony_ci	s32 err;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	/* we will need to pull all of the fields for verification */
120262306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
120362306a36Sopenharmony_ci	tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
120462306a36Sopenharmony_ci
120562306a36Sopenharmony_ci	/* if we are in connect just update our data and go */
120662306a36Sopenharmony_ci	if (mbx->state == FM10K_STATE_CONNECT) {
120762306a36Sopenharmony_ci		mbx->tail = head;
120862306a36Sopenharmony_ci		mbx->state = FM10K_STATE_OPEN;
120962306a36Sopenharmony_ci	}
121062306a36Sopenharmony_ci
121162306a36Sopenharmony_ci	/* abort on message size errors */
121262306a36Sopenharmony_ci	err = fm10k_mbx_push_tail(hw, mbx, tail);
121362306a36Sopenharmony_ci	if (err < 0)
121462306a36Sopenharmony_ci		return err;
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci	/* verify the checksum on the incoming data */
121762306a36Sopenharmony_ci	err = fm10k_mbx_verify_remote_crc(mbx);
121862306a36Sopenharmony_ci	if (err)
121962306a36Sopenharmony_ci		return err;
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	/* process messages if we have received any */
122262306a36Sopenharmony_ci	fm10k_mbx_dequeue_rx(hw, mbx);
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_ci	return fm10k_mbx_create_reply(hw, mbx, head);
122562306a36Sopenharmony_ci}
122662306a36Sopenharmony_ci
122762306a36Sopenharmony_ci/**
122862306a36Sopenharmony_ci *  fm10k_mbx_process_disconnect - Process disconnect header
122962306a36Sopenharmony_ci *  @hw: pointer to hardware structure
123062306a36Sopenharmony_ci *  @mbx: pointer to mailbox
123162306a36Sopenharmony_ci *
123262306a36Sopenharmony_ci *  This function will read an incoming disconnect header and reply with the
123362306a36Sopenharmony_ci *  appropriate message.  It will return a value indicating the number of
123462306a36Sopenharmony_ci *  data DWORDs on success, or will return a negative value on failure.
123562306a36Sopenharmony_ci **/
123662306a36Sopenharmony_cistatic s32 fm10k_mbx_process_disconnect(struct fm10k_hw *hw,
123762306a36Sopenharmony_ci					struct fm10k_mbx_info *mbx)
123862306a36Sopenharmony_ci{
123962306a36Sopenharmony_ci	const enum fm10k_mbx_state state = mbx->state;
124062306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
124162306a36Sopenharmony_ci	u16 head;
124262306a36Sopenharmony_ci	s32 err;
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	/* we will need to pull the header field for verification */
124562306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
124662306a36Sopenharmony_ci
124762306a36Sopenharmony_ci	/* We should not be receiving disconnect if Rx is incomplete */
124862306a36Sopenharmony_ci	if (mbx->pushed)
124962306a36Sopenharmony_ci		return FM10K_MBX_ERR_TAIL;
125062306a36Sopenharmony_ci
125162306a36Sopenharmony_ci	/* we have already verified mbx->head == tail so we know this is 0 */
125262306a36Sopenharmony_ci	mbx->head_len = 0;
125362306a36Sopenharmony_ci
125462306a36Sopenharmony_ci	/* verify the checksum on the incoming header is correct */
125562306a36Sopenharmony_ci	err = fm10k_mbx_verify_remote_crc(mbx);
125662306a36Sopenharmony_ci	if (err)
125762306a36Sopenharmony_ci		return err;
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_ci	switch (state) {
126062306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
126162306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
126262306a36Sopenharmony_ci		/* state doesn't change if we still have work to do */
126362306a36Sopenharmony_ci		if (!fm10k_mbx_tx_complete(mbx))
126462306a36Sopenharmony_ci			break;
126562306a36Sopenharmony_ci
126662306a36Sopenharmony_ci		/* verify the head indicates we completed all transmits */
126762306a36Sopenharmony_ci		if (head != mbx->tail)
126862306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci		/* reset any in-progress work */
127162306a36Sopenharmony_ci		fm10k_mbx_connect_reset(mbx);
127262306a36Sopenharmony_ci		break;
127362306a36Sopenharmony_ci	default:
127462306a36Sopenharmony_ci		break;
127562306a36Sopenharmony_ci	}
127662306a36Sopenharmony_ci
127762306a36Sopenharmony_ci	return fm10k_mbx_create_reply(hw, mbx, head);
127862306a36Sopenharmony_ci}
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci/**
128162306a36Sopenharmony_ci *  fm10k_mbx_process_error - Process error header
128262306a36Sopenharmony_ci *  @hw: pointer to hardware structure
128362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
128462306a36Sopenharmony_ci *
128562306a36Sopenharmony_ci *  This function will read an incoming error header and reply with the
128662306a36Sopenharmony_ci *  appropriate message.  It will return a value indicating the number of
128762306a36Sopenharmony_ci *  data DWORDs on success, or will return a negative value on failure.
128862306a36Sopenharmony_ci **/
128962306a36Sopenharmony_cistatic s32 fm10k_mbx_process_error(struct fm10k_hw *hw,
129062306a36Sopenharmony_ci				   struct fm10k_mbx_info *mbx)
129162306a36Sopenharmony_ci{
129262306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
129362306a36Sopenharmony_ci	u16 head;
129462306a36Sopenharmony_ci
129562306a36Sopenharmony_ci	/* we will need to pull all of the fields for verification */
129662306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
129762306a36Sopenharmony_ci
129862306a36Sopenharmony_ci	switch (mbx->state) {
129962306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
130062306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
130162306a36Sopenharmony_ci		/* flush any uncompleted work */
130262306a36Sopenharmony_ci		fm10k_mbx_reset_work(mbx);
130362306a36Sopenharmony_ci
130462306a36Sopenharmony_ci		/* reset CRC seeds */
130562306a36Sopenharmony_ci		mbx->local = FM10K_MBX_CRC_SEED;
130662306a36Sopenharmony_ci		mbx->remote = FM10K_MBX_CRC_SEED;
130762306a36Sopenharmony_ci
130862306a36Sopenharmony_ci		/* reset tail index and size to prepare for reconnect */
130962306a36Sopenharmony_ci		mbx->tail = head;
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci		/* if open then reset max_size and go back to connect */
131262306a36Sopenharmony_ci		if (mbx->state == FM10K_STATE_OPEN) {
131362306a36Sopenharmony_ci			mbx->state = FM10K_STATE_CONNECT;
131462306a36Sopenharmony_ci			break;
131562306a36Sopenharmony_ci		}
131662306a36Sopenharmony_ci
131762306a36Sopenharmony_ci		/* send a connect message to get data flowing again */
131862306a36Sopenharmony_ci		fm10k_mbx_create_connect_hdr(mbx);
131962306a36Sopenharmony_ci		return 0;
132062306a36Sopenharmony_ci	default:
132162306a36Sopenharmony_ci		break;
132262306a36Sopenharmony_ci	}
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ci	return fm10k_mbx_create_reply(hw, mbx, mbx->tail);
132562306a36Sopenharmony_ci}
132662306a36Sopenharmony_ci
132762306a36Sopenharmony_ci/**
132862306a36Sopenharmony_ci *  fm10k_mbx_process - Process mailbox interrupt
132962306a36Sopenharmony_ci *  @hw: pointer to hardware structure
133062306a36Sopenharmony_ci *  @mbx: pointer to mailbox
133162306a36Sopenharmony_ci *
133262306a36Sopenharmony_ci *  This function will process incoming mailbox events and generate mailbox
133362306a36Sopenharmony_ci *  replies.  It will return a value indicating the number of DWORDs
133462306a36Sopenharmony_ci *  transmitted excluding header on success or a negative value on error.
133562306a36Sopenharmony_ci **/
133662306a36Sopenharmony_cistatic s32 fm10k_mbx_process(struct fm10k_hw *hw,
133762306a36Sopenharmony_ci			     struct fm10k_mbx_info *mbx)
133862306a36Sopenharmony_ci{
133962306a36Sopenharmony_ci	s32 err;
134062306a36Sopenharmony_ci
134162306a36Sopenharmony_ci	/* we do not read mailbox if closed */
134262306a36Sopenharmony_ci	if (mbx->state == FM10K_STATE_CLOSED)
134362306a36Sopenharmony_ci		return 0;
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	/* copy data from mailbox */
134662306a36Sopenharmony_ci	err = fm10k_mbx_read(hw, mbx);
134762306a36Sopenharmony_ci	if (err)
134862306a36Sopenharmony_ci		return err;
134962306a36Sopenharmony_ci
135062306a36Sopenharmony_ci	/* validate type, source, and destination */
135162306a36Sopenharmony_ci	err = fm10k_mbx_validate_msg_hdr(mbx);
135262306a36Sopenharmony_ci	if (err < 0)
135362306a36Sopenharmony_ci		goto msg_err;
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, TYPE)) {
135662306a36Sopenharmony_ci	case FM10K_MSG_CONNECT:
135762306a36Sopenharmony_ci		err = fm10k_mbx_process_connect(hw, mbx);
135862306a36Sopenharmony_ci		break;
135962306a36Sopenharmony_ci	case FM10K_MSG_DATA:
136062306a36Sopenharmony_ci		err = fm10k_mbx_process_data(hw, mbx);
136162306a36Sopenharmony_ci		break;
136262306a36Sopenharmony_ci	case FM10K_MSG_DISCONNECT:
136362306a36Sopenharmony_ci		err = fm10k_mbx_process_disconnect(hw, mbx);
136462306a36Sopenharmony_ci		break;
136562306a36Sopenharmony_ci	case FM10K_MSG_ERROR:
136662306a36Sopenharmony_ci		err = fm10k_mbx_process_error(hw, mbx);
136762306a36Sopenharmony_ci		break;
136862306a36Sopenharmony_ci	default:
136962306a36Sopenharmony_ci		err = FM10K_MBX_ERR_TYPE;
137062306a36Sopenharmony_ci		break;
137162306a36Sopenharmony_ci	}
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_cimsg_err:
137462306a36Sopenharmony_ci	/* notify partner of errors on our end */
137562306a36Sopenharmony_ci	if (err < 0)
137662306a36Sopenharmony_ci		fm10k_mbx_create_error_msg(mbx, err);
137762306a36Sopenharmony_ci
137862306a36Sopenharmony_ci	/* copy data from mailbox */
137962306a36Sopenharmony_ci	fm10k_mbx_write(hw, mbx);
138062306a36Sopenharmony_ci
138162306a36Sopenharmony_ci	return err;
138262306a36Sopenharmony_ci}
138362306a36Sopenharmony_ci
138462306a36Sopenharmony_ci/**
138562306a36Sopenharmony_ci *  fm10k_mbx_disconnect - Shutdown mailbox connection
138662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
138762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
138862306a36Sopenharmony_ci *
138962306a36Sopenharmony_ci *  This function will shut down the mailbox.  It places the mailbox first
139062306a36Sopenharmony_ci *  in the disconnect state, it then allows up to a predefined timeout for
139162306a36Sopenharmony_ci *  the mailbox to transition to close on its own.  If this does not occur
139262306a36Sopenharmony_ci *  then the mailbox will be forced into the closed state.
139362306a36Sopenharmony_ci *
139462306a36Sopenharmony_ci *  Any mailbox transactions not completed before calling this function
139562306a36Sopenharmony_ci *  are not guaranteed to complete and may be dropped.
139662306a36Sopenharmony_ci **/
139762306a36Sopenharmony_cistatic void fm10k_mbx_disconnect(struct fm10k_hw *hw,
139862306a36Sopenharmony_ci				 struct fm10k_mbx_info *mbx)
139962306a36Sopenharmony_ci{
140062306a36Sopenharmony_ci	int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
140162306a36Sopenharmony_ci
140262306a36Sopenharmony_ci	/* Place mbx in ready to disconnect state */
140362306a36Sopenharmony_ci	mbx->state = FM10K_STATE_DISCONNECT;
140462306a36Sopenharmony_ci
140562306a36Sopenharmony_ci	/* trigger interrupt to start shutdown process */
140662306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
140762306a36Sopenharmony_ci					  FM10K_MBX_INTERRUPT_DISABLE);
140862306a36Sopenharmony_ci	do {
140962306a36Sopenharmony_ci		udelay(FM10K_MBX_POLL_DELAY);
141062306a36Sopenharmony_ci		mbx->ops.process(hw, mbx);
141162306a36Sopenharmony_ci		timeout -= FM10K_MBX_POLL_DELAY;
141262306a36Sopenharmony_ci	} while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
141362306a36Sopenharmony_ci
141462306a36Sopenharmony_ci	/* in case we didn't close, just force the mailbox into shutdown and
141562306a36Sopenharmony_ci	 * drop all left over messages in the FIFO.
141662306a36Sopenharmony_ci	 */
141762306a36Sopenharmony_ci	fm10k_mbx_connect_reset(mbx);
141862306a36Sopenharmony_ci	fm10k_fifo_drop_all(&mbx->tx);
141962306a36Sopenharmony_ci
142062306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbmem_reg, 0);
142162306a36Sopenharmony_ci}
142262306a36Sopenharmony_ci
142362306a36Sopenharmony_ci/**
142462306a36Sopenharmony_ci *  fm10k_mbx_connect - Start mailbox connection
142562306a36Sopenharmony_ci *  @hw: pointer to hardware structure
142662306a36Sopenharmony_ci *  @mbx: pointer to mailbox
142762306a36Sopenharmony_ci *
142862306a36Sopenharmony_ci *  This function will initiate a mailbox connection.  It will populate the
142962306a36Sopenharmony_ci *  mailbox with a broadcast connect message and then initialize the lock.
143062306a36Sopenharmony_ci *  This is safe since the connect message is a single DWORD so the mailbox
143162306a36Sopenharmony_ci *  transaction is guaranteed to be atomic.
143262306a36Sopenharmony_ci *
143362306a36Sopenharmony_ci *  This function will return an error if the mailbox has not been initiated
143462306a36Sopenharmony_ci *  or is currently in use.
143562306a36Sopenharmony_ci **/
143662306a36Sopenharmony_cistatic s32 fm10k_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
143762306a36Sopenharmony_ci{
143862306a36Sopenharmony_ci	/* we cannot connect an uninitialized mailbox */
143962306a36Sopenharmony_ci	if (!mbx->rx.buffer)
144062306a36Sopenharmony_ci		return FM10K_MBX_ERR_NO_SPACE;
144162306a36Sopenharmony_ci
144262306a36Sopenharmony_ci	/* we cannot connect an already connected mailbox */
144362306a36Sopenharmony_ci	if (mbx->state != FM10K_STATE_CLOSED)
144462306a36Sopenharmony_ci		return FM10K_MBX_ERR_BUSY;
144562306a36Sopenharmony_ci
144662306a36Sopenharmony_ci	/* mailbox timeout can now become active */
144762306a36Sopenharmony_ci	mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
144862306a36Sopenharmony_ci
144962306a36Sopenharmony_ci	/* Place mbx in ready to connect state */
145062306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CONNECT;
145162306a36Sopenharmony_ci
145262306a36Sopenharmony_ci	fm10k_mbx_reset_work(mbx);
145362306a36Sopenharmony_ci
145462306a36Sopenharmony_ci	/* initialize header of remote mailbox */
145562306a36Sopenharmony_ci	fm10k_mbx_create_fake_disconnect_hdr(mbx);
145662306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len, mbx->mbx_hdr);
145762306a36Sopenharmony_ci
145862306a36Sopenharmony_ci	/* enable interrupt and notify other party of new message */
145962306a36Sopenharmony_ci	mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
146062306a36Sopenharmony_ci			FM10K_MBX_INTERRUPT_ENABLE;
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ci	/* generate and load connect header into mailbox */
146362306a36Sopenharmony_ci	fm10k_mbx_create_connect_hdr(mbx);
146462306a36Sopenharmony_ci	fm10k_mbx_write(hw, mbx);
146562306a36Sopenharmony_ci
146662306a36Sopenharmony_ci	return 0;
146762306a36Sopenharmony_ci}
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci/**
147062306a36Sopenharmony_ci *  fm10k_mbx_validate_handlers - Validate layout of message parsing data
147162306a36Sopenharmony_ci *  @msg_data: handlers for mailbox events
147262306a36Sopenharmony_ci *
147362306a36Sopenharmony_ci *  This function validates the layout of the message parsing data.  This
147462306a36Sopenharmony_ci *  should be mostly static, but it is important to catch any errors that
147562306a36Sopenharmony_ci *  are made when constructing the parsers.
147662306a36Sopenharmony_ci **/
147762306a36Sopenharmony_cistatic s32 fm10k_mbx_validate_handlers(const struct fm10k_msg_data *msg_data)
147862306a36Sopenharmony_ci{
147962306a36Sopenharmony_ci	const struct fm10k_tlv_attr *attr;
148062306a36Sopenharmony_ci	unsigned int id;
148162306a36Sopenharmony_ci
148262306a36Sopenharmony_ci	/* Allow NULL mailboxes that transmit but don't receive */
148362306a36Sopenharmony_ci	if (!msg_data)
148462306a36Sopenharmony_ci		return 0;
148562306a36Sopenharmony_ci
148662306a36Sopenharmony_ci	while (msg_data->id != FM10K_TLV_ERROR) {
148762306a36Sopenharmony_ci		/* all messages should have a function handler */
148862306a36Sopenharmony_ci		if (!msg_data->func)
148962306a36Sopenharmony_ci			return FM10K_ERR_PARAM;
149062306a36Sopenharmony_ci
149162306a36Sopenharmony_ci		/* parser is optional */
149262306a36Sopenharmony_ci		attr = msg_data->attr;
149362306a36Sopenharmony_ci		if (attr) {
149462306a36Sopenharmony_ci			while (attr->id != FM10K_TLV_ERROR) {
149562306a36Sopenharmony_ci				id = attr->id;
149662306a36Sopenharmony_ci				attr++;
149762306a36Sopenharmony_ci				/* ID should always be increasing */
149862306a36Sopenharmony_ci				if (id >= attr->id)
149962306a36Sopenharmony_ci					return FM10K_ERR_PARAM;
150062306a36Sopenharmony_ci				/* ID should fit in results array */
150162306a36Sopenharmony_ci				if (id >= FM10K_TLV_RESULTS_MAX)
150262306a36Sopenharmony_ci					return FM10K_ERR_PARAM;
150362306a36Sopenharmony_ci			}
150462306a36Sopenharmony_ci
150562306a36Sopenharmony_ci			/* verify terminator is in the list */
150662306a36Sopenharmony_ci			if (attr->id != FM10K_TLV_ERROR)
150762306a36Sopenharmony_ci				return FM10K_ERR_PARAM;
150862306a36Sopenharmony_ci		}
150962306a36Sopenharmony_ci
151062306a36Sopenharmony_ci		id = msg_data->id;
151162306a36Sopenharmony_ci		msg_data++;
151262306a36Sopenharmony_ci		/* ID should always be increasing */
151362306a36Sopenharmony_ci		if (id >= msg_data->id)
151462306a36Sopenharmony_ci			return FM10K_ERR_PARAM;
151562306a36Sopenharmony_ci	}
151662306a36Sopenharmony_ci
151762306a36Sopenharmony_ci	/* verify terminator is in the list */
151862306a36Sopenharmony_ci	if ((msg_data->id != FM10K_TLV_ERROR) || !msg_data->func)
151962306a36Sopenharmony_ci		return FM10K_ERR_PARAM;
152062306a36Sopenharmony_ci
152162306a36Sopenharmony_ci	return 0;
152262306a36Sopenharmony_ci}
152362306a36Sopenharmony_ci
152462306a36Sopenharmony_ci/**
152562306a36Sopenharmony_ci *  fm10k_mbx_register_handlers - Register a set of handler ops for mailbox
152662306a36Sopenharmony_ci *  @mbx: pointer to mailbox
152762306a36Sopenharmony_ci *  @msg_data: handlers for mailbox events
152862306a36Sopenharmony_ci *
152962306a36Sopenharmony_ci *  This function associates a set of message handling ops with a mailbox.
153062306a36Sopenharmony_ci **/
153162306a36Sopenharmony_cistatic s32 fm10k_mbx_register_handlers(struct fm10k_mbx_info *mbx,
153262306a36Sopenharmony_ci				       const struct fm10k_msg_data *msg_data)
153362306a36Sopenharmony_ci{
153462306a36Sopenharmony_ci	/* validate layout of handlers before assigning them */
153562306a36Sopenharmony_ci	if (fm10k_mbx_validate_handlers(msg_data))
153662306a36Sopenharmony_ci		return FM10K_ERR_PARAM;
153762306a36Sopenharmony_ci
153862306a36Sopenharmony_ci	/* initialize the message handlers */
153962306a36Sopenharmony_ci	mbx->msg_data = msg_data;
154062306a36Sopenharmony_ci
154162306a36Sopenharmony_ci	return 0;
154262306a36Sopenharmony_ci}
154362306a36Sopenharmony_ci
154462306a36Sopenharmony_ci/**
154562306a36Sopenharmony_ci *  fm10k_pfvf_mbx_init - Initialize mailbox memory for PF/VF mailbox
154662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
154762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
154862306a36Sopenharmony_ci *  @msg_data: handlers for mailbox events
154962306a36Sopenharmony_ci *  @id: ID reference for PF as it supports up to 64 PF/VF mailboxes
155062306a36Sopenharmony_ci *
155162306a36Sopenharmony_ci *  This function initializes the mailbox for use.  It will split the
155262306a36Sopenharmony_ci *  buffer provided and use that to populate both the Tx and Rx FIFO by
155362306a36Sopenharmony_ci *  evenly splitting it.  In order to allow for easy masking of head/tail
155462306a36Sopenharmony_ci *  the value reported in size must be a power of 2 and is reported in
155562306a36Sopenharmony_ci *  DWORDs, not bytes.  Any invalid values will cause the mailbox to return
155662306a36Sopenharmony_ci *  error.
155762306a36Sopenharmony_ci **/
155862306a36Sopenharmony_cis32 fm10k_pfvf_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
155962306a36Sopenharmony_ci			const struct fm10k_msg_data *msg_data, u8 id)
156062306a36Sopenharmony_ci{
156162306a36Sopenharmony_ci	/* initialize registers */
156262306a36Sopenharmony_ci	switch (hw->mac.type) {
156362306a36Sopenharmony_ci	case fm10k_mac_vf:
156462306a36Sopenharmony_ci		mbx->mbx_reg = FM10K_VFMBX;
156562306a36Sopenharmony_ci		mbx->mbmem_reg = FM10K_VFMBMEM(FM10K_VFMBMEM_VF_XOR);
156662306a36Sopenharmony_ci		break;
156762306a36Sopenharmony_ci	case fm10k_mac_pf:
156862306a36Sopenharmony_ci		/* there are only 64 VF <-> PF mailboxes */
156962306a36Sopenharmony_ci		if (id < 64) {
157062306a36Sopenharmony_ci			mbx->mbx_reg = FM10K_MBX(id);
157162306a36Sopenharmony_ci			mbx->mbmem_reg = FM10K_MBMEM_VF(id, 0);
157262306a36Sopenharmony_ci			break;
157362306a36Sopenharmony_ci		}
157462306a36Sopenharmony_ci		fallthrough;
157562306a36Sopenharmony_ci	default:
157662306a36Sopenharmony_ci		return FM10K_MBX_ERR_NO_MBX;
157762306a36Sopenharmony_ci	}
157862306a36Sopenharmony_ci
157962306a36Sopenharmony_ci	/* start out in closed state */
158062306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CLOSED;
158162306a36Sopenharmony_ci
158262306a36Sopenharmony_ci	/* validate layout of handlers before assigning them */
158362306a36Sopenharmony_ci	if (fm10k_mbx_validate_handlers(msg_data))
158462306a36Sopenharmony_ci		return FM10K_ERR_PARAM;
158562306a36Sopenharmony_ci
158662306a36Sopenharmony_ci	/* initialize the message handlers */
158762306a36Sopenharmony_ci	mbx->msg_data = msg_data;
158862306a36Sopenharmony_ci
158962306a36Sopenharmony_ci	/* start mailbox as timed out and let the reset_hw call
159062306a36Sopenharmony_ci	 * set the timeout value to begin communications
159162306a36Sopenharmony_ci	 */
159262306a36Sopenharmony_ci	mbx->timeout = 0;
159362306a36Sopenharmony_ci	mbx->udelay = FM10K_MBX_INIT_DELAY;
159462306a36Sopenharmony_ci
159562306a36Sopenharmony_ci	/* initialize tail and head */
159662306a36Sopenharmony_ci	mbx->tail = 1;
159762306a36Sopenharmony_ci	mbx->head = 1;
159862306a36Sopenharmony_ci
159962306a36Sopenharmony_ci	/* initialize CRC seeds */
160062306a36Sopenharmony_ci	mbx->local = FM10K_MBX_CRC_SEED;
160162306a36Sopenharmony_ci	mbx->remote = FM10K_MBX_CRC_SEED;
160262306a36Sopenharmony_ci
160362306a36Sopenharmony_ci	/* Split buffer for use by Tx/Rx FIFOs */
160462306a36Sopenharmony_ci	mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
160562306a36Sopenharmony_ci	mbx->mbmem_len = FM10K_VFMBMEM_VF_XOR;
160662306a36Sopenharmony_ci
160762306a36Sopenharmony_ci	/* initialize the FIFOs, sizes are in 4 byte increments */
160862306a36Sopenharmony_ci	fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
160962306a36Sopenharmony_ci	fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
161062306a36Sopenharmony_ci			FM10K_MBX_RX_BUFFER_SIZE);
161162306a36Sopenharmony_ci
161262306a36Sopenharmony_ci	/* initialize function pointers */
161362306a36Sopenharmony_ci	mbx->ops.connect = fm10k_mbx_connect;
161462306a36Sopenharmony_ci	mbx->ops.disconnect = fm10k_mbx_disconnect;
161562306a36Sopenharmony_ci	mbx->ops.rx_ready = fm10k_mbx_rx_ready;
161662306a36Sopenharmony_ci	mbx->ops.tx_ready = fm10k_mbx_tx_ready;
161762306a36Sopenharmony_ci	mbx->ops.tx_complete = fm10k_mbx_tx_complete;
161862306a36Sopenharmony_ci	mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
161962306a36Sopenharmony_ci	mbx->ops.process = fm10k_mbx_process;
162062306a36Sopenharmony_ci	mbx->ops.register_handlers = fm10k_mbx_register_handlers;
162162306a36Sopenharmony_ci
162262306a36Sopenharmony_ci	return 0;
162362306a36Sopenharmony_ci}
162462306a36Sopenharmony_ci
162562306a36Sopenharmony_ci/**
162662306a36Sopenharmony_ci *  fm10k_sm_mbx_create_data_hdr - Generate a mailbox header for local FIFO
162762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
162862306a36Sopenharmony_ci *
162962306a36Sopenharmony_ci *  This function returns a data mailbox header
163062306a36Sopenharmony_ci **/
163162306a36Sopenharmony_cistatic void fm10k_sm_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
163262306a36Sopenharmony_ci{
163362306a36Sopenharmony_ci	if (mbx->tail_len)
163462306a36Sopenharmony_ci		mbx->mbx_lock |= FM10K_MBX_REQ;
163562306a36Sopenharmony_ci
163662306a36Sopenharmony_ci	mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
163762306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
163862306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD);
163962306a36Sopenharmony_ci}
164062306a36Sopenharmony_ci
164162306a36Sopenharmony_ci/**
164262306a36Sopenharmony_ci *  fm10k_sm_mbx_create_connect_hdr - Generate a mailbox header for local FIFO
164362306a36Sopenharmony_ci *  @mbx: pointer to mailbox
164462306a36Sopenharmony_ci *  @err: error flags to report if any
164562306a36Sopenharmony_ci *
164662306a36Sopenharmony_ci *  This function returns a connection mailbox header
164762306a36Sopenharmony_ci **/
164862306a36Sopenharmony_cistatic void fm10k_sm_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx, u8 err)
164962306a36Sopenharmony_ci{
165062306a36Sopenharmony_ci	if (mbx->local)
165162306a36Sopenharmony_ci		mbx->mbx_lock |= FM10K_MBX_REQ;
165262306a36Sopenharmony_ci
165362306a36Sopenharmony_ci	mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
165462306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
165562306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD) |
165662306a36Sopenharmony_ci		       FM10K_MSG_HDR_FIELD_SET(err, SM_ERR);
165762306a36Sopenharmony_ci}
165862306a36Sopenharmony_ci
165962306a36Sopenharmony_ci/**
166062306a36Sopenharmony_ci *  fm10k_sm_mbx_connect_reset - Reset following request for reset
166162306a36Sopenharmony_ci *  @mbx: pointer to mailbox
166262306a36Sopenharmony_ci *
166362306a36Sopenharmony_ci *  This function resets the mailbox to a just connected state
166462306a36Sopenharmony_ci **/
166562306a36Sopenharmony_cistatic void fm10k_sm_mbx_connect_reset(struct fm10k_mbx_info *mbx)
166662306a36Sopenharmony_ci{
166762306a36Sopenharmony_ci	/* flush any uncompleted work */
166862306a36Sopenharmony_ci	fm10k_mbx_reset_work(mbx);
166962306a36Sopenharmony_ci
167062306a36Sopenharmony_ci	/* set local version to max and remote version to 0 */
167162306a36Sopenharmony_ci	mbx->local = FM10K_SM_MBX_VERSION;
167262306a36Sopenharmony_ci	mbx->remote = 0;
167362306a36Sopenharmony_ci
167462306a36Sopenharmony_ci	/* initialize tail and head */
167562306a36Sopenharmony_ci	mbx->tail = 1;
167662306a36Sopenharmony_ci	mbx->head = 1;
167762306a36Sopenharmony_ci
167862306a36Sopenharmony_ci	/* reset state back to connect */
167962306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CONNECT;
168062306a36Sopenharmony_ci}
168162306a36Sopenharmony_ci
168262306a36Sopenharmony_ci/**
168362306a36Sopenharmony_ci *  fm10k_sm_mbx_connect - Start switch manager mailbox connection
168462306a36Sopenharmony_ci *  @hw: pointer to hardware structure
168562306a36Sopenharmony_ci *  @mbx: pointer to mailbox
168662306a36Sopenharmony_ci *
168762306a36Sopenharmony_ci *  This function will initiate a mailbox connection with the switch
168862306a36Sopenharmony_ci *  manager.  To do this it will first disconnect the mailbox, and then
168962306a36Sopenharmony_ci *  reconnect it in order to complete a reset of the mailbox.
169062306a36Sopenharmony_ci *
169162306a36Sopenharmony_ci *  This function will return an error if the mailbox has not been initiated
169262306a36Sopenharmony_ci *  or is currently in use.
169362306a36Sopenharmony_ci **/
169462306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
169562306a36Sopenharmony_ci{
169662306a36Sopenharmony_ci	/* we cannot connect an uninitialized mailbox */
169762306a36Sopenharmony_ci	if (!mbx->rx.buffer)
169862306a36Sopenharmony_ci		return FM10K_MBX_ERR_NO_SPACE;
169962306a36Sopenharmony_ci
170062306a36Sopenharmony_ci	/* we cannot connect an already connected mailbox */
170162306a36Sopenharmony_ci	if (mbx->state != FM10K_STATE_CLOSED)
170262306a36Sopenharmony_ci		return FM10K_MBX_ERR_BUSY;
170362306a36Sopenharmony_ci
170462306a36Sopenharmony_ci	/* mailbox timeout can now become active */
170562306a36Sopenharmony_ci	mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
170662306a36Sopenharmony_ci
170762306a36Sopenharmony_ci	/* Place mbx in ready to connect state */
170862306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CONNECT;
170962306a36Sopenharmony_ci	mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
171062306a36Sopenharmony_ci
171162306a36Sopenharmony_ci	/* reset interface back to connect */
171262306a36Sopenharmony_ci	fm10k_sm_mbx_connect_reset(mbx);
171362306a36Sopenharmony_ci
171462306a36Sopenharmony_ci	/* enable interrupt and notify other party of new message */
171562306a36Sopenharmony_ci	mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
171662306a36Sopenharmony_ci			FM10K_MBX_INTERRUPT_ENABLE;
171762306a36Sopenharmony_ci
171862306a36Sopenharmony_ci	/* generate and load connect header into mailbox */
171962306a36Sopenharmony_ci	fm10k_sm_mbx_create_connect_hdr(mbx, 0);
172062306a36Sopenharmony_ci	fm10k_mbx_write(hw, mbx);
172162306a36Sopenharmony_ci
172262306a36Sopenharmony_ci	return 0;
172362306a36Sopenharmony_ci}
172462306a36Sopenharmony_ci
172562306a36Sopenharmony_ci/**
172662306a36Sopenharmony_ci *  fm10k_sm_mbx_disconnect - Shutdown mailbox connection
172762306a36Sopenharmony_ci *  @hw: pointer to hardware structure
172862306a36Sopenharmony_ci *  @mbx: pointer to mailbox
172962306a36Sopenharmony_ci *
173062306a36Sopenharmony_ci *  This function will shut down the mailbox.  It places the mailbox first
173162306a36Sopenharmony_ci *  in the disconnect state, it then allows up to a predefined timeout for
173262306a36Sopenharmony_ci *  the mailbox to transition to close on its own.  If this does not occur
173362306a36Sopenharmony_ci *  then the mailbox will be forced into the closed state.
173462306a36Sopenharmony_ci *
173562306a36Sopenharmony_ci *  Any mailbox transactions not completed before calling this function
173662306a36Sopenharmony_ci *  are not guaranteed to complete and may be dropped.
173762306a36Sopenharmony_ci **/
173862306a36Sopenharmony_cistatic void fm10k_sm_mbx_disconnect(struct fm10k_hw *hw,
173962306a36Sopenharmony_ci				    struct fm10k_mbx_info *mbx)
174062306a36Sopenharmony_ci{
174162306a36Sopenharmony_ci	int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
174262306a36Sopenharmony_ci
174362306a36Sopenharmony_ci	/* Place mbx in ready to disconnect state */
174462306a36Sopenharmony_ci	mbx->state = FM10K_STATE_DISCONNECT;
174562306a36Sopenharmony_ci
174662306a36Sopenharmony_ci	/* trigger interrupt to start shutdown process */
174762306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
174862306a36Sopenharmony_ci					  FM10K_MBX_INTERRUPT_DISABLE);
174962306a36Sopenharmony_ci	do {
175062306a36Sopenharmony_ci		udelay(FM10K_MBX_POLL_DELAY);
175162306a36Sopenharmony_ci		mbx->ops.process(hw, mbx);
175262306a36Sopenharmony_ci		timeout -= FM10K_MBX_POLL_DELAY;
175362306a36Sopenharmony_ci	} while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
175462306a36Sopenharmony_ci
175562306a36Sopenharmony_ci	/* in case we didn't close just force the mailbox into shutdown */
175662306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CLOSED;
175762306a36Sopenharmony_ci	mbx->remote = 0;
175862306a36Sopenharmony_ci	fm10k_mbx_reset_work(mbx);
175962306a36Sopenharmony_ci	fm10k_fifo_drop_all(&mbx->tx);
176062306a36Sopenharmony_ci
176162306a36Sopenharmony_ci	fm10k_write_reg(hw, mbx->mbmem_reg, 0);
176262306a36Sopenharmony_ci}
176362306a36Sopenharmony_ci
176462306a36Sopenharmony_ci/**
176562306a36Sopenharmony_ci *  fm10k_sm_mbx_validate_fifo_hdr - Validate fields in the remote FIFO header
176662306a36Sopenharmony_ci *  @mbx: pointer to mailbox
176762306a36Sopenharmony_ci *
176862306a36Sopenharmony_ci *  This function will parse up the fields in the mailbox header and return
176962306a36Sopenharmony_ci *  an error if the header contains any of a number of invalid configurations
177062306a36Sopenharmony_ci *  including unrecognized offsets or version numbers.
177162306a36Sopenharmony_ci **/
177262306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_validate_fifo_hdr(struct fm10k_mbx_info *mbx)
177362306a36Sopenharmony_ci{
177462306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
177562306a36Sopenharmony_ci	u16 tail, head, ver;
177662306a36Sopenharmony_ci
177762306a36Sopenharmony_ci	tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
177862306a36Sopenharmony_ci	ver = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_VER);
177962306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
178062306a36Sopenharmony_ci
178162306a36Sopenharmony_ci	switch (ver) {
178262306a36Sopenharmony_ci	case 0:
178362306a36Sopenharmony_ci		break;
178462306a36Sopenharmony_ci	case FM10K_SM_MBX_VERSION:
178562306a36Sopenharmony_ci		if (!head || head > FM10K_SM_MBX_FIFO_LEN)
178662306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
178762306a36Sopenharmony_ci		if (!tail || tail > FM10K_SM_MBX_FIFO_LEN)
178862306a36Sopenharmony_ci			return FM10K_MBX_ERR_TAIL;
178962306a36Sopenharmony_ci		if (mbx->tail < head)
179062306a36Sopenharmony_ci			head += mbx->mbmem_len - 1;
179162306a36Sopenharmony_ci		if (tail < mbx->head)
179262306a36Sopenharmony_ci			tail += mbx->mbmem_len - 1;
179362306a36Sopenharmony_ci		if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
179462306a36Sopenharmony_ci			return FM10K_MBX_ERR_HEAD;
179562306a36Sopenharmony_ci		if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
179662306a36Sopenharmony_ci			break;
179762306a36Sopenharmony_ci		return FM10K_MBX_ERR_TAIL;
179862306a36Sopenharmony_ci	default:
179962306a36Sopenharmony_ci		return FM10K_MBX_ERR_SRC;
180062306a36Sopenharmony_ci	}
180162306a36Sopenharmony_ci
180262306a36Sopenharmony_ci	return 0;
180362306a36Sopenharmony_ci}
180462306a36Sopenharmony_ci
180562306a36Sopenharmony_ci/**
180662306a36Sopenharmony_ci *  fm10k_sm_mbx_process_error - Process header with error flag set
180762306a36Sopenharmony_ci *  @mbx: pointer to mailbox
180862306a36Sopenharmony_ci *
180962306a36Sopenharmony_ci *  This function is meant to respond to a request where the error flag
181062306a36Sopenharmony_ci *  is set.  As a result we will terminate a connection if one is present
181162306a36Sopenharmony_ci *  and fall back into the reset state with a connection header of version
181262306a36Sopenharmony_ci *  0 (RESET).
181362306a36Sopenharmony_ci **/
181462306a36Sopenharmony_cistatic void fm10k_sm_mbx_process_error(struct fm10k_mbx_info *mbx)
181562306a36Sopenharmony_ci{
181662306a36Sopenharmony_ci	const enum fm10k_mbx_state state = mbx->state;
181762306a36Sopenharmony_ci
181862306a36Sopenharmony_ci	switch (state) {
181962306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
182062306a36Sopenharmony_ci		/* if there is an error just disconnect */
182162306a36Sopenharmony_ci		mbx->remote = 0;
182262306a36Sopenharmony_ci		break;
182362306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
182462306a36Sopenharmony_ci		/* flush any uncompleted work */
182562306a36Sopenharmony_ci		fm10k_sm_mbx_connect_reset(mbx);
182662306a36Sopenharmony_ci		break;
182762306a36Sopenharmony_ci	case FM10K_STATE_CONNECT:
182862306a36Sopenharmony_ci		/* try connecting at lower version */
182962306a36Sopenharmony_ci		if (mbx->remote) {
183062306a36Sopenharmony_ci			while (mbx->local > 1)
183162306a36Sopenharmony_ci				mbx->local--;
183262306a36Sopenharmony_ci			mbx->remote = 0;
183362306a36Sopenharmony_ci		}
183462306a36Sopenharmony_ci		break;
183562306a36Sopenharmony_ci	default:
183662306a36Sopenharmony_ci		break;
183762306a36Sopenharmony_ci	}
183862306a36Sopenharmony_ci
183962306a36Sopenharmony_ci	fm10k_sm_mbx_create_connect_hdr(mbx, 0);
184062306a36Sopenharmony_ci}
184162306a36Sopenharmony_ci
184262306a36Sopenharmony_ci/**
184362306a36Sopenharmony_ci *  fm10k_sm_mbx_create_error_msg - Process an error in FIFO header
184462306a36Sopenharmony_ci *  @mbx: pointer to mailbox
184562306a36Sopenharmony_ci *  @err: local error encountered
184662306a36Sopenharmony_ci *
184762306a36Sopenharmony_ci *  This function will interpret the error provided by err, and based on
184862306a36Sopenharmony_ci *  that it may set the error bit in the local message header
184962306a36Sopenharmony_ci **/
185062306a36Sopenharmony_cistatic void fm10k_sm_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
185162306a36Sopenharmony_ci{
185262306a36Sopenharmony_ci	/* only generate an error message for these types */
185362306a36Sopenharmony_ci	switch (err) {
185462306a36Sopenharmony_ci	case FM10K_MBX_ERR_TAIL:
185562306a36Sopenharmony_ci	case FM10K_MBX_ERR_HEAD:
185662306a36Sopenharmony_ci	case FM10K_MBX_ERR_SRC:
185762306a36Sopenharmony_ci	case FM10K_MBX_ERR_SIZE:
185862306a36Sopenharmony_ci	case FM10K_MBX_ERR_RSVD0:
185962306a36Sopenharmony_ci		break;
186062306a36Sopenharmony_ci	default:
186162306a36Sopenharmony_ci		return;
186262306a36Sopenharmony_ci	}
186362306a36Sopenharmony_ci
186462306a36Sopenharmony_ci	/* process it as though we received an error, and send error reply */
186562306a36Sopenharmony_ci	fm10k_sm_mbx_process_error(mbx);
186662306a36Sopenharmony_ci	fm10k_sm_mbx_create_connect_hdr(mbx, 1);
186762306a36Sopenharmony_ci}
186862306a36Sopenharmony_ci
186962306a36Sopenharmony_ci/**
187062306a36Sopenharmony_ci *  fm10k_sm_mbx_receive - Take message from Rx mailbox FIFO and put it in Rx
187162306a36Sopenharmony_ci *  @hw: pointer to hardware structure
187262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
187362306a36Sopenharmony_ci *  @tail: tail index of message
187462306a36Sopenharmony_ci *
187562306a36Sopenharmony_ci *  This function will dequeue one message from the Rx switch manager mailbox
187662306a36Sopenharmony_ci *  FIFO and place it in the Rx mailbox FIFO for processing by software.
187762306a36Sopenharmony_ci **/
187862306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_receive(struct fm10k_hw *hw,
187962306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx,
188062306a36Sopenharmony_ci				u16 tail)
188162306a36Sopenharmony_ci{
188262306a36Sopenharmony_ci	/* reduce length by 1 to convert to a mask */
188362306a36Sopenharmony_ci	u16 mbmem_len = mbx->mbmem_len - 1;
188462306a36Sopenharmony_ci	s32 err;
188562306a36Sopenharmony_ci
188662306a36Sopenharmony_ci	/* push tail in front of head */
188762306a36Sopenharmony_ci	if (tail < mbx->head)
188862306a36Sopenharmony_ci		tail += mbmem_len;
188962306a36Sopenharmony_ci
189062306a36Sopenharmony_ci	/* copy data to the Rx FIFO */
189162306a36Sopenharmony_ci	err = fm10k_mbx_push_tail(hw, mbx, tail);
189262306a36Sopenharmony_ci	if (err < 0)
189362306a36Sopenharmony_ci		return err;
189462306a36Sopenharmony_ci
189562306a36Sopenharmony_ci	/* process messages if we have received any */
189662306a36Sopenharmony_ci	fm10k_mbx_dequeue_rx(hw, mbx);
189762306a36Sopenharmony_ci
189862306a36Sopenharmony_ci	/* guarantee head aligns with the end of the last message */
189962306a36Sopenharmony_ci	mbx->head = fm10k_mbx_head_sub(mbx, mbx->pushed);
190062306a36Sopenharmony_ci	mbx->pushed = 0;
190162306a36Sopenharmony_ci
190262306a36Sopenharmony_ci	/* clear any extra bits left over since index adds 1 extra bit */
190362306a36Sopenharmony_ci	if (mbx->head > mbmem_len)
190462306a36Sopenharmony_ci		mbx->head -= mbmem_len;
190562306a36Sopenharmony_ci
190662306a36Sopenharmony_ci	return err;
190762306a36Sopenharmony_ci}
190862306a36Sopenharmony_ci
190962306a36Sopenharmony_ci/**
191062306a36Sopenharmony_ci *  fm10k_sm_mbx_transmit - Take message from Tx and put it in Tx mailbox FIFO
191162306a36Sopenharmony_ci *  @hw: pointer to hardware structure
191262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
191362306a36Sopenharmony_ci *  @head: head index of message
191462306a36Sopenharmony_ci *
191562306a36Sopenharmony_ci *  This function will dequeue one message from the Tx mailbox FIFO and place
191662306a36Sopenharmony_ci *  it in the Tx switch manager mailbox FIFO for processing by hardware.
191762306a36Sopenharmony_ci **/
191862306a36Sopenharmony_cistatic void fm10k_sm_mbx_transmit(struct fm10k_hw *hw,
191962306a36Sopenharmony_ci				  struct fm10k_mbx_info *mbx, u16 head)
192062306a36Sopenharmony_ci{
192162306a36Sopenharmony_ci	struct fm10k_mbx_fifo *fifo = &mbx->tx;
192262306a36Sopenharmony_ci	/* reduce length by 1 to convert to a mask */
192362306a36Sopenharmony_ci	u16 mbmem_len = mbx->mbmem_len - 1;
192462306a36Sopenharmony_ci	u16 tail_len, len = 0;
192562306a36Sopenharmony_ci
192662306a36Sopenharmony_ci	/* push head behind tail */
192762306a36Sopenharmony_ci	if (mbx->tail < head)
192862306a36Sopenharmony_ci		head += mbmem_len;
192962306a36Sopenharmony_ci
193062306a36Sopenharmony_ci	fm10k_mbx_pull_head(hw, mbx, head);
193162306a36Sopenharmony_ci
193262306a36Sopenharmony_ci	/* determine msg aligned offset for end of buffer */
193362306a36Sopenharmony_ci	do {
193462306a36Sopenharmony_ci		u32 *msg;
193562306a36Sopenharmony_ci
193662306a36Sopenharmony_ci		msg = fifo->buffer + fm10k_fifo_head_offset(fifo, len);
193762306a36Sopenharmony_ci		tail_len = len;
193862306a36Sopenharmony_ci		len += FM10K_TLV_DWORD_LEN(*msg);
193962306a36Sopenharmony_ci	} while ((len <= mbx->tail_len) && (len < mbmem_len));
194062306a36Sopenharmony_ci
194162306a36Sopenharmony_ci	/* guarantee we stop on a message boundary */
194262306a36Sopenharmony_ci	if (mbx->tail_len > tail_len) {
194362306a36Sopenharmony_ci		mbx->tail = fm10k_mbx_tail_sub(mbx, mbx->tail_len - tail_len);
194462306a36Sopenharmony_ci		mbx->tail_len = tail_len;
194562306a36Sopenharmony_ci	}
194662306a36Sopenharmony_ci
194762306a36Sopenharmony_ci	/* clear any extra bits left over since index adds 1 extra bit */
194862306a36Sopenharmony_ci	if (mbx->tail > mbmem_len)
194962306a36Sopenharmony_ci		mbx->tail -= mbmem_len;
195062306a36Sopenharmony_ci}
195162306a36Sopenharmony_ci
195262306a36Sopenharmony_ci/**
195362306a36Sopenharmony_ci *  fm10k_sm_mbx_create_reply - Generate reply based on state and remote head
195462306a36Sopenharmony_ci *  @hw: pointer to hardware structure
195562306a36Sopenharmony_ci *  @mbx: pointer to mailbox
195662306a36Sopenharmony_ci *  @head: acknowledgement number
195762306a36Sopenharmony_ci *
195862306a36Sopenharmony_ci *  This function will generate an outgoing message based on the current
195962306a36Sopenharmony_ci *  mailbox state and the remote FIFO head.  It will return the length
196062306a36Sopenharmony_ci *  of the outgoing message excluding header on success, and a negative value
196162306a36Sopenharmony_ci *  on error.
196262306a36Sopenharmony_ci **/
196362306a36Sopenharmony_cistatic void fm10k_sm_mbx_create_reply(struct fm10k_hw *hw,
196462306a36Sopenharmony_ci				      struct fm10k_mbx_info *mbx, u16 head)
196562306a36Sopenharmony_ci{
196662306a36Sopenharmony_ci	switch (mbx->state) {
196762306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
196862306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
196962306a36Sopenharmony_ci		/* flush out Tx data */
197062306a36Sopenharmony_ci		fm10k_sm_mbx_transmit(hw, mbx, head);
197162306a36Sopenharmony_ci
197262306a36Sopenharmony_ci		/* generate new header based on data */
197362306a36Sopenharmony_ci		if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN)) {
197462306a36Sopenharmony_ci			fm10k_sm_mbx_create_data_hdr(mbx);
197562306a36Sopenharmony_ci		} else {
197662306a36Sopenharmony_ci			mbx->remote = 0;
197762306a36Sopenharmony_ci			fm10k_sm_mbx_create_connect_hdr(mbx, 0);
197862306a36Sopenharmony_ci		}
197962306a36Sopenharmony_ci		break;
198062306a36Sopenharmony_ci	case FM10K_STATE_CONNECT:
198162306a36Sopenharmony_ci	case FM10K_STATE_CLOSED:
198262306a36Sopenharmony_ci		fm10k_sm_mbx_create_connect_hdr(mbx, 0);
198362306a36Sopenharmony_ci		break;
198462306a36Sopenharmony_ci	default:
198562306a36Sopenharmony_ci		break;
198662306a36Sopenharmony_ci	}
198762306a36Sopenharmony_ci}
198862306a36Sopenharmony_ci
198962306a36Sopenharmony_ci/**
199062306a36Sopenharmony_ci *  fm10k_sm_mbx_process_reset - Process header with version == 0 (RESET)
199162306a36Sopenharmony_ci *  @hw: pointer to hardware structure
199262306a36Sopenharmony_ci *  @mbx: pointer to mailbox
199362306a36Sopenharmony_ci *
199462306a36Sopenharmony_ci *  This function is meant to respond to a request where the version data
199562306a36Sopenharmony_ci *  is set to 0.  As such we will either terminate the connection or go
199662306a36Sopenharmony_ci *  into the connect state in order to re-establish the connection.  This
199762306a36Sopenharmony_ci *  function can also be used to respond to an error as the connection
199862306a36Sopenharmony_ci *  resetting would also be a means of dealing with errors.
199962306a36Sopenharmony_ci **/
200062306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_process_reset(struct fm10k_hw *hw,
200162306a36Sopenharmony_ci				      struct fm10k_mbx_info *mbx)
200262306a36Sopenharmony_ci{
200362306a36Sopenharmony_ci	s32 err = 0;
200462306a36Sopenharmony_ci	const enum fm10k_mbx_state state = mbx->state;
200562306a36Sopenharmony_ci
200662306a36Sopenharmony_ci	switch (state) {
200762306a36Sopenharmony_ci	case FM10K_STATE_DISCONNECT:
200862306a36Sopenharmony_ci		/* drop remote connections and disconnect */
200962306a36Sopenharmony_ci		mbx->state = FM10K_STATE_CLOSED;
201062306a36Sopenharmony_ci		mbx->remote = 0;
201162306a36Sopenharmony_ci		mbx->local = 0;
201262306a36Sopenharmony_ci		break;
201362306a36Sopenharmony_ci	case FM10K_STATE_OPEN:
201462306a36Sopenharmony_ci		/* flush any incomplete work */
201562306a36Sopenharmony_ci		fm10k_sm_mbx_connect_reset(mbx);
201662306a36Sopenharmony_ci		err = FM10K_ERR_RESET_REQUESTED;
201762306a36Sopenharmony_ci		break;
201862306a36Sopenharmony_ci	case FM10K_STATE_CONNECT:
201962306a36Sopenharmony_ci		/* Update remote value to match local value */
202062306a36Sopenharmony_ci		mbx->remote = mbx->local;
202162306a36Sopenharmony_ci		break;
202262306a36Sopenharmony_ci	default:
202362306a36Sopenharmony_ci		break;
202462306a36Sopenharmony_ci	}
202562306a36Sopenharmony_ci
202662306a36Sopenharmony_ci	fm10k_sm_mbx_create_reply(hw, mbx, mbx->tail);
202762306a36Sopenharmony_ci
202862306a36Sopenharmony_ci	return err;
202962306a36Sopenharmony_ci}
203062306a36Sopenharmony_ci
203162306a36Sopenharmony_ci/**
203262306a36Sopenharmony_ci *  fm10k_sm_mbx_process_version_1 - Process header with version == 1
203362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
203462306a36Sopenharmony_ci *  @mbx: pointer to mailbox
203562306a36Sopenharmony_ci *
203662306a36Sopenharmony_ci *  This function is meant to process messages received when the remote
203762306a36Sopenharmony_ci *  mailbox is active.
203862306a36Sopenharmony_ci **/
203962306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_process_version_1(struct fm10k_hw *hw,
204062306a36Sopenharmony_ci					  struct fm10k_mbx_info *mbx)
204162306a36Sopenharmony_ci{
204262306a36Sopenharmony_ci	const u32 *hdr = &mbx->mbx_hdr;
204362306a36Sopenharmony_ci	u16 head, tail;
204462306a36Sopenharmony_ci	s32 len;
204562306a36Sopenharmony_ci
204662306a36Sopenharmony_ci	/* pull all fields needed for verification */
204762306a36Sopenharmony_ci	tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
204862306a36Sopenharmony_ci	head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
204962306a36Sopenharmony_ci
205062306a36Sopenharmony_ci	/* if we are in connect and wanting version 1 then start up and go */
205162306a36Sopenharmony_ci	if (mbx->state == FM10K_STATE_CONNECT) {
205262306a36Sopenharmony_ci		if (!mbx->remote)
205362306a36Sopenharmony_ci			goto send_reply;
205462306a36Sopenharmony_ci		if (mbx->remote != 1)
205562306a36Sopenharmony_ci			return FM10K_MBX_ERR_SRC;
205662306a36Sopenharmony_ci
205762306a36Sopenharmony_ci		mbx->state = FM10K_STATE_OPEN;
205862306a36Sopenharmony_ci	}
205962306a36Sopenharmony_ci
206062306a36Sopenharmony_ci	do {
206162306a36Sopenharmony_ci		/* abort on message size errors */
206262306a36Sopenharmony_ci		len = fm10k_sm_mbx_receive(hw, mbx, tail);
206362306a36Sopenharmony_ci		if (len < 0)
206462306a36Sopenharmony_ci			return len;
206562306a36Sopenharmony_ci
206662306a36Sopenharmony_ci		/* continue until we have flushed the Rx FIFO */
206762306a36Sopenharmony_ci	} while (len);
206862306a36Sopenharmony_ci
206962306a36Sopenharmony_cisend_reply:
207062306a36Sopenharmony_ci	fm10k_sm_mbx_create_reply(hw, mbx, head);
207162306a36Sopenharmony_ci
207262306a36Sopenharmony_ci	return 0;
207362306a36Sopenharmony_ci}
207462306a36Sopenharmony_ci
207562306a36Sopenharmony_ci/**
207662306a36Sopenharmony_ci *  fm10k_sm_mbx_process - Process switch manager mailbox interrupt
207762306a36Sopenharmony_ci *  @hw: pointer to hardware structure
207862306a36Sopenharmony_ci *  @mbx: pointer to mailbox
207962306a36Sopenharmony_ci *
208062306a36Sopenharmony_ci *  This function will process incoming mailbox events and generate mailbox
208162306a36Sopenharmony_ci *  replies.  It will return a value indicating the number of DWORDs
208262306a36Sopenharmony_ci *  transmitted excluding header on success or a negative value on error.
208362306a36Sopenharmony_ci **/
208462306a36Sopenharmony_cistatic s32 fm10k_sm_mbx_process(struct fm10k_hw *hw,
208562306a36Sopenharmony_ci				struct fm10k_mbx_info *mbx)
208662306a36Sopenharmony_ci{
208762306a36Sopenharmony_ci	s32 err;
208862306a36Sopenharmony_ci
208962306a36Sopenharmony_ci	/* we do not read mailbox if closed */
209062306a36Sopenharmony_ci	if (mbx->state == FM10K_STATE_CLOSED)
209162306a36Sopenharmony_ci		return 0;
209262306a36Sopenharmony_ci
209362306a36Sopenharmony_ci	/* retrieve data from switch manager */
209462306a36Sopenharmony_ci	err = fm10k_mbx_read(hw, mbx);
209562306a36Sopenharmony_ci	if (err)
209662306a36Sopenharmony_ci		return err;
209762306a36Sopenharmony_ci
209862306a36Sopenharmony_ci	err = fm10k_sm_mbx_validate_fifo_hdr(mbx);
209962306a36Sopenharmony_ci	if (err < 0)
210062306a36Sopenharmony_ci		goto fifo_err;
210162306a36Sopenharmony_ci
210262306a36Sopenharmony_ci	if (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_ERR)) {
210362306a36Sopenharmony_ci		fm10k_sm_mbx_process_error(mbx);
210462306a36Sopenharmony_ci		goto fifo_err;
210562306a36Sopenharmony_ci	}
210662306a36Sopenharmony_ci
210762306a36Sopenharmony_ci	switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_VER)) {
210862306a36Sopenharmony_ci	case 0:
210962306a36Sopenharmony_ci		err = fm10k_sm_mbx_process_reset(hw, mbx);
211062306a36Sopenharmony_ci		break;
211162306a36Sopenharmony_ci	case FM10K_SM_MBX_VERSION:
211262306a36Sopenharmony_ci		err = fm10k_sm_mbx_process_version_1(hw, mbx);
211362306a36Sopenharmony_ci		break;
211462306a36Sopenharmony_ci	}
211562306a36Sopenharmony_ci
211662306a36Sopenharmony_cififo_err:
211762306a36Sopenharmony_ci	if (err < 0)
211862306a36Sopenharmony_ci		fm10k_sm_mbx_create_error_msg(mbx, err);
211962306a36Sopenharmony_ci
212062306a36Sopenharmony_ci	/* report data to switch manager */
212162306a36Sopenharmony_ci	fm10k_mbx_write(hw, mbx);
212262306a36Sopenharmony_ci
212362306a36Sopenharmony_ci	return err;
212462306a36Sopenharmony_ci}
212562306a36Sopenharmony_ci
212662306a36Sopenharmony_ci/**
212762306a36Sopenharmony_ci *  fm10k_sm_mbx_init - Initialize mailbox memory for PF/SM mailbox
212862306a36Sopenharmony_ci *  @hw: pointer to hardware structure
212962306a36Sopenharmony_ci *  @mbx: pointer to mailbox
213062306a36Sopenharmony_ci *  @msg_data: handlers for mailbox events
213162306a36Sopenharmony_ci *
213262306a36Sopenharmony_ci *  This function initializes the PF/SM mailbox for use.  It will split the
213362306a36Sopenharmony_ci *  buffer provided and use that to populate both the Tx and Rx FIFO by
213462306a36Sopenharmony_ci *  evenly splitting it.  In order to allow for easy masking of head/tail
213562306a36Sopenharmony_ci *  the value reported in size must be a power of 2 and is reported in
213662306a36Sopenharmony_ci *  DWORDs, not bytes.  Any invalid values will cause the mailbox to return
213762306a36Sopenharmony_ci *  error.
213862306a36Sopenharmony_ci **/
213962306a36Sopenharmony_cis32 fm10k_sm_mbx_init(struct fm10k_hw __always_unused *hw,
214062306a36Sopenharmony_ci		      struct fm10k_mbx_info *mbx,
214162306a36Sopenharmony_ci		      const struct fm10k_msg_data *msg_data)
214262306a36Sopenharmony_ci{
214362306a36Sopenharmony_ci	mbx->mbx_reg = FM10K_GMBX;
214462306a36Sopenharmony_ci	mbx->mbmem_reg = FM10K_MBMEM_PF(0);
214562306a36Sopenharmony_ci
214662306a36Sopenharmony_ci	/* start out in closed state */
214762306a36Sopenharmony_ci	mbx->state = FM10K_STATE_CLOSED;
214862306a36Sopenharmony_ci
214962306a36Sopenharmony_ci	/* validate layout of handlers before assigning them */
215062306a36Sopenharmony_ci	if (fm10k_mbx_validate_handlers(msg_data))
215162306a36Sopenharmony_ci		return FM10K_ERR_PARAM;
215262306a36Sopenharmony_ci
215362306a36Sopenharmony_ci	/* initialize the message handlers */
215462306a36Sopenharmony_ci	mbx->msg_data = msg_data;
215562306a36Sopenharmony_ci
215662306a36Sopenharmony_ci	/* start mailbox as timed out and let the reset_hw call
215762306a36Sopenharmony_ci	 * set the timeout value to begin communications
215862306a36Sopenharmony_ci	 */
215962306a36Sopenharmony_ci	mbx->timeout = 0;
216062306a36Sopenharmony_ci	mbx->udelay = FM10K_MBX_INIT_DELAY;
216162306a36Sopenharmony_ci
216262306a36Sopenharmony_ci	/* Split buffer for use by Tx/Rx FIFOs */
216362306a36Sopenharmony_ci	mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
216462306a36Sopenharmony_ci	mbx->mbmem_len = FM10K_MBMEM_PF_XOR;
216562306a36Sopenharmony_ci
216662306a36Sopenharmony_ci	/* initialize the FIFOs, sizes are in 4 byte increments */
216762306a36Sopenharmony_ci	fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
216862306a36Sopenharmony_ci	fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
216962306a36Sopenharmony_ci			FM10K_MBX_RX_BUFFER_SIZE);
217062306a36Sopenharmony_ci
217162306a36Sopenharmony_ci	/* initialize function pointers */
217262306a36Sopenharmony_ci	mbx->ops.connect = fm10k_sm_mbx_connect;
217362306a36Sopenharmony_ci	mbx->ops.disconnect = fm10k_sm_mbx_disconnect;
217462306a36Sopenharmony_ci	mbx->ops.rx_ready = fm10k_mbx_rx_ready;
217562306a36Sopenharmony_ci	mbx->ops.tx_ready = fm10k_mbx_tx_ready;
217662306a36Sopenharmony_ci	mbx->ops.tx_complete = fm10k_mbx_tx_complete;
217762306a36Sopenharmony_ci	mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
217862306a36Sopenharmony_ci	mbx->ops.process = fm10k_sm_mbx_process;
217962306a36Sopenharmony_ci	mbx->ops.register_handlers = fm10k_mbx_register_handlers;
218062306a36Sopenharmony_ci
218162306a36Sopenharmony_ci	return 0;
218262306a36Sopenharmony_ci}
2183