Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'eth-fbnic-cleanup-macros-and-string-function'

Lee Trager says:

====================
eth: fbnic: Cleanup macros and string function

We have received some feedback that the macros we use for reading FW mailbox
attributes are too large in scope and confusing to understanding. Additionally
the string function did not provide errors allowing it to silently succeed.
This patch set fixes theses issues.
====================

Link: https://patch.msgid.link/20250228191935.3953712-1-lee@trager.us
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+106 -97
+48 -53
drivers/net/ethernet/meta/fbnic/fbnic_fw.c
··· 494 494 495 495 static int fbnic_fw_parse_cap_resp(void *opaque, struct fbnic_tlv_msg **results) 496 496 { 497 - u32 active_slot = 0, all_multi = 0; 497 + u32 all_multi = 0, version = 0; 498 498 struct fbnic_dev *fbd = opaque; 499 - u32 speed = 0, fec = 0; 500 - size_t commit_size = 0; 501 499 bool bmc_present; 502 500 int err; 503 501 504 - get_unsigned_result(FBNIC_FW_CAP_RESP_VERSION, 505 - fbd->fw_cap.running.mgmt.version); 506 - 502 + version = fta_get_uint(results, FBNIC_FW_CAP_RESP_VERSION); 503 + fbd->fw_cap.running.mgmt.version = version; 507 504 if (!fbd->fw_cap.running.mgmt.version) 508 505 return -EINVAL; 509 506 ··· 521 524 return -EINVAL; 522 525 } 523 526 524 - get_string_result(FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, commit_size, 525 - fbd->fw_cap.running.mgmt.commit, 526 - FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 527 - if (!commit_size) 527 + if (fta_get_str(results, FBNIC_FW_CAP_RESP_VERSION_COMMIT_STR, 528 + fbd->fw_cap.running.mgmt.commit, 529 + FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE) <= 0) 528 530 dev_warn(fbd->dev, "Firmware did not send mgmt commit!\n"); 529 531 530 - get_unsigned_result(FBNIC_FW_CAP_RESP_STORED_VERSION, 531 - fbd->fw_cap.stored.mgmt.version); 532 - get_string_result(FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, commit_size, 533 - fbd->fw_cap.stored.mgmt.commit, 534 - FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 532 + version = fta_get_uint(results, FBNIC_FW_CAP_RESP_STORED_VERSION); 533 + fbd->fw_cap.stored.mgmt.version = version; 534 + fta_get_str(results, FBNIC_FW_CAP_RESP_STORED_COMMIT_STR, 535 + fbd->fw_cap.stored.mgmt.commit, 536 + FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 535 537 536 - get_unsigned_result(FBNIC_FW_CAP_RESP_CMRT_VERSION, 537 - fbd->fw_cap.running.bootloader.version); 538 - get_string_result(FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, commit_size, 539 - fbd->fw_cap.running.bootloader.commit, 540 - FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 538 + version = fta_get_uint(results, FBNIC_FW_CAP_RESP_CMRT_VERSION); 539 + fbd->fw_cap.running.bootloader.version = version; 540 + fta_get_str(results, FBNIC_FW_CAP_RESP_CMRT_COMMIT_STR, 541 + fbd->fw_cap.running.bootloader.commit, 542 + FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 541 543 542 - get_unsigned_result(FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION, 543 - fbd->fw_cap.stored.bootloader.version); 544 - get_string_result(FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, commit_size, 545 - fbd->fw_cap.stored.bootloader.commit, 546 - FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 544 + version = fta_get_uint(results, FBNIC_FW_CAP_RESP_STORED_CMRT_VERSION); 545 + fbd->fw_cap.stored.bootloader.version = version; 546 + fta_get_str(results, FBNIC_FW_CAP_RESP_STORED_CMRT_COMMIT_STR, 547 + fbd->fw_cap.stored.bootloader.commit, 548 + FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 547 549 548 - get_unsigned_result(FBNIC_FW_CAP_RESP_UEFI_VERSION, 549 - fbd->fw_cap.stored.undi.version); 550 - get_string_result(FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, commit_size, 551 - fbd->fw_cap.stored.undi.commit, 552 - FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 550 + version = fta_get_uint(results, FBNIC_FW_CAP_RESP_UEFI_VERSION); 551 + fbd->fw_cap.stored.undi.version = version; 552 + fta_get_str(results, FBNIC_FW_CAP_RESP_UEFI_COMMIT_STR, 553 + fbd->fw_cap.stored.undi.commit, 554 + FBNIC_FW_CAP_RESP_COMMIT_MAX_SIZE); 553 555 554 - get_unsigned_result(FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT, active_slot); 555 - fbd->fw_cap.active_slot = active_slot; 556 - 557 - get_unsigned_result(FBNIC_FW_CAP_RESP_FW_LINK_SPEED, speed); 558 - get_unsigned_result(FBNIC_FW_CAP_RESP_FW_LINK_FEC, fec); 559 - fbd->fw_cap.link_speed = speed; 560 - fbd->fw_cap.link_fec = fec; 556 + fbd->fw_cap.active_slot = 557 + fta_get_uint(results, FBNIC_FW_CAP_RESP_ACTIVE_FW_SLOT); 558 + fbd->fw_cap.link_speed = 559 + fta_get_uint(results, FBNIC_FW_CAP_RESP_FW_LINK_SPEED); 560 + fbd->fw_cap.link_fec = 561 + fta_get_uint(results, FBNIC_FW_CAP_RESP_FW_LINK_FEC); 561 562 562 563 bmc_present = !!results[FBNIC_FW_CAP_RESP_BMC_PRESENT]; 563 564 if (bmc_present) { ··· 570 575 if (err) 571 576 return err; 572 577 573 - get_unsigned_result(FBNIC_FW_CAP_RESP_BMC_ALL_MULTI, all_multi); 578 + all_multi = 579 + fta_get_uint(results, FBNIC_FW_CAP_RESP_BMC_ALL_MULTI); 574 580 } else { 575 581 memset(fbd->fw_cap.bmc_mac_addr, 0, 576 582 sizeof(fbd->fw_cap.bmc_mac_addr)); ··· 739 743 } 740 744 741 745 static const struct fbnic_tlv_index fbnic_tsene_read_resp_index[] = { 742 - FBNIC_TLV_ATTR_S32(FBNIC_TSENE_THERM), 743 - FBNIC_TLV_ATTR_S32(FBNIC_TSENE_VOLT), 744 - FBNIC_TLV_ATTR_S32(FBNIC_TSENE_ERROR), 746 + FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_THERM), 747 + FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_VOLT), 748 + FBNIC_TLV_ATTR_S32(FBNIC_FW_TSENE_ERROR), 745 749 FBNIC_TLV_ATTR_LAST 746 750 }; 747 751 ··· 750 754 { 751 755 struct fbnic_fw_completion *cmpl_data; 752 756 struct fbnic_dev *fbd = opaque; 757 + s32 err_resp; 753 758 int err = 0; 754 759 755 760 /* Verify we have a completion pointer to provide with data */ 756 761 cmpl_data = fbnic_fw_get_cmpl_by_type(fbd, 757 762 FBNIC_TLV_MSG_ID_TSENE_READ_RESP); 758 763 if (!cmpl_data) 759 - return -EINVAL; 764 + return -ENOSPC; 760 765 761 - if (results[FBNIC_TSENE_ERROR]) { 762 - err = fbnic_tlv_attr_get_unsigned(results[FBNIC_TSENE_ERROR]); 763 - if (err) 764 - goto exit_complete; 765 - } 766 + err_resp = fta_get_sint(results, FBNIC_FW_TSENE_ERROR); 767 + if (err_resp) 768 + goto msg_err; 766 769 767 - if (!results[FBNIC_TSENE_THERM] || !results[FBNIC_TSENE_VOLT]) { 770 + if (!results[FBNIC_FW_TSENE_THERM] || !results[FBNIC_FW_TSENE_VOLT]) { 768 771 err = -EINVAL; 769 - goto exit_complete; 772 + goto msg_err; 770 773 } 771 774 772 775 cmpl_data->u.tsene.millidegrees = 773 - fbnic_tlv_attr_get_signed(results[FBNIC_TSENE_THERM]); 776 + fta_get_sint(results, FBNIC_FW_TSENE_THERM); 774 777 cmpl_data->u.tsene.millivolts = 775 - fbnic_tlv_attr_get_signed(results[FBNIC_TSENE_VOLT]); 778 + fta_get_sint(results, FBNIC_FW_TSENE_VOLT); 776 779 777 - exit_complete: 778 - cmpl_data->result = err; 780 + msg_err: 781 + cmpl_data->result = err_resp ? : err; 779 782 complete(&cmpl_data->done); 780 783 fbnic_fw_put_cmpl(cmpl_data); 781 784
+4 -4
drivers/net/ethernet/meta/fbnic/fbnic_fw.h
··· 139 139 }; 140 140 141 141 enum { 142 - FBNIC_TSENE_THERM = 0x0, 143 - FBNIC_TSENE_VOLT = 0x1, 144 - FBNIC_TSENE_ERROR = 0x2, 145 - FBNIC_TSENE_MSG_MAX 142 + FBNIC_FW_TSENE_THERM = 0x0, 143 + FBNIC_FW_TSENE_VOLT = 0x1, 144 + FBNIC_FW_TSENE_ERROR = 0x2, 145 + FBNIC_FW_TSENE_MSG_MAX 146 146 }; 147 147 148 148 enum {
+43 -12
drivers/net/ethernet/meta/fbnic/fbnic_tlv.c
··· 196 196 /** 197 197 * fbnic_tlv_attr_get_unsigned - Retrieve unsigned value from result 198 198 * @attr: Attribute to retrieve data from 199 + * @def: The default value if attr is NULL 199 200 * 200 201 * Return: unsigned 64b value containing integer value 201 202 **/ 202 - u64 fbnic_tlv_attr_get_unsigned(struct fbnic_tlv_msg *attr) 203 + u64 fbnic_tlv_attr_get_unsigned(struct fbnic_tlv_msg *attr, u64 def) 203 204 { 204 205 __le64 le64_value = 0; 206 + 207 + if (!attr) 208 + return def; 205 209 206 210 memcpy(&le64_value, &attr->value[0], 207 211 le16_to_cpu(attr->hdr.len) - sizeof(*attr)); ··· 216 212 /** 217 213 * fbnic_tlv_attr_get_signed - Retrieve signed value from result 218 214 * @attr: Attribute to retrieve data from 215 + * @def: The default value if attr is NULL 219 216 * 220 217 * Return: signed 64b value containing integer value 221 218 **/ 222 - s64 fbnic_tlv_attr_get_signed(struct fbnic_tlv_msg *attr) 219 + s64 fbnic_tlv_attr_get_signed(struct fbnic_tlv_msg *attr, s64 def) 223 220 { 224 - int shift = (8 + sizeof(*attr) - le16_to_cpu(attr->hdr.len)) * 8; 225 221 __le64 le64_value = 0; 222 + int shift; 226 223 s64 value; 224 + 225 + if (!attr) 226 + return def; 227 + 228 + shift = (8 + sizeof(*attr) - le16_to_cpu(attr->hdr.len)) * 8; 227 229 228 230 /* Copy the value and adjust for byte ordering */ 229 231 memcpy(&le64_value, &attr->value[0], ··· 243 233 /** 244 234 * fbnic_tlv_attr_get_string - Retrieve string value from result 245 235 * @attr: Attribute to retrieve data from 246 - * @str: Pointer to an allocated string to store the data 247 - * @max_size: The maximum size which can be in str 236 + * @dst: Pointer to an allocated string to store the data 237 + * @dstsize: The maximum size which can be in dst 248 238 * 249 - * Return: the size of the string read from firmware 239 + * Return: the size of the string read from firmware or negative error. 250 240 **/ 251 - size_t fbnic_tlv_attr_get_string(struct fbnic_tlv_msg *attr, char *str, 252 - size_t max_size) 241 + ssize_t fbnic_tlv_attr_get_string(struct fbnic_tlv_msg *attr, char *dst, 242 + size_t dstsize) 253 243 { 254 - max_size = min_t(size_t, max_size, 255 - (le16_to_cpu(attr->hdr.len) * 4) - sizeof(*attr)); 256 - memcpy(str, &attr->value, max_size); 244 + size_t srclen, len; 245 + ssize_t ret; 257 246 258 - return max_size; 247 + if (!attr) 248 + return -EINVAL; 249 + 250 + if (dstsize == 0) 251 + return -E2BIG; 252 + 253 + srclen = le16_to_cpu(attr->hdr.len) - sizeof(*attr); 254 + if (srclen > 0 && attr->value[srclen - 1] == '\0') 255 + srclen--; 256 + 257 + if (srclen >= dstsize) { 258 + len = dstsize - 1; 259 + ret = -E2BIG; 260 + } else { 261 + len = srclen; 262 + ret = len; 263 + } 264 + 265 + memcpy(dst, &attr->value, len); 266 + /* Zero pad end of dst. */ 267 + memset(dst + len, 0, dstsize - len); 268 + 269 + return ret; 259 270 } 260 271 261 272 /**
+11 -28
drivers/net/ethernet/meta/fbnic/fbnic_tlv.h
··· 114 114 return !!attr; 115 115 } 116 116 117 - u64 fbnic_tlv_attr_get_unsigned(struct fbnic_tlv_msg *attr); 118 - s64 fbnic_tlv_attr_get_signed(struct fbnic_tlv_msg *attr); 119 - size_t fbnic_tlv_attr_get_string(struct fbnic_tlv_msg *attr, char *str, 120 - size_t max_size); 121 - 122 - #define get_unsigned_result(id, location) \ 123 - do { \ 124 - struct fbnic_tlv_msg *result = results[id]; \ 125 - if (result) \ 126 - location = fbnic_tlv_attr_get_unsigned(result); \ 127 - } while (0) 128 - 129 - #define get_signed_result(id, location) \ 130 - do { \ 131 - struct fbnic_tlv_msg *result = results[id]; \ 132 - if (result) \ 133 - location = fbnic_tlv_attr_get_signed(result); \ 134 - } while (0) 135 - 136 - #define get_string_result(id, size, str, max_size) \ 137 - do { \ 138 - struct fbnic_tlv_msg *result = results[id]; \ 139 - if (result) \ 140 - size = fbnic_tlv_attr_get_string(result, str, max_size); \ 141 - } while (0) 142 - 143 - #define get_bool(id) (!!(results[id])) 144 - 117 + u64 fbnic_tlv_attr_get_unsigned(struct fbnic_tlv_msg *attr, u64 def); 118 + s64 fbnic_tlv_attr_get_signed(struct fbnic_tlv_msg *attr, s64 def); 119 + ssize_t fbnic_tlv_attr_get_string(struct fbnic_tlv_msg *attr, char *dst, 120 + size_t dstsize); 145 121 struct fbnic_tlv_msg *fbnic_tlv_msg_alloc(u16 msg_id); 146 122 int fbnic_tlv_attr_put_flag(struct fbnic_tlv_msg *msg, const u16 attr_id); 147 123 int fbnic_tlv_attr_put_value(struct fbnic_tlv_msg *msg, const u16 attr_id, ··· 145 169 int fbnic_tlv_msg_parse(void *opaque, struct fbnic_tlv_msg *msg, 146 170 const struct fbnic_tlv_parser *parser); 147 171 int fbnic_tlv_parser_error(void *opaque, struct fbnic_tlv_msg **results); 172 + 173 + #define fta_get_uint(_results, _id) \ 174 + fbnic_tlv_attr_get_unsigned(_results[_id], 0) 175 + #define fta_get_sint(_results, _id) \ 176 + fbnic_tlv_attr_get_signed(_results[_id], 0) 177 + #define fta_get_str(_results, _id, _dst, _dstsize) \ 178 + fbnic_tlv_attr_get_string(_results[_id], _dst, _dstsize) 148 179 149 180 #define FBNIC_TLV_MSG_ERROR \ 150 181 FBNIC_TLV_PARSER(UNKNOWN, NULL, fbnic_tlv_parser_error)