xref: /linux/drivers/net/ethernet/google/gve/gve_main.c (revision 110d3047a3ec033de00322b1a8068b1215efa97a)
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->qpl_cfg = &priv->qpl_cfg;
833 	cfg->ring_size = priv->tx_desc_cnt;
834 	cfg->start_idx = 0;
835 	cfg->num_rings = gve_num_tx_queues(priv);
836 	cfg->tx = priv->tx;
837 }
838 
839 static void gve_tx_stop_rings(struct gve_priv *priv, int start_id, int num_rings)
840 {
841 	int i;
842 
843 	if (!priv->tx)
844 		return;
845 
846 	for (i = start_id; i < start_id + num_rings; i++) {
847 		if (gve_is_gqi(priv))
848 			gve_tx_stop_ring_gqi(priv, i);
849 		else
850 			gve_tx_stop_ring_dqo(priv, i);
851 	}
852 }
853 
854 static void gve_tx_start_rings(struct gve_priv *priv, int start_id,
855 			       int num_rings)
856 {
857 	int i;
858 
859 	for (i = start_id; i < start_id + num_rings; i++) {
860 		if (gve_is_gqi(priv))
861 			gve_tx_start_ring_gqi(priv, i);
862 		else
863 			gve_tx_start_ring_dqo(priv, i);
864 	}
865 }
866 
867 static int gve_alloc_xdp_rings(struct gve_priv *priv)
868 {
869 	struct gve_tx_alloc_rings_cfg cfg = {0};
870 	int err = 0;
871 
872 	if (!priv->num_xdp_queues)
873 		return 0;
874 
875 	gve_tx_get_curr_alloc_cfg(priv, &cfg);
876 	cfg.start_idx = gve_xdp_tx_start_queue_id(priv);
877 	cfg.num_rings = priv->num_xdp_queues;
878 
879 	err = gve_tx_alloc_rings_gqi(priv, &cfg);
880 	if (err)
881 		return err;
882 
883 	gve_tx_start_rings(priv, cfg.start_idx, cfg.num_rings);
884 	init_xdp_sync_stats(priv);
885 
886 	return 0;
887 }
888 
889 static int gve_alloc_rings(struct gve_priv *priv,
890 			   struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
891 			   struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
892 {
893 	int err;
894 
895 	if (gve_is_gqi(priv))
896 		err = gve_tx_alloc_rings_gqi(priv, tx_alloc_cfg);
897 	else
898 		err = gve_tx_alloc_rings_dqo(priv, tx_alloc_cfg);
899 	if (err)
900 		return err;
901 
902 	if (gve_is_gqi(priv))
903 		err = gve_rx_alloc_rings_gqi(priv, rx_alloc_cfg);
904 	else
905 		err = gve_rx_alloc_rings_dqo(priv, rx_alloc_cfg);
906 	if (err)
907 		goto free_tx;
908 
909 	return 0;
910 
911 free_tx:
912 	if (gve_is_gqi(priv))
913 		gve_tx_free_rings_gqi(priv, tx_alloc_cfg);
914 	else
915 		gve_tx_free_rings_dqo(priv, tx_alloc_cfg);
916 	return err;
917 }
918 
919 static int gve_destroy_xdp_rings(struct gve_priv *priv)
920 {
921 	int start_id;
922 	int err;
923 
924 	start_id = gve_xdp_tx_start_queue_id(priv);
925 	err = gve_adminq_destroy_tx_queues(priv,
926 					   start_id,
927 					   priv->num_xdp_queues);
928 	if (err) {
929 		netif_err(priv, drv, priv->dev,
930 			  "failed to destroy XDP queues\n");
931 		/* This failure will trigger a reset - no need to clean up */
932 		return err;
933 	}
934 	netif_dbg(priv, drv, priv->dev, "destroyed XDP queues\n");
935 
936 	return 0;
937 }
938 
939 static int gve_destroy_rings(struct gve_priv *priv)
940 {
941 	int num_tx_queues = gve_num_tx_queues(priv);
942 	int err;
943 
944 	err = gve_adminq_destroy_tx_queues(priv, 0, num_tx_queues);
945 	if (err) {
946 		netif_err(priv, drv, priv->dev,
947 			  "failed to destroy tx queues\n");
948 		/* This failure will trigger a reset - no need to clean up */
949 		return err;
950 	}
951 	netif_dbg(priv, drv, priv->dev, "destroyed tx queues\n");
952 	err = gve_adminq_destroy_rx_queues(priv, priv->rx_cfg.num_queues);
953 	if (err) {
954 		netif_err(priv, drv, priv->dev,
955 			  "failed to destroy rx queues\n");
956 		/* This failure will trigger a reset - no need to clean up */
957 		return err;
958 	}
959 	netif_dbg(priv, drv, priv->dev, "destroyed rx queues\n");
960 	return 0;
961 }
962 
963 static void gve_free_xdp_rings(struct gve_priv *priv)
964 {
965 	struct gve_tx_alloc_rings_cfg cfg = {0};
966 
967 	gve_tx_get_curr_alloc_cfg(priv, &cfg);
968 	cfg.start_idx = gve_xdp_tx_start_queue_id(priv);
969 	cfg.num_rings = priv->num_xdp_queues;
970 
971 	if (priv->tx) {
972 		gve_tx_stop_rings(priv, cfg.start_idx, cfg.num_rings);
973 		gve_tx_free_rings_gqi(priv, &cfg);
974 	}
975 }
976 
977 static void gve_free_rings(struct gve_priv *priv,
978 			   struct gve_tx_alloc_rings_cfg *tx_cfg,
979 			   struct gve_rx_alloc_rings_cfg *rx_cfg)
980 {
981 	if (gve_is_gqi(priv)) {
982 		gve_tx_free_rings_gqi(priv, tx_cfg);
983 		gve_rx_free_rings_gqi(priv, rx_cfg);
984 	} else {
985 		gve_tx_free_rings_dqo(priv, tx_cfg);
986 		gve_rx_free_rings_dqo(priv, rx_cfg);
987 	}
988 }
989 
990 int gve_alloc_page(struct gve_priv *priv, struct device *dev,
991 		   struct page **page, dma_addr_t *dma,
992 		   enum dma_data_direction dir, gfp_t gfp_flags)
993 {
994 	*page = alloc_page(gfp_flags);
995 	if (!*page) {
996 		priv->page_alloc_fail++;
997 		return -ENOMEM;
998 	}
999 	*dma = dma_map_page(dev, *page, 0, PAGE_SIZE, dir);
1000 	if (dma_mapping_error(dev, *dma)) {
1001 		priv->dma_mapping_error++;
1002 		put_page(*page);
1003 		return -ENOMEM;
1004 	}
1005 	return 0;
1006 }
1007 
1008 static int gve_alloc_queue_page_list(struct gve_priv *priv,
1009 				     struct gve_queue_page_list *qpl,
1010 				     u32 id, int pages)
1011 {
1012 	int err;
1013 	int i;
1014 
1015 	qpl->id = id;
1016 	qpl->num_entries = 0;
1017 	qpl->pages = kvcalloc(pages, sizeof(*qpl->pages), GFP_KERNEL);
1018 	/* caller handles clean up */
1019 	if (!qpl->pages)
1020 		return -ENOMEM;
1021 	qpl->page_buses = kvcalloc(pages, sizeof(*qpl->page_buses), GFP_KERNEL);
1022 	/* caller handles clean up */
1023 	if (!qpl->page_buses)
1024 		return -ENOMEM;
1025 
1026 	for (i = 0; i < pages; i++) {
1027 		err = gve_alloc_page(priv, &priv->pdev->dev, &qpl->pages[i],
1028 				     &qpl->page_buses[i],
1029 				     gve_qpl_dma_dir(priv, id), GFP_KERNEL);
1030 		/* caller handles clean up */
1031 		if (err)
1032 			return -ENOMEM;
1033 		qpl->num_entries++;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 void gve_free_page(struct device *dev, struct page *page, dma_addr_t dma,
1040 		   enum dma_data_direction dir)
1041 {
1042 	if (!dma_mapping_error(dev, dma))
1043 		dma_unmap_page(dev, dma, PAGE_SIZE, dir);
1044 	if (page)
1045 		put_page(page);
1046 }
1047 
1048 static void gve_free_queue_page_list(struct gve_priv *priv,
1049 				     struct gve_queue_page_list *qpl,
1050 				     int id)
1051 {
1052 	int i;
1053 
1054 	if (!qpl->pages)
1055 		return;
1056 	if (!qpl->page_buses)
1057 		goto free_pages;
1058 
1059 	for (i = 0; i < qpl->num_entries; i++)
1060 		gve_free_page(&priv->pdev->dev, qpl->pages[i],
1061 			      qpl->page_buses[i], gve_qpl_dma_dir(priv, id));
1062 
1063 	kvfree(qpl->page_buses);
1064 	qpl->page_buses = NULL;
1065 free_pages:
1066 	kvfree(qpl->pages);
1067 	qpl->pages = NULL;
1068 }
1069 
1070 static void gve_free_n_qpls(struct gve_priv *priv,
1071 			    struct gve_queue_page_list *qpls,
1072 			    int start_id,
1073 			    int num_qpls)
1074 {
1075 	int i;
1076 
1077 	for (i = start_id; i < start_id + num_qpls; i++)
1078 		gve_free_queue_page_list(priv, &qpls[i], i);
1079 }
1080 
1081 static int gve_alloc_n_qpls(struct gve_priv *priv,
1082 			    struct gve_queue_page_list *qpls,
1083 			    int page_count,
1084 			    int start_id,
1085 			    int num_qpls)
1086 {
1087 	int err;
1088 	int i;
1089 
1090 	for (i = start_id; i < start_id + num_qpls; i++) {
1091 		err = gve_alloc_queue_page_list(priv, &qpls[i], i, page_count);
1092 		if (err)
1093 			goto free_qpls;
1094 	}
1095 
1096 	return 0;
1097 
1098 free_qpls:
1099 	/* Must include the failing QPL too for gve_alloc_queue_page_list fails
1100 	 * without cleaning up.
1101 	 */
1102 	gve_free_n_qpls(priv, qpls, start_id, i - start_id + 1);
1103 	return err;
1104 }
1105 
1106 static int gve_alloc_qpls(struct gve_priv *priv,
1107 			  struct gve_qpls_alloc_cfg *cfg)
1108 {
1109 	int max_queues = cfg->tx_cfg->max_queues + cfg->rx_cfg->max_queues;
1110 	int rx_start_id, tx_num_qpls, rx_num_qpls;
1111 	struct gve_queue_page_list *qpls;
1112 	int page_count;
1113 	int err;
1114 
1115 	if (cfg->raw_addressing)
1116 		return 0;
1117 
1118 	qpls = kvcalloc(max_queues, sizeof(*qpls), GFP_KERNEL);
1119 	if (!qpls)
1120 		return -ENOMEM;
1121 
1122 	cfg->qpl_cfg->qpl_map_size = BITS_TO_LONGS(max_queues) *
1123 		sizeof(unsigned long) * BITS_PER_BYTE;
1124 	cfg->qpl_cfg->qpl_id_map = kvcalloc(BITS_TO_LONGS(max_queues),
1125 					    sizeof(unsigned long), GFP_KERNEL);
1126 	if (!cfg->qpl_cfg->qpl_id_map) {
1127 		err = -ENOMEM;
1128 		goto free_qpl_array;
1129 	}
1130 
1131 	/* Allocate TX QPLs */
1132 	page_count = priv->tx_pages_per_qpl;
1133 	tx_num_qpls = gve_num_tx_qpls(cfg->tx_cfg, cfg->num_xdp_queues,
1134 				      gve_is_qpl(priv));
1135 	err = gve_alloc_n_qpls(priv, qpls, page_count, 0, tx_num_qpls);
1136 	if (err)
1137 		goto free_qpl_map;
1138 
1139 	/* Allocate RX QPLs */
1140 	rx_start_id = gve_rx_start_qpl_id(cfg->tx_cfg);
1141 	/* For GQI_QPL number of pages allocated have 1:1 relationship with
1142 	 * number of descriptors. For DQO, number of pages required are
1143 	 * more than descriptors (because of out of order completions).
1144 	 */
1145 	page_count = cfg->is_gqi ? priv->rx_data_slot_cnt : priv->rx_pages_per_qpl;
1146 	rx_num_qpls = gve_num_rx_qpls(cfg->rx_cfg, gve_is_qpl(priv));
1147 	err = gve_alloc_n_qpls(priv, qpls, page_count, rx_start_id, rx_num_qpls);
1148 	if (err)
1149 		goto free_tx_qpls;
1150 
1151 	cfg->qpls = qpls;
1152 	return 0;
1153 
1154 free_tx_qpls:
1155 	gve_free_n_qpls(priv, qpls, 0, tx_num_qpls);
1156 free_qpl_map:
1157 	kvfree(cfg->qpl_cfg->qpl_id_map);
1158 	cfg->qpl_cfg->qpl_id_map = NULL;
1159 free_qpl_array:
1160 	kvfree(qpls);
1161 	return err;
1162 }
1163 
1164 static void gve_free_qpls(struct gve_priv *priv,
1165 			  struct gve_qpls_alloc_cfg *cfg)
1166 {
1167 	int max_queues = cfg->tx_cfg->max_queues + cfg->rx_cfg->max_queues;
1168 	struct gve_queue_page_list *qpls = cfg->qpls;
1169 	int i;
1170 
1171 	if (!qpls)
1172 		return;
1173 
1174 	kvfree(cfg->qpl_cfg->qpl_id_map);
1175 	cfg->qpl_cfg->qpl_id_map = NULL;
1176 
1177 	for (i = 0; i < max_queues; i++)
1178 		gve_free_queue_page_list(priv, &qpls[i], i);
1179 
1180 	kvfree(qpls);
1181 	cfg->qpls = NULL;
1182 }
1183 
1184 /* Use this to schedule a reset when the device is capable of continuing
1185  * to handle other requests in its current state. If it is not, do a reset
1186  * in thread instead.
1187  */
1188 void gve_schedule_reset(struct gve_priv *priv)
1189 {
1190 	gve_set_do_reset(priv);
1191 	queue_work(priv->gve_wq, &priv->service_task);
1192 }
1193 
1194 static void gve_reset_and_teardown(struct gve_priv *priv, bool was_up);
1195 static int gve_reset_recovery(struct gve_priv *priv, bool was_up);
1196 static void gve_turndown(struct gve_priv *priv);
1197 static void gve_turnup(struct gve_priv *priv);
1198 
1199 static int gve_reg_xdp_info(struct gve_priv *priv, struct net_device *dev)
1200 {
1201 	struct napi_struct *napi;
1202 	struct gve_rx_ring *rx;
1203 	int err = 0;
1204 	int i, j;
1205 	u32 tx_qid;
1206 
1207 	if (!priv->num_xdp_queues)
1208 		return 0;
1209 
1210 	for (i = 0; i < priv->rx_cfg.num_queues; i++) {
1211 		rx = &priv->rx[i];
1212 		napi = &priv->ntfy_blocks[rx->ntfy_id].napi;
1213 
1214 		err = xdp_rxq_info_reg(&rx->xdp_rxq, dev, i,
1215 				       napi->napi_id);
1216 		if (err)
1217 			goto err;
1218 		err = xdp_rxq_info_reg_mem_model(&rx->xdp_rxq,
1219 						 MEM_TYPE_PAGE_SHARED, NULL);
1220 		if (err)
1221 			goto err;
1222 		rx->xsk_pool = xsk_get_pool_from_qid(dev, i);
1223 		if (rx->xsk_pool) {
1224 			err = xdp_rxq_info_reg(&rx->xsk_rxq, dev, i,
1225 					       napi->napi_id);
1226 			if (err)
1227 				goto err;
1228 			err = xdp_rxq_info_reg_mem_model(&rx->xsk_rxq,
1229 							 MEM_TYPE_XSK_BUFF_POOL, NULL);
1230 			if (err)
1231 				goto err;
1232 			xsk_pool_set_rxq_info(rx->xsk_pool,
1233 					      &rx->xsk_rxq);
1234 		}
1235 	}
1236 
1237 	for (i = 0; i < priv->num_xdp_queues; i++) {
1238 		tx_qid = gve_xdp_tx_queue_id(priv, i);
1239 		priv->tx[tx_qid].xsk_pool = xsk_get_pool_from_qid(dev, i);
1240 	}
1241 	return 0;
1242 
1243 err:
1244 	for (j = i; j >= 0; j--) {
1245 		rx = &priv->rx[j];
1246 		if (xdp_rxq_info_is_reg(&rx->xdp_rxq))
1247 			xdp_rxq_info_unreg(&rx->xdp_rxq);
1248 		if (xdp_rxq_info_is_reg(&rx->xsk_rxq))
1249 			xdp_rxq_info_unreg(&rx->xsk_rxq);
1250 	}
1251 	return err;
1252 }
1253 
1254 static void gve_unreg_xdp_info(struct gve_priv *priv)
1255 {
1256 	int i, tx_qid;
1257 
1258 	if (!priv->num_xdp_queues)
1259 		return;
1260 
1261 	for (i = 0; i < priv->rx_cfg.num_queues; i++) {
1262 		struct gve_rx_ring *rx = &priv->rx[i];
1263 
1264 		xdp_rxq_info_unreg(&rx->xdp_rxq);
1265 		if (rx->xsk_pool) {
1266 			xdp_rxq_info_unreg(&rx->xsk_rxq);
1267 			rx->xsk_pool = NULL;
1268 		}
1269 	}
1270 
1271 	for (i = 0; i < priv->num_xdp_queues; i++) {
1272 		tx_qid = gve_xdp_tx_queue_id(priv, i);
1273 		priv->tx[tx_qid].xsk_pool = NULL;
1274 	}
1275 }
1276 
1277 static void gve_drain_page_cache(struct gve_priv *priv)
1278 {
1279 	struct page_frag_cache *nc;
1280 	int i;
1281 
1282 	for (i = 0; i < priv->rx_cfg.num_queues; i++) {
1283 		nc = &priv->rx[i].page_cache;
1284 		if (nc->va) {
1285 			__page_frag_cache_drain(virt_to_page(nc->va),
1286 						nc->pagecnt_bias);
1287 			nc->va = NULL;
1288 		}
1289 	}
1290 }
1291 
1292 static void gve_qpls_get_curr_alloc_cfg(struct gve_priv *priv,
1293 					struct gve_qpls_alloc_cfg *cfg)
1294 {
1295 	  cfg->raw_addressing = !gve_is_qpl(priv);
1296 	  cfg->is_gqi = gve_is_gqi(priv);
1297 	  cfg->num_xdp_queues = priv->num_xdp_queues;
1298 	  cfg->qpl_cfg = &priv->qpl_cfg;
1299 	  cfg->tx_cfg = &priv->tx_cfg;
1300 	  cfg->rx_cfg = &priv->rx_cfg;
1301 	  cfg->qpls = priv->qpls;
1302 }
1303 
1304 static void gve_rx_get_curr_alloc_cfg(struct gve_priv *priv,
1305 				      struct gve_rx_alloc_rings_cfg *cfg)
1306 {
1307 	cfg->qcfg = &priv->rx_cfg;
1308 	cfg->qcfg_tx = &priv->tx_cfg;
1309 	cfg->raw_addressing = !gve_is_qpl(priv);
1310 	cfg->qpls = priv->qpls;
1311 	cfg->qpl_cfg = &priv->qpl_cfg;
1312 	cfg->ring_size = priv->rx_desc_cnt;
1313 	cfg->rx = priv->rx;
1314 }
1315 
1316 static void gve_get_curr_alloc_cfgs(struct gve_priv *priv,
1317 				    struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1318 				    struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1319 				    struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1320 {
1321 	gve_qpls_get_curr_alloc_cfg(priv, qpls_alloc_cfg);
1322 	gve_tx_get_curr_alloc_cfg(priv, tx_alloc_cfg);
1323 	gve_rx_get_curr_alloc_cfg(priv, rx_alloc_cfg);
1324 }
1325 
1326 static void gve_rx_start_rings(struct gve_priv *priv, int num_rings)
1327 {
1328 	int i;
1329 
1330 	for (i = 0; i < num_rings; i++) {
1331 		if (gve_is_gqi(priv))
1332 			gve_rx_start_ring_gqi(priv, i);
1333 		else
1334 			gve_rx_start_ring_dqo(priv, i);
1335 	}
1336 }
1337 
1338 static void gve_rx_stop_rings(struct gve_priv *priv, int num_rings)
1339 {
1340 	int i;
1341 
1342 	if (!priv->rx)
1343 		return;
1344 
1345 	for (i = 0; i < num_rings; i++) {
1346 		if (gve_is_gqi(priv))
1347 			gve_rx_stop_ring_gqi(priv, i);
1348 		else
1349 			gve_rx_stop_ring_dqo(priv, i);
1350 	}
1351 }
1352 
1353 static void gve_queues_mem_free(struct gve_priv *priv,
1354 				struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1355 				struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1356 				struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1357 {
1358 	gve_free_rings(priv, tx_alloc_cfg, rx_alloc_cfg);
1359 	gve_free_qpls(priv, qpls_alloc_cfg);
1360 }
1361 
1362 static int gve_queues_mem_alloc(struct gve_priv *priv,
1363 				struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1364 				struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1365 				struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1366 {
1367 	int err;
1368 
1369 	err = gve_alloc_qpls(priv, qpls_alloc_cfg);
1370 	if (err) {
1371 		netif_err(priv, drv, priv->dev, "Failed to alloc QPLs\n");
1372 		return err;
1373 	}
1374 	tx_alloc_cfg->qpls = qpls_alloc_cfg->qpls;
1375 	rx_alloc_cfg->qpls = qpls_alloc_cfg->qpls;
1376 	err = gve_alloc_rings(priv, tx_alloc_cfg, rx_alloc_cfg);
1377 	if (err) {
1378 		netif_err(priv, drv, priv->dev, "Failed to alloc rings\n");
1379 		goto free_qpls;
1380 	}
1381 
1382 	return 0;
1383 
1384 free_qpls:
1385 	gve_free_qpls(priv, qpls_alloc_cfg);
1386 	return err;
1387 }
1388 
1389 static void gve_queues_mem_remove(struct gve_priv *priv)
1390 {
1391 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1392 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1393 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1394 
1395 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1396 				&tx_alloc_cfg, &rx_alloc_cfg);
1397 	gve_queues_mem_free(priv, &qpls_alloc_cfg,
1398 			    &tx_alloc_cfg, &rx_alloc_cfg);
1399 	priv->qpls = NULL;
1400 	priv->tx = NULL;
1401 	priv->rx = NULL;
1402 }
1403 
1404 /* The passed-in queue memory is stored into priv and the queues are made live.
1405  * No memory is allocated. Passed-in memory is freed on errors.
1406  */
1407 static int gve_queues_start(struct gve_priv *priv,
1408 			    struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1409 			    struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1410 			    struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1411 {
1412 	struct net_device *dev = priv->dev;
1413 	int err;
1414 
1415 	/* Record new resources into priv */
1416 	priv->qpls = qpls_alloc_cfg->qpls;
1417 	priv->tx = tx_alloc_cfg->tx;
1418 	priv->rx = rx_alloc_cfg->rx;
1419 
1420 	/* Record new configs into priv */
1421 	priv->qpl_cfg = *qpls_alloc_cfg->qpl_cfg;
1422 	priv->tx_cfg = *tx_alloc_cfg->qcfg;
1423 	priv->rx_cfg = *rx_alloc_cfg->qcfg;
1424 	priv->tx_desc_cnt = tx_alloc_cfg->ring_size;
1425 	priv->rx_desc_cnt = rx_alloc_cfg->ring_size;
1426 
1427 	if (priv->xdp_prog)
1428 		priv->num_xdp_queues = priv->rx_cfg.num_queues;
1429 	else
1430 		priv->num_xdp_queues = 0;
1431 
1432 	gve_tx_start_rings(priv, 0, tx_alloc_cfg->num_rings);
1433 	gve_rx_start_rings(priv, rx_alloc_cfg->qcfg->num_queues);
1434 	gve_init_sync_stats(priv);
1435 
1436 	err = netif_set_real_num_tx_queues(dev, priv->tx_cfg.num_queues);
1437 	if (err)
1438 		goto stop_and_free_rings;
1439 	err = netif_set_real_num_rx_queues(dev, priv->rx_cfg.num_queues);
1440 	if (err)
1441 		goto stop_and_free_rings;
1442 
1443 	err = gve_reg_xdp_info(priv, dev);
1444 	if (err)
1445 		goto stop_and_free_rings;
1446 
1447 	err = gve_register_qpls(priv);
1448 	if (err)
1449 		goto reset;
1450 
1451 	if (!gve_is_gqi(priv)) {
1452 		/* Hard code this for now. This may be tuned in the future for
1453 		 * performance.
1454 		 */
1455 		priv->data_buffer_size_dqo = GVE_DEFAULT_RX_BUFFER_SIZE;
1456 	}
1457 	err = gve_create_rings(priv);
1458 	if (err)
1459 		goto reset;
1460 
1461 	gve_set_device_rings_ok(priv);
1462 
1463 	if (gve_get_report_stats(priv))
1464 		mod_timer(&priv->stats_report_timer,
1465 			  round_jiffies(jiffies +
1466 				msecs_to_jiffies(priv->stats_report_timer_period)));
1467 
1468 	gve_turnup(priv);
1469 	queue_work(priv->gve_wq, &priv->service_task);
1470 	priv->interface_up_cnt++;
1471 	return 0;
1472 
1473 reset:
1474 	if (gve_get_reset_in_progress(priv))
1475 		goto stop_and_free_rings;
1476 	gve_reset_and_teardown(priv, true);
1477 	/* if this fails there is nothing we can do so just ignore the return */
1478 	gve_reset_recovery(priv, false);
1479 	/* return the original error */
1480 	return err;
1481 stop_and_free_rings:
1482 	gve_tx_stop_rings(priv, 0, gve_num_tx_queues(priv));
1483 	gve_rx_stop_rings(priv, priv->rx_cfg.num_queues);
1484 	gve_queues_mem_remove(priv);
1485 	return err;
1486 }
1487 
1488 static int gve_open(struct net_device *dev)
1489 {
1490 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1491 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1492 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1493 	struct gve_priv *priv = netdev_priv(dev);
1494 	int err;
1495 
1496 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1497 				&tx_alloc_cfg, &rx_alloc_cfg);
1498 
1499 	err = gve_queues_mem_alloc(priv, &qpls_alloc_cfg,
1500 				   &tx_alloc_cfg, &rx_alloc_cfg);
1501 	if (err)
1502 		return err;
1503 
1504 	/* No need to free on error: ownership of resources is lost after
1505 	 * calling gve_queues_start.
1506 	 */
1507 	err = gve_queues_start(priv, &qpls_alloc_cfg,
1508 			       &tx_alloc_cfg, &rx_alloc_cfg);
1509 	if (err)
1510 		return err;
1511 
1512 	return 0;
1513 }
1514 
1515 static int gve_queues_stop(struct gve_priv *priv)
1516 {
1517 	int err;
1518 
1519 	netif_carrier_off(priv->dev);
1520 	if (gve_get_device_rings_ok(priv)) {
1521 		gve_turndown(priv);
1522 		gve_drain_page_cache(priv);
1523 		err = gve_destroy_rings(priv);
1524 		if (err)
1525 			goto err;
1526 		err = gve_unregister_qpls(priv);
1527 		if (err)
1528 			goto err;
1529 		gve_clear_device_rings_ok(priv);
1530 	}
1531 	del_timer_sync(&priv->stats_report_timer);
1532 
1533 	gve_unreg_xdp_info(priv);
1534 
1535 	gve_tx_stop_rings(priv, 0, gve_num_tx_queues(priv));
1536 	gve_rx_stop_rings(priv, priv->rx_cfg.num_queues);
1537 
1538 	priv->interface_down_cnt++;
1539 	return 0;
1540 
1541 err:
1542 	/* This must have been called from a reset due to the rtnl lock
1543 	 * so just return at this point.
1544 	 */
1545 	if (gve_get_reset_in_progress(priv))
1546 		return err;
1547 	/* Otherwise reset before returning */
1548 	gve_reset_and_teardown(priv, true);
1549 	return gve_reset_recovery(priv, false);
1550 }
1551 
1552 static int gve_close(struct net_device *dev)
1553 {
1554 	struct gve_priv *priv = netdev_priv(dev);
1555 	int err;
1556 
1557 	err = gve_queues_stop(priv);
1558 	if (err)
1559 		return err;
1560 
1561 	gve_queues_mem_remove(priv);
1562 	return 0;
1563 }
1564 
1565 static int gve_remove_xdp_queues(struct gve_priv *priv)
1566 {
1567 	int qpl_start_id;
1568 	int err;
1569 
1570 	qpl_start_id = gve_xdp_tx_start_queue_id(priv);
1571 
1572 	err = gve_destroy_xdp_rings(priv);
1573 	if (err)
1574 		return err;
1575 
1576 	err = gve_unregister_xdp_qpls(priv);
1577 	if (err)
1578 		return err;
1579 
1580 	gve_unreg_xdp_info(priv);
1581 	gve_free_xdp_rings(priv);
1582 
1583 	gve_free_n_qpls(priv, priv->qpls, qpl_start_id, gve_num_xdp_qpls(priv));
1584 	priv->num_xdp_queues = 0;
1585 	return 0;
1586 }
1587 
1588 static int gve_add_xdp_queues(struct gve_priv *priv)
1589 {
1590 	int start_id;
1591 	int err;
1592 
1593 	priv->num_xdp_queues = priv->rx_cfg.num_queues;
1594 
1595 	start_id = gve_xdp_tx_start_queue_id(priv);
1596 	err = gve_alloc_n_qpls(priv, priv->qpls, priv->tx_pages_per_qpl,
1597 			       start_id, gve_num_xdp_qpls(priv));
1598 	if (err)
1599 		goto err;
1600 
1601 	err = gve_alloc_xdp_rings(priv);
1602 	if (err)
1603 		goto free_xdp_qpls;
1604 
1605 	err = gve_reg_xdp_info(priv, priv->dev);
1606 	if (err)
1607 		goto free_xdp_rings;
1608 
1609 	err = gve_register_xdp_qpls(priv);
1610 	if (err)
1611 		goto free_xdp_rings;
1612 
1613 	err = gve_create_xdp_rings(priv);
1614 	if (err)
1615 		goto free_xdp_rings;
1616 
1617 	return 0;
1618 
1619 free_xdp_rings:
1620 	gve_free_xdp_rings(priv);
1621 free_xdp_qpls:
1622 	gve_free_n_qpls(priv, priv->qpls, start_id, gve_num_xdp_qpls(priv));
1623 err:
1624 	priv->num_xdp_queues = 0;
1625 	return err;
1626 }
1627 
1628 static void gve_handle_link_status(struct gve_priv *priv, bool link_status)
1629 {
1630 	if (!gve_get_napi_enabled(priv))
1631 		return;
1632 
1633 	if (link_status == netif_carrier_ok(priv->dev))
1634 		return;
1635 
1636 	if (link_status) {
1637 		netdev_info(priv->dev, "Device link is up.\n");
1638 		netif_carrier_on(priv->dev);
1639 	} else {
1640 		netdev_info(priv->dev, "Device link is down.\n");
1641 		netif_carrier_off(priv->dev);
1642 	}
1643 }
1644 
1645 static int gve_set_xdp(struct gve_priv *priv, struct bpf_prog *prog,
1646 		       struct netlink_ext_ack *extack)
1647 {
1648 	struct bpf_prog *old_prog;
1649 	int err = 0;
1650 	u32 status;
1651 
1652 	old_prog = READ_ONCE(priv->xdp_prog);
1653 	if (!netif_carrier_ok(priv->dev)) {
1654 		WRITE_ONCE(priv->xdp_prog, prog);
1655 		if (old_prog)
1656 			bpf_prog_put(old_prog);
1657 		return 0;
1658 	}
1659 
1660 	gve_turndown(priv);
1661 	if (!old_prog && prog) {
1662 		// Allocate XDP TX queues if an XDP program is
1663 		// being installed
1664 		err = gve_add_xdp_queues(priv);
1665 		if (err)
1666 			goto out;
1667 	} else if (old_prog && !prog) {
1668 		// Remove XDP TX queues if an XDP program is
1669 		// being uninstalled
1670 		err = gve_remove_xdp_queues(priv);
1671 		if (err)
1672 			goto out;
1673 	}
1674 	WRITE_ONCE(priv->xdp_prog, prog);
1675 	if (old_prog)
1676 		bpf_prog_put(old_prog);
1677 
1678 out:
1679 	gve_turnup(priv);
1680 	status = ioread32be(&priv->reg_bar0->device_status);
1681 	gve_handle_link_status(priv, GVE_DEVICE_STATUS_LINK_STATUS_MASK & status);
1682 	return err;
1683 }
1684 
1685 static int gve_xsk_pool_enable(struct net_device *dev,
1686 			       struct xsk_buff_pool *pool,
1687 			       u16 qid)
1688 {
1689 	struct gve_priv *priv = netdev_priv(dev);
1690 	struct napi_struct *napi;
1691 	struct gve_rx_ring *rx;
1692 	int tx_qid;
1693 	int err;
1694 
1695 	if (qid >= priv->rx_cfg.num_queues) {
1696 		dev_err(&priv->pdev->dev, "xsk pool invalid qid %d", qid);
1697 		return -EINVAL;
1698 	}
1699 	if (xsk_pool_get_rx_frame_size(pool) <
1700 	     priv->dev->max_mtu + sizeof(struct ethhdr)) {
1701 		dev_err(&priv->pdev->dev, "xsk pool frame_len too small");
1702 		return -EINVAL;
1703 	}
1704 
1705 	err = xsk_pool_dma_map(pool, &priv->pdev->dev,
1706 			       DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1707 	if (err)
1708 		return err;
1709 
1710 	/* If XDP prog is not installed, return */
1711 	if (!priv->xdp_prog)
1712 		return 0;
1713 
1714 	rx = &priv->rx[qid];
1715 	napi = &priv->ntfy_blocks[rx->ntfy_id].napi;
1716 	err = xdp_rxq_info_reg(&rx->xsk_rxq, dev, qid, napi->napi_id);
1717 	if (err)
1718 		goto err;
1719 
1720 	err = xdp_rxq_info_reg_mem_model(&rx->xsk_rxq,
1721 					 MEM_TYPE_XSK_BUFF_POOL, NULL);
1722 	if (err)
1723 		goto err;
1724 
1725 	xsk_pool_set_rxq_info(pool, &rx->xsk_rxq);
1726 	rx->xsk_pool = pool;
1727 
1728 	tx_qid = gve_xdp_tx_queue_id(priv, qid);
1729 	priv->tx[tx_qid].xsk_pool = pool;
1730 
1731 	return 0;
1732 err:
1733 	if (xdp_rxq_info_is_reg(&rx->xsk_rxq))
1734 		xdp_rxq_info_unreg(&rx->xsk_rxq);
1735 
1736 	xsk_pool_dma_unmap(pool,
1737 			   DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1738 	return err;
1739 }
1740 
1741 static int gve_xsk_pool_disable(struct net_device *dev,
1742 				u16 qid)
1743 {
1744 	struct gve_priv *priv = netdev_priv(dev);
1745 	struct napi_struct *napi_rx;
1746 	struct napi_struct *napi_tx;
1747 	struct xsk_buff_pool *pool;
1748 	int tx_qid;
1749 
1750 	pool = xsk_get_pool_from_qid(dev, qid);
1751 	if (!pool)
1752 		return -EINVAL;
1753 	if (qid >= priv->rx_cfg.num_queues)
1754 		return -EINVAL;
1755 
1756 	/* If XDP prog is not installed, unmap DMA and return */
1757 	if (!priv->xdp_prog)
1758 		goto done;
1759 
1760 	tx_qid = gve_xdp_tx_queue_id(priv, qid);
1761 	if (!netif_running(dev)) {
1762 		priv->rx[qid].xsk_pool = NULL;
1763 		xdp_rxq_info_unreg(&priv->rx[qid].xsk_rxq);
1764 		priv->tx[tx_qid].xsk_pool = NULL;
1765 		goto done;
1766 	}
1767 
1768 	napi_rx = &priv->ntfy_blocks[priv->rx[qid].ntfy_id].napi;
1769 	napi_disable(napi_rx); /* make sure current rx poll is done */
1770 
1771 	napi_tx = &priv->ntfy_blocks[priv->tx[tx_qid].ntfy_id].napi;
1772 	napi_disable(napi_tx); /* make sure current tx poll is done */
1773 
1774 	priv->rx[qid].xsk_pool = NULL;
1775 	xdp_rxq_info_unreg(&priv->rx[qid].xsk_rxq);
1776 	priv->tx[tx_qid].xsk_pool = NULL;
1777 	smp_mb(); /* Make sure it is visible to the workers on datapath */
1778 
1779 	napi_enable(napi_rx);
1780 	if (gve_rx_work_pending(&priv->rx[qid]))
1781 		napi_schedule(napi_rx);
1782 
1783 	napi_enable(napi_tx);
1784 	if (gve_tx_clean_pending(priv, &priv->tx[tx_qid]))
1785 		napi_schedule(napi_tx);
1786 
1787 done:
1788 	xsk_pool_dma_unmap(pool,
1789 			   DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
1790 	return 0;
1791 }
1792 
1793 static int gve_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
1794 {
1795 	struct gve_priv *priv = netdev_priv(dev);
1796 	int tx_queue_id = gve_xdp_tx_queue_id(priv, queue_id);
1797 
1798 	if (queue_id >= priv->rx_cfg.num_queues || !priv->xdp_prog)
1799 		return -EINVAL;
1800 
1801 	if (flags & XDP_WAKEUP_TX) {
1802 		struct gve_tx_ring *tx = &priv->tx[tx_queue_id];
1803 		struct napi_struct *napi =
1804 			&priv->ntfy_blocks[tx->ntfy_id].napi;
1805 
1806 		if (!napi_if_scheduled_mark_missed(napi)) {
1807 			/* Call local_bh_enable to trigger SoftIRQ processing */
1808 			local_bh_disable();
1809 			napi_schedule(napi);
1810 			local_bh_enable();
1811 		}
1812 
1813 		tx->xdp_xsk_wakeup++;
1814 	}
1815 
1816 	return 0;
1817 }
1818 
1819 static int verify_xdp_configuration(struct net_device *dev)
1820 {
1821 	struct gve_priv *priv = netdev_priv(dev);
1822 
1823 	if (dev->features & NETIF_F_LRO) {
1824 		netdev_warn(dev, "XDP is not supported when LRO is on.\n");
1825 		return -EOPNOTSUPP;
1826 	}
1827 
1828 	if (priv->queue_format != GVE_GQI_QPL_FORMAT) {
1829 		netdev_warn(dev, "XDP is not supported in mode %d.\n",
1830 			    priv->queue_format);
1831 		return -EOPNOTSUPP;
1832 	}
1833 
1834 	if (dev->mtu > GVE_DEFAULT_RX_BUFFER_SIZE - sizeof(struct ethhdr) - GVE_RX_PAD) {
1835 		netdev_warn(dev, "XDP is not supported for mtu %d.\n",
1836 			    dev->mtu);
1837 		return -EOPNOTSUPP;
1838 	}
1839 
1840 	if (priv->rx_cfg.num_queues != priv->tx_cfg.num_queues ||
1841 	    (2 * priv->tx_cfg.num_queues > priv->tx_cfg.max_queues)) {
1842 		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",
1843 			    priv->rx_cfg.num_queues,
1844 			    priv->tx_cfg.num_queues,
1845 			    priv->tx_cfg.max_queues);
1846 		return -EINVAL;
1847 	}
1848 	return 0;
1849 }
1850 
1851 static int gve_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1852 {
1853 	struct gve_priv *priv = netdev_priv(dev);
1854 	int err;
1855 
1856 	err = verify_xdp_configuration(dev);
1857 	if (err)
1858 		return err;
1859 	switch (xdp->command) {
1860 	case XDP_SETUP_PROG:
1861 		return gve_set_xdp(priv, xdp->prog, xdp->extack);
1862 	case XDP_SETUP_XSK_POOL:
1863 		if (xdp->xsk.pool)
1864 			return gve_xsk_pool_enable(dev, xdp->xsk.pool, xdp->xsk.queue_id);
1865 		else
1866 			return gve_xsk_pool_disable(dev, xdp->xsk.queue_id);
1867 	default:
1868 		return -EINVAL;
1869 	}
1870 }
1871 
1872 static int gve_adjust_config(struct gve_priv *priv,
1873 			     struct gve_qpls_alloc_cfg *qpls_alloc_cfg,
1874 			     struct gve_tx_alloc_rings_cfg *tx_alloc_cfg,
1875 			     struct gve_rx_alloc_rings_cfg *rx_alloc_cfg)
1876 {
1877 	int err;
1878 
1879 	/* Allocate resources for the new confiugration */
1880 	err = gve_queues_mem_alloc(priv, qpls_alloc_cfg,
1881 				   tx_alloc_cfg, rx_alloc_cfg);
1882 	if (err) {
1883 		netif_err(priv, drv, priv->dev,
1884 			  "Adjust config failed to alloc new queues");
1885 		return err;
1886 	}
1887 
1888 	/* Teardown the device and free existing resources */
1889 	err = gve_close(priv->dev);
1890 	if (err) {
1891 		netif_err(priv, drv, priv->dev,
1892 			  "Adjust config failed to close old queues");
1893 		gve_queues_mem_free(priv, qpls_alloc_cfg,
1894 				    tx_alloc_cfg, rx_alloc_cfg);
1895 		return err;
1896 	}
1897 
1898 	/* Bring the device back up again with the new resources. */
1899 	err = gve_queues_start(priv, qpls_alloc_cfg,
1900 			       tx_alloc_cfg, rx_alloc_cfg);
1901 	if (err) {
1902 		netif_err(priv, drv, priv->dev,
1903 			  "Adjust config failed to start new queues, !!! DISABLING ALL QUEUES !!!\n");
1904 		/* No need to free on error: ownership of resources is lost after
1905 		 * calling gve_queues_start.
1906 		 */
1907 		gve_turndown(priv);
1908 		return err;
1909 	}
1910 
1911 	return 0;
1912 }
1913 
1914 int gve_adjust_queues(struct gve_priv *priv,
1915 		      struct gve_queue_config new_rx_config,
1916 		      struct gve_queue_config new_tx_config)
1917 {
1918 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
1919 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
1920 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
1921 	struct gve_qpl_config new_qpl_cfg;
1922 	int err;
1923 
1924 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
1925 				&tx_alloc_cfg, &rx_alloc_cfg);
1926 
1927 	/* qpl_cfg is not read-only, it contains a map that gets updated as
1928 	 * rings are allocated, which is why we cannot use the yet unreleased
1929 	 * one in priv.
1930 	 */
1931 	qpls_alloc_cfg.qpl_cfg = &new_qpl_cfg;
1932 	tx_alloc_cfg.qpl_cfg = &new_qpl_cfg;
1933 	rx_alloc_cfg.qpl_cfg = &new_qpl_cfg;
1934 
1935 	/* Relay the new config from ethtool */
1936 	qpls_alloc_cfg.tx_cfg = &new_tx_config;
1937 	tx_alloc_cfg.qcfg = &new_tx_config;
1938 	rx_alloc_cfg.qcfg_tx = &new_tx_config;
1939 	qpls_alloc_cfg.rx_cfg = &new_rx_config;
1940 	rx_alloc_cfg.qcfg = &new_rx_config;
1941 	tx_alloc_cfg.num_rings = new_tx_config.num_queues;
1942 
1943 	if (netif_carrier_ok(priv->dev)) {
1944 		err = gve_adjust_config(priv, &qpls_alloc_cfg,
1945 					&tx_alloc_cfg, &rx_alloc_cfg);
1946 		return err;
1947 	}
1948 	/* Set the config for the next up. */
1949 	priv->tx_cfg = new_tx_config;
1950 	priv->rx_cfg = new_rx_config;
1951 
1952 	return 0;
1953 }
1954 
1955 static void gve_turndown(struct gve_priv *priv)
1956 {
1957 	int idx;
1958 
1959 	if (netif_carrier_ok(priv->dev))
1960 		netif_carrier_off(priv->dev);
1961 
1962 	if (!gve_get_napi_enabled(priv))
1963 		return;
1964 
1965 	/* Disable napi to prevent more work from coming in */
1966 	for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
1967 		int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
1968 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1969 
1970 		napi_disable(&block->napi);
1971 	}
1972 	for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
1973 		int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx);
1974 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1975 
1976 		napi_disable(&block->napi);
1977 	}
1978 
1979 	/* Stop tx queues */
1980 	netif_tx_disable(priv->dev);
1981 
1982 	gve_clear_napi_enabled(priv);
1983 	gve_clear_report_stats(priv);
1984 }
1985 
1986 static void gve_turnup(struct gve_priv *priv)
1987 {
1988 	int idx;
1989 
1990 	/* Start the tx queues */
1991 	netif_tx_start_all_queues(priv->dev);
1992 
1993 	/* Enable napi and unmask interrupts for all queues */
1994 	for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
1995 		int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
1996 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
1997 
1998 		napi_enable(&block->napi);
1999 		if (gve_is_gqi(priv)) {
2000 			iowrite32be(0, gve_irq_doorbell(priv, block));
2001 		} else {
2002 			gve_set_itr_coalesce_usecs_dqo(priv, block,
2003 						       priv->tx_coalesce_usecs);
2004 		}
2005 	}
2006 	for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
2007 		int ntfy_idx = gve_rx_idx_to_ntfy(priv, idx);
2008 		struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
2009 
2010 		napi_enable(&block->napi);
2011 		if (gve_is_gqi(priv)) {
2012 			iowrite32be(0, gve_irq_doorbell(priv, block));
2013 		} else {
2014 			gve_set_itr_coalesce_usecs_dqo(priv, block,
2015 						       priv->rx_coalesce_usecs);
2016 		}
2017 	}
2018 
2019 	gve_set_napi_enabled(priv);
2020 }
2021 
2022 static void gve_tx_timeout(struct net_device *dev, unsigned int txqueue)
2023 {
2024 	struct gve_notify_block *block;
2025 	struct gve_tx_ring *tx = NULL;
2026 	struct gve_priv *priv;
2027 	u32 last_nic_done;
2028 	u32 current_time;
2029 	u32 ntfy_idx;
2030 
2031 	netdev_info(dev, "Timeout on tx queue, %d", txqueue);
2032 	priv = netdev_priv(dev);
2033 	if (txqueue > priv->tx_cfg.num_queues)
2034 		goto reset;
2035 
2036 	ntfy_idx = gve_tx_idx_to_ntfy(priv, txqueue);
2037 	if (ntfy_idx >= priv->num_ntfy_blks)
2038 		goto reset;
2039 
2040 	block = &priv->ntfy_blocks[ntfy_idx];
2041 	tx = block->tx;
2042 
2043 	current_time = jiffies_to_msecs(jiffies);
2044 	if (tx->last_kick_msec + MIN_TX_TIMEOUT_GAP > current_time)
2045 		goto reset;
2046 
2047 	/* Check to see if there are missed completions, which will allow us to
2048 	 * kick the queue.
2049 	 */
2050 	last_nic_done = gve_tx_load_event_counter(priv, tx);
2051 	if (last_nic_done - tx->done) {
2052 		netdev_info(dev, "Kicking queue %d", txqueue);
2053 		iowrite32be(GVE_IRQ_MASK, gve_irq_doorbell(priv, block));
2054 		napi_schedule(&block->napi);
2055 		tx->last_kick_msec = current_time;
2056 		goto out;
2057 	} // Else reset.
2058 
2059 reset:
2060 	gve_schedule_reset(priv);
2061 
2062 out:
2063 	if (tx)
2064 		tx->queue_timeout++;
2065 	priv->tx_timeo_cnt++;
2066 }
2067 
2068 static int gve_set_features(struct net_device *netdev,
2069 			    netdev_features_t features)
2070 {
2071 	const netdev_features_t orig_features = netdev->features;
2072 	struct gve_tx_alloc_rings_cfg tx_alloc_cfg = {0};
2073 	struct gve_rx_alloc_rings_cfg rx_alloc_cfg = {0};
2074 	struct gve_qpls_alloc_cfg qpls_alloc_cfg = {0};
2075 	struct gve_priv *priv = netdev_priv(netdev);
2076 	struct gve_qpl_config new_qpl_cfg;
2077 	int err;
2078 
2079 	gve_get_curr_alloc_cfgs(priv, &qpls_alloc_cfg,
2080 				&tx_alloc_cfg, &rx_alloc_cfg);
2081 	/* qpl_cfg is not read-only, it contains a map that gets updated as
2082 	 * rings are allocated, which is why we cannot use the yet unreleased
2083 	 * one in priv.
2084 	 */
2085 	qpls_alloc_cfg.qpl_cfg = &new_qpl_cfg;
2086 	tx_alloc_cfg.qpl_cfg = &new_qpl_cfg;
2087 	rx_alloc_cfg.qpl_cfg = &new_qpl_cfg;
2088 
2089 	if ((netdev->features & NETIF_F_LRO) != (features & NETIF_F_LRO)) {
2090 		netdev->features ^= NETIF_F_LRO;
2091 		if (netif_carrier_ok(netdev)) {
2092 			err = gve_adjust_config(priv, &qpls_alloc_cfg,
2093 						&tx_alloc_cfg, &rx_alloc_cfg);
2094 			if (err) {
2095 				/* Revert the change on error. */
2096 				netdev->features = orig_features;
2097 				return err;
2098 			}
2099 		}
2100 	}
2101 
2102 	return 0;
2103 }
2104 
2105 static const struct net_device_ops gve_netdev_ops = {
2106 	.ndo_start_xmit		=	gve_start_xmit,
2107 	.ndo_features_check	=	gve_features_check,
2108 	.ndo_open		=	gve_open,
2109 	.ndo_stop		=	gve_close,
2110 	.ndo_get_stats64	=	gve_get_stats,
2111 	.ndo_tx_timeout         =       gve_tx_timeout,
2112 	.ndo_set_features	=	gve_set_features,
2113 	.ndo_bpf		=	gve_xdp,
2114 	.ndo_xdp_xmit		=	gve_xdp_xmit,
2115 	.ndo_xsk_wakeup		=	gve_xsk_wakeup,
2116 };
2117 
2118 static void gve_handle_status(struct gve_priv *priv, u32 status)
2119 {
2120 	if (GVE_DEVICE_STATUS_RESET_MASK & status) {
2121 		dev_info(&priv->pdev->dev, "Device requested reset.\n");
2122 		gve_set_do_reset(priv);
2123 	}
2124 	if (GVE_DEVICE_STATUS_REPORT_STATS_MASK & status) {
2125 		priv->stats_report_trigger_cnt++;
2126 		gve_set_do_report_stats(priv);
2127 	}
2128 }
2129 
2130 static void gve_handle_reset(struct gve_priv *priv)
2131 {
2132 	/* A service task will be scheduled at the end of probe to catch any
2133 	 * resets that need to happen, and we don't want to reset until
2134 	 * probe is done.
2135 	 */
2136 	if (gve_get_probe_in_progress(priv))
2137 		return;
2138 
2139 	if (gve_get_do_reset(priv)) {
2140 		rtnl_lock();
2141 		gve_reset(priv, false);
2142 		rtnl_unlock();
2143 	}
2144 }
2145 
2146 void gve_handle_report_stats(struct gve_priv *priv)
2147 {
2148 	struct stats *stats = priv->stats_report->stats;
2149 	int idx, stats_idx = 0;
2150 	unsigned int start = 0;
2151 	u64 tx_bytes;
2152 
2153 	if (!gve_get_report_stats(priv))
2154 		return;
2155 
2156 	be64_add_cpu(&priv->stats_report->written_count, 1);
2157 	/* tx stats */
2158 	if (priv->tx) {
2159 		for (idx = 0; idx < gve_num_tx_queues(priv); idx++) {
2160 			u32 last_completion = 0;
2161 			u32 tx_frames = 0;
2162 
2163 			/* DQO doesn't currently support these metrics. */
2164 			if (gve_is_gqi(priv)) {
2165 				last_completion = priv->tx[idx].done;
2166 				tx_frames = priv->tx[idx].req;
2167 			}
2168 
2169 			do {
2170 				start = u64_stats_fetch_begin(&priv->tx[idx].statss);
2171 				tx_bytes = priv->tx[idx].bytes_done;
2172 			} while (u64_stats_fetch_retry(&priv->tx[idx].statss, start));
2173 			stats[stats_idx++] = (struct stats) {
2174 				.stat_name = cpu_to_be32(TX_WAKE_CNT),
2175 				.value = cpu_to_be64(priv->tx[idx].wake_queue),
2176 				.queue_id = cpu_to_be32(idx),
2177 			};
2178 			stats[stats_idx++] = (struct stats) {
2179 				.stat_name = cpu_to_be32(TX_STOP_CNT),
2180 				.value = cpu_to_be64(priv->tx[idx].stop_queue),
2181 				.queue_id = cpu_to_be32(idx),
2182 			};
2183 			stats[stats_idx++] = (struct stats) {
2184 				.stat_name = cpu_to_be32(TX_FRAMES_SENT),
2185 				.value = cpu_to_be64(tx_frames),
2186 				.queue_id = cpu_to_be32(idx),
2187 			};
2188 			stats[stats_idx++] = (struct stats) {
2189 				.stat_name = cpu_to_be32(TX_BYTES_SENT),
2190 				.value = cpu_to_be64(tx_bytes),
2191 				.queue_id = cpu_to_be32(idx),
2192 			};
2193 			stats[stats_idx++] = (struct stats) {
2194 				.stat_name = cpu_to_be32(TX_LAST_COMPLETION_PROCESSED),
2195 				.value = cpu_to_be64(last_completion),
2196 				.queue_id = cpu_to_be32(idx),
2197 			};
2198 			stats[stats_idx++] = (struct stats) {
2199 				.stat_name = cpu_to_be32(TX_TIMEOUT_CNT),
2200 				.value = cpu_to_be64(priv->tx[idx].queue_timeout),
2201 				.queue_id = cpu_to_be32(idx),
2202 			};
2203 		}
2204 	}
2205 	/* rx stats */
2206 	if (priv->rx) {
2207 		for (idx = 0; idx < priv->rx_cfg.num_queues; idx++) {
2208 			stats[stats_idx++] = (struct stats) {
2209 				.stat_name = cpu_to_be32(RX_NEXT_EXPECTED_SEQUENCE),
2210 				.value = cpu_to_be64(priv->rx[idx].desc.seqno),
2211 				.queue_id = cpu_to_be32(idx),
2212 			};
2213 			stats[stats_idx++] = (struct stats) {
2214 				.stat_name = cpu_to_be32(RX_BUFFERS_POSTED),
2215 				.value = cpu_to_be64(priv->rx[0].fill_cnt),
2216 				.queue_id = cpu_to_be32(idx),
2217 			};
2218 		}
2219 	}
2220 }
2221 
2222 /* Handle NIC status register changes, reset requests and report stats */
2223 static void gve_service_task(struct work_struct *work)
2224 {
2225 	struct gve_priv *priv = container_of(work, struct gve_priv,
2226 					     service_task);
2227 	u32 status = ioread32be(&priv->reg_bar0->device_status);
2228 
2229 	gve_handle_status(priv, status);
2230 
2231 	gve_handle_reset(priv);
2232 	gve_handle_link_status(priv, GVE_DEVICE_STATUS_LINK_STATUS_MASK & status);
2233 }
2234 
2235 static void gve_set_netdev_xdp_features(struct gve_priv *priv)
2236 {
2237 	if (priv->queue_format == GVE_GQI_QPL_FORMAT) {
2238 		priv->dev->xdp_features = NETDEV_XDP_ACT_BASIC;
2239 		priv->dev->xdp_features |= NETDEV_XDP_ACT_REDIRECT;
2240 		priv->dev->xdp_features |= NETDEV_XDP_ACT_NDO_XMIT;
2241 		priv->dev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
2242 	} else {
2243 		priv->dev->xdp_features = 0;
2244 	}
2245 }
2246 
2247 static int gve_init_priv(struct gve_priv *priv, bool skip_describe_device)
2248 {
2249 	int num_ntfy;
2250 	int err;
2251 
2252 	/* Set up the adminq */
2253 	err = gve_adminq_alloc(&priv->pdev->dev, priv);
2254 	if (err) {
2255 		dev_err(&priv->pdev->dev,
2256 			"Failed to alloc admin queue: err=%d\n", err);
2257 		return err;
2258 	}
2259 
2260 	err = gve_verify_driver_compatibility(priv);
2261 	if (err) {
2262 		dev_err(&priv->pdev->dev,
2263 			"Could not verify driver compatibility: err=%d\n", err);
2264 		goto err;
2265 	}
2266 
2267 	priv->num_registered_pages = 0;
2268 
2269 	if (skip_describe_device)
2270 		goto setup_device;
2271 
2272 	priv->queue_format = GVE_QUEUE_FORMAT_UNSPECIFIED;
2273 	/* Get the initial information we need from the device */
2274 	err = gve_adminq_describe_device(priv);
2275 	if (err) {
2276 		dev_err(&priv->pdev->dev,
2277 			"Could not get device information: err=%d\n", err);
2278 		goto err;
2279 	}
2280 	priv->dev->mtu = priv->dev->max_mtu;
2281 	num_ntfy = pci_msix_vec_count(priv->pdev);
2282 	if (num_ntfy <= 0) {
2283 		dev_err(&priv->pdev->dev,
2284 			"could not count MSI-x vectors: err=%d\n", num_ntfy);
2285 		err = num_ntfy;
2286 		goto err;
2287 	} else if (num_ntfy < GVE_MIN_MSIX) {
2288 		dev_err(&priv->pdev->dev, "gve needs at least %d MSI-x vectors, but only has %d\n",
2289 			GVE_MIN_MSIX, num_ntfy);
2290 		err = -EINVAL;
2291 		goto err;
2292 	}
2293 
2294 	/* Big TCP is only supported on DQ*/
2295 	if (!gve_is_gqi(priv))
2296 		netif_set_tso_max_size(priv->dev, GVE_DQO_TX_MAX);
2297 
2298 	priv->rx_copybreak = GVE_DEFAULT_RX_COPYBREAK;
2299 	/* gvnic has one Notification Block per MSI-x vector, except for the
2300 	 * management vector
2301 	 */
2302 	priv->num_ntfy_blks = (num_ntfy - 1) & ~0x1;
2303 	priv->mgmt_msix_idx = priv->num_ntfy_blks;
2304 
2305 	priv->tx_cfg.max_queues =
2306 		min_t(int, priv->tx_cfg.max_queues, priv->num_ntfy_blks / 2);
2307 	priv->rx_cfg.max_queues =
2308 		min_t(int, priv->rx_cfg.max_queues, priv->num_ntfy_blks / 2);
2309 
2310 	priv->tx_cfg.num_queues = priv->tx_cfg.max_queues;
2311 	priv->rx_cfg.num_queues = priv->rx_cfg.max_queues;
2312 	if (priv->default_num_queues > 0) {
2313 		priv->tx_cfg.num_queues = min_t(int, priv->default_num_queues,
2314 						priv->tx_cfg.num_queues);
2315 		priv->rx_cfg.num_queues = min_t(int, priv->default_num_queues,
2316 						priv->rx_cfg.num_queues);
2317 	}
2318 
2319 	dev_info(&priv->pdev->dev, "TX queues %d, RX queues %d\n",
2320 		 priv->tx_cfg.num_queues, priv->rx_cfg.num_queues);
2321 	dev_info(&priv->pdev->dev, "Max TX queues %d, Max RX queues %d\n",
2322 		 priv->tx_cfg.max_queues, priv->rx_cfg.max_queues);
2323 
2324 	if (!gve_is_gqi(priv)) {
2325 		priv->tx_coalesce_usecs = GVE_TX_IRQ_RATELIMIT_US_DQO;
2326 		priv->rx_coalesce_usecs = GVE_RX_IRQ_RATELIMIT_US_DQO;
2327 	}
2328 
2329 setup_device:
2330 	gve_set_netdev_xdp_features(priv);
2331 	err = gve_setup_device_resources(priv);
2332 	if (!err)
2333 		return 0;
2334 err:
2335 	gve_adminq_free(&priv->pdev->dev, priv);
2336 	return err;
2337 }
2338 
2339 static void gve_teardown_priv_resources(struct gve_priv *priv)
2340 {
2341 	gve_teardown_device_resources(priv);
2342 	gve_adminq_free(&priv->pdev->dev, priv);
2343 }
2344 
2345 static void gve_trigger_reset(struct gve_priv *priv)
2346 {
2347 	/* Reset the device by releasing the AQ */
2348 	gve_adminq_release(priv);
2349 }
2350 
2351 static void gve_reset_and_teardown(struct gve_priv *priv, bool was_up)
2352 {
2353 	gve_trigger_reset(priv);
2354 	/* With the reset having already happened, close cannot fail */
2355 	if (was_up)
2356 		gve_close(priv->dev);
2357 	gve_teardown_priv_resources(priv);
2358 }
2359 
2360 static int gve_reset_recovery(struct gve_priv *priv, bool was_up)
2361 {
2362 	int err;
2363 
2364 	err = gve_init_priv(priv, true);
2365 	if (err)
2366 		goto err;
2367 	if (was_up) {
2368 		err = gve_open(priv->dev);
2369 		if (err)
2370 			goto err;
2371 	}
2372 	return 0;
2373 err:
2374 	dev_err(&priv->pdev->dev, "Reset failed! !!! DISABLING ALL QUEUES !!!\n");
2375 	gve_turndown(priv);
2376 	return err;
2377 }
2378 
2379 int gve_reset(struct gve_priv *priv, bool attempt_teardown)
2380 {
2381 	bool was_up = netif_carrier_ok(priv->dev);
2382 	int err;
2383 
2384 	dev_info(&priv->pdev->dev, "Performing reset\n");
2385 	gve_clear_do_reset(priv);
2386 	gve_set_reset_in_progress(priv);
2387 	/* If we aren't attempting to teardown normally, just go turndown and
2388 	 * reset right away.
2389 	 */
2390 	if (!attempt_teardown) {
2391 		gve_turndown(priv);
2392 		gve_reset_and_teardown(priv, was_up);
2393 	} else {
2394 		/* Otherwise attempt to close normally */
2395 		if (was_up) {
2396 			err = gve_close(priv->dev);
2397 			/* If that fails reset as we did above */
2398 			if (err)
2399 				gve_reset_and_teardown(priv, was_up);
2400 		}
2401 		/* Clean up any remaining resources */
2402 		gve_teardown_priv_resources(priv);
2403 	}
2404 
2405 	/* Set it all back up */
2406 	err = gve_reset_recovery(priv, was_up);
2407 	gve_clear_reset_in_progress(priv);
2408 	priv->reset_cnt++;
2409 	priv->interface_up_cnt = 0;
2410 	priv->interface_down_cnt = 0;
2411 	priv->stats_report_trigger_cnt = 0;
2412 	return err;
2413 }
2414 
2415 static void gve_write_version(u8 __iomem *driver_version_register)
2416 {
2417 	const char *c = gve_version_prefix;
2418 
2419 	while (*c) {
2420 		writeb(*c, driver_version_register);
2421 		c++;
2422 	}
2423 
2424 	c = gve_version_str;
2425 	while (*c) {
2426 		writeb(*c, driver_version_register);
2427 		c++;
2428 	}
2429 	writeb('\n', driver_version_register);
2430 }
2431 
2432 static int gve_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2433 {
2434 	int max_tx_queues, max_rx_queues;
2435 	struct net_device *dev;
2436 	__be32 __iomem *db_bar;
2437 	struct gve_registers __iomem *reg_bar;
2438 	struct gve_priv *priv;
2439 	int err;
2440 
2441 	err = pci_enable_device(pdev);
2442 	if (err)
2443 		return err;
2444 
2445 	err = pci_request_regions(pdev, gve_driver_name);
2446 	if (err)
2447 		goto abort_with_enabled;
2448 
2449 	pci_set_master(pdev);
2450 
2451 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2452 	if (err) {
2453 		dev_err(&pdev->dev, "Failed to set dma mask: err=%d\n", err);
2454 		goto abort_with_pci_region;
2455 	}
2456 
2457 	reg_bar = pci_iomap(pdev, GVE_REGISTER_BAR, 0);
2458 	if (!reg_bar) {
2459 		dev_err(&pdev->dev, "Failed to map pci bar!\n");
2460 		err = -ENOMEM;
2461 		goto abort_with_pci_region;
2462 	}
2463 
2464 	db_bar = pci_iomap(pdev, GVE_DOORBELL_BAR, 0);
2465 	if (!db_bar) {
2466 		dev_err(&pdev->dev, "Failed to map doorbell bar!\n");
2467 		err = -ENOMEM;
2468 		goto abort_with_reg_bar;
2469 	}
2470 
2471 	gve_write_version(&reg_bar->driver_version);
2472 	/* Get max queues to alloc etherdev */
2473 	max_tx_queues = ioread32be(&reg_bar->max_tx_queues);
2474 	max_rx_queues = ioread32be(&reg_bar->max_rx_queues);
2475 	/* Alloc and setup the netdev and priv */
2476 	dev = alloc_etherdev_mqs(sizeof(*priv), max_tx_queues, max_rx_queues);
2477 	if (!dev) {
2478 		dev_err(&pdev->dev, "could not allocate netdev\n");
2479 		err = -ENOMEM;
2480 		goto abort_with_db_bar;
2481 	}
2482 	SET_NETDEV_DEV(dev, &pdev->dev);
2483 	pci_set_drvdata(pdev, dev);
2484 	dev->ethtool_ops = &gve_ethtool_ops;
2485 	dev->netdev_ops = &gve_netdev_ops;
2486 
2487 	/* Set default and supported features.
2488 	 *
2489 	 * Features might be set in other locations as well (such as
2490 	 * `gve_adminq_describe_device`).
2491 	 */
2492 	dev->hw_features = NETIF_F_HIGHDMA;
2493 	dev->hw_features |= NETIF_F_SG;
2494 	dev->hw_features |= NETIF_F_HW_CSUM;
2495 	dev->hw_features |= NETIF_F_TSO;
2496 	dev->hw_features |= NETIF_F_TSO6;
2497 	dev->hw_features |= NETIF_F_TSO_ECN;
2498 	dev->hw_features |= NETIF_F_RXCSUM;
2499 	dev->hw_features |= NETIF_F_RXHASH;
2500 	dev->features = dev->hw_features;
2501 	dev->watchdog_timeo = 5 * HZ;
2502 	dev->min_mtu = ETH_MIN_MTU;
2503 	netif_carrier_off(dev);
2504 
2505 	priv = netdev_priv(dev);
2506 	priv->dev = dev;
2507 	priv->pdev = pdev;
2508 	priv->msg_enable = DEFAULT_MSG_LEVEL;
2509 	priv->reg_bar0 = reg_bar;
2510 	priv->db_bar2 = db_bar;
2511 	priv->service_task_flags = 0x0;
2512 	priv->state_flags = 0x0;
2513 	priv->ethtool_flags = 0x0;
2514 
2515 	gve_set_probe_in_progress(priv);
2516 	priv->gve_wq = alloc_ordered_workqueue("gve", 0);
2517 	if (!priv->gve_wq) {
2518 		dev_err(&pdev->dev, "Could not allocate workqueue");
2519 		err = -ENOMEM;
2520 		goto abort_with_netdev;
2521 	}
2522 	INIT_WORK(&priv->service_task, gve_service_task);
2523 	INIT_WORK(&priv->stats_report_task, gve_stats_report_task);
2524 	priv->tx_cfg.max_queues = max_tx_queues;
2525 	priv->rx_cfg.max_queues = max_rx_queues;
2526 
2527 	err = gve_init_priv(priv, false);
2528 	if (err)
2529 		goto abort_with_wq;
2530 
2531 	err = register_netdev(dev);
2532 	if (err)
2533 		goto abort_with_gve_init;
2534 
2535 	dev_info(&pdev->dev, "GVE version %s\n", gve_version_str);
2536 	dev_info(&pdev->dev, "GVE queue format %d\n", (int)priv->queue_format);
2537 	gve_clear_probe_in_progress(priv);
2538 	queue_work(priv->gve_wq, &priv->service_task);
2539 	return 0;
2540 
2541 abort_with_gve_init:
2542 	gve_teardown_priv_resources(priv);
2543 
2544 abort_with_wq:
2545 	destroy_workqueue(priv->gve_wq);
2546 
2547 abort_with_netdev:
2548 	free_netdev(dev);
2549 
2550 abort_with_db_bar:
2551 	pci_iounmap(pdev, db_bar);
2552 
2553 abort_with_reg_bar:
2554 	pci_iounmap(pdev, reg_bar);
2555 
2556 abort_with_pci_region:
2557 	pci_release_regions(pdev);
2558 
2559 abort_with_enabled:
2560 	pci_disable_device(pdev);
2561 	return err;
2562 }
2563 
2564 static void gve_remove(struct pci_dev *pdev)
2565 {
2566 	struct net_device *netdev = pci_get_drvdata(pdev);
2567 	struct gve_priv *priv = netdev_priv(netdev);
2568 	__be32 __iomem *db_bar = priv->db_bar2;
2569 	void __iomem *reg_bar = priv->reg_bar0;
2570 
2571 	unregister_netdev(netdev);
2572 	gve_teardown_priv_resources(priv);
2573 	destroy_workqueue(priv->gve_wq);
2574 	free_netdev(netdev);
2575 	pci_iounmap(pdev, db_bar);
2576 	pci_iounmap(pdev, reg_bar);
2577 	pci_release_regions(pdev);
2578 	pci_disable_device(pdev);
2579 }
2580 
2581 static void gve_shutdown(struct pci_dev *pdev)
2582 {
2583 	struct net_device *netdev = pci_get_drvdata(pdev);
2584 	struct gve_priv *priv = netdev_priv(netdev);
2585 	bool was_up = netif_carrier_ok(priv->dev);
2586 
2587 	rtnl_lock();
2588 	if (was_up && gve_close(priv->dev)) {
2589 		/* If the dev was up, attempt to close, if close fails, reset */
2590 		gve_reset_and_teardown(priv, was_up);
2591 	} else {
2592 		/* If the dev wasn't up or close worked, finish tearing down */
2593 		gve_teardown_priv_resources(priv);
2594 	}
2595 	rtnl_unlock();
2596 }
2597 
2598 #ifdef CONFIG_PM
2599 static int gve_suspend(struct pci_dev *pdev, pm_message_t state)
2600 {
2601 	struct net_device *netdev = pci_get_drvdata(pdev);
2602 	struct gve_priv *priv = netdev_priv(netdev);
2603 	bool was_up = netif_carrier_ok(priv->dev);
2604 
2605 	priv->suspend_cnt++;
2606 	rtnl_lock();
2607 	if (was_up && gve_close(priv->dev)) {
2608 		/* If the dev was up, attempt to close, if close fails, reset */
2609 		gve_reset_and_teardown(priv, was_up);
2610 	} else {
2611 		/* If the dev wasn't up or close worked, finish tearing down */
2612 		gve_teardown_priv_resources(priv);
2613 	}
2614 	priv->up_before_suspend = was_up;
2615 	rtnl_unlock();
2616 	return 0;
2617 }
2618 
2619 static int gve_resume(struct pci_dev *pdev)
2620 {
2621 	struct net_device *netdev = pci_get_drvdata(pdev);
2622 	struct gve_priv *priv = netdev_priv(netdev);
2623 	int err;
2624 
2625 	priv->resume_cnt++;
2626 	rtnl_lock();
2627 	err = gve_reset_recovery(priv, priv->up_before_suspend);
2628 	rtnl_unlock();
2629 	return err;
2630 }
2631 #endif /* CONFIG_PM */
2632 
2633 static const struct pci_device_id gve_id_table[] = {
2634 	{ PCI_DEVICE(PCI_VENDOR_ID_GOOGLE, PCI_DEV_ID_GVNIC) },
2635 	{ }
2636 };
2637 
2638 static struct pci_driver gve_driver = {
2639 	.name		= gve_driver_name,
2640 	.id_table	= gve_id_table,
2641 	.probe		= gve_probe,
2642 	.remove		= gve_remove,
2643 	.shutdown	= gve_shutdown,
2644 #ifdef CONFIG_PM
2645 	.suspend        = gve_suspend,
2646 	.resume         = gve_resume,
2647 #endif
2648 };
2649 
2650 module_pci_driver(gve_driver);
2651 
2652 MODULE_DEVICE_TABLE(pci, gve_id_table);
2653 MODULE_AUTHOR("Google, Inc.");
2654 MODULE_DESCRIPTION("Google Virtual NIC Driver");
2655 MODULE_LICENSE("Dual MIT/GPL");
2656 MODULE_VERSION(GVE_VERSION);
2657