]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
sed-opal: Add STACK_RESET command
authorMilan Broz <gmazyland@gmail.com>
Tue, 10 Mar 2026 09:53:49 +0000 (10:53 +0100)
committerJens Axboe <axboe@kernel.dk>
Tue, 31 Mar 2026 13:04:00 +0000 (07:04 -0600)
The TCG Opal device could enter a state where no new session can be
created, blocking even Discovery or PSID reset. While a power cycle
or waiting for the timeout should work, there is another possibility
for recovery: using the Stack Reset command.

The Stack Reset command is defined in the TCG Storage Architecture Core
Specification and is mandatory for all Opal devices (see Section 3.3.6
of the Opal SSC specification).

This patch implements the Stack Reset command. Sending it should clear
all active sessions immediately, allowing subsequent commands to run
successfully. While it is a TCG transport layer command, the Linux
kernel implements only Opal ioctls, so it makes sense to use the
IOC_OPAL ioctl interface.

The Stack Reset takes no arguments; the response can be success or pending.
If the command reports a pending state, userspace can try to repeat it;
in this case, the code returns -EBUSY.

Signed-off-by: Milan Broz <gmazyland@gmail.com>
Reviewed-by: Ondrej Kozina <okozina@redhat.com>
Link: https://patch.msgid.link/20260310095349.411287-1-gmazyland@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/opal_proto.h
block/sed-opal.c
include/linux/sed-opal.h
include/uapi/linux/sed-opal.h

index d138785b819822b45a123ee6d11f9c405ce89bc3..7c24247aa186a56f1549abddd726dd59c8843a91 100644 (file)
@@ -19,6 +19,7 @@
 enum {
        TCG_SECP_00 = 0,
        TCG_SECP_01,
+       TCG_SECP_02,
 };
 
 /*
@@ -273,6 +274,25 @@ struct opal_header {
        struct opal_data_subpacket subpkt;
 };
 
+/*
+ * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
+ * Section: 3.3.4.7.5 STACK_RESET
+ */
+#define OPAL_STACK_RESET 0x0002
+
+struct opal_stack_reset {
+       u8 extendedComID[4];
+       __be32 request_code;
+};
+
+struct opal_stack_reset_response {
+       u8 extendedComID[4];
+       __be32 request_code;
+       u8 reserved0[2];
+       __be16 data_length;
+       __be32 response;
+};
+
 #define FC_TPER       0x0001
 #define FC_LOCKING    0x0002
 #define FC_GEOMETRY   0x0003
index c34d19e912018d61865d5b0b7aa53925bd92bbdf..79b290d9458a9ec62525193d944212dda4dd2195 100644 (file)
@@ -3545,6 +3545,50 @@ static int opal_get_sum_ranges(struct opal_dev *dev, struct opal_sum_ranges *opa
        return ret;
 }
 
+static int opal_stack_reset(struct opal_dev *dev)
+{
+       struct opal_stack_reset *req;
+       struct opal_stack_reset_response *resp;
+       int ret;
+
+       mutex_lock(&dev->dev_lock);
+
+       memset(dev->cmd, 0, IO_BUFFER_LENGTH);
+       req = (struct opal_stack_reset *)dev->cmd;
+       req->extendedComID[0] = dev->comid >> 8;
+       req->extendedComID[1] = dev->comid & 0xFF;
+       req->request_code = cpu_to_be32(OPAL_STACK_RESET);
+
+       ret = dev->send_recv(dev->data, dev->comid, TCG_SECP_02,
+                            dev->cmd, IO_BUFFER_LENGTH, true);
+       if (ret) {
+               pr_debug("Error sending stack reset: %d\n", ret);
+               goto out;
+       }
+
+       memset(dev->resp, 0, IO_BUFFER_LENGTH);
+       ret = dev->send_recv(dev->data, dev->comid, TCG_SECP_02,
+                            dev->resp, IO_BUFFER_LENGTH, false);
+       if (ret) {
+               pr_debug("Error receiving stack reset response: %d\n", ret);
+               goto out;
+       }
+
+       resp = (struct opal_stack_reset_response *)dev->resp;
+       if (be16_to_cpu(resp->data_length) != 4) {
+               pr_debug("Stack reset pending\n");
+               ret = -EBUSY;
+               goto out;
+       }
+       if (be32_to_cpu(resp->response) != 0) {
+               pr_debug("Stack reset failed: %u\n", be32_to_cpu(resp->response));
+               ret = -EIO;
+       }
+out:
+       mutex_unlock(&dev->dev_lock);
+       return ret;
+}
+
 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
 {
        void *p;
@@ -3642,6 +3686,9 @@ int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
        case IOC_OPAL_GET_SUM_STATUS:
                ret = opal_get_sum_ranges(dev, p, arg);
                break;
+       case IOC_OPAL_STACK_RESET:
+               ret = opal_stack_reset(dev);
+               break;
 
        default:
                break;
index aa006edb612bc161d22c5f03e9ea56db1f29f663..0630430cc01a6b7da22680e631ffcef54e503e6b 100644 (file)
@@ -57,6 +57,7 @@ static inline bool is_sed_ioctl(unsigned int cmd)
        case IOC_OPAL_LR_SET_START_LEN:
        case IOC_OPAL_ENABLE_DISABLE_LR:
        case IOC_OPAL_GET_SUM_STATUS:
+       case IOC_OPAL_STACK_RESET:
                return true;
        }
        return false;
index 9830298ec51cc6981e2a6fb2da28eeb6de3c2d80..ef4d3be6ca7fe48422753f56d9b8dde07582d42e 100644 (file)
@@ -245,5 +245,6 @@ struct opal_revert_lsp {
 #define IOC_OPAL_LR_SET_START_LEN   _IOW('p', 243, struct opal_user_lr_setup)
 #define IOC_OPAL_ENABLE_DISABLE_LR  _IOW('p', 244, struct opal_user_lr_setup)
 #define IOC_OPAL_GET_SUM_STATUS     _IOW('p', 245, struct opal_sum_ranges)
+#define IOC_OPAL_STACK_RESET        _IO('p', 246)
 
 #endif /* _UAPI_SED_OPAL_H */