From 8bc989277e7643d82f78d075d88a16ca30145ae9 Mon Sep 17 00:00:00 2001 From: Paul Yan Date: Thu, 8 Apr 2021 10:48:23 -0700 Subject: [PATCH] Add 'no MRPC' version of switchtec_open function in library This new function opens a device without issuing MRPC command, so the contents in gas MRPC region is intact after calling this function --- inc/switchtec/switchtec.h | 1 + lib/switchtec.c | 104 +++++++++++++++++++++++++++++--------- 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/inc/switchtec/switchtec.h b/inc/switchtec/switchtec.h index 8223846b..746c3133 100644 --- a/inc/switchtec/switchtec.h +++ b/inc/switchtec/switchtec.h @@ -313,6 +313,7 @@ enum switchtec_event_id { /*********** Platform Functions ***********/ struct switchtec_dev *switchtec_open(const char *device); +struct switchtec_dev *switchtec_open_no_mrpc(const char *device); struct switchtec_dev *switchtec_open_by_path(const char *path); struct switchtec_dev *switchtec_open_by_index(int index); struct switchtec_dev *switchtec_open_by_pci_addr(int domain, int bus, diff --git a/lib/switchtec.c b/lib/switchtec.c index fded3540..cde9b8e8 100644 --- a/lib/switchtec.c +++ b/lib/switchtec.c @@ -147,10 +147,9 @@ static const struct switchtec_device_id switchtec_device_id_tbl[] = { {0}, }; -static int set_gen_variant(struct switchtec_dev * dev) +static int find_gen_variant_from_list(struct switchtec_dev *dev) { const struct switchtec_device_id *id = switchtec_device_id_tbl; - int ret; dev->boot_phase = SWITCHTEC_BOOT_PHASE_FW; dev->gen = SWITCHTEC_GEN_UNKNOWN; @@ -168,6 +167,17 @@ static int set_gen_variant(struct switchtec_dev * dev) id++; } + return -1; +} + +static int set_gen_variant(struct switchtec_dev * dev) +{ + int ret; + + ret = find_gen_variant_from_list(dev); + if (!ret) + return 0; + ret = switchtec_get_device_info(dev, &dev->boot_phase, &dev->gen, NULL); if (ret) return -1; @@ -194,24 +204,7 @@ static int set_local_pax_id(struct switchtec_dev *dev) return 0; } -/** - * @brief Open a Switchtec device by string - * @param[in] device A string representing the device to open - * @return A switchtec_dev structure for use in other library functions - * or NULL if an error occurred. - * - * The string can be specified as: - * * A path to the device (/dev/switchtec0) - * * An index (0, 1, etc) - * * An index with a 'switchtec' prefix (switchtec0) - * * A BDF (bus, device function) string (3:00.1) - * * An I2C device with slave number (/dev/i2c-1@0x20) - * * An I2C adapter number and slave number (0@0x20) - * * An I2C device delimited with a colon (/dev/i2c-1:0x20) - * (must start with a / so that it is distinguishable from a BDF) - * * A UART device (/dev/ttyUSB0) - */ -struct switchtec_dev *switchtec_open(const char *device) +static struct switchtec_dev *open_dev_by_name(const char *device) { int idx; int domain = 0; @@ -269,14 +262,38 @@ struct switchtec_dev *switchtec_open(const char *device) goto found; } - errno = ENODEV; - return NULL; - + ret = NULL; found: - if (!ret) { + if (!ret) errno = ENODEV; + + return ret; +} + +/** + * @brief Open a Switchtec device by string + * @param[in] device A string representing the device to open + * @return A switchtec_dev structure for use in other library functions + * or NULL if an error occurred. + * + * The string can be specified as: + * * A path to the device (/dev/switchtec0) + * * An index (0, 1, etc) + * * An index with a 'switchtec' prefix (switchtec0) + * * A BDF (bus, device function) string (3:00.1) + * * An I2C device with slave number (/dev/i2c-1@0x20) + * * An I2C adapter number and slave number (0@0x20) + * * An I2C device delimited with a colon (/dev/i2c-1:0x20) + * (must start with a / so that it is distinguishable from a BDF) + * * A UART device (/dev/ttyUSB0) + */ +struct switchtec_dev *switchtec_open(const char *device) +{ + struct switchtec_dev *ret; + + ret = open_dev_by_name(device); + if (!ret) return NULL; - } snprintf(ret->name, sizeof(ret->name), "%s", device); @@ -289,6 +306,43 @@ struct switchtec_dev *switchtec_open(const char *device) return ret; } +/** + * @brief Open a Switchtec device by string, without sending MRPC command + * @param[in] device A string representing the device to open + * @return A switchtec_dev structure for use in other library functions + * or NULL if an error occurred. + * + * The string can be specified as: + * * A path to the device (/dev/switchtec0) + * * An index (0, 1, etc) + * * An index with a 'switchtec' prefix (switchtec0) + * * A BDF (bus, device function) string (3:00.1) + * * An I2C device with slave number (/dev/i2c-1@0x20) + * * An I2C adapter number and slave number (0@0x20) + * * An I2C device delimited with a colon (/dev/i2c-1:0x20) + * (must start with a / so that it is distinguishable from a BDF) + * * A UART device (/dev/ttyUSB0) + */ +struct switchtec_dev *switchtec_open_no_mrpc(const char *device) +{ + struct switchtec_dev *ret; + int not_found; + + ret = open_dev_by_name(device); + if (!ret) + return NULL; + + snprintf(ret->name, sizeof(ret->name), "%s", device); + + not_found = find_gen_variant_from_list(ret); + if (not_found) + return NULL; + + ret->local_pax_id = -1; + + return ret; +} + /** * @brief Get the device id of the device * @param[in] dev Switchtec device handle