Skip to content

Commit 51f421c

Browse files
RevanthRajashekaraxboe
authored andcommitted
block: sed-opal: Add support to read/write opal tables generically
This feature gives the user RW access to any opal table with admin1 authority. The flags described in the new structure determines if the user wants to read/write the data. Flags are checked for valid values in order to allow future features to be added to the ioctl. The user can provide the desired table's UID. Also, the ioctl provides a size and offset field and internally will loop data accesses to return the full data block. Read overrun is prevented by the initiator's sec_send_recv() backend. The ioctl provides a private field with the intention to accommodate any future expansions to the ioctl. Reviewed-by: Scott Bauer <sbauer@plzdonthack.me> Reviewed-by: Jon Derrick <jonathan.derrick@intel.com> Signed-off-by: Revanth Rajashekar <revanth.rajashekar@intel.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 3495ea1 commit 51f421c

4 files changed

Lines changed: 193 additions & 1 deletion

File tree

‎block/opal_proto.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ enum opal_response_token {
7676
* Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
7777
* Section: 6.3 Assigned UIDs
7878
*/
79-
#define OPAL_UID_LENGTH 8
8079
#define OPAL_METHOD_LENGTH 8
8180
#define OPAL_MSID_KEYLEN 15
8281
#define OPAL_UID_LENGTH_HALF 4

‎block/sed-opal.c‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,113 @@ static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
19671967
return 0;
19681968
}
19691969

1970+
static int write_table_data(struct opal_dev *dev, void *data)
1971+
{
1972+
struct opal_read_write_table *write_tbl = data;
1973+
1974+
return generic_table_write_data(dev, write_tbl->data, write_tbl->offset,
1975+
write_tbl->size, write_tbl->table_uid);
1976+
}
1977+
1978+
static int read_table_data_cont(struct opal_dev *dev)
1979+
{
1980+
int err;
1981+
const char *data_read;
1982+
1983+
err = parse_and_check_status(dev);
1984+
if (err)
1985+
return err;
1986+
1987+
dev->prev_d_len = response_get_string(&dev->parsed, 1, &data_read);
1988+
dev->prev_data = (void *)data_read;
1989+
if (!dev->prev_data) {
1990+
pr_debug("%s: Couldn't read data from the table.\n", __func__);
1991+
return OPAL_INVAL_PARAM;
1992+
}
1993+
1994+
return 0;
1995+
}
1996+
1997+
/*
1998+
* IO_BUFFER_LENGTH = 2048
1999+
* sizeof(header) = 56
2000+
* No. of Token Bytes in the Response = 11
2001+
* MAX size of data that can be carried in response buffer
2002+
* at a time is : 2048 - (56 + 11) = 1981 = 0x7BD.
2003+
*/
2004+
#define OPAL_MAX_READ_TABLE (0x7BD)
2005+
2006+
static int read_table_data(struct opal_dev *dev, void *data)
2007+
{
2008+
struct opal_read_write_table *read_tbl = data;
2009+
int err;
2010+
size_t off = 0, max_read_size = OPAL_MAX_READ_TABLE;
2011+
u64 table_len, len;
2012+
u64 offset = read_tbl->offset, read_size = read_tbl->size - 1;
2013+
u8 __user *dst;
2014+
2015+
err = generic_get_table_info(dev, read_tbl->table_uid, OPAL_TABLE_ROWS);
2016+
if (err) {
2017+
pr_debug("Couldn't get the table size\n");
2018+
return err;
2019+
}
2020+
2021+
table_len = response_get_u64(&dev->parsed, 4);
2022+
2023+
/* Check if the user is trying to read from the table limits */
2024+
if (read_size > table_len || offset > table_len - read_size) {
2025+
pr_debug("Read size exceeds the Table size limits (%llu vs. %llu)\n",
2026+
offset + read_size, table_len);
2027+
return -EINVAL;
2028+
}
2029+
2030+
while (off < read_size) {
2031+
err = cmd_start(dev, read_tbl->table_uid, opalmethod[OPAL_GET]);
2032+
2033+
add_token_u8(&err, dev, OPAL_STARTLIST);
2034+
add_token_u8(&err, dev, OPAL_STARTNAME);
2035+
add_token_u8(&err, dev, OPAL_STARTROW);
2036+
add_token_u64(&err, dev, offset + off); /* start row value */
2037+
add_token_u8(&err, dev, OPAL_ENDNAME);
2038+
2039+
add_token_u8(&err, dev, OPAL_STARTNAME);
2040+
add_token_u8(&err, dev, OPAL_ENDROW);
2041+
2042+
len = min(max_read_size, (size_t)(read_size - off));
2043+
add_token_u64(&err, dev, offset + off + len); /* end row value
2044+
*/
2045+
add_token_u8(&err, dev, OPAL_ENDNAME);
2046+
add_token_u8(&err, dev, OPAL_ENDLIST);
2047+
2048+
if (err) {
2049+
pr_debug("Error building read table data command.\n");
2050+
break;
2051+
}
2052+
2053+
err = finalize_and_send(dev, read_table_data_cont);
2054+
if (err)
2055+
break;
2056+
2057+
/* len+1: This includes the NULL terminator at the end*/
2058+
if (dev->prev_d_len > len + 1) {
2059+
err = -EOVERFLOW;
2060+
break;
2061+
}
2062+
2063+
dst = (u8 __user *)(uintptr_t)read_tbl->data;
2064+
if (copy_to_user(dst + off, dev->prev_data, dev->prev_d_len)) {
2065+
pr_debug("Error copying data to userspace\n");
2066+
err = -EFAULT;
2067+
break;
2068+
}
2069+
dev->prev_data = NULL;
2070+
2071+
off += len;
2072+
}
2073+
2074+
return err;
2075+
}
2076+
19702077
static int end_opal_session(struct opal_dev *dev, void *data)
19712078
{
19722079
int err = 0;
@@ -2453,6 +2560,68 @@ bool opal_unlock_from_suspend(struct opal_dev *dev)
24532560
}
24542561
EXPORT_SYMBOL(opal_unlock_from_suspend);
24552562

