Skip to contentSkip to conversation

SED OPAL Library

PATCH Superseded Scott Bauer 2 participants 11 messages Jan 31, 2017 – Feb 3, 2017

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover SED OPAL Library

Changes from v4->v5:
0) Fixed single user mode Locking Range activation.
0a) You can now activate mutltiple SUM ranges. This required a small refactoring

     of the userland ABI and thus the tooling has changed which is available here:
     https://github.com/ScottyBauer/sed-opal-temp
     For those who want to audit the new userland->kernel entry see the
     sed-opal.c:2230:opal_activate_lsp

1) Removed the sed_context and merged it into struct opal_dev
1a) We now pass around opal_devs
2) Removed some more "dead code" that wasn't really being used and didn't have any potential use in the future.
3) Moved the sed_context -- now the opal_dev from one per namespace to one per controller. Once Multi namespace locking comes around we can switch back. This means there is a opal_dev structure in the nvme_ctrl structure.
4) Per some discussion we will now pass a namespace id of 0 to sec_send/sec_recv until either a TCG errata or NVMe errata is available.
5) Removed the BIT/GENMASK stuff from opal_proto and replaced with hex.
6) Moved variable declarations from opal_proto.h to sed-opal.c
7) Removed include/linux/sed.h -- no longer needed
9) Removed sed-ioctl.c and pushed the ioctls into the bottom sed-opal.c
8) Refactored some code in opal_proto.h to be __be types.
10) Did *not* remove the save_discovery bool from opal dev as it's needed
11) did some small refactoring around unlock and unlock_sum there are now "private" helper functions __opal_lock_unlock_sum and __opal_lock_unlock which allow us to not have the function table in the global scope, but on the stack.
12) nvme opal support is only dependent on the config in block/Kconfig now

===============================================================================
This Patch series implements a large portion of the Opal protocol for self encrypting devices. The driver has the capability of storing a locking range's password. The password can then be replayed during a resume from previous suspend-to-RAM.

The driver also supports logic to bring the device out of a factory default-inactive state into a functional Opal state.

The following logic is supported in order to bring the tper into a working state:

1) Taking Ownership of the drive (Setting the Admin CPIN).
2) Activating the Locking SP (In Single User Mode or Normal Mode).
3) Setting up Locking Ranges (Single User or Normal Mode).
4) Adding users to Locking Ranges (Normal Mode Only).
5) Locking or Unlocking Locking Ranges (Single User Mode or Normal Mode).
6) Reverting the TPer (Restore to factory default).
7) Setting LR/User passwords (Single User Mode or Normal Mode).
8) Enabling/disabling Shadow MBR.
9) Enabling Users in the LockingSP (Normal Mode Only).
10) Saving Password for resume from suspend.
11) Erase and Secure erasing locking ranges.

