linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: scott.bauer@intel.com (Scott Bauer)
Subject: [PATCH v2 3/4] nvme: Implement resume_from_suspend and sed block ioctl
Date: Tue, 29 Nov 2016 14:52:01 -0700	[thread overview]
Message-ID: <1480456322-27339-4-git-send-email-scott.bauer@intel.com> (raw)
In-Reply-To: <1480456322-27339-1-git-send-email-scott.bauer@intel.com>

This patch implements the necessary logic to unlock a SED
enabled device coming back from an S3.

The patch also implements the ioctl handling from the block
layer.

Signed-off-by: Scott Bauer <scott.bauer at intel.com>
Signed-off-by: Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 drivers/nvme/host/core.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h |  4 ++-
 drivers/nvme/host/pci.c  |  7 ++++-
 3 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 79e679d..9a3eb41 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -28,6 +28,8 @@
 #include <linux/t10-pi.h>
 #include <scsi/sg.h>
 #include <asm/unaligned.h>
+#include <linux/sed.h>
+#include <linux/sed-opal.h>
 
 #include "nvme.h"
 #include "fabrics.h"
@@ -778,11 +780,57 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
 	return status;
 }
 
+static int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer,
+			   size_t len, bool send)
+{
+	struct request_queue *q;
+	struct request *req;
+	struct nvme_ns *ns;
+	struct nvme_command cmd = { 0 };
+	int ret;
+
+	ns = data;
+
+	if (send)
+		cmd.common.opcode = (u8)nvme_admin_security_send;
+	else
+		cmd.common.opcode = (u8)nvme_admin_security_recv;
+
+	cmd.common.nsid = ns->ns_id;
+	cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
+	cmd.common.cdw10[1] = cpu_to_le32(len);
+
+	q = ns->ctrl->admin_q;
+
+	req = nvme_alloc_request(q, &cmd, 0, NVME_QID_ANY);
+	if (IS_ERR(req)) {
+		ret = PTR_ERR(req);
+		return ret;
+	}
+
+	req->timeout = ADMIN_TIMEOUT;
+	req->special = NULL;
+
+	if (buffer && len) {
+		ret = blk_rq_map_kern(q, req, buffer, len, GFP_KERNEL);
+		if (ret)
+			goto out;
+	}
+
+	ret = blk_execute_rq(req->q, ns->disk, req, 1);
+ out:
+	blk_mq_free_request(req);
+	return ret;
+}
+
 static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
 		unsigned int cmd, unsigned long arg)
 {
 	struct nvme_ns *ns = bdev->bd_disk->private_data;
 
+	if (is_sed_ioctl(cmd))
+		return blkdev_sed_ioctl(bdev, mode, cmd, arg,
+					ns, nvme_sec_submit);
 	switch (cmd) {
 	case NVME_IOCTL_ID:
 		force_successful_syscall_return();
@@ -1067,6 +1115,34 @@ static const struct pr_ops nvme_pr_ops = {
 	.pr_clear	= nvme_pr_clear,
 };
 
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	struct opal_suspend_unlk ulk = { 0 };
+	struct nvme_ns *ns;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	if (list_empty(&ctrl->namespaces))
+		goto out_no_namespace;
+
+	ulk.submit_data = ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
+	kref_get(&ns->kref);
+
+	mutex_unlock(&ctrl->namespaces_mutex);
+
+	ulk.submit_fn = nvme_sec_submit;
+	ulk.dev = disk_devt(ns->disk);
+
+	if (opal_unlock_from_suspend(&ulk))
+		pr_warn("Failed to unlock one or more locking ranges!\n");
+
+	nvme_put_ns(ns);
+	return;
+
+ out_no_namespace:
+	mutex_unlock(&ctrl->namespaces_mutex);
+}
+EXPORT_SYMBOL_GPL(nvme_unlock_from_suspend);
+
 static const struct block_device_operations nvme_fops = {
 	.owner		= THIS_MODULE,
 	.ioctl		= nvme_ioctl,
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index d47f5a5..ac7e5b1 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -240,7 +240,8 @@ static inline int nvme_error_status(u16 status)
 
 static inline bool nvme_req_needs_retry(struct request *req, u16 status)
 {
-	return !(status & NVME_SC_DNR || blk_noretry_request(req)) &&
+	return !(status & NVME_SC_DNR || status & NVME_SC_ACCESS_DENIED ||
+		 blk_noretry_request(req)) &&
 		(jiffies - req->start_time) < req->timeout &&
 		req->retries < nvme_max_retries;
 }
@@ -259,6 +260,7 @@ int nvme_init_identify(struct nvme_ctrl *ctrl);
 
 void nvme_queue_scan(struct nvme_ctrl *ctrl);
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl);
 
 #define NVME_NR_AERS	1
 void nvme_complete_async_event(struct nvme_ctrl *ctrl,
diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index 5e52034..1a1fc9b 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -43,6 +43,7 @@
 #include <linux/types.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <asm/unaligned.h>
+#include <linux/sed-opal.h>
 
 #include "nvme.h"
 
@@ -1748,10 +1749,11 @@ static void nvme_reset_work(struct work_struct *work)
 {
 	struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
 	int result = -ENODEV;
-
+	bool was_suspend = false;
 	if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))
 		goto out;
 
+	was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
 	/*
 	 * If we're called to reset a live controller first shut it down before
 	 * moving on.
@@ -1779,6 +1781,9 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
+	if (was_suspend)
+		nvme_unlock_from_suspend(&dev->ctrl);
+
 	result = nvme_setup_io_queues(dev);
 	if (result)
 		goto out;
-- 
2.7.4

  parent reply	other threads:[~2016-11-29 21:52 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-29 21:51 [PATCH v2 0/4] SED OPAL Library Scott Bauer
2016-11-29 21:51 ` [PATCH v2 1/4] include: Add definitions for sed Scott Bauer
2016-11-29 21:52 ` [PATCH v2 2/4] block: Add Sed-opal library Scott Bauer
2016-11-30 18:13   ` Keith Busch
2016-11-30 18:09     ` Scott Bauer
2016-12-01  0:50   ` Keith Busch
2016-12-01 10:04     ` Christoph Hellwig
2016-12-01 17:53       ` Scott Bauer
2016-12-01 18:22         ` Keith Busch
2016-12-09 17:45           ` Scott Bauer
2016-12-09 18:30             ` Christoph Hellwig
2016-12-09 18:50               ` Scott Bauer
2016-11-29 21:52 ` Scott Bauer [this message]
2016-12-01  0:50   ` [PATCH v2 3/4] nvme: Implement resume_from_suspend and sed block ioctl Keith Busch
2016-11-29 21:52 ` [PATCH v2 4/4] Maintainers: Add Information for SED Opal library Scott Bauer
2017-02-10 16:46   ` Elliott, Robert (Persistent Memory)
2017-02-10 16:44     ` Scott Bauer
2017-02-11  2:24       ` Elliott, Robert (Persistent Memory)
2017-02-13  8:04       ` hch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1480456322-27339-4-git-send-email-scott.bauer@intel.com \
    --to=scott.bauer@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).