Skip to content

Commit 38fb699

Browse files
avri-altman-sndkstorulf
authored andcommitted
mmc: core Convert UNSTUFF_BITS macro to inline function
The UNSTUFF_BITS macro, which is defined in both drivers/mmc/core/mmc.c and drivers/mmc/core/sd.c, has been converted to an inline function to improve readability, maintainability, and type safety. Signed-off-by: Avri Altman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ulf Hansson <[email protected]>
1 parent 1c97ea1 commit 38fb699

File tree

3 files changed

+113
-127
lines changed

3 files changed

+113
-127
lines changed

drivers/mmc/core/mmc.c

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ static const unsigned int taac_mant[] = {
5151
35, 40, 45, 50, 55, 60, 70, 80,
5252
};
5353

54-
#define UNSTUFF_BITS(resp,start,size) \
55-
({ \
56-
const int __size = size; \
57-
const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
58-
const int __off = 3 - ((start) / 32); \
59-
const int __shft = (start) & 31; \
60-
u32 __res; \
61-
\
62-
__res = resp[__off] >> __shft; \
63-
if (__size + __shft > 32) \
64-
__res |= resp[__off-1] << ((32 - __shft) % 32); \
65-
__res & __mask; \
66-
})
67-
6854
/*
6955
* Given the decoded CSD structure, decode the raw CID to our CID structure.
7056
*/
@@ -85,36 +71,36 @@ static int mmc_decode_cid(struct mmc_card *card)
8571
switch (card->csd.mmca_vsn) {
8672
case 0: /* MMC v1.0 - v1.2 */
8773
case 1: /* MMC v1.4 */
88-
card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
89-
card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
90-
card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
91-
card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
92-
card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
93-
card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
94-
card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
95-
card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
96-
card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
97-
card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
98-
card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
99-
card->cid.month = UNSTUFF_BITS(resp, 12, 4);
100-
card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
74+
card->cid.manfid = unstuff_bits(resp, 104, 24);
75+
card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
76+
card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
77+
card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
78+
card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
79+
card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
80+
card->cid.prod_name[5] = unstuff_bits(resp, 56, 8);
81+
card->cid.prod_name[6] = unstuff_bits(resp, 48, 8);
82+
card->cid.hwrev = unstuff_bits(resp, 44, 4);
83+
card->cid.fwrev = unstuff_bits(resp, 40, 4);
84+
card->cid.serial = unstuff_bits(resp, 16, 24);
85+
card->cid.month = unstuff_bits(resp, 12, 4);
86+
card->cid.year = unstuff_bits(resp, 8, 4) + 1997;
10187
break;
10288

10389
case 2: /* MMC v2.0 - v2.2 */
10490
case 3: /* MMC v3.1 - v3.3 */
10591
case 4: /* MMC v4 */
106-
card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
107-
card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
108-
card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
109-
card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
110-
card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
111-
card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
112-
card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
113-
card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
114-
card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
115-
card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
116-
card->cid.month = UNSTUFF_BITS(resp, 12, 4);
117-
card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
92+
card->cid.manfid = unstuff_bits(resp, 120, 8);
93+
card->cid.oemid = unstuff_bits(resp, 104, 16);
94+
card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
95+
card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
96+
card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
97+
card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
98+
card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
99+
card->cid.prod_name[5] = unstuff_bits(resp, 56, 8);
100+
card->cid.prv = unstuff_bits(resp, 48, 8);
101+
card->cid.serial = unstuff_bits(resp, 16, 32);
102+
card->cid.month = unstuff_bits(resp, 12, 4);
103+
card->cid.year = unstuff_bits(resp, 8, 4) + 1997;
118104
break;
119105

120106
default:
@@ -161,43 +147,43 @@ static int mmc_decode_csd(struct mmc_card *card)
161147
* v1.2 has extra information in bits 15, 11 and 10.
162148
* We also support eMMC v4.4 & v4.41.
163149
*/
164-
csd->structure = UNSTUFF_BITS(resp, 126, 2);
150+
csd->structure = unstuff_bits(resp, 126, 2);
165151
if (csd->structure == 0) {
166152
pr_err("%s: unrecognised CSD structure version %d\n",
167153
mmc_hostname(card->host), csd->structure);
168154
return -EINVAL;
169155
}
170156

171-
csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
172-
m = UNSTUFF_BITS(resp, 115, 4);
173-
e = UNSTUFF_BITS(resp, 112, 3);
157+
csd->mmca_vsn = unstuff_bits(resp, 122, 4);
158+
m = unstuff_bits(resp, 115, 4);
159+
e = unstuff_bits(resp, 112, 3);
174160
csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
175-
csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
161+
csd->taac_clks = unstuff_bits(resp, 104, 8) * 100;
176162

177-
m = UNSTUFF_BITS(resp, 99, 4);
178-
e = UNSTUFF_BITS(resp, 96, 3);
163+
m = unstuff_bits(resp, 99, 4);
164+
e = unstuff_bits(resp, 96, 3);
179165
csd->max_dtr = tran_exp[e] * tran_mant[m];
180-
csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
166+
csd->cmdclass = unstuff_bits(resp, 84, 12);
181167

182-
e = UNSTUFF_BITS(resp, 47, 3);
183-
m = UNSTUFF_BITS(resp, 62, 12);
168+
e = unstuff_bits(resp, 47, 3);
169+
m = unstuff_bits(resp, 62, 12);
184170
csd->capacity = (1 + m) << (e + 2);
185171

186-
csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
187-
csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
188-
csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
189-
csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
190-
csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
191-
csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
192-
csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
193-
csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
172+
csd->read_blkbits = unstuff_bits(resp, 80, 4);
173+
csd->read_partial = unstuff_bits(resp, 79, 1);
174+
csd->write_misalign = unstuff_bits(resp, 78, 1);
175+
csd->read_misalign = unstuff_bits(resp, 77, 1);
176+
csd->dsr_imp = unstuff_bits(resp, 76, 1);
177+
csd->r2w_factor = unstuff_bits(resp, 26, 3);
178+
csd->write_blkbits = unstuff_bits(resp, 22, 4);
179+
csd->write_partial = unstuff_bits(resp, 21, 1);
194180

195181
if (csd->write_blkbits >= 9) {
196-
a = UNSTUFF_BITS(resp, 42, 5);
197-
b = UNSTUFF_BITS(resp, 37, 5);
182+
a = unstuff_bits(resp, 42, 5);
183+
b = unstuff_bits(resp, 37, 5);
198184
csd->erase_size = (a + 1) * (b + 1);
199185
csd->erase_size <<= csd->write_blkbits - 9;
200-
csd->wp_grp_size = UNSTUFF_BITS(resp, 32, 5);
186+
csd->wp_grp_size = unstuff_bits(resp, 32, 5);
201187
}
202188

203189
return 0;

drivers/mmc/core/mmc_ops.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,19 @@ int mmc_cmdq_enable(struct mmc_card *card);
5656
int mmc_cmdq_disable(struct mmc_card *card);
5757
int mmc_sanitize(struct mmc_card *card, unsigned int timeout_ms);
5858

59+
static inline u32 unstuff_bits(const u32 *resp, int start, int size)
60+
{
61+
const int __size = size;
62+
const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;
63+
const int __off = 3 - (start / 32);
64+
const int __shft = start & 31;
65+
u32 __res = resp[__off] >> __shft;
66+
67+
if (__size + __shft > 32)
68+
__res |= resp[__off - 1] << ((32 - __shft) % 32);
69+
70+
return __res & __mask;
71+
}
72+
5973
#endif
6074

drivers/mmc/core/sd.c

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ static const unsigned int sd_au_size[] = {
5656
SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
5757
};
5858

59-
#define UNSTUFF_BITS(resp,start,size) \
60-
({ \
61-
const int __size = size; \
62-
const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
63-
const int __off = 3 - ((start) / 32); \
64-
const int __shft = (start) & 31; \
65-
u32 __res; \
66-
\
67-
__res = resp[__off] >> __shft; \
68-
if (__size + __shft > 32) \
69-
__res |= resp[__off-1] << ((32 - __shft) % 32); \
70-
__res & __mask; \
71-
})
72-
7359
#define SD_POWEROFF_NOTIFY_TIMEOUT_MS 1000
7460
#define SD_WRITE_EXTR_SINGLE_TIMEOUT_MS 1000
7561

@@ -95,18 +81,18 @@ void mmc_decode_cid(struct mmc_card *card)
9581
* SD doesn't currently have a version field so we will
9682
* have to assume we can parse this.
9783
*/
98-
card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
99-
card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
100-
card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
101-
card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
102-
card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
103-
card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
104-
card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
105-
card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
106-
card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
107-
card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
108-
card->cid.year = UNSTUFF_BITS(resp, 12, 8);
109-
card->cid.month = UNSTUFF_BITS(resp, 8, 4);
84+
card->cid.manfid = unstuff_bits(resp, 120, 8);
85+
card->cid.oemid = unstuff_bits(resp, 104, 16);
86+
card->cid.prod_name[0] = unstuff_bits(resp, 96, 8);
87+
card->cid.prod_name[1] = unstuff_bits(resp, 88, 8);
88+
card->cid.prod_name[2] = unstuff_bits(resp, 80, 8);
89+
card->cid.prod_name[3] = unstuff_bits(resp, 72, 8);
90+
card->cid.prod_name[4] = unstuff_bits(resp, 64, 8);
91+
card->cid.hwrev = unstuff_bits(resp, 60, 4);
92+
card->cid.fwrev = unstuff_bits(resp, 56, 4);
93+
card->cid.serial = unstuff_bits(resp, 24, 32);
94+
card->cid.year = unstuff_bits(resp, 12, 8);
95+
card->cid.month = unstuff_bits(resp, 8, 4);
11096

11197
card->cid.year += 2000; /* SD cards year offset */
11298
}
@@ -120,41 +106,41 @@ static int mmc_decode_csd(struct mmc_card *card)
120106
unsigned int e, m, csd_struct;
121107
u32 *resp = card->raw_csd;
122108

