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