Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / ufs / ufs.h
blob4bcc41f53a0d65d8b341d8b8525c9afe111f1a90
1 /*
2 * QEMU UFS
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
6 * Written by Jeuk Kim <jeuk20.kim@samsung.com>
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #ifndef HW_UFS_UFS_H
12 #define HW_UFS_UFS_H
14 #include "hw/pci/pci_device.h"
15 #include "hw/scsi/scsi.h"
16 #include "block/ufs.h"
18 #define UFS_MAX_LUS 32
19 #define UFS_MAX_MCQ_QNUM 32
20 #define UFS_BLOCK_SIZE_SHIFT 12
21 #define UFS_BLOCK_SIZE (1 << UFS_BLOCK_SIZE_SHIFT)
23 typedef struct UfsBusClass {
24 BusClass parent_class;
25 bool (*parent_check_address)(BusState *bus, DeviceState *dev, Error **errp);
26 } UfsBusClass;
28 typedef struct UfsBus {
29 BusState parent_bus;
30 } UfsBus;
32 #define TYPE_UFS_BUS "ufs-bus"
33 DECLARE_OBJ_CHECKERS(UfsBus, UfsBusClass, UFS_BUS, TYPE_UFS_BUS)
35 typedef enum UfsRequestState {
36 UFS_REQUEST_IDLE = 0,
37 UFS_REQUEST_READY = 1,
38 UFS_REQUEST_RUNNING = 2,
39 UFS_REQUEST_COMPLETE = 3,
40 UFS_REQUEST_ERROR = 4,
41 } UfsRequestState;
43 typedef enum UfsReqResult {
44 UFS_REQUEST_SUCCESS = 0,
45 UFS_REQUEST_FAIL = 1,
46 UFS_REQUEST_NO_COMPLETE = 2,
47 } UfsReqResult;
49 #define UFS_INVALID_SLOT (-1)
50 typedef struct UfsRequest {
51 struct UfsHc *hc;
52 UfsRequestState state;
53 int slot; /* -1 when it's a MCQ request */
55 UtpTransferReqDesc utrd;
56 UtpUpiuReq req_upiu;
57 UtpUpiuRsp rsp_upiu;
59 /* for scsi command */
60 QEMUSGList *sg;
61 uint32_t data_len;
63 /* for MCQ */
64 struct UfsSq *sq;
65 struct UfsCqEntry cqe;
66 QTAILQ_ENTRY(UfsRequest) entry;
67 } UfsRequest;
69 static inline bool ufs_mcq_req(UfsRequest *req)
71 return req->sq != NULL;
74 struct UfsLu;
75 typedef UfsReqResult (*UfsScsiOp)(struct UfsLu *, UfsRequest *);
77 typedef struct UfsLu {
78 DeviceState qdev;
79 uint8_t lun;
80 UnitDescriptor unit_desc;
81 SCSIBus bus;
82 SCSIDevice *scsi_dev;
83 BlockConf conf;
84 UfsScsiOp scsi_op;
85 } UfsLu;
87 typedef struct UfsParams {
88 char *serial;
89 uint8_t nutrs; /* Number of UTP Transfer Request Slots */
90 uint8_t nutmrs; /* Number of UTP Task Management Request Slots */
91 bool mcq; /* Multiple Command Queue support */
92 uint8_t mcq_qcfgptr; /* MCQ Queue Configuration Pointer in MCQCAP */
93 uint8_t mcq_maxq; /* MCQ Maximum number of Queues */
94 } UfsParams;
97 * MCQ Properties
99 typedef struct UfsSq {
100 struct UfsHc *u;
101 uint8_t sqid;
102 struct UfsCq *cq;
103 uint64_t addr;
104 uint16_t size; /* A number of entries (qdepth) */
106 QEMUBH *bh; /* Bottom half to process requests in async */
107 UfsRequest *req;
108 QTAILQ_HEAD(, UfsRequest) req_list; /* Free request list */
109 } UfsSq;
111 typedef struct UfsCq {
112 struct UfsHc *u;
113 uint8_t cqid;
114 uint64_t addr;
115 uint16_t size; /* A number of entries (qdepth) */
117 QEMUBH *bh;
118 QTAILQ_HEAD(, UfsRequest) req_list;
119 } UfsCq;
121 typedef struct UfsHc {
122 PCIDevice parent_obj;
123 UfsBus bus;
124 MemoryRegion iomem;
125 UfsReg reg;
126 UfsMcqReg mcq_reg[UFS_MAX_MCQ_QNUM];
127 UfsMcqOpReg mcq_op_reg[UFS_MAX_MCQ_QNUM];
128 UfsParams params;
129 uint32_t reg_size;
130 UfsRequest *req_list;
132 UfsLu *lus[UFS_MAX_LUS];
133 UfsLu report_wlu;
134 UfsLu dev_wlu;
135 UfsLu boot_wlu;
136 UfsLu rpmb_wlu;
137 DeviceDescriptor device_desc;
138 GeometryDescriptor geometry_desc;
139 Attributes attributes;
140 Flags flags;
142 qemu_irq irq;
143 QEMUBH *doorbell_bh;
144 QEMUBH *complete_bh;
146 /* MCQ properties */
147 UfsSq *sq[UFS_MAX_MCQ_QNUM];
148 UfsCq *cq[UFS_MAX_MCQ_QNUM];
149 } UfsHc;
151 static inline uint32_t ufs_mcq_sq_tail(UfsHc *u, uint32_t qid)
153 return u->mcq_op_reg[qid].sq.tp;
156 static inline void ufs_mcq_update_sq_tail(UfsHc *u, uint32_t qid, uint32_t db)
158 u->mcq_op_reg[qid].sq.tp = db;
161 static inline uint32_t ufs_mcq_sq_head(UfsHc *u, uint32_t qid)
163 return u->mcq_op_reg[qid].sq.hp;
166 static inline void ufs_mcq_update_sq_head(UfsHc *u, uint32_t qid, uint32_t db)
168 u->mcq_op_reg[qid].sq.hp = db;
171 static inline bool ufs_mcq_sq_empty(UfsHc *u, uint32_t qid)
173 return ufs_mcq_sq_tail(u, qid) == ufs_mcq_sq_head(u, qid);
176 static inline uint32_t ufs_mcq_cq_tail(UfsHc *u, uint32_t qid)
178 return u->mcq_op_reg[qid].cq.tp;
181 static inline void ufs_mcq_update_cq_tail(UfsHc *u, uint32_t qid, uint32_t db)
183 u->mcq_op_reg[qid].cq.tp = db;
186 static inline uint32_t ufs_mcq_cq_head(UfsHc *u, uint32_t qid)
188 return u->mcq_op_reg[qid].cq.hp;
191 static inline void ufs_mcq_update_cq_head(UfsHc *u, uint32_t qid, uint32_t db)
193 u->mcq_op_reg[qid].cq.hp = db;
196 static inline bool ufs_mcq_cq_empty(UfsHc *u, uint32_t qid)
198 return ufs_mcq_cq_tail(u, qid) == ufs_mcq_cq_head(u, qid);
201 #define TYPE_UFS "ufs"
202 #define UFS(obj) OBJECT_CHECK(UfsHc, (obj), TYPE_UFS)
204 #define TYPE_UFS_LU "ufs-lu"
205 #define UFSLU(obj) OBJECT_CHECK(UfsLu, (obj), TYPE_UFS_LU)
207 typedef enum UfsQueryFlagPerm {
208 UFS_QUERY_FLAG_NONE = 0x0,
209 UFS_QUERY_FLAG_READ = 0x1,
210 UFS_QUERY_FLAG_SET = 0x2,
211 UFS_QUERY_FLAG_CLEAR = 0x4,
212 UFS_QUERY_FLAG_TOGGLE = 0x8,
213 } UfsQueryFlagPerm;
215 typedef enum UfsQueryAttrPerm {
216 UFS_QUERY_ATTR_NONE = 0x0,
217 UFS_QUERY_ATTR_READ = 0x1,
218 UFS_QUERY_ATTR_WRITE = 0x2,
219 } UfsQueryAttrPerm;
221 static inline bool is_wlun(uint8_t lun)
223 return (lun == UFS_UPIU_REPORT_LUNS_WLUN ||
224 lun == UFS_UPIU_UFS_DEVICE_WLUN || lun == UFS_UPIU_BOOT_WLUN ||
225 lun == UFS_UPIU_RPMB_WLUN);
228 void ufs_build_upiu_header(UfsRequest *req, uint8_t trans_type, uint8_t flags,
229 uint8_t response, uint8_t scsi_status,
230 uint16_t data_segment_length);
231 void ufs_build_query_response(UfsRequest *req);
232 void ufs_complete_req(UfsRequest *req, UfsReqResult req_result);
233 void ufs_init_wlu(UfsLu *wlu, uint8_t wlun);
234 #endif /* HW_UFS_UFS_H */