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