From patchwork Wed Aug 1 08:04:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Avri Altman X-Patchwork-Id: 10551817 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5EF3A157D for ; Wed, 1 Aug 2018 08:06:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50D5E2AC3D for ; Wed, 1 Aug 2018 08:06:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 43A0D2ADEA; Wed, 1 Aug 2018 08:06:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 885E42AC3D for ; Wed, 1 Aug 2018 08:06:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387509AbeHAJu5 (ORCPT ); Wed, 1 Aug 2018 05:50:57 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:37492 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733173AbeHAJu5 (ORCPT ); Wed, 1 Aug 2018 05:50:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1533110791; x=1564646791; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GhfA0xbpJm3eJ+VE3vsJLVvALcEJmc0jvnv5jf67KJ4=; b=Pfx7GbWsxEXabSuO4bqrp/hVTXrDKM8rSiUrmSwMdi23AbgQzbgk33us t4jbqiqM336QiDDMu9EdXrTfcscnSBbzGz7Y8rOQelu8KPJa1UWCxBQCE t3M1dwj8w2gmsyNt5olknpo9A8WWs17JFwMT6340slYOyWlsUXRx4Mur0 mmyzBz1RnIEURuCejBzIj+K9nYyP5RxFpLf9LFKlRuZQYbnE3mWFLoqp6 VTiVHMoeva0VIlL4PaScKnGoQFw7iHqPrtCTn/i2FbcRGC02LnVfwZaC1 DcnxFBRAYgCkeuTwUzBc9qgWKR/pd1P6gbnWkHB/0CgKbXjJwdnH9eaAQ Q==; X-IronPort-AV: E=Sophos;i="5.51,431,1526313600"; d="scan'208";a="85780136" Received: from h199-255-45-15.hgst.com (HELO uls-op-cesaep02.wdc.com) ([199.255.45.15]) by ob1.hgst.iphmx.com with ESMTP; 01 Aug 2018 16:06:30 +0800 Received: from uls-op-cesaip02.wdc.com ([10.248.3.37]) by uls-op-cesaep02.wdc.com with ESMTP; 01 Aug 2018 00:54:19 -0700 Received: from kfae422988.sdcorp.global.sandisk.com ([10.0.231.37]) by uls-op-cesaip02.wdc.com with ESMTP; 01 Aug 2018 01:06:27 -0700 From: Avri Altman To: Christoph Hellwig , Johannes Thumshirn , Hannes Reinecke , Bart Van Assche , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Cc: Stanislav Nijnikov , Avi Shchislowski , Alex Lemberg , Subhash Jadavani , Vinayak Holikatti , Avri Altman Subject: [PATCH 2/6] scsi: ufs: Add ufs-bsg module Date: Wed, 1 Aug 2018 11:04:28 +0300 Message-Id: <1533110672-12785-3-git-send-email-avri.altman@wdc.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1533110672-12785-1-git-send-email-avri.altman@wdc.com> References: <1533110672-12785-1-git-send-email-avri.altman@wdc.com> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A LLD companion for the ufs scsi transport. For now, just provide an API to instantiate the ufs transport, allocating and removing ufs-ports. As we are attaching the transport template to the scsi host that was already alocated for the ufs hba, we'll need to pay extra attention on where to call the provided API from ufshcd. For the time being, implements an empty bsg_request() - will add some more functionality in coming patches. Nonetheless, we reveal here the protocol we are planning to use: UFS Transport Protocol Transactions. UFS transactions consist of packets called UFS Protocol Information Units (UPIU). There are UPIU’s defined for UFS SCSI commands, responses, data in and data out, task management, utility functions, vendor functions, transaction synchronization and control, and more. By using UPIUs, we get access to the most fine-grained internals of this protocol, and able to communicate with the device in ways, that are sometimes beyond the capacity of the ufs driver. Signed-off-by: Avri Altman --- drivers/scsi/ufs/Makefile | 1 + drivers/scsi/ufs/ufs_bsg.c | 127 +++++++++++++++++++++++++++++++++++++++ drivers/scsi/ufs/ufs_bsg.h | 74 +++++++++++++++++++++++ include/uapi/scsi/scsi_bsg_ufs.h | 56 +++++++++++++++++ 4 files changed, 258 insertions(+) create mode 100644 drivers/scsi/ufs/ufs_bsg.c create mode 100644 drivers/scsi/ufs/ufs_bsg.h create mode 100644 include/uapi/scsi/scsi_bsg_ufs.h diff --git a/drivers/scsi/ufs/Makefile b/drivers/scsi/ufs/Makefile index 2c50f03..5227bfb 100644 --- a/drivers/scsi/ufs/Makefile +++ b/drivers/scsi/ufs/Makefile @@ -8,3 +8,4 @@ ufshcd-core-objs := ufshcd.o ufs-sysfs.o obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o +obj-$(CONFIG_SCSI_UFS_ATTRS) += ufs_bsg.o diff --git a/drivers/scsi/ufs/ufs_bsg.c b/drivers/scsi/ufs/ufs_bsg.c new file mode 100644 index 0000000..71826ba --- /dev/null +++ b/drivers/scsi/ufs/ufs_bsg.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * SSCSI transport companion for UFS HBA + * + * Copyright (C) 2018 Western Digital Corporation + */ +#include "ufs_bsg.h" + +struct ufs_bsg { + struct ufs_hba *hba; + struct ufs_port *port; +}; +static struct ufs_bsg *bsg_host; + +static int ufs_bsg_request(struct bsg_job *job); + +static struct scsi_transport_template *ufs_transport_template; +static struct ufs_function_template ufs_transport_functions = { + .bsg_request = ufs_bsg_request, +}; + +static int ufs_bsg_request(struct bsg_job *job) +{ + struct ufs_bsg_request *bsg_request = job->request; + struct ufs_bsg_reply *bsg_reply = job->reply; + int ret = -ENOTSUPP; + + bsg_reply->reply_payload_rcv_len = 0; + + /* Do Nothing for now */ + dev_err(job->dev, "unsupported message_code 0x%x\n", + bsg_request->msgcode); + + bsg_reply->result = ret; + if (ret) + job->reply_len = sizeof(uint32_t); + else + job->reply_len = sizeof(struct ufs_bsg_reply) + + bsg_reply->reply_payload_rcv_len; + + bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len); + + return ret; +} + +/** + * ufs_bsg_attach_transport - instantiate UFS transport template + * @hba: hba to attach the transport template to + * + * Call this before scsi_add_host() publishes it to the scsi midlayer + */ +void ufs_bsg_attach_transport(struct ufs_hba *hba) +{ + ufs_transport_template = + ufs_attach_transport(&ufs_transport_functions); + if (!ufs_transport_template) + return; + + hba->host->transportt = ufs_transport_template; +} + +/** + * ufs_bsg_remove - detach and remove the added ufs-ports + * @shost: scsi host that is parenting the ufs-ports + * + * Should be called when unloading the driver. + */ +void ufs_bsg_remove(struct Scsi_Host *shost) +{ + if (!bsg_host) + return; + + ufs_remove_host(shost); + + if (ufs_transport_template) { + ufs_release_transport(ufs_transport_template); + ufs_transport_template = NULL; + } + + kfree(bsg_host); +} + +/** + * ufs_bsg_probe - Add and report ufs device to ufs transport + * @hba: per adapter object + * + * Called during initial loading of the driver, and before scsi_scan_host. + * Should be called only if the attach phase was successful + */ + +int ufs_bsg_probe(struct ufs_hba *hba) +{ + struct ufs_port *port; + int ret; + + if (WARN_ON(!ufs_transport_template)) + return -ENODEV; + + bsg_host = kzalloc(sizeof(*bsg_host), GFP_KERNEL); + if (!bsg_host) { + ret = -ENOMEM; + goto out; + } + + port = ufs_port_alloc(hba->host); + if (!port) { + ret = -ENOMEM; + goto out_free_bsg_host; + } + + ret = ufs_port_add(port); + if (ret) + goto out_free_port; + + bsg_host->port = port; + bsg_host->hba = hba; + + return 0; + +out_free_port: + ufs_port_free(port); +out_free_bsg_host: + kfree(bsg_host); +out: + ufs_transport_template = NULL; + return ret; +} diff --git a/drivers/scsi/ufs/ufs_bsg.h b/drivers/scsi/ufs/ufs_bsg.h new file mode 100644 index 0000000..de5511f --- /dev/null +++ b/drivers/scsi/ufs/ufs_bsg.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2018 Western Digital Corporation + */ +#ifndef UFS_BSG_H +#define UFS_BSG_H + +#include +#include +#include + +#include +#include "ufshcd.h" +#include "ufs.h" + +#define UPIU_TRANSACTION_UIC_CMD 0x1F + +enum { + REQ_UPIU_SIZE_DWORDS = 8, + RSP_UPIU_SIZE_DWORDS = 8, +}; + +struct ufs_bsg_request { + uint32_t msgcode; + struct utp_upiu_header header; + union { + struct utp_upiu_query qr; + struct utp_upiu_task_req tr; + /* use utp_upiu_query to host the 4 dwards of uic command */ + struct utp_upiu_query uc; + } tsf; + uint8_t data[0]; +} __packed; + + +struct ufs_bsg_reply { + /* + * The completion result. Result exists in two forms: + * if negative, it is an -Exxx system errno value. There will + * be no further reply information supplied. + * else, it's the 4-byte scsi error result, with driver, host, + * msg and status fields. The per-msgcode reply structure + * will contain valid data. + */ + uint32_t result; + + /* If there was reply_payload, how much was recevied ? */ + uint32_t reply_payload_rcv_len; + + struct utp_upiu_header header; + union { + struct utp_upiu_query qr; + struct utp_upiu_task_rsp tr; + struct utp_upiu_query uc; + } tsf; + uint8_t data[0]; +}; + +struct ufs_bsg_raw_upiu { + struct ufs_bsg_upiu request; + struct ufs_bsg_upiu reply; +}; + +#ifdef CONFIG_SCSI_UFS_ATTRS +void ufs_bsg_attach_transport(struct ufs_hba *hba); +void ufs_bsg_remove(struct Scsi_Host *shost); +int ufs_bsg_probe(struct ufs_hba *hba); +#else +static inline void ufs_bsg_attach_transport(struct ufs_hba *hba) {} +static inline void ufs_bsg_remove(struct Scsi_Host *shost) {} +static inline int ufs_bsg_probe(struct ufs_hba *hba) {return 0; } +#endif + +#endif /* UFS_BSG_H */ diff --git a/include/uapi/scsi/scsi_bsg_ufs.h b/include/uapi/scsi/scsi_bsg_ufs.h new file mode 100644 index 0000000..5fb37be --- /dev/null +++ b/include/uapi/scsi/scsi_bsg_ufs.h @@ -0,0 +1,56 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * UFS Transport SGIO v4 BSG Message Support + * + * Copyright (C) 2018 Western Digital Corporation + */ +#ifndef SCSI_BSG_UFS_H +#define SCSI_BSG_UFS_H + +/* + * This file intended to be included by both kernel and user space + */ + + +/** + * struct ufs_bsg_upiu - upiu transaction structure + * + * @header: upiu header + * @tsf: Transaction Specific Fields + * @data: payload pointer + * + * This structure supports all ufs transaction types per JEDEC UFSv2.1 + * paragraph 10.7 + */ +struct ufs_bsg_upiu { + uint32_t header[3]; + uint32_t tsf[5]; + uint8_t data[0]; +}; + +/* request (CDB) structure of the sg_io_v4 */ +struct ufs_bsg_upiu_request { + uint32_t msgcode; + struct ufs_bsg_upiu upiu; +} __packed; + +/* response (request sense data) structure of the sg_io_v4 */ +struct ufs_bsg_upiu_reply { + /* + * The completion result. Result exists in two forms: + * if negative, it is an -Exxx system errno value. There will + * be no further reply information supplied. + * else, it's the 4-byte scsi error result, with driver, host, + * msg and status fields. The per-msgcode reply structure + * will contain valid data. + */ + uint32_t result; + + /* If there was reply_payload, how much was recevied ? */ + uint32_t reply_payload_rcv_len; + + struct ufs_bsg_upiu upiu; + +}; + +#endif /* SCSI_BSG_UFS_H */