xref: /linux/include/linux/mmc/core.h (revision 021bc4b9)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  linux/include/linux/mmc/core.h
4  */
5 #ifndef LINUX_MMC_CORE_H
6 #define LINUX_MMC_CORE_H
7 
8 #include <linux/completion.h>
9 #include <linux/types.h>
10 
11 struct mmc_data;
12 struct mmc_request;
13 
14 enum mmc_blk_status {
15 	MMC_BLK_SUCCESS = 0,
16 	MMC_BLK_PARTIAL,
17 	MMC_BLK_CMD_ERR,
18 	MMC_BLK_RETRY,
19 	MMC_BLK_ABORT,
20 	MMC_BLK_DATA_ERR,
21 	MMC_BLK_ECC_ERR,
22 	MMC_BLK_NOMEDIUM,
23 	MMC_BLK_NEW_REQUEST,
24 };
25 
26 struct mmc_command {
27 	u32			opcode;
28 	u32			arg;
29 #define MMC_CMD23_ARG_REL_WR	(1 << 31)
30 #define MMC_CMD23_ARG_TAG_REQ	(1 << 29)
31 	u32			resp[4];
32 	unsigned int		flags;		/* expected response type */
33 #define MMC_RSP_PRESENT	(1 << 0)
34 #define MMC_RSP_136	(1 << 1)		/* 136 bit response */
35 #define MMC_RSP_CRC	(1 << 2)		/* expect valid crc */
36 #define MMC_RSP_BUSY	(1 << 3)		/* card may send busy */
37 #define MMC_RSP_OPCODE	(1 << 4)		/* response contains opcode */
38 
39 #define MMC_CMD_MASK	(3 << 5)		/* non-SPI command type */
40 #define MMC_CMD_AC	(0 << 5)
41 #define MMC_CMD_ADTC	(1 << 5)
42 #define MMC_CMD_BC	(2 << 5)
43 #define MMC_CMD_BCR	(3 << 5)
44 
45 #define MMC_RSP_SPI_S1	(1 << 7)		/* one status byte */
46 #define MMC_RSP_SPI_S2	(1 << 8)		/* second byte */
47 #define MMC_RSP_SPI_B4	(1 << 9)		/* four data bytes */
48 #define MMC_RSP_SPI_BUSY (1 << 10)		/* card may send busy */
49 
50 /*
51  * These are the native response types, and correspond to valid bit
52  * patterns of the above flags.  One additional valid pattern
53  * is all zeros, which means we don't expect a response.
54  */
55 #define MMC_RSP_NONE	(0)
56 #define MMC_RSP_R1	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
57 #define MMC_RSP_R1B	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE|MMC_RSP_BUSY)
58 #define MMC_RSP_R2	(MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC)
59 #define MMC_RSP_R3	(MMC_RSP_PRESENT)
60 #define MMC_RSP_R4	(MMC_RSP_PRESENT)
61 #define MMC_RSP_R5	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
62 #define MMC_RSP_R6	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
63 #define MMC_RSP_R7	(MMC_RSP_PRESENT|MMC_RSP_CRC|MMC_RSP_OPCODE)
64 
65 /* Can be used by core to poll after switch to MMC HS mode */
66 #define MMC_RSP_R1_NO_CRC	(MMC_RSP_PRESENT|MMC_RSP_OPCODE)
67 
68 #define mmc_resp_type(cmd)	((cmd)->flags & (MMC_RSP_PRESENT|MMC_RSP_136|MMC_RSP_CRC|MMC_RSP_BUSY|MMC_RSP_OPCODE))
69 
70 /*
71  * These are the SPI response types for MMC, SD, and SDIO cards.
72  * Commands return R1, with maybe more info.  Zero is an error type;
73  * callers must always provide the appropriate MMC_RSP_SPI_Rx flags.
74  */
75 #define MMC_RSP_SPI_R1	(MMC_RSP_SPI_S1)
76 #define MMC_RSP_SPI_R1B	(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY)
77 #define MMC_RSP_SPI_R2	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
78 #define MMC_RSP_SPI_R3	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
79 #define MMC_RSP_SPI_R4	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
80 #define MMC_RSP_SPI_R5	(MMC_RSP_SPI_S1|MMC_RSP_SPI_S2)
81 #define MMC_RSP_SPI_R7	(MMC_RSP_SPI_S1|MMC_RSP_SPI_B4)
82 
83 #define mmc_spi_resp_type(cmd)	((cmd)->flags & \
84 		(MMC_RSP_SPI_S1|MMC_RSP_SPI_BUSY|MMC_RSP_SPI_S2|MMC_RSP_SPI_B4))
85 
86 /*
87  * These are the command types.
88  */
89 #define mmc_cmd_type(cmd)	((cmd)->flags & MMC_CMD_MASK)
90 
91 	unsigned int		retries;	/* max number of retries */
92 	int			error;		/* command error */
93 
94 /*
95  * Standard errno values are used for errors, but some have specific
96  * meaning in the MMC layer:
97  *
98  * ETIMEDOUT    Card took too long to respond
99  * EILSEQ       Basic format problem with the received or sent data
100  *              (e.g. CRC check failed, incorrect opcode in response
101  *              or bad end bit)
102  * EINVAL       Request cannot be performed because of restrictions
103  *              in hardware and/or the driver
104  * ENOMEDIUM    Host can determine that the slot is empty and is
105  *              actively failing requests
106  */
107 
108 	unsigned int		busy_timeout;	/* busy detect timeout in ms */
109 	struct mmc_data		*data;		/* data segment associated with cmd */
110 	struct mmc_request	*mrq;		/* associated request */
111 };
112 
113 struct mmc_data {
114 	unsigned int		timeout_ns;	/* data timeout (in ns, max 80ms) */
115 	unsigned int		timeout_clks;	/* data timeout (in clocks) */
116 	unsigned int		blksz;		/* data block size */
117 	unsigned int		blocks;		/* number of blocks */
118 	unsigned int		blk_addr;	/* block address */
119 	int			error;		/* data error */
120 	unsigned int		flags;
121 
122 #define MMC_DATA_WRITE		BIT(8)
123 #define MMC_DATA_READ		BIT(9)
124 /* Extra flags used by CQE */
125 #define MMC_DATA_QBR		BIT(10)		/* CQE queue barrier*/
126 #define MMC_DATA_PRIO		BIT(11)		/* CQE high priority */
127 #define MMC_DATA_REL_WR		BIT(12)		/* Reliable write */
128 #define MMC_DATA_DAT_TAG	BIT(13)		/* Tag request */
129 #define MMC_DATA_FORCED_PRG	BIT(14)		/* Forced programming */
130 
131 	unsigned int		bytes_xfered;
132 
133 	struct mmc_command	*stop;		/* stop command */
134 	struct mmc_request	*mrq;		/* associated request */
135 
136 	unsigned int		sg_len;		/* size of scatter list */
137 	int			sg_count;	/* mapped sg entries */
138 	struct scatterlist	*sg;		/* I/O scatter list */
139 	s32			host_cookie;	/* host private data */
140 };
141 
142 struct mmc_host;
143 struct mmc_request {
144 	struct mmc_command	*sbc;		/* SET_BLOCK_COUNT for multiblock */
145 	struct mmc_command	*cmd;
146 	struct mmc_data		*data;
147 	struct mmc_command	*stop;
148 
149 	struct completion	completion;
150 	struct completion	cmd_completion;
151 	void			(*done)(struct mmc_request *);/* completion function */
152 	/*
153 	 * Notify uppers layers (e.g. mmc block driver) that recovery is needed
154 	 * due to an error associated with the mmc_request. Currently used only
155 	 * by CQE.
156 	 */
157 	void			(*recovery_notifier)(struct mmc_request *);
158 	struct mmc_host		*host;
159 
160 	/* Allow other commands during this ongoing data transfer or busy wait */
161 	bool			cap_cmd_during_tfr;
162 
163 	int			tag;
164 
165 #ifdef CONFIG_MMC_CRYPTO
166 	const struct bio_crypt_ctx *crypto_ctx;
167 	int			crypto_key_slot;
168 #endif
169 };
170 
171 struct mmc_card;
172 
173 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq);
174 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd,
175 		int retries);
176 
177 int mmc_hw_reset(struct mmc_card *card);
178 int mmc_sw_reset(struct mmc_card *card);
179 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card);
180 
181 #endif /* LINUX_MMC_CORE_H */
182