1 // SPDX-License-Identifier: (GPL-2.0 OR MIT) 2 /* Google virtual Ethernet (gve) driver 3 * 4 * Copyright (C) 2015-2021 Google, Inc. 5 */ 6 7 #include <linux/etherdevice.h> 8 #include <linux/pci.h> 9 #include "gve.h" 10 #include "gve_adminq.h" 11 #include "gve_register.h" 12 13 #define GVE_MAX_ADMINQ_RELEASE_CHECK 500 14 #define GVE_ADMINQ_SLEEP_LEN 20 15 #define GVE_MAX_ADMINQ_EVENT_COUNTER_CHECK 100 16 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" 20 21 #define GVE_DEVICE_OPTION_TOO_BIG_FMT "Length of %s option larger than expected. Possible older version of guest driver.\n" 22 23 static 24 struct gve_device_option *gve_get_next_option(struct gve_device_descriptor *descriptor, 25 struct gve_device_option *option) 26 { 27 void *option_end, *descriptor_end; 28 29 option_end = (void *)(option + 1) + be16_to_cpu(option->option_length); 30 descriptor_end = (void *)descriptor + be16_to_cpu(descriptor->total_length); 31 32 return option_end > descriptor_end ? NULL : (struct gve_device_option *)option_end; 33 } 34 35 #define GVE_DEVICE_OPTION_NO_MIN_RING_SIZE 8 36 37 static 38 void gve_parse_device_option(struct gve_priv *priv, 39 struct gve_device_descriptor *device_descriptor, 40 struct gve_device_option *option, 41 struct gve_device_option_gqi_rda **dev_op_gqi_rda, 42 struct gve_device_option_gqi_qpl **dev_op_gqi_qpl, 43 struct gve_device_option_dqo_rda **dev_op_dqo_rda, 44 struct gve_device_option_jumbo_frames **dev_op_jumbo_frames, 45 struct gve_device_option_dqo_qpl **dev_op_dqo_qpl, 46 struct gve_device_option_buffer_sizes **dev_op_buffer_sizes, 47 struct gve_device_option_flow_steering **dev_op_flow_steering, 48 struct gve_device_option_rss_config **dev_op_rss_config, 49 struct gve_device_option_nic_timestamp **dev_op_nic_timestamp, 50 struct gve_device_option_modify_ring **dev_op_modify_ring) 51 { 52 u32 req_feat_mask = be32_to_cpu(option->required_features_mask); 53 u16 option_length = be16_to_cpu(option->option_length); 54 u16 option_id = be16_to_cpu(option->option_id); 55 56 /* If the length or feature mask doesn't match, continue without 57 * enabling the feature. 58 */ 59 switch (option_id) { 60 case GVE_DEV_OPT_ID_GQI_RAW_ADDRESSING: 61 if (option_length != GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING || 62 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING) { 63 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 64 "Raw Addressing", 65 GVE_DEV_OPT_LEN_GQI_RAW_ADDRESSING, 66 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING, 67 option_length, req_feat_mask); 68 break; 69 } 70 71 dev_info(&priv->pdev->dev, 72 "Gqi raw addressing device option enabled.\n"); 73 priv->queue_format = GVE_GQI_RDA_FORMAT; 74 break; 75 case GVE_DEV_OPT_ID_GQI_RDA: 76 if (option_length < sizeof(**dev_op_gqi_rda) || 77 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA) { 78 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 79 "GQI RDA", (int)sizeof(**dev_op_gqi_rda), 80 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA, 81 option_length, req_feat_mask); 82 break; 83 } 84 85 if (option_length > sizeof(**dev_op_gqi_rda)) { 86 dev_warn(&priv->pdev->dev, 87 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI RDA"); 88 } 89 *dev_op_gqi_rda = (void *)(option + 1); 90 break; 91 case GVE_DEV_OPT_ID_GQI_QPL: 92 if (option_length < sizeof(**dev_op_gqi_qpl) || 93 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL) { 94 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 95 "GQI QPL", (int)sizeof(**dev_op_gqi_qpl), 96 GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL, 97 option_length, req_feat_mask); 98 break; 99 } 100 101 if (option_length > sizeof(**dev_op_gqi_qpl)) { 102 dev_warn(&priv->pdev->dev, 103 GVE_DEVICE_OPTION_TOO_BIG_FMT, "GQI QPL"); 104 } 105 *dev_op_gqi_qpl = (void *)(option + 1); 106 break; 107 case GVE_DEV_OPT_ID_DQO_RDA: 108 if (option_length < sizeof(**dev_op_dqo_rda) || 109 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA) { 110 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 111 "DQO RDA", (int)sizeof(**dev_op_dqo_rda), 112 GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA, 113 option_length, req_feat_mask); 114 break; 115 } 116 117 if (option_length > sizeof(**dev_op_dqo_rda)) { 118 dev_warn(&priv->pdev->dev, 119 GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO RDA"); 120 } 121 *dev_op_dqo_rda = (void *)(option + 1); 122 break; 123 case GVE_DEV_OPT_ID_DQO_QPL: 124 if (option_length < sizeof(**dev_op_dqo_qpl) || 125 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_DQO_QPL) { 126 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 127 "DQO QPL", (int)sizeof(**dev_op_dqo_qpl), 128 GVE_DEV_OPT_REQ_FEAT_MASK_DQO_QPL, 129 option_length, req_feat_mask); 130 break; 131 } 132 133 if (option_length > sizeof(**dev_op_dqo_qpl)) { 134 dev_warn(&priv->pdev->dev, 135 GVE_DEVICE_OPTION_TOO_BIG_FMT, "DQO QPL"); 136 } 137 *dev_op_dqo_qpl = (void *)(option + 1); 138 break; 139 case GVE_DEV_OPT_ID_JUMBO_FRAMES: 140 if (option_length < sizeof(**dev_op_jumbo_frames) || 141 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_JUMBO_FRAMES) { 142 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 143 "Jumbo Frames", 144 (int)sizeof(**dev_op_jumbo_frames), 145 GVE_DEV_OPT_REQ_FEAT_MASK_JUMBO_FRAMES, 146 option_length, req_feat_mask); 147 break; 148 } 149 150 if (option_length > sizeof(**dev_op_jumbo_frames)) { 151 dev_warn(&priv->pdev->dev, 152 GVE_DEVICE_OPTION_TOO_BIG_FMT, 153 "Jumbo Frames"); 154 } 155 *dev_op_jumbo_frames = (void *)(option + 1); 156 break; 157 case GVE_DEV_OPT_ID_BUFFER_SIZES: 158 if (option_length < sizeof(**dev_op_buffer_sizes) || 159 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_BUFFER_SIZES) { 160 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 161 "Buffer Sizes", 162 (int)sizeof(**dev_op_buffer_sizes), 163 GVE_DEV_OPT_REQ_FEAT_MASK_BUFFER_SIZES, 164 option_length, req_feat_mask); 165 break; 166 } 167 168 if (option_length > sizeof(**dev_op_buffer_sizes)) 169 dev_warn(&priv->pdev->dev, 170 GVE_DEVICE_OPTION_TOO_BIG_FMT, 171 "Buffer Sizes"); 172 *dev_op_buffer_sizes = (void *)(option + 1); 173 break; 174 case GVE_DEV_OPT_ID_MODIFY_RING: 175 if (option_length < GVE_DEVICE_OPTION_NO_MIN_RING_SIZE || 176 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_MODIFY_RING) { 177 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 178 "Modify Ring", (int)sizeof(**dev_op_modify_ring), 179 GVE_DEV_OPT_REQ_FEAT_MASK_MODIFY_RING, 180 option_length, req_feat_mask); 181 break; 182 } 183 184 if (option_length > sizeof(**dev_op_modify_ring)) { 185 dev_warn(&priv->pdev->dev, 186 GVE_DEVICE_OPTION_TOO_BIG_FMT, "Modify Ring"); 187 } 188 189 *dev_op_modify_ring = (void *)(option + 1); 190 191 /* device has not provided min ring size */ 192 if (option_length == GVE_DEVICE_OPTION_NO_MIN_RING_SIZE) 193 priv->default_min_ring_size = true; 194 break; 195 case GVE_DEV_OPT_ID_FLOW_STEERING: 196 if (option_length < sizeof(**dev_op_flow_steering) || 197 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_FLOW_STEERING) { 198 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 199 "Flow Steering", 200 (int)sizeof(**dev_op_flow_steering), 201 GVE_DEV_OPT_REQ_FEAT_MASK_FLOW_STEERING, 202 option_length, req_feat_mask); 203 break; 204 } 205 206 if (option_length > sizeof(**dev_op_flow_steering)) 207 dev_warn(&priv->pdev->dev, 208 GVE_DEVICE_OPTION_TOO_BIG_FMT, 209 "Flow Steering"); 210 *dev_op_flow_steering = (void *)(option + 1); 211 break; 212 case GVE_DEV_OPT_ID_RSS_CONFIG: 213 if (option_length < sizeof(**dev_op_rss_config) || 214 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_RSS_CONFIG) { 215 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 216 "RSS config", 217 (int)sizeof(**dev_op_rss_config), 218 GVE_DEV_OPT_REQ_FEAT_MASK_RSS_CONFIG, 219 option_length, req_feat_mask); 220 break; 221 } 222 223 if (option_length > sizeof(**dev_op_rss_config)) 224 dev_warn(&priv->pdev->dev, 225 GVE_DEVICE_OPTION_TOO_BIG_FMT, 226 "RSS config"); 227 *dev_op_rss_config = (void *)(option + 1); 228 break; 229 case GVE_DEV_OPT_ID_NIC_TIMESTAMP: 230 if (option_length < sizeof(**dev_op_nic_timestamp) || 231 req_feat_mask != GVE_DEV_OPT_REQ_FEAT_MASK_NIC_TIMESTAMP) { 232 dev_warn(&priv->pdev->dev, GVE_DEVICE_OPTION_ERROR_FMT, 233 "Nic Timestamp", 234 (int)sizeof(**dev_op_nic_timestamp), 235 GVE_DEV_OPT_REQ_FEAT_MASK_NIC_TIMESTAMP, 236 option_length, req_feat_mask); 237 break; 238 } 239 240 if (option_length > sizeof(**dev_op_nic_timestamp)) 241 dev_warn(&priv->pdev->dev, 242 GVE_DEVICE_OPTION_TOO_BIG_FMT, 243 "Nic Timestamp"); 244 *dev_op_nic_timestamp = (void *)(option + 1); 245 break; 246 default: 247 /* If we don't recognize the option just continue 248 * without doing anything. 249 */ 250 dev_dbg(&priv->pdev->dev, "Unrecognized device option 0x%hx not enabled.\n", 251 option_id); 252 } 253 } 254 255 /* Process all device options for a given describe device call. */ 256 static int 257 gve_process_device_options(struct gve_priv *priv, 258 struct gve_device_descriptor *descriptor, 259 struct gve_device_option_gqi_rda **dev_op_gqi_rda, 260 struct gve_device_option_gqi_qpl **dev_op_gqi_qpl, 261 struct gve_device_option_dqo_rda **dev_op_dqo_rda, 262 struct gve_device_option_jumbo_frames **dev_op_jumbo_frames, 263 struct gve_device_option_dqo_qpl **dev_op_dqo_qpl, 264 struct gve_device_option_buffer_sizes **dev_op_buffer_sizes, 265 struct gve_device_option_flow_steering **dev_op_flow_steering, 266 struct gve_device_option_rss_config **dev_op_rss_config, 267 struct gve_device_option_nic_timestamp **dev_op_nic_timestamp, 268 struct gve_device_option_modify_ring **dev_op_modify_ring) 269 { 270 const int num_options = be16_to_cpu(descriptor->num_device_options); 271 struct gve_device_option *dev_opt; 272 int i; 273 274 /* The options struct directly follows the device descriptor. */ 275 dev_opt = (void *)(descriptor + 1); 276 for (i = 0; i < num_options; i++) { 277 struct gve_device_option *next_opt; 278 279 next_opt = gve_get_next_option(descriptor, dev_opt); 280 if (!next_opt) { 281 dev_err(&priv->dev->dev, 282 "options exceed device_descriptor's total length.\n"); 283 return -EINVAL; 284 } 285 286 gve_parse_device_option(priv, descriptor, dev_opt, 287 dev_op_gqi_rda, dev_op_gqi_qpl, 288 dev_op_dqo_rda, dev_op_jumbo_frames, 289 dev_op_dqo_qpl, dev_op_buffer_sizes, 290 dev_op_flow_steering, dev_op_rss_config, 291 dev_op_nic_timestamp, 292 dev_op_modify_ring); 293 dev_opt = next_opt; 294 } 295 296 return 0; 297 } 298 299 int gve_adminq_alloc(struct device *dev, struct gve_priv *priv) 300 { 301 priv->adminq_pool = dma_pool_create("adminq_pool", dev, 302 GVE_ADMINQ_BUFFER_SIZE, 0, 0); 303 if (unlikely(!priv->adminq_pool)) 304 return -ENOMEM; 305 priv->adminq = dma_pool_alloc(priv->adminq_pool, GFP_KERNEL, 306 &priv->adminq_bus_addr); 307 if (unlikely(!priv->adminq)) { 308 dma_pool_destroy(priv->adminq_pool); 309 return -ENOMEM; 310 } 311 312 priv->adminq_mask = 313 (GVE_ADMINQ_BUFFER_SIZE / sizeof(union gve_adminq_command)) - 1; 314 priv->adminq_prod_cnt = 0; 315 priv->adminq_cmd_fail = 0; 316 priv->adminq_timeouts = 0; 317 priv->adminq_describe_device_cnt = 0; 318 priv->adminq_cfg_device_resources_cnt = 0; 319 priv->adminq_register_page_list_cnt = 0; 320 priv->adminq_unregister_page_list_cnt = 0; 321 priv->adminq_create_tx_queue_cnt = 0; 322 priv->adminq_create_rx_queue_cnt = 0; 323 priv->adminq_destroy_tx_queue_cnt = 0; 324 priv->adminq_destroy_rx_queue_cnt = 0; 325 priv->adminq_dcfg_device_resources_cnt = 0; 326 priv->adminq_set_driver_parameter_cnt = 0; 327 priv->adminq_report_stats_cnt = 0; 328 priv->adminq_report_link_speed_cnt = 0; 329 priv->adminq_report_nic_timestamp_cnt = 0; 330 priv->adminq_get_ptype_map_cnt = 0; 331 priv->adminq_query_flow_rules_cnt = 0; 332 priv->adminq_cfg_flow_rule_cnt = 0; 333 priv->adminq_cfg_rss_cnt = 0; 334 priv->adminq_query_rss_cnt = 0; 335 336 /* Setup Admin queue with the device */ 337 if (priv->pdev->revision < 0x1) { 338 iowrite32be(priv->adminq_bus_addr / PAGE_SIZE, 339 &priv->reg_bar0->adminq_pfn); 340 } else { 341 iowrite16be(GVE_ADMINQ_BUFFER_SIZE, 342 &priv->reg_bar0->adminq_length); 343 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 344 iowrite32be(priv->adminq_bus_addr >> 32, 345 &priv->reg_bar0->adminq_base_address_hi); 346 #endif 347 iowrite32be(priv->adminq_bus_addr, 348 &priv->reg_bar0->adminq_base_address_lo); 349 iowrite32be(GVE_DRIVER_STATUS_RUN_MASK, &priv->reg_bar0->driver_status); 350 } 351 mutex_init(&priv->adminq_lock); 352 gve_set_admin_queue_ok(priv); 353 return 0; 354 } 355 356 void gve_adminq_release(struct gve_priv *priv) 357 { 358 int i = 0; 359 360 /* Tell the device the adminq is leaving */ 361 if (priv->pdev->revision < 0x1) { 362 iowrite32be(0x0, &priv->reg_bar0->adminq_pfn); 363 while (ioread32be(&priv->reg_bar0->adminq_pfn)) { 364 /* If this is reached the device is unrecoverable and still 365 * holding memory. Continue looping to avoid memory corruption, 366 * but WARN so it is visible what is going on. 367 */ 368 if (i == GVE_MAX_ADMINQ_RELEASE_CHECK) 369 WARN(1, "Unrecoverable platform error!"); 370 i++; 371 msleep(GVE_ADMINQ_SLEEP_LEN); 372 } 373 } else { 374 iowrite32be(GVE_DRIVER_STATUS_RESET_MASK, &priv->reg_bar0->driver_status); 375 while (!(ioread32be(&priv->reg_bar0->device_status) 376 & GVE_DEVICE_STATUS_DEVICE_IS_RESET)) { 377 if (i == GVE_MAX_ADMINQ_RELEASE_CHECK) 378 WARN(1, "Unrecoverable platform error!"); 379 i++; 380 msleep(GVE_ADMINQ_SLEEP_LEN); 381 } 382 } 383 gve_clear_device_rings_ok(priv); 384 gve_clear_device_resources_ok(priv); 385 gve_clear_admin_queue_ok(priv); 386 } 387 388 void gve_adminq_free(struct device *dev, struct gve_priv *priv) 389 { 390 if (!gve_get_admin_queue_ok(priv)) 391 return; 392 gve_adminq_release(priv); 393 dma_pool_free(priv->adminq_pool, priv->adminq, priv->adminq_bus_addr); 394 dma_pool_destroy(priv->adminq_pool); 395 gve_clear_admin_queue_ok(priv); 396 } 397 398 static void gve_adminq_kick_cmd(struct gve_priv *priv, u32 prod_cnt) 399 { 400 iowrite32be(prod_cnt, &priv->reg_bar0->adminq_doorbell); 401 } 402 403 static bool gve_adminq_wait_for_cmd(struct gve_priv *priv, u32 prod_cnt) 404 { 405 int i; 406 407 for (i = 0; i < GVE_MAX_ADMINQ_EVENT_COUNTER_CHECK; i++) { 408 if (ioread32be(&priv->reg_bar0->adminq_event_counter) 409 == prod_cnt) 410 return true; 411 msleep(GVE_ADMINQ_SLEEP_LEN); 412 } 413 414 return false; 415 } 416 417 static int gve_adminq_parse_err(struct gve_priv *priv, u32 status) 418 { 419 if (status != GVE_ADMINQ_COMMAND_PASSED && 420 status != GVE_ADMINQ_COMMAND_UNSET) { 421 dev_err(&priv->pdev->dev, "AQ command failed with status %d\n", status); 422 priv->adminq_cmd_fail++; 423 } 424 switch (status) { 425 case GVE_ADMINQ_COMMAND_PASSED: 426 return 0; 427 case GVE_ADMINQ_COMMAND_UNSET: 428 dev_err(&priv->pdev->dev, "parse_aq_err: err and status both unset, this should not be possible.\n"); 429 return -EINVAL; 430 case GVE_ADMINQ_COMMAND_ERROR_ABORTED: 431 case GVE_ADMINQ_COMMAND_ERROR_CANCELLED: 432 case GVE_ADMINQ_COMMAND_ERROR_DATALOSS: 433 case GVE_ADMINQ_COMMAND_ERROR_FAILED_PRECONDITION: 434 case GVE_ADMINQ_COMMAND_ERROR_UNAVAILABLE: 435 return -EAGAIN; 436 case GVE_ADMINQ_COMMAND_ERROR_ALREADY_EXISTS: 437 case GVE_ADMINQ_COMMAND_ERROR_INTERNAL_ERROR: 438 case GVE_ADMINQ_COMMAND_ERROR_INVALID_ARGUMENT: 439 case GVE_ADMINQ_COMMAND_ERROR_NOT_FOUND: 440 case GVE_ADMINQ_COMMAND_ERROR_OUT_OF_RANGE: 441 case GVE_ADMINQ_COMMAND_ERROR_UNKNOWN_ERROR: 442 return -EINVAL; 443 case GVE_ADMINQ_COMMAND_ERROR_DEADLINE_EXCEEDED: 444 return -ETIME; 445 case GVE_ADMINQ_COMMAND_ERROR_PERMISSION_DENIED: 446 case GVE_ADMINQ_COMMAND_ERROR_UNAUTHENTICATED: 447 return -EACCES; 448 case GVE_ADMINQ_COMMAND_ERROR_RESOURCE_EXHAUSTED: 449 return -ENOMEM; 450 case GVE_ADMINQ_COMMAND_ERROR_UNIMPLEMENTED: 451 return -EOPNOTSUPP; 452 default: 453 dev_err(&priv->pdev->dev, "parse_aq_err: unknown status code %d\n", status); 454 return -EINVAL; 455 } 456 } 457 458 /* Flushes all AQ commands currently queued and waits for them to complete. 459 * If there are failures, it will return the first error. 460 */ 461 static int gve_adminq_kick_and_wait(struct gve_priv *priv) 462 { 463 int tail, head; 464 int i; 465 466 lockdep_assert_held(&priv->adminq_lock); 467 468 tail = ioread32be(&priv->reg_bar0->adminq_event_counter); 469 head = priv->adminq_prod_cnt; 470 471 gve_adminq_kick_cmd(priv, head); 472 if (!gve_adminq_wait_for_cmd(priv, head)) { 473 dev_err(&priv->pdev->dev, "AQ commands timed out, need to reset AQ\n"); 474 priv->adminq_timeouts++; 475 return -ENOTRECOVERABLE; 476 } 477 478 for (i = tail; i < head; i++) { 479 union gve_adminq_command *cmd; 480 u32 status, err; 481 482 cmd = &priv->adminq[i & priv->adminq_mask]; 483 status = be32_to_cpu(READ_ONCE(cmd->status)); 484 err = gve_adminq_parse_err(priv, status); 485 if (err) 486 // Return the first error if we failed. 487 return err; 488 } 489 490 return 0; 491 } 492 493 static int gve_adminq_issue_cmd(struct gve_priv *priv, 494 union gve_adminq_command *cmd_orig) 495 { 496 union gve_adminq_command *cmd; 497 u32 opcode; 498 u32 tail; 499 500 lockdep_assert_held(&priv->adminq_lock); 501 502 tail = ioread32be(&priv->reg_bar0->adminq_event_counter); 503 504 // Check if next command will overflow the buffer. 505 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) == 506 (tail & priv->adminq_mask)) { 507 int err; 508 509 // Flush existing commands to make room. 510 err = gve_adminq_kick_and_wait(priv); 511 if (err) 512 return err; 513 514 // Retry. 515 tail = ioread32be(&priv->reg_bar0->adminq_event_counter); 516 if (((priv->adminq_prod_cnt + 1) & priv->adminq_mask) == 517 (tail & priv->adminq_mask)) { 518 // This should never happen. We just flushed the 519 // command queue so there should be enough space. 520 return -ENOMEM; 521 } 522 } 523 524 cmd = &priv->adminq[priv->adminq_prod_cnt & priv->adminq_mask]; 525 priv->adminq_prod_cnt++; 526 527 memcpy(cmd, cmd_orig, sizeof(*cmd_orig)); 528 opcode = be32_to_cpu(READ_ONCE(cmd->opcode)); 529 if (opcode == GVE_ADMINQ_EXTENDED_COMMAND) 530 opcode = be32_to_cpu(cmd->extended_command.inner_opcode); 531 532 switch (opcode) { 533 case GVE_ADMINQ_DESCRIBE_DEVICE: 534 priv->adminq_describe_device_cnt++; 535 break; 536 case GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES: 537 priv->adminq_cfg_device_resources_cnt++; 538 break; 539 case GVE_ADMINQ_REGISTER_PAGE_LIST: 540 priv->adminq_register_page_list_cnt++; 541 break; 542 case GVE_ADMINQ_UNREGISTER_PAGE_LIST: 543 priv->adminq_unregister_page_list_cnt++; 544 break; 545 case GVE_ADMINQ_CREATE_TX_QUEUE: 546 priv->adminq_create_tx_queue_cnt++; 547 break; 548 case GVE_ADMINQ_CREATE_RX_QUEUE: 549 priv->adminq_create_rx_queue_cnt++; 550 break; 551 case GVE_ADMINQ_DESTROY_TX_QUEUE: 552 priv->adminq_destroy_tx_queue_cnt++; 553 break; 554 case GVE_ADMINQ_DESTROY_RX_QUEUE: 555 priv->adminq_destroy_rx_queue_cnt++; 556 break; 557 case GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES: 558 priv->adminq_dcfg_device_resources_cnt++; 559 break; 560 case GVE_ADMINQ_SET_DRIVER_PARAMETER: 561 priv->adminq_set_driver_parameter_cnt++; 562 break; 563 case GVE_ADMINQ_REPORT_STATS: 564 priv->adminq_report_stats_cnt++; 565 break; 566 case GVE_ADMINQ_REPORT_LINK_SPEED: 567 priv->adminq_report_link_speed_cnt++; 568 break; 569 case GVE_ADMINQ_REPORT_NIC_TIMESTAMP: 570 priv->adminq_report_nic_timestamp_cnt++; 571 break; 572 case GVE_ADMINQ_GET_PTYPE_MAP: 573 priv->adminq_get_ptype_map_cnt++; 574 break; 575 case GVE_ADMINQ_VERIFY_DRIVER_COMPATIBILITY: 576 priv->adminq_verify_driver_compatibility_cnt++; 577 break; 578 case GVE_ADMINQ_QUERY_FLOW_RULES: 579 priv->adminq_query_flow_rules_cnt++; 580 break; 581 case GVE_ADMINQ_CONFIGURE_FLOW_RULE: 582 priv->adminq_cfg_flow_rule_cnt++; 583 break; 584 case GVE_ADMINQ_CONFIGURE_RSS: 585 priv->adminq_cfg_rss_cnt++; 586 break; 587 case GVE_ADMINQ_QUERY_RSS: 588 priv->adminq_query_rss_cnt++; 589 break; 590 default: 591 dev_err(&priv->pdev->dev, "unknown AQ command opcode %d\n", opcode); 592 return -EINVAL; 593 } 594 595 return 0; 596 } 597 598 static int gve_adminq_execute_cmd(struct gve_priv *priv, 599 union gve_adminq_command *cmd_orig) 600 { 601 u32 tail, head; 602 int err; 603 604 mutex_lock(&priv->adminq_lock); 605 tail = ioread32be(&priv->reg_bar0->adminq_event_counter); 606 head = priv->adminq_prod_cnt; 607 if (tail != head) { 608 err = -EINVAL; 609 goto out; 610 } 611 612 err = gve_adminq_issue_cmd(priv, cmd_orig); 613 if (err) 614 goto out; 615 616 err = gve_adminq_kick_and_wait(priv); 617 618 out: 619 mutex_unlock(&priv->adminq_lock); 620 return err; 621 } 622 623 static int gve_adminq_execute_extended_cmd(struct gve_priv *priv, u32 opcode, 624 size_t cmd_size, void *cmd_orig) 625 { 626 union gve_adminq_command cmd; 627 dma_addr_t inner_cmd_bus; 628 void *inner_cmd; 629 int err; 630 631 inner_cmd = dma_alloc_coherent(&priv->pdev->dev, cmd_size, 632 &inner_cmd_bus, GFP_KERNEL); 633 if (!inner_cmd) 634 return -ENOMEM; 635 636 memcpy(inner_cmd, cmd_orig, cmd_size); 637 638 memset(&cmd, 0, sizeof(cmd)); 639 cmd.opcode = cpu_to_be32(GVE_ADMINQ_EXTENDED_COMMAND); 640 cmd.extended_command = (struct gve_adminq_extended_command) { 641 .inner_opcode = cpu_to_be32(opcode), 642 .inner_length = cpu_to_be32(cmd_size), 643 .inner_command_addr = cpu_to_be64(inner_cmd_bus), 644 }; 645 646 err = gve_adminq_execute_cmd(priv, &cmd); 647 648 dma_free_coherent(&priv->pdev->dev, cmd_size, inner_cmd, inner_cmd_bus); 649 return err; 650 } 651 652 /* The device specifies that the management vector can either be the first irq 653 * or the last irq. ntfy_blk_msix_base_idx indicates the first irq assigned to 654 * the ntfy blks. If it is 0 then the management vector is last, if it is 1 then 655 * the management vector is first. 656 * 657 * gve arranges the msix vectors so that the management vector is last. 658 */ 659 #define GVE_NTFY_BLK_BASE_MSIX_IDX 0 660 int gve_adminq_configure_device_resources(struct gve_priv *priv, 661 dma_addr_t counter_array_bus_addr, 662 u32 num_counters, 663 dma_addr_t db_array_bus_addr, 664 u32 num_ntfy_blks) 665 { 666 union gve_adminq_command cmd; 667 668 memset(&cmd, 0, sizeof(cmd)); 669 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES); 670 cmd.configure_device_resources = 671 (struct gve_adminq_configure_device_resources) { 672 .counter_array = cpu_to_be64(counter_array_bus_addr), 673 .num_counters = cpu_to_be32(num_counters), 674 .irq_db_addr = cpu_to_be64(db_array_bus_addr), 675 .num_irq_dbs = cpu_to_be32(num_ntfy_blks), 676 .irq_db_stride = cpu_to_be32(sizeof(*priv->irq_db_indices)), 677 .ntfy_blk_msix_base_idx = 678 cpu_to_be32(GVE_NTFY_BLK_BASE_MSIX_IDX), 679 .queue_format = priv->queue_format, 680 }; 681 682 return gve_adminq_execute_cmd(priv, &cmd); 683 } 684 685 int gve_adminq_deconfigure_device_resources(struct gve_priv *priv) 686 { 687 union gve_adminq_command cmd; 688 689 memset(&cmd, 0, sizeof(cmd)); 690 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES); 691 692 return gve_adminq_execute_cmd(priv, &cmd); 693 } 694 695 static int gve_adminq_create_tx_queue(struct gve_priv *priv, u32 queue_index) 696 { 697 struct gve_tx_ring *tx = &priv->tx[queue_index]; 698 union gve_adminq_command cmd; 699 700 memset(&cmd, 0, sizeof(cmd)); 701 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CREATE_TX_QUEUE); 702 cmd.create_tx_queue = (struct gve_adminq_create_tx_queue) { 703 .queue_id = cpu_to_be32(queue_index), 704 .queue_resources_addr = 705 cpu_to_be64(tx->q_resources_bus), 706 .tx_ring_addr = cpu_to_be64(tx->bus), 707 .ntfy_id = cpu_to_be32(tx->ntfy_id), 708 .tx_ring_size = cpu_to_be16(priv->tx_desc_cnt), 709 }; 710 711 if (gve_is_gqi(priv)) { 712 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ? 713 GVE_RAW_ADDRESSING_QPL_ID : tx->tx_fifo.qpl->id; 714 715 cmd.create_tx_queue.queue_page_list_id = cpu_to_be32(qpl_id); 716 } else { 717 u32 qpl_id = 0; 718 719 if (priv->queue_format == GVE_DQO_RDA_FORMAT) 720 qpl_id = GVE_RAW_ADDRESSING_QPL_ID; 721 else 722 qpl_id = tx->dqo.qpl->id; 723 cmd.create_tx_queue.queue_page_list_id = cpu_to_be32(qpl_id); 724 cmd.create_tx_queue.tx_comp_ring_addr = 725 cpu_to_be64(tx->complq_bus_dqo); 726 cmd.create_tx_queue.tx_comp_ring_size = 727 cpu_to_be16(priv->tx_desc_cnt); 728 } 729 730 return gve_adminq_issue_cmd(priv, &cmd); 731 } 732 733 int gve_adminq_create_tx_queues(struct gve_priv *priv, u32 start_id, u32 num_queues) 734 { 735 int err; 736 int i; 737 738 mutex_lock(&priv->adminq_lock); 739 740 for (i = start_id; i < start_id + num_queues; i++) { 741 err = gve_adminq_create_tx_queue(priv, i); 742 if (err) 743 goto out; 744 } 745 746 err = gve_adminq_kick_and_wait(priv); 747 748 out: 749 mutex_unlock(&priv->adminq_lock); 750 return err; 751 } 752 753 static void gve_adminq_get_create_rx_queue_cmd(struct gve_priv *priv, 754 union gve_adminq_command *cmd, 755 u32 queue_index) 756 { 757 struct gve_rx_ring *rx = &priv->rx[queue_index]; 758 759 memset(cmd, 0, sizeof(*cmd)); 760 cmd->opcode = cpu_to_be32(GVE_ADMINQ_CREATE_RX_QUEUE); 761 cmd->create_rx_queue = (struct gve_adminq_create_rx_queue) { 762 .queue_id = cpu_to_be32(queue_index), 763 .ntfy_id = cpu_to_be32(rx->ntfy_id), 764 .queue_resources_addr = cpu_to_be64(rx->q_resources_bus), 765 .rx_ring_size = cpu_to_be16(priv->rx_desc_cnt), 766 .packet_buffer_size = cpu_to_be16(rx->packet_buffer_size), 767 }; 768 769 if (gve_is_gqi(priv)) { 770 u32 qpl_id = priv->queue_format == GVE_GQI_RDA_FORMAT ? 771 GVE_RAW_ADDRESSING_QPL_ID : rx->data.qpl->id; 772 773 cmd->create_rx_queue.rx_desc_ring_addr = 774 cpu_to_be64(rx->desc.bus); 775 cmd->create_rx_queue.rx_data_ring_addr = 776 cpu_to_be64(rx->data.data_bus); 777 cmd->create_rx_queue.index = cpu_to_be32(queue_index); 778 cmd->create_rx_queue.queue_page_list_id = cpu_to_be32(qpl_id); 779 } else { 780 u32 qpl_id = 0; 781 782 if (priv->queue_format == GVE_DQO_RDA_FORMAT) 783 qpl_id = GVE_RAW_ADDRESSING_QPL_ID; 784 else 785 qpl_id = rx->dqo.qpl->id; 786 cmd->create_rx_queue.queue_page_list_id = cpu_to_be32(qpl_id); 787 cmd->create_rx_queue.rx_desc_ring_addr = 788 cpu_to_be64(rx->dqo.complq.bus); 789 cmd->create_rx_queue.rx_data_ring_addr = 790 cpu_to_be64(rx->dqo.bufq.bus); 791 cmd->create_rx_queue.rx_buff_ring_size = 792 cpu_to_be16(priv->rx_desc_cnt); 793 cmd->create_rx_queue.enable_rsc = 794 !!(priv->dev->features & NETIF_F_LRO); 795 if (priv->header_split_enabled) 796 cmd->create_rx_queue.header_buffer_size = 797 cpu_to_be16(priv->header_buf_size); 798 } 799 } 800 801 static int gve_adminq_create_rx_queue(struct gve_priv *priv, u32 queue_index) 802 { 803 union gve_adminq_command cmd; 804 805 gve_adminq_get_create_rx_queue_cmd(priv, &cmd, queue_index); 806 return gve_adminq_issue_cmd(priv, &cmd); 807 } 808 809 /* Unlike gve_adminq_create_rx_queue, this actually rings the doorbell */ 810 int gve_adminq_create_single_rx_queue(struct gve_priv *priv, u32 queue_index) 811 { 812 union gve_adminq_command cmd; 813 814 gve_adminq_get_create_rx_queue_cmd(priv, &cmd, queue_index); 815 return gve_adminq_execute_cmd(priv, &cmd); 816 } 817 818 int gve_adminq_create_rx_queues(struct gve_priv *priv, u32 num_queues) 819 { 820 int err; 821 int i; 822 823 mutex_lock(&priv->adminq_lock); 824 825 for (i = 0; i < num_queues; i++) { 826 err = gve_adminq_create_rx_queue(priv, i); 827 if (err) 828 goto out; 829 } 830 831 err = gve_adminq_kick_and_wait(priv); 832 833 out: 834 mutex_unlock(&priv->adminq_lock); 835 return err; 836 } 837 838 static int gve_adminq_destroy_tx_queue(struct gve_priv *priv, u32 queue_index) 839 { 840 union gve_adminq_command cmd; 841 int err; 842 843 memset(&cmd, 0, sizeof(cmd)); 844 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_TX_QUEUE); 845 cmd.destroy_tx_queue = (struct gve_adminq_destroy_tx_queue) { 846 .queue_id = cpu_to_be32(queue_index), 847 }; 848 849 err = gve_adminq_issue_cmd(priv, &cmd); 850 if (err) 851 return err; 852 853 return 0; 854 } 855 856 int gve_adminq_destroy_tx_queues(struct gve_priv *priv, u32 start_id, u32 num_queues) 857 { 858 int err; 859 int i; 860 861 mutex_lock(&priv->adminq_lock); 862 863 for (i = start_id; i < start_id + num_queues; i++) { 864 err = gve_adminq_destroy_tx_queue(priv, i); 865 if (err) 866 goto out; 867 } 868 869 err = gve_adminq_kick_and_wait(priv); 870 871 out: 872 mutex_unlock(&priv->adminq_lock); 873 return err; 874 } 875 876 static void gve_adminq_make_destroy_rx_queue_cmd(union gve_adminq_command *cmd, 877 u32 queue_index) 878 { 879 memset(cmd, 0, sizeof(*cmd)); 880 cmd->opcode = cpu_to_be32(GVE_ADMINQ_DESTROY_RX_QUEUE); 881 cmd->destroy_rx_queue = (struct gve_adminq_destroy_rx_queue) { 882 .queue_id = cpu_to_be32(queue_index), 883 }; 884 } 885 886 static int gve_adminq_destroy_rx_queue(struct gve_priv *priv, u32 queue_index) 887 { 888 union gve_adminq_command cmd; 889 890 gve_adminq_make_destroy_rx_queue_cmd(&cmd, queue_index); 891 return gve_adminq_issue_cmd(priv, &cmd); 892 } 893 894 /* Unlike gve_adminq_destroy_rx_queue, this actually rings the doorbell */ 895 int gve_adminq_destroy_single_rx_queue(struct gve_priv *priv, u32 queue_index) 896 { 897 union gve_adminq_command cmd; 898 899 gve_adminq_make_destroy_rx_queue_cmd(&cmd, queue_index); 900 return gve_adminq_execute_cmd(priv, &cmd); 901 } 902 903 int gve_adminq_destroy_rx_queues(struct gve_priv *priv, u32 num_queues) 904 { 905 int err; 906 int i; 907 908 mutex_lock(&priv->adminq_lock); 909 910 for (i = 0; i < num_queues; i++) { 911 err = gve_adminq_destroy_rx_queue(priv, i); 912 if (err) 913 goto out; 914 } 915 916 err = gve_adminq_kick_and_wait(priv); 917 918 out: 919 mutex_unlock(&priv->adminq_lock); 920 return err; 921 } 922 923 static void gve_set_default_desc_cnt(struct gve_priv *priv, 924 const struct gve_device_descriptor *descriptor) 925 { 926 priv->tx_desc_cnt = be16_to_cpu(descriptor->tx_queue_entries); 927 priv->rx_desc_cnt = be16_to_cpu(descriptor->rx_queue_entries); 928 929 /* set default ranges */ 930 priv->max_tx_desc_cnt = priv->tx_desc_cnt; 931 priv->max_rx_desc_cnt = priv->rx_desc_cnt; 932 priv->min_tx_desc_cnt = priv->tx_desc_cnt; 933 priv->min_rx_desc_cnt = priv->rx_desc_cnt; 934 } 935 936 static void gve_set_default_rss_sizes(struct gve_priv *priv) 937 { 938 if (!gve_is_gqi(priv)) { 939 priv->rss_key_size = GVE_RSS_KEY_SIZE; 940 priv->rss_lut_size = GVE_RSS_INDIR_SIZE; 941 priv->cache_rss_config = true; 942 } 943 } 944 945 static void gve_enable_supported_features(struct gve_priv *priv, 946 u32 supported_features_mask, 947 const struct gve_device_option_jumbo_frames 948 *dev_op_jumbo_frames, 949 const struct gve_device_option_dqo_qpl 950 *dev_op_dqo_qpl, 951 const struct gve_device_option_buffer_sizes 952 *dev_op_buffer_sizes, 953 const struct gve_device_option_flow_steering 954 *dev_op_flow_steering, 955 const struct gve_device_option_rss_config 956 *dev_op_rss_config, 957 const struct gve_device_option_nic_timestamp 958 *dev_op_nic_timestamp, 959 const struct gve_device_option_modify_ring 960 *dev_op_modify_ring) 961 { 962 /* Before control reaches this point, the page-size-capped max MTU from 963 * the gve_device_descriptor field has already been stored in 964 * priv->dev->max_mtu. We overwrite it with the true max MTU below. 965 */ 966 if (dev_op_jumbo_frames && 967 (supported_features_mask & GVE_SUP_JUMBO_FRAMES_MASK)) { 968 dev_info(&priv->pdev->dev, 969 "JUMBO FRAMES device option enabled.\n"); 970 priv->dev->max_mtu = be16_to_cpu(dev_op_jumbo_frames->max_mtu); 971 } 972 973 /* Override pages for qpl for DQO-QPL */ 974 if (dev_op_dqo_qpl) { 975 priv->tx_pages_per_qpl = 976 be16_to_cpu(dev_op_dqo_qpl->tx_pages_per_qpl); 977 if (priv->tx_pages_per_qpl == 0) 978 priv->tx_pages_per_qpl = DQO_QPL_DEFAULT_TX_PAGES; 979 } 980 981 if (dev_op_buffer_sizes && 982 (supported_features_mask & GVE_SUP_BUFFER_SIZES_MASK)) { 983 priv->max_rx_buffer_size = 984 be16_to_cpu(dev_op_buffer_sizes->packet_buffer_size); 985 priv->header_buf_size = 986 be16_to_cpu(dev_op_buffer_sizes->header_buffer_size); 987 dev_info(&priv->pdev->dev, 988 "BUFFER SIZES device option enabled with max_rx_buffer_size of %u, header_buf_size of %u.\n", 989 priv->max_rx_buffer_size, priv->header_buf_size); 990 } 991 992 /* Read and store ring size ranges given by device */ 993 if (dev_op_modify_ring && 994 (supported_features_mask & GVE_SUP_MODIFY_RING_MASK)) { 995 priv->modify_ring_size_enabled = true; 996 997 /* max ring size for DQO QPL should not be overwritten because of device limit */ 998 if (priv->queue_format != GVE_DQO_QPL_FORMAT) { 999 priv->max_rx_desc_cnt = be16_to_cpu(dev_op_modify_ring->max_rx_ring_size); 1000 priv->max_tx_desc_cnt = be16_to_cpu(dev_op_modify_ring->max_tx_ring_size); 1001 } 1002 if (priv->default_min_ring_size) { 1003 /* If device hasn't provided minimums, use default minimums */ 1004 priv->min_tx_desc_cnt = GVE_DEFAULT_MIN_TX_RING_SIZE; 1005 priv->min_rx_desc_cnt = GVE_DEFAULT_MIN_RX_RING_SIZE; 1006 } else { 1007 priv->min_rx_desc_cnt = be16_to_cpu(dev_op_modify_ring->min_rx_ring_size); 1008 priv->min_tx_desc_cnt = be16_to_cpu(dev_op_modify_ring->min_tx_ring_size); 1009 } 1010 } 1011 1012 if (dev_op_flow_steering && 1013 (supported_features_mask & GVE_SUP_FLOW_STEERING_MASK)) { 1014 if (dev_op_flow_steering->max_flow_rules) { 1015 priv->max_flow_rules = 1016 be32_to_cpu(dev_op_flow_steering->max_flow_rules); 1017 priv->dev->hw_features |= NETIF_F_NTUPLE; 1018 dev_info(&priv->pdev->dev, 1019 "FLOW STEERING device option enabled with max rule limit of %u.\n", 1020 priv->max_flow_rules); 1021 } 1022 } 1023 1024 if (dev_op_rss_config && 1025 (supported_features_mask & GVE_SUP_RSS_CONFIG_MASK)) { 1026 priv->rss_key_size = 1027 be16_to_cpu(dev_op_rss_config->hash_key_size); 1028 priv->rss_lut_size = 1029 be16_to_cpu(dev_op_rss_config->hash_lut_size); 1030 priv->cache_rss_config = false; 1031 dev_dbg(&priv->pdev->dev, 1032 "RSS device option enabled with key size of %u, lut size of %u.\n", 1033 priv->rss_key_size, priv->rss_lut_size); 1034 } 1035 1036 if (dev_op_nic_timestamp && 1037 (supported_features_mask & GVE_SUP_NIC_TIMESTAMP_MASK)) 1038 priv->nic_timestamp_supported = true; 1039 } 1040 1041 int gve_adminq_describe_device(struct gve_priv *priv) 1042 { 1043 struct gve_device_option_nic_timestamp *dev_op_nic_timestamp = NULL; 1044 struct gve_device_option_flow_steering *dev_op_flow_steering = NULL; 1045 struct gve_device_option_buffer_sizes *dev_op_buffer_sizes = NULL; 1046 struct gve_device_option_jumbo_frames *dev_op_jumbo_frames = NULL; 1047 struct gve_device_option_modify_ring *dev_op_modify_ring = NULL; 1048 struct gve_device_option_rss_config *dev_op_rss_config = NULL; 1049 struct gve_device_option_gqi_rda *dev_op_gqi_rda = NULL; 1050 struct gve_device_option_gqi_qpl *dev_op_gqi_qpl = NULL; 1051 struct gve_device_option_dqo_rda *dev_op_dqo_rda = NULL; 1052 struct gve_device_option_dqo_qpl *dev_op_dqo_qpl = NULL; 1053 struct gve_device_descriptor *descriptor; 1054 u32 supported_features_mask = 0; 1055 union gve_adminq_command cmd; 1056 dma_addr_t descriptor_bus; 1057 int err = 0; 1058 u8 *mac; 1059 u16 mtu; 1060 1061 memset(&cmd, 0, sizeof(cmd)); 1062 descriptor = dma_pool_alloc(priv->adminq_pool, GFP_KERNEL, 1063 &descriptor_bus); 1064 if (!descriptor) 1065 return -ENOMEM; 1066 cmd.opcode = cpu_to_be32(GVE_ADMINQ_DESCRIBE_DEVICE); 1067 cmd.describe_device.device_descriptor_addr = 1068 cpu_to_be64(descriptor_bus); 1069 cmd.describe_device.device_descriptor_version = 1070 cpu_to_be32(GVE_ADMINQ_DEVICE_DESCRIPTOR_VERSION); 1071 cmd.describe_device.available_length = 1072 cpu_to_be32(GVE_ADMINQ_BUFFER_SIZE); 1073 1074 err = gve_adminq_execute_cmd(priv, &cmd); 1075 if (err) 1076 goto free_device_descriptor; 1077 1078 err = gve_process_device_options(priv, descriptor, &dev_op_gqi_rda, 1079 &dev_op_gqi_qpl, &dev_op_dqo_rda, 1080 &dev_op_jumbo_frames, &dev_op_dqo_qpl, 1081 &dev_op_buffer_sizes, 1082 &dev_op_flow_steering, 1083 &dev_op_rss_config, 1084 &dev_op_nic_timestamp, 1085 &dev_op_modify_ring); 1086 if (err) 1087 goto free_device_descriptor; 1088 1089 /* If the GQI_RAW_ADDRESSING option is not enabled and the queue format 1090 * is not set to GqiRda, choose the queue format in a priority order: 1091 * DqoRda, DqoQpl, GqiRda, GqiQpl. Use GqiQpl as default. 1092 */ 1093 if (dev_op_dqo_rda) { 1094 priv->queue_format = GVE_DQO_RDA_FORMAT; 1095 dev_info(&priv->pdev->dev, 1096 "Driver is running with DQO RDA queue format.\n"); 1097 supported_features_mask = 1098 be32_to_cpu(dev_op_dqo_rda->supported_features_mask); 1099 } else if (dev_op_dqo_qpl) { 1100 priv->queue_format = GVE_DQO_QPL_FORMAT; 1101 supported_features_mask = 1102 be32_to_cpu(dev_op_dqo_qpl->supported_features_mask); 1103 } else if (dev_op_gqi_rda) { 1104 priv->queue_format = GVE_GQI_RDA_FORMAT; 1105 dev_info(&priv->pdev->dev, 1106 "Driver is running with GQI RDA queue format.\n"); 1107 supported_features_mask = 1108 be32_to_cpu(dev_op_gqi_rda->supported_features_mask); 1109 } else if (priv->queue_format == GVE_GQI_RDA_FORMAT) { 1110 dev_info(&priv->pdev->dev, 1111 "Driver is running with GQI RDA queue format.\n"); 1112 } else { 1113 priv->queue_format = GVE_GQI_QPL_FORMAT; 1114 if (dev_op_gqi_qpl) 1115 supported_features_mask = 1116 be32_to_cpu(dev_op_gqi_qpl->supported_features_mask); 1117 dev_info(&priv->pdev->dev, 1118 "Driver is running with GQI QPL queue format.\n"); 1119 } 1120 1121 /* set default descriptor counts */ 1122 gve_set_default_desc_cnt(priv, descriptor); 1123 1124 gve_set_default_rss_sizes(priv); 1125 1126 /* DQO supports LRO. */ 1127 if (!gve_is_gqi(priv)) 1128 priv->dev->hw_features |= NETIF_F_LRO; 1129 1130 priv->max_registered_pages = 1131 be64_to_cpu(descriptor->max_registered_pages); 1132 mtu = be16_to_cpu(descriptor->mtu); 1133 if (mtu < ETH_MIN_MTU) { 1134 dev_err(&priv->pdev->dev, "MTU %d below minimum MTU\n", mtu); 1135 err = -EINVAL; 1136 goto free_device_descriptor; 1137 } 1138 priv->dev->max_mtu = mtu; 1139 priv->num_event_counters = be16_to_cpu(descriptor->counters); 1140 eth_hw_addr_set(priv->dev, descriptor->mac); 1141 mac = descriptor->mac; 1142 dev_info(&priv->pdev->dev, "MAC addr: %pM\n", mac); 1143 priv->tx_pages_per_qpl = be16_to_cpu(descriptor->tx_pages_per_qpl); 1144 priv->default_num_queues = be16_to_cpu(descriptor->default_num_queues); 1145 1146 gve_enable_supported_features(priv, supported_features_mask, 1147 dev_op_jumbo_frames, dev_op_dqo_qpl, 1148 dev_op_buffer_sizes, dev_op_flow_steering, 1149 dev_op_rss_config, dev_op_nic_timestamp, 1150 dev_op_modify_ring); 1151 1152 free_device_descriptor: 1153 dma_pool_free(priv->adminq_pool, descriptor, descriptor_bus); 1154 return err; 1155 } 1156 1157 int gve_adminq_register_page_list(struct gve_priv *priv, 1158 struct gve_queue_page_list *qpl) 1159 { 1160 struct device *hdev = &priv->pdev->dev; 1161 u32 num_entries = qpl->num_entries; 1162 u32 size = num_entries * sizeof(qpl->page_buses[0]); 1163 union gve_adminq_command cmd; 1164 dma_addr_t page_list_bus; 1165 __be64 *page_list; 1166 int err; 1167 int i; 1168 1169 memset(&cmd, 0, sizeof(cmd)); 1170 page_list = dma_alloc_coherent(hdev, size, &page_list_bus, GFP_KERNEL); 1171 if (!page_list) 1172 return -ENOMEM; 1173 1174 for (i = 0; i < num_entries; i++) 1175 page_list[i] = cpu_to_be64(qpl->page_buses[i]); 1176 1177 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REGISTER_PAGE_LIST); 1178 cmd.reg_page_list = (struct gve_adminq_register_page_list) { 1179 .page_list_id = cpu_to_be32(qpl->id), 1180 .num_pages = cpu_to_be32(num_entries), 1181 .page_address_list_addr = cpu_to_be64(page_list_bus), 1182 .page_size = cpu_to_be64(PAGE_SIZE), 1183 }; 1184 1185 err = gve_adminq_execute_cmd(priv, &cmd); 1186 dma_free_coherent(hdev, size, page_list, page_list_bus); 1187 return err; 1188 } 1189 1190 int gve_adminq_unregister_page_list(struct gve_priv *priv, u32 page_list_id) 1191 { 1192 union gve_adminq_command cmd; 1193 1194 memset(&cmd, 0, sizeof(cmd)); 1195 cmd.opcode = cpu_to_be32(GVE_ADMINQ_UNREGISTER_PAGE_LIST); 1196 cmd.unreg_page_list = (struct gve_adminq_unregister_page_list) { 1197 .page_list_id = cpu_to_be32(page_list_id), 1198 }; 1199 1200 return gve_adminq_execute_cmd(priv, &cmd); 1201 } 1202 1203 int gve_adminq_report_stats(struct gve_priv *priv, u64 stats_report_len, 1204 dma_addr_t stats_report_addr, u64 interval) 1205 { 1206 union gve_adminq_command cmd; 1207 1208 memset(&cmd, 0, sizeof(cmd)); 1209 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_STATS); 1210 cmd.report_stats = (struct gve_adminq_report_stats) { 1211 .stats_report_len = cpu_to_be64(stats_report_len), 1212 .stats_report_addr = cpu_to_be64(stats_report_addr), 1213 .interval = cpu_to_be64(interval), 1214 }; 1215 1216 return gve_adminq_execute_cmd(priv, &cmd); 1217 } 1218 1219 int gve_adminq_verify_driver_compatibility(struct gve_priv *priv, 1220 u64 driver_info_len, 1221 dma_addr_t driver_info_addr) 1222 { 1223 union gve_adminq_command cmd; 1224 1225 memset(&cmd, 0, sizeof(cmd)); 1226 cmd.opcode = cpu_to_be32(GVE_ADMINQ_VERIFY_DRIVER_COMPATIBILITY); 1227 cmd.verify_driver_compatibility = (struct gve_adminq_verify_driver_compatibility) { 1228 .driver_info_len = cpu_to_be64(driver_info_len), 1229 .driver_info_addr = cpu_to_be64(driver_info_addr), 1230 }; 1231 1232 return gve_adminq_execute_cmd(priv, &cmd); 1233 } 1234 1235 int gve_adminq_report_link_speed(struct gve_priv *priv) 1236 { 1237 union gve_adminq_command gvnic_cmd; 1238 dma_addr_t link_speed_region_bus; 1239 __be64 *link_speed_region; 1240 int err; 1241 1242 link_speed_region = 1243 dma_alloc_coherent(&priv->pdev->dev, sizeof(*link_speed_region), 1244 &link_speed_region_bus, GFP_KERNEL); 1245 1246 if (!link_speed_region) 1247 return -ENOMEM; 1248 1249 memset(&gvnic_cmd, 0, sizeof(gvnic_cmd)); 1250 gvnic_cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_LINK_SPEED); 1251 gvnic_cmd.report_link_speed.link_speed_address = 1252 cpu_to_be64(link_speed_region_bus); 1253 1254 err = gve_adminq_execute_cmd(priv, &gvnic_cmd); 1255 1256 priv->link_speed = be64_to_cpu(*link_speed_region); 1257 dma_free_coherent(&priv->pdev->dev, sizeof(*link_speed_region), link_speed_region, 1258 link_speed_region_bus); 1259 return err; 1260 } 1261 1262 int gve_adminq_report_nic_ts(struct gve_priv *priv, 1263 dma_addr_t nic_ts_report_addr) 1264 { 1265 union gve_adminq_command cmd; 1266 1267 memset(&cmd, 0, sizeof(cmd)); 1268 cmd.opcode = cpu_to_be32(GVE_ADMINQ_REPORT_NIC_TIMESTAMP); 1269 cmd.report_nic_ts = (struct gve_adminq_report_nic_ts) { 1270 .nic_ts_report_len = 1271 cpu_to_be64(sizeof(struct gve_nic_ts_report)), 1272 .nic_ts_report_addr = cpu_to_be64(nic_ts_report_addr), 1273 }; 1274 1275 return gve_adminq_execute_cmd(priv, &cmd); 1276 } 1277 1278 int gve_adminq_get_ptype_map_dqo(struct gve_priv *priv, 1279 struct gve_ptype_lut *ptype_lut) 1280 { 1281 struct gve_ptype_map *ptype_map; 1282 union gve_adminq_command cmd; 1283 dma_addr_t ptype_map_bus; 1284 int err = 0; 1285 int i; 1286 1287 memset(&cmd, 0, sizeof(cmd)); 1288 ptype_map = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ptype_map), 1289 &ptype_map_bus, GFP_KERNEL); 1290 if (!ptype_map) 1291 return -ENOMEM; 1292 1293 cmd.opcode = cpu_to_be32(GVE_ADMINQ_GET_PTYPE_MAP); 1294 cmd.get_ptype_map = (struct gve_adminq_get_ptype_map) { 1295 .ptype_map_len = cpu_to_be64(sizeof(*ptype_map)), 1296 .ptype_map_addr = cpu_to_be64(ptype_map_bus), 1297 }; 1298 1299 err = gve_adminq_execute_cmd(priv, &cmd); 1300 if (err) 1301 goto err; 1302 1303 /* Populate ptype_lut. */ 1304 for (i = 0; i < GVE_NUM_PTYPES; i++) { 1305 ptype_lut->ptypes[i].l3_type = 1306 ptype_map->ptypes[i].l3_type; 1307 ptype_lut->ptypes[i].l4_type = 1308 ptype_map->ptypes[i].l4_type; 1309 } 1310 err: 1311 dma_free_coherent(&priv->pdev->dev, sizeof(*ptype_map), ptype_map, 1312 ptype_map_bus); 1313 return err; 1314 } 1315 1316 static int 1317 gve_adminq_configure_flow_rule(struct gve_priv *priv, 1318 struct gve_adminq_configure_flow_rule *flow_rule_cmd) 1319 { 1320 int err = gve_adminq_execute_extended_cmd(priv, 1321 GVE_ADMINQ_CONFIGURE_FLOW_RULE, 1322 sizeof(struct gve_adminq_configure_flow_rule), 1323 flow_rule_cmd); 1324 1325 if (err == -ETIME) { 1326 dev_err(&priv->pdev->dev, "Timeout to configure the flow rule, trigger reset"); 1327 gve_reset(priv, true); 1328 } else if (!err) { 1329 priv->flow_rules_cache.rules_cache_synced = false; 1330 } 1331 1332 return err; 1333 } 1334 1335 int gve_adminq_add_flow_rule(struct gve_priv *priv, struct gve_adminq_flow_rule *rule, u32 loc) 1336 { 1337 struct gve_adminq_configure_flow_rule flow_rule_cmd = { 1338 .opcode = cpu_to_be16(GVE_FLOW_RULE_CFG_ADD), 1339 .location = cpu_to_be32(loc), 1340 .rule = *rule, 1341 }; 1342 1343 return gve_adminq_configure_flow_rule(priv, &flow_rule_cmd); 1344 } 1345 1346 int gve_adminq_del_flow_rule(struct gve_priv *priv, u32 loc) 1347 { 1348 struct gve_adminq_configure_flow_rule flow_rule_cmd = { 1349 .opcode = cpu_to_be16(GVE_FLOW_RULE_CFG_DEL), 1350 .location = cpu_to_be32(loc), 1351 }; 1352 1353 return gve_adminq_configure_flow_rule(priv, &flow_rule_cmd); 1354 } 1355 1356 int gve_adminq_reset_flow_rules(struct gve_priv *priv) 1357 { 1358 struct gve_adminq_configure_flow_rule flow_rule_cmd = { 1359 .opcode = cpu_to_be16(GVE_FLOW_RULE_CFG_RESET), 1360 }; 1361 1362 return gve_adminq_configure_flow_rule(priv, &flow_rule_cmd); 1363 } 1364 1365 int gve_adminq_configure_rss(struct gve_priv *priv, struct ethtool_rxfh_param *rxfh) 1366 { 1367 const u32 *hash_lut_to_config = NULL; 1368 const u8 *hash_key_to_config = NULL; 1369 dma_addr_t lut_bus = 0, key_bus = 0; 1370 union gve_adminq_command cmd; 1371 __be32 *lut = NULL; 1372 u8 hash_alg = 0; 1373 u8 *key = NULL; 1374 int err = 0; 1375 u16 i; 1376 1377 switch (rxfh->hfunc) { 1378 case ETH_RSS_HASH_NO_CHANGE: 1379 fallthrough; 1380 case ETH_RSS_HASH_TOP: 1381 hash_alg = ETH_RSS_HASH_TOP; 1382 break; 1383 default: 1384 return -EOPNOTSUPP; 1385 } 1386 1387 if (rxfh->indir) { 1388 if (rxfh->indir_size != priv->rss_lut_size) 1389 return -EINVAL; 1390 1391 hash_lut_to_config = rxfh->indir; 1392 } else if (priv->cache_rss_config) { 1393 hash_lut_to_config = priv->rss_config.hash_lut; 1394 } 1395 1396 if (hash_lut_to_config) { 1397 lut = dma_alloc_coherent(&priv->pdev->dev, 1398 priv->rss_lut_size * sizeof(*lut), 1399 &lut_bus, GFP_KERNEL); 1400 if (!lut) 1401 return -ENOMEM; 1402 1403 for (i = 0; i < priv->rss_lut_size; i++) 1404 lut[i] = cpu_to_be32(hash_lut_to_config[i]); 1405 } 1406 1407 if (rxfh->key) { 1408 if (rxfh->key_size != priv->rss_key_size) { 1409 err = -EINVAL; 1410 goto out; 1411 } 1412 1413 hash_key_to_config = rxfh->key; 1414 } else if (priv->cache_rss_config) { 1415 hash_key_to_config = priv->rss_config.hash_key; 1416 } 1417 1418 if (hash_key_to_config) { 1419 key = dma_alloc_coherent(&priv->pdev->dev, 1420 priv->rss_key_size, 1421 &key_bus, GFP_KERNEL); 1422 if (!key) { 1423 err = -ENOMEM; 1424 goto out; 1425 } 1426 1427 memcpy(key, hash_key_to_config, priv->rss_key_size); 1428 } 1429 1430 /* Zero-valued fields in the cmd.configure_rss instruct the device to 1431 * not update those fields. 1432 */ 1433 memset(&cmd, 0, sizeof(cmd)); 1434 cmd.opcode = cpu_to_be32(GVE_ADMINQ_CONFIGURE_RSS); 1435 cmd.configure_rss = (struct gve_adminq_configure_rss) { 1436 .hash_types = cpu_to_be16(BIT(GVE_RSS_HASH_TCPV4) | 1437 BIT(GVE_RSS_HASH_UDPV4) | 1438 BIT(GVE_RSS_HASH_TCPV6) | 1439 BIT(GVE_RSS_HASH_UDPV6)), 1440 .hash_alg = hash_alg, 1441 .hash_key_size = 1442 cpu_to_be16((key_bus) ? priv->rss_key_size : 0), 1443 .hash_lut_size = 1444 cpu_to_be16((lut_bus) ? priv->rss_lut_size : 0), 1445 .hash_key_addr = cpu_to_be64(key_bus), 1446 .hash_lut_addr = cpu_to_be64(lut_bus), 1447 }; 1448 1449 err = gve_adminq_execute_cmd(priv, &cmd); 1450 1451 out: 1452 if (lut) 1453 dma_free_coherent(&priv->pdev->dev, 1454 priv->rss_lut_size * sizeof(*lut), 1455 lut, lut_bus); 1456 if (key) 1457 dma_free_coherent(&priv->pdev->dev, 1458 priv->rss_key_size, key, key_bus); 1459 return err; 1460 } 1461 1462 /* In the dma memory that the driver allocated for the device to query the flow rules, the device 1463 * will first write it with a struct of gve_query_flow_rules_descriptor. Next to it, the device 1464 * will write an array of rules or rule ids with the count that specified in the descriptor. 1465 * For GVE_FLOW_RULE_QUERY_STATS, the device will only write the descriptor. 1466 */ 1467 static int gve_adminq_process_flow_rules_query(struct gve_priv *priv, u16 query_opcode, 1468 struct gve_query_flow_rules_descriptor *descriptor) 1469 { 1470 struct gve_flow_rules_cache *flow_rules_cache = &priv->flow_rules_cache; 1471 u32 num_queried_rules, total_memory_len, rule_info_len; 1472 void *rule_info; 1473 1474 total_memory_len = be32_to_cpu(descriptor->total_length); 1475 num_queried_rules = be32_to_cpu(descriptor->num_queried_rules); 1476 rule_info = (void *)(descriptor + 1); 1477 1478 switch (query_opcode) { 1479 case GVE_FLOW_RULE_QUERY_RULES: 1480 rule_info_len = num_queried_rules * sizeof(*flow_rules_cache->rules_cache); 1481 if (sizeof(*descriptor) + rule_info_len != total_memory_len) { 1482 dev_err(&priv->dev->dev, "flow rules query is out of memory.\n"); 1483 return -ENOMEM; 1484 } 1485 1486 memcpy(flow_rules_cache->rules_cache, rule_info, rule_info_len); 1487 flow_rules_cache->rules_cache_num = num_queried_rules; 1488 break; 1489 case GVE_FLOW_RULE_QUERY_IDS: 1490 rule_info_len = num_queried_rules * sizeof(*flow_rules_cache->rule_ids_cache); 1491 if (sizeof(*descriptor) + rule_info_len != total_memory_len) { 1492 dev_err(&priv->dev->dev, "flow rule ids query is out of memory.\n"); 1493 return -ENOMEM; 1494 } 1495 1496 memcpy(flow_rules_cache->rule_ids_cache, rule_info, rule_info_len); 1497 flow_rules_cache->rule_ids_cache_num = num_queried_rules; 1498 break; 1499 case GVE_FLOW_RULE_QUERY_STATS: 1500 priv->num_flow_rules = be32_to_cpu(descriptor->num_flow_rules); 1501 priv->max_flow_rules = be32_to_cpu(descriptor->max_flow_rules); 1502 return 0; 1503 default: 1504 return -EINVAL; 1505 } 1506 1507 return 0; 1508 } 1509 1510 int gve_adminq_query_flow_rules(struct gve_priv *priv, u16 query_opcode, u32 starting_loc) 1511 { 1512 struct gve_query_flow_rules_descriptor *descriptor; 1513 union gve_adminq_command cmd; 1514 dma_addr_t descriptor_bus; 1515 int err = 0; 1516 1517 memset(&cmd, 0, sizeof(cmd)); 1518 descriptor = dma_pool_alloc(priv->adminq_pool, GFP_KERNEL, &descriptor_bus); 1519 if (!descriptor) 1520 return -ENOMEM; 1521 1522 cmd.opcode = cpu_to_be32(GVE_ADMINQ_QUERY_FLOW_RULES); 1523 cmd.query_flow_rules = (struct gve_adminq_query_flow_rules) { 1524 .opcode = cpu_to_be16(query_opcode), 1525 .starting_rule_id = cpu_to_be32(starting_loc), 1526 .available_length = cpu_to_be64(GVE_ADMINQ_BUFFER_SIZE), 1527 .rule_descriptor_addr = cpu_to_be64(descriptor_bus), 1528 }; 1529 err = gve_adminq_execute_cmd(priv, &cmd); 1530 if (err) 1531 goto out; 1532 1533 err = gve_adminq_process_flow_rules_query(priv, query_opcode, descriptor); 1534 1535 out: 1536 dma_pool_free(priv->adminq_pool, descriptor, descriptor_bus); 1537 return err; 1538 } 1539 1540 static int gve_adminq_process_rss_query(struct gve_priv *priv, 1541 struct gve_query_rss_descriptor *descriptor, 1542 struct ethtool_rxfh_param *rxfh) 1543 { 1544 u32 total_memory_length; 1545 u16 hash_lut_length; 1546 void *rss_info_addr; 1547 __be32 *lut; 1548 u16 i; 1549 1550 total_memory_length = be32_to_cpu(descriptor->total_length); 1551 hash_lut_length = priv->rss_lut_size * sizeof(*rxfh->indir); 1552 1553 if (sizeof(*descriptor) + priv->rss_key_size + hash_lut_length != total_memory_length) { 1554 dev_err(&priv->dev->dev, 1555 "rss query desc from device has invalid length parameter.\n"); 1556 return -EINVAL; 1557 } 1558 1559 rxfh->hfunc = descriptor->hash_alg; 1560 1561 rss_info_addr = (void *)(descriptor + 1); 1562 if (rxfh->key) { 1563 rxfh->key_size = priv->rss_key_size; 1564 memcpy(rxfh->key, rss_info_addr, priv->rss_key_size); 1565 } 1566 1567 rss_info_addr += priv->rss_key_size; 1568 lut = (__be32 *)rss_info_addr; 1569 if (rxfh->indir) { 1570 rxfh->indir_size = priv->rss_lut_size; 1571 for (i = 0; i < priv->rss_lut_size; i++) 1572 rxfh->indir[i] = be32_to_cpu(lut[i]); 1573 } 1574 1575 return 0; 1576 } 1577 1578 int gve_adminq_query_rss_config(struct gve_priv *priv, struct ethtool_rxfh_param *rxfh) 1579 { 1580 struct gve_query_rss_descriptor *descriptor; 1581 union gve_adminq_command cmd; 1582 dma_addr_t descriptor_bus; 1583 int err = 0; 1584 1585 descriptor = dma_pool_alloc(priv->adminq_pool, GFP_KERNEL, &descriptor_bus); 1586 if (!descriptor) 1587 return -ENOMEM; 1588 1589 memset(&cmd, 0, sizeof(cmd)); 1590 cmd.opcode = cpu_to_be32(GVE_ADMINQ_QUERY_RSS); 1591 cmd.query_rss = (struct gve_adminq_query_rss) { 1592 .available_length = cpu_to_be64(GVE_ADMINQ_BUFFER_SIZE), 1593 .rss_descriptor_addr = cpu_to_be64(descriptor_bus), 1594 }; 1595 err = gve_adminq_execute_cmd(priv, &cmd); 1596 if (err) 1597 goto out; 1598 1599 err = gve_adminq_process_rss_query(priv, descriptor, rxfh); 1600 1601 out: 1602 dma_pool_free(priv->adminq_pool, descriptor, descriptor_bus); 1603 return err; 1604 } 1605