xref: /linux/drivers/net/ethernet/google/gve/gve_main.c (revision 1a20a9a0ddef17c0bd67eece34a7439b02a7b0ba)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
3  *
4  * Copyright (C) 2015-2021 Google, Inc.
5  */
6 
7 #include <linux/bpf.h>
8 #include <linux/cpumask.h>
9 #include <linux/etherdevice.h>
10 #include <linux/filter.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/workqueue.h>
17 #include <linux/utsname.h>
18 #include <linux/version.h>
19 #include <net/sch_generic.h>
20 #include <net/xdp_sock_drv.h>
21 #include "gve.h"
22 #include "gve_dqo.h"
23 #include "gve_adminq.h"
24 #include "gve_register.h"
25 #include "gve_utils.h"
26 
27 #define GVE_DEFAULT_RX_COPYBREAK	(256)
28 
29 #define DEFAULT_MSG_LEVEL	(NETIF_MSG_DRV | NETIF_MSG_LINK)
30 #define GVE_VERSION		"1.0.0"
31 #define GVE_VERSION_PREFIX	"GVE-"
32 
33 // Minimum amount of time between queue kicks in msec (10 seconds)
34 #define MIN_TX_TIMEOUT_GAP (1000 * 10)
35 
36 char gve_driver_name[] = "gve";
37 const char gve_version_str[] = GVE_VERSION;
38 static const char gve_version_prefix[] = GVE_VERSION_PREFIX;
39 
40 static int gve_verify_driver_compatibility(struct gve_priv *priv)
41 {
42 	int err;
43 	struct gve_driver_info *driver_info;
44 	dma_addr_t driver_info_bus;
45 
46 	driver_info = dma_alloc_coherent(&priv->pdev->dev,
47 					 sizeof(struct gve_driver_info),
48 					 &driver_info_bus, GFP_KERNEL);
49 	if (!driver_info)
50 		return -ENOMEM;
51 
52 	*driver_info = (struct gve_driver_info) {
53 		.os_type = 1, /* Linux */
54 		.os_version_major = cpu_to_be32(LINUX_VERSION_MAJOR),
55 		.os_version_minor = cpu_to_be32(LINUX_VERSION_SUBLEVEL),
56 		.os_version_sub = cpu_to_be32(LINUX_VERSION_PATCHLEVEL),
57 		.driver_capability_flags = {
58 			cpu_to_be64(GVE_DRIVER_CAPABILITY_FLAGS1),
59 			cpu_to_be64(GVE_DRIVER_CAPABILITY_FLAGS2),
60 			cpu_to_be64(GVE_DRIVER_CAPABILITY_FLAGS3),
61 			cpu_to_be64(GVE_DRIVER_CAPABILITY_FLAGS4),
62 		},
63 	};
64 	strscpy(driver_info->os_version_str1, utsname()->release,
65 		sizeof(driver_info->os_version_str1));
66 	strscpy(driver_info->os_version_str2, utsname()->version,
67 		sizeof(driver_info->os_version_str2));
68 
69 	err = gve_adminq_verify_driver_compatibility(priv,
70 						     sizeof(struct gve_driver_info),
71 						     driver_info_bus);
72 
73 	/* It's ok if the device doesn't support this */
74 	if (err == -EOPNOTSUPP)
75 		err = 0;
76 
77 	dma_free_coherent(&priv->pdev->dev,
78 			  sizeof(struct gve_driver_info),
79 			  driver_info, driver_info_bus);
80 	return err;
81 }
82 
83 static netdev_features_t gve_features_check(struct sk_buff *skb,
84 					    struct net_device *dev,
85 					    netdev_features_t features)
86 {
87 	struct gve_priv *priv = netdev_priv(dev);
88 
89 	if (!gve_is_gqi(priv))
90 		return gve_features_check_dqo(skb, dev, features);
91 
92 	return features;
93 }
94 
95 static netdev_tx_t gve_start_xmit(struct sk_buff *skb, struct net_device *dev)
96 {
97 	struct gve_priv *priv = netdev_priv(dev);
98 
99 	if (gve_is_gqi(priv))
100 		return gve_tx(skb, dev);
101 	else
102 		return gve_tx_dqo(skb, dev);
103 }
104 
105 static void gve_get_stats(struct net_device *dev, struct rtnl_link_stats64 *s)
106 {
107 	struct gve_priv *priv = netdev_priv(dev);
108 	unsigned int start;
109 	u64 packets, bytes;
110 	int num_tx_queues;
111 	int ring;
112 
113 	num_tx_queues = gve_num_tx_queues(priv);
114 	if (priv->rx) {
115 		for (ring = 0; ring < priv->rx_cfg.num_queues; ring++) {
116 			do {
117 				start =
118 				  u64_stats_fetch_begin(&priv->rx[ring].statss);
119 				packets = priv->rx[ring].rpackets;
120 				bytes = priv->rx[ring].rbytes;
121 			} while (u64_stats_fetch_retry(&priv->rx[ring].statss,
122 						       start));
123 			s->rx_packets += packets;
124 			s->rx_bytes += bytes;
125 		}
126 	}
127 	if (priv->tx) {
128 		for (ring = 0; ring < num_tx_queues; ring++) {
129 			do {
130 				start =
131 				  u64_stats_fetch_begin(&priv->tx[ring].statss);
132 				packets = priv->tx[ring].pkt_done;
133 				bytes = priv->tx[ring].bytes_done;
134 			} while (u64_stats_fetch_retry(&priv->tx[ring].statss,
135 						       start));
136 			s->tx_packets += packets;
137 			s->tx_bytes += bytes;
138 		}
139 	}
140 }
141 
142 static int gve_alloc_counter_array(struct gve_priv *priv)
143 {
144 	priv->counter_array =
145 		dma_alloc_coherent(&priv->pdev->dev,
146 				   priv->num_event_counters *
147 				   sizeof(*priv->counter_array),
148 				   &priv->counter_array_bus, GFP_KERNEL);
149 	if (!priv->counter_array)
150 		return -ENOMEM;
151 
152 	return 0;
153 }
154 
155 static void gve_free_counter_array(struct gve_priv *priv)
156 {
157 	if (!priv->counter_array)
158 		return;
159 
160 	dma_free_coherent(&priv->pdev->dev,
161 			  priv->num_event_counters *
162 			  sizeof(*priv->counter_array),
163 			  priv->counter_array, priv->counter_array_bus);
164 	priv->counter_array = NULL;
165 }
166 
167 /* NIC requests to report stats */
168 static void gve_stats_report_task(struct work_struct *work)
169 {
170 	struct gve_priv *priv = container_of(work, struct gve_priv,
171 					     stats_report_task);
172 	if (gve_get_do_report_stats(priv)) {
173 		gve_handle_report_stats(priv);
174 		gve_clear_do_report_stats(priv);
175 	}
176 }
177 
178 static void gve_stats_report_schedule(struct gve_priv *priv)
179 {
180 	if (!gve_get_probe_in_progress(priv) &&
181 	    !gve_get_reset_in_progress(priv)) {
182 		gve_set_do_report_stats(priv);
183 		queue_work(priv->gve_wq, &priv->stats_report_task);
184 	}
185 }
186 
187 static void gve_stats_report_timer(struct timer_list *t)
188 {
189 	struct gve_priv *priv = from_timer(priv, t, stats_report_timer);
190 
191 	mod_timer(&priv->stats_report_timer,
192 		  round_jiffies(jiffies +
193 		  msecs_to_jiffies(priv->stats_report_timer_period)));
194 	gve_stats_report_schedule(priv);
195 }
196 
197 static int gve_alloc_stats_report(struct gve_priv *priv)
198 {
199 	int tx_stats_num, rx_stats_num;
200 
201 	tx_stats_num = (GVE_TX_STATS_REPORT_NUM + NIC_TX_STATS_REPORT_NUM) *
202 		       gve_num_tx_queues(priv);
203 	rx_stats_num = (GVE_RX_STATS_REPORT_NUM + NIC_RX_STATS_REPORT_NUM) *
204 		       priv->rx_cfg.num_queues;
205 	priv->stats_report_len = struct_size(priv->stats_report, stats,
206 					     size_add(tx_stats_num, rx_stats_num));
207 	priv->stats_report =
208 		dma_alloc_coherent(&priv->pdev->dev, priv->stats_report_len,
209 				   &priv->stats_report_bus, GFP_KERNEL);
210 	if (!priv->stats_report)
211 		return -ENOMEM;
212 	/* Set up timer for the report-stats task */
213 	timer_setup(&priv->stats_report_timer, gve_stats_report_timer, 0);
214 	priv->stats_report_timer_period = GVE_STATS_REPORT_TIMER_PERIOD;
215 	return 0;
216 }
217 
218 static void gve_free_stats_report(struct gve_priv *priv)
219 {
220 	if (!priv->stats_report)
221 		return;
222 
223 	del_timer_sync(&priv->stats_report_timer);
224 	dma_free_coherent(&priv->pdev->dev, priv->stats_report_len,
225 			  priv->stats_report, priv->stats_report_bus);
226 	priv->stats_report = NULL;
227 }
228 
229 static irqreturn_t gve_mgmnt_intr(int irq, void *arg)
230 {
231 	struct gve_priv *priv = arg;
232 
233 	queue_work(priv->gve_wq, &priv->service_task);
234 	return IRQ_HANDLED;
235 }
236 
237 static irqreturn_t gve_intr(int irq, void *arg)
238 {
239 	struct gve_notify_block *block = arg;
240 	struct gve_priv *priv = block->priv;
241 
242 	iowrite32be(GVE_IRQ_MASK, gve_irq_doorbell(priv, block));
243 	napi_schedule_irqoff(&block->napi);
244 	return IRQ_HANDLED;
245 }
246 
247 static irqreturn_t gve_intr_dqo(int irq, void *arg)
248 {
249 	struct gve_notify_block *block = arg;
250 
251 	/* Interrupts are automatically masked */
252 	napi_schedule_irqoff(&block->napi);
253 	return IRQ_HANDLED;
254 }
255 
256 int gve_napi_poll(struct napi_struct *napi, int budget)
257 {
258 	struct gve_notify_block *block;
259 	__be32 __iomem *irq_doorbell;
260 	bool reschedule = false;
261 	struct gve_priv *priv;
262 	int work_done = 0;
263 
264 	block = container_of(napi, struct gve_notify_block, napi);
265 	priv = block->priv;
266 
267 	if (block->tx) {
268 		if (block->tx->q_num < priv->tx_cfg.num_queues)
269 			reschedule |= gve_tx_poll(block, budget);
270 		else if (budget)
271 			reschedule |= gve_xdp_poll(block, budget);
272 	}
273 
274 	if (!budget)
275 		return 0;
276 
277 	if (block->rx) {
278 		work_done = gve_rx_poll(block, budget);
279 		reschedule |= work_done == budget;
280 	}
281 
282 	if (reschedule)
283 		return budget;
284 
285        /* Complete processing - don't unmask irq if busy polling is enabled */
286 	if (likely(napi_complete_done(napi, work_done))) {
287 		irq_doorbell = gve_irq_doorbell(priv, block);
288 		iowrite32be(GVE_IRQ_ACK | GVE_IRQ_EVENT, irq_doorbell);
289 
290 		/* Ensure IRQ ACK is visible before we check pending work.
291 		 * If queue had issued updates, it would be truly visible.
292 		 */
293 		mb();
294 
295 		if (block->tx)
296 			reschedule |= gve_tx_clean_pending(priv, block->tx);
297 		if (block->rx)
298 			reschedule |= gve_rx_work_pending(block->rx);
299 
300 		if (reschedule && napi_schedule(napi))
301 			iowrite32be(GVE_IRQ_MASK, irq_doorbell);
302 	}
303 	return work_done;
304 }
305 
306 int gve_napi_poll_dqo(struct napi_struct *napi, int budget)
307 {
308 	struct gve_notify_block *block =
309 		container_of(napi, struct gve_notify_block, napi);
310 	struct gve_priv *priv = block->priv;
311 	bool reschedule = false;
312 	int work_done = 0;
313 
314 	if (block->tx)
315 		reschedule |= gve_tx_poll_dqo(block, /*do_clean=*/true);
316 
317 	if (!budget)
318 		return 0;
319 
320 	if (block->rx) {
321 		work_done = gve_rx_poll_dqo(block, budget);
322 		reschedule |= work_done == budget;
323 	}
324 
325 	if (reschedule)
326 		return budget;
327 
328 	if (likely(napi_complete_done(napi, work_done))) {
329 		/* Enable interrupts again.
330 		 *
331 		 * We don't need to repoll afterwards because HW supports the
332 		 * PCI MSI-X PBA feature.
333 		 *
334 		 * Another interrupt would be triggered if a new event came in
335 		 * since the last one.
336 		 */
337 		gve_write_irq_doorbell_dqo(priv, block,
338 					   GVE_ITR_NO_UPDATE_DQO | GVE_ITR_ENABLE_BIT_DQO);
339 	}
340 
341 	return work_done;
342 }
343 
344 static int gve_alloc_notify_blocks(struct gve_priv *priv)
345 {
346 	int num_vecs_requested = priv->num_ntfy_blks + 1;
347 	unsigned int active_cpus;
348 	int vecs_enabled;
349 	int i, j;
350 	int err;
351 
352 	priv->msix_vectors = kvcalloc(num_vecs_requested,
353 				      sizeof(*priv->msix_vectors), GFP_KERNEL);
354 	if (!priv->msix_vectors)
355 		return -ENOMEM;
356 	for (i = 0; i < num_vecs_requested; i++)
357 		priv->msix_vectors[i].entry = i;
358 	vecs_enabled = pci_enable_msix_range(priv->pdev, priv->msix_vectors,
359 					     GVE_MIN_MSIX, num_vecs_requested);
360 	if (vecs_enabled < 0) {
361 		dev_err(&priv->pdev->dev, "Could not enable min msix %d/%d\n",
362 			GVE_MIN_MSIX, vecs_enabled);
363 		err = vecs_enabled;
364 		goto abort_with_msix_vectors;
365 	}
366 	if (vecs_enabled != num_vecs_requested) {
367 		int new_num_ntfy_blks = (vecs_enabled - 1) & ~0x1;
368 		int vecs_per_type = new_num_ntfy_blks / 2;
369 		int vecs_left = new_num_ntfy_blks % 2;
370 
371 		priv->num_ntfy_blks = new_num_ntfy_blks;
372 		priv->mgmt_msix_idx = priv->num_ntfy_blks;
373 		priv->tx_cfg.max_queues = min_t(int, priv->tx_cfg.max_queues,
374 						vecs_per_type);
375 		priv->rx_cfg.max_queues = min_t(int, priv->rx_cfg.max_queues,
376 						vecs_per_type + vecs_left);
377 		dev_err(&priv->pdev->dev,
378 			"Could not enable desired msix, only enabled %d, adjusting tx max queues to %d, and rx max queues to %d\n",
379 			vecs_enabled, priv->tx_cfg.max_queues,
380 			priv->rx_cfg.max_queues);
381 		if (priv->tx_cfg.num_queues > priv->tx_cfg.max_queues)
382 			priv->tx_cfg.num_queues = priv->tx_cfg.max_queues;
383 		if (priv->rx_cfg.num_queues > priv->rx_cfg.max_queues)
384 			priv->rx_cfg.num_queues = priv->rx_cfg.max_queues;
385 	}
386 	/* Half the notification blocks go to TX and half to RX */
387 	active_cpus = min_t(int, priv->num_ntfy_blks / 2, num_online_cpus());
388 
389 	/* Setup Management Vector  - the last vector */
390 	snprintf(priv->mgmt_msix_name, sizeof(priv->mgmt_msix_name), "gve-mgmnt@pci:%s",
391 		 pci_name(priv->pdev));
392 	err = request_irq(priv->msix_vectors[priv->mgmt_msix_idx].vector,
393 			  gve_mgmnt_intr, 0, priv->mgmt_msix_name, priv);
394 	if (err) {
395 		dev_err(&priv->pdev->dev, "Did not receive management vector.\n");
396 		goto abort_with_msix_enabled;
397 	}
398 	priv->irq_db_indices =
399 		dma_alloc_coherent(&priv->pdev->dev,
400 				   priv->num_ntfy_blks *
401 				   sizeof(*priv->irq_db_indices),
402 				   &priv->irq_db_indices_bus, GFP_KERNEL);
403 	if (!priv->irq_db_indices) {
404 		err = -ENOMEM;
405 		goto abort_with_mgmt_vector;
406 	}
407 
408 	priv->ntfy_blocks = kvzalloc(priv->num_ntfy_blks *
409 				     sizeof(*priv->ntfy_blocks), GFP_KERNEL);
410 	if (!priv->ntfy_blocks) {
411 		err = -ENOMEM;
412 		goto abort_with_irq_db_indices;
413 	}
414 
415 	/* Setup the other blocks - the first n-1 vectors */
416 	for (i = 0; i < priv->num_ntfy_blks; i++) {
417 		struct gve_notify_block *block = &priv->ntfy_blocks[i];
418 		int msix_idx = i;
419 
420 		snprintf(block->name, sizeof(block->name), "gve-ntfy-blk%d@pci:%s",
421 			 i, pci_name(priv->pdev));
422 		block->priv = priv;
423 		err = request_irq(priv->msix_vectors[msix_idx].vector,
424 				  gve_is_gqi(priv) ? gve_intr : gve_intr_dqo,
425 				  0, block->name, block);
426 		if (err) {
427 			dev_err(&priv->pdev->dev,
428 				"Failed to receive msix vector %d\n", i);
429 			goto abort_with_some_ntfy_blocks;
430 		}
431 		irq_set_affinity_hint(priv->msix_vectors[msix_idx].vector,
432 				      get_cpu_mask(i % active_cpus));
433 		block->irq_db_index = &priv->irq_db_indices[i].index;
434 	}
435 	return 0;
436 abort_with_some_ntfy_blocks:
437 	for (j = 0; j < i; j++) {
438 		struct gve_notify_block *block = &priv->ntfy_blocks[j];
439 		int msix_idx = j;
440 
441 		irq_set_affinity_hint(priv->msix_vectors[msix_idx].vector,
442 				      NULL);
443 		free_irq(priv->msix_vectors[msix_idx].vector, block);
444 	}
445 	kvfree(priv->ntfy_blocks);
446 	priv->ntfy_blocks = NULL;
447 abort_with_irq_db_indices:
448 	dma_free_coherent(&priv->pdev->dev, priv->num_ntfy_blks *
449 			  sizeof(*priv->irq_db_indices),
450 			  priv->irq_db_indices, priv->irq_db_indices_bus);
451 	priv->irq_db_indices = NULL;
452 abort_with_mgmt_vector:
453 	free_irq(priv->msix_vectors[priv->mgmt_msix_idx].vector, priv);
454 abort_with_msix_enabled:
455 	pci_disable_msix(priv->pdev);
456 abort_with_msix_vectors:
457 	kvfree(priv->msix_vectors);
458 	priv->msix_vectors = NULL;
459 	return err;
460 }
461 
462 static void gve_free_notify_blocks(struct gve_priv *priv)
463 {
464 	int i;
465 
466 	if (!priv->msix_vectors)
467 		return;
468 
469 	/* Free the irqs */
470 	for (i = 0; i < priv->num_ntfy_blks; i++) {
471 		struct gve_notify_block *block = &priv->ntfy_blocks[i];
472 		int msix_idx = i;
473 
474 		irq_set_affinity_hint(priv->msix_vectors[msix_idx].vector,
475 				      NULL);
476 		free_irq(priv->msix_vectors[msix_idx].vector, block);
477 	}
478 	free_irq(priv->msix_vectors[priv->mgmt_msix_idx].vector, priv);
479 	kvfree(priv->ntfy_blocks);
480 	priv->ntfy_blocks = NULL;
481 	dma_free_coherent(&priv->pdev->dev, priv->num_ntfy_blks *
482 			  sizeof(*priv->irq_db_indices),
483 			  priv->irq_db_indices, priv->irq_db_indices_bus);
484 	priv->irq_db_indices = NULL;
485 	pci_disable_msix(priv->pdev);
486 	kvfree(priv->msix_vectors);
487 	priv->msix_vectors = NULL;
488 }
489 
490 static int gve_setup_device_resources(struct gve_priv *priv)
491 {
492 	int err;
493 
494 	err = gve_alloc_counter_array(priv);
495 	if (err)
496 		return err;
497 	err = gve_alloc_notify_blocks(priv);
498 	if (err)
499 		goto abort_with_counter;
500 	err = gve_alloc_stats_report(priv);
501 	if (err)
502 		goto abort_with_ntfy_blocks;
503 	err = gve_adminq_configure_device_resources(priv,
504 						    priv->counter_array_bus,
505 						    priv->num_event_counters,
506 						    priv->irq_db_indices_bus,
507 						    priv->num_ntfy_blks);
508 	if (unlikely(err)) {
509 		dev_err(&priv->pdev->dev,
510 			"could not setup device_resources: err=%d\n", err);
511 		err = -ENXIO;
512 		goto abort_with_stats_report;
513 	}
514 
515 	if (!gve_is_gqi(priv)) {
516 		priv->ptype_lut_dqo = kvzalloc(sizeof(*priv->ptype_lut_dqo),
517 					       GFP_KERNEL);
518 		if (!priv->ptype_lut_dqo) {
519 			err = -ENOMEM;
520 			goto abort_with_stats_report;
521 		}
522 		err = gve_adminq_get_ptype_map_dqo(priv, priv->ptype_lut_dqo);
523 		if (err) {
524 			dev_err(&priv->pdev->dev,
525 				"Failed to get ptype map: err=%d\n", err);
526 			goto abort_with_ptype_lut;
527 		}
528 	}
529 
530 	err = gve_adminq_report_stats(priv, priv->stats_report_len,
531 				      priv->stats_report_bus,
532 				      GVE_STATS_REPORT_TIMER_PERIOD);
533 	if (err)
534 		dev_err(&priv->pdev->dev,
535 			"Failed to report stats: err=%d\n", err);
536 	gve_set_device_resources_ok(priv);
537 	return 0;
538 
539 abort_with_ptype_lut:
540 	kvfree(priv->ptype_lut_dqo);
541 	priv->ptype_lut_dqo = NULL;
542 abort_with_stats_report:
543 	gve_free_stats_report(priv);
544 abort_with_ntfy_blocks:
545 	gve_free_notify_blocks(priv);
546 abort_with_counter:
547 	gve_free_counter_array(priv);
548 
549 	return err;
550 }
551 
552 static void gve_trigger_reset(struct gve_priv *priv);
553 
554 static void gve_teardown_device_resources(struct gve_priv *priv)
555 {
556 	int err;
557 
558 	/* Tell device its resources are being freed */
559 	if (gve_get_device_resources_ok(priv)) {
560 		/* detach the stats report */
561 		err = gve_adminq_report_stats(priv, 0, 0x0, GVE_STATS_REPORT_TIMER_PERIOD);
562 		if (err) {
563 			dev_err(&priv->pdev->dev,
564 				"Failed to detach stats report: err=%d\n", err);
565 			gve_trigger_reset(priv);
566 		}
567 		err = gve_adminq_deconfigure_device_resources(priv);
568 		if (err) {
569 			dev_err(&priv->pdev->dev,
570 				"Could not deconfigure device resources: err=%d\n",
571 				err);
572 			gve_trigger_reset(priv);
573 		}
574 	}
575 
576 	kvfree(priv->ptype_lut_dqo);
577 	priv->ptype_lut_dqo = NULL;
578 
579 	gve_free_counter_array(priv);
580 	gve_free_notify_blocks(priv);
581 	gve_free_stats_report(priv);
582 	gve_clear_device_resources_ok(priv);
583 }
584 
585 static int gve_unregister_qpl(struct gve_priv *priv, u32 i)
586 {
587 	int err;
588 
589 	err = gve_adminq_unregister_page_list(priv, priv->qpls[i].id);
590 	if (err) {
591 		netif_err(priv, drv, priv->dev,
592 			  "Failed to unregister queue page list %d\n",
593 			  priv->qpls[i].id);
594 		return err;
595 	}
596 
597 	priv->num_registered_pages -= priv->qpls[i].num_entries;
598 	return 0;
599 }
600 
601 static int gve_register_qpl(struct gve_priv *priv, u32 i)
602 {
603 	int num_rx_qpls;
604 	int pages;
605 	int err;
606 
607 	/* Rx QPLs succeed Tx QPLs in the priv->qpls array. */
608 	num_rx_qpls = gve_num_rx_qpls(&priv->rx_cfg, gve_is_qpl(priv));
609 	if (i >= gve_rx_start_qpl_id(&priv->tx_cfg) + num_rx_qpls) {
610 		netif_err(priv, drv, priv->dev,
611 			  "Cannot register nonexisting QPL at index %d\n", i);
612 		return -EINVAL;
613 	}
614 
615 	pages = priv->qpls[i].num_entries;
616 
617 	if (pages + priv->num_registered_pages > priv->max_registered_pages) {
618 		netif_err(priv, drv, priv->dev,
619 			  "Reached max number of registered pages %llu > %llu\n",
620 			  pages + priv->num_registered_pages,
621 			  priv->max_registered_pages);
622 		return -EINVAL;
623 	}
624 
625 	err = gve_adminq_register_page_list(priv, &priv->qpls[i]);
626 	if (err) {
627 		netif_err(priv, drv, priv->dev,
628 			  "failed to register queue page list %d\n",
629 			  priv->qpls[i].id);
630 		/* This failure will trigger a reset - no need to clean
631 		 * up
632 		 */
633 		return err;
634 	}
635 
636 	priv->num_registered_pages += pages;
637 	return 0;
638 }
639 
640 static int gve_register_xdp_qpls(struct gve_priv *priv)
641 {
642 	int start_id;
643 	int err;
644 	int i;
645 
646 	start_id = gve_xdp_tx_start_queue_id(priv);
647 	for (i = start_id; i < start_id + gve_num_xdp_qpls(priv); i++) {
648 		err = gve_register_qpl(priv, i);
649 		/* This failure will trigger a reset - no need to clean up */
650 		if (err)
651 			return err;
652 	}
653 	return 0;
654 }
655 
656 static int gve_register_qpls(struct gve_priv *priv)
657 {
658 	int num_tx_qpls, num_rx_qpls;
659 	int start_id;
660 	int err;
661 	int i;
662 
663 	num_tx_qpls = gve_num_tx_qpls(&priv->tx_cfg, gve_num_xdp_qpls(priv),
664 				      gve_is_qpl(priv));
665 	num_rx_qpls = gve_num_rx_qpls(&priv->rx_cfg, gve_is_qpl(priv));
666 
667 	for (i = 0; i < num_tx_qpls; i++) {
668 		err = gve_register_qpl(priv, i);
669 		if (err)
670 			return err;
671 	}
672 
673 	/* there might be a gap between the tx and rx qpl ids */
674 	start_id = gve_rx_start_qpl_id(&priv->tx_cfg);
675 	for (i = 0; i < num_rx_qpls; i++) {
676 		err = gve_register_qpl(priv, start_id + i);
677 		if (err)
678 			return err;
679 	}
680 
681 	return 0;
682 }
683 
684 static int gve_unregister_xdp_qpls(struct gve_priv *priv)
685 {
686 	int start_id;
687 	int err;
688 	int i;
689 
690 	start_id = gve_xdp_tx_start_queue_id(priv);
691 	for (i = start_id; i < start_id + gve_num_xdp_qpls(priv); i++) {
692 		err = gve_unregister_qpl(priv, i);
693 		/* This failure will trigger a reset - no need to clean */
694 		if (err)
695 			return err;
696 	}
697 	return 0;
698 }
699 
700 static int gve_unregister_qpls(struct gve_priv *priv)
701 {
702 	int num_tx_qpls, num_rx_qpls;
703 	int start_id;
704 	int err;
705 	int i;
706 
707 	num_tx_qpls = gve_num_tx_qpls(&priv->tx_cfg, gve_num_xdp_qpls(priv),
708 				      gve_is_qpl(priv));
709 	num_rx_qpls = gve_num_rx_qpls(&priv->rx_cfg, gve_is_qpl(priv));
710 
711 	for (i = 0; i < num_tx_qpls; i++) {
712 		err = gve_unregister_qpl(priv, i);
713 		/* This failure will trigger a reset - no need to clean */
714 		if (err)
715 			return err;
716 	}
717 
718 	start_id = gve_rx_start_qpl_id(&priv->tx_cfg);
719 	for (i = 0; i < num_rx_qpls; i++) {
720 		err = gve_unregister_qpl(priv, start_id + i);
721 		/* This failure will trigger a reset - no need to clean */
722 		if (err)
723 			return err;
724 	}
725 	return 0;
726 }
727 
728 static int gve_create_xdp_rings(struct gve_priv *priv)
729 {
730 	int err;
731 
732 	err = gve_adminq_create_tx_queues(priv,
733 					  gve_xdp_tx_start_queue_id(priv),
734 					  priv->num_xdp_queues);
735 	if (err) {
736 		netif_err(priv, drv, priv->dev, "failed to create %d XDP tx queues\n",
737 			  priv->num_xdp_queues);
738 		/* This failure will trigger a reset - no need to clean
739 		 * up
740 		 */
741 		return err;
742 	}
743 	netif_dbg(priv, drv, priv->dev, "created %d XDP tx queues\n",
744 		  priv->num_xdp_queues);
745 
746 	return 0;
747 }
748 
749 static int gve_create_rings(struct gve_priv *priv)
750 {
751 	int num_tx_queues = gve_num_tx_queues(priv);
752 	int err;
753 	int i;
754 
755 	err = gve_adminq_create_tx_queues(priv, 0, num_tx_queues);
756 	if (err) {
757 		netif_err(priv, drv, priv->dev, "failed to create %d tx queues\n",
758 			  num_tx_queues);
759 		/* This failure will trigger a reset - no need to clean
760 		 * up
761 		 */
762 		return err;
763 	}
764 	netif_dbg(priv, drv, priv->dev, "created %d tx queues\n",
765 		  num_tx_queues);
766 
767 	err = gve_adminq_create_rx_queues(priv, priv->rx_cfg.num_queues);
768 	if (err) {
769 		netif_err(priv, drv, priv->dev, "failed to create %d rx queues\n",
770 			  priv->rx_cfg.num_queues);
771 		/* This failure will trigger a reset - no need to clean
772 		 * up
773 		 */
774 		return err;
775 	}
776 	netif_dbg(priv, drv, priv->dev, "created %d rx queues\n",
777 		  priv->rx_cfg.num_queues);
778 
779 	if (gve_is_gqi(priv)) {
780 		/* Rx data ring has been prefilled with packet buffers at queue
781 		 * allocation time.
782 		 *
783 		 * Write the doorbell to provide descriptor slots and packet
784 		 * buffers to the NIC.
785 		 */
786 		for (i = 0; i < priv->rx_cfg.num_queues; i++)
787 			gve_rx_write_doorbell(priv, &priv->rx[i]);
788 	} else {
789 		for (i = 0; i < priv->rx_cfg.num_queues; i++) {
790 			/* Post buffers and ring doorbell. */
791 			gve_rx_post_buffers_dqo(&priv->rx[i]);
792 		}
793 	}
794 
795 	return 0;
796 }
797 
798 static void init_xdp_sync_stats(struct gve_priv *priv)
799 {
800 	int start_id = gve_xdp_tx_start_queue_id(priv);
801 	int i;
802 
803 	/* Init stats */
804 	for (i = start_id; i < start_id + priv->num_xdp_queues; i++) {
805 		int ntfy_idx = gve_tx_idx_to_ntfy(priv, i);
806 
807 		u64_stats_init(&priv->tx[i].statss);
808 		priv->tx[i].ntfy_id = ntfy_idx;
809 	}
810 }
811 
812 static void gve_init_sync_stats(struct gve_priv *priv)
813 {
814 	int i;
815 
816 	for (i = 0; i < priv->tx_cfg.num_queues; i++)
817 		u64_stats_init(&priv->tx[i].statss);
818 
819 	/* Init stats for XDP TX queues */
820 	init_xdp_sync_stats(priv);
821 
822 	for (i = 0; i < priv->rx_cfg.num_queues; i++)
823 		u64_stats_init(&priv->rx[i].statss);
824 }
825 
826 static void gve_tx_get_curr_alloc_cfg(struct gve_priv *priv,
827 				      struct gve_tx_alloc_rings_cfg *cfg)
828 {
829 	cfg->qcfg = &priv->tx_cfg;
830 	cfg->raw_addressing = !gve_is_qpl(priv);
831 	cfg->qpls = priv->qpls;
832 	cfg->ring_size = priv->tx_desc_cnt;
833 	cfg->start_idx = 0;
834 	cfg->num_rings = gve_num_tx_queues(priv);
835 	cfg->tx = priv->tx;
836 }
837 
838 static void gve_tx_stop_rings(struct gve_priv *priv, int start_id, int num_rings)
839 {
840 	int i;
841 
842 	if (!priv->tx)
843 		return;
844 
845 	for (i = start_id; i < start_id + num_rings; i++) {
846 		if (gve_is_gqi(priv))
847 			gve_tx_stop_ring_gqi(priv, i);
848 		else
849 			gve_tx_stop_ring_dqo(priv, i);
850 	}
851 }
852 
853 static void gve_tx_start_rings(struct gve_priv *priv, int start_id,
854 			       int num_rings)
855 {
856 	int i;
857 
858 	for (i = start_id; i < start_id + num_rings; i++) {
859 		if (gve_is_gqi(priv))
860 			gve_tx_start_ring_gqi(priv, i);
861 		else
862 			gve_tx_start_ring_dqo(priv, i);
863 	}
864 }
865 
866 static int gve_alloc_xdp_rings(struct gve_priv *priv)
867 {
868 	struct gve_tx_alloc_rings_cfg cfg = {0};
869 	int err = 0;
870 
871 	if (!priv->num_xdp_queues)
872 		return 0;
873 
874 	gve_tx_get_curr_alloc_cfg(priv, &cfg);
875 	cfg.start_idx = gve_xdp_tx_start_queue_id(priv);
876 	cfg.num_rings = priv->num_xdp_queues;
877 
878 	err = gve_tx_alloc_rings_gqi(priv, &cfg);
879 	if (err)
880 		return err;
881 
882 	gve_tx_start_rings(priv, cfg.start_idx, cfg.num_rings);
883 	init_xdp_sync_stats(priv);
884 
885 	return 0;
886 }
887 
888 static int gve_alloc_rings(struct gve_priv *priv,
889 			   struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
890 			   struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
891 {
892 	int err;
893 
894 	if (gve_is_gqi(priv))
895 		err = gve_tx_alloc_rings_gqi(priv, tx_alloc_cfg);
896 	else
897 		err = gve_tx_alloc_rings_dqo(priv, tx_alloc_cfg);
898 	if (err)
899 		return err;
900 
901 	if (gve_is_gqi(priv))
902 		err = gve_rx_alloc_rings_gqi(priv, rx_alloc_cfg);
903 	else
904 		err = gve_rx_alloc_rings_dqo(priv, rx_alloc_cfg);
905 	if (err)
906 		goto free_tx;
907 
908 	return 0;
909 
910 free_tx:
911 	if (gve_is_gqi(priv))
912 		gve_tx_free_rings_gqi(priv, tx_alloc_cfg);
913 	else
914 		gve_tx_free_rings_dqo(priv, tx_alloc_cfg);
915 	return err;
916 }
917 
918 static int gve_destroy_xdp_rings(struct gve_priv *priv)
919 {
920 	int start_id;
921 	int err;
922 
923 	start_id = gve_xdp_tx_start_queue_id(priv);
924 	err = gve_adminq_destroy_tx_queues(priv,
925 					   start_id,
926 					   priv->num_xdp_queues);
927 	if (err) {
928 		netif_err(priv, drv, priv->dev,
929 			  "failed to destroy XDP queues\n");
930 		/* This failure will trigger a reset - no need to clean up */
931 		return err;
932 	}
933 	netif_dbg(priv, drv, priv->dev, "destroyed XDP queues\n");
934 
935 	return 0;
936 }
937 
938 static int gve_destroy_rings(struct gve_priv *priv)
939 {
940 	int num_tx_queues = gve_num_tx_queues(priv);
941 	int err;
942 
943 	err = gve_adminq_destroy_tx_queues(priv, 0, num_tx_queues);
944 	if (err) {
945 		netif_err(priv, drv, priv->dev,
946 			  "failed to destroy tx queues\n");
947 		/* This failure will trigger a reset - no need to clean up */
948 		return err;
949 	}
950 	netif_dbg(priv, drv, priv->dev, "destroyed tx queues\n");
951 	err = gve_adminq_destroy_rx_queues(priv, priv->rx_cfg.num_queues);
952 	if (err) {
953 		netif_err(priv, drv, priv->dev,
954 			  "failed to destroy rx queues\n");
955 		/* This failure will trigger a reset - no need to clean up */
956 		return err;
957 	}
958 	netif_dbg(priv, drv, priv->dev, "destroyed rx queues\n");
959 	return 0;
960 }
961 
962 static void gve_free_xdp_rings(struct gve_priv *priv)
963 {
964 	struct gve_tx_alloc_rings_cfg cfg = {0};
965 
966 	gve_tx_get_curr_alloc_cfg(priv, &cfg);
967 	cfg.start_idx = gve_xdp_tx_start_queue_id(priv);
968 	cfg.num_rings = priv->num_xdp_queues;
969 
970 	if (priv->tx) {
971 		gve_tx_stop_rings(priv, cfg.start_idx, cfg.num_rings);
972 		gve_tx_free_rings_gqi(priv, &cfg);
973 	}
974 }
975 
976 static void gve_free_rings(struct gve_priv *priv,
977 			   struct gve_tx_alloc_rings_cfg *tx_cfg,
978 			   struct gve_rx_alloc_rings_cfg *rx_cfg)
979 {
980 	if (gve_is_gqi(priv)) {
981 		gve_tx_free_rings_gqi(priv, tx_cfg);
982 		gve_rx_free_rings_gqi(priv, rx_cfg);
983 	} else {
984 		gve_tx_free_rings_dqo(priv, tx_cfg);
985 		gve_rx_free_rings_dqo(priv, rx_cfg);
986 	}
987 }
988 
989 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
990 		   struct page **page, dma_addr_t *dma,
991 		   enum dma_data_direction dir, gfp_t gfp_flags)
992 {
993 	*page = alloc_page(gfp_flags);
994 	if (!*page) {
995 		priv->page_alloc_fail++;
996 		return -ENOMEM;
997 	}
998 	*dma = dma_map_page(dev, *page, 0, PAGE_SIZE, dir);
999 	if (dma_mapping_error(dev, *dma)) {
1000 		priv->dma_mapping_error++;
1001 		put_page(*page);
1002 		return -ENOMEM;
1003 	}
1004 	return 0;
1005 }
1006 
1007 static int gve_alloc_queue_page_list(struct gve_priv *priv,
1008 				     struct gve_queue_page_list *qpl,
1009 				     u32 id, int pages)
1010 {
1011 	int err;
1012 	int i;
1013 
1014 	qpl->id = id;
1015 	qpl->num_entries = 0;
1016 	qpl->pages = kvcalloc(pages, sizeof(*qpl->pages), GFP_KERNEL);
1017 	/* caller handles clean up */
1018 	if (!qpl->pages)
1019 		return -ENOMEM;
1020 	qpl->page_buses = kvcalloc(pages, sizeof(*qpl->page_buses), GFP_KERNEL);
1021 	/* caller handles clean up */
1022 	if (!qpl->page_buses)
1023 		return -ENOMEM;
1024 
1025 	for (i = 0; i < pages; i++) {
1026 		err = gve_alloc_page(priv, &priv->pdev->dev, &qpl->pages[i],
1027 				     &qpl->page_buses[i],
1028 				     gve_qpl_dma_dir(priv, id), GFP_KERNEL);
1029 		/* caller handles clean up */
1030 		if (err)
1031 			return -ENOMEM;
1032 		qpl->num_entries++;
1033 	}
1034 
1035 	return 0;
1036 }
1037 
1038 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1039 		   enum dma_data_direction dir)
1040 {
1041 	if (!dma_mapping_error(dev, dma))
1042 		dma_unmap_page(dev, dma, PAGE_SIZE, dir);
1043 	if (page)
1044 		put_page(page);
1045 }
1046 
1047 static void gve_free_queue_page_list(struct gve_priv *priv,
1048 				     struct gve_queue_page_list *qpl,
1049 				     int id)
1050 {
1051 	int i;
1052 
1053 	if (!qpl->pages)
1054 		return;
1055 	if (!qpl->page_buses)
1056 		goto free_pages;
1057 
1058 	for (i = 0; i < qpl->num_entries; i++)
1059 		gve_free_page(&priv->pdev->dev, qpl->pages[i],
1060 			      qpl->page_buses[i], gve_qpl_dma_dir(priv, id));
1061 
1062 	kvfree(qpl->page_buses);
1063 	qpl->page_buses = NULL;
1064 free_pages:
1065 	kvfree(qpl->pages);
1066 	qpl->pages = NULL;
1067 }
1068 
1069 static void gve_free_n_qpls(struct gve_priv *priv,
1070 			    struct gve_queue_page_list *qpls,
1071 			    int start_id,
1072 			    int num_qpls)
1073 {
1074 	int i;
1075 
1076 	for (i = start_id; i < start_id + num_qpls; i++)
1077 		gve_free_queue_page_list(priv, &qpls[i], i);
1078 }
1079 
1080 static int gve_alloc_n_qpls(struct gve_priv *priv,
1081 			    struct gve_queue_page_list *qpls,
1082 			    int page_count,
1083 			    int start_id,
1084 			    int num_qpls)
1085 {
1086 	int err;
1087 	int i;
1088 
1089 	for (i = start_id; i < start_id + num_qpls; i++) {
1090 		err = gve_alloc_queue_page_list(priv, &qpls[i], i, page_count);
1091 		if (err)
1092 			goto free_qpls;
1093 	}
1094 
1095 	return 0;
1096 
1097 free_qpls:
1098 	/* Must include the failing QPL too for gve_alloc_queue_page_list fails
1099 	 * without cleaning up.
1100 	 */
1101 	gve_free_n_qpls(priv, qpls, start_id, i - start_id + 1);
1102 	return err;
1103 }
1104 
1105 static int gve_alloc_qpls(struct gve_priv *priv, struct gve_qpls_alloc_cfg *cfg,
1106 			  struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1107 {
1108 	int max_queues = cfg->tx_cfg->max_queues + cfg->rx_cfg->max_queues;
1109 	int rx_start_id, tx_num_qpls, rx_num_qpls;
1110 	struct gve_queue_page_list *qpls;
1111 	u32 page_count;
1112 	int err;
1113 
1114 	if (cfg->raw_addressing)
1115 		return 0;
1116 
1117 	qpls = kvcalloc(max_queues, sizeof(*qpls), GFP_KERNEL);
1118 	if (!qpls)
1119 		return -ENOMEM;
1120 
1121 	/* Allocate TX QPLs */
1122 	page_count = priv->tx_pages_per_qpl;
1123 	tx_num_qpls = gve_num_tx_qpls(cfg->tx_cfg, cfg->num_xdp_queues,
1124 				      gve_is_qpl(priv));
1125 	err = gve_alloc_n_qpls(priv, qpls, page_count, 0, tx_num_qpls);
1126 	if (err)
1127 		goto free_qpl_array;
1128 
1129 	/* Allocate RX QPLs */
1130 	rx_start_id = gve_rx_start_qpl_id(cfg->tx_cfg);
1131 	/* For GQI_QPL number of pages allocated have 1:1 relationship with
1132 	 * number of descriptors. For DQO, number of pages required are
1133 	 * more than descriptors (because of out of order completions).
1134 	 * Set it to twice the number of descriptors.
1135 	 */
1136 	if (cfg->is_gqi)
1137 		page_count = rx_alloc_cfg->ring_size;
1138 	else
1139 		page_count = gve_get_rx_pages_per_qpl_dqo(rx_alloc_cfg->ring_size);
1140 	rx_num_qpls = gve_num_rx_qpls(cfg->rx_cfg, gve_is_qpl(priv));
1141 	err = gve_alloc_n_qpls(priv, qpls, page_count, rx_start_id, rx_num_qpls);
1142 	if (err)
1143 		goto free_tx_qpls;
1144 
1145 	cfg->qpls = qpls;
1146 	return 0;
1147 
1148 free_tx_qpls:
1149 	gve_free_n_qpls(priv, qpls, 0, tx_num_qpls);
1150 free_qpl_array:
1151 	kvfree(qpls);
1152 	return err;
1153 }
1154 
1155 static void gve_free_qpls(struct gve_priv *priv,
1156 			  struct gve_qpls_alloc_cfg *cfg)
1157 {
1158 	int max_queues = cfg->tx_cfg->max_queues + cfg->rx_cfg->max_queues;
1159 	struct gve_queue_page_list *qpls = cfg->qpls;
1160 	int i;
1161 
1162 	if (!qpls)
1163 		return;
1164 
1165 	for (i = 0; i < max_queues; i++)
1166 		gve_free_queue_page_list(priv, &qpls[i], i);
1167 
1168 	kvfree(qpls);
1169 	cfg->qpls = NULL;
1170 }
1171 
1172 /* Use this to schedule a reset when the device is capable of continuing
1173  * to handle other requests in its current state. If it is not, do a reset
1174  * in thread instead.
1175  */
1176 void gve_schedule_reset(struct gve_priv *priv)
1177 {
1178 	gve_set_do_reset(priv);
1179 	queue_work(priv->gve_wq, &priv->service_task);
1180 }
1181 
1182 static void gve_reset_and_teardown(struct gve_priv *priv, bool was_up);
1183 static int gve_reset_recovery(struct gve_priv *priv, bool was_up);
1184 static void gve_turndown(struct gve_priv *priv);
1185 static void gve_turnup(struct gve_priv *priv);
1186 
1187 static int gve_reg_xdp_info(struct gve_priv *priv, struct net_device *dev)
1188 {
1189 	struct napi_struct *napi;
1190 	struct gve_rx_ring *rx;
1191 	int err = 0;
1192 	int i, j;
1193 	u32 tx_qid;
1194 
1195 	if (!priv->num_xdp_queues)
1196 		return 0;
1197 
1198 	for (i = 0; i < priv->rx_cfg.num_queues; i++) {
1199 		rx = &priv->rx[i];
1200 		napi = &priv->ntfy_blocks[rx->ntfy_id].napi;
1201 
1202 		err = xdp_rxq_info_reg(&rx->xdp_rxq, dev, i,
1203 				       napi->napi_id);
1204 		if (err)
1205 			goto err;
1206 		err = xdp_rxq_info_reg_mem_model(&rx->xdp_rxq,
1207 						 MEM_TYPE_PAGE_SHARED, NULL);
1208 		if (err)
1209 			goto err;
1210 		rx->xsk_pool = xsk_get_pool_from_qid(dev, i);
1211 		if (rx->xsk_pool) {
1212 			err = xdp_rxq_info_reg(&rx->xsk_rxq, dev, i,
1213 					       napi->napi_id);
1214 			if (err)
1215 				goto err;
1216 			err = xdp_rxq_info_reg_mem_model(&rx->xsk_rxq,
1217 							 MEM_TYPE_XSK_BUFF_POOL, NULL);
1218 			if (err)
1219 				goto err;
1220 			xsk_pool_set_rxq_info(rx->xsk_pool,
1221 					      &rx->xsk_rxq);
1222 		}
1223 	}
1224 
1225 	for (i = 0; i < priv->num_xdp_queues; i++) {
1226 		tx_qid = gve_xdp_tx_queue_id(priv, i);
1227 		priv->tx[tx_qid].xsk_pool = xsk_get_pool_from_qid(dev, i);
1228 	}
1229 	return 0;
1230 
1231 err:
1232 	for (j = i; j >= 0; j--) {
1233 		rx = &priv->rx[j];
1234 		if (xdp_rxq_info_is_reg(&rx->xdp_rxq))
1235 			xdp_rxq_info_unreg(&rx->xdp_rxq);
1236 		if (xdp_rxq_info_is_reg(&rx->xsk_rxq))
1237 			xdp_rxq_info_unreg(&rx->xsk_rxq);
1238 	}
1239 	return err;
1240 }
1241 
1242 static void gve_unreg_xdp_info(struct gve_priv *priv)
1243 {
1244 	int i, tx_qid;
1245 
1246 	if (!priv->num_xdp_queues)
1247 		return;
1248 
1249 	for (i = 0; i < priv->rx_cfg.num_queues; i++) {
1250 		struct gve_rx_ring *rx = &priv->rx[i];
1251 
1252 		xdp_rxq_info_unreg(&rx->xdp_rxq);
1253 		if (rx->xsk_pool) {
1254 			xdp_rxq_info_unreg(&rx->xsk_rxq);
1255 			rx->xsk_pool = NULL;
1256 		}
1257 	}
1258 
1259 	for (i = 0; i < priv->num_xdp_queues; i++) {
1260 		tx_qid = gve_xdp_tx_queue_id(priv, i);
1261 		priv->tx[tx_qid].xsk_pool = NULL;
1262 	}
1263 }
1264 
1265 static void gve_drain_page_cache(struct gve_priv *priv)
1266 {
1267 	int i;
1268 
1269 	for (i = 0; i < priv->rx_cfg.num_queues; i++)
1270 		page_frag_cache_drain(&priv->rx[i].page_cache);
1271 }
1272 
1273 static void gve_qpls_get_curr_alloc_cfg(struct gve_priv *priv,
1274 					struct gve_qpls_alloc_cfg *cfg)
1275 {
1276 	  cfg->raw_addressing = !gve_is_qpl(priv);
1277 	  cfg->is_gqi = gve_is_gqi(priv);
1278 	  cfg->num_xdp_queues = priv->num_xdp_queues;
1279 	  cfg->tx_cfg = &priv->tx_cfg;
1280 	  cfg->rx_cfg = &priv->rx_cfg;
1281 	  cfg->qpls = priv->qpls;
1282 }
1283 
1284 static void gve_rx_get_curr_alloc_cfg(struct gve_priv *priv,
1285 				      struct gve_rx_alloc_rings_cfg *cfg)
1286 {
1287 	cfg->qcfg = &priv->rx_cfg;
1288 	cfg->qcfg_tx = &priv->tx_cfg;
1289 	cfg->raw_addressing = !gve_is_qpl(priv);
1290 	cfg->enable_header_split = priv->header_split_enabled;
1291 	cfg->qpls = priv->qpls;
1292 	cfg->ring_size = priv->rx_desc_cnt;
1293 	cfg->packet_buffer_size = gve_is_gqi(priv) ?
1294 				  GVE_DEFAULT_RX_BUFFER_SIZE :
1295 				  priv->data_buffer_size_dqo;
1296 	cfg->rx = priv->rx;
1297 }
1298 
1299 void gve_get_curr_alloc_cfgs(struct gve_priv *priv,
1300 			     struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1301 			     struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1302 			     struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1303 {
1304 	gve_qpls_get_curr_alloc_cfg(priv, qpls_alloc_cfg);
1305 	gve_tx_get_curr_alloc_cfg(priv, tx_alloc_cfg);
1306 	gve_rx_get_curr_alloc_cfg(priv, rx_alloc_cfg);
1307 }
1308 
1309 static void gve_rx_start_rings(struct gve_priv *priv, int num_rings)
1310 {
1311 	int i;
1312 
1313 	for (i = 0; i < num_rings; i++) {
1314 		if (gve_is_gqi(priv))
1315 			gve_rx_start_ring_gqi(priv, i);
1316 		else
1317 			gve_rx_start_ring_dqo(priv, i);
1318 	}
1319 }
1320 
1321 static void gve_rx_stop_rings(struct gve_priv *priv, int num_rings)
1322 {
1323 	int i;
1324 
1325 	if (!priv->rx)
1326 		return;
1327 
1328 	for (i = 0; i < num_rings; i++) {
1329 		if (gve_is_gqi(priv))
1330 			gve_rx_stop_ring_gqi(priv, i);
1331 		else
1332 			gve_rx_stop_ring_dqo(priv, i);
1333 	}
1334 }
1335 
1336 static void gve_queues_mem_free(struct gve_priv *priv,
1337 				struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1338 				struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1339 				struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1340 {
1341 	gve_free_rings(priv, tx_alloc_cfg, rx_alloc_cfg);
1342 	gve_free_qpls(priv, qpls_alloc_cfg);
1343 }
1344 
1345 static int gve_queues_mem_alloc(struct gve_priv *priv,
1346 				struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1347 				struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1348 				struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1349 {
1350 	int err;
1351 
1352 	err = gve_alloc_qpls(priv, qpls_alloc_cfg, rx_alloc_cfg);
1353 	if (err) {
1354 		netif_err(priv, drv, priv->dev, "Failed to alloc QPLs\n");
1355 		return err;
1356 	}
1357 	tx_alloc_cfg->qpls = qpls_alloc_cfg->qpls;
1358 	rx_alloc_cfg->qpls = qpls_alloc_cfg->qpls;
1359 	err = gve_alloc_rings(priv, tx_alloc_cfg, rx_alloc_cfg);
1360 	if (err) {
1361 		netif_err(priv, drv, priv->dev, "Failed to alloc rings\n");
1362 		goto free_qpls;
1363 	}
1364 
1365 	return 0;
1366 
1367 free_qpls:
1368 	gve_free_qpls(priv, qpls_alloc_cfg);
1369 	return err;
1370 }
1371 
1372 static void gve_queues_mem_remove(struct gve_priv *priv)
1373 {
1374 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1375 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1376 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1377 
1378 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1379 				&tx_alloc_cfg, &rx_alloc_cfg);
1380 	gve_queues_mem_free(priv, &qpls_alloc_cfg,
1381 			    &tx_alloc_cfg, &rx_alloc_cfg);
1382 	priv->qpls = NULL;
1383 	priv->tx = NULL;
1384 	priv->rx = NULL;
1385 }
1386 
1387 /* The passed-in queue memory is stored into priv and the queues are made live.
1388  * No memory is allocated. Passed-in memory is freed on errors.
1389  */
1390 static int gve_queues_start(struct gve_priv *priv,
1391 			    struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1392 			    struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1393 			    struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1394 {
1395 	struct net_device *dev = priv->dev;
1396 	int err;
1397 
1398 	/* Record new resources into priv */
1399 	priv->qpls = qpls_alloc_cfg->qpls;
1400 	priv->tx = tx_alloc_cfg->tx;
1401 	priv->rx = rx_alloc_cfg->rx;
1402 
1403 	/* Record new configs into priv */
1404 	priv->tx_cfg = *tx_alloc_cfg->qcfg;
1405 	priv->rx_cfg = *rx_alloc_cfg->qcfg;
1406 	priv->tx_desc_cnt = tx_alloc_cfg->ring_size;
1407 	priv->rx_desc_cnt = rx_alloc_cfg->ring_size;
1408 
1409 	if (priv->xdp_prog)
1410 		priv->num_xdp_queues = priv->rx_cfg.num_queues;
1411 	else
1412 		priv->num_xdp_queues = 0;
1413 
1414 	gve_tx_start_rings(priv, 0, tx_alloc_cfg->num_rings);
1415 	gve_rx_start_rings(priv, rx_alloc_cfg->qcfg->num_queues);
1416 	gve_init_sync_stats(priv);
1417 
1418 	err = netif_set_real_num_tx_queues(dev, priv->tx_cfg.num_queues);
1419 	if (err)
1420 		goto stop_and_free_rings;
1421 	err = netif_set_real_num_rx_queues(dev, priv->rx_cfg.num_queues);
1422 	if (err)
1423 		goto stop_and_free_rings;
1424 
1425 	err = gve_reg_xdp_info(priv, dev);
1426 	if (err)
1427 		goto stop_and_free_rings;
1428 
1429 	err = gve_register_qpls(priv);
1430 	if (err)
1431 		goto reset;
1432 
1433 	priv->header_split_enabled = rx_alloc_cfg->enable_header_split;
1434 	priv->data_buffer_size_dqo = rx_alloc_cfg->packet_buffer_size;
1435 
1436 	err = gve_create_rings(priv);
1437 	if (err)
1438 		goto reset;
1439 
1440 	gve_set_device_rings_ok(priv);
1441 
1442 	if (gve_get_report_stats(priv))
1443 		mod_timer(&priv->stats_report_timer,
1444 			  round_jiffies(jiffies +
1445 				msecs_to_jiffies(priv->stats_report_timer_period)));
1446 
1447 	gve_turnup(priv);
1448 	queue_work(priv->gve_wq, &priv->service_task);
1449 	priv->interface_up_cnt++;
1450 	return 0;
1451 
1452 reset:
1453 	if (gve_get_reset_in_progress(priv))
1454 		goto stop_and_free_rings;
1455 	gve_reset_and_teardown(priv, true);
1456 	/* if this fails there is nothing we can do so just ignore the return */
1457 	gve_reset_recovery(priv, false);
1458 	/* return the original error */
1459 	return err;
1460 stop_and_free_rings:
1461 	gve_tx_stop_rings(priv, 0, gve_num_tx_queues(priv));
1462 	gve_rx_stop_rings(priv, priv->rx_cfg.num_queues);
1463 	gve_queues_mem_remove(priv);
1464 	return err;
1465 }
1466 
1467 static int gve_open(struct net_device *dev)
1468 {
1469 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1470 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1471 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1472 	struct gve_priv *priv = netdev_priv(dev);
1473 	int err;
1474 
1475 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1476 				&tx_alloc_cfg, &rx_alloc_cfg);
1477 
1478 	err = gve_queues_mem_alloc(priv, &qpls_alloc_cfg,
1479 				   &tx_alloc_cfg, &rx_alloc_cfg);
1480 	if (err)
1481 		return err;
1482 
1483 	/* No need to free on error: ownership of resources is lost after
1484 	 * calling gve_queues_start.
1485 	 */
1486 	err = gve_queues_start(priv, &qpls_alloc_cfg,
1487 			       &tx_alloc_cfg, &rx_alloc_cfg);
1488 	if (err)
1489 		return err;
1490 
1491 	return 0;
1492 }
1493 
1494 static int gve_queues_stop(struct gve_priv *priv)
1495 {
1496 	int err;
1497 
1498 	netif_carrier_off(priv->dev);
1499 	if (gve_get_device_rings_ok(priv)) {
1500 		gve_turndown(priv);
1501 		gve_drain_page_cache(priv);
1502 		err = gve_destroy_rings(priv);
1503 		if (err)
1504 			goto err;
1505 		err = gve_unregister_qpls(priv);
1506 		if (err)
1507 			goto err;
1508 		gve_clear_device_rings_ok(priv);
1509 	}
1510 	del_timer_sync(&priv->stats_report_timer);
1511 
1512 	gve_unreg_xdp_info(priv);
1513 
1514 	gve_tx_stop_rings(priv, 0, gve_num_tx_queues(priv));
1515 	gve_rx_stop_rings(priv, priv->rx_cfg.num_queues);
1516 
1517 	priv->interface_down_cnt++;
1518 	return 0;
1519 
1520 err:
1521 	/* This must have been called from a reset due to the rtnl lock
1522 	 * so just return at this point.
1523 	 */
1524 	if (gve_get_reset_in_progress(priv))
1525 		return err;
1526 	/* Otherwise reset before returning */
1527 	gve_reset_and_teardown(priv, true);
1528 	return gve_reset_recovery(priv, false);
1529 }
1530 
1531 static int gve_close(struct net_device *dev)
1532 {
1533 	struct gve_priv *priv = netdev_priv(dev);
1534 	int err;
1535 
1536 	err = gve_queues_stop(priv);
1537 	if (err)
1538 		return err;
1539 
1540 	gve_queues_mem_remove(priv);
1541 	return 0;
1542 }
1543 
1544 static int gve_remove_xdp_queues(struct gve_priv *priv)
1545 {
1546 	int qpl_start_id;
1547 	int err;
1548 
1549 	qpl_start_id = gve_xdp_tx_start_queue_id(priv);
1550 
1551 	err = gve_destroy_xdp_rings(priv);
1552 	if (err)
1553 		return err;
1554 
1555 	err = gve_unregister_xdp_qpls(priv);
1556 	if (err)
1557 		return err;
1558 
1559 	gve_unreg_xdp_info(priv);
1560 	gve_free_xdp_rings(priv);
1561 
1562 	gve_free_n_qpls(priv, priv->qpls, qpl_start_id, gve_num_xdp_qpls(priv));
1563 	priv->num_xdp_queues = 0;
1564 	return 0;
1565 }
1566 
1567 static int gve_add_xdp_queues(struct gve_priv *priv)
1568 {
1569 	int start_id;
1570 	int err;
1571 
1572 	priv->num_xdp_queues = priv->rx_cfg.num_queues;
1573 
1574 	start_id = gve_xdp_tx_start_queue_id(priv);
1575 	err = gve_alloc_n_qpls(priv, priv->qpls, priv->tx_pages_per_qpl,
1576 			       start_id, gve_num_xdp_qpls(priv));
1577 	if (err)
1578 		goto err;
1579 
1580 	err = gve_alloc_xdp_rings(priv);
1581 	if (err)
1582 		goto free_xdp_qpls;
1583 
1584 	err = gve_reg_xdp_info(priv, priv->dev);
1585 	if (err)
1586 		goto free_xdp_rings;
1587 
1588 	err = gve_register_xdp_qpls(priv);
1589 	if (err)
1590 		goto free_xdp_rings;
1591 
1592 	err = gve_create_xdp_rings(priv);
1593 	if (err)
1594 		goto free_xdp_rings;
1595 
1596 	return 0;
1597 
1598 free_xdp_rings:
1599 	gve_free_xdp_rings(priv);
1600 free_xdp_qpls:
1601 	gve_free_n_qpls(priv, priv->qpls, start_id, gve_num_xdp_qpls(priv));
1602 err:
1603 	priv->num_xdp_queues = 0;
1604 	return err;
1605 }
1606 
1607 static void gve_handle_link_status(struct gve_priv *priv, bool link_status)
1608 {
1609 	if (!gve_get_napi_enabled(priv))
1610 		return;
1611 
1612 	if (link_status == netif_carrier_ok(priv->dev))
1613 		return;
1614 
1615 	if (link_status) {
1616 		netdev_info(priv->dev, "Device link is up.\n");
1617 		netif_carrier_on(priv->dev);
1618 	} else {
1619 		netdev_info(priv->dev, "Device link is down.\n");
1620 		netif_carrier_off(priv->dev);
1621 	}
1622 }
1623 
1624 static int gve_set_xdp(struct gve_priv *priv, struct bpf_prog *prog,
1625 		       struct netlink_ext_ack *extack)
1626 {
1627 	struct bpf_prog *old_prog;
1628 	int err = 0;
1629 	u32 status;
1630 
1631 	old_prog = READ_ONCE(priv->xdp_prog);
1632 	if (!netif_carrier_ok(priv->dev)) {
1633 		WRITE_ONCE(priv->xdp_prog, prog);
1634 		if (old_prog)
1635 			bpf_prog_put(old_prog);
1636 		return 0;
1637 	}
1638 
1639 	gve_turndown(priv);
1640 	if (!old_prog && prog) {
1641 		// Allocate XDP TX queues if an XDP program is
1642 		// being installed
1643 		err = gve_add_xdp_queues(priv);
1644 		if (err)
1645 			goto out;
1646 	} else if (old_prog && !prog) {
1647 		// Remove XDP TX queues if an XDP program is
1648 		// being uninstalled
1649 		err = gve_remove_xdp_queues(priv);
1650 		if (err)
1651 			goto out;
1652 	}
1653 	WRITE_ONCE(priv->xdp_prog, prog);
1654 	if (old_prog)
1655 		bpf_prog_put(old_prog);
1656 
1657 out:
1658 	gve_turnup(priv);
1659 	status = ioread32be(&priv->reg_bar0->device_status);
1660 	gve_handle_link_status(priv, GVE_DEVICE_STATUS_LINK_STATUS_MASK & status);
1661 	return err;
1662 }
1663 
1664 static int gve_xsk_pool_enable(struct net_device *dev,
1665 			       struct xsk_buff_pool *pool,
1666 			       u16 qid)
1667 {
1668 	struct gve_priv *priv = netdev_priv(dev);
1669 	struct napi_struct *napi;
1670 	struct gve_rx_ring *rx;
1671 	int tx_qid;
1672 	int err;
1673 
1674 	if (qid >= priv->rx_cfg.num_queues) {
1675 		dev_err(&priv->pdev->dev, "xsk pool invalid qid %d", qid);
1676 		return -EINVAL;
1677 	}
1678 	if (xsk_pool_get_rx_frame_size(pool) <
1679 	     priv->dev->max_mtu + sizeof(struct ethhdr)) {
1680 		dev_err(&priv->pdev->dev, "xsk pool frame_len too small");
1681 		return -EINVAL;
1682 	}
1683 
1684 	err = xsk_pool_dma_map(pool, &priv->pdev->dev,
1685 			       DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1686 	if (err)
1687 		return err;
1688 
1689 	/* If XDP prog is not installed, return */
1690 	if (!priv->xdp_prog)
1691 		return 0;
1692 
1693 	rx = &priv->rx[qid];
1694 	napi = &priv->ntfy_blocks[rx->ntfy_id].napi;
1695 	err = xdp_rxq_info_reg(&rx->xsk_rxq, dev, qid, napi->napi_id);
1696 	if (err)
1697 		goto err;
1698 
1699 	err = xdp_rxq_info_reg_mem_model(&rx->xsk_rxq,
1700 					 MEM_TYPE_XSK_BUFF_POOL, NULL);
1701 	if (err)
1702 		goto err;
1703 
1704 	xsk_pool_set_rxq_info(pool, &rx->xsk_rxq);
1705 	rx->xsk_pool = pool;
1706 
1707 	tx_qid = gve_xdp_tx_queue_id(priv, qid);
1708 	priv->tx[tx_qid].xsk_pool = pool;
1709 
1710 	return 0;
1711 err:
1712 	if (xdp_rxq_info_is_reg(&rx->xsk_rxq))
1713 		xdp_rxq_info_unreg(&rx->xsk_rxq);
1714 
1715 	xsk_pool_dma_unmap(pool,
1716 			   DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1717 	return err;
1718 }
1719 
1720 static int gve_xsk_pool_disable(struct net_device *dev,
1721 				u16 qid)
1722 {
1723 	struct gve_priv *priv = netdev_priv(dev);
1724 	struct napi_struct *napi_rx;
1725 	struct napi_struct *napi_tx;
1726 	struct xsk_buff_pool *pool;
1727 	int tx_qid;
1728 
1729 	pool = xsk_get_pool_from_qid(dev, qid);
1730 	if (!pool)
1731 		return -EINVAL;
1732 	if (qid >= priv->rx_cfg.num_queues)
1733 		return -EINVAL;
1734 
1735 	/* If XDP prog is not installed, unmap DMA and return */
1736 	if (!priv->xdp_prog)
1737 		goto done;
1738 
1739 	tx_qid = gve_xdp_tx_queue_id(priv, qid);
1740 	if (!netif_running(dev)) {
1741 		priv->rx[qid].xsk_pool = NULL;
1742 		xdp_rxq_info_unreg(&priv->rx[qid].xsk_rxq);
1743 		priv->tx[tx_qid].xsk_pool = NULL;
1744 		goto done;
1745 	}
1746 
1747 	napi_rx = &priv->ntfy_blocks[priv->rx[qid].ntfy_id].napi;
1748 	napi_disable(napi_rx); /* make sure current rx poll is done */
1749 
1750 	napi_tx = &priv->ntfy_blocks[priv->tx[tx_qid].ntfy_id].napi;
1751 	napi_disable(napi_tx); /* make sure current tx poll is done */
1752 
1753 	priv->rx[qid].xsk_pool = NULL;
1754 	xdp_rxq_info_unreg(&priv->rx[qid].xsk_rxq);
1755 	priv->tx[tx_qid].xsk_pool = NULL;
1756 	smp_mb(); /* Make sure it is visible to the workers on datapath */
1757 
1758 	napi_enable(napi_rx);
1759 	if (gve_rx_work_pending(&priv->rx[qid]))
1760 		napi_schedule(napi_rx);
1761 
1762 	napi_enable(napi_tx);
1763 	if (gve_tx_clean_pending(priv, &priv->tx[tx_qid]))
1764 		napi_schedule(napi_tx);
1765 
1766 done:
1767 	xsk_pool_dma_unmap(pool,
1768 			   DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1769 	return 0;
1770 }
1771 
1772 static int gve_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
1773 {
1774 	struct gve_priv *priv = netdev_priv(dev);
1775 	int tx_queue_id = gve_xdp_tx_queue_id(priv, queue_id);
1776 
1777 	if (queue_id >= priv->rx_cfg.num_queues || !priv->xdp_prog)
1778 		return -EINVAL;
1779 
1780 	if (flags & XDP_WAKEUP_TX) {
1781 		struct gve_tx_ring *tx = &priv->tx[tx_queue_id];
1782 		struct napi_struct *napi =
1783 			&priv->ntfy_blocks[tx->ntfy_id].napi;
1784 
1785 		if (!napi_if_scheduled_mark_missed(napi)) {
1786 			/* Call local_bh_enable to trigger SoftIRQ processing */
1787 			local_bh_disable();
1788 			napi_schedule(napi);
1789 			local_bh_enable();
1790 		}
1791 
1792 		tx->xdp_xsk_wakeup++;
1793 	}
1794 
1795 	return 0;
1796 }
1797 
1798 static int verify_xdp_configuration(struct net_device *dev)
1799 {
1800 	struct gve_priv *priv = netdev_priv(dev);
1801 
1802 	if (dev->features & NETIF_F_LRO) {
1803 		netdev_warn(dev, "XDP is not supported when LRO is on.\n");
1804 		return -EOPNOTSUPP;
1805 	}
1806 
1807 	if (priv->queue_format != GVE_GQI_QPL_FORMAT) {
1808 		netdev_warn(dev, "XDP is not supported in mode %d.\n",
1809 			    priv->queue_format);
1810 		return -EOPNOTSUPP;
1811 	}
1812 
1813 	if (dev->mtu > GVE_DEFAULT_RX_BUFFER_SIZE - sizeof(struct ethhdr) - GVE_RX_PAD) {
1814 		netdev_warn(dev, "XDP is not supported for mtu %d.\n",
1815 			    dev->mtu);
1816 		return -EOPNOTSUPP;
1817 	}
1818 
1819 	if (priv->rx_cfg.num_queues != priv->tx_cfg.num_queues ||
1820 	    (2 * priv->tx_cfg.num_queues > priv->tx_cfg.max_queues)) {
1821 		netdev_warn(dev, "XDP load failed: The number of configured RX queues %d should be equal to the number of configured TX queues %d and the number of configured RX/TX queues should be less than or equal to half the maximum number of RX/TX queues %d",
1822 			    priv->rx_cfg.num_queues,
1823 			    priv->tx_cfg.num_queues,
1824 			    priv->tx_cfg.max_queues);
1825 		return -EINVAL;
1826 	}
1827 	return 0;
1828 }
1829 
1830 static int gve_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1831 {
1832 	struct gve_priv *priv = netdev_priv(dev);
1833 	int err;
1834 
1835 	err = verify_xdp_configuration(dev);
1836 	if (err)
1837 		return err;
1838 	switch (xdp->command) {
1839 	case XDP_SETUP_PROG:
1840 		return gve_set_xdp(priv, xdp->prog, xdp->extack);
1841 	case XDP_SETUP_XSK_POOL:
1842 		if (xdp->xsk.pool)
1843 			return gve_xsk_pool_enable(dev, xdp->xsk.pool, xdp->xsk.queue_id);
1844 		else
1845 			return gve_xsk_pool_disable(dev, xdp->xsk.queue_id);
1846 	default:
1847 		return -EINVAL;
1848 	}
1849 }
1850 
1851 int gve_adjust_config(struct gve_priv *priv,
1852 		      struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1853 		      struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1854 		      struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1855 {
1856 	int err;
1857 
1858 	/* Allocate resources for the new confiugration */
1859 	err = gve_queues_mem_alloc(priv, qpls_alloc_cfg,
1860 				   tx_alloc_cfg, rx_alloc_cfg);
1861 	if (err) {
1862 		netif_err(priv, drv, priv->dev,
1863 			  "Adjust config failed to alloc new queues");
1864 		return err;
1865 	}
1866 
1867 	/* Teardown the device and free existing resources */
1868 	err = gve_close(priv->dev);
1869 	if (err) {
1870 		netif_err(priv, drv, priv->dev,
1871 			  "Adjust config failed to close old queues");
1872 		gve_queues_mem_free(priv, qpls_alloc_cfg,
1873 				    tx_alloc_cfg, rx_alloc_cfg);
1874 		return err;
1875 	}
1876 
1877 	/* Bring the device back up again with the new resources. */
1878 	err = gve_queues_start(priv, qpls_alloc_cfg,
1879 			       tx_alloc_cfg, rx_alloc_cfg);
1880 	if (err) {
1881 		netif_err(priv, drv, priv->dev,
1882 			  "Adjust config failed to start new queues, !!! DISABLING ALL QUEUES !!!\n");
1883 		/* No need to free on error: ownership of resources is lost after
1884 		 * calling gve_queues_start.
1885 		 */
1886 		gve_turndown(priv);
1887 		return err;
1888 	}
1889 
1890 	return 0;
1891 }
1892 
1893 int gve_adjust_queues(struct gve_priv *priv,
1894 		      struct gve_queue_config new_rx_config,
1895 		      struct gve_queue_config new_tx_config)
1896 {
1897 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1898 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1899 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1900 	int err;
1901 
1902 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1903 				&tx_alloc_cfg, &rx_alloc_cfg);
1904 
1905 	/* Relay the new config from ethtool */
1906 	qpls_alloc_cfg.tx_cfg = &new_tx_config;
1907 	tx_alloc_cfg.qcfg = &new_tx_config;
1908 	rx_alloc_cfg.qcfg_tx = &new_tx_config;
1909 	qpls_alloc_cfg.rx_cfg = &new_rx_config;
1910 	rx_alloc_cfg.qcfg = &new_rx_config;
1911 	tx_alloc_cfg.num_rings = new_tx_config.num_queues;
1912 
1913 	if (netif_carrier_ok(priv->dev)) {
1914 		err = gve_adjust_config(priv, &qpls_alloc_cfg,
1915 					&tx_alloc_cfg, &rx_alloc_cfg);
1916 		return err;
1917 	}
1918 	/* Set the config for the next up. */
1919 	priv->tx_cfg = new_tx_config;
1920 	priv->rx_cfg = new_rx_config;
1921 
1922 	return 0;
1923 }
1924 
1925 static void gve_turndown(struct gve_priv *priv)
1926 {
1927 	int idx;
1928 
1929 	if (netif_carrier_ok(priv->dev))
1930 		netif_carrier_off(priv->dev);
1931 
1932 	if (!gve_get_napi_enabled(priv))
1933 		return;
1934 
1935 	/* Disable napi to prevent more work from coming in */
1936 	for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
1937 		int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
1938 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1939 
1940 		napi_disable(&block->napi);
1941 	}
1942 	for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
1943 		int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx);
1944 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1945 
1946 		napi_disable(&block->napi);
1947 	}
1948 
1949 	/* Stop tx queues */
1950 	netif_tx_disable(priv->dev);
1951 
1952 	gve_clear_napi_enabled(priv);
1953 	gve_clear_report_stats(priv);
1954 }
1955 
1956 static void gve_turnup(struct gve_priv *priv)
1957 {
1958 	int idx;
1959 
1960 	/* Start the tx queues */
1961 	netif_tx_start_all_queues(priv->dev);
1962 
1963 	/* Enable napi and unmask interrupts for all queues */
1964 	for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
1965 		int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
1966 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1967 
1968 		napi_enable(&block->napi);
1969 		if (gve_is_gqi(priv)) {
1970 			iowrite32be(0, gve_irq_doorbell(priv, block));
1971 		} else {
1972 			gve_set_itr_coalesce_usecs_dqo(priv, block,
1973 						       priv->tx_coalesce_usecs);
1974 		}
1975 	}
1976 	for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
1977 		int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx);
1978 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1979 
1980 		napi_enable(&block->napi);
1981 		if (gve_is_gqi(priv)) {
1982 			iowrite32be(0, gve_irq_doorbell(priv, block));
1983 		} else {
1984 			gve_set_itr_coalesce_usecs_dqo(priv, block,
1985 						       priv->rx_coalesce_usecs);
1986 		}
1987 	}
1988 
1989 	gve_set_napi_enabled(priv);
1990 }
1991 
1992 static void gve_tx_timeout(struct net_device *dev, unsigned int txqueue)
1993 {
1994 	struct gve_notify_block *block;
1995 	struct gve_tx_ring *tx = NULL;
1996 	struct gve_priv *priv;
1997 	u32 last_nic_done;
1998 	u32 current_time;
1999 	u32 ntfy_idx;
2000 
2001 	netdev_info(dev, "Timeout on tx queue, %d", txqueue);
2002 	priv = netdev_priv(dev);
2003 	if (txqueue > priv->tx_cfg.num_queues)
2004 		goto reset;
2005 
2006 	ntfy_idx = gve_tx_idx_to_ntfy(priv, txqueue);
2007 	if (ntfy_idx >= priv->num_ntfy_blks)
2008 		goto reset;
2009 
2010 	block = &priv->ntfy_blocks[ntfy_idx];
2011 	tx = block->tx;
2012 
2013 	current_time = jiffies_to_msecs(jiffies);
2014 	if (tx->last_kick_msec + MIN_TX_TIMEOUT_GAP > current_time)
2015 		goto reset;
2016 
2017 	/* Check to see if there are missed completions, which will allow us to
2018 	 * kick the queue.
2019 	 */
2020 	last_nic_done = gve_tx_load_event_counter(priv, tx);
2021 	if (last_nic_done - tx->done) {
2022 		netdev_info(dev, "Kicking queue %d", txqueue);
2023 		iowrite32be(GVE_IRQ_MASK, gve_irq_doorbell(priv, block));
2024 		napi_schedule(&block->napi);
2025 		tx->last_kick_msec = current_time;
2026 		goto out;
2027 	} // Else reset.
2028 
2029 reset:
2030 	gve_schedule_reset(priv);
2031 
2032 out:
2033 	if (tx)
2034 		tx->queue_timeout++;
2035 	priv->tx_timeo_cnt++;
2036 }
2037 
2038 u16 gve_get_pkt_buf_size(const struct gve_priv *priv, bool enable_hsplit)
2039 {
2040 	if (enable_hsplit && priv->max_rx_buffer_size >= GVE_MAX_RX_BUFFER_SIZE)
2041 		return GVE_MAX_RX_BUFFER_SIZE;
2042 	else
2043 		return GVE_DEFAULT_RX_BUFFER_SIZE;
2044 }
2045 
2046 /* header-split is not supported on non-DQO_RDA yet even if device advertises it */
2047 bool gve_header_split_supported(const struct gve_priv *priv)
2048 {
2049 	return priv->header_buf_size && priv->queue_format == GVE_DQO_RDA_FORMAT;
2050 }
2051 
2052 int gve_set_hsplit_config(struct gve_priv *priv, u8 tcp_data_split)
2053 {
2054 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
2055 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
2056 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
2057 	bool enable_hdr_split;
2058 	int err = 0;
2059 
2060 	if (tcp_data_split == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN)
2061 		return 0;
2062 
2063 	if (!gve_header_split_supported(priv)) {
2064 		dev_err(&priv->pdev->dev, "Header-split not supported\n");
2065 		return -EOPNOTSUPP;
2066 	}
2067 
2068 	if (tcp_data_split == ETHTOOL_TCP_DATA_SPLIT_ENABLED)
2069 		enable_hdr_split = true;
2070 	else
2071 		enable_hdr_split = false;
2072 
2073 	if (enable_hdr_split == priv->header_split_enabled)
2074 		return 0;
2075 
2076 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
2077 				&tx_alloc_cfg, &rx_alloc_cfg);
2078 
2079 	rx_alloc_cfg.enable_header_split = enable_hdr_split;
2080 	rx_alloc_cfg.packet_buffer_size = gve_get_pkt_buf_size(priv, enable_hdr_split);
2081 
2082 	if (netif_running(priv->dev))
2083 		err = gve_adjust_config(priv, &qpls_alloc_cfg,
2084 					&tx_alloc_cfg, &rx_alloc_cfg);
2085 	return err;
2086 }
2087 
2088 static int gve_set_features(struct net_device *netdev,
2089 			    netdev_features_t features)
2090 {
2091 	const netdev_features_t orig_features = netdev->features;
2092 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
2093 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
2094 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
2095 	struct gve_priv *priv = netdev_priv(netdev);
2096 	int err;
2097 
2098 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
2099 				&tx_alloc_cfg, &rx_alloc_cfg);
2100 
2101 	if ((netdev->features & NETIF_F_LRO) != (features & NETIF_F_LRO)) {
2102 		netdev->features ^= NETIF_F_LRO;
2103 		if (netif_carrier_ok(netdev)) {
2104 			err = gve_adjust_config(priv, &qpls_alloc_cfg,
2105 						&tx_alloc_cfg, &rx_alloc_cfg);
2106 			if (err) {
2107 				/* Revert the change on error. */
2108 				netdev->features = orig_features;
2109 				return err;
2110 			}
2111 		}
2112 	}
2113 
2114 	return 0;
2115 }
2116 
2117 static const struct net_device_ops gve_netdev_ops = {
2118 	.ndo_start_xmit		=	gve_start_xmit,
2119 	.ndo_features_check	=	gve_features_check,
2120 	.ndo_open		=	gve_open,
2121 	.ndo_stop		=	gve_close,
2122 	.ndo_get_stats64	=	gve_get_stats,
2123 	.ndo_tx_timeout         =       gve_tx_timeout,
2124 	.ndo_set_features	=	gve_set_features,
2125 	.ndo_bpf		=	gve_xdp,
2126 	.ndo_xdp_xmit		=	gve_xdp_xmit,
2127 	.ndo_xsk_wakeup		=	gve_xsk_wakeup,
2128 };
2129 
2130 static void gve_handle_status(struct gve_priv *priv, u32 status)
2131 {
2132 	if (GVE_DEVICE_STATUS_RESET_MASK & status) {
2133 		dev_info(&priv->pdev->dev, "Device requested reset.\n");
2134 		gve_set_do_reset(priv);
2135 	}
2136 	if (GVE_DEVICE_STATUS_REPORT_STATS_MASK & status) {
2137 		priv->stats_report_trigger_cnt++;
2138 		gve_set_do_report_stats(priv);
2139 	}
2140 }
2141 
2142 static void gve_handle_reset(struct gve_priv *priv)
2143 {
2144 	/* A service task will be scheduled at the end of probe to catch any
2145 	 * resets that need to happen, and we don't want to reset until
2146 	 * probe is done.
2147 	 */
2148 	if (gve_get_probe_in_progress(priv))
2149 		return;
2150 
2151 	if (gve_get_do_reset(priv)) {
2152 		rtnl_lock();
2153 		gve_reset(priv, false);
2154 		rtnl_unlock();
2155 	}
2156 }
2157 
2158 void gve_handle_report_stats(struct gve_priv *priv)
2159 {
2160 	struct stats *stats = priv->stats_report->stats;
2161 	int idx, stats_idx = 0;
2162 	unsigned int start = 0;
2163 	u64 tx_bytes;
2164 
2165 	if (!gve_get_report_stats(priv))
2166 		return;
2167 
2168 	be64_add_cpu(&priv->stats_report->written_count, 1);
2169 	/* tx stats */
2170 	if (priv->tx) {
2171 		for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
2172 			u32 last_completion = 0;
2173 			u32 tx_frames = 0;
2174 
2175 			/* DQO doesn't currently support these metrics. */
2176 			if (gve_is_gqi(priv)) {
2177 				last_completion = priv->tx[idx].done;
2178 				tx_frames = priv->tx[idx].req;
2179 			}
2180 
2181 			do {
2182 				start = u64_stats_fetch_begin(&priv->tx[idx].statss);
2183 				tx_bytes = priv->tx[idx].bytes_done;
2184 			} while (u64_stats_fetch_retry(&priv->tx[idx].statss, start));
2185 			stats[stats_idx++] = (struct stats) {
2186 				.stat_name = cpu_to_be32(TX_WAKE_CNT),
2187 				.value = cpu_to_be64(priv->tx[idx].wake_queue),
2188 				.queue_id = cpu_to_be32(idx),
2189 			};
2190 			stats[stats_idx++] = (struct stats) {
2191 				.stat_name = cpu_to_be32(TX_STOP_CNT),
2192 				.value = cpu_to_be64(priv->tx[idx].stop_queue),
2193 				.queue_id = cpu_to_be32(idx),
2194 			};
2195 			stats[stats_idx++] = (struct stats) {
2196 				.stat_name = cpu_to_be32(TX_FRAMES_SENT),
2197 				.value = cpu_to_be64(tx_frames),
2198 				.queue_id = cpu_to_be32(idx),
2199 			};
2200 			stats[stats_idx++] = (struct stats) {
2201 				.stat_name = cpu_to_be32(TX_BYTES_SENT),
2202 				.value = cpu_to_be64(tx_bytes),
2203 				.queue_id = cpu_to_be32(idx),
2204 			};
2205 			stats[stats_idx++] = (struct stats) {
2206 				.stat_name = cpu_to_be32(TX_LAST_COMPLETION_PROCESSED),
2207 				.value = cpu_to_be64(last_completion),
2208 				.queue_id = cpu_to_be32(idx),
2209 			};
2210 			stats[stats_idx++] = (struct stats) {
2211 				.stat_name = cpu_to_be32(TX_TIMEOUT_CNT),
2212 				.value = cpu_to_be64(priv->tx[idx].queue_timeout),
2213 				.queue_id = cpu_to_be32(idx),
2214 			};
2215 		}
2216 	}
2217 	/* rx stats */
2218 	if (priv->rx) {
2219 		for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
2220 			stats[stats_idx++] = (struct stats) {
2221 				.stat_name = cpu_to_be32(RX_NEXT_EXPECTED_SEQUENCE),
2222 				.value = cpu_to_be64(priv->rx[idx].desc.seqno),
2223 				.queue_id = cpu_to_be32(idx),
2224 			};
2225 			stats[stats_idx++] = (struct stats) {
2226 				.stat_name = cpu_to_be32(RX_BUFFERS_POSTED),
2227 				.value = cpu_to_be64(priv->rx[0].fill_cnt),
2228 				.queue_id = cpu_to_be32(idx),
2229 			};
2230 		}
2231 	}
2232 }
2233 
2234 /* Handle NIC status register changes, reset requests and report stats */
2235 static void gve_service_task(struct work_struct *work)
2236 {
2237 	struct gve_priv *priv = container_of(work, struct gve_priv,
2238 					     service_task);
2239 	u32 status = ioread32be(&priv->reg_bar0->device_status);
2240 
2241 	gve_handle_status(priv, status);
2242 
2243 	gve_handle_reset(priv);
2244 	gve_handle_link_status(priv, GVE_DEVICE_STATUS_LINK_STATUS_MASK & status);
2245 }
2246 
2247 static void gve_set_netdev_xdp_features(struct gve_priv *priv)
2248 {
2249 	if (priv->queue_format == GVE_GQI_QPL_FORMAT) {
2250 		priv->dev->xdp_features = NETDEV_XDP_ACT_BASIC;
2251 		priv->dev->xdp_features |= NETDEV_XDP_ACT_REDIRECT;
2252 		priv->dev->xdp_features |= NETDEV_XDP_ACT_NDO_XMIT;
2253 		priv->dev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
2254 	} else {
2255 		priv->dev->xdp_features = 0;
2256 	}
2257 }
2258 
2259 static int gve_init_priv(struct gve_priv *priv, bool skip_describe_device)
2260 {
2261 	int num_ntfy;
2262 	int err;
2263 
2264 	/* Set up the adminq */
2265 	err = gve_adminq_alloc(&priv->pdev->dev, priv);
2266 	if (err) {
2267 		dev_err(&priv->pdev->dev,
2268 			"Failed to alloc admin queue: err=%d\n", err);
2269 		return err;
2270 	}
2271 
2272 	err = gve_verify_driver_compatibility(priv);
2273 	if (err) {
2274 		dev_err(&priv->pdev->dev,
2275 			"Could not verify driver compatibility: err=%d\n", err);
2276 		goto err;
2277 	}
2278 
2279 	priv->num_registered_pages = 0;
2280 
2281 	if (skip_describe_device)
2282 		goto setup_device;
2283 
2284 	priv->queue_format = GVE_QUEUE_FORMAT_UNSPECIFIED;
2285 	/* Get the initial information we need from the device */
2286 	err = gve_adminq_describe_device(priv);
2287 	if (err) {
2288 		dev_err(&priv->pdev->dev,
2289 			"Could not get device information: err=%d\n", err);
2290 		goto err;
2291 	}
2292 	priv->dev->mtu = priv->dev->max_mtu;
2293 	num_ntfy = pci_msix_vec_count(priv->pdev);
2294 	if (num_ntfy <= 0) {
2295 		dev_err(&priv->pdev->dev,
2296 			"could not count MSI-x vectors: err=%d\n", num_ntfy);
2297 		err = num_ntfy;
2298 		goto err;
2299 	} else if (num_ntfy < GVE_MIN_MSIX) {
2300 		dev_err(&priv->pdev->dev, "gve needs at least %d MSI-x vectors, but only has %d\n",
2301 			GVE_MIN_MSIX, num_ntfy);
2302 		err = -EINVAL;
2303 		goto err;
2304 	}
2305 
2306 	/* Big TCP is only supported on DQ*/
2307 	if (!gve_is_gqi(priv))
2308 		netif_set_tso_max_size(priv->dev, GVE_DQO_TX_MAX);
2309 
2310 	priv->rx_copybreak = GVE_DEFAULT_RX_COPYBREAK;
2311 	/* gvnic has one Notification Block per MSI-x vector, except for the
2312 	 * management vector
2313 	 */
2314 	priv->num_ntfy_blks = (num_ntfy - 1) & ~0x1;
2315 	priv->mgmt_msix_idx = priv->num_ntfy_blks;
2316 
2317 	priv->tx_cfg.max_queues =
2318 		min_t(int, priv->tx_cfg.max_queues, priv->num_ntfy_blks / 2);
2319 	priv->rx_cfg.max_queues =
2320 		min_t(int, priv->rx_cfg.max_queues, priv->num_ntfy_blks / 2);
2321 
2322 	priv->tx_cfg.num_queues = priv->tx_cfg.max_queues;
2323 	priv->rx_cfg.num_queues = priv->rx_cfg.max_queues;
2324 	if (priv->default_num_queues > 0) {
2325 		priv->tx_cfg.num_queues = min_t(int, priv->default_num_queues,
2326 						priv->tx_cfg.num_queues);
2327 		priv->rx_cfg.num_queues = min_t(int, priv->default_num_queues,
2328 						priv->rx_cfg.num_queues);
2329 	}
2330 
2331 	dev_info(&priv->pdev->dev, "TX queues %d, RX queues %d\n",
2332 		 priv->tx_cfg.num_queues, priv->rx_cfg.num_queues);
2333 	dev_info(&priv->pdev->dev, "Max TX queues %d, Max RX queues %d\n",
2334 		 priv->tx_cfg.max_queues, priv->rx_cfg.max_queues);
2335 
2336 	if (!gve_is_gqi(priv)) {
2337 		priv->tx_coalesce_usecs = GVE_TX_IRQ_RATELIMIT_US_DQO;
2338 		priv->rx_coalesce_usecs = GVE_RX_IRQ_RATELIMIT_US_DQO;
2339 	}
2340 
2341 setup_device:
2342 	gve_set_netdev_xdp_features(priv);
2343 	err = gve_setup_device_resources(priv);
2344 	if (!err)
2345 		return 0;
2346 err:
2347 	gve_adminq_free(&priv->pdev->dev, priv);
2348 	return err;
2349 }
2350 
2351 static void gve_teardown_priv_resources(struct gve_priv *priv)
2352 {
2353 	gve_teardown_device_resources(priv);
2354 	gve_adminq_free(&priv->pdev->dev, priv);
2355 }
2356 
2357 static void gve_trigger_reset(struct gve_priv *priv)
2358 {
2359 	/* Reset the device by releasing the AQ */
2360 	gve_adminq_release(priv);
2361 }
2362 
2363 static void gve_reset_and_teardown(struct gve_priv *priv, bool was_up)
2364 {
2365 	gve_trigger_reset(priv);
2366 	/* With the reset having already happened, close cannot fail */
2367 	if (was_up)
2368 		gve_close(priv->dev);
2369 	gve_teardown_priv_resources(priv);
2370 }
2371 
2372 static int gve_reset_recovery(struct gve_priv *priv, bool was_up)
2373 {
2374 	int err;
2375 
2376 	err = gve_init_priv(priv, true);
2377 	if (err)
2378 		goto err;
2379 	if (was_up) {
2380 		err = gve_open(priv->dev);
2381 		if (err)
2382 			goto err;
2383 	}
2384 	return 0;
2385 err:
2386 	dev_err(&priv->pdev->dev, "Reset failed! !!! DISABLING ALL QUEUES !!!\n");
2387 	gve_turndown(priv);
2388 	return err;
2389 }
2390 
2391 int gve_reset(struct gve_priv *priv, bool attempt_teardown)
2392 {
2393 	bool was_up = netif_carrier_ok(priv->dev);
2394 	int err;
2395 
2396 	dev_info(&priv->pdev->dev, "Performing reset\n");
2397 	gve_clear_do_reset(priv);
2398 	gve_set_reset_in_progress(priv);
2399 	/* If we aren't attempting to teardown normally, just go turndown and
2400 	 * reset right away.
2401 	 */
2402 	if (!attempt_teardown) {
2403 		gve_turndown(priv);
2404 		gve_reset_and_teardown(priv, was_up);
2405 	} else {
2406 		/* Otherwise attempt to close normally */
2407 		if (was_up) {
2408 			err = gve_close(priv->dev);
2409 			/* If that fails reset as we did above */
2410 			if (err)
2411 				gve_reset_and_teardown(priv, was_up);
2412 		}
2413 		/* Clean up any remaining resources */
2414 		gve_teardown_priv_resources(priv);
2415 	}
2416 
2417 	/* Set it all back up */
2418 	err = gve_reset_recovery(priv, was_up);
2419 	gve_clear_reset_in_progress(priv);
2420 	priv->reset_cnt++;
2421 	priv->interface_up_cnt = 0;
2422 	priv->interface_down_cnt = 0;
2423 	priv->stats_report_trigger_cnt = 0;
2424 	return err;
2425 }
2426 
2427 static void gve_write_version(u8 __iomem *driver_version_register)
2428 {
2429 	const char *c = gve_version_prefix;
2430 
2431 	while (*c) {
2432 		writeb(*c, driver_version_register);
2433 		c++;
2434 	}
2435 
2436 	c = gve_version_str;
2437 	while (*c) {
2438 		writeb(*c, driver_version_register);
2439 		c++;
2440 	}
2441 	writeb('\n', driver_version_register);
2442 }
2443 
2444 static int gve_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2445 {
2446 	int max_tx_queues, max_rx_queues;
2447 	struct net_device *dev;
2448 	__be32 __iomem *db_bar;
2449 	struct gve_registers __iomem *reg_bar;
2450 	struct gve_priv *priv;
2451 	int err;
2452 
2453 	err = pci_enable_device(pdev);
2454 	if (err)
2455 		return err;
2456 
2457 	err = pci_request_regions(pdev, gve_driver_name);
2458 	if (err)
2459 		goto abort_with_enabled;
2460 
2461 	pci_set_master(pdev);
2462 
2463 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2464 	if (err) {
2465 		dev_err(&pdev->dev, "Failed to set dma mask: err=%d\n", err);
2466 		goto abort_with_pci_region;
2467 	}
2468 
2469 	reg_bar = pci_iomap(pdev, GVE_REGISTER_BAR, 0);
2470 	if (!reg_bar) {
2471 		dev_err(&pdev->dev, "Failed to map pci bar!\n");
2472 		err = -ENOMEM;
2473 		goto abort_with_pci_region;
2474 	}
2475 
2476 	db_bar = pci_iomap(pdev, GVE_DOORBELL_BAR, 0);
2477 	if (!db_bar) {
2478 		dev_err(&pdev->dev, "Failed to map doorbell bar!\n");
2479 		err = -ENOMEM;
2480 		goto abort_with_reg_bar;
2481 	}
2482 
2483 	gve_write_version(&reg_bar->driver_version);
2484 	/* Get max queues to alloc etherdev */
2485 	max_tx_queues = ioread32be(&reg_bar->max_tx_queues);
2486 	max_rx_queues = ioread32be(&reg_bar->max_rx_queues);
2487 	/* Alloc and setup the netdev and priv */
2488 	dev = alloc_etherdev_mqs(sizeof(*priv), max_tx_queues, max_rx_queues);
2489 	if (!dev) {
2490 		dev_err(&pdev->dev, "could not allocate netdev\n");
2491 		err = -ENOMEM;
2492 		goto abort_with_db_bar;
2493 	}
2494 	SET_NETDEV_DEV(dev, &pdev->dev);
2495 	pci_set_drvdata(pdev, dev);
2496 	dev->ethtool_ops = &gve_ethtool_ops;
2497 	dev->netdev_ops = &gve_netdev_ops;
2498 
2499 	/* Set default and supported features.
2500 	 *
2501 	 * Features might be set in other locations as well (such as
2502 	 * `gve_adminq_describe_device`).
2503 	 */
2504 	dev->hw_features = NETIF_F_HIGHDMA;
2505 	dev->hw_features |= NETIF_F_SG;
2506 	dev->hw_features |= NETIF_F_HW_CSUM;
2507 	dev->hw_features |= NETIF_F_TSO;
2508 	dev->hw_features |= NETIF_F_TSO6;
2509 	dev->hw_features |= NETIF_F_TSO_ECN;
2510 	dev->hw_features |= NETIF_F_RXCSUM;
2511 	dev->hw_features |= NETIF_F_RXHASH;
2512 	dev->features = dev->hw_features;
2513 	dev->watchdog_timeo = 5 * HZ;
2514 	dev->min_mtu = ETH_MIN_MTU;
2515 	netif_carrier_off(dev);
2516 
2517 	priv = netdev_priv(dev);
2518 	priv->dev = dev;
2519 	priv->pdev = pdev;
2520 	priv->msg_enable = DEFAULT_MSG_LEVEL;
2521 	priv->reg_bar0 = reg_bar;
2522 	priv->db_bar2 = db_bar;
2523 	priv->service_task_flags = 0x0;
2524 	priv->state_flags = 0x0;
2525 	priv->ethtool_flags = 0x0;
2526 	priv->data_buffer_size_dqo = GVE_DEFAULT_RX_BUFFER_SIZE;
2527 	priv->max_rx_buffer_size = GVE_DEFAULT_RX_BUFFER_SIZE;
2528 
2529 	gve_set_probe_in_progress(priv);
2530 	priv->gve_wq = alloc_ordered_workqueue("gve", 0);
2531 	if (!priv->gve_wq) {
2532 		dev_err(&pdev->dev, "Could not allocate workqueue");
2533 		err = -ENOMEM;
2534 		goto abort_with_netdev;
2535 	}
2536 	INIT_WORK(&priv->service_task, gve_service_task);
2537 	INIT_WORK(&priv->stats_report_task, gve_stats_report_task);
2538 	priv->tx_cfg.max_queues = max_tx_queues;
2539 	priv->rx_cfg.max_queues = max_rx_queues;
2540 
2541 	err = gve_init_priv(priv, false);
2542 	if (err)
2543 		goto abort_with_wq;
2544 
2545 	err = register_netdev(dev);
2546 	if (err)
2547 		goto abort_with_gve_init;
2548 
2549 	dev_info(&pdev->dev, "GVE version %s\n", gve_version_str);
2550 	dev_info(&pdev->dev, "GVE queue format %d\n", (int)priv->queue_format);
2551 	gve_clear_probe_in_progress(priv);
2552 	queue_work(priv->gve_wq, &priv->service_task);
2553 	return 0;
2554 
2555 abort_with_gve_init:
2556 	gve_teardown_priv_resources(priv);
2557 
2558 abort_with_wq:
2559 	destroy_workqueue(priv->gve_wq);
2560 
2561 abort_with_netdev:
2562 	free_netdev(dev);
2563 
2564 abort_with_db_bar:
2565 	pci_iounmap(pdev, db_bar);
2566 
2567 abort_with_reg_bar:
2568 	pci_iounmap(pdev, reg_bar);
2569 
2570 abort_with_pci_region:
2571 	pci_release_regions(pdev);
2572 
2573 abort_with_enabled:
2574 	pci_disable_device(pdev);
2575 	return err;
2576 }
2577 
2578 static void gve_remove(struct pci_dev *pdev)
2579 {
2580 	struct net_device *netdev = pci_get_drvdata(pdev);
2581 	struct gve_priv *priv = netdev_priv(netdev);
2582 	__be32 __iomem *db_bar = priv->db_bar2;
2583 	void __iomem *reg_bar = priv->reg_bar0;
2584 
2585 	unregister_netdev(netdev);
2586 	gve_teardown_priv_resources(priv);
2587 	destroy_workqueue(priv->gve_wq);
2588 	free_netdev(netdev);
2589 	pci_iounmap(pdev, db_bar);
2590 	pci_iounmap(pdev, reg_bar);
2591 	pci_release_regions(pdev);
2592 	pci_disable_device(pdev);
2593 }
2594 
2595 static void gve_shutdown(struct pci_dev *pdev)
2596 {
2597 	struct net_device *netdev = pci_get_drvdata(pdev);
2598 	struct gve_priv *priv = netdev_priv(netdev);
2599 	bool was_up = netif_carrier_ok(priv->dev);
2600 
2601 	rtnl_lock();
2602 	if (was_up && gve_close(priv->dev)) {
2603 		/* If the dev was up, attempt to close, if close fails, reset */
2604 		gve_reset_and_teardown(priv, was_up);
2605 	} else {
2606 		/* If the dev wasn't up or close worked, finish tearing down */
2607 		gve_teardown_priv_resources(priv);
2608 	}
2609 	rtnl_unlock();
2610 }
2611 
2612 #ifdef CONFIG_PM
2613 static int gve_suspend(struct pci_dev *pdev, pm_message_t state)
2614 {
2615 	struct net_device *netdev = pci_get_drvdata(pdev);
2616 	struct gve_priv *priv = netdev_priv(netdev);
2617 	bool was_up = netif_carrier_ok(priv->dev);
2618 
2619 	priv->suspend_cnt++;
2620 	rtnl_lock();
2621 	if (was_up && gve_close(priv->dev)) {
2622 		/* If the dev was up, attempt to close, if close fails, reset */
2623 		gve_reset_and_teardown(priv, was_up);
2624 	} else {
2625 		/* If the dev wasn't up or close worked, finish tearing down */
2626 		gve_teardown_priv_resources(priv);
2627 	}
2628 	priv->up_before_suspend = was_up;
2629 	rtnl_unlock();
2630 	return 0;
2631 }
2632 
2633 static int gve_resume(struct pci_dev *pdev)
2634 {
2635 	struct net_device *netdev = pci_get_drvdata(pdev);
2636 	struct gve_priv *priv = netdev_priv(netdev);
2637 	int err;
2638 
2639 	priv->resume_cnt++;
2640 	rtnl_lock();
2641 	err = gve_reset_recovery(priv, priv->up_before_suspend);
2642 	rtnl_unlock();
2643 	return err;
2644 }
2645 #endif /* CONFIG_PM */
2646 
2647 static const struct pci_device_id gve_id_table[] = {
2648 	{ PCI_DEVICE(PCI_VENDOR_ID_GOOGLE, PCI_DEV_ID_GVNIC) },
2649 	{ }
2650 };
2651 
2652 static struct pci_driver gve_driver = {
2653 	.name		= gve_driver_name,
2654 	.id_table	= gve_id_table,
2655 	.probe		= gve_probe,
2656 	.remove		= gve_remove,
2657 	.shutdown	= gve_shutdown,
2658 #ifdef CONFIG_PM
2659 	.suspend        = gve_suspend,
2660 	.resume         = gve_resume,
2661 #endif
2662 };
2663 
2664 module_pci_driver(gve_driver);
2665 
2666 MODULE_DEVICE_TABLE(pci, gve_id_table);
2667 MODULE_AUTHOR("Google, Inc.");
2668 MODULE_DESCRIPTION("Google Virtual NIC Driver");
2669 MODULE_LICENSE("Dual MIT/GPL");
2670 MODULE_VERSION(GVE_VERSION);
2671