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/types.h> 11 12 #include "fbnic.h" 13 #include "fbnic_tlv.h" 14 15 static void __fbnic_mbx_wr_desc(struct fbnic_dev *fbd, int mbx_idx, 16 int desc_idx, u64 desc) 17 { 18 u32 desc_offset = FBNIC_IPC_MBX(mbx_idx, desc_idx); 19 20 fw_wr32(fbd, desc_offset + 1, upper_32_bits(desc)); 21 fw_wrfl(fbd); 22 fw_wr32(fbd, desc_offset, lower_32_bits(desc)); 23 } 24 25 static u64 __fbnic_mbx_rd_desc(struct fbnic_dev *fbd, int mbx_idx, int desc_idx) 26 { 27 u32 desc_offset = FBNIC_IPC_MBX(mbx_idx, desc_idx); 28 u64 desc; 29 30 desc = fw_rd32(fbd, desc_offset); 31 desc |= (u64)fw_rd32(fbd, desc_offset + 1) << 32; 32 33 return desc; 34 } 35 36 static void fbnic_mbx_init_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 37 { 38 int desc_idx; 39 40 /* Initialize first descriptor to all 0s. Doing this gives us a 41 * solid stop for the firmware to hit when it is done looping 42 * through the ring. 43 */ 44 __fbnic_mbx_wr_desc(fbd, mbx_idx, 0, 0); 45 46 fw_wrfl(fbd); 47 48 /* We then fill the rest of the ring starting at the end and moving 49 * back toward descriptor 0 with skip descriptors that have no 50 * length nor address, and tell the firmware that they can skip 51 * them and just move past them to the one we initialized to 0. 52 */ 53 for (desc_idx = FBNIC_IPC_MBX_DESC_LEN; --desc_idx;) { 54 __fbnic_mbx_wr_desc(fbd, mbx_idx, desc_idx, 55 FBNIC_IPC_MBX_DESC_FW_CMPL | 56 FBNIC_IPC_MBX_DESC_HOST_CMPL); 57 fw_wrfl(fbd); 58 } 59 } 60 61 void fbnic_mbx_init(struct fbnic_dev *fbd) 62 { 63 int i; 64 65 /* Initialize lock to protect Tx ring */ 66 spin_lock_init(&fbd->fw_tx_lock); 67 68 /* Reinitialize mailbox memory */ 69 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 70 memset(&fbd->mbx[i], 0, sizeof(struct fbnic_fw_mbx)); 71 72 /* Do not auto-clear the FW mailbox interrupt, let SW clear it */ 73 wr32(fbd, FBNIC_INTR_SW_AC_MODE(0), ~(1u << FBNIC_FW_MSIX_ENTRY)); 74 75 /* Clear any stale causes in vector 0 as that is used for doorbell */ 76 wr32(fbd, FBNIC_INTR_CLEAR(0), 1u << FBNIC_FW_MSIX_ENTRY); 77 78 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 79 fbnic_mbx_init_desc_ring(fbd, i); 80 } 81 82 static int fbnic_mbx_map_msg(struct fbnic_dev *fbd, int mbx_idx, 83 struct fbnic_tlv_msg *msg, u16 length, u8 eom) 84 { 85 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 86 u8 tail = mbx->tail; 87 dma_addr_t addr; 88 int direction; 89 90 if (!mbx->ready || !fbnic_fw_present(fbd)) 91 return -ENODEV; 92 93 direction = (mbx_idx == FBNIC_IPC_MBX_RX_IDX) ? DMA_FROM_DEVICE : 94 DMA_TO_DEVICE; 95 96 if (mbx->head == ((tail + 1) % FBNIC_IPC_MBX_DESC_LEN)) 97 return -EBUSY; 98 99 addr = dma_map_single(fbd->dev, msg, PAGE_SIZE, direction); 100 if (dma_mapping_error(fbd->dev, addr)) { 101 free_page((unsigned long)msg); 102 103 return -ENOSPC; 104 } 105 106 mbx->buf_info[tail].msg = msg; 107 mbx->buf_info[tail].addr = addr; 108 109 mbx->tail = (tail + 1) % FBNIC_IPC_MBX_DESC_LEN; 110 111 fw_wr32(fbd, FBNIC_IPC_MBX(mbx_idx, mbx->tail), 0); 112 113 __fbnic_mbx_wr_desc(fbd, mbx_idx, tail, 114 FIELD_PREP(FBNIC_IPC_MBX_DESC_LEN_MASK, length) | 115 (addr & FBNIC_IPC_MBX_DESC_ADDR_MASK) | 116 (eom ? FBNIC_IPC_MBX_DESC_EOM : 0) | 117 FBNIC_IPC_MBX_DESC_HOST_CMPL); 118 119 return 0; 120 } 121 122 static void fbnic_mbx_unmap_and_free_msg(struct fbnic_dev *fbd, int mbx_idx, 123 int desc_idx) 124 { 125 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 126 int direction; 127 128 if (!mbx->buf_info[desc_idx].msg) 129 return; 130 131 direction = (mbx_idx == FBNIC_IPC_MBX_RX_IDX) ? DMA_FROM_DEVICE : 132 DMA_TO_DEVICE; 133 dma_unmap_single(fbd->dev, mbx->buf_info[desc_idx].addr, 134 PAGE_SIZE, direction); 135 136 free_page((unsigned long)mbx->buf_info[desc_idx].msg); 137 mbx->buf_info[desc_idx].msg = NULL; 138 } 139 140 static void fbnic_mbx_clean_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 141 { 142 int i; 143 144 fbnic_mbx_init_desc_ring(fbd, mbx_idx); 145 146 for (i = FBNIC_IPC_MBX_DESC_LEN; i--;) 147 fbnic_mbx_unmap_and_free_msg(fbd, mbx_idx, i); 148 } 149 150 void fbnic_mbx_clean(struct fbnic_dev *fbd) 151 { 152 int i; 153 154 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 155 fbnic_mbx_clean_desc_ring(fbd, i); 156 } 157 158 #define FBNIC_MBX_MAX_PAGE_SIZE FIELD_MAX(FBNIC_IPC_MBX_DESC_LEN_MASK) 159 #define FBNIC_RX_PAGE_SIZE min_t(int, PAGE_SIZE, FBNIC_MBX_MAX_PAGE_SIZE) 160 161 static int fbnic_mbx_alloc_rx_msgs(struct fbnic_dev *fbd) 162 { 163 struct fbnic_fw_mbx *rx_mbx = &fbd->mbx[FBNIC_IPC_MBX_RX_IDX]; 164 u8 tail = rx_mbx->tail, head = rx_mbx->head, count; 165 int err = 0; 166 167 /* Do nothing if mailbox is not ready, or we already have pages on 168 * the ring that can be used by the firmware 169 */ 170 if (!rx_mbx->ready) 171 return -ENODEV; 172 173 /* Fill all but 1 unused descriptors in the Rx queue. */ 174 count = (head - tail - 1) % FBNIC_IPC_MBX_DESC_LEN; 175 while (!err && count--) { 176 struct fbnic_tlv_msg *msg; 177 178 msg = (struct fbnic_tlv_msg *)__get_free_page(GFP_ATOMIC | 179 __GFP_NOWARN); 180 if (!msg) { 181 err = -ENOMEM; 182 break; 183 } 184 185 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_RX_IDX, msg, 186 FBNIC_RX_PAGE_SIZE, 0); 187 if (err) 188 free_page((unsigned long)msg); 189 } 190 191 return err; 192 } 193 194 static int fbnic_mbx_map_tlv_msg(struct fbnic_dev *fbd, 195 struct fbnic_tlv_msg *msg) 196 { 197 unsigned long flags; 198 int err; 199 200 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 201 202 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_TX_IDX, msg, 203 le16_to_cpu(msg->hdr.len) * sizeof(u32), 1); 204 205 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 206 207 return err; 208 } 209 210 static void fbnic_mbx_process_tx_msgs(struct fbnic_dev *fbd) 211 { 212 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 213 u8 head = tx_mbx->head; 214 u64 desc; 215 216 while (head != tx_mbx->tail) { 217 desc = __fbnic_mbx_rd_desc(fbd, FBNIC_IPC_MBX_TX_IDX, head); 218 if (!(desc & FBNIC_IPC_MBX_DESC_FW_CMPL)) 219 break; 220 221 fbnic_mbx_unmap_and_free_msg(fbd, FBNIC_IPC_MBX_TX_IDX, head); 222 223 head++; 224 head %= FBNIC_IPC_MBX_DESC_LEN; 225 } 226 227 /* Record head for next interrupt */ 228 tx_mbx->head = head; 229 } 230 231 static int fbnic_mbx_map_req_w_cmpl(struct fbnic_dev *fbd, 232 struct fbnic_tlv_msg *msg, 233 struct fbnic_fw_completion *cmpl_data) 234 { 235 unsigned long flags; 236 int err; 237 238 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 239 240 /* If we are already waiting on a completion then abort */ 241 if (cmpl_data && fbd->cmpl_data) { 242 err = -EBUSY; 243 goto unlock_mbx; 244 } 245 246 /* Record completion location and submit request */ 247 if (cmpl_data) 248 fbd->cmpl_data = cmpl_data; 249 250 err = fbnic_mbx_map_msg(fbd, FBNIC_IPC_MBX_TX_IDX, msg, 251 le16_to_cpu(msg->hdr.len) * sizeof(u32), 1); 252 253 /* If msg failed then clear completion data for next caller */ 254 if (err && cmpl_data) 255 fbd->cmpl_data = NULL; 256 257 unlock_mbx: 258 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 259 260 return err; 261 } 262 263 static void fbnic_fw_release_cmpl_data(struct kref *kref) 264 { 265 struct fbnic_fw_completion *cmpl_data; 266 267 cmpl_data = container_of(kref, struct fbnic_fw_completion, 268 ref_count); 269 kfree(cmpl_data); 270 } 271 272 static struct fbnic_fw_completion * 273 fbnic_fw_get_cmpl_by_type(struct fbnic_dev *fbd, u32 msg_type) 274 { 275 struct fbnic_fw_completion *cmpl_data = NULL; 276 unsigned long flags; 277 278 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 279 if (fbd->cmpl_data && fbd->cmpl_data->msg_type == msg_type) { 280 cmpl_data = fbd->cmpl_data; 281 kref_get(&fbd->cmpl_data->ref_count); 282 } 283 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 284 285 return cmpl_data; 286 } 287 288 /** 289 * fbnic_fw_xmit_simple_msg - Transmit a simple single TLV message w/o data 290 * @fbd: FBNIC device structure 291 * @msg_type: ENUM value indicating message type to send 292 * 293 * Return: 294 * One the following values: 295 * -EOPNOTSUPP: Is not ASIC so mailbox is not supported 296 * -ENODEV: Device I/O error 297 * -ENOMEM: Failed to allocate message 298 * -EBUSY: No space in mailbox 299 * -ENOSPC: DMA mapping failed 300 * 301 * This function sends a single TLV header indicating the host wants to take 302 * some action. However there are no other side effects which means that any 303 * response will need to be caught via a completion if this action is 304 * expected to kick off a resultant action. 305 */ 306 static int fbnic_fw_xmit_simple_msg(struct fbnic_dev *fbd, u32 msg_type) 307 { 308 struct fbnic_tlv_msg *msg; 309 int err = 0; 310 311 if (!fbnic_fw_present(fbd)) 312 return -ENODEV; 313 314 msg = fbnic_tlv_msg_alloc(msg_type); 315 if (!msg) 316 return -ENOMEM; 317 318 err = fbnic_mbx_map_tlv_msg(fbd, msg); 319 if (err) 320 free_page((unsigned long)msg); 321 322 return err; 323 } 324 325 /** 326 * fbnic_fw_xmit_cap_msg - Allocate and populate a FW capabilities message 327 * @fbd: FBNIC device structure 328 * 329 * Return: NULL on failure to allocate, error pointer on error, or pointer 330 * to new TLV test message. 331 * 332 * Sends a single TLV header indicating the host wants the firmware to 333 * confirm the capabilities and version. 334 **/ 335 static int fbnic_fw_xmit_cap_msg(struct fbnic_dev *fbd) 336 { 337 int err = fbnic_fw_xmit_simple_msg(fbd, FBNIC_TLV_MSG_ID_HOST_CAP_REQ); 338 339 /* Return 0 if we are not calling this on ASIC */ 340 return (err == -EOPNOTSUPP) ? 0 : err; 341 } 342 343 static void fbnic_mbx_postinit_desc_ring(struct fbnic_dev *fbd, int mbx_idx) 344 { 345 struct fbnic_fw_mbx *mbx = &fbd->mbx[mbx_idx]; 346 347 /* This is a one time init, so just exit if it is completed */ 348 if (mbx->ready) 349 return; 350 351 mbx->ready = true; 352 353 switch (mbx_idx) { 354 case FBNIC_IPC_MBX_RX_IDX: 355 /* Make sure we have a page for the FW to write to */ 356 fbnic_mbx_alloc_rx_msgs(fbd); 357 break; 358 case FBNIC_IPC_MBX_TX_IDX: 359 /* Force version to 1 if we successfully requested an update 360 * from the firmware. This should be overwritten once we get 361 * the actual version from the firmware in the capabilities 362 * request message. 363 */ 364 if (!fbnic_fw_xmit_cap_msg(fbd) && 365 !fbd->fw_cap.running.mgmt.version) 366 fbd->fw_cap.running.mgmt.version = 1; 367 break; 368 } 369 } 370 371 static void fbnic_mbx_postinit(struct fbnic_dev *fbd) 372 { 373 int i; 374 375 /* We only need to do this on the first interrupt following init. 376 * this primes the mailbox so that we will have cleared all the 377 * skip descriptors. 378 */ 379 if (!(rd32(fbd, FBNIC_INTR_STATUS(0)) & (1u << FBNIC_FW_MSIX_ENTRY))) 380 return; 381 382 wr32(fbd, FBNIC_INTR_CLEAR(0), 1u << FBNIC_FW_MSIX_ENTRY); 383 384 for (i = 0; i < FBNIC_IPC_MBX_INDICES; i++) 385 fbnic_mbx_postinit_desc_ring(fbd, i); 386 } 387 388 /** 389 * fbnic_fw_xmit_ownership_msg - Create and transmit a host ownership message 390 * to FW mailbox 391 * 392 * @fbd: FBNIC device structure 393 * @take_ownership: take/release the ownership 394 * 395 * Return: zero on success, negative value on failure 396 * 397 * Notifies the firmware that the driver either takes ownership of the NIC 398 * (when @take_ownership is true) or releases it. 399 */ 400 int fbnic_fw_xmit_ownership_msg(struct fbnic_dev *fbd, bool take_ownership) 401 { 402 unsigned long req_time = jiffies; 403 struct fbnic_tlv_msg *msg; 404 int err = 0; 405 406 if (!fbnic_fw_present(fbd)) 407 return -ENODEV; 408 409 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_OWNERSHIP_REQ); 410 if (!msg) 411 return -ENOMEM; 412 413 if (take_ownership) { 414 err = fbnic_tlv_attr_put_flag(msg, FBNIC_FW_OWNERSHIP_FLAG); 415 if (err) 416 goto free_message; 417 } 418 419 err = fbnic_mbx_map_tlv_msg(fbd, msg); 420 if (err) 421 goto free_message; 422 423 /* Initialize heartbeat, set last response to 1 second in the past 424 * so that we will trigger a timeout if the firmware doesn't respond 425 */ 426 fbd->last_heartbeat_response = req_time - HZ; 427 428 fbd->last_heartbeat_request = req_time; 429 430 /* Set heartbeat detection based on if we are taking ownership */ 431 fbd->fw_heartbeat_enabled = take_ownership; 432 433 return err; 434 435 free_message: 436 free_page((unsigned long)msg); 437 return err; 438 } 439 440 static const struct fbnic_tlv_index fbnic_fw_cap_resp_index[] = { 441 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_VERSION), 442 FBNIC_TLV_ATTR_FLAG(FBNIC_FW_CAP_RESP_BMC_PRESENT), 443 FBNIC_TLV_ATTR_MAC_ADDR(FBNIC_FW_CAP_RESP_BMC_MAC_ADDR), 444 FBNIC_TLV_ATTR_ARRAY(FBNIC_FW_CAP_RESP_BMC_MAC_ARRAY), 445 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_STORED_VERSION), 446 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT), 447 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, 448 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 449 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_BMC_ALL_MULTI), 450 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_FW_LINK_SPEED), 451 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_FW_LINK_FEC), 452 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, 453 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 454 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_CMRT_VERSION), 455 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION), 456 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, 457 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 458 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, 459 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 460 FBNIC_TLV_ATTR_U32(FBNIC_FW_CAP_RESP_UEFI_VERSION), 461 FBNIC_TLV_ATTR_STRING(FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, 462 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE), 463 FBNIC_TLV_ATTR_LAST 464 }; 465 466 static int fbnic_fw_parse_bmc_addrs(u8 bmc_mac_addr[][ETH_ALEN], 467 struct fbnic_tlv_msg *attr, int len) 468 { 469 int attr_len = le16_to_cpu(attr->hdr.len) / sizeof(u32) - 1; 470 struct fbnic_tlv_msg *mac_results[8]; 471 int err, i = 0; 472 473 /* Make sure we have enough room to process all the MAC addresses */ 474 if (len > 8) 475 return -ENOSPC; 476 477 /* Parse the array */ 478 err = fbnic_tlv_attr_parse_array(&attr[1], attr_len, mac_results, 479 fbnic_fw_cap_resp_index, 480 FBNIC_FW_CAP_RESP_BMC_MAC_ADDR, len); 481 if (err) 482 return err; 483 484 /* Copy results into MAC addr array */ 485 for (i = 0; i < len && mac_results[i]; i++) 486 fbnic_tlv_attr_addr_copy(bmc_mac_addr[i], mac_results[i]); 487 488 /* Zero remaining unused addresses */ 489 while (i < len) 490 eth_zero_addr(bmc_mac_addr[i++]); 491 492 return 0; 493 } 494 495 static int fbnic_fw_parse_cap_resp(void *opaque, struct fbnic_tlv_msg **results) 496 { 497 u32 active_slot = 0, all_multi = 0; 498 struct fbnic_dev *fbd = opaque; 499 u32 speed = 0, fec = 0; 500 size_t commit_size = 0; 501 bool bmc_present; 502 int err; 503 504 get_unsigned_result(FBNIC_FW_CAP_RESP_VERSION, 505 fbd->fw_cap.running.mgmt.version); 506 507 if (!fbd->fw_cap.running.mgmt.version) 508 return -EINVAL; 509 510 if (fbd->fw_cap.running.mgmt.version < MIN_FW_VERSION_CODE) { 511 char running_ver[FBNIC_FW_VER_MAX_SIZE]; 512 513 fbnic_mk_fw_ver_str(fbd->fw_cap.running.mgmt.version, 514 running_ver); 515 dev_err(fbd->dev, "Device firmware version(%s) is older than minimum required version(%02d.%02d.%02d)\n", 516 running_ver, 517 MIN_FW_MAJOR_VERSION, 518 MIN_FW_MINOR_VERSION, 519 MIN_FW_BUILD_VERSION); 520 /* Disable TX mailbox to prevent card use until firmware is 521 * updated. 522 */ 523 fbd->mbx[FBNIC_IPC_MBX_TX_IDX].ready = false; 524 return -EINVAL; 525 } 526 527 get_string_result(FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, commit_size, 528 fbd->fw_cap.running.mgmt.commit, 529 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 530 if (!commit_size) 531 dev_warn(fbd->dev, "Firmware did not send mgmt commit!\n"); 532 533 get_unsigned_result(FBNIC_FW_CAP_RESP_STORED_VERSION, 534 fbd->fw_cap.stored.mgmt.version); 535 get_string_result(FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, commit_size, 536 fbd->fw_cap.stored.mgmt.commit, 537 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 538 539 get_unsigned_result(FBNIC_FW_CAP_RESP_CMRT_VERSION, 540 fbd->fw_cap.running.bootloader.version); 541 get_string_result(FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, commit_size, 542 fbd->fw_cap.running.bootloader.commit, 543 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 544 545 get_unsigned_result(FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION, 546 fbd->fw_cap.stored.bootloader.version); 547 get_string_result(FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, commit_size, 548 fbd->fw_cap.stored.bootloader.commit, 549 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 550 551 get_unsigned_result(FBNIC_FW_CAP_RESP_UEFI_VERSION, 552 fbd->fw_cap.stored.undi.version); 553 get_string_result(FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, commit_size, 554 fbd->fw_cap.stored.undi.commit, 555 FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 556 557 get_unsigned_result(FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT, active_slot); 558 fbd->fw_cap.active_slot = active_slot; 559 560 get_unsigned_result(FBNIC_FW_CAP_RESP_FW_LINK_SPEED, speed); 561 get_unsigned_result(FBNIC_FW_CAP_RESP_FW_LINK_FEC, fec); 562 fbd->fw_cap.link_speed = speed; 563 fbd->fw_cap.link_fec = fec; 564 565 bmc_present = !!results[FBNIC_FW_CAP_RESP_BMC_PRESENT]; 566 if (bmc_present) { 567 struct fbnic_tlv_msg *attr; 568 569 attr = results[FBNIC_FW_CAP_RESP_BMC_MAC_ARRAY]; 570 if (!attr) 571 return -EINVAL; 572 573 err = fbnic_fw_parse_bmc_addrs(fbd->fw_cap.bmc_mac_addr, 574 attr, 4); 575 if (err) 576 return err; 577 578 get_unsigned_result(FBNIC_FW_CAP_RESP_BMC_ALL_MULTI, all_multi); 579 } else { 580 memset(fbd->fw_cap.bmc_mac_addr, 0, 581 sizeof(fbd->fw_cap.bmc_mac_addr)); 582 } 583 584 fbd->fw_cap.bmc_present = bmc_present; 585 586 if (results[FBNIC_FW_CAP_RESP_BMC_ALL_MULTI] || !bmc_present) 587 fbd->fw_cap.all_multi = all_multi; 588 589 return 0; 590 } 591 592 static const struct fbnic_tlv_index fbnic_ownership_resp_index[] = { 593 FBNIC_TLV_ATTR_LAST 594 }; 595 596 static int fbnic_fw_parse_ownership_resp(void *opaque, 597 struct fbnic_tlv_msg **results) 598 { 599 struct fbnic_dev *fbd = (struct fbnic_dev *)opaque; 600 601 /* Count the ownership response as a heartbeat reply */ 602 fbd->last_heartbeat_response = jiffies; 603 604 return 0; 605 } 606 607 static const struct fbnic_tlv_index fbnic_heartbeat_resp_index[] = { 608 FBNIC_TLV_ATTR_LAST 609 }; 610 611 static int fbnic_fw_parse_heartbeat_resp(void *opaque, 612 struct fbnic_tlv_msg **results) 613 { 614 struct fbnic_dev *fbd = (struct fbnic_dev *)opaque; 615 616 fbd->last_heartbeat_response = jiffies; 617 618 return 0; 619 } 620 621 static int fbnic_fw_xmit_heartbeat_message(struct fbnic_dev *fbd) 622 { 623 unsigned long req_time = jiffies; 624 struct fbnic_tlv_msg *msg; 625 int err = 0; 626 627 if (!fbnic_fw_present(fbd)) 628 return -ENODEV; 629 630 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_HEARTBEAT_REQ); 631 if (!msg) 632 return -ENOMEM; 633 634 err = fbnic_mbx_map_tlv_msg(fbd, msg); 635 if (err) 636 goto free_message; 637 638 fbd->last_heartbeat_request = req_time; 639 640 return err; 641 642 free_message: 643 free_page((unsigned long)msg); 644 return err; 645 } 646 647 static bool fbnic_fw_heartbeat_current(struct fbnic_dev *fbd) 648 { 649 unsigned long last_response = fbd->last_heartbeat_response; 650 unsigned long last_request = fbd->last_heartbeat_request; 651 652 return !time_before(last_response, last_request); 653 } 654 655 int fbnic_fw_init_heartbeat(struct fbnic_dev *fbd, bool poll) 656 { 657 int err = -ETIMEDOUT; 658 int attempts = 50; 659 660 if (!fbnic_fw_present(fbd)) 661 return -ENODEV; 662 663 while (attempts--) { 664 msleep(200); 665 if (poll) 666 fbnic_mbx_poll(fbd); 667 668 if (!fbnic_fw_heartbeat_current(fbd)) 669 continue; 670 671 /* Place new message on mailbox to elicit a response */ 672 err = fbnic_fw_xmit_heartbeat_message(fbd); 673 if (err) 674 dev_warn(fbd->dev, 675 "Failed to send heartbeat message: %d\n", 676 err); 677 break; 678 } 679 680 return err; 681 } 682 683 void fbnic_fw_check_heartbeat(struct fbnic_dev *fbd) 684 { 685 unsigned long last_request = fbd->last_heartbeat_request; 686 int err; 687 688 /* Do not check heartbeat or send another request until current 689 * period has expired. Otherwise we might start spamming requests. 690 */ 691 if (time_is_after_jiffies(last_request + FW_HEARTBEAT_PERIOD)) 692 return; 693 694 /* We already reported no mailbox. Wait for it to come back */ 695 if (!fbd->fw_heartbeat_enabled) 696 return; 697 698 /* Was the last heartbeat response long time ago? */ 699 if (!fbnic_fw_heartbeat_current(fbd)) { 700 dev_warn(fbd->dev, 701 "Firmware did not respond to heartbeat message\n"); 702 fbd->fw_heartbeat_enabled = false; 703 } 704 705 /* Place new message on mailbox to elicit a response */ 706 err = fbnic_fw_xmit_heartbeat_message(fbd); 707 if (err) 708 dev_warn(fbd->dev, "Failed to send heartbeat message\n"); 709 } 710 711 /** 712 * fbnic_fw_xmit_tsene_read_msg - Create and transmit a sensor read request 713 * @fbd: FBNIC device structure 714 * @cmpl_data: Completion data structure to store sensor response 715 * 716 * Asks the firmware to provide an update with the latest sensor data. 717 * The response will contain temperature and voltage readings. 718 * 719 * Return: 0 on success, negative error value on failure 720 */ 721 int fbnic_fw_xmit_tsene_read_msg(struct fbnic_dev *fbd, 722 struct fbnic_fw_completion *cmpl_data) 723 { 724 struct fbnic_tlv_msg *msg; 725 int err; 726 727 if (!fbnic_fw_present(fbd)) 728 return -ENODEV; 729 730 msg = fbnic_tlv_msg_alloc(FBNIC_TLV_MSG_ID_TSENE_READ_REQ); 731 if (!msg) 732 return -ENOMEM; 733 734 err = fbnic_mbx_map_req_w_cmpl(fbd, msg, cmpl_data); 735 if (err) 736 goto free_message; 737 738 return 0; 739 740 free_message: 741 free_page((unsigned long)msg); 742 return err; 743 } 744 745 static const struct fbnic_tlv_index fbnic_tsene_read_resp_index[] = { 746 FBNIC_TLV_ATTR_S32(FBNIC_TSENE_THERM), 747 FBNIC_TLV_ATTR_S32(FBNIC_TSENE_VOLT), 748 FBNIC_TLV_ATTR_S32(FBNIC_TSENE_ERROR), 749 FBNIC_TLV_ATTR_LAST 750 }; 751 752 static int fbnic_fw_parse_tsene_read_resp(void *opaque, 753 struct fbnic_tlv_msg **results) 754 { 755 struct fbnic_fw_completion *cmpl_data; 756 struct fbnic_dev *fbd = opaque; 757 int err = 0; 758 759 /* Verify we have a completion pointer to provide with data */ 760 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, 761 FBNIC_TLV_MSG_ID_TSENE_READ_RESP); 762 if (!cmpl_data) 763 return -EINVAL; 764 765 if (results[FBNIC_TSENE_ERROR]) { 766 err = fbnic_tlv_attr_get_unsigned(results[FBNIC_TSENE_ERROR]); 767 if (err) 768 goto exit_complete; 769 } 770 771 if (!results[FBNIC_TSENE_THERM] || !results[FBNIC_TSENE_VOLT]) { 772 err = -EINVAL; 773 goto exit_complete; 774 } 775 776 cmpl_data->u.tsene.millidegrees = 777 fbnic_tlv_attr_get_signed(results[FBNIC_TSENE_THERM]); 778 cmpl_data->u.tsene.millivolts = 779 fbnic_tlv_attr_get_signed(results[FBNIC_TSENE_VOLT]); 780 781 exit_complete: 782 cmpl_data->result = err; 783 complete(&cmpl_data->done); 784 fbnic_fw_put_cmpl(cmpl_data); 785 786 return err; 787 } 788 789 static const struct fbnic_tlv_parser fbnic_fw_tlv_parser[] = { 790 FBNIC_TLV_PARSER(FW_CAP_RESP, fbnic_fw_cap_resp_index, 791 fbnic_fw_parse_cap_resp), 792 FBNIC_TLV_PARSER(OWNERSHIP_RESP, fbnic_ownership_resp_index, 793 fbnic_fw_parse_ownership_resp), 794 FBNIC_TLV_PARSER(HEARTBEAT_RESP, fbnic_heartbeat_resp_index, 795 fbnic_fw_parse_heartbeat_resp), 796 FBNIC_TLV_PARSER(TSENE_READ_RESP, 797 fbnic_tsene_read_resp_index, 798 fbnic_fw_parse_tsene_read_resp), 799 FBNIC_TLV_MSG_ERROR 800 }; 801 802 static void fbnic_mbx_process_rx_msgs(struct fbnic_dev *fbd) 803 { 804 struct fbnic_fw_mbx *rx_mbx = &fbd->mbx[FBNIC_IPC_MBX_RX_IDX]; 805 u8 head = rx_mbx->head; 806 u64 desc, length; 807 808 while (head != rx_mbx->tail) { 809 struct fbnic_tlv_msg *msg; 810 int err; 811 812 desc = __fbnic_mbx_rd_desc(fbd, FBNIC_IPC_MBX_RX_IDX, head); 813 if (!(desc & FBNIC_IPC_MBX_DESC_FW_CMPL)) 814 break; 815 816 dma_unmap_single(fbd->dev, rx_mbx->buf_info[head].addr, 817 PAGE_SIZE, DMA_FROM_DEVICE); 818 819 msg = rx_mbx->buf_info[head].msg; 820 821 length = FIELD_GET(FBNIC_IPC_MBX_DESC_LEN_MASK, desc); 822 823 /* Ignore NULL mailbox descriptors */ 824 if (!length) 825 goto next_page; 826 827 /* Report descriptors with length greater than page size */ 828 if (length > PAGE_SIZE) { 829 dev_warn(fbd->dev, 830 "Invalid mailbox descriptor length: %lld\n", 831 length); 832 goto next_page; 833 } 834 835 if (le16_to_cpu(msg->hdr.len) * sizeof(u32) > length) 836 dev_warn(fbd->dev, "Mailbox message length mismatch\n"); 837 838 /* If parsing fails dump contents of message to dmesg */ 839 err = fbnic_tlv_msg_parse(fbd, msg, fbnic_fw_tlv_parser); 840 if (err) { 841 dev_warn(fbd->dev, "Unable to process message: %d\n", 842 err); 843 print_hex_dump(KERN_WARNING, "fbnic:", 844 DUMP_PREFIX_OFFSET, 16, 2, 845 msg, length, true); 846 } 847 848 dev_dbg(fbd->dev, "Parsed msg type %d\n", msg->hdr.type); 849 next_page: 850 851 free_page((unsigned long)rx_mbx->buf_info[head].msg); 852 rx_mbx->buf_info[head].msg = NULL; 853 854 head++; 855 head %= FBNIC_IPC_MBX_DESC_LEN; 856 } 857 858 /* Record head for next interrupt */ 859 rx_mbx->head = head; 860 861 /* Make sure we have at least one page for the FW to write to */ 862 fbnic_mbx_alloc_rx_msgs(fbd); 863 } 864 865 void fbnic_mbx_poll(struct fbnic_dev *fbd) 866 { 867 fbnic_mbx_postinit(fbd); 868 869 fbnic_mbx_process_tx_msgs(fbd); 870 fbnic_mbx_process_rx_msgs(fbd); 871 } 872 873 int fbnic_mbx_poll_tx_ready(struct fbnic_dev *fbd) 874 { 875 struct fbnic_fw_mbx *tx_mbx; 876 int attempts = 50; 877 878 /* Immediate fail if BAR4 isn't there */ 879 if (!fbnic_fw_present(fbd)) 880 return -ENODEV; 881 882 tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 883 while (!tx_mbx->ready && --attempts) { 884 /* Force the firmware to trigger an interrupt response to 885 * avoid the mailbox getting stuck closed if the interrupt 886 * is reset. 887 */ 888 fbnic_mbx_init_desc_ring(fbd, FBNIC_IPC_MBX_TX_IDX); 889 890 msleep(200); 891 892 fbnic_mbx_poll(fbd); 893 } 894 895 return attempts ? 0 : -ETIMEDOUT; 896 } 897 898 void fbnic_mbx_flush_tx(struct fbnic_dev *fbd) 899 { 900 struct fbnic_fw_mbx *tx_mbx; 901 int attempts = 50; 902 u8 count = 0; 903 904 /* Nothing to do if there is no mailbox */ 905 if (!fbnic_fw_present(fbd)) 906 return; 907 908 /* Record current Rx stats */ 909 tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 910 911 /* Nothing to do if mailbox never got to ready */ 912 if (!tx_mbx->ready) 913 return; 914 915 /* Give firmware time to process packet, 916 * we will wait up to 10 seconds which is 50 waits of 200ms. 917 */ 918 do { 919 u8 head = tx_mbx->head; 920 921 if (head == tx_mbx->tail) 922 break; 923 924 msleep(200); 925 fbnic_mbx_process_tx_msgs(fbd); 926 927 count += (tx_mbx->head - head) % FBNIC_IPC_MBX_DESC_LEN; 928 } while (count < FBNIC_IPC_MBX_DESC_LEN && --attempts); 929 } 930 931 void fbnic_get_fw_ver_commit_str(struct fbnic_dev *fbd, char *fw_version, 932 const size_t str_sz) 933 { 934 struct fbnic_fw_ver *mgmt = &fbd->fw_cap.running.mgmt; 935 const char *delim = ""; 936 937 if (mgmt->commit[0]) 938 delim = "_"; 939 940 fbnic_mk_full_fw_ver_str(mgmt->version, delim, mgmt->commit, 941 fw_version, str_sz); 942 } 943 944 void fbnic_fw_init_cmpl(struct fbnic_fw_completion *fw_cmpl, 945 u32 msg_type) 946 { 947 fw_cmpl->msg_type = msg_type; 948 init_completion(&fw_cmpl->done); 949 kref_init(&fw_cmpl->ref_count); 950 } 951 952 void fbnic_fw_clear_compl(struct fbnic_dev *fbd) 953 { 954 unsigned long flags; 955 956 spin_lock_irqsave(&fbd->fw_tx_lock, flags); 957 fbd->cmpl_data = NULL; 958 spin_unlock_irqrestore(&fbd->fw_tx_lock, flags); 959 } 960 961 void fbnic_fw_put_cmpl(struct fbnic_fw_completion *fw_cmpl) 962 { 963 kref_put(&fw_cmpl->ref_count, fbnic_fw_release_cmpl_data); 964 } 965