Skip to contentSkip to conversation

hw/ufs: Add support for REPORT_LUNS SCSI command for Well Known LUNs

PATCH Superseded Hamza Dridi 2 participants 4 messages Oct 4 – Oct 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ufs: Add support for REPORT_LUNS SCSI command for Well Known LUNs

From: dridiha <[email protected]>

According to the UFS specification, the REPORT_LUNS SCSI command can query Well Known LUNs by setting CDB[2] to 0x01 (select Well Known LUNs) or 0x02 (select all LUNs).

Currently, the UFS emulation layer lacks handling for these CDB[2] select report values, causing command failures when guests attempt to report Well Known LUNs.

This patch adds support for handling CDB[2] values 0x01 and 0x02 in the REPORT_LUNS command, returning the appropriate Well Known LUN descriptors in the SCSI response payload.

---
 hw/ufs/lu.c         | 41 +++++++++++++++++++++++++++++++----------
 include/block/ufs.h |  3 +++
 2 files changed, 34 insertions(+), 10 deletions(-)
hw/ufs/lu.c +31 -10
index bdb1650851..4d77c2da2f 100644
@@ -211,33 +211,54 @@ static const struct SCSIBusInfo ufs_scsi_info = {
     .cancel = ufs_scsi_command_cancelled,
 };
 
