Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <avri.altman@wdc.com>
Link: https://lore.kernel.org/r/20240902123331.3566447-1-avri.altman@wdc.com
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>

authored by

Avri Altman and committed by
Ulf Hansson
38fb6997 1c97ea11

+113 -127
+46 -60
drivers/mmc/core/mmc.c
··· 51 51 35, 40, 45, 50, 55, 60, 70, 80, 52 52 }; 53 53 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 - 68 54 /* 69 55 * Given the decoded CSD structure, decode the raw CID to our CID structure. 70 56 */ ··· 71 85 switch (card->csd.mmca_vsn) { 72 86 case 0: /* MMC v1.0 - v1.2 */ 73 87 case 1: /* MMC v1.4 */ 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; 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; 87 101 break; 88 102 89 103 case 2: /* MMC v2.0 - v2.2 */ 90 104 case 3: /* MMC v3.1 - v3.3 */ 91 105 case 4: /* MMC v4 */ 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; 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; 104 118 break; 105 119 106 120 default: ··· 147 161 * v1.2 has extra information in bits 15, 11 and 10. 148 162 * We also support eMMC v4.4 & v4.41. 149 163 */ 150 - csd->structure = UNSTUFF_BITS(resp, 126, 2); 164 + csd->structure = unstuff_bits(resp, 126, 2); 151 165 if (csd->structure == 0) { 152 166 pr_err("%s: unrecognised CSD structure version %d\n", 153 167 mmc_hostname(card->host), csd->structure); 154 168 return -EINVAL; 155 169 } 156 170 157 - csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); 158 - m = UNSTUFF_BITS(resp, 115, 4); 159 - e = UNSTUFF_BITS(resp, 112, 3); 171 + csd->mmca_vsn = unstuff_bits(resp, 122, 4); 172 + m = unstuff_bits(resp, 115, 4); 173 + e = unstuff_bits(resp, 112, 3); 160 174 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10; 161 - csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 175 + csd->taac_clks = unstuff_bits(resp, 104, 8) * 100; 162 176 163 - m = UNSTUFF_BITS(resp, 99, 4); 164 - e = UNSTUFF_BITS(resp, 96, 3); 177 + m = unstuff_bits(resp, 99, 4); 178 + e = unstuff_bits(resp, 96, 3); 165 179 csd->max_dtr = tran_exp[e] * tran_mant[m]; 166 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 180 + csd->cmdclass = unstuff_bits(resp, 84, 12); 167 181 168 - e = UNSTUFF_BITS(resp, 47, 3); 169 - m = UNSTUFF_BITS(resp, 62, 12); 182 + e = unstuff_bits(resp, 47, 3); 183 + m = unstuff_bits(resp, 62, 12); 170 184 csd->capacity = (1 + m) << (e + 2); 171 185 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); 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); 180 194 181 195 if (csd->write_blkbits >= 9) { 182 - a = UNSTUFF_BITS(resp, 42, 5); 183 - b = UNSTUFF_BITS(resp, 37, 5); 196 + a = unstuff_bits(resp, 42, 5); 197 + b = unstuff_bits(resp, 37, 5); 184 198 csd->erase_size = (a + 1) * (b + 1); 185 199 csd->erase_size <<= csd->write_blkbits - 9; 186 - csd->wp_grp_size = UNSTUFF_BITS(resp, 32, 5); 200 + csd->wp_grp_size = unstuff_bits(resp, 32, 5); 187 201 } 188 202 189 203 return 0;
+14
drivers/mmc/core/mmc_ops.h
··· 56 56 int mmc_cmdq_disable(struct mmc_card *card); 57 57 int mmc_sanitize(struct mmc_card *card, unsigned int timeout_ms); 58 58 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 + 59 73 #endif 60 74
+53 -67
drivers/mmc/core/sd.c
··· 56 56 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512, 57 57 }; 58 58 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 - 73 59 #define SD_POWEROFF_NOTIFY_TIMEOUT_MS 1000 74 60 #define SD_WRITE_EXTR_SINGLE_TIMEOUT_MS 1000 75 61 ··· 81 95 * SD doesn't currently have a version field so we will 82 96 * have to assume we can parse this. 83 97 */ 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); 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); 96 110 97 111 card->cid.year += 2000; /* SD cards year offset */ 98 112 } ··· 106 120 unsigned int e, m, csd_struct; 107 121 u32 *resp = card->raw_csd; 108 122 109 - csd_struct = UNSTUFF_BITS(resp, 126, 2); 123 + csd_struct = unstuff_bits(resp, 126, 2); 110 124 111 125 switch (csd_struct) { 112 126 case 0: 113 - m = UNSTUFF_BITS(resp, 115, 4); 114 - e = UNSTUFF_BITS(resp, 112, 3); 127 + m = unstuff_bits(resp, 115, 4); 128 + e = unstuff_bits(resp, 112, 3); 115 129 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10; 116 - csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100; 130 + csd->taac_clks = unstuff_bits(resp, 104, 8) * 100; 117 131 118 - m = UNSTUFF_BITS(resp, 99, 4); 119 - e = UNSTUFF_BITS(resp, 96, 3); 132 + m = unstuff_bits(resp, 99, 4); 133 + e = unstuff_bits(resp, 96, 3); 120 134 csd->max_dtr = tran_exp[e] * tran_mant[m]; 121 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 135 + csd->cmdclass = unstuff_bits(resp, 84, 12); 122 136 123 - e = UNSTUFF_BITS(resp, 47, 3); 124 - m = UNSTUFF_BITS(resp, 62, 12); 137 + e = unstuff_bits(resp, 47, 3); 138 + m = unstuff_bits(resp, 62, 12); 125 139 csd->capacity = (1 + m) << (e + 2); 126 140 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); 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); 135 149 136 - if (UNSTUFF_BITS(resp, 46, 1)) { 150 + if (unstuff_bits(resp, 46, 1)) { 137 151 csd->erase_size = 1; 138 152 } else if (csd->write_blkbits >= 9) { 139 - csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; 153 + csd->erase_size = unstuff_bits(resp, 39, 7) + 1; 140 154 csd->erase_size <<= csd->write_blkbits - 9; 141 155 } 142 156 143 - if (UNSTUFF_BITS(resp, 13, 1)) 157 + if (unstuff_bits(resp, 13, 1)) 144 158 mmc_card_set_readonly(card); 145 159 break; 146 160 case 1: ··· 155 169 csd->taac_ns = 0; /* Unused */ 156 170 csd->taac_clks = 0; /* Unused */ 157 171 158 - m = UNSTUFF_BITS(resp, 99, 4); 159 - e = UNSTUFF_BITS(resp, 96, 3); 172 + m = unstuff_bits(resp, 99, 4); 173 + e = unstuff_bits(resp, 96, 3); 160 174 csd->max_dtr = tran_exp[e] * tran_mant[m]; 161 - csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); 162 - csd->c_size = UNSTUFF_BITS(resp, 48, 22); 175 + csd->cmdclass = unstuff_bits(resp, 84, 12); 176 + csd->c_size = unstuff_bits(resp, 48, 22); 163 177 164 178 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */ 165 179 if (csd->c_size >= 0xFFFF) 166 180 mmc_card_set_ext_capacity(card); 167 181 168 - m = UNSTUFF_BITS(resp, 48, 22); 182 + m = unstuff_bits(resp, 48, 22); 169 183 csd->capacity = (1 + m) << 10; 170 184 171 185 csd->read_blkbits = 9; ··· 177 191 csd->write_partial = 0; 178 192 csd->erase_size = 1; 179 193 180 - if (UNSTUFF_BITS(resp, 13, 1)) 194 + if (unstuff_bits(resp, 13, 1)) 181 195 mmc_card_set_readonly(card); 182 196 break; 183 197 default: ··· 203 217 resp[3] = card->raw_scr[1]; 204 218 resp[2] = card->raw_scr[0]; 205 219 206 - scr_struct = UNSTUFF_BITS(resp, 60, 4); 220 + scr_struct = unstuff_bits(resp, 60, 4); 207 221 if (scr_struct != 0) { 208 222 pr_err("%s: unrecognised SCR structure version %d\n", 209 223 mmc_hostname(card->host), scr_struct); 210 224 return -EINVAL; 211 225 } 212 226 213 - scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 214 - scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 227 + scr->sda_vsn = unstuff_bits(resp, 56, 4); 228 + scr->bus_widths = unstuff_bits(resp, 48, 4); 215 229 if (scr->sda_vsn == SCR_SPEC_VER_2) 216 230 /* Check if Physical Layer Spec v3.0 is supported */ 217 - scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1); 231 + scr->sda_spec3 = unstuff_bits(resp, 47, 1); 218 232 219 233 if (scr->sda_spec3) { 220 - scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1); 221 - scr->sda_specx = UNSTUFF_BITS(resp, 38, 4); 234 + scr->sda_spec4 = unstuff_bits(resp, 42, 1); 235 + scr->sda_specx = unstuff_bits(resp, 38, 4); 222 236 } 223 237 224 - if (UNSTUFF_BITS(resp, 55, 1)) 238 + if (unstuff_bits(resp, 55, 1)) 225 239 card->erased_byte = 0xFF; 226 240 else 227 241 card->erased_byte = 0x0; 228 242 229 243 if (scr->sda_spec4) 230 - scr->cmds = UNSTUFF_BITS(resp, 32, 4); 244 + scr->cmds = unstuff_bits(resp, 32, 4); 231 245 else if (scr->sda_spec3) 232 - scr->cmds = UNSTUFF_BITS(resp, 32, 2); 246 + scr->cmds = unstuff_bits(resp, 32, 2); 233 247 234 248 /* SD Spec says: any SD Card shall set at least bits 0 and 2 */ 235 249 if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) || ··· 275 289 kfree(raw_ssr); 276 290 277 291 /* 278 - * UNSTUFF_BITS only works with four u32s so we have to offset the 292 + * unstuff_bits only works with four u32s so we have to offset the 279 293 * bitfield positions accordingly. 280 294 */ 281 - au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4); 295 + au = unstuff_bits(card->raw_ssr, 428 - 384, 4); 282 296 if (au) { 283 297 if (au <= 9 || card->scr.sda_spec3) { 284 298 card->ssr.au = sd_au_size[au]; 285 - es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16); 286 - et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6); 299 + es = unstuff_bits(card->raw_ssr, 408 - 384, 16); 300 + et = unstuff_bits(card->raw_ssr, 402 - 384, 6); 287 301 if (es && et) { 288 - eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2); 302 + eo = unstuff_bits(card->raw_ssr, 400 - 384, 2); 289 303 card->ssr.erase_timeout = (et * 1000) / es; 290 304 card->ssr.erase_offset = eo * 1000; 291 305 } ··· 299 313 * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set 300 314 */ 301 315 resp[3] = card->raw_ssr[6]; 302 - discard_support = UNSTUFF_BITS(resp, 313 - 288, 1); 316 + discard_support = unstuff_bits(resp, 313 - 288, 1); 303 317 card->erase_arg = (card->scr.sda_specx && discard_support) ? 304 318 SD_DISCARD_ARG : SD_ERASE_ARG; 305 319