[PATCH v2 11/20] nvme: Add pr_ops read_reservation support

Mike Christie
2022-08-09T00:07:04Z

This patch adds support for the pr_ops read_reservation callout by
calling the NVMe Reservation Report helper. It then parses that info to
detect if there is a reservation and if there is then convert the
returned info to a pr_ops pr_held_reservation struct.

Signed-off-by: Mike Christie <michael.christie@oracle.com>
---
drivers/nvme/host/core.c | 68 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 230e5deca391..5bbc1d84a87e 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2267,6 +2267,73 @@ static int nvme_pr_read_keys(struct block_device *bdev,
return ret;
}

+static int nvme_pr_read_reservation(struct block_device *bdev,
+ struct pr_held_reservation *resv)
+{
+ struct nvme_reservation_status tmp_status, *status;
+ int ret, i, num_regs;
+ u32 data_len;
+ bool eds;
+ u8 *data;
+
+ memset(resv, 0, sizeof(*resv));
+
+retry:
+ /*
+ * Get the number of registrations so we know how big to allocate
+ * the response buffer.
+ */
+ ret = nvme_pr_resv_report(bdev, (u8 *)&tmp_status, sizeof(tmp_status),
+ &eds);
+ if (ret)
+ return 0;
+
+ num_regs = get_unaligned_le16(&tmp_status.regctl);
+ if (!num_regs) {
+ resv->generation = le32_to_cpu(tmp_status.gen);
+ return 0;
+ }
+
+ data_len = sizeof(*status) +
+ num_regs * sizeof(struct nvme_registered_ctrl_ext);
+ data = kzalloc(data_len, GFP_KERNEL);
+ if (!data)
+ return -ENOMEM;
+
+ ret = nvme_pr_resv_report(bdev, data, data_len, &eds);
+ if (ret)
+ goto free_data;
+ status = (struct nvme_reservation_status *)data;
+
+ if (num_regs != get_unaligned_le16(&status->regctl)) {
+ kfree(data);
+ goto retry;
+ }
+
+ resv->generation = le32_to_cpu(status->gen);
+ resv->type = block_pr_type(status->rtype);
+
+ for (i = 0; i < num_regs; i++) {
+ if (eds) {
+ if (status->regctl_eds[i].rcsts) {
+ resv->key =
+ le64_to_cpu(status->regctl_eds[i].rkey);
+ break;
+ }
+ } else {
+ if (status->regctl_ds[i].rcsts) {
+ resv->key =
+ le64_to_cpu(status->regctl_ds[i].rkey);
+ break;
+ }
+ }
+ }
+
+free_data:
+ kfree(data);
+ return ret;
+}
+
const struct pr_ops nvme_pr_ops = {
.pr_register = nvme_pr_register,
.pr_reserve = nvme_pr_reserve,
@@ -2274,6 +2341,7 @@ const struct pr_ops nvme_pr_ops = {
.pr_preempt = nvme_pr_preempt,
.pr_clear = nvme_pr_clear,
.pr_read_keys = nvme_pr_read_keys,
+ .pr_read_reservation = nvme_pr_read_reservation,
};

#ifdef CONFIG_BLK_SED_OPAL
--
2.18.2