From patchwork Mon Dec 9 17:48:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Li X-Patchwork-Id: 13900129 Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2046.outbound.protection.outlook.com [40.107.247.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BC001E9B3E; Mon, 9 Dec 2024 17:48:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.247.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733766541; cv=fail; b=GVcFh3MeLLRXV0DniOyJEJ2FW7x08ndftQCggvtFgXD+jXz6Mw9rzV6teS98u/NRaJWuZyS7juAB8wb6FKMK1cS4GRH9XVqKkglNJvxlhxmnobPx4FC/qBlNolwbjYcFFshbxFZMuFGCFmYBCKfRzLfvOTEfsVYF33B98d/K6DM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733766541; c=relaxed/simple; bh=ecsUMST1rqgl5nkhoe/wR/oiBwEmdq7KRl7zuhcNim8=; h=From:Date:Subject:Content-Type:Message-Id:References:In-Reply-To: To:Cc:MIME-Version; b=iYRN895TtKqI78JRI9WjjUlYdXzNlTOuk/AySREVHQsauwevN4/CftZmqUnbjuDkyq7WmEY2n31gluxPlRp9rgSScirJ/NmY9VbUB93UjDmoxGiG2gOYxSDqzYpaMY27HSEwBy4AwyR7ZOIvj4dCe8uBsm0UNaCISGSJRK76OBE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com; spf=pass smtp.mailfrom=nxp.com; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b=RjNqUNt5; arc=fail smtp.client-ip=40.107.247.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nxp.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nxp.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nxp.com header.i=@nxp.com header.b="RjNqUNt5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pgWgeAjOv3Ot2XgiWyR63xYErmKvgwZHXYM9cu5uBcS3wLvrG+fGDQouoyKeiFQyQmlcZU5PjDrCg3yrddQSDIZGkRblwcCU2YoSSXGKX4nLjy8tI5FyNbxZVQmc4YBv8KSTzx2/FeWBO80BESv79i8CTlsZbVM65OM2Y+E0bxNrM4XDcq1MMrMrKlZM3GQNjtM3z8hNTcoNJu6t7nj5x2KLWHH2Rhu7Js6klUtEuOtygJqwGIQGc5LkpIGB2tMGk0NZDZCCjfFsm/B3xEh9vr9Dv7C2A4hYg01UtRKAuUeQXXBSHLcTyngQ/JZ2QQniSoCixp5MTmpFt7KRYVtbcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=86wpQXYBbhA0fUx6zsRkAJcExYphOAfZcp3Q386zG4s=; b=hUC89UKWnH6w/WOgerKUOR5mYTXFtaFe/OBmOYnAzOv/jiZS3bN2eDxbS6+HO+PT0s2nrUYSGaQ1qSDEnbLxhoTsoTxf9G8EqYTWdjxlGFBJ9v923Z4xnaTWQn95GxSf5zaUNdLZ8k+pa5DFZqIMikaMsS/OQiohmg8lCqOPLXtaUkVPilBZZYlIRtxRyBhSOQt28eSIuASDOcdh958eiEhVUdgFKdPbmZ6+gkktNlGQco4FdGtV9WXzrRwFGr9qH5okmlkhsrp5I7LMK7MWpdHH9PM/H+QVLRb3vmE8kNNO7hoyCQRA32qvXZCPnEpj9UA7TQTjUW21Xpl8UzCmsw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=86wpQXYBbhA0fUx6zsRkAJcExYphOAfZcp3Q386zG4s=; b=RjNqUNt5Q0MrGRgCVaM2H6LWBKSitrHjBMiuF9yHp7v1Wy8G9eChb1Hot2lnB+0Vbv/SUBv18YhYu+mOl8FCgNdf2JiJmzyLDWA0u2FNeNwMkZ5TT7y+iOvcdsDbYvMRX3YAtQxZHMubpN4sMRa/4NkKp5thmD1n/8Z7KFHvHXDAtjVRD5AS4cSNu5Mlp+MEOkSHbmpdt/SvREb1c8WyccUQZl1D5883wkEtsMR2ttZxKN2ghkm3Jh9Mhq2D7wNZ+p8LMaf86aqR8hKTGqHWMFwyUONdchZsM5UPKdVawIa1JOfp8H5sgIEtILHey42NF2sxAqbeoEuxDrVADkqGnA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) by GVXPR04MB11017.eurprd04.prod.outlook.com (2603:10a6:150:21c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.18; Mon, 9 Dec 2024 17:48:56 +0000 Received: from PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06]) by PAXPR04MB9642.eurprd04.prod.outlook.com ([fe80::9126:a61e:341d:4b06%5]) with mapi id 15.20.8230.010; Mon, 9 Dec 2024 17:48:56 +0000 From: Frank Li Date: Mon, 09 Dec 2024 12:48:18 -0500 Subject: [PATCH v11 5/7] PCI: endpoint: pci-epf-test: Add doorbell test support Message-Id: <20241209-ep-msi-v11-5-7434fa8397bd@nxp.com> References: <20241209-ep-msi-v11-0-7434fa8397bd@nxp.com> In-Reply-To: <20241209-ep-msi-v11-0-7434fa8397bd@nxp.com> To: Manivannan Sadhasivam , =?utf-8?q?Krzy?= =?utf-8?q?sztof_Wilczy=C5=84ski?= , Kishon Vijay Abraham I , Bjorn Helgaas , Arnd Bergmann , Greg Kroah-Hartman , "Rafael J. Wysocki" , Thomas Gleixner , Anup Patel , Marc Zyngier Cc: linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, imx@lists.linux.dev, Niklas Cassel , dlemoal@kernel.org, jdmason@kudzu.us, linux-arm-kernel@lists.infradead.org, Frank Li X-Mailer: b4 0.13-dev-e586c X-Developer-Signature: v=1; a=ed25519-sha256; t=1733766511; l=7892; i=Frank.Li@nxp.com; s=20240130; h=from:subject:message-id; bh=ecsUMST1rqgl5nkhoe/wR/oiBwEmdq7KRl7zuhcNim8=; b=uxujQ6ohRzq4QBUCI2MSdKbUWs+86awXR45sexW93IlWsrlBWguXOEuw2DYxoC2Uyt4kjkNb9 dBYfD9YWLsLBpQqfN22jF0QmwCoqpNsAzTJ1vr1jLAB5yDOjJ1LMqkL X-Developer-Key: i=Frank.Li@nxp.com; a=ed25519; pk=I0L1sDUfPxpAkRvPKy7MdauTuSENRq+DnA+G4qcS94Q= X-ClientProxiedBy: BY5PR17CA0070.namprd17.prod.outlook.com (2603:10b6:a03:167::47) To PAXPR04MB9642.eurprd04.prod.outlook.com (2603:10a6:102:240::14) Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PAXPR04MB9642:EE_|GVXPR04MB11017:EE_ X-MS-Office365-Filtering-Correlation-Id: ddf06acc-f235-4701-cf32-08dd1879c0ef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|52116014|376014|366016|1800799024|7416014|38350700014|921020; X-Microsoft-Antispam-Message-Info: =?utf-8?q?Vpo75sBMYxJ/Gk8jZ/BuQY4/M9bn6mY?= =?utf-8?q?m83jo5ZLDAxC0KFWbtkbLbTleVxzTycil0Fi48iRq0mscaF398os/RIYlNvYhm1cG?= =?utf-8?q?EHT/+8leoQC+dsKU217+yRA6l09w99qM68nXxfc57gsd4GTeDtPJ4V9OjuwQ09m+A?= =?utf-8?q?6Qe8BP7dACEXnYBFB9hoRqN1EuYlO1NSP9fZeIHgrmVx4/daVfH2ykUEHjgxaW9HF?= =?utf-8?q?I33w6E8D2Sa4YQTXq7DhRxLpqmB1vyJozbnFjmGAYEgg+uUillCvYQqQmqXPHtnJv?= =?utf-8?q?uEYe4In89a0yhAIdEFrCP7+EZizyYqbJ2l+HIy2c8NOfee06QNSMMywnJM1/ouVpR?= =?utf-8?q?gSvYmUlKaJTaLyZzbIXPEIsQDWUkVup/VzxQDo6KPFAPOSBytP7ehJVwIxOfiyZL2?= =?utf-8?q?8OFA0NAiV58VY84XTTlSEZKqoe4PlWm8hJXA7or5OkUMRdXs/vsLNmiGLTlPdzRV8?= =?utf-8?q?wdNo52BOMpkLWfRZK55tjESDARfpnNTNoXGc9L1S1qC+V88ujQNb69mPg3CG4Wdb0?= =?utf-8?q?GJjnyuT+EiaaKU5hIevDWuwtDUueAaFYOK9SkSpE/j75SOnlyp2XhDMtSpedImoSw?= =?utf-8?q?n4WaQ5BoOGWY7jUmtAiRulKViBU0yDHDBIyBcSXQr1bSXh6CiFKv1u81JNq0KuwVk?= =?utf-8?q?ch6fl9gxn2pqYB6PJSs+a3bRk5DZ02k1UUF9gdY2wn1i6Haz2upiRUmUEOh+FdXgG?= =?utf-8?q?f5q+KQk9vvTIvxpsJXEfXCMREH/eT38ymwahhkXKDq02OXKx4xWdNBvdHh/2Ry+ME?= =?utf-8?q?lXvOQSqi7YYwDCHwesYnGJnRL1a1S8e/rK/zfjQi8Hkv0a4S5W+vCaLBiXmXKiy/B?= =?utf-8?q?hWWaF9kKxXayBpETsP1SNPanAmdVs5n1ydJLqiUdDP3f3HbI1vj7Z0HTir4hXSRGA?= =?utf-8?q?Q0juisJEdYOhb7SeOCIAB8EQBh2i6V88/Z9b6IHqrOaDMsX5W5/4j6veW0bUywn9l?= =?utf-8?q?mmYycbADaSKh9sY750clgfs+6nZjerpKXvbNpEQ+bdQFVAYnfXckOV17bDNy5S6t6?= =?utf-8?q?YHR9WoxeLEiD7bz/XO1xZPig0NXOx3wtNJveRyReEm4+gE9cOu/ZrsJNCC85Mrbou?= =?utf-8?q?60Hp4/luLn1s+tG0jSnEHD+VhsAG90ZPf+QrS3Gd6+dadk5QVuSFGcc0b8lUIr8+V?= =?utf-8?q?Qx/e4ZN+VJ2jIWGUKCYiTO3aR/QcNqLlOxhbmTCO+lAvAX0PXl421/QVcvL41puPs?= =?utf-8?q?918aUcJlYFEzpBUUTCrfuUqKsa7zlM58feWfAdS5NW71+k/cp+DozHCPVJ0zs4DBV?= =?utf-8?q?79IC1JXIFkqaSECRDAR7mSepI1XxptTCsjICFf9ATrmzRehOp85l3sF1pH+U5zjVN?= =?utf-8?q?d/glarQu1zEzkABBeKwsa9sEbgkW1za73X9877iXelrlKxU3eeearec=3D?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PAXPR04MB9642.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(52116014)(376014)(366016)(1800799024)(7416014)(38350700014)(921020);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?36CMcDg6nBt5gGR+oD560Y1REFr6?= =?utf-8?q?FKKr0upRK/qi5oMdRK1C/jRWy0X0Gg1FmuRoyOYE0aE4gNR+21ZPMqZIl5UrDbLoO?= =?utf-8?q?IbyJT3h4lRw+op0kNbRlI/zGyTTUtCyqTGLvrqp4muZfsYCv1vrD0p9ITicOj4uyn?= =?utf-8?q?lku8TGQVmLrsqoB1zZvFaoEMm+9ncmfZU11dpZGcFI33QedRt4DOOH8LxvcbVI+Kv?= =?utf-8?q?tGcv1YaxfQKz2EGpFpkadLXTysL1wAzlk/WNyTgPNfHkJrbb4xK1Yy2fwrY+1cJ/b?= =?utf-8?q?8yf4MNPHff1AYDmysjYFfE1twntDvl2jRw/Gl4RuGdRfu8iAP1rqUNVLcGer9oErc?= =?utf-8?q?HmS/mRwR3njv+L+LMGXqKYt8H6iKVNpFkQheSSc4mLodbpRNUGoKRCezULt0hviKn?= =?utf-8?q?RVhtLrhy0lArMO0COdPAdWSfKhHwne8bbck47Wml5loj48WZxnBRvflMw/x/CFUmB?= =?utf-8?q?E4l48PAlp8FQ3g1FmhzwWG1E33boEtQmJSGg3gewRKucNRtOg3keNZWC9bR/I4pfj?= =?utf-8?q?g1IGLbuEvnw5L6P1spi6mO8M7CdlA2ODOL23TeEkB2MlDkSQ5pg0MmhXTJ1Xy9Mio?= =?utf-8?q?mJY3Svrz8BR8XW9/UK71CdTm7SSXEF7zcVJijkJXtZmXr0zUeEQVToesFIIxzUfKF?= =?utf-8?q?rvPGdEW98ptHKozAkepvffzXtOzw9iQ0mfUfSxVaRQltyPGqAoq30seIP0DRUs8tl?= =?utf-8?q?sdfXJJ2cj+YDJjCXSd94KxJSWsODQrKtCxGuNSog1rxLMXC60JNMxfzEoRP/LD4R4?= =?utf-8?q?PbxefCfseAz+Z93PlHmLdS+d2vhTETpzXCRovgGeUv0T4BYiLXDwkwm+qGYIYXvnG?= =?utf-8?q?KgCJtJIz6Ff4h9PwMN1k47U1VinmJ08LqSOpmFFbDoq4ZSFRp5JzcRoMkt6Xdy6K/?= =?utf-8?q?aofjoSC8ib22POlPD7r+z7rCGU5c58aKGhR6HPt4JSPRhaeXcNWxcEn+s7GqBt7qh?= =?utf-8?q?CIzOSp+iZgliLZtrEHA0Fva+h01dr+EPhPqMayFFE0ozlrKe/j4Or3PNL+YpjKiJ4?= =?utf-8?q?UJc+oaTxaWXY7p3wOVWBRkFHGlzskFy8t7eKnO1GDzaPT1R+IEWm+RU90lDPp7ZEX?= =?utf-8?q?OzhMU+kblTplWD6pRZoOJeUgJCVN9d/G4xVKQC3sZoUJivGpdRXWJejJNd3G3yGW/?= =?utf-8?q?TzeuOaU+Ax68CCoEsH9ogstRPBEZB/6i6D1if0VGQFO3el/S5ChHDkmSrJCnl78pr?= =?utf-8?q?2Js5wZFmmzssm3nZp03WhJ6hQXlc6gbmg7HhenVaL0Qm4UDiJG1dnCkBjFrWqHgAU?= =?utf-8?q?Ob7m+nioxQPhP2vIm7NPRz9RiYNCWpv8WBptb4GoJT/E2EeV5OOpd7vks9Ia2N9wS?= =?utf-8?q?UZx7PdQjUYtCyCB8oxXs5qqKSWF9LAkQ6lTSYTRjA4wpukX85ZhMh98Av5vvPCmxq?= =?utf-8?q?qIFXDS+PeWj5RjzuHd4SAFuQY0NdLeaSl510/18NBjTMs58QRMAg4+NqBFXVptaDM?= =?utf-8?q?cVjdTTVW1uJOG+KbgI0wLmIAJ7ZbUwY+9M34zvRjrElMLoD6a3q2A2F4=3D?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: ddf06acc-f235-4701-cf32-08dd1879c0ef X-MS-Exchange-CrossTenant-AuthSource: PAXPR04MB9642.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Dec 2024 17:48:56.4770 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: uIyRwQeWlVF1/wk3/UoD11qonQ0g27U0CETDxq33nHybu6t68EZMEEX4q9rcLr/YGnxiaKK4TjC9I8U32A/Hyg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR04MB11017 Add three registers: doorbell_bar, doorbell_addr, and doorbell_data. Use pci_epf_alloc_doorbell() to allocate a doorbell address space. Enable the Root Complex (RC) side driver to trigger pci-epc-test's doorbell callback handler by writing doorbell_data to the mapped doorbell_bar's address space. Set STATUS_DOORBELL_SUCCESS in the doorbell callback to indicate completion. Avoid breaking compatibility between host and endpoint, add new command COMMAND_ENABLE_DOORBELL and COMMAND_DISABLE_DOORBELL. Host side need send COMMAND_ENABLE_DOORBELL to map one bar's inbound address to MSI space. the command COMMAND_DISABLE_DOORBELL to recovery original inbound address mapping. Host side new driver Host side old driver EP: new driver S F EP: old driver F F S: If EP side support MSI, 'pcitest -B' return success. If EP side doesn't support MSI, the same to 'F'. F: 'pcitest -B' return failure, other case as usual. Tested-by: Niklas Cassel Signed-off-by: Frank Li --- Change from v9 to v11 - none Change from v8 to v9 - move pci_epf_alloc_doorbell() into pci_epf_{enable/disable}_doorbell(). - remove doorbell_done in commit message. - rename pci_epf_{enable/disable}_doorbell() to pci_epf_test_{enable/disable}_doorbell() to align corrent code style. Change from v7 to v8 - rename to pci_epf_align_inbound_addr_lo_hi() Change from v6 to v7 - use help function pci_epf_align_addr_lo_hi() Change from v5 to v6 - rename doorbell_addr to doorbell_offset Chagne from v4 to v5 - Add doorbell free at unbind function. - Move msi irq handler to here to more complex user case, such as differece doorbell can use difference handler function. - Add Niklas's code to handle fixed bar's case. If need add your signed-off tag or co-developer tag, please let me know. change from v3 to v4 - remove revid requirement - Add command COMMAND_ENABLE_DOORBELL and COMMAND_DISABLE_DOORBELL. - call pci_epc_set_bar() to map inbound address to MSI space only at COMMAND_ENABLE_DOORBELL. --- drivers/pci/endpoint/functions/pci-epf-test.c | 132 ++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c index ef6677f34116e..a0a0e86a081cb 100644 --- a/drivers/pci/endpoint/functions/pci-epf-test.c +++ b/drivers/pci/endpoint/functions/pci-epf-test.c @@ -11,12 +11,14 @@ #include #include #include +#include #include #include #include #include #include +#include #include #define IRQ_TYPE_INTX 0 @@ -29,6 +31,8 @@ #define COMMAND_READ BIT(3) #define COMMAND_WRITE BIT(4) #define COMMAND_COPY BIT(5) +#define COMMAND_ENABLE_DOORBELL BIT(6) +#define COMMAND_DISABLE_DOORBELL BIT(7) #define STATUS_READ_SUCCESS BIT(0) #define STATUS_READ_FAIL BIT(1) @@ -39,6 +43,11 @@ #define STATUS_IRQ_RAISED BIT(6) #define STATUS_SRC_ADDR_INVALID BIT(7) #define STATUS_DST_ADDR_INVALID BIT(8) +#define STATUS_DOORBELL_SUCCESS BIT(9) +#define STATUS_DOORBELL_ENABLE_SUCCESS BIT(10) +#define STATUS_DOORBELL_ENABLE_FAIL BIT(11) +#define STATUS_DOORBELL_DISABLE_SUCCESS BIT(12) +#define STATUS_DOORBELL_DISABLE_FAIL BIT(13) #define FLAG_USE_DMA BIT(0) @@ -74,6 +83,9 @@ struct pci_epf_test_reg { u32 irq_type; u32 irq_number; u32 flags; + u32 doorbell_bar; + u32 doorbell_offset; + u32 doorbell_data; } __packed; static struct pci_epf_header test_header = { @@ -642,6 +654,117 @@ static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test, } } +static irqreturn_t pci_epf_test_doorbell_handler(int irq, void *data) +{ + struct pci_epf_test *epf_test = data; + enum pci_barno test_reg_bar = epf_test->test_reg_bar; + struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar]; + + reg->status |= STATUS_DOORBELL_SUCCESS; + pci_epf_test_raise_irq(epf_test, reg); + + return IRQ_HANDLED; +} + +static void pci_epf_test_doorbell_cleanup(struct pci_epf_test *epf_test) +{ + struct pci_epf_test_reg *reg = epf_test->reg[epf_test->test_reg_bar]; + struct pci_epf *epf = epf_test->epf; + + if (reg->doorbell_bar > 0) { + free_irq(epf->db_msg[0].virq, epf_test); + reg->doorbell_bar = NO_BAR; + } + + if (epf->db_msg) + pci_epf_free_doorbell(epf); +} + +static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test, + struct pci_epf_test_reg *reg) +{ + struct pci_epf *epf = epf_test->epf; + struct pci_epf_bar db_bar = {}; + struct pci_epc *epc = epf->epc; + struct msi_msg *msg; + enum pci_barno bar; + size_t offset; + int ret; + + ret = pci_epf_alloc_doorbell(epf, 1); + if (ret) { + reg->status |= STATUS_DOORBELL_ENABLE_FAIL; + return; + } + + msg = &epf->db_msg[0].msg; + bar = pci_epc_get_next_free_bar(epf_test->epc_features, epf_test->test_reg_bar + 1); + if (bar < BAR_0 || bar == epf_test->test_reg_bar || !epf->db_msg) { + reg->status |= STATUS_DOORBELL_ENABLE_FAIL; + return; + } + + ret = request_irq(epf->db_msg[0].virq, pci_epf_test_doorbell_handler, 0, + "pci-test-doorbell", epf_test); + if (ret) { + dev_err(&epf->dev, + "Failed to request irq %d, doorbell feature is not supported\n", + epf->db_msg[0].virq); + reg->status |= STATUS_DOORBELL_ENABLE_FAIL; + pci_epf_test_doorbell_cleanup(epf_test); + return; + } + + reg->doorbell_data = msg->data; + reg->doorbell_bar = bar; + + msg = &epf->db_msg[0].msg; + ret = pci_epf_align_inbound_addr(epf, bar, ((u64)msg->address_hi << 32) | msg->address_lo, + &db_bar.phys_addr, &offset); + + if (ret) { + reg->status |= STATUS_DOORBELL_ENABLE_FAIL; + pci_epf_test_doorbell_cleanup(epf_test); + return; + } + + reg->doorbell_offset = offset; + + db_bar.barno = bar; + db_bar.size = epf->bar[bar].size; + db_bar.flags = epf->bar[bar].flags; + + ret = pci_epc_set_bar(epc, epf->func_no, epf->vfunc_no, &db_bar); + if (ret) { + reg->status |= STATUS_DOORBELL_ENABLE_FAIL; + pci_epf_test_doorbell_cleanup(epf_test); + } else { + reg->status |= STATUS_DOORBELL_ENABLE_SUCCESS; + } +} + +static void pci_epf_test_disable_doorbell(struct pci_epf_test *epf_test, + struct pci_epf_test_reg *reg) +{ + enum pci_barno bar = reg->doorbell_bar; + struct pci_epf *epf = epf_test->epf; + struct pci_epc *epc = epf->epc; + int ret; + + if (bar < BAR_0 || bar == epf_test->test_reg_bar || !epf->db_msg) { + reg->status |= STATUS_DOORBELL_DISABLE_FAIL; + return; + } + + ret = pci_epc_set_bar(epc, epf->func_no, epf->vfunc_no, &epf->bar[bar]); + if (ret) + reg->status |= STATUS_DOORBELL_DISABLE_FAIL; + else + reg->status |= STATUS_DOORBELL_DISABLE_SUCCESS; + + pci_epf_test_doorbell_cleanup(epf_test); +} + static void pci_epf_test_cmd_handler(struct work_struct *work) { u32 command; @@ -688,6 +811,14 @@ static void pci_epf_test_cmd_handler(struct work_struct *work) pci_epf_test_copy(epf_test, reg); pci_epf_test_raise_irq(epf_test, reg); break; + case COMMAND_ENABLE_DOORBELL: + pci_epf_test_enable_doorbell(epf_test, reg); + pci_epf_test_raise_irq(epf_test, reg); + break; + case COMMAND_DISABLE_DOORBELL: + pci_epf_test_disable_doorbell(epf_test, reg); + pci_epf_test_raise_irq(epf_test, reg); + break; default: dev_err(dev, "Invalid command 0x%x\n", command); break; @@ -934,6 +1065,7 @@ static void pci_epf_test_unbind(struct pci_epf *epf) pci_epf_test_clean_dma_chan(epf_test); pci_epf_test_clear_bar(epf); } + pci_epf_test_doorbell_cleanup(epf_test); pci_epf_test_free_space(epf); }