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,
42 struct gve_device_option_jumbo_frames **dev_op_jumbo_frames)
44 u32 req_feat_mask = be32_to_cpu(option->required_features_mask);
45 u16 option_length = be16_to_cpu(option->option_length);
46 u16 option_id = be16_to_cpu(option->option_id);
48 /* If the length or feature mask doesn't match, continue without
49 * enabling the feature.
52 case GVE_DEV_OPT_ID_GQI_RAW_ADDRESSING:
53 if (option_length != GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING ||
54 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING) {
55 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
57 GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING,
58 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING,
59 option_length, req_feat_mask);
63 dev_info(&priv->pdev->dev,
64 "Gqi raw addressing device option enabled.\n");
65 priv->queue_format = GVE_GQI_RDA_FORMAT;
67 case GVE_DEV_OPT_ID_GQI_RDA:
68 if (option_length < sizeof(**dev_op_gqi_rda) ||
69 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA) {
70 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
71 "GQI RDA", (int)sizeof(**dev_op_gqi_rda),
72 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA,
73 option_length, req_feat_mask);
77 if (option_length > sizeof(**dev_op_gqi_rda)) {
78 dev_warn(&priv->pdev->dev,
79 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI RDA");
81 *dev_op_gqi_rda = (void *)(option + 1);
83 case GVE_DEV_OPT_ID_GQI_QPL:
84 if (option_length < sizeof(**dev_op_gqi_qpl) ||
85 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL) {
86 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
87 "GQI QPL", (int)sizeof(**dev_op_gqi_qpl),
88 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL,
89 option_length, req_feat_mask);
93 if (option_length > sizeof(**dev_op_gqi_qpl)) {
94 dev_warn(&priv->pdev->dev,
95 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI QPL");
97 *dev_op_gqi_qpl = (void *)(option + 1);
99 case GVE_DEV_OPT_ID_DQO_RDA:
100 if (option_length < sizeof(**dev_op_dqo_rda) ||
101 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA) {
102 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
103 "DQO RDA", (int)sizeof(**dev_op_dqo_rda),
104 GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA,
105 option_length, req_feat_mask);
109 if (option_length > sizeof(**dev_op_dqo_rda)) {
110 dev_warn(&priv->pdev->dev,
111 GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO RDA");
113 *dev_op_dqo_rda = (void *)(option + 1);
115 case GVE_DEV_OPT_ID_JUMBO_FRAMES:
116 if (option_length < sizeof(**dev_op_jumbo_frames) ||
117 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_JUMBO_FRAMES) {
118 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT,
120 (int)sizeof(**dev_op_jumbo_frames),
121 GVE_DEV_OPT_REQ_FEAT_MASK_JUMBO_FRAMES,
122 option_length, req_feat_mask);
126 if (option_length > sizeof(**dev_op_jumbo_frames)) {
127 dev_warn(&priv->pdev->dev,
128 GVE_DEVICE_OPTION_TOO_BIG_FMT,
131 *dev_op_jumbo_frames = (void *)(option + 1);
134 /* If we don't recognize the option just continue
135 * without doing anything.
137 dev_dbg(&priv->pdev->dev, "Unrecognized device option 0x%hx not enabled.\n",
142 /* Process all device options for a given describe device call. */
144 gve_process_device_options(struct gve_priv *priv,
145 struct gve_device_descriptor *descriptor,
146 struct gve_device_option_gqi_rda **dev_op_gqi_rda,
147 struct gve_device_option_gqi_qpl **dev_op_gqi_qpl,
148 struct gve_device_option_dqo_rda **dev_op_dqo_rda,
149 struct gve_device_option_jumbo_frames **dev_op_jumbo_frames)
151 const int num_options = be16_to_cpu(descriptor->num_device_options);
152 struct gve_device_option *dev_opt;
155 /* The options struct directly follows the device descriptor. */
156 dev_opt = (void *)(descriptor + 1);
157 for (i = 0; i < num_options; i++) {
158 struct gve_device_option *next_opt;
160 next_opt = gve_get_next_option(descriptor, dev_opt);
162 dev_err(&priv->dev->dev,
163 "options exceed device_descriptor's total length.\n");
167 gve_parse_device_option(priv, descriptor, dev_opt,
168 dev_op_gqi_rda, dev_op_gqi_qpl,
169 dev_op_dqo_rda, dev_op_jumbo_frames);
176 int gve_adminq_alloc(struct device *dev, struct gve_priv *priv)
178 priv->adminq = dma_alloc_coherent(dev, PAGE_SIZE,
179 &priv->adminq_bus_addr, GFP_KERNEL);
180 if (unlikely(!priv->adminq))
183 priv->adminq_mask = (PAGE_SIZE / sizeof(union gve_adminq_command)) - 1;
184 priv->adminq_prod_cnt = 0;
185 priv->adminq_cmd_fail = 0;
186 priv->adminq_timeouts = 0;
187 priv->adminq_describe_device_cnt = 0;
188 priv->adminq_cfg_device_resources_cnt = 0;
189 priv->adminq_register_page_list_cnt = 0;
190 priv->adminq_unregister_page_list_cnt = 0;
191 priv->adminq_create_tx_queue_cnt = 0;
192 priv->adminq_create_rx_queue_cnt = 0;
193 priv->adminq_destroy_tx_queue_cnt = 0;
194 priv->adminq_destroy_rx_queue_cnt = 0;
195 priv->adminq_dcfg_device_resources_cnt = 0;
196 priv->adminq_set_driver_parameter_cnt = 0;
197 priv->adminq_report_stats_cnt = 0;
198 priv->adminq_report_link_speed_cnt = 0;
199 priv->adminq_get_ptype_map_cnt = 0;
201 /* Setup Admin queue with the device */
202 iowrite32be(priv->adminq_bus_addr / PAGE_SIZE,
203 &priv->reg_bar0->adminq_pfn);
205 gve_set_admin_queue_ok(priv);
209 void gve_adminq_release(struct gve_priv *priv)
213 /* Tell the device the adminq is leaving */
214 iowrite32be(0x0, &priv->reg_bar0->adminq_pfn);
215 while (ioread32be(&priv->reg_bar0->adminq_pfn)) {
216 /* If this is reached the device is unrecoverable and still
217 * holding memory. Continue looping to avoid memory corruption,
218 * but WARN so it is visible what is going on.
220 if (i == GVE_MAX_ADMINQ_RELEASE_CHECK)
221 WARN(1, "Unrecoverable platform error!");
223 msleep(GVE_ADMINQ_SLEEP_LEN);
225 gve_clear_device_rings_ok(priv);
226 gve_clear_device_resources_ok(priv);
227 gve_clear_admin_queue_ok(priv);
230 void gve_adminq_free(struct device *dev, struct gve_priv *priv)
232 if (!gve_get_admin_queue_ok(priv))
234 gve_adminq_release(priv);
235 dma_free_coherent(dev, PAGE_SIZE, priv->adminq, priv->adminq_bus_addr);
236 gve_clear_admin_queue_ok(priv);
239 static void gve_adminq_kick_cmd(struct gve_priv *priv, u32 prod_cnt)
241 iowrite32be(prod_cnt, &priv->reg_bar0->adminq_doorbell);
244 static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt)
248 for (i = 0; i < GVE_MAX_ADMINQ_EVENT_COUNTER_CHECK; i++) {
249 if (ioread32be(&priv->reg_bar0->adminq_event_counter)
252 msleep(GVE_ADMINQ_SLEEP_LEN);
258 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status)
260 if (status != GVE_ADMINQ_COMMAND_PASSED &&
261 status != GVE_ADMINQ_COMMAND_UNSET) {
262 dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status);
263 priv->adminq_cmd_fail++;
266 case GVE_ADMINQ_COMMAND_PASSED:
268 case GVE_ADMINQ_COMMAND_UNSET:
269 dev_err(&priv->pdev->dev, "parse_aq_err: err and status both unset, this should not be possible.\n");
271 case GVE_ADMINQ_COMMAND_ERROR_ABORTED:
272 case GVE_ADMINQ_COMMAND_ERROR_CANCELLED:
273 case GVE_ADMINQ_COMMAND_ERROR_DATALOSS:
274 case GVE_ADMINQ_COMMAND_ERROR_FAILED_PRECONDITION:
275 case GVE_ADMINQ_COMMAND_ERROR_UNAVAILABLE:
277 case GVE_ADMINQ_COMMAND_ERROR_ALREADY_EXISTS:
278 case GVE_ADMINQ_COMMAND_ERROR_INTERNAL_ERROR:
279 case GVE_ADMINQ_COMMAND_ERROR_INVALID_ARGUMENT:
280 case GVE_ADMINQ_COMMAND_ERROR_NOT_FOUND:
281 case GVE_ADMINQ_COMMAND_ERROR_OUT_OF_RANGE:
282 case GVE_ADMINQ_COMMAND_ERROR_UNKNOWN_ERROR:
284 case GVE_ADMINQ_COMMAND_ERROR_DEADLINE_EXCEEDED:
286 case GVE_ADMINQ_COMMAND_ERROR_PERMISSION_DENIED:
287 case GVE_ADMINQ_COMMAND_ERROR_UNAUTHENTICATED:
289 case GVE_ADMINQ_COMMAND_ERROR_RESOURCE_EXHAUSTED:
291 case GVE_ADMINQ_COMMAND_ERROR_UNIMPLEMENTED:
294 dev_err(&priv->pdev->dev, "parse_aq_err: unknown status code %d\n", status);
299 /* Flushes all AQ commands currently queued and waits for them to complete.
300 * If there are failures, it will return the first error.
302 static int gve_adminq_kick_and_wait(struct gve_priv *priv)
307 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
308 head = priv->adminq_prod_cnt;
310 gve_adminq_kick_cmd(priv, head);
311 if (!gve_adminq_wait_for_cmd(priv, head)) {
312 dev_err(&priv->pdev->dev, "AQ commands timed out, need to reset AQ\n");
313 priv->adminq_timeouts++;
314 return -ENOTRECOVERABLE;
317 for (i = tail; i < head; i++) {
318 union gve_adminq_command *cmd;
321 cmd = &priv->adminq[i & priv->adminq_mask];
322 status = be32_to_cpu(READ_ONCE(cmd->status));
323 err = gve_adminq_parse_err(priv, status);
325 // Return the first error if we failed.
332 /* This function is not threadsafe - the caller is responsible for any
335 static int gve_adminq_issue_cmd(struct gve_priv *priv,
336 union gve_adminq_command *cmd_orig)
338 union gve_adminq_command *cmd;
342 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
344 // Check if next command will overflow the buffer.
345 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) ==
346 (tail & priv->adminq_mask)) {
349 // Flush existing commands to make room.
350 err = gve_adminq_kick_and_wait(priv);
355 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
356 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) ==
357 (tail & priv->adminq_mask)) {
358 // This should never happen. We just flushed the
359 // command queue so there should be enough space.
364 cmd = &priv->adminq[priv->adminq_prod_cnt & priv->adminq_mask];
365 priv->adminq_prod_cnt++;
367 memcpy(cmd, cmd_orig, sizeof(*cmd_orig));
368 opcode = be32_to_cpu(READ_ONCE(cmd->opcode));
371 case GVE_ADMINQ_DESCRIBE_DEVICE:
372 priv->adminq_describe_device_cnt++;
374 case GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES:
375 priv->adminq_cfg_device_resources_cnt++;
377 case GVE_ADMINQ_REGISTER_PAGE_LIST:
378 priv->adminq_register_page_list_cnt++;
380 case GVE_ADMINQ_UNREGISTER_PAGE_LIST:
381 priv->adminq_unregister_page_list_cnt++;
383 case GVE_ADMINQ_CREATE_TX_QUEUE:
384 priv->adminq_create_tx_queue_cnt++;
386 case GVE_ADMINQ_CREATE_RX_QUEUE:
387 priv->adminq_create_rx_queue_cnt++;
389 case GVE_ADMINQ_DESTROY_TX_QUEUE:
390 priv->adminq_destroy_tx_queue_cnt++;
392 case GVE_ADMINQ_DESTROY_RX_QUEUE:
393 priv->adminq_destroy_rx_queue_cnt++;
395 case GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES:
396 priv->adminq_dcfg_device_resources_cnt++;
398 case GVE_ADMINQ_SET_DRIVER_PARAMETER:
399 priv->adminq_set_driver_parameter_cnt++;
401 case GVE_ADMINQ_REPORT_STATS:
402 priv->adminq_report_stats_cnt++;
404 case GVE_ADMINQ_REPORT_LINK_SPEED:
405 priv->adminq_report_link_speed_cnt++;
407 case GVE_ADMINQ_GET_PTYPE_MAP:
408 priv->adminq_get_ptype_map_cnt++;
411 dev_err(&priv->pdev->dev, "unknown AQ command opcode %d\n", opcode);
417 /* This function is not threadsafe - the caller is responsible for any
419 * The caller is also responsible for making sure there are no commands
420 * waiting to be executed.
422 static int gve_adminq_execute_cmd(struct gve_priv *priv,
423 union gve_adminq_command *cmd_orig)
428 tail = ioread32be(&priv->reg_bar0->adminq_event_counter);
429 head = priv->adminq_prod_cnt;
431 // This is not a valid path
434 err = gve_adminq_issue_cmd(priv, cmd_orig);
438 return gve_adminq_kick_and_wait(priv);
441 /* The device specifies that the management vector can either be the first irq
442 * or the last irq. ntfy_blk_msix_base_idx indicates the first irq assigned to
443 * the ntfy blks. It if is 0 then the management vector is last, if it is 1 then
444 * the management vector is first.
446 * gve arranges the msix vectors so that the management vector is last.
448 #define GVE_NTFY_BLK_BASE_MSIX_IDX 0
449 int gve_adminq_configure_device_resources(struct gve_priv *priv,
450 dma_addr_t counter_array_bus_addr,
452 dma_addr_t db_array_bus_addr,
455 union gve_adminq_command cmd;
457 memset(&cmd, 0, sizeof(cmd));
458 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES);
459 cmd.configure_device_resources =
460 (struct gve_adminq_configure_device_resources) {
461 .counter_array = cpu_to_be64(counter_array_bus_addr),
462 .num_counters = cpu_to_be32(num_counters),
463 .irq_db_addr = cpu_to_be64(db_array_bus_addr),
464 .num_irq_dbs = cpu_to_be32(num_ntfy_blks),
465 .irq_db_stride = cpu_to_be32(sizeof(*priv->irq_db_indices)),
466 .ntfy_blk_msix_base_idx =
467 cpu_to_be32(GVE_NTFY_BLK_BASE_MSIX_IDX),
468 .queue_format = priv->queue_format,
471 return gve_adminq_execute_cmd(priv, &cmd);
474 int gve_adminq_deconfigure_device_resources(struct gve_priv *priv)
476 union gve_adminq_command cmd;
478 memset(&cmd, 0, sizeof(cmd));
479 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES);
481 return gve_adminq_execute_cmd(priv, &cmd);
484 static int gve_adminq_create_tx_queue(struct gve_priv *priv, u32 queue_index)
486 struct gve_tx_ring *tx = &priv->tx[queue_index];
487 union gve_adminq_command cmd;
489 memset(&cmd, 0, sizeof(cmd));
490 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CREATE_TX_QUEUE);
491 cmd.create_tx_queue = (struct gve_adminq_create_tx_queue) {
492 .queue_id = cpu_to_be32(queue_index),
493 .queue_resources_addr =
494 cpu_to_be64(tx->q_resources_bus),
495 .tx_ring_addr = cpu_to_be64(tx->bus),
496 .ntfy_id = cpu_to_be32(tx->ntfy_id),
499 if (gve_is_gqi(priv)) {
500 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ?
501 GVE_RAW_ADDRESSING_QPL_ID : tx->tx_fifo.qpl->id;
503 cmd.create_tx_queue.queue_page_list_id = cpu_to_be32(qpl_id);
505 cmd.create_tx_queue.tx_ring_size =
506 cpu_to_be16(priv->tx_desc_cnt);
507 cmd.create_tx_queue.tx_comp_ring_addr =
508 cpu_to_be64(tx->complq_bus_dqo);
509 cmd.create_tx_queue.tx_comp_ring_size =
510 cpu_to_be16(priv->options_dqo_rda.tx_comp_ring_entries);
513 return gve_adminq_issue_cmd(priv, &cmd);
516 int gve_adminq_create_tx_queues(struct gve_priv *priv, u32 num_queues)
521 for (i = 0; i < num_queues; i++) {
522 err = gve_adminq_create_tx_queue(priv, i);
527 return gve_adminq_kick_and_wait(priv);
530 static int gve_adminq_create_rx_queue(struct gve_priv *priv, u32 queue_index)
532 struct gve_rx_ring *rx = &priv->rx[queue_index];
533 union gve_adminq_command cmd;
535 memset(&cmd, 0, sizeof(cmd));
536 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CREATE_RX_QUEUE);
537 cmd.create_rx_queue = (struct gve_adminq_create_rx_queue) {
538 .queue_id = cpu_to_be32(queue_index),
539 .ntfy_id = cpu_to_be32(rx->ntfy_id),
540 .queue_resources_addr = cpu_to_be64(rx->q_resources_bus),
543 if (gve_is_gqi(priv)) {
544 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ?
545 GVE_RAW_ADDRESSING_QPL_ID : rx->data.qpl->id;
547 cmd.create_rx_queue.rx_desc_ring_addr =
548 cpu_to_be64(rx->desc.bus),
549 cmd.create_rx_queue.rx_data_ring_addr =
550 cpu_to_be64(rx->data.data_bus),
551 cmd.create_rx_queue.index = cpu_to_be32(queue_index);
552 cmd.create_rx_queue.queue_page_list_id = cpu_to_be32(qpl_id);
553 cmd.create_rx_queue.packet_buffer_size = cpu_to_be16(rx->packet_buffer_size);
555 cmd.create_rx_queue.rx_ring_size =
556 cpu_to_be16(priv->rx_desc_cnt);
557 cmd.create_rx_queue.rx_desc_ring_addr =
558 cpu_to_be64(rx->dqo.complq.bus);
559 cmd.create_rx_queue.rx_data_ring_addr =
560 cpu_to_be64(rx->dqo.bufq.bus);
561 cmd.create_rx_queue.packet_buffer_size =
562 cpu_to_be16(priv->data_buffer_size_dqo);
563 cmd.create_rx_queue.rx_buff_ring_size =
564 cpu_to_be16(priv->options_dqo_rda.rx_buff_ring_entries);
565 cmd.create_rx_queue.enable_rsc =
566 !!(priv->dev->features & NETIF_F_LRO);
569 return gve_adminq_issue_cmd(priv, &cmd);
572 int gve_adminq_create_rx_queues(struct gve_priv *priv, u32 num_queues)
577 for (i = 0; i < num_queues; i++) {
578 err = gve_adminq_create_rx_queue(priv, i);
583 return gve_adminq_kick_and_wait(priv);
586 static int gve_adminq_destroy_tx_queue(struct gve_priv *priv, u32 queue_index)
588 union gve_adminq_command cmd;
591 memset(&cmd, 0, sizeof(cmd));
592 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_TX_QUEUE);
593 cmd.destroy_tx_queue = (struct gve_adminq_destroy_tx_queue) {
594 .queue_id = cpu_to_be32(queue_index),
597 err = gve_adminq_issue_cmd(priv, &cmd);
604 int gve_adminq_destroy_tx_queues(struct gve_priv *priv, u32 num_queues)
609 for (i = 0; i < num_queues; i++) {
610 err = gve_adminq_destroy_tx_queue(priv, i);
615 return gve_adminq_kick_and_wait(priv);
618 static int gve_adminq_destroy_rx_queue(struct gve_priv *priv, u32 queue_index)
620 union gve_adminq_command cmd;
623 memset(&cmd, 0, sizeof(cmd));
624 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_RX_QUEUE);
625 cmd.destroy_rx_queue = (struct gve_adminq_destroy_rx_queue) {
626 .queue_id = cpu_to_be32(queue_index),
629 err = gve_adminq_issue_cmd(priv, &cmd);
636 int gve_adminq_destroy_rx_queues(struct gve_priv *priv, u32 num_queues)
641 for (i = 0; i < num_queues; i++) {
642 err = gve_adminq_destroy_rx_queue(priv, i);
647 return gve_adminq_kick_and_wait(priv);
650 static int gve_set_desc_cnt(struct gve_priv *priv,
651 struct gve_device_descriptor *descriptor)
653 priv->tx_desc_cnt = be16_to_cpu(descriptor->tx_queue_entries);
654 if (priv->tx_desc_cnt * sizeof(priv->tx->desc[0]) < PAGE_SIZE) {
655 dev_err(&priv->pdev->dev, "Tx desc count %d too low\n",
659 priv->rx_desc_cnt = be16_to_cpu(descriptor->rx_queue_entries);
660 if (priv->rx_desc_cnt * sizeof(priv->rx->desc.desc_ring[0])
662 dev_err(&priv->pdev->dev, "Rx desc count %d too low\n",
670 gve_set_desc_cnt_dqo(struct gve_priv *priv,
671 const struct gve_device_descriptor *descriptor,
672 const struct gve_device_option_dqo_rda *dev_op_dqo_rda)
674 priv->tx_desc_cnt = be16_to_cpu(descriptor->tx_queue_entries);
675 priv->options_dqo_rda.tx_comp_ring_entries =
676 be16_to_cpu(dev_op_dqo_rda->tx_comp_ring_entries);
677 priv->rx_desc_cnt = be16_to_cpu(descriptor->rx_queue_entries);
678 priv->options_dqo_rda.rx_buff_ring_entries =
679 be16_to_cpu(dev_op_dqo_rda->rx_buff_ring_entries);
684 static void gve_enable_supported_features(struct gve_priv *priv,
685 u32 supported_features_mask,
686 const struct gve_device_option_jumbo_frames
687 *dev_op_jumbo_frames)
689 /* Before control reaches this point, the page-size-capped max MTU from
690 * the gve_device_descriptor field has already been stored in
691 * priv->dev->max_mtu. We overwrite it with the true max MTU below.
693 if (dev_op_jumbo_frames &&
694 (supported_features_mask & GVE_SUP_JUMBO_FRAMES_MASK)) {
695 dev_info(&priv->pdev->dev,
696 "JUMBO FRAMES device option enabled.\n");
697 priv->dev->max_mtu = be16_to_cpu(dev_op_jumbo_frames->max_mtu);
701 int gve_adminq_describe_device(struct gve_priv *priv)
703 struct gve_device_option_jumbo_frames *dev_op_jumbo_frames = NULL;
704 struct gve_device_option_gqi_rda *dev_op_gqi_rda = NULL;
705 struct gve_device_option_gqi_qpl *dev_op_gqi_qpl = NULL;
706 struct gve_device_option_dqo_rda *dev_op_dqo_rda = NULL;
707 struct gve_device_descriptor *descriptor;
708 u32 supported_features_mask = 0;
709 union gve_adminq_command cmd;
710 dma_addr_t descriptor_bus;
715 memset(&cmd, 0, sizeof(cmd));
716 descriptor = dma_alloc_coherent(&priv->pdev->dev, PAGE_SIZE,
717 &descriptor_bus, GFP_KERNEL);
720 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESCRIBE_DEVICE);
721 cmd.describe_device.device_descriptor_addr =
722 cpu_to_be64(descriptor_bus);
723 cmd.describe_device.device_descriptor_version =
724 cpu_to_be32(GVE_ADMINQ_DEVICE_DESCRIPTOR_VERSION);
725 cmd.describe_device.available_length = cpu_to_be32(PAGE_SIZE);
727 err = gve_adminq_execute_cmd(priv, &cmd);
729 goto free_device_descriptor;
731 err = gve_process_device_options(priv, descriptor, &dev_op_gqi_rda,
732 &dev_op_gqi_qpl, &dev_op_dqo_rda,
733 &dev_op_jumbo_frames);
735 goto free_device_descriptor;
737 /* If the GQI_RAW_ADDRESSING option is not enabled and the queue format
738 * is not set to GqiRda, choose the queue format in a priority order:
739 * DqoRda, GqiRda, GqiQpl. Use GqiQpl as default.
741 if (dev_op_dqo_rda) {
742 priv->queue_format = GVE_DQO_RDA_FORMAT;
743 dev_info(&priv->pdev->dev,
744 "Driver is running with DQO RDA queue format.\n");
745 supported_features_mask =
746 be32_to_cpu(dev_op_dqo_rda->supported_features_mask);
747 } else if (dev_op_gqi_rda) {
748 priv->queue_format = GVE_GQI_RDA_FORMAT;
749 dev_info(&priv->pdev->dev,
750 "Driver is running with GQI RDA queue format.\n");
751 supported_features_mask =
752 be32_to_cpu(dev_op_gqi_rda->supported_features_mask);
753 } else if (priv->queue_format == GVE_GQI_RDA_FORMAT) {
754 dev_info(&priv->pdev->dev,
755 "Driver is running with GQI RDA queue format.\n");
757 priv->queue_format = GVE_GQI_QPL_FORMAT;
759 supported_features_mask =
760 be32_to_cpu(dev_op_gqi_qpl->supported_features_mask);
761 dev_info(&priv->pdev->dev,
762 "Driver is running with GQI QPL queue format.\n");
764 if (gve_is_gqi(priv)) {
765 err = gve_set_desc_cnt(priv, descriptor);
767 /* DQO supports LRO. */
768 priv->dev->hw_features |= NETIF_F_LRO;
769 err = gve_set_desc_cnt_dqo(priv, descriptor, dev_op_dqo_rda);
772 goto free_device_descriptor;
774 priv->max_registered_pages =
775 be64_to_cpu(descriptor->max_registered_pages);
776 mtu = be16_to_cpu(descriptor->mtu);
777 if (mtu < ETH_MIN_MTU) {
778 dev_err(&priv->pdev->dev, "MTU %d below minimum MTU\n", mtu);
780 goto free_device_descriptor;
782 priv->dev->max_mtu = mtu;
783 priv->num_event_counters = be16_to_cpu(descriptor->counters);
784 eth_hw_addr_set(priv->dev, descriptor->mac);
785 mac = descriptor->mac;
786 dev_info(&priv->pdev->dev, "MAC addr: %pM\n", mac);
787 priv->tx_pages_per_qpl = be16_to_cpu(descriptor->tx_pages_per_qpl);
788 priv->rx_data_slot_cnt = be16_to_cpu(descriptor->rx_pages_per_qpl);
790 if (gve_is_gqi(priv) && priv->rx_data_slot_cnt < priv->rx_desc_cnt) {
791 dev_err(&priv->pdev->dev, "rx_data_slot_cnt cannot be smaller than rx_desc_cnt, setting rx_desc_cnt down to %d.\n",
792 priv->rx_data_slot_cnt);
793 priv->rx_desc_cnt = priv->rx_data_slot_cnt;
795 priv->default_num_queues = be16_to_cpu(descriptor->default_num_queues);
797 gve_enable_supported_features(priv, supported_features_mask,
798 dev_op_jumbo_frames);
800 free_device_descriptor:
801 dma_free_coherent(&priv->pdev->dev, PAGE_SIZE, descriptor,
806 int gve_adminq_register_page_list(struct gve_priv *priv,
807 struct gve_queue_page_list *qpl)
809 struct device *hdev = &priv->pdev->dev;
810 u32 num_entries = qpl->num_entries;
811 u32 size = num_entries * sizeof(qpl->page_buses[0]);
812 union gve_adminq_command cmd;
813 dma_addr_t page_list_bus;
818 memset(&cmd, 0, sizeof(cmd));
819 page_list = dma_alloc_coherent(hdev, size, &page_list_bus, GFP_KERNEL);
823 for (i = 0; i < num_entries; i++)
824 page_list[i] = cpu_to_be64(qpl->page_buses[i]);
826 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REGISTER_PAGE_LIST);
827 cmd.reg_page_list = (struct gve_adminq_register_page_list) {
828 .page_list_id = cpu_to_be32(qpl->id),
829 .num_pages = cpu_to_be32(num_entries),
830 .page_address_list_addr = cpu_to_be64(page_list_bus),
833 err = gve_adminq_execute_cmd(priv, &cmd);
834 dma_free_coherent(hdev, size, page_list, page_list_bus);
838 int gve_adminq_unregister_page_list(struct gve_priv *priv, u32 page_list_id)
840 union gve_adminq_command cmd;
842 memset(&cmd, 0, sizeof(cmd));
843 cmd.opcode = cpu_to_be32(GVE_ADMINQ_UNREGISTER_PAGE_LIST);
844 cmd.unreg_page_list = (struct gve_adminq_unregister_page_list) {
845 .page_list_id = cpu_to_be32(page_list_id),
848 return gve_adminq_execute_cmd(priv, &cmd);
851 int gve_adminq_set_mtu(struct gve_priv *priv, u64 mtu)
853 union gve_adminq_command cmd;
855 memset(&cmd, 0, sizeof(cmd));
856 cmd.opcode = cpu_to_be32(GVE_ADMINQ_SET_DRIVER_PARAMETER);
857 cmd.set_driver_param = (struct gve_adminq_set_driver_parameter) {
858 .parameter_type = cpu_to_be32(GVE_SET_PARAM_MTU),
859 .parameter_value = cpu_to_be64(mtu),
862 return gve_adminq_execute_cmd(priv, &cmd);
865 int gve_adminq_report_stats(struct gve_priv *priv, u64 stats_report_len,
866 dma_addr_t stats_report_addr, u64 interval)
868 union gve_adminq_command cmd;
870 memset(&cmd, 0, sizeof(cmd));
871 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_STATS);
872 cmd.report_stats = (struct gve_adminq_report_stats) {
873 .stats_report_len = cpu_to_be64(stats_report_len),
874 .stats_report_addr = cpu_to_be64(stats_report_addr),
875 .interval = cpu_to_be64(interval),
878 return gve_adminq_execute_cmd(priv, &cmd);
881 int gve_adminq_report_link_speed(struct gve_priv *priv)
883 union gve_adminq_command gvnic_cmd;
884 dma_addr_t link_speed_region_bus;
885 __be64 *link_speed_region;
889 dma_alloc_coherent(&priv->pdev->dev, sizeof(*link_speed_region),
890 &link_speed_region_bus, GFP_KERNEL);
892 if (!link_speed_region)
895 memset(&gvnic_cmd, 0, sizeof(gvnic_cmd));
896 gvnic_cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_LINK_SPEED);
897 gvnic_cmd.report_link_speed.link_speed_address =
898 cpu_to_be64(link_speed_region_bus);
900 err = gve_adminq_execute_cmd(priv, &gvnic_cmd);
902 priv->link_speed = be64_to_cpu(*link_speed_region);
903 dma_free_coherent(&priv->pdev->dev, sizeof(*link_speed_region), link_speed_region,
904 link_speed_region_bus);
908 int gve_adminq_get_ptype_map_dqo(struct gve_priv *priv,
909 struct gve_ptype_lut *ptype_lut)
911 struct gve_ptype_map *ptype_map;
912 union gve_adminq_command cmd;
913 dma_addr_t ptype_map_bus;
917 memset(&cmd, 0, sizeof(cmd));
918 ptype_map = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ptype_map),
919 &ptype_map_bus, GFP_KERNEL);
923 cmd.opcode = cpu_to_be32(GVE_ADMINQ_GET_PTYPE_MAP);
924 cmd.get_ptype_map = (struct gve_adminq_get_ptype_map) {
925 .ptype_map_len = cpu_to_be64(sizeof(*ptype_map)),
926 .ptype_map_addr = cpu_to_be64(ptype_map_bus),
929 err = gve_adminq_execute_cmd(priv, &cmd);
933 /* Populate ptype_lut. */
934 for (i = 0; i < GVE_NUM_PTYPES; i++) {
935 ptype_lut->ptypes[i].l3_type =
936 ptype_map->ptypes[i].l3_type;
937 ptype_lut->ptypes[i].l4_type =
938 ptype_map->ptypes[i].l4_type;
941 dma_free_coherent(&priv->pdev->dev, sizeof(*ptype_map), ptype_map,