Skip to content

Commit 029cc98

Browse files
guixinliu1995keithbusch
authored andcommitted
nvme: tuning pr code by using defined structs and macros
All the modifications are simply to make the code more readable, and this patch does not include any functional changes. Signed-off-by: Guixin Liu <kanie@linux.alibaba.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Keith Busch <kbusch@kernel.org>
1 parent 7d2f9f8 commit 029cc98

File tree

1 file changed

+75
-47
lines changed
  • drivers/nvme/host

1 file changed

+75
-47
lines changed

drivers/nvme/host/pr.c

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -94,109 +94,137 @@ static int nvme_status_to_pr_err(int status)
9494
}
9595
}
9696

97-
static int nvme_send_pr_command(struct block_device *bdev,
98-
struct nvme_command *c, void *data, unsigned int data_len)
97+
static int __nvme_send_pr_command(struct block_device *bdev, u32 cdw10,
98+
u32 cdw11, u8 op, void *data, unsigned int data_len)
9999
{
100-
if (nvme_disk_is_ns_head(bdev->bd_disk))
101-
return nvme_send_ns_head_pr_command(bdev, c, data, data_len);
100+
struct nvme_command c = { 0 };
102101

103-
return nvme_send_ns_pr_command(bdev->bd_disk->private_data, c, data,
104-
data_len);
102+
c.common.opcode = op;
103+
c.common.cdw10 = cpu_to_le32(cdw10);
104+
c.common.cdw11 = cpu_to_le32(cdw11);
105+
106+
if (nvme_disk_is_ns_head(bdev->bd_disk))
107+
return nvme_send_ns_head_pr_command(bdev, &c, data, data_len);
108+
return nvme_send_ns_pr_command(bdev->bd_disk->private_data, &c,
109+
data, data_len);
105110
}
106111

107-
static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
108-
u64 key, u64 sa_key, u8 op)
112+
static int nvme_send_pr_command(struct block_device *bdev, u32 cdw10, u32 cdw11,
113+
u8 op, void *data, unsigned int data_len)
109114
{
110-
struct nvme_command c = { };
111-
u8 data[16] = { 0, };
112115
int ret;
113116

114-
put_unaligned_le64(key, &data[0]);
115-
put_unaligned_le64(sa_key, &data[8]);
116-
117-
c.common.opcode = op;
118-
c.common.cdw10 = cpu_to_le32(cdw10);
119-
120-
ret = nvme_send_pr_command(bdev, &c, data, sizeof(data));
121-
if (ret < 0)
122-
return ret;
123-
124-
return nvme_status_to_pr_err(ret);
117+
ret = __nvme_send_pr_command(bdev, cdw10, cdw11, op, data, data_len);
118+
return ret < 0 ? ret : nvme_status_to_pr_err(ret);
125119
}
126120

127-
static int nvme_pr_register(struct block_device *bdev, u64 old,
128-
u64 new, unsigned flags)
121+
static int nvme_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
122+
unsigned int flags)
129123
{
124+
struct nvmet_pr_register_data data = { 0 };
130125
u32 cdw10;
131126

132127
if (flags & ~PR_FL_IGNORE_KEY)
133128
return -EOPNOTSUPP;
134129

135-
cdw10 = old ? 2 : 0;
136-
cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
137-
cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
138-
return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
130+
data.crkey = cpu_to_le64(old_key);
131+
data.nrkey = cpu_to_le64(new_key);
132+
133+
cdw10 = old_key ? NVME_PR_REGISTER_ACT_REPLACE :
134+
NVME_PR_REGISTER_ACT_REG;
135+
cdw10 |= (flags & PR_FL_IGNORE_KEY) ? NVME_PR_IGNORE_KEY : 0;
136+
cdw10 |= NVME_PR_CPTPL_PERSIST;
137+
138+
return nvme_send_pr_command(bdev, cdw10, 0, nvme_cmd_resv_register,
139+
&data, sizeof(data));
139140
}
140141

