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