Skip to content

Commit df032bf

Browse files
avri-altman-sndkmartinkpetersen
authored andcommitted
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 <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent a851b2b commit df032bf

File tree

8 files changed

+192
-1
lines changed

8 files changed

+192
-1
lines changed

Documentation/scsi/ufs.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ The current UFSHCD implementation supports following functionality,
128128
In this version of UFSHCD Query requests and power management
129129
functionality are not implemented.
130130

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+
131151
UFS Specifications can be found at,
132152
UFS - http://www.jedec.org/sites/default/files/docs/JESD220.pdf
133153
UFSHCI - http://www.jedec.org/sites/default/files/docs/JESD223.pdf

drivers/scsi/ufs/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,22 @@ config SCSI_UFS_HISI
109109

110110
Select this if you have UFS controller on Hisilicon chipset.
111111
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.

drivers/scsi/ufs/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ obj-$(CONFIG_SCSI_UFS_DWC_TC_PCI) += tc-dwc-g210-pci.o ufshcd-dwc.o tc-dwc-g210.
44
obj-$(CONFIG_SCSI_UFS_DWC_TC_PLATFORM) += tc-dwc-g210-pltfrm.o ufshcd-dwc.o tc-dwc-g210.o
55
obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
66
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
89
obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
910
obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
1011
obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o

drivers/scsi/ufs/ufs_bsg.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

drivers/scsi/ufs/ufs_bsg.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 */

drivers/scsi/ufs/ufshcd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "ufs_quirks.h"
4747
#include "unipro.h"
4848
#include "ufs-sysfs.h"
49+
#include "ufs_bsg.h"
4950

5051
#define CREATE_TRACE_POINTS
5152
#include <trace/events/ufs.h>
@@ -6633,6 +6634,8 @@ static int ufshcd_probe_hba(struct ufs_hba *hba)
66336634
hba->clk_scaling.is_allowed = true;
66346635
}
66356636

6637+
ufs_bsg_probe(hba);
6638+
66366639
scsi_scan_host(hba->host);
66376640
pm_runtime_put_sync(hba->dev);
66386641
}
@@ -7854,6 +7857,7 @@ EXPORT_SYMBOL(ufshcd_shutdown);
78547857
*/
78557858
void ufshcd_remove(struct ufs_hba *hba)
78567859
{
7860+
ufs_bsg_remove(hba);
78577861
ufs_sysfs_remove_nodes(hba->dev);
78587862
scsi_remove_host(hba->host);
78597863
/* disable interrupts */

drivers/scsi/ufs/ufshcd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ struct ufs_hba {
702702
struct rw_semaphore clk_scaling_lock;
703703
struct ufs_desc_size desc_size;
704704
atomic_t scsi_block_reqs_cnt;
705+
706+
struct device bsg_dev;
707+
struct request_queue *bsg_queue;
705708
};
706709

707710
/* Returns true if clocks can be gated. Otherwise false */

include/uapi/scsi/scsi_bsg_ufs.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* UFS Transport SGIO v4 BSG Message Support
44
*
55
* Copyright (C) 2011-2013 Samsung India Software Operations
6+
* Copyright (C) 2018 Western Digital Corporation
67
*/
78
#ifndef SCSI_BSG_UFS_H
89
#define SCSI_BSG_UFS_H
@@ -69,6 +70,33 @@ struct utp_upiu_req {
6970
union {
7071
struct utp_upiu_cmd sc;
7172
struct utp_upiu_query qr;
73+
struct utp_upiu_query tr;
74+
/* use utp_upiu_query to host the 4 dwords of uic command */
75+
struct utp_upiu_query uc;
7276
};
7377
};
78+
79+
/* request (CDB) structure of the sg_io_v4 */
80+
struct ufs_bsg_request {
81+
uint32_t msgcode;
82+
struct utp_upiu_req upiu_req;
83+
};
84+
85+
/* response (request sense data) structure of the sg_io_v4 */
86+
struct ufs_bsg_reply {
87+
/*
88+
* The completion result. Result exists in two forms:
89+
* if negative, it is an -Exxx system errno value. There will
90+
* be no further reply information supplied.
91+
* else, it's the 4-byte scsi error result, with driver, host,
92+
* msg and status fields. The per-msgcode reply structure
93+
* will contain valid data.
94+
*/
95+
uint32_t result;
96+
97+
/* If there was reply_payload, how much was received? */
98+
uint32_t reply_payload_rcv_len;
99+
100+
struct utp_upiu_req upiu_rsp;
101+
};
74102
#endif /* UFS_BSG_H */

0 commit comments

Comments
 (0)