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