Scott Bauer (4):
Include: Uapi: Add user ABI for Sed/Opal
block: Add Sed-opal library
nvme: Add Support for Opal: Unlock from S3 & Opal Allocation/Ioctls
Maintainers: Add maintainer info for SED/Opal code

 MAINTAINERS                   |   11 +
 block/Kconfig                 |    9 +
 block/Makefile                |    1 +
 block/opal_proto.h            |  434 ++++++++
 block/sed-opal.c              | 2476 +++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/core.c      |   50 +
 drivers/nvme/host/nvme.h      |   20 +-
 drivers/nvme/host/pci.c       |    8 +
 include/linux/sed-opal.h      |  181 +++
 include/uapi/linux/sed-opal.h |  115 ++
 10 files changed, 3304 insertions(+), 1 deletion(-)
 create mode 100644 block/opal_proto.h
 create mode 100644 block/sed-opal.c
 create mode 100644 include/linux/sed-opal.h
 create mode 100644 include/uapi/linux/sed-opal.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH v5 1/4 Include: Uapi: Add user ABI for Sed/Opal

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 include/uapi/linux/sed-opal.h | 115 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 include/uapi/linux/sed-opal.h
include/uapi/linux/sed-opal.h new file +115 -0
new file mode 100644
index 0000000..723ad89
@@ -0,0 +1,115 @@
+/*
+ * Copyright ? 2016 Intel Corporation
+ *
+ * Authors:
+ *    Rafael Antognolli <rafael.antognolli at intel.com>
+ *    Scott  Bauer      <scott.bauer at intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _UAPI_SED_OPAL_H
+#define _UAPI_SED_OPAL_H
+
+#include <linux/types.h>
+
+#define OPAL_KEY_MAX 256
+#define OPAL_MAX_LRS 9
+
+enum opal_mbr {
+	OPAL_MBR_ENABLE,
+	OPAL_MBR_DISABLE,
+};
+
+enum opal_user {
+	OPAL_ADMIN1,
+	OPAL_USER1,
+	OPAL_USER2,
+	OPAL_USER3,
+	OPAL_USER4,
+	OPAL_USER5,
+	OPAL_USER6,
+	OPAL_USER7,
+	OPAL_USER8,
+	OPAL_USER9,
+};
+
+enum opal_lock_state {
+	OPAL_RO = 0x01, /* 0001 */
+	OPAL_RW = 0x02, /* 0010 */
+	OPAL_LK = 0x04, /* 0100 */
+};
+
+struct opal_key {
+	uint8_t	lr;
+	uint8_t	key_len;
+	char 	key[OPAL_KEY_MAX];
+};
+
+struct opal_lr_act {
+	int sum;
+	uint8_t num_lrs;
+	uint8_t lr[OPAL_MAX_LRS];
+	struct opal_key key;
+};
+
+struct opal_session_info {
+	int sum;
+	struct opal_key opal_key;
+	enum opal_user who;
+};
+
+struct opal_user_lr_setup {
+	struct opal_session_info session;
+	size_t range_start;
+	size_t range_length;
+	int    RLE; /* Read Lock enabled */
+	int    WLE; /* Write Lock Enabled */
+};
+
+struct opal_lock_unlock {
+	struct opal_session_info session;
+	enum opal_lock_state l_state;
+};
+
+struct opal_new_pw {
+	struct opal_session_info session;
+
+	/* When we're not operating in sum, and we first set
+	 * passwords we need to set them via ADMIN authority.
+	 * After passwords are changed, we can set them via,
+	 * User authorities.
+	 * Because of this restriction we need to know about
+	 * Two different users. One in 'session' which we will use
+	 * to start the session and new_userr_pw as the user we're
+	 * chaning the pw for.
+	 */
+	struct opal_session_info new_user_pw;
+};
+
+struct opal_mbr_data {
+	u8 enable_disable;
+	struct opal_key key;
+};
+
+#define IOC_OPAL_SAVE		    _IOW('p', 220, struct opal_lock_unlock)
+#define IOC_OPAL_LOCK_UNLOCK	    _IOW('p', 221, struct opal_lock_unlock)
+#define IOC_OPAL_TAKE_OWNERSHIP	    _IOW('p', 222, struct opal_key)
+#define IOC_OPAL_ACTIVATE_LSP       _IOW('p', 223, struct opal_key)
+#define IOC_OPAL_SET_PW             _IOW('p', 224, struct opal_new_pw)
+#define IOC_OPAL_ACTIVATE_USR       _IOW('p', 225, struct opal_session_info)
+#define IOC_OPAL_REVERT_TPR         _IOW('p', 226, struct opal_key)
+#define IOC_OPAL_LR_SETUP           _IOW('p', 227, struct opal_user_lr_setup)
+#define IOC_OPAL_ADD_USR_TO_LR      _IOW('p', 228, struct opal_lock_unlock)
+#define IOC_OPAL_ENABLE_DISABLE_MBR _IOW('p', 229, struct opal_mbr_data)
+#define IOC_OPAL_ERASE_LR           _IOW('p', 230, struct opal_session_info)
+#define IOC_OPAL_SECURE_ERASE_LR    _IOW('p', 231, struct opal_session_info)
+
+#endif /* _UAPI_SED_OPAL_H */
Signature · 1 line
2.7.4
#2 lore raw

