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