1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */ 3 4 #include <linux/bitfield.h> 5 #include <linux/etherdevice.h> 6 #include <linux/delay.h> 7 #include <linux/dev_printk.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/gfp.h> 10 #include <linux/iopoll.h> 11 #include <linux/types.h> 12 13 #include "fbnic.h" 14 #include "fbnic_tlv.h" 15 16 static void __fbnic_mbx_wr_desc(struct fbnic_dev *fbd, int mbx_idx, 17 int desc_idx, u64 desc) 18 { 19 u32 desc_offset = FBNIC_IPC_MBX(mbx_idx, desc_idx); 20 21 /* Write the upper 32b and then the lower 32b. Doing this the 22 * FW can then read lower, upper, lower to verify that the state 23 * of the descriptor wasn't changed mid-transaction. 24 */ 25 fw_wr32(fbd, desc_offset + 1, upper_32_bits(desc)); 26 fw_wrfl(fbd); 27 fw_wr32(fbd, desc_offset, lower_32_bits(desc)); 28 } 29 30 static void __fbnic_mbx_invalidate_desc(struct fbnic_dev *fbd, int mbx_idx, 31 int desc_idx, u32 desc) 32 { 33 u32 desc_offset = FBNIC_IPC_MBX(mbx_idx, desc_idx); 34 35 /* For initialization we write the lower 32b of the descriptor first. 36 * This way we can set the state to mark it invalid before we clear the 37 * upper 32b. 38 */ 39 fw_wr32(fbd, desc_offset, desc); 40 fw_wrfl(fbd); 41 fw_wr32(fbd, desc_offset + 1, 0); 42 } 43 44 u64 __fbnic_mbx_rd_desc(struct fbnic_dev *fbd, int mbx_idx, int desc_idx) 45 { 46 u32 desc_offset = FBNIC_IPC_MBX(mbx_idx, desc_idx); 47 u64 desc; 48 49 desc = fw_rd32(fbd, desc_offset); 50 desc |= (u64)fw_rd32(fbd, desc_offset + 1) << 32; 51 52 return desc; 53 } 54 55 static void fbnic_mbx_reset_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 56 { 57 int desc_idx; 58 59 /* Disable DMA transactions from the device, 60 * and flush any transactions triggered during cleaning 61 */ 62 switch (mbx_idx) { 63 case FBNIC_IPC_MBX_RX_IDX: 64 /* Clearing BME blocks the device from writing to the host 65 * but leaves the requests parked in the write pipeline. The 66 * write path only clears outstanding requests when both FLUSH 67 * and FLUSH_MODE are set; FLUSH_MODE lets them drain without 68 * landing on the host. 69 */ 70 wr32(fbd, FBNIC_PUL_OB_TLP_HDR_AW_CFG, 71 FBNIC_PUL_OB_TLP_HDR_AW_CFG_FLUSH | 72 FBNIC_PUL_OB_TLP_HDR_AW_CFG_FLUSH_MODE); 73 break; 74 case FBNIC_IPC_MBX_TX_IDX: 75 wr32(fbd, FBNIC_PUL_OB_TLP_HDR_AR_CFG, 76 FBNIC_PUL_OB_TLP_HDR_AR_CFG_FLUSH); 77 break; 78 } 79 80 wrfl(fbd); 81 82 /* Initialize first descriptor to all 0s. Doing this gives us a 83 * solid stop for the firmware to hit when it is done looping 84 * through the ring. 85 */ 86 __fbnic_mbx_invalidate_desc(fbd, mbx_idx, 0, 0); 87 88 /* We then fill the rest of the ring starting at the end and moving 89 * back toward descriptor 0 with skip descriptors that have no 90 * length nor address, and tell the firmware that they can skip 91 * them and just move past them to the one we initialized to 0. 92 */ 93 for (desc_idx = FBNIC_IPC_MBX_DESC_LEN; --desc_idx;) 94 __fbnic_mbx_invalidate_desc(fbd, mbx_idx, desc_idx, 95 FBNIC_IPC_MBX_DESC_FW_CMPL | 96 FBNIC_IPC_MBX_DESC_HOST_CMPL); 97 } 98 99 void fbnic_mbx_init(struct fbnic_dev *fbd) 100 { 101 int i; 102 103 /* Initialize lock to protect Tx ring */ 104 spin_lock_init(&fbd->fw_tx_lock); 105 106 /* Reset FW Capabilities */ 107 memset(&fbd->fw_cap, 0, sizeof(fbd->fw_cap)); 108 109 /* Reinitialize mailbox memory */ 110 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 111 memset(&fbd->mbx[i], 0, sizeof(struct fbnic_fw_mbx)); 112 113 /* Do not auto-clear the FW mailbox interrupt, let SW clear it */ 114 wr32(fbd, FBNIC_INTR_SW_AC_MODE(0), ~(1u << FBNIC_FW_MSIX_ENTRY)); 115 116 /* Clear any stale causes in vector 0 as that is used for doorbell */ 117 wr32(fbd, FBNIC_INTR_CLEAR(0), 1u << FBNIC_FW_MSIX_ENTRY); 118 119 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 120 fbnic_mbx_reset_desc_ring(fbd, i); 121 } 122 123 static int fbnic_mbx_map_msg(struct fbnic_dev *fbd, int mbx_idx, 124 struct fbnic_tlv_msg *msg, u16 length, u8 eom) 125 { 126 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 127 u8 tail = mbx->tail; 128 dma_addr_t addr; 129 int direction; 130 131 if (!mbx->ready || !fbnic_fw_present(fbd)) 132 return -ENODEV; 133 134 direction = (mbx_idx == FBNIC_IPC_MBX_RX_IDX) ? DMA_FROM_DEVICE : 135 DMA_TO_DEVICE; 136 137 if (mbx->head == ((tail + 1) % FBNIC_IPC_MBX_DESC_LEN)) 138 return -EBUSY; 139 140 addr = dma_map_single(fbd->dev, msg, PAGE_SIZE, direction); 141 if (dma_mapping_error(fbd->dev, addr)) 142 return -ENOSPC; 143 144 mbx->buf_info[tail].msg = msg; 145 mbx->buf_info[tail].addr = addr; 146 147 mbx->tail = (tail + 1) % FBNIC_IPC_MBX_DESC_LEN; 148 149 fw_wr32(fbd, FBNIC_IPC_MBX(mbx_idx, mbx->tail), 0); 150 151 __fbnic_mbx_wr_desc(fbd, mbx_idx, tail, 152 FIELD_PREP(FBNIC_IPC_MBX_DESC_LEN_MASK, length) | 153 (addr & FBNIC_IPC_MBX_DESC_ADDR_MASK) | 154 (eom ? FBNIC_IPC_MBX_DESC_EOM : 0) | 155 FBNIC_IPC_MBX_DESC_HOST_CMPL); 156 157 return 0; 158 } 159 160 static void fbnic_mbx_unmap_and_free_msg(struct fbnic_dev *fbd, int mbx_idx, 161 int desc_idx) 162 { 163 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 164 int direction; 165 166 if (!mbx->buf_info[desc_idx].msg) 167 return; 168 169 direction = (mbx_idx == FBNIC_IPC_MBX_RX_IDX) ? DMA_FROM_DEVICE : 170 DMA_TO_DEVICE; 171 dma_unmap_single(fbd->dev, mbx->buf_info[desc_idx].addr, 172 PAGE_SIZE, direction); 173 174 free_page((unsigned long)mbx->buf_info[desc_idx].msg); 175 mbx->buf_info[desc_idx].msg = NULL; 176 } 177 178 static void fbnic_mbx_clean_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 179 { 180 u32 flush_done, val; 181 int i; 182 183 fbnic_mbx_reset_desc_ring(fbd, mbx_idx); 184 185 /* Arming the flush only starts it. Wait for the outbound path to 186 * retire the requests it still holds before the buffers they may 187 * target are unmapped and freed. A lost link reads back as all ones, 188 * which ends the poll on the first pass. 189 */ 190 flush_done = mbx_idx == FBNIC_IPC_MBX_RX_IDX ? 191 FBNIC_PUL_USER_IP_LINK_RST_STATUS_AW_FLUSH_DONE : 192 FBNIC_PUL_USER_IP_LINK_RST_STATUS_AR_FLUSH_DONE; 193 if (read_poll_timeout_atomic(fbnic_rd32, val, val & flush_done, 2, 194 1000, false, fbd, 195 FBNIC_PUL_USER_IP_LINK_RST_STATUS)) 196 dev_warn(fbd->dev, "Outbound flush did not complete: %08x\n", 197 val); 198 199 for (i = FBNIC_IPC_MBX_DESC_LEN; i--;) 200 fbnic_mbx_unmap_and_free_msg(fbd, mbx_idx, i); 201 } 202 203 void fbnic_mbx_clean(struct fbnic_dev *fbd) 204 { 205 int i; 206 207 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 208 fbnic_mbx_clean_desc_ring(fbd, i); 209 } 210 211 #define FBNIC_MBX_MAX_PAGE_SIZE FIELD_MAX(FBNIC_IPC_MBX_DESC_LEN_MASK) 212 #define FBNIC_RX_PAGE_SIZE min_t(int, PAGE_SIZE, FBNIC_MBX_MAX_PAGE_SIZE) 213 214 static int fbnic_mbx_alloc_rx_msgs(struct fbnic_dev *fbd) 215 { 216 struct fbnic_fw_mbx *rx_mbx = &fbd->mbx[FBNIC_IPC_MBX_RX_IDX]; 217 u8 tail = rx_mbx->tail, head = rx_mbx->head, count; 218 int err = 0; 219 220 /* Do nothing if mailbox is not ready, or we already have pages on 221 * the ring that can be used by the firmware 222 */ 223 if (!rx_mbx->ready) 224 return -ENODEV; 225 226 /* Fill all but 1 unused descriptors in the Rx queue. */ 227 count = (head - tail - 1) & (FBNIC_IPC_MBX_DESC_LEN - 1); 228 while (!err && count--) { 229 struct fbnic_tlv_msg *msg; 230 231 msg = (struct fbnic_tlv_msg *)__get_free_page(GFP_KERNEL); 232 if (!msg) { 233 err = -ENOMEM; 234 break; 235 } 236 237 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_RX_IDX, msg, 238 FBNIC_RX_PAGE_SIZE, 0); 239 if (err) 240 free_page((unsigned long)msg); 241 } 242 243 return err; 244 } 245 246 static int fbnic_mbx_map_tlv_msg(struct fbnic_dev *fbd, 247 struct fbnic_tlv_msg *msg) 248 { 249 unsigned long flags; 250 int err; 251 252 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 253 254 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_TX_IDX, msg, 255 le16_to_cpu(msg->hdr.len) * sizeof(u32), 1); 256 257 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 258 259 return err; 260 } 261 262 static int fbnic_mbx_set_cmpl_slot(struct fbnic_dev *fbd, 263 struct fbnic_fw_completion *cmpl_data) 264 { 265 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 266 int free = -EXFULL; 267 int i; 268 269 if (!tx_mbx->ready) 270 return -ENODEV; 271 272 for (i = 0; i < FBNIC_MBX_CMPL_SLOTS; i++) { 273 if (!fbd->cmpl_data[i]) 274 free = i; 275 else if (fbd->cmpl_data[i]->msg_type == cmpl_data->msg_type) 276 return -EEXIST; 277 } 278 279 if (free == -EXFULL) 280 return -EXFULL; 281 282 fbd->cmpl_data[free] = cmpl_data; 283 284 return 0; 285 } 286 287 static void fbnic_mbx_clear_cmpl_slot(struct fbnic_dev *fbd, 288 struct fbnic_fw_completion *cmpl_data) 289 { 290 int i; 291 292 for (i = 0; i < FBNIC_MBX_CMPL_SLOTS; i++) { 293 if (fbd->cmpl_data[i] == cmpl_data) { 294 fbd->cmpl_data[i] = NULL; 295 break; 296 } 297 } 298 } 299 300 static void fbnic_mbx_process_tx_msgs(struct fbnic_dev *fbd) 301 { 302 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 303 u8 head = tx_mbx->head; 304 u64 desc; 305 306 while (head != tx_mbx->tail) { 307 desc = __fbnic_mbx_rd_desc(fbd, FBNIC_IPC_MBX_TX_IDX, head); 308 if (!(desc & FBNIC_IPC_MBX_DESC_FW_CMPL)) 309 break; 310 311 if (desc & FBNIC_IPC_MBX_DESC_FW_ERR) { 312 tx_mbx->resp_error++; 313 dev_warn_ratelimited(fbd->dev, 314 "FW completed a Tx mailbox request with an error\n"); 315 } 316 317 fbnic_mbx_unmap_and_free_msg(fbd, FBNIC_IPC_MBX_TX_IDX, head); 318 319 head++; 320 head %= FBNIC_IPC_MBX_DESC_LEN; 321 } 322 323 /* Record head for next interrupt */ 324 tx_mbx->head = head; 325 } 326 327 int fbnic_mbx_set_cmpl(struct fbnic_dev *fbd, 328 struct fbnic_fw_completion *cmpl_data) 329 { 330 unsigned long flags; 331 int err; 332 333 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 334 err = fbnic_mbx_set_cmpl_slot(fbd, cmpl_data); 335 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 336 337 return err; 338 } 339 340 static int fbnic_mbx_map_req_w_cmpl(struct fbnic_dev *fbd, 341 struct fbnic_tlv_msg *msg, 342 struct fbnic_fw_completion *cmpl_data) 343 { 344 unsigned long flags; 345 int err; 346 347 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 348 if (cmpl_data) { 349 err = fbnic_mbx_set_cmpl_slot(fbd, cmpl_data); 350 if (err) 351 goto unlock_mbx; 352 } 353 354 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_TX_IDX, msg, 355 le16_to_cpu(msg->hdr.len) * sizeof(u32), 1); 356 357 /* If we successfully reserved a completion and msg failed 358 * then clear completion data for next caller 359 */ 360 if (err && cmpl_data) 361 fbnic_mbx_clear_cmpl_slot(fbd, cmpl_data); 362 363 unlock_mbx: 364 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 365 366 return err; 367 } 368 369 void fbnic_mbx_clear_cmpl(struct fbnic_dev *fbd, 370 struct fbnic_fw_completion *fw_cmpl) 371 { 372 unsigned long flags; 373 374 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 375 fbnic_mbx_clear_cmpl_slot(fbd, fw_cmpl); 376 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 377 } 378 379 static void fbnic_fw_release_cmpl_data(struct kref *kref) 380 { 381 struct fbnic_fw_completion *cmpl_data; 382 383 cmpl_data = container_of(kref, struct fbnic_fw_completion, 384 ref_count); 385 kfree(cmpl_data); 386 } 387 388 static struct fbnic_fw_completion * 389 fbnic_fw_get_cmpl_by_type(struct fbnic_dev *fbd, u32 msg_type) 390 { 391 struct fbnic_fw_completion *cmpl_data = NULL; 392 unsigned long flags; 393 int i; 394 395 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 396 for (i = 0; i < FBNIC_MBX_CMPL_SLOTS; i++) { 397 if (fbd->cmpl_data[i] && 398 fbd->cmpl_data[i]->msg_type == msg_type) { 399 cmpl_data = fbd->cmpl_data[i]; 400 kref_get(&cmpl_data->ref_count); 401 break; 402 } 403 } 404 405 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 406 407 return cmpl_data; 408 } 409 410 /** 411 * fbnic_fw_xmit_test_msg - Create and transmit a test message to FW mailbox 412 * @fbd: FBNIC device structure 413 * @cmpl: fw completion struct 414 * 415 * Return: zero on success, negative value on failure 416 * 417 * Generates a single page mailbox test message and places it in the Tx 418 * mailbox queue. Expectation is that the FW will validate that the nested 419 * value matches the external values, and then will echo them back to us. 420 * 421 * Also sets a completion slot for use in the completion wait calls when 422 * the cmpl arg is non-NULL. 423 */ 424 int fbnic_fw_xmit_test_msg(struct fbnic_dev *fbd, 425 struct fbnic_fw_completion *cmpl) 426 { 427 struct fbnic_tlv_msg *test_msg; 428 int err; 429 430 test_msg = fbnic_tlv_test_create(fbd); 431 if (!test_msg) 432 return -ENOMEM; 433 434 err = fbnic_mbx_map_req_w_cmpl(fbd, test_msg, cmpl); 435 if (err) 436 free_page((unsigned long)test_msg); 437 438 return err; 439 } 440 441 /** 442 * fbnic_fw_xmit_simple_msg - Transmit a simple single TLV message w/o data 443 * @fbd: FBNIC device structure 444 * @msg_type: ENUM value indicating message type to send 445 * 446 * Return: 447 * One the following values: 448 * -EOPNOTSUPP: Is not ASIC so mailbox is not supported 449 * -ENODEV: Device I/O error 450 * -ENOMEM: Failed to allocate message 451 * -EBUSY: No space in mailbox 452 * -ENOSPC: DMA mapping failed 453 * 454 * This function sends a single TLV header indicating the host wants to take 455 * some action. However there are no other side effects which means that any 456 * response will need to be caught via a completion if this action is 457 * expected to kick off a resultant action. 458 */ 459 static int fbnic_fw_xmit_simple_msg(struct fbnic_dev *fbd, u32 msg_type) 460 { 461 struct fbnic_tlv_msg *msg; 462 int err = 0; 463 464 if (!fbnic_fw_present(fbd)) 465 return -ENODEV; 466 467 msg = fbnic_tlv_msg_alloc(msg_type); 468 if (!msg) 469 return -ENOMEM; 470 471 err = fbnic_mbx_map_tlv_msg(fbd, msg); 472 if (err) 473 free_page((unsigned long)msg); 474 475 return err; 476 } 477 478 static int fbnic_mbx_init_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 479 { 480 u8 tlp_attr = fbd->relaxed_ord ? FBNIC_TLP_ATTR_RO : 0; 481 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 482 483 mbx->ready = true; 484 485 switch (mbx_idx) { 486 case FBNIC_IPC_MBX_RX_IDX: 487 /* Enable DMA writes from the device */ 488 wr32(fbd, FBNIC_PUL_OB_TLP_HDR_AW_CFG, 489 FBNIC_PUL_OB_TLP_HDR_AW_CFG_BME | 490 FIELD_PREP(FBNIC_PUL_OB_TLP_HDR_AW_CFG_RDE_ATTR, 491 tlp_attr) | 492 FIELD_PREP(FBNIC_PUL_OB_TLP_HDR_AW_CFG_TQM_ATTR, 493 tlp_attr)); 494 495 /* Make sure we have a page for the FW to write to */ 496 return fbnic_mbx_alloc_rx_msgs(fbd); 497 case FBNIC_IPC_MBX_TX_IDX: 498 /* Enable DMA reads from the device */ 499 wr32(fbd, FBNIC_PUL_OB_TLP_HDR_AR_CFG, 500 FBNIC_PUL_OB_TLP_HDR_AR_CFG_BME | 501 FIELD_PREP(FBNIC_PUL_OB_TLP_HDR_AR_CFG_TDE_ATTR, 502 tlp_attr) | 503 FIELD_PREP(FBNIC_PUL_OB_TLP_HDR_AR_CFG_RQM_ATTR, 504 tlp_attr) | 505 FIELD_PREP(FBNIC_PUL_OB_TLP_HDR_AR_CFG_TQM_ATTR, 506 tlp_attr)); 507 break; 508 } 509 510 return 0; 511 } 512 513 static bool fbnic_mbx_event(struct fbnic_dev *fbd) 514 { 515 /* We only need to do this on the first interrupt following reset. 516 * this primes the mailbox so that we will have cleared all the 517 * skip descriptors. 518 */ 519 if (!(rd32(fbd, FBNIC_INTR_STATUS(0)) & (1u << FBNIC_FW_MSIX_ENTRY))) 520 return false; 521 522 wr32(fbd, FBNIC_INTR_CLEAR(0), 1u << FBNIC_FW_MSIX_ENTRY); 523 524 return true; 525 } 526 527 /** 528 * fbnic_fw_xmit_ownership_msg - Create and transmit a host ownership message 529 * to FW mailbox 530 * 531 * @fbd: FBNIC device structure 532 * @take_ownership: take/release the ownership 533 * 534 * Return: zero on success, negative value on failure 535 * 536 * Notifies the firmware that the driver either takes ownership of the NIC 537 * (when @take_ownership is true) or releases it. 538 */ 539 int fbnic_fw_xmit_ownership_msg(struct fbnic_dev *fbd, bool take_ownership) 540 { 541 unsigned long req_time = jiffies; 542 struct fbnic_tlv_msg *msg; 543 int err = 0; 544 545 if (!fbnic_fw_present(fbd)) 546 return -ENODEV; 547 548 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_OWNERSHIP_REQ); 549 if (!msg) 550 return -ENOMEM; 551 552 if (take_ownership) { 553 err = fbnic_tlv_attr_put_flag(msg, FBNIC_FW_OWNERSHIP_FLAG); 554 if (err) 555 goto free_message; 556 } 557 558 /* Initialize heartbeat, set last response to 1 second in the past 559 * so that we will trigger a timeout if the firmware doesn't respond 560 */ 561 fbd->last_heartbeat_response = req_time - HZ; 562 fbd->last_heartbeat_request = req_time; 563 564 /* Set prev_firmware_time to 0 to avoid triggering firmware crash 565 * detection until we receive the second uptime in a heartbeat resp. 566 */ 567 fbd->prev_firmware_time = 0; 568 569 err = fbnic_mbx_map_tlv_msg(fbd, msg); 570 if (err) 571 goto free_message; 572 573 /* Set heartbeat detection based on if we are taking ownership */ 574 fbd->fw_heartbeat_enabled = take_ownership; 575 576 return err; 577 578 free_message: 579 free_page((unsigned long)msg); 580 return err; 581 } 582 583 static const struct fbnic_tlv_index fbnic_fw_cap_resp_index[] = { 584 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_VERSION), 585 FBNIC_TLV_ATTR_FLAG(FBNIC_FW_CAP_RESP_BMC_PRESENT), 586 FBNIC_TLV_ATTR_MAC_ADDR(FBNIC_FW_CAP_RESP_BMC_MAC_ADDR), 587 FBNIC_TLV_ATTR_ARRAY(FBNIC_FW_CAP_RESP_BMC_MAC_ARRAY), 588 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_STORED_VERSION), 589 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT), 590 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, 591 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 592 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_BMC_ALL_MULTI), 593 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_FW_LINK_SPEED), 594 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_FW_LINK_FEC), 595 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, 596 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 597 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_CMRT_VERSION), 598 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION), 599 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, 600 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 601 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, 602 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 603 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_UEFI_VERSION), 604 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, 605 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 606 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_ANTI_ROLLBACK_VERSION), 607 FBNIC_TLV_ATTR_LAST 608 }; 609 610 static int fbnic_fw_parse_bmc_addrs(u8 bmc_mac_addr[][ETH_ALEN], 611 struct fbnic_tlv_msg *attr, int len) 612 { 613 int attr_len = le16_to_cpu(attr->hdr.len) / sizeof(u32) - 1; 614 struct fbnic_tlv_msg *mac_results[8]; 615 int err, i = 0; 616 617 /* Make sure we have enough room to process all the MAC addresses */ 618 if (len > 8) 619 return -ENOSPC; 620 621 /* Parse the array */ 622 err = fbnic_tlv_attr_parse_array(&attr[1], attr_len, mac_results, 623 fbnic_fw_cap_resp_index, 624 FBNIC_FW_CAP_RESP_BMC_MAC_ADDR, len); 625 if (err) 626 return err; 627 628 /* Copy results into MAC addr array */ 629 for (i = 0; i < len && mac_results[i]; i++) 630 fbnic_tlv_attr_addr_copy(bmc_mac_addr[i], mac_results[i]); 631 632 /* Zero remaining unused addresses */ 633 while (i < len) 634 eth_zero_addr(bmc_mac_addr[i++]); 635 636 return 0; 637 } 638 639 static int fbnic_fw_parse_bmc_cap(struct fbnic_dev *fbd, 640 struct fbnic_tlv_msg **results) 641 { 642 struct fbnic_tlv_msg *attr; 643 bool bmc_present = false; 644 u32 all_multi = 0; 645 int err; 646 647 /* FW reports the BMC present once its NC-SI channel is enabled, before 648 * it has a MAC. No MAC array means nothing to program, so treat it as 649 * absent rather than rejecting the message. 650 */ 651 attr = results[FBNIC_FW_CAP_RESP_BMC_PRESENT] ? 652 results[FBNIC_FW_CAP_RESP_BMC_MAC_ARRAY] : NULL; 653 if (attr) { 654 err = fbnic_fw_parse_bmc_addrs(fbd->fw_cap.bmc_mac_addr, 655 attr, 4); 656 if (err) 657 return err; 658 659 all_multi = fta_get_uint(results, 660 FBNIC_FW_CAP_RESP_BMC_ALL_MULTI); 661 bmc_present = true; 662 } else { 663 memset(fbd->fw_cap.bmc_mac_addr, 0, 664 sizeof(fbd->fw_cap.bmc_mac_addr)); 665 } 666 667 /* all_multi is only read out of the message when a BMC is present, so 668 * it is never set on its own and callers can test it without pairing 669 * it with the presence check. 670 */ 671 assign_bit(FBNIC_FW_CAP_F_BMC_PRESENT, &fbd->fw_cap.state, bmc_present); 672 assign_bit(FBNIC_FW_CAP_F_BMC_ALL_MULTI, &fbd->fw_cap.state, all_multi); 673 674 /* Always assume we need a BMC reinit. The barrier orders the BMC state 675 * published above ahead of the flag, pairing with the ordering implied 676 * by the test_and_clear_bit() in fbnic_bmc_rpc_check(). 677 */ 678 smp_mb__before_atomic(); 679 set_bit(FBNIC_FW_CAP_F_BMC_TCAM_REINIT, &fbd->fw_cap.state); 680 681 return 0; 682 } 683 684 static int fbnic_fw_parse_cap_resp(void *opaque, struct fbnic_tlv_msg **results) 685 { 686 struct fbnic_dev *fbd = opaque; 687 u32 version = 0; 688 int err; 689 690 version = fta_get_uint(results, FBNIC_FW_CAP_RESP_VERSION); 691 fbd->fw_cap.running.mgmt.version = version; 692 if (!fbd->fw_cap.running.mgmt.version) 693 return -EINVAL; 694 695 if (fbd->fw_cap.running.mgmt.version < MIN_FW_VER_CODE) { 696 char required_ver[FBNIC_FW_VER_MAX_SIZE]; 697 char running_ver[FBNIC_FW_VER_MAX_SIZE]; 698 699 fbnic_mk_fw_ver_str(fbd->fw_cap.running.mgmt.version, 700 running_ver); 701 fbnic_mk_fw_ver_str(MIN_FW_VER_CODE, required_ver); 702 dev_err(fbd->dev, "Device firmware version(%s) is older than minimum required version(%s)\n", 703 running_ver, required_ver); 704 /* Disable TX mailbox to prevent card use until firmware is 705 * updated. 706 */ 707 fbd->mbx[FBNIC_IPC_MBX_TX_IDX].ready = false; 708 return -EINVAL; 709 } 710 711 if (fta_get_str(results, FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, 712 fbd->fw_cap.running.mgmt.commit, 713 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE) <= 0) 714 dev_warn(fbd->dev, "Firmware did not send mgmt commit!\n"); 715 716 version = fta_get_uint(results, FBNIC_FW_CAP_RESP_STORED_VERSION); 717 fbd->fw_cap.stored.mgmt.version = version; 718 fta_get_str(results, FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, 719 fbd->fw_cap.stored.mgmt.commit, 720 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 721 722 version = fta_get_uint(results, FBNIC_FW_CAP_RESP_CMRT_VERSION); 723 fbd->fw_cap.running.bootloader.version = version; 724 fta_get_str(results, FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, 725 fbd->fw_cap.running.bootloader.commit, 726 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 727 728 version = fta_get_uint(results, FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION); 729 fbd->fw_cap.stored.bootloader.version = version; 730 fta_get_str(results, FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, 731 fbd->fw_cap.stored.bootloader.commit, 732 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 733 734 version = fta_get_uint(results, FBNIC_FW_CAP_RESP_UEFI_VERSION); 735 fbd->fw_cap.stored.undi.version = version; 736 fta_get_str(results, FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, 737 fbd->fw_cap.stored.undi.commit, 738 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 739 740 fbd->fw_cap.active_slot = 741 fta_get_uint(results, FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT); 742 fbd->fw_cap.link_speed = 743 fta_get_uint(results, FBNIC_FW_CAP_RESP_FW_LINK_SPEED); 744 fbd->fw_cap.link_fec = 745 fta_get_uint(results, FBNIC_FW_CAP_RESP_FW_LINK_FEC); 746 747 err = fbnic_fw_parse_bmc_cap(fbd, results); 748 if (err) 749 return err; 750 751 fbd->fw_cap.anti_rollback_version = 752 fta_get_uint(results, FBNIC_FW_CAP_RESP_ANTI_ROLLBACK_VERSION); 753 754 return 0; 755 } 756 757 static const struct fbnic_tlv_index fbnic_ownership_resp_index[] = { 758 FBNIC_TLV_ATTR_U64(FBNIC_FW_OWNERSHIP_TIME), 759 FBNIC_TLV_ATTR_LAST 760 }; 761 762 static int fbnic_fw_parse_ownership_resp(void *opaque, 763 struct fbnic_tlv_msg **results) 764 { 765 struct fbnic_dev *fbd = (struct fbnic_dev *)opaque; 766 767 /* Count the ownership response as a heartbeat reply */ 768 fbd->last_heartbeat_response = jiffies; 769 770 /* Capture firmware time for logging and firmware crash check */ 771 fbd->firmware_time = fta_get_uint(results, FBNIC_FW_OWNERSHIP_TIME); 772 773 return 0; 774 } 775 776 static const struct fbnic_tlv_index fbnic_heartbeat_resp_index[] = { 777 FBNIC_TLV_ATTR_U64(FBNIC_FW_HEARTBEAT_UPTIME), 778 FBNIC_TLV_ATTR_LAST 779 }; 780 781 static int fbnic_fw_parse_heartbeat_resp(void *opaque, 782 struct fbnic_tlv_msg **results) 783 { 784 struct fbnic_dev *fbd = (struct fbnic_dev *)opaque; 785 786 fbd->last_heartbeat_response = jiffies; 787 788 /* Capture firmware time for logging and firmware crash check */ 789 fbd->firmware_time = fta_get_uint(results, FBNIC_FW_HEARTBEAT_UPTIME); 790 791 return 0; 792 } 793 794 static int fbnic_fw_xmit_heartbeat_message(struct fbnic_dev *fbd) 795 { 796 unsigned long req_time = jiffies; 797 struct fbnic_tlv_msg *msg; 798 int err = 0; 799 800 if (!fbnic_fw_present(fbd)) 801 return -ENODEV; 802 803 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_HEARTBEAT_REQ); 804 if (!msg) 805 return -ENOMEM; 806 807 err = fbnic_mbx_map_tlv_msg(fbd, msg); 808 if (err) 809 goto free_message; 810 811 fbd->last_heartbeat_request = req_time; 812 fbd->prev_firmware_time = fbd->firmware_time; 813 814 return err; 815 816 free_message: 817 free_page((unsigned long)msg); 818 return err; 819 } 820 821 static bool fbnic_fw_heartbeat_current(struct fbnic_dev *fbd) 822 { 823 unsigned long last_response = fbd->last_heartbeat_response; 824 unsigned long last_request = fbd->last_heartbeat_request; 825 826 return !time_before(last_response, last_request); 827 } 828 829 int fbnic_fw_init_heartbeat(struct fbnic_dev *fbd, bool poll) 830 { 831 int err = -ETIMEDOUT; 832 int attempts = 50; 833 834 if (!fbnic_fw_present(fbd)) 835 return -ENODEV; 836 837 while (attempts--) { 838 msleep(200); 839 if (poll) 840 fbnic_mbx_poll(fbd); 841 842 if (!fbnic_fw_heartbeat_current(fbd)) 843 continue; 844 845 /* Place new message on mailbox to elicit a response */ 846 err = fbnic_fw_xmit_heartbeat_message(fbd); 847 if (err) 848 dev_warn(fbd->dev, 849 "Failed to send heartbeat message: %d\n", 850 err); 851 break; 852 } 853 854 return err; 855 } 856 857 void fbnic_fw_check_heartbeat(struct fbnic_dev *fbd) 858 { 859 unsigned long last_request = fbd->last_heartbeat_request; 860 int err; 861 862 /* Do not check heartbeat or send another request until current 863 * period has expired. Otherwise we might start spamming requests. 864 */ 865 if (time_is_after_jiffies(last_request + FW_HEARTBEAT_PERIOD)) 866 return; 867 868 /* We already reported no mailbox. Wait for it to come back */ 869 if (!fbd->fw_heartbeat_enabled) 870 return; 871 872 /* Was the last heartbeat response long time ago? */ 873 if (!fbnic_fw_heartbeat_current(fbd) || 874 fbd->firmware_time < fbd->prev_firmware_time) { 875 dev_warn(fbd->dev, 876 "Firmware did not respond to heartbeat message\n"); 877 fbd->fw_heartbeat_enabled = false; 878 } 879 880 /* Place new message on mailbox to elicit a response */ 881 err = fbnic_fw_xmit_heartbeat_message(fbd); 882 if (err) 883 dev_warn(fbd->dev, "Failed to send heartbeat message\n"); 884 } 885 886 /** 887 * fbnic_fw_xmit_coredump_info_msg - Create and transmit a coredump info message 888 * @fbd: FBNIC device structure 889 * @cmpl_data: Structure to store info in 890 * @force: Force coredump event if one hasn't already occurred 891 * 892 * Return: zero on success, negative errno on failure 893 * 894 * Asks the FW for info related to coredump. If a coredump doesn't exist it 895 * can optionally force one if force is true. 896 */ 897 int fbnic_fw_xmit_coredump_info_msg(struct fbnic_dev *fbd, 898 struct fbnic_fw_completion *cmpl_data, 899 bool force) 900 { 901 struct fbnic_tlv_msg *msg; 902 int err = 0; 903 904 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_COREDUMP_GET_INFO_REQ); 905 if (!msg) 906 return -ENOMEM; 907 908 if (force) { 909 err = fbnic_tlv_attr_put_flag(msg, FBNIC_FW_COREDUMP_REQ_INFO_CREATE); 910 if (err) 911 goto free_msg; 912 } 913 914 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 915 if (err) 916 goto free_msg; 917 918 return 0; 919 920 free_msg: 921 free_page((unsigned long)msg); 922 return err; 923 } 924 925 static const struct fbnic_tlv_index fbnic_coredump_info_resp_index[] = { 926 FBNIC_TLV_ATTR_FLAG(FBNIC_FW_COREDUMP_INFO_AVAILABLE), 927 FBNIC_TLV_ATTR_U32(FBNIC_FW_COREDUMP_INFO_SIZE), 928 FBNIC_TLV_ATTR_S32(FBNIC_FW_COREDUMP_INFO_ERROR), 929 FBNIC_TLV_ATTR_LAST 930 }; 931 932 static int 933 fbnic_fw_parse_coredump_info_resp(void *opaque, struct fbnic_tlv_msg **results) 934 { 935 struct fbnic_fw_completion *cmpl_data; 936 struct fbnic_dev *fbd = opaque; 937 u32 msg_type; 938 s32 err; 939 940 /* Verify we have a completion pointer to provide with data */ 941 msg_type = FBNIC_TLV_MSG_ID_COREDUMP_GET_INFO_RESP; 942 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, msg_type); 943 if (!cmpl_data) 944 return -ENOSPC; 945 946 err = fta_get_sint(results, FBNIC_FW_COREDUMP_INFO_ERROR); 947 if (err) 948 goto msg_err; 949 950 if (!results[FBNIC_FW_COREDUMP_INFO_AVAILABLE]) { 951 err = -ENOENT; 952 goto msg_err; 953 } 954 955 cmpl_data->u.coredump_info.size = 956 fta_get_uint(results, FBNIC_FW_COREDUMP_INFO_SIZE); 957 958 msg_err: 959 cmpl_data->result = err; 960 complete(&cmpl_data->done); 961 fbnic_fw_put_cmpl(cmpl_data); 962 963 return err; 964 } 965 966 /** 967 * fbnic_fw_xmit_coredump_read_msg - Create and transmit a coredump read request 968 * @fbd: FBNIC device structure 969 * @cmpl_data: Completion struct to store coredump 970 * @offset: Offset into coredump requested 971 * @length: Length of section of coredump to fetch 972 * 973 * Return: zero on success, negative errno on failure 974 * 975 * Asks the firmware to provide a section of the coredump back in a message. 976 * The response will have an offset and size matching the values provided. 977 */ 978 int fbnic_fw_xmit_coredump_read_msg(struct fbnic_dev *fbd, 979 struct fbnic_fw_completion *cmpl_data, 980 u32 offset, u32 length) 981 { 982 struct fbnic_tlv_msg *msg; 983 int err = 0; 984 985 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_COREDUMP_READ_REQ); 986 if (!msg) 987 return -ENOMEM; 988 989 if (offset) { 990 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_COREDUMP_READ_OFFSET, 991 offset); 992 if (err) 993 goto free_message; 994 } 995 996 if (length) { 997 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_COREDUMP_READ_LENGTH, 998 length); 999 if (err) 1000 goto free_message; 1001 } 1002 1003 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 1004 if (err) 1005 goto free_message; 1006 1007 return 0; 1008 1009 free_message: 1010 free_page((unsigned long)msg); 1011 return err; 1012 } 1013 1014 static const struct fbnic_tlv_index fbnic_coredump_resp_index[] = { 1015 FBNIC_TLV_ATTR_U32(FBNIC_FW_COREDUMP_READ_OFFSET), 1016 FBNIC_TLV_ATTR_U32(FBNIC_FW_COREDUMP_READ_LENGTH), 1017 FBNIC_TLV_ATTR_RAW_DATA(FBNIC_FW_COREDUMP_READ_DATA), 1018 FBNIC_TLV_ATTR_S32(FBNIC_FW_COREDUMP_READ_ERROR), 1019 FBNIC_TLV_ATTR_LAST 1020 }; 1021 1022 static int fbnic_fw_parse_coredump_resp(void *opaque, 1023 struct fbnic_tlv_msg **results) 1024 { 1025 struct fbnic_fw_completion *cmpl_data; 1026 u32 index, last_offset, last_length; 1027 struct fbnic_dev *fbd = opaque; 1028 struct fbnic_tlv_msg *data_hdr; 1029 u32 length, offset; 1030 u32 msg_type; 1031 s32 err; 1032 1033 /* Verify we have a completion pointer to provide with data */ 1034 msg_type = FBNIC_TLV_MSG_ID_COREDUMP_READ_RESP; 1035 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, msg_type); 1036 if (!cmpl_data) 1037 return -ENOSPC; 1038 1039 err = fta_get_sint(results, FBNIC_FW_COREDUMP_READ_ERROR); 1040 if (err) 1041 goto msg_err; 1042 1043 data_hdr = results[FBNIC_FW_COREDUMP_READ_DATA]; 1044 if (!data_hdr) { 1045 err = -ENODATA; 1046 goto msg_err; 1047 } 1048 1049 offset = fta_get_uint(results, FBNIC_FW_COREDUMP_READ_OFFSET); 1050 length = fta_get_uint(results, FBNIC_FW_COREDUMP_READ_LENGTH); 1051 1052 if (length > le16_to_cpu(data_hdr->hdr.len) - sizeof(u32)) { 1053 dev_err(fbd->dev, "length greater than size of message\n"); 1054 err = -EINVAL; 1055 goto msg_err; 1056 } 1057 1058 /* Only the last offset can have a length != stride */ 1059 last_length = 1060 (cmpl_data->u.coredump.size % cmpl_data->u.coredump.stride) ? : 1061 cmpl_data->u.coredump.stride; 1062 last_offset = cmpl_data->u.coredump.size - last_length; 1063 1064 /* Verify offset and length */ 1065 if (offset % cmpl_data->u.coredump.stride || offset > last_offset) { 1066 dev_err(fbd->dev, "offset %d out of range\n", offset); 1067 err = -EINVAL; 1068 } else if (length != ((offset == last_offset) ? 1069 last_length : cmpl_data->u.coredump.stride)) { 1070 dev_err(fbd->dev, "length %d out of range for offset %d\n", 1071 length, offset); 1072 err = -EINVAL; 1073 } 1074 if (err) 1075 goto msg_err; 1076 1077 /* If data pointer is NULL it is already filled, just skip the copy */ 1078 index = offset / cmpl_data->u.coredump.stride; 1079 if (!cmpl_data->u.coredump.data[index]) 1080 goto msg_err; 1081 1082 /* Copy data and mark index filled by setting pointer to NULL */ 1083 memcpy(cmpl_data->u.coredump.data[index], 1084 fbnic_tlv_attr_get_value_ptr(data_hdr), length); 1085 cmpl_data->u.coredump.data[index] = NULL; 1086 1087 msg_err: 1088 cmpl_data->result = err; 1089 complete(&cmpl_data->done); 1090 fbnic_fw_put_cmpl(cmpl_data); 1091 1092 return err; 1093 } 1094 1095 int fbnic_fw_xmit_fw_start_upgrade(struct fbnic_dev *fbd, 1096 struct fbnic_fw_completion *cmpl_data, 1097 unsigned int id, unsigned int len) 1098 { 1099 struct fbnic_tlv_msg *msg; 1100 int err; 1101 1102 if (!fbnic_fw_present(fbd)) 1103 return -ENODEV; 1104 1105 if (!len) 1106 return -EINVAL; 1107 1108 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_FW_START_UPGRADE_REQ); 1109 if (!msg) 1110 return -ENOMEM; 1111 1112 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_START_UPGRADE_SECTION, id); 1113 if (err) 1114 goto free_message; 1115 1116 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_START_UPGRADE_IMAGE_LENGTH, 1117 len); 1118 if (err) 1119 goto free_message; 1120 1121 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 1122 if (err) 1123 goto free_message; 1124 1125 return 0; 1126 1127 free_message: 1128 free_page((unsigned long)msg); 1129 return err; 1130 } 1131 1132 static const struct fbnic_tlv_index fbnic_fw_start_upgrade_resp_index[] = { 1133 FBNIC_TLV_ATTR_S32(FBNIC_FW_START_UPGRADE_ERROR), 1134 FBNIC_TLV_ATTR_LAST 1135 }; 1136 1137 static int fbnic_fw_parse_fw_start_upgrade_resp(void *opaque, 1138 struct fbnic_tlv_msg **results) 1139 { 1140 struct fbnic_fw_completion *cmpl_data; 1141 struct fbnic_dev *fbd = opaque; 1142 u32 msg_type; 1143 s32 err; 1144 1145 /* Verify we have a completion pointer */ 1146 msg_type = FBNIC_TLV_MSG_ID_FW_START_UPGRADE_REQ; 1147 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, msg_type); 1148 if (!cmpl_data) 1149 return -ENOSPC; 1150 1151 /* Check for errors */ 1152 err = fta_get_sint(results, FBNIC_FW_START_UPGRADE_ERROR); 1153 1154 cmpl_data->result = err; 1155 complete(&cmpl_data->done); 1156 fbnic_fw_put_cmpl(cmpl_data); 1157 1158 return 0; 1159 } 1160 1161 int fbnic_fw_xmit_fw_write_chunk(struct fbnic_dev *fbd, 1162 const u8 *data, u32 offset, u16 length, 1163 int cancel_error) 1164 { 1165 struct fbnic_tlv_msg *msg; 1166 int err; 1167 1168 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_FW_WRITE_CHUNK_RESP); 1169 if (!msg) 1170 return -ENOMEM; 1171 1172 /* Report error to FW to cancel upgrade */ 1173 if (cancel_error) { 1174 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_WRITE_CHUNK_ERROR, 1175 cancel_error); 1176 if (err) 1177 goto free_message; 1178 } 1179 1180 if (data) { 1181 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_WRITE_CHUNK_OFFSET, 1182 offset); 1183 if (err) 1184 goto free_message; 1185 1186 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_WRITE_CHUNK_LENGTH, 1187 length); 1188 if (err) 1189 goto free_message; 1190 1191 err = fbnic_tlv_attr_put_value(msg, FBNIC_FW_WRITE_CHUNK_DATA, 1192 data + offset, length); 1193 if (err) 1194 goto free_message; 1195 } 1196 1197 err = fbnic_mbx_map_tlv_msg(fbd, msg); 1198 if (err) 1199 goto free_message; 1200 1201 return 0; 1202 1203 free_message: 1204 free_page((unsigned long)msg); 1205 return err; 1206 } 1207 1208 static const struct fbnic_tlv_index fbnic_fw_write_chunk_req_index[] = { 1209 FBNIC_TLV_ATTR_U32(FBNIC_FW_WRITE_CHUNK_OFFSET), 1210 FBNIC_TLV_ATTR_U32(FBNIC_FW_WRITE_CHUNK_LENGTH), 1211 FBNIC_TLV_ATTR_LAST 1212 }; 1213 1214 static int fbnic_fw_parse_fw_write_chunk_req(void *opaque, 1215 struct fbnic_tlv_msg **results) 1216 { 1217 struct fbnic_fw_completion *cmpl_data; 1218 struct fbnic_dev *fbd = opaque; 1219 u32 msg_type; 1220 u32 offset; 1221 u32 length; 1222 1223 /* Verify we have a completion pointer */ 1224 msg_type = FBNIC_TLV_MSG_ID_FW_WRITE_CHUNK_REQ; 1225 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, msg_type); 1226 if (!cmpl_data) 1227 return -ENOSPC; 1228 1229 /* Pull length/offset pair and mark it as complete */ 1230 offset = fta_get_uint(results, FBNIC_FW_WRITE_CHUNK_OFFSET); 1231 length = fta_get_uint(results, FBNIC_FW_WRITE_CHUNK_LENGTH); 1232 cmpl_data->u.fw_update.offset = offset; 1233 cmpl_data->u.fw_update.length = length; 1234 1235 complete(&cmpl_data->done); 1236 fbnic_fw_put_cmpl(cmpl_data); 1237 1238 return 0; 1239 } 1240 1241 static const struct fbnic_tlv_index fbnic_fw_finish_upgrade_req_index[] = { 1242 FBNIC_TLV_ATTR_S32(FBNIC_FW_FINISH_UPGRADE_ERROR), 1243 FBNIC_TLV_ATTR_LAST 1244 }; 1245 1246 static int fbnic_fw_parse_fw_finish_upgrade_req(void *opaque, 1247 struct fbnic_tlv_msg **results) 1248 { 1249 struct fbnic_fw_completion *cmpl_data; 1250 struct fbnic_dev *fbd = opaque; 1251 u32 msg_type; 1252 s32 err; 1253 1254 /* Verify we have a completion pointer */ 1255 msg_type = FBNIC_TLV_MSG_ID_FW_WRITE_CHUNK_REQ; 1256 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, msg_type); 1257 if (!cmpl_data) 1258 return -ENOSPC; 1259 1260 /* Check for errors */ 1261 err = fta_get_sint(results, FBNIC_FW_FINISH_UPGRADE_ERROR); 1262 1263 /* Close out update by incrementing offset by length which should 1264 * match the total size of the component. Set length to 0 since no 1265 * new chunks will be requested. 1266 */ 1267 cmpl_data->u.fw_update.offset += cmpl_data->u.fw_update.length; 1268 cmpl_data->u.fw_update.length = 0; 1269 1270 cmpl_data->result = err; 1271 complete(&cmpl_data->done); 1272 fbnic_fw_put_cmpl(cmpl_data); 1273 1274 return 0; 1275 } 1276 1277 /** 1278 * fbnic_fw_xmit_qsfp_read_msg - Transmit a QSFP read request 1279 * @fbd: FBNIC device structure 1280 * @cmpl_data: Structure to store EEPROM response in 1281 * @page: Refers to page number on page enabled QSFP modules 1282 * @bank: Refers to a collection of pages 1283 * @offset: Offset into QSFP EEPROM requested 1284 * @length: Length of section of QSFP EEPROM to fetch 1285 * 1286 * Return: zero on success, negative value on failure 1287 * 1288 * Asks the firmware to provide a section of the QSFP EEPROM back in a 1289 * message. The response will have an offset and size matching the values 1290 * provided. 1291 */ 1292 int fbnic_fw_xmit_qsfp_read_msg(struct fbnic_dev *fbd, 1293 struct fbnic_fw_completion *cmpl_data, 1294 u32 page, u32 bank, u32 offset, u32 length) 1295 { 1296 struct fbnic_tlv_msg *msg; 1297 int err = 0; 1298 1299 if (!length || length > TLV_MAX_DATA) 1300 return -EINVAL; 1301 1302 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_QSFP_READ_REQ); 1303 if (!msg) 1304 return -ENOMEM; 1305 1306 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_QSFP_BANK, bank); 1307 if (err) 1308 goto free_message; 1309 1310 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_QSFP_PAGE, page); 1311 if (err) 1312 goto free_message; 1313 1314 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_QSFP_OFFSET, offset); 1315 if (err) 1316 goto free_message; 1317 1318 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_QSFP_LENGTH, length); 1319 if (err) 1320 goto free_message; 1321 1322 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 1323 if (err) 1324 goto free_message; 1325 1326 return 0; 1327 1328 free_message: 1329 free_page((unsigned long)msg); 1330 return err; 1331 } 1332 1333 static const struct fbnic_tlv_index fbnic_qsfp_read_resp_index[] = { 1334 FBNIC_TLV_ATTR_U32(FBNIC_FW_QSFP_BANK), 1335 FBNIC_TLV_ATTR_U32(FBNIC_FW_QSFP_PAGE), 1336 FBNIC_TLV_ATTR_U32(FBNIC_FW_QSFP_OFFSET), 1337 FBNIC_TLV_ATTR_U32(FBNIC_FW_QSFP_LENGTH), 1338 FBNIC_TLV_ATTR_RAW_DATA(FBNIC_FW_QSFP_DATA), 1339 FBNIC_TLV_ATTR_S32(FBNIC_FW_QSFP_ERROR), 1340 FBNIC_TLV_ATTR_LAST 1341 }; 1342 1343 static int fbnic_fw_parse_qsfp_read_resp(void *opaque, 1344 struct fbnic_tlv_msg **results) 1345 { 1346 struct fbnic_fw_completion *cmpl_data; 1347 struct fbnic_dev *fbd = opaque; 1348 struct fbnic_tlv_msg *data_hdr; 1349 u32 length, offset, page, bank; 1350 u8 *data; 1351 s32 err; 1352 1353 /* Verify we have a completion pointer to provide with data */ 1354 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, 1355 FBNIC_TLV_MSG_ID_QSFP_READ_RESP); 1356 if (!cmpl_data) 1357 return -ENOSPC; 1358 1359 bank = fta_get_uint(results, FBNIC_FW_QSFP_BANK); 1360 if (bank != cmpl_data->u.qsfp.bank) { 1361 dev_warn(fbd->dev, "bank not equal to bank requested: %d vs %d\n", 1362 bank, cmpl_data->u.qsfp.bank); 1363 err = -EINVAL; 1364 goto msg_err; 1365 } 1366 1367 page = fta_get_uint(results, FBNIC_FW_QSFP_PAGE); 1368 if (page != cmpl_data->u.qsfp.page) { 1369 dev_warn(fbd->dev, "page not equal to page requested: %d vs %d\n", 1370 page, cmpl_data->u.qsfp.page); 1371 err = -EINVAL; 1372 goto msg_err; 1373 } 1374 1375 offset = fta_get_uint(results, FBNIC_FW_QSFP_OFFSET); 1376 length = fta_get_uint(results, FBNIC_FW_QSFP_LENGTH); 1377 1378 if (length != cmpl_data->u.qsfp.length || 1379 offset != cmpl_data->u.qsfp.offset) { 1380 dev_warn(fbd->dev, 1381 "offset/length not equal to size requested: %d/%d vs %d/%d\n", 1382 offset, length, 1383 cmpl_data->u.qsfp.offset, cmpl_data->u.qsfp.length); 1384 err = -EINVAL; 1385 goto msg_err; 1386 } 1387 1388 err = fta_get_sint(results, FBNIC_FW_QSFP_ERROR); 1389 if (err) 1390 goto msg_err; 1391 1392 data_hdr = results[FBNIC_FW_QSFP_DATA]; 1393 if (!data_hdr) { 1394 err = -ENODATA; 1395 goto msg_err; 1396 } 1397 1398 /* Copy data */ 1399 data = fbnic_tlv_attr_get_value_ptr(data_hdr); 1400 memcpy(cmpl_data->u.qsfp.data, data, length); 1401 msg_err: 1402 cmpl_data->result = err; 1403 complete(&cmpl_data->done); 1404 fbnic_fw_put_cmpl(cmpl_data); 1405 1406 return err; 1407 } 1408 1409 /** 1410 * fbnic_fw_xmit_tsene_read_msg - Create and transmit a sensor read request 1411 * @fbd: FBNIC device structure 1412 * @cmpl_data: Completion data structure to store sensor response 1413 * 1414 * Asks the firmware to provide an update with the latest sensor data. 1415 * The response will contain temperature and voltage readings. 1416 * 1417 * Return: 0 on success, negative error value on failure 1418 */ 1419 int fbnic_fw_xmit_tsene_read_msg(struct fbnic_dev *fbd, 1420 struct fbnic_fw_completion *cmpl_data) 1421 { 1422 struct fbnic_tlv_msg *msg; 1423 int err; 1424 1425 if (!fbnic_fw_present(fbd)) 1426 return -ENODEV; 1427 1428 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_TSENE_READ_REQ); 1429 if (!msg) 1430 return -ENOMEM; 1431 1432 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 1433 if (err) 1434 goto free_message; 1435 1436 return 0; 1437 1438 free_message: 1439 free_page((unsigned long)msg); 1440 return err; 1441 } 1442 1443 static const struct fbnic_tlv_index fbnic_tsene_read_resp_index[] = { 1444 FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_THERM), 1445 FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_VOLT), 1446 FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_ERROR), 1447 FBNIC_TLV_ATTR_LAST 1448 }; 1449 1450 static int fbnic_fw_parse_tsene_read_resp(void *opaque, 1451 struct fbnic_tlv_msg **results) 1452 { 1453 struct fbnic_fw_completion *cmpl_data; 1454 struct fbnic_dev *fbd = opaque; 1455 s32 err_resp; 1456 int err = 0; 1457 1458 /* Verify we have a completion pointer to provide with data */ 1459 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, 1460 FBNIC_TLV_MSG_ID_TSENE_READ_RESP); 1461 if (!cmpl_data) 1462 return -ENOSPC; 1463 1464 err_resp = fta_get_sint(results, FBNIC_FW_TSENE_ERROR); 1465 if (err_resp) 1466 goto msg_err; 1467 1468 if (!results[FBNIC_FW_TSENE_THERM] || !results[FBNIC_FW_TSENE_VOLT]) { 1469 err = -EINVAL; 1470 goto msg_err; 1471 } 1472 1473 cmpl_data->u.tsene.millidegrees = 1474 fta_get_sint(results, FBNIC_FW_TSENE_THERM); 1475 cmpl_data->u.tsene.millivolts = 1476 fta_get_sint(results, FBNIC_FW_TSENE_VOLT); 1477 1478 msg_err: 1479 cmpl_data->result = err_resp ? : err; 1480 complete(&cmpl_data->done); 1481 fbnic_fw_put_cmpl(cmpl_data); 1482 1483 return err; 1484 } 1485 1486 static const struct fbnic_tlv_index fbnic_fw_log_req_index[] = { 1487 FBNIC_TLV_ATTR_U32(FBNIC_FW_LOG_MSEC), 1488 FBNIC_TLV_ATTR_U64(FBNIC_FW_LOG_INDEX), 1489 FBNIC_TLV_ATTR_STRING(FBNIC_FW_LOG_MSG, FBNIC_FW_LOG_MAX_SIZE), 1490 FBNIC_TLV_ATTR_U32(FBNIC_FW_LOG_LENGTH), 1491 FBNIC_TLV_ATTR_ARRAY(FBNIC_FW_LOG_MSEC_ARRAY), 1492 FBNIC_TLV_ATTR_ARRAY(FBNIC_FW_LOG_INDEX_ARRAY), 1493 FBNIC_TLV_ATTR_ARRAY(FBNIC_FW_LOG_MSG_ARRAY), 1494 FBNIC_TLV_ATTR_LAST 1495 }; 1496 1497 static int fbnic_fw_process_log_array(struct fbnic_tlv_msg **results, 1498 u16 length, u16 arr_type_idx, 1499 u16 attr_type_idx, 1500 struct fbnic_tlv_msg **tlv_array_out) 1501 { 1502 struct fbnic_tlv_msg *attr; 1503 int attr_len; 1504 int err; 1505 1506 if (!results[attr_type_idx]) 1507 return -EINVAL; 1508 1509 tlv_array_out[0] = results[attr_type_idx]; 1510 1511 if (!length) 1512 return 0; 1513 1514 if (!results[arr_type_idx]) 1515 return -EINVAL; 1516 1517 attr = results[arr_type_idx]; 1518 attr_len = le16_to_cpu(attr->hdr.len) / sizeof(u32) - 1; 1519 err = fbnic_tlv_attr_parse_array(&attr[1], attr_len, &tlv_array_out[1], 1520 fbnic_fw_log_req_index, 1521 attr_type_idx, 1522 length); 1523 if (err) 1524 return err; 1525 1526 return 0; 1527 } 1528 1529 static int fbnic_fw_parse_logs(struct fbnic_dev *fbd, 1530 struct fbnic_tlv_msg **msec_tlv, 1531 struct fbnic_tlv_msg **index_tlv, 1532 struct fbnic_tlv_msg **log_tlv, 1533 int count) 1534 { 1535 int i; 1536 1537 for (i = 0; i < count; i++) { 1538 char log[FBNIC_FW_LOG_MAX_SIZE]; 1539 ssize_t len; 1540 u64 index; 1541 u32 msec; 1542 int err; 1543 1544 if (!msec_tlv[i] || !index_tlv[i] || !log_tlv[i]) { 1545 dev_warn(fbd->dev, "Received log message with missing attributes!\n"); 1546 return -EINVAL; 1547 } 1548 1549 index = fbnic_tlv_attr_get_signed(index_tlv[i], 0); 1550 msec = fbnic_tlv_attr_get_signed(msec_tlv[i], 0); 1551 len = fbnic_tlv_attr_get_string(log_tlv[i], log, 1552 FBNIC_FW_LOG_MAX_SIZE); 1553 if (len < 0) 1554 return len; 1555 1556 err = fbnic_fw_log_write(fbd, index, msec, log); 1557 if (err) 1558 return err; 1559 } 1560 1561 return 0; 1562 } 1563 1564 static int fbnic_fw_parse_log_req(void *opaque, 1565 struct fbnic_tlv_msg **results) 1566 { 1567 struct fbnic_tlv_msg *index_tlv[FBNIC_FW_MAX_LOG_HISTORY]; 1568 struct fbnic_tlv_msg *msec_tlv[FBNIC_FW_MAX_LOG_HISTORY]; 1569 struct fbnic_tlv_msg *log_tlv[FBNIC_FW_MAX_LOG_HISTORY]; 1570 struct fbnic_dev *fbd = opaque; 1571 u16 length; 1572 int err; 1573 1574 length = fta_get_uint(results, FBNIC_FW_LOG_LENGTH); 1575 if (length >= FBNIC_FW_MAX_LOG_HISTORY) 1576 return -E2BIG; 1577 1578 err = fbnic_fw_process_log_array(results, length, 1579 FBNIC_FW_LOG_MSEC_ARRAY, 1580 FBNIC_FW_LOG_MSEC, msec_tlv); 1581 if (err) 1582 return err; 1583 1584 err = fbnic_fw_process_log_array(results, length, 1585 FBNIC_FW_LOG_INDEX_ARRAY, 1586 FBNIC_FW_LOG_INDEX, index_tlv); 1587 if (err) 1588 return err; 1589 1590 err = fbnic_fw_process_log_array(results, length, 1591 FBNIC_FW_LOG_MSG_ARRAY, 1592 FBNIC_FW_LOG_MSG, log_tlv); 1593 if (err) 1594 return err; 1595 1596 err = fbnic_fw_parse_logs(fbd, msec_tlv, index_tlv, log_tlv, 1597 length + 1); 1598 if (err) 1599 return err; 1600 1601 return 0; 1602 } 1603 1604 int fbnic_fw_xmit_send_logs(struct fbnic_dev *fbd, bool enable, 1605 bool send_log_history) 1606 { 1607 struct fbnic_tlv_msg *msg; 1608 int err; 1609 1610 if (fbd->fw_cap.running.mgmt.version < MIN_FW_VER_CODE_LOG) { 1611 dev_warn(fbd->dev, "Firmware version is too old to support firmware logs!\n"); 1612 return -EOPNOTSUPP; 1613 } 1614 1615 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_LOG_SEND_LOGS_REQ); 1616 if (!msg) 1617 return -ENOMEM; 1618 1619 if (enable) { 1620 err = fbnic_tlv_attr_put_flag(msg, FBNIC_SEND_LOGS); 1621 if (err) 1622 goto free_message; 1623 1624 /* Report request for version 1 of logs */ 1625 err = fbnic_tlv_attr_put_int(msg, FBNIC_SEND_LOGS_VERSION, 1626 FBNIC_FW_LOG_VERSION); 1627 if (err) 1628 goto free_message; 1629 1630 if (send_log_history) { 1631 err = fbnic_tlv_attr_put_flag(msg, 1632 FBNIC_SEND_LOGS_HISTORY); 1633 if (err) 1634 goto free_message; 1635 } 1636 } 1637 1638 err = fbnic_mbx_map_tlv_msg(fbd, msg); 1639 if (err) 1640 goto free_message; 1641 1642 return 0; 1643 1644 free_message: 1645 free_page((unsigned long)msg); 1646 return err; 1647 } 1648 1649 static int 1650 fbnic_fw_parser_test(void *opaque, struct fbnic_tlv_msg **results) 1651 { 1652 struct fbnic_fw_completion *cmpl; 1653 struct fbnic_dev *fbd = opaque; 1654 int err; 1655 1656 /* find cmpl */ 1657 cmpl = fbnic_fw_get_cmpl_by_type(fbd, FBNIC_TLV_MSG_ID_TEST); 1658 if (!cmpl) 1659 return -ENOSPC; 1660 1661 err = fbnic_tlv_parser_test(opaque, results); 1662 1663 cmpl->result = err; 1664 complete(&cmpl->done); 1665 fbnic_fw_put_cmpl(cmpl); 1666 1667 return err; 1668 } 1669 1670 static const struct fbnic_tlv_parser fbnic_fw_tlv_parser[] = { 1671 FBNIC_TLV_PARSER(TEST, fbnic_tlv_test_index, fbnic_fw_parser_test), 1672 FBNIC_TLV_PARSER(FW_CAP_RESP, fbnic_fw_cap_resp_index, 1673 fbnic_fw_parse_cap_resp), 1674 FBNIC_TLV_PARSER(OWNERSHIP_RESP, fbnic_ownership_resp_index, 1675 fbnic_fw_parse_ownership_resp), 1676 FBNIC_TLV_PARSER(HEARTBEAT_RESP, fbnic_heartbeat_resp_index, 1677 fbnic_fw_parse_heartbeat_resp), 1678 FBNIC_TLV_PARSER(COREDUMP_GET_INFO_RESP, 1679 fbnic_coredump_info_resp_index, 1680 fbnic_fw_parse_coredump_info_resp), 1681 FBNIC_TLV_PARSER(COREDUMP_READ_RESP, fbnic_coredump_resp_index, 1682 fbnic_fw_parse_coredump_resp), 1683 FBNIC_TLV_PARSER(FW_START_UPGRADE_RESP, 1684 fbnic_fw_start_upgrade_resp_index, 1685 fbnic_fw_parse_fw_start_upgrade_resp), 1686 FBNIC_TLV_PARSER(FW_WRITE_CHUNK_REQ, 1687 fbnic_fw_write_chunk_req_index, 1688 fbnic_fw_parse_fw_write_chunk_req), 1689 FBNIC_TLV_PARSER(FW_FINISH_UPGRADE_REQ, 1690 fbnic_fw_finish_upgrade_req_index, 1691 fbnic_fw_parse_fw_finish_upgrade_req), 1692 FBNIC_TLV_PARSER(QSFP_READ_RESP, 1693 fbnic_qsfp_read_resp_index, 1694 fbnic_fw_parse_qsfp_read_resp), 1695 FBNIC_TLV_PARSER(TSENE_READ_RESP, 1696 fbnic_tsene_read_resp_index, 1697 fbnic_fw_parse_tsene_read_resp), 1698 FBNIC_TLV_PARSER(LOG_MSG_REQ, 1699 fbnic_fw_log_req_index, 1700 fbnic_fw_parse_log_req), 1701 FBNIC_TLV_MSG_ERROR 1702 }; 1703 1704 static void fbnic_mbx_process_rx_msgs(struct fbnic_dev *fbd) 1705 { 1706 struct fbnic_fw_mbx *rx_mbx = &fbd->mbx[FBNIC_IPC_MBX_RX_IDX]; 1707 u8 head = rx_mbx->head, tail = rx_mbx->tail; 1708 u64 desc, length; 1709 1710 while (head != rx_mbx->tail) { 1711 struct fbnic_tlv_msg *msg; 1712 int err; 1713 1714 desc = __fbnic_mbx_rd_desc(fbd, FBNIC_IPC_MBX_RX_IDX, head); 1715 if (!(desc & FBNIC_IPC_MBX_DESC_FW_CMPL)) 1716 break; 1717 1718 if (desc & FBNIC_IPC_MBX_DESC_FW_ERR) { 1719 rx_mbx->resp_error++; 1720 dev_warn_ratelimited(fbd->dev, 1721 "FW reported an error on an Rx mailbox message; dropping\n"); 1722 goto next_page; 1723 } 1724 1725 dma_sync_single_for_cpu(fbd->dev, rx_mbx->buf_info[head].addr, 1726 FBNIC_RX_PAGE_SIZE, DMA_FROM_DEVICE); 1727 1728 msg = rx_mbx->buf_info[head].msg; 1729 1730 length = FIELD_GET(FBNIC_IPC_MBX_DESC_LEN_MASK, desc); 1731 1732 /* Ignore NULL mailbox descriptors */ 1733 if (!length) 1734 goto next_page; 1735 1736 /* Report descriptors with invalid message extents. */ 1737 if (length < sizeof(msg->hdr) || length > PAGE_SIZE) { 1738 dev_warn(fbd->dev, 1739 "Invalid mailbox descriptor length: %lld\n", 1740 length); 1741 goto next_page; 1742 } 1743 1744 if (!le16_to_cpu(msg->hdr.len) || 1745 le16_to_cpu(msg->hdr.len) * sizeof(u32) > length) { 1746 dev_warn(fbd->dev, "Mailbox message length mismatch\n"); 1747 goto next_page; 1748 } 1749 1750 /* If parsing fails dump contents of message to dmesg */ 1751 err = fbnic_tlv_msg_parse(fbd, msg, fbnic_fw_tlv_parser); 1752 if (err) { 1753 dev_warn(fbd->dev, "Unable to process message: %d\n", 1754 err); 1755 print_hex_dump(KERN_WARNING, "fbnic:", 1756 DUMP_PREFIX_OFFSET, 16, 2, 1757 msg, length, true); 1758 } 1759 1760 dev_dbg(fbd->dev, "Parsed msg type %d\n", msg->hdr.type); 1761 next_page: 1762 fw_wr32(fbd, FBNIC_IPC_MBX(FBNIC_IPC_MBX_RX_IDX, head), 0); 1763 1764 rx_mbx->buf_info[tail] = rx_mbx->buf_info[head]; 1765 rx_mbx->buf_info[head].msg = NULL; 1766 rx_mbx->buf_info[head].addr = 0; 1767 1768 __fbnic_mbx_wr_desc(fbd, FBNIC_IPC_MBX_RX_IDX, tail, 1769 FIELD_PREP(FBNIC_IPC_MBX_DESC_LEN_MASK, 1770 FBNIC_RX_PAGE_SIZE) | 1771 (rx_mbx->buf_info[tail].addr & 1772 FBNIC_IPC_MBX_DESC_ADDR_MASK) | 1773 FBNIC_IPC_MBX_DESC_HOST_CMPL); 1774 1775 head = (head + 1) & (FBNIC_IPC_MBX_DESC_LEN - 1); 1776 tail = (tail + 1) & (FBNIC_IPC_MBX_DESC_LEN - 1); 1777 } 1778 1779 /* Record head for next interrupt */ 1780 rx_mbx->head = head; 1781 rx_mbx->tail = tail; 1782 } 1783 1784 void fbnic_mbx_poll(struct fbnic_dev *fbd) 1785 { 1786 fbnic_mbx_event(fbd); 1787 1788 fbnic_mbx_process_tx_msgs(fbd); 1789 fbnic_mbx_process_rx_msgs(fbd); 1790 } 1791 1792 int fbnic_mbx_poll_tx_ready(struct fbnic_dev *fbd) 1793 { 1794 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 1795 struct fbnic_fw_mbx *rx_mbx = &fbd->mbx[FBNIC_IPC_MBX_RX_IDX]; 1796 unsigned long timeout = jiffies + 10 * HZ + 1; 1797 u64 tx_resp_error, rx_resp_error; 1798 int err, i; 1799 1800 do { 1801 if (!time_is_after_jiffies(timeout)) 1802 return -ETIMEDOUT; 1803 1804 /* Force the firmware to trigger an interrupt response to 1805 * avoid the mailbox getting stuck closed if the interrupt 1806 * is reset. 1807 */ 1808 fbnic_mbx_reset_desc_ring(fbd, FBNIC_IPC_MBX_TX_IDX); 1809 1810 /* Immediate fail if BAR4 went away */ 1811 if (!fbnic_fw_present(fbd)) 1812 return -ENODEV; 1813 1814 msleep(20); 1815 } while (!fbnic_mbx_event(fbd)); 1816 1817 /* FW has shown signs of life. Enable DMA and start Tx/Rx */ 1818 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) { 1819 err = fbnic_mbx_init_desc_ring(fbd, i); 1820 if (err) 1821 goto clean_mbx; 1822 } 1823 1824 /* Request an update from the firmware. This should overwrite 1825 * mgmt.version once we get the actual version from the firmware 1826 * in the capabilities request message. 1827 */ 1828 send_cap_req: 1829 tx_resp_error = tx_mbx->resp_error; 1830 rx_resp_error = rx_mbx->resp_error; 1831 err = fbnic_fw_xmit_simple_msg(fbd, FBNIC_TLV_MSG_ID_HOST_CAP_REQ); 1832 if (err) 1833 goto clean_mbx; 1834 1835 /* Poll until we get a current management firmware version, use "1" 1836 * to indicate we entered the polling state waiting for a response 1837 */ 1838 for (fbd->fw_cap.running.mgmt.version = 1; 1839 fbd->fw_cap.running.mgmt.version < MIN_FW_VER_CODE;) { 1840 if (!tx_mbx->ready) 1841 err = -ENODEV; 1842 if (err) 1843 goto clean_mbx; 1844 1845 msleep(20); 1846 fbnic_mbx_poll(fbd); 1847 1848 /* A valid capabilities response ends the poll. Check it 1849 * before the FW_ERR retry below so a response parsed in the 1850 * same poll as an unrelated FW_ERR is not discarded. 1851 */ 1852 if (fbd->fw_cap.running.mgmt.version >= MIN_FW_VER_CODE) 1853 break; 1854 1855 /* set err, but wait till mgmt.version check to report it */ 1856 if (!time_is_after_jiffies(timeout)) { 1857 err = -ETIMEDOUT; 1858 continue; 1859 } 1860 1861 /* The FW can flag our capabilities request (Tx) or its 1862 * response (Rx) with FW_ERR, in which case it produced no 1863 * usable response. The ring is not wedged, so re-issue the 1864 * request instead of spinning until the timeout. 1865 */ 1866 if (tx_mbx->resp_error != tx_resp_error || 1867 rx_mbx->resp_error != rx_resp_error) 1868 goto send_cap_req; 1869 } 1870 1871 return 0; 1872 clean_mbx: 1873 /* Cleanup Rx buffers and disable mailbox */ 1874 fbnic_mbx_clean(fbd); 1875 return err; 1876 } 1877 1878 static void __fbnic_fw_evict_cmpl(struct fbnic_fw_completion *cmpl_data) 1879 { 1880 cmpl_data->result = -EPIPE; 1881 complete(&cmpl_data->done); 1882 } 1883 1884 static void fbnic_mbx_evict_all_cmpl(struct fbnic_dev *fbd) 1885 { 1886 int i; 1887 1888 for (i = 0; i < FBNIC_MBX_CMPL_SLOTS; i++) { 1889 struct fbnic_fw_completion *cmpl_data = fbd->cmpl_data[i]; 1890 1891 if (cmpl_data) 1892 __fbnic_fw_evict_cmpl(cmpl_data); 1893 } 1894 1895 memset(fbd->cmpl_data, 0, sizeof(fbd->cmpl_data)); 1896 } 1897 1898 void fbnic_mbx_flush_tx(struct fbnic_dev *fbd) 1899 { 1900 unsigned long timeout = jiffies + 10 * HZ + 1; 1901 struct fbnic_fw_mbx *tx_mbx; 1902 u8 tail; 1903 1904 /* Record current Rx stats */ 1905 tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 1906 1907 spin_lock_irq(&fbd->fw_tx_lock); 1908 1909 /* Clear ready to prevent any further attempts to transmit */ 1910 tx_mbx->ready = false; 1911 1912 /* Read tail to determine the last tail state for the ring */ 1913 tail = tx_mbx->tail; 1914 1915 /* Flush any completions as we are no longer processing Rx */ 1916 fbnic_mbx_evict_all_cmpl(fbd); 1917 1918 spin_unlock_irq(&fbd->fw_tx_lock); 1919 1920 /* Give firmware time to process packet, 1921 * we will wait up to 10 seconds which is 500 waits of 20ms. 1922 */ 1923 do { 1924 u8 head = tx_mbx->head; 1925 1926 /* Tx ring is empty once head == tail */ 1927 if (head == tail) 1928 break; 1929 1930 msleep(20); 1931 fbnic_mbx_process_tx_msgs(fbd); 1932 } while (time_is_after_jiffies(timeout)); 1933 } 1934 1935 /** 1936 * fbnic_fw_mbx_self_test() - verify firmware interface 1937 * @fbd: device to test 1938 * 1939 * This function tests the interfaces to/from the firmware. 1940 * 1941 * Return: See enum fbnic_fw_self_test_codes 1942 **/ 1943 enum fbnic_fw_self_test_codes fbnic_fw_mbx_self_test(struct fbnic_dev *fbd) 1944 { 1945 enum fbnic_fw_self_test_codes err; 1946 struct fbnic_fw_completion *cmpl; 1947 1948 /* Skip test if FW interface is not present */ 1949 if (!fbnic_fw_present(fbd)) 1950 return FBNIC_TEST_FW_NO_FIRMWARE; 1951 1952 cmpl = fbnic_fw_alloc_cmpl(FBNIC_TLV_MSG_ID_TEST); 1953 if (!cmpl) 1954 return FBNIC_TEST_FW_NO_CMPL; 1955 1956 /* Load a test message onto the FW mailbox interface 1957 * and arm the completion. 1958 */ 1959 err = fbnic_fw_xmit_test_msg(fbd, cmpl); 1960 if (err) { 1961 err = FBNIC_TEST_FW_NO_XMIT; 1962 goto exit_free; 1963 } 1964 1965 /* Verify we received a message back */ 1966 if (!fbnic_mbx_wait_for_cmpl(cmpl)) { 1967 err = FBNIC_TEST_FW_NO_MSG; 1968 goto exit_cleanup; 1969 } 1970 1971 /* Verify there were no parsing errors */ 1972 if (cmpl->result) 1973 err = FBNIC_TEST_FW_PARSE; 1974 exit_cleanup: 1975 fbnic_mbx_clear_cmpl(fbd, cmpl); 1976 exit_free: 1977 fbnic_fw_put_cmpl(cmpl); 1978 1979 return err; 1980 } 1981 1982 int fbnic_fw_xmit_rpc_macda_sync(struct fbnic_dev *fbd) 1983 { 1984 struct fbnic_tlv_msg *mac_array; 1985 int i, addr_count = 0, err; 1986 struct fbnic_tlv_msg *msg; 1987 u32 rx_flags = 0; 1988 1989 /* Nothing to do if there is no FW to sync with */ 1990 if (!fbd->mbx[FBNIC_IPC_MBX_TX_IDX].ready) 1991 return 0; 1992 1993 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_RPC_MAC_SYNC_REQ); 1994 if (!msg) 1995 return -ENOMEM; 1996 1997 mac_array = fbnic_tlv_attr_nest_start(msg, 1998 FBNIC_FW_RPC_MAC_SYNC_UC_ARRAY); 1999 if (!mac_array) 2000 goto free_message_nospc; 2001 2002 /* Populate the unicast MAC addrs and capture PROMISC/ALLMULTI flags */ 2003 for (addr_count = 0, i = FBNIC_RPC_TCAM_MACDA_PROMISC_IDX; 2004 i >= fbd->mac_addr_boundary; i--) { 2005 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i]; 2006 2007 if (mac_addr->state != FBNIC_TCAM_S_VALID) 2008 continue; 2009 if (test_bit(FBNIC_MAC_ADDR_T_ALLMULTI, mac_addr->act_tcam)) 2010 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_ALLMULTI; 2011 if (test_bit(FBNIC_MAC_ADDR_T_PROMISC, mac_addr->act_tcam)) 2012 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_PROMISC; 2013 if (!test_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam)) 2014 continue; 2015 if (addr_count == FW_RPC_MAC_SYNC_UC_ARRAY_SIZE) { 2016 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_PROMISC; 2017 continue; 2018 } 2019 2020 err = fbnic_tlv_attr_put_value(mac_array, 2021 FBNIC_FW_RPC_MAC_SYNC_MAC_ADDR, 2022 mac_addr->value.addr8, 2023 ETH_ALEN); 2024 if (err) 2025 goto free_message; 2026 addr_count++; 2027 } 2028 2029 /* Close array */ 2030 fbnic_tlv_attr_nest_stop(msg); 2031 2032 mac_array = fbnic_tlv_attr_nest_start(msg, 2033 FBNIC_FW_RPC_MAC_SYNC_MC_ARRAY); 2034 if (!mac_array) 2035 goto free_message_nospc; 2036 2037 /* Repeat for multicast addrs, record BROADCAST/ALLMULTI flags */ 2038 for (addr_count = 0, i = FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX; 2039 i < fbd->mac_addr_boundary; i++) { 2040 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i]; 2041 2042 if (mac_addr->state != FBNIC_TCAM_S_VALID) 2043 continue; 2044 if (test_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam)) 2045 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_BROADCAST; 2046 if (test_bit(FBNIC_MAC_ADDR_T_ALLMULTI, mac_addr->act_tcam)) 2047 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_ALLMULTI; 2048 if (!test_bit(FBNIC_MAC_ADDR_T_MULTICAST, mac_addr->act_tcam)) 2049 continue; 2050 if (addr_count == FW_RPC_MAC_SYNC_MC_ARRAY_SIZE) { 2051 rx_flags |= FW_RPC_MAC_SYNC_RX_FLAGS_ALLMULTI; 2052 continue; 2053 } 2054 2055 err = fbnic_tlv_attr_put_value(mac_array, 2056 FBNIC_FW_RPC_MAC_SYNC_MAC_ADDR, 2057 mac_addr->value.addr8, 2058 ETH_ALEN); 2059 if (err) 2060 goto free_message; 2061 addr_count++; 2062 } 2063 2064 /* Close array */ 2065 fbnic_tlv_attr_nest_stop(msg); 2066 2067 /* Report flags at end of list */ 2068 err = fbnic_tlv_attr_put_int(msg, FBNIC_FW_RPC_MAC_SYNC_RX_FLAGS, 2069 rx_flags); 2070 if (err) 2071 goto free_message; 2072 2073 /* Send message off to FW notifying it of current RPC config */ 2074 err = fbnic_mbx_map_tlv_msg(fbd, msg); 2075 if (err) 2076 goto free_message; 2077 return 0; 2078 free_message_nospc: 2079 err = -ENOSPC; 2080 free_message: 2081 free_page((unsigned long)msg); 2082 return err; 2083 } 2084 2085 void fbnic_get_fw_ver_commit_str(struct fbnic_dev *fbd, char *fw_version, 2086 const size_t str_sz) 2087 { 2088 struct fbnic_fw_ver *mgmt = &fbd->fw_cap.running.mgmt; 2089 const char *delim = ""; 2090 2091 if (mgmt->commit[0]) 2092 delim = "_"; 2093 2094 fbnic_mk_full_fw_ver_str(mgmt->version, delim, mgmt->commit, 2095 fw_version, str_sz); 2096 } 2097 2098 struct fbnic_fw_completion *__fbnic_fw_alloc_cmpl(u32 msg_type, 2099 size_t priv_size) 2100 { 2101 struct fbnic_fw_completion *cmpl; 2102 2103 cmpl = kzalloc(sizeof(*cmpl) + priv_size, GFP_KERNEL); 2104 if (!cmpl) 2105 return NULL; 2106 2107 cmpl->msg_type = msg_type; 2108 init_completion(&cmpl->done); 2109 kref_init(&cmpl->ref_count); 2110 2111 return cmpl; 2112 } 2113 2114 struct fbnic_fw_completion *fbnic_fw_alloc_cmpl(u32 msg_type) 2115 { 2116 return __fbnic_fw_alloc_cmpl(msg_type, 0); 2117 } 2118 2119 void fbnic_fw_put_cmpl(struct fbnic_fw_completion *fw_cmpl) 2120 { 2121 kref_put(&fw_cmpl->ref_count, fbnic_fw_release_cmpl_data); 2122 } 2123