141142
static int nvme_pr_reserve(struct block_device *bdev, u64 key,
142143
enum pr_type type, unsigned flags)
143144
{
145+
struct nvmet_pr_acquire_data data = { 0 };
144146
u32 cdw10;
145147

146148
if (flags & ~PR_FL_IGNORE_KEY)
147149
return -EOPNOTSUPP;
148150

149-
cdw10 = nvme_pr_type_from_blk(type) << 8;
150-
cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
151-
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
151+
data.crkey = cpu_to_le64(key);
152+
153+
cdw10 = NVME_PR_ACQUIRE_ACT_ACQUIRE;
154+
cdw10 |= nvme_pr_type_from_blk(type) << 8;
155+
cdw10 |= (flags & PR_FL_IGNORE_KEY) ? NVME_PR_IGNORE_KEY : 0;
156+
157+
return nvme_send_pr_command(bdev, cdw10, 0, nvme_cmd_resv_acquire,
158+
&data, sizeof(data));
152159
}
153160

154161
static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
155162
enum pr_type type, bool abort)
156163
{
157-
u32 cdw10 = nvme_pr_type_from_blk(type) << 8 | (abort ? 2 : 1);
164+
struct nvmet_pr_acquire_data data = { 0 };
165+
u32 cdw10;
166+
167+
data.crkey = cpu_to_le64(old);
168+
data.prkey = cpu_to_le64(new);
158169

159-
return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
170+
cdw10 = abort ? NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT :
171+
NVME_PR_ACQUIRE_ACT_PREEMPT;
172+
cdw10 |= nvme_pr_type_from_blk(type) << 8;
173+
174+
return nvme_send_pr_command(bdev, cdw10, 0, nvme_cmd_resv_acquire,
175+
&data, sizeof(data));
160176
}
161177

162178
static int nvme_pr_clear(struct block_device *bdev, u64 key)
163179
{
164-
u32 cdw10 = 1 | (key ? 0 : 1 << 3);
180+
struct nvmet_pr_release_data data = { 0 };
181+
u32 cdw10;
182+
183+
data.crkey = cpu_to_le64(key);
165184

166-
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
185+
cdw10 = NVME_PR_RELEASE_ACT_CLEAR;
186+
cdw10 |= key ? 0 : NVME_PR_IGNORE_KEY;
187+
188+
return nvme_send_pr_command(bdev, cdw10, 0, nvme_cmd_resv_release,
189+
&data, sizeof(data));
167190
}
168191

169192
static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
170193
{
171-
u32 cdw10 = nvme_pr_type_from_blk(type) << 8 | (key ? 0 : 1 << 3);
194+
struct nvmet_pr_release_data data = { 0 };
195+
u32 cdw10;
196+
197+
data.crkey = cpu_to_le64(key);
172198

173-
return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
199+
cdw10 = NVME_PR_RELEASE_ACT_RELEASE;
200+
cdw10 |= nvme_pr_type_from_blk(type) << 8;
201+
cdw10 |= key ? 0 : NVME_PR_IGNORE_KEY;
202+
203+
return nvme_send_pr_command(bdev, cdw10, 0, nvme_cmd_resv_release,
204+
&data, sizeof(data));
174205
}
175206

176207
static int nvme_pr_resv_report(struct block_device *bdev, void *data,
177208
u32 data_len, bool *eds)
178209
{
179-
struct nvme_command c = { };
210+
u32 cdw10, cdw11;
180211
int ret;
181212

182-
c.common.opcode = nvme_cmd_resv_report;
183-
c.common.cdw10 = cpu_to_le32(nvme_bytes_to_numd(data_len));
184-
c.common.cdw11 = cpu_to_le32(NVME_EXTENDED_DATA_STRUCT);
213+
cdw10 = nvme_bytes_to_numd(data_len);
214+
cdw11 = NVME_EXTENDED_DATA_STRUCT;
185215
*eds = true;
186216

187217
retry:
188-
ret = nvme_send_pr_command(bdev, &c, data, data_len);
218+
ret = __nvme_send_pr_command(bdev, cdw10, cdw11, nvme_cmd_resv_report,
219+
data, data_len);
189220
if (ret == NVME_SC_HOST_ID_INCONSIST &&
190-
c.common.cdw11 == cpu_to_le32(NVME_EXTENDED_DATA_STRUCT)) {
191-
c.common.cdw11 = 0;
221+
cdw11 == NVME_EXTENDED_DATA_STRUCT) {
222+
cdw11 = 0;
192223
*eds = false;
193224
goto retry;
194225
}
195226

196-
if (ret < 0)
197-
return ret;
198-
199-
return nvme_status_to_pr_err(ret);
227+
return ret < 0 ? ret : nvme_status_to_pr_err(ret);
200228
}
201229

202230
static int nvme_pr_read_keys(struct block_device *bdev,

0 commit comments

Comments
 (0)