// SPDX-License-Identifier: GPL-2.0 /* * NVM helpers * * Copyright (C) 2020, Intel Corporation * Author: Mika Westerberg <[email protected]> */ #include <linux/idr.h> #include <linux/slab.h> #include <linux/vmalloc.h> #include "tb.h" #define NVM_MIN_SIZE … #define NVM_MAX_SIZE … #define NVM_DATA_DWORDS … /* Intel specific NVM offsets */ #define INTEL_NVM_DEVID … #define INTEL_NVM_VERSION … #define INTEL_NVM_CSS … #define INTEL_NVM_FLASH_SIZE … /* ASMedia specific NVM offsets */ #define ASMEDIA_NVM_DATE … #define ASMEDIA_NVM_VERSION … static DEFINE_IDA(nvm_ida); /** * struct tb_nvm_vendor_ops - Vendor specific NVM operations * @read_version: Reads out NVM version from the flash * @validate: Validates the NVM image before update (optional) * @write_headers: Writes headers before the rest of the image (optional) */ struct tb_nvm_vendor_ops { … }; /** * struct tb_nvm_vendor - Vendor to &struct tb_nvm_vendor_ops mapping * @vendor: Vendor ID * @vops: Vendor specific NVM operations * * Maps vendor ID to NVM vendor operations. If there is no mapping then * NVM firmware upgrade is disabled for the device. */ struct tb_nvm_vendor { … }; static int intel_switch_nvm_version(struct tb_nvm *nvm) { … } static int intel_switch_nvm_validate(struct tb_nvm *nvm) { … } static int intel_switch_nvm_write_headers(struct tb_nvm *nvm) { … } static const struct tb_nvm_vendor_ops intel_switch_nvm_ops = …; static int asmedia_switch_nvm_version(struct tb_nvm *nvm) { … } static const struct tb_nvm_vendor_ops asmedia_switch_nvm_ops = …; /* Router vendor NVM support table */ static const struct tb_nvm_vendor switch_nvm_vendors[] = …; static int intel_retimer_nvm_version(struct tb_nvm *nvm) { … } static int intel_retimer_nvm_validate(struct tb_nvm *nvm) { … } static const struct tb_nvm_vendor_ops intel_retimer_nvm_ops = …; /* Retimer vendor NVM support table */ static const struct tb_nvm_vendor retimer_nvm_vendors[] = …; /** * tb_nvm_alloc() - Allocate new NVM structure * @dev: Device owning the NVM * * Allocates new NVM structure with unique @id and returns it. In case * of error returns ERR_PTR(). Specifically returns %-EOPNOTSUPP if the * NVM format of the @dev is not known by the kernel. */ struct tb_nvm *tb_nvm_alloc(struct device *dev) { … } /** * tb_nvm_read_version() - Read and populate NVM version * @nvm: NVM structure * * Uses vendor specific means to read out and fill in the existing * active NVM version. Returns %0 in case of success and negative errno * otherwise. */ int tb_nvm_read_version(struct tb_nvm *nvm) { … } /** * tb_nvm_validate() - Validate new NVM image * @nvm: NVM structure * * Runs vendor specific validation over the new NVM image and if all * checks pass returns %0. As side effect updates @nvm->buf_data_start * and @nvm->buf_data_size fields to match the actual data to be written * to the NVM. * * If the validation does not pass then returns negative errno. */ int tb_nvm_validate(struct tb_nvm *nvm) { … } /** * tb_nvm_write_headers() - Write headers before the rest of the image * @nvm: NVM structure * * If the vendor NVM format requires writing headers before the rest of * the image, this function does that. Can be called even if the device * does not need this. * * Returns %0 in case of success and negative errno otherwise. */ int tb_nvm_write_headers(struct tb_nvm *nvm) { … } /** * tb_nvm_add_active() - Adds active NVMem device to NVM * @nvm: NVM structure * @reg_read: Pointer to the function to read the NVM (passed directly to the * NVMem device) * * Registers new active NVmem device for @nvm. The @reg_read is called * directly from NVMem so it must handle possible concurrent access if * needed. The first parameter passed to @reg_read is @nvm structure. * Returns %0 in success and negative errno otherwise. */ int tb_nvm_add_active(struct tb_nvm *nvm, nvmem_reg_read_t reg_read) { … } /** * tb_nvm_write_buf() - Write data to @nvm buffer * @nvm: NVM structure * @offset: Offset where to write the data * @val: Data buffer to write * @bytes: Number of bytes to write * * Helper function to cache the new NVM image before it is actually * written to the flash. Copies @bytes from @val to @nvm->buf starting * from @offset. */ int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val, size_t bytes) { … } /** * tb_nvm_add_non_active() - Adds non-active NVMem device to NVM * @nvm: NVM structure * @reg_write: Pointer to the function to write the NVM (passed directly * to the NVMem device) * * Registers new non-active NVmem device for @nvm. The @reg_write is called * directly from NVMem so it must handle possible concurrent access if * needed. The first parameter passed to @reg_write is @nvm structure. * The size of the NVMem device is set to %NVM_MAX_SIZE. * * Returns %0 in success and negative errno otherwise. */ int tb_nvm_add_non_active(struct tb_nvm *nvm, nvmem_reg_write_t reg_write) { … } /** * tb_nvm_free() - Release NVM and its resources * @nvm: NVM structure to release * * Releases NVM and the NVMem devices if they were registered. */ void tb_nvm_free(struct tb_nvm *nvm) { … } /** * tb_nvm_read_data() - Read data from NVM * @address: Start address on the flash * @buf: Buffer where the read data is copied * @size: Size of the buffer in bytes * @retries: Number of retries if block read fails * @read_block: Function that reads block from the flash * @read_block_data: Data passsed to @read_block * * This is a generic function that reads data from NVM or NVM like * device. * * Returns %0 on success and negative errno otherwise. */ int tb_nvm_read_data(unsigned int address, void *buf, size_t size, unsigned int retries, read_block_fn read_block, void *read_block_data) { … } /** * tb_nvm_write_data() - Write data to NVM * @address: Start address on the flash * @buf: Buffer where the data is copied from * @size: Size of the buffer in bytes * @retries: Number of retries if the block write fails * @write_block: Function that writes block to the flash * @write_block_data: Data passwd to @write_block * * This is generic function that writes data to NVM or NVM like device. * * Returns %0 on success and negative errno otherwise. */ int tb_nvm_write_data(unsigned int address, const void *buf, size_t size, unsigned int retries, write_block_fn write_block, void *write_block_data) { … } void tb_nvm_exit(void) { … }