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