xref: /openbmc/qemu/hw/ufs/ufs.h (revision 259ebed4)
1 /*
2  * QEMU UFS
3  *
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Written by Jeuk Kim <jeuk20.kim@samsung.com>
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10 
11 #ifndef HW_UFS_UFS_H
12 #define HW_UFS_UFS_H
13 
14 #include "hw/pci/pci_device.h"
15 #include "hw/scsi/scsi.h"
16 #include "block/ufs.h"
17 
18 #define UFS_MAX_LUS 32
19 #define UFS_BLOCK_SIZE_SHIFT 12
20 #define UFS_BLOCK_SIZE (1 << UFS_BLOCK_SIZE_SHIFT)
21 
22 typedef struct UfsBusClass {
23     BusClass parent_class;
24     bool (*parent_check_address)(BusState *bus, DeviceState *dev, Error **errp);
25 } UfsBusClass;
26 
27 typedef struct UfsBus {
28     BusState parent_bus;
29 } UfsBus;
30 
31 #define TYPE_UFS_BUS "ufs-bus"
32 DECLARE_OBJ_CHECKERS(UfsBus, UfsBusClass, UFS_BUS, TYPE_UFS_BUS)
33 
34 typedef enum UfsRequestState {
35     UFS_REQUEST_IDLE = 0,
36     UFS_REQUEST_READY = 1,
37     UFS_REQUEST_RUNNING = 2,
38     UFS_REQUEST_COMPLETE = 3,
39     UFS_REQUEST_ERROR = 4,
40 } UfsRequestState;
41 
42 typedef enum UfsReqResult {
43     UFS_REQUEST_SUCCESS = 0,
44     UFS_REQUEST_FAIL = 1,
45     UFS_REQUEST_NO_COMPLETE = 2,
46 } UfsReqResult;
47 
48 typedef struct UfsRequest {
49     struct UfsHc *hc;
50     UfsRequestState state;
51     int slot;
52 
53     UtpTransferReqDesc utrd;
54     UtpUpiuReq req_upiu;
55     UtpUpiuRsp rsp_upiu;
56 
57     /* for scsi command */
58     QEMUSGList *sg;
59     uint32_t data_len;
60 } UfsRequest;
61 
62 struct UfsLu;
63 typedef UfsReqResult (*UfsScsiOp)(struct UfsLu *, UfsRequest *);
64 
65 typedef struct UfsLu {
66     DeviceState qdev;
67     uint8_t lun;
68     UnitDescriptor unit_desc;
69     SCSIBus bus;
70     SCSIDevice *scsi_dev;
71     BlockConf conf;
72     UfsScsiOp scsi_op;
73 } UfsLu;
74 
75 typedef struct UfsParams {
76     char *serial;
77     uint8_t nutrs; /* Number of UTP Transfer Request Slots */
78     uint8_t nutmrs; /* Number of UTP Task Management Request Slots */
79 } UfsParams;
80 
81 typedef struct UfsHc {
82     PCIDevice parent_obj;
83     UfsBus bus;
84     MemoryRegion iomem;
85     UfsReg reg;
86     UfsParams params;
87     uint32_t reg_size;
88     UfsRequest *req_list;
89 
90     UfsLu *lus[UFS_MAX_LUS];
91     UfsLu report_wlu;
92     UfsLu dev_wlu;
93     UfsLu boot_wlu;
94     UfsLu rpmb_wlu;
95     DeviceDescriptor device_desc;
96     GeometryDescriptor geometry_desc;
97     Attributes attributes;
98     Flags flags;
99 
100     qemu_irq irq;
101     QEMUBH *doorbell_bh;
102     QEMUBH *complete_bh;
103 } UfsHc;
104 
105 #define TYPE_UFS "ufs"
106 #define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS)
107 
108 #define TYPE_UFS_LU "ufs-lu"
109 #define UFSLU(obj) OBJECT_CHECK(UfsLu, (obj), TYPE_UFS_LU)
110 
111 typedef enum UfsQueryFlagPerm {
112     UFS_QUERY_FLAG_NONE = 0x0,
113     UFS_QUERY_FLAG_READ = 0x1,
114     UFS_QUERY_FLAG_SET = 0x2,
115     UFS_QUERY_FLAG_CLEAR = 0x4,
116     UFS_QUERY_FLAG_TOGGLE = 0x8,
117 } UfsQueryFlagPerm;
118 
119 typedef enum UfsQueryAttrPerm {
120     UFS_QUERY_ATTR_NONE = 0x0,
121     UFS_QUERY_ATTR_READ = 0x1,
122     UFS_QUERY_ATTR_WRITE = 0x2,
123 } UfsQueryAttrPerm;
124 
125 static inline bool is_wlun(uint8_t lun)
126 {
127     return (lun == UFS_UPIU_REPORT_LUNS_WLUN ||
128             lun == UFS_UPIU_UFS_DEVICE_WLUN || lun == UFS_UPIU_BOOT_WLUN ||
129             lun == UFS_UPIU_RPMB_WLUN);
130 }
131 
132 void ufs_build_upiu_header(UfsRequest *req, uint8_t trans_type, uint8_t flags,
133                            uint8_t response, uint8_t scsi_status,
134                            uint16_t data_segment_length);
135 void ufs_complete_req(UfsRequest *req, UfsReqResult req_result);
136 void ufs_init_wlu(UfsLu *wlu, uint8_t wlun);
137 #endif /* HW_UFS_UFS_H */
138