Merge branch 'for-3.10/core' of git://git.kernel.dk/linux-block
[cascardo/linux.git] / drivers / net / ethernet / intel / 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 "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  *  igb_read_mbx - Reads a message from the mailbox
32  *  @hw: pointer to the HW structure
33  *  @msg: The message buffer
34  *  @size: Length of buffer
35  *  @mbx_id: id of mailbox to read
36  *
37  *  returns SUCCESS if it successfully read message from buffer
38  **/
39 s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
40 {
41         struct e1000_mbx_info *mbx = &hw->mbx;
42         s32 ret_val = -E1000_ERR_MBX;
43
44         /* limit read to size of mailbox */
45         if (size > mbx->size)
46                 size = mbx->size;
47
48         if (mbx->ops.read)
49                 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
50
51         return ret_val;
52 }
53
54 /**
55  *  igb_write_mbx - Write a message to the mailbox
56  *  @hw: pointer to the HW structure
57  *  @msg: The message buffer
58  *  @size: Length of buffer
59  *  @mbx_id: id of mailbox to write
60  *
61  *  returns SUCCESS if it successfully copied message into the buffer
62  **/
63 s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
64 {
65         struct e1000_mbx_info *mbx = &hw->mbx;
66         s32 ret_val = 0;
67
68         if (size > mbx->size)
69                 ret_val = -E1000_ERR_MBX;
70
71         else if (mbx->ops.write)
72                 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
73
74         return ret_val;
75 }
76
77 /**
78  *  igb_check_for_msg - checks to see if someone sent us mail
79  *  @hw: pointer to the HW structure
80  *  @mbx_id: id of mailbox to check
81  *
82  *  returns SUCCESS if the Status bit was found or else ERR_MBX
83  **/
84 s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
85 {
86         struct e1000_mbx_info *mbx = &hw->mbx;
87         s32 ret_val = -E1000_ERR_MBX;
88
89         if (mbx->ops.check_for_msg)
90                 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
91
92         return ret_val;
93 }
94
95 /**
96  *  igb_check_for_ack - checks to see if someone sent us ACK
97  *  @hw: pointer to the HW structure
98  *  @mbx_id: id of mailbox to check
99  *
100  *  returns SUCCESS if the Status bit was found or else ERR_MBX
101  **/
102 s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
103 {
104         struct e1000_mbx_info *mbx = &hw->mbx;
105         s32 ret_val = -E1000_ERR_MBX;
106
107         if (mbx->ops.check_for_ack)
108                 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
109
110         return ret_val;
111 }
112
113 /**
114  *  igb_check_for_rst - checks to see if other side has reset
115  *  @hw: pointer to the HW structure
116  *  @mbx_id: id of mailbox to check
117  *
118  *  returns SUCCESS if the Status bit was found or else ERR_MBX
119  **/
120 s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
121 {
122         struct e1000_mbx_info *mbx = &hw->mbx;
123         s32 ret_val = -E1000_ERR_MBX;
124
125         if (mbx->ops.check_for_rst)
126                 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
127
128         return ret_val;
129 }
130
131 /**
132  *  igb_poll_for_msg - Wait for message notification
133  *  @hw: pointer to the HW structure
134  *  @mbx_id: id of mailbox to write
135  *
136  *  returns SUCCESS if it successfully received a message notification
137  **/
138 static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
139 {
140         struct e1000_mbx_info *mbx = &hw->mbx;
141         int countdown = mbx->timeout;
142
143         if (!countdown || !mbx->ops.check_for_msg)
144                 goto out;
145
146         while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
147                 countdown--;
148                 if (!countdown)
149                         break;
150                 udelay(mbx->usec_delay);
151         }
152
153         /* if we failed, all future posted messages fail until reset */
154         if (!countdown)
155                 mbx->timeout = 0;
156 out:
157         return countdown ? 0 : -E1000_ERR_MBX;
158 }
159
160 /**
161  *  igb_poll_for_ack - Wait for message acknowledgement
162  *  @hw: pointer to the HW structure
163  *  @mbx_id: id of mailbox to write
164  *
165  *  returns SUCCESS if it successfully received a message acknowledgement
166  **/
167 static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
168 {
169         struct e1000_mbx_info *mbx = &hw->mbx;
170         int countdown = mbx->timeout;
171
172         if (!countdown || !mbx->ops.check_for_ack)
173                 goto out;
174
175         while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
176                 countdown--;
177                 if (!countdown)
178                         break;
179                 udelay(mbx->usec_delay);
180         }
181
182         /* if we failed, all future posted messages fail until reset */
183         if (!countdown)
184                 mbx->timeout = 0;
185 out:
186         return countdown ? 0 : -E1000_ERR_MBX;
187 }
188
189 /**
190  *  igb_read_posted_mbx - Wait for message notification and receive message
191  *  @hw: pointer to the HW structure
192  *  @msg: The message buffer
193  *  @size: Length of buffer
194  *  @mbx_id: id of mailbox to write
195  *
196  *  returns SUCCESS if it successfully received a message notification and
197  *  copied it into the receive buffer.
198  **/
199 static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
200                                u16 mbx_id)
201 {
202         struct e1000_mbx_info *mbx = &hw->mbx;
203         s32 ret_val = -E1000_ERR_MBX;
204
205         if (!mbx->ops.read)
206                 goto out;
207
208         ret_val = igb_poll_for_msg(hw, mbx_id);
209
210         if (!ret_val)
211                 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
212 out:
213         return ret_val;
214 }
215
216 /**
217  *  igb_write_posted_mbx - Write a message to the mailbox, wait for ack
218  *  @hw: pointer to the HW structure
219  *  @msg: The message buffer
220  *  @size: Length of buffer
221  *  @mbx_id: id of mailbox to write
222  *
223  *  returns SUCCESS if it successfully copied message into the buffer and
224  *  received an ack to that message within delay * timeout period
225  **/
226 static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size,
227                                 u16 mbx_id)
228 {
229         struct e1000_mbx_info *mbx = &hw->mbx;
230         s32 ret_val = -E1000_ERR_MBX;
231
232         /* exit if either we can't write or there isn't a defined timeout */
233         if (!mbx->ops.write || !mbx->timeout)
234                 goto out;
235
236         /* send msg */
237         ret_val = mbx->ops.write(hw, msg, size, mbx_id);
238
239         /* if msg sent wait until we receive an ack */
240         if (!ret_val)
241                 ret_val = igb_poll_for_ack(hw, mbx_id);
242 out:
243         return ret_val;
244 }
245
246 static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
247 {
248         u32 mbvficr = rd32(E1000_MBVFICR);
249         s32 ret_val = -E1000_ERR_MBX;
250
251         if (mbvficr & mask) {
252                 ret_val = 0;
253                 wr32(E1000_MBVFICR, mask);
254         }
255
256         return ret_val;
257 }
258
259 /**
260  *  igb_check_for_msg_pf - checks to see if the VF has sent mail
261  *  @hw: pointer to the HW structure
262  *  @vf_number: the VF index
263  *
264  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
265  **/
266 static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
267 {
268         s32 ret_val = -E1000_ERR_MBX;
269
270         if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
271                 ret_val = 0;
272                 hw->mbx.stats.reqs++;
273         }
274
275         return ret_val;
276 }
277
278 /**
279  *  igb_check_for_ack_pf - checks to see if the VF has ACKed
280  *  @hw: pointer to the HW structure
281  *  @vf_number: the VF index
282  *
283  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
284  **/
285 static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
286 {
287         s32 ret_val = -E1000_ERR_MBX;
288
289         if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
290                 ret_val = 0;
291                 hw->mbx.stats.acks++;
292         }
293
294         return ret_val;
295 }
296
297 /**
298  *  igb_check_for_rst_pf - checks to see if the VF has reset
299  *  @hw: pointer to the HW structure
300  *  @vf_number: the VF index
301  *
302  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
303  **/
304 static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
305 {
306         u32 vflre = rd32(E1000_VFLRE);
307         s32 ret_val = -E1000_ERR_MBX;
308
309         if (vflre & (1 << vf_number)) {
310                 ret_val = 0;
311                 wr32(E1000_VFLRE, (1 << vf_number));
312                 hw->mbx.stats.rsts++;
313         }
314
315         return ret_val;
316 }
317
318 /**
319  *  igb_obtain_mbx_lock_pf - obtain mailbox lock
320  *  @hw: pointer to the HW structure
321  *  @vf_number: the VF index
322  *
323  *  return SUCCESS if we obtained the mailbox lock
324  **/
325 static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
326 {
327         s32 ret_val = -E1000_ERR_MBX;
328         u32 p2v_mailbox;
329
330         /* Take ownership of the buffer */
331         wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
332
333         /* reserve mailbox for vf use */
334         p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number));
335         if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
336                 ret_val = 0;
337
338         return ret_val;
339 }
340
341 /**
342  *  igb_write_mbx_pf - Places a message in the mailbox
343  *  @hw: pointer to the HW structure
344  *  @msg: The message buffer
345  *  @size: Length of buffer
346  *  @vf_number: the VF index
347  *
348  *  returns SUCCESS if it successfully copied message into the buffer
349  **/
350 static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
351                             u16 vf_number)
352 {
353         s32 ret_val;
354         u16 i;
355
356         /* lock the mailbox to prevent pf/vf race condition */
357         ret_val = igb_obtain_mbx_lock_pf(hw, vf_number);
358         if (ret_val)
359                 goto out_no_write;
360
361         /* flush msg and acks as we are overwriting the message buffer */
362         igb_check_for_msg_pf(hw, vf_number);
363         igb_check_for_ack_pf(hw, vf_number);
364
365         /* copy the caller specified message to the mailbox memory buffer */
366         for (i = 0; i < size; i++)
367                 array_wr32(E1000_VMBMEM(vf_number), i, msg[i]);
368
369         /* Interrupt VF to tell it a message has been sent and release buffer*/
370         wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
371
372         /* update stats */
373         hw->mbx.stats.msgs_tx++;
374
375 out_no_write:
376         return ret_val;
377
378 }
379
380 /**
381  *  igb_read_mbx_pf - Read a message from the mailbox
382  *  @hw: pointer to the HW structure
383  *  @msg: The message buffer
384  *  @size: Length of buffer
385  *  @vf_number: the VF index
386  *
387  *  This function copies a message from the mailbox buffer to the caller's
388  *  memory buffer.  The presumption is that the caller knows that there was
389  *  a message due to a VF request so no polling for message is needed.
390  **/
391 static s32 igb_read_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_read;
401
402         /* copy the message to the mailbox memory buffer */
403         for (i = 0; i < size; i++)
404                 msg[i] = array_rd32(E1000_VMBMEM(vf_number), i);
405
406         /* Acknowledge the message and release buffer */
407         wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
408
409         /* update stats */
410         hw->mbx.stats.msgs_rx++;
411
412 out_no_read:
413         return ret_val;
414 }
415
416 /**
417  *  e1000_init_mbx_params_pf - set initial values for pf mailbox
418  *  @hw: pointer to the HW structure
419  *
420  *  Initializes the hw->mbx struct to correct values for pf mailbox
421  */
422 s32 igb_init_mbx_params_pf(struct e1000_hw *hw)
423 {
424         struct e1000_mbx_info *mbx = &hw->mbx;
425
426         mbx->timeout = 0;
427         mbx->usec_delay = 0;
428
429         mbx->size = E1000_VFMAILBOX_SIZE;
430
431         mbx->ops.read = igb_read_mbx_pf;
432         mbx->ops.write = igb_write_mbx_pf;
433         mbx->ops.read_posted = igb_read_posted_mbx;
434         mbx->ops.write_posted = igb_write_posted_mbx;
435         mbx->ops.check_for_msg = igb_check_for_msg_pf;
436         mbx->ops.check_for_ack = igb_check_for_ack_pf;
437         mbx->ops.check_for_rst = igb_check_for_rst_pf;
438
439         mbx->stats.msgs_tx = 0;
440         mbx->stats.msgs_rx = 0;
441         mbx->stats.reqs = 0;
442         mbx->stats.acks = 0;
443         mbx->stats.rsts = 0;
444
445         return 0;
446 }
447