yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for media (i.e., flash cards) |
| 3 | * |
| 4 | * Copyright 2002 Hewlett-Packard Company |
| 5 | * Copyright 2005-2008 Pierre Ossman |
| 6 | * |
| 7 | * Use consistent with the GNU GPL is permitted, |
| 8 | * provided that this copyright notice is |
| 9 | * preserved in its entirety in all copies and derived works. |
| 10 | * |
| 11 | * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, |
| 12 | * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS |
| 13 | * FITNESS FOR ANY PARTICULAR PURPOSE. |
| 14 | * |
| 15 | * Many thanks to Alessandro Rubini and Jonathan Corbet! |
| 16 | * |
| 17 | * Author: Andrew Christian |
| 18 | * 28 May 2002 |
| 19 | */ |
| 20 | #include <linux/moduleparam.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/hdreg.h> |
| 29 | #include <linux/kdev_t.h> |
| 30 | #include <linux/blkdev.h> |
| 31 | #include <linux/mutex.h> |
| 32 | #include <linux/scatterlist.h> |
| 33 | #include <linux/string_helpers.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/capability.h> |
| 36 | #include <linux/compat.h> |
| 37 | |
| 38 | #include <linux/mmc/ioctl.h> |
| 39 | #include <linux/mmc/card.h> |
| 40 | #include <linux/mmc/host.h> |
| 41 | #include <linux/mmc/mmc.h> |
| 42 | #include <linux/mmc/sd.h> |
| 43 | |
| 44 | #include <asm/uaccess.h> |
| 45 | |
| 46 | #include "queue.h" |
| 47 | #include "../core/core.h" |
| 48 | #include "../core/mmc_ops.h" |
| 49 | |
| 50 | |
| 51 | MODULE_ALIAS("mmc:block"); |
| 52 | #ifdef MODULE_PARAM_PREFIX |
| 53 | #undef MODULE_PARAM_PREFIX |
| 54 | #endif |
| 55 | #define MODULE_PARAM_PREFIX "mmcblk." |
| 56 | |
| 57 | //#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
| 58 | #define MMC_BLK_TIMEOUT_MS (1 * 1 * 1000) /* 1 second timeout. refer EC 616000480970 */ |
| 59 | |
| 60 | #define INAND_CMD38_ARG_EXT_CSD 113 |
| 61 | #define INAND_CMD38_ARG_ERASE 0x00 |
| 62 | #define INAND_CMD38_ARG_TRIM 0x01 |
| 63 | #define INAND_CMD38_ARG_SECERASE 0x80 |
| 64 | #define INAND_CMD38_ARG_SECTRIM1 0x81 |
| 65 | #define INAND_CMD38_ARG_SECTRIM2 0x88 |
| 66 | |
| 67 | static DEFINE_MUTEX(block_mutex); |
| 68 | |
| 69 | /* |
| 70 | * The defaults come from config options but can be overriden by module |
| 71 | * or bootarg options. |
| 72 | */ |
| 73 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
| 74 | |
| 75 | /* |
| 76 | * We've only got one major, so number of mmcblk devices is |
| 77 | * limited to 256 / number of minors per device. |
| 78 | */ |
| 79 | static int max_devices; |
| 80 | |
| 81 | /* 256 minors, so at most 256 separate devices */ |
| 82 | static DECLARE_BITMAP(dev_use, 256); |
| 83 | static DECLARE_BITMAP(name_use, 256); |
| 84 | |
| 85 | /* |
| 86 | * There is one mmc_blk_data per slot. |
| 87 | */ |
| 88 | struct mmc_blk_data { |
| 89 | spinlock_t lock; |
| 90 | struct gendisk *disk; |
| 91 | struct mmc_queue queue; |
| 92 | struct list_head part; |
| 93 | |
| 94 | unsigned int flags; |
| 95 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ |
| 96 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ |
| 97 | |
| 98 | unsigned int usage; |
| 99 | unsigned int read_only; |
| 100 | unsigned int part_type; |
| 101 | unsigned int name_idx; |
| 102 | unsigned int reset_done; |
| 103 | #define MMC_BLK_READ BIT(0) |
| 104 | #define MMC_BLK_WRITE BIT(1) |
| 105 | #define MMC_BLK_DISCARD BIT(2) |
| 106 | #define MMC_BLK_SECDISCARD BIT(3) |
| 107 | |
| 108 | /* |
| 109 | * Only set in main mmc_blk_data associated |
| 110 | * with mmc_card with mmc_set_drvdata, and keeps |
| 111 | * track of the current selected device partition. |
| 112 | */ |
| 113 | unsigned int part_curr; |
| 114 | struct device_attribute force_ro; |
| 115 | struct device_attribute power_ro_lock; |
| 116 | int area_type; |
| 117 | }; |
| 118 | |
| 119 | static DEFINE_MUTEX(open_lock); |
| 120 | |
| 121 | enum mmc_blk_status { |
| 122 | MMC_BLK_SUCCESS = 0, |
| 123 | MMC_BLK_PARTIAL, |
| 124 | MMC_BLK_CMD_ERR, |
| 125 | MMC_BLK_RETRY, |
| 126 | MMC_BLK_ABORT, |
| 127 | MMC_BLK_DATA_ERR, |
| 128 | MMC_BLK_ECC_ERR, |
| 129 | MMC_BLK_NOMEDIUM, |
| 130 | }; |
| 131 | |
| 132 | module_param(perdev_minors, int, 0444); |
| 133 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); |
| 134 | |
| 135 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 136 | { |
| 137 | struct mmc_blk_data *md; |
| 138 | |
| 139 | mutex_lock(&open_lock); |
| 140 | md = disk->private_data; |
| 141 | if (md && md->usage == 0) |
| 142 | md = NULL; |
| 143 | if (md) |
| 144 | md->usage++; |
| 145 | mutex_unlock(&open_lock); |
| 146 | |
| 147 | return md; |
| 148 | } |
| 149 | |
| 150 | static inline int mmc_get_devidx(struct gendisk *disk) |
| 151 | { |
| 152 | int devidx = disk->first_minor / perdev_minors; |
| 153 | return devidx; |
| 154 | } |
| 155 | |
| 156 | static void mmc_blk_put(struct mmc_blk_data *md) |
| 157 | { |
| 158 | mutex_lock(&open_lock); |
| 159 | md->usage--; |
| 160 | if (md->usage == 0) { |
| 161 | int devidx = mmc_get_devidx(md->disk); |
| 162 | blk_cleanup_queue(md->queue.queue); |
| 163 | |
| 164 | __clear_bit(devidx, dev_use); |
| 165 | |
| 166 | put_disk(md->disk); |
| 167 | kfree(md); |
| 168 | } |
| 169 | mutex_unlock(&open_lock); |
| 170 | } |
| 171 | |
| 172 | static ssize_t power_ro_lock_show(struct device *dev, |
| 173 | struct device_attribute *attr, char *buf) |
| 174 | { |
| 175 | int ret; |
| 176 | struct mmc_blk_data *md; |
| 177 | struct mmc_card *card; |
| 178 | int locked = 0; |
| 179 | |
| 180 | md = mmc_blk_get(dev_to_disk(dev)); |
| 181 | #ifdef CONFIG_KLOCWORK |
| 182 | if (!md) |
| 183 | return 0; |
| 184 | #endif |
| 185 | card = md->queue.card; |
| 186 | |
| 187 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) |
| 188 | locked = 2; |
| 189 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) |
| 190 | locked = 1; |
| 191 | |
| 192 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static ssize_t power_ro_lock_store(struct device *dev, |
| 198 | struct device_attribute *attr, const char *buf, size_t count) |
| 199 | { |
| 200 | int ret; |
| 201 | struct mmc_blk_data *md, *part_md; |
| 202 | struct mmc_card *card; |
| 203 | unsigned long set; |
| 204 | |
| 205 | if (kstrtoul(buf, 0, &set)) |
| 206 | return -EINVAL; |
| 207 | |
| 208 | if (set != 1) |
| 209 | return count; |
| 210 | |
| 211 | md = mmc_blk_get(dev_to_disk(dev)); |
| 212 | #ifdef CONFIG_KLOCWORK |
| 213 | if (!md) |
| 214 | return -ENODEV; |
| 215 | #endif |
| 216 | card = md->queue.card; |
| 217 | |
| 218 | mmc_claim_host(card->host); |
| 219 | |
| 220 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, |
| 221 | card->ext_csd.boot_ro_lock | |
| 222 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, |
| 223 | card->ext_csd.part_time); |
| 224 | if (ret) |
| 225 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret); |
| 226 | else |
| 227 | card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN; |
| 228 | |
| 229 | mmc_release_host(card->host); |
| 230 | |
| 231 | if (!ret) { |
| 232 | pr_info("%s: Locking boot partition ro until next power on\n", |
| 233 | md->disk->disk_name); |
| 234 | set_disk_ro(md->disk, 1); |
| 235 | |
| 236 | list_for_each_entry(part_md, &md->part, part) |
| 237 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { |
| 238 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); |
| 239 | set_disk_ro(part_md->disk, 1); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | mmc_blk_put(md); |
| 244 | return count; |
| 245 | } |
| 246 | |
| 247 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
| 248 | char *buf) |
| 249 | { |
| 250 | int ret; |
| 251 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 252 | |
| 253 | #ifdef CONFIG_KLOCWORK |
| 254 | if (!md) |
| 255 | return 0; |
| 256 | #endif |
| 257 | |
| 258 | ret = snprintf(buf, PAGE_SIZE, "%d", |
| 259 | get_disk_ro(dev_to_disk(dev)) ^ |
| 260 | md->read_only); |
| 261 | mmc_blk_put(md); |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, |
| 266 | const char *buf, size_t count) |
| 267 | { |
| 268 | int ret; |
| 269 | char *end; |
| 270 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 271 | unsigned long set = simple_strtoul(buf, &end, 0); |
| 272 | #ifdef CONFIG_KLOCWORK |
| 273 | if (!md) |
| 274 | return -ENODEV; |
| 275 | #endif |
| 276 | if (end == buf) { |
| 277 | ret = -EINVAL; |
| 278 | goto out; |
| 279 | } |
| 280 | |
| 281 | set_disk_ro(dev_to_disk(dev), set || md->read_only); |
| 282 | ret = count; |
| 283 | out: |
| 284 | mmc_blk_put(md); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
| 289 | { |
| 290 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
| 291 | int ret = -ENXIO; |
| 292 | |
| 293 | mutex_lock(&block_mutex); |
| 294 | if (md) { |
| 295 | if (md->usage == 2) |
| 296 | check_disk_change(bdev); |
| 297 | ret = 0; |
| 298 | |
| 299 | if ((mode & FMODE_WRITE) && md->read_only) { |
| 300 | mmc_blk_put(md); |
| 301 | ret = -EROFS; |
| 302 | } |
| 303 | } |
| 304 | mutex_unlock(&block_mutex); |
| 305 | |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static int mmc_blk_release(struct gendisk *disk, fmode_t mode) |
| 310 | { |
| 311 | struct mmc_blk_data *md = disk->private_data; |
| 312 | |
| 313 | mutex_lock(&block_mutex); |
| 314 | mmc_blk_put(md); |
| 315 | mutex_unlock(&block_mutex); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int |
| 320 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 321 | { |
| 322 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 323 | geo->heads = 4; |
| 324 | geo->sectors = 16; |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | struct mmc_blk_ioc_data { |
| 329 | struct mmc_ioc_cmd ic; |
| 330 | unsigned char *buf; |
| 331 | u64 buf_bytes; |
| 332 | }; |
| 333 | |
| 334 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( |
| 335 | struct mmc_ioc_cmd __user *user) |
| 336 | { |
| 337 | struct mmc_blk_ioc_data *idata; |
| 338 | int err; |
| 339 | |
| 340 | idata = kzalloc(sizeof(*idata), GFP_KERNEL); |
| 341 | if (!idata) { |
| 342 | err = -ENOMEM; |
| 343 | goto out; |
| 344 | } |
| 345 | |
| 346 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { |
| 347 | err = -EFAULT; |
| 348 | goto idata_err; |
| 349 | } |
| 350 | if((idata->ic.blocks < MMC_IOC_MAX_BYTES)&&(idata->ic.blksz < MMC_IOC_MAX_BYTES)) |
| 351 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; |
| 352 | |
| 353 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { |
| 354 | err = -EOVERFLOW; |
| 355 | goto idata_err; |
| 356 | } |
| 357 | |
| 358 | if (!idata->buf_bytes) |
| 359 | return idata; |
| 360 | |
| 361 | idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL); |
| 362 | if (!idata->buf) { |
| 363 | err = -ENOMEM; |
| 364 | goto idata_err; |
| 365 | } |
| 366 | |
| 367 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) |
| 368 | idata->ic.data_ptr, idata->buf_bytes)) { |
| 369 | err = -EFAULT; |
| 370 | goto copy_err; |
| 371 | } |
| 372 | |
| 373 | return idata; |
| 374 | |
| 375 | copy_err: |
| 376 | kfree(idata->buf); |
| 377 | idata_err: |
| 378 | kfree(idata); |
| 379 | out: |
| 380 | return ERR_PTR(err); |
| 381 | } |
| 382 | |
| 383 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, |
| 384 | struct mmc_ioc_cmd __user *ic_ptr) |
| 385 | { |
| 386 | struct mmc_blk_ioc_data *idata; |
| 387 | struct mmc_blk_data *md; |
| 388 | struct mmc_card *card; |
| 389 | struct mmc_command cmd = {0}; |
| 390 | struct mmc_data data = {0}; |
| 391 | struct mmc_request mrq = {NULL}; |
| 392 | struct scatterlist sg; |
| 393 | int err = 0; |
| 394 | |
| 395 | /* |
| 396 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the |
| 397 | * whole block device, not on a partition. This prevents overspray |
| 398 | * between sibling partitions. |
| 399 | */ |
| 400 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) |
| 401 | return -EPERM; |
| 402 | |
| 403 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
| 404 | if (IS_ERR(idata)) |
| 405 | return PTR_ERR(idata); |
| 406 | |
| 407 | md = mmc_blk_get(bdev->bd_disk); |
| 408 | if (!md) { |
| 409 | err = -EINVAL; |
| 410 | goto cmd_done; |
| 411 | } |
| 412 | |
| 413 | card = md->queue.card; |
| 414 | if (IS_ERR(card)) { |
| 415 | err = PTR_ERR(card); |
| 416 | goto cmd_done; |
| 417 | } |
| 418 | |
| 419 | cmd.opcode = idata->ic.opcode; |
| 420 | cmd.arg = idata->ic.arg; |
| 421 | cmd.flags = idata->ic.flags; |
| 422 | |
| 423 | if (idata->buf_bytes) { |
| 424 | data.sg = &sg; |
| 425 | data.sg_len = 1; |
| 426 | data.blksz = idata->ic.blksz; |
| 427 | data.blocks = idata->ic.blocks; |
| 428 | |
| 429 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); |
| 430 | |
| 431 | if (idata->ic.write_flag) |
| 432 | data.flags = MMC_DATA_WRITE; |
| 433 | else |
| 434 | data.flags = MMC_DATA_READ; |
| 435 | |
| 436 | /* data.flags must already be set before doing this. */ |
| 437 | mmc_set_data_timeout(&data, card); |
| 438 | |
| 439 | /* Allow overriding the timeout_ns for empirical tuning. */ |
| 440 | if (idata->ic.data_timeout_ns) |
| 441 | data.timeout_ns = idata->ic.data_timeout_ns; |
| 442 | |
| 443 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { |
| 444 | /* |
| 445 | * Pretend this is a data transfer and rely on the |
| 446 | * host driver to compute timeout. When all host |
| 447 | * drivers support cmd.cmd_timeout for R1B, this |
| 448 | * can be changed to: |
| 449 | * |
| 450 | * mrq.data = NULL; |
| 451 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; |
| 452 | */ |
| 453 | if(idata->ic.cmd_timeout_ms < (UINT_MAX/100000)) |
| 454 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; |
| 455 | } |
| 456 | |
| 457 | mrq.data = &data; |
| 458 | } |
| 459 | |
| 460 | mrq.cmd = &cmd; |
| 461 | |
| 462 | mmc_claim_host(card->host); |
| 463 | |
| 464 | if (idata->ic.is_acmd) { |
| 465 | err = mmc_app_cmd(card->host, card); |
| 466 | if (err) |
| 467 | goto cmd_rel_host; |
| 468 | } |
| 469 | |
| 470 | mmc_wait_for_req(card->host, &mrq); |
| 471 | |
| 472 | if (cmd.error) { |
| 473 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", |
| 474 | __func__, cmd.error); |
| 475 | err = cmd.error; |
| 476 | goto cmd_rel_host; |
| 477 | } |
| 478 | if (data.error) { |
| 479 | dev_err(mmc_dev(card->host), "%s: data error %d\n", |
| 480 | __func__, data.error); |
| 481 | err = data.error; |
| 482 | goto cmd_rel_host; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * According to the SD specs, some commands require a delay after |
| 487 | * issuing the command. |
| 488 | */ |
| 489 | if ((idata->ic.postsleep_min_us)&&(idata->ic.postsleep_min_us < idata->ic.postsleep_max_us)) |
| 490 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); |
| 491 | |
| 492 | if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) { |
| 493 | err = -EFAULT; |
| 494 | goto cmd_rel_host; |
| 495 | } |
| 496 | |
| 497 | if (!idata->ic.write_flag) { |
| 498 | if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr, |
| 499 | idata->buf, idata->buf_bytes)) { |
| 500 | err = -EFAULT; |
| 501 | goto cmd_rel_host; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | cmd_rel_host: |
| 506 | mmc_release_host(card->host); |
| 507 | |
| 508 | cmd_done: |
| 509 | if (md) |
| 510 | mmc_blk_put(md); |
| 511 | kfree(idata->buf); |
| 512 | kfree(idata); |
| 513 | return err; |
| 514 | } |
| 515 | |
| 516 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
| 517 | unsigned int cmd, unsigned long arg) |
| 518 | { |
| 519 | int ret = -EINVAL; |
| 520 | if (cmd == MMC_IOC_CMD) |
| 521 | ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg); |
| 522 | return ret; |
| 523 | } |
| 524 | |
| 525 | #ifdef CONFIG_COMPAT |
| 526 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 527 | unsigned int cmd, unsigned long arg) |
| 528 | { |
| 529 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | static const struct block_device_operations mmc_bdops = { |
| 534 | .open = mmc_blk_open, |
| 535 | .release = mmc_blk_release, |
| 536 | .getgeo = mmc_blk_getgeo, |
| 537 | .owner = THIS_MODULE, |
| 538 | .ioctl = mmc_blk_ioctl, |
| 539 | #ifdef CONFIG_COMPAT |
| 540 | .compat_ioctl = mmc_blk_compat_ioctl, |
| 541 | #endif |
| 542 | }; |
| 543 | |
| 544 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
| 545 | struct mmc_blk_data *md) |
| 546 | { |
| 547 | int ret; |
| 548 | struct mmc_blk_data *main_md = mmc_get_drvdata(card); |
| 549 | |
| 550 | if(!main_md) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | if (main_md->part_curr == md->part_type) |
| 554 | return 0; |
| 555 | |
| 556 | if (mmc_card_mmc(card)) { |
| 557 | u8 part_config = card->ext_csd.part_config; |
| 558 | |
| 559 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 560 | part_config |= md->part_type; |
| 561 | |
| 562 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 563 | EXT_CSD_PART_CONFIG, part_config, |
| 564 | card->ext_csd.part_time); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | card->ext_csd.part_config = part_config; |
| 569 | } |
| 570 | |
| 571 | main_md->part_curr = md->part_type; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 576 | { |
| 577 | int err; |
| 578 | u32 result; |
| 579 | __be32 *blocks; |
| 580 | |
| 581 | struct mmc_request mrq = {NULL}; |
| 582 | struct mmc_command cmd = {0}; |
| 583 | struct mmc_data data = {0}; |
| 584 | unsigned int timeout_us; |
| 585 | |
| 586 | struct scatterlist sg; |
| 587 | |
| 588 | cmd.opcode = MMC_APP_CMD; |
| 589 | cmd.arg = card->rca << 16; |
| 590 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 591 | |
| 592 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 593 | if (err) |
| 594 | return (u32)-1; |
| 595 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) |
| 596 | return (u32)-1; |
| 597 | |
| 598 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 599 | |
| 600 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 601 | cmd.arg = 0; |
| 602 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 603 | |
| 604 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 605 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 606 | |
| 607 | timeout_us = data.timeout_ns / 1000; |
| 608 | timeout_us += data.timeout_clks * 1000 / |
| 609 | (card->host->ios.clock / 1000); |
| 610 | |
| 611 | if (timeout_us > 100000) { |
| 612 | data.timeout_ns = 100000000; |
| 613 | data.timeout_clks = 0; |
| 614 | } |
| 615 | |
| 616 | data.blksz = 4; |
| 617 | data.blocks = 1; |
| 618 | data.flags = MMC_DATA_READ; |
| 619 | data.sg = &sg; |
| 620 | data.sg_len = 1; |
| 621 | |
| 622 | mrq.cmd = &cmd; |
| 623 | mrq.data = &data; |
| 624 | |
| 625 | #ifdef CONFIG_KLOCWORK |
| 626 | blocks = kzalloc(4, GFP_KERNEL); |
| 627 | #else |
| 628 | blocks = kmalloc(4, GFP_KERNEL); |
| 629 | #endif |
| 630 | if (!blocks) |
| 631 | return (u32)-1; |
| 632 | |
| 633 | sg_init_one(&sg, blocks, 4); |
| 634 | |
| 635 | mmc_wait_for_req(card->host, &mrq); |
| 636 | |
| 637 | result = ntohl(*blocks); |
| 638 | kfree(blocks); |
| 639 | |
| 640 | if (cmd.error || data.error) |
| 641 | result = (u32)-1; |
| 642 | |
| 643 | return result; |
| 644 | } |
| 645 | |
| 646 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, |
| 647 | bool hw_busy_detect, struct request *req, bool *gen_err) |
| 648 | { |
| 649 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 650 | int err = 0; |
| 651 | u32 status; |
| 652 | int check_cnt = 0; |
| 653 | |
| 654 | do { |
| 655 | err = __mmc_send_status(card, &status, 5); |
| 656 | if (err) { |
| 657 | pr_err("%s: error %d requesting status\n", |
| 658 | req->rq_disk->disk_name, err); |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | if (status & R1_ERROR) { |
| 663 | pr_err("%s: %s: error sending status cmd, status %#x\n", |
| 664 | req->rq_disk->disk_name, __func__, status); |
| 665 | *gen_err = true; |
| 666 | } |
| 667 | |
| 668 | /* We may rely on the host hw to handle busy detection.*/ |
| 669 | if (hw_busy_detect && |
| 670 | (card->host->caps & MMC_CAP_WAIT_WHILE_BUSY)) |
| 671 | break; |
| 672 | |
| 673 | /* |
| 674 | * Timeout if the device never becomes ready for data and never |
| 675 | * leaves the program state. |
| 676 | */ |
| 677 | if (time_after(jiffies, timeout)) { |
| 678 | pr_err("%s: Card stuck in programming state! %s %s\n", |
| 679 | mmc_hostname(card->host), |
| 680 | req->rq_disk->disk_name, __func__); |
| 681 | return -ETIMEDOUT; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | check_cnt--; |
| 686 | if(!check_cnt) |
| 687 | break; |
| 688 | */ |
| 689 | check_cnt++; |
| 690 | if (check_cnt > 30) |
| 691 | mmc_delay(10); |
| 692 | else if (check_cnt > 20) |
| 693 | mmc_delay(1); |
| 694 | else if (check_cnt > 5) |
| 695 | udelay(100); |
| 696 | |
| 697 | /* |
| 698 | * Some cards mishandle the status bits, |
| 699 | * so make sure to check both the busy |
| 700 | * indication and the card state. |
| 701 | */ |
| 702 | } while (!(status & R1_READY_FOR_DATA) || |
| 703 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); |
| 704 | |
| 705 | return err; |
| 706 | } |
| 707 | |
| 708 | static int send_stop(struct mmc_card *card, u32 *status) |
| 709 | { |
| 710 | struct mmc_command cmd = {0}; |
| 711 | int err; |
| 712 | |
| 713 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 714 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 715 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 716 | if (err == 0) |
| 717 | *status = cmd.resp[0]; |
| 718 | return err; |
| 719 | } |
| 720 | |
| 721 | static int get_card_status(struct mmc_card *card, u32 *status, int retries) |
| 722 | { |
| 723 | struct mmc_command cmd = {0}; |
| 724 | int err; |
| 725 | |
| 726 | cmd.opcode = MMC_SEND_STATUS; |
| 727 | if (!mmc_host_is_spi(card->host)) |
| 728 | cmd.arg = card->rca << 16; |
| 729 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 730 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
| 731 | if (err == 0) |
| 732 | *status = cmd.resp[0]; |
| 733 | return err; |
| 734 | } |
| 735 | |
| 736 | #define ERR_NOMEDIUM 3 |
| 737 | #define ERR_RETRY 2 |
| 738 | #define ERR_ABORT 1 |
| 739 | #define ERR_CONTINUE 0 |
| 740 | |
| 741 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, |
| 742 | bool status_valid, u32 status) |
| 743 | { |
| 744 | switch (error) { |
| 745 | case -EILSEQ: |
| 746 | /* response crc error, retry the r/w cmd */ |
| 747 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 748 | req->rq_disk->disk_name, "response CRC error", |
| 749 | name, status); |
| 750 | return ERR_RETRY; |
| 751 | |
| 752 | case -ETIMEDOUT: |
| 753 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 754 | req->rq_disk->disk_name, "timed out", name, status); |
| 755 | |
| 756 | /* If the status cmd initially failed, retry the r/w cmd */ |
| 757 | if (!status_valid) { |
| 758 | pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name); |
| 759 | return ERR_RETRY; |
| 760 | } |
| 761 | /* |
| 762 | * If it was a r/w cmd crc error, or illegal command |
| 763 | * (eg, issued in wrong state) then retry - we should |
| 764 | * have corrected the state problem above. |
| 765 | */ |
| 766 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
| 767 | pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name); |
| 768 | return ERR_RETRY; |
| 769 | } |
| 770 | |
| 771 | /* Otherwise abort the command */ |
| 772 | pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name); |
| 773 | return ERR_ABORT; |
| 774 | |
| 775 | default: |
| 776 | /* We don't understand the error code the driver gave us */ |
| 777 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", |
| 778 | req->rq_disk->disk_name, error, status); |
| 779 | return ERR_ABORT; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /* |
| 784 | * Initial r/w and stop cmd error recovery. |
| 785 | * We don't know whether the card received the r/w cmd or not, so try to |
| 786 | * restore things back to a sane state. Essentially, we do this as follows: |
| 787 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 788 | * the status word will reflect the failed status cmd, not the failed |
| 789 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 790 | * longer communicate with the card. |
| 791 | * - Check the card state. If the card received the cmd but there was a |
| 792 | * transient problem with the response, it might still be in a data transfer |
| 793 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 794 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 795 | * transient, so retry the cmd. |
| 796 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 797 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 798 | * illegal cmd, retry. |
| 799 | * Otherwise we don't understand what happened, so abort. |
| 800 | */ |
| 801 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, |
| 802 | struct mmc_blk_request *brq, int *ecc_err, int *gen_err) |
| 803 | { |
| 804 | bool prev_cmd_status_valid = true; |
| 805 | u32 status, stop_status = 0; |
| 806 | int err, retry; |
| 807 | |
| 808 | if (mmc_card_removed(card)) |
| 809 | return ERR_NOMEDIUM; |
| 810 | |
| 811 | /* |
| 812 | * Try to get card status which indicates both the card state |
| 813 | * and why there was no response. If the first attempt fails, |
| 814 | * we can't be sure the returned status is for the r/w command. |
| 815 | */ |
| 816 | for (retry = 2; retry >= 0; retry--) { |
| 817 | err = get_card_status(card, &status, 0); |
| 818 | if (!err) |
| 819 | break; |
| 820 | |
| 821 | prev_cmd_status_valid = false; |
| 822 | pr_err("%s: error %d sending status command, %sing\n", |
| 823 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); |
| 824 | } |
| 825 | |
| 826 | /* We couldn't get a response from the card. Give up. */ |
| 827 | if (err) { |
| 828 | /* Check if the card is removed */ |
| 829 | if (mmc_detect_card_removed(card->host)) |
| 830 | return ERR_NOMEDIUM; |
| 831 | return ERR_ABORT; |
| 832 | } |
| 833 | |
| 834 | /* Flag ECC errors */ |
| 835 | if ((status & R1_CARD_ECC_FAILED) || |
| 836 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || |
| 837 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) |
| 838 | *ecc_err = 1; |
| 839 | |
| 840 | /* Flag General errors */ |
| 841 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) |
| 842 | if ((status & R1_ERROR) || |
| 843 | (brq->stop.resp[0] & R1_ERROR)) { |
| 844 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", |
| 845 | req->rq_disk->disk_name, __func__, |
| 846 | brq->stop.resp[0], status); |
| 847 | *gen_err = 1; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Check the current card state. If it is in some data transfer |
| 852 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 853 | */ |
| 854 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 855 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
| 856 | err = send_stop(card, &stop_status); |
| 857 | if (err) |
| 858 | pr_err("%s: error %d sending stop command\n", |
| 859 | req->rq_disk->disk_name, err); |
| 860 | |
| 861 | /* |
| 862 | * If the stop cmd also timed out, the card is probably |
| 863 | * not present, so abort. Other errors are bad news too. |
| 864 | */ |
| 865 | if (err) |
| 866 | return ERR_ABORT; |
| 867 | if (stop_status & R1_CARD_ECC_FAILED) |
| 868 | *ecc_err = 1; |
| 869 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) |
| 870 | if (stop_status & R1_ERROR) { |
| 871 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", |
| 872 | req->rq_disk->disk_name, __func__, |
| 873 | stop_status); |
| 874 | *gen_err = 1; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /* Check for set block count errors */ |
| 879 | if (brq->sbc.error) |
| 880 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 881 | prev_cmd_status_valid, status); |
| 882 | |
| 883 | /* Check for r/w command errors */ |
| 884 | if (brq->cmd.error) |
| 885 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 886 | prev_cmd_status_valid, status); |
| 887 | |
| 888 | /* Data errors */ |
| 889 | if (!brq->stop.error) |
| 890 | return ERR_CONTINUE; |
| 891 | |
| 892 | /* Now for stop errors. These aren't fatal to the transfer. */ |
| 893 | pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
| 894 | req->rq_disk->disk_name, brq->stop.error, |
| 895 | brq->cmd.resp[0], status); |
| 896 | |
| 897 | /* |
| 898 | * Subsitute in our own stop status as this will give the error |
| 899 | * state which happened during the execution of the r/w command. |
| 900 | */ |
| 901 | if (stop_status) { |
| 902 | brq->stop.resp[0] = stop_status; |
| 903 | brq->stop.error = 0; |
| 904 | } |
| 905 | return ERR_CONTINUE; |
| 906 | } |
| 907 | |
| 908 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
| 909 | int type) |
| 910 | { |
| 911 | int err; |
| 912 | |
| 913 | if (md->reset_done & type) |
| 914 | return -EEXIST; |
| 915 | |
| 916 | md->reset_done |= type; |
| 917 | err = mmc_hw_reset(host); |
| 918 | /* Ensure we switch back to the correct partition */ |
| 919 | if (err != -EOPNOTSUPP) { |
| 920 | struct mmc_blk_data *main_md = mmc_get_drvdata(host->card); |
| 921 | int part_err; |
| 922 | |
| 923 | if(!main_md) |
| 924 | return -EINVAL; |
| 925 | |
| 926 | main_md->part_curr = main_md->part_type; |
| 927 | part_err = mmc_blk_part_switch(host->card, md); |
| 928 | if (part_err) { |
| 929 | /* |
| 930 | * We have failed to get back into the correct |
| 931 | * partition, so we need to abort the whole request. |
| 932 | */ |
| 933 | return -ENODEV; |
| 934 | } |
| 935 | } |
| 936 | return err; |
| 937 | } |
| 938 | |
| 939 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) |
| 940 | { |
| 941 | md->reset_done &= ~type; |
| 942 | } |
| 943 | |
| 944 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 945 | { |
| 946 | struct mmc_blk_data *md = mq->data; |
| 947 | struct mmc_card *card = md->queue.card; |
| 948 | unsigned int from, nr, arg; |
| 949 | int err = 0, type = MMC_BLK_DISCARD; |
| 950 | |
| 951 | if (!mmc_can_erase(card)) { |
| 952 | err = -EOPNOTSUPP; |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | from = blk_rq_pos(req); |
| 957 | nr = blk_rq_sectors(req); |
| 958 | |
| 959 | if (mmc_can_discard(card)) |
| 960 | arg = MMC_DISCARD_ARG; |
| 961 | else if (mmc_can_trim(card)) |
| 962 | arg = MMC_TRIM_ARG; |
| 963 | else |
| 964 | arg = MMC_ERASE_ARG; |
| 965 | retry: |
| 966 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 967 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 968 | INAND_CMD38_ARG_EXT_CSD, |
| 969 | arg == MMC_TRIM_ARG ? |
| 970 | INAND_CMD38_ARG_TRIM : |
| 971 | INAND_CMD38_ARG_ERASE, |
| 972 | 0); |
| 973 | if (err) |
| 974 | goto out; |
| 975 | } |
| 976 | err = mmc_erase(card, from, nr, arg); |
| 977 | out: |
| 978 | if (err == -EIO && !mmc_blk_reset(md, card->host, type)) |
| 979 | goto retry; |
| 980 | if (!err) |
| 981 | mmc_blk_reset_success(md, type); |
| 982 | spin_lock_irq(&md->lock); |
| 983 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 984 | spin_unlock_irq(&md->lock); |
| 985 | |
| 986 | return err ? 0 : 1; |
| 987 | } |
| 988 | |
| 989 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 990 | struct request *req) |
| 991 | { |
| 992 | struct mmc_blk_data *md = mq->data; |
| 993 | struct mmc_card *card = md->queue.card; |
| 994 | unsigned int from, nr, arg, trim_arg, erase_arg; |
| 995 | int err = 0, type = MMC_BLK_SECDISCARD; |
| 996 | |
| 997 | if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) { |
| 998 | err = -EOPNOTSUPP; |
| 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | from = blk_rq_pos(req); |
| 1003 | nr = blk_rq_sectors(req); |
| 1004 | |
| 1005 | /* The sanitize operation is supported at v4.5 only */ |
| 1006 | if (mmc_can_sanitize(card)) { |
| 1007 | erase_arg = MMC_ERASE_ARG; |
| 1008 | trim_arg = MMC_TRIM_ARG; |
| 1009 | } else { |
| 1010 | erase_arg = MMC_SECURE_ERASE_ARG; |
| 1011 | trim_arg = MMC_SECURE_TRIM1_ARG; |
| 1012 | } |
| 1013 | |
| 1014 | if (mmc_erase_group_aligned(card, from, nr)) |
| 1015 | arg = erase_arg; |
| 1016 | else if (mmc_can_trim(card)) |
| 1017 | arg = trim_arg; |
| 1018 | else { |
| 1019 | err = -EINVAL; |
| 1020 | goto out; |
| 1021 | } |
| 1022 | retry: |
| 1023 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1024 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1025 | INAND_CMD38_ARG_EXT_CSD, |
| 1026 | arg == MMC_SECURE_TRIM1_ARG ? |
| 1027 | INAND_CMD38_ARG_SECTRIM1 : |
| 1028 | INAND_CMD38_ARG_SECERASE, |
| 1029 | 0); |
| 1030 | if (err) |
| 1031 | goto out_retry; |
| 1032 | } |
| 1033 | |
| 1034 | err = mmc_erase(card, from, nr, arg); |
| 1035 | if (err == -EIO) |
| 1036 | goto out_retry; |
| 1037 | if (err) |
| 1038 | goto out; |
| 1039 | |
| 1040 | if (arg == MMC_SECURE_TRIM1_ARG) { |
| 1041 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1042 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1043 | INAND_CMD38_ARG_EXT_CSD, |
| 1044 | INAND_CMD38_ARG_SECTRIM2, |
| 1045 | 0); |
| 1046 | if (err) |
| 1047 | goto out_retry; |
| 1048 | } |
| 1049 | |
| 1050 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
| 1051 | if (err == -EIO) |
| 1052 | goto out_retry; |
| 1053 | if (err) |
| 1054 | goto out; |
| 1055 | } |
| 1056 | |
| 1057 | if (mmc_can_sanitize(card)) |
| 1058 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1059 | EXT_CSD_SANITIZE_START, 1, 0); |
| 1060 | out_retry: |
| 1061 | if (err && !mmc_blk_reset(md, card->host, type)) |
| 1062 | goto retry; |
| 1063 | if (!err) |
| 1064 | mmc_blk_reset_success(md, type); |
| 1065 | out: |
| 1066 | spin_lock_irq(&md->lock); |
| 1067 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 1068 | spin_unlock_irq(&md->lock); |
| 1069 | |
| 1070 | return err ? 0 : 1; |
| 1071 | } |
| 1072 | |
| 1073 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 1074 | { |
| 1075 | struct mmc_blk_data *md = mq->data; |
| 1076 | struct mmc_card *card = md->queue.card; |
| 1077 | int ret = 0; |
| 1078 | |
| 1079 | ret = mmc_flush_cache(card); |
| 1080 | if (ret) |
| 1081 | ret = -EIO; |
| 1082 | |
| 1083 | spin_lock_irq(&md->lock); |
| 1084 | __blk_end_request_all(req, ret); |
| 1085 | spin_unlock_irq(&md->lock); |
| 1086 | |
| 1087 | return ret ? 0 : 1; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Reformat current write as a reliable write, supporting |
| 1092 | * both legacy and the enhanced reliable write MMC cards. |
| 1093 | * In each transfer we'll handle only as much as a single |
| 1094 | * reliable write can handle, thus finish the request in |
| 1095 | * partial completions. |
| 1096 | */ |
| 1097 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 1098 | struct mmc_card *card, |
| 1099 | struct request *req) |
| 1100 | { |
| 1101 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 1102 | /* Legacy mode imposes restrictions on transfers. */ |
| 1103 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 1104 | brq->data.blocks = 1; |
| 1105 | |
| 1106 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 1107 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 1108 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 1109 | brq->data.blocks = 1; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | #define CMD_ERRORS \ |
| 1114 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ |
| 1115 | R1_ADDRESS_ERROR | /* Misaligned address */ \ |
| 1116 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
| 1117 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ |
| 1118 | R1_CC_ERROR | /* Card controller error */ \ |
| 1119 | R1_ERROR) /* General/unknown error */ |
| 1120 | |
| 1121 | static int mmc_blk_err_check(struct mmc_card *card, |
| 1122 | struct mmc_async_req *areq) |
| 1123 | { |
| 1124 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
| 1125 | mmc_active); |
| 1126 | struct mmc_blk_request *brq = &mq_mrq->brq; |
| 1127 | struct request *req = mq_mrq->req; |
| 1128 | int ecc_err = 0, gen_err = 0; |
| 1129 | // int check_cnt = 100; |
| 1130 | /* |
| 1131 | * sbc.error indicates a problem with the set block count |
| 1132 | * command. No data will have been transferred. |
| 1133 | * |
| 1134 | * cmd.error indicates a problem with the r/w command. No |
| 1135 | * data will have been transferred. |
| 1136 | * |
| 1137 | * stop.error indicates a problem with the stop command. Data |
| 1138 | * may have been transferred, or may still be transferring. |
| 1139 | */ |
| 1140 | if (brq->sbc.error || brq->cmd.error || |
| 1141 | brq->stop.error || brq->data.error) { |
| 1142 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
| 1143 | case ERR_RETRY: |
| 1144 | return MMC_BLK_RETRY; |
| 1145 | case ERR_ABORT: |
| 1146 | return MMC_BLK_ABORT; |
| 1147 | case ERR_NOMEDIUM: |
| 1148 | return MMC_BLK_NOMEDIUM; |
| 1149 | case ERR_CONTINUE: |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Check for errors relating to the execution of the |
| 1156 | * initial command - such as address errors. No data |
| 1157 | * has been transferred. |
| 1158 | */ |
| 1159 | if (brq->cmd.resp[0] & CMD_ERRORS) { |
| 1160 | pr_err("%s: r/w command failed, status = %#x\n", |
| 1161 | req->rq_disk->disk_name, brq->cmd.resp[0]); |
| 1162 | return MMC_BLK_ABORT; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * Everything else is either success, or a data error of some |
| 1167 | * kind. If it was a write, we may have transitioned to |
| 1168 | * program mode, which we have to wait for it to complete. |
| 1169 | */ |
| 1170 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
| 1171 | /* u32 status; */ |
| 1172 | int err; |
| 1173 | |
| 1174 | /* Check stop command response */ |
| 1175 | if (brq->stop.resp[0] & R1_ERROR) { |
| 1176 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", |
| 1177 | req->rq_disk->disk_name, __func__, |
| 1178 | brq->stop.resp[0]); |
| 1179 | gen_err = 1; |
| 1180 | } |
| 1181 | |
| 1182 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
| 1183 | &gen_err); |
| 1184 | if (err) |
| 1185 | return MMC_BLK_CMD_ERR; |
| 1186 | } |
| 1187 | |
| 1188 | /* if general error occurs, retry the write operation. */ |
| 1189 | if (gen_err) { |
| 1190 | pr_warning("%s: retrying write for general error\n", |
| 1191 | req->rq_disk->disk_name); |
| 1192 | return MMC_BLK_RETRY; |
| 1193 | } |
| 1194 | |
| 1195 | if (brq->data.error) { |
| 1196 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
| 1197 | req->rq_disk->disk_name, brq->data.error, |
| 1198 | (unsigned)blk_rq_pos(req), |
| 1199 | (unsigned)blk_rq_sectors(req), |
| 1200 | brq->cmd.resp[0], brq->stop.resp[0]); |
| 1201 | |
| 1202 | if (rq_data_dir(req) == READ) { |
| 1203 | if (ecc_err) |
| 1204 | return MMC_BLK_ECC_ERR; |
| 1205 | return MMC_BLK_DATA_ERR; |
| 1206 | } else { |
| 1207 | return MMC_BLK_CMD_ERR; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (!brq->data.bytes_xfered) |
| 1212 | return MMC_BLK_RETRY; |
| 1213 | |
| 1214 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
| 1215 | return MMC_BLK_PARTIAL; |
| 1216 | |
| 1217 | return MMC_BLK_SUCCESS; |
| 1218 | } |
| 1219 | |
| 1220 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
| 1221 | struct mmc_card *card, |
| 1222 | int disable_multi, |
| 1223 | struct mmc_queue *mq) |
| 1224 | { |
| 1225 | u32 readcmd, writecmd; |
| 1226 | struct mmc_blk_request *brq = &mqrq->brq; |
| 1227 | struct request *req = mqrq->req; |
| 1228 | struct mmc_blk_data *md = mq->data; |
| 1229 | bool do_data_tag; |
| 1230 | |
| 1231 | /* |
| 1232 | * Reliable writes are used to implement Forced Unit Access and |
| 1233 | * REQ_META accesses, and are supported only on MMCs. |
| 1234 | * |
| 1235 | * XXX: this really needs a good explanation of why REQ_META |
| 1236 | * is treated special. |
| 1237 | */ |
| 1238 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 1239 | (req->cmd_flags & REQ_META)) && |
| 1240 | (rq_data_dir(req) == WRITE) && |
| 1241 | (md->flags & MMC_BLK_REL_WR); |
| 1242 | |
| 1243 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 1244 | brq->mrq.cmd = &brq->cmd; |
| 1245 | brq->mrq.data = &brq->data; |
| 1246 | |
| 1247 | brq->cmd.arg = blk_rq_pos(req); |
| 1248 | if (!mmc_card_blockaddr(card)) |
| 1249 | brq->cmd.arg <<= 9; |
| 1250 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1251 | brq->data.blksz = 512; |
| 1252 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 1253 | brq->stop.arg = 0; |
| 1254 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1255 | brq->data.blocks = blk_rq_sectors(req); |
| 1256 | |
| 1257 | /* |
| 1258 | * The block layer doesn't support all sector count |
| 1259 | * restrictions, so we need to be prepared for too big |
| 1260 | * requests. |
| 1261 | */ |
| 1262 | if (brq->data.blocks > card->host->max_blk_count) |
| 1263 | brq->data.blocks = card->host->max_blk_count; |
| 1264 | |
| 1265 | if (brq->data.blocks > 1) { |
| 1266 | /* |
| 1267 | * After a read error, we redo the request one sector |
| 1268 | * at a time in order to accurately determine which |
| 1269 | * sectors can be read successfully. |
| 1270 | */ |
| 1271 | if (disable_multi) |
| 1272 | brq->data.blocks = 1; |
| 1273 | |
| 1274 | /* Some controllers can't do multiblock reads due to hw bugs */ |
| 1275 | if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ && |
| 1276 | rq_data_dir(req) == READ) |
| 1277 | brq->data.blocks = 1; |
| 1278 | } |
| 1279 | |
| 1280 | if (brq->data.blocks > 1 || do_rel_wr) { |
| 1281 | /* SPI multiblock writes terminate using a special |
| 1282 | * token, not a STOP_TRANSMISSION request. |
| 1283 | */ |
| 1284 | if (!mmc_host_is_spi(card->host) || |
| 1285 | rq_data_dir(req) == READ) |
| 1286 | brq->mrq.stop = &brq->stop; |
| 1287 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 1288 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
| 1289 | } else { |
| 1290 | brq->mrq.stop = NULL; |
| 1291 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 1292 | writecmd = MMC_WRITE_BLOCK; |
| 1293 | } |
| 1294 | if (rq_data_dir(req) == READ) { |
| 1295 | brq->cmd.opcode = readcmd; |
| 1296 | brq->data.flags |= MMC_DATA_READ; |
| 1297 | } else { |
| 1298 | brq->cmd.opcode = writecmd; |
| 1299 | brq->data.flags |= MMC_DATA_WRITE; |
| 1300 | } |
| 1301 | |
| 1302 | if (do_rel_wr) |
| 1303 | mmc_apply_rel_rw(brq, card, req); |
| 1304 | |
| 1305 | /* |
| 1306 | * Data tag is used only during writing meta data to speed |
| 1307 | * up write and any subsequent read of this meta data |
| 1308 | */ |
| 1309 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 1310 | (req->cmd_flags & REQ_META) && |
| 1311 | (rq_data_dir(req) == WRITE) && |
| 1312 | ((brq->data.blocks * brq->data.blksz) >= |
| 1313 | card->ext_csd.data_tag_unit_size); |
| 1314 | |
| 1315 | /* |
| 1316 | * Pre-defined multi-block transfers are preferable to |
| 1317 | * open ended-ones (and necessary for reliable writes). |
| 1318 | * However, it is not sufficient to just send CMD23, |
| 1319 | * and avoid the final CMD12, as on an error condition |
| 1320 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 1321 | * with Auto-CMD23 enhancements provided by some |
| 1322 | * hosts, means that the complexity of dealing |
| 1323 | * with this is best left to the host. If CMD23 is |
| 1324 | * supported by card and host, we'll fill sbc in and let |
| 1325 | * the host deal with handling it correctly. This means |
| 1326 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 1327 | * change of behavior will be observed. |
| 1328 | * |
| 1329 | * N.B: Some MMC cards experience perf degradation. |
| 1330 | * We'll avoid using CMD23-bounded multiblock writes for |
| 1331 | * these, while retaining features like reliable writes. |
| 1332 | */ |
| 1333 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
| 1334 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || |
| 1335 | do_data_tag)) { |
| 1336 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 1337 | brq->sbc.arg = brq->data.blocks | |
| 1338 | (do_rel_wr ? (1 << 31) : 0) | |
| 1339 | (do_data_tag ? (1 << 29) : 0); |
| 1340 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1341 | brq->mrq.sbc = &brq->sbc; |
| 1342 | } |
| 1343 | |
| 1344 | mmc_set_data_timeout(&brq->data, card); |
| 1345 | |
| 1346 | brq->data.sg = mqrq->sg; |
| 1347 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 1348 | |
| 1349 | /* |
| 1350 | * Adjust the sg list so it is the same size as the |
| 1351 | * request. |
| 1352 | */ |
| 1353 | if (brq->data.blocks != blk_rq_sectors(req)) { |
| 1354 | int i, data_size = brq->data.blocks << 9; |
| 1355 | struct scatterlist *sg; |
| 1356 | |
| 1357 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { |
| 1358 | data_size -= sg->length; |
| 1359 | if (data_size <= 0) { |
| 1360 | sg->length += data_size; |
| 1361 | i++; |
| 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | brq->data.sg_len = i; |
| 1366 | } |
| 1367 | |
| 1368 | mqrq->mmc_active.mrq = &brq->mrq; |
| 1369 | mqrq->mmc_active.err_check = mmc_blk_err_check; |
| 1370 | |
| 1371 | mmc_queue_bounce_pre(mqrq); |
| 1372 | } |
| 1373 | |
| 1374 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 1375 | struct mmc_blk_request *brq, struct request *req, |
| 1376 | int ret) |
| 1377 | { |
| 1378 | /* |
| 1379 | * If this is an SD card and we're writing, we can first |
| 1380 | * mark the known good sectors as ok. |
| 1381 | * |
| 1382 | * If the card is not SD, we can still ok written sectors |
| 1383 | * as reported by the controller (which might be less than |
| 1384 | * the real number of written sectors, but never more). |
| 1385 | */ |
| 1386 | if (mmc_card_sd(card)) { |
| 1387 | u32 blocks; |
| 1388 | |
| 1389 | blocks = mmc_sd_num_wr_blocks(card); |
| 1390 | if (blocks != (u32)-1) { |
| 1391 | spin_lock_irq(&md->lock); |
| 1392 | ret = __blk_end_request(req, 0, blocks << 9); |
| 1393 | spin_unlock_irq(&md->lock); |
| 1394 | } |
| 1395 | } else { |
| 1396 | spin_lock_irq(&md->lock); |
| 1397 | ret = __blk_end_request(req, 0, brq->data.bytes_xfered); |
| 1398 | spin_unlock_irq(&md->lock); |
| 1399 | } |
| 1400 | return ret; |
| 1401 | } |
| 1402 | |
| 1403 | static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc) |
| 1404 | { |
| 1405 | struct mmc_blk_data *md = mq->data; |
| 1406 | struct mmc_card *card = md->queue.card; |
| 1407 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
| 1408 | int ret = 1, disable_multi = 0, retry = 0, type; |
| 1409 | enum mmc_blk_status status; |
| 1410 | struct mmc_queue_req *mq_rq; |
| 1411 | struct request *req; |
| 1412 | struct mmc_async_req *areq; |
| 1413 | |
| 1414 | if (!rqc && !mq->mqrq_prev->req) |
| 1415 | return 0; |
| 1416 | |
| 1417 | do { |
| 1418 | if (rqc) { |
| 1419 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 1420 | areq = &mq->mqrq_cur->mmc_active; |
| 1421 | } else |
| 1422 | areq = NULL; |
| 1423 | areq = mmc_start_req(card->host, areq, (int *) &status); |
| 1424 | if (!areq) |
| 1425 | return 0; |
| 1426 | |
| 1427 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); |
| 1428 | brq = &mq_rq->brq; |
| 1429 | req = mq_rq->req; |
| 1430 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
| 1431 | mmc_queue_bounce_post(mq_rq); |
| 1432 | |
| 1433 | switch (status) { |
| 1434 | case MMC_BLK_SUCCESS: |
| 1435 | case MMC_BLK_PARTIAL: |
| 1436 | /* |
| 1437 | * A block was successfully transferred. |
| 1438 | */ |
| 1439 | mmc_blk_reset_success(md, type); |
| 1440 | spin_lock_irq(&md->lock); |
| 1441 | ret = __blk_end_request(req, 0, |
| 1442 | brq->data.bytes_xfered); |
| 1443 | spin_unlock_irq(&md->lock); |
| 1444 | /* |
| 1445 | * If the blk_end_request function returns non-zero even |
| 1446 | * though all data has been transferred and no errors |
| 1447 | * were returned by the host controller, it's a bug. |
| 1448 | */ |
| 1449 | if (status == MMC_BLK_SUCCESS && ret) { |
| 1450 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
| 1451 | __func__, blk_rq_bytes(req), |
| 1452 | brq->data.bytes_xfered); |
| 1453 | rqc = NULL; |
| 1454 | goto cmd_abort; |
| 1455 | } |
| 1456 | break; |
| 1457 | case MMC_BLK_CMD_ERR: |
| 1458 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
| 1459 | if (mmc_blk_reset(md, card->host, type)) |
| 1460 | goto cmd_abort; |
| 1461 | if (!ret) |
| 1462 | goto start_new_req; |
| 1463 | break; |
| 1464 | case MMC_BLK_RETRY: |
| 1465 | if (retry++ < 5) |
| 1466 | break; |
| 1467 | /* Fall through */ |
| 1468 | case MMC_BLK_ABORT: |
| 1469 | if (!mmc_blk_reset(md, card->host, type)) |
| 1470 | break; |
| 1471 | goto cmd_abort; |
| 1472 | case MMC_BLK_DATA_ERR: { |
| 1473 | int err; |
| 1474 | |
| 1475 | err = mmc_blk_reset(md, card->host, type); |
| 1476 | if (!err) |
| 1477 | break; |
| 1478 | if (err == -ENODEV) |
| 1479 | goto cmd_abort; |
| 1480 | /* Fall through */ |
| 1481 | } |
| 1482 | case MMC_BLK_ECC_ERR: |
| 1483 | if (brq->data.blocks > 1) { |
| 1484 | /* Redo read one sector at a time */ |
| 1485 | pr_warning("%s: retrying using single block read\n", |
| 1486 | req->rq_disk->disk_name); |
| 1487 | disable_multi = 1; |
| 1488 | break; |
| 1489 | } |
| 1490 | /* |
| 1491 | * After an error, we redo I/O one sector at a |
| 1492 | * time, so we only reach here after trying to |
| 1493 | * read a single sector. |
| 1494 | */ |
| 1495 | spin_lock_irq(&md->lock); |
| 1496 | ret = __blk_end_request(req, -EIO, |
| 1497 | brq->data.blksz); |
| 1498 | spin_unlock_irq(&md->lock); |
| 1499 | if (!ret) |
| 1500 | goto start_new_req; |
| 1501 | break; |
| 1502 | case MMC_BLK_NOMEDIUM: |
| 1503 | goto cmd_abort; |
| 1504 | } |
| 1505 | |
| 1506 | if (ret) { |
| 1507 | /* |
| 1508 | * In case of a incomplete request |
| 1509 | * prepare it again and resend. |
| 1510 | */ |
| 1511 | mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq); |
| 1512 | mmc_start_req(card->host, &mq_rq->mmc_active, NULL); |
| 1513 | } |
| 1514 | } while (ret); |
| 1515 | |
| 1516 | return 1; |
| 1517 | |
| 1518 | cmd_abort: |
| 1519 | spin_lock_irq(&md->lock); |
| 1520 | if (mmc_card_removed(card)) |
| 1521 | req->cmd_flags |= REQ_QUIET; |
| 1522 | while (ret) |
| 1523 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
| 1524 | spin_unlock_irq(&md->lock); |
| 1525 | |
| 1526 | start_new_req: |
| 1527 | if (rqc) { |
| 1528 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 1529 | mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL); |
| 1530 | } |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1536 | static int mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 1537 | { |
| 1538 | int err; |
| 1539 | |
| 1540 | mmc_claim_host(card->host); |
| 1541 | err = mmc_set_blocklen(card, 512); |
| 1542 | mmc_release_host(card->host); |
| 1543 | |
| 1544 | if (err) { |
| 1545 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 1546 | md->disk->disk_name, err); |
| 1547 | return -EINVAL; |
| 1548 | } |
| 1549 | |
| 1550 | return 0; |
| 1551 | } |
| 1552 | |
| 1553 | #endif |
| 1554 | |
| 1555 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 1556 | { |
| 1557 | int ret; |
| 1558 | struct mmc_blk_data *md = mq->data; |
| 1559 | struct mmc_card *card = md->queue.card; |
| 1560 | |
| 1561 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1562 | if (mmc_bus_needs_resume(card->host)) { |
| 1563 | mmc_resume_bus(card->host); |
| 1564 | mmc_blk_set_blksize(md, card); |
| 1565 | } |
| 1566 | #endif |
| 1567 | |
| 1568 | if (req && !mq->mqrq_prev->req) |
| 1569 | /* claim host only for the first request */ |
| 1570 | mmc_claim_host(card->host); |
| 1571 | |
| 1572 | ret = mmc_blk_part_switch(card, md); |
| 1573 | if (ret) { |
| 1574 | if (req) { |
| 1575 | spin_lock_irq(&md->lock); |
| 1576 | __blk_end_request_all(req, -EIO); |
| 1577 | spin_unlock_irq(&md->lock); |
| 1578 | } |
| 1579 | ret = 0; |
| 1580 | goto out; |
| 1581 | } |
| 1582 | |
| 1583 | if (req && req->cmd_flags & REQ_DISCARD) { |
| 1584 | /* complete ongoing async transfer before issuing discard */ |
| 1585 | if (card->host->areq) |
| 1586 | mmc_blk_issue_rw_rq(mq, NULL); |
| 1587 | if (req->cmd_flags & REQ_SECURE && |
| 1588 | !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN)) |
| 1589 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
| 1590 | else |
| 1591 | ret = mmc_blk_issue_discard_rq(mq, req); |
| 1592 | } else if (req && req->cmd_flags & REQ_FLUSH) { |
| 1593 | /* complete ongoing async transfer before issuing flush */ |
| 1594 | if (card->host->areq) |
| 1595 | mmc_blk_issue_rw_rq(mq, NULL); |
| 1596 | ret = mmc_blk_issue_flush(mq, req); |
| 1597 | } else { |
| 1598 | ret = mmc_blk_issue_rw_rq(mq, req); |
| 1599 | } |
| 1600 | |
| 1601 | out: |
| 1602 | if (!req) |
| 1603 | /* release host only when there are no more requests */ |
| 1604 | mmc_release_host(card->host); |
| 1605 | return ret; |
| 1606 | } |
| 1607 | |
| 1608 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 1609 | { |
| 1610 | return mmc_card_readonly(card) || |
| 1611 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 1612 | } |
| 1613 | |
| 1614 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 1615 | struct device *parent, |
| 1616 | sector_t size, |
| 1617 | bool default_ro, |
| 1618 | const char *subname, |
| 1619 | int area_type) |
| 1620 | { |
| 1621 | struct mmc_blk_data *md; |
| 1622 | int devidx, ret; |
| 1623 | |
| 1624 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 1625 | if (devidx >= max_devices) |
| 1626 | return ERR_PTR(-ENOSPC); |
| 1627 | __set_bit(devidx, dev_use); |
| 1628 | |
| 1629 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
| 1630 | if (!md) { |
| 1631 | ret = -ENOMEM; |
| 1632 | goto out; |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * !subname implies we are creating main mmc_blk_data that will be |
| 1637 | * associated with mmc_card with mmc_set_drvdata. Due to device |
| 1638 | * partitions, devidx will not coincide with a per-physical card |
| 1639 | * index anymore so we keep track of a name index. |
| 1640 | */ |
| 1641 | if (!subname) { |
| 1642 | md->name_idx = find_first_zero_bit(name_use, max_devices); |
| 1643 | __set_bit(md->name_idx, name_use); |
| 1644 | } else |
| 1645 | md->name_idx = ((struct mmc_blk_data *) |
| 1646 | dev_to_disk(parent)->private_data)->name_idx; |
| 1647 | |
| 1648 | md->area_type = area_type; |
| 1649 | |
| 1650 | /* |
| 1651 | * Set the read-only status based on the supported commands |
| 1652 | * and the write protect switch. |
| 1653 | */ |
| 1654 | md->read_only = mmc_blk_readonly(card); |
| 1655 | |
| 1656 | md->disk = alloc_disk(perdev_minors); |
| 1657 | if (md->disk == NULL) { |
| 1658 | ret = -ENOMEM; |
| 1659 | goto err_kfree; |
| 1660 | } |
| 1661 | |
| 1662 | spin_lock_init(&md->lock); |
| 1663 | INIT_LIST_HEAD(&md->part); |
| 1664 | md->usage = 1; |
| 1665 | |
| 1666 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
| 1667 | if (ret) |
| 1668 | goto err_putdisk; |
| 1669 | |
| 1670 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 1671 | md->queue.data = md; |
| 1672 | |
| 1673 | md->disk->major = MMC_BLOCK_MAJOR; |
| 1674 | md->disk->first_minor = devidx * perdev_minors; |
| 1675 | md->disk->fops = &mmc_bdops; |
| 1676 | md->disk->private_data = md; |
| 1677 | md->disk->queue = md->queue.queue; |
| 1678 | md->disk->driverfs_dev = parent; |
| 1679 | set_disk_ro(md->disk, md->read_only || default_ro); |
| 1680 | md->disk->flags = GENHD_FL_EXT_DEVT; |
| 1681 | |
| 1682 | /* |
| 1683 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 1684 | * |
| 1685 | * - be set for removable media with permanent block devices |
| 1686 | * - be unset for removable block devices with permanent media |
| 1687 | * |
| 1688 | * Since MMC block devices clearly fall under the second |
| 1689 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 1690 | * should use the block device creation/destruction hotplug |
| 1691 | * messages to tell when the card is present. |
| 1692 | */ |
| 1693 | |
| 1694 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 1695 | "mmcblk%d%s", md->name_idx, subname ? subname : ""); |
| 1696 | |
| 1697 | blk_queue_logical_block_size(md->queue.queue, 512); |
| 1698 | set_capacity(md->disk, size); |
| 1699 | |
| 1700 | if (mmc_host_cmd23(card->host)) { |
| 1701 | if (mmc_card_mmc(card) || |
| 1702 | (mmc_card_sd(card) && |
| 1703 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 1704 | md->flags |= MMC_BLK_CMD23; |
| 1705 | } |
| 1706 | |
| 1707 | if (mmc_card_mmc(card) && |
| 1708 | md->flags & MMC_BLK_CMD23 && |
| 1709 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 1710 | card->ext_csd.rel_sectors)) { |
| 1711 | md->flags |= MMC_BLK_REL_WR; |
| 1712 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
| 1713 | } |
| 1714 | |
| 1715 | return md; |
| 1716 | |
| 1717 | err_putdisk: |
| 1718 | put_disk(md->disk); |
| 1719 | err_kfree: |
| 1720 | kfree(md); |
| 1721 | out: |
| 1722 | return ERR_PTR(ret); |
| 1723 | } |
| 1724 | |
| 1725 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 1726 | { |
| 1727 | sector_t size; |
| 1728 | struct mmc_blk_data *md; |
| 1729 | |
| 1730 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 1731 | /* |
| 1732 | * The EXT_CSD sector count is in number or 512 byte |
| 1733 | * sectors. |
| 1734 | */ |
| 1735 | size = card->ext_csd.sectors; |
| 1736 | } else { |
| 1737 | /* |
| 1738 | * The CSD capacity field is in units of read_blkbits. |
| 1739 | * set_capacity takes units of 512 bytes. |
| 1740 | */ |
| 1741 | size = (sector_t)card->csd.capacity << (card->csd.read_blkbits - 9); |
| 1742 | } |
| 1743 | |
| 1744 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
| 1745 | MMC_BLK_DATA_AREA_MAIN); |
| 1746 | return md; |
| 1747 | } |
| 1748 | |
| 1749 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 1750 | struct mmc_blk_data *md, |
| 1751 | unsigned int part_type, |
| 1752 | sector_t size, |
| 1753 | bool default_ro, |
| 1754 | const char *subname, |
| 1755 | int area_type) |
| 1756 | { |
| 1757 | char cap_str[10]; |
| 1758 | struct mmc_blk_data *part_md; |
| 1759 | |
| 1760 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, |
| 1761 | subname, area_type); |
| 1762 | if (IS_ERR(part_md)) |
| 1763 | return PTR_ERR(part_md); |
| 1764 | part_md->part_type = part_type; |
| 1765 | list_add(&part_md->part, &md->part); |
| 1766 | |
| 1767 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 1768 | cap_str, sizeof(cap_str)); |
| 1769 | pr_info("%s: %s %s partition %u %s\n", |
| 1770 | part_md->disk->disk_name, mmc_card_id(card), |
| 1771 | mmc_card_name(card), part_md->part_type, cap_str); |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | /* MMC Physical partitions consist of two boot partitions and |
| 1776 | * up to four general purpose partitions. |
| 1777 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 1778 | * to provide access to the partition. |
| 1779 | */ |
| 1780 | |
| 1781 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 1782 | { |
| 1783 | int idx, ret = 0; |
| 1784 | |
| 1785 | if (!mmc_card_mmc(card)) |
| 1786 | return 0; |
| 1787 | |
| 1788 | for (idx = 0; idx < card->nr_parts; idx++) { |
| 1789 | if (card->part[idx].size) { |
| 1790 | ret = mmc_blk_alloc_part(card, md, |
| 1791 | card->part[idx].part_cfg, |
| 1792 | card->part[idx].size >> 9, |
| 1793 | card->part[idx].force_ro, |
| 1794 | card->part[idx].name, |
| 1795 | card->part[idx].area_type); |
| 1796 | if (ret) |
| 1797 | return ret; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | return ret; |
| 1802 | } |
| 1803 | |
| 1804 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 1805 | { |
| 1806 | struct mmc_card *card; |
| 1807 | |
| 1808 | if (md) { |
| 1809 | card = md->queue.card; |
| 1810 | if (md->disk->flags & GENHD_FL_UP) { |
| 1811 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 1812 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 1813 | card->ext_csd.boot_ro_lockable) |
| 1814 | device_remove_file(disk_to_dev(md->disk), |
| 1815 | &md->power_ro_lock); |
| 1816 | |
| 1817 | /* Stop new requests from getting into the queue */ |
| 1818 | del_gendisk(md->disk); |
| 1819 | } |
| 1820 | |
| 1821 | /* Then flush out any already in there */ |
| 1822 | mmc_cleanup_queue(&md->queue); |
| 1823 | mmc_blk_put(md); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 1828 | struct mmc_blk_data *md) |
| 1829 | { |
| 1830 | struct list_head *pos, *q; |
| 1831 | struct mmc_blk_data *part_md; |
| 1832 | |
| 1833 | __clear_bit(md->name_idx, name_use); |
| 1834 | list_for_each_safe(pos, q, &md->part) { |
| 1835 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 1836 | list_del(pos); |
| 1837 | mmc_blk_remove_req(part_md); |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 1842 | { |
| 1843 | int ret; |
| 1844 | struct mmc_card *card = md->queue.card; |
| 1845 | |
| 1846 | add_disk(md->disk); |
| 1847 | md->force_ro.show = force_ro_show; |
| 1848 | md->force_ro.store = force_ro_store; |
| 1849 | sysfs_attr_init(&md->force_ro.attr); |
| 1850 | md->force_ro.attr.name = "force_ro"; |
| 1851 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 1852 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 1853 | if (ret) |
| 1854 | goto force_ro_fail; |
| 1855 | |
| 1856 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 1857 | card->ext_csd.boot_ro_lockable) { |
| 1858 | umode_t mode; |
| 1859 | |
| 1860 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 1861 | mode = S_IRUGO; |
| 1862 | else |
| 1863 | mode = S_IRUGO | S_IWUSR; |
| 1864 | |
| 1865 | md->power_ro_lock.show = power_ro_lock_show; |
| 1866 | md->power_ro_lock.store = power_ro_lock_store; |
| 1867 | sysfs_attr_init(&md->power_ro_lock.attr); |
| 1868 | md->power_ro_lock.attr.mode = mode; |
| 1869 | md->power_ro_lock.attr.name = |
| 1870 | "ro_lock_until_next_power_on"; |
| 1871 | ret = device_create_file(disk_to_dev(md->disk), |
| 1872 | &md->power_ro_lock); |
| 1873 | if (ret) |
| 1874 | goto power_ro_lock_fail; |
| 1875 | } |
| 1876 | return ret; |
| 1877 | |
| 1878 | power_ro_lock_fail: |
| 1879 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 1880 | force_ro_fail: |
| 1881 | del_gendisk(md->disk); |
| 1882 | |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
| 1886 | #define CID_MANFID_SANDISK 0x2 |
| 1887 | #define CID_MANFID_TOSHIBA 0x11 |
| 1888 | #define CID_MANFID_MICRON 0x13 |
| 1889 | #define CID_MANFID_SAMSUNG 0x15 |
| 1890 | |
| 1891 | static const struct mmc_fixup blk_fixups[] = |
| 1892 | { |
| 1893 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 1894 | MMC_QUIRK_INAND_CMD38), |
| 1895 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 1896 | MMC_QUIRK_INAND_CMD38), |
| 1897 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 1898 | MMC_QUIRK_INAND_CMD38), |
| 1899 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 1900 | MMC_QUIRK_INAND_CMD38), |
| 1901 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 1902 | MMC_QUIRK_INAND_CMD38), |
| 1903 | |
| 1904 | /* |
| 1905 | * Some MMC cards experience performance degradation with CMD23 |
| 1906 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 1907 | * black list what's bad... |
| 1908 | * - Certain Toshiba cards. |
| 1909 | * |
| 1910 | * N.B. This doesn't affect SD cards. |
| 1911 | */ |
| 1912 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 1913 | MMC_QUIRK_BLK_NO_CMD23), |
| 1914 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 1915 | MMC_QUIRK_BLK_NO_CMD23), |
| 1916 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 1917 | MMC_QUIRK_BLK_NO_CMD23), |
| 1918 | |
| 1919 | /* |
| 1920 | * Some Micron MMC cards needs longer data read timeout than |
| 1921 | * indicated in CSD. |
| 1922 | */ |
| 1923 | MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc, |
| 1924 | MMC_QUIRK_LONG_READ_TIME), |
| 1925 | |
| 1926 | /* |
| 1927 | * On these Samsung MoviNAND parts, performing secure erase or |
| 1928 | * secure trim can result in unrecoverable corruption due to a |
| 1929 | * firmware bug. |
| 1930 | */ |
| 1931 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1932 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1933 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1934 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1935 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1936 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1937 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1938 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1939 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1940 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1941 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1942 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1943 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1944 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1945 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 1946 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 1947 | |
| 1948 | END_FIXUP |
| 1949 | }; |
| 1950 | |
| 1951 | static int mmc_blk_probe(struct mmc_card *card) |
| 1952 | { |
| 1953 | struct mmc_blk_data *md, *part_md; |
| 1954 | char cap_str[10]; |
| 1955 | |
| 1956 | /* |
| 1957 | * Check that the card supports the command class(es) we need. |
| 1958 | */ |
| 1959 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
| 1960 | return -ENODEV; |
| 1961 | |
| 1962 | md = mmc_blk_alloc(card); |
| 1963 | if (IS_ERR(md)) |
| 1964 | return PTR_ERR(md); |
| 1965 | |
| 1966 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
| 1967 | cap_str, sizeof(cap_str)); |
| 1968 | pr_info("%s: %s %s %s %s\n", |
| 1969 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
| 1970 | cap_str, md->read_only ? "(ro)" : ""); |
| 1971 | |
| 1972 | if (mmc_blk_alloc_parts(card, md)) |
| 1973 | goto out; |
| 1974 | |
| 1975 | mmc_set_drvdata(card, md); |
| 1976 | mmc_fixup_device(card, blk_fixups); |
| 1977 | |
| 1978 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1979 | mmc_set_bus_resume_policy(card->host, 1); |
| 1980 | #endif |
| 1981 | if (mmc_add_disk(md)) |
| 1982 | goto out; |
| 1983 | |
| 1984 | list_for_each_entry(part_md, &md->part, part) { |
| 1985 | if (mmc_add_disk(part_md)) |
| 1986 | goto out; |
| 1987 | } |
| 1988 | return 0; |
| 1989 | |
| 1990 | out: |
| 1991 | mmc_blk_remove_parts(card, md); |
| 1992 | mmc_blk_remove_req(md); |
| 1993 | return 0; |
| 1994 | } |
| 1995 | |
| 1996 | static void mmc_blk_remove(struct mmc_card *card) |
| 1997 | { |
| 1998 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1999 | |
| 2000 | if(!md) |
| 2001 | return; |
| 2002 | |
| 2003 | mmc_blk_remove_parts(card, md); |
| 2004 | mmc_claim_host(card->host); |
| 2005 | mmc_blk_part_switch(card, md); |
| 2006 | mmc_release_host(card->host); |
| 2007 | mmc_blk_remove_req(md); |
| 2008 | mmc_set_drvdata(card, NULL); |
| 2009 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 2010 | mmc_set_bus_resume_policy(card->host, 0); |
| 2011 | #endif |
| 2012 | } |
| 2013 | |
| 2014 | #ifdef CONFIG_PM |
| 2015 | static int mmc_blk_suspend(struct mmc_card *card) |
| 2016 | { |
| 2017 | struct mmc_blk_data *part_md; |
| 2018 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 2019 | |
| 2020 | if (md) { |
| 2021 | mmc_queue_suspend(&md->queue); |
| 2022 | list_for_each_entry(part_md, &md->part, part) { |
| 2023 | mmc_queue_suspend(&part_md->queue); |
| 2024 | } |
| 2025 | } |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
| 2029 | static int mmc_blk_resume(struct mmc_card *card) |
| 2030 | { |
| 2031 | struct mmc_blk_data *part_md; |
| 2032 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 2033 | |
| 2034 | if (md) { |
| 2035 | /* |
| 2036 | * Resume involves the card going into idle state, |
| 2037 | * so current partition is always the main one. |
| 2038 | */ |
| 2039 | md->part_curr = md->part_type; |
| 2040 | mmc_queue_resume(&md->queue); |
| 2041 | list_for_each_entry(part_md, &md->part, part) { |
| 2042 | mmc_queue_resume(&part_md->queue); |
| 2043 | } |
| 2044 | } |
| 2045 | return 0; |
| 2046 | } |
| 2047 | #else |
| 2048 | #define mmc_blk_suspend NULL |
| 2049 | #define mmc_blk_resume NULL |
| 2050 | #endif |
| 2051 | |
| 2052 | static struct mmc_driver mmc_driver = { |
| 2053 | .drv = { |
| 2054 | .name = "mmcblk", |
| 2055 | }, |
| 2056 | .probe = mmc_blk_probe, |
| 2057 | .remove = mmc_blk_remove, |
| 2058 | .suspend = mmc_blk_suspend, |
| 2059 | .resume = mmc_blk_resume, |
| 2060 | }; |
| 2061 | |
| 2062 | static int __init mmc_blk_init(void) |
| 2063 | { |
| 2064 | int res; |
| 2065 | |
| 2066 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 2067 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 2068 | |
| 2069 | max_devices = 256 / perdev_minors; |
| 2070 | |
| 2071 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 2072 | if (res) |
| 2073 | goto out; |
| 2074 | |
| 2075 | res = mmc_register_driver(&mmc_driver); |
| 2076 | if (res) |
| 2077 | goto out2; |
| 2078 | |
| 2079 | return 0; |
| 2080 | out2: |
| 2081 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 2082 | out: |
| 2083 | return res; |
| 2084 | } |
| 2085 | |
| 2086 | static void __exit mmc_blk_exit(void) |
| 2087 | { |
| 2088 | mmc_unregister_driver(&mmc_driver); |
| 2089 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 2090 | } |
| 2091 | |
| 2092 | module_init(mmc_blk_init); |
| 2093 | module_exit(mmc_blk_exit); |
| 2094 | |
| 2095 | MODULE_LICENSE("GPL"); |
| 2096 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 2097 | |