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)
246 "%s", basename(pci_path));
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,
256 get_device_str(link_path,
"product_revision",
268 static int linux_get_device_id(
struct switchtec_dev *dev)
271 char link_path[PATH_MAX];
272 struct switchtec_linux *ldev = to_switchtec_linux(dev);
274 ret = dev_to_sysfs_path(ldev,
"device/device", link_path,
279 return sysfs_read_int(link_path, 16);
282 static int linux_get_fw_version(
struct switchtec_dev *dev,
char *buf,
287 char syspath[PATH_MAX];
288 struct switchtec_linux *ldev = to_switchtec_linux(dev);
290 ret = dev_to_sysfs_path(ldev,
"fw_version", syspath,
sizeof(syspath));
294 version = sysfs_read_int(syspath, 16);
298 version_to_string(version, buf, buflen);
303 static int submit_cmd(
struct switchtec_linux *ldev, uint32_t cmd,
304 const void *payload,
size_t payload_len)
307 size_t bufsize = payload_len +
sizeof(cmd);
311 memcpy(buf, &cmd,
sizeof(cmd));
312 memcpy(&buf[
sizeof(cmd)], payload, payload_len);
314 ret = write(ldev->fd, buf, bufsize);
319 if (ret != bufsize) {
327 static int read_resp(
struct switchtec_linux *ldev,
void *resp,
331 size_t bufsize =
sizeof(uint32_t) + resp_len;
334 ret = read(ldev->fd, buf, bufsize);
339 if (ret != bufsize) {
344 memcpy(&ret, buf,
sizeof(ret));
351 memcpy(resp, &buf[
sizeof(ret)], resp_len);
356 static int linux_cmd(
struct switchtec_dev *dev, uint32_t cmd,
357 const void *payload,
size_t payload_len,
void *resp,
361 struct switchtec_linux *ldev = to_switchtec_linux(dev);
364 ret = submit_cmd(ldev, cmd, payload, payload_len);
365 if (errno == EBADE) {
366 read_resp(ldev, NULL, 0);
374 return read_resp(ldev, resp, resp_len);
377 static int get_class_devices(
const char *searchpath,
382 char syspath[PATH_MAX];
385 const size_t MAX_LEN = 256;
387 snprintf(syspath,
sizeof(syspath),
"%s*/*/device", searchpath);
388 glob(syspath, 0, NULL, &paths);
390 for (i = 0; i < paths.gl_pathc; i++) {
391 char *p = paths.gl_pathv[i];
393 len = readlink(p, syspath,
sizeof(syspath));
405 ", %s", basename(p));
415 static void get_port_bdf(
const char *searchpath,
int port,
418 char syspath[PATH_MAX];
422 ret = snprintf(syspath,
sizeof(syspath),
"%s/*:*:%02x.*",
424 if (ret >=
sizeof(syspath))
427 glob(syspath, 0, NULL, &paths);
429 if (paths.gl_pathc == 1)
430 status->
pci_bdf = strdup(basename(paths.gl_pathv[0]));
438 char rpath[PATH_MAX];
439 int domain, bus, dev, fn;
447 snprintf(path,
sizeof(path),
"/sys/bus/pci/devices/%s",
450 if (!realpath(path, rpath))
453 subpath = strtok(rpath,
"/");
455 ret = sscanf(subpath,
"%x:%x:%x.%x", &domain, &bus, &dev, &fn);
458 ret = snprintf(path + ptr,
sizeof(path) - ptr,
459 "%04x:%02x:%02x:%x/",
460 domain, bus, dev, fn);
462 ret = snprintf(path + ptr,
sizeof(path) - ptr,
463 "%02x.%x/", dev, fn);
465 if (ret <= 0 || ret >=
sizeof(path) - ptr)
470 subpath = strtok(NULL,
"/");
482 char syspath[PATH_MAX];
488 snprintf(syspath,
sizeof(syspath),
"/sys/bus/pci/devices/%s/*:*:*/",
491 glob(syspath, 0, NULL, &paths);
493 for (i = 0; i < paths.gl_pathc; i++) {
494 char *p = paths.gl_pathv[i];
496 snprintf(syspath,
sizeof(syspath),
"%s/vendor", p);
497 status->
vendor_id = sysfs_read_int(syspath, 16);
501 snprintf(syspath,
sizeof(syspath),
"%s/device", p);
502 status->
device_id = sysfs_read_int(syspath, 16);
506 if (get_class_devices(p, status)) {
509 status->
pci_dev = strdup(basename(p));
513 status->
pci_dev = strdup(basename(p));
523 char syspath[PATH_MAX];
525 int pos = PCI_EXT_CAP_OFFSET;
528 snprintf(syspath,
sizeof(syspath),
"/sys/bus/pci/devices/%s/config",
531 fd = open(syspath, O_RDONLY);
536 ret = pread(fd, &extcap,
sizeof(extcap), pos);
537 if (ret !=
sizeof(extcap) || !extcap)
540 if (PCI_EXT_CAP_ID(extcap) == PCI_EXT_CAP_ID_ACS)
543 pos = PCI_EXT_CAP_NEXT(extcap);
544 if (pos < PCI_EXT_CAP_OFFSET)
548 ret = pread(fd, &acs,
sizeof(acs), pos + PCI_ACS_CTRL);
549 if (ret !=
sizeof(acs))
558 static int linux_get_devices(
struct switchtec_dev *dev,
565 char syspath[PATH_MAX];
566 char searchpath[PATH_MAX];
567 struct switchtec_linux *ldev = to_switchtec_linux(dev);
569 ret = dev_to_sysfs_path(ldev,
"device", syspath,
574 if (!realpath(syspath, searchpath)) {
580 searchpath[strlen(searchpath) - 1] =
'0';
584 for (i = 0; i < ports; i++) {
585 if (status[i].port.partition != local_part)
588 if (status[i].port.upstream) {
589 status[i].
pci_bdf = strdup(basename(searchpath));
590 get_port_bdf_path(&status[i]);
594 get_port_bdf(searchpath, status[i].port.log_id - 1, &status[i]);
595 get_port_bdf_path(&status[i]);
596 get_port_info(&status[i]);
597 get_config_info(&status[i]);
603 static int linux_pff_to_port(
struct switchtec_dev *dev,
int pff,
604 int *partition,
int *port)
607 struct switchtec_ioctl_pff_port p;
608 struct switchtec_linux *ldev = to_switchtec_linux(dev);
611 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_PFF_TO_PORT, &p);
616 *partition = p.partition;
623 static int linux_port_to_pff(
struct switchtec_dev *dev,
int partition,
627 struct switchtec_ioctl_pff_port p;
628 struct switchtec_linux *ldev = to_switchtec_linux(dev);
631 p.partition = partition;
633 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_PORT_TO_PFF, &p);
644 #define __force __attribute__((force))
649 static ssize_t resource_size(
struct switchtec_linux *ldev,
const char *fname)
651 char respath[PATH_MAX];
655 ret = dev_to_sysfs_path(ldev, fname, respath,
662 fd = open(respath, O_RDONLY);
666 ret = fstat(fd, &stat);
676 static int mmap_resource(
struct switchtec_linux *ldev,
const char *fname,
677 void *addr,
size_t offset,
size_t size,
int writeable)
679 char respath[PATH_MAX];
683 ret = dev_to_sysfs_path(ldev, fname, respath,
690 fd = open(respath, writeable ? O_RDWR : O_RDONLY);
694 map = mmap(addr, size, (writeable ? PROT_WRITE : 0) | PROT_READ,
695 MAP_SHARED | MAP_FIXED, fd, offset);
696 if (map == MAP_FAILED)
710 static gasptr_t linux_gas_map(
struct switchtec_dev *dev,
int writeable,
716 struct switchtec_linux *ldev = to_switchtec_linux(dev);
718 msize = resource_size(ldev,
"device/resource0");
720 return SWITCHTEC_MAP_FAILED;
725 map = mmap(NULL, msize, PROT_NONE,
726 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
727 if (map == MAP_FAILED)
728 return SWITCHTEC_MAP_FAILED;
730 ret = mmap_resource(ldev,
"device/resource0_wc", map, 0,
731 SWITCHTEC_GAS_TOP_CFG_OFFSET, writeable);
733 ret = mmap_resource(ldev,
"device/resource0", map, 0,
734 SWITCHTEC_GAS_TOP_CFG_OFFSET,
740 ret = mmap_resource(ldev,
"device/resource0",
741 map + SWITCHTEC_GAS_TOP_CFG_OFFSET,
742 SWITCHTEC_GAS_TOP_CFG_OFFSET,
743 msize - SWITCHTEC_GAS_TOP_CFG_OFFSET,
751 dev->gas_map = (
gasptr_t __force)map;
752 dev->gas_map_size = msize;
754 ret = gasop_access_check(dev);
763 return SWITCHTEC_MAP_FAILED;
766 static void linux_gas_unmap(
struct switchtec_dev *dev,
gasptr_t map)
768 munmap((
void __force *)map, dev->gas_map_size);
771 static int linux_flash_part(
struct switchtec_dev *dev,
773 enum switchtec_fw_image_part_id_gen3 part)
775 struct switchtec_linux *ldev = to_switchtec_linux(dev);
776 struct switchtec_ioctl_flash_part_info ioctl_info = {0};
780 case SWITCHTEC_FW_PART_ID_G3_IMG0:
781 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_IMG0;
783 case SWITCHTEC_FW_PART_ID_G3_IMG1:
784 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_IMG1;
786 case SWITCHTEC_FW_PART_ID_G3_DAT0:
787 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_CFG0;
789 case SWITCHTEC_FW_PART_ID_G3_DAT1:
790 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_CFG1;
792 case SWITCHTEC_FW_PART_ID_G3_NVLOG:
793 ioctl_info.flash_partition = SWITCHTEC_IOCTL_PART_NVLOG;
799 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_FLASH_PART_INFO, &ioctl_info);
805 info->active =
false;
806 info->running =
false;
808 if (ioctl_info.active & SWITCHTEC_IOCTL_PART_ACTIVE)
811 if (ioctl_info.active & SWITCHTEC_IOCTL_PART_RUNNING)
812 info->running =
true;
818 struct switchtec_ioctl_event_summary *src,
823 dst->
global = src->global;
827 for (i = 0; i < SWITCHTEC_MAX_PARTS; i++)
828 dst->
part[i] = src->part[i];
830 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR && i < size; i++)
831 dst->
pff[i] = src->pff[i];
834 #define EV(t, n)[SWITCHTEC_ ## t ## _EVT_ ## n] = \
835 SWITCHTEC_IOCTL_EVENT_ ## n
837 static const int event_map[] = {
838 EV(GLOBAL, STACK_ERROR),
839 EV(GLOBAL, PPU_ERROR),
840 EV(GLOBAL, ISP_ERROR),
841 EV(GLOBAL, SYS_RESET),
844 EV(GLOBAL, FW_NON_FATAL),
845 EV(GLOBAL, FW_FATAL),
846 EV(GLOBAL, TWI_MRPC_COMP),
847 EV(GLOBAL, TWI_MRPC_COMP_ASYNC),
848 EV(GLOBAL, CLI_MRPC_COMP),
849 EV(GLOBAL, CLI_MRPC_COMP_ASYNC),
850 EV(GLOBAL, GPIO_INT),
852 EV(PART, PART_RESET),
854 EV(PART, MRPC_COMP_ASYNC),
855 EV(PART, DYN_PART_BIND_COMP),
865 EV(PFF, TLP_THROTTLING),
866 EV(PFF, FORCE_SPEED),
867 EV(PFF, CREDIT_TIMEOUT),
871 static int linux_event_summary(
struct switchtec_dev *dev,
875 struct switchtec_ioctl_event_summary isum;
876 struct switchtec_ioctl_event_summary_legacy isum_legacy;
877 struct switchtec_linux *ldev = to_switchtec_linux(dev);
882 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY, &isum);
884 event_summary_copy(sum, &isum, ARRAY_SIZE(isum.pff));
888 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY, &isum);
892 event_summary_copy(sum, &isum, ARRAY_SIZE(isum_legacy.pff));
897 static int linux_event_ctl(
struct switchtec_dev *dev,
899 int index,
int flags,
903 struct switchtec_ioctl_event_ctl ctl;
904 struct switchtec_linux *ldev = to_switchtec_linux(dev);
906 if (e >= SWITCHTEC_MAX_EVENTS)
909 ctl.event_id = event_map[e];
912 if (flags & SWITCHTEC_EVT_FLAG_CLEAR)
913 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR;
914 if (flags & SWITCHTEC_EVT_FLAG_EN_POLL)
915 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
916 if (flags & SWITCHTEC_EVT_FLAG_EN_LOG)
917 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
918 if (flags & SWITCHTEC_EVT_FLAG_EN_CLI)
919 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
920 if (flags & SWITCHTEC_EVT_FLAG_EN_FATAL)
921 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
922 if (flags & SWITCHTEC_EVT_FLAG_DIS_POLL)
923 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL;
924 if (flags & SWITCHTEC_EVT_FLAG_DIS_LOG)
925 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG;
926 if (flags & SWITCHTEC_EVT_FLAG_DIS_CLI)
927 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI;
928 if (flags & SWITCHTEC_EVT_FLAG_DIS_FATAL)
929 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL;
932 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_CTL, &ctl);
938 memcpy(data, ctl.data,
sizeof(ctl.data));
943 static int linux_event_wait(
struct switchtec_dev *dev,
int timeout_ms)
946 struct switchtec_linux *ldev = to_switchtec_linux(dev);
947 struct pollfd fds = {
952 ret = poll(&fds, 1, timeout_ms);
956 if (fds.revents & POLLERR) {
961 if (fds.revents & POLLPRI)
967 static const struct switchtec_ops linux_ops = {
968 .close = linux_close,
969 .get_device_id = linux_get_device_id,
970 .get_fw_version = linux_get_fw_version,
972 .get_devices = linux_get_devices,
973 .pff_to_port = linux_pff_to_port,
974 .port_to_pff = linux_port_to_pff,
975 .gas_map = linux_gas_map,
976 .gas_unmap = linux_gas_unmap,
977 .flash_part = linux_flash_part,
978 .event_summary = linux_event_summary,
979 .event_ctl = linux_event_ctl,
980 .event_wait = linux_event_wait,
982 .gas_read8 = mmap_gas_read8,
983 .gas_read16 = mmap_gas_read16,
984 .gas_read32 = mmap_gas_read32,
985 .gas_read64 = mmap_gas_read64,
986 .gas_write8 = mmap_gas_write8,
987 .gas_write16 = mmap_gas_write16,
988 .gas_write32 = mmap_gas_write32,
989 .gas_write32_no_retry = mmap_gas_write32,
990 .gas_write64 = mmap_gas_write64,
991 .memcpy_to_gas = mmap_memcpy_to_gas,
992 .memcpy_from_gas = mmap_memcpy_from_gas,
993 .write_from_gas = mmap_write_from_gas,
998 struct switchtec_linux *ldev;
1001 fd = open(path, O_RDWR | O_CLOEXEC);
1010 ldev = malloc(
sizeof(*ldev));
1016 if (check_switchtec_device(ldev))
1017 goto err_close_free;
1019 if (get_partition(ldev))
1020 goto err_close_free;
1022 ldev->dev.ops = &linux_ops;
1034 char path[PATH_MAX];
1035 struct switchtec_dev *dev;
1037 snprintf(path,
sizeof(path),
"/dev/switchtec%d", index);
1041 if (errno == ENOENT)
1048 int device,
int func)
1050 char path[PATH_MAX];
1051 struct switchtec_dev *dev;
1052 struct dirent *dirent;
1055 snprintf(path,
sizeof(path),
1056 "/sys/bus/pci/devices/%04x:%02x:%02x.%x/switchtec",
1057 domain, bus, device, func);
1059 dir = opendir(path);
1063 while ((dirent = readdir(dir))) {
1064 if (dirent->d_name[0] !=
'.')
1078 snprintf(path,
sizeof(path),
"/dev/%s", dirent->d_name);
1079 printf(
"%s\n", path);
struct switchtec_dev * switchtec_open_uart(int fd)
Open a switchtec device behind a uart device.
int switchtec_list(struct switchtec_device_info **devlist)
List all the switchtec devices in the system.
struct switchtec_dev * switchtec_open_by_path(const char *path)
Open a switchtec device by path.
struct switchtec_dev * switchtec_open(const char *device)
Open a Switchtec device by string.
struct switchtec_dev * switchtec_open_by_index(int index)
Open a switchtec device by index.
_PURE int switchtec_partition(struct switchtec_dev *dev)
Get the partiton number of the device that was opened.
struct switchtec_dev * switchtec_open_by_pci_addr(int domain, int bus, int device, int func)
Open a switchtec device by PCI address (BDF)
Gas Operations for platforms that the gas is mapped into the address space.
Represents a Switchtec device in the switchtec_list() function.
char fw_version[32]
Firmware version.
char pci_dev[256]
PCI BDF string.
char path[PATH_MAX]
Path to the device.
char name[256]
Device name, eg. switchtec0.
char product_id[32]
Product ID.
char product_rev[8]
Product revision.
uint64_t part_bitmap
Bitmap of partitions with active events.
uint64_t global
Bitmap of global events.
unsigned part[SWITCHTEC_MAX_PARTS]
Bitmap of events in each partition.
unsigned local_part
Bitmap of events in the local partition.
unsigned pff[SWITCHTEC_MAX_PFF_CSR]
Bitmap of events in each port function.
Information about a firmware image or partition.
size_t part_addr
Address of the partition.
size_t part_len
Length of the partition.
unsigned int acs_ctrl
ACS Setting of the Port.
char * pci_bdf_path
PCI BDF path of the port.
char * pci_bdf
PCI BDF of the port.
char * class_devices
Comma seperated list of classes.
char * pci_dev
PCI BDF of the device on the port.
switchtec_event_id
Enumeration of all possible events.
__gas struct switchtec_gas * gasptr_t
Shortform for a pointer to the GAS register space.