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";
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);
118 char syspath[PATH_MAX];
120 ret = dev_to_sysfs_path(ldev,
"device/switchtec", syspath,
125 ret = access(syspath, F_OK);
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)
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];
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];
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);
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) {
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,
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];
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;
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;
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,
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)
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];
833 for (; i < SWITCHTEC_MAX_PFF_CSR; i++)
837 #define EV(t, n)[SWITCHTEC_ ## t ## _EVT_ ## n] = \
838 SWITCHTEC_IOCTL_EVENT_ ## n
840 static const int event_map[] = {
841 EV(GLOBAL, STACK_ERROR),
842 EV(GLOBAL, PPU_ERROR),
843 EV(GLOBAL, ISP_ERROR),
844 EV(GLOBAL, SYS_RESET),
847 EV(GLOBAL, FW_NON_FATAL),
848 EV(GLOBAL, FW_FATAL),
849 EV(GLOBAL, TWI_MRPC_COMP),
850 EV(GLOBAL, TWI_MRPC_COMP_ASYNC),
851 EV(GLOBAL, CLI_MRPC_COMP),
852 EV(GLOBAL, CLI_MRPC_COMP_ASYNC),
853 EV(GLOBAL, GPIO_INT),
855 EV(PART, PART_RESET),
857 EV(PART, MRPC_COMP_ASYNC),
858 EV(PART, DYN_PART_BIND_COMP),
868 EV(PFF, TLP_THROTTLING),
869 EV(PFF, FORCE_SPEED),
870 EV(PFF, CREDIT_TIMEOUT),
874 static int linux_event_summary(
struct switchtec_dev *dev,
878 struct switchtec_ioctl_event_summary isum;
879 struct switchtec_ioctl_event_summary_legacy isum_legacy;
885 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY, &isum);
887 event_summary_copy(sum, &isum, ARRAY_SIZE(isum.pff));
891 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY, &isum);
895 event_summary_copy(sum, &isum, ARRAY_SIZE(isum_legacy.pff));
900 static int linux_event_ctl(
struct switchtec_dev *dev,
902 int index,
int flags,
906 struct switchtec_ioctl_event_ctl ctl;
909 if (e >= SWITCHTEC_MAX_EVENTS)
912 ctl.event_id = event_map[e];
915 if (flags & SWITCHTEC_EVT_FLAG_CLEAR)
916 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR;
917 if (flags & SWITCHTEC_EVT_FLAG_EN_POLL)
918 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
919 if (flags & SWITCHTEC_EVT_FLAG_EN_LOG)
920 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
921 if (flags & SWITCHTEC_EVT_FLAG_EN_CLI)
922 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
923 if (flags & SWITCHTEC_EVT_FLAG_EN_FATAL)
924 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
925 if (flags & SWITCHTEC_EVT_FLAG_DIS_POLL)
926 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL;
927 if (flags & SWITCHTEC_EVT_FLAG_DIS_LOG)
928 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG;
929 if (flags & SWITCHTEC_EVT_FLAG_DIS_CLI)
930 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI;
931 if (flags & SWITCHTEC_EVT_FLAG_DIS_FATAL)
932 ctl.flags |= SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL;
935 ret = ioctl(ldev->fd, SWITCHTEC_IOCTL_EVENT_CTL, &ctl);
941 memcpy(data, ctl.data,
sizeof(ctl.data));
946 static int linux_event_wait(
struct switchtec_dev *dev,
int timeout_ms)
950 struct pollfd fds = {
955 ret = poll(&fds, 1, timeout_ms);
959 if (fds.revents & POLLERR) {
964 if (fds.revents & POLLPRI)
970 static const struct switchtec_ops linux_ops = {
971 .close = linux_close,
972 .get_device_id = linux_get_device_id,
973 .get_fw_version = linux_get_fw_version,
975 .get_devices = linux_get_devices,
976 .pff_to_port = linux_pff_to_port,
977 .port_to_pff = linux_port_to_pff,
978 .gas_map = linux_gas_map,
979 .gas_unmap = linux_gas_unmap,
980 .flash_part = linux_flash_part,
981 .event_summary = linux_event_summary,
982 .event_ctl = linux_event_ctl,
983 .event_wait = linux_event_wait,
985 .gas_read8 = mmap_gas_read8,
986 .gas_read16 = mmap_gas_read16,
987 .gas_read32 = mmap_gas_read32,
988 .gas_read64 = mmap_gas_read64,
989 .gas_write8 = mmap_gas_write8,
990 .gas_write16 = mmap_gas_write16,
991 .gas_write32 = mmap_gas_write32,
992 .gas_write32_no_retry = mmap_gas_write32,
993 .gas_write64 = mmap_gas_write64,
994 .memcpy_to_gas = mmap_memcpy_to_gas,
995 .memcpy_from_gas = mmap_memcpy_from_gas,
996 .write_from_gas = mmap_write_from_gas,
1004 fd = open(path, O_RDWR | O_CLOEXEC);
1013 ldev = malloc(
sizeof(*ldev));
1019 if (check_switchtec_device(ldev))
1020 goto err_close_free;
1022 if (get_partition(ldev))
1023 goto err_close_free;
1025 ldev->dev.ops = &linux_ops;
1037 char path[PATH_MAX];
1038 struct switchtec_dev *dev;
1040 snprintf(path,
sizeof(path),
"/dev/switchtec%d", index);
1044 if (errno == ENOENT)
1051 int device,
int func)
1053 char path[PATH_MAX];
1054 struct switchtec_dev *dev;
1055 struct dirent *dirent;
1058 snprintf(path,
sizeof(path),
1059 "/sys/bus/pci/devices/%04x:%02x:%02x.%x/switchtec",
1060 domain, bus, device, func);
1062 dir = opendir(path);
1066 while ((dirent = readdir(dir))) {
1067 if (dirent->d_name[0] !=
'.')
1081 snprintf(path,
sizeof(path),
"/dev/%s", dirent->d_name);
1082 printf(
"%s\n", path);