27 #define SWITCHTEC_LIB_LINUX 29 #include "../switchtec_priv.h" 31 #include "switchtec/pci.h" 32 #include "switchtec/utils.h" 36 #include <linux/switchtec_ioctl.h> 44 #include <sys/ioctl.h> 46 #include <sys/sysmacros.h> 54 static const char *sys_path =
"/sys/class/switchtec";
56 struct switchtec_linux {
57 struct switchtec_dev dev;
61 #define to_switchtec_linux(d) \ 62 ((struct switchtec_linux *) \ 63 ((char *)d - offsetof(struct switchtec_linux, dev))) 65 const char *platform_strerror(
void)
70 static int dev_to_sysfs_path(
struct switchtec_linux *ldev,
const char *suffix,
71 char *buf,
size_t buflen)
76 ret = fstat(ldev->fd, &stat);
81 "/sys/dev/char/%d:%d/%s",
82 major(stat.st_rdev), minor(stat.st_rdev), suffix);
87 static int sysfs_read_str(
const char *path,
char *buf,
size_t buflen)
92 fd = open(path, O_RDONLY);
96 ret = read(fd, buf, buflen);
103 static long long sysfs_read_int(
const char *path,
int base)
108 ret = sysfs_read_str(path, buf,
sizeof(buf));
112 return strtoll(buf, NULL, base);
115 static int check_switchtec_device(
struct switchtec_linux *ldev)
118 char syspath[PATH_MAX];
120 ret = dev_to_sysfs_path(ldev,
"device/switchtec", syspath,
125 ret = access(syspath, F_OK);
132 static int get_partition(
struct switchtec_linux *ldev)
135 char syspath[PATH_MAX];
137 ret = dev_to_sysfs_path(ldev,
"partition", syspath,
142 ldev->dev.partition = sysfs_read_int(syspath, 10);
143 if (ldev->dev.partition < 0)
144 return ldev->dev.partition;
146 ret = dev_to_sysfs_path(ldev,
"partition_count", syspath,
151 ldev->dev.partition_count = sysfs_read_int(syspath, 10);
152 if (ldev->dev.partition_count < 1)
158 static void linux_close(
struct switchtec_dev *dev)
160 struct switchtec_linux *ldev = to_switchtec_linux(dev);
166 static int scan_dev_filter(
const struct dirent *d)
168 if (d->d_name[0] ==
'.')
174 static void get_device_str(
const char *path,
const char *file,
175 char *buf,
size_t buflen)
177 char sysfs_path[PATH_MAX];
180 snprintf(sysfs_path,
sizeof(sysfs_path),
"%s/%s",
183 ret = sysfs_read_str(sysfs_path, buf, buflen);
184 if (ret < 0 || buf[0] == -1)
185 snprintf(buf, buflen,
"unknown");
187 buf[strcspn(buf,
"\n")] = 0;
190 static void get_fw_version(
const char *path,
char *buf,
size_t buflen)
192 char sysfs_path[PATH_MAX];
196 ret = snprintf(sysfs_path,
sizeof(sysfs_path),
"%s/fw_version",
198 if (ret >=
sizeof(sysfs_path))
199 goto unknown_version;
201 fw_ver = sysfs_read_int(sysfs_path, 16);
204 goto unknown_version;
206 version_to_string(fw_ver, buf, buflen);
210 snprintf(buf, buflen,
"unknown");
215 struct dirent **devices;
217 char link_path[PATH_MAX];
218 char pci_path[PATH_MAX] =
"";
221 n = scandir(sys_path, &devices, scan_dev_filter, alphasort);
228 for (i = 0; i < n; i++)
235 for (i = 0; i < n; i++) {
236 snprintf(dl[i].
name,
sizeof(dl[i].name),
237 "%s", devices[i]->d_name);
238 snprintf(dl[i].path,
sizeof(dl[i].path),
239 "/dev/%s", devices[i]->d_name);
241 snprintf(link_path,
sizeof(link_path),
"%s/%s/device",
242 sys_path, devices[i]->d_name);
244 if (readlink(link_path, pci_path,
sizeof(pci_path)) > 0)
245 snprintf(dl[i].
pci_dev,
sizeof(dl[i].pci_dev),
246 "%s", basename(pci_path));
248 snprintf(dl[i].pci_dev,
sizeof(dl[i].pci_dev),
249 "unknown pci device");
251 snprintf(link_path,
sizeof(link_path),
"%s/%s",
252 sys_path, devices[i]->d_name);
254 get_device_str(link_path,
"product_id", dl[i].
product_id,
255 sizeof(dl[i].product_id));
256 get_device_str(link_path,
"product_revision",
259 sizeof(dl[i].fw_version));
268 static int linux_get_device_id(
struct switchtec_dev *dev)
270 char link_path[PATH_MAX];
272 snprintf(link_path,
sizeof(link_path),
"%s/%s/device/device",
273 sys_path, basename(dev->name));
275 return sysfs_read_int(link_path, 16);
278 static int linux_get_fw_version(
struct switchtec_dev *dev,
char *buf,
283 char syspath[PATH_MAX];
284 struct switchtec_linux *ldev = to_switchtec_linux(dev);
286 ret = dev_to_sysfs_path(ldev,
"fw_version", syspath,
sizeof(syspath));
290 version = sysfs_read_int(syspath, 16);
294 version_to_string(version, buf, buflen);
299 static int submit_cmd(
struct switchtec_linux *ldev, uint32_t cmd,
300 const void *payload,
size_t payload_len)
303 size_t bufsize = payload_len +
sizeof(cmd);
307 memcpy(buf, &cmd,
sizeof(cmd));
308 memcpy(&buf[
sizeof(cmd)], payload, payload_len);
310 ret = write(ldev->fd, buf, bufsize);
315 if (ret != bufsize) {
323 static int read_resp(
struct switchtec_linux *ldev,
void *resp,
327 size_t bufsize =
sizeof(uint32_t) + resp_len;
330 ret = read(ldev->fd, buf, bufsize);
335 if (ret != bufsize) {
340 memcpy(&ret, buf,
sizeof(ret));
347 memcpy(resp, &buf[
sizeof(ret)], resp_len);
352 static int linux_cmd(
struct switchtec_dev *dev, uint32_t cmd,
353 const void *payload,
size_t payload_len,
void *resp,
357 struct switchtec_linux *ldev = to_switchtec_linux(dev);
360 ret = submit_cmd(ldev, cmd, payload, payload_len);
361 if (errno == EBADE) {
362 read_resp(ldev, NULL, 0);
370 return read_resp(ldev, resp, resp_len);
373 static int get_class_devices(
const char *searchpath,
378 char syspath[PATH_MAX];
381 const size_t MAX_LEN = 256;
383 snprintf(syspath,
sizeof(syspath),
"%s*/*/device", searchpath);
384 glob(syspath, 0, NULL, &paths);
386 for (i = 0; i < paths.gl_pathc; i++) {
387 char *p = paths.gl_pathv[i];
389 len = readlink(p, syspath,
sizeof(syspath));
401 ", %s", basename(p));
411 static void get_port_bdf(
const char *searchpath,
int port,
414 char syspath[PATH_MAX];
418 ret = snprintf(syspath,
sizeof(syspath),
"%s/*:*:%02x.*",
420 if (ret >=
sizeof(syspath))
423 glob(syspath, 0, NULL, &paths);
425 if (paths.gl_pathc == 1)
426 status->
pci_bdf = strdup(basename(paths.gl_pathv[0]));
434 char rpath[PATH_MAX];
435 int domain, bus, dev, fn;
443 snprintf(path,
sizeof(path),
"/sys/bus/pci/devices/%s",
446 if (!realpath(path, rpath))
449 subpath = strtok(rpath,
"/");
451 ret = sscanf(subpath,
"%x:%x:%x.%x", &domain, &bus, &dev, &fn);
454 ret = snprintf(path + ptr,
sizeof(path) - ptr,
455 "%04x:%02x:%02x:%x/",
456 domain, bus, dev, fn);
458 ret = snprintf(path + ptr,
sizeof(path) - ptr,
459 "%02x.%x/", dev, fn);
461 if (ret <= 0 || ret >=
sizeof(path) - ptr)
466 subpath = strtok(NULL,
"/");
478 char syspath[PATH_MAX];
484 snprintf(syspath,
sizeof(syspath),
"/sys/bus/pci/devices/%s/*:*:*/",
487 glob(syspath, 0, NULL, &paths);
489 for (i = 0; i < paths.gl_pathc; i++) {
490 char *p = paths.gl_pathv[i];
492 snprintf(syspath,
sizeof(syspath),
"%s/vendor", p);
493 status->
vendor_id = sysfs_read_int(syspath, 16);
497 snprintf(syspath,
sizeof(syspath),
"%s/device", p);
498 status->
device_id = sysfs_read_int(syspath, 16);
502 if (get_class_devices(p, status)) {
505 status->
pci_dev = strdup(basename(p));
509 status->
pci_dev = strdup(basename(p));
519 char syspath[PATH_MAX];
521 int pos = PCI_EXT_CAP_OFFSET;
524 snprintf(syspath,
sizeof(syspath),
"/sys/bus/pci/devices/%s/config",
527 fd = open(syspath, O_RDONLY);
532 ret = pread(fd, &extcap,
sizeof(extcap), pos);
533 if (ret !=
sizeof(extcap) || !extcap)
536 if (PCI_EXT_CAP_ID(extcap) == PCI_EXT_CAP_ID_ACS)
539 pos = PCI_EXT_CAP_NEXT(extcap);
540 if (pos < PCI_EXT_CAP_OFFSET)
544 ret = pread(fd, &acs,
sizeof(acs), pos + PCI_ACS_CTRL);
545 if (ret !=
sizeof(acs))
554 static int linux_get_devices(
struct switchtec_dev *dev,
561 char syspath[PATH_MAX];
562 char searchpath[PATH_MAX];
563 struct switchtec_linux *ldev = to_switchtec_linux(dev);
565 ret = dev_to_sysfs_path(ldev,
"device", syspath,
570 if (!realpath(syspath, searchpath)) {
576 searchpath[strlen(searchpath) - 1] =
'0';
580 for (i = 0; i < ports; i++) {
581 if (status[i].port.partition != local_part)
584 if (status[i].port.upstream) {
585 status->
pci_bdf = strdup(basename(searchpath));
586 get_port_bdf_path(&status[i]);
590 get_port_bdf(searchpath, status[i].port.log_id - 1, &status[i]);
591 get_port_bdf_path(&status[i]);
592 get_port_info(&status[i]);
593 get_config_info(&status[i]);
599 static int linux_pff_to_port(
struct switchtec_dev *dev,
int pff,
600 int *partition,
int *port)
603 struct switchtec_ioctl_pff_port p;
604 struct switchtec_linux *ldev = to_switchtec_linux(dev);
607 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_PFF_TO_PORT, &p);
612 *partition = p.partition;
619 static int linux_port_to_pff(
struct switchtec_dev *dev,
int partition,
623 struct switchtec_ioctl_pff_port p;
624 struct switchtec_linux *ldev = to_switchtec_linux(dev);
627 p.partition = partition;
629 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_PORT_TO_PFF, &p);
640 #define __force __attribute__((force)) 645 static ssize_t resource_size(
struct switchtec_linux *ldev,
const char *fname)
647 char respath[PATH_MAX];
651 ret = dev_to_sysfs_path(ldev, fname, respath,
658 fd = open(respath, O_RDONLY);
662 ret = fstat(fd, &stat);
672 static int mmap_resource(
struct switchtec_linux *ldev,
const char *fname,
673 void *addr,
size_t offset,
size_t size,
int writeable)
675 char respath[PATH_MAX];
679 ret = dev_to_sysfs_path(ldev, fname, respath,
686 fd = open(respath, writeable ? O_RDWR : O_RDONLY);
690 map = mmap(addr, size, (writeable ? PROT_WRITE : 0) | PROT_READ,
691 MAP_SHARED | MAP_FIXED, fd, offset);
692 if (map == MAP_FAILED)
706 static gasptr_t linux_gas_map(
struct switchtec_dev *dev,
int writeable,
712 struct switchtec_linux *ldev = to_switchtec_linux(dev);
714 msize = resource_size(ldev,
"device/resource0");
716 return SWITCHTEC_MAP_FAILED;
721 map = mmap(NULL, msize, PROT_NONE,
722 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
723 if (map == MAP_FAILED)
724 return SWITCHTEC_MAP_FAILED;
726 ret = mmap_resource(ldev,
"device/resource0_wc", map, 0,
727 SWITCHTEC_GAS_TOP_CFG_OFFSET, writeable);
729 ret = mmap_resource(ldev,
"device/resource0", map, 0,
730 SWITCHTEC_GAS_TOP_CFG_OFFSET,
736 ret = mmap_resource(ldev,
"device/resource0",
737 map + SWITCHTEC_GAS_TOP_CFG_OFFSET,
738 SWITCHTEC_GAS_TOP_CFG_OFFSET,
739 msize - SWITCHTEC_GAS_TOP_CFG_OFFSET,
747 dev->gas_map = (
gasptr_t __force)map;
748 dev->gas_map_size = msize;
750 ret = gasop_access_check(dev);
759 return SWITCHTEC_MAP_FAILED;
762 static void linux_gas_unmap(
struct switchtec_dev *dev,
gasptr_t map)
764 munmap((
void __force *)map, dev->gas_map_size);
767 static int linux_flash_part(
struct switchtec_dev *dev,
769 enum switchtec_fw_image_part_id_gen3 part)
771 struct switchtec_linux *ldev = to_switchtec_linux(dev);
772 struct switchtec_ioctl_flash_part_info ioctl_info = {0};
776 case SWITCHTEC_FW_PART_ID_G3_IMG0:
777 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_IMG0;
779 case SWITCHTEC_FW_PART_ID_G3_IMG1:
780 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_IMG1;
782 case SWITCHTEC_FW_PART_ID_G3_DAT0:
783 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_CFG0;
785 case SWITCHTEC_FW_PART_ID_G3_DAT1:
786 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_CFG1;
788 case SWITCHTEC_FW_PART_ID_G3_NVLOG:
789 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_NVLOG;
795 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_FLASH_PART_INFO, &ioctl_info);
801 info->active =
false;
802 info->running =
false;
804 if (ioctl_info.active & SWITCHTEC_IOCTL_PART_ACTIVE)
807 if (ioctl_info.active & SWITCHTEC_IOCTL_PART_RUNNING)
808 info->running =
true;
814 struct switchtec_ioctl_event_summary *src,
819 dst->
global = src->global;
823 for (i = 0; i < SWITCHTEC_MAX_PARTS; i++)
824 dst->
part[i] = src->part[i];
826 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR && i < size; i++)
827 dst->
pff[i] = src->pff[i];
830 #define EV(t, n)[SWITCHTEC_ ## t ## _EVT_ ## n] = \ 831 SWITCHTEC_IOCTL_EVENT_ ## n 833 static const int event_map[] = {
834 EV(GLOBAL, STACK_ERROR),
835 EV(GLOBAL, PPU_ERROR),
836 EV(GLOBAL, ISP_ERROR),
837 EV(GLOBAL, SYS_RESET),
840 EV(GLOBAL, FW_NON_FATAL),
841 EV(GLOBAL, FW_FATAL),
842 EV(GLOBAL, TWI_MRPC_COMP),
843 EV(GLOBAL, TWI_MRPC_COMP_ASYNC),
844 EV(GLOBAL, CLI_MRPC_COMP),
845 EV(GLOBAL, CLI_MRPC_COMP_ASYNC),
846 EV(GLOBAL, GPIO_INT),
848 EV(PART, PART_RESET),
850 EV(PART, MRPC_COMP_ASYNC),
851 EV(PART, DYN_PART_BIND_COMP),
861 EV(PFF, TLP_THROTTLING),
862 EV(PFF, FORCE_SPEED),
863 EV(PFF, CREDIT_TIMEOUT),
867 static int linux_event_summary(
struct switchtec_dev *dev,
871 struct switchtec_ioctl_event_summary isum;
872 struct switchtec_ioctl_event_summary_legacy isum_legacy;
873 struct switchtec_linux *ldev = to_switchtec_linux(dev);
878 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY, &isum);
880 event_summary_copy(sum, &isum, ARRAY_SIZE(isum.pff));
884 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY, &isum);
888 event_summary_copy(sum, &isum, ARRAY_SIZE(isum_legacy.pff));
893 static int linux_event_ctl(
struct switchtec_dev *dev,
895 int index,
int flags,
899 struct switchtec_ioctl_event_ctl ctl;
900 struct switchtec_linux *ldev = to_switchtec_linux(dev);
902 if (e >= SWITCHTEC_MAX_EVENTS)
905 ctl.event_id = event_map[e];
908 if (flags & SWITCHTEC_EVT_FLAG_CLEAR)
909 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR;
910 if (flags & SWITCHTEC_EVT_FLAG_EN_POLL)
911 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
912 if (flags & SWITCHTEC_EVT_FLAG_EN_LOG)
913 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
914 if (flags & SWITCHTEC_EVT_FLAG_EN_CLI)
915 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
916 if (flags & SWITCHTEC_EVT_FLAG_EN_FATAL)
917 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
918 if (flags & SWITCHTEC_EVT_FLAG_DIS_POLL)
919 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL;
920 if (flags & SWITCHTEC_EVT_FLAG_DIS_LOG)
921 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG;
922 if (flags & SWITCHTEC_EVT_FLAG_DIS_CLI)
923 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI;
924 if (flags & SWITCHTEC_EVT_FLAG_DIS_FATAL)
925 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL;
928 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_CTL, &ctl);
934 memcpy(data, ctl.data,
sizeof(ctl.data));
939 static int linux_event_wait(
struct switchtec_dev *dev,
int timeout_ms)
942 struct switchtec_linux *ldev = to_switchtec_linux(dev);
943 struct pollfd fds = {
948 ret = poll(&fds, 1, timeout_ms);
952 if (fds.revents & POLLERR) {
957 if (fds.revents & POLLPRI)
963 static const struct switchtec_ops linux_ops = {
964 .close = linux_close,
965 .get_device_id = linux_get_device_id,
966 .get_fw_version = linux_get_fw_version,
968 .get_devices = linux_get_devices,
969 .pff_to_port = linux_pff_to_port,
970 .port_to_pff = linux_port_to_pff,
971 .gas_map = linux_gas_map,
972 .gas_unmap = linux_gas_unmap,
973 .flash_part = linux_flash_part,
974 .event_summary = linux_event_summary,
975 .event_ctl = linux_event_ctl,
976 .event_wait = linux_event_wait,
978 .gas_read8 = mmap_gas_read8,
979 .gas_read16 = mmap_gas_read16,
980 .gas_read32 = mmap_gas_read32,
981 .gas_read64 = mmap_gas_read64,
982 .gas_write8 = mmap_gas_write8,
983 .gas_write16 = mmap_gas_write16,
984 .gas_write32 = mmap_gas_write32,
985 .gas_write32_no_retry = mmap_gas_write32,
986 .gas_write64 = mmap_gas_write64,
987 .memcpy_to_gas = mmap_memcpy_to_gas,
988 .memcpy_from_gas = mmap_memcpy_from_gas,
989 .write_from_gas = mmap_write_from_gas,
994 struct switchtec_linux *ldev;
997 fd = open(path, O_RDWR | O_CLOEXEC);
1006 ldev = malloc(
sizeof(*ldev));
1012 if (check_switchtec_device(ldev))
1013 goto err_close_free;
1015 if (get_partition(ldev))
1016 goto err_close_free;
1018 ldev->dev.ops = &linux_ops;
1030 char path[PATH_MAX];
1031 struct switchtec_dev *dev;
1033 snprintf(path,
sizeof(path),
"/dev/switchtec%d", index);
1037 if (errno == ENOENT)
1044 int device,
int func)
1046 char path[PATH_MAX];
1047 struct switchtec_dev *dev;
1048 struct dirent *dirent;
1051 snprintf(path,
sizeof(path),
1052 "/sys/bus/pci/devices/%04x:%02x:%02x.%x/switchtec",
1053 domain, bus, device, func);
1055 dir = opendir(path);
1059 while ((dirent = readdir(dir))) {
1060 if (dirent->d_name[0] !=
'.')
1074 snprintf(path,
sizeof(path),
"/dev/%s", dirent->d_name);
1075 printf(
"%s\n", path);
char product_rev[8]
Product revision.
char * pci_dev
PCI BDF of the device on the port.
unsigned int acs_ctrl
ACS Setting of the Port.
size_t part_addr
Address of the partition.
Gas Operations for platforms that the gas is mapped into the address space.
switchtec_event_id
Enumeration of all possible events.
Information about a firmware image or partition.
char * pci_bdf_path
PCI BDF path of the port.
__gas struct switchtec_gas * gasptr_t
Shortform for a pointer to the GAS register space.
char product_id[32]
Product ID.
int switchtec_list(struct switchtec_device_info **devlist)
List all the switchtec devices in the system.
uint64_t part_bitmap
Bitmap of partitions with active events.
_PURE int switchtec_partition(struct switchtec_dev *dev)
Get the partiton number of the device that was opened.
char * class_devices
Comma seperated list of classes.
uint64_t global
Bitmap of global events.
unsigned pff[SWITCHTEC_MAX_PFF_CSR]
Bitmap of events in each port function.
unsigned part[SWITCHTEC_MAX_PARTS]
Bitmap of events in each partition.
char pci_dev[256]
PCI BDF string.
struct switchtec_dev * switchtec_open_uart(int fd)
Open a switchtec device behind a uart device.
struct switchtec_dev * switchtec_open_by_path(const char *path)
Open a switchtec device by path.
char fw_version[32]
Firmware version.
size_t part_len
Length of the partition.
char name[256]
Device name, eg. switchtec0.
struct switchtec_dev * switchtec_open(const char *device)
Open a Switchtec device by string.
char * pci_bdf
PCI BDF of the port.
Represents a Switchtec device in the switchtec_list() function.
struct switchtec_dev * switchtec_open_by_index(int index)
Open a switchtec device by index.
struct switchtec_dev * switchtec_open_by_pci_addr(int domain, int bus, int device, int func)
Open a switchtec device by PCI address (BDF)
unsigned local_part
Bitmap of events in the local partition.