PATCH v5 1/4 Include: Uapi: Add user ABI for Sed/Opal

Quoted 31 lines · Scott Bauer
Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 include/uapi/linux/sed-opal.h | 115 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
 create mode 100644 include/uapi/linux/sed-opal.h
include/uapi/linux/sed-opal.h new file +18 -0
new file mode 100644
index 0000000..723ad89
@@ -0,0 +1,115 @@
+/*
+ * Copyright ? 2016 Intel Corporation
+ *
+ * Authors:
+ *    Rafael Antognolli <rafael.antognolli at intel.com>
+ *    Scott  Bauer      <scott.bauer at intel.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _UAPI_SED_OPAL_H

include/uapi/linux/sed-opal.h line 28

Quoted 10 lines
include/uapi/linux/sed-opal.h new file +10 -0
+#define _UAPI_SED_OPAL_H
+
+#include <linux/types.h>
+
+#define OPAL_KEY_MAX 256
+#define OPAL_MAX_LRS 9
+
+enum opal_mbr {
+	OPAL_MBR_ENABLE,
+	OPAL_MBR_DISABLE,

For all user ABIs please always use explicitly assigned values. There is a religious fight going on if these should be enmums or #defines, but at least I don't care about that part.

Quoted 6 lines
include/uapi/linux/sed-opal.h new file +6 -0
+	OPAL_RW = 0x02, /* 0010 */
+	OPAL_LK = 0x04, /* 0100 */
+};
+
+struct opal_key {
+	uint8_t	lr;

include/uapi/linux/sed-opal.h line 61

Quoted 10 lines
include/uapi/linux/sed-opal.h new file +10 -0
+	uint8_t	key_len;
+	char 	key[OPAL_KEY_MAX];
+};
+
+struct opal_lr_act {
+	int sum;
+	uint8_t num_lrs;
+	uint8_t lr[OPAL_MAX_LRS];
+	struct opal_key key;
+};

Can you go over all theses types and make sure that the structs are nicely aligned and all holes in the layout are explicitly padded with reserved values?

#3 lore raw

PATCH v5 2/4 block: Add Sed-opal library

In reply to Scott Bauer

This patch implements the necessary logic to bring an Opal enabled drive out of a factory-enabled into a working Opal state.

This patch set also enables logic to save a password to be replayed during a resume from suspend.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
Signed-off-by Rafael Antognolli <Rafael.Antognolli at intel.com>
---
 block/Kconfig            |    9 +
 block/Makefile           |    1 +
 block/opal_proto.h       |  434 ++++++++
 block/sed-opal.c         | 2476 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/sed-opal.h |  181 ++++
 5 files changed, 3101 insertions(+)
 create mode 100644 block/opal_proto.h
 create mode 100644 block/sed-opal.c
 create mode 100644 include/linux/sed-opal.h
block/Kconfig +9 -0
index 8bf114a..107f244 100644
@@ -147,6 +147,15 @@ config BLK_WBT_MQ
 	Multiqueue currently doesn't have support for IO scheduling,
 	enabling this option is recommended.
 
+config BLK_SED_OPAL
+        bool "Logic for interfacing with Opal enabled SEDs"
+	default n
+	---help---
+	Builds Logic for interfacing with Opal enabled controllers.
+	Enabling this option enables users to setup/unlock/lock
+	Locking ranges for SED devices using the Opal protocol.
+	Currently enables support on NVMe devices.
+
 menu "Partition Types"
 
 source "block/partitions/Kconfig"
block/Makefile +1 -0
index a827f98..e8bfd51 100644
@@ -25,3 +25,4 @@ obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o
 obj-$(CONFIG_BLK_MQ_PCI)	+= blk-mq-pci.o
 obj-$(CONFIG_BLK_DEV_ZONED)	+= blk-zoned.o
 obj-$(CONFIG_BLK_WBT)		+= blk-wbt.o
