nvme: make pr code more readble
v2 cover nvme: make pr code more readble
Changes from v1 to v2:
- Move the generic definitions to linux/nvme.h and add a separate patch for the changes of linux/nvme.h.
- Define the different CPTPL bits separately.
Guixin Liu (2):
nvme: introduce change ptpl and iekey definition
nvme: tuning pr code by using defined structs and macros
drivers/nvme/host/pr.c | 122 +++++++++++++++++++++++++---------------- include/linux/nvme.h | 9 +++ 2 files changed, 84 insertions(+), 47 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 2/2 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.
--- drivers/nvme/host/pr.c | 122 +++++++++++++++++++++++++---------------- 1 file changed, 75 insertions(+), 47 deletions(-)
drivers/nvme/host/pr.c +75 -47
@@ -94,109 +94,137 @@ static int nvme_status_to_pr_err(int status) } } -static int nvme_send_pr_command (struct block_device *bdev, -struct nvme_command *c,void *data,unsigned int data_len) +static int __nvme_send_pr_command (struct block_device *bdev,u32 cdw10, +u32 cdw11,u8 op,void *data,unsigned int data_len) { -if (nvme_disk_is_ns_head (bdev->bd_disk)) -return nvme_send_ns_head_pr_command (bdev, c, data, data_len); +struct nvme_command c = {0 }; -return nvme_send_ns_pr_command (bdev->bd_disk->private_data, c, data, - data_len); + c.common.opcode = op; + c.common.cdw10 =cpu_to_le32 (cdw10); + c.common.cdw11 =cpu_to_le32 (cdw11); + +if (nvme_disk_is_ns_head (bdev->bd_disk)) +return nvme_send_ns_head_pr_command (bdev, &c, data, data_len); +return nvme_send_ns_pr_command (bdev->bd_disk->private_data, &c, + data, data_len); } -static int nvme_pr_command (struct block_device *bdev,u32 cdw10, -u64 key,u64 sa_key,u8 op) +static int nvme_send_pr_command (struct block_device *bdev,u32 cdw10,u32 cdw11, +u8 op,void *data,unsigned int data_len) { -struct nvme_command c = { }; -u8 data[16 ] = {0 , };int ret; -put_unaligned_le64 (key, &data[0 ]); -put_unaligned_le64 (sa_key, &data[8 ]); - - c.common.opcode = op; - c.common.cdw10 =cpu_to_le32 (cdw10); - - ret =nvme_send_pr_command (bdev, &c, data,sizeof (data)); -if (ret <0 ) -return ret; - -return nvme_status_to_pr_err (ret); + ret =__nvme_send_pr_command (bdev, cdw10, cdw11, op, data, data_len); +return ret <0 ? ret :nvme_status_to_pr_err (ret); } -static int nvme_pr_register (struct block_device *bdev,u64 old, -u64 new,unsigned flags) +static int nvme_pr_register (struct block_device *bdev,u64 old_key,u64 new_key, +unsigned int flags) { +struct nvmet_pr_register_data data = {0 };u32 cdw10;if (flags & ~PR_FL_IGNORE_KEY )return -EOPNOTSUPP ; - cdw10 = old ?2 :0 ; - cdw10 |= (flags &PR_FL_IGNORE_KEY ) ?1 <<3 :0 ; - cdw10 |= (1 <<30 ) | (1 <<31 );/* PTPL=1 */ -return nvme_pr_command (bdev, cdw10, old, new, nvme_cmd_resv_register); + data.crkey =cpu_to_le64 (old_key); + data.nrkey =cpu_to_le64 (new_key); + + cdw10 = old_key ?NVME_PR_REGISTER_ACT_REPLACE : +NVME_PR_REGISTER_ACT_REG ; + cdw10 |= (flags &PR_FL_IGNORE_KEY ) ?NVME_PR_IGNORE_KEY :0 ; + cdw10 |=NVME_PR_CPTPL_PERSIST ; + +return nvme_send_pr_command (bdev, cdw10,0 , nvme_cmd_resv_register, + &data,sizeof (data)); }static int nvme_pr_reserve (struct block_device *bdev,u64 key,enum pr_type type,unsigned flags) { +struct nvmet_pr_acquire_data data = {0 };u32 cdw10;if (flags & ~PR_FL_IGNORE_KEY )return -EOPNOTSUPP ; - cdw10 =nvme_pr_type_from_blk (type) <<8 ; - cdw10 |= ((flags &PR_FL_IGNORE_KEY ) ?1 <<3 :0 ); -return nvme_pr_command (bdev, cdw10, key,0 , nvme_cmd_resv_acquire); + data.crkey =cpu_to_le64 (key); + + cdw10 =NVME_PR_ACQUIRE_ACT_ACQUIRE ; + cdw10 |=nvme_pr_type_from_blk (type) <<8 ; + cdw10 |= (flags &PR_FL_IGNORE_KEY ) ?NVME_PR_IGNORE_KEY :0 ; + +return nvme_send_pr_command (bdev, cdw10,0 , nvme_cmd_resv_acquire, + &data,sizeof (data)); }static int nvme_pr_preempt (struct block_device *bdev,u64 old,u64 new,enum pr_type type,bool abort) { -u32 cdw10 =nvme_pr_type_from_blk (type) <<8 | (abort ?2 :1 ); +struct nvmet_pr_acquire_data data = {0 }; +u32 cdw10; + + data.crkey =cpu_to_le64 (old); + data.prkey =cpu_to_le64 (new); -return nvme_pr_command (bdev, cdw10, old, new, nvme_cmd_resv_acquire); + cdw10 = abort ?NVME_PR_ACQUIRE_ACT_PREEMPT_AND_ABORT : +NVME_PR_ACQUIRE_ACT_PREEMPT ; + cdw10 |=nvme_pr_type_from_blk (type) <<8 ; + +return nvme_send_pr_command (bdev, cdw10,0 , nvme_cmd_resv_acquire, + &data,sizeof (data)); }static int nvme_pr_clear (struct block_device *bdev,u64 key) { -u32 cdw10 =1 | (key ?0 :1 <<3 ); +struct nvmet_pr_release_data data = {0 }; +u32 cdw10; + + data.crkey =cpu_to_le64 (key); -return nvme_pr_command (bdev, cdw10, key,0 , nvme_cmd_resv_release); + cdw10 =NVME_PR_RELEASE_ACT_CLEAR ; + cdw10 |= key ?0 :NVME_PR_IGNORE_KEY ; + +return nvme_send_pr_command (bdev, cdw10,0 , nvme_cmd_resv_release, + &data,sizeof (data)); }static int nvme_pr_release (struct block_device *bdev,u64 key,enum pr_type type) { -u32 cdw10 =nvme_pr_type_from_blk (type) <<8 | (key ?0 :1 <<3 ); +struct nvmet_pr_release_data data = {0 }; +u32 cdw10; + + data.crkey =cpu_to_le64 (key); -return nvme_pr_command (bdev, cdw10, key,0 , nvme_cmd_resv_release); + cdw10 =NVME_PR_RELEASE_ACT_RELEASE ; + cdw10 |=nvme_pr_type_from_blk (type) <<8 ; + cdw10 |= key ?0 :NVME_PR_IGNORE_KEY ; + +return nvme_send_pr_command (bdev, cdw10,0 , nvme_cmd_resv_release, + &data,sizeof (data)); }static int nvme_pr_resv_report (struct block_device *bdev,void *data,u32 data_len,bool *eds) { -struct nvme_command c = { }; +u32 cdw10, cdw11;int ret; - c.common.opcode = nvme_cmd_resv_report; - c.common.cdw10 =cpu_to_le32 (nvme_bytes_to_numd (data_len)); - c.common.cdw11 =cpu_to_le32 (NVME_EXTENDED_DATA_STRUCT ); + cdw10 =nvme_bytes_to_numd (data_len); + cdw11 =NVME_EXTENDED_DATA_STRUCT ; *eds = true; retry: - ret =nvme_send_pr_command (bdev, &c, data, data_len); + ret =__nvme_send_pr_command (bdev, cdw10, cdw11, nvme_cmd_resv_report, + data, data_len);if (ret ==NVME_SC_HOST_ID_INCONSIST && - c.common.cdw11 ==cpu_to_le32 (NVME_EXTENDED_DATA_STRUCT )) { - c.common.cdw11 =0 ; + cdw11 ==NVME_EXTENDED_DATA_STRUCT ) { + cdw11 =0 ; *eds = false; goto retry; } -if (ret <0 ) -return ret; - -return nvme_status_to_pr_err (ret); +return ret <0 ? ret :nvme_status_to_pr_err (ret); } static int nvme_pr_read_keys(struct block_device *bdev,
Signature · 1 line
2.43.0
PATCH v2 1/2 nvme: introduce change ptpl and iekey definition
In reply to Guixin LiuThis is for the next tuning pr code more readble patch, make linux/nvme.h's changes separately.
--- include/linux/nvme.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/nvme.h +9 -0
@@ -2166,4 +2166,13 @@ enum nvme_pr_release_action { NVME_PR_RELEASE_ACT_CLEAR = 1, }; +enum nvme_pr_change_ptpl { +NVME_PR_CPTPL_NO_CHANGE =0 , +NVME_PR_CPTPL_RESV =1 <<30 , +NVME_PR_CPTPL_CLEARED =2 <<30 , +NVME_PR_CPTPL_PERSIST =3 <<30 , +}; + +#define NVME_PR_IGNORE_KEY (1 << 3) +#endif /* _LINUX_NVME_H */
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] nvme: introduce change ptpl and iekey definition
Quoted 2 lines · Guixin Liu
This is for the next tuning pr code more readble patch, make linux/nvme.h's changes separately.
Looks good:
Re: [PATCH v2 2/2] nvme: tuning pr code by using defined structs and macros
In reply to Guixin LiuLooks good:
Quoted 8 lines · Guixin Liu
Changes from v1 to v2:
- Move the generic definitions tolinux/nvme.hand add a separate patch for the changes oflinux/nvme.h.
- Define the different CPTPL bits separately.Guixin Liu (2):
nvme: introduce change ptpl and iekey definition
nvme: tuning pr code by using defined structs and macros
Thanks, applied to nvme-6.13.