2563+
static int opal_read_table(struct opal_dev *dev,
2564+
struct opal_read_write_table *rw_tbl)
2565+
{
2566+
const struct opal_step read_table_steps[] = {
2567+
{ start_admin1LSP_opal_session, &rw_tbl->key },
2568+
{ read_table_data, rw_tbl },
2569+
{ end_opal_session, }
2570+
};
2571+
int ret = 0;
2572+
2573+
if (!rw_tbl->size)
2574+
return ret;
2575+
2576+
return execute_steps(dev, read_table_steps,
2577+
ARRAY_SIZE(read_table_steps));
2578+
}
2579+
2580+
static int opal_write_table(struct opal_dev *dev,
2581+
struct opal_read_write_table *rw_tbl)
2582+
{
2583+
const struct opal_step write_table_steps[] = {
2584+
{ start_admin1LSP_opal_session, &rw_tbl->key },
2585+
{ write_table_data, rw_tbl },
2586+
{ end_opal_session, }
2587+
};
2588+
int ret = 0;
2589+
2590+
if (!rw_tbl->size)
2591+
return ret;
2592+
2593+
return execute_steps(dev, write_table_steps,
2594+
ARRAY_SIZE(write_table_steps));
2595+
}
2596+
2597+
static int opal_generic_read_write_table(struct opal_dev *dev,
2598+
struct opal_read_write_table *rw_tbl)
2599+
{
2600+
int ret, bit_set;
2601+
2602+
mutex_lock(&dev->dev_lock);
2603+
setup_opal_dev(dev);
2604+
2605+
bit_set = fls64(rw_tbl->flags) - 1;
2606+
switch (bit_set) {
2607+
case OPAL_READ_TABLE:
2608+
ret = opal_read_table(dev, rw_tbl);
2609+
break;
2610+
case OPAL_WRITE_TABLE:
2611+
ret = opal_write_table(dev, rw_tbl);
2612+
break;
2613+
default:
2614+
pr_debug("Invalid bit set in the flag (%016llx).\n",
2615+
rw_tbl->flags);
2616+
ret = -EINVAL;
2617+
break;
2618+
}
2619+
2620+
mutex_unlock(&dev->dev_lock);
2621+
2622+
return ret;
2623+
}
2624+
24562625
int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
24572626
{
24582627
void *p;
@@ -2515,6 +2684,9 @@ int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
25152684
case IOC_OPAL_PSID_REVERT_TPR:
25162685
ret = opal_reverttper(dev, p, true);
25172686
break;
2687+
case IOC_OPAL_GENERIC_TABLE_RW:
2688+
ret = opal_generic_read_write_table(dev, p);
2689+
break;
25182690
default:
25192691
break;
25202692
}

‎include/linux/sed-opal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static inline bool is_sed_ioctl(unsigned int cmd)
4242
case IOC_OPAL_PSID_REVERT_TPR:
4343
case IOC_OPAL_MBR_DONE:
4444
case IOC_OPAL_WRITE_SHADOW_MBR:
45+
case IOC_OPAL_GENERIC_TABLE_RW:
4546
return true;
4647
}
4748
return false;

‎include/uapi/linux/sed-opal.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ struct opal_shadow_mbr {
113113
__u64 size;
114114
};
115115

116+
/* Opal table operations */
117+
enum opal_table_ops {
118+
OPAL_READ_TABLE,
119+
OPAL_WRITE_TABLE,
120+
};
121+
122+
#define OPAL_UID_LENGTH 8
123+
struct opal_read_write_table {
124+
struct opal_key key;
125+
const __u64 data;
126+
const __u8 table_uid[OPAL_UID_LENGTH];
127+
__u64 offset;
128+
__u64 size;
129+
#define OPAL_TABLE_READ (1 << OPAL_READ_TABLE)
130+
#define OPAL_TABLE_WRITE (1 << OPAL_WRITE_TABLE)
131+
__u64 flags;
132+
__u64 priv;
133+
};
134+
116135
#define IOC_OPAL_SAVE _IOW('p', 220, struct opal_lock_unlock)
117136
#define IOC_OPAL_LOCK_UNLOCK _IOW('p', 221, struct opal_lock_unlock)
118137
#define IOC_OPAL_TAKE_OWNERSHIP _IOW('p', 222, struct opal_key)
@@ -128,5 +147,6 @@ struct opal_shadow_mbr {
128147
#define IOC_OPAL_PSID_REVERT_TPR _IOW('p', 232, struct opal_key)
129148
#define IOC_OPAL_MBR_DONE _IOW('p', 233, struct opal_mbr_done)
130149
#define IOC_OPAL_WRITE_SHADOW_MBR _IOW('p', 234, struct opal_shadow_mbr)
150+
#define IOC_OPAL_GENERIC_TABLE_RW _IOW('p', 235, struct opal_read_write_table)
131151

132152
#endif /* _UAPI_SED_OPAL_H */

0 commit comments

Comments
 (0)