+obj-$(CONFIG_BLK_SED_OPAL)	+= sed-opal.o
block/opal_proto.h new file +434 -0

434 lines not on this page · raw message

block/sed-opal.c new file +2476 -0

2,476 lines not on this page · raw message

include/linux/sed-opal.h new file +181 -0

71 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.7.4

Part of the diff is not shown here. View the raw message.

#4 lore raw

PATCH v5 2/4 block: Add Sed-opal library

block/Kconfig config BLK_WBT_MQ · line 151

Quoted 2 lines
block/Kconfig +2 -0
+config BLK_SED_OPAL
+        bool "Logic for interfacing with Opal enabled SEDs"

switch to tabe for indendtation for the above line, all others already get it right.

block/Kconfig config BLK_WBT_MQ · line 153

Quoted 2 lines
block/Kconfig +2 -0
+	default n
+	---help---

n is the default default, you can just remove it.

block/Kconfig config BLK_WBT_MQ · line 157

Quoted 4 lines
block/Kconfig +4 -0
+	Builds Logic for interfacing with Opal enabled controllers.
+	Enabling this option enables users to setup/unlock/lock
+	Locking ranges for SED devices using the Opal protocol.
+	Currently enables support on NVMe devices.

I'd drop that last line, it's just guaranteed to get stale quickly.

block/opal_proto.h line 68