123-
csd_struct = UNSTUFF_BITS(resp, 126, 2);
109+
csd_struct = unstuff_bits(resp, 126, 2);
124110

125111
switch (csd_struct) {
126112
case 0:
127-
m = UNSTUFF_BITS(resp, 115, 4);
128-
e = UNSTUFF_BITS(resp, 112, 3);
113+
m = unstuff_bits(resp, 115, 4);
114+
e = unstuff_bits(resp, 112, 3);
129115
csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
130-
csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
116+
csd->taac_clks = unstuff_bits(resp, 104, 8) * 100;
131117

132-
m = UNSTUFF_BITS(resp, 99, 4);
133-
e = UNSTUFF_BITS(resp, 96, 3);
118+
m = unstuff_bits(resp, 99, 4);
119+
e = unstuff_bits(resp, 96, 3);
134120
csd->max_dtr = tran_exp[e] * tran_mant[m];
135-
csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
121+
csd->cmdclass = unstuff_bits(resp, 84, 12);
136122

137-
e = UNSTUFF_BITS(resp, 47, 3);
138-
m = UNSTUFF_BITS(resp, 62, 12);
123+
e = unstuff_bits(resp, 47, 3);
124+
m = unstuff_bits(resp, 62, 12);
139125
csd->capacity = (1 + m) << (e + 2);
140126

141-
csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
142-
csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
143-
csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
144-
csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
145-
csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
146-
csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
147-
csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
148-
csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
127+
csd->read_blkbits = unstuff_bits(resp, 80, 4);
128+
csd->read_partial = unstuff_bits(resp, 79, 1);
129+
csd->write_misalign = unstuff_bits(resp, 78, 1);
130+
csd->read_misalign = unstuff_bits(resp, 77, 1);
131+
csd->dsr_imp = unstuff_bits(resp, 76, 1);
132+
csd->r2w_factor = unstuff_bits(resp, 26, 3);
133+
csd->write_blkbits = unstuff_bits(resp, 22, 4);
134+
csd->write_partial = unstuff_bits(resp, 21, 1);
149135

150-
if (UNSTUFF_BITS(resp, 46, 1)) {
136+
if (unstuff_bits(resp, 46, 1)) {
151137
csd->erase_size = 1;
152138
} else if (csd->write_blkbits >= 9) {
153-
csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
139+
csd->erase_size = unstuff_bits(resp, 39, 7) + 1;
154140
csd->erase_size <<= csd->write_blkbits - 9;
155141
}
156142

157-
if (UNSTUFF_BITS(resp, 13, 1))
143+
if (unstuff_bits(resp, 13, 1))
158144
mmc_card_set_readonly(card);
159145
break;
160146
case 1:
@@ -169,17 +155,17 @@ static int mmc_decode_csd(struct mmc_card *card)
169155
csd->taac_ns = 0; /* Unused */
170156
csd->taac_clks = 0; /* Unused */
171157

172-
m = UNSTUFF_BITS(resp, 99, 4);
173-
e = UNSTUFF_BITS(resp, 96, 3);
158+
m = unstuff_bits(resp, 99, 4);
159+
e = unstuff_bits(resp, 96, 3);
174160
csd->max_dtr = tran_exp[e] * tran_mant[m];
175-
csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
176-
csd->c_size = UNSTUFF_BITS(resp, 48, 22);
161+
csd->cmdclass = unstuff_bits(resp, 84, 12);
162+
csd->c_size = unstuff_bits(resp, 48, 22);
177163

178164
/* SDXC cards have a minimum C_SIZE of 0x00FFFF */
179165
if (csd->c_size >= 0xFFFF)
180166
mmc_card_set_ext_capacity(card);
181167

182-
m = UNSTUFF_BITS(resp, 48, 22);
168+
m = unstuff_bits(resp, 48, 22);
183169
csd->capacity = (1 + m) << 10;
184170

185171
csd->read_blkbits = 9;
@@ -191,7 +177,7 @@ static int mmc_decode_csd(struct mmc_card *card)
191177
csd->write_partial = 0;
192178
csd->erase_size = 1;
193179

194-
if (UNSTUFF_BITS(resp, 13, 1))
180+
if (unstuff_bits(resp, 13, 1))
195181
mmc_card_set_readonly(card);
196182
break;
197183
default:
@@ -217,33 +203,33 @@ static int mmc_decode_scr(struct mmc_card *card)
217203
resp[3] = card->raw_scr[1];
218204
resp[2] = card->raw_scr[0];
219205

220-
scr_struct = UNSTUFF_BITS(resp, 60, 4);
206+
scr_struct = unstuff_bits(resp, 60, 4);
221207
if (scr_struct != 0) {
222208
pr_err("%s: unrecognised SCR structure version %d\n",
223209
mmc_hostname(card->host), scr_struct);
224210
return -EINVAL;
225211
}
226212

227-
scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
228-
scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
213+
scr->sda_vsn = unstuff_bits(resp, 56, 4);
214+
scr->bus_widths = unstuff_bits(resp, 48, 4);
229215
if (scr->sda_vsn == SCR_SPEC_VER_2)
230216
/* Check if Physical Layer Spec v3.0 is supported */
231-
scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
217+
scr->sda_spec3 = unstuff_bits(resp, 47, 1);
232218

233219
if (scr->sda_spec3) {
234-
scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
235-
scr->sda_specx = UNSTUFF_BITS(resp, 38, 4);
220+
scr->sda_spec4 = unstuff_bits(resp, 42, 1);
221+
scr->sda_specx = unstuff_bits(resp, 38, 4);
236222
}
237223

238-
if (UNSTUFF_BITS(resp, 55, 1))
224+
if (unstuff_bits(resp, 55, 1))
239225
card->erased_byte = 0xFF;
240226
else
241227
card->erased_byte = 0x0;
242228

243229
if (scr->sda_spec4)
244-
scr->cmds = UNSTUFF_BITS(resp, 32, 4);
230+
scr->cmds = unstuff_bits(resp, 32, 4);
245231
else if (scr->sda_spec3)
246-
scr->cmds = UNSTUFF_BITS(resp, 32, 2);
232+
scr->cmds = unstuff_bits(resp, 32, 2);
247233

248234
/* SD Spec says: any SD Card shall set at least bits 0 and 2 */
249235
if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) ||
@@ -289,17 +275,17 @@ static int mmc_read_ssr(struct mmc_card *card)
289275
kfree(raw_ssr);
290276

291277
/*
292-
* UNSTUFF_BITS only works with four u32s so we have to offset the
278+
* unstuff_bits only works with four u32s so we have to offset the
293279
* bitfield positions accordingly.
294280
*/
295-
au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4);
281+
au = unstuff_bits(card->raw_ssr, 428 - 384, 4);
296282
if (au) {
297283
if (au <= 9 || card->scr.sda_spec3) {
298284
card->ssr.au = sd_au_size[au];
299-
es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16);
300-
et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6);
285+
es = unstuff_bits(card->raw_ssr, 408 - 384, 16);
286+
et = unstuff_bits(card->raw_ssr, 402 - 384, 6);
301287
if (es && et) {
302-
eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2);
288+
eo = unstuff_bits(card->raw_ssr, 400 - 384, 2);
303289
card->ssr.erase_timeout = (et * 1000) / es;
304290
card->ssr.erase_offset = eo * 1000;
305291
}
@@ -313,7 +299,7 @@ static int mmc_read_ssr(struct mmc_card *card)
313299
* starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
314300
*/
315301
resp[3] = card->raw_ssr[6];
316-
discard_support = UNSTUFF_BITS(resp, 313 - 288, 1);
302+
discard_support = unstuff_bits(resp, 313 - 288, 1);
317303
card->erase_arg = (card->scr.sda_specx && discard_support) ?
318304
SD_DISCARD_ARG : SD_ERASE_ARG;
319305

0 commit comments

Comments
 (0)