1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "mbx.h"
29
30
31
32
33
34
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
50 if (!countdown)
51 mbx->timeout = 0;
52out:
53 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
54}
55
56
57
58
59
60
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
76 if (!countdown)
77 mbx->timeout = 0;
78out:
79 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
80}
81
82
83
84
85
86
87
88
89
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
102 if (!ret_val)
103 ret_val = mbx->ops.read(hw, msg, size);
104out:
105 return ret_val;
106}
107
108
109
110
111
112
113
114
115
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
123 if (!mbx->ops.write || !mbx->timeout)
124 goto out;
125
126
127 ret_val = mbx->ops.write(hw, msg, size);
128
129
130 if (!ret_val)
131 ret_val = e1000_poll_for_ack(hw);
132out:
133 return ret_val;
134}
135
136
137
138
139
140
141
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
155
156
157
158
159
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
176
177
178
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
194
195
196
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
212
213
214
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
231
232
233
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
242 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
243
244
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
257
258
259
260
261
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
271 err = e1000_obtain_mbx_lock_vf(hw);
272 if (err)
273 goto out_no_write;
274
275
276 e1000_check_for_ack_vf(hw);
277 e1000_check_for_msg_vf(hw);
278
279
280 for (i = 0; i < size; i++)
281 array_ew32(VMBMEM(0), i, msg[i]);
282
283
284 hw->mbx.stats.msgs_tx++;
285
286
287 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
288
289out_no_write:
290 return err;
291}
292
293
294
295
296
297
298
299
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
309 err = e1000_obtain_mbx_lock_vf(hw);
310 if (err)
311 goto out_no_read;
312
313
314 for (i = 0; i < size; i++)
315 msg[i] = array_er32(VMBMEM(0), i);
316
317
318 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
319
320
321 hw->mbx.stats.msgs_rx++;
322
323out_no_read:
324 return err;
325}
326
327
328
329
330
331
332
333s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
334{
335 struct e1000_mbx_info *mbx = &hw->mbx;
336
337
338
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}
361