Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2009 - 2018 Intel Corporation. */
3
4#include <linux/bitfield.h>
5#include "mbx.h"
6
7/**
8 * e1000_poll_for_msg - Wait for message notification
9 * @hw: pointer to the HW structure
10 *
11 * returns SUCCESS if it successfully received a message notification
12 **/
13static s32 e1000_poll_for_msg(struct e1000_hw *hw)
14{
15 struct e1000_mbx_info *mbx = &hw->mbx;
16 int countdown = mbx->timeout;
17
18 if (!mbx->ops.check_for_msg)
19 goto out;
20
21 while (countdown && mbx->ops.check_for_msg(hw)) {
22 countdown--;
23 udelay(mbx->usec_delay);
24 }
25
26 /* if we failed, all future posted messages fail until reset */
27 if (!countdown)
28 mbx->timeout = 0;
29out:
30 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
31}
32
33/**
34 * e1000_poll_for_ack - Wait for message acknowledgment
35 * @hw: pointer to the HW structure
36 *
37 * returns SUCCESS if it successfully received a message acknowledgment
38 **/
39static s32 e1000_poll_for_ack(struct e1000_hw *hw)
40{
41 struct e1000_mbx_info *mbx = &hw->mbx;
42 int countdown = mbx->timeout;
43
44 if (!mbx->ops.check_for_ack)
45 goto out;
46
47 while (countdown && mbx->ops.check_for_ack(hw)) {
48 countdown--;
49 udelay(mbx->usec_delay);
50 }
51
52 /* if we failed, all future posted messages fail until reset */
53 if (!countdown)
54 mbx->timeout = 0;
55out:
56 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
57}
58
59/**
60 * e1000_read_posted_mbx - Wait for message notification and receive message
61 * @hw: pointer to the HW structure
62 * @msg: The message buffer
63 * @size: Length of buffer
64 *
65 * returns SUCCESS if it successfully received a message notification and
66 * copied it into the receive buffer.
67 **/
68static s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
69{
70 struct e1000_mbx_info *mbx = &hw->mbx;
71 s32 ret_val = -E1000_ERR_MBX;
72
73 if (!mbx->ops.read)
74 goto out;
75
76 ret_val = e1000_poll_for_msg(hw);
77
78 /* if ack received read message, otherwise we timed out */
79 if (!ret_val)
80 ret_val = mbx->ops.read(hw, msg, size);
81out:
82 return ret_val;
83}
84
85/**
86 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
87 * @hw: pointer to the HW structure
88 * @msg: The message buffer
89 * @size: Length of buffer
90 *
91 * returns SUCCESS if it successfully copied message into the buffer and
92 * received an ack to that message within delay * timeout period
93 **/
94static s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
95{
96 struct e1000_mbx_info *mbx = &hw->mbx;
97 s32 ret_val = -E1000_ERR_MBX;
98
99 /* exit if we either can't write or there isn't a defined timeout */
100 if (!mbx->ops.write || !mbx->timeout)
101 goto out;
102
103 /* send msg*/
104 ret_val = mbx->ops.write(hw, msg, size);
105
106 /* if msg sent wait until we receive an ack */
107 if (!ret_val)
108 ret_val = e1000_poll_for_ack(hw);
109out:
110 return ret_val;
111}
112
113/**
114 * e1000_read_v2p_mailbox - read v2p mailbox
115 * @hw: pointer to the HW structure
116 *
117 * This function is used to read the v2p mailbox without losing the read to
118 * clear status bits.
119 **/
120static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
121{
122 u32 v2p_mailbox = er32(V2PMAILBOX(0));
123
124 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
125 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
126
127 return v2p_mailbox;
128}
129
130/**
131 * e1000_check_for_bit_vf - Determine if a status bit was set
132 * @hw: pointer to the HW structure
133 * @mask: bitmask for bits to be tested and cleared
134 *
135 * This function is used to check for the read to clear bits within
136 * the V2P mailbox.
137 **/
138static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
139{
140 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
141 s32 ret_val = -E1000_ERR_MBX;
142
143 if (v2p_mailbox & mask)
144 ret_val = E1000_SUCCESS;
145
146 hw->dev_spec.vf.v2p_mailbox &= ~mask;
147
148 return ret_val;
149}
150
151/**
152 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
153 * @hw: pointer to the HW structure
154 *
155 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
156 **/
157static s32 e1000_check_for_msg_vf(struct e1000_hw *hw)
158{
159 s32 ret_val = -E1000_ERR_MBX;
160
161 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
162 ret_val = E1000_SUCCESS;
163 hw->mbx.stats.reqs++;
164 }
165
166 return ret_val;
167}
168
169/**
170 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
171 * @hw: pointer to the HW structure
172 *
173 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
174 **/
175static s32 e1000_check_for_ack_vf(struct e1000_hw *hw)
176{
177 s32 ret_val = -E1000_ERR_MBX;
178
179 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
180 ret_val = E1000_SUCCESS;
181 hw->mbx.stats.acks++;
182 }
183
184 return ret_val;
185}
186
187/**
188 * e1000_check_for_rst_vf - checks to see if the PF has reset
189 * @hw: pointer to the HW structure
190 *
191 * returns true if the PF has set the reset done bit or else false
192 **/
193static s32 e1000_check_for_rst_vf(struct e1000_hw *hw)
194{
195 s32 ret_val = -E1000_ERR_MBX;
196
197 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
198 E1000_V2PMAILBOX_RSTI))) {
199 ret_val = E1000_SUCCESS;
200 hw->mbx.stats.rsts++;
201 }
202
203 return ret_val;
204}
205
206/**
207 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
208 * @hw: pointer to the HW structure
209 *
210 * return SUCCESS if we obtained the mailbox lock
211 **/
212static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
213{
214 s32 ret_val = -E1000_ERR_MBX;
215 int count = 10;
216
217 do {
218 /* Take ownership of the buffer */
219 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
220
221 /* reserve mailbox for VF use */
222 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
223 ret_val = 0;
224 break;
225 }
226 udelay(1000);
227 } while (count-- > 0);
228
229 return ret_val;
230}
231
232/**
233 * e1000_write_mbx_vf - Write a message to the mailbox
234 * @hw: pointer to the HW structure
235 * @msg: The message buffer
236 * @size: Length of buffer
237 *
238 * returns SUCCESS if it successfully copied message into the buffer
239 **/
240static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
241{
242 s32 err;
243 u16 i;
244
245 lockdep_assert_held(&hw->mbx_lock);
246
247 /* lock the mailbox to prevent pf/vf race condition */
248 err = e1000_obtain_mbx_lock_vf(hw);
249 if (err)
250 goto out_no_write;
251
252 /* flush any ack or msg as we are going to overwrite mailbox */
253 e1000_check_for_ack_vf(hw);
254 e1000_check_for_msg_vf(hw);
255
256 /* copy the caller specified message to the mailbox memory buffer */
257 for (i = 0; i < size; i++)
258 array_ew32(VMBMEM(0), i, msg[i]);
259
260 /* update stats */
261 hw->mbx.stats.msgs_tx++;
262
263 /* Drop VFU and interrupt the PF to tell it a message has been sent */
264 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
265
266out_no_write:
267 return err;
268}
269
270/**
271 * e1000_read_mbx_vf - Reads a message from the inbox intended for VF
272 * @hw: pointer to the HW structure
273 * @msg: The message buffer
274 * @size: Length of buffer
275 *
276 * returns SUCCESS if it successfully read message from buffer
277 **/
278static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
279{
280 s32 err;
281 u16 i;
282
283 lockdep_assert_held(&hw->mbx_lock);
284
285 /* lock the mailbox to prevent pf/vf race condition */
286 err = e1000_obtain_mbx_lock_vf(hw);
287 if (err)
288 goto out_no_read;
289
290 /* copy the message from the mailbox memory buffer */
291 for (i = 0; i < size; i++)
292 msg[i] = array_er32(VMBMEM(0), i);
293
294 /* Acknowledge receipt and release mailbox, then we're done */
295 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
296
297 /* update stats */
298 hw->mbx.stats.msgs_rx++;
299
300out_no_read:
301 return err;
302}
303
304/**
305 * e1000_init_mbx_params_vf - set initial values for VF mailbox
306 * @hw: pointer to the HW structure
307 *
308 * Initializes the hw->mbx struct to correct values for VF mailbox
309 */
310s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
311{
312 struct e1000_mbx_info *mbx = &hw->mbx;
313
314 /* start mailbox as timed out and let the reset_hw call set the timeout
315 * value to being communications
316 */
317 mbx->timeout = 0;
318 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
319
320 mbx->size = E1000_VFMAILBOX_SIZE;
321
322 mbx->ops.read = e1000_read_mbx_vf;
323 mbx->ops.write = e1000_write_mbx_vf;
324 mbx->ops.read_posted = e1000_read_posted_mbx;
325 mbx->ops.write_posted = e1000_write_posted_mbx;
326 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
327 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
328 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
329
330 mbx->stats.msgs_tx = 0;
331 mbx->stats.msgs_rx = 0;
332 mbx->stats.reqs = 0;
333 mbx->stats.acks = 0;
334 mbx->stats.rsts = 0;
335
336 return E1000_SUCCESS;
337}
1/*******************************************************************************
2
3 Intel(R) 82576 Virtual Function Linux driver
4 Copyright(c) 2009 - 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, see <http://www.gnu.org/licenses/>.
17
18 The full GNU General Public License is included in this distribution in
19 the file called "COPYING".
20
21 Contact Information:
22 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25*******************************************************************************/
26
27#include "mbx.h"
28
29/**
30 * e1000_poll_for_msg - Wait for message notification
31 * @hw: pointer to the HW structure
32 *
33 * returns SUCCESS if it successfully received a message notification
34 **/
35static s32 e1000_poll_for_msg(struct e1000_hw *hw)
36{
37 struct e1000_mbx_info *mbx = &hw->mbx;
38 int countdown = mbx->timeout;
39
40 if (!mbx->ops.check_for_msg)
41 goto out;
42
43 while (countdown && mbx->ops.check_for_msg(hw)) {
44 countdown--;
45 udelay(mbx->usec_delay);
46 }
47
48 /* if we failed, all future posted messages fail until reset */
49 if (!countdown)
50 mbx->timeout = 0;
51out:
52 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
53}
54
55/**
56 * e1000_poll_for_ack - Wait for message acknowledgment
57 * @hw: pointer to the HW structure
58 *
59 * returns SUCCESS if it successfully received a message acknowledgment
60 **/
61static s32 e1000_poll_for_ack(struct e1000_hw *hw)
62{
63 struct e1000_mbx_info *mbx = &hw->mbx;
64 int countdown = mbx->timeout;
65
66 if (!mbx->ops.check_for_ack)
67 goto out;
68
69 while (countdown && mbx->ops.check_for_ack(hw)) {
70 countdown--;
71 udelay(mbx->usec_delay);
72 }
73
74 /* if we failed, all future posted messages fail until reset */
75 if (!countdown)
76 mbx->timeout = 0;
77out:
78 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
79}
80
81/**
82 * e1000_read_posted_mbx - Wait for message notification and receive message
83 * @hw: pointer to the HW structure
84 * @msg: The message buffer
85 * @size: Length of buffer
86 *
87 * returns SUCCESS if it successfully received a message notification and
88 * copied it into the receive buffer.
89 **/
90static s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
91{
92 struct e1000_mbx_info *mbx = &hw->mbx;
93 s32 ret_val = -E1000_ERR_MBX;
94
95 if (!mbx->ops.read)
96 goto out;
97
98 ret_val = e1000_poll_for_msg(hw);
99
100 /* if ack received read message, otherwise we timed out */
101 if (!ret_val)
102 ret_val = mbx->ops.read(hw, msg, size);
103out:
104 return ret_val;
105}
106
107/**
108 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
109 * @hw: pointer to the HW structure
110 * @msg: The message buffer
111 * @size: Length of buffer
112 *
113 * returns SUCCESS if it successfully copied message into the buffer and
114 * received an ack to that message within delay * timeout period
115 **/
116static s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
117{
118 struct e1000_mbx_info *mbx = &hw->mbx;
119 s32 ret_val = -E1000_ERR_MBX;
120
121 /* exit if we either can't write or there isn't a defined timeout */
122 if (!mbx->ops.write || !mbx->timeout)
123 goto out;
124
125 /* send msg*/
126 ret_val = mbx->ops.write(hw, msg, size);
127
128 /* if msg sent wait until we receive an ack */
129 if (!ret_val)
130 ret_val = e1000_poll_for_ack(hw);
131out:
132 return ret_val;
133}
134
135/**
136 * e1000_read_v2p_mailbox - read v2p mailbox
137 * @hw: pointer to the HW structure
138 *
139 * This function is used to read the v2p mailbox without losing the read to
140 * clear status bits.
141 **/
142static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
143{
144 u32 v2p_mailbox = er32(V2PMAILBOX(0));
145
146 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
147 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
148
149 return v2p_mailbox;
150}
151
152/**
153 * e1000_check_for_bit_vf - Determine if a status bit was set
154 * @hw: pointer to the HW structure
155 * @mask: bitmask for bits to be tested and cleared
156 *
157 * This function is used to check for the read to clear bits within
158 * the V2P mailbox.
159 **/
160static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
161{
162 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
163 s32 ret_val = -E1000_ERR_MBX;
164
165 if (v2p_mailbox & mask)
166 ret_val = E1000_SUCCESS;
167
168 hw->dev_spec.vf.v2p_mailbox &= ~mask;
169
170 return ret_val;
171}
172
173/**
174 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
175 * @hw: pointer to the HW structure
176 *
177 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
178 **/
179static s32 e1000_check_for_msg_vf(struct e1000_hw *hw)
180{
181 s32 ret_val = -E1000_ERR_MBX;
182
183 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
184 ret_val = E1000_SUCCESS;
185 hw->mbx.stats.reqs++;
186 }
187
188 return ret_val;
189}
190
191/**
192 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
193 * @hw: pointer to the HW structure
194 *
195 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
196 **/
197static s32 e1000_check_for_ack_vf(struct e1000_hw *hw)
198{
199 s32 ret_val = -E1000_ERR_MBX;
200
201 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
202 ret_val = E1000_SUCCESS;
203 hw->mbx.stats.acks++;
204 }
205
206 return ret_val;
207}
208
209/**
210 * e1000_check_for_rst_vf - checks to see if the PF has reset
211 * @hw: pointer to the HW structure
212 *
213 * returns true if the PF has set the reset done bit or else false
214 **/
215static s32 e1000_check_for_rst_vf(struct e1000_hw *hw)
216{
217 s32 ret_val = -E1000_ERR_MBX;
218
219 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
220 E1000_V2PMAILBOX_RSTI))) {
221 ret_val = E1000_SUCCESS;
222 hw->mbx.stats.rsts++;
223 }
224
225 return ret_val;
226}
227
228/**
229 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
230 * @hw: pointer to the HW structure
231 *
232 * return SUCCESS if we obtained the mailbox lock
233 **/
234static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
235{
236 s32 ret_val = -E1000_ERR_MBX;
237 int count = 10;
238
239 do {
240 /* Take ownership of the buffer */
241 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
242
243 /* reserve mailbox for VF use */
244 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
245 ret_val = 0;
246 break;
247 }
248 udelay(1000);
249 } while (count-- > 0);
250
251 return ret_val;
252}
253
254/**
255 * e1000_write_mbx_vf - Write a message to the mailbox
256 * @hw: pointer to the HW structure
257 * @msg: The message buffer
258 * @size: Length of buffer
259 *
260 * returns SUCCESS if it successfully copied message into the buffer
261 **/
262static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
263{
264 s32 err;
265 u16 i;
266
267 /* lock the mailbox to prevent pf/vf race condition */
268 err = e1000_obtain_mbx_lock_vf(hw);
269 if (err)
270 goto out_no_write;
271
272 /* flush any ack or msg as we are going to overwrite mailbox */
273 e1000_check_for_ack_vf(hw);
274 e1000_check_for_msg_vf(hw);
275
276 /* copy the caller specified message to the mailbox memory buffer */
277 for (i = 0; i < size; i++)
278 array_ew32(VMBMEM(0), i, msg[i]);
279
280 /* update stats */
281 hw->mbx.stats.msgs_tx++;
282
283 /* Drop VFU and interrupt the PF to tell it a message has been sent */
284 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
285
286out_no_write:
287 return err;
288}
289
290/**
291 * e1000_read_mbx_vf - Reads a message from the inbox intended for VF
292 * @hw: pointer to the HW structure
293 * @msg: The message buffer
294 * @size: Length of buffer
295 *
296 * returns SUCCESS if it successfully read message from buffer
297 **/
298static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
299{
300 s32 err;
301 u16 i;
302
303 /* lock the mailbox to prevent pf/vf race condition */
304 err = e1000_obtain_mbx_lock_vf(hw);
305 if (err)
306 goto out_no_read;
307
308 /* copy the message from the mailbox memory buffer */
309 for (i = 0; i < size; i++)
310 msg[i] = array_er32(VMBMEM(0), i);
311
312 /* Acknowledge receipt and release mailbox, then we're done */
313 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
314
315 /* update stats */
316 hw->mbx.stats.msgs_rx++;
317
318out_no_read:
319 return err;
320}
321
322/**
323 * e1000_init_mbx_params_vf - set initial values for VF mailbox
324 * @hw: pointer to the HW structure
325 *
326 * Initializes the hw->mbx struct to correct values for VF mailbox
327 */
328s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
329{
330 struct e1000_mbx_info *mbx = &hw->mbx;
331
332 /* start mailbox as timed out and let the reset_hw call set the timeout
333 * value to being communications
334 */
335 mbx->timeout = 0;
336 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
337
338 mbx->size = E1000_VFMAILBOX_SIZE;
339
340 mbx->ops.read = e1000_read_mbx_vf;
341 mbx->ops.write = e1000_write_mbx_vf;
342 mbx->ops.read_posted = e1000_read_posted_mbx;
343 mbx->ops.write_posted = e1000_write_posted_mbx;
344 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
345 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
346 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
347
348 mbx->stats.msgs_tx = 0;
349 mbx->stats.msgs_rx = 0;
350 mbx->stats.reqs = 0;
351 mbx->stats.acks = 0;
352 mbx->stats.rsts = 0;
353
354 return E1000_SUCCESS;
355}