1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
4 * Copyright (C) 2015-2021 Google, Inc.
7 #include <linux/etherdevice.h>
10 #include "gve_adminq.h"
11 #include "gve_register.h"
13 #define GVE_MAX_ADMINQ_RELEASE_CHECK 500
14 #define GVE_ADMINQ_SLEEP_LEN 20
15 #define GVE_MAX_ADMINQ_EVENT_COUNTER_CHECK 100
17 #define GVE_DEVICE_OPTION_ERROR_FMT "%s option error:\n" \
18 "Expected: length=%d, feature_mask=%x.\n" \
19 "Actual: length=%d, feature_mask=%x.\n"
21 #define GVE_DEVICE_OPTION_TOO_BIG_FMT "Length of %s option larger than expected. Possible older version of guest driver.\n"
24 struct gve_device_option *gve_get_next_option(struct gve_device_descriptor *descriptor,
25 struct gve_device_option *option)
27 void *option_end, *descriptor_end;
29 option_end = (void *)(option + 1) + be16_to_cpu(option->option_length);
30 descriptor_end = (void *)descriptor + be16_to_cpu(descriptor->total_length);
32 return option_end > descriptor_end ? NULL : (struct gve_device_option *)option_end;
36 void gve_parse_device_option(struct gve_priv *priv,
37 struct gve_device_descriptor *device_descriptor,
38 struct gve_device_option *option,
39 struct gve_device_option_gqi_rda **dev_op_gqi_rda,
40 struct gve_device_option_gqi_qpl **dev_op_gqi_qpl,
41 struct gve_device_option_dqo_rda **dev_op_dqo_rda)
43 u32 req_feat_mask = be32_to_cpu(option->required_features_mask);
44 u16 option_length = be16_to_cpu(option->option_length);
45 u16 option_id = be16_to_cpu(option->option_id);
47 /* If the length or feature mask doesn't match, continue without
48 * enabling the feature.
51 case GVE_DEV_OPT_ID_GQI_RAW_ADDRESSING:
52 if (option_length != GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING ||
53 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING) {
54 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
56 GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING,
57 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING,
58 option_length, req_feat_mask);
62 dev_info(&priv->pdev->dev,
63 "Gqi raw addressing device option enabled.\n");
64 priv->queue_format = GVE_GQI_RDA_FORMAT;
66 case GVE_DEV_OPT_ID_GQI_RDA:
67 if (option_length < sizeof(**dev_op_gqi_rda) ||
68 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA) {
69 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
70 "GQI RDA", (int)sizeof(**dev_op_gqi_rda),
71 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA,
72 option_length, req_feat_mask);
76 if (option_length > sizeof(**dev_op_gqi_rda)) {
77 dev_warn(&priv->pdev->dev,
78 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI RDA");
80 *dev_op_gqi_rda = (void *)(option + 1);
82 case GVE_DEV_OPT_ID_GQI_QPL:
83 if (option_length < sizeof(**dev_op_gqi_qpl) ||
84 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL) {
85 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
86 "GQI QPL", (int)sizeof(**dev_op_gqi_qpl),
87 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL,
88 option_length, req_feat_mask);
92 if (option_length > sizeof(**dev_op_gqi_qpl)) {
93 dev_warn(&priv->pdev->dev,
94 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI QPL");
96 *dev_op_gqi_qpl = (void *)(option + 1);
98 case GVE_DEV_OPT_ID_DQO_RDA:
99 if (option_length < sizeof(**dev_op_dqo_rda) ||
100 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA) {
101 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
102 "DQO RDA", (int)sizeof(**dev_op_dqo_rda),
103 GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA,
104 option_length, req_feat_mask);
108 if (option_length > sizeof(**dev_op_dqo_rda)) {
109 dev_warn(&priv->pdev->dev,
110 GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO RDA");
112 *dev_op_dqo_rda = (void *)(option + 1);
115 /* If we don't recognize the option just continue
116 * without doing anything.
118 dev_dbg(&priv->pdev->dev, "Unrecognized device option 0x%hx not enabled.\n",
123 /* Process all device options for a given describe device call. */
125 gve_process_device_options(struct gve_priv *priv,
126 struct gve_device_descriptor *descriptor,
127 struct gve_device_option_gqi_rda **dev_op_gqi_rda,
128 struct gve_device_option_gqi_qpl **dev_op_gqi_qpl,
129 struct gve_device_option_dqo_rda **dev_op_dqo_rda)
131 const int num_options = be16_to_cpu(descriptor->num_device_options);
132 struct gve_device_option *dev_opt;
135 /* The options struct directly follows the device descriptor. */
136 dev_opt = (void *)(descriptor + 1);
137 for (i = 0; i < num_options; i++) {
138 struct gve_device_option *next_opt;
140 next_opt = gve_get_next_option(descriptor, dev_opt);
142 dev_err(&priv->dev->dev,
143 "options exceed device_descriptor's total length.\n");
147 gve_parse_device_option(priv, descriptor, dev_opt,
148 dev_op_gqi_rda, dev_op_gqi_qpl,
156 int gve_adminq_alloc(struct device *dev, struct gve_priv *priv)
158 priv->adminq = dma_alloc_coherent(dev, PAGE_SIZE,
159 &priv->adminq_bus_addr, GFP_KERNEL);
160 if (unlikely(!priv->adminq))
163 priv->adminq_mask = (PAGE_SIZE / sizeof(union gve_adminq_command)) - 1;
164 priv->adminq_prod_cnt = 0;
165 priv->adminq_cmd_fail = 0;
166 priv->adminq_timeouts = 0;
167 priv->adminq_describe_device_cnt = 0;
168 priv->adminq_cfg_device_resources_cnt = 0;
169 priv->adminq_register_page_list_cnt = 0;
170 priv->adminq_unregister_page_list_cnt = 0;
171 priv->adminq_create_tx_queue_cnt = 0;
172 priv->adminq_create_rx_queue_cnt = 0;
173 priv->adminq_destroy_tx_queue_cnt = 0;
174 priv->adminq_destroy_rx_queue_cnt = 0;
175 priv->adminq_dcfg_device_resources_cnt = 0;
176 priv->adminq_set_driver_parameter_cnt = 0;
177 priv->adminq_report_stats_cnt = 0;
178 priv->adminq_report_link_speed_cnt = 0;
179 priv->adminq_get_ptype_map_cnt = 0;
181 /* Setup Admin queue with the device */
182 iowrite32be(priv->adminq_bus_addr / PAGE_SIZE,
183 &priv->reg_bar0->adminq_pfn);
185 gve_set_admin_queue_ok(priv);
189 void gve_adminq_release(struct gve_priv *priv)
193 /* Tell the device the adminq is leaving */
194 iowrite32be(0x0, &priv->reg_bar0->adminq_pfn);
195 while (ioread32be(&priv->reg_bar0->adminq_pfn)) {
196 /* If this is reached the device is unrecoverable and still
197 * holding memory. Continue looping to avoid memory corruption,
198 * but WARN so it is visible what is going on.
200 if (i == GVE_MAX_ADMINQ_RELEASE_CHECK)
201 WARN(1, "Unrecoverable platform error!");
203 msleep(GVE_ADMINQ_SLEEP_LEN);
205 gve_clear_device_rings_ok(priv);
206 gve_clear_device_resources_ok(priv);
207 gve_clear_admin_queue_ok(priv);
210 void gve_adminq_free(struct device *dev, struct gve_priv *priv)
212 if (!gve_get_admin_queue_ok(priv))
214 gve_adminq_release(priv);
215 dma_free_coherent(dev, PAGE_SIZE, priv->adminq, priv->adminq_bus_addr);
216 gve_clear_admin_queue_ok(priv);
219 static void gve_adminq_kick_cmd(struct gve_priv *priv, u32 prod_cnt)
221 iowrite32be(prod_cnt, &priv->reg_bar0->adminq_doorbell);
224 static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt)
228 for (i = 0; i < GVE_MAX_ADMINQ_EVENT_COUNTER_CHECK; i++) {
229 if (ioread32be(&priv->reg_bar0->adminq_event_counter)
232 msleep(GVE_ADMINQ_SLEEP_LEN);
238 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
240 if (status != GVE_ADMINQ_COMMAND_PASSED &&
241 status != GVE_ADMINQ_COMMAND_UNSET) {
242 dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status);
243 priv->adminq_cmd_fail++;
246 case GVE_ADMINQ_COMMAND_PASSED:
248 case GVE_ADMINQ_COMMAND_UNSET:
249 dev_err(&priv->pdev->dev, "parse_aq_err: err and status both unset, this should not be possible.\n");
251 case GVE_ADMINQ_COMMAND_ERROR_ABORTED:
252 case GVE_ADMINQ_COMMAND_ERROR_CANCELLED:
253 case GVE_ADMINQ_COMMAND_ERROR_DATALOSS:
254 case GVE_ADMINQ_COMMAND_ERROR_FAILED_PRECONDITION:
255 case GVE_ADMINQ_COMMAND_ERROR_UNAVAILABLE:
257 case GVE_ADMINQ_COMMAND_ERROR_ALREADY_EXISTS:
258 case GVE_ADMINQ_COMMAND_ERROR_INTERNAL_ERROR:
259 case GVE_ADMINQ_COMMAND_ERROR_INVALID_ARGUMENT:
260 case GVE_ADMINQ_COMMAND_ERROR_NOT_FOUND:
261 case GVE_ADMINQ_COMMAND_ERROR_OUT_OF_RANGE:
262 case GVE_ADMINQ_COMMAND_ERROR_UNKNOWN_ERROR:
264 case GVE_ADMINQ_COMMAND_ERROR_DEADLINE_EXCEEDED:
266 case GVE_ADMINQ_COMMAND_ERROR_PERMISSION_DENIED:
267 case GVE_ADMINQ_COMMAND_ERROR_UNAUTHENTICATED:
269 case GVE_ADMINQ_COMMAND_ERROR_RESOURCE_EXHAUSTED:
271 case GVE_ADMINQ_COMMAND_ERROR_UNIMPLEMENTED:
274 dev_err(&priv->pdev->dev, "parse_aq_err: unknown status code %d\n", status);
279 /* Flushes all AQ commands currently queued and waits for them to complete.
280 * If there are failures, it will return the first error.
282 static int gve_adminq_kick_and_wait(struct gve_priv *priv)
287 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
288 head = priv->adminq_prod_cnt;
290 gve_adminq_kick_cmd(priv, head);
291 if (!gve_adminq_wait_for_cmd(priv, head)) {
292 dev_err(&priv->pdev->dev, "AQ commands timed out, need to reset AQ\n");
293 priv->adminq_timeouts++;
294 return -ENOTRECOVERABLE;
297 for (i = tail; i < head; i++) {
298 union gve_adminq_command *cmd;
301 cmd = &priv->adminq[i & priv->adminq_mask];
302 status = be32_to_cpu(READ_ONCE(cmd->status));
303 err = gve_adminq_parse_err(priv, status);
305 // Return the first error if we failed.
312 /* This function is not threadsafe - the caller is responsible for any
315 static int gve_adminq_issue_cmd(struct gve_priv *priv,
316 union gve_adminq_command *cmd_orig)
318 union gve_adminq_command *cmd;
322 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
324 // Check if next command will overflow the buffer.
325 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) ==
326 (tail & priv->adminq_mask)) {
329 // Flush existing commands to make room.
330 err = gve_adminq_kick_and_wait(priv);
335 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
336 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) ==
337 (tail & priv->adminq_mask)) {
338 // This should never happen. We just flushed the
339 // command queue so there should be enough space.
344 cmd = &priv->adminq[priv->adminq_prod_cnt & priv->adminq_mask];
345 priv->adminq_prod_cnt++;
347 memcpy(cmd, cmd_orig, sizeof(*cmd_orig));
348 opcode = be32_to_cpu(READ_ONCE(cmd->opcode));
351 case GVE_ADMINQ_DESCRIBE_DEVICE:
352 priv->adminq_describe_device_cnt++;
354 case GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES:
355 priv->adminq_cfg_device_resources_cnt++;
357 case GVE_ADMINQ_REGISTER_PAGE_LIST:
358 priv->adminq_register_page_list_cnt++;
360 case GVE_ADMINQ_UNREGISTER_PAGE_LIST:
361 priv->adminq_unregister_page_list_cnt++;
363 case GVE_ADMINQ_CREATE_TX_QUEUE:
364 priv->adminq_create_tx_queue_cnt++;
366 case GVE_ADMINQ_CREATE_RX_QUEUE:
367 priv->adminq_create_rx_queue_cnt++;
369 case GVE_ADMINQ_DESTROY_TX_QUEUE:
370 priv->adminq_destroy_tx_queue_cnt++;
372 case GVE_ADMINQ_DESTROY_RX_QUEUE:
373 priv->adminq_destroy_rx_queue_cnt++;
375 case GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES:
376 priv->adminq_dcfg_device_resources_cnt++;
378 case GVE_ADMINQ_SET_DRIVER_PARAMETER:
379 priv->adminq_set_driver_parameter_cnt++;
381 case GVE_ADMINQ_REPORT_STATS:
382 priv->adminq_report_stats_cnt++;
384 case GVE_ADMINQ_REPORT_LINK_SPEED:
385 priv->adminq_report_link_speed_cnt++;
387 case GVE_ADMINQ_GET_PTYPE_MAP:
388 priv->adminq_get_ptype_map_cnt++;
391 dev_err(&priv->pdev->dev, "unknown AQ command opcode %d\n", opcode);
397 /* This function is not threadsafe - the caller is responsible for any
399 * The caller is also responsible for making sure there are no commands
400 * waiting to be executed.
402 static int gve_adminq_execute_cmd(struct gve_priv *priv,
403 union gve_adminq_command *cmd_orig)
408 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
409 head = priv->adminq_prod_cnt;
411 // This is not a valid path
414 err = gve_adminq_issue_cmd(priv, cmd_orig);
418 return gve_adminq_kick_and_wait(priv);
421 /* The device specifies that the management vector can either be the first irq
422 * or the last irq. ntfy_blk_msix_base_idx indicates the first irq assigned to
423 * the ntfy blks. It if is 0 then the management vector is last, if it is 1 then
424 * the management vector is first.
426 * gve arranges the msix vectors so that the management vector is last.
428 #define GVE_NTFY_BLK_BASE_MSIX_IDX 0
429 int gve_adminq_configure_device_resources(struct gve_priv *priv,
430 dma_addr_t counter_array_bus_addr,
432 dma_addr_t db_array_bus_addr,
435 union gve_adminq_command cmd;
437 memset(&cmd, 0, sizeof(cmd));
438 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES);
439 cmd.configure_device_resources =
440 (struct gve_adminq_configure_device_resources) {
441 .counter_array = cpu_to_be64(counter_array_bus_addr),
442 .num_counters = cpu_to_be32(num_counters),
443 .irq_db_addr = cpu_to_be64(db_array_bus_addr),
444 .num_irq_dbs = cpu_to_be32(num_ntfy_blks),
445 .irq_db_stride = cpu_to_be32(sizeof(priv->ntfy_blocks[0])),
446 .ntfy_blk_msix_base_idx =
447 cpu_to_be32(GVE_NTFY_BLK_BASE_MSIX_IDX),
448 .queue_format = priv->queue_format,
451 return gve_adminq_execute_cmd(priv, &cmd);
454 int gve_adminq_deconfigure_device_resources(struct gve_priv *priv)
456 union gve_adminq_command cmd;
458 memset(&cmd, 0, sizeof(cmd));
459 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES);
461 return gve_adminq_execute_cmd(priv, &cmd);
464 static int gve_adminq_create_tx_queue(struct gve_priv *priv, u32 queue_index)
466 struct gve_tx_ring *tx = &priv->tx[queue_index];
467 union gve_adminq_command cmd;
469 memset(&cmd, 0, sizeof(cmd));
470 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CREATE_TX_QUEUE);
471 cmd.create_tx_queue = (struct gve_adminq_create_tx_queue) {
472 .queue_id = cpu_to_be32(queue_index),
473 .queue_resources_addr =
474 cpu_to_be64(tx->q_resources_bus),
475 .tx_ring_addr = cpu_to_be64(tx->bus),
476 .ntfy_id = cpu_to_be32(tx->ntfy_id),
479 if (gve_is_gqi(priv)) {
480 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ?
481 GVE_RAW_ADDRESSING_QPL_ID : tx->tx_fifo.qpl->id;
483 cmd.create_tx_queue.queue_page_list_id = cpu_to_be32(qpl_id);
485 cmd.create_tx_queue.tx_ring_size =
486 cpu_to_be16(priv->tx_desc_cnt);
487 cmd.create_tx_queue.tx_comp_ring_addr =
488 cpu_to_be64(tx->complq_bus_dqo);
489 cmd.create_tx_queue.tx_comp_ring_size =
490 cpu_to_be16(priv->options_dqo_rda.tx_comp_ring_entries);
493 return gve_adminq_issue_cmd(priv, &cmd);
496 int gve_adminq_create_tx_queues(struct gve_priv *priv, u32 num_queues)
501 for (i = 0; i < num_queues; i++) {
502 err = gve_adminq_create_tx_queue(priv, i);
507 return gve_adminq_kick_and_wait(priv);
510 static int gve_adminq_create_rx_queue(struct gve_priv *priv, u32 queue_index)
512 struct gve_rx_ring *rx = &priv->rx[queue_index];
513 union gve_adminq_command cmd;
515 memset(&cmd, 0, sizeof(cmd));
516 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CREATE_RX_QUEUE);
517 cmd.create_rx_queue = (struct gve_adminq_create_rx_queue) {
518 .queue_id = cpu_to_be32(queue_index),
519 .ntfy_id = cpu_to_be32(rx->ntfy_id),
520 .queue_resources_addr = cpu_to_be64(rx->q_resources_bus),
523 if (gve_is_gqi(priv)) {
524 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ?
525 GVE_RAW_ADDRESSING_QPL_ID : rx->data.qpl->id;
527 cmd.create_rx_queue.rx_desc_ring_addr =
528 cpu_to_be64(rx->desc.bus),
529 cmd.create_rx_queue.rx_data_ring_addr =
530 cpu_to_be64(rx->data.data_bus),
531 cmd.create_rx_queue.index = cpu_to_be32(queue_index);
532 cmd.create_rx_queue.queue_page_list_id = cpu_to_be32(qpl_id);
534 cmd.create_rx_queue.rx_ring_size =
535 cpu_to_be16(priv->rx_desc_cnt);
536 cmd.create_rx_queue.rx_desc_ring_addr =
537 cpu_to_be64(rx->dqo.complq.bus);
538 cmd.create_rx_queue.rx_data_ring_addr =
539 cpu_to_be64(rx->dqo.bufq.bus);
540 cmd.create_rx_queue.packet_buffer_size =
541 cpu_to_be16(priv->data_buffer_size_dqo);
542 cmd.create_rx_queue.rx_buff_ring_size =
543 cpu_to_be16(priv->options_dqo_rda.rx_buff_ring_entries);
544 cmd.create_rx_queue.enable_rsc =
545 !!(priv->dev->features & NETIF_F_LRO);
548 return gve_adminq_issue_cmd(priv, &cmd);
551 int gve_adminq_create_rx_queues(struct gve_priv *priv, u32 num_queues)
556 for (i = 0; i < num_queues; i++) {
557 err = gve_adminq_create_rx_queue(priv, i);
562 return gve_adminq_kick_and_wait(priv);
565 static int gve_adminq_destroy_tx_queue(struct gve_priv *priv, u32 queue_index)
567 union gve_adminq_command cmd;
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_TX_QUEUE);
572 cmd.destroy_tx_queue = (struct gve_adminq_destroy_tx_queue) {
573 .queue_id = cpu_to_be32(queue_index),
576 err = gve_adminq_issue_cmd(priv, &cmd);
583 int gve_adminq_destroy_tx_queues(struct gve_priv *priv, u32 num_queues)
588 for (i = 0; i < num_queues; i++) {
589 err = gve_adminq_destroy_tx_queue(priv, i);
594 return gve_adminq_kick_and_wait(priv);
597 static int gve_adminq_destroy_rx_queue(struct gve_priv *priv, u32 queue_index)
599 union gve_adminq_command cmd;
602 memset(&cmd, 0, sizeof(cmd));
603 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_RX_QUEUE);
604 cmd.destroy_rx_queue = (struct gve_adminq_destroy_rx_queue) {
605 .queue_id = cpu_to_be32(queue_index),
608 err = gve_adminq_issue_cmd(priv, &cmd);
615 int gve_adminq_destroy_rx_queues(struct gve_priv *priv, u32 num_queues)
620 for (i = 0; i < num_queues; i++) {
621 err = gve_adminq_destroy_rx_queue(priv, i);
626 return gve_adminq_kick_and_wait(priv);
629 static int gve_set_desc_cnt(struct gve_priv *priv,
630 struct gve_device_descriptor *descriptor)
632 priv->tx_desc_cnt = be16_to_cpu(descriptor->tx_queue_entries);
633 if (priv->tx_desc_cnt * sizeof(priv->tx->desc[0]) < PAGE_SIZE) {
634 dev_err(&priv->pdev->dev, "Tx desc count %d too low\n",
638 priv->rx_desc_cnt = be16_to_cpu(descriptor->rx_queue_entries);
639 if (priv->rx_desc_cnt * sizeof(priv->rx->desc.desc_ring[0])
641 dev_err(&priv->pdev->dev, "Rx desc count %d too low\n",
649 gve_set_desc_cnt_dqo(struct gve_priv *priv,
650 const struct gve_device_descriptor *descriptor,
651 const struct gve_device_option_dqo_rda *dev_op_dqo_rda)
653 priv->tx_desc_cnt = be16_to_cpu(descriptor->tx_queue_entries);
654 priv->options_dqo_rda.tx_comp_ring_entries =
655 be16_to_cpu(dev_op_dqo_rda->tx_comp_ring_entries);
656 priv->rx_desc_cnt = be16_to_cpu(descriptor->rx_queue_entries);
657 priv->options_dqo_rda.rx_buff_ring_entries =
658 be16_to_cpu(dev_op_dqo_rda->rx_buff_ring_entries);
663 int gve_adminq_describe_device(struct gve_priv *priv)
665 struct gve_device_option_gqi_rda *dev_op_gqi_rda = NULL;
666 struct gve_device_option_gqi_qpl *dev_op_gqi_qpl = NULL;
667 struct gve_device_option_dqo_rda *dev_op_dqo_rda = NULL;
668 struct gve_device_descriptor *descriptor;
669 union gve_adminq_command cmd;
670 dma_addr_t descriptor_bus;
675 memset(&cmd, 0, sizeof(cmd));
676 descriptor = dma_alloc_coherent(&priv->pdev->dev, PAGE_SIZE,
677 &descriptor_bus, GFP_KERNEL);
680 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESCRIBE_DEVICE);
681 cmd.describe_device.device_descriptor_addr =
682 cpu_to_be64(descriptor_bus);
683 cmd.describe_device.device_descriptor_version =
684 cpu_to_be32(GVE_ADMINQ_DEVICE_DESCRIPTOR_VERSION);
685 cmd.describe_device.available_length = cpu_to_be32(PAGE_SIZE);
687 err = gve_adminq_execute_cmd(priv, &cmd);
689 goto free_device_descriptor;
691 err = gve_process_device_options(priv, descriptor, &dev_op_gqi_rda,
692 &dev_op_gqi_qpl, &dev_op_dqo_rda);
694 goto free_device_descriptor;
696 /* If the GQI_RAW_ADDRESSING option is not enabled and the queue format
697 * is not set to GqiRda, choose the queue format in a priority order:
698 * DqoRda, GqiRda, GqiQpl. Use GqiQpl as default.
700 if (priv->queue_format == GVE_GQI_RDA_FORMAT) {
701 dev_info(&priv->pdev->dev,
702 "Driver is running with GQI RDA queue format.\n");
703 } else if (dev_op_dqo_rda) {
704 priv->queue_format = GVE_DQO_RDA_FORMAT;
705 dev_info(&priv->pdev->dev,
706 "Driver is running with DQO RDA queue format.\n");
707 } else if (dev_op_gqi_rda) {
708 priv->queue_format = GVE_GQI_RDA_FORMAT;
709 dev_info(&priv->pdev->dev,
710 "Driver is running with GQI RDA queue format.\n");
712 priv->queue_format = GVE_GQI_QPL_FORMAT;
713 dev_info(&priv->pdev->dev,
714 "Driver is running with GQI QPL queue format.\n");
716 if (gve_is_gqi(priv)) {
717 err = gve_set_desc_cnt(priv, descriptor);
719 /* DQO supports LRO. */
720 priv->dev->hw_features |= NETIF_F_LRO;
721 err = gve_set_desc_cnt_dqo(priv, descriptor, dev_op_dqo_rda);
724 goto free_device_descriptor;
726 priv->max_registered_pages =
727 be64_to_cpu(descriptor->max_registered_pages);
728 mtu = be16_to_cpu(descriptor->mtu);
729 if (mtu < ETH_MIN_MTU) {
730 dev_err(&priv->pdev->dev, "MTU %d below minimum MTU\n", mtu);
732 goto free_device_descriptor;
734 priv->dev->max_mtu = mtu;
735 priv->num_event_counters = be16_to_cpu(descriptor->counters);
736 eth_hw_addr_set(priv->dev, descriptor->mac);
737 mac = descriptor->mac;
738 dev_info(&priv->pdev->dev, "MAC addr: %pM\n", mac);
739 priv->tx_pages_per_qpl = be16_to_cpu(descriptor->tx_pages_per_qpl);
740 priv->rx_data_slot_cnt = be16_to_cpu(descriptor->rx_pages_per_qpl);
742 if (gve_is_gqi(priv) && priv->rx_data_slot_cnt < priv->rx_desc_cnt) {
743 dev_err(&priv->pdev->dev, "rx_data_slot_cnt cannot be smaller than rx_desc_cnt, setting rx_desc_cnt down to %d.\n",
744 priv->rx_data_slot_cnt);
745 priv->rx_desc_cnt = priv->rx_data_slot_cnt;
747 priv->default_num_queues = be16_to_cpu(descriptor->default_num_queues);
749 free_device_descriptor:
750 dma_free_coherent(&priv->pdev->dev, PAGE_SIZE, descriptor,
755 int gve_adminq_register_page_list(struct gve_priv *priv,
756 struct gve_queue_page_list *qpl)
758 struct device *hdev = &priv->pdev->dev;
759 u32 num_entries = qpl->num_entries;
760 u32 size = num_entries * sizeof(qpl->page_buses[0]);
761 union gve_adminq_command cmd;
762 dma_addr_t page_list_bus;
767 memset(&cmd, 0, sizeof(cmd));
768 page_list = dma_alloc_coherent(hdev, size, &page_list_bus, GFP_KERNEL);
772 for (i = 0; i < num_entries; i++)
773 page_list[i] = cpu_to_be64(qpl->page_buses[i]);
775 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REGISTER_PAGE_LIST);
776 cmd.reg_page_list = (struct gve_adminq_register_page_list) {
777 .page_list_id = cpu_to_be32(qpl->id),
778 .num_pages = cpu_to_be32(num_entries),
779 .page_address_list_addr = cpu_to_be64(page_list_bus),
782 err = gve_adminq_execute_cmd(priv, &cmd);
783 dma_free_coherent(hdev, size, page_list, page_list_bus);
787 int gve_adminq_unregister_page_list(struct gve_priv *priv, u32 page_list_id)
789 union gve_adminq_command cmd;
791 memset(&cmd, 0, sizeof(cmd));
792 cmd.opcode = cpu_to_be32(GVE_ADMINQ_UNREGISTER_PAGE_LIST);
793 cmd.unreg_page_list = (struct gve_adminq_unregister_page_list) {
794 .page_list_id = cpu_to_be32(page_list_id),
797 return gve_adminq_execute_cmd(priv, &cmd);
800 int gve_adminq_set_mtu(struct gve_priv *priv, u64 mtu)
802 union gve_adminq_command cmd;
804 memset(&cmd, 0, sizeof(cmd));
805 cmd.opcode = cpu_to_be32(GVE_ADMINQ_SET_DRIVER_PARAMETER);
806 cmd.set_driver_param = (struct gve_adminq_set_driver_parameter) {
807 .parameter_type = cpu_to_be32(GVE_SET_PARAM_MTU),
808 .parameter_value = cpu_to_be64(mtu),
811 return gve_adminq_execute_cmd(priv, &cmd);
814 int gve_adminq_report_stats(struct gve_priv *priv, u64 stats_report_len,
815 dma_addr_t stats_report_addr, u64 interval)
817 union gve_adminq_command cmd;
819 memset(&cmd, 0, sizeof(cmd));
820 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_STATS);
821 cmd.report_stats = (struct gve_adminq_report_stats) {
822 .stats_report_len = cpu_to_be64(stats_report_len),
823 .stats_report_addr = cpu_to_be64(stats_report_addr),
824 .interval = cpu_to_be64(interval),
827 return gve_adminq_execute_cmd(priv, &cmd);
830 int gve_adminq_report_link_speed(struct gve_priv *priv)
832 union gve_adminq_command gvnic_cmd;
833 dma_addr_t link_speed_region_bus;
834 __be64 *link_speed_region;
838 dma_alloc_coherent(&priv->pdev->dev, sizeof(*link_speed_region),
839 &link_speed_region_bus, GFP_KERNEL);
841 if (!link_speed_region)
844 memset(&gvnic_cmd, 0, sizeof(gvnic_cmd));
845 gvnic_cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_LINK_SPEED);
846 gvnic_cmd.report_link_speed.link_speed_address =
847 cpu_to_be64(link_speed_region_bus);
849 err = gve_adminq_execute_cmd(priv, &gvnic_cmd);
851 priv->link_speed = be64_to_cpu(*link_speed_region);
852 dma_free_coherent(&priv->pdev->dev, sizeof(*link_speed_region), link_speed_region,
853 link_speed_region_bus);
857 int gve_adminq_get_ptype_map_dqo(struct gve_priv *priv,
858 struct gve_ptype_lut *ptype_lut)
860 struct gve_ptype_map *ptype_map;
861 union gve_adminq_command cmd;
862 dma_addr_t ptype_map_bus;
866 memset(&cmd, 0, sizeof(cmd));
867 ptype_map = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ptype_map),
868 &ptype_map_bus, GFP_KERNEL);
872 cmd.opcode = cpu_to_be32(GVE_ADMINQ_GET_PTYPE_MAP);
873 cmd.get_ptype_map = (struct gve_adminq_get_ptype_map) {
874 .ptype_map_len = cpu_to_be64(sizeof(*ptype_map)),
875 .ptype_map_addr = cpu_to_be64(ptype_map_bus),
878 err = gve_adminq_execute_cmd(priv, &cmd);
882 /* Populate ptype_lut. */
883 for (i = 0; i < GVE_NUM_PTYPES; i++) {
884 ptype_lut->ptypes[i].l3_type =
885 ptype_map->ptypes[i].l3_type;
886 ptype_lut->ptypes[i].l4_type =
887 ptype_map->ptypes[i].l4_type;
890 dma_free_coherent(&priv->pdev->dev, sizeof(*ptype_map), ptype_map,