1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2025 Cisco Systems, Inc. All rights reserved.
3
4 #include <linux/kernel.h>
5 #include <linux/netdevice.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/delay.h>
8 #include <linux/completion.h>
9
10 #include "vnic_dev.h"
11 #include "vnic_wq.h"
12 #include "vnic_cq.h"
13 #include "enic.h"
14 #include "enic_admin.h"
15 #include "enic_mbox.h"
16 #include "wq_enet_desc.h"
17
18 #define ENIC_MBOX_POLL_TIMEOUT_US 5000000
19 #define ENIC_MBOX_POLL_INTERVAL_US 100
20
enic_mbox_fill_hdr(struct enic * enic,struct enic_mbox_hdr * hdr,u8 msg_type,u16 dst_vnic_id,u16 msg_len)21 static void enic_mbox_fill_hdr(struct enic *enic, struct enic_mbox_hdr *hdr,
22 u8 msg_type, u16 dst_vnic_id, u16 msg_len)
23 {
24 memset(hdr, 0, sizeof(*hdr));
25 hdr->dst_vnic_id = cpu_to_le16(dst_vnic_id);
26 hdr->msg_type = msg_type;
27 hdr->msg_len = cpu_to_le16(msg_len);
28 hdr->msg_num = cpu_to_le64(++enic->mbox_msg_num);
29 }
30
enic_mbox_send_msg(struct enic * enic,u8 msg_type,u16 dst_vnic_id,void * payload,u16 payload_len)31 int enic_mbox_send_msg(struct enic *enic, u8 msg_type, u16 dst_vnic_id,
32 void *payload, u16 payload_len)
33 {
34 size_t total_len = sizeof(struct enic_mbox_hdr) + payload_len;
35 struct vnic_wq *wq = &enic->admin_wq;
36 struct wq_enet_desc *desc;
37 unsigned long timeout;
38 dma_addr_t dma_addr;
39 u16 vlan_tag;
40 void *buf;
41 int err;
42
43 /* Reject payloads that cannot fit in a single admin buffer. Checked
44 * before taking mbox_lock; total_len is computed as size_t so the
45 * sizeof() + payload_len sum cannot wrap.
46 */
47 if (payload_len > ENIC_ADMIN_BUF_SIZE - sizeof(struct enic_mbox_hdr))
48 return -EINVAL;
49
50 /* Serialize MBOX sends. The admin channel is a low-frequency
51 * control path; holding the mutex across the poll is acceptable.
52 */
53 mutex_lock(&enic->mbox_lock);
54
55 if (!enic->has_admin_channel || READ_ONCE(enic->mbox_send_disabled)) {
56 err = -ENODEV;
57 goto unlock;
58 }
59
60 if (vnic_wq_desc_avail(wq) == 0) {
61 err = -ENOSPC;
62 goto unlock;
63 }
64
65 buf = kmalloc(total_len, GFP_KERNEL);
66 if (!buf) {
67 err = -ENOMEM;
68 goto unlock;
69 }
70
71 enic_mbox_fill_hdr(enic, buf, msg_type, dst_vnic_id, total_len);
72 if (payload_len) {
73 void *dst = buf + sizeof(struct enic_mbox_hdr);
74
75 memcpy(dst, payload, payload_len);
76 }
77
78 dma_addr = dma_map_single(&enic->pdev->dev, buf, total_len,
79 DMA_TO_DEVICE);
80 if (dma_mapping_error(&enic->pdev->dev, dma_addr)) {
81 kfree(buf);
82 err = -ENOMEM;
83 goto unlock;
84 }
85
86 /* Firmware uses vlan field for routing: 0 = PF, 1-based = VF index */
87 if (dst_vnic_id == ENIC_MBOX_DST_PF)
88 vlan_tag = 0;
89 else
90 vlan_tag = dst_vnic_id + 1;
91
92 desc = vnic_wq_next_desc(wq);
93 wq_enet_desc_enc(desc, (u64)dma_addr | VNIC_PADDR_TARGET,
94 total_len,
95 0, 0, 0, /* mss, hdr_len, offload_mode */
96 1, 1, /* eop, cq_entry */
97 0, /* fcoe_encap */
98 1, vlan_tag, /* vlan_tag_insert, vlan_tag */
99 0); /* loopback */
100 vnic_wq_post(wq, buf, dma_addr, total_len,
101 1, 1, /* sop, eop */
102 1, 1, /* desc_skip_cnt, cq_entry */
103 0, 0); /* compressed_send, wrid */
104 vnic_wq_doorbell(wq);
105
106 timeout = jiffies + usecs_to_jiffies(ENIC_MBOX_POLL_TIMEOUT_US);
107 err = -ETIMEDOUT;
108 while (time_before(jiffies, timeout)) {
109 if (enic_admin_wq_cq_service(enic)) {
110 err = 0;
111 break;
112 }
113 usleep_range(ENIC_MBOX_POLL_INTERVAL_US,
114 ENIC_MBOX_POLL_INTERVAL_US + 50);
115 }
116 /* Final check in case completion arrived during the last sleep */
117 if (err && enic_admin_wq_cq_service(enic))
118 err = 0;
119
120 if (!err) {
121 wq->to_clean = wq->to_clean->next;
122 wq->ring.desc_avail++;
123 dma_unmap_single(&enic->pdev->dev, dma_addr, total_len,
124 DMA_TO_DEVICE);
125 kfree(buf);
126 } else {
127 netdev_err(enic->netdev,
128 "MBOX send timed out (type %u dst %u), disabling channel\n",
129 msg_type, dst_vnic_id);
130 /*
131 * The WQ descriptor is still live in hardware. Do not unmap
132 * or free the buffer: the device may still DMA from dma_addr.
133 * Mark the channel unusable so no further sends are attempted.
134 */
135 WRITE_ONCE(enic->mbox_send_disabled, true);
136 }
137
138 netdev_dbg(enic->netdev,
139 "MBOX send msg_type %u dst %u vlan %u err %d\n",
140 msg_type, dst_vnic_id, vlan_tag, err);
141 unlock:
142 mutex_unlock(&enic->mbox_lock);
143 return err;
144 }
145
enic_mbox_wait_reply(struct enic * enic,unsigned long timeout_ms)146 static int enic_mbox_wait_reply(struct enic *enic, unsigned long timeout_ms)
147 {
148 unsigned long left;
149
150 left = wait_for_completion_timeout(&enic->mbox_comp,
151 msecs_to_jiffies(timeout_ms));
152
153 return left ? 0 : -ETIMEDOUT;
154 }
155
enic_mbox_send_link_state(struct enic * enic,u16 vf_id,u32 link_state)156 int enic_mbox_send_link_state(struct enic *enic, u16 vf_id, u32 link_state)
157 {
158 struct enic_mbox_pf_link_state_notif_msg notif = {};
159
160 if (!enic->vf_state || vf_id >= enic->num_vfs ||
161 !enic->vf_state[vf_id].registered) {
162 netdev_dbg(enic->netdev,
163 "MBOX: skip link state to unregistered VF %u\n",
164 vf_id);
165 return 0;
166 }
167
168 notif.link_state = cpu_to_le32(link_state);
169 return enic_mbox_send_msg(enic, ENIC_MBOX_PF_LINK_STATE_NOTIF, vf_id,
170 ¬if, sizeof(notif));
171 }
172
enic_mbox_pf_handle_capability(struct enic * enic,void * msg,u16 vf_id,u64 msg_num)173 static int enic_mbox_pf_handle_capability(struct enic *enic, void *msg,
174 u16 vf_id, u64 msg_num)
175 {
176 struct enic_mbox_vf_capability_reply_msg reply = {};
177
178 reply.reply.ret_major = cpu_to_le16(0);
179 reply.version = cpu_to_le32(ENIC_MBOX_CAP_VERSION_1);
180
181 return enic_mbox_send_msg(enic, ENIC_MBOX_VF_CAPABILITY_REPLY, vf_id,
182 &reply, sizeof(reply));
183 }
184
enic_mbox_pf_handle_register(struct enic * enic,void * msg,u16 vf_id,u64 msg_num)185 static int enic_mbox_pf_handle_register(struct enic *enic, void *msg,
186 u16 vf_id, u64 msg_num)
187 {
188 struct enic_mbox_vf_register_reply_msg reply = {};
189 u32 link_state;
190 int err;
191
192 if (!enic->vf_state || vf_id >= enic->num_vfs) {
193 if (net_ratelimit())
194 netdev_warn(enic->netdev,
195 "MBOX: register from invalid VF %u\n",
196 vf_id);
197 return -EINVAL;
198 }
199
200 /* VF re-registering (e.g. guest reboot without clean unregister):
201 * mark the previous registration inactive before accepting the new one.
202 */
203 if (enic->vf_state[vf_id].registered) {
204 netdev_dbg(enic->netdev,
205 "MBOX: VF %u re-register, cleaning previous state\n",
206 vf_id);
207 enic->vf_state[vf_id].registered = false;
208 }
209
210 reply.reply.ret_major = cpu_to_le16(0);
211 err = enic_mbox_send_msg(enic, ENIC_MBOX_VF_REGISTER_REPLY, vf_id,
212 &reply, sizeof(reply));
213 if (err)
214 return err;
215
216 enic->vf_state[vf_id].registered = true;
217 if (net_ratelimit())
218 netdev_info(enic->netdev, "VF %u registered via MBOX\n", vf_id);
219
220 link_state = netif_carrier_ok(enic->netdev) ?
221 ENIC_MBOX_LINK_STATE_ENABLE :
222 ENIC_MBOX_LINK_STATE_DISABLE;
223 err = enic_mbox_send_link_state(enic, vf_id, link_state);
224 if (err && net_ratelimit())
225 netdev_warn(enic->netdev,
226 "VF %u: failed to send initial link state: %d\n",
227 vf_id, err);
228 /* Registration succeeded; initial link state notification attempted
229 * above. Subsequent link state changes are sent from the PF
230 * when enic_link_check() detects carrier changes.
231 */
232 return 0;
233 }
234
enic_mbox_pf_handle_unregister(struct enic * enic,void * msg,u16 vf_id,u64 msg_num)235 static int enic_mbox_pf_handle_unregister(struct enic *enic, void *msg,
236 u16 vf_id, u64 msg_num)
237 {
238 struct enic_mbox_vf_register_reply_msg reply = {};
239 int err;
240
241 if (!enic->vf_state || vf_id >= enic->num_vfs) {
242 if (net_ratelimit())
243 netdev_warn(enic->netdev,
244 "MBOX: unregister from invalid VF %u\n",
245 vf_id);
246 return -EINVAL;
247 }
248
249 /* VF is unloading; clear local state regardless of whether
250 * the reply is successfully delivered to avoid the PF treating
251 * a dead VF as still registered.
252 */
253 enic->vf_state[vf_id].registered = false;
254
255 reply.reply.ret_major = cpu_to_le16(0);
256 err = enic_mbox_send_msg(enic, ENIC_MBOX_VF_UNREGISTER_REPLY, vf_id,
257 &reply, sizeof(reply));
258
259 if (net_ratelimit())
260 netdev_info(enic->netdev,
261 "VF %u unregistered via MBOX\n", vf_id);
262
263 return err;
264 }
265
enic_mbox_pf_process_msg(struct enic * enic,struct enic_mbox_hdr * hdr,void * payload)266 static void enic_mbox_pf_process_msg(struct enic *enic,
267 struct enic_mbox_hdr *hdr, void *payload)
268 {
269 u16 vf_id = le16_to_cpu(hdr->src_vnic_id);
270 u16 msg_len = le16_to_cpu(hdr->msg_len);
271 int err = 0;
272
273 if (!enic->vf_state) {
274 netdev_dbg(enic->netdev,
275 "MBOX: PF received msg but SRIOV not active\n");
276 return;
277 }
278
279 if (vf_id >= enic->num_vfs) {
280 if (net_ratelimit())
281 netdev_warn(enic->netdev,
282 "MBOX: PF received msg from invalid VF %u\n",
283 vf_id);
284 return;
285 }
286
287 switch (hdr->msg_type) {
288 case ENIC_MBOX_VF_CAPABILITY_REQUEST:
289 err = enic_mbox_pf_handle_capability(enic, payload, vf_id,
290 le64_to_cpu(hdr->msg_num));
291 break;
292 case ENIC_MBOX_VF_REGISTER_REQUEST:
293 err = enic_mbox_pf_handle_register(enic, payload, vf_id,
294 le64_to_cpu(hdr->msg_num));
295 break;
296 case ENIC_MBOX_VF_UNREGISTER_REQUEST:
297 err = enic_mbox_pf_handle_unregister(enic, payload, vf_id,
298 le64_to_cpu(hdr->msg_num));
299 break;
300 case ENIC_MBOX_PF_LINK_STATE_ACK: {
301 struct enic_mbox_pf_link_state_ack_msg *ack = payload;
302
303 if (msg_len < sizeof(*hdr) + sizeof(*ack))
304 break;
305 if (le16_to_cpu(ack->ack.ret_major) && net_ratelimit())
306 netdev_warn(enic->netdev,
307 "MBOX: VF %u link state ACK error %u/%u\n",
308 vf_id,
309 le16_to_cpu(ack->ack.ret_major),
310 le16_to_cpu(ack->ack.ret_minor));
311 break;
312 }
313 default:
314 netdev_dbg(enic->netdev,
315 "MBOX: PF unhandled msg type %u from VF %u\n",
316 hdr->msg_type, vf_id);
317 err = -EOPNOTSUPP;
318 break;
319 }
320
321 if (err && net_ratelimit())
322 netdev_warn(enic->netdev,
323 "MBOX: PF handler for msg type %u from VF %u failed: %d\n",
324 hdr->msg_type, vf_id, err);
325 }
326
enic_mbox_vf_handle_capability_reply(struct enic * enic,void * payload)327 static void enic_mbox_vf_handle_capability_reply(struct enic *enic,
328 void *payload)
329 {
330 struct enic_mbox_vf_capability_reply_msg *reply = payload;
331
332 if (READ_ONCE(enic->mbox_expected_reply) != ENIC_MBOX_VF_CAPABILITY_REPLY) {
333 netdev_warn(enic->netdev,
334 "MBOX: stale capability reply (expected %u), drop\n",
335 READ_ONCE(enic->mbox_expected_reply));
336 return;
337 }
338
339 if (le16_to_cpu(reply->reply.ret_major) == 0)
340 enic->pf_cap_version = le32_to_cpu(reply->version);
341 else
342 netdev_warn(enic->netdev,
343 "MBOX: PF rejected capability request: %u/%u\n",
344 le16_to_cpu(reply->reply.ret_major),
345 le16_to_cpu(reply->reply.ret_minor));
346 complete(&enic->mbox_comp);
347 }
348
enic_mbox_vf_handle_register_reply(struct enic * enic,void * payload)349 static void enic_mbox_vf_handle_register_reply(struct enic *enic,
350 void *payload)
351 {
352 struct enic_mbox_vf_register_reply_msg *reply = payload;
353
354 if (READ_ONCE(enic->mbox_expected_reply) != ENIC_MBOX_VF_REGISTER_REPLY) {
355 netdev_warn(enic->netdev,
356 "MBOX: stale register reply (expected %u), drop\n",
357 READ_ONCE(enic->mbox_expected_reply));
358 return;
359 }
360
361 if (le16_to_cpu(reply->reply.ret_major)) {
362 netdev_warn(enic->netdev,
363 "MBOX: VF register rejected by PF: %u/%u\n",
364 le16_to_cpu(reply->reply.ret_major),
365 le16_to_cpu(reply->reply.ret_minor));
366 } else {
367 enic->vf_registered = true;
368 }
369 complete(&enic->mbox_comp);
370 }
371
enic_mbox_vf_handle_unregister_reply(struct enic * enic,void * payload)372 static void enic_mbox_vf_handle_unregister_reply(struct enic *enic,
373 void *payload)
374 {
375 struct enic_mbox_vf_register_reply_msg *reply = payload;
376
377 if (READ_ONCE(enic->mbox_expected_reply) != ENIC_MBOX_VF_UNREGISTER_REPLY) {
378 netdev_warn(enic->netdev,
379 "MBOX: stale unregister reply (expected %u), drop\n",
380 READ_ONCE(enic->mbox_expected_reply));
381 return;
382 }
383
384 if (le16_to_cpu(reply->reply.ret_major)) {
385 netdev_warn(enic->netdev,
386 "MBOX: VF unregister rejected by PF: %u/%u\n",
387 le16_to_cpu(reply->reply.ret_major),
388 le16_to_cpu(reply->reply.ret_minor));
389 } else {
390 enic->vf_registered = false;
391 }
392 complete(&enic->mbox_comp);
393 }
394
enic_mbox_vf_handle_link_state(struct enic * enic,void * payload)395 static void enic_mbox_vf_handle_link_state(struct enic *enic, void *payload)
396 {
397 struct enic_mbox_pf_link_state_notif_msg *notif = payload;
398 struct enic_mbox_pf_link_state_ack_msg ack = {};
399 int err;
400
401 switch (le32_to_cpu(notif->link_state)) {
402 case ENIC_MBOX_LINK_STATE_ENABLE:
403 if (!netif_carrier_ok(enic->netdev))
404 netif_carrier_on(enic->netdev);
405 netdev_dbg(enic->netdev, "MBOX: link state -> UP\n");
406 break;
407 case ENIC_MBOX_LINK_STATE_DISABLE:
408 if (netif_carrier_ok(enic->netdev))
409 netif_carrier_off(enic->netdev);
410 netdev_dbg(enic->netdev, "MBOX: link state -> DOWN\n");
411 break;
412 default:
413 netdev_warn(enic->netdev, "MBOX: unknown link state %u\n",
414 le32_to_cpu(notif->link_state));
415 ack.ack.ret_major = cpu_to_le16(ENIC_MBOX_ERR_GENERIC);
416 break;
417 }
418
419 err = enic_mbox_send_msg(enic, ENIC_MBOX_PF_LINK_STATE_ACK,
420 ENIC_MBOX_DST_PF, &ack, sizeof(ack));
421 if (err && net_ratelimit())
422 netdev_warn(enic->netdev,
423 "MBOX: failed to send link state ACK: %d\n", err);
424 }
425
enic_mbox_vf_payload_ok(struct enic * enic,u8 msg_type,u16 payload_len,size_t min_len)426 static bool enic_mbox_vf_payload_ok(struct enic *enic, u8 msg_type,
427 u16 payload_len, size_t min_len)
428 {
429 if (payload_len < min_len) {
430 netdev_warn(enic->netdev,
431 "MBOX: short payload for type %u (%u < %zu)\n",
432 msg_type, payload_len, min_len);
433 return false;
434 }
435 return true;
436 }
437
enic_mbox_vf_process_msg(struct enic * enic,struct enic_mbox_hdr * hdr,void * payload,u16 payload_len)438 static void enic_mbox_vf_process_msg(struct enic *enic,
439 struct enic_mbox_hdr *hdr, void *payload,
440 u16 payload_len)
441 {
442 switch (hdr->msg_type) {
443 case ENIC_MBOX_VF_CAPABILITY_REPLY: {
444 size_t exp = sizeof(struct enic_mbox_vf_capability_reply_msg);
445
446 if (!enic_mbox_vf_payload_ok(enic, hdr->msg_type,
447 payload_len, exp))
448 return;
449 enic_mbox_vf_handle_capability_reply(enic, payload);
450 break;
451 }
452 case ENIC_MBOX_VF_REGISTER_REPLY: {
453 size_t exp = sizeof(struct enic_mbox_vf_register_reply_msg);
454
455 if (!enic_mbox_vf_payload_ok(enic, hdr->msg_type,
456 payload_len, exp))
457 return;
458 enic_mbox_vf_handle_register_reply(enic, payload);
459 break;
460 }
461 case ENIC_MBOX_VF_UNREGISTER_REPLY: {
462 size_t exp = sizeof(struct enic_mbox_vf_register_reply_msg);
463
464 if (!enic_mbox_vf_payload_ok(enic, hdr->msg_type,
465 payload_len, exp))
466 return;
467 enic_mbox_vf_handle_unregister_reply(enic, payload);
468 break;
469 }
470 case ENIC_MBOX_PF_LINK_STATE_NOTIF: {
471 size_t exp = sizeof(struct enic_mbox_pf_link_state_notif_msg);
472
473 if (!enic_mbox_vf_payload_ok(enic, hdr->msg_type,
474 payload_len, exp))
475 return;
476 enic_mbox_vf_handle_link_state(enic, payload);
477 break;
478 }
479 default:
480 netdev_dbg(enic->netdev,
481 "MBOX: VF unhandled msg type %u\n",
482 hdr->msg_type);
483 break;
484 }
485 }
486
enic_mbox_recv_handler(struct enic * enic,void * buf,unsigned int len)487 static void enic_mbox_recv_handler(struct enic *enic, void *buf,
488 unsigned int len)
489 {
490 struct enic_mbox_hdr *hdr = buf;
491 void *payload;
492 u16 msg_len;
493
494 if (len < sizeof(*hdr)) {
495 if (net_ratelimit())
496 netdev_warn(enic->netdev,
497 "MBOX: truncated message (len %u < %zu)\n",
498 len, sizeof(*hdr));
499 return;
500 }
501
502 if (hdr->msg_type >= ENIC_MBOX_MAX) {
503 if (net_ratelimit())
504 netdev_warn(enic->netdev,
505 "MBOX: unknown msg type %u\n",
506 hdr->msg_type);
507 return;
508 }
509
510 msg_len = le16_to_cpu(hdr->msg_len);
511 if (msg_len < sizeof(*hdr) || msg_len > len) {
512 if (net_ratelimit())
513 netdev_warn(enic->netdev,
514 "MBOX: invalid msg_len %u (buf len %u)\n",
515 msg_len, len);
516 return;
517 }
518
519 netdev_dbg(enic->netdev,
520 "MBOX recv: type %u from vnic %u len %u\n",
521 hdr->msg_type, le16_to_cpu(hdr->src_vnic_id), msg_len);
522
523 payload = buf + sizeof(*hdr);
524
525 if (enic->vf_state) {
526 enic_mbox_pf_process_msg(enic, hdr, payload);
527 } else if (le16_to_cpu(hdr->src_vnic_id) == ENIC_MBOX_DST_PF) {
528 /* src_vnic_id was overwritten from the hardware-verified CQ
529 * VLAN sender field, so a VF only accepts messages that the
530 * adapter attributes to the PF. Its sole admin-channel peer is
531 * the PF; drop anything else as a spoofed notification.
532 */
533 enic_mbox_vf_process_msg(enic, hdr, payload,
534 msg_len - (u16)sizeof(*hdr));
535 } else if (net_ratelimit()) {
536 netdev_warn(enic->netdev,
537 "MBOX: VF dropping non-PF message from vnic %u\n",
538 le16_to_cpu(hdr->src_vnic_id));
539 }
540 }
541
enic_mbox_vf_capability_check(struct enic * enic)542 int enic_mbox_vf_capability_check(struct enic *enic)
543 {
544 struct enic_mbox_vf_capability_msg req = {};
545 int err;
546
547 enic->pf_cap_version = 0;
548 reinit_completion(&enic->mbox_comp);
549 WRITE_ONCE(enic->mbox_expected_reply, ENIC_MBOX_VF_CAPABILITY_REPLY);
550 req.version = cpu_to_le32(ENIC_MBOX_CAP_VERSION_1);
551
552 err = enic_mbox_send_msg(enic, ENIC_MBOX_VF_CAPABILITY_REQUEST,
553 ENIC_MBOX_DST_PF, &req, sizeof(req));
554 if (err) {
555 WRITE_ONCE(enic->mbox_expected_reply, 0);
556 return err;
557 }
558
559 err = enic_mbox_wait_reply(enic, 3000);
560 WRITE_ONCE(enic->mbox_expected_reply, 0);
561 if (err) {
562 netdev_warn(enic->netdev,
563 "MBOX: no capability reply from PF\n");
564 return err;
565 }
566
567 if (enic->pf_cap_version < ENIC_MBOX_CAP_VERSION_1) {
568 netdev_warn(enic->netdev,
569 "MBOX: PF rejected capability request or reported unsupported version %u\n",
570 enic->pf_cap_version);
571 return -EOPNOTSUPP;
572 }
573
574 return 0;
575 }
576
enic_mbox_vf_register(struct enic * enic)577 int enic_mbox_vf_register(struct enic *enic)
578 {
579 int err;
580
581 enic->vf_registered = false;
582 reinit_completion(&enic->mbox_comp);
583 WRITE_ONCE(enic->mbox_expected_reply, ENIC_MBOX_VF_REGISTER_REPLY);
584
585 err = enic_mbox_send_msg(enic, ENIC_MBOX_VF_REGISTER_REQUEST,
586 ENIC_MBOX_DST_PF, NULL, 0);
587 if (err) {
588 WRITE_ONCE(enic->mbox_expected_reply, 0);
589 return err;
590 }
591
592 err = enic_mbox_wait_reply(enic, 3000);
593 WRITE_ONCE(enic->mbox_expected_reply, 0);
594 if (err) {
595 netdev_warn(enic->netdev,
596 "MBOX: VF registration with PF timed out\n");
597 return err;
598 }
599
600 if (!enic->vf_registered)
601 return -ENODEV;
602
603 return 0;
604 }
605
enic_mbox_vf_unregister(struct enic * enic)606 int enic_mbox_vf_unregister(struct enic *enic)
607 {
608 int err;
609
610 if (!enic->vf_registered)
611 return 0;
612
613 reinit_completion(&enic->mbox_comp);
614 WRITE_ONCE(enic->mbox_expected_reply, ENIC_MBOX_VF_UNREGISTER_REPLY);
615
616 err = enic_mbox_send_msg(enic, ENIC_MBOX_VF_UNREGISTER_REQUEST,
617 ENIC_MBOX_DST_PF, NULL, 0);
618 if (err) {
619 WRITE_ONCE(enic->mbox_expected_reply, 0);
620 return err;
621 }
622
623 err = enic_mbox_wait_reply(enic, 3000);
624 WRITE_ONCE(enic->mbox_expected_reply, 0);
625 if (err)
626 return err;
627 if (enic->vf_registered)
628 return -EACCES;
629 return 0;
630 }
631
enic_mbox_init(struct enic * enic)632 void enic_mbox_init(struct enic *enic)
633 {
634 /* mbox_lock and mbox_comp must be initialized exactly once per
635 * device lifetime; the PF sriov_configure path can re-enter this
636 * on each enable cycle where these primitives are already set up.
637 */
638 if (!enic->mbox_initialized) {
639 mutex_init(&enic->mbox_lock);
640 init_completion(&enic->mbox_comp);
641 enic->mbox_initialized = true;
642 } else {
643 reinit_completion(&enic->mbox_comp);
644 }
645 enic->mbox_msg_num = 0;
646 enic->admin_rq_handler = enic_mbox_recv_handler;
647 }
648