1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 2013 - 2019 Intel Corporation. */
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include "fm10k_common.h"
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun /**
7*4882a593Smuzhiyun * fm10k_fifo_init - Initialize a message FIFO
8*4882a593Smuzhiyun * @fifo: pointer to FIFO
9*4882a593Smuzhiyun * @buffer: pointer to memory to be used to store FIFO
10*4882a593Smuzhiyun * @size: maximum message size to store in FIFO, must be 2^n - 1
11*4882a593Smuzhiyun **/
fm10k_fifo_init(struct fm10k_mbx_fifo * fifo,u32 * buffer,u16 size)12*4882a593Smuzhiyun static void fm10k_fifo_init(struct fm10k_mbx_fifo *fifo, u32 *buffer, u16 size)
13*4882a593Smuzhiyun {
14*4882a593Smuzhiyun fifo->buffer = buffer;
15*4882a593Smuzhiyun fifo->size = size;
16*4882a593Smuzhiyun fifo->head = 0;
17*4882a593Smuzhiyun fifo->tail = 0;
18*4882a593Smuzhiyun }
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * fm10k_fifo_used - Retrieve used space in FIFO
22*4882a593Smuzhiyun * @fifo: pointer to FIFO
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * This function returns the number of DWORDs used in the FIFO
25*4882a593Smuzhiyun **/
fm10k_fifo_used(struct fm10k_mbx_fifo * fifo)26*4882a593Smuzhiyun static u16 fm10k_fifo_used(struct fm10k_mbx_fifo *fifo)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun return fifo->tail - fifo->head;
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun * fm10k_fifo_unused - Retrieve unused space in FIFO
33*4882a593Smuzhiyun * @fifo: pointer to FIFO
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * This function returns the number of unused DWORDs in the FIFO
36*4882a593Smuzhiyun **/
fm10k_fifo_unused(struct fm10k_mbx_fifo * fifo)37*4882a593Smuzhiyun static u16 fm10k_fifo_unused(struct fm10k_mbx_fifo *fifo)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun return fifo->size + fifo->head - fifo->tail;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * fm10k_fifo_empty - Test to verify if FIFO is empty
44*4882a593Smuzhiyun * @fifo: pointer to FIFO
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * This function returns true if the FIFO is empty, else false
47*4882a593Smuzhiyun **/
fm10k_fifo_empty(struct fm10k_mbx_fifo * fifo)48*4882a593Smuzhiyun static bool fm10k_fifo_empty(struct fm10k_mbx_fifo *fifo)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun return fifo->head == fifo->tail;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * fm10k_fifo_head_offset - returns indices of head with given offset
55*4882a593Smuzhiyun * @fifo: pointer to FIFO
56*4882a593Smuzhiyun * @offset: offset to add to head
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * This function returns the indices into the FIFO based on head + offset
59*4882a593Smuzhiyun **/
fm10k_fifo_head_offset(struct fm10k_mbx_fifo * fifo,u16 offset)60*4882a593Smuzhiyun static u16 fm10k_fifo_head_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return (fifo->head + offset) & (fifo->size - 1);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun * fm10k_fifo_tail_offset - returns indices of tail with given offset
67*4882a593Smuzhiyun * @fifo: pointer to FIFO
68*4882a593Smuzhiyun * @offset: offset to add to tail
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * This function returns the indices into the FIFO based on tail + offset
71*4882a593Smuzhiyun **/
fm10k_fifo_tail_offset(struct fm10k_mbx_fifo * fifo,u16 offset)72*4882a593Smuzhiyun static u16 fm10k_fifo_tail_offset(struct fm10k_mbx_fifo *fifo, u16 offset)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun return (fifo->tail + offset) & (fifo->size - 1);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun * fm10k_fifo_head_len - Retrieve length of first message in FIFO
79*4882a593Smuzhiyun * @fifo: pointer to FIFO
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * This function returns the size of the first message in the FIFO
82*4882a593Smuzhiyun **/
fm10k_fifo_head_len(struct fm10k_mbx_fifo * fifo)83*4882a593Smuzhiyun static u16 fm10k_fifo_head_len(struct fm10k_mbx_fifo *fifo)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun u32 *head = fifo->buffer + fm10k_fifo_head_offset(fifo, 0);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* verify there is at least 1 DWORD in the fifo so *head is valid */
88*4882a593Smuzhiyun if (fm10k_fifo_empty(fifo))
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* retieve the message length */
92*4882a593Smuzhiyun return FM10K_TLV_DWORD_LEN(*head);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun * fm10k_fifo_head_drop - Drop the first message in FIFO
97*4882a593Smuzhiyun * @fifo: pointer to FIFO
98*4882a593Smuzhiyun *
99*4882a593Smuzhiyun * This function returns the size of the message dropped from the FIFO
100*4882a593Smuzhiyun **/
fm10k_fifo_head_drop(struct fm10k_mbx_fifo * fifo)101*4882a593Smuzhiyun static u16 fm10k_fifo_head_drop(struct fm10k_mbx_fifo *fifo)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun u16 len = fm10k_fifo_head_len(fifo);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /* update head so it is at the start of next frame */
106*4882a593Smuzhiyun fifo->head += len;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return len;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /**
112*4882a593Smuzhiyun * fm10k_fifo_drop_all - Drop all messages in FIFO
113*4882a593Smuzhiyun * @fifo: pointer to FIFO
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * This function resets the head pointer to drop all messages in the FIFO and
116*4882a593Smuzhiyun * ensure the FIFO is empty.
117*4882a593Smuzhiyun **/
fm10k_fifo_drop_all(struct fm10k_mbx_fifo * fifo)118*4882a593Smuzhiyun static void fm10k_fifo_drop_all(struct fm10k_mbx_fifo *fifo)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun fifo->head = fifo->tail;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun * fm10k_mbx_index_len - Convert a head/tail index into a length value
125*4882a593Smuzhiyun * @mbx: pointer to mailbox
126*4882a593Smuzhiyun * @head: head index
127*4882a593Smuzhiyun * @tail: head index
128*4882a593Smuzhiyun *
129*4882a593Smuzhiyun * This function takes the head and tail index and determines the length
130*4882a593Smuzhiyun * of the data indicated by this pair.
131*4882a593Smuzhiyun **/
fm10k_mbx_index_len(struct fm10k_mbx_info * mbx,u16 head,u16 tail)132*4882a593Smuzhiyun static u16 fm10k_mbx_index_len(struct fm10k_mbx_info *mbx, u16 head, u16 tail)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun u16 len = tail - head;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* we wrapped so subtract 2, one for index 0, one for all 1s index */
137*4882a593Smuzhiyun if (len > tail)
138*4882a593Smuzhiyun len -= 2;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return len & ((mbx->mbmem_len << 1) - 1);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /**
144*4882a593Smuzhiyun * fm10k_mbx_tail_add - Determine new tail value with added offset
145*4882a593Smuzhiyun * @mbx: pointer to mailbox
146*4882a593Smuzhiyun * @offset: length to add to tail offset
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * This function takes the local tail index and recomputes it for
149*4882a593Smuzhiyun * a given length added as an offset.
150*4882a593Smuzhiyun **/
fm10k_mbx_tail_add(struct fm10k_mbx_info * mbx,u16 offset)151*4882a593Smuzhiyun static u16 fm10k_mbx_tail_add(struct fm10k_mbx_info *mbx, u16 offset)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun u16 tail = (mbx->tail + offset + 1) & ((mbx->mbmem_len << 1) - 1);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* add/sub 1 because we cannot have offset 0 or all 1s */
156*4882a593Smuzhiyun return (tail > mbx->tail) ? --tail : ++tail;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun * fm10k_mbx_tail_sub - Determine new tail value with subtracted offset
161*4882a593Smuzhiyun * @mbx: pointer to mailbox
162*4882a593Smuzhiyun * @offset: length to add to tail offset
163*4882a593Smuzhiyun *
164*4882a593Smuzhiyun * This function takes the local tail index and recomputes it for
165*4882a593Smuzhiyun * a given length added as an offset.
166*4882a593Smuzhiyun **/
fm10k_mbx_tail_sub(struct fm10k_mbx_info * mbx,u16 offset)167*4882a593Smuzhiyun static u16 fm10k_mbx_tail_sub(struct fm10k_mbx_info *mbx, u16 offset)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun u16 tail = (mbx->tail - offset - 1) & ((mbx->mbmem_len << 1) - 1);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* sub/add 1 because we cannot have offset 0 or all 1s */
172*4882a593Smuzhiyun return (tail < mbx->tail) ? ++tail : --tail;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /**
176*4882a593Smuzhiyun * fm10k_mbx_head_add - Determine new head value with added offset
177*4882a593Smuzhiyun * @mbx: pointer to mailbox
178*4882a593Smuzhiyun * @offset: length to add to head offset
179*4882a593Smuzhiyun *
180*4882a593Smuzhiyun * This function takes the local head index and recomputes it for
181*4882a593Smuzhiyun * a given length added as an offset.
182*4882a593Smuzhiyun **/
fm10k_mbx_head_add(struct fm10k_mbx_info * mbx,u16 offset)183*4882a593Smuzhiyun static u16 fm10k_mbx_head_add(struct fm10k_mbx_info *mbx, u16 offset)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun u16 head = (mbx->head + offset + 1) & ((mbx->mbmem_len << 1) - 1);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /* add/sub 1 because we cannot have offset 0 or all 1s */
188*4882a593Smuzhiyun return (head > mbx->head) ? --head : ++head;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /**
192*4882a593Smuzhiyun * fm10k_mbx_head_sub - Determine new head value with subtracted offset
193*4882a593Smuzhiyun * @mbx: pointer to mailbox
194*4882a593Smuzhiyun * @offset: length to add to head offset
195*4882a593Smuzhiyun *
196*4882a593Smuzhiyun * This function takes the local head index and recomputes it for
197*4882a593Smuzhiyun * a given length added as an offset.
198*4882a593Smuzhiyun **/
fm10k_mbx_head_sub(struct fm10k_mbx_info * mbx,u16 offset)199*4882a593Smuzhiyun static u16 fm10k_mbx_head_sub(struct fm10k_mbx_info *mbx, u16 offset)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun u16 head = (mbx->head - offset - 1) & ((mbx->mbmem_len << 1) - 1);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* sub/add 1 because we cannot have offset 0 or all 1s */
204*4882a593Smuzhiyun return (head < mbx->head) ? ++head : --head;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun * fm10k_mbx_pushed_tail_len - Retrieve the length of message being pushed
209*4882a593Smuzhiyun * @mbx: pointer to mailbox
210*4882a593Smuzhiyun *
211*4882a593Smuzhiyun * This function will return the length of the message currently being
212*4882a593Smuzhiyun * pushed onto the tail of the Rx queue.
213*4882a593Smuzhiyun **/
fm10k_mbx_pushed_tail_len(struct fm10k_mbx_info * mbx)214*4882a593Smuzhiyun static u16 fm10k_mbx_pushed_tail_len(struct fm10k_mbx_info *mbx)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun u32 *tail = mbx->rx.buffer + fm10k_fifo_tail_offset(&mbx->rx, 0);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /* pushed tail is only valid if pushed is set */
219*4882a593Smuzhiyun if (!mbx->pushed)
220*4882a593Smuzhiyun return 0;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun return FM10K_TLV_DWORD_LEN(*tail);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /**
226*4882a593Smuzhiyun * fm10k_fifo_write_copy - pulls data off of msg and places it in FIFO
227*4882a593Smuzhiyun * @fifo: pointer to FIFO
228*4882a593Smuzhiyun * @msg: message array to populate
229*4882a593Smuzhiyun * @tail_offset: additional offset to add to tail pointer
230*4882a593Smuzhiyun * @len: length of FIFO to copy into message header
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * This function will take a message and copy it into a section of the
233*4882a593Smuzhiyun * FIFO. In order to get something into a location other than just
234*4882a593Smuzhiyun * the tail you can use tail_offset to adjust the pointer.
235*4882a593Smuzhiyun **/
fm10k_fifo_write_copy(struct fm10k_mbx_fifo * fifo,const u32 * msg,u16 tail_offset,u16 len)236*4882a593Smuzhiyun static void fm10k_fifo_write_copy(struct fm10k_mbx_fifo *fifo,
237*4882a593Smuzhiyun const u32 *msg, u16 tail_offset, u16 len)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun u16 end = fm10k_fifo_tail_offset(fifo, tail_offset);
240*4882a593Smuzhiyun u32 *tail = fifo->buffer + end;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun /* track when we should cross the end of the FIFO */
243*4882a593Smuzhiyun end = fifo->size - end;
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /* copy end of message before start of message */
246*4882a593Smuzhiyun if (end < len)
247*4882a593Smuzhiyun memcpy(fifo->buffer, msg + end, (len - end) << 2);
248*4882a593Smuzhiyun else
249*4882a593Smuzhiyun end = len;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Copy remaining message into Tx FIFO */
252*4882a593Smuzhiyun memcpy(tail, msg, end << 2);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun * fm10k_fifo_enqueue - Enqueues the message to the tail of the FIFO
257*4882a593Smuzhiyun * @fifo: pointer to FIFO
258*4882a593Smuzhiyun * @msg: message array to read
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * This function enqueues a message up to the size specified by the length
261*4882a593Smuzhiyun * contained in the first DWORD of the message and will place at the tail
262*4882a593Smuzhiyun * of the FIFO. It will return 0 on success, or a negative value on error.
263*4882a593Smuzhiyun **/
fm10k_fifo_enqueue(struct fm10k_mbx_fifo * fifo,const u32 * msg)264*4882a593Smuzhiyun static s32 fm10k_fifo_enqueue(struct fm10k_mbx_fifo *fifo, const u32 *msg)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun u16 len = FM10K_TLV_DWORD_LEN(*msg);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* verify parameters */
269*4882a593Smuzhiyun if (len > fifo->size)
270*4882a593Smuzhiyun return FM10K_MBX_ERR_SIZE;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* verify there is room for the message */
273*4882a593Smuzhiyun if (len > fm10k_fifo_unused(fifo))
274*4882a593Smuzhiyun return FM10K_MBX_ERR_NO_SPACE;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* Copy message into FIFO */
277*4882a593Smuzhiyun fm10k_fifo_write_copy(fifo, msg, 0, len);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /* memory barrier to guarantee FIFO is written before tail update */
280*4882a593Smuzhiyun wmb();
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* Update Tx FIFO tail */
283*4882a593Smuzhiyun fifo->tail += len;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return 0;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun * fm10k_mbx_validate_msg_size - Validate incoming message based on size
290*4882a593Smuzhiyun * @mbx: pointer to mailbox
291*4882a593Smuzhiyun * @len: length of data pushed onto buffer
292*4882a593Smuzhiyun *
293*4882a593Smuzhiyun * This function analyzes the frame and will return a non-zero value when
294*4882a593Smuzhiyun * the start of a message larger than the mailbox is detected.
295*4882a593Smuzhiyun **/
fm10k_mbx_validate_msg_size(struct fm10k_mbx_info * mbx,u16 len)296*4882a593Smuzhiyun static u16 fm10k_mbx_validate_msg_size(struct fm10k_mbx_info *mbx, u16 len)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->rx;
299*4882a593Smuzhiyun u16 total_len = 0, msg_len;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /* length should include previous amounts pushed */
302*4882a593Smuzhiyun len += mbx->pushed;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /* offset in message is based off of current message size */
305*4882a593Smuzhiyun do {
306*4882a593Smuzhiyun u32 *msg;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun msg = fifo->buffer + fm10k_fifo_tail_offset(fifo, total_len);
309*4882a593Smuzhiyun msg_len = FM10K_TLV_DWORD_LEN(*msg);
310*4882a593Smuzhiyun total_len += msg_len;
311*4882a593Smuzhiyun } while (total_len < len);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /* message extends out of pushed section, but fits in FIFO */
314*4882a593Smuzhiyun if ((len < total_len) && (msg_len <= mbx->max_size))
315*4882a593Smuzhiyun return 0;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* return length of invalid section */
318*4882a593Smuzhiyun return (len < total_len) ? len : (len - total_len);
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /**
322*4882a593Smuzhiyun * fm10k_mbx_write_copy - pulls data off of Tx FIFO and places it in mbmem
323*4882a593Smuzhiyun * @hw: pointer to hardware structure
324*4882a593Smuzhiyun * @mbx: pointer to mailbox
325*4882a593Smuzhiyun *
326*4882a593Smuzhiyun * This function will take a section of the Tx FIFO and copy it into the
327*4882a593Smuzhiyun * mailbox memory. The offset in mbmem is based on the lower bits of the
328*4882a593Smuzhiyun * tail and len determines the length to copy.
329*4882a593Smuzhiyun **/
fm10k_mbx_write_copy(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)330*4882a593Smuzhiyun static void fm10k_mbx_write_copy(struct fm10k_hw *hw,
331*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->tx;
334*4882a593Smuzhiyun u32 mbmem = mbx->mbmem_reg;
335*4882a593Smuzhiyun u32 *head = fifo->buffer;
336*4882a593Smuzhiyun u16 end, len, tail, mask;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun if (!mbx->tail_len)
339*4882a593Smuzhiyun return;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* determine data length and mbmem tail index */
342*4882a593Smuzhiyun mask = mbx->mbmem_len - 1;
343*4882a593Smuzhiyun len = mbx->tail_len;
344*4882a593Smuzhiyun tail = fm10k_mbx_tail_sub(mbx, len);
345*4882a593Smuzhiyun if (tail > mask)
346*4882a593Smuzhiyun tail++;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /* determine offset in the ring */
349*4882a593Smuzhiyun end = fm10k_fifo_head_offset(fifo, mbx->pulled);
350*4882a593Smuzhiyun head += end;
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /* memory barrier to guarantee data is ready to be read */
353*4882a593Smuzhiyun rmb();
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun /* Copy message from Tx FIFO */
356*4882a593Smuzhiyun for (end = fifo->size - end; len; head = fifo->buffer) {
357*4882a593Smuzhiyun do {
358*4882a593Smuzhiyun /* adjust tail to match offset for FIFO */
359*4882a593Smuzhiyun tail &= mask;
360*4882a593Smuzhiyun if (!tail)
361*4882a593Smuzhiyun tail++;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun mbx->tx_mbmem_pulled++;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* write message to hardware FIFO */
366*4882a593Smuzhiyun fm10k_write_reg(hw, mbmem + tail++, *(head++));
367*4882a593Smuzhiyun } while (--len && --end);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /**
372*4882a593Smuzhiyun * fm10k_mbx_pull_head - Pulls data off of head of Tx FIFO
373*4882a593Smuzhiyun * @hw: pointer to hardware structure
374*4882a593Smuzhiyun * @mbx: pointer to mailbox
375*4882a593Smuzhiyun * @head: acknowledgement number last received
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * This function will push the tail index forward based on the remote
378*4882a593Smuzhiyun * head index. It will then pull up to mbmem_len DWORDs off of the
379*4882a593Smuzhiyun * head of the FIFO and will place it in the MBMEM registers
380*4882a593Smuzhiyun * associated with the mailbox.
381*4882a593Smuzhiyun **/
fm10k_mbx_pull_head(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 head)382*4882a593Smuzhiyun static void fm10k_mbx_pull_head(struct fm10k_hw *hw,
383*4882a593Smuzhiyun struct fm10k_mbx_info *mbx, u16 head)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun u16 mbmem_len, len, ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
386*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->tx;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun /* update number of bytes pulled and update bytes in transit */
389*4882a593Smuzhiyun mbx->pulled += mbx->tail_len - ack;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /* determine length of data to pull, reserve space for mbmem header */
392*4882a593Smuzhiyun mbmem_len = mbx->mbmem_len - 1;
393*4882a593Smuzhiyun len = fm10k_fifo_used(fifo) - mbx->pulled;
394*4882a593Smuzhiyun if (len > mbmem_len)
395*4882a593Smuzhiyun len = mbmem_len;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /* update tail and record number of bytes in transit */
398*4882a593Smuzhiyun mbx->tail = fm10k_mbx_tail_add(mbx, len - ack);
399*4882a593Smuzhiyun mbx->tail_len = len;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun /* drop pulled messages from the FIFO */
402*4882a593Smuzhiyun for (len = fm10k_fifo_head_len(fifo);
403*4882a593Smuzhiyun len && (mbx->pulled >= len);
404*4882a593Smuzhiyun len = fm10k_fifo_head_len(fifo)) {
405*4882a593Smuzhiyun mbx->pulled -= fm10k_fifo_head_drop(fifo);
406*4882a593Smuzhiyun mbx->tx_messages++;
407*4882a593Smuzhiyun mbx->tx_dwords += len;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* Copy message out from the Tx FIFO */
411*4882a593Smuzhiyun fm10k_mbx_write_copy(hw, mbx);
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun * fm10k_mbx_read_copy - pulls data off of mbmem and places it in Rx FIFO
416*4882a593Smuzhiyun * @hw: pointer to hardware structure
417*4882a593Smuzhiyun * @mbx: pointer to mailbox
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * This function will take a section of the mailbox memory and copy it
420*4882a593Smuzhiyun * into the Rx FIFO. The offset is based on the lower bits of the
421*4882a593Smuzhiyun * head and len determines the length to copy.
422*4882a593Smuzhiyun **/
fm10k_mbx_read_copy(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)423*4882a593Smuzhiyun static void fm10k_mbx_read_copy(struct fm10k_hw *hw,
424*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->rx;
427*4882a593Smuzhiyun u32 mbmem = mbx->mbmem_reg ^ mbx->mbmem_len;
428*4882a593Smuzhiyun u32 *tail = fifo->buffer;
429*4882a593Smuzhiyun u16 end, len, head;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* determine data length and mbmem head index */
432*4882a593Smuzhiyun len = mbx->head_len;
433*4882a593Smuzhiyun head = fm10k_mbx_head_sub(mbx, len);
434*4882a593Smuzhiyun if (head >= mbx->mbmem_len)
435*4882a593Smuzhiyun head++;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /* determine offset in the ring */
438*4882a593Smuzhiyun end = fm10k_fifo_tail_offset(fifo, mbx->pushed);
439*4882a593Smuzhiyun tail += end;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* Copy message into Rx FIFO */
442*4882a593Smuzhiyun for (end = fifo->size - end; len; tail = fifo->buffer) {
443*4882a593Smuzhiyun do {
444*4882a593Smuzhiyun /* adjust head to match offset for FIFO */
445*4882a593Smuzhiyun head &= mbx->mbmem_len - 1;
446*4882a593Smuzhiyun if (!head)
447*4882a593Smuzhiyun head++;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun mbx->rx_mbmem_pushed++;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /* read message from hardware FIFO */
452*4882a593Smuzhiyun *(tail++) = fm10k_read_reg(hw, mbmem + head++);
453*4882a593Smuzhiyun } while (--len && --end);
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun /* memory barrier to guarantee FIFO is written before tail update */
457*4882a593Smuzhiyun wmb();
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun /**
461*4882a593Smuzhiyun * fm10k_mbx_push_tail - Pushes up to 15 DWORDs on to tail of FIFO
462*4882a593Smuzhiyun * @hw: pointer to hardware structure
463*4882a593Smuzhiyun * @mbx: pointer to mailbox
464*4882a593Smuzhiyun * @tail: tail index of message
465*4882a593Smuzhiyun *
466*4882a593Smuzhiyun * This function will first validate the tail index and size for the
467*4882a593Smuzhiyun * incoming message. It then updates the acknowledgment number and
468*4882a593Smuzhiyun * copies the data into the FIFO. It will return the number of messages
469*4882a593Smuzhiyun * dequeued on success and a negative value on error.
470*4882a593Smuzhiyun **/
fm10k_mbx_push_tail(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 tail)471*4882a593Smuzhiyun static s32 fm10k_mbx_push_tail(struct fm10k_hw *hw,
472*4882a593Smuzhiyun struct fm10k_mbx_info *mbx,
473*4882a593Smuzhiyun u16 tail)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->rx;
476*4882a593Smuzhiyun u16 len, seq = fm10k_mbx_index_len(mbx, mbx->head, tail);
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /* determine length of data to push */
479*4882a593Smuzhiyun len = fm10k_fifo_unused(fifo) - mbx->pushed;
480*4882a593Smuzhiyun if (len > seq)
481*4882a593Smuzhiyun len = seq;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /* update head and record bytes received */
484*4882a593Smuzhiyun mbx->head = fm10k_mbx_head_add(mbx, len);
485*4882a593Smuzhiyun mbx->head_len = len;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* nothing to do if there is no data */
488*4882a593Smuzhiyun if (!len)
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* Copy msg into Rx FIFO */
492*4882a593Smuzhiyun fm10k_mbx_read_copy(hw, mbx);
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /* determine if there are any invalid lengths in message */
495*4882a593Smuzhiyun if (fm10k_mbx_validate_msg_size(mbx, len))
496*4882a593Smuzhiyun return FM10K_MBX_ERR_SIZE;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun /* Update pushed */
499*4882a593Smuzhiyun mbx->pushed += len;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun /* flush any completed messages */
502*4882a593Smuzhiyun for (len = fm10k_mbx_pushed_tail_len(mbx);
503*4882a593Smuzhiyun len && (mbx->pushed >= len);
504*4882a593Smuzhiyun len = fm10k_mbx_pushed_tail_len(mbx)) {
505*4882a593Smuzhiyun fifo->tail += len;
506*4882a593Smuzhiyun mbx->pushed -= len;
507*4882a593Smuzhiyun mbx->rx_messages++;
508*4882a593Smuzhiyun mbx->rx_dwords += len;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return 0;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun /* pre-generated data for generating the CRC based on the poly 0xAC9A. */
515*4882a593Smuzhiyun static const u16 fm10k_crc_16b_table[256] = {
516*4882a593Smuzhiyun 0x0000, 0x7956, 0xF2AC, 0x8BFA, 0xBC6D, 0xC53B, 0x4EC1, 0x3797,
517*4882a593Smuzhiyun 0x21EF, 0x58B9, 0xD343, 0xAA15, 0x9D82, 0xE4D4, 0x6F2E, 0x1678,
518*4882a593Smuzhiyun 0x43DE, 0x3A88, 0xB172, 0xC824, 0xFFB3, 0x86E5, 0x0D1F, 0x7449,
519*4882a593Smuzhiyun 0x6231, 0x1B67, 0x909D, 0xE9CB, 0xDE5C, 0xA70A, 0x2CF0, 0x55A6,
520*4882a593Smuzhiyun 0x87BC, 0xFEEA, 0x7510, 0x0C46, 0x3BD1, 0x4287, 0xC97D, 0xB02B,
521*4882a593Smuzhiyun 0xA653, 0xDF05, 0x54FF, 0x2DA9, 0x1A3E, 0x6368, 0xE892, 0x91C4,
522*4882a593Smuzhiyun 0xC462, 0xBD34, 0x36CE, 0x4F98, 0x780F, 0x0159, 0x8AA3, 0xF3F5,
523*4882a593Smuzhiyun 0xE58D, 0x9CDB, 0x1721, 0x6E77, 0x59E0, 0x20B6, 0xAB4C, 0xD21A,
524*4882a593Smuzhiyun 0x564D, 0x2F1B, 0xA4E1, 0xDDB7, 0xEA20, 0x9376, 0x188C, 0x61DA,
525*4882a593Smuzhiyun 0x77A2, 0x0EF4, 0x850E, 0xFC58, 0xCBCF, 0xB299, 0x3963, 0x4035,
526*4882a593Smuzhiyun 0x1593, 0x6CC5, 0xE73F, 0x9E69, 0xA9FE, 0xD0A8, 0x5B52, 0x2204,
527*4882a593Smuzhiyun 0x347C, 0x4D2A, 0xC6D0, 0xBF86, 0x8811, 0xF147, 0x7ABD, 0x03EB,
528*4882a593Smuzhiyun 0xD1F1, 0xA8A7, 0x235D, 0x5A0B, 0x6D9C, 0x14CA, 0x9F30, 0xE666,
529*4882a593Smuzhiyun 0xF01E, 0x8948, 0x02B2, 0x7BE4, 0x4C73, 0x3525, 0xBEDF, 0xC789,
530*4882a593Smuzhiyun 0x922F, 0xEB79, 0x6083, 0x19D5, 0x2E42, 0x5714, 0xDCEE, 0xA5B8,
531*4882a593Smuzhiyun 0xB3C0, 0xCA96, 0x416C, 0x383A, 0x0FAD, 0x76FB, 0xFD01, 0x8457,
532*4882a593Smuzhiyun 0xAC9A, 0xD5CC, 0x5E36, 0x2760, 0x10F7, 0x69A1, 0xE25B, 0x9B0D,
533*4882a593Smuzhiyun 0x8D75, 0xF423, 0x7FD9, 0x068F, 0x3118, 0x484E, 0xC3B4, 0xBAE2,
534*4882a593Smuzhiyun 0xEF44, 0x9612, 0x1DE8, 0x64BE, 0x5329, 0x2A7F, 0xA185, 0xD8D3,
535*4882a593Smuzhiyun 0xCEAB, 0xB7FD, 0x3C07, 0x4551, 0x72C6, 0x0B90, 0x806A, 0xF93C,
536*4882a593Smuzhiyun 0x2B26, 0x5270, 0xD98A, 0xA0DC, 0x974B, 0xEE1D, 0x65E7, 0x1CB1,
537*4882a593Smuzhiyun 0x0AC9, 0x739F, 0xF865, 0x8133, 0xB6A4, 0xCFF2, 0x4408, 0x3D5E,
538*4882a593Smuzhiyun 0x68F8, 0x11AE, 0x9A54, 0xE302, 0xD495, 0xADC3, 0x2639, 0x5F6F,
539*4882a593Smuzhiyun 0x4917, 0x3041, 0xBBBB, 0xC2ED, 0xF57A, 0x8C2C, 0x07D6, 0x7E80,
540*4882a593Smuzhiyun 0xFAD7, 0x8381, 0x087B, 0x712D, 0x46BA, 0x3FEC, 0xB416, 0xCD40,
541*4882a593Smuzhiyun 0xDB38, 0xA26E, 0x2994, 0x50C2, 0x6755, 0x1E03, 0x95F9, 0xECAF,
542*4882a593Smuzhiyun 0xB909, 0xC05F, 0x4BA5, 0x32F3, 0x0564, 0x7C32, 0xF7C8, 0x8E9E,
543*4882a593Smuzhiyun 0x98E6, 0xE1B0, 0x6A4A, 0x131C, 0x248B, 0x5DDD, 0xD627, 0xAF71,
544*4882a593Smuzhiyun 0x7D6B, 0x043D, 0x8FC7, 0xF691, 0xC106, 0xB850, 0x33AA, 0x4AFC,
545*4882a593Smuzhiyun 0x5C84, 0x25D2, 0xAE28, 0xD77E, 0xE0E9, 0x99BF, 0x1245, 0x6B13,
546*4882a593Smuzhiyun 0x3EB5, 0x47E3, 0xCC19, 0xB54F, 0x82D8, 0xFB8E, 0x7074, 0x0922,
547*4882a593Smuzhiyun 0x1F5A, 0x660C, 0xEDF6, 0x94A0, 0xA337, 0xDA61, 0x519B, 0x28CD };
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun * fm10k_crc_16b - Generate a 16 bit CRC for a region of 16 bit data
551*4882a593Smuzhiyun * @data: pointer to data to process
552*4882a593Smuzhiyun * @seed: seed value for CRC
553*4882a593Smuzhiyun * @len: length measured in 16 bits words
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * This function will generate a CRC based on the polynomial 0xAC9A and
556*4882a593Smuzhiyun * whatever value is stored in the seed variable. Note that this
557*4882a593Smuzhiyun * value inverts the local seed and the result in order to capture all
558*4882a593Smuzhiyun * leading and trailing zeros.
559*4882a593Smuzhiyun */
fm10k_crc_16b(const u32 * data,u16 seed,u16 len)560*4882a593Smuzhiyun static u16 fm10k_crc_16b(const u32 *data, u16 seed, u16 len)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun u32 result = seed;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun while (len--) {
565*4882a593Smuzhiyun result ^= *(data++);
566*4882a593Smuzhiyun result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
567*4882a593Smuzhiyun result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (!(len--))
570*4882a593Smuzhiyun break;
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
573*4882a593Smuzhiyun result = (result >> 8) ^ fm10k_crc_16b_table[result & 0xFF];
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun return (u16)result;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun * fm10k_fifo_crc - generate a CRC based off of FIFO data
581*4882a593Smuzhiyun * @fifo: pointer to FIFO
582*4882a593Smuzhiyun * @offset: offset point for start of FIFO
583*4882a593Smuzhiyun * @len: number of DWORDS words to process
584*4882a593Smuzhiyun * @seed: seed value for CRC
585*4882a593Smuzhiyun *
586*4882a593Smuzhiyun * This function generates a CRC for some region of the FIFO
587*4882a593Smuzhiyun **/
fm10k_fifo_crc(struct fm10k_mbx_fifo * fifo,u16 offset,u16 len,u16 seed)588*4882a593Smuzhiyun static u16 fm10k_fifo_crc(struct fm10k_mbx_fifo *fifo, u16 offset,
589*4882a593Smuzhiyun u16 len, u16 seed)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun u32 *data = fifo->buffer + offset;
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun /* track when we should cross the end of the FIFO */
594*4882a593Smuzhiyun offset = fifo->size - offset;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /* if we are in 2 blocks process the end of the FIFO first */
597*4882a593Smuzhiyun if (offset < len) {
598*4882a593Smuzhiyun seed = fm10k_crc_16b(data, seed, offset * 2);
599*4882a593Smuzhiyun data = fifo->buffer;
600*4882a593Smuzhiyun len -= offset;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /* process any remaining bits */
604*4882a593Smuzhiyun return fm10k_crc_16b(data, seed, len * 2);
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /**
608*4882a593Smuzhiyun * fm10k_mbx_update_local_crc - Update the local CRC for outgoing data
609*4882a593Smuzhiyun * @mbx: pointer to mailbox
610*4882a593Smuzhiyun * @head: head index provided by remote mailbox
611*4882a593Smuzhiyun *
612*4882a593Smuzhiyun * This function will generate the CRC for all data from the end of the
613*4882a593Smuzhiyun * last head update to the current one. It uses the result of the
614*4882a593Smuzhiyun * previous CRC as the seed for this update. The result is stored in
615*4882a593Smuzhiyun * mbx->local.
616*4882a593Smuzhiyun **/
fm10k_mbx_update_local_crc(struct fm10k_mbx_info * mbx,u16 head)617*4882a593Smuzhiyun static void fm10k_mbx_update_local_crc(struct fm10k_mbx_info *mbx, u16 head)
618*4882a593Smuzhiyun {
619*4882a593Smuzhiyun u16 len = mbx->tail_len - fm10k_mbx_index_len(mbx, head, mbx->tail);
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /* determine the offset for the start of the region to be pulled */
622*4882a593Smuzhiyun head = fm10k_fifo_head_offset(&mbx->tx, mbx->pulled);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun /* update local CRC to include all of the pulled data */
625*4882a593Smuzhiyun mbx->local = fm10k_fifo_crc(&mbx->tx, head, len, mbx->local);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun /**
629*4882a593Smuzhiyun * fm10k_mbx_verify_remote_crc - Verify the CRC is correct for current data
630*4882a593Smuzhiyun * @mbx: pointer to mailbox
631*4882a593Smuzhiyun *
632*4882a593Smuzhiyun * This function will take all data that has been provided from the remote
633*4882a593Smuzhiyun * end and generate a CRC for it. This is stored in mbx->remote. The
634*4882a593Smuzhiyun * CRC for the header is then computed and if the result is non-zero this
635*4882a593Smuzhiyun * is an error and we signal an error dropping all data and resetting the
636*4882a593Smuzhiyun * connection.
637*4882a593Smuzhiyun */
fm10k_mbx_verify_remote_crc(struct fm10k_mbx_info * mbx)638*4882a593Smuzhiyun static s32 fm10k_mbx_verify_remote_crc(struct fm10k_mbx_info *mbx)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->rx;
641*4882a593Smuzhiyun u16 len = mbx->head_len;
642*4882a593Smuzhiyun u16 offset = fm10k_fifo_tail_offset(fifo, mbx->pushed) - len;
643*4882a593Smuzhiyun u16 crc;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun /* update the remote CRC if new data has been received */
646*4882a593Smuzhiyun if (len)
647*4882a593Smuzhiyun mbx->remote = fm10k_fifo_crc(fifo, offset, len, mbx->remote);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* process the full header as we have to validate the CRC */
650*4882a593Smuzhiyun crc = fm10k_crc_16b(&mbx->mbx_hdr, mbx->remote, 1);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /* notify other end if we have a problem */
653*4882a593Smuzhiyun return crc ? FM10K_MBX_ERR_CRC : 0;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun /**
657*4882a593Smuzhiyun * fm10k_mbx_rx_ready - Indicates that a message is ready in the Rx FIFO
658*4882a593Smuzhiyun * @mbx: pointer to mailbox
659*4882a593Smuzhiyun *
660*4882a593Smuzhiyun * This function returns true if there is a message in the Rx FIFO to dequeue.
661*4882a593Smuzhiyun **/
fm10k_mbx_rx_ready(struct fm10k_mbx_info * mbx)662*4882a593Smuzhiyun static bool fm10k_mbx_rx_ready(struct fm10k_mbx_info *mbx)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun u16 msg_size = fm10k_fifo_head_len(&mbx->rx);
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun return msg_size && (fm10k_fifo_used(&mbx->rx) >= msg_size);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun /**
670*4882a593Smuzhiyun * fm10k_mbx_tx_ready - Indicates that the mailbox is in state ready for Tx
671*4882a593Smuzhiyun * @mbx: pointer to mailbox
672*4882a593Smuzhiyun * @len: verify free space is >= this value
673*4882a593Smuzhiyun *
674*4882a593Smuzhiyun * This function returns true if the mailbox is in a state ready to transmit.
675*4882a593Smuzhiyun **/
fm10k_mbx_tx_ready(struct fm10k_mbx_info * mbx,u16 len)676*4882a593Smuzhiyun static bool fm10k_mbx_tx_ready(struct fm10k_mbx_info *mbx, u16 len)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun u16 fifo_unused = fm10k_fifo_unused(&mbx->tx);
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun return (mbx->state == FM10K_STATE_OPEN) && (fifo_unused >= len);
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun /**
684*4882a593Smuzhiyun * fm10k_mbx_tx_complete - Indicates that the Tx FIFO has been emptied
685*4882a593Smuzhiyun * @mbx: pointer to mailbox
686*4882a593Smuzhiyun *
687*4882a593Smuzhiyun * This function returns true if the Tx FIFO is empty.
688*4882a593Smuzhiyun **/
fm10k_mbx_tx_complete(struct fm10k_mbx_info * mbx)689*4882a593Smuzhiyun static bool fm10k_mbx_tx_complete(struct fm10k_mbx_info *mbx)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun return fm10k_fifo_empty(&mbx->tx);
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun /**
695*4882a593Smuzhiyun * fm10k_mbx_deqeueue_rx - Dequeues the message from the head in the Rx FIFO
696*4882a593Smuzhiyun * @hw: pointer to hardware structure
697*4882a593Smuzhiyun * @mbx: pointer to mailbox
698*4882a593Smuzhiyun *
699*4882a593Smuzhiyun * This function dequeues messages and hands them off to the TLV parser.
700*4882a593Smuzhiyun * It will return the number of messages processed when called.
701*4882a593Smuzhiyun **/
fm10k_mbx_dequeue_rx(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)702*4882a593Smuzhiyun static u16 fm10k_mbx_dequeue_rx(struct fm10k_hw *hw,
703*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
704*4882a593Smuzhiyun {
705*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->rx;
706*4882a593Smuzhiyun s32 err;
707*4882a593Smuzhiyun u16 cnt;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun /* parse Rx messages out of the Rx FIFO to empty it */
710*4882a593Smuzhiyun for (cnt = 0; !fm10k_fifo_empty(fifo); cnt++) {
711*4882a593Smuzhiyun err = fm10k_tlv_msg_parse(hw, fifo->buffer + fifo->head,
712*4882a593Smuzhiyun mbx, mbx->msg_data);
713*4882a593Smuzhiyun if (err < 0)
714*4882a593Smuzhiyun mbx->rx_parse_err++;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun fm10k_fifo_head_drop(fifo);
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /* shift remaining bytes back to start of FIFO */
720*4882a593Smuzhiyun memmove(fifo->buffer, fifo->buffer + fifo->tail, mbx->pushed << 2);
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* shift head and tail based on the memory we moved */
723*4882a593Smuzhiyun fifo->tail -= fifo->head;
724*4882a593Smuzhiyun fifo->head = 0;
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun return cnt;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun /**
730*4882a593Smuzhiyun * fm10k_mbx_enqueue_tx - Enqueues the message to the tail of the Tx FIFO
731*4882a593Smuzhiyun * @hw: pointer to hardware structure
732*4882a593Smuzhiyun * @mbx: pointer to mailbox
733*4882a593Smuzhiyun * @msg: message array to read
734*4882a593Smuzhiyun *
735*4882a593Smuzhiyun * This function enqueues a message up to the size specified by the length
736*4882a593Smuzhiyun * contained in the first DWORD of the message and will place at the tail
737*4882a593Smuzhiyun * of the FIFO. It will return 0 on success, or a negative value on error.
738*4882a593Smuzhiyun **/
fm10k_mbx_enqueue_tx(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,const u32 * msg)739*4882a593Smuzhiyun static s32 fm10k_mbx_enqueue_tx(struct fm10k_hw *hw,
740*4882a593Smuzhiyun struct fm10k_mbx_info *mbx, const u32 *msg)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun u32 countdown = mbx->timeout;
743*4882a593Smuzhiyun s32 err;
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun switch (mbx->state) {
746*4882a593Smuzhiyun case FM10K_STATE_CLOSED:
747*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
748*4882a593Smuzhiyun return FM10K_MBX_ERR_NO_MBX;
749*4882a593Smuzhiyun default:
750*4882a593Smuzhiyun break;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun /* enqueue the message on the Tx FIFO */
754*4882a593Smuzhiyun err = fm10k_fifo_enqueue(&mbx->tx, msg);
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun /* if it failed give the FIFO a chance to drain */
757*4882a593Smuzhiyun while (err && countdown) {
758*4882a593Smuzhiyun countdown--;
759*4882a593Smuzhiyun udelay(mbx->udelay);
760*4882a593Smuzhiyun mbx->ops.process(hw, mbx);
761*4882a593Smuzhiyun err = fm10k_fifo_enqueue(&mbx->tx, msg);
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun /* if we failed treat the error */
765*4882a593Smuzhiyun if (err) {
766*4882a593Smuzhiyun mbx->timeout = 0;
767*4882a593Smuzhiyun mbx->tx_busy++;
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun /* begin processing message, ignore errors as this is just meant
771*4882a593Smuzhiyun * to start the mailbox flow so we are not concerned if there
772*4882a593Smuzhiyun * is a bad error, or the mailbox is already busy with a request
773*4882a593Smuzhiyun */
774*4882a593Smuzhiyun if (!mbx->tail_len)
775*4882a593Smuzhiyun mbx->ops.process(hw, mbx);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun return 0;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun /**
781*4882a593Smuzhiyun * fm10k_mbx_read - Copies the mbmem to local message buffer
782*4882a593Smuzhiyun * @hw: pointer to hardware structure
783*4882a593Smuzhiyun * @mbx: pointer to mailbox
784*4882a593Smuzhiyun *
785*4882a593Smuzhiyun * This function copies the message from the mbmem to the message array
786*4882a593Smuzhiyun **/
fm10k_mbx_read(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)787*4882a593Smuzhiyun static s32 fm10k_mbx_read(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun /* only allow one reader in here at a time */
790*4882a593Smuzhiyun if (mbx->mbx_hdr)
791*4882a593Smuzhiyun return FM10K_MBX_ERR_BUSY;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /* read to capture initial interrupt bits */
794*4882a593Smuzhiyun if (fm10k_read_reg(hw, mbx->mbx_reg) & FM10K_MBX_REQ_INTERRUPT)
795*4882a593Smuzhiyun mbx->mbx_lock = FM10K_MBX_ACK;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun /* write back interrupt bits to clear */
798*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbx_reg,
799*4882a593Smuzhiyun FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT);
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun /* read remote header */
802*4882a593Smuzhiyun mbx->mbx_hdr = fm10k_read_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun return 0;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun /**
808*4882a593Smuzhiyun * fm10k_mbx_write - Copies the local message buffer to mbmem
809*4882a593Smuzhiyun * @hw: pointer to hardware structure
810*4882a593Smuzhiyun * @mbx: pointer to mailbox
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * This function copies the message from the the message array to mbmem
813*4882a593Smuzhiyun **/
fm10k_mbx_write(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)814*4882a593Smuzhiyun static void fm10k_mbx_write(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun u32 mbmem = mbx->mbmem_reg;
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /* write new msg header to notify recipient of change */
819*4882a593Smuzhiyun fm10k_write_reg(hw, mbmem, mbx->mbx_hdr);
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun /* write mailbox to send interrupt */
822*4882a593Smuzhiyun if (mbx->mbx_lock)
823*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbx_reg, mbx->mbx_lock);
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /* we no longer are using the header so free it */
826*4882a593Smuzhiyun mbx->mbx_hdr = 0;
827*4882a593Smuzhiyun mbx->mbx_lock = 0;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun /**
831*4882a593Smuzhiyun * fm10k_mbx_create_connect_hdr - Generate a connect mailbox header
832*4882a593Smuzhiyun * @mbx: pointer to mailbox
833*4882a593Smuzhiyun *
834*4882a593Smuzhiyun * This function returns a connection mailbox header
835*4882a593Smuzhiyun **/
fm10k_mbx_create_connect_hdr(struct fm10k_mbx_info * mbx)836*4882a593Smuzhiyun static void fm10k_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_REQ;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_CONNECT, TYPE) |
841*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD) |
842*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->rx.size - 1, CONNECT_SIZE);
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun /**
846*4882a593Smuzhiyun * fm10k_mbx_create_data_hdr - Generate a data mailbox header
847*4882a593Smuzhiyun * @mbx: pointer to mailbox
848*4882a593Smuzhiyun *
849*4882a593Smuzhiyun * This function returns a data mailbox header
850*4882a593Smuzhiyun **/
fm10k_mbx_create_data_hdr(struct fm10k_mbx_info * mbx)851*4882a593Smuzhiyun static void fm10k_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DATA, TYPE) |
854*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
855*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
856*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->tx;
857*4882a593Smuzhiyun u16 crc;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun if (mbx->tail_len)
860*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_REQ;
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun /* generate CRC for data in flight and header */
863*4882a593Smuzhiyun crc = fm10k_fifo_crc(fifo, fm10k_fifo_head_offset(fifo, mbx->pulled),
864*4882a593Smuzhiyun mbx->tail_len, mbx->local);
865*4882a593Smuzhiyun crc = fm10k_crc_16b(&hdr, crc, 1);
866*4882a593Smuzhiyun
867*4882a593Smuzhiyun /* load header to memory to be written */
868*4882a593Smuzhiyun mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun /**
872*4882a593Smuzhiyun * fm10k_mbx_create_disconnect_hdr - Generate a disconnect mailbox header
873*4882a593Smuzhiyun * @mbx: pointer to mailbox
874*4882a593Smuzhiyun *
875*4882a593Smuzhiyun * This function returns a disconnect mailbox header
876*4882a593Smuzhiyun **/
fm10k_mbx_create_disconnect_hdr(struct fm10k_mbx_info * mbx)877*4882a593Smuzhiyun static void fm10k_mbx_create_disconnect_hdr(struct fm10k_mbx_info *mbx)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
880*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->tail, TAIL) |
881*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
882*4882a593Smuzhiyun u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_ACK;
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun /* load header to memory to be written */
887*4882a593Smuzhiyun mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun /**
891*4882a593Smuzhiyun * fm10k_mbx_create_fake_disconnect_hdr - Generate a false disconnect mbox hdr
892*4882a593Smuzhiyun * @mbx: pointer to mailbox
893*4882a593Smuzhiyun *
894*4882a593Smuzhiyun * This function creates a fake disconnect header for loading into remote
895*4882a593Smuzhiyun * mailbox header. The primary purpose is to prevent errors on immediate
896*4882a593Smuzhiyun * start up after mbx->connect.
897*4882a593Smuzhiyun **/
fm10k_mbx_create_fake_disconnect_hdr(struct fm10k_mbx_info * mbx)898*4882a593Smuzhiyun static void fm10k_mbx_create_fake_disconnect_hdr(struct fm10k_mbx_info *mbx)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun u32 hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_DISCONNECT, TYPE) |
901*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, TAIL) |
902*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->tail, HEAD);
903*4882a593Smuzhiyun u16 crc = fm10k_crc_16b(&hdr, mbx->local, 1);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_ACK;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun /* load header to memory to be written */
908*4882a593Smuzhiyun mbx->mbx_hdr = hdr | FM10K_MSG_HDR_FIELD_SET(crc, CRC);
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun /**
912*4882a593Smuzhiyun * fm10k_mbx_create_error_msg - Generate an error message
913*4882a593Smuzhiyun * @mbx: pointer to mailbox
914*4882a593Smuzhiyun * @err: local error encountered
915*4882a593Smuzhiyun *
916*4882a593Smuzhiyun * This function will interpret the error provided by err, and based on
917*4882a593Smuzhiyun * that it may shift the message by 1 DWORD and then place an error header
918*4882a593Smuzhiyun * at the start of the message.
919*4882a593Smuzhiyun **/
fm10k_mbx_create_error_msg(struct fm10k_mbx_info * mbx,s32 err)920*4882a593Smuzhiyun static void fm10k_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
921*4882a593Smuzhiyun {
922*4882a593Smuzhiyun /* only generate an error message for these types */
923*4882a593Smuzhiyun switch (err) {
924*4882a593Smuzhiyun case FM10K_MBX_ERR_TAIL:
925*4882a593Smuzhiyun case FM10K_MBX_ERR_HEAD:
926*4882a593Smuzhiyun case FM10K_MBX_ERR_TYPE:
927*4882a593Smuzhiyun case FM10K_MBX_ERR_SIZE:
928*4882a593Smuzhiyun case FM10K_MBX_ERR_RSVD0:
929*4882a593Smuzhiyun case FM10K_MBX_ERR_CRC:
930*4882a593Smuzhiyun break;
931*4882a593Smuzhiyun default:
932*4882a593Smuzhiyun return;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_REQ;
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(FM10K_MSG_ERROR, TYPE) |
938*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(err, ERR_NO) |
939*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, HEAD);
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun /**
943*4882a593Smuzhiyun * fm10k_mbx_validate_msg_hdr - Validate common fields in the message header
944*4882a593Smuzhiyun * @mbx: pointer to mailbox
945*4882a593Smuzhiyun *
946*4882a593Smuzhiyun * This function will parse up the fields in the mailbox header and return
947*4882a593Smuzhiyun * an error if the header contains any of a number of invalid configurations
948*4882a593Smuzhiyun * including unrecognized type, invalid route, or a malformed message.
949*4882a593Smuzhiyun **/
fm10k_mbx_validate_msg_hdr(struct fm10k_mbx_info * mbx)950*4882a593Smuzhiyun static s32 fm10k_mbx_validate_msg_hdr(struct fm10k_mbx_info *mbx)
951*4882a593Smuzhiyun {
952*4882a593Smuzhiyun u16 type, rsvd0, head, tail, size;
953*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun type = FM10K_MSG_HDR_FIELD_GET(*hdr, TYPE);
956*4882a593Smuzhiyun rsvd0 = FM10K_MSG_HDR_FIELD_GET(*hdr, RSVD0);
957*4882a593Smuzhiyun tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
958*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
959*4882a593Smuzhiyun size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun if (rsvd0)
962*4882a593Smuzhiyun return FM10K_MBX_ERR_RSVD0;
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun switch (type) {
965*4882a593Smuzhiyun case FM10K_MSG_DISCONNECT:
966*4882a593Smuzhiyun /* validate that all data has been received */
967*4882a593Smuzhiyun if (tail != mbx->head)
968*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun fallthrough;
971*4882a593Smuzhiyun case FM10K_MSG_DATA:
972*4882a593Smuzhiyun /* validate that head is moving correctly */
973*4882a593Smuzhiyun if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
974*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
975*4882a593Smuzhiyun if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
976*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun /* validate that tail is moving correctly */
979*4882a593Smuzhiyun if (!tail || (tail == FM10K_MSG_HDR_MASK(TAIL)))
980*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
981*4882a593Smuzhiyun if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
982*4882a593Smuzhiyun break;
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
985*4882a593Smuzhiyun case FM10K_MSG_CONNECT:
986*4882a593Smuzhiyun /* validate size is in range and is power of 2 mask */
987*4882a593Smuzhiyun if ((size < FM10K_VFMBX_MSG_MTU) || (size & (size + 1)))
988*4882a593Smuzhiyun return FM10K_MBX_ERR_SIZE;
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun fallthrough;
991*4882a593Smuzhiyun case FM10K_MSG_ERROR:
992*4882a593Smuzhiyun if (!head || (head == FM10K_MSG_HDR_MASK(HEAD)))
993*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
994*4882a593Smuzhiyun /* neither create nor error include a tail offset */
995*4882a593Smuzhiyun if (tail)
996*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun break;
999*4882a593Smuzhiyun default:
1000*4882a593Smuzhiyun return FM10K_MBX_ERR_TYPE;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun return 0;
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun /**
1007*4882a593Smuzhiyun * fm10k_mbx_create_reply - Generate reply based on state and remote head
1008*4882a593Smuzhiyun * @hw: pointer to hardware structure
1009*4882a593Smuzhiyun * @mbx: pointer to mailbox
1010*4882a593Smuzhiyun * @head: acknowledgement number
1011*4882a593Smuzhiyun *
1012*4882a593Smuzhiyun * This function will generate an outgoing message based on the current
1013*4882a593Smuzhiyun * mailbox state and the remote FIFO head. It will return the length
1014*4882a593Smuzhiyun * of the outgoing message excluding header on success, and a negative value
1015*4882a593Smuzhiyun * on error.
1016*4882a593Smuzhiyun **/
fm10k_mbx_create_reply(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 head)1017*4882a593Smuzhiyun static s32 fm10k_mbx_create_reply(struct fm10k_hw *hw,
1018*4882a593Smuzhiyun struct fm10k_mbx_info *mbx, u16 head)
1019*4882a593Smuzhiyun {
1020*4882a593Smuzhiyun switch (mbx->state) {
1021*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1022*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1023*4882a593Smuzhiyun /* update our checksum for the outgoing data */
1024*4882a593Smuzhiyun fm10k_mbx_update_local_crc(mbx, head);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun /* as long as other end recognizes us keep sending data */
1027*4882a593Smuzhiyun fm10k_mbx_pull_head(hw, mbx, head);
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun /* generate new header based on data */
1030*4882a593Smuzhiyun if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN))
1031*4882a593Smuzhiyun fm10k_mbx_create_data_hdr(mbx);
1032*4882a593Smuzhiyun else
1033*4882a593Smuzhiyun fm10k_mbx_create_disconnect_hdr(mbx);
1034*4882a593Smuzhiyun break;
1035*4882a593Smuzhiyun case FM10K_STATE_CONNECT:
1036*4882a593Smuzhiyun /* send disconnect even if we aren't connected */
1037*4882a593Smuzhiyun fm10k_mbx_create_connect_hdr(mbx);
1038*4882a593Smuzhiyun break;
1039*4882a593Smuzhiyun case FM10K_STATE_CLOSED:
1040*4882a593Smuzhiyun /* generate new header based on data */
1041*4882a593Smuzhiyun fm10k_mbx_create_disconnect_hdr(mbx);
1042*4882a593Smuzhiyun default:
1043*4882a593Smuzhiyun break;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun return 0;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun /**
1050*4882a593Smuzhiyun * fm10k_mbx_reset_work- Reset internal pointers for any pending work
1051*4882a593Smuzhiyun * @mbx: pointer to mailbox
1052*4882a593Smuzhiyun *
1053*4882a593Smuzhiyun * This function will reset all internal pointers so any work in progress
1054*4882a593Smuzhiyun * is dropped. This call should occur every time we transition from the
1055*4882a593Smuzhiyun * open state to the connect state.
1056*4882a593Smuzhiyun **/
fm10k_mbx_reset_work(struct fm10k_mbx_info * mbx)1057*4882a593Smuzhiyun static void fm10k_mbx_reset_work(struct fm10k_mbx_info *mbx)
1058*4882a593Smuzhiyun {
1059*4882a593Smuzhiyun u16 len, head, ack;
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun /* reset our outgoing max size back to Rx limits */
1062*4882a593Smuzhiyun mbx->max_size = mbx->rx.size - 1;
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /* update mbx->pulled to account for tail_len and ack */
1065*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, HEAD);
1066*4882a593Smuzhiyun ack = fm10k_mbx_index_len(mbx, head, mbx->tail);
1067*4882a593Smuzhiyun mbx->pulled += mbx->tail_len - ack;
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun /* now drop any messages which have started or finished transmitting */
1070*4882a593Smuzhiyun while (fm10k_fifo_head_len(&mbx->tx) && mbx->pulled) {
1071*4882a593Smuzhiyun len = fm10k_fifo_head_drop(&mbx->tx);
1072*4882a593Smuzhiyun mbx->tx_dropped++;
1073*4882a593Smuzhiyun if (mbx->pulled >= len)
1074*4882a593Smuzhiyun mbx->pulled -= len;
1075*4882a593Smuzhiyun else
1076*4882a593Smuzhiyun mbx->pulled = 0;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun /* just do a quick resysnc to start of message */
1080*4882a593Smuzhiyun mbx->pushed = 0;
1081*4882a593Smuzhiyun mbx->pulled = 0;
1082*4882a593Smuzhiyun mbx->tail_len = 0;
1083*4882a593Smuzhiyun mbx->head_len = 0;
1084*4882a593Smuzhiyun mbx->rx.tail = 0;
1085*4882a593Smuzhiyun mbx->rx.head = 0;
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun /**
1089*4882a593Smuzhiyun * fm10k_mbx_update_max_size - Update the max_size and drop any large messages
1090*4882a593Smuzhiyun * @mbx: pointer to mailbox
1091*4882a593Smuzhiyun * @size: new value for max_size
1092*4882a593Smuzhiyun *
1093*4882a593Smuzhiyun * This function updates the max_size value and drops any outgoing messages
1094*4882a593Smuzhiyun * at the head of the Tx FIFO if they are larger than max_size. It does not
1095*4882a593Smuzhiyun * drop all messages, as this is too difficult to parse and remove them from
1096*4882a593Smuzhiyun * the FIFO. Instead, rely on the checking to ensure that messages larger
1097*4882a593Smuzhiyun * than max_size aren't pushed into the memory buffer.
1098*4882a593Smuzhiyun **/
fm10k_mbx_update_max_size(struct fm10k_mbx_info * mbx,u16 size)1099*4882a593Smuzhiyun static void fm10k_mbx_update_max_size(struct fm10k_mbx_info *mbx, u16 size)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun u16 len;
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun mbx->max_size = size;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun /* flush any oversized messages from the queue */
1106*4882a593Smuzhiyun for (len = fm10k_fifo_head_len(&mbx->tx);
1107*4882a593Smuzhiyun len > size;
1108*4882a593Smuzhiyun len = fm10k_fifo_head_len(&mbx->tx)) {
1109*4882a593Smuzhiyun fm10k_fifo_head_drop(&mbx->tx);
1110*4882a593Smuzhiyun mbx->tx_dropped++;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun /**
1115*4882a593Smuzhiyun * fm10k_mbx_connect_reset - Reset following request for reset
1116*4882a593Smuzhiyun * @mbx: pointer to mailbox
1117*4882a593Smuzhiyun *
1118*4882a593Smuzhiyun * This function resets the mailbox to either a disconnected state
1119*4882a593Smuzhiyun * or a connect state depending on the current mailbox state
1120*4882a593Smuzhiyun **/
fm10k_mbx_connect_reset(struct fm10k_mbx_info * mbx)1121*4882a593Smuzhiyun static void fm10k_mbx_connect_reset(struct fm10k_mbx_info *mbx)
1122*4882a593Smuzhiyun {
1123*4882a593Smuzhiyun /* just do a quick resysnc to start of frame */
1124*4882a593Smuzhiyun fm10k_mbx_reset_work(mbx);
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun /* reset CRC seeds */
1127*4882a593Smuzhiyun mbx->local = FM10K_MBX_CRC_SEED;
1128*4882a593Smuzhiyun mbx->remote = FM10K_MBX_CRC_SEED;
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun /* we cannot exit connect until the size is good */
1131*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_OPEN)
1132*4882a593Smuzhiyun mbx->state = FM10K_STATE_CONNECT;
1133*4882a593Smuzhiyun else
1134*4882a593Smuzhiyun mbx->state = FM10K_STATE_CLOSED;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun /**
1138*4882a593Smuzhiyun * fm10k_mbx_process_connect - Process connect header
1139*4882a593Smuzhiyun * @hw: pointer to hardware structure
1140*4882a593Smuzhiyun * @mbx: pointer to mailbox
1141*4882a593Smuzhiyun *
1142*4882a593Smuzhiyun * This function will read an incoming connect header and reply with the
1143*4882a593Smuzhiyun * appropriate message. It will return a value indicating the number of
1144*4882a593Smuzhiyun * data DWORDs on success, or will return a negative value on failure.
1145*4882a593Smuzhiyun **/
fm10k_mbx_process_connect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1146*4882a593Smuzhiyun static s32 fm10k_mbx_process_connect(struct fm10k_hw *hw,
1147*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun const enum fm10k_mbx_state state = mbx->state;
1150*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
1151*4882a593Smuzhiyun u16 size, head;
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun /* we will need to pull all of the fields for verification */
1154*4882a593Smuzhiyun size = FM10K_MSG_HDR_FIELD_GET(*hdr, CONNECT_SIZE);
1155*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun switch (state) {
1158*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1159*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1160*4882a593Smuzhiyun /* reset any in-progress work */
1161*4882a593Smuzhiyun fm10k_mbx_connect_reset(mbx);
1162*4882a593Smuzhiyun break;
1163*4882a593Smuzhiyun case FM10K_STATE_CONNECT:
1164*4882a593Smuzhiyun /* we cannot exit connect until the size is good */
1165*4882a593Smuzhiyun if (size > mbx->rx.size) {
1166*4882a593Smuzhiyun mbx->max_size = mbx->rx.size - 1;
1167*4882a593Smuzhiyun } else {
1168*4882a593Smuzhiyun /* record the remote system requesting connection */
1169*4882a593Smuzhiyun mbx->state = FM10K_STATE_OPEN;
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun fm10k_mbx_update_max_size(mbx, size);
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun break;
1174*4882a593Smuzhiyun default:
1175*4882a593Smuzhiyun break;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun /* align our tail index to remote head index */
1179*4882a593Smuzhiyun mbx->tail = head;
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun return fm10k_mbx_create_reply(hw, mbx, head);
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun /**
1185*4882a593Smuzhiyun * fm10k_mbx_process_data - Process data header
1186*4882a593Smuzhiyun * @hw: pointer to hardware structure
1187*4882a593Smuzhiyun * @mbx: pointer to mailbox
1188*4882a593Smuzhiyun *
1189*4882a593Smuzhiyun * This function will read an incoming data header and reply with the
1190*4882a593Smuzhiyun * appropriate message. It will return a value indicating the number of
1191*4882a593Smuzhiyun * data DWORDs on success, or will return a negative value on failure.
1192*4882a593Smuzhiyun **/
fm10k_mbx_process_data(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1193*4882a593Smuzhiyun static s32 fm10k_mbx_process_data(struct fm10k_hw *hw,
1194*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
1197*4882a593Smuzhiyun u16 head, tail;
1198*4882a593Smuzhiyun s32 err;
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun /* we will need to pull all of the fields for verification */
1201*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
1202*4882a593Smuzhiyun tail = FM10K_MSG_HDR_FIELD_GET(*hdr, TAIL);
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun /* if we are in connect just update our data and go */
1205*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_CONNECT) {
1206*4882a593Smuzhiyun mbx->tail = head;
1207*4882a593Smuzhiyun mbx->state = FM10K_STATE_OPEN;
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun
1210*4882a593Smuzhiyun /* abort on message size errors */
1211*4882a593Smuzhiyun err = fm10k_mbx_push_tail(hw, mbx, tail);
1212*4882a593Smuzhiyun if (err < 0)
1213*4882a593Smuzhiyun return err;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun /* verify the checksum on the incoming data */
1216*4882a593Smuzhiyun err = fm10k_mbx_verify_remote_crc(mbx);
1217*4882a593Smuzhiyun if (err)
1218*4882a593Smuzhiyun return err;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun /* process messages if we have received any */
1221*4882a593Smuzhiyun fm10k_mbx_dequeue_rx(hw, mbx);
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun return fm10k_mbx_create_reply(hw, mbx, head);
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun /**
1227*4882a593Smuzhiyun * fm10k_mbx_process_disconnect - Process disconnect header
1228*4882a593Smuzhiyun * @hw: pointer to hardware structure
1229*4882a593Smuzhiyun * @mbx: pointer to mailbox
1230*4882a593Smuzhiyun *
1231*4882a593Smuzhiyun * This function will read an incoming disconnect header and reply with the
1232*4882a593Smuzhiyun * appropriate message. It will return a value indicating the number of
1233*4882a593Smuzhiyun * data DWORDs on success, or will return a negative value on failure.
1234*4882a593Smuzhiyun **/
fm10k_mbx_process_disconnect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1235*4882a593Smuzhiyun static s32 fm10k_mbx_process_disconnect(struct fm10k_hw *hw,
1236*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun const enum fm10k_mbx_state state = mbx->state;
1239*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
1240*4882a593Smuzhiyun u16 head;
1241*4882a593Smuzhiyun s32 err;
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun /* we will need to pull the header field for verification */
1244*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun /* We should not be receiving disconnect if Rx is incomplete */
1247*4882a593Smuzhiyun if (mbx->pushed)
1248*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun /* we have already verified mbx->head == tail so we know this is 0 */
1251*4882a593Smuzhiyun mbx->head_len = 0;
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun /* verify the checksum on the incoming header is correct */
1254*4882a593Smuzhiyun err = fm10k_mbx_verify_remote_crc(mbx);
1255*4882a593Smuzhiyun if (err)
1256*4882a593Smuzhiyun return err;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun switch (state) {
1259*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1260*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1261*4882a593Smuzhiyun /* state doesn't change if we still have work to do */
1262*4882a593Smuzhiyun if (!fm10k_mbx_tx_complete(mbx))
1263*4882a593Smuzhiyun break;
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun /* verify the head indicates we completed all transmits */
1266*4882a593Smuzhiyun if (head != mbx->tail)
1267*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun /* reset any in-progress work */
1270*4882a593Smuzhiyun fm10k_mbx_connect_reset(mbx);
1271*4882a593Smuzhiyun break;
1272*4882a593Smuzhiyun default:
1273*4882a593Smuzhiyun break;
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun return fm10k_mbx_create_reply(hw, mbx, head);
1277*4882a593Smuzhiyun }
1278*4882a593Smuzhiyun
1279*4882a593Smuzhiyun /**
1280*4882a593Smuzhiyun * fm10k_mbx_process_error - Process error header
1281*4882a593Smuzhiyun * @hw: pointer to hardware structure
1282*4882a593Smuzhiyun * @mbx: pointer to mailbox
1283*4882a593Smuzhiyun *
1284*4882a593Smuzhiyun * This function will read an incoming error header and reply with the
1285*4882a593Smuzhiyun * appropriate message. It will return a value indicating the number of
1286*4882a593Smuzhiyun * data DWORDs on success, or will return a negative value on failure.
1287*4882a593Smuzhiyun **/
fm10k_mbx_process_error(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1288*4882a593Smuzhiyun static s32 fm10k_mbx_process_error(struct fm10k_hw *hw,
1289*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1290*4882a593Smuzhiyun {
1291*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
1292*4882a593Smuzhiyun u16 head;
1293*4882a593Smuzhiyun
1294*4882a593Smuzhiyun /* we will need to pull all of the fields for verification */
1295*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, HEAD);
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun switch (mbx->state) {
1298*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1299*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1300*4882a593Smuzhiyun /* flush any uncompleted work */
1301*4882a593Smuzhiyun fm10k_mbx_reset_work(mbx);
1302*4882a593Smuzhiyun
1303*4882a593Smuzhiyun /* reset CRC seeds */
1304*4882a593Smuzhiyun mbx->local = FM10K_MBX_CRC_SEED;
1305*4882a593Smuzhiyun mbx->remote = FM10K_MBX_CRC_SEED;
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun /* reset tail index and size to prepare for reconnect */
1308*4882a593Smuzhiyun mbx->tail = head;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun /* if open then reset max_size and go back to connect */
1311*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_OPEN) {
1312*4882a593Smuzhiyun mbx->state = FM10K_STATE_CONNECT;
1313*4882a593Smuzhiyun break;
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun /* send a connect message to get data flowing again */
1317*4882a593Smuzhiyun fm10k_mbx_create_connect_hdr(mbx);
1318*4882a593Smuzhiyun return 0;
1319*4882a593Smuzhiyun default:
1320*4882a593Smuzhiyun break;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun return fm10k_mbx_create_reply(hw, mbx, mbx->tail);
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun /**
1327*4882a593Smuzhiyun * fm10k_mbx_process - Process mailbox interrupt
1328*4882a593Smuzhiyun * @hw: pointer to hardware structure
1329*4882a593Smuzhiyun * @mbx: pointer to mailbox
1330*4882a593Smuzhiyun *
1331*4882a593Smuzhiyun * This function will process incoming mailbox events and generate mailbox
1332*4882a593Smuzhiyun * replies. It will return a value indicating the number of DWORDs
1333*4882a593Smuzhiyun * transmitted excluding header on success or a negative value on error.
1334*4882a593Smuzhiyun **/
fm10k_mbx_process(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1335*4882a593Smuzhiyun static s32 fm10k_mbx_process(struct fm10k_hw *hw,
1336*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1337*4882a593Smuzhiyun {
1338*4882a593Smuzhiyun s32 err;
1339*4882a593Smuzhiyun
1340*4882a593Smuzhiyun /* we do not read mailbox if closed */
1341*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_CLOSED)
1342*4882a593Smuzhiyun return 0;
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun /* copy data from mailbox */
1345*4882a593Smuzhiyun err = fm10k_mbx_read(hw, mbx);
1346*4882a593Smuzhiyun if (err)
1347*4882a593Smuzhiyun return err;
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun /* validate type, source, and destination */
1350*4882a593Smuzhiyun err = fm10k_mbx_validate_msg_hdr(mbx);
1351*4882a593Smuzhiyun if (err < 0)
1352*4882a593Smuzhiyun goto msg_err;
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, TYPE)) {
1355*4882a593Smuzhiyun case FM10K_MSG_CONNECT:
1356*4882a593Smuzhiyun err = fm10k_mbx_process_connect(hw, mbx);
1357*4882a593Smuzhiyun break;
1358*4882a593Smuzhiyun case FM10K_MSG_DATA:
1359*4882a593Smuzhiyun err = fm10k_mbx_process_data(hw, mbx);
1360*4882a593Smuzhiyun break;
1361*4882a593Smuzhiyun case FM10K_MSG_DISCONNECT:
1362*4882a593Smuzhiyun err = fm10k_mbx_process_disconnect(hw, mbx);
1363*4882a593Smuzhiyun break;
1364*4882a593Smuzhiyun case FM10K_MSG_ERROR:
1365*4882a593Smuzhiyun err = fm10k_mbx_process_error(hw, mbx);
1366*4882a593Smuzhiyun break;
1367*4882a593Smuzhiyun default:
1368*4882a593Smuzhiyun err = FM10K_MBX_ERR_TYPE;
1369*4882a593Smuzhiyun break;
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun msg_err:
1373*4882a593Smuzhiyun /* notify partner of errors on our end */
1374*4882a593Smuzhiyun if (err < 0)
1375*4882a593Smuzhiyun fm10k_mbx_create_error_msg(mbx, err);
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun /* copy data from mailbox */
1378*4882a593Smuzhiyun fm10k_mbx_write(hw, mbx);
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun return err;
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun /**
1384*4882a593Smuzhiyun * fm10k_mbx_disconnect - Shutdown mailbox connection
1385*4882a593Smuzhiyun * @hw: pointer to hardware structure
1386*4882a593Smuzhiyun * @mbx: pointer to mailbox
1387*4882a593Smuzhiyun *
1388*4882a593Smuzhiyun * This function will shut down the mailbox. It places the mailbox first
1389*4882a593Smuzhiyun * in the disconnect state, it then allows up to a predefined timeout for
1390*4882a593Smuzhiyun * the mailbox to transition to close on its own. If this does not occur
1391*4882a593Smuzhiyun * then the mailbox will be forced into the closed state.
1392*4882a593Smuzhiyun *
1393*4882a593Smuzhiyun * Any mailbox transactions not completed before calling this function
1394*4882a593Smuzhiyun * are not guaranteed to complete and may be dropped.
1395*4882a593Smuzhiyun **/
fm10k_mbx_disconnect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1396*4882a593Smuzhiyun static void fm10k_mbx_disconnect(struct fm10k_hw *hw,
1397*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1398*4882a593Smuzhiyun {
1399*4882a593Smuzhiyun int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
1400*4882a593Smuzhiyun
1401*4882a593Smuzhiyun /* Place mbx in ready to disconnect state */
1402*4882a593Smuzhiyun mbx->state = FM10K_STATE_DISCONNECT;
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun /* trigger interrupt to start shutdown process */
1405*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
1406*4882a593Smuzhiyun FM10K_MBX_INTERRUPT_DISABLE);
1407*4882a593Smuzhiyun do {
1408*4882a593Smuzhiyun udelay(FM10K_MBX_POLL_DELAY);
1409*4882a593Smuzhiyun mbx->ops.process(hw, mbx);
1410*4882a593Smuzhiyun timeout -= FM10K_MBX_POLL_DELAY;
1411*4882a593Smuzhiyun } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun /* in case we didn't close, just force the mailbox into shutdown and
1414*4882a593Smuzhiyun * drop all left over messages in the FIFO.
1415*4882a593Smuzhiyun */
1416*4882a593Smuzhiyun fm10k_mbx_connect_reset(mbx);
1417*4882a593Smuzhiyun fm10k_fifo_drop_all(&mbx->tx);
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbmem_reg, 0);
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun /**
1423*4882a593Smuzhiyun * fm10k_mbx_connect - Start mailbox connection
1424*4882a593Smuzhiyun * @hw: pointer to hardware structure
1425*4882a593Smuzhiyun * @mbx: pointer to mailbox
1426*4882a593Smuzhiyun *
1427*4882a593Smuzhiyun * This function will initiate a mailbox connection. It will populate the
1428*4882a593Smuzhiyun * mailbox with a broadcast connect message and then initialize the lock.
1429*4882a593Smuzhiyun * This is safe since the connect message is a single DWORD so the mailbox
1430*4882a593Smuzhiyun * transaction is guaranteed to be atomic.
1431*4882a593Smuzhiyun *
1432*4882a593Smuzhiyun * This function will return an error if the mailbox has not been initiated
1433*4882a593Smuzhiyun * or is currently in use.
1434*4882a593Smuzhiyun **/
fm10k_mbx_connect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1435*4882a593Smuzhiyun static s32 fm10k_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
1436*4882a593Smuzhiyun {
1437*4882a593Smuzhiyun /* we cannot connect an uninitialized mailbox */
1438*4882a593Smuzhiyun if (!mbx->rx.buffer)
1439*4882a593Smuzhiyun return FM10K_MBX_ERR_NO_SPACE;
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun /* we cannot connect an already connected mailbox */
1442*4882a593Smuzhiyun if (mbx->state != FM10K_STATE_CLOSED)
1443*4882a593Smuzhiyun return FM10K_MBX_ERR_BUSY;
1444*4882a593Smuzhiyun
1445*4882a593Smuzhiyun /* mailbox timeout can now become active */
1446*4882a593Smuzhiyun mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
1447*4882a593Smuzhiyun
1448*4882a593Smuzhiyun /* Place mbx in ready to connect state */
1449*4882a593Smuzhiyun mbx->state = FM10K_STATE_CONNECT;
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun fm10k_mbx_reset_work(mbx);
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun /* initialize header of remote mailbox */
1454*4882a593Smuzhiyun fm10k_mbx_create_fake_disconnect_hdr(mbx);
1455*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbmem_reg ^ mbx->mbmem_len, mbx->mbx_hdr);
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun /* enable interrupt and notify other party of new message */
1458*4882a593Smuzhiyun mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
1459*4882a593Smuzhiyun FM10K_MBX_INTERRUPT_ENABLE;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun /* generate and load connect header into mailbox */
1462*4882a593Smuzhiyun fm10k_mbx_create_connect_hdr(mbx);
1463*4882a593Smuzhiyun fm10k_mbx_write(hw, mbx);
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun return 0;
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun /**
1469*4882a593Smuzhiyun * fm10k_mbx_validate_handlers - Validate layout of message parsing data
1470*4882a593Smuzhiyun * @msg_data: handlers for mailbox events
1471*4882a593Smuzhiyun *
1472*4882a593Smuzhiyun * This function validates the layout of the message parsing data. This
1473*4882a593Smuzhiyun * should be mostly static, but it is important to catch any errors that
1474*4882a593Smuzhiyun * are made when constructing the parsers.
1475*4882a593Smuzhiyun **/
fm10k_mbx_validate_handlers(const struct fm10k_msg_data * msg_data)1476*4882a593Smuzhiyun static s32 fm10k_mbx_validate_handlers(const struct fm10k_msg_data *msg_data)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun const struct fm10k_tlv_attr *attr;
1479*4882a593Smuzhiyun unsigned int id;
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun /* Allow NULL mailboxes that transmit but don't receive */
1482*4882a593Smuzhiyun if (!msg_data)
1483*4882a593Smuzhiyun return 0;
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun while (msg_data->id != FM10K_TLV_ERROR) {
1486*4882a593Smuzhiyun /* all messages should have a function handler */
1487*4882a593Smuzhiyun if (!msg_data->func)
1488*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun /* parser is optional */
1491*4882a593Smuzhiyun attr = msg_data->attr;
1492*4882a593Smuzhiyun if (attr) {
1493*4882a593Smuzhiyun while (attr->id != FM10K_TLV_ERROR) {
1494*4882a593Smuzhiyun id = attr->id;
1495*4882a593Smuzhiyun attr++;
1496*4882a593Smuzhiyun /* ID should always be increasing */
1497*4882a593Smuzhiyun if (id >= attr->id)
1498*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1499*4882a593Smuzhiyun /* ID should fit in results array */
1500*4882a593Smuzhiyun if (id >= FM10K_TLV_RESULTS_MAX)
1501*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun /* verify terminator is in the list */
1505*4882a593Smuzhiyun if (attr->id != FM10K_TLV_ERROR)
1506*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1507*4882a593Smuzhiyun }
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun id = msg_data->id;
1510*4882a593Smuzhiyun msg_data++;
1511*4882a593Smuzhiyun /* ID should always be increasing */
1512*4882a593Smuzhiyun if (id >= msg_data->id)
1513*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun
1516*4882a593Smuzhiyun /* verify terminator is in the list */
1517*4882a593Smuzhiyun if ((msg_data->id != FM10K_TLV_ERROR) || !msg_data->func)
1518*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun return 0;
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun /**
1524*4882a593Smuzhiyun * fm10k_mbx_register_handlers - Register a set of handler ops for mailbox
1525*4882a593Smuzhiyun * @mbx: pointer to mailbox
1526*4882a593Smuzhiyun * @msg_data: handlers for mailbox events
1527*4882a593Smuzhiyun *
1528*4882a593Smuzhiyun * This function associates a set of message handling ops with a mailbox.
1529*4882a593Smuzhiyun **/
fm10k_mbx_register_handlers(struct fm10k_mbx_info * mbx,const struct fm10k_msg_data * msg_data)1530*4882a593Smuzhiyun static s32 fm10k_mbx_register_handlers(struct fm10k_mbx_info *mbx,
1531*4882a593Smuzhiyun const struct fm10k_msg_data *msg_data)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun /* validate layout of handlers before assigning them */
1534*4882a593Smuzhiyun if (fm10k_mbx_validate_handlers(msg_data))
1535*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun /* initialize the message handlers */
1538*4882a593Smuzhiyun mbx->msg_data = msg_data;
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun return 0;
1541*4882a593Smuzhiyun }
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun /**
1544*4882a593Smuzhiyun * fm10k_pfvf_mbx_init - Initialize mailbox memory for PF/VF mailbox
1545*4882a593Smuzhiyun * @hw: pointer to hardware structure
1546*4882a593Smuzhiyun * @mbx: pointer to mailbox
1547*4882a593Smuzhiyun * @msg_data: handlers for mailbox events
1548*4882a593Smuzhiyun * @id: ID reference for PF as it supports up to 64 PF/VF mailboxes
1549*4882a593Smuzhiyun *
1550*4882a593Smuzhiyun * This function initializes the mailbox for use. It will split the
1551*4882a593Smuzhiyun * buffer provided and use that to populate both the Tx and Rx FIFO by
1552*4882a593Smuzhiyun * evenly splitting it. In order to allow for easy masking of head/tail
1553*4882a593Smuzhiyun * the value reported in size must be a power of 2 and is reported in
1554*4882a593Smuzhiyun * DWORDs, not bytes. Any invalid values will cause the mailbox to return
1555*4882a593Smuzhiyun * error.
1556*4882a593Smuzhiyun **/
fm10k_pfvf_mbx_init(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,const struct fm10k_msg_data * msg_data,u8 id)1557*4882a593Smuzhiyun s32 fm10k_pfvf_mbx_init(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx,
1558*4882a593Smuzhiyun const struct fm10k_msg_data *msg_data, u8 id)
1559*4882a593Smuzhiyun {
1560*4882a593Smuzhiyun /* initialize registers */
1561*4882a593Smuzhiyun switch (hw->mac.type) {
1562*4882a593Smuzhiyun case fm10k_mac_vf:
1563*4882a593Smuzhiyun mbx->mbx_reg = FM10K_VFMBX;
1564*4882a593Smuzhiyun mbx->mbmem_reg = FM10K_VFMBMEM(FM10K_VFMBMEM_VF_XOR);
1565*4882a593Smuzhiyun break;
1566*4882a593Smuzhiyun case fm10k_mac_pf:
1567*4882a593Smuzhiyun /* there are only 64 VF <-> PF mailboxes */
1568*4882a593Smuzhiyun if (id < 64) {
1569*4882a593Smuzhiyun mbx->mbx_reg = FM10K_MBX(id);
1570*4882a593Smuzhiyun mbx->mbmem_reg = FM10K_MBMEM_VF(id, 0);
1571*4882a593Smuzhiyun break;
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun fallthrough;
1574*4882a593Smuzhiyun default:
1575*4882a593Smuzhiyun return FM10K_MBX_ERR_NO_MBX;
1576*4882a593Smuzhiyun }
1577*4882a593Smuzhiyun
1578*4882a593Smuzhiyun /* start out in closed state */
1579*4882a593Smuzhiyun mbx->state = FM10K_STATE_CLOSED;
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun /* validate layout of handlers before assigning them */
1582*4882a593Smuzhiyun if (fm10k_mbx_validate_handlers(msg_data))
1583*4882a593Smuzhiyun return FM10K_ERR_PARAM;
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun /* initialize the message handlers */
1586*4882a593Smuzhiyun mbx->msg_data = msg_data;
1587*4882a593Smuzhiyun
1588*4882a593Smuzhiyun /* start mailbox as timed out and let the reset_hw call
1589*4882a593Smuzhiyun * set the timeout value to begin communications
1590*4882a593Smuzhiyun */
1591*4882a593Smuzhiyun mbx->timeout = 0;
1592*4882a593Smuzhiyun mbx->udelay = FM10K_MBX_INIT_DELAY;
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun /* initialize tail and head */
1595*4882a593Smuzhiyun mbx->tail = 1;
1596*4882a593Smuzhiyun mbx->head = 1;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun /* initialize CRC seeds */
1599*4882a593Smuzhiyun mbx->local = FM10K_MBX_CRC_SEED;
1600*4882a593Smuzhiyun mbx->remote = FM10K_MBX_CRC_SEED;
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun /* Split buffer for use by Tx/Rx FIFOs */
1603*4882a593Smuzhiyun mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
1604*4882a593Smuzhiyun mbx->mbmem_len = FM10K_VFMBMEM_VF_XOR;
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun /* initialize the FIFOs, sizes are in 4 byte increments */
1607*4882a593Smuzhiyun fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
1608*4882a593Smuzhiyun fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
1609*4882a593Smuzhiyun FM10K_MBX_RX_BUFFER_SIZE);
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun /* initialize function pointers */
1612*4882a593Smuzhiyun mbx->ops.connect = fm10k_mbx_connect;
1613*4882a593Smuzhiyun mbx->ops.disconnect = fm10k_mbx_disconnect;
1614*4882a593Smuzhiyun mbx->ops.rx_ready = fm10k_mbx_rx_ready;
1615*4882a593Smuzhiyun mbx->ops.tx_ready = fm10k_mbx_tx_ready;
1616*4882a593Smuzhiyun mbx->ops.tx_complete = fm10k_mbx_tx_complete;
1617*4882a593Smuzhiyun mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
1618*4882a593Smuzhiyun mbx->ops.process = fm10k_mbx_process;
1619*4882a593Smuzhiyun mbx->ops.register_handlers = fm10k_mbx_register_handlers;
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun return 0;
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun /**
1625*4882a593Smuzhiyun * fm10k_sm_mbx_create_data_hdr - Generate a mailbox header for local FIFO
1626*4882a593Smuzhiyun * @mbx: pointer to mailbox
1627*4882a593Smuzhiyun *
1628*4882a593Smuzhiyun * This function returns a data mailbox header
1629*4882a593Smuzhiyun **/
fm10k_sm_mbx_create_data_hdr(struct fm10k_mbx_info * mbx)1630*4882a593Smuzhiyun static void fm10k_sm_mbx_create_data_hdr(struct fm10k_mbx_info *mbx)
1631*4882a593Smuzhiyun {
1632*4882a593Smuzhiyun if (mbx->tail_len)
1633*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_REQ;
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
1636*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
1637*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD);
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun /**
1641*4882a593Smuzhiyun * fm10k_sm_mbx_create_connect_hdr - Generate a mailbox header for local FIFO
1642*4882a593Smuzhiyun * @mbx: pointer to mailbox
1643*4882a593Smuzhiyun * @err: error flags to report if any
1644*4882a593Smuzhiyun *
1645*4882a593Smuzhiyun * This function returns a connection mailbox header
1646*4882a593Smuzhiyun **/
fm10k_sm_mbx_create_connect_hdr(struct fm10k_mbx_info * mbx,u8 err)1647*4882a593Smuzhiyun static void fm10k_sm_mbx_create_connect_hdr(struct fm10k_mbx_info *mbx, u8 err)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun if (mbx->local)
1650*4882a593Smuzhiyun mbx->mbx_lock |= FM10K_MBX_REQ;
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun mbx->mbx_hdr = FM10K_MSG_HDR_FIELD_SET(mbx->tail, SM_TAIL) |
1653*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->remote, SM_VER) |
1654*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(mbx->head, SM_HEAD) |
1655*4882a593Smuzhiyun FM10K_MSG_HDR_FIELD_SET(err, SM_ERR);
1656*4882a593Smuzhiyun }
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun /**
1659*4882a593Smuzhiyun * fm10k_sm_mbx_connect_reset - Reset following request for reset
1660*4882a593Smuzhiyun * @mbx: pointer to mailbox
1661*4882a593Smuzhiyun *
1662*4882a593Smuzhiyun * This function resets the mailbox to a just connected state
1663*4882a593Smuzhiyun **/
fm10k_sm_mbx_connect_reset(struct fm10k_mbx_info * mbx)1664*4882a593Smuzhiyun static void fm10k_sm_mbx_connect_reset(struct fm10k_mbx_info *mbx)
1665*4882a593Smuzhiyun {
1666*4882a593Smuzhiyun /* flush any uncompleted work */
1667*4882a593Smuzhiyun fm10k_mbx_reset_work(mbx);
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun /* set local version to max and remote version to 0 */
1670*4882a593Smuzhiyun mbx->local = FM10K_SM_MBX_VERSION;
1671*4882a593Smuzhiyun mbx->remote = 0;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun /* initialize tail and head */
1674*4882a593Smuzhiyun mbx->tail = 1;
1675*4882a593Smuzhiyun mbx->head = 1;
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun /* reset state back to connect */
1678*4882a593Smuzhiyun mbx->state = FM10K_STATE_CONNECT;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun /**
1682*4882a593Smuzhiyun * fm10k_sm_mbx_connect - Start switch manager mailbox connection
1683*4882a593Smuzhiyun * @hw: pointer to hardware structure
1684*4882a593Smuzhiyun * @mbx: pointer to mailbox
1685*4882a593Smuzhiyun *
1686*4882a593Smuzhiyun * This function will initiate a mailbox connection with the switch
1687*4882a593Smuzhiyun * manager. To do this it will first disconnect the mailbox, and then
1688*4882a593Smuzhiyun * reconnect it in order to complete a reset of the mailbox.
1689*4882a593Smuzhiyun *
1690*4882a593Smuzhiyun * This function will return an error if the mailbox has not been initiated
1691*4882a593Smuzhiyun * or is currently in use.
1692*4882a593Smuzhiyun **/
fm10k_sm_mbx_connect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1693*4882a593Smuzhiyun static s32 fm10k_sm_mbx_connect(struct fm10k_hw *hw, struct fm10k_mbx_info *mbx)
1694*4882a593Smuzhiyun {
1695*4882a593Smuzhiyun /* we cannot connect an uninitialized mailbox */
1696*4882a593Smuzhiyun if (!mbx->rx.buffer)
1697*4882a593Smuzhiyun return FM10K_MBX_ERR_NO_SPACE;
1698*4882a593Smuzhiyun
1699*4882a593Smuzhiyun /* we cannot connect an already connected mailbox */
1700*4882a593Smuzhiyun if (mbx->state != FM10K_STATE_CLOSED)
1701*4882a593Smuzhiyun return FM10K_MBX_ERR_BUSY;
1702*4882a593Smuzhiyun
1703*4882a593Smuzhiyun /* mailbox timeout can now become active */
1704*4882a593Smuzhiyun mbx->timeout = FM10K_MBX_INIT_TIMEOUT;
1705*4882a593Smuzhiyun
1706*4882a593Smuzhiyun /* Place mbx in ready to connect state */
1707*4882a593Smuzhiyun mbx->state = FM10K_STATE_CONNECT;
1708*4882a593Smuzhiyun mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun /* reset interface back to connect */
1711*4882a593Smuzhiyun fm10k_sm_mbx_connect_reset(mbx);
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun /* enable interrupt and notify other party of new message */
1714*4882a593Smuzhiyun mbx->mbx_lock = FM10K_MBX_REQ_INTERRUPT | FM10K_MBX_ACK_INTERRUPT |
1715*4882a593Smuzhiyun FM10K_MBX_INTERRUPT_ENABLE;
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun /* generate and load connect header into mailbox */
1718*4882a593Smuzhiyun fm10k_sm_mbx_create_connect_hdr(mbx, 0);
1719*4882a593Smuzhiyun fm10k_mbx_write(hw, mbx);
1720*4882a593Smuzhiyun
1721*4882a593Smuzhiyun return 0;
1722*4882a593Smuzhiyun }
1723*4882a593Smuzhiyun
1724*4882a593Smuzhiyun /**
1725*4882a593Smuzhiyun * fm10k_sm_mbx_disconnect - Shutdown mailbox connection
1726*4882a593Smuzhiyun * @hw: pointer to hardware structure
1727*4882a593Smuzhiyun * @mbx: pointer to mailbox
1728*4882a593Smuzhiyun *
1729*4882a593Smuzhiyun * This function will shut down the mailbox. It places the mailbox first
1730*4882a593Smuzhiyun * in the disconnect state, it then allows up to a predefined timeout for
1731*4882a593Smuzhiyun * the mailbox to transition to close on its own. If this does not occur
1732*4882a593Smuzhiyun * then the mailbox will be forced into the closed state.
1733*4882a593Smuzhiyun *
1734*4882a593Smuzhiyun * Any mailbox transactions not completed before calling this function
1735*4882a593Smuzhiyun * are not guaranteed to complete and may be dropped.
1736*4882a593Smuzhiyun **/
fm10k_sm_mbx_disconnect(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1737*4882a593Smuzhiyun static void fm10k_sm_mbx_disconnect(struct fm10k_hw *hw,
1738*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
1739*4882a593Smuzhiyun {
1740*4882a593Smuzhiyun int timeout = mbx->timeout ? FM10K_MBX_DISCONNECT_TIMEOUT : 0;
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun /* Place mbx in ready to disconnect state */
1743*4882a593Smuzhiyun mbx->state = FM10K_STATE_DISCONNECT;
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun /* trigger interrupt to start shutdown process */
1746*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbx_reg, FM10K_MBX_REQ |
1747*4882a593Smuzhiyun FM10K_MBX_INTERRUPT_DISABLE);
1748*4882a593Smuzhiyun do {
1749*4882a593Smuzhiyun udelay(FM10K_MBX_POLL_DELAY);
1750*4882a593Smuzhiyun mbx->ops.process(hw, mbx);
1751*4882a593Smuzhiyun timeout -= FM10K_MBX_POLL_DELAY;
1752*4882a593Smuzhiyun } while ((timeout > 0) && (mbx->state != FM10K_STATE_CLOSED));
1753*4882a593Smuzhiyun
1754*4882a593Smuzhiyun /* in case we didn't close just force the mailbox into shutdown */
1755*4882a593Smuzhiyun mbx->state = FM10K_STATE_CLOSED;
1756*4882a593Smuzhiyun mbx->remote = 0;
1757*4882a593Smuzhiyun fm10k_mbx_reset_work(mbx);
1758*4882a593Smuzhiyun fm10k_fifo_drop_all(&mbx->tx);
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun fm10k_write_reg(hw, mbx->mbmem_reg, 0);
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun /**
1764*4882a593Smuzhiyun * fm10k_sm_mbx_validate_fifo_hdr - Validate fields in the remote FIFO header
1765*4882a593Smuzhiyun * @mbx: pointer to mailbox
1766*4882a593Smuzhiyun *
1767*4882a593Smuzhiyun * This function will parse up the fields in the mailbox header and return
1768*4882a593Smuzhiyun * an error if the header contains any of a number of invalid configurations
1769*4882a593Smuzhiyun * including unrecognized offsets or version numbers.
1770*4882a593Smuzhiyun **/
fm10k_sm_mbx_validate_fifo_hdr(struct fm10k_mbx_info * mbx)1771*4882a593Smuzhiyun static s32 fm10k_sm_mbx_validate_fifo_hdr(struct fm10k_mbx_info *mbx)
1772*4882a593Smuzhiyun {
1773*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
1774*4882a593Smuzhiyun u16 tail, head, ver;
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
1777*4882a593Smuzhiyun ver = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_VER);
1778*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun switch (ver) {
1781*4882a593Smuzhiyun case 0:
1782*4882a593Smuzhiyun break;
1783*4882a593Smuzhiyun case FM10K_SM_MBX_VERSION:
1784*4882a593Smuzhiyun if (!head || head > FM10K_SM_MBX_FIFO_LEN)
1785*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
1786*4882a593Smuzhiyun if (!tail || tail > FM10K_SM_MBX_FIFO_LEN)
1787*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
1788*4882a593Smuzhiyun if (mbx->tail < head)
1789*4882a593Smuzhiyun head += mbx->mbmem_len - 1;
1790*4882a593Smuzhiyun if (tail < mbx->head)
1791*4882a593Smuzhiyun tail += mbx->mbmem_len - 1;
1792*4882a593Smuzhiyun if (fm10k_mbx_index_len(mbx, head, mbx->tail) > mbx->tail_len)
1793*4882a593Smuzhiyun return FM10K_MBX_ERR_HEAD;
1794*4882a593Smuzhiyun if (fm10k_mbx_index_len(mbx, mbx->head, tail) < mbx->mbmem_len)
1795*4882a593Smuzhiyun break;
1796*4882a593Smuzhiyun return FM10K_MBX_ERR_TAIL;
1797*4882a593Smuzhiyun default:
1798*4882a593Smuzhiyun return FM10K_MBX_ERR_SRC;
1799*4882a593Smuzhiyun }
1800*4882a593Smuzhiyun
1801*4882a593Smuzhiyun return 0;
1802*4882a593Smuzhiyun }
1803*4882a593Smuzhiyun
1804*4882a593Smuzhiyun /**
1805*4882a593Smuzhiyun * fm10k_sm_mbx_process_error - Process header with error flag set
1806*4882a593Smuzhiyun * @mbx: pointer to mailbox
1807*4882a593Smuzhiyun *
1808*4882a593Smuzhiyun * This function is meant to respond to a request where the error flag
1809*4882a593Smuzhiyun * is set. As a result we will terminate a connection if one is present
1810*4882a593Smuzhiyun * and fall back into the reset state with a connection header of version
1811*4882a593Smuzhiyun * 0 (RESET).
1812*4882a593Smuzhiyun **/
fm10k_sm_mbx_process_error(struct fm10k_mbx_info * mbx)1813*4882a593Smuzhiyun static void fm10k_sm_mbx_process_error(struct fm10k_mbx_info *mbx)
1814*4882a593Smuzhiyun {
1815*4882a593Smuzhiyun const enum fm10k_mbx_state state = mbx->state;
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun switch (state) {
1818*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1819*4882a593Smuzhiyun /* if there is an error just disconnect */
1820*4882a593Smuzhiyun mbx->remote = 0;
1821*4882a593Smuzhiyun break;
1822*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1823*4882a593Smuzhiyun /* flush any uncompleted work */
1824*4882a593Smuzhiyun fm10k_sm_mbx_connect_reset(mbx);
1825*4882a593Smuzhiyun break;
1826*4882a593Smuzhiyun case FM10K_STATE_CONNECT:
1827*4882a593Smuzhiyun /* try connnecting at lower version */
1828*4882a593Smuzhiyun if (mbx->remote) {
1829*4882a593Smuzhiyun while (mbx->local > 1)
1830*4882a593Smuzhiyun mbx->local--;
1831*4882a593Smuzhiyun mbx->remote = 0;
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun break;
1834*4882a593Smuzhiyun default:
1835*4882a593Smuzhiyun break;
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun fm10k_sm_mbx_create_connect_hdr(mbx, 0);
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun /**
1842*4882a593Smuzhiyun * fm10k_sm_mbx_create_error_msg - Process an error in FIFO header
1843*4882a593Smuzhiyun * @mbx: pointer to mailbox
1844*4882a593Smuzhiyun * @err: local error encountered
1845*4882a593Smuzhiyun *
1846*4882a593Smuzhiyun * This function will interpret the error provided by err, and based on
1847*4882a593Smuzhiyun * that it may set the error bit in the local message header
1848*4882a593Smuzhiyun **/
fm10k_sm_mbx_create_error_msg(struct fm10k_mbx_info * mbx,s32 err)1849*4882a593Smuzhiyun static void fm10k_sm_mbx_create_error_msg(struct fm10k_mbx_info *mbx, s32 err)
1850*4882a593Smuzhiyun {
1851*4882a593Smuzhiyun /* only generate an error message for these types */
1852*4882a593Smuzhiyun switch (err) {
1853*4882a593Smuzhiyun case FM10K_MBX_ERR_TAIL:
1854*4882a593Smuzhiyun case FM10K_MBX_ERR_HEAD:
1855*4882a593Smuzhiyun case FM10K_MBX_ERR_SRC:
1856*4882a593Smuzhiyun case FM10K_MBX_ERR_SIZE:
1857*4882a593Smuzhiyun case FM10K_MBX_ERR_RSVD0:
1858*4882a593Smuzhiyun break;
1859*4882a593Smuzhiyun default:
1860*4882a593Smuzhiyun return;
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun /* process it as though we received an error, and send error reply */
1864*4882a593Smuzhiyun fm10k_sm_mbx_process_error(mbx);
1865*4882a593Smuzhiyun fm10k_sm_mbx_create_connect_hdr(mbx, 1);
1866*4882a593Smuzhiyun }
1867*4882a593Smuzhiyun
1868*4882a593Smuzhiyun /**
1869*4882a593Smuzhiyun * fm10k_sm_mbx_receive - Take message from Rx mailbox FIFO and put it in Rx
1870*4882a593Smuzhiyun * @hw: pointer to hardware structure
1871*4882a593Smuzhiyun * @mbx: pointer to mailbox
1872*4882a593Smuzhiyun * @tail: tail index of message
1873*4882a593Smuzhiyun *
1874*4882a593Smuzhiyun * This function will dequeue one message from the Rx switch manager mailbox
1875*4882a593Smuzhiyun * FIFO and place it in the Rx mailbox FIFO for processing by software.
1876*4882a593Smuzhiyun **/
fm10k_sm_mbx_receive(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 tail)1877*4882a593Smuzhiyun static s32 fm10k_sm_mbx_receive(struct fm10k_hw *hw,
1878*4882a593Smuzhiyun struct fm10k_mbx_info *mbx,
1879*4882a593Smuzhiyun u16 tail)
1880*4882a593Smuzhiyun {
1881*4882a593Smuzhiyun /* reduce length by 1 to convert to a mask */
1882*4882a593Smuzhiyun u16 mbmem_len = mbx->mbmem_len - 1;
1883*4882a593Smuzhiyun s32 err;
1884*4882a593Smuzhiyun
1885*4882a593Smuzhiyun /* push tail in front of head */
1886*4882a593Smuzhiyun if (tail < mbx->head)
1887*4882a593Smuzhiyun tail += mbmem_len;
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun /* copy data to the Rx FIFO */
1890*4882a593Smuzhiyun err = fm10k_mbx_push_tail(hw, mbx, tail);
1891*4882a593Smuzhiyun if (err < 0)
1892*4882a593Smuzhiyun return err;
1893*4882a593Smuzhiyun
1894*4882a593Smuzhiyun /* process messages if we have received any */
1895*4882a593Smuzhiyun fm10k_mbx_dequeue_rx(hw, mbx);
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun /* guarantee head aligns with the end of the last message */
1898*4882a593Smuzhiyun mbx->head = fm10k_mbx_head_sub(mbx, mbx->pushed);
1899*4882a593Smuzhiyun mbx->pushed = 0;
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun /* clear any extra bits left over since index adds 1 extra bit */
1902*4882a593Smuzhiyun if (mbx->head > mbmem_len)
1903*4882a593Smuzhiyun mbx->head -= mbmem_len;
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun return err;
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun
1908*4882a593Smuzhiyun /**
1909*4882a593Smuzhiyun * fm10k_sm_mbx_transmit - Take message from Tx and put it in Tx mailbox FIFO
1910*4882a593Smuzhiyun * @hw: pointer to hardware structure
1911*4882a593Smuzhiyun * @mbx: pointer to mailbox
1912*4882a593Smuzhiyun * @head: head index of message
1913*4882a593Smuzhiyun *
1914*4882a593Smuzhiyun * This function will dequeue one message from the Tx mailbox FIFO and place
1915*4882a593Smuzhiyun * it in the Tx switch manager mailbox FIFO for processing by hardware.
1916*4882a593Smuzhiyun **/
fm10k_sm_mbx_transmit(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 head)1917*4882a593Smuzhiyun static void fm10k_sm_mbx_transmit(struct fm10k_hw *hw,
1918*4882a593Smuzhiyun struct fm10k_mbx_info *mbx, u16 head)
1919*4882a593Smuzhiyun {
1920*4882a593Smuzhiyun struct fm10k_mbx_fifo *fifo = &mbx->tx;
1921*4882a593Smuzhiyun /* reduce length by 1 to convert to a mask */
1922*4882a593Smuzhiyun u16 mbmem_len = mbx->mbmem_len - 1;
1923*4882a593Smuzhiyun u16 tail_len, len = 0;
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun /* push head behind tail */
1926*4882a593Smuzhiyun if (mbx->tail < head)
1927*4882a593Smuzhiyun head += mbmem_len;
1928*4882a593Smuzhiyun
1929*4882a593Smuzhiyun fm10k_mbx_pull_head(hw, mbx, head);
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun /* determine msg aligned offset for end of buffer */
1932*4882a593Smuzhiyun do {
1933*4882a593Smuzhiyun u32 *msg;
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun msg = fifo->buffer + fm10k_fifo_head_offset(fifo, len);
1936*4882a593Smuzhiyun tail_len = len;
1937*4882a593Smuzhiyun len += FM10K_TLV_DWORD_LEN(*msg);
1938*4882a593Smuzhiyun } while ((len <= mbx->tail_len) && (len < mbmem_len));
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun /* guarantee we stop on a message boundary */
1941*4882a593Smuzhiyun if (mbx->tail_len > tail_len) {
1942*4882a593Smuzhiyun mbx->tail = fm10k_mbx_tail_sub(mbx, mbx->tail_len - tail_len);
1943*4882a593Smuzhiyun mbx->tail_len = tail_len;
1944*4882a593Smuzhiyun }
1945*4882a593Smuzhiyun
1946*4882a593Smuzhiyun /* clear any extra bits left over since index adds 1 extra bit */
1947*4882a593Smuzhiyun if (mbx->tail > mbmem_len)
1948*4882a593Smuzhiyun mbx->tail -= mbmem_len;
1949*4882a593Smuzhiyun }
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun /**
1952*4882a593Smuzhiyun * fm10k_sm_mbx_create_reply - Generate reply based on state and remote head
1953*4882a593Smuzhiyun * @hw: pointer to hardware structure
1954*4882a593Smuzhiyun * @mbx: pointer to mailbox
1955*4882a593Smuzhiyun * @head: acknowledgement number
1956*4882a593Smuzhiyun *
1957*4882a593Smuzhiyun * This function will generate an outgoing message based on the current
1958*4882a593Smuzhiyun * mailbox state and the remote FIFO head. It will return the length
1959*4882a593Smuzhiyun * of the outgoing message excluding header on success, and a negative value
1960*4882a593Smuzhiyun * on error.
1961*4882a593Smuzhiyun **/
fm10k_sm_mbx_create_reply(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx,u16 head)1962*4882a593Smuzhiyun static void fm10k_sm_mbx_create_reply(struct fm10k_hw *hw,
1963*4882a593Smuzhiyun struct fm10k_mbx_info *mbx, u16 head)
1964*4882a593Smuzhiyun {
1965*4882a593Smuzhiyun switch (mbx->state) {
1966*4882a593Smuzhiyun case FM10K_STATE_OPEN:
1967*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
1968*4882a593Smuzhiyun /* flush out Tx data */
1969*4882a593Smuzhiyun fm10k_sm_mbx_transmit(hw, mbx, head);
1970*4882a593Smuzhiyun
1971*4882a593Smuzhiyun /* generate new header based on data */
1972*4882a593Smuzhiyun if (mbx->tail_len || (mbx->state == FM10K_STATE_OPEN)) {
1973*4882a593Smuzhiyun fm10k_sm_mbx_create_data_hdr(mbx);
1974*4882a593Smuzhiyun } else {
1975*4882a593Smuzhiyun mbx->remote = 0;
1976*4882a593Smuzhiyun fm10k_sm_mbx_create_connect_hdr(mbx, 0);
1977*4882a593Smuzhiyun }
1978*4882a593Smuzhiyun break;
1979*4882a593Smuzhiyun case FM10K_STATE_CONNECT:
1980*4882a593Smuzhiyun case FM10K_STATE_CLOSED:
1981*4882a593Smuzhiyun fm10k_sm_mbx_create_connect_hdr(mbx, 0);
1982*4882a593Smuzhiyun break;
1983*4882a593Smuzhiyun default:
1984*4882a593Smuzhiyun break;
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun }
1987*4882a593Smuzhiyun
1988*4882a593Smuzhiyun /**
1989*4882a593Smuzhiyun * fm10k_sm_mbx_process_reset - Process header with version == 0 (RESET)
1990*4882a593Smuzhiyun * @hw: pointer to hardware structure
1991*4882a593Smuzhiyun * @mbx: pointer to mailbox
1992*4882a593Smuzhiyun *
1993*4882a593Smuzhiyun * This function is meant to respond to a request where the version data
1994*4882a593Smuzhiyun * is set to 0. As such we will either terminate the connection or go
1995*4882a593Smuzhiyun * into the connect state in order to re-establish the connection. This
1996*4882a593Smuzhiyun * function can also be used to respond to an error as the connection
1997*4882a593Smuzhiyun * resetting would also be a means of dealing with errors.
1998*4882a593Smuzhiyun **/
fm10k_sm_mbx_process_reset(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)1999*4882a593Smuzhiyun static s32 fm10k_sm_mbx_process_reset(struct fm10k_hw *hw,
2000*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
2001*4882a593Smuzhiyun {
2002*4882a593Smuzhiyun s32 err = 0;
2003*4882a593Smuzhiyun const enum fm10k_mbx_state state = mbx->state;
2004*4882a593Smuzhiyun
2005*4882a593Smuzhiyun switch (state) {
2006*4882a593Smuzhiyun case FM10K_STATE_DISCONNECT:
2007*4882a593Smuzhiyun /* drop remote connections and disconnect */
2008*4882a593Smuzhiyun mbx->state = FM10K_STATE_CLOSED;
2009*4882a593Smuzhiyun mbx->remote = 0;
2010*4882a593Smuzhiyun mbx->local = 0;
2011*4882a593Smuzhiyun break;
2012*4882a593Smuzhiyun case FM10K_STATE_OPEN:
2013*4882a593Smuzhiyun /* flush any incomplete work */
2014*4882a593Smuzhiyun fm10k_sm_mbx_connect_reset(mbx);
2015*4882a593Smuzhiyun err = FM10K_ERR_RESET_REQUESTED;
2016*4882a593Smuzhiyun break;
2017*4882a593Smuzhiyun case FM10K_STATE_CONNECT:
2018*4882a593Smuzhiyun /* Update remote value to match local value */
2019*4882a593Smuzhiyun mbx->remote = mbx->local;
2020*4882a593Smuzhiyun default:
2021*4882a593Smuzhiyun break;
2022*4882a593Smuzhiyun }
2023*4882a593Smuzhiyun
2024*4882a593Smuzhiyun fm10k_sm_mbx_create_reply(hw, mbx, mbx->tail);
2025*4882a593Smuzhiyun
2026*4882a593Smuzhiyun return err;
2027*4882a593Smuzhiyun }
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun /**
2030*4882a593Smuzhiyun * fm10k_sm_mbx_process_version_1 - Process header with version == 1
2031*4882a593Smuzhiyun * @hw: pointer to hardware structure
2032*4882a593Smuzhiyun * @mbx: pointer to mailbox
2033*4882a593Smuzhiyun *
2034*4882a593Smuzhiyun * This function is meant to process messages received when the remote
2035*4882a593Smuzhiyun * mailbox is active.
2036*4882a593Smuzhiyun **/
fm10k_sm_mbx_process_version_1(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)2037*4882a593Smuzhiyun static s32 fm10k_sm_mbx_process_version_1(struct fm10k_hw *hw,
2038*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
2039*4882a593Smuzhiyun {
2040*4882a593Smuzhiyun const u32 *hdr = &mbx->mbx_hdr;
2041*4882a593Smuzhiyun u16 head, tail;
2042*4882a593Smuzhiyun s32 len;
2043*4882a593Smuzhiyun
2044*4882a593Smuzhiyun /* pull all fields needed for verification */
2045*4882a593Smuzhiyun tail = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_TAIL);
2046*4882a593Smuzhiyun head = FM10K_MSG_HDR_FIELD_GET(*hdr, SM_HEAD);
2047*4882a593Smuzhiyun
2048*4882a593Smuzhiyun /* if we are in connect and wanting version 1 then start up and go */
2049*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_CONNECT) {
2050*4882a593Smuzhiyun if (!mbx->remote)
2051*4882a593Smuzhiyun goto send_reply;
2052*4882a593Smuzhiyun if (mbx->remote != 1)
2053*4882a593Smuzhiyun return FM10K_MBX_ERR_SRC;
2054*4882a593Smuzhiyun
2055*4882a593Smuzhiyun mbx->state = FM10K_STATE_OPEN;
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun
2058*4882a593Smuzhiyun do {
2059*4882a593Smuzhiyun /* abort on message size errors */
2060*4882a593Smuzhiyun len = fm10k_sm_mbx_receive(hw, mbx, tail);
2061*4882a593Smuzhiyun if (len < 0)
2062*4882a593Smuzhiyun return len;
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun /* continue until we have flushed the Rx FIFO */
2065*4882a593Smuzhiyun } while (len);
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun send_reply:
2068*4882a593Smuzhiyun fm10k_sm_mbx_create_reply(hw, mbx, head);
2069*4882a593Smuzhiyun
2070*4882a593Smuzhiyun return 0;
2071*4882a593Smuzhiyun }
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun /**
2074*4882a593Smuzhiyun * fm10k_sm_mbx_process - Process switch manager mailbox interrupt
2075*4882a593Smuzhiyun * @hw: pointer to hardware structure
2076*4882a593Smuzhiyun * @mbx: pointer to mailbox
2077*4882a593Smuzhiyun *
2078*4882a593Smuzhiyun * This function will process incoming mailbox events and generate mailbox
2079*4882a593Smuzhiyun * replies. It will return a value indicating the number of DWORDs
2080*4882a593Smuzhiyun * transmitted excluding header on success or a negative value on error.
2081*4882a593Smuzhiyun **/
fm10k_sm_mbx_process(struct fm10k_hw * hw,struct fm10k_mbx_info * mbx)2082*4882a593Smuzhiyun static s32 fm10k_sm_mbx_process(struct fm10k_hw *hw,
2083*4882a593Smuzhiyun struct fm10k_mbx_info *mbx)
2084*4882a593Smuzhiyun {
2085*4882a593Smuzhiyun s32 err;
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun /* we do not read mailbox if closed */
2088*4882a593Smuzhiyun if (mbx->state == FM10K_STATE_CLOSED)
2089*4882a593Smuzhiyun return 0;
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun /* retrieve data from switch manager */
2092*4882a593Smuzhiyun err = fm10k_mbx_read(hw, mbx);
2093*4882a593Smuzhiyun if (err)
2094*4882a593Smuzhiyun return err;
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun err = fm10k_sm_mbx_validate_fifo_hdr(mbx);
2097*4882a593Smuzhiyun if (err < 0)
2098*4882a593Smuzhiyun goto fifo_err;
2099*4882a593Smuzhiyun
2100*4882a593Smuzhiyun if (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_ERR)) {
2101*4882a593Smuzhiyun fm10k_sm_mbx_process_error(mbx);
2102*4882a593Smuzhiyun goto fifo_err;
2103*4882a593Smuzhiyun }
2104*4882a593Smuzhiyun
2105*4882a593Smuzhiyun switch (FM10K_MSG_HDR_FIELD_GET(mbx->mbx_hdr, SM_VER)) {
2106*4882a593Smuzhiyun case 0:
2107*4882a593Smuzhiyun err = fm10k_sm_mbx_process_reset(hw, mbx);
2108*4882a593Smuzhiyun break;
2109*4882a593Smuzhiyun case FM10K_SM_MBX_VERSION:
2110*4882a593Smuzhiyun err = fm10k_sm_mbx_process_version_1(hw, mbx);
2111*4882a593Smuzhiyun break;
2112*4882a593Smuzhiyun }
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun fifo_err:
2115*4882a593Smuzhiyun if (err < 0)
2116*4882a593Smuzhiyun fm10k_sm_mbx_create_error_msg(mbx, err);
2117*4882a593Smuzhiyun
2118*4882a593Smuzhiyun /* report data to switch manager */
2119*4882a593Smuzhiyun fm10k_mbx_write(hw, mbx);
2120*4882a593Smuzhiyun
2121*4882a593Smuzhiyun return err;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun /**
2125*4882a593Smuzhiyun * fm10k_sm_mbx_init - Initialize mailbox memory for PF/SM mailbox
2126*4882a593Smuzhiyun * @hw: pointer to hardware structure
2127*4882a593Smuzhiyun * @mbx: pointer to mailbox
2128*4882a593Smuzhiyun * @msg_data: handlers for mailbox events
2129*4882a593Smuzhiyun *
2130*4882a593Smuzhiyun * This function initializes the PF/SM mailbox for use. It will split the
2131*4882a593Smuzhiyun * buffer provided and use that to populate both the Tx and Rx FIFO by
2132*4882a593Smuzhiyun * evenly splitting it. In order to allow for easy masking of head/tail
2133*4882a593Smuzhiyun * the value reported in size must be a power of 2 and is reported in
2134*4882a593Smuzhiyun * DWORDs, not bytes. Any invalid values will cause the mailbox to return
2135*4882a593Smuzhiyun * error.
2136*4882a593Smuzhiyun **/
fm10k_sm_mbx_init(struct fm10k_hw __always_unused * hw,struct fm10k_mbx_info * mbx,const struct fm10k_msg_data * msg_data)2137*4882a593Smuzhiyun s32 fm10k_sm_mbx_init(struct fm10k_hw __always_unused *hw,
2138*4882a593Smuzhiyun struct fm10k_mbx_info *mbx,
2139*4882a593Smuzhiyun const struct fm10k_msg_data *msg_data)
2140*4882a593Smuzhiyun {
2141*4882a593Smuzhiyun mbx->mbx_reg = FM10K_GMBX;
2142*4882a593Smuzhiyun mbx->mbmem_reg = FM10K_MBMEM_PF(0);
2143*4882a593Smuzhiyun
2144*4882a593Smuzhiyun /* start out in closed state */
2145*4882a593Smuzhiyun mbx->state = FM10K_STATE_CLOSED;
2146*4882a593Smuzhiyun
2147*4882a593Smuzhiyun /* validate layout of handlers before assigning them */
2148*4882a593Smuzhiyun if (fm10k_mbx_validate_handlers(msg_data))
2149*4882a593Smuzhiyun return FM10K_ERR_PARAM;
2150*4882a593Smuzhiyun
2151*4882a593Smuzhiyun /* initialize the message handlers */
2152*4882a593Smuzhiyun mbx->msg_data = msg_data;
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun /* start mailbox as timed out and let the reset_hw call
2155*4882a593Smuzhiyun * set the timeout value to begin communications
2156*4882a593Smuzhiyun */
2157*4882a593Smuzhiyun mbx->timeout = 0;
2158*4882a593Smuzhiyun mbx->udelay = FM10K_MBX_INIT_DELAY;
2159*4882a593Smuzhiyun
2160*4882a593Smuzhiyun /* Split buffer for use by Tx/Rx FIFOs */
2161*4882a593Smuzhiyun mbx->max_size = FM10K_MBX_MSG_MAX_SIZE;
2162*4882a593Smuzhiyun mbx->mbmem_len = FM10K_MBMEM_PF_XOR;
2163*4882a593Smuzhiyun
2164*4882a593Smuzhiyun /* initialize the FIFOs, sizes are in 4 byte increments */
2165*4882a593Smuzhiyun fm10k_fifo_init(&mbx->tx, mbx->buffer, FM10K_MBX_TX_BUFFER_SIZE);
2166*4882a593Smuzhiyun fm10k_fifo_init(&mbx->rx, &mbx->buffer[FM10K_MBX_TX_BUFFER_SIZE],
2167*4882a593Smuzhiyun FM10K_MBX_RX_BUFFER_SIZE);
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun /* initialize function pointers */
2170*4882a593Smuzhiyun mbx->ops.connect = fm10k_sm_mbx_connect;
2171*4882a593Smuzhiyun mbx->ops.disconnect = fm10k_sm_mbx_disconnect;
2172*4882a593Smuzhiyun mbx->ops.rx_ready = fm10k_mbx_rx_ready;
2173*4882a593Smuzhiyun mbx->ops.tx_ready = fm10k_mbx_tx_ready;
2174*4882a593Smuzhiyun mbx->ops.tx_complete = fm10k_mbx_tx_complete;
2175*4882a593Smuzhiyun mbx->ops.enqueue_tx = fm10k_mbx_enqueue_tx;
2176*4882a593Smuzhiyun mbx->ops.process = fm10k_sm_mbx_process;
2177*4882a593Smuzhiyun mbx->ops.register_handlers = fm10k_mbx_register_handlers;
2178*4882a593Smuzhiyun
2179*4882a593Smuzhiyun return 0;
2180*4882a593Smuzhiyun }
2181