Quoted 2 lines
block/opal_proto.h new file +2 -0
+/* Enum to index OPALUID array */
+enum OPAL_UID {

Can you switch all the type names to lower case, e.g. opal_uid for this one. I though I rought this up earlier, but it might have gotten lost between all the more important comments.

Note this only applies to the enum and struct type names, not the constants defined in the enum, those are usually all upper case by convention as in your code.

Also the arrays might want to use named initalizers to make the relation to the indices more clear, e.g.

	[OPAL_SMUID_UID] =
		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
	...

It's a bit more verbose, but also more future-proof. And it probably allows you to drop the comments explaining each array member.

Quoted 4 lines

+ * Structures to build and decode the Opal SSC messages
+ * fields that are NOT really numeric are defined as u8[] to
+ * help reduce the endianness issues
+ */

That's normal in all on disk / protocol defintions. I personally would just drop this comment, but it's not really a major issue.

Quoted 1 line

+static const u8 OPALUID[][8] = {

Variable names should be lower cases, too. Also the magic 8 here is OPAL_UID_LENGTH, isn't it?

Quoted 1 line

+struct opal_dev;

We don't need the forward delcaration here, do we?

block/sed-opal.c line 349

Quoted 9 lines
block/sed-opal.c new file +9 -0
+{
+	bool found_com_id = false, supported = true, single_user = false;
+	const struct d0_header *hdr;
+	const u8 *epos, *cpos;
+	u16 comid = 0;
+
+	epos = dev->resp;
+	cpos = dev->resp;
+	hdr = (struct d0_header *)dev->resp;

Shouldn't need the cast, and could be assigned in the variable declaration line. The same thing appears in various other functions below.

Quoted 4 lines

+ * struct opal_dev - The structure representing a OPAL enabled SED.
+ * @supported: Whether or not OPAL is supported on this controller.
+ * @send_recv: The combined sec_send/sec_recv function pointer.
+ * @opal_step: A series of opal methods that are necessary to complete a command.

Quoted 8 lines

+ * @func_data: An array of parameters for the opal methods above.
+ * @state: Describes the current opal_step we're working on.
+ * @dev_lock: Locks the entire opal_dev structure.
+ * @parsed: Parsed response from controller.
+ * @prev_data: Data returned from a method to the controller.
+ * @unlk_lst: A list of Locking ranges to unlock on this device during a resume.
+ */
+struct opal_dev {

It seems like the driver only needs the struct defintion for looking at the initialized and supported flags, as well as to use container_of to get back to containing structure.

I wonder if it might be better to keep the defintion private after all (and I probably suggested something else earlier, but so much changed since then).

I suggest you keep it as-is for now and I'll look into an incremental patch to change it on top yours to make sure the code goes in this window.

include/linux/sed-opal.h line 134

Quoted 2 lines
include/linux/sed-opal.h new file +2 -0
+int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk);
+int opal_lock_unlock(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk);

...

These don't seem to be used anywhere outside of sed-opal.c, so please drop the prototypes and mark the functions static.

Review

Quoted 2 lines
(quoted diff) +2 -0
+#ifdef CONFIG_BLK_SED_OPAL
+static inline int is_sed_ioctl(unsigned int cmd)

This should return a bool value.

#5 lore raw

PATCH v5 2/4 block: Add Sed-opal library

Quoted 56 lines · Christoph Hellwig

block/Kconfig config BLK_WBT_MQ · line 151

Quoted 2 lines
block/Kconfig +2 -0
+config BLK_SED_OPAL
+        bool "Logic for interfacing with Opal enabled SEDs"

switch to tabe for indendtation for the above line, all others already get it right.

block/Kconfig config BLK_WBT_MQ · line 153

Quoted 2 lines
block/Kconfig +2 -0
+	default n
+	---help---

n is the default default, you can just remove it.

block/Kconfig config BLK_WBT_MQ · line 157

Quoted 4 lines
block/Kconfig +4 -0
+	Builds Logic for interfacing with Opal enabled controllers.
+	Enabling this option enables users to setup/unlock/lock
+	Locking ranges for SED devices using the Opal protocol.
+	Currently enables support on NVMe devices.

I'd drop that last line, it's just guaranteed to get stale quickly.

block/opal_proto.h line 68

Quoted 2 lines
block/opal_proto.h new file +2 -0
+/* Enum to index OPALUID array */
+enum OPAL_UID {

Can you switch all the type names to lower case, e.g. opal_uid for this one. I though I rought this up earlier, but it might have gotten lost between all the more important comments.

Note this only applies to the enum and struct type names, not the constants defined in the enum, those are usually all upper case by convention as in your code.

Also the arrays might want to use named initalizers to make the relation to the indices more clear, e.g.

	[OPAL_SMUID_UID] =
		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
	...

It's a bit more verbose, but also more future-proof. And it probably allows you to drop the comments explaining each array member.

Quoted 4 lines

+ * Structures to build and decode the Opal SSC messages
+ * fields that are NOT really numeric are defined as u8[] to
+ * help reduce the endianness issues
+ */

That's normal in all on disk / protocol defintions. I personally would just drop this comment, but it's not really a major issue.

Quoted 1 line

+static const u8 OPALUID[][8] = {

Variable names should be lower cases, too. Also the magic 8 here is OPAL_UID_LENGTH, isn't it?

Quoted 1 line

+struct opal_dev;

We don't need the forward delcaration here, do we?

Quoted 13 lines

block/sed-opal.c line 349

Quoted 9 lines
block/sed-opal.c new file +9 -0
+{
+	bool found_com_id = false, supported = true, single_user = false;
+	const struct d0_header *hdr;
+	const u8 *epos, *cpos;
+	u16 comid = 0;
+
+	epos = dev->resp;
+	cpos = dev->resp;
+	hdr = (struct d0_header *)dev->resp;

Shouldn't need the cast, and could be assigned in the variable declaration line. The same thing appears in various other functions below.

Cast has to stay, resp isn't a void*. Resp could be a void * but dev->cmd needs to stay a u8 * since we do a lot of inline dev->cmd[idx] = something. We can change it after it lands.

#6 lore raw

PATCH v5 3/4 nvme: Add Support for Opal: Unlock from S3 & Opal Allocation/Ioctls

In reply to Scott Bauer

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

The patch also implements the SED/Opal allocation necessary to support the opal ioctls.

Signed-off-by Scott Bauer <scott.bauer at intel.com>
---
 drivers/nvme/host/core.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h | 20 ++++++++++++++++++-
 drivers/nvme/host/pci.c  |  8 ++++++++
 3 files changed, 77 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +50 -0
index 8a3c3e3..d0ab750 100644
@@ -28,6 +28,7 @@
 #include <linux/t10-pi.h>
 #include <scsi/sg.h>
 #include <asm/unaligned.h>
+#include <linux/sed-opal.h>
 
 #include "nvme.h"
 #include "fabrics.h"
@@ -784,6 +785,8 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
 		return nvme_sg_io(ns, (void __user *)arg);
 #endif
 	default:
+		if (is_sed_ioctl(cmd))
+			return sed_ioctl(&ns->ctrl->opal_dev, cmd, arg);
 		return -ENOTTY;
 	}
 }
@@ -1051,6 +1054,53 @@ static const struct pr_ops nvme_pr_ops = {
 	.pr_clear	= nvme_pr_clear,
 };
 
+#ifdef CONFIG_BLK_SED_OPAL
+static int nvme_sec_submit(struct opal_dev *dev, u16 spsp, u8 secp,
+			   void *buffer, size_t len, bool send)
+{
+	struct nvme_command cmd;
+	struct nvme_ctrl *ctrl = NULL;
+
+	memset(&cmd, 0, sizeof(cmd));
+	if (send)
+		cmd.common.opcode = nvme_admin_security_send;
+	else
+		cmd.common.opcode = nvme_admin_security_recv;
+	ctrl = container_of(dev, struct nvme_ctrl, opal_dev);
+	cmd.common.nsid = 0;
+	cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
+	cmd.common.cdw10[1] = cpu_to_le32(len);
+
+	return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
+				      ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
+}
+
+int nvme_opal_initialize(struct nvme_ctrl *ctrl)
+{
+	/* Opal dev has already been initialized for this controller */
+	if (ctrl->opal_dev.initialized)
+		return 0;
+	init_opal_dev(&ctrl->opal_dev, &nvme_sec_submit);
+
+	if (check_opal_support(&ctrl->opal_dev, NULL, NULL) < 0)
+		dev_warn(ctrl->device, "Opal is not supported\n");
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(nvme_opal_initialize);
+
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	if (!ctrl->opal_dev.supported)
+		return;
+	if (opal_unlock_from_suspend(&ctrl->opal_dev))
+		dev_warn(ctrl->dev, "Failed to unlock one or more locking ranges!\n");
+
+}
+EXPORT_SYMBOL_GPL(nvme_unlock_from_suspend);
+
+#endif /* CONFIG_BLK_SED_OPAL */
+
 static const struct block_device_operations nvme_fops = {
 	.owner		= THIS_MODULE,
 	.ioctl		= nvme_ioctl,
drivers/nvme/host/nvme.h +19 -1
index aead6d0..f6de604 100644
@@ -19,6 +19,7 @@
 #include <linux/kref.h>
 #include <linux/blk-mq.h>
 #include <linux/lightnvm.h>
+#include <linux/sed-opal.h>
 
 enum {
 	/*
@@ -125,6 +126,8 @@ struct nvme_ctrl {
 	struct list_head node;
 	struct ida ns_ida;
 
+	struct opal_dev opal_dev;
+
 	char name[12];
 	char serial[20];
 	char model[40];
@@ -247,7 +250,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;
 }
@@ -267,6 +271,20 @@ int nvme_init_identify(struct nvme_ctrl *ctrl);
 void nvme_queue_scan(struct nvme_ctrl *ctrl);
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
 
+#ifdef CONFIG_BLK_SED_OPAL
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl);
+int nvme_opal_initialize(struct nvme_ctrl *ctrl);
+#else
+static inline void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	return;
+}
+static inline int nvme_opal_initialize(struct nvme_ctrl *ctrl)
+{
+	return 0;
+}
+#endif /* CONFIG_BLK_DEV_SED_OPAL */
+
 #define NVME_NR_AERS	1
 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
 		union nvme_result *res);
drivers/nvme/host/pci.c +8 -0
index 3faefab..f7cf9eb 100644
@@ -1754,6 +1754,7 @@ static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
 static void nvme_reset_work(struct work_struct *work)
 {
 	struct nvme_dev *dev = container_of(work, struct nvme_dev, reset_work);
+	bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
 	int result = -ENODEV;
 
 	if (WARN_ON(dev->ctrl.state == NVME_CTRL_RESETTING))
@@ -1786,6 +1787,13 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
+	result = nvme_opal_initialize(&dev->ctrl);
+	if (result)
+		goto out;
+
+	if (was_suspend)
+		nvme_unlock_from_suspend(&dev->ctrl);
+
 	result = nvme_setup_io_queues(dev);
 	if (result)
 		goto out;
Signature · 1 line
2.7.4
#7 lore raw

PATCH v5 3/4 nvme: Add Support for Opal: Unlock from S3 & Opal Allocation/Ioctls

drivers/nvme/host/core.c static const struct pr_ops nvme_pr_ops = { · line 1089

Quoted 12 lines
drivers/nvme/host/core.c +12 -0
+int nvme_opal_initialize(struct nvme_ctrl *ctrl)
+{
+	/* Opal dev has already been initialized for this controller */
+	if (ctrl->opal_dev.initialized)
+		return 0;
+	init_opal_dev(&ctrl->opal_dev, &nvme_sec_submit);
+
+	if (check_opal_support(&ctrl->opal_dev, NULL, NULL) < 0)
+		dev_warn(ctrl->device, "Opal is not supported\n");
+
+	return 0;
+}

Can you move the initialized and supported checks into init_opal_dev?

That way we can remove this function entirely.

drivers/nvme/host/core.c static const struct pr_ops nvme_pr_ops = { · line 1097

Quoted 6 lines
drivers/nvme/host/core.c +6 -0
+void nvme_unlock_from_suspend(struct nvme_ctrl *ctrl)
+{
+	if (!ctrl->opal_dev.supported)
+		return;
+	if (opal_unlock_from_suspend(&ctrl->opal_dev))
+		dev_warn(ctrl->dev, "Failed to unlock one or more locking ranges!\n");

Similarly I think the supported check should simply go into opal_unlock_from_suspend, and the nvme-pci driver could call opal_unlock_from_suspend directly.

#8 lore raw

PATCH v5 4/4 Maintainers: Add maintainer info for SED/Opal code

In reply to Scott Bauer
Signed-off-by Scott Bauer <scott.bauer at intel.com>
---
 MAINTAINERS | 11 +++++++++++
 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
index 5f10c28..8ddf6bd 100644
@@ -11091,6 +11091,17 @@ L:	linux-mmc at vger.kernel.org
 S:	Maintained
 F:	drivers/mmc/host/sdhci-spear.c
 
+SECURE ENCRYPTING DEVICE (SED) OPAL DRIVER
+M:	Scott Bauer <scott.bauer at intel.com>
+M:	Jonathan Derrick <jonathan.derrick at intel.com>
+M:	Rafael Antognolli <rafael.antognolli at intel.com>
+L:	linux-nvme at lists.infradead.org
+S:	Supported
+F:	block/sed*
+F:      block/opal_proto.h
+F:	include/linux/sed*
+F:	include/uapi/linux/sed*
+
 SECURITY SUBSYSTEM
 M:	James Morris <james.l.morris at oracle.com>
 M:	"Serge E. Hallyn" <serge at hallyn.com>
Signature · 1 line
2.7.4
#9 lore raw

PATCH v5 4/4 Maintainers: Add maintainer info for SED/Opal code

Just fold this into patch 2.

#10 lore raw

v5 cover SED OPAL Library

In reply to Scott Bauer

Hi Scott,

I have a few more comments, but we're basically down to style nitpicks. If you do a quick respin I hope we can get this merged for this merge window.