1bdf86d0eSRuss Weight // SPDX-License-Identifier: GPL-2.0 2bdf86d0eSRuss Weight /* 3bdf86d0eSRuss Weight * Intel MAX10 Board Management Controller Secure Update Driver 4bdf86d0eSRuss Weight * 5bdf86d0eSRuss Weight * Copyright (C) 2019-2022 Intel Corporation. All rights reserved. 6bdf86d0eSRuss Weight * 7bdf86d0eSRuss Weight */ 8bdf86d0eSRuss Weight #include <linux/bitfield.h> 9bdf86d0eSRuss Weight #include <linux/device.h> 10bdf86d0eSRuss Weight #include <linux/firmware.h> 11bdf86d0eSRuss Weight #include <linux/mfd/intel-m10-bmc.h> 12bdf86d0eSRuss Weight #include <linux/mod_devicetable.h> 13bdf86d0eSRuss Weight #include <linux/module.h> 14bdf86d0eSRuss Weight #include <linux/platform_device.h> 15bdf86d0eSRuss Weight #include <linux/slab.h> 16bdf86d0eSRuss Weight 17001a734aSIlpo Järvinen struct m10bmc_sec; 18001a734aSIlpo Järvinen 19001a734aSIlpo Järvinen struct m10bmc_sec_ops { 20001a734aSIlpo Järvinen int (*rsu_status)(struct m10bmc_sec *sec); 21001a734aSIlpo Järvinen }; 22001a734aSIlpo Järvinen 23bdf86d0eSRuss Weight struct m10bmc_sec { 24bdf86d0eSRuss Weight struct device *dev; 25bdf86d0eSRuss Weight struct intel_m10bmc *m10bmc; 265cd339b3SRuss Weight struct fw_upload *fwl; 275cd339b3SRuss Weight char *fw_name; 285cd339b3SRuss Weight u32 fw_name_id; 295cd339b3SRuss Weight bool cancel_request; 30001a734aSIlpo Järvinen const struct m10bmc_sec_ops *ops; 31bdf86d0eSRuss Weight }; 32bdf86d0eSRuss Weight 335cd339b3SRuss Weight static DEFINE_XARRAY_ALLOC(fw_upload_xa); 345cd339b3SRuss Weight 35bdf86d0eSRuss Weight /* Root Entry Hash (REH) support */ 36bdf86d0eSRuss Weight #define REH_SHA256_SIZE 32 37bdf86d0eSRuss Weight #define REH_SHA384_SIZE 48 38bdf86d0eSRuss Weight #define REH_MAGIC GENMASK(15, 0) 39bdf86d0eSRuss Weight #define REH_SHA_NUM_BYTES GENMASK(31, 16) 40bdf86d0eSRuss Weight 413e10c805SIlpo Järvinen static int m10bmc_sec_write(struct m10bmc_sec *sec, const u8 *buf, u32 offset, u32 size) 423e10c805SIlpo Järvinen { 433e10c805SIlpo Järvinen struct intel_m10bmc *m10bmc = sec->m10bmc; 443e10c805SIlpo Järvinen unsigned int stride = regmap_get_reg_stride(m10bmc->regmap); 453e10c805SIlpo Järvinen u32 write_count = size / stride; 463e10c805SIlpo Järvinen u32 leftover_offset = write_count * stride; 473e10c805SIlpo Järvinen u32 leftover_size = size - leftover_offset; 483e10c805SIlpo Järvinen u32 leftover_tmp = 0; 493e10c805SIlpo Järvinen int ret; 503e10c805SIlpo Järvinen 51acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops) 52acf63c45SIlpo Järvinen return sec->m10bmc->flash_bulk_ops->write(m10bmc, buf, offset, size); 53acf63c45SIlpo Järvinen 543e10c805SIlpo Järvinen if (WARN_ON_ONCE(stride > sizeof(leftover_tmp))) 553e10c805SIlpo Järvinen return -EINVAL; 563e10c805SIlpo Järvinen 573e10c805SIlpo Järvinen ret = regmap_bulk_write(m10bmc->regmap, M10BMC_STAGING_BASE + offset, 583e10c805SIlpo Järvinen buf + offset, write_count); 593e10c805SIlpo Järvinen if (ret) 603e10c805SIlpo Järvinen return ret; 613e10c805SIlpo Järvinen 623e10c805SIlpo Järvinen /* If size is not aligned to stride, handle the remainder bytes with regmap_write() */ 633e10c805SIlpo Järvinen if (leftover_size) { 643e10c805SIlpo Järvinen memcpy(&leftover_tmp, buf + leftover_offset, leftover_size); 653e10c805SIlpo Järvinen ret = regmap_write(m10bmc->regmap, M10BMC_STAGING_BASE + offset + leftover_offset, 663e10c805SIlpo Järvinen leftover_tmp); 673e10c805SIlpo Järvinen if (ret) 683e10c805SIlpo Järvinen return ret; 693e10c805SIlpo Järvinen } 703e10c805SIlpo Järvinen 713e10c805SIlpo Järvinen return 0; 723e10c805SIlpo Järvinen } 733e10c805SIlpo Järvinen 743e10c805SIlpo Järvinen static int m10bmc_sec_read(struct m10bmc_sec *sec, u8 *buf, u32 addr, u32 size) 753e10c805SIlpo Järvinen { 763e10c805SIlpo Järvinen struct intel_m10bmc *m10bmc = sec->m10bmc; 773e10c805SIlpo Järvinen unsigned int stride = regmap_get_reg_stride(m10bmc->regmap); 783e10c805SIlpo Järvinen u32 read_count = size / stride; 793e10c805SIlpo Järvinen u32 leftover_offset = read_count * stride; 803e10c805SIlpo Järvinen u32 leftover_size = size - leftover_offset; 813e10c805SIlpo Järvinen u32 leftover_tmp; 823e10c805SIlpo Järvinen int ret; 833e10c805SIlpo Järvinen 84acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops) 85acf63c45SIlpo Järvinen return sec->m10bmc->flash_bulk_ops->read(m10bmc, buf, addr, size); 86acf63c45SIlpo Järvinen 873e10c805SIlpo Järvinen if (WARN_ON_ONCE(stride > sizeof(leftover_tmp))) 883e10c805SIlpo Järvinen return -EINVAL; 893e10c805SIlpo Järvinen 903e10c805SIlpo Järvinen ret = regmap_bulk_read(m10bmc->regmap, addr, buf, read_count); 913e10c805SIlpo Järvinen if (ret) 923e10c805SIlpo Järvinen return ret; 933e10c805SIlpo Järvinen 943e10c805SIlpo Järvinen /* If size is not aligned to stride, handle the remainder bytes with regmap_read() */ 953e10c805SIlpo Järvinen if (leftover_size) { 963e10c805SIlpo Järvinen ret = regmap_read(m10bmc->regmap, addr + leftover_offset, &leftover_tmp); 973e10c805SIlpo Järvinen if (ret) 983e10c805SIlpo Järvinen return ret; 993e10c805SIlpo Järvinen memcpy(buf + leftover_offset, &leftover_tmp, leftover_size); 1003e10c805SIlpo Järvinen } 1013e10c805SIlpo Järvinen 1023e10c805SIlpo Järvinen return 0; 1033e10c805SIlpo Järvinen } 1043e10c805SIlpo Järvinen 1053e10c805SIlpo Järvinen 106bdf86d0eSRuss Weight static ssize_t 107bdf86d0eSRuss Weight show_root_entry_hash(struct device *dev, u32 exp_magic, 108bdf86d0eSRuss Weight u32 prog_addr, u32 reh_addr, char *buf) 109bdf86d0eSRuss Weight { 110bdf86d0eSRuss Weight struct m10bmc_sec *sec = dev_get_drvdata(dev); 111bdf86d0eSRuss Weight int sha_num_bytes, i, ret, cnt = 0; 112bdf86d0eSRuss Weight u8 hash[REH_SHA384_SIZE]; 113bdf86d0eSRuss Weight u32 magic; 114bdf86d0eSRuss Weight 1153e10c805SIlpo Järvinen ret = m10bmc_sec_read(sec, (u8 *)&magic, prog_addr, sizeof(magic)); 116bdf86d0eSRuss Weight if (ret) 117bdf86d0eSRuss Weight return ret; 118bdf86d0eSRuss Weight 119bdf86d0eSRuss Weight if (FIELD_GET(REH_MAGIC, magic) != exp_magic) 120bdf86d0eSRuss Weight return sysfs_emit(buf, "hash not programmed\n"); 121bdf86d0eSRuss Weight 122bdf86d0eSRuss Weight sha_num_bytes = FIELD_GET(REH_SHA_NUM_BYTES, magic) / 8; 1233e10c805SIlpo Järvinen if (sha_num_bytes != REH_SHA256_SIZE && 1243e10c805SIlpo Järvinen sha_num_bytes != REH_SHA384_SIZE) { 125bdf86d0eSRuss Weight dev_err(sec->dev, "%s bad sha num bytes %d\n", __func__, 126bdf86d0eSRuss Weight sha_num_bytes); 127bdf86d0eSRuss Weight return -EINVAL; 128bdf86d0eSRuss Weight } 129bdf86d0eSRuss Weight 1303e10c805SIlpo Järvinen ret = m10bmc_sec_read(sec, hash, reh_addr, sha_num_bytes); 131bdf86d0eSRuss Weight if (ret) { 1323e10c805SIlpo Järvinen dev_err(dev, "failed to read root entry hash\n"); 133bdf86d0eSRuss Weight return ret; 134bdf86d0eSRuss Weight } 135bdf86d0eSRuss Weight 136bdf86d0eSRuss Weight for (i = 0; i < sha_num_bytes; i++) 137bdf86d0eSRuss Weight cnt += sprintf(buf + cnt, "%02x", hash[i]); 138bdf86d0eSRuss Weight cnt += sprintf(buf + cnt, "\n"); 139bdf86d0eSRuss Weight 140bdf86d0eSRuss Weight return cnt; 141bdf86d0eSRuss Weight } 142bdf86d0eSRuss Weight 1436052a005SIlpo Järvinen #define DEVICE_ATTR_SEC_REH_RO(_name) \ 144bdf86d0eSRuss Weight static ssize_t _name##_root_entry_hash_show(struct device *dev, \ 145bdf86d0eSRuss Weight struct device_attribute *attr, \ 146bdf86d0eSRuss Weight char *buf) \ 1476052a005SIlpo Järvinen { \ 1486052a005SIlpo Järvinen struct m10bmc_sec *sec = dev_get_drvdata(dev); \ 1496052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; \ 1506052a005SIlpo Järvinen \ 1516052a005SIlpo Järvinen return show_root_entry_hash(dev, csr_map->_name##_magic, \ 1526052a005SIlpo Järvinen csr_map->_name##_prog_addr, \ 1536052a005SIlpo Järvinen csr_map->_name##_reh_addr, \ 1546052a005SIlpo Järvinen buf); \ 1556052a005SIlpo Järvinen } \ 156bdf86d0eSRuss Weight static DEVICE_ATTR_RO(_name##_root_entry_hash) 157bdf86d0eSRuss Weight 1586052a005SIlpo Järvinen DEVICE_ATTR_SEC_REH_RO(bmc); 1596052a005SIlpo Järvinen DEVICE_ATTR_SEC_REH_RO(sr); 1606052a005SIlpo Järvinen DEVICE_ATTR_SEC_REH_RO(pr); 161bdf86d0eSRuss Weight 1627f03d84aSRuss Weight #define CSK_BIT_LEN 128U 1637f03d84aSRuss Weight #define CSK_32ARRAY_SIZE DIV_ROUND_UP(CSK_BIT_LEN, 32) 1647f03d84aSRuss Weight 1657f03d84aSRuss Weight static ssize_t 1667f03d84aSRuss Weight show_canceled_csk(struct device *dev, u32 addr, char *buf) 1677f03d84aSRuss Weight { 1683e10c805SIlpo Järvinen unsigned int i, size = CSK_32ARRAY_SIZE * sizeof(u32); 1697f03d84aSRuss Weight struct m10bmc_sec *sec = dev_get_drvdata(dev); 1707f03d84aSRuss Weight DECLARE_BITMAP(csk_map, CSK_BIT_LEN); 1717f03d84aSRuss Weight __le32 csk_le32[CSK_32ARRAY_SIZE]; 1727f03d84aSRuss Weight u32 csk32[CSK_32ARRAY_SIZE]; 1737f03d84aSRuss Weight int ret; 1747f03d84aSRuss Weight 1753e10c805SIlpo Järvinen ret = m10bmc_sec_read(sec, (u8 *)&csk_le32, addr, size); 1767f03d84aSRuss Weight if (ret) { 1773e10c805SIlpo Järvinen dev_err(sec->dev, "failed to read CSK vector\n"); 1787f03d84aSRuss Weight return ret; 1797f03d84aSRuss Weight } 1807f03d84aSRuss Weight 1817f03d84aSRuss Weight for (i = 0; i < CSK_32ARRAY_SIZE; i++) 1827f03d84aSRuss Weight csk32[i] = le32_to_cpu(((csk_le32[i]))); 1837f03d84aSRuss Weight 1847f03d84aSRuss Weight bitmap_from_arr32(csk_map, csk32, CSK_BIT_LEN); 1857f03d84aSRuss Weight bitmap_complement(csk_map, csk_map, CSK_BIT_LEN); 1867f03d84aSRuss Weight return bitmap_print_to_pagebuf(1, buf, csk_map, CSK_BIT_LEN); 1877f03d84aSRuss Weight } 1887f03d84aSRuss Weight 1896052a005SIlpo Järvinen #define DEVICE_ATTR_SEC_CSK_RO(_name) \ 1907f03d84aSRuss Weight static ssize_t _name##_canceled_csks_show(struct device *dev, \ 1917f03d84aSRuss Weight struct device_attribute *attr, \ 1927f03d84aSRuss Weight char *buf) \ 1936052a005SIlpo Järvinen { \ 1946052a005SIlpo Järvinen struct m10bmc_sec *sec = dev_get_drvdata(dev); \ 1956052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; \ 1966052a005SIlpo Järvinen \ 1976052a005SIlpo Järvinen return show_canceled_csk(dev, \ 1986052a005SIlpo Järvinen csr_map->_name##_prog_addr + CSK_VEC_OFFSET, \ 1996052a005SIlpo Järvinen buf); \ 2006052a005SIlpo Järvinen } \ 2017f03d84aSRuss Weight static DEVICE_ATTR_RO(_name##_canceled_csks) 2027f03d84aSRuss Weight 2037f03d84aSRuss Weight #define CSK_VEC_OFFSET 0x34 2047f03d84aSRuss Weight 2056052a005SIlpo Järvinen DEVICE_ATTR_SEC_CSK_RO(bmc); 2066052a005SIlpo Järvinen DEVICE_ATTR_SEC_CSK_RO(sr); 2076052a005SIlpo Järvinen DEVICE_ATTR_SEC_CSK_RO(pr); 2087f03d84aSRuss Weight 209154afa5cSRuss Weight #define FLASH_COUNT_SIZE 4096 /* count stored as inverted bit vector */ 210154afa5cSRuss Weight 211154afa5cSRuss Weight static ssize_t flash_count_show(struct device *dev, 212154afa5cSRuss Weight struct device_attribute *attr, char *buf) 213154afa5cSRuss Weight { 214154afa5cSRuss Weight struct m10bmc_sec *sec = dev_get_drvdata(dev); 2156052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 2163e10c805SIlpo Järvinen unsigned int num_bits; 217154afa5cSRuss Weight u8 *flash_buf; 218154afa5cSRuss Weight int cnt, ret; 219154afa5cSRuss Weight 220154afa5cSRuss Weight num_bits = FLASH_COUNT_SIZE * 8; 221154afa5cSRuss Weight 222468c9d92SRuss Weight flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL); 223468c9d92SRuss Weight if (!flash_buf) 224468c9d92SRuss Weight return -ENOMEM; 225468c9d92SRuss Weight 2263e10c805SIlpo Järvinen ret = m10bmc_sec_read(sec, flash_buf, csr_map->rsu_update_counter, 2273e10c805SIlpo Järvinen FLASH_COUNT_SIZE); 228154afa5cSRuss Weight if (ret) { 2293e10c805SIlpo Järvinen dev_err(sec->dev, "failed to read flash count\n"); 230154afa5cSRuss Weight goto exit_free; 231154afa5cSRuss Weight } 232154afa5cSRuss Weight cnt = num_bits - bitmap_weight((unsigned long *)flash_buf, num_bits); 233154afa5cSRuss Weight 234154afa5cSRuss Weight exit_free: 235154afa5cSRuss Weight kfree(flash_buf); 236154afa5cSRuss Weight 237154afa5cSRuss Weight return ret ? : sysfs_emit(buf, "%u\n", cnt); 238154afa5cSRuss Weight } 239154afa5cSRuss Weight static DEVICE_ATTR_RO(flash_count); 240154afa5cSRuss Weight 241bdf86d0eSRuss Weight static struct attribute *m10bmc_security_attrs[] = { 242154afa5cSRuss Weight &dev_attr_flash_count.attr, 243bdf86d0eSRuss Weight &dev_attr_bmc_root_entry_hash.attr, 244bdf86d0eSRuss Weight &dev_attr_sr_root_entry_hash.attr, 245bdf86d0eSRuss Weight &dev_attr_pr_root_entry_hash.attr, 2467f03d84aSRuss Weight &dev_attr_sr_canceled_csks.attr, 2477f03d84aSRuss Weight &dev_attr_pr_canceled_csks.attr, 2487f03d84aSRuss Weight &dev_attr_bmc_canceled_csks.attr, 249bdf86d0eSRuss Weight NULL, 250bdf86d0eSRuss Weight }; 251bdf86d0eSRuss Weight 252bdf86d0eSRuss Weight static struct attribute_group m10bmc_security_attr_group = { 253bdf86d0eSRuss Weight .name = "security", 254bdf86d0eSRuss Weight .attrs = m10bmc_security_attrs, 255bdf86d0eSRuss Weight }; 256bdf86d0eSRuss Weight 257bdf86d0eSRuss Weight static const struct attribute_group *m10bmc_sec_attr_groups[] = { 258bdf86d0eSRuss Weight &m10bmc_security_attr_group, 259bdf86d0eSRuss Weight NULL, 260bdf86d0eSRuss Weight }; 261bdf86d0eSRuss Weight 2625cd339b3SRuss Weight static void log_error_regs(struct m10bmc_sec *sec, u32 doorbell) 2635cd339b3SRuss Weight { 2646052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 2655cd339b3SRuss Weight u32 auth_result; 2665cd339b3SRuss Weight 267001a734aSIlpo Järvinen dev_err(sec->dev, "Doorbell: 0x%08x\n", doorbell); 2685cd339b3SRuss Weight 2696052a005SIlpo Järvinen if (!m10bmc_sys_read(sec->m10bmc, csr_map->auth_result, &auth_result)) 2705cd339b3SRuss Weight dev_err(sec->dev, "RSU auth result: 0x%08x\n", auth_result); 2715cd339b3SRuss Weight } 2725cd339b3SRuss Weight 273001a734aSIlpo Järvinen static int m10bmc_sec_n3000_rsu_status(struct m10bmc_sec *sec) 274001a734aSIlpo Järvinen { 275001a734aSIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 276001a734aSIlpo Järvinen u32 doorbell; 277001a734aSIlpo Järvinen int ret; 278001a734aSIlpo Järvinen 279001a734aSIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, &doorbell); 280001a734aSIlpo Järvinen if (ret) 281001a734aSIlpo Järvinen return ret; 282001a734aSIlpo Järvinen 283001a734aSIlpo Järvinen return FIELD_GET(DRBL_RSU_STATUS, doorbell); 284001a734aSIlpo Järvinen } 285001a734aSIlpo Järvinen 286acf63c45SIlpo Järvinen static int m10bmc_sec_n6000_rsu_status(struct m10bmc_sec *sec) 287acf63c45SIlpo Järvinen { 288acf63c45SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 289acf63c45SIlpo Järvinen u32 auth_result; 290acf63c45SIlpo Järvinen int ret; 291acf63c45SIlpo Järvinen 292acf63c45SIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->auth_result, &auth_result); 293acf63c45SIlpo Järvinen if (ret) 294acf63c45SIlpo Järvinen return ret; 295acf63c45SIlpo Järvinen 296acf63c45SIlpo Järvinen return FIELD_GET(AUTH_RESULT_RSU_STATUS, auth_result); 297acf63c45SIlpo Järvinen } 298acf63c45SIlpo Järvinen 299da04fa8cSIlpo Järvinen static bool rsu_status_ok(u32 status) 300da04fa8cSIlpo Järvinen { 301da04fa8cSIlpo Järvinen return (status == RSU_STAT_NORMAL || 302da04fa8cSIlpo Järvinen status == RSU_STAT_NIOS_OK || 303da04fa8cSIlpo Järvinen status == RSU_STAT_USER_OK || 304da04fa8cSIlpo Järvinen status == RSU_STAT_FACTORY_OK); 305da04fa8cSIlpo Järvinen } 306da04fa8cSIlpo Järvinen 307da04fa8cSIlpo Järvinen static bool rsu_progress_done(u32 progress) 308da04fa8cSIlpo Järvinen { 309da04fa8cSIlpo Järvinen return (progress == RSU_PROG_IDLE || 310da04fa8cSIlpo Järvinen progress == RSU_PROG_RSU_DONE); 311da04fa8cSIlpo Järvinen } 312da04fa8cSIlpo Järvinen 313da04fa8cSIlpo Järvinen static bool rsu_progress_busy(u32 progress) 314da04fa8cSIlpo Järvinen { 315da04fa8cSIlpo Järvinen return (progress == RSU_PROG_AUTHENTICATING || 316da04fa8cSIlpo Järvinen progress == RSU_PROG_COPYING || 317da04fa8cSIlpo Järvinen progress == RSU_PROG_UPDATE_CANCEL || 318da04fa8cSIlpo Järvinen progress == RSU_PROG_PROGRAM_KEY_HASH); 319da04fa8cSIlpo Järvinen } 320da04fa8cSIlpo Järvinen 321001a734aSIlpo Järvinen static int m10bmc_sec_progress_status(struct m10bmc_sec *sec, u32 *doorbell_reg, 322001a734aSIlpo Järvinen u32 *progress, u32 *status) 323001a734aSIlpo Järvinen { 324001a734aSIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 325001a734aSIlpo Järvinen int ret; 326001a734aSIlpo Järvinen 327001a734aSIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, doorbell_reg); 328001a734aSIlpo Järvinen if (ret) 329001a734aSIlpo Järvinen return ret; 330001a734aSIlpo Järvinen 331001a734aSIlpo Järvinen ret = sec->ops->rsu_status(sec); 332001a734aSIlpo Järvinen if (ret < 0) 333001a734aSIlpo Järvinen return ret; 334001a734aSIlpo Järvinen 335001a734aSIlpo Järvinen *status = ret; 336001a734aSIlpo Järvinen *progress = rsu_prog(*doorbell_reg); 337001a734aSIlpo Järvinen 338001a734aSIlpo Järvinen return 0; 339001a734aSIlpo Järvinen } 340001a734aSIlpo Järvinen 3415cd339b3SRuss Weight static enum fw_upload_err rsu_check_idle(struct m10bmc_sec *sec) 3425cd339b3SRuss Weight { 3436052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 3445cd339b3SRuss Weight u32 doorbell; 3455cd339b3SRuss Weight int ret; 3465cd339b3SRuss Weight 3476052a005SIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, &doorbell); 3485cd339b3SRuss Weight if (ret) 3495cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 3505cd339b3SRuss Weight 351da04fa8cSIlpo Järvinen if (!rsu_progress_done(rsu_prog(doorbell))) { 3525cd339b3SRuss Weight log_error_regs(sec, doorbell); 3535cd339b3SRuss Weight return FW_UPLOAD_ERR_BUSY; 3545cd339b3SRuss Weight } 3555cd339b3SRuss Weight 3565cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 3575cd339b3SRuss Weight } 3585cd339b3SRuss Weight 359001a734aSIlpo Järvinen static inline bool rsu_start_done(u32 doorbell_reg, u32 progress, u32 status) 3605cd339b3SRuss Weight { 361001a734aSIlpo Järvinen if (doorbell_reg & DRBL_RSU_REQUEST) 3625cd339b3SRuss Weight return false; 3635cd339b3SRuss Weight 3645cd339b3SRuss Weight if (status == RSU_STAT_ERASE_FAIL || status == RSU_STAT_WEAROUT) 3655cd339b3SRuss Weight return true; 3665cd339b3SRuss Weight 367da04fa8cSIlpo Järvinen if (!rsu_progress_done(progress)) 3685cd339b3SRuss Weight return true; 3695cd339b3SRuss Weight 3705cd339b3SRuss Weight return false; 3715cd339b3SRuss Weight } 3725cd339b3SRuss Weight 3735cd339b3SRuss Weight static enum fw_upload_err rsu_update_init(struct m10bmc_sec *sec) 3745cd339b3SRuss Weight { 3756052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 376001a734aSIlpo Järvinen u32 doorbell_reg, progress, status; 377001a734aSIlpo Järvinen int ret, err; 3785cd339b3SRuss Weight 379c452e3bdSIlpo Järvinen ret = m10bmc_sys_update_bits(sec->m10bmc, csr_map->doorbell, 3805cd339b3SRuss Weight DRBL_RSU_REQUEST | DRBL_HOST_STATUS, 3815cd339b3SRuss Weight DRBL_RSU_REQUEST | 3825cd339b3SRuss Weight FIELD_PREP(DRBL_HOST_STATUS, 3835cd339b3SRuss Weight HOST_STATUS_IDLE)); 3845cd339b3SRuss Weight if (ret) 3855cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 3865cd339b3SRuss Weight 387001a734aSIlpo Järvinen ret = read_poll_timeout(m10bmc_sec_progress_status, err, 388001a734aSIlpo Järvinen err < 0 || rsu_start_done(doorbell_reg, progress, status), 3895cd339b3SRuss Weight NIOS_HANDSHAKE_INTERVAL_US, 390001a734aSIlpo Järvinen NIOS_HANDSHAKE_TIMEOUT_US, 391001a734aSIlpo Järvinen false, 392001a734aSIlpo Järvinen sec, &doorbell_reg, &progress, &status); 3935cd339b3SRuss Weight 3945cd339b3SRuss Weight if (ret == -ETIMEDOUT) { 395001a734aSIlpo Järvinen log_error_regs(sec, doorbell_reg); 3965cd339b3SRuss Weight return FW_UPLOAD_ERR_TIMEOUT; 397001a734aSIlpo Järvinen } else if (err) { 3985cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 3995cd339b3SRuss Weight } 4005cd339b3SRuss Weight 4015cd339b3SRuss Weight if (status == RSU_STAT_WEAROUT) { 4025cd339b3SRuss Weight dev_warn(sec->dev, "Excessive flash update count detected\n"); 4035cd339b3SRuss Weight return FW_UPLOAD_ERR_WEAROUT; 4045cd339b3SRuss Weight } else if (status == RSU_STAT_ERASE_FAIL) { 405001a734aSIlpo Järvinen log_error_regs(sec, doorbell_reg); 4065cd339b3SRuss Weight return FW_UPLOAD_ERR_HW_ERROR; 4075cd339b3SRuss Weight } 4085cd339b3SRuss Weight 4095cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 4105cd339b3SRuss Weight } 4115cd339b3SRuss Weight 4125cd339b3SRuss Weight static enum fw_upload_err rsu_prog_ready(struct m10bmc_sec *sec) 4135cd339b3SRuss Weight { 4146052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 4155cd339b3SRuss Weight unsigned long poll_timeout; 4165cd339b3SRuss Weight u32 doorbell, progress; 4175cd339b3SRuss Weight int ret; 4185cd339b3SRuss Weight 4196052a005SIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, &doorbell); 4205cd339b3SRuss Weight if (ret) 4215cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 4225cd339b3SRuss Weight 4235cd339b3SRuss Weight poll_timeout = jiffies + msecs_to_jiffies(RSU_PREP_TIMEOUT_MS); 4245cd339b3SRuss Weight while (rsu_prog(doorbell) == RSU_PROG_PREPARE) { 4255cd339b3SRuss Weight msleep(RSU_PREP_INTERVAL_MS); 4265cd339b3SRuss Weight if (time_after(jiffies, poll_timeout)) 4275cd339b3SRuss Weight break; 4285cd339b3SRuss Weight 4296052a005SIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, &doorbell); 4305cd339b3SRuss Weight if (ret) 4315cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 4325cd339b3SRuss Weight } 4335cd339b3SRuss Weight 4345cd339b3SRuss Weight progress = rsu_prog(doorbell); 4355cd339b3SRuss Weight if (progress == RSU_PROG_PREPARE) { 4365cd339b3SRuss Weight log_error_regs(sec, doorbell); 4375cd339b3SRuss Weight return FW_UPLOAD_ERR_TIMEOUT; 4385cd339b3SRuss Weight } else if (progress != RSU_PROG_READY) { 4395cd339b3SRuss Weight log_error_regs(sec, doorbell); 4405cd339b3SRuss Weight return FW_UPLOAD_ERR_HW_ERROR; 4415cd339b3SRuss Weight } 4425cd339b3SRuss Weight 4435cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 4445cd339b3SRuss Weight } 4455cd339b3SRuss Weight 4465cd339b3SRuss Weight static enum fw_upload_err rsu_send_data(struct m10bmc_sec *sec) 4475cd339b3SRuss Weight { 4486052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 449001a734aSIlpo Järvinen u32 doorbell_reg, status; 4505cd339b3SRuss Weight int ret; 4515cd339b3SRuss Weight 452c452e3bdSIlpo Järvinen ret = m10bmc_sys_update_bits(sec->m10bmc, csr_map->doorbell, 4535cd339b3SRuss Weight DRBL_HOST_STATUS, 4545cd339b3SRuss Weight FIELD_PREP(DRBL_HOST_STATUS, 4555cd339b3SRuss Weight HOST_STATUS_WRITE_DONE)); 4565cd339b3SRuss Weight if (ret) 4575cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 4585cd339b3SRuss Weight 4595cd339b3SRuss Weight ret = regmap_read_poll_timeout(sec->m10bmc->regmap, 4606052a005SIlpo Järvinen csr_map->base + csr_map->doorbell, 461001a734aSIlpo Järvinen doorbell_reg, 462001a734aSIlpo Järvinen rsu_prog(doorbell_reg) != RSU_PROG_READY, 4635cd339b3SRuss Weight NIOS_HANDSHAKE_INTERVAL_US, 4645cd339b3SRuss Weight NIOS_HANDSHAKE_TIMEOUT_US); 4655cd339b3SRuss Weight 4665cd339b3SRuss Weight if (ret == -ETIMEDOUT) { 467001a734aSIlpo Järvinen log_error_regs(sec, doorbell_reg); 4685cd339b3SRuss Weight return FW_UPLOAD_ERR_TIMEOUT; 4695cd339b3SRuss Weight } else if (ret) { 4705cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 4715cd339b3SRuss Weight } 4725cd339b3SRuss Weight 473001a734aSIlpo Järvinen ret = sec->ops->rsu_status(sec); 474001a734aSIlpo Järvinen if (ret < 0) 475c3d79fdaSIlpo Järvinen return FW_UPLOAD_ERR_HW_ERROR; 476001a734aSIlpo Järvinen status = ret; 477001a734aSIlpo Järvinen 478001a734aSIlpo Järvinen if (!rsu_status_ok(status)) { 479001a734aSIlpo Järvinen log_error_regs(sec, doorbell_reg); 4805cd339b3SRuss Weight return FW_UPLOAD_ERR_HW_ERROR; 4815cd339b3SRuss Weight } 4825cd339b3SRuss Weight 4835cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 4845cd339b3SRuss Weight } 4855cd339b3SRuss Weight 486001a734aSIlpo Järvinen static int rsu_check_complete(struct m10bmc_sec *sec, u32 *doorbell_reg) 4875cd339b3SRuss Weight { 488001a734aSIlpo Järvinen u32 progress, status; 4896052a005SIlpo Järvinen 490001a734aSIlpo Järvinen if (m10bmc_sec_progress_status(sec, doorbell_reg, &progress, &status)) 4915cd339b3SRuss Weight return -EIO; 4925cd339b3SRuss Weight 493001a734aSIlpo Järvinen if (!rsu_status_ok(status)) 4945cd339b3SRuss Weight return -EINVAL; 4955cd339b3SRuss Weight 496001a734aSIlpo Järvinen if (rsu_progress_done(progress)) 4975cd339b3SRuss Weight return 0; 498da04fa8cSIlpo Järvinen 499001a734aSIlpo Järvinen if (rsu_progress_busy(progress)) 5005cd339b3SRuss Weight return -EAGAIN; 501da04fa8cSIlpo Järvinen 5025cd339b3SRuss Weight return -EINVAL; 5035cd339b3SRuss Weight } 5045cd339b3SRuss Weight 5055cd339b3SRuss Weight static enum fw_upload_err rsu_cancel(struct m10bmc_sec *sec) 5065cd339b3SRuss Weight { 5076052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 5085cd339b3SRuss Weight u32 doorbell; 5095cd339b3SRuss Weight int ret; 5105cd339b3SRuss Weight 5116052a005SIlpo Järvinen ret = m10bmc_sys_read(sec->m10bmc, csr_map->doorbell, &doorbell); 5125cd339b3SRuss Weight if (ret) 5135cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 5145cd339b3SRuss Weight 5155cd339b3SRuss Weight if (rsu_prog(doorbell) != RSU_PROG_READY) 5165cd339b3SRuss Weight return FW_UPLOAD_ERR_BUSY; 5175cd339b3SRuss Weight 518c452e3bdSIlpo Järvinen ret = m10bmc_sys_update_bits(sec->m10bmc, csr_map->doorbell, 5195cd339b3SRuss Weight DRBL_HOST_STATUS, 5205cd339b3SRuss Weight FIELD_PREP(DRBL_HOST_STATUS, 5215cd339b3SRuss Weight HOST_STATUS_ABORT_RSU)); 5225cd339b3SRuss Weight if (ret) 5235cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 5245cd339b3SRuss Weight 5255cd339b3SRuss Weight return FW_UPLOAD_ERR_CANCELED; 5265cd339b3SRuss Weight } 5275cd339b3SRuss Weight 5285cd339b3SRuss Weight static enum fw_upload_err m10bmc_sec_prepare(struct fw_upload *fwl, 5295cd339b3SRuss Weight const u8 *data, u32 size) 5305cd339b3SRuss Weight { 5315cd339b3SRuss Weight struct m10bmc_sec *sec = fwl->dd_handle; 532*74c6317dSIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 5335cd339b3SRuss Weight u32 ret; 5345cd339b3SRuss Weight 5355cd339b3SRuss Weight sec->cancel_request = false; 5365cd339b3SRuss Weight 537*74c6317dSIlpo Järvinen if (!size || size > csr_map->staging_size) 5385cd339b3SRuss Weight return FW_UPLOAD_ERR_INVALID_SIZE; 5395cd339b3SRuss Weight 540acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops) 541acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops->lock_write(sec->m10bmc)) 542acf63c45SIlpo Järvinen return FW_UPLOAD_ERR_BUSY; 543acf63c45SIlpo Järvinen 5445cd339b3SRuss Weight ret = rsu_check_idle(sec); 5455cd339b3SRuss Weight if (ret != FW_UPLOAD_ERR_NONE) 546acf63c45SIlpo Järvinen goto unlock_flash; 5475cd339b3SRuss Weight 548867cae44SIlpo Järvinen m10bmc_fw_state_set(sec->m10bmc, M10BMC_FW_STATE_SEC_UPDATE_PREPARE); 549867cae44SIlpo Järvinen 5505cd339b3SRuss Weight ret = rsu_update_init(sec); 5515cd339b3SRuss Weight if (ret != FW_UPLOAD_ERR_NONE) 552867cae44SIlpo Järvinen goto fw_state_exit; 5535cd339b3SRuss Weight 5545cd339b3SRuss Weight ret = rsu_prog_ready(sec); 5555cd339b3SRuss Weight if (ret != FW_UPLOAD_ERR_NONE) 556867cae44SIlpo Järvinen goto fw_state_exit; 5575cd339b3SRuss Weight 558acf63c45SIlpo Järvinen if (sec->cancel_request) { 559acf63c45SIlpo Järvinen ret = rsu_cancel(sec); 560867cae44SIlpo Järvinen goto fw_state_exit; 561acf63c45SIlpo Järvinen } 5625cd339b3SRuss Weight 563867cae44SIlpo Järvinen m10bmc_fw_state_set(sec->m10bmc, M10BMC_FW_STATE_SEC_UPDATE_WRITE); 564867cae44SIlpo Järvinen 5655cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 566acf63c45SIlpo Järvinen 567867cae44SIlpo Järvinen fw_state_exit: 568867cae44SIlpo Järvinen m10bmc_fw_state_set(sec->m10bmc, M10BMC_FW_STATE_NORMAL); 569867cae44SIlpo Järvinen 570acf63c45SIlpo Järvinen unlock_flash: 571acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops) 572acf63c45SIlpo Järvinen sec->m10bmc->flash_bulk_ops->unlock_write(sec->m10bmc); 573acf63c45SIlpo Järvinen return ret; 5745cd339b3SRuss Weight } 5755cd339b3SRuss Weight 5765cd339b3SRuss Weight #define WRITE_BLOCK_SIZE 0x4000 /* Default write-block size is 0x4000 bytes */ 5775cd339b3SRuss Weight 5783e10c805SIlpo Järvinen static enum fw_upload_err m10bmc_sec_fw_write(struct fw_upload *fwl, const u8 *data, 5795cd339b3SRuss Weight u32 offset, u32 size, u32 *written) 5805cd339b3SRuss Weight { 5815cd339b3SRuss Weight struct m10bmc_sec *sec = fwl->dd_handle; 5826052a005SIlpo Järvinen const struct m10bmc_csr_map *csr_map = sec->m10bmc->info->csr_map; 5833e10c805SIlpo Järvinen struct intel_m10bmc *m10bmc = sec->m10bmc; 5843e10c805SIlpo Järvinen u32 blk_size, doorbell; 5855cd339b3SRuss Weight int ret; 5865cd339b3SRuss Weight 5875cd339b3SRuss Weight if (sec->cancel_request) 5885cd339b3SRuss Weight return rsu_cancel(sec); 5895cd339b3SRuss Weight 5903e10c805SIlpo Järvinen ret = m10bmc_sys_read(m10bmc, csr_map->doorbell, &doorbell); 5915cd339b3SRuss Weight if (ret) { 5925cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 5935cd339b3SRuss Weight } else if (rsu_prog(doorbell) != RSU_PROG_READY) { 5945cd339b3SRuss Weight log_error_regs(sec, doorbell); 5955cd339b3SRuss Weight return FW_UPLOAD_ERR_HW_ERROR; 5965cd339b3SRuss Weight } 5975cd339b3SRuss Weight 5983e10c805SIlpo Järvinen WARN_ON_ONCE(WRITE_BLOCK_SIZE % regmap_get_reg_stride(m10bmc->regmap)); 5995cd339b3SRuss Weight blk_size = min_t(u32, WRITE_BLOCK_SIZE, size); 6003e10c805SIlpo Järvinen ret = m10bmc_sec_write(sec, data, offset, blk_size); 6015cd339b3SRuss Weight if (ret) 6025cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 6035cd339b3SRuss Weight 6045cd339b3SRuss Weight *written = blk_size; 6055cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 6065cd339b3SRuss Weight } 6075cd339b3SRuss Weight 6085cd339b3SRuss Weight static enum fw_upload_err m10bmc_sec_poll_complete(struct fw_upload *fwl) 6095cd339b3SRuss Weight { 6105cd339b3SRuss Weight struct m10bmc_sec *sec = fwl->dd_handle; 6115cd339b3SRuss Weight unsigned long poll_timeout; 6125cd339b3SRuss Weight u32 doorbell, result; 6135cd339b3SRuss Weight int ret; 6145cd339b3SRuss Weight 6155cd339b3SRuss Weight if (sec->cancel_request) 6165cd339b3SRuss Weight return rsu_cancel(sec); 6175cd339b3SRuss Weight 618867cae44SIlpo Järvinen m10bmc_fw_state_set(sec->m10bmc, M10BMC_FW_STATE_SEC_UPDATE_PROGRAM); 619867cae44SIlpo Järvinen 6205cd339b3SRuss Weight result = rsu_send_data(sec); 6215cd339b3SRuss Weight if (result != FW_UPLOAD_ERR_NONE) 6225cd339b3SRuss Weight return result; 6235cd339b3SRuss Weight 6245cd339b3SRuss Weight poll_timeout = jiffies + msecs_to_jiffies(RSU_COMPLETE_TIMEOUT_MS); 6255cd339b3SRuss Weight do { 6265cd339b3SRuss Weight msleep(RSU_COMPLETE_INTERVAL_MS); 6275cd339b3SRuss Weight ret = rsu_check_complete(sec, &doorbell); 6285cd339b3SRuss Weight } while (ret == -EAGAIN && !time_after(jiffies, poll_timeout)); 6295cd339b3SRuss Weight 6305cd339b3SRuss Weight if (ret == -EAGAIN) { 6315cd339b3SRuss Weight log_error_regs(sec, doorbell); 6325cd339b3SRuss Weight return FW_UPLOAD_ERR_TIMEOUT; 6335cd339b3SRuss Weight } else if (ret == -EIO) { 6345cd339b3SRuss Weight return FW_UPLOAD_ERR_RW_ERROR; 6355cd339b3SRuss Weight } else if (ret) { 6365cd339b3SRuss Weight log_error_regs(sec, doorbell); 6375cd339b3SRuss Weight return FW_UPLOAD_ERR_HW_ERROR; 6385cd339b3SRuss Weight } 6395cd339b3SRuss Weight 6405cd339b3SRuss Weight return FW_UPLOAD_ERR_NONE; 6415cd339b3SRuss Weight } 6425cd339b3SRuss Weight 6435cd339b3SRuss Weight /* 6445cd339b3SRuss Weight * m10bmc_sec_cancel() may be called asynchronously with an on-going update. 6455cd339b3SRuss Weight * All other functions are called sequentially in a single thread. To avoid 6465cd339b3SRuss Weight * contention on register accesses, m10bmc_sec_cancel() must only update 6475cd339b3SRuss Weight * the cancel_request flag. Other functions will check this flag and handle 6485cd339b3SRuss Weight * the cancel request synchronously. 6495cd339b3SRuss Weight */ 6505cd339b3SRuss Weight static void m10bmc_sec_cancel(struct fw_upload *fwl) 6515cd339b3SRuss Weight { 6525cd339b3SRuss Weight struct m10bmc_sec *sec = fwl->dd_handle; 6535cd339b3SRuss Weight 6545cd339b3SRuss Weight sec->cancel_request = true; 6555cd339b3SRuss Weight } 6565cd339b3SRuss Weight 6575cd339b3SRuss Weight static void m10bmc_sec_cleanup(struct fw_upload *fwl) 6585cd339b3SRuss Weight { 6595cd339b3SRuss Weight struct m10bmc_sec *sec = fwl->dd_handle; 6605cd339b3SRuss Weight 6615cd339b3SRuss Weight (void)rsu_cancel(sec); 662acf63c45SIlpo Järvinen 663867cae44SIlpo Järvinen m10bmc_fw_state_set(sec->m10bmc, M10BMC_FW_STATE_NORMAL); 664867cae44SIlpo Järvinen 665acf63c45SIlpo Järvinen if (sec->m10bmc->flash_bulk_ops) 666acf63c45SIlpo Järvinen sec->m10bmc->flash_bulk_ops->unlock_write(sec->m10bmc); 6675cd339b3SRuss Weight } 6685cd339b3SRuss Weight 6695cd339b3SRuss Weight static const struct fw_upload_ops m10bmc_ops = { 6705cd339b3SRuss Weight .prepare = m10bmc_sec_prepare, 6713e10c805SIlpo Järvinen .write = m10bmc_sec_fw_write, 6725cd339b3SRuss Weight .poll_complete = m10bmc_sec_poll_complete, 6735cd339b3SRuss Weight .cancel = m10bmc_sec_cancel, 6745cd339b3SRuss Weight .cleanup = m10bmc_sec_cleanup, 6755cd339b3SRuss Weight }; 6765cd339b3SRuss Weight 677001a734aSIlpo Järvinen static const struct m10bmc_sec_ops m10sec_n3000_ops = { 678001a734aSIlpo Järvinen .rsu_status = m10bmc_sec_n3000_rsu_status, 679001a734aSIlpo Järvinen }; 680001a734aSIlpo Järvinen 681acf63c45SIlpo Järvinen static const struct m10bmc_sec_ops m10sec_n6000_ops = { 682acf63c45SIlpo Järvinen .rsu_status = m10bmc_sec_n6000_rsu_status, 683acf63c45SIlpo Järvinen }; 684acf63c45SIlpo Järvinen 685bdf86d0eSRuss Weight #define SEC_UPDATE_LEN_MAX 32 686bdf86d0eSRuss Weight static int m10bmc_sec_probe(struct platform_device *pdev) 687bdf86d0eSRuss Weight { 6885cd339b3SRuss Weight char buf[SEC_UPDATE_LEN_MAX]; 689bdf86d0eSRuss Weight struct m10bmc_sec *sec; 6905cd339b3SRuss Weight struct fw_upload *fwl; 6915cd339b3SRuss Weight unsigned int len; 6925cd339b3SRuss Weight int ret; 693bdf86d0eSRuss Weight 694bdf86d0eSRuss Weight sec = devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL); 695bdf86d0eSRuss Weight if (!sec) 696bdf86d0eSRuss Weight return -ENOMEM; 697bdf86d0eSRuss Weight 698bdf86d0eSRuss Weight sec->dev = &pdev->dev; 699bdf86d0eSRuss Weight sec->m10bmc = dev_get_drvdata(pdev->dev.parent); 700001a734aSIlpo Järvinen sec->ops = (struct m10bmc_sec_ops *)platform_get_device_id(pdev)->driver_data; 701bdf86d0eSRuss Weight dev_set_drvdata(&pdev->dev, sec); 702bdf86d0eSRuss Weight 7035cd339b3SRuss Weight ret = xa_alloc(&fw_upload_xa, &sec->fw_name_id, sec, 7045cd339b3SRuss Weight xa_limit_32b, GFP_KERNEL); 7055cd339b3SRuss Weight if (ret) 7065cd339b3SRuss Weight return ret; 7075cd339b3SRuss Weight 7085cd339b3SRuss Weight len = scnprintf(buf, SEC_UPDATE_LEN_MAX, "secure-update%d", 7095cd339b3SRuss Weight sec->fw_name_id); 7105cd339b3SRuss Weight sec->fw_name = kmemdup_nul(buf, len, GFP_KERNEL); 71160ce26d1SIlpo Järvinen if (!sec->fw_name) { 71260ce26d1SIlpo Järvinen ret = -ENOMEM; 71360ce26d1SIlpo Järvinen goto fw_name_fail; 71460ce26d1SIlpo Järvinen } 7155cd339b3SRuss Weight 7165cd339b3SRuss Weight fwl = firmware_upload_register(THIS_MODULE, sec->dev, sec->fw_name, 7175cd339b3SRuss Weight &m10bmc_ops, sec); 7185cd339b3SRuss Weight if (IS_ERR(fwl)) { 7195cd339b3SRuss Weight dev_err(sec->dev, "Firmware Upload driver failed to start\n"); 72060ce26d1SIlpo Järvinen ret = PTR_ERR(fwl); 72160ce26d1SIlpo Järvinen goto fw_uploader_fail; 7225cd339b3SRuss Weight } 7235cd339b3SRuss Weight 7245cd339b3SRuss Weight sec->fwl = fwl; 7255cd339b3SRuss Weight return 0; 72660ce26d1SIlpo Järvinen 72760ce26d1SIlpo Järvinen fw_uploader_fail: 72860ce26d1SIlpo Järvinen kfree(sec->fw_name); 72960ce26d1SIlpo Järvinen fw_name_fail: 73060ce26d1SIlpo Järvinen xa_erase(&fw_upload_xa, sec->fw_name_id); 73160ce26d1SIlpo Järvinen return ret; 7325cd339b3SRuss Weight } 7335cd339b3SRuss Weight 73484a313b7SUwe Kleine-König static void m10bmc_sec_remove(struct platform_device *pdev) 7355cd339b3SRuss Weight { 7365cd339b3SRuss Weight struct m10bmc_sec *sec = dev_get_drvdata(&pdev->dev); 7375cd339b3SRuss Weight 7385cd339b3SRuss Weight firmware_upload_unregister(sec->fwl); 7395cd339b3SRuss Weight kfree(sec->fw_name); 7405cd339b3SRuss Weight xa_erase(&fw_upload_xa, sec->fw_name_id); 741bdf86d0eSRuss Weight } 742bdf86d0eSRuss Weight 743bdf86d0eSRuss Weight static const struct platform_device_id intel_m10bmc_sec_ids[] = { 744bdf86d0eSRuss Weight { 745bdf86d0eSRuss Weight .name = "n3000bmc-sec-update", 746001a734aSIlpo Järvinen .driver_data = (kernel_ulong_t)&m10sec_n3000_ops, 747bdf86d0eSRuss Weight }, 748562d0bf2SRuss Weight { 749562d0bf2SRuss Weight .name = "d5005bmc-sec-update", 750001a734aSIlpo Järvinen .driver_data = (kernel_ulong_t)&m10sec_n3000_ops, 751562d0bf2SRuss Weight }, 752acf63c45SIlpo Järvinen { 753acf63c45SIlpo Järvinen .name = "n6000bmc-sec-update", 754acf63c45SIlpo Järvinen .driver_data = (kernel_ulong_t)&m10sec_n6000_ops, 755bdf86d0eSRuss Weight }, 756bdf86d0eSRuss Weight { } 757bdf86d0eSRuss Weight }; 758bdf86d0eSRuss Weight MODULE_DEVICE_TABLE(platform, intel_m10bmc_sec_ids); 7595cd339b3SRuss Weight 760bdf86d0eSRuss Weight static struct platform_driver intel_m10bmc_sec_driver = { 761bdf86d0eSRuss Weight .probe = m10bmc_sec_probe, 76284a313b7SUwe Kleine-König .remove_new = m10bmc_sec_remove, 763bdf86d0eSRuss Weight .driver = { 764bdf86d0eSRuss Weight .name = "intel-m10bmc-sec-update", 765bdf86d0eSRuss Weight .dev_groups = m10bmc_sec_attr_groups, 766bdf86d0eSRuss Weight }, 767bdf86d0eSRuss Weight .id_table = intel_m10bmc_sec_ids, 768bdf86d0eSRuss Weight }; 769bdf86d0eSRuss Weight module_platform_driver(intel_m10bmc_sec_driver); 770bdf86d0eSRuss Weight 771bdf86d0eSRuss Weight MODULE_AUTHOR("Intel Corporation"); 772bdf86d0eSRuss Weight MODULE_DESCRIPTION("Intel MAX10 BMC Secure Update"); 773bdf86d0eSRuss Weight MODULE_LICENSE("GPL"); 774c452e3bdSIlpo Järvinen MODULE_IMPORT_NS(INTEL_M10_BMC_CORE); 775