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