From b64b44f3aec06d456eeed7be5e7e66f9078b9884 Mon Sep 17 00:00:00 2001 From: kib Date: Fri, 19 Apr 2019 13:18:54 +0000 Subject: [PATCH] MFC r345966, r345968: Implement devctl(8) command 'reset', using DEV_RESET /dev/devctl2 ioctl. --- lib/libdevctl/devctl.3 | 46 +++++++++++++++++++++++++++++++++++++++- lib/libdevctl/devctl.c | 8 +++++++ lib/libdevctl/devctl.h | 1 + usr.sbin/devctl/devctl.8 | 34 ++++++++++++++++++++++++++++- usr.sbin/devctl/devctl.c | 40 ++++++++++++++++++++++++++++++++-- 5 files changed, 125 insertions(+), 4 deletions(-) diff --git a/lib/libdevctl/devctl.3 b/lib/libdevctl/devctl.3 index a4fe0620ac3..29f936b68c4 100644 --- a/lib/libdevctl/devctl.3 +++ b/lib/libdevctl/devctl.3 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 29, 2016 +.Dd April 4, 2019 .Dt DEVCTL 3 .Os .Sh NAME @@ -37,6 +37,7 @@ .Nm devctl_disable , .Nm devctl_enable , .Nm devctl_rescan , +.Nm devctl_reset , .Nm devctl_resume , .Nm devctl_set_driver , .Nm devctl_suspend @@ -60,6 +61,8 @@ .Ft int .Fn devctl_rescan "const char *device" .Ft int +.Fn devctl_reset "const char *device" "bool detach" +.Ft int .Fn devctl_resume "const char *device" .Ft int .Fn devctl_set_driver "const char *device" "const char *driver" "bool force" @@ -189,6 +192,15 @@ The .Fn devctl_rescan function rescans a bus device checking for devices that have been added or removed. +.Pp +The +.Fn devctl_reset +function resets the specified device using bus-specific reset method. +The +.Fa detach +argument, if true, specifies that the device driver is detached before +the reset, and re-attached afterwards. +If false, the device is suspended before the reset, and resumed after. .Sh RETURN VALUES .Rv -std devctl_attach devctl_clear_driver devctl_delete devctl_detach \ devctl_disable devctl_enable devctl_suspend devctl_rescan devctl_resume \ @@ -362,6 +374,21 @@ is false. .Fa dev is the root device of the device tree. .El +.Pp +The +.Fn devctl_reset +function may fail if: +.Bl -tag -width Er +.It Bq Er ENXIO +The bus does not implement the reset method. +.It Bq Er ETIMEDOUT +The device failed to respond after the reset in the time limits +specific to the bus. +.El +The +.Fn devctl_reset +function may also return errors caused by the attach, detach, suspend, +and resume methods of the device driver. .Sh SEE ALSO .Xr devinfo 3 , .Xr devstat 3 , @@ -376,3 +403,20 @@ If a device is suspended individually via .Fn devctl_suspend and the entire machine is subsequently suspended, the device will be resumed when the machine resumes. +.Pp +Similarly, if the device is suspended, and +.Fn devctl_reset +is called on the device with +.Fa detach +set to +.Va false , +the device is resumed by the +.Fn devctl_reset +call. +Or, if the driver for the device is detached manually, and +.Fn devctl_reset +is called on the device with +.Fa detach +set to +.Va true , +device reset re-attaches the driver. diff --git a/lib/libdevctl/devctl.c b/lib/libdevctl/devctl.c index 5013fb0b9d5..1ce17aacbe4 100644 --- a/lib/libdevctl/devctl.c +++ b/lib/libdevctl/devctl.c @@ -145,3 +145,11 @@ devctl_delete(const char *device, bool force) return (devctl_simple_request(DEV_DELETE, device, force ? DEVF_FORCE_DELETE : 0)); } + +int +devctl_reset(const char *device, bool detach) +{ + + return (devctl_simple_request(DEV_RESET, device, detach ? + DEVF_RESET_DETACH : 0)); +} diff --git a/lib/libdevctl/devctl.h b/lib/libdevctl/devctl.h index f5df31ae2df..bd327fdb8c5 100644 --- a/lib/libdevctl/devctl.h +++ b/lib/libdevctl/devctl.h @@ -42,6 +42,7 @@ int devctl_set_driver(const char *device, const char *driver, bool force); int devctl_clear_driver(const char *device, bool force); int devctl_rescan(const char *device); int devctl_delete(const char *device, bool force); +int devctl_reset(const char *device, bool detach); __END_DECLS #endif /* !__DEVCTL_H__ */ diff --git a/usr.sbin/devctl/devctl.8 b/usr.sbin/devctl/devctl.8 index 67b870960fe..0460e89022e 100644 --- a/usr.sbin/devctl/devctl.8 +++ b/usr.sbin/devctl/devctl.8 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 29, 2016 +.Dd April 4, 2019 .Dt DEVCTL 8 .Os .Sh NAME @@ -67,6 +67,10 @@ .Cm delete .Op Fl f .Ar device +.Nm +.Cm reset +.Op Fl d +.Ar device .Sh DESCRIPTION The .Nm @@ -167,7 +171,35 @@ the device will be deleted even if it is physically present. This command should be used with care as a device that is deleted but present can no longer be used unless the parent bus device rediscovers the device via a rescan request. +.It Xo Cm reset +.Op Fl d +.Ar device +.Xc +Reset the device, using bus-specific reset method. +Drivers for the devices being reset are suspended around the reset. +If the +.Fl d +option is specified, drivers are detached instead. +.Pp +Currently, resets are implemented for PCIe buses and PCI devices. +For PCIe bus, the link is disabled and then re-trained, causing all +children of the bus to reset. +Use +.Fl p +option of +.Xr devinfo 8 +tool to report parent bus for the device. +For PCI device, if Function-Level Reset is implemented by it, FLR is +tried first; if failed or not implemented, power reset is tried. +.Pp +If you have detached or suspended a child device explicitly and then +do a reset, the child device will end up attached. .El +.Sh BUGS +Currently there is no administrative flag to prevent re-attach or resume +of the manually detached or suspended devices after reset. +Similarly, there is no flag to prevent un-suspending of the the manually +suspended devices after system resume. .Sh SEE ALSO .Xr devctl 3 , .Xr devinfo 8 diff --git a/usr.sbin/devctl/devctl.c b/usr.sbin/devctl/devctl.c index 6b266f42fe4..54e05d97f54 100644 --- a/usr.sbin/devctl/devctl.c +++ b/usr.sbin/devctl/devctl.c @@ -71,7 +71,7 @@ DEVCTL_TABLE(top, set); static void usage(void) { - fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", + fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "usage: devctl attach device", " devctl detach [-f] device", " devctl disable [-f] device", @@ -81,7 +81,9 @@ usage(void) " devctl set driver [-f] device driver", " devctl clear driver [-f] device", " devctl rescan device", - " devctl delete [-f] device"); + " devctl delete [-f] device", + " devctl reset [-d] device" + ); exit(1); } @@ -343,6 +345,40 @@ delete(int ac, char **av) } DEVCTL_COMMAND(top, delete, delete); +static void +reset_usage(void) +{ + + fprintf(stderr, "usage: devctl reset [-d] device\n"); + exit(1); +} + +static int +reset(int ac, char **av) +{ + bool detach_drv; + int ch; + + detach_drv = false; + while ((ch = getopt(ac, av, "d")) != -1) + switch (ch) { + case 'd': + detach_drv = true; + break; + default: + reset_usage(); + } + ac -= optind; + av += optind; + + if (ac != 1) + reset_usage(); + if (devctl_reset(av[0], detach_drv) < 0) + err(1, "Failed to reset %s", av[0]); + return (0); +} +DEVCTL_COMMAND(top, reset, reset); + int main(int ac, char *av[]) { -- 2.45.0