New upstream version 18.08
[deb_dpdk.git] / kernel / linux / kni / ethtool / igb / e1000_mbx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*******************************************************************************
3
4   Intel(R) Gigabit Ethernet Linux driver
5   Copyright(c) 2007-2013 Intel Corporation.
6
7   Contact Information:
8   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
9   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
10
11 *******************************************************************************/
12
13 #include "e1000_mbx.h"
14
15 /**
16  *  e1000_null_mbx_check_for_flag - No-op function, return 0
17  *  @hw: pointer to the HW structure
18  **/
19 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
20                                          u16 E1000_UNUSEDARG mbx_id)
21 {
22         DEBUGFUNC("e1000_null_mbx_check_flag");
23
24         return E1000_SUCCESS;
25 }
26
27 /**
28  *  e1000_null_mbx_transact - No-op function, return 0
29  *  @hw: pointer to the HW structure
30  **/
31 static s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
32                                    u32 E1000_UNUSEDARG *msg,
33                                    u16 E1000_UNUSEDARG size,
34                                    u16 E1000_UNUSEDARG mbx_id)
35 {
36         DEBUGFUNC("e1000_null_mbx_rw_msg");
37
38         return E1000_SUCCESS;
39 }
40
41 /**
42  *  e1000_read_mbx - Reads a message from the mailbox
43  *  @hw: pointer to the HW structure
44  *  @msg: The message buffer
45  *  @size: Length of buffer
46  *  @mbx_id: id of mailbox to read
47  *
48  *  returns SUCCESS if it successfully read message from buffer
49  **/
50 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
51 {
52         struct e1000_mbx_info *mbx = &hw->mbx;
53         s32 ret_val = -E1000_ERR_MBX;
54
55         DEBUGFUNC("e1000_read_mbx");
56
57         /* limit read to size of mailbox */
58         if (size > mbx->size)
59                 size = mbx->size;
60
61         if (mbx->ops.read)
62                 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
63
64         return ret_val;
65 }
66
67 /**
68  *  e1000_write_mbx - Write a message to the mailbox
69  *  @hw: pointer to the HW structure
70  *  @msg: The message buffer
71  *  @size: Length of buffer
72  *  @mbx_id: id of mailbox to write
73  *
74  *  returns SUCCESS if it successfully copied message into the buffer
75  **/
76 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
77 {
78         struct e1000_mbx_info *mbx = &hw->mbx;
79         s32 ret_val = E1000_SUCCESS;
80
81         DEBUGFUNC("e1000_write_mbx");
82
83         if (size > mbx->size)
84                 ret_val = -E1000_ERR_MBX;
85
86         else if (mbx->ops.write)
87                 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
88
89         return ret_val;
90 }
91
92 /**
93  *  e1000_check_for_msg - checks to see if someone sent us mail
94  *  @hw: pointer to the HW structure
95  *  @mbx_id: id of mailbox to check
96  *
97  *  returns SUCCESS if the Status bit was found or else ERR_MBX
98  **/
99 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
100 {
101         struct e1000_mbx_info *mbx = &hw->mbx;
102         s32 ret_val = -E1000_ERR_MBX;
103
104         DEBUGFUNC("e1000_check_for_msg");
105
106         if (mbx->ops.check_for_msg)
107                 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
108
109         return ret_val;
110 }
111
112 /**
113  *  e1000_check_for_ack - checks to see if someone sent us ACK
114  *  @hw: pointer to the HW structure
115  *  @mbx_id: id of mailbox to check
116  *
117  *  returns SUCCESS if the Status bit was found or else ERR_MBX
118  **/
119 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
120 {
121         struct e1000_mbx_info *mbx = &hw->mbx;
122         s32 ret_val = -E1000_ERR_MBX;
123
124         DEBUGFUNC("e1000_check_for_ack");
125
126         if (mbx->ops.check_for_ack)
127                 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
128
129         return ret_val;
130 }
131
132 /**
133  *  e1000_check_for_rst - checks to see if other side has reset
134  *  @hw: pointer to the HW structure
135  *  @mbx_id: id of mailbox to check
136  *
137  *  returns SUCCESS if the Status bit was found or else ERR_MBX
138  **/
139 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
140 {
141         struct e1000_mbx_info *mbx = &hw->mbx;
142         s32 ret_val = -E1000_ERR_MBX;
143
144         DEBUGFUNC("e1000_check_for_rst");
145
146         if (mbx->ops.check_for_rst)
147                 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
148
149         return ret_val;
150 }
151
152 /**
153  *  e1000_poll_for_msg - Wait for message notification
154  *  @hw: pointer to the HW structure
155  *  @mbx_id: id of mailbox to write
156  *
157  *  returns SUCCESS if it successfully received a message notification
158  **/
159 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
160 {
161         struct e1000_mbx_info *mbx = &hw->mbx;
162         int countdown = mbx->timeout;
163
164         DEBUGFUNC("e1000_poll_for_msg");
165
166         if (!countdown || !mbx->ops.check_for_msg)
167                 goto out;
168
169         while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
170                 countdown--;
171                 if (!countdown)
172                         break;
173                 usec_delay(mbx->usec_delay);
174         }
175
176         /* if we failed, all future posted messages fail until reset */
177         if (!countdown)
178                 mbx->timeout = 0;
179 out:
180         return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
181 }
182
183 /**
184  *  e1000_poll_for_ack - Wait for message acknowledgement
185  *  @hw: pointer to the HW structure
186  *  @mbx_id: id of mailbox to write
187  *
188  *  returns SUCCESS if it successfully received a message acknowledgement
189  **/
190 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
191 {
192         struct e1000_mbx_info *mbx = &hw->mbx;
193         int countdown = mbx->timeout;
194
195         DEBUGFUNC("e1000_poll_for_ack");
196
197         if (!countdown || !mbx->ops.check_for_ack)
198                 goto out;
199
200         while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
201                 countdown--;
202                 if (!countdown)
203                         break;
204                 usec_delay(mbx->usec_delay);
205         }
206
207         /* if we failed, all future posted messages fail until reset */
208         if (!countdown)
209                 mbx->timeout = 0;
210 out:
211         return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
212 }
213
214 /**
215  *  e1000_read_posted_mbx - Wait for message notification and receive message
216  *  @hw: pointer to the HW structure
217  *  @msg: The message buffer
218  *  @size: Length of buffer
219  *  @mbx_id: id of mailbox to write
220  *
221  *  returns SUCCESS if it successfully received a message notification and
222  *  copied it into the receive buffer.
223  **/
224 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
225 {
226         struct e1000_mbx_info *mbx = &hw->mbx;
227         s32 ret_val = -E1000_ERR_MBX;
228
229         DEBUGFUNC("e1000_read_posted_mbx");
230
231         if (!mbx->ops.read)
232                 goto out;
233
234         ret_val = e1000_poll_for_msg(hw, mbx_id);
235
236         /* if ack received read message, otherwise we timed out */
237         if (!ret_val)
238                 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
239 out:
240         return ret_val;
241 }
242
243 /**
244  *  e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
245  *  @hw: pointer to the HW structure
246  *  @msg: The message buffer
247  *  @size: Length of buffer
248  *  @mbx_id: id of mailbox to write
249  *
250  *  returns SUCCESS if it successfully copied message into the buffer and
251  *  received an ack to that message within delay * timeout period
252  **/
253 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
254 {
255         struct e1000_mbx_info *mbx = &hw->mbx;
256         s32 ret_val = -E1000_ERR_MBX;
257
258         DEBUGFUNC("e1000_write_posted_mbx");
259
260         /* exit if either we can't write or there isn't a defined timeout */
261         if (!mbx->ops.write || !mbx->timeout)
262                 goto out;
263
264         /* send msg */
265         ret_val = mbx->ops.write(hw, msg, size, mbx_id);
266
267         /* if msg sent wait until we receive an ack */
268         if (!ret_val)
269                 ret_val = e1000_poll_for_ack(hw, mbx_id);
270 out:
271         return ret_val;
272 }
273
274 /**
275  *  e1000_init_mbx_ops_generic - Initialize mbx function pointers
276  *  @hw: pointer to the HW structure
277  *
278  *  Sets the function pointers to no-op functions
279  **/
280 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
281 {
282         struct e1000_mbx_info *mbx = &hw->mbx;
283         mbx->ops.init_params = e1000_null_ops_generic;
284         mbx->ops.read = e1000_null_mbx_transact;
285         mbx->ops.write = e1000_null_mbx_transact;
286         mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
287         mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
288         mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
289         mbx->ops.read_posted = e1000_read_posted_mbx;
290         mbx->ops.write_posted = e1000_write_posted_mbx;
291 }
292
293 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
294 {
295         u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
296         s32 ret_val = -E1000_ERR_MBX;
297
298         if (mbvficr & mask) {
299                 ret_val = E1000_SUCCESS;
300                 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
301         }
302
303         return ret_val;
304 }
305
306 /**
307  *  e1000_check_for_msg_pf - checks to see if the VF has sent mail
308  *  @hw: pointer to the HW structure
309  *  @vf_number: the VF index
310  *
311  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
312  **/
313 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
314 {
315         s32 ret_val = -E1000_ERR_MBX;
316
317         DEBUGFUNC("e1000_check_for_msg_pf");
318
319         if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
320                 ret_val = E1000_SUCCESS;
321                 hw->mbx.stats.reqs++;
322         }
323
324         return ret_val;
325 }
326
327 /**
328  *  e1000_check_for_ack_pf - checks to see if the VF has ACKed
329  *  @hw: pointer to the HW structure
330  *  @vf_number: the VF index
331  *
332  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
333  **/
334 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
335 {
336         s32 ret_val = -E1000_ERR_MBX;
337
338         DEBUGFUNC("e1000_check_for_ack_pf");
339
340         if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
341                 ret_val = E1000_SUCCESS;
342                 hw->mbx.stats.acks++;
343         }
344
345         return ret_val;
346 }
347
348 /**
349  *  e1000_check_for_rst_pf - checks to see if the VF has reset
350  *  @hw: pointer to the HW structure
351  *  @vf_number: the VF index
352  *
353  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
354  **/
355 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
356 {
357         u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
358         s32 ret_val = -E1000_ERR_MBX;
359
360         DEBUGFUNC("e1000_check_for_rst_pf");
361
362         if (vflre & (1 << vf_number)) {
363                 ret_val = E1000_SUCCESS;
364                 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
365                 hw->mbx.stats.rsts++;
366         }
367
368         return ret_val;
369 }
370
371 /**
372  *  e1000_obtain_mbx_lock_pf - obtain mailbox lock
373  *  @hw: pointer to the HW structure
374  *  @vf_number: the VF index
375  *
376  *  return SUCCESS if we obtained the mailbox lock
377  **/
378 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
379 {
380         s32 ret_val = -E1000_ERR_MBX;
381         u32 p2v_mailbox;
382
383         DEBUGFUNC("e1000_obtain_mbx_lock_pf");
384
385         /* Take ownership of the buffer */
386         E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
387
388         /* reserve mailbox for vf use */
389         p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
390         if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
391                 ret_val = E1000_SUCCESS;
392
393         return ret_val;
394 }
395
396 /**
397  *  e1000_write_mbx_pf - Places a message in the mailbox
398  *  @hw: pointer to the HW structure
399  *  @msg: The message buffer
400  *  @size: Length of buffer
401  *  @vf_number: the VF index
402  *
403  *  returns SUCCESS if it successfully copied message into the buffer
404  **/
405 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
406                               u16 vf_number)
407 {
408         s32 ret_val;
409         u16 i;
410
411         DEBUGFUNC("e1000_write_mbx_pf");
412
413         /* lock the mailbox to prevent pf/vf race condition */
414         ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
415         if (ret_val)
416                 goto out_no_write;
417
418         /* flush msg and acks as we are overwriting the message buffer */
419         e1000_check_for_msg_pf(hw, vf_number);
420         e1000_check_for_ack_pf(hw, vf_number);
421
422         /* copy the caller specified message to the mailbox memory buffer */
423         for (i = 0; i < size; i++)
424                 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
425
426         /* Interrupt VF to tell it a message has been sent and release buffer*/
427         E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
428
429         /* update stats */
430         hw->mbx.stats.msgs_tx++;
431
432 out_no_write:
433         return ret_val;
434
435 }
436
437 /**
438  *  e1000_read_mbx_pf - Read a message from the mailbox
439  *  @hw: pointer to the HW structure
440  *  @msg: The message buffer
441  *  @size: Length of buffer
442  *  @vf_number: the VF index
443  *
444  *  This function copies a message from the mailbox buffer to the caller's
445  *  memory buffer.  The presumption is that the caller knows that there was
446  *  a message due to a VF request so no polling for message is needed.
447  **/
448 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
449                              u16 vf_number)
450 {
451         s32 ret_val;
452         u16 i;
453
454         DEBUGFUNC("e1000_read_mbx_pf");
455
456         /* lock the mailbox to prevent pf/vf race condition */
457         ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
458         if (ret_val)
459                 goto out_no_read;
460
461         /* copy the message to the mailbox memory buffer */
462         for (i = 0; i < size; i++)
463                 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
464
465         /* Acknowledge the message and release buffer */
466         E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
467
468         /* update stats */
469         hw->mbx.stats.msgs_rx++;
470
471 out_no_read:
472         return ret_val;
473 }
474
475 /**
476  *  e1000_init_mbx_params_pf - set initial values for pf mailbox
477  *  @hw: pointer to the HW structure
478  *
479  *  Initializes the hw->mbx struct to correct values for pf mailbox
480  */
481 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
482 {
483         struct e1000_mbx_info *mbx = &hw->mbx;
484
485         switch (hw->mac.type) {
486         case e1000_82576:
487         case e1000_i350:
488         case e1000_i354:
489                 mbx->timeout = 0;
490                 mbx->usec_delay = 0;
491
492                 mbx->size = E1000_VFMAILBOX_SIZE;
493
494                 mbx->ops.read = e1000_read_mbx_pf;
495                 mbx->ops.write = e1000_write_mbx_pf;
496                 mbx->ops.read_posted = e1000_read_posted_mbx;
497                 mbx->ops.write_posted = e1000_write_posted_mbx;
498                 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
499                 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
500                 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
501
502                 mbx->stats.msgs_tx = 0;
503                 mbx->stats.msgs_rx = 0;
504                 mbx->stats.reqs = 0;
505                 mbx->stats.acks = 0;
506                 mbx->stats.rsts = 0;
507         default:
508                 return E1000_SUCCESS;
509         }
510 }