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