From owner-svn-src-all@freebsd.org Sat Nov 3 00:37:52 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7E5C510E0D67; Sat, 3 Nov 2018 00:37:52 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 3546586C2A; Sat, 3 Nov 2018 00:37:52 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 12F78124C0; Sat, 3 Nov 2018 00:37:52 +0000 (UTC) (envelope-from imp@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wA30bp2k070787; Sat, 3 Nov 2018 00:37:51 GMT (envelope-from imp@FreeBSD.org) Received: (from imp@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wA30bpaJ070785; Sat, 3 Nov 2018 00:37:51 GMT (envelope-from imp@FreeBSD.org) Message-Id: <201811030037.wA30bpaJ070785@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: imp set sender to imp@FreeBSD.org using -f From: Warner Losh Date: Sat, 3 Nov 2018 00:37:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r340092 - head/sys/dev/ahci X-SVN-Group: head X-SVN-Commit-Author: imp X-SVN-Commit-Paths: head/sys/dev/ahci X-SVN-Commit-Revision: 340092 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Nov 2018 00:37:52 -0000 Author: imp Date: Sat Nov 3 00:37:51 2018 New Revision: 340092 URL: https://svnweb.freebsd.org/changeset/base/340092 Log: Implement ability to turn on/off PHYs for AHCI devices. As part of Chuck's work on fixing kernel crashes caused by disk I/O errors, it is useful to be able to trigger various kinds of errors. This patch allows causing an AHCI-attached disk to disappear, by having the driver keep the PHY disabled when the driver would otherwise enable the PHY. It also allows making the disk reappear by having the driver go back to setting the PHY enable/disable state as it normal would and simulating the hardware event that causes a bus rescan. Submitted by: Chuck Silvers Sponsored by: Netflix, Inc Differential Revision: https://reviews.freebsd.org/D16043 Modified: head/sys/dev/ahci/ahci.c head/sys/dev/ahci/ahci.h Modified: head/sys/dev/ahci/ahci.c ============================================================================== --- head/sys/dev/ahci/ahci.c Fri Nov 2 23:03:40 2018 (r340091) +++ head/sys/dev/ahci/ahci.c Sat Nov 3 00:37:51 2018 (r340092) @@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -80,6 +81,8 @@ static void ahci_stop(struct ahci_channel *ch); static void ahci_clo(struct ahci_channel *ch); static void ahci_start_fr(struct ahci_channel *ch); static void ahci_stop_fr(struct ahci_channel *ch); +static int ahci_phy_check_events(struct ahci_channel *ch, u_int32_t serr); +static uint32_t ahci_ch_detval(struct ahci_channel *ch, uint32_t val); static int ahci_sata_connect(struct ahci_channel *ch); static int ahci_sata_phy_reset(struct ahci_channel *ch); @@ -100,6 +103,13 @@ static MALLOC_DEFINE(M_AHCI, "AHCI driver", "AHCI driv #define RECOVERY_REQUEST_SENSE 2 #define recovery_slot spriv_field1 +static uint32_t +ahci_ch_detval(struct ahci_channel *ch, uint32_t val) +{ + + return ch->disablephy ? ATA_SC_DET_DISABLE : val; +} + int ahci_ctlr_setup(device_t dev) { @@ -665,11 +675,38 @@ ahci_ch_probe(device_t dev) } static int +ahci_ch_disablephy_proc(SYSCTL_HANDLER_ARGS) +{ + struct ahci_channel *ch; + int error, value; + + ch = arg1; + value = ch->disablephy; + error = sysctl_handle_int(oidp, &value, 0, req); + if (error != 0 || req->newptr == NULL || (value != 0 && value != 1)) + return (error); + + mtx_lock(&ch->mtx); + ch->disablephy = value; + if (value) { + ahci_ch_deinit(ch->dev); + } else { + ahci_ch_init(ch->dev); + ahci_phy_check_events(ch, ATA_SE_PHY_CHANGED | ATA_SE_EXCHANGED); + } + mtx_unlock(&ch->mtx); + + return (0); +} + +static int ahci_ch_attach(device_t dev) { struct ahci_controller *ctlr = device_get_softc(device_get_parent(dev)); struct ahci_channel *ch = device_get_softc(dev); struct cam_devq *devq; + struct sysctl_ctx_list *ctx; + struct sysctl_oid *tree; int rid, error, i, sata_rev = 0; u_int32_t version; @@ -787,6 +824,11 @@ ahci_ch_attach(device_t dev) ahci_ch_pm, ch); } mtx_unlock(&ch->mtx); + ctx = device_get_sysctl_ctx(dev); + tree = device_get_sysctl_tree(dev); + SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "disable_phy", + CTLFLAG_RW | CTLTYPE_UINT, ch, 0, ahci_ch_disablephy_proc, "IU", + "Disable PHY"); return (0); err3: @@ -2497,7 +2539,7 @@ static int ahci_sata_phy_reset(struct ahci_channel *ch) { int sata_rev; - uint32_t val; + uint32_t val, detval; if (ch->listening) { val = ATA_INL(ch->r_mem, AHCI_P_CMD); @@ -2514,12 +2556,14 @@ ahci_sata_phy_reset(struct ahci_channel *ch) val = ATA_SC_SPD_SPEED_GEN3; else val = 0; + detval = ahci_ch_detval(ch, ATA_SC_DET_RESET); ATA_OUTL(ch->r_mem, AHCI_P_SCTL, - ATA_SC_DET_RESET | val | + detval | val | ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER); DELAY(1000); + detval = ahci_ch_detval(ch, ATA_SC_DET_IDLE); ATA_OUTL(ch->r_mem, AHCI_P_SCTL, - ATA_SC_DET_IDLE | val | ((ch->pm_level > 0) ? 0 : + detval | val | ((ch->pm_level > 0) ? 0 : (ATA_SC_IPM_DIS_PARTIAL | ATA_SC_IPM_DIS_SLUMBER))); if (!ahci_sata_connect(ch)) { if (ch->caps & AHCI_CAP_SSS) { Modified: head/sys/dev/ahci/ahci.h ============================================================================== --- head/sys/dev/ahci/ahci.h Fri Nov 2 23:03:40 2018 (r340091) +++ head/sys/dev/ahci/ahci.h Sat Nov 3 00:37:51 2018 (r340092) @@ -461,6 +461,8 @@ struct ahci_channel { struct mtx_padalign mtx; /* state lock */ STAILQ_HEAD(, ccb_hdr) doneq; /* queue of completed CCBs */ int batch; /* doneq is in use */ + + int disablephy; /* keep PHY disabled */ }; struct ahci_enclosure {