xref: /linux/drivers/net/ethernet/cisco/enic/enic_admin.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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/interrupt.h>
8 
9 #include "vnic_dev.h"
10 #include "vnic_wq.h"
11 #include "vnic_rq.h"
12 #include "vnic_cq.h"
13 #include "vnic_intr.h"
14 #include "vnic_resource.h"
15 #include "vnic_devcmd.h"
16 #include "enic.h"
17 #include "enic_admin.h"
18 #include "cq_desc.h"
19 #include "cq_enet_desc.h"
20 #include "wq_enet_desc.h"
21 #include "rq_enet_desc.h"
22 #include "enic_mbox.h"
23 
24 /* Retry interval for a failed admin RQ refill.  Short so the control
25  * channel recovers quickly once memory is available again.
26  */
27 #define ENIC_ADMIN_RQ_REFILL_RETRY_MS	100
28 
29 /* Clean up any admin WQ buffers still held by hardware at close time.
30  * Normally buffers are freed inline after send completion, but a timed-out
31  * send intentionally leaves the buffer live until the queue is stopped.
32  */
enic_admin_wq_buf_clean(struct vnic_wq * wq,struct vnic_wq_buf * buf)33 static void enic_admin_wq_buf_clean(struct vnic_wq *wq,
34 				    struct vnic_wq_buf *buf)
35 {
36 	struct enic *enic = vnic_dev_priv(wq->vdev);
37 
38 	if (buf->os_buf) {
39 		dma_unmap_single(&enic->pdev->dev, buf->dma_addr,
40 				 buf->len, DMA_TO_DEVICE);
41 		kfree(buf->os_buf);
42 		buf->os_buf = NULL;
43 	}
44 }
45 
enic_admin_rq_buf_clean(struct vnic_rq * rq,struct vnic_rq_buf * buf)46 static void enic_admin_rq_buf_clean(struct vnic_rq *rq,
47 				    struct vnic_rq_buf *buf)
48 {
49 	struct enic *enic = vnic_dev_priv(rq->vdev);
50 
51 	if (!buf->os_buf)
52 		return;
53 
54 	dma_unmap_single(&enic->pdev->dev, buf->dma_addr, buf->len,
55 			 DMA_FROM_DEVICE);
56 	kfree(buf->os_buf);
57 	buf->os_buf = NULL;
58 }
59 
enic_admin_rq_post_one(struct enic * enic,gfp_t gfp)60 static int enic_admin_rq_post_one(struct enic *enic, gfp_t gfp)
61 {
62 	struct vnic_rq *rq = &enic->admin_rq;
63 	struct rq_enet_desc *desc;
64 	dma_addr_t dma_addr;
65 	void *buf;
66 
67 	buf = kzalloc(ENIC_ADMIN_BUF_SIZE, gfp);
68 	if (!buf)
69 		return -ENOMEM;
70 
71 	dma_addr = dma_map_single(&enic->pdev->dev, buf, ENIC_ADMIN_BUF_SIZE,
72 				  DMA_FROM_DEVICE);
73 	if (dma_mapping_error(&enic->pdev->dev, dma_addr)) {
74 		kfree(buf);
75 		return -ENOMEM;
76 	}
77 
78 	desc = vnic_rq_next_desc(rq);
79 	rq_enet_desc_enc(desc, (u64)dma_addr | VNIC_PADDR_TARGET,
80 			 RQ_ENET_TYPE_ONLY_SOP, ENIC_ADMIN_BUF_SIZE);
81 	vnic_rq_post(rq, buf, 0, dma_addr, ENIC_ADMIN_BUF_SIZE, 0);
82 
83 	return 0;
84 }
85 
enic_admin_rq_fill(struct enic * enic,gfp_t gfp)86 static int enic_admin_rq_fill(struct enic *enic, gfp_t gfp)
87 {
88 	struct vnic_rq *rq = &enic->admin_rq;
89 	int err;
90 
91 	while (vnic_rq_desc_avail(rq) > 0) {
92 		err = enic_admin_rq_post_one(enic, gfp);
93 		if (err)
94 			return err;
95 	}
96 
97 	return 0;
98 }
99 
enic_admin_rq_drain(struct enic * enic)100 static void enic_admin_rq_drain(struct enic *enic)
101 {
102 	vnic_rq_clean(&enic->admin_rq, enic_admin_rq_buf_clean);
103 }
104 
enic_admin_cq_color(void * cq_desc,unsigned int desc_size)105 static unsigned int enic_admin_cq_color(void *cq_desc, unsigned int desc_size)
106 {
107 	u8 type_color = *((u8 *)cq_desc + desc_size - 1);
108 
109 	return (type_color >> CQ_DESC_COLOR_SHIFT) & CQ_DESC_COLOR_MASK;
110 }
111 
enic_admin_wq_cq_service(struct enic * enic)112 unsigned int enic_admin_wq_cq_service(struct enic *enic)
113 {
114 	struct vnic_cq *cq = &enic->admin_cq[0];
115 	unsigned int work = 0;
116 	void *desc;
117 
118 	desc = vnic_cq_to_clean(cq);
119 	while (enic_admin_cq_color(desc, cq->ring.desc_size) !=
120 	       cq->last_color) {
121 		vnic_cq_inc_to_clean(cq);
122 		work++;
123 		desc = vnic_cq_to_clean(cq);
124 	}
125 
126 	return work;
127 }
128 
129 /* Upper bound on pending admin messages.  A buggy or hostile VF could flood
130  * the PF admin channel faster than admin_msg_work drains it; cap the backlog
131  * so a guest cannot drive the host out of memory.
132  */
133 #define ENIC_ADMIN_MSG_MAX	256
134 
enic_admin_msg_enqueue(struct enic * enic,void * buf,unsigned int len)135 static void enic_admin_msg_enqueue(struct enic *enic, void *buf,
136 				   unsigned int len)
137 {
138 	struct enic_admin_msg *msg;
139 
140 	msg = kmalloc(struct_size(msg, data, len), GFP_KERNEL);
141 	if (!msg)
142 		return;
143 
144 	msg->len = len;
145 	memcpy(msg->data, buf, len);
146 
147 	spin_lock(&enic->admin_msg_lock);
148 	if (enic->admin_msg_count >= ENIC_ADMIN_MSG_MAX) {
149 		spin_unlock(&enic->admin_msg_lock);
150 		kfree(msg);
151 		if (net_ratelimit())
152 			netdev_warn(enic->netdev,
153 				    "admin msg backlog full (%u); dropping\n",
154 				    ENIC_ADMIN_MSG_MAX);
155 		return;
156 	}
157 	list_add_tail(&msg->list, &enic->admin_msg_list);
158 	enic->admin_msg_count++;
159 	spin_unlock(&enic->admin_msg_lock);
160 }
161 
enic_admin_rq_cq_service(struct enic * enic)162 unsigned int enic_admin_rq_cq_service(struct enic *enic)
163 {
164 	struct vnic_cq *cq = &enic->admin_cq[1];
165 	struct vnic_rq *rq = &enic->admin_rq;
166 	struct cq_enet_rq_desc *rq_desc;
167 	struct vnic_rq_buf *buf;
168 	u16 bwf, bytes_written;
169 	unsigned int work = 0;
170 	void *desc;
171 
172 	/* The admin RQ and its CQ form a single in-order channel: firmware
173 	 * posts exactly one CQE per consumed RQ descriptor, in submission
174 	 * order.  Each CQE therefore pairs with rq->to_clean below without a
175 	 * completed_index cross-check, mirroring the in-order assumption of
176 	 * the main enic RX path.
177 	 */
178 	desc = vnic_cq_to_clean(cq);
179 	while (enic_admin_cq_color(desc, cq->ring.desc_size) !=
180 	       cq->last_color) {
181 		/* Ensure DMA descriptor fields are read after
182 		 * the color/valid check.  dma_rmb() is the
183 		 * correct barrier for DMA-written descriptors.
184 		 */
185 		dma_rmb();
186 		buf = rq->to_clean;
187 
188 		/* Decode the actual number of bytes hardware wrote into
189 		 * the RX buffer.  buf->len is the static allocation size
190 		 * (ENIC_ADMIN_BUF_SIZE); copying that many bytes would read
191 		 * beyond the actual DMA payload.  bytes_written_flags is at
192 		 * the same offset in every cq_enet_rq_desc[_32|_64] variant.
193 		 */
194 		rq_desc = desc;
195 		bwf = le16_to_cpu(rq_desc->bytes_written_flags);
196 		bytes_written = bwf & CQ_ENET_RQ_DESC_BYTES_WRITTEN_MASK;
197 		if (bytes_written > buf->len)
198 			goto next_desc;
199 
200 		dma_sync_single_for_cpu(&enic->pdev->dev,
201 					buf->dma_addr, buf->len,
202 					DMA_FROM_DEVICE);
203 
204 		/* Drop on hardware error indications.  Admin messages
205 		 * are internal to the VIC, not received over the wire.
206 		 * Firmware sets TRUNCATED when the message does not fit
207 		 * in the posted buffer, and FCS_OK is always set on
208 		 * healthy admin completions.
209 		 */
210 		if (bwf & CQ_ENET_RQ_DESC_FLAGS_TRUNCATED) {
211 			netdev_warn_once(enic->netdev,
212 					 "admin RQ: truncated message dropped\n");
213 			goto next_desc;
214 		}
215 		if (!(rq_desc->flags & CQ_ENET_RQ_DESC_FLAGS_FCS_OK)) {
216 			netdev_warn_once(enic->netdev,
217 					 "admin RQ: bad FCS, dropping message\n");
218 			goto next_desc;
219 		}
220 
221 		if (enic->admin_rq_handler) {
222 			u16 sender_vlan;
223 
224 			/* Firmware sets the CQ VLAN field to identify the
225 			 * sender: 0 = PF, 1-based = VF index.  Overwrite
226 			 * the untrusted src_vnic_id in the MBOX header with
227 			 * the hardware-verified value.
228 			 */
229 			sender_vlan = le16_to_cpu(rq_desc->vlan);
230 			if (bytes_written >= sizeof(struct enic_mbox_hdr)) {
231 				struct enic_mbox_hdr *hdr = buf->os_buf;
232 
233 				hdr->src_vnic_id = (sender_vlan == 0) ?
234 					cpu_to_le16(ENIC_MBOX_DST_PF) :
235 					cpu_to_le16(sender_vlan - 1);
236 			}
237 
238 			enic_admin_msg_enqueue(enic, buf->os_buf,
239 					       bytes_written);
240 		}
241 
242 next_desc:
243 		enic_admin_rq_buf_clean(rq, rq->to_clean);
244 		rq->to_clean = rq->to_clean->next;
245 		rq->ring.desc_avail++;
246 
247 		vnic_cq_inc_to_clean(cq);
248 		work++;
249 		desc = vnic_cq_to_clean(cq);
250 	}
251 
252 	if (enic_admin_rq_fill(enic, GFP_KERNEL)) {
253 		/* Some RX buffers could not be reposted (transient memory
254 		 * pressure).  If the ring is left empty the channel would stall
255 		 * with no completion to drive the next refill, so arm a delayed
256 		 * re-run of this poll work (the sole owner of the admin RQ ring)
257 		 * to repost buffers and re-arm the RQ from a single context.
258 		 */
259 		if (net_ratelimit())
260 			netdev_warn(enic->netdev,
261 				    "admin RQ refill failed; scheduling retry\n");
262 		schedule_delayed_work(&enic->admin_poll_work,
263 				      msecs_to_jiffies(ENIC_ADMIN_RQ_REFILL_RETRY_MS));
264 	}
265 
266 	return work;
267 }
268 
enic_admin_isr_msix(int irq,void * data)269 static irqreturn_t enic_admin_isr_msix(int irq, void *data)
270 {
271 	struct enic *enic = data;
272 
273 	schedule_delayed_work(&enic->admin_poll_work, 0);
274 
275 	return IRQ_HANDLED;
276 }
277 
enic_admin_msg_work_handler(struct work_struct * work)278 static void enic_admin_msg_work_handler(struct work_struct *work)
279 {
280 	struct enic *enic = container_of(work, struct enic, admin_msg_work);
281 	struct enic_admin_msg *msg, *tmp;
282 	LIST_HEAD(local_list);
283 
284 	spin_lock_bh(&enic->admin_msg_lock);
285 	list_splice_init(&enic->admin_msg_list, &local_list);
286 	enic->admin_msg_count = 0;
287 	spin_unlock_bh(&enic->admin_msg_lock);
288 
289 	list_for_each_entry_safe(msg, tmp, &local_list, list) {
290 		if (enic->admin_rq_handler)
291 			enic->admin_rq_handler(enic, msg->data, msg->len);
292 		list_del(&msg->list);
293 		kfree(msg);
294 	}
295 }
296 
enic_admin_poll_work_handler(struct work_struct * work)297 static void enic_admin_poll_work_handler(struct work_struct *work)
298 {
299 	struct enic *enic = container_of(to_delayed_work(work), struct enic,
300 					 admin_poll_work);
301 	unsigned int credits;
302 	unsigned int rq_work;
303 
304 	/* Snapshot the pending credit count before draining so we acknowledge
305 	 * exactly what the hardware reported for this interrupt.  Credits that
306 	 * accrue while enic_admin_rq_cq_service() runs are left for the next
307 	 * interrupt, which is harmless on this low-rate control path.
308 	 */
309 	credits = vnic_intr_credits(&enic->admin_intr);
310 
311 	rq_work = enic_admin_rq_cq_service(enic);
312 
313 	if (rq_work > 0)
314 		schedule_work(&enic->admin_msg_work);
315 
316 	/* Acknowledge the snapshotted credits and unmask the vector.  Unlike
317 	 * the NAPI data path, the admin channel is not re-polled, so the vector
318 	 * must be re-armed here to receive the next completion.  The unmask is
319 	 * applied through the interrupt mask register independently of the
320 	 * credit count, so returning zero credits on a spurious wakeup still
321 	 * re-arms the vector.
322 	 */
323 	vnic_intr_return_credits(&enic->admin_intr,
324 				 credits,
325 				 1 /* unmask */, 0);
326 }
327 
enic_admin_setup_intr(struct enic * enic)328 static int enic_admin_setup_intr(struct enic *enic)
329 {
330 	unsigned int intr_index = enic->intr_count;
331 	int err;
332 
333 	if (vnic_dev_get_intr_mode(enic->vdev) != VNIC_DEV_INTR_MODE_MSIX ||
334 	    intr_index >= enic->intr_avail)
335 		return -ENODEV;
336 
337 	/* The admin INTR uses a slot in the same RES_TYPE_INTR_CTRL
338 	 * strided array of per-vector control blocks (mask, coalescing
339 	 * timer, credit return) that the data-path IRQs occupy in BAR0.
340 	 * vnic_intr_alloc() defaults to RES_TYPE_INTR_CTRL, which is what
341 	 * we want here.
342 	 */
343 	err = vnic_intr_alloc(enic->vdev, &enic->admin_intr, intr_index);
344 	if (err) {
345 		netdev_warn(enic->netdev,
346 			    "Failed to alloc admin intr at index %u: %d\n",
347 			    intr_index, err);
348 		return err;
349 	}
350 
351 	enic->admin_intr_index = intr_index;
352 
353 	/* Mask the admin vector before requesting the IRQ so an early or
354 	 * spurious completion cannot run the poll handler against the
355 	 * not-yet-initialised admin rings.  enic_admin_channel_open() unmasks
356 	 * it only after the rings are initialised and filled.
357 	 */
358 	vnic_intr_mask(&enic->admin_intr);
359 
360 	/* A V2 VF opens the admin channel during probe, before
361 	 * register_netdev() resolves the "eth%d" name template, so using
362 	 * netdev->name here would register the literal "eth%d-admin" in
363 	 * /proc/interrupts.  Use the already-stable PCI device name instead.
364 	 */
365 	snprintf(enic->msix[intr_index].devname,
366 		 sizeof(enic->msix[intr_index].devname),
367 		 "%s-admin", pci_name(enic->pdev));
368 	enic->msix[intr_index].isr = enic_admin_isr_msix;
369 	enic->msix[intr_index].devid = enic;
370 
371 	err = request_irq(enic->msix_entry[intr_index].vector,
372 			  enic->msix[intr_index].isr, 0,
373 			  enic->msix[intr_index].devname,
374 			  enic->msix[intr_index].devid);
375 	if (err) {
376 		netdev_warn(enic->netdev,
377 			    "Failed to request admin MSI-X irq: %d\n", err);
378 		vnic_intr_free(&enic->admin_intr);
379 		return err;
380 	}
381 
382 	enic->msix[intr_index].requested = 1;
383 
384 	netdev_dbg(enic->netdev,
385 		   "admin channel using MSI-X interrupt (index %u)\n",
386 		   intr_index);
387 
388 	return 0;
389 }
390 
enic_admin_teardown_intr(struct enic * enic)391 static void enic_admin_teardown_intr(struct enic *enic)
392 {
393 	unsigned int intr_index = enic->admin_intr_index;
394 
395 	free_irq(enic->msix_entry[intr_index].vector,
396 		 enic->msix[intr_index].devid);
397 	cancel_delayed_work_sync(&enic->admin_poll_work);
398 	enic->msix[intr_index].requested = 0;
399 }
400 
enic_admin_qp_type_set(struct enic * enic,u32 enable)401 static int enic_admin_qp_type_set(struct enic *enic, u32 enable)
402 {
403 	u64 a0 = QP_TYPE_ADMIN, a1 = enable;
404 	int wait = 1000;
405 	int err;
406 
407 	spin_lock_bh(&enic->devcmd_lock);
408 	err = vnic_dev_cmd(enic->vdev, CMD_QP_TYPE_SET, &a0, &a1, wait);
409 	spin_unlock_bh(&enic->devcmd_lock);
410 
411 	return err;
412 }
413 
enic_admin_alloc_resources(struct enic * enic)414 static int enic_admin_alloc_resources(struct enic *enic)
415 {
416 	int err;
417 
418 	err = vnic_wq_alloc_with_type(enic->vdev, &enic->admin_wq, 0,
419 				      ENIC_ADMIN_DESC_COUNT,
420 				      sizeof(struct wq_enet_desc),
421 				      RES_TYPE_ADMIN_WQ);
422 	if (err)
423 		return err;
424 
425 	err = vnic_rq_alloc_with_type(enic->vdev, &enic->admin_rq, 0,
426 				      ENIC_ADMIN_DESC_COUNT,
427 				      sizeof(struct rq_enet_desc),
428 				      RES_TYPE_ADMIN_RQ);
429 	if (err)
430 		goto free_wq;
431 
432 	/* admin_cq[0] is the WQ completion queue.  WQ CQEs are always
433 	 * 16 bytes wide; firmware always writes 16-byte CQEs for WQ
434 	 * completions on every WQ, including the admin channel WQ.
435 	 * Use sizeof(struct cq_desc) accordingly.
436 	 */
437 	err = vnic_cq_alloc_with_type(enic->vdev, &enic->admin_cq[0], 0,
438 				      ENIC_ADMIN_DESC_COUNT,
439 				      sizeof(struct cq_desc),
440 				      RES_TYPE_ADMIN_CQ);
441 	if (err)
442 		goto free_rq;
443 
444 	/* admin_cq[1] is the RQ completion queue.  Its descriptor size
445 	 * must match what firmware writes.  enic_ext_cq() called earlier
446 	 * in probe issues CMD_CQ_ENTRY_SIZE_SET for VNIC_RQ_ALL,
447 	 * programming firmware to write CQ entries of (16 << enic->ext_cq)
448 	 * bytes for every RQ CQ on the vNIC, including the admin RQ CQ.
449 	 * Allocating with the same size keeps the host poller and
450 	 * firmware in lockstep:
451 	 *
452 	 *   - The color/valid bit lives at byte (desc_size - 1) of every
453 	 *     cq_enet_rq_desc[_32|_64] variant, so enic_admin_cq_color()
454 	 *     reads it from the correct offset.
455 	 *   - Only the first 15 bytes of the descriptor (vlan,
456 	 *     bytes_written_flags, ...) are accessed by the admin path;
457 	 *     these fields are identical across all three variants (see
458 	 *     comment in enic_rq.c above cq_enet_rq_desc_dec()).
459 	 */
460 	err = vnic_cq_alloc_with_type(enic->vdev, &enic->admin_cq[1], 1,
461 				      ENIC_ADMIN_DESC_COUNT,
462 				      16 << enic->ext_cq,
463 				      RES_TYPE_ADMIN_CQ);
464 	if (err)
465 		goto free_cq0;
466 
467 	return 0;
468 
469 free_cq0:
470 	vnic_cq_free(&enic->admin_cq[0]);
471 free_rq:
472 	vnic_rq_free(&enic->admin_rq);
473 free_wq:
474 	vnic_wq_free(&enic->admin_wq);
475 	return err;
476 }
477 
enic_admin_free_resources(struct enic * enic)478 static void enic_admin_free_resources(struct enic *enic)
479 {
480 	vnic_intr_free(&enic->admin_intr);
481 	vnic_cq_free(&enic->admin_cq[1]);
482 	vnic_cq_free(&enic->admin_cq[0]);
483 	vnic_rq_free(&enic->admin_rq);
484 	vnic_wq_free(&enic->admin_wq);
485 }
486 
enic_admin_init_resources(struct enic * enic)487 static void enic_admin_init_resources(struct enic *enic)
488 {
489 	unsigned int intr_offset = enic->admin_intr_index;
490 
491 	vnic_wq_init(&enic->admin_wq,
492 		     0, 0, 0); /* cq_index, err_intr_enable, err_intr_offset */
493 	vnic_rq_init(&enic->admin_rq,
494 		     1, 0, 0); /* cq_index, err_intr_enable, err_intr_offset */
495 	vnic_cq_init(&enic->admin_cq[0],
496 		     VNIC_CQ_FC_DISABLE,
497 		     VNIC_CQ_COLOR_ENABLE,
498 		     0, 0, 1, /* cq_head, cq_tail, cq_tail_color */
499 		     VNIC_CQ_INTR_DISABLE, /* polled synchronously by mbox send */
500 		     VNIC_CQ_ENTRY_ENABLE,
501 		     VNIC_CQ_MSG_DISABLE,
502 		     intr_offset,
503 		     0 /* cq_message_addr */);
504 	vnic_cq_init(&enic->admin_cq[1],
505 		     VNIC_CQ_FC_DISABLE,
506 		     VNIC_CQ_COLOR_ENABLE,
507 		     0, 0, 1, /* cq_head, cq_tail, cq_tail_color */
508 		     VNIC_CQ_INTR_ENABLE,
509 		     VNIC_CQ_ENTRY_ENABLE,
510 		     VNIC_CQ_MSG_DISABLE,
511 		     intr_offset,
512 		     0 /* cq_message_addr */);
513 	/* coalescing_timer, coalescing_type, mask_on_assertion */
514 	vnic_intr_init(&enic->admin_intr,
515 		       0, 0, 1);
516 }
517 
enic_admin_msg_drain(struct enic * enic)518 static void enic_admin_msg_drain(struct enic *enic)
519 {
520 	struct enic_admin_msg *msg, *tmp;
521 
522 	spin_lock_bh(&enic->admin_msg_lock);
523 	list_for_each_entry_safe(msg, tmp, &enic->admin_msg_list, list) {
524 		list_del(&msg->list);
525 		kfree(msg);
526 	}
527 	enic->admin_msg_count = 0;
528 	spin_unlock_bh(&enic->admin_msg_lock);
529 }
530 
enic_admin_channel_open(struct enic * enic)531 int enic_admin_channel_open(struct enic *enic)
532 {
533 	int err;
534 
535 	if (!enic->has_admin_channel)
536 		return -ENODEV;
537 
538 	/* Keep MBOX sends disabled for the entire open sequence.  It is
539 	 * cleared only after every resource is allocated and enabled below,
540 	 * so any early error return here leaves sends disabled and a
541 	 * concurrent sender cannot touch a half-open or freed admin_wq.
542 	 */
543 	WRITE_ONCE(enic->mbox_send_disabled, true);
544 
545 	err = enic_admin_alloc_resources(enic);
546 	if (err) {
547 		netdev_err(enic->netdev,
548 			   "Failed to alloc admin channel resources: %d\n",
549 			   err);
550 		return err;
551 	}
552 
553 	spin_lock_init(&enic->admin_msg_lock);
554 	INIT_LIST_HEAD(&enic->admin_msg_list);
555 	INIT_WORK(&enic->admin_msg_work, enic_admin_msg_work_handler);
556 	INIT_DELAYED_WORK(&enic->admin_poll_work, enic_admin_poll_work_handler);
557 
558 	err = enic_admin_setup_intr(enic);
559 	if (err) {
560 		netdev_err(enic->netdev,
561 			   "Admin channel requires MSI-X, SR-IOV unavailable: %d\n",
562 			   err);
563 		goto free_resources;
564 	}
565 
566 	enic_admin_init_resources(enic);
567 
568 	vnic_wq_enable(&enic->admin_wq);
569 	vnic_rq_enable(&enic->admin_rq);
570 
571 	err = enic_admin_rq_fill(enic, GFP_KERNEL);
572 	if (err) {
573 		netdev_err(enic->netdev,
574 			   "Failed to fill admin RQ buffers: %d\n", err);
575 		goto disable_queues;
576 	}
577 
578 	err = enic_admin_qp_type_set(enic, QP_ENABLE);
579 	if (err) {
580 		netdev_err(enic->netdev,
581 			   "Failed to set admin QP type: %d\n", err);
582 		goto disable_queues;
583 	}
584 
585 	vnic_intr_unmask(&enic->admin_intr);
586 
587 	/* Only now that the admin WQ/RQ/CQ and interrupt are fully allocated,
588 	 * programmed and enabled is it safe to allow MBOX sends.  Clearing this
589 	 * earlier opened a window where a concurrent sender (e.g. link-notify
590 	 * work scheduled by a post-reset link-up) could call enic_mbox_send_msg()
591 	 * against a not-yet-allocated admin_wq and crash.
592 	 */
593 	WRITE_ONCE(enic->mbox_send_disabled, false);
594 
595 	netdev_dbg(enic->netdev,
596 		   "admin channel open: intr=%u wq_avail=%u rq_avail=%u cq0_color=%u cq1_color=%u\n",
597 		   enic->admin_intr_index,
598 		   vnic_wq_desc_avail(&enic->admin_wq),
599 		   vnic_rq_desc_avail(&enic->admin_rq),
600 		   enic->admin_cq[0].last_color,
601 		   enic->admin_cq[1].last_color);
602 
603 	enic->admin_chan_up = true;
604 
605 	return 0;
606 
607 disable_queues:
608 	enic_admin_teardown_intr(enic);
609 	enic_admin_qp_type_set(enic, QP_DISABLE);
610 	if (vnic_wq_disable(&enic->admin_wq))
611 		netdev_warn(enic->netdev, "Failed to disable admin WQ\n");
612 	if (vnic_rq_disable(&enic->admin_rq))
613 		netdev_warn(enic->netdev, "Failed to disable admin RQ\n");
614 	cancel_work_sync(&enic->admin_msg_work);
615 	enic_admin_msg_drain(enic);
616 	enic_admin_rq_drain(enic);
617 free_resources:
618 	enic_admin_free_resources(enic);
619 	return err;
620 }
621 
enic_admin_channel_close(struct enic * enic)622 void enic_admin_channel_close(struct enic *enic)
623 {
624 	int err;
625 
626 	if (!enic->has_admin_channel)
627 		return;
628 
629 	/* Nothing to tear down if the channel was never (re)opened, e.g. a
630 	 * failed enic_admin_channel_open() in probe or in the reset path;
631 	 * otherwise the disable/clean calls below dereference freed resources.
632 	 */
633 	if (!enic->admin_chan_up)
634 		return;
635 
636 	WRITE_ONCE(enic->mbox_send_disabled, true);
637 
638 	netdev_dbg(enic->netdev, "admin channel close\n");
639 
640 	vnic_intr_mask(&enic->admin_intr);
641 	enic_admin_teardown_intr(enic);
642 	cancel_work_sync(&enic->link_notify_work);
643 	cancel_work_sync(&enic->admin_msg_work);
644 	enic_admin_msg_drain(enic);
645 
646 	enic_admin_qp_type_set(enic, QP_DISABLE);
647 
648 	err = vnic_wq_disable(&enic->admin_wq);
649 	if (err)
650 		netdev_warn(enic->netdev,
651 			    "Failed to disable admin WQ: %d\n", err);
652 	err = vnic_rq_disable(&enic->admin_rq);
653 	if (err)
654 		netdev_warn(enic->netdev,
655 			    "Failed to disable admin RQ: %d\n", err);
656 
657 	vnic_wq_clean(&enic->admin_wq, enic_admin_wq_buf_clean);
658 	enic_admin_rq_drain(enic);
659 	vnic_cq_clean(&enic->admin_cq[0]);
660 	vnic_cq_clean(&enic->admin_cq[1]);
661 	vnic_intr_clean(&enic->admin_intr);
662 
663 	enic->admin_rq_handler = NULL;
664 	enic_admin_free_resources(enic);
665 
666 	enic->admin_chan_up = false;
667 }
668