Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: ufs: Add a bsg endpoint that supports UPIUs

For now, just provide an API to allocate and remove ufs-bsg node. We
will use this framework to manage ufs devices by sending UPIU
transactions.

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.

Moreover and as a result, our core structure - ufs_bsg_node has a pretty
lean structure: using upiu transactions that contains the outmost
detailed info, so we don't really need complex constructs to support it.

Signed-off-by: Avri Altman <avri.altman@wdc.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Bart Van Assche <Bart.VanAssche@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Avri Altman and committed by
Martin K. Petersen
df032bf2 a851b2bd

+192 -1
+20
Documentation/scsi/ufs.txt
··· 128 128 In this version of UFSHCD Query requests and power management 129 129 functionality are not implemented. 130 130 131 + 4. BSG Support 132 + ------------------ 133 + 134 + This transport driver supports exchanging UFS protocol information units 135 + (UPIUs) with a UFS device. Typically, user space will allocate 136 + struct ufs_bsg_request and struct ufs_bsg_reply (see ufs_bsg.h) as 137 + request_upiu and reply_upiu respectively. Filling those UPIUs should 138 + be done in accordance with JEDEC spec UFS2.1 paragraph 10.7. 139 + *Caveat emptor*: The driver makes no further input validations and sends the 140 + UPIU to the device as it is. Open the bsg device in /dev/ufs-bsg and 141 + send SG_IO with the applicable sg_io_v4: 142 + 143 + io_hdr_v4.guard = 'Q'; 144 + io_hdr_v4.protocol = BSG_PROTOCOL_SCSI; 145 + io_hdr_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_TRANSPORT; 146 + io_hdr_v4.response = (__u64)reply_upiu; 147 + io_hdr_v4.max_response_len = reply_len; 148 + io_hdr_v4.request_len = request_len; 149 + io_hdr_v4.request = (__u64)request_upiu; 150 + 131 151 UFS Specifications can be found at, 132 152 UFS - http://www.jedec.org/sites/default/files/docs/JESD220.pdf 133 153 UFSHCI - http://www.jedec.org/sites/default/files/docs/JESD223.pdf
+19
drivers/scsi/ufs/Kconfig
··· 109 109 110 110 Select this if you have UFS controller on Hisilicon chipset. 111 111 If unsure, say N. 112 + 113 + config SCSI_UFS_BSG 114 + bool "Universal Flash Storage BSG device node" 115 + depends on SCSI_UFSHCD 116 + select BLK_DEV_BSGLIB 117 + help 118 + Universal Flash Storage (UFS) is SCSI transport specification for 119 + accessing flash storage on digital cameras, mobile phones and 120 + consumer electronic devices. 121 + A UFS controller communicates with a UFS device by exchanging 122 + UFS Protocol Information Units (UPIUs). 123 + UPIUs can not only be used as a transport layer for the SCSI protocol 124 + but are also used by the UFS native command set. 125 + This transport driver supports exchanging UFS protocol information units 126 + with a UFS device. See also the ufshcd driver, which is a SCSI driver 127 + that supports UFS devices. 128 + 129 + Select this if you need a bsg device node for your UFS controller. 130 + If unsure, say N.
+2 -1
drivers/scsi/ufs/Makefile
··· 4 4 obj-$(CONFIG_SCSI_UFS_DWC_TC_PLATFORM) += tc-dwc-g210-pltfrm.o ufshcd-dwc.o tc-dwc-g210.o 5 5 obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o 6 6 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o 7 - ufshcd-core-objs := ufshcd.o ufs-sysfs.o 7 + ufshcd-core-y += ufshcd.o ufs-sysfs.o 8 + ufshcd-core-$(CONFIG_SCSI_UFS_BSG) += ufs_bsg.o 8 9 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o 9 10 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o 10 11 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
+93
drivers/scsi/ufs/ufs_bsg.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * bsg endpoint that supports UPIUs 4 + * 5 + * Copyright (C) 2018 Western Digital Corporation 6 + */ 7 + #include "ufs_bsg.h" 8 + 9 + 10 + static int ufs_bsg_request(struct bsg_job *job) 11 + { 12 + struct ufs_bsg_request *bsg_request = job->request; 13 + struct ufs_bsg_reply *bsg_reply = job->reply; 14 + int ret = -ENOTSUPP; 15 + 16 + bsg_reply->reply_payload_rcv_len = 0; 17 + 18 + /* Do Nothing for now */ 19 + dev_err(job->dev, "unsupported message_code 0x%x\n", 20 + bsg_request->msgcode); 21 + 22 + bsg_reply->result = ret; 23 + job->reply_len = sizeof(struct ufs_bsg_reply) + 24 + bsg_reply->reply_payload_rcv_len; 25 + 26 + bsg_job_done(job, ret, bsg_reply->reply_payload_rcv_len); 27 + 28 + return ret; 29 + } 30 + 31 + /** 32 + * ufs_bsg_remove - detach and remove the added ufs-bsg node 33 + * 34 + * Should be called when unloading the driver. 35 + */ 36 + void ufs_bsg_remove(struct ufs_hba *hba) 37 + { 38 + struct device *bsg_dev = &hba->bsg_dev; 39 + 40 + if (!hba->bsg_queue) 41 + return; 42 + 43 + bsg_unregister_queue(hba->bsg_queue); 44 + 45 + device_del(bsg_dev); 46 + put_device(bsg_dev); 47 + } 48 + 49 + static inline void ufs_bsg_node_release(struct device *dev) 50 + { 51 + put_device(dev->parent); 52 + } 53 + 54 + /** 55 + * ufs_bsg_probe - Add ufs bsg device node 56 + * @hba: per adapter object 57 + * 58 + * Called during initial loading of the driver, and before scsi_scan_host. 59 + */ 60 + int ufs_bsg_probe(struct ufs_hba *hba) 61 + { 62 + struct device *bsg_dev = &hba->bsg_dev; 63 + struct Scsi_Host *shost = hba->host; 64 + struct device *parent = &shost->shost_gendev; 65 + struct request_queue *q; 66 + int ret; 67 + 68 + device_initialize(bsg_dev); 69 + 70 + bsg_dev->parent = get_device(parent); 71 + bsg_dev->release = ufs_bsg_node_release; 72 + 73 + dev_set_name(bsg_dev, "ufs-bsg"); 74 + 75 + ret = device_add(bsg_dev); 76 + if (ret) 77 + goto out; 78 + 79 + q = bsg_setup_queue(bsg_dev, dev_name(bsg_dev), ufs_bsg_request, 0); 80 + if (IS_ERR(q)) { 81 + ret = PTR_ERR(q); 82 + goto out; 83 + } 84 + 85 + hba->bsg_queue = q; 86 + 87 + return 0; 88 + 89 + out: 90 + dev_err(bsg_dev, "fail to initialize a bsg dev %d\n", shost->host_no); 91 + put_device(bsg_dev); 92 + return ret; 93 + }
+23
drivers/scsi/ufs/ufs_bsg.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (C) 2018 Western Digital Corporation 4 + */ 5 + #ifndef UFS_BSG_H 6 + #define UFS_BSG_H 7 + 8 + #include <linux/bsg-lib.h> 9 + #include <scsi/scsi.h> 10 + #include <scsi/scsi_host.h> 11 + 12 + #include "ufshcd.h" 13 + #include "ufs.h" 14 + 15 + #ifdef CONFIG_SCSI_UFS_BSG 16 + void ufs_bsg_remove(struct ufs_hba *hba); 17 + int ufs_bsg_probe(struct ufs_hba *hba); 18 + #else 19 + static inline void ufs_bsg_remove(struct ufs_hba *hba) {} 20 + static inline int ufs_bsg_probe(struct ufs_hba *hba) {return 0; } 21 + #endif 22 + 23 + #endif /* UFS_BSG_H */
+4
drivers/scsi/ufs/ufshcd.c
··· 46 46 #include "ufs_quirks.h" 47 47 #include "unipro.h" 48 48 #include "ufs-sysfs.h" 49 + #include "ufs_bsg.h" 49 50 50 51 #define CREATE_TRACE_POINTS 51 52 #include <trace/events/ufs.h> ··· 6634 6633 hba->clk_scaling.is_allowed = true; 6635 6634 } 6636 6635 6636 + ufs_bsg_probe(hba); 6637 + 6637 6638 scsi_scan_host(hba->host); 6638 6639 pm_runtime_put_sync(hba->dev); 6639 6640 } ··· 7857 7854 */ 7858 7855 void ufshcd_remove(struct ufs_hba *hba) 7859 7856 { 7857 + ufs_bsg_remove(hba); 7860 7858 ufs_sysfs_remove_nodes(hba->dev); 7861 7859 scsi_remove_host(hba->host); 7862 7860 /* disable interrupts */
+3
drivers/scsi/ufs/ufshcd.h
··· 702 702 struct rw_semaphore clk_scaling_lock; 703 703 struct ufs_desc_size desc_size; 704 704 atomic_t scsi_block_reqs_cnt; 705 + 706 + struct device bsg_dev; 707 + struct request_queue *bsg_queue; 705 708 }; 706 709 707 710 /* Returns true if clocks can be gated. Otherwise false */
+28
include/uapi/scsi/scsi_bsg_ufs.h
··· 3 3 * UFS Transport SGIO v4 BSG Message Support 4 4 * 5 5 * Copyright (C) 2011-2013 Samsung India Software Operations 6 + * Copyright (C) 2018 Western Digital Corporation 6 7 */ 7 8 #ifndef SCSI_BSG_UFS_H 8 9 #define SCSI_BSG_UFS_H ··· 70 69 union { 71 70 struct utp_upiu_cmd sc; 72 71 struct utp_upiu_query qr; 72 + struct utp_upiu_query tr; 73 + /* use utp_upiu_query to host the 4 dwords of uic command */ 74 + struct utp_upiu_query uc; 73 75 }; 76 + }; 77 + 78 + /* request (CDB) structure of the sg_io_v4 */ 79 + struct ufs_bsg_request { 80 + uint32_t msgcode; 81 + struct utp_upiu_req upiu_req; 82 + }; 83 + 84 + /* response (request sense data) structure of the sg_io_v4 */ 85 + struct ufs_bsg_reply { 86 + /* 87 + * The completion result. Result exists in two forms: 88 + * if negative, it is an -Exxx system errno value. There will 89 + * be no further reply information supplied. 90 + * else, it's the 4-byte scsi error result, with driver, host, 91 + * msg and status fields. The per-msgcode reply structure 92 + * will contain valid data. 93 + */ 94 + uint32_t result; 95 + 96 + /* If there was reply_payload, how much was received? */ 97 + uint32_t reply_payload_rcv_len; 98 + 99 + struct utp_upiu_req upiu_rsp; 74 100 }; 75 101 #endif /* UFS_BSG_H */