+static bool ufs_append_lun(uint8_t *outbuf, uint32_t outbuf_len, int *len,
+                           uint8_t lun, uint8_t lun_msb)
+{
+    if (*len + 8 > outbuf_len) {
+        return false;
+    }
+
+    memset(outbuf + *len, 0, 8);
+    outbuf[*len] = lun_msb;
+    outbuf[*len + 1] = lun;
+    *len += 8;
+
+    return true;
+}
+
 static int ufs_emulate_report_luns(UfsRequest *req, uint8_t *outbuf,
                                    uint32_t outbuf_len)
 {
     UfsHc *u = req->hc;
     int len = 0;
 
-    /* TODO: Support for cases where SELECT REPORT is 1 and 2 */
-    if (req->req_upiu.sc.cdb[2] != 0) {
+    uint8_t report_lun_cmd = req->req_upiu.sc.cdb[2];
+
+    if (report_lun_cmd > 2) {
         return SCSI_COMMAND_FAIL;
     }
 
     if (outbuf_len < 8) {
         return SCSI_COMMAND_FAIL;
     }
+
     memset(outbuf, 0, 8);
     len += 8;
 
-    for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
-        if (u->lus[lun]) {
-            if (len + 8 > outbuf_len) {
+    if (report_lun_cmd == 2 || report_lun_cmd == 1) {
+        ufs_append_lun(outbuf, outbuf_len, &len, u->report_wlu.lun,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->dev_wlu.lun, UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->boot_wlu.lun, UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->rpmb_wlu.lun, UFS_WLUN_MSB);
+    }
+
+    if (report_lun_cmd == 0 || report_lun_cmd == 2) {
+        for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
+            if (u->lus[lun] &&
+                !ufs_append_lun(outbuf, outbuf_len, &len, lun, 0)) {
                 break;
             }
-
-            memset(outbuf + len, 0, 8);
-            outbuf[len] = 0;
-            outbuf[len + 1] = lun;
-            len += 8;
         }
     }
 
include/block/ufs.h +3 -0
index d19b3c65ef..9d5ec4c77f 100644
@@ -818,6 +818,9 @@ typedef struct QEMU_PACKED UtpTaskReqDesc {
  */
 #define UFS_EHS_OFFSET_IN_RESPONSE 32
 
+/* Most significant byte in the Report Lun response for Well Known LU */
+#define UFS_WLUN_MSB 0xC1
+
 /* Well known logical unit id in LUN field of UPIU */
 enum {
     UFS_UPIU_REPORT_LUNS_WLUN = 0x81,
Signature · 1 line
2.43.0
#1 lore raw
Quoted 61 lines

According to the UFS specification, the REPORT_LUNS SCSI command can query Well Known LUNs by setting CDB[2] to 0x01 (select Well Known LUNs) or 0x02 (select all LUNs).

Currently, the UFS emulation layer lacks handling for these CDB[2] select report values, causing command failures when guests attempt to report Well Known LUNs.

This patch adds support for handling CDB[2] values 0x01 and 0x02 in the REPORT_LUNS command, returning the appropriate Well Known LUN descriptors in the SCSI response payload.

---
 hw/ufs/lu.c         | 41 +++++++++++++++++++++++++++++++----------
 include/block/ufs.h |  3 +++
 2 files changed, 34 insertions(+), 10 deletions(-)
hw/ufs/lu.c +19 -2
index bdb16508512..4d77c2da2fd 100644
@@ -211,33 +211,54 @@ static const struct SCSIBusInfo ufs_scsi_info = {
     .cancel = ufs_scsi_command_cancelled,
 };
 
+static bool ufs_append_lun(uint8_t *outbuf, uint32_t outbuf_len, int *len,
+                           uint8_t lun, uint8_t lun_msb)
+{
+    if (*len + 8 > outbuf_len) {
+        return false;
+    }
+
+    memset(outbuf + *len, 0, 8);
+    outbuf[*len] = lun_msb;
+    outbuf[*len + 1] = lun;
+    *len += 8;
+
+    return true;
+}
+
 static int ufs_emulate_report_luns(UfsRequest *req, uint8_t *outbuf,
                                    uint32_t outbuf_len)
 {
     UfsHc *u = req->hc;
     int len = 0;
 
-    /* TODO: Support for cases where SELECT REPORT is 1 and 2 */
-    if (req->req_upiu.sc.cdb[2] != 0) {
+    uint8_t report_lun_cmd = req->req_upiu.sc.cdb[2];
+
+    if (report_lun_cmd > 2) {
         return SCSI_COMMAND_FAIL;
     }
 
     if (outbuf_len < 8) {
         return SCSI_COMMAND_FAIL;
     }
+
     memset(outbuf, 0, 8);
     len += 8;

hw/ufs/lu.c static const struct SCSIBusInfo ufs_scsi_info = { · line 253

Quoted 10 lines
hw/ufs/lu.c +6 -3
 
-    for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
-        if (u->lus[lun]) {
-            if (len + 8 > outbuf_len) {
+    if (report_lun_cmd == 2 || report_lun_cmd == 1) {
+        ufs_append_lun(outbuf, outbuf_len, &len, u->report_wlu.lun,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->dev_wlu.lun, UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->boot_wlu.lun, UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->rpmb_wlu.lun, UFS_WLUN_MSB);

These values include the UPIU WLUN_ID bit, but REPORT LUNS requires the SCSI W-LUN value. UFS 4.1 Table 10.65 shows C1 30 for Boot and C1 44 for RPMB.

I checked SELECT REPORT=1 and 2 on a Samsung KLUEG8UHDB-C2D1 UFS device and in a Debian guest running the patched QEMU. Both report types returned the following first two bytes for each WLUN record:

    WLUN          Real device   Patched QEMU
    REPORT LUNS   C1 01         C1 81
    UFS Device    C1 50         C1 D0
    Boot          C1 30         C1 B0
    RPMB          C1 44         C1 C4

The remaining six bytes were zero in both cases. Please clear the UPIU WLUN_ID bit before writing the W-LUN byte.

Signature · 1 line
Jeuk Kim <[email protected]>
#2 lore raw

PATCH v2 1/1 hw/ufs: Add support for REPORT_LUNS SCSI command for Well Known LUNs

In reply to Hamza Dridi

From: dridiha <[email protected]>

According to the UFS specification, the REPORT_LUNS SCSI command can query Well Known LUNs by setting CDB[2] to 0x01 (select Well Known LUNs) or 0x02 (select all LUNs).

Currently, the UFS emulation layer lacks handling for these CDB[2] select report values, causing command failures when guests attempt to report Well Known LUNs.

This patch adds support for handling CDB[2] values 0x01 and 0x02 in the REPORT_LUNS command, returning the appropriate Well Known LUN descriptors in the SCSI response payload.

---
v1 -> v2:
- Cleard the UPIU WLUN-ID before writing the Wlun byte.

 hw/ufs/lu.c         | 44 ++++++++++++++++++++++++++++++++++----------
 include/block/ufs.h |  3 +++
 2 files changed, 37 insertions(+), 10 deletions(-)
hw/ufs/lu.c +34 -10
index bdb1650851..4f1f4f265f 100644
@@ -211,33 +211,57 @@ static const struct SCSIBusInfo ufs_scsi_info = {
     .cancel = ufs_scsi_command_cancelled,
 };
 
+static bool ufs_append_lun(uint8_t *outbuf, uint32_t outbuf_len, int *len,
+                           uint8_t lun, uint8_t lun_msb)
+{
+    if (*len + 8 > outbuf_len) {
+        return false;
+    }
+
+    memset(outbuf + *len, 0, 8);
+    outbuf[*len] = lun_msb;
+    outbuf[*len + 1] = lun;
+    *len += 8;
+
+    return true;
+}
+
 static int ufs_emulate_report_luns(UfsRequest *req, uint8_t *outbuf,
                                    uint32_t outbuf_len)
 {
     UfsHc *u = req->hc;
     int len = 0;
 
-    /* TODO: Support for cases where SELECT REPORT is 1 and 2 */
-    if (req->req_upiu.sc.cdb[2] != 0) {
+    uint8_t report_lun_cmd = req->req_upiu.sc.cdb[2];
+
+    if (report_lun_cmd > 2) {
         return SCSI_COMMAND_FAIL;
     }
 
     if (outbuf_len < 8) {
         return SCSI_COMMAND_FAIL;
     }
+
     memset(outbuf, 0, 8);
     len += 8;
 
-    for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
-        if (u->lus[lun]) {
-            if (len + 8 > outbuf_len) {
+    if (report_lun_cmd == 2 || report_lun_cmd == 1) {
+        ufs_append_lun(outbuf, outbuf_len, &len, u->report_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->dev_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->boot_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->rpmb_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+    }
+
+    if (report_lun_cmd == 0 || report_lun_cmd == 2) {
+        for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
+            if (u->lus[lun] &&
+                !ufs_append_lun(outbuf, outbuf_len, &len, lun, 0)) {
                 break;
             }
-
-            memset(outbuf + len, 0, 8);
-            outbuf[len] = 0;
-            outbuf[len + 1] = lun;
-            len += 8;
         }
     }
 
include/block/ufs.h +3 -0
index d19b3c65ef..9d5ec4c77f 100644
@@ -818,6 +818,9 @@ typedef struct QEMU_PACKED UtpTaskReqDesc {
  */
 #define UFS_EHS_OFFSET_IN_RESPONSE 32
 
+/* Most significant byte in the Report Lun response for Well Known LU */
+#define UFS_WLUN_MSB 0xC1
+
 /* Well known logical unit id in LUN field of UPIU */
 enum {
     UFS_UPIU_REPORT_LUNS_WLUN = 0x81,
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#3 lore raw
Quoted 98 lines

According to the UFS specification, the REPORT_LUNS SCSI command can query Well Known LUNs by setting CDB[2] to 0x01 (select Well Known LUNs) or 0x02 (select all LUNs).

Currently, the UFS emulation layer lacks handling for these CDB[2] select report values, causing command failures when guests attempt to report Well Known LUNs.

This patch adds support for handling CDB[2] values 0x01 and 0x02 in the REPORT_LUNS command, returning the appropriate Well Known LUN descriptors in the SCSI response payload.

---
v1 -> v2:
- Cleard the UPIU WLUN-ID before writing the Wlun byte.

 hw/ufs/lu.c         | 44 ++++++++++++++++++++++++++++++++++----------
 include/block/ufs.h |  3 +++
 2 files changed, 37 insertions(+), 10 deletions(-)
hw/ufs/lu.c +34 -10
index bdb1650851..4f1f4f265f 100644
@@ -211,33 +211,57 @@ static const struct SCSIBusInfo ufs_scsi_info = {
     .cancel = ufs_scsi_command_cancelled,
 };
 
+static bool ufs_append_lun(uint8_t *outbuf, uint32_t outbuf_len, int *len,
+                           uint8_t lun, uint8_t lun_msb)
+{
+    if (*len + 8 > outbuf_len) {
+        return false;
+    }
+
+    memset(outbuf + *len, 0, 8);
+    outbuf[*len] = lun_msb;
+    outbuf[*len + 1] = lun;
+    *len += 8;
+
+    return true;
+}
+
 static int ufs_emulate_report_luns(UfsRequest *req, uint8_t *outbuf,
                                    uint32_t outbuf_len)
 {
     UfsHc *u = req->hc;
     int len = 0;
 
-    /* TODO: Support for cases where SELECT REPORT is 1 and 2 */
-    if (req->req_upiu.sc.cdb[2] != 0) {
+    uint8_t report_lun_cmd = req->req_upiu.sc.cdb[2];
+
+    if (report_lun_cmd > 2) {
         return SCSI_COMMAND_FAIL;
     }
 
     if (outbuf_len < 8) {
         return SCSI_COMMAND_FAIL;
     }
+
     memset(outbuf, 0, 8);
     len += 8;
 
-    for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
-        if (u->lus[lun]) {
-            if (len + 8 > outbuf_len) {
+    if (report_lun_cmd == 2 || report_lun_cmd == 1) {
+        ufs_append_lun(outbuf, outbuf_len, &len, u->report_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->dev_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->boot_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+        ufs_append_lun(outbuf, outbuf_len, &len, u->rpmb_wlu.lun & 0x7F,
+                       UFS_WLUN_MSB);
+    }
+
+    if (report_lun_cmd == 0 || report_lun_cmd == 2) {
+        for (uint8_t lun = 0; lun < UFS_MAX_LUS; ++lun) {
+            if (u->lus[lun] &&
+                !ufs_append_lun(outbuf, outbuf_len, &len, lun, 0)) {
                 break;
             }
-
-            memset(outbuf + len, 0, 8);
-            outbuf[len] = 0;
-            outbuf[len + 1] = lun;
-            len += 8;
         }
     }
 
include/block/ufs.h +0 -0
index d19b3c65ef..9d5ec4c77f 100644

include/block/ufs.h typedef struct QEMU_PACKED UtpTaskReqDesc { · line 826

Quoted 9 lines
include/block/ufs.h +3 -0
@@ -818,6 +818,9 @@ typedef struct QEMU_PACKED UtpTaskReqDesc {
  */
 #define UFS_EHS_OFFSET_IN_RESPONSE 32
 
+/* Most significant byte in the Report Lun response for Well Known LU */
+#define UFS_WLUN_MSB 0xC1
+
 /* Well known logical unit id in LUN field of UPIU */
 enum {
     UFS_UPIU_REPORT_LUNS_WLUN = 0x81,

Thank you for your contribution.

Signature · 1 line
Jeuk Kim <[email protected]>