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/cdev.h>
32 #include <linux/mutex.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35 #include <linux/delay.h>
36 #include <linux/capability.h>
37 #include <linux/compat.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/idr.h>
40 #include <linux/debugfs.h>
41
42 #include <linux/mmc/ioctl.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/mmc.h>
46 #include <linux/mmc/sd.h>
47
48 #include <linux/uaccess.h>
49
50 #include <trace/hooks/mmc_core.h>
51
52 #include "queue.h"
53 #include "block.h"
54 #include "core.h"
55 #include "card.h"
56 #include "crypto.h"
57 #include "host.h"
58 #include "bus.h"
59 #include "mmc_ops.h"
60 #include "quirks.h"
61 #include "sd_ops.h"
62
63 MODULE_ALIAS("mmc:block");
64 #ifdef MODULE_PARAM_PREFIX
65 #undef MODULE_PARAM_PREFIX
66 #endif
67 #define MODULE_PARAM_PREFIX "mmcblk."
68
69 /*
70 * Set a 10 second timeout for polling write request busy state. Note, mmc core
71 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
72 * second software timer to timeout the whole request, so 10 seconds should be
73 * ample.
74 */
75 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
76 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
77 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
78
79 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
80 (rq_data_dir(req) == WRITE))
81 static DEFINE_MUTEX(block_mutex);
82
83 /*
84 * The defaults come from config options but can be overriden by module
85 * or bootarg options.
86 */
87 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
88
89 /*
90 * We've only got one major, so number of mmcblk devices is
91 * limited to (1 << 20) / number of minors per device. It is also
92 * limited by the MAX_DEVICES below.
93 */
94 static int max_devices;
95
96 #define MAX_DEVICES 256
97
98 static DEFINE_IDA(mmc_blk_ida);
99 static DEFINE_IDA(mmc_rpmb_ida);
100
101 /*
102 * There is one mmc_blk_data per slot.
103 */
104 struct mmc_blk_data {
105 struct device *parent;
106 struct gendisk *disk;
107 struct mmc_queue queue;
108 struct list_head part;
109 struct list_head rpmbs;
110
111 unsigned int flags;
112 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
113 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
114
115 unsigned int usage;
116 unsigned int read_only;
117 unsigned int part_type;
118 unsigned int reset_done;
119 #define MMC_BLK_READ BIT(0)
120 #define MMC_BLK_WRITE BIT(1)
121 #define MMC_BLK_DISCARD BIT(2)
122 #define MMC_BLK_SECDISCARD BIT(3)
123 #define MMC_BLK_CQE_RECOVERY BIT(4)
124
125 /*
126 * Only set in main mmc_blk_data associated
127 * with mmc_card with dev_set_drvdata, and keeps
128 * track of the current selected device partition.
129 */
130 unsigned int part_curr;
131 struct device_attribute force_ro;
132 struct device_attribute power_ro_lock;
133 int area_type;
134
135 /* debugfs files (only in main mmc_blk_data) */
136 struct dentry *status_dentry;
137 struct dentry *ext_csd_dentry;
138 };
139
140 /* Device type for RPMB character devices */
141 static dev_t mmc_rpmb_devt;
142
143 /* Bus type for RPMB character devices */
144 static struct bus_type mmc_rpmb_bus_type = {
145 .name = "mmc_rpmb",
146 };
147
148 /**
149 * struct mmc_rpmb_data - special RPMB device type for these areas
150 * @dev: the device for the RPMB area
151 * @chrdev: character device for the RPMB area
152 * @id: unique device ID number
153 * @part_index: partition index (0 on first)
154 * @md: parent MMC block device
155 * @node: list item, so we can put this device on a list
156 */
157 struct mmc_rpmb_data {
158 struct device dev;
159 struct cdev chrdev;
160 int id;
161 unsigned int part_index;
162 struct mmc_blk_data *md;
163 struct list_head node;
164 };
165
166 static DEFINE_MUTEX(open_lock);
167
168 module_param(perdev_minors, int, 0444);
169 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
170
171 static inline int mmc_blk_part_switch(struct mmc_card *card,
172 unsigned int part_type);
173 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
174 struct mmc_card *card,
175 int recovery_mode,
176 struct mmc_queue *mq);
177 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
178
mmc_blk_get(struct gendisk * disk)179 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
180 {
181 struct mmc_blk_data *md;
182
183 mutex_lock(&open_lock);
184 md = disk->private_data;
185 if (md && md->usage == 0)
186 md = NULL;
187 if (md)
188 md->usage++;
189 mutex_unlock(&open_lock);
190
191 return md;
192 }
193
mmc_get_devidx(struct gendisk * disk)194 static inline int mmc_get_devidx(struct gendisk *disk)
195 {
196 int devidx = disk->first_minor / perdev_minors;
197 return devidx;
198 }
199
mmc_blk_put(struct mmc_blk_data * md)200 static void mmc_blk_put(struct mmc_blk_data *md)
201 {
202 mutex_lock(&open_lock);
203 md->usage--;
204 if (md->usage == 0) {
205 int devidx = mmc_get_devidx(md->disk);
206 blk_put_queue(md->queue.queue);
207 ida_simple_remove(&mmc_blk_ida, devidx);
208 put_disk(md->disk);
209 kfree(md);
210 }
211 mutex_unlock(&open_lock);
212 }
213
power_ro_lock_show(struct device * dev,struct device_attribute * attr,char * buf)214 static ssize_t power_ro_lock_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
216 {
217 int ret;
218 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
219 struct mmc_card *card = md->queue.card;
220 int locked = 0;
221
222 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
223 locked = 2;
224 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
225 locked = 1;
226
227 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
228
229 mmc_blk_put(md);
230
231 return ret;
232 }
233
power_ro_lock_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)234 static ssize_t power_ro_lock_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
236 {
237 int ret;
238 struct mmc_blk_data *md, *part_md;
239 struct mmc_queue *mq;
240 struct request *req;
241 unsigned long set;
242
243 if (kstrtoul(buf, 0, &set))
244 return -EINVAL;
245
246 if (set != 1)
247 return count;
248
249 md = mmc_blk_get(dev_to_disk(dev));
250 mq = &md->queue;
251
252 /* Dispatch locking to the block layer */
253 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
254 if (IS_ERR(req)) {
255 count = PTR_ERR(req);
256 goto out_put;
257 }
258 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
259 blk_execute_rq(mq->queue, NULL, req, 0);
260 ret = req_to_mmc_queue_req(req)->drv_op_result;
261 blk_put_request(req);
262
263 if (!ret) {
264 pr_info("%s: Locking boot partition ro until next power on\n",
265 md->disk->disk_name);
266 set_disk_ro(md->disk, 1);
267
268 list_for_each_entry(part_md, &md->part, part)
269 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
270 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
271 set_disk_ro(part_md->disk, 1);
272 }
273 }
274 out_put:
275 mmc_blk_put(md);
276 return count;
277 }
278
force_ro_show(struct device * dev,struct device_attribute * attr,char * buf)279 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
280 char *buf)
281 {
282 int ret;
283 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
284
285 ret = snprintf(buf, PAGE_SIZE, "%d\n",
286 get_disk_ro(dev_to_disk(dev)) ^
287 md->read_only);
288 mmc_blk_put(md);
289 return ret;
290 }
291
force_ro_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)292 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
294 {
295 int ret;
296 char *end;
297 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
298 unsigned long set = simple_strtoul(buf, &end, 0);
299 if (end == buf) {
300 ret = -EINVAL;
301 goto out;
302 }
303
304 set_disk_ro(dev_to_disk(dev), set || md->read_only);
305 ret = count;
306 out:
307 mmc_blk_put(md);
308 return ret;
309 }
310
mmc_blk_open(struct block_device * bdev,fmode_t mode)311 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
312 {
313 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
314 int ret = -ENXIO;
315
316 mutex_lock(&block_mutex);
317 if (md) {
318 ret = 0;
319 if ((mode & FMODE_WRITE) && md->read_only) {
320 mmc_blk_put(md);
321 ret = -EROFS;
322 }
323 }
324 mutex_unlock(&block_mutex);
325
326 return ret;
327 }
328
mmc_blk_release(struct gendisk * disk,fmode_t mode)329 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
330 {
331 struct mmc_blk_data *md = disk->private_data;
332
333 mutex_lock(&block_mutex);
334 mmc_blk_put(md);
335 mutex_unlock(&block_mutex);
336 }
337
338 static int
mmc_blk_getgeo(struct block_device * bdev,struct hd_geometry * geo)339 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
340 {
341 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
342 geo->heads = 4;
343 geo->sectors = 16;
344 return 0;
345 }
346
347 struct mmc_blk_ioc_data {
348 struct mmc_ioc_cmd ic;
349 unsigned char *buf;
350 u64 buf_bytes;
351 struct mmc_rpmb_data *rpmb;
352 };
353
mmc_blk_ioctl_copy_from_user(struct mmc_ioc_cmd __user * user)354 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
355 struct mmc_ioc_cmd __user *user)
356 {
357 struct mmc_blk_ioc_data *idata;
358 int err;
359
360 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
361 if (!idata) {
362 err = -ENOMEM;
363 goto out;
364 }
365
366 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
367 err = -EFAULT;
368 goto idata_err;
369 }
370
371 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
372 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
373 err = -EOVERFLOW;
374 goto idata_err;
375 }
376
377 if (!idata->buf_bytes) {
378 idata->buf = NULL;
379 return idata;
380 }
381
382 idata->buf = memdup_user((void __user *)(unsigned long)
383 idata->ic.data_ptr, idata->buf_bytes);
384 if (IS_ERR(idata->buf)) {
385 err = PTR_ERR(idata->buf);
386 goto idata_err;
387 }
388
389 return idata;
390
391 idata_err:
392 kfree(idata);
393 out:
394 return ERR_PTR(err);
395 }
396
mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user * ic_ptr,struct mmc_blk_ioc_data * idata)397 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
398 struct mmc_blk_ioc_data *idata)
399 {
400 struct mmc_ioc_cmd *ic = &idata->ic;
401
402 if (copy_to_user(&(ic_ptr->response), ic->response,
403 sizeof(ic->response)))
404 return -EFAULT;
405
406 if (!idata->ic.write_flag) {
407 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
408 idata->buf, idata->buf_bytes))
409 return -EFAULT;
410 }
411
412 return 0;
413 }
414
card_busy_detect(struct mmc_card * card,unsigned int timeout_ms,u32 * resp_errs)415 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
416 u32 *resp_errs)
417 {
418 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
419 int err = 0;
420 u32 status;
421
422 do {
423 bool done = time_after(jiffies, timeout);
424
425 if (!(card->host->caps2 & MMC_CAP2_NO_SD) && card->host->ops->card_busy) {
426 status = card->host->ops->card_busy(card->host) ?
427 0 : R1_READY_FOR_DATA | R1_STATE_TRAN << 9;
428
429 if (!status)
430 usleep_range(100, 150);
431 } else {
432 err = __mmc_send_status(card, &status, 5);
433 if (err) {
434 dev_err(mmc_dev(card->host),
435 "error %d requesting status\n", err);
436 return err;
437 }
438
439 /* Accumulate any response error bits seen */
440 if (resp_errs)
441 *resp_errs |= status;
442 }
443
444 /*
445 * Timeout if the device never becomes ready for data and never
446 * leaves the program state.
447 */
448 if (done) {
449 dev_err(mmc_dev(card->host),
450 "Card stuck in wrong state! %s status: %#x\n",
451 __func__, status);
452 return -ETIMEDOUT;
453 }
454 } while (!mmc_ready_for_data(status));
455
456 return err;
457 }
458
__mmc_blk_ioctl_cmd(struct mmc_card * card,struct mmc_blk_data * md,struct mmc_blk_ioc_data * idata)459 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
460 struct mmc_blk_ioc_data *idata)
461 {
462 struct mmc_command cmd = {}, sbc = {};
463 struct mmc_data data = {};
464 struct mmc_request mrq = {};
465 struct scatterlist sg;
466 int err;
467 unsigned int target_part;
468
469 if (!card || !md || !idata)
470 return -EINVAL;
471
472 /*
473 * The RPMB accesses comes in from the character device, so we
474 * need to target these explicitly. Else we just target the
475 * partition type for the block device the ioctl() was issued
476 * on.
477 */
478 if (idata->rpmb) {
479 /* Support multiple RPMB partitions */
480 target_part = idata->rpmb->part_index;
481 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
482 } else {
483 target_part = md->part_type;
484 }
485
486 cmd.opcode = idata->ic.opcode;
487 cmd.arg = idata->ic.arg;
488 cmd.flags = idata->ic.flags;
489
490 if (idata->buf_bytes) {
491 data.sg = &sg;
492 data.sg_len = 1;
493 data.blksz = idata->ic.blksz;
494 data.blocks = idata->ic.blocks;
495
496 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
497
498 if (idata->ic.write_flag)
499 data.flags = MMC_DATA_WRITE;
500 else
501 data.flags = MMC_DATA_READ;
502
503 /* data.flags must already be set before doing this. */
504 mmc_set_data_timeout(&data, card);
505
506 /* Allow overriding the timeout_ns for empirical tuning. */
507 if (idata->ic.data_timeout_ns)
508 data.timeout_ns = idata->ic.data_timeout_ns;
509
510 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
511 /*
512 * Pretend this is a data transfer and rely on the
513 * host driver to compute timeout. When all host
514 * drivers support cmd.cmd_timeout for R1B, this
515 * can be changed to:
516 *
517 * mrq.data = NULL;
518 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
519 */
520 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
521 }
522
523 mrq.data = &data;
524 }
525
526 mrq.cmd = &cmd;
527
528 err = mmc_blk_part_switch(card, target_part);
529 if (err)
530 return err;
531
532 if (idata->ic.is_acmd) {
533 err = mmc_app_cmd(card->host, card);
534 if (err)
535 return err;
536 }
537
538 if (idata->rpmb) {
539 sbc.opcode = MMC_SET_BLOCK_COUNT;
540 /*
541 * We don't do any blockcount validation because the max size
542 * may be increased by a future standard. We just copy the
543 * 'Reliable Write' bit here.
544 */
545 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
546 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
547 mrq.sbc = &sbc;
548 }
549
550 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
551 (cmd.opcode == MMC_SWITCH))
552 return mmc_sanitize(card);
553
554 mmc_wait_for_req(card->host, &mrq);
555 memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
556
557 if (cmd.error) {
558 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
559 __func__, cmd.error);
560 return cmd.error;
561 }
562 if (data.error) {
563 dev_err(mmc_dev(card->host), "%s: data error %d\n",
564 __func__, data.error);
565 return data.error;
566 }
567
568 /*
569 * Make sure the cache of the PARTITION_CONFIG register and
570 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
571 * changed it successfully.
572 */
573 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
574 (cmd.opcode == MMC_SWITCH)) {
575 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
576 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
577
578 /*
579 * Update cache so the next mmc_blk_part_switch call operates
580 * on up-to-date data.
581 */
582 card->ext_csd.part_config = value;
583 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
584 }
585
586 /*
587 * Make sure to update CACHE_CTRL in case it was changed. The cache
588 * will get turned back on if the card is re-initialized, e.g.
589 * suspend/resume or hw reset in recovery.
590 */
591 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
592 (cmd.opcode == MMC_SWITCH)) {
593 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
594
595 card->ext_csd.cache_ctrl = value;
596 }
597
598 /*
599 * According to the SD specs, some commands require a delay after
600 * issuing the command.
601 */
602 if (idata->ic.postsleep_min_us)
603 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
604
605 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
606 /*
607 * Ensure RPMB/R1B command has completed by polling CMD13
608 * "Send Status".
609 */
610 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
611 }
612
613 return err;
614 }
615
mmc_blk_ioctl_cmd(struct mmc_blk_data * md,struct mmc_ioc_cmd __user * ic_ptr,struct mmc_rpmb_data * rpmb)616 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
617 struct mmc_ioc_cmd __user *ic_ptr,
618 struct mmc_rpmb_data *rpmb)
619 {
620 struct mmc_blk_ioc_data *idata;
621 struct mmc_blk_ioc_data *idatas[1];
622 struct mmc_queue *mq;
623 struct mmc_card *card;
624 int err = 0, ioc_err = 0;
625 struct request *req;
626
627 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
628 if (IS_ERR(idata))
629 return PTR_ERR(idata);
630 /* This will be NULL on non-RPMB ioctl():s */
631 idata->rpmb = rpmb;
632
633 card = md->queue.card;
634 if (IS_ERR(card)) {
635 err = PTR_ERR(card);
636 goto cmd_done;
637 }
638
639 /*
640 * Dispatch the ioctl() into the block request queue.
641 */
642 mq = &md->queue;
643 req = blk_get_request(mq->queue,
644 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
645 if (IS_ERR(req)) {
646 err = PTR_ERR(req);
647 goto cmd_done;
648 }
649 idatas[0] = idata;
650 req_to_mmc_queue_req(req)->drv_op =
651 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
652 req_to_mmc_queue_req(req)->drv_op_data = idatas;
653 req_to_mmc_queue_req(req)->ioc_count = 1;
654 blk_execute_rq(mq->queue, NULL, req, 0);
655 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
656 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
657 blk_put_request(req);
658
659 cmd_done:
660 kfree(idata->buf);
661 kfree(idata);
662 return ioc_err ? ioc_err : err;
663 }
664
mmc_blk_ioctl_multi_cmd(struct mmc_blk_data * md,struct mmc_ioc_multi_cmd __user * user,struct mmc_rpmb_data * rpmb)665 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
666 struct mmc_ioc_multi_cmd __user *user,
667 struct mmc_rpmb_data *rpmb)
668 {
669 struct mmc_blk_ioc_data **idata = NULL;
670 struct mmc_ioc_cmd __user *cmds = user->cmds;
671 struct mmc_card *card;
672 struct mmc_queue *mq;
673 int i, err = 0, ioc_err = 0;
674 __u64 num_of_cmds;
675 struct request *req;
676
677 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
678 sizeof(num_of_cmds)))
679 return -EFAULT;
680
681 if (!num_of_cmds)
682 return 0;
683
684 if (num_of_cmds > MMC_IOC_MAX_CMDS)
685 return -EINVAL;
686
687 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
688 if (!idata)
689 return -ENOMEM;
690
691 for (i = 0; i < num_of_cmds; i++) {
692 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
693 if (IS_ERR(idata[i])) {
694 err = PTR_ERR(idata[i]);
695 num_of_cmds = i;
696 goto cmd_err;
697 }
698 /* This will be NULL on non-RPMB ioctl():s */
699 idata[i]->rpmb = rpmb;
700 }
701
702 card = md->queue.card;
703 if (IS_ERR(card)) {
704 err = PTR_ERR(card);
705 goto cmd_err;
706 }
707
708
709 /*
710 * Dispatch the ioctl()s into the block request queue.
711 */
712 mq = &md->queue;
713 req = blk_get_request(mq->queue,
714 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
715 if (IS_ERR(req)) {
716 err = PTR_ERR(req);
717 goto cmd_err;
718 }
719 req_to_mmc_queue_req(req)->drv_op =
720 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
721 req_to_mmc_queue_req(req)->drv_op_data = idata;
722 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
723 blk_execute_rq(mq->queue, NULL, req, 0);
724 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
725
726 /* copy to user if data and response */
727 for (i = 0; i < num_of_cmds && !err; i++)
728 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
729
730 blk_put_request(req);
731
732 cmd_err:
733 for (i = 0; i < num_of_cmds; i++) {
734 kfree(idata[i]->buf);
735 kfree(idata[i]);
736 }
737 kfree(idata);
738 return ioc_err ? ioc_err : err;
739 }
740
mmc_blk_check_blkdev(struct block_device * bdev)741 static int mmc_blk_check_blkdev(struct block_device *bdev)
742 {
743 /*
744 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
745 * whole block device, not on a partition. This prevents overspray
746 * between sibling partitions.
747 */
748 if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
749 return -EPERM;
750 return 0;
751 }
752
mmc_blk_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)753 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
754 unsigned int cmd, unsigned long arg)
755 {
756 struct mmc_blk_data *md;
757 int ret;
758
759 switch (cmd) {
760 case MMC_IOC_CMD:
761 ret = mmc_blk_check_blkdev(bdev);
762 if (ret)
763 return ret;
764 md = mmc_blk_get(bdev->bd_disk);
765 if (!md)
766 return -EINVAL;
767 ret = mmc_blk_ioctl_cmd(md,
768 (struct mmc_ioc_cmd __user *)arg,
769 NULL);
770 mmc_blk_put(md);
771 return ret;
772 case MMC_IOC_MULTI_CMD:
773 ret = mmc_blk_check_blkdev(bdev);
774 if (ret)
775 return ret;
776 md = mmc_blk_get(bdev->bd_disk);
777 if (!md)
778 return -EINVAL;
779 ret = mmc_blk_ioctl_multi_cmd(md,
780 (struct mmc_ioc_multi_cmd __user *)arg,
781 NULL);
782 mmc_blk_put(md);
783 return ret;
784 default:
785 return -EINVAL;
786 }
787 }
788
789 #ifdef CONFIG_COMPAT
mmc_blk_compat_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)790 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
791 unsigned int cmd, unsigned long arg)
792 {
793 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
794 }
795 #endif
796
797 static const struct block_device_operations mmc_bdops = {
798 .open = mmc_blk_open,
799 .release = mmc_blk_release,
800 .getgeo = mmc_blk_getgeo,
801 .owner = THIS_MODULE,
802 .ioctl = mmc_blk_ioctl,
803 #ifdef CONFIG_COMPAT
804 .compat_ioctl = mmc_blk_compat_ioctl,
805 #endif
806 };
807
mmc_blk_part_switch_pre(struct mmc_card * card,unsigned int part_type)808 static int mmc_blk_part_switch_pre(struct mmc_card *card,
809 unsigned int part_type)
810 {
811 int ret = 0;
812
813 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
814 if (card->ext_csd.cmdq_en) {
815 ret = mmc_cmdq_disable(card);
816 if (ret)
817 return ret;
818 }
819 mmc_retune_pause(card->host);
820 }
821
822 return ret;
823 }
824
mmc_blk_part_switch_post(struct mmc_card * card,unsigned int part_type)825 static int mmc_blk_part_switch_post(struct mmc_card *card,
826 unsigned int part_type)
827 {
828 int ret = 0;
829
830 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
831 mmc_retune_unpause(card->host);
832 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
833 ret = mmc_cmdq_enable(card);
834 }
835
836 return ret;
837 }
838
mmc_blk_part_switch(struct mmc_card * card,unsigned int part_type)839 static inline int mmc_blk_part_switch(struct mmc_card *card,
840 unsigned int part_type)
841 {
842 int ret = 0;
843 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
844
845 if (main_md->part_curr == part_type)
846 return 0;
847
848 if (mmc_card_mmc(card)) {
849 u8 part_config = card->ext_csd.part_config;
850
851 ret = mmc_blk_part_switch_pre(card, part_type);
852 if (ret)
853 return ret;
854
855 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
856 part_config |= part_type;
857
858 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
859 EXT_CSD_PART_CONFIG, part_config,
860 card->ext_csd.part_time);
861 if (ret) {
862 mmc_blk_part_switch_post(card, part_type);
863 return ret;
864 }
865
866 card->ext_csd.part_config = part_config;
867
868 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
869 }
870
871 main_md->part_curr = part_type;
872 return ret;
873 }
874
mmc_sd_num_wr_blocks(struct mmc_card * card,u32 * written_blocks)875 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
876 {
877 int err;
878 u32 result;
879 __be32 *blocks;
880
881 struct mmc_request mrq = {};
882 struct mmc_command cmd = {};
883 struct mmc_data data = {};
884
885 struct scatterlist sg;
886
887 cmd.opcode = MMC_APP_CMD;
888 cmd.arg = card->rca << 16;
889 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
890
891 err = mmc_wait_for_cmd(card->host, &cmd, 0);
892 if (err)
893 return err;
894 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
895 return -EIO;
896
897 memset(&cmd, 0, sizeof(struct mmc_command));
898
899 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
900 cmd.arg = 0;
901 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
902
903 data.blksz = 4;
904 data.blocks = 1;
905 data.flags = MMC_DATA_READ;
906 data.sg = &sg;
907 data.sg_len = 1;
908 mmc_set_data_timeout(&data, card);
909
910 mrq.cmd = &cmd;
911 mrq.data = &data;
912
913 blocks = kmalloc(4, GFP_KERNEL);
914 if (!blocks)
915 return -ENOMEM;
916
917 sg_init_one(&sg, blocks, 4);
918
919 mmc_wait_for_req(card->host, &mrq);
920
921 result = ntohl(*blocks);
922 kfree(blocks);
923
924 if (cmd.error || data.error)
925 return -EIO;
926
927 *written_blocks = result;
928
929 return 0;
930 }
931
mmc_blk_clock_khz(struct mmc_host * host)932 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
933 {
934 if (host->actual_clock)
935 return host->actual_clock / 1000;
936
937 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
938 if (host->ios.clock)
939 return host->ios.clock / 2000;
940
941 /* How can there be no clock */
942 WARN_ON_ONCE(1);
943 return 100; /* 100 kHz is minimum possible value */
944 }
945
mmc_blk_data_timeout_ms(struct mmc_host * host,struct mmc_data * data)946 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
947 struct mmc_data *data)
948 {
949 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
950 unsigned int khz;
951
952 if (data->timeout_clks) {
953 khz = mmc_blk_clock_khz(host);
954 ms += DIV_ROUND_UP(data->timeout_clks, khz);
955 }
956
957 return ms;
958 }
959
mmc_blk_reset(struct mmc_blk_data * md,struct mmc_host * host,int type)960 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
961 int type)
962 {
963 int err;
964
965 if (md->reset_done & type)
966 return -EEXIST;
967
968 md->reset_done |= type;
969 err = mmc_hw_reset(host);
970 /* Ensure we switch back to the correct partition */
971 if (err != -EOPNOTSUPP) {
972 struct mmc_blk_data *main_md =
973 dev_get_drvdata(&host->card->dev);
974 int part_err;
975 bool allow = true;
976
977 trace_android_vh_mmc_blk_reset(host, err, &allow);
978 if (!allow)
979 return -ENODEV;
980
981 main_md->part_curr = main_md->part_type;
982 part_err = mmc_blk_part_switch(host->card, md->part_type);
983 if (part_err) {
984 /*
985 * We have failed to get back into the correct
986 * partition, so we need to abort the whole request.
987 */
988 return -ENODEV;
989 }
990 }
991 return err;
992 }
993
mmc_blk_reset_success(struct mmc_blk_data * md,int type)994 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
995 {
996 md->reset_done &= ~type;
997 }
998
999 /*
1000 * The non-block commands come back from the block layer after it queued it and
1001 * processed it with all other requests and then they get issued in this
1002 * function.
1003 */
mmc_blk_issue_drv_op(struct mmc_queue * mq,struct request * req)1004 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1005 {
1006 struct mmc_queue_req *mq_rq;
1007 struct mmc_card *card = mq->card;
1008 struct mmc_blk_data *md = mq->blkdata;
1009 struct mmc_blk_ioc_data **idata;
1010 bool rpmb_ioctl;
1011 u8 **ext_csd;
1012 u32 status;
1013 int ret;
1014 int i;
1015
1016 mq_rq = req_to_mmc_queue_req(req);
1017 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1018
1019 switch (mq_rq->drv_op) {
1020 case MMC_DRV_OP_IOCTL:
1021 if (card->ext_csd.cmdq_en) {
1022 ret = mmc_cmdq_disable(card);
1023 if (ret)
1024 break;
1025 }
1026 fallthrough;
1027 case MMC_DRV_OP_IOCTL_RPMB:
1028 idata = mq_rq->drv_op_data;
1029 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1030 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1031 if (ret)
1032 break;
1033 }
1034 /* Always switch back to main area after RPMB access */
1035 if (rpmb_ioctl)
1036 mmc_blk_part_switch(card, 0);
1037 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1038 mmc_cmdq_enable(card);
1039 break;
1040 case MMC_DRV_OP_BOOT_WP:
1041 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1042 card->ext_csd.boot_ro_lock |
1043 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1044 card->ext_csd.part_time);
1045 if (ret)
1046 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1047 md->disk->disk_name, ret);
1048 else
1049 card->ext_csd.boot_ro_lock |=
1050 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1051 break;
1052 case MMC_DRV_OP_GET_CARD_STATUS:
1053 ret = mmc_send_status(card, &status);
1054 if (!ret)
1055 ret = status;
1056 break;
1057 case MMC_DRV_OP_GET_EXT_CSD:
1058 ext_csd = mq_rq->drv_op_data;
1059 ret = mmc_get_ext_csd(card, ext_csd);
1060 break;
1061 default:
1062 pr_err("%s: unknown driver specific operation\n",
1063 md->disk->disk_name);
1064 ret = -EINVAL;
1065 break;
1066 }
1067 mq_rq->drv_op_result = ret;
1068 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1069 }
1070
mmc_blk_issue_discard_rq(struct mmc_queue * mq,struct request * req)1071 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1072 {
1073 struct mmc_blk_data *md = mq->blkdata;
1074 struct mmc_card *card = md->queue.card;
1075 unsigned int from, nr;
1076 int err = 0, type = MMC_BLK_DISCARD;
1077 blk_status_t status = BLK_STS_OK;
1078
1079 if (!mmc_can_erase(card)) {
1080 status = BLK_STS_NOTSUPP;
1081 goto fail;
1082 }
1083
1084 from = blk_rq_pos(req);
1085 nr = blk_rq_sectors(req);
1086
1087 do {
1088 unsigned int erase_arg = card->erase_arg;
1089
1090 if (mmc_card_broken_sd_discard(card))
1091 erase_arg = SD_ERASE_ARG;
1092
1093 err = 0;
1094 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1095 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1096 INAND_CMD38_ARG_EXT_CSD,
1097 card->erase_arg == MMC_TRIM_ARG ?
1098 INAND_CMD38_ARG_TRIM :
1099 INAND_CMD38_ARG_ERASE,
1100 card->ext_csd.generic_cmd6_time);
1101 }
1102 if (!err)
1103 err = mmc_erase(card, from, nr, erase_arg);
1104 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1105 if (err)
1106 status = BLK_STS_IOERR;
1107 else
1108 mmc_blk_reset_success(md, type);
1109 fail:
1110 blk_mq_end_request(req, status);
1111 }
1112
mmc_blk_issue_secdiscard_rq(struct mmc_queue * mq,struct request * req)1113 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1114 struct request *req)
1115 {
1116 struct mmc_blk_data *md = mq->blkdata;
1117 struct mmc_card *card = md->queue.card;
1118 unsigned int from, nr, arg;
1119 int err = 0, type = MMC_BLK_SECDISCARD;
1120 blk_status_t status = BLK_STS_OK;
1121
1122 if (!(mmc_can_secure_erase_trim(card))) {
1123 status = BLK_STS_NOTSUPP;
1124 goto out;
1125 }
1126
1127 from = blk_rq_pos(req);
1128 nr = blk_rq_sectors(req);
1129
1130 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1131 arg = MMC_SECURE_TRIM1_ARG;
1132 else
1133 arg = MMC_SECURE_ERASE_ARG;
1134
1135 retry:
1136 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1137 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1138 INAND_CMD38_ARG_EXT_CSD,
1139 arg == MMC_SECURE_TRIM1_ARG ?
1140 INAND_CMD38_ARG_SECTRIM1 :
1141 INAND_CMD38_ARG_SECERASE,
1142 card->ext_csd.generic_cmd6_time);
1143 if (err)
1144 goto out_retry;
1145 }
1146
1147 err = mmc_erase(card, from, nr, arg);
1148 if (err == -EIO)
1149 goto out_retry;
1150 if (err) {
1151 status = BLK_STS_IOERR;
1152 goto out;
1153 }
1154
1155 if (arg == MMC_SECURE_TRIM1_ARG) {
1156 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1157 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1158 INAND_CMD38_ARG_EXT_CSD,
1159 INAND_CMD38_ARG_SECTRIM2,
1160 card->ext_csd.generic_cmd6_time);
1161 if (err)
1162 goto out_retry;
1163 }
1164
1165 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1166 if (err == -EIO)
1167 goto out_retry;
1168 if (err) {
1169 status = BLK_STS_IOERR;
1170 goto out;
1171 }
1172 }
1173
1174 out_retry:
1175 if (err && !mmc_blk_reset(md, card->host, type))
1176 goto retry;
1177 if (!err)
1178 mmc_blk_reset_success(md, type);
1179 out:
1180 blk_mq_end_request(req, status);
1181 }
1182
mmc_blk_issue_flush(struct mmc_queue * mq,struct request * req)1183 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1184 {
1185 struct mmc_blk_data *md = mq->blkdata;
1186 struct mmc_card *card = md->queue.card;
1187 int ret = 0;
1188
1189 ret = mmc_flush_cache(card);
1190 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1191 }
1192
1193 /*
1194 * Reformat current write as a reliable write, supporting
1195 * both legacy and the enhanced reliable write MMC cards.
1196 * In each transfer we'll handle only as much as a single
1197 * reliable write can handle, thus finish the request in
1198 * partial completions.
1199 */
mmc_apply_rel_rw(struct mmc_blk_request * brq,struct mmc_card * card,struct request * req)1200 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1201 struct mmc_card *card,
1202 struct request *req)
1203 {
1204 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1205 /* Legacy mode imposes restrictions on transfers. */
1206 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1207 brq->data.blocks = 1;
1208
1209 if (brq->data.blocks > card->ext_csd.rel_sectors)
1210 brq->data.blocks = card->ext_csd.rel_sectors;
1211 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1212 brq->data.blocks = 1;
1213 }
1214 }
1215
1216 #define CMD_ERRORS_EXCL_OOR \
1217 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1218 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1219 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1220 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1221 R1_CC_ERROR | /* Card controller error */ \
1222 R1_ERROR) /* General/unknown error */
1223
1224 #define CMD_ERRORS \
1225 (CMD_ERRORS_EXCL_OOR | \
1226 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1227
mmc_blk_eval_resp_error(struct mmc_blk_request * brq)1228 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1229 {
1230 u32 val;
1231
1232 /*
1233 * Per the SD specification(physical layer version 4.10)[1],
1234 * section 4.3.3, it explicitly states that "When the last
1235 * block of user area is read using CMD18, the host should
1236 * ignore OUT_OF_RANGE error that may occur even the sequence
1237 * is correct". And JESD84-B51 for eMMC also has a similar
1238 * statement on section 6.8.3.
1239 *
1240 * Multiple block read/write could be done by either predefined
1241 * method, namely CMD23, or open-ending mode. For open-ending mode,
1242 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1243 *
1244 * However the spec[1] doesn't tell us whether we should also
1245 * ignore that for predefined method. But per the spec[1], section
1246 * 4.15 Set Block Count Command, it says"If illegal block count
1247 * is set, out of range error will be indicated during read/write
1248 * operation (For example, data transfer is stopped at user area
1249 * boundary)." In another word, we could expect a out of range error
1250 * in the response for the following CMD18/25. And if argument of
1251 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1252 * we could also expect to get a -ETIMEDOUT or any error number from
1253 * the host drivers due to missing data response(for write)/data(for
1254 * read), as the cards will stop the data transfer by itself per the
1255 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1256 */
1257
1258 if (!brq->stop.error) {
1259 bool oor_with_open_end;
1260 /* If there is no error yet, check R1 response */
1261
1262 val = brq->stop.resp[0] & CMD_ERRORS;
1263 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1264
1265 if (val && !oor_with_open_end)
1266 brq->stop.error = -EIO;
1267 }
1268 }
1269
mmc_blk_data_prep(struct mmc_queue * mq,struct mmc_queue_req * mqrq,int recovery_mode,bool * do_rel_wr_p,bool * do_data_tag_p)1270 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1271 int recovery_mode, bool *do_rel_wr_p,
1272 bool *do_data_tag_p)
1273 {
1274 struct mmc_blk_data *md = mq->blkdata;
1275 struct mmc_card *card = md->queue.card;
1276 struct mmc_blk_request *brq = &mqrq->brq;
1277 struct request *req = mmc_queue_req_to_req(mqrq);
1278 bool do_rel_wr, do_data_tag;
1279
1280 /*
1281 * Reliable writes are used to implement Forced Unit Access and
1282 * are supported only on MMCs.
1283 */
1284 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1285 rq_data_dir(req) == WRITE &&
1286 (md->flags & MMC_BLK_REL_WR);
1287
1288 memset(brq, 0, sizeof(struct mmc_blk_request));
1289
1290 mmc_crypto_prepare_req(mqrq);
1291
1292 brq->mrq.data = &brq->data;
1293 brq->mrq.tag = req->tag;
1294
1295 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1296 brq->stop.arg = 0;
1297
1298 if (rq_data_dir(req) == READ) {
1299 brq->data.flags = MMC_DATA_READ;
1300 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1301 } else {
1302 brq->data.flags = MMC_DATA_WRITE;
1303 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1304 }
1305
1306 brq->data.blksz = 512;
1307 brq->data.blocks = blk_rq_sectors(req);
1308 brq->data.blk_addr = blk_rq_pos(req);
1309
1310 /*
1311 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1312 * The eMMC will give "high" priority tasks priority over "simple"
1313 * priority tasks. Here we always set "simple" priority by not setting
1314 * MMC_DATA_PRIO.
1315 */
1316
1317 /*
1318 * The block layer doesn't support all sector count
1319 * restrictions, so we need to be prepared for too big
1320 * requests.
1321 */
1322 if (brq->data.blocks > card->host->max_blk_count)
1323 brq->data.blocks = card->host->max_blk_count;
1324
1325 if (brq->data.blocks > 1) {
1326 /*
1327 * Some SD cards in SPI mode return a CRC error or even lock up
1328 * completely when trying to read the last block using a
1329 * multiblock read command.
1330 */
1331 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1332 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1333 get_capacity(md->disk)))
1334 brq->data.blocks--;
1335
1336 /*
1337 * After a read error, we redo the request one (native) sector
1338 * at a time in order to accurately determine which
1339 * sectors can be read successfully.
1340 */
1341 if (recovery_mode)
1342 brq->data.blocks = queue_physical_block_size(mq->queue) >> 9;
1343
1344 /*
1345 * Some controllers have HW issues while operating
1346 * in multiple I/O mode
1347 */
1348 if (card->host->ops->multi_io_quirk)
1349 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1350 (rq_data_dir(req) == READ) ?
1351 MMC_DATA_READ : MMC_DATA_WRITE,
1352 brq->data.blocks);
1353 }
1354
1355 if (do_rel_wr) {
1356 mmc_apply_rel_rw(brq, card, req);
1357 brq->data.flags |= MMC_DATA_REL_WR;
1358 }
1359
1360 /*
1361 * Data tag is used only during writing meta data to speed
1362 * up write and any subsequent read of this meta data
1363 */
1364 do_data_tag = card->ext_csd.data_tag_unit_size &&
1365 (req->cmd_flags & REQ_META) &&
1366 (rq_data_dir(req) == WRITE) &&
1367 ((brq->data.blocks * brq->data.blksz) >=
1368 card->ext_csd.data_tag_unit_size);
1369
1370 if (do_data_tag)
1371 brq->data.flags |= MMC_DATA_DAT_TAG;
1372
1373 mmc_set_data_timeout(&brq->data, card);
1374
1375 brq->data.sg = mqrq->sg;
1376 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1377
1378 /*
1379 * Adjust the sg list so it is the same size as the
1380 * request.
1381 */
1382 if (brq->data.blocks != blk_rq_sectors(req)) {
1383 int i, data_size = brq->data.blocks << 9;
1384 struct scatterlist *sg;
1385
1386 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1387 data_size -= sg->length;
1388 if (data_size <= 0) {
1389 sg->length += data_size;
1390 i++;
1391 break;
1392 }
1393 }
1394 brq->data.sg_len = i;
1395 }
1396
1397 if (do_rel_wr_p)
1398 *do_rel_wr_p = do_rel_wr;
1399
1400 if (do_data_tag_p)
1401 *do_data_tag_p = do_data_tag;
1402 }
1403
1404 #define MMC_CQE_RETRIES 2
1405
mmc_blk_cqe_complete_rq(struct mmc_queue * mq,struct request * req)1406 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1407 {
1408 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1409 struct mmc_request *mrq = &mqrq->brq.mrq;
1410 struct request_queue *q = req->q;
1411 struct mmc_host *host = mq->card->host;
1412 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1413 unsigned long flags;
1414 bool put_card;
1415 int err;
1416
1417 mmc_cqe_post_req(host, mrq);
1418
1419 if (mrq->cmd && mrq->cmd->error)
1420 err = mrq->cmd->error;
1421 else if (mrq->data && mrq->data->error)
1422 err = mrq->data->error;
1423 else
1424 err = 0;
1425
1426 if (err) {
1427 if (mqrq->retries++ < MMC_CQE_RETRIES)
1428 blk_mq_requeue_request(req, true);
1429 else
1430 blk_mq_end_request(req, BLK_STS_IOERR);
1431 } else if (mrq->data) {
1432 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1433 blk_mq_requeue_request(req, true);
1434 else
1435 __blk_mq_end_request(req, BLK_STS_OK);
1436 } else {
1437 blk_mq_end_request(req, BLK_STS_OK);
1438 }
1439
1440 spin_lock_irqsave(&mq->lock, flags);
1441
1442 mq->in_flight[issue_type] -= 1;
1443
1444 put_card = (mmc_tot_in_flight(mq) == 0);
1445
1446 mmc_cqe_check_busy(mq);
1447
1448 spin_unlock_irqrestore(&mq->lock, flags);
1449
1450 if (!mq->cqe_busy)
1451 blk_mq_run_hw_queues(q, true);
1452
1453 if (put_card)
1454 mmc_put_card(mq->card, &mq->ctx);
1455 }
1456
mmc_blk_cqe_recovery(struct mmc_queue * mq)1457 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1458 {
1459 struct mmc_card *card = mq->card;
1460 struct mmc_host *host = card->host;
1461 int err;
1462
1463 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1464
1465 err = mmc_cqe_recovery(host);
1466 if (err)
1467 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1468 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1469
1470 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1471 }
1472
mmc_blk_cqe_req_done(struct mmc_request * mrq)1473 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1474 {
1475 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1476 brq.mrq);
1477 struct request *req = mmc_queue_req_to_req(mqrq);
1478 struct request_queue *q = req->q;
1479 struct mmc_queue *mq = q->queuedata;
1480
1481 /*
1482 * Block layer timeouts race with completions which means the normal
1483 * completion path cannot be used during recovery.
1484 */
1485 if (mq->in_recovery)
1486 mmc_blk_cqe_complete_rq(mq, req);
1487 else if (likely(!blk_should_fake_timeout(req->q)))
1488 blk_mq_complete_request(req);
1489 }
1490
mmc_blk_cqe_start_req(struct mmc_host * host,struct mmc_request * mrq)1491 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1492 {
1493 mrq->done = mmc_blk_cqe_req_done;
1494 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1495
1496 return mmc_cqe_start_req(host, mrq);
1497 }
1498
mmc_blk_cqe_prep_dcmd(struct mmc_queue_req * mqrq,struct request * req)1499 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1500 struct request *req)
1501 {
1502 struct mmc_blk_request *brq = &mqrq->brq;
1503
1504 memset(brq, 0, sizeof(*brq));
1505
1506 brq->mrq.cmd = &brq->cmd;
1507 brq->mrq.tag = req->tag;
1508
1509 return &brq->mrq;
1510 }
1511
mmc_blk_cqe_issue_flush(struct mmc_queue * mq,struct request * req)1512 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1513 {
1514 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1515 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1516
1517 mrq->cmd->opcode = MMC_SWITCH;
1518 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1519 (EXT_CSD_FLUSH_CACHE << 16) |
1520 (1 << 8) |
1521 EXT_CSD_CMD_SET_NORMAL;
1522 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1523
1524 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1525 }
1526
mmc_blk_hsq_issue_rw_rq(struct mmc_queue * mq,struct request * req)1527 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1528 {
1529 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1530 struct mmc_host *host = mq->card->host;
1531 int err;
1532
1533 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1534 mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1535 mmc_pre_req(host, &mqrq->brq.mrq);
1536
1537 err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1538 if (err)
1539 mmc_post_req(host, &mqrq->brq.mrq, err);
1540
1541 return err;
1542 }
1543
mmc_blk_cqe_issue_rw_rq(struct mmc_queue * mq,struct request * req)1544 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1545 {
1546 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1547 struct mmc_host *host = mq->card->host;
1548
1549 if (host->hsq_enabled)
1550 return mmc_blk_hsq_issue_rw_rq(mq, req);
1551
1552 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1553
1554 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1555 }
1556
mmc_blk_rw_rq_prep(struct mmc_queue_req * mqrq,struct mmc_card * card,int recovery_mode,struct mmc_queue * mq)1557 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1558 struct mmc_card *card,
1559 int recovery_mode,
1560 struct mmc_queue *mq)
1561 {
1562 u32 readcmd, writecmd;
1563 struct mmc_blk_request *brq = &mqrq->brq;
1564 struct request *req = mmc_queue_req_to_req(mqrq);
1565 struct mmc_blk_data *md = mq->blkdata;
1566 bool do_rel_wr, do_data_tag;
1567
1568 mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag);
1569
1570 brq->mrq.cmd = &brq->cmd;
1571
1572 brq->cmd.arg = blk_rq_pos(req);
1573 if (!mmc_card_blockaddr(card))
1574 brq->cmd.arg <<= 9;
1575 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1576
1577 if (brq->data.blocks > 1 || do_rel_wr) {
1578 /* SPI multiblock writes terminate using a special
1579 * token, not a STOP_TRANSMISSION request.
1580 */
1581 if (!mmc_host_is_spi(card->host) ||
1582 rq_data_dir(req) == READ)
1583 brq->mrq.stop = &brq->stop;
1584 readcmd = MMC_READ_MULTIPLE_BLOCK;
1585 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1586 } else {
1587 brq->mrq.stop = NULL;
1588 readcmd = MMC_READ_SINGLE_BLOCK;
1589 writecmd = MMC_WRITE_BLOCK;
1590 }
1591 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1592
1593 /*
1594 * Pre-defined multi-block transfers are preferable to
1595 * open ended-ones (and necessary for reliable writes).
1596 * However, it is not sufficient to just send CMD23,
1597 * and avoid the final CMD12, as on an error condition
1598 * CMD12 (stop) needs to be sent anyway. This, coupled
1599 * with Auto-CMD23 enhancements provided by some
1600 * hosts, means that the complexity of dealing
1601 * with this is best left to the host. If CMD23 is
1602 * supported by card and host, we'll fill sbc in and let
1603 * the host deal with handling it correctly. This means
1604 * that for hosts that don't expose MMC_CAP_CMD23, no
1605 * change of behavior will be observed.
1606 *
1607 * N.B: Some MMC cards experience perf degradation.
1608 * We'll avoid using CMD23-bounded multiblock writes for
1609 * these, while retaining features like reliable writes.
1610 */
1611 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1612 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1613 do_data_tag)) {
1614 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1615 brq->sbc.arg = brq->data.blocks |
1616 (do_rel_wr ? (1 << 31) : 0) |
1617 (do_data_tag ? (1 << 29) : 0);
1618 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1619 brq->mrq.sbc = &brq->sbc;
1620 }
1621 }
1622
1623 #define MMC_MAX_RETRIES 5
1624 #define MMC_DATA_RETRIES 2
1625 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1626
mmc_blk_send_stop(struct mmc_card * card,unsigned int timeout)1627 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1628 {
1629 struct mmc_command cmd = {
1630 .opcode = MMC_STOP_TRANSMISSION,
1631 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1632 /* Some hosts wait for busy anyway, so provide a busy timeout */
1633 .busy_timeout = timeout,
1634 };
1635
1636 return mmc_wait_for_cmd(card->host, &cmd, 5);
1637 }
1638
mmc_blk_fix_state(struct mmc_card * card,struct request * req)1639 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1640 {
1641 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1642 struct mmc_blk_request *brq = &mqrq->brq;
1643 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1644 int err;
1645
1646 mmc_retune_hold_now(card->host);
1647
1648 mmc_blk_send_stop(card, timeout);
1649
1650 err = card_busy_detect(card, timeout, NULL);
1651
1652 mmc_retune_release(card->host);
1653
1654 return err;
1655 }
1656
1657 #define MMC_READ_SINGLE_RETRIES 2
1658
1659 /* Single (native) sector read during recovery */
mmc_blk_read_single(struct mmc_queue * mq,struct request * req)1660 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1661 {
1662 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1663 struct mmc_request *mrq = &mqrq->brq.mrq;
1664 struct mmc_card *card = mq->card;
1665 struct mmc_host *host = card->host;
1666 blk_status_t error = BLK_STS_OK;
1667 size_t bytes_per_read = queue_physical_block_size(mq->queue);
1668
1669 do {
1670 u32 status;
1671 int err;
1672 int retries = 0;
1673
1674 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1675 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1676
1677 mmc_wait_for_req(host, mrq);
1678
1679 err = mmc_send_status(card, &status);
1680 if (err)
1681 goto error_exit;
1682
1683 if (!mmc_host_is_spi(host) &&
1684 !mmc_ready_for_data(status)) {
1685 err = mmc_blk_fix_state(card, req);
1686 if (err)
1687 goto error_exit;
1688 }
1689
1690 if (!mrq->cmd->error)
1691 break;
1692 }
1693
1694 if (mrq->cmd->error ||
1695 mrq->data->error ||
1696 (!mmc_host_is_spi(host) &&
1697 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1698 error = BLK_STS_IOERR;
1699 else
1700 error = BLK_STS_OK;
1701
1702 } while (blk_update_request(req, error, bytes_per_read));
1703
1704 return;
1705
1706 error_exit:
1707 mrq->data->bytes_xfered = 0;
1708 blk_update_request(req, BLK_STS_IOERR, bytes_per_read);
1709 /* Let it try the remaining request again */
1710 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1711 mqrq->retries = MMC_MAX_RETRIES - 1;
1712 }
1713
mmc_blk_oor_valid(struct mmc_blk_request * brq)1714 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1715 {
1716 return !!brq->mrq.sbc;
1717 }
1718
mmc_blk_stop_err_bits(struct mmc_blk_request * brq)1719 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1720 {
1721 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1722 }
1723
1724 /*
1725 * Check for errors the host controller driver might not have seen such as
1726 * response mode errors or invalid card state.
1727 */
mmc_blk_status_error(struct request * req,u32 status)1728 static bool mmc_blk_status_error(struct request *req, u32 status)
1729 {
1730 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1731 struct mmc_blk_request *brq = &mqrq->brq;
1732 struct mmc_queue *mq = req->q->queuedata;
1733 u32 stop_err_bits;
1734
1735 if (mmc_host_is_spi(mq->card->host))
1736 return false;
1737
1738 stop_err_bits = mmc_blk_stop_err_bits(brq);
1739
1740 return brq->cmd.resp[0] & CMD_ERRORS ||
1741 brq->stop.resp[0] & stop_err_bits ||
1742 status & stop_err_bits ||
1743 (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1744 }
1745
mmc_blk_cmd_started(struct mmc_blk_request * brq)1746 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1747 {
1748 return !brq->sbc.error && !brq->cmd.error &&
1749 !(brq->cmd.resp[0] & CMD_ERRORS);
1750 }
1751
1752 /*
1753 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1754 * policy:
1755 * 1. A request that has transferred at least some data is considered
1756 * successful and will be requeued if there is remaining data to
1757 * transfer.
1758 * 2. Otherwise the number of retries is incremented and the request
1759 * will be requeued if there are remaining retries.
1760 * 3. Otherwise the request will be errored out.
1761 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1762 * mqrq->retries. So there are only 4 possible actions here:
1763 * 1. do not accept the bytes_xfered value i.e. set it to zero
1764 * 2. change mqrq->retries to determine the number of retries
1765 * 3. try to reset the card
1766 * 4. read one sector at a time
1767 */
mmc_blk_mq_rw_recovery(struct mmc_queue * mq,struct request * req)1768 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1769 {
1770 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1771 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1772 struct mmc_blk_request *brq = &mqrq->brq;
1773 struct mmc_blk_data *md = mq->blkdata;
1774 struct mmc_card *card = mq->card;
1775 u32 status;
1776 u32 blocks;
1777 int err;
1778
1779 /*
1780 * Some errors the host driver might not have seen. Set the number of
1781 * bytes transferred to zero in that case.
1782 */
1783 err = __mmc_send_status(card, &status, 0);
1784 if (err || mmc_blk_status_error(req, status))
1785 brq->data.bytes_xfered = 0;
1786
1787 mmc_retune_release(card->host);
1788
1789 /*
1790 * Try again to get the status. This also provides an opportunity for
1791 * re-tuning.
1792 */
1793 if (err)
1794 err = __mmc_send_status(card, &status, 0);
1795
1796 /*
1797 * Nothing more to do after the number of bytes transferred has been
1798 * updated and there is no card.
1799 */
1800 if (err && mmc_detect_card_removed(card->host))
1801 return;
1802
1803 /* Try to get back to "tran" state */
1804 if (!mmc_host_is_spi(mq->card->host) &&
1805 (err || !mmc_ready_for_data(status)))
1806 err = mmc_blk_fix_state(mq->card, req);
1807
1808 /*
1809 * Special case for SD cards where the card might record the number of
1810 * blocks written.
1811 */
1812 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1813 rq_data_dir(req) == WRITE) {
1814 if (mmc_sd_num_wr_blocks(card, &blocks))
1815 brq->data.bytes_xfered = 0;
1816 else
1817 brq->data.bytes_xfered = blocks << 9;
1818 }
1819
1820 /* Reset if the card is in a bad state */
1821 if (!mmc_host_is_spi(mq->card->host) &&
1822 err && mmc_blk_reset(md, card->host, type)) {
1823 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1824 mqrq->retries = MMC_NO_RETRIES;
1825 trace_android_vh_mmc_blk_mq_rw_recovery(card);
1826 return;
1827 }
1828
1829 /*
1830 * If anything was done, just return and if there is anything remaining
1831 * on the request it will get requeued.
1832 */
1833 if (brq->data.bytes_xfered)
1834 return;
1835
1836 /* Reset before last retry */
1837 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1838 mmc_blk_reset(md, card->host, type);
1839
1840 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1841 if (brq->sbc.error || brq->cmd.error)
1842 return;
1843
1844 /* Reduce the remaining retries for data errors */
1845 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1846 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1847 return;
1848 }
1849
1850 if (rq_data_dir(req) == READ && brq->data.blocks >
1851 queue_physical_block_size(mq->queue) >> 9) {
1852 /* Read one (native) sector at a time */
1853 mmc_blk_read_single(mq, req);
1854 return;
1855 }
1856 }
1857
mmc_blk_rq_error(struct mmc_blk_request * brq)1858 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1859 {
1860 mmc_blk_eval_resp_error(brq);
1861
1862 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1863 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1864 }
1865
mmc_blk_card_busy(struct mmc_card * card,struct request * req)1866 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1867 {
1868 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1869 u32 status = 0;
1870 int err;
1871
1872 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1873 return 0;
1874
1875 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status);
1876
1877 /*
1878 * Do not assume data transferred correctly if there are any error bits
1879 * set.
1880 */
1881 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1882 mqrq->brq.data.bytes_xfered = 0;
1883 err = err ? err : -EIO;
1884 }
1885
1886 /* Copy the exception bit so it will be seen later on */
1887 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1888 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1889
1890 return err;
1891 }
1892
mmc_blk_rw_reset_success(struct mmc_queue * mq,struct request * req)1893 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1894 struct request *req)
1895 {
1896 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1897
1898 mmc_blk_reset_success(mq->blkdata, type);
1899 }
1900
mmc_blk_mq_complete_rq(struct mmc_queue * mq,struct request * req)1901 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1902 {
1903 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1904 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1905
1906 if (nr_bytes) {
1907 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1908 blk_mq_requeue_request(req, true);
1909 else
1910 __blk_mq_end_request(req, BLK_STS_OK);
1911 } else if (!blk_rq_bytes(req)) {
1912 __blk_mq_end_request(req, BLK_STS_IOERR);
1913 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1914 blk_mq_requeue_request(req, true);
1915 } else {
1916 if (mmc_card_removed(mq->card))
1917 req->rq_flags |= RQF_QUIET;
1918 blk_mq_end_request(req, BLK_STS_IOERR);
1919 }
1920 }
1921
mmc_blk_urgent_bkops_needed(struct mmc_queue * mq,struct mmc_queue_req * mqrq)1922 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1923 struct mmc_queue_req *mqrq)
1924 {
1925 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1926 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1927 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1928 }
1929
mmc_blk_urgent_bkops(struct mmc_queue * mq,struct mmc_queue_req * mqrq)1930 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1931 struct mmc_queue_req *mqrq)
1932 {
1933 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1934 mmc_run_bkops(mq->card);
1935 }
1936
mmc_blk_hsq_req_done(struct mmc_request * mrq)1937 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
1938 {
1939 struct mmc_queue_req *mqrq =
1940 container_of(mrq, struct mmc_queue_req, brq.mrq);
1941 struct request *req = mmc_queue_req_to_req(mqrq);
1942 struct request_queue *q = req->q;
1943 struct mmc_queue *mq = q->queuedata;
1944 struct mmc_host *host = mq->card->host;
1945 unsigned long flags;
1946
1947 if (mmc_blk_rq_error(&mqrq->brq) ||
1948 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
1949 spin_lock_irqsave(&mq->lock, flags);
1950 mq->recovery_needed = true;
1951 mq->recovery_req = req;
1952 spin_unlock_irqrestore(&mq->lock, flags);
1953
1954 host->cqe_ops->cqe_recovery_start(host);
1955
1956 schedule_work(&mq->recovery_work);
1957 return;
1958 }
1959
1960 mmc_blk_rw_reset_success(mq, req);
1961
1962 /*
1963 * Block layer timeouts race with completions which means the normal
1964 * completion path cannot be used during recovery.
1965 */
1966 if (mq->in_recovery)
1967 mmc_blk_cqe_complete_rq(mq, req);
1968 else if (likely(!blk_should_fake_timeout(req->q)))
1969 blk_mq_complete_request(req);
1970 }
1971
mmc_blk_mq_complete(struct request * req)1972 void mmc_blk_mq_complete(struct request *req)
1973 {
1974 struct mmc_queue *mq = req->q->queuedata;
1975
1976 if (mq->use_cqe)
1977 mmc_blk_cqe_complete_rq(mq, req);
1978 else if (likely(!blk_should_fake_timeout(req->q)))
1979 mmc_blk_mq_complete_rq(mq, req);
1980 }
1981
mmc_blk_mq_poll_completion(struct mmc_queue * mq,struct request * req)1982 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1983 struct request *req)
1984 {
1985 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1986 struct mmc_host *host = mq->card->host;
1987
1988 if (mmc_blk_rq_error(&mqrq->brq) ||
1989 mmc_blk_card_busy(mq->card, req)) {
1990 mmc_blk_mq_rw_recovery(mq, req);
1991 } else {
1992 mmc_blk_rw_reset_success(mq, req);
1993 mmc_retune_release(host);
1994 }
1995
1996 mmc_blk_urgent_bkops(mq, mqrq);
1997 }
1998
mmc_blk_mq_dec_in_flight(struct mmc_queue * mq,struct request * req)1999 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
2000 {
2001 unsigned long flags;
2002 bool put_card;
2003
2004 spin_lock_irqsave(&mq->lock, flags);
2005
2006 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2007
2008 put_card = (mmc_tot_in_flight(mq) == 0);
2009
2010 spin_unlock_irqrestore(&mq->lock, flags);
2011
2012 if (put_card)
2013 mmc_put_card(mq->card, &mq->ctx);
2014 }
2015
mmc_blk_mq_post_req(struct mmc_queue * mq,struct request * req)2016 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2017 {
2018 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2019 struct mmc_request *mrq = &mqrq->brq.mrq;
2020 struct mmc_host *host = mq->card->host;
2021
2022 mmc_post_req(host, mrq, 0);
2023
2024 /*
2025 * Block layer timeouts race with completions which means the normal
2026 * completion path cannot be used during recovery.
2027 */
2028 if (mq->in_recovery)
2029 mmc_blk_mq_complete_rq(mq, req);
2030 else if (likely(!blk_should_fake_timeout(req->q)))
2031 blk_mq_complete_request(req);
2032
2033 mmc_blk_mq_dec_in_flight(mq, req);
2034 }
2035
mmc_blk_mq_recovery(struct mmc_queue * mq)2036 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2037 {
2038 struct request *req = mq->recovery_req;
2039 struct mmc_host *host = mq->card->host;
2040 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2041
2042 mq->recovery_req = NULL;
2043 mq->rw_wait = false;
2044
2045 if (mmc_blk_rq_error(&mqrq->brq)) {
2046 mmc_retune_hold_now(host);
2047 mmc_blk_mq_rw_recovery(mq, req);
2048 }
2049
2050 mmc_blk_urgent_bkops(mq, mqrq);
2051
2052 mmc_blk_mq_post_req(mq, req);
2053 }
2054
mmc_blk_mq_complete_prev_req(struct mmc_queue * mq,struct request ** prev_req)2055 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2056 struct request **prev_req)
2057 {
2058 if (mmc_host_done_complete(mq->card->host))
2059 return;
2060
2061 mutex_lock(&mq->complete_lock);
2062
2063 if (!mq->complete_req)
2064 goto out_unlock;
2065
2066 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2067
2068 if (prev_req)
2069 *prev_req = mq->complete_req;
2070 else
2071 mmc_blk_mq_post_req(mq, mq->complete_req);
2072
2073 mq->complete_req = NULL;
2074
2075 out_unlock:
2076 mutex_unlock(&mq->complete_lock);
2077 }
2078
mmc_blk_mq_complete_work(struct work_struct * work)2079 void mmc_blk_mq_complete_work(struct work_struct *work)
2080 {
2081 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2082 complete_work);
2083
2084 mmc_blk_mq_complete_prev_req(mq, NULL);
2085 }
2086
mmc_blk_mq_req_done(struct mmc_request * mrq)2087 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2088 {
2089 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2090 brq.mrq);
2091 struct request *req = mmc_queue_req_to_req(mqrq);
2092 struct request_queue *q = req->q;
2093 struct mmc_queue *mq = q->queuedata;
2094 struct mmc_host *host = mq->card->host;
2095 unsigned long flags;
2096
2097 if (!mmc_host_done_complete(host)) {
2098 bool waiting;
2099
2100 /*
2101 * We cannot complete the request in this context, so record
2102 * that there is a request to complete, and that a following
2103 * request does not need to wait (although it does need to
2104 * complete complete_req first).
2105 */
2106 spin_lock_irqsave(&mq->lock, flags);
2107 mq->complete_req = req;
2108 mq->rw_wait = false;
2109 waiting = mq->waiting;
2110 spin_unlock_irqrestore(&mq->lock, flags);
2111
2112 /*
2113 * If 'waiting' then the waiting task will complete this
2114 * request, otherwise queue a work to do it. Note that
2115 * complete_work may still race with the dispatch of a following
2116 * request.
2117 */
2118 if (waiting)
2119 wake_up(&mq->wait);
2120 else
2121 queue_work(mq->card->complete_wq, &mq->complete_work);
2122
2123 return;
2124 }
2125
2126 /* Take the recovery path for errors or urgent background operations */
2127 if (mmc_blk_rq_error(&mqrq->brq) ||
2128 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2129 spin_lock_irqsave(&mq->lock, flags);
2130 mq->recovery_needed = true;
2131 mq->recovery_req = req;
2132 spin_unlock_irqrestore(&mq->lock, flags);
2133 wake_up(&mq->wait);
2134 schedule_work(&mq->recovery_work);
2135 return;
2136 }
2137
2138 mmc_blk_rw_reset_success(mq, req);
2139
2140 mq->rw_wait = false;
2141 wake_up(&mq->wait);
2142
2143 mmc_blk_mq_post_req(mq, req);
2144 }
2145
mmc_blk_rw_wait_cond(struct mmc_queue * mq,int * err)2146 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2147 {
2148 unsigned long flags;
2149 bool done;
2150
2151 /*
2152 * Wait while there is another request in progress, but not if recovery
2153 * is needed. Also indicate whether there is a request waiting to start.
2154 */
2155 spin_lock_irqsave(&mq->lock, flags);
2156 if (mq->recovery_needed) {
2157 *err = -EBUSY;
2158 done = true;
2159 } else {
2160 done = !mq->rw_wait;
2161 }
2162 mq->waiting = !done;
2163 spin_unlock_irqrestore(&mq->lock, flags);
2164
2165 return done;
2166 }
2167
mmc_blk_rw_wait(struct mmc_queue * mq,struct request ** prev_req)2168 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2169 {
2170 int err = 0;
2171
2172 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2173
2174 /* Always complete the previous request if there is one */
2175 mmc_blk_mq_complete_prev_req(mq, prev_req);
2176
2177 return err;
2178 }
2179
mmc_blk_mq_issue_rw_rq(struct mmc_queue * mq,struct request * req)2180 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2181 struct request *req)
2182 {
2183 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2184 struct mmc_host *host = mq->card->host;
2185 struct request *prev_req = NULL;
2186 int err = 0;
2187
2188 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2189
2190 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2191
2192 mmc_pre_req(host, &mqrq->brq.mrq);
2193
2194 err = mmc_blk_rw_wait(mq, &prev_req);
2195 if (err)
2196 goto out_post_req;
2197
2198 mq->rw_wait = true;
2199
2200 err = mmc_start_request(host, &mqrq->brq.mrq);
2201
2202 if (prev_req)
2203 mmc_blk_mq_post_req(mq, prev_req);
2204
2205 if (err)
2206 mq->rw_wait = false;
2207
2208 /* Release re-tuning here where there is no synchronization required */
2209 if (err || mmc_host_done_complete(host))
2210 mmc_retune_release(host);
2211
2212 out_post_req:
2213 if (err)
2214 mmc_post_req(host, &mqrq->brq.mrq, err);
2215
2216 return err;
2217 }
2218
mmc_blk_wait_for_idle(struct mmc_queue * mq,struct mmc_host * host)2219 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2220 {
2221 if (mq->use_cqe)
2222 return host->cqe_ops->cqe_wait_for_idle(host);
2223
2224 return mmc_blk_rw_wait(mq, NULL);
2225 }
2226
mmc_blk_mq_issue_rq(struct mmc_queue * mq,struct request * req)2227 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2228 {
2229 struct mmc_blk_data *md = mq->blkdata;
2230 struct mmc_card *card = md->queue.card;
2231 struct mmc_host *host = card->host;
2232 int ret;
2233
2234 ret = mmc_blk_part_switch(card, md->part_type);
2235 if (ret)
2236 return MMC_REQ_FAILED_TO_START;
2237
2238 switch (mmc_issue_type(mq, req)) {
2239 case MMC_ISSUE_SYNC:
2240 ret = mmc_blk_wait_for_idle(mq, host);
2241 if (ret)
2242 return MMC_REQ_BUSY;
2243 switch (req_op(req)) {
2244 case REQ_OP_DRV_IN:
2245 case REQ_OP_DRV_OUT:
2246 mmc_blk_issue_drv_op(mq, req);
2247 break;
2248 case REQ_OP_DISCARD:
2249 mmc_blk_issue_discard_rq(mq, req);
2250 break;
2251 case REQ_OP_SECURE_ERASE:
2252 mmc_blk_issue_secdiscard_rq(mq, req);
2253 break;
2254 case REQ_OP_FLUSH:
2255 mmc_blk_issue_flush(mq, req);
2256 break;
2257 default:
2258 WARN_ON_ONCE(1);
2259 return MMC_REQ_FAILED_TO_START;
2260 }
2261 return MMC_REQ_FINISHED;
2262 case MMC_ISSUE_DCMD:
2263 case MMC_ISSUE_ASYNC:
2264 switch (req_op(req)) {
2265 case REQ_OP_FLUSH:
2266 if (!mmc_cache_enabled(host)) {
2267 blk_mq_end_request(req, BLK_STS_OK);
2268 return MMC_REQ_FINISHED;
2269 }
2270 ret = mmc_blk_cqe_issue_flush(mq, req);
2271 break;
2272 case REQ_OP_READ:
2273 case REQ_OP_WRITE:
2274 if (mq->use_cqe)
2275 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2276 else
2277 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2278 break;
2279 default:
2280 WARN_ON_ONCE(1);
2281 ret = -EINVAL;
2282 }
2283 if (!ret)
2284 return MMC_REQ_STARTED;
2285 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2286 default:
2287 WARN_ON_ONCE(1);
2288 return MMC_REQ_FAILED_TO_START;
2289 }
2290 }
2291
mmc_blk_readonly(struct mmc_card * card)2292 static inline int mmc_blk_readonly(struct mmc_card *card)
2293 {
2294 return mmc_card_readonly(card) ||
2295 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2296 }
2297
mmc_blk_alloc_req(struct mmc_card * card,struct device * parent,sector_t size,bool default_ro,const char * subname,int area_type)2298 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2299 struct device *parent,
2300 sector_t size,
2301 bool default_ro,
2302 const char *subname,
2303 int area_type)
2304 {
2305 struct mmc_blk_data *md;
2306 int devidx, ret;
2307
2308 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2309 if (devidx < 0) {
2310 /*
2311 * We get -ENOSPC because there are no more any available
2312 * devidx. The reason may be that, either userspace haven't yet
2313 * unmounted the partitions, which postpones mmc_blk_release()
2314 * from being called, or the device has more partitions than
2315 * what we support.
2316 */
2317 if (devidx == -ENOSPC)
2318 dev_err(mmc_dev(card->host),
2319 "no more device IDs available\n");
2320
2321 return ERR_PTR(devidx);
2322 }
2323
2324 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2325 if (!md) {
2326 ret = -ENOMEM;
2327 goto out;
2328 }
2329
2330 md->area_type = area_type;
2331
2332 /*
2333 * Set the read-only status based on the supported commands
2334 * and the write protect switch.
2335 */
2336 md->read_only = mmc_blk_readonly(card);
2337
2338 md->disk = alloc_disk(perdev_minors);
2339 if (md->disk == NULL) {
2340 ret = -ENOMEM;
2341 goto err_kfree;
2342 }
2343
2344 INIT_LIST_HEAD(&md->part);
2345 INIT_LIST_HEAD(&md->rpmbs);
2346 md->usage = 1;
2347
2348 ret = mmc_init_queue(&md->queue, card);
2349 if (ret)
2350 goto err_putdisk;
2351
2352 md->queue.blkdata = md;
2353
2354 /*
2355 * Keep an extra reference to the queue so that we can shutdown the
2356 * queue (i.e. call blk_cleanup_queue()) while there are still
2357 * references to the 'md'. The corresponding blk_put_queue() is in
2358 * mmc_blk_put().
2359 */
2360 if (!blk_get_queue(md->queue.queue)) {
2361 mmc_cleanup_queue(&md->queue);
2362 ret = -ENODEV;
2363 goto err_putdisk;
2364 }
2365
2366 md->disk->major = MMC_BLOCK_MAJOR;
2367 md->disk->first_minor = devidx * perdev_minors;
2368 md->disk->fops = &mmc_bdops;
2369 md->disk->private_data = md;
2370 md->disk->queue = md->queue.queue;
2371 md->parent = parent;
2372 set_disk_ro(md->disk, md->read_only || default_ro);
2373 md->disk->flags = GENHD_FL_EXT_DEVT;
2374 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2375 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2376 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2377
2378 /*
2379 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2380 *
2381 * - be set for removable media with permanent block devices
2382 * - be unset for removable block devices with permanent media
2383 *
2384 * Since MMC block devices clearly fall under the second
2385 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2386 * should use the block device creation/destruction hotplug
2387 * messages to tell when the card is present.
2388 */
2389
2390 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2391 "mmcblk%u%s", card->host->index, subname ? subname : "");
2392
2393 set_capacity(md->disk, size);
2394
2395 if (mmc_host_cmd23(card->host)) {
2396 if ((mmc_card_mmc(card) &&
2397 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2398 (mmc_card_sd(card) &&
2399 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2400 md->flags |= MMC_BLK_CMD23;
2401 }
2402
2403 if (mmc_card_mmc(card) &&
2404 md->flags & MMC_BLK_CMD23 &&
2405 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2406 card->ext_csd.rel_sectors)) {
2407 md->flags |= MMC_BLK_REL_WR;
2408 blk_queue_write_cache(md->queue.queue, true, true);
2409 }
2410
2411 return md;
2412
2413 err_putdisk:
2414 put_disk(md->disk);
2415 err_kfree:
2416 kfree(md);
2417 out:
2418 ida_simple_remove(&mmc_blk_ida, devidx);
2419 return ERR_PTR(ret);
2420 }
2421
mmc_blk_alloc(struct mmc_card * card)2422 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2423 {
2424 sector_t size;
2425
2426 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2427 /*
2428 * The EXT_CSD sector count is in number or 512 byte
2429 * sectors.
2430 */
2431 size = card->ext_csd.sectors;
2432 } else {
2433 /*
2434 * The CSD capacity field is in units of read_blkbits.
2435 * set_capacity takes units of 512 bytes.
2436 */
2437 size = (typeof(sector_t))card->csd.capacity
2438 << (card->csd.read_blkbits - 9);
2439 }
2440
2441 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2442 MMC_BLK_DATA_AREA_MAIN);
2443 }
2444
mmc_blk_alloc_part(struct mmc_card * card,struct mmc_blk_data * md,unsigned int part_type,sector_t size,bool default_ro,const char * subname,int area_type)2445 static int mmc_blk_alloc_part(struct mmc_card *card,
2446 struct mmc_blk_data *md,
2447 unsigned int part_type,
2448 sector_t size,
2449 bool default_ro,
2450 const char *subname,
2451 int area_type)
2452 {
2453 char cap_str[10];
2454 struct mmc_blk_data *part_md;
2455
2456 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2457 subname, area_type);
2458 if (IS_ERR(part_md))
2459 return PTR_ERR(part_md);
2460 part_md->part_type = part_type;
2461 list_add(&part_md->part, &md->part);
2462
2463 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2464 cap_str, sizeof(cap_str));
2465 pr_info("%s: %s %s partition %u %s\n",
2466 part_md->disk->disk_name, mmc_card_id(card),
2467 mmc_card_name(card), part_md->part_type, cap_str);
2468 return 0;
2469 }
2470
2471 /**
2472 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2473 * @filp: the character device file
2474 * @cmd: the ioctl() command
2475 * @arg: the argument from userspace
2476 *
2477 * This will essentially just redirect the ioctl()s coming in over to
2478 * the main block device spawning the RPMB character device.
2479 */
mmc_rpmb_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)2480 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2481 unsigned long arg)
2482 {
2483 struct mmc_rpmb_data *rpmb = filp->private_data;
2484 int ret;
2485
2486 switch (cmd) {
2487 case MMC_IOC_CMD:
2488 ret = mmc_blk_ioctl_cmd(rpmb->md,
2489 (struct mmc_ioc_cmd __user *)arg,
2490 rpmb);
2491 break;
2492 case MMC_IOC_MULTI_CMD:
2493 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2494 (struct mmc_ioc_multi_cmd __user *)arg,
2495 rpmb);
2496 break;
2497 default:
2498 ret = -EINVAL;
2499 break;
2500 }
2501
2502 return ret;
2503 }
2504
2505 #ifdef CONFIG_COMPAT
mmc_rpmb_ioctl_compat(struct file * filp,unsigned int cmd,unsigned long arg)2506 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2507 unsigned long arg)
2508 {
2509 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2510 }
2511 #endif
2512
mmc_rpmb_chrdev_open(struct inode * inode,struct file * filp)2513 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2514 {
2515 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2516 struct mmc_rpmb_data, chrdev);
2517
2518 get_device(&rpmb->dev);
2519 filp->private_data = rpmb;
2520 mmc_blk_get(rpmb->md->disk);
2521
2522 return nonseekable_open(inode, filp);
2523 }
2524
mmc_rpmb_chrdev_release(struct inode * inode,struct file * filp)2525 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2526 {
2527 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2528 struct mmc_rpmb_data, chrdev);
2529
2530 mmc_blk_put(rpmb->md);
2531 put_device(&rpmb->dev);
2532
2533 return 0;
2534 }
2535
2536 static const struct file_operations mmc_rpmb_fileops = {
2537 .release = mmc_rpmb_chrdev_release,
2538 .open = mmc_rpmb_chrdev_open,
2539 .owner = THIS_MODULE,
2540 .llseek = no_llseek,
2541 .unlocked_ioctl = mmc_rpmb_ioctl,
2542 #ifdef CONFIG_COMPAT
2543 .compat_ioctl = mmc_rpmb_ioctl_compat,
2544 #endif
2545 };
2546
mmc_blk_rpmb_device_release(struct device * dev)2547 static void mmc_blk_rpmb_device_release(struct device *dev)
2548 {
2549 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2550
2551 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2552 kfree(rpmb);
2553 }
2554
mmc_blk_alloc_rpmb_part(struct mmc_card * card,struct mmc_blk_data * md,unsigned int part_index,sector_t size,const char * subname)2555 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2556 struct mmc_blk_data *md,
2557 unsigned int part_index,
2558 sector_t size,
2559 const char *subname)
2560 {
2561 int devidx, ret;
2562 char rpmb_name[DISK_NAME_LEN];
2563 char cap_str[10];
2564 struct mmc_rpmb_data *rpmb;
2565
2566 /* This creates the minor number for the RPMB char device */
2567 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2568 if (devidx < 0)
2569 return devidx;
2570
2571 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2572 if (!rpmb) {
2573 ida_simple_remove(&mmc_rpmb_ida, devidx);
2574 return -ENOMEM;
2575 }
2576
2577 snprintf(rpmb_name, sizeof(rpmb_name),
2578 "mmcblk%u%s", card->host->index, subname ? subname : "");
2579
2580 rpmb->id = devidx;
2581 rpmb->part_index = part_index;
2582 rpmb->dev.init_name = rpmb_name;
2583 rpmb->dev.bus = &mmc_rpmb_bus_type;
2584 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2585 rpmb->dev.parent = &card->dev;
2586 rpmb->dev.release = mmc_blk_rpmb_device_release;
2587 device_initialize(&rpmb->dev);
2588 dev_set_drvdata(&rpmb->dev, rpmb);
2589 rpmb->md = md;
2590
2591 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2592 rpmb->chrdev.owner = THIS_MODULE;
2593 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2594 if (ret) {
2595 pr_err("%s: could not add character device\n", rpmb_name);
2596 goto out_put_device;
2597 }
2598
2599 list_add(&rpmb->node, &md->rpmbs);
2600
2601 string_get_size((u64)size, 512, STRING_UNITS_2,
2602 cap_str, sizeof(cap_str));
2603
2604 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2605 rpmb_name, mmc_card_id(card),
2606 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2607 MAJOR(mmc_rpmb_devt), rpmb->id);
2608
2609 return 0;
2610
2611 out_put_device:
2612 put_device(&rpmb->dev);
2613 return ret;
2614 }
2615
mmc_blk_remove_rpmb_part(struct mmc_rpmb_data * rpmb)2616 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2617
2618 {
2619 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2620 put_device(&rpmb->dev);
2621 }
2622
2623 /* MMC Physical partitions consist of two boot partitions and
2624 * up to four general purpose partitions.
2625 * For each partition enabled in EXT_CSD a block device will be allocatedi
2626 * to provide access to the partition.
2627 */
2628
mmc_blk_alloc_parts(struct mmc_card * card,struct mmc_blk_data * md)2629 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2630 {
2631 int idx, ret;
2632
2633 if (!mmc_card_mmc(card))
2634 return 0;
2635
2636 for (idx = 0; idx < card->nr_parts; idx++) {
2637 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2638 /*
2639 * RPMB partitions does not provide block access, they
2640 * are only accessed using ioctl():s. Thus create
2641 * special RPMB block devices that do not have a
2642 * backing block queue for these.
2643 */
2644 ret = mmc_blk_alloc_rpmb_part(card, md,
2645 card->part[idx].part_cfg,
2646 card->part[idx].size >> 9,
2647 card->part[idx].name);
2648 if (ret)
2649 return ret;
2650 } else if (card->part[idx].size) {
2651 ret = mmc_blk_alloc_part(card, md,
2652 card->part[idx].part_cfg,
2653 card->part[idx].size >> 9,
2654 card->part[idx].force_ro,
2655 card->part[idx].name,
2656 card->part[idx].area_type);
2657 if (ret)
2658 return ret;
2659 }
2660 }
2661
2662 return 0;
2663 }
2664
mmc_blk_remove_req(struct mmc_blk_data * md)2665 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2666 {
2667 struct mmc_card *card;
2668
2669 if (md) {
2670 /*
2671 * Flush remaining requests and free queues. It
2672 * is freeing the queue that stops new requests
2673 * from being accepted.
2674 */
2675 card = md->queue.card;
2676 if (md->disk->flags & GENHD_FL_UP) {
2677 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2678 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2679 card->ext_csd.boot_ro_lockable)
2680 device_remove_file(disk_to_dev(md->disk),
2681 &md->power_ro_lock);
2682
2683 del_gendisk(md->disk);
2684 }
2685 mmc_cleanup_queue(&md->queue);
2686 mmc_blk_put(md);
2687 }
2688 }
2689
mmc_blk_remove_parts(struct mmc_card * card,struct mmc_blk_data * md)2690 static void mmc_blk_remove_parts(struct mmc_card *card,
2691 struct mmc_blk_data *md)
2692 {
2693 struct list_head *pos, *q;
2694 struct mmc_blk_data *part_md;
2695 struct mmc_rpmb_data *rpmb;
2696
2697 /* Remove RPMB partitions */
2698 list_for_each_safe(pos, q, &md->rpmbs) {
2699 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2700 list_del(pos);
2701 mmc_blk_remove_rpmb_part(rpmb);
2702 }
2703 /* Remove block partitions */
2704 list_for_each_safe(pos, q, &md->part) {
2705 part_md = list_entry(pos, struct mmc_blk_data, part);
2706 list_del(pos);
2707 mmc_blk_remove_req(part_md);
2708 }
2709 }
2710
mmc_add_disk(struct mmc_blk_data * md)2711 static int mmc_add_disk(struct mmc_blk_data *md)
2712 {
2713 int ret;
2714 struct mmc_card *card = md->queue.card;
2715
2716 device_add_disk(md->parent, md->disk, NULL);
2717 md->force_ro.show = force_ro_show;
2718 md->force_ro.store = force_ro_store;
2719 sysfs_attr_init(&md->force_ro.attr);
2720 md->force_ro.attr.name = "force_ro";
2721 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2722 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2723 if (ret)
2724 goto force_ro_fail;
2725
2726 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2727 card->ext_csd.boot_ro_lockable) {
2728 umode_t mode;
2729
2730 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2731 mode = S_IRUGO;
2732 else
2733 mode = S_IRUGO | S_IWUSR;
2734
2735 md->power_ro_lock.show = power_ro_lock_show;
2736 md->power_ro_lock.store = power_ro_lock_store;
2737 sysfs_attr_init(&md->power_ro_lock.attr);
2738 md->power_ro_lock.attr.mode = mode;
2739 md->power_ro_lock.attr.name =
2740 "ro_lock_until_next_power_on";
2741 ret = device_create_file(disk_to_dev(md->disk),
2742 &md->power_ro_lock);
2743 if (ret)
2744 goto power_ro_lock_fail;
2745 }
2746 return ret;
2747
2748 power_ro_lock_fail:
2749 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2750 force_ro_fail:
2751 del_gendisk(md->disk);
2752
2753 return ret;
2754 }
2755
2756 #ifdef CONFIG_DEBUG_FS
2757
mmc_dbg_card_status_get(void * data,u64 * val)2758 static int mmc_dbg_card_status_get(void *data, u64 *val)
2759 {
2760 struct mmc_card *card = data;
2761 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2762 struct mmc_queue *mq = &md->queue;
2763 struct request *req;
2764 int ret;
2765
2766 /* Ask the block layer about the card status */
2767 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2768 if (IS_ERR(req))
2769 return PTR_ERR(req);
2770 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2771 blk_execute_rq(mq->queue, NULL, req, 0);
2772 ret = req_to_mmc_queue_req(req)->drv_op_result;
2773 if (ret >= 0) {
2774 *val = ret;
2775 ret = 0;
2776 }
2777 blk_put_request(req);
2778
2779 return ret;
2780 }
2781 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2782 NULL, "%08llx\n");
2783
2784 /* That is two digits * 512 + 1 for newline */
2785 #define EXT_CSD_STR_LEN 1025
2786
mmc_ext_csd_open(struct inode * inode,struct file * filp)2787 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2788 {
2789 struct mmc_card *card = inode->i_private;
2790 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2791 struct mmc_queue *mq = &md->queue;
2792 struct request *req;
2793 char *buf;
2794 ssize_t n = 0;
2795 u8 *ext_csd;
2796 int err, i;
2797
2798 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2799 if (!buf)
2800 return -ENOMEM;
2801
2802 /* Ask the block layer for the EXT CSD */
2803 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2804 if (IS_ERR(req)) {
2805 err = PTR_ERR(req);
2806 goto out_free;
2807 }
2808 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2809 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2810 blk_execute_rq(mq->queue, NULL, req, 0);
2811 err = req_to_mmc_queue_req(req)->drv_op_result;
2812 blk_put_request(req);
2813 if (err) {
2814 pr_err("FAILED %d\n", err);
2815 goto out_free;
2816 }
2817
2818 for (i = 0; i < 512; i++)
2819 n += sprintf(buf + n, "%02x", ext_csd[i]);
2820 n += sprintf(buf + n, "\n");
2821
2822 if (n != EXT_CSD_STR_LEN) {
2823 err = -EINVAL;
2824 kfree(ext_csd);
2825 goto out_free;
2826 }
2827
2828 filp->private_data = buf;
2829 kfree(ext_csd);
2830 return 0;
2831
2832 out_free:
2833 kfree(buf);
2834 return err;
2835 }
2836
mmc_ext_csd_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)2837 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2838 size_t cnt, loff_t *ppos)
2839 {
2840 char *buf = filp->private_data;
2841
2842 return simple_read_from_buffer(ubuf, cnt, ppos,
2843 buf, EXT_CSD_STR_LEN);
2844 }
2845
mmc_ext_csd_release(struct inode * inode,struct file * file)2846 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2847 {
2848 kfree(file->private_data);
2849 return 0;
2850 }
2851
2852 static const struct file_operations mmc_dbg_ext_csd_fops = {
2853 .open = mmc_ext_csd_open,
2854 .read = mmc_ext_csd_read,
2855 .release = mmc_ext_csd_release,
2856 .llseek = default_llseek,
2857 };
2858
mmc_blk_add_debugfs(struct mmc_card * card,struct mmc_blk_data * md)2859 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2860 {
2861 struct dentry *root;
2862
2863 if (!card->debugfs_root)
2864 return 0;
2865
2866 root = card->debugfs_root;
2867
2868 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2869 md->status_dentry =
2870 debugfs_create_file_unsafe("status", 0400, root,
2871 card,
2872 &mmc_dbg_card_status_fops);
2873 if (!md->status_dentry)
2874 return -EIO;
2875 }
2876
2877 if (mmc_card_mmc(card)) {
2878 md->ext_csd_dentry =
2879 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2880 &mmc_dbg_ext_csd_fops);
2881 if (!md->ext_csd_dentry)
2882 return -EIO;
2883 }
2884
2885 return 0;
2886 }
2887
mmc_blk_remove_debugfs(struct mmc_card * card,struct mmc_blk_data * md)2888 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2889 struct mmc_blk_data *md)
2890 {
2891 if (!card->debugfs_root)
2892 return;
2893
2894 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2895 debugfs_remove(md->status_dentry);
2896 md->status_dentry = NULL;
2897 }
2898
2899 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2900 debugfs_remove(md->ext_csd_dentry);
2901 md->ext_csd_dentry = NULL;
2902 }
2903 }
2904
2905 #else
2906
mmc_blk_add_debugfs(struct mmc_card * card,struct mmc_blk_data * md)2907 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2908 {
2909 return 0;
2910 }
2911
mmc_blk_remove_debugfs(struct mmc_card * card,struct mmc_blk_data * md)2912 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2913 struct mmc_blk_data *md)
2914 {
2915 }
2916
2917 #endif /* CONFIG_DEBUG_FS */
2918
2919 struct mmc_card *this_card;
2920 EXPORT_SYMBOL(this_card);
2921
mmc_blk_probe(struct mmc_card * card)2922 static int mmc_blk_probe(struct mmc_card *card)
2923 {
2924 struct mmc_blk_data *md, *part_md;
2925 char cap_str[10];
2926
2927 /*
2928 * Check that the card supports the command class(es) we need.
2929 */
2930 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2931 return -ENODEV;
2932
2933 mmc_fixup_device(card, mmc_blk_fixups);
2934
2935 card->complete_wq = alloc_workqueue("mmc_complete",
2936 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2937 if (unlikely(!card->complete_wq)) {
2938 pr_err("Failed to create mmc completion workqueue");
2939 return -ENOMEM;
2940 }
2941
2942 md = mmc_blk_alloc(card);
2943 if (IS_ERR(md))
2944 return PTR_ERR(md);
2945
2946 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2947 cap_str, sizeof(cap_str));
2948 pr_info("%s: %s %s %s %s\n",
2949 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2950 cap_str, md->read_only ? "(ro)" : "");
2951
2952 if (mmc_blk_alloc_parts(card, md))
2953 goto out;
2954
2955 dev_set_drvdata(&card->dev, md);
2956
2957 #if defined(CONFIG_MMC_DW_ROCKCHIP) || defined(CONFIG_MMC_SDHCI_OF_ARASAN)
2958 if (card->type == MMC_TYPE_MMC)
2959 this_card = card;
2960 #endif
2961
2962 if (mmc_add_disk(md))
2963 goto out;
2964
2965 list_for_each_entry(part_md, &md->part, part) {
2966 if (mmc_add_disk(part_md))
2967 goto out;
2968 }
2969
2970 /* Add two debugfs entries */
2971 mmc_blk_add_debugfs(card, md);
2972
2973 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2974 pm_runtime_use_autosuspend(&card->dev);
2975
2976 /*
2977 * Don't enable runtime PM for SD-combo cards here. Leave that
2978 * decision to be taken during the SDIO init sequence instead.
2979 */
2980 if (card->type != MMC_TYPE_SD_COMBO) {
2981 pm_runtime_set_active(&card->dev);
2982 pm_runtime_enable(&card->dev);
2983 }
2984
2985 return 0;
2986
2987 out:
2988 mmc_blk_remove_parts(card, md);
2989 mmc_blk_remove_req(md);
2990 return 0;
2991 }
2992
mmc_blk_remove(struct mmc_card * card)2993 static void mmc_blk_remove(struct mmc_card *card)
2994 {
2995 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2996
2997 mmc_blk_remove_debugfs(card, md);
2998
2999 #if defined(CONFIG_MMC_DW_ROCKCHIP)
3000 if (card->type == MMC_TYPE_MMC)
3001 this_card = NULL;
3002 #endif
3003
3004 mmc_blk_remove_parts(card, md);
3005 pm_runtime_get_sync(&card->dev);
3006 if (md->part_curr != md->part_type) {
3007 mmc_claim_host(card->host);
3008 mmc_blk_part_switch(card, md->part_type);
3009 mmc_release_host(card->host);
3010 }
3011 if (card->type != MMC_TYPE_SD_COMBO)
3012 pm_runtime_disable(&card->dev);
3013 pm_runtime_put_noidle(&card->dev);
3014 mmc_blk_remove_req(md);
3015 dev_set_drvdata(&card->dev, NULL);
3016 destroy_workqueue(card->complete_wq);
3017 }
3018
_mmc_blk_suspend(struct mmc_card * card)3019 static int _mmc_blk_suspend(struct mmc_card *card)
3020 {
3021 struct mmc_blk_data *part_md;
3022 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3023
3024 if (md) {
3025 mmc_queue_suspend(&md->queue);
3026 list_for_each_entry(part_md, &md->part, part) {
3027 mmc_queue_suspend(&part_md->queue);
3028 }
3029 }
3030 return 0;
3031 }
3032
mmc_blk_shutdown(struct mmc_card * card)3033 static void mmc_blk_shutdown(struct mmc_card *card)
3034 {
3035 _mmc_blk_suspend(card);
3036 }
3037
3038 #ifdef CONFIG_PM_SLEEP
mmc_blk_suspend(struct device * dev)3039 static int mmc_blk_suspend(struct device *dev)
3040 {
3041 struct mmc_card *card = mmc_dev_to_card(dev);
3042
3043 return _mmc_blk_suspend(card);
3044 }
3045
mmc_blk_resume(struct device * dev)3046 static int mmc_blk_resume(struct device *dev)
3047 {
3048 struct mmc_blk_data *part_md;
3049 struct mmc_blk_data *md = dev_get_drvdata(dev);
3050
3051 if (md) {
3052 /*
3053 * Resume involves the card going into idle state,
3054 * so current partition is always the main one.
3055 */
3056 md->part_curr = md->part_type;
3057 mmc_queue_resume(&md->queue);
3058 list_for_each_entry(part_md, &md->part, part) {
3059 mmc_queue_resume(&part_md->queue);
3060 }
3061 }
3062 return 0;
3063 }
3064 #endif
3065
3066 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3067
3068 static struct mmc_driver mmc_driver = {
3069 .drv = {
3070 .name = "mmcblk",
3071 .pm = &mmc_blk_pm_ops,
3072 },
3073 .probe = mmc_blk_probe,
3074 .remove = mmc_blk_remove,
3075 .shutdown = mmc_blk_shutdown,
3076 };
3077
mmc_blk_init(void)3078 static int __init mmc_blk_init(void)
3079 {
3080 int res;
3081
3082 res = bus_register(&mmc_rpmb_bus_type);
3083 if (res < 0) {
3084 pr_err("mmcblk: could not register RPMB bus type\n");
3085 return res;
3086 }
3087 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3088 if (res < 0) {
3089 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3090 goto out_bus_unreg;
3091 }
3092
3093 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3094 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3095
3096 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3097
3098 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3099 if (res)
3100 goto out_chrdev_unreg;
3101
3102 res = mmc_register_driver(&mmc_driver);
3103 if (res)
3104 goto out_blkdev_unreg;
3105
3106 return 0;
3107
3108 out_blkdev_unreg:
3109 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3110 out_chrdev_unreg:
3111 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3112 out_bus_unreg:
3113 bus_unregister(&mmc_rpmb_bus_type);
3114 return res;
3115 }
3116
mmc_blk_exit(void)3117 static void __exit mmc_blk_exit(void)
3118 {
3119 mmc_unregister_driver(&mmc_driver);
3120 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3121 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3122 bus_unregister(&mmc_rpmb_bus_type);
3123 }
3124
3125 module_init(mmc_blk_init);
3126 module_exit(mmc_blk_exit);
3127
3128 MODULE_LICENSE("GPL");
3129 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3130
3131