blob: d8486168415ae1123f6a31d7126cfc9192fe884b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040034#include <linux/delay.h>
35#include <linux/capability.h>
36#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
John Calixtocb87ea22011-04-26 18:56:29 -040039#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020041#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010042#include <linux/mmc/mmc.h>
43#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
Pierre Ossman98ac2162006-12-23 20:03:02 +010047#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000049MODULE_ALIAS("mmc:block");
Andy Shevchenko829b6962015-06-24 14:32:21 +030050
51#ifdef KERNEL
Olof Johansson5e71b7a2010-09-17 21:19:57 -040052#ifdef MODULE_PARAM_PREFIX
53#undef MODULE_PARAM_PREFIX
54#endif
55#define MODULE_PARAM_PREFIX "mmcblk."
Andy Shevchenko829b6962015-06-24 14:32:21 +030056#endif
David Woodhouse1dff3142007-11-21 18:45:12 +010057
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050058#define INAND_CMD38_ARG_EXT_CSD 113
59#define INAND_CMD38_ARG_ERASE 0x00
60#define INAND_CMD38_ARG_TRIM 0x01
61#define INAND_CMD38_ARG_SECERASE 0x80
62#define INAND_CMD38_ARG_SECTRIM1 0x81
63#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060064#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
70#define PACKED_CMD_VER 0x01
71#define PACKED_CMD_WR 0x02
72
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020073static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040074
75/*
76 * The defaults come from config options but can be overriden by module
77 * or bootarg options.
78 */
79static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80
81/*
82 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000083 * limited to (1 << 20) / number of minors per device. It is also
84 * currently limited by the size of the static bitmaps below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040085 */
86static int max_devices;
87
Ben Hutchingsa26eba62014-11-06 03:35:09 +000088#define MAX_DEVICES 256
89
90/* TODO: Replace these with struct ida */
91static DECLARE_BITMAP(dev_use, MAX_DEVICES);
92static DECLARE_BITMAP(name_use, MAX_DEVICES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * There is one mmc_blk_data per slot.
96 */
97struct mmc_blk_data {
98 spinlock_t lock;
99 struct gendisk *disk;
100 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500101 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500103 unsigned int flags;
104#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
105#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900106#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000109 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500110 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500111 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300112 unsigned int reset_done;
113#define MMC_BLK_READ BIT(0)
114#define MMC_BLK_WRITE BIT(1)
115#define MMC_BLK_DISCARD BIT(2)
116#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500117
118 /*
119 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200120 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500121 * track of the current selected device partition.
122 */
123 unsigned int part_curr;
124 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100125 struct device_attribute power_ro_lock;
126 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Arjan van de Vena621aae2006-01-12 18:43:35 +0000129static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900131enum {
132 MMC_PACKED_NR_IDX = -1,
133 MMC_PACKED_NR_ZERO,
134 MMC_PACKED_NR_SINGLE,
135};
136
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400137module_param(perdev_minors, int, 0444);
138MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
139
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200140static inline int mmc_blk_part_switch(struct mmc_card *card,
141 struct mmc_blk_data *md);
142static int get_card_status(struct mmc_card *card, u32 *status, int retries);
143
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900144static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
145{
146 struct mmc_packed *packed = mqrq->packed;
147
148 BUG_ON(!packed);
149
150 mqrq->cmd_type = MMC_PACKED_NONE;
151 packed->nr_entries = MMC_PACKED_NR_ZERO;
152 packed->idx_failure = MMC_PACKED_NR_IDX;
153 packed->retries = 0;
154 packed->blocks = 0;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
158{
159 struct mmc_blk_data *md;
160
Arjan van de Vena621aae2006-01-12 18:43:35 +0000161 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 md = disk->private_data;
163 if (md && md->usage == 0)
164 md = NULL;
165 if (md)
166 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000167 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return md;
170}
171
Andrei Warkentin371a6892011-04-11 18:10:25 -0500172static inline int mmc_get_devidx(struct gendisk *disk)
173{
174 int devmaj = MAJOR(disk_devt(disk));
175 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
176
177 if (!devmaj)
178 devidx = disk->first_minor / perdev_minors;
179 return devidx;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void mmc_blk_put(struct mmc_blk_data *md)
183{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000184 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 md->usage--;
186 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500187 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800188 blk_cleanup_queue(md->queue.queue);
189
David Woodhouse1dff3142007-11-21 18:45:12 +0100190 __clear_bit(devidx, dev_use);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 kfree(md);
194 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000195 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Johan Rudholmadd710e2011-12-02 08:51:06 +0100198static ssize_t power_ro_lock_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
200{
201 int ret;
202 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
203 struct mmc_card *card = md->queue.card;
204 int locked = 0;
205
206 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
207 locked = 2;
208 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
209 locked = 1;
210
211 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
212
Tomas Winkler9098f842015-07-16 15:50:45 +0200213 mmc_blk_put(md);
214
Johan Rudholmadd710e2011-12-02 08:51:06 +0100215 return ret;
216}
217
218static ssize_t power_ro_lock_store(struct device *dev,
219 struct device_attribute *attr, const char *buf, size_t count)
220{
221 int ret;
222 struct mmc_blk_data *md, *part_md;
223 struct mmc_card *card;
224 unsigned long set;
225
226 if (kstrtoul(buf, 0, &set))
227 return -EINVAL;
228
229 if (set != 1)
230 return count;
231
232 md = mmc_blk_get(dev_to_disk(dev));
233 card = md->queue.card;
234
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200235 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100236
237 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
238 card->ext_csd.boot_ro_lock |
239 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
240 card->ext_csd.part_time);
241 if (ret)
242 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
243 else
244 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
245
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200246 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100247
248 if (!ret) {
249 pr_info("%s: Locking boot partition ro until next power on\n",
250 md->disk->disk_name);
251 set_disk_ro(md->disk, 1);
252
253 list_for_each_entry(part_md, &md->part, part)
254 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
255 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
256 set_disk_ro(part_md->disk, 1);
257 }
258 }
259
260 mmc_blk_put(md);
261 return count;
262}
263
Andrei Warkentin371a6892011-04-11 18:10:25 -0500264static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
265 char *buf)
266{
267 int ret;
268 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
269
Baruch Siach0031a982014-09-22 10:12:51 +0300270 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500271 get_disk_ro(dev_to_disk(dev)) ^
272 md->read_only);
273 mmc_blk_put(md);
274 return ret;
275}
276
277static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
279{
280 int ret;
281 char *end;
282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
283 unsigned long set = simple_strtoul(buf, &end, 0);
284 if (end == buf) {
285 ret = -EINVAL;
286 goto out;
287 }
288
289 set_disk_ro(dev_to_disk(dev), set || md->read_only);
290 ret = count;
291out:
292 mmc_blk_put(md);
293 return ret;
294}
295
Al Viroa5a15612008-03-02 10:33:30 -0500296static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Al Viroa5a15612008-03-02 10:33:30 -0500298 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int ret = -ENXIO;
300
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200301 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (md) {
303 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500304 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700306
Al Viroa5a15612008-03-02 10:33:30 -0500307 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700308 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700309 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200312 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 return ret;
315}
316
Al Virodb2a1442013-05-05 21:52:57 -0400317static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Al Viroa5a15612008-03-02 10:33:30 -0500319 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200321 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200323 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800327mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800329 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
330 geo->heads = 4;
331 geo->sectors = 16;
332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
John Calixtocb87ea22011-04-26 18:56:29 -0400335struct mmc_blk_ioc_data {
336 struct mmc_ioc_cmd ic;
337 unsigned char *buf;
338 u64 buf_bytes;
339};
340
341static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
342 struct mmc_ioc_cmd __user *user)
343{
344 struct mmc_blk_ioc_data *idata;
345 int err;
346
347 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
348 if (!idata) {
349 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400350 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400351 }
352
353 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
354 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400355 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400356 }
357
358 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
359 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
360 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400361 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400362 }
363
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100364 if (!idata->buf_bytes)
365 return idata;
366
John Calixtocb87ea22011-04-26 18:56:29 -0400367 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
368 if (!idata->buf) {
369 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400370 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400371 }
372
373 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
374 idata->ic.data_ptr, idata->buf_bytes)) {
375 err = -EFAULT;
376 goto copy_err;
377 }
378
379 return idata;
380
381copy_err:
382 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400383idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400384 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400385out:
John Calixtocb87ea22011-04-26 18:56:29 -0400386 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400387}
388
Jon Huntera5f57742015-09-22 10:27:53 +0100389static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
390 struct mmc_blk_ioc_data *idata)
391{
392 struct mmc_ioc_cmd *ic = &idata->ic;
393
394 if (copy_to_user(&(ic_ptr->response), ic->response,
395 sizeof(ic->response)))
396 return -EFAULT;
397
398 if (!idata->ic.write_flag) {
399 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
400 idata->buf, idata->buf_bytes))
401 return -EFAULT;
402 }
403
404 return 0;
405}
406
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200407static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
408 u32 retries_max)
409{
410 int err;
411 u32 retry_count = 0;
412
413 if (!status || !retries_max)
414 return -EINVAL;
415
416 do {
417 err = get_card_status(card, status, 5);
418 if (err)
419 break;
420
421 if (!R1_STATUS(*status) &&
422 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
423 break; /* RPMB programming operation complete */
424
425 /*
426 * Rechedule to give the MMC device a chance to continue
427 * processing the previous command without being polled too
428 * frequently.
429 */
430 usleep_range(1000, 5000);
431 } while (++retry_count < retries_max);
432
433 if (retry_count == retries_max)
434 err = -EPERM;
435
436 return err;
437}
438
Maya Erez775a9362013-04-18 15:41:55 +0300439static int ioctl_do_sanitize(struct mmc_card *card)
440{
441 int err;
442
Ulf Hanssona2d10862013-12-16 14:37:26 +0100443 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300444 pr_warn("%s: %s - SANITIZE is not supported\n",
445 mmc_hostname(card->host), __func__);
446 err = -EOPNOTSUPP;
447 goto out;
448 }
449
450 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
451 mmc_hostname(card->host), __func__);
452
453 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
454 EXT_CSD_SANITIZE_START, 1,
455 MMC_SANITIZE_REQ_TIMEOUT);
456
457 if (err)
458 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
459 mmc_hostname(card->host), __func__, err);
460
461 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
462 __func__);
463out:
464 return err;
465}
466
Jon Huntera5f57742015-09-22 10:27:53 +0100467static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
468 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400469{
John Calixtocb87ea22011-04-26 18:56:29 -0400470 struct mmc_command cmd = {0};
471 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530472 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400473 struct scatterlist sg;
474 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200475 int is_rpmb = false;
476 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400477
Jon Huntera5f57742015-09-22 10:27:53 +0100478 if (!card || !md || !idata)
479 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400480
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200481 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
482 is_rpmb = true;
483
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100484 cmd.opcode = idata->ic.opcode;
485 cmd.arg = idata->ic.arg;
486 cmd.flags = idata->ic.flags;
487
488 if (idata->buf_bytes) {
489 data.sg = &sg;
490 data.sg_len = 1;
491 data.blksz = idata->ic.blksz;
492 data.blocks = idata->ic.blocks;
493
494 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
495
496 if (idata->ic.write_flag)
497 data.flags = MMC_DATA_WRITE;
498 else
499 data.flags = MMC_DATA_READ;
500
501 /* data.flags must already be set before doing this. */
502 mmc_set_data_timeout(&data, card);
503
504 /* Allow overriding the timeout_ns for empirical tuning. */
505 if (idata->ic.data_timeout_ns)
506 data.timeout_ns = idata->ic.data_timeout_ns;
507
508 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
509 /*
510 * Pretend this is a data transfer and rely on the
511 * host driver to compute timeout. When all host
512 * drivers support cmd.cmd_timeout for R1B, this
513 * can be changed to:
514 *
515 * mrq.data = NULL;
516 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
517 */
518 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
519 }
520
521 mrq.data = &data;
522 }
523
524 mrq.cmd = &cmd;
525
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200526 err = mmc_blk_part_switch(card, md);
527 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100528 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200529
John Calixtocb87ea22011-04-26 18:56:29 -0400530 if (idata->ic.is_acmd) {
531 err = mmc_app_cmd(card->host, card);
532 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100533 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400534 }
535
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200536 if (is_rpmb) {
537 err = mmc_set_blockcount(card, data.blocks,
538 idata->ic.write_flag & (1 << 31));
539 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100540 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200541 }
542
Yaniv Gardia82e4842013-06-05 14:13:08 +0300543 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
544 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300545 err = ioctl_do_sanitize(card);
546
547 if (err)
548 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
549 __func__, err);
550
Jon Huntera5f57742015-09-22 10:27:53 +0100551 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300552 }
553
John Calixtocb87ea22011-04-26 18:56:29 -0400554 mmc_wait_for_req(card->host, &mrq);
555
556 if (cmd.error) {
557 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
558 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100559 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400560 }
561 if (data.error) {
562 dev_err(mmc_dev(card->host), "%s: data error %d\n",
563 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100564 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400565 }
566
567 /*
568 * According to the SD specs, some commands require a delay after
569 * issuing the command.
570 */
571 if (idata->ic.postsleep_min_us)
572 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
573
Jon Huntera5f57742015-09-22 10:27:53 +0100574 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400575
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200576 if (is_rpmb) {
577 /*
578 * Ensure RPMB command has completed by polling CMD13
579 * "Send Status".
580 */
581 err = ioctl_rpmb_card_status_poll(card, &status, 5);
582 if (err)
583 dev_err(mmc_dev(card->host),
584 "%s: Card Status=0x%08X, error %d\n",
585 __func__, status, err);
586 }
587
Jon Huntera5f57742015-09-22 10:27:53 +0100588 return err;
589}
590
591static int mmc_blk_ioctl_cmd(struct block_device *bdev,
592 struct mmc_ioc_cmd __user *ic_ptr)
593{
594 struct mmc_blk_ioc_data *idata;
595 struct mmc_blk_data *md;
596 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -0700597 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100598
599 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
600 if (IS_ERR(idata))
601 return PTR_ERR(idata);
602
603 md = mmc_blk_get(bdev->bd_disk);
604 if (!md) {
605 err = -EINVAL;
606 goto cmd_err;
607 }
608
609 card = md->queue.card;
610 if (IS_ERR(card)) {
611 err = PTR_ERR(card);
612 goto cmd_done;
613 }
614
615 mmc_get_card(card);
616
Grant Grundlerb0934102015-09-23 18:30:33 -0700617 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100618
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200619 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -0400620
Grant Grundlerb0934102015-09-23 18:30:33 -0700621 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +0100622
John Calixtocb87ea22011-04-26 18:56:29 -0400623cmd_done:
624 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300625cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400626 kfree(idata->buf);
627 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700628 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -0400629}
630
Jon Huntera5f57742015-09-22 10:27:53 +0100631static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
632 struct mmc_ioc_multi_cmd __user *user)
633{
634 struct mmc_blk_ioc_data **idata = NULL;
635 struct mmc_ioc_cmd __user *cmds = user->cmds;
636 struct mmc_card *card;
637 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -0700638 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +0100639 __u64 num_of_cmds;
640
641 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
642 sizeof(num_of_cmds)))
643 return -EFAULT;
644
645 if (num_of_cmds > MMC_IOC_MAX_CMDS)
646 return -EINVAL;
647
648 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
649 if (!idata)
650 return -ENOMEM;
651
652 for (i = 0; i < num_of_cmds; i++) {
653 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
654 if (IS_ERR(idata[i])) {
655 err = PTR_ERR(idata[i]);
656 num_of_cmds = i;
657 goto cmd_err;
658 }
659 }
660
661 md = mmc_blk_get(bdev->bd_disk);
662 if (!md)
663 goto cmd_err;
664
665 card = md->queue.card;
666 if (IS_ERR(card)) {
667 err = PTR_ERR(card);
668 goto cmd_done;
669 }
670
671 mmc_get_card(card);
672
Grant Grundlerb0934102015-09-23 18:30:33 -0700673 for (i = 0; i < num_of_cmds && !ioc_err; i++)
674 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100675
676 mmc_put_card(card);
677
678 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -0700679 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +0100680 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +0100681
682cmd_done:
683 mmc_blk_put(md);
684cmd_err:
685 for (i = 0; i < num_of_cmds; i++) {
686 kfree(idata[i]->buf);
687 kfree(idata[i]);
688 }
689 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -0700690 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +0100691}
692
John Calixtocb87ea22011-04-26 18:56:29 -0400693static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
694 unsigned int cmd, unsigned long arg)
695{
Jon Huntera5f57742015-09-22 10:27:53 +0100696 /*
697 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
698 * whole block device, not on a partition. This prevents overspray
699 * between sibling partitions.
700 */
701 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
702 return -EPERM;
703
704 switch (cmd) {
705 case MMC_IOC_CMD:
706 return mmc_blk_ioctl_cmd(bdev,
707 (struct mmc_ioc_cmd __user *)arg);
708 case MMC_IOC_MULTI_CMD:
709 return mmc_blk_ioctl_multi_cmd(bdev,
710 (struct mmc_ioc_multi_cmd __user *)arg);
711 default:
712 return -EINVAL;
713 }
John Calixtocb87ea22011-04-26 18:56:29 -0400714}
715
716#ifdef CONFIG_COMPAT
717static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
718 unsigned int cmd, unsigned long arg)
719{
720 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
721}
722#endif
723
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700724static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500725 .open = mmc_blk_open,
726 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800727 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400729 .ioctl = mmc_blk_ioctl,
730#ifdef CONFIG_COMPAT
731 .compat_ioctl = mmc_blk_compat_ioctl,
732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
Andrei Warkentin371a6892011-04-11 18:10:25 -0500735static inline int mmc_blk_part_switch(struct mmc_card *card,
736 struct mmc_blk_data *md)
737{
738 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200739 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300740
Andrei Warkentin371a6892011-04-11 18:10:25 -0500741 if (main_md->part_curr == md->part_type)
742 return 0;
743
744 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300745 u8 part_config = card->ext_csd.part_config;
746
747 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
748 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500749
750 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300751 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500752 card->ext_csd.part_time);
753 if (ret)
754 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300755
756 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300757 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500758
759 main_md->part_curr = md->part_type;
760 return 0;
761}
762
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700763static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
764{
765 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100766 u32 result;
767 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700768
Venkatraman Sad5fd972011-08-25 00:30:50 +0530769 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400770 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400771 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700772
773 struct scatterlist sg;
774
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700775 cmd.opcode = MMC_APP_CMD;
776 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700777 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700778
779 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700780 if (err)
781 return (u32)-1;
782 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700783 return (u32)-1;
784
785 memset(&cmd, 0, sizeof(struct mmc_command));
786
787 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
788 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700789 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700790
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700791 data.blksz = 4;
792 data.blocks = 1;
793 data.flags = MMC_DATA_READ;
794 data.sg = &sg;
795 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530796 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700797
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700798 mrq.cmd = &cmd;
799 mrq.data = &data;
800
Ben Dooks051913d2009-06-08 23:33:57 +0100801 blocks = kmalloc(4, GFP_KERNEL);
802 if (!blocks)
803 return (u32)-1;
804
805 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700806
807 mmc_wait_for_req(card->host, &mrq);
808
Ben Dooks051913d2009-06-08 23:33:57 +0100809 result = ntohl(*blocks);
810 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700811
Ben Dooks051913d2009-06-08 23:33:57 +0100812 if (cmd.error || data.error)
813 result = (u32)-1;
814
815 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700816}
817
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100818static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300819{
Chris Ball1278dba2011-04-13 23:40:30 -0400820 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300821 int err;
822
Adrian Hunter504f1912008-10-16 12:55:25 +0300823 cmd.opcode = MMC_SEND_STATUS;
824 if (!mmc_host_is_spi(card->host))
825 cmd.arg = card->rca << 16;
826 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100827 err = mmc_wait_for_cmd(card->host, &cmd, retries);
828 if (err == 0)
829 *status = cmd.resp[0];
830 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300831}
832
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100833static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +0100834 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100835{
836 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
837 int err = 0;
838 u32 status;
839
840 do {
841 err = get_card_status(card, &status, 5);
842 if (err) {
843 pr_err("%s: error %d requesting status\n",
844 req->rq_disk->disk_name, err);
845 return err;
846 }
847
848 if (status & R1_ERROR) {
849 pr_err("%s: %s: error sending status cmd, status %#x\n",
850 req->rq_disk->disk_name, __func__, status);
851 *gen_err = 1;
852 }
853
Ulf Hansson95a91292014-01-29 13:11:27 +0100854 /* We may rely on the host hw to handle busy detection.*/
855 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
856 hw_busy_detect)
857 break;
858
Ulf Hanssonc49433f2014-01-29 11:01:55 +0100859 /*
860 * Timeout if the device never becomes ready for data and never
861 * leaves the program state.
862 */
863 if (time_after(jiffies, timeout)) {
864 pr_err("%s: Card stuck in programming state! %s %s\n",
865 mmc_hostname(card->host),
866 req->rq_disk->disk_name, __func__);
867 return -ETIMEDOUT;
868 }
869
870 /*
871 * Some cards mishandle the status bits,
872 * so make sure to check both the busy
873 * indication and the card state.
874 */
875 } while (!(status & R1_READY_FOR_DATA) ||
876 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
877
878 return err;
879}
880
Ulf Hanssonbb5cba42014-01-14 21:31:35 +0100881static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
882 struct request *req, int *gen_err, u32 *stop_status)
883{
884 struct mmc_host *host = card->host;
885 struct mmc_command cmd = {0};
886 int err;
887 bool use_r1b_resp = rq_data_dir(req) == WRITE;
888
889 /*
890 * Normally we use R1B responses for WRITE, but in cases where the host
891 * has specified a max_busy_timeout we need to validate it. A failure
892 * means we need to prevent the host from doing hw busy detection, which
893 * is done by converting to a R1 response instead.
894 */
895 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
896 use_r1b_resp = false;
897
898 cmd.opcode = MMC_STOP_TRANSMISSION;
899 if (use_r1b_resp) {
900 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
901 cmd.busy_timeout = timeout_ms;
902 } else {
903 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
904 }
905
906 err = mmc_wait_for_cmd(host, &cmd, 5);
907 if (err)
908 return err;
909
910 *stop_status = cmd.resp[0];
911
912 /* No need to check card status in case of READ. */
913 if (rq_data_dir(req) == READ)
914 return 0;
915
916 if (!mmc_host_is_spi(host) &&
917 (*stop_status & R1_ERROR)) {
918 pr_err("%s: %s: general error sending stop command, resp %#x\n",
919 req->rq_disk->disk_name, __func__, *stop_status);
920 *gen_err = 1;
921 }
922
923 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
924}
925
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530926#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100927#define ERR_RETRY 2
928#define ERR_ABORT 1
929#define ERR_CONTINUE 0
930
931static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
932 bool status_valid, u32 status)
933{
934 switch (error) {
935 case -EILSEQ:
936 /* response crc error, retry the r/w cmd */
937 pr_err("%s: %s sending %s command, card status %#x\n",
938 req->rq_disk->disk_name, "response CRC error",
939 name, status);
940 return ERR_RETRY;
941
942 case -ETIMEDOUT:
943 pr_err("%s: %s sending %s command, card status %#x\n",
944 req->rq_disk->disk_name, "timed out", name, status);
945
946 /* If the status cmd initially failed, retry the r/w cmd */
947 if (!status_valid)
948 return ERR_RETRY;
949
950 /*
951 * If it was a r/w cmd crc error, or illegal command
952 * (eg, issued in wrong state) then retry - we should
953 * have corrected the state problem above.
954 */
955 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
956 return ERR_RETRY;
957
958 /* Otherwise abort the command */
959 return ERR_ABORT;
960
961 default:
962 /* We don't understand the error code the driver gave us */
963 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
964 req->rq_disk->disk_name, error, status);
965 return ERR_ABORT;
966 }
967}
968
969/*
970 * Initial r/w and stop cmd error recovery.
971 * We don't know whether the card received the r/w cmd or not, so try to
972 * restore things back to a sane state. Essentially, we do this as follows:
973 * - Obtain card status. If the first attempt to obtain card status fails,
974 * the status word will reflect the failed status cmd, not the failed
975 * r/w cmd. If we fail to obtain card status, it suggests we can no
976 * longer communicate with the card.
977 * - Check the card state. If the card received the cmd but there was a
978 * transient problem with the response, it might still be in a data transfer
979 * mode. Try to send it a stop command. If this fails, we can't recover.
980 * - If the r/w cmd failed due to a response CRC error, it was probably
981 * transient, so retry the cmd.
982 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
983 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
984 * illegal cmd, retry.
985 * Otherwise we don't understand what happened, so abort.
986 */
987static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +0900988 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100989{
990 bool prev_cmd_status_valid = true;
991 u32 status, stop_status = 0;
992 int err, retry;
993
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530994 if (mmc_card_removed(card))
995 return ERR_NOMEDIUM;
996
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100997 /*
998 * Try to get card status which indicates both the card state
999 * and why there was no response. If the first attempt fails,
1000 * we can't be sure the returned status is for the r/w command.
1001 */
1002 for (retry = 2; retry >= 0; retry--) {
1003 err = get_card_status(card, &status, 0);
1004 if (!err)
1005 break;
1006
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001007 /* Re-tune if needed */
1008 mmc_retune_recheck(card->host);
1009
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001010 prev_cmd_status_valid = false;
1011 pr_err("%s: error %d sending status command, %sing\n",
1012 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1013 }
1014
1015 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301016 if (err) {
1017 /* Check if the card is removed */
1018 if (mmc_detect_card_removed(card->host))
1019 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001020 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301021 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001022
Adrian Hunter67716322011-08-29 16:42:15 +03001023 /* Flag ECC errors */
1024 if ((status & R1_CARD_ECC_FAILED) ||
1025 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1026 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1027 *ecc_err = 1;
1028
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001029 /* Flag General errors */
1030 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1031 if ((status & R1_ERROR) ||
1032 (brq->stop.resp[0] & R1_ERROR)) {
1033 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1034 req->rq_disk->disk_name, __func__,
1035 brq->stop.resp[0], status);
1036 *gen_err = 1;
1037 }
1038
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001039 /*
1040 * Check the current card state. If it is in some data transfer
1041 * mode, tell it to stop (and hopefully transition back to TRAN.)
1042 */
1043 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1044 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001045 err = send_stop(card,
1046 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1047 req, gen_err, &stop_status);
1048 if (err) {
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001049 pr_err("%s: error %d sending stop command\n",
1050 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001051 /*
1052 * If the stop cmd also timed out, the card is probably
1053 * not present, so abort. Other errors are bad news too.
1054 */
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001055 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001056 }
1057
Adrian Hunter67716322011-08-29 16:42:15 +03001058 if (stop_status & R1_CARD_ECC_FAILED)
1059 *ecc_err = 1;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001060 }
1061
1062 /* Check for set block count errors */
1063 if (brq->sbc.error)
1064 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1065 prev_cmd_status_valid, status);
1066
1067 /* Check for r/w command errors */
1068 if (brq->cmd.error)
1069 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1070 prev_cmd_status_valid, status);
1071
Adrian Hunter67716322011-08-29 16:42:15 +03001072 /* Data errors */
1073 if (!brq->stop.error)
1074 return ERR_CONTINUE;
1075
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001076 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001077 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001078 req->rq_disk->disk_name, brq->stop.error,
1079 brq->cmd.resp[0], status);
1080
1081 /*
1082 * Subsitute in our own stop status as this will give the error
1083 * state which happened during the execution of the r/w command.
1084 */
1085 if (stop_status) {
1086 brq->stop.resp[0] = stop_status;
1087 brq->stop.error = 0;
1088 }
1089 return ERR_CONTINUE;
1090}
1091
Adrian Hunter67716322011-08-29 16:42:15 +03001092static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1093 int type)
1094{
1095 int err;
1096
1097 if (md->reset_done & type)
1098 return -EEXIST;
1099
1100 md->reset_done |= type;
1101 err = mmc_hw_reset(host);
1102 /* Ensure we switch back to the correct partition */
1103 if (err != -EOPNOTSUPP) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001104 struct mmc_blk_data *main_md =
1105 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001106 int part_err;
1107
1108 main_md->part_curr = main_md->part_type;
1109 part_err = mmc_blk_part_switch(host->card, md);
1110 if (part_err) {
1111 /*
1112 * We have failed to get back into the correct
1113 * partition, so we need to abort the whole request.
1114 */
1115 return -ENODEV;
1116 }
1117 }
1118 return err;
1119}
1120
1121static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1122{
1123 md->reset_done &= ~type;
1124}
1125
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001126int mmc_access_rpmb(struct mmc_queue *mq)
1127{
1128 struct mmc_blk_data *md = mq->data;
1129 /*
1130 * If this is a RPMB partition access, return ture
1131 */
1132 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1133 return true;
1134
1135 return false;
1136}
1137
Adrian Hunterbd788c92010-08-11 14:17:47 -07001138static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1139{
1140 struct mmc_blk_data *md = mq->data;
1141 struct mmc_card *card = md->queue.card;
1142 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001143 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001144
Adrian Hunterbd788c92010-08-11 14:17:47 -07001145 if (!mmc_can_erase(card)) {
1146 err = -EOPNOTSUPP;
1147 goto out;
1148 }
1149
1150 from = blk_rq_pos(req);
1151 nr = blk_rq_sectors(req);
1152
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001153 if (mmc_can_discard(card))
1154 arg = MMC_DISCARD_ARG;
1155 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001156 arg = MMC_TRIM_ARG;
1157 else
1158 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001159retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001160 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1161 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1162 INAND_CMD38_ARG_EXT_CSD,
1163 arg == MMC_TRIM_ARG ?
1164 INAND_CMD38_ARG_TRIM :
1165 INAND_CMD38_ARG_ERASE,
1166 0);
1167 if (err)
1168 goto out;
1169 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001170 err = mmc_erase(card, from, nr, arg);
1171out:
Adrian Hunter67716322011-08-29 16:42:15 +03001172 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1173 goto retry;
1174 if (!err)
1175 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301176 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001177
Adrian Hunterbd788c92010-08-11 14:17:47 -07001178 return err ? 0 : 1;
1179}
1180
Adrian Hunter49804542010-08-11 14:17:50 -07001181static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1182 struct request *req)
1183{
1184 struct mmc_blk_data *md = mq->data;
1185 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001186 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001187 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001188
Maya Erez775a9362013-04-18 15:41:55 +03001189 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001190 err = -EOPNOTSUPP;
1191 goto out;
1192 }
1193
1194 from = blk_rq_pos(req);
1195 nr = blk_rq_sectors(req);
1196
Maya Erez775a9362013-04-18 15:41:55 +03001197 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1198 arg = MMC_SECURE_TRIM1_ARG;
1199 else
1200 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001201
Adrian Hunter67716322011-08-29 16:42:15 +03001202retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001203 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1204 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1205 INAND_CMD38_ARG_EXT_CSD,
1206 arg == MMC_SECURE_TRIM1_ARG ?
1207 INAND_CMD38_ARG_SECTRIM1 :
1208 INAND_CMD38_ARG_SECERASE,
1209 0);
1210 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001211 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001212 }
Adrian Hunter28302812012-04-05 14:45:48 +03001213
Adrian Hunter49804542010-08-11 14:17:50 -07001214 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001215 if (err == -EIO)
1216 goto out_retry;
1217 if (err)
1218 goto out;
1219
1220 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001221 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1222 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1223 INAND_CMD38_ARG_EXT_CSD,
1224 INAND_CMD38_ARG_SECTRIM2,
1225 0);
1226 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001227 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001228 }
Adrian Hunter28302812012-04-05 14:45:48 +03001229
Adrian Hunter49804542010-08-11 14:17:50 -07001230 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001231 if (err == -EIO)
1232 goto out_retry;
1233 if (err)
1234 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001235 }
Adrian Hunter28302812012-04-05 14:45:48 +03001236
Adrian Hunter28302812012-04-05 14:45:48 +03001237out_retry:
1238 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001239 goto retry;
1240 if (!err)
1241 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001242out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301243 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001244
Adrian Hunter49804542010-08-11 14:17:50 -07001245 return err ? 0 : 1;
1246}
1247
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001248static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1249{
1250 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001251 struct mmc_card *card = md->queue.card;
1252 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001253
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001254 ret = mmc_flush_cache(card);
1255 if (ret)
1256 ret = -EIO;
1257
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301258 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001259
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001260 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001261}
1262
1263/*
1264 * Reformat current write as a reliable write, supporting
1265 * both legacy and the enhanced reliable write MMC cards.
1266 * In each transfer we'll handle only as much as a single
1267 * reliable write can handle, thus finish the request in
1268 * partial completions.
1269 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001270static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1271 struct mmc_card *card,
1272 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001273{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001274 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1275 /* Legacy mode imposes restrictions on transfers. */
1276 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1277 brq->data.blocks = 1;
1278
1279 if (brq->data.blocks > card->ext_csd.rel_sectors)
1280 brq->data.blocks = card->ext_csd.rel_sectors;
1281 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1282 brq->data.blocks = 1;
1283 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001284}
1285
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001286#define CMD_ERRORS \
1287 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1288 R1_ADDRESS_ERROR | /* Misaligned address */ \
1289 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1290 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1291 R1_CC_ERROR | /* Card controller error */ \
1292 R1_ERROR) /* General/unknown error */
1293
Per Forlinee8a43a2011-07-01 18:55:33 +02001294static int mmc_blk_err_check(struct mmc_card *card,
1295 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001296{
Per Forlinee8a43a2011-07-01 18:55:33 +02001297 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1298 mmc_active);
1299 struct mmc_blk_request *brq = &mq_mrq->brq;
1300 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001301 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001302 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001303
1304 /*
1305 * sbc.error indicates a problem with the set block count
1306 * command. No data will have been transferred.
1307 *
1308 * cmd.error indicates a problem with the r/w command. No
1309 * data will have been transferred.
1310 *
1311 * stop.error indicates a problem with the stop command. Data
1312 * may have been transferred, or may still be transferring.
1313 */
Adrian Hunter67716322011-08-29 16:42:15 +03001314 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1315 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001316 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001317 case ERR_RETRY:
1318 return MMC_BLK_RETRY;
1319 case ERR_ABORT:
1320 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301321 case ERR_NOMEDIUM:
1322 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001323 case ERR_CONTINUE:
1324 break;
1325 }
1326 }
1327
1328 /*
1329 * Check for errors relating to the execution of the
1330 * initial command - such as address errors. No data
1331 * has been transferred.
1332 */
1333 if (brq->cmd.resp[0] & CMD_ERRORS) {
1334 pr_err("%s: r/w command failed, status = %#x\n",
1335 req->rq_disk->disk_name, brq->cmd.resp[0]);
1336 return MMC_BLK_ABORT;
1337 }
1338
1339 /*
1340 * Everything else is either success, or a data error of some
1341 * kind. If it was a write, we may have transitioned to
1342 * program mode, which we have to wait for it to complete.
1343 */
1344 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001345 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001346
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001347 /* Check stop command response */
1348 if (brq->stop.resp[0] & R1_ERROR) {
1349 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1350 req->rq_disk->disk_name, __func__,
1351 brq->stop.resp[0]);
1352 gen_err = 1;
1353 }
1354
Ulf Hansson95a91292014-01-29 13:11:27 +01001355 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1356 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001357 if (err)
1358 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001359 }
1360
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001361 /* if general error occurs, retry the write operation. */
1362 if (gen_err) {
1363 pr_warn("%s: retrying write for general error\n",
1364 req->rq_disk->disk_name);
1365 return MMC_BLK_RETRY;
1366 }
1367
Per Forlind78d4a82011-07-01 18:55:30 +02001368 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001369 if (need_retune && !brq->retune_retry_done) {
1370 pr_info("%s: retrying because a re-tune was needed\n",
1371 req->rq_disk->disk_name);
1372 brq->retune_retry_done = 1;
1373 return MMC_BLK_RETRY;
1374 }
Per Forlind78d4a82011-07-01 18:55:30 +02001375 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1376 req->rq_disk->disk_name, brq->data.error,
1377 (unsigned)blk_rq_pos(req),
1378 (unsigned)blk_rq_sectors(req),
1379 brq->cmd.resp[0], brq->stop.resp[0]);
1380
1381 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001382 if (ecc_err)
1383 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001384 return MMC_BLK_DATA_ERR;
1385 } else {
1386 return MMC_BLK_CMD_ERR;
1387 }
1388 }
1389
Adrian Hunter67716322011-08-29 16:42:15 +03001390 if (!brq->data.bytes_xfered)
1391 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001392
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001393 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1394 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1395 return MMC_BLK_PARTIAL;
1396 else
1397 return MMC_BLK_SUCCESS;
1398 }
1399
Adrian Hunter67716322011-08-29 16:42:15 +03001400 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1401 return MMC_BLK_PARTIAL;
1402
1403 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001404}
1405
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001406static int mmc_blk_packed_err_check(struct mmc_card *card,
1407 struct mmc_async_req *areq)
1408{
1409 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1410 mmc_active);
1411 struct request *req = mq_rq->req;
1412 struct mmc_packed *packed = mq_rq->packed;
1413 int err, check, status;
1414 u8 *ext_csd;
1415
1416 BUG_ON(!packed);
1417
1418 packed->retries--;
1419 check = mmc_blk_err_check(card, areq);
1420 err = get_card_status(card, &status, 0);
1421 if (err) {
1422 pr_err("%s: error %d sending status command\n",
1423 req->rq_disk->disk_name, err);
1424 return MMC_BLK_ABORT;
1425 }
1426
1427 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02001428 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001429 if (err) {
1430 pr_err("%s: error %d sending ext_csd\n",
1431 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02001432 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001433 }
1434
1435 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1436 EXT_CSD_PACKED_FAILURE) &&
1437 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1438 EXT_CSD_PACKED_GENERIC_ERROR)) {
1439 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1440 EXT_CSD_PACKED_INDEXED_ERROR) {
1441 packed->idx_failure =
1442 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1443 check = MMC_BLK_PARTIAL;
1444 }
1445 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1446 "failure index: %d\n",
1447 req->rq_disk->disk_name, packed->nr_entries,
1448 packed->blocks, packed->idx_failure);
1449 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001450 kfree(ext_csd);
1451 }
1452
1453 return check;
1454}
1455
Per Forlin54d49d72011-07-01 18:55:29 +02001456static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1457 struct mmc_card *card,
1458 int disable_multi,
1459 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Per Forlin54d49d72011-07-01 18:55:29 +02001461 u32 readcmd, writecmd;
1462 struct mmc_blk_request *brq = &mqrq->brq;
1463 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301465 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001467 /*
1468 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00001469 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001470 */
Luca Porziod3df0462015-11-06 15:12:26 +00001471 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001472 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001473 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001474
Per Forlin54d49d72011-07-01 18:55:29 +02001475 memset(brq, 0, sizeof(struct mmc_blk_request));
1476 brq->mrq.cmd = &brq->cmd;
1477 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Per Forlin54d49d72011-07-01 18:55:29 +02001479 brq->cmd.arg = blk_rq_pos(req);
1480 if (!mmc_card_blockaddr(card))
1481 brq->cmd.arg <<= 9;
1482 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1483 brq->data.blksz = 512;
1484 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1485 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001486 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Per Forlin54d49d72011-07-01 18:55:29 +02001488 /*
1489 * The block layer doesn't support all sector count
1490 * restrictions, so we need to be prepared for too big
1491 * requests.
1492 */
1493 if (brq->data.blocks > card->host->max_blk_count)
1494 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001496 if (brq->data.blocks > 1) {
1497 /*
1498 * After a read error, we redo the request one sector
1499 * at a time in order to accurately determine which
1500 * sectors can be read successfully.
1501 */
1502 if (disable_multi)
1503 brq->data.blocks = 1;
1504
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07001505 /*
1506 * Some controllers have HW issues while operating
1507 * in multiple I/O mode
1508 */
1509 if (card->host->ops->multi_io_quirk)
1510 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1511 (rq_data_dir(req) == READ) ?
1512 MMC_DATA_READ : MMC_DATA_WRITE,
1513 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001514 }
Per Forlin54d49d72011-07-01 18:55:29 +02001515
1516 if (brq->data.blocks > 1 || do_rel_wr) {
1517 /* SPI multiblock writes terminate using a special
1518 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001519 */
Per Forlin54d49d72011-07-01 18:55:29 +02001520 if (!mmc_host_is_spi(card->host) ||
1521 rq_data_dir(req) == READ)
1522 brq->mrq.stop = &brq->stop;
1523 readcmd = MMC_READ_MULTIPLE_BLOCK;
1524 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1525 } else {
1526 brq->mrq.stop = NULL;
1527 readcmd = MMC_READ_SINGLE_BLOCK;
1528 writecmd = MMC_WRITE_BLOCK;
1529 }
1530 if (rq_data_dir(req) == READ) {
1531 brq->cmd.opcode = readcmd;
1532 brq->data.flags |= MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001533 if (brq->mrq.stop)
1534 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1535 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001536 } else {
1537 brq->cmd.opcode = writecmd;
1538 brq->data.flags |= MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01001539 if (brq->mrq.stop)
1540 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1541 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02001542 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001543
Per Forlin54d49d72011-07-01 18:55:29 +02001544 if (do_rel_wr)
1545 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001546
Per Forlin54d49d72011-07-01 18:55:29 +02001547 /*
Saugata Das42659002011-12-21 13:09:17 +05301548 * Data tag is used only during writing meta data to speed
1549 * up write and any subsequent read of this meta data
1550 */
1551 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1552 (req->cmd_flags & REQ_META) &&
1553 (rq_data_dir(req) == WRITE) &&
1554 ((brq->data.blocks * brq->data.blksz) >=
1555 card->ext_csd.data_tag_unit_size);
1556
1557 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001558 * Pre-defined multi-block transfers are preferable to
1559 * open ended-ones (and necessary for reliable writes).
1560 * However, it is not sufficient to just send CMD23,
1561 * and avoid the final CMD12, as on an error condition
1562 * CMD12 (stop) needs to be sent anyway. This, coupled
1563 * with Auto-CMD23 enhancements provided by some
1564 * hosts, means that the complexity of dealing
1565 * with this is best left to the host. If CMD23 is
1566 * supported by card and host, we'll fill sbc in and let
1567 * the host deal with handling it correctly. This means
1568 * that for hosts that don't expose MMC_CAP_CMD23, no
1569 * change of behavior will be observed.
1570 *
1571 * N.B: Some MMC cards experience perf degradation.
1572 * We'll avoid using CMD23-bounded multiblock writes for
1573 * these, while retaining features like reliable writes.
1574 */
Saugata Das42659002011-12-21 13:09:17 +05301575 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1576 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1577 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001578 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1579 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301580 (do_rel_wr ? (1 << 31) : 0) |
1581 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001582 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1583 brq->mrq.sbc = &brq->sbc;
1584 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001585
Per Forlin54d49d72011-07-01 18:55:29 +02001586 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001587
Per Forlin54d49d72011-07-01 18:55:29 +02001588 brq->data.sg = mqrq->sg;
1589 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001590
Per Forlin54d49d72011-07-01 18:55:29 +02001591 /*
1592 * Adjust the sg list so it is the same size as the
1593 * request.
1594 */
1595 if (brq->data.blocks != blk_rq_sectors(req)) {
1596 int i, data_size = brq->data.blocks << 9;
1597 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001598
Per Forlin54d49d72011-07-01 18:55:29 +02001599 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1600 data_size -= sg->length;
1601 if (data_size <= 0) {
1602 sg->length += data_size;
1603 i++;
1604 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001605 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001606 }
Per Forlin54d49d72011-07-01 18:55:29 +02001607 brq->data.sg_len = i;
1608 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001609
Per Forlinee8a43a2011-07-01 18:55:33 +02001610 mqrq->mmc_active.mrq = &brq->mrq;
1611 mqrq->mmc_active.err_check = mmc_blk_err_check;
1612
Per Forlin54d49d72011-07-01 18:55:29 +02001613 mmc_queue_bounce_pre(mqrq);
1614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001616static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1617 struct mmc_card *card)
1618{
1619 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1620 unsigned int max_seg_sz = queue_max_segment_size(q);
1621 unsigned int len, nr_segs = 0;
1622
1623 do {
1624 len = min(hdr_sz, max_seg_sz);
1625 hdr_sz -= len;
1626 nr_segs++;
1627 } while (hdr_sz);
1628
1629 return nr_segs;
1630}
1631
1632static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1633{
1634 struct request_queue *q = mq->queue;
1635 struct mmc_card *card = mq->card;
1636 struct request *cur = req, *next = NULL;
1637 struct mmc_blk_data *md = mq->data;
1638 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1639 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1640 unsigned int req_sectors = 0, phys_segments = 0;
1641 unsigned int max_blk_count, max_phys_segs;
1642 bool put_back = true;
1643 u8 max_packed_rw = 0;
1644 u8 reqs = 0;
1645
1646 if (!(md->flags & MMC_BLK_PACKED_CMD))
1647 goto no_packed;
1648
1649 if ((rq_data_dir(cur) == WRITE) &&
1650 mmc_host_packed_wr(card->host))
1651 max_packed_rw = card->ext_csd.max_packed_writes;
1652
1653 if (max_packed_rw == 0)
1654 goto no_packed;
1655
1656 if (mmc_req_rel_wr(cur) &&
1657 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1658 goto no_packed;
1659
1660 if (mmc_large_sector(card) &&
1661 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1662 goto no_packed;
1663
1664 mmc_blk_clear_packed(mqrq);
1665
1666 max_blk_count = min(card->host->max_blk_count,
1667 card->host->max_req_size >> 9);
1668 if (unlikely(max_blk_count > 0xffff))
1669 max_blk_count = 0xffff;
1670
1671 max_phys_segs = queue_max_segments(q);
1672 req_sectors += blk_rq_sectors(cur);
1673 phys_segments += cur->nr_phys_segments;
1674
1675 if (rq_data_dir(cur) == WRITE) {
1676 req_sectors += mmc_large_sector(card) ? 8 : 1;
1677 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1678 }
1679
1680 do {
1681 if (reqs >= max_packed_rw - 1) {
1682 put_back = false;
1683 break;
1684 }
1685
1686 spin_lock_irq(q->queue_lock);
1687 next = blk_fetch_request(q);
1688 spin_unlock_irq(q->queue_lock);
1689 if (!next) {
1690 put_back = false;
1691 break;
1692 }
1693
1694 if (mmc_large_sector(card) &&
1695 !IS_ALIGNED(blk_rq_sectors(next), 8))
1696 break;
1697
1698 if (next->cmd_flags & REQ_DISCARD ||
1699 next->cmd_flags & REQ_FLUSH)
1700 break;
1701
1702 if (rq_data_dir(cur) != rq_data_dir(next))
1703 break;
1704
1705 if (mmc_req_rel_wr(next) &&
1706 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1707 break;
1708
1709 req_sectors += blk_rq_sectors(next);
1710 if (req_sectors > max_blk_count)
1711 break;
1712
1713 phys_segments += next->nr_phys_segments;
1714 if (phys_segments > max_phys_segs)
1715 break;
1716
1717 list_add_tail(&next->queuelist, &mqrq->packed->list);
1718 cur = next;
1719 reqs++;
1720 } while (1);
1721
1722 if (put_back) {
1723 spin_lock_irq(q->queue_lock);
1724 blk_requeue_request(q, next);
1725 spin_unlock_irq(q->queue_lock);
1726 }
1727
1728 if (reqs > 0) {
1729 list_add(&req->queuelist, &mqrq->packed->list);
1730 mqrq->packed->nr_entries = ++reqs;
1731 mqrq->packed->retries = reqs;
1732 return reqs;
1733 }
1734
1735no_packed:
1736 mqrq->cmd_type = MMC_PACKED_NONE;
1737 return 0;
1738}
1739
1740static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1741 struct mmc_card *card,
1742 struct mmc_queue *mq)
1743{
1744 struct mmc_blk_request *brq = &mqrq->brq;
1745 struct request *req = mqrq->req;
1746 struct request *prq;
1747 struct mmc_blk_data *md = mq->data;
1748 struct mmc_packed *packed = mqrq->packed;
1749 bool do_rel_wr, do_data_tag;
1750 u32 *packed_cmd_hdr;
1751 u8 hdr_blocks;
1752 u8 i = 1;
1753
1754 BUG_ON(!packed);
1755
1756 mqrq->cmd_type = MMC_PACKED_WRITE;
1757 packed->blocks = 0;
1758 packed->idx_failure = MMC_PACKED_NR_IDX;
1759
1760 packed_cmd_hdr = packed->cmd_hdr;
1761 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1762 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1763 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1764 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1765
1766 /*
1767 * Argument for each entry of packed group
1768 */
1769 list_for_each_entry(prq, &packed->list, queuelist) {
1770 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1771 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1772 (prq->cmd_flags & REQ_META) &&
1773 (rq_data_dir(prq) == WRITE) &&
1774 ((brq->data.blocks * brq->data.blksz) >=
1775 card->ext_csd.data_tag_unit_size);
1776 /* Argument of CMD23 */
1777 packed_cmd_hdr[(i * 2)] =
1778 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1779 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1780 blk_rq_sectors(prq);
1781 /* Argument of CMD18 or CMD25 */
1782 packed_cmd_hdr[((i * 2)) + 1] =
1783 mmc_card_blockaddr(card) ?
1784 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1785 packed->blocks += blk_rq_sectors(prq);
1786 i++;
1787 }
1788
1789 memset(brq, 0, sizeof(struct mmc_blk_request));
1790 brq->mrq.cmd = &brq->cmd;
1791 brq->mrq.data = &brq->data;
1792 brq->mrq.sbc = &brq->sbc;
1793 brq->mrq.stop = &brq->stop;
1794
1795 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1796 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1797 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1798
1799 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1800 brq->cmd.arg = blk_rq_pos(req);
1801 if (!mmc_card_blockaddr(card))
1802 brq->cmd.arg <<= 9;
1803 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1804
1805 brq->data.blksz = 512;
1806 brq->data.blocks = packed->blocks + hdr_blocks;
1807 brq->data.flags |= MMC_DATA_WRITE;
1808
1809 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1810 brq->stop.arg = 0;
1811 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1812
1813 mmc_set_data_timeout(&brq->data, card);
1814
1815 brq->data.sg = mqrq->sg;
1816 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1817
1818 mqrq->mmc_active.mrq = &brq->mrq;
1819 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1820
1821 mmc_queue_bounce_pre(mqrq);
1822}
1823
Adrian Hunter67716322011-08-29 16:42:15 +03001824static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1825 struct mmc_blk_request *brq, struct request *req,
1826 int ret)
1827{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001828 struct mmc_queue_req *mq_rq;
1829 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1830
Adrian Hunter67716322011-08-29 16:42:15 +03001831 /*
1832 * If this is an SD card and we're writing, we can first
1833 * mark the known good sectors as ok.
1834 *
1835 * If the card is not SD, we can still ok written sectors
1836 * as reported by the controller (which might be less than
1837 * the real number of written sectors, but never more).
1838 */
1839 if (mmc_card_sd(card)) {
1840 u32 blocks;
1841
1842 blocks = mmc_sd_num_wr_blocks(card);
1843 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301844 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001845 }
1846 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001847 if (!mmc_packed_cmd(mq_rq->cmd_type))
1848 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001849 }
1850 return ret;
1851}
1852
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001853static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1854{
1855 struct request *prq;
1856 struct mmc_packed *packed = mq_rq->packed;
1857 int idx = packed->idx_failure, i = 0;
1858 int ret = 0;
1859
1860 BUG_ON(!packed);
1861
1862 while (!list_empty(&packed->list)) {
1863 prq = list_entry_rq(packed->list.next);
1864 if (idx == i) {
1865 /* retry from error index */
1866 packed->nr_entries -= idx;
1867 mq_rq->req = prq;
1868 ret = 1;
1869
1870 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1871 list_del_init(&prq->queuelist);
1872 mmc_blk_clear_packed(mq_rq);
1873 }
1874 return ret;
1875 }
1876 list_del_init(&prq->queuelist);
1877 blk_end_request(prq, 0, blk_rq_bytes(prq));
1878 i++;
1879 }
1880
1881 mmc_blk_clear_packed(mq_rq);
1882 return ret;
1883}
1884
1885static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1886{
1887 struct request *prq;
1888 struct mmc_packed *packed = mq_rq->packed;
1889
1890 BUG_ON(!packed);
1891
1892 while (!list_empty(&packed->list)) {
1893 prq = list_entry_rq(packed->list.next);
1894 list_del_init(&prq->queuelist);
1895 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1896 }
1897
1898 mmc_blk_clear_packed(mq_rq);
1899}
1900
1901static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1902 struct mmc_queue_req *mq_rq)
1903{
1904 struct request *prq;
1905 struct request_queue *q = mq->queue;
1906 struct mmc_packed *packed = mq_rq->packed;
1907
1908 BUG_ON(!packed);
1909
1910 while (!list_empty(&packed->list)) {
1911 prq = list_entry_rq(packed->list.prev);
1912 if (prq->queuelist.prev != &packed->list) {
1913 list_del_init(&prq->queuelist);
1914 spin_lock_irq(q->queue_lock);
1915 blk_requeue_request(mq->queue, prq);
1916 spin_unlock_irq(q->queue_lock);
1917 } else {
1918 list_del_init(&prq->queuelist);
1919 }
1920 }
1921
1922 mmc_blk_clear_packed(mq_rq);
1923}
1924
Per Forlinee8a43a2011-07-01 18:55:33 +02001925static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001926{
1927 struct mmc_blk_data *md = mq->data;
1928 struct mmc_card *card = md->queue.card;
1929 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001930 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001931 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001932 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301933 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001934 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001935 const u8 packed_nr = 2;
1936 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001937
1938 if (!rqc && !mq->mqrq_prev->req)
1939 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001940
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001941 if (rqc)
1942 reqs = mmc_blk_prep_packed_list(mq, rqc);
1943
Per Forlin54d49d72011-07-01 18:55:29 +02001944 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001945 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301946 /*
1947 * When 4KB native sector is enabled, only 8 blocks
1948 * multiple read or write is allowed
1949 */
1950 if ((brq->data.blocks & 0x07) &&
1951 (card->ext_csd.data_sector_size == 4096)) {
1952 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1953 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001954 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301955 goto cmd_abort;
1956 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001957
1958 if (reqs >= packed_nr)
1959 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1960 card, mq);
1961 else
1962 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001963 areq = &mq->mqrq_cur->mmc_active;
1964 } else
1965 areq = NULL;
1966 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001967 if (!areq) {
1968 if (status == MMC_BLK_NEW_REQUEST)
1969 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001970 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001971 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001972
Per Forlinee8a43a2011-07-01 18:55:33 +02001973 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1974 brq = &mq_rq->brq;
1975 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001976 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001977 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001978
Per Forlind78d4a82011-07-01 18:55:30 +02001979 switch (status) {
1980 case MMC_BLK_SUCCESS:
1981 case MMC_BLK_PARTIAL:
1982 /*
1983 * A block was successfully transferred.
1984 */
Adrian Hunter67716322011-08-29 16:42:15 +03001985 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001986
1987 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1988 ret = mmc_blk_end_packed_req(mq_rq);
1989 break;
1990 } else {
1991 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001992 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001993 }
1994
Adrian Hunter67716322011-08-29 16:42:15 +03001995 /*
1996 * If the blk_end_request function returns non-zero even
1997 * though all data has been transferred and no errors
1998 * were returned by the host controller, it's a bug.
1999 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002000 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302001 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002002 __func__, blk_rq_bytes(req),
2003 brq->data.bytes_xfered);
2004 rqc = NULL;
2005 goto cmd_abort;
2006 }
Per Forlind78d4a82011-07-01 18:55:30 +02002007 break;
2008 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002009 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002010 if (mmc_blk_reset(md, card->host, type))
2011 goto cmd_abort;
2012 if (!ret)
2013 goto start_new_req;
2014 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002015 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002016 retune_retry_done = brq->retune_retry_done;
Per Forlind78d4a82011-07-01 18:55:30 +02002017 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002018 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002019 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002020 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03002021 if (!mmc_blk_reset(md, card->host, type))
2022 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002023 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002024 case MMC_BLK_DATA_ERR: {
2025 int err;
2026
2027 err = mmc_blk_reset(md, card->host, type);
2028 if (!err)
2029 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002030 if (err == -ENODEV ||
2031 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03002032 goto cmd_abort;
2033 /* Fall through */
2034 }
2035 case MMC_BLK_ECC_ERR:
2036 if (brq->data.blocks > 1) {
2037 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002038 pr_warn("%s: retrying using single block read\n",
2039 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002040 disable_multi = 1;
2041 break;
2042 }
Per Forlind78d4a82011-07-01 18:55:30 +02002043 /*
2044 * After an error, we redo I/O one sector at a
2045 * time, so we only reach here after trying to
2046 * read a single sector.
2047 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302048 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002049 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002050 if (!ret)
2051 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002052 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302053 case MMC_BLK_NOMEDIUM:
2054 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002055 default:
2056 pr_err("%s: Unhandled return value (%d)",
2057 req->rq_disk->disk_name, status);
2058 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002059 }
2060
Per Forlinee8a43a2011-07-01 18:55:33 +02002061 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002062 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2063 if (!mq_rq->packed->retries)
2064 goto cmd_abort;
2065 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2066 mmc_start_req(card->host,
2067 &mq_rq->mmc_active, NULL);
2068 } else {
2069
2070 /*
2071 * In case of a incomplete request
2072 * prepare it again and resend.
2073 */
2074 mmc_blk_rw_rq_prep(mq_rq, card,
2075 disable_multi, mq);
2076 mmc_start_req(card->host,
2077 &mq_rq->mmc_active, NULL);
2078 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002079 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 } while (ret);
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 1;
2084
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01002085 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002086 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2087 mmc_blk_abort_packed_req(mq_rq);
2088 } else {
2089 if (mmc_card_removed(card))
2090 req->cmd_flags |= REQ_QUIET;
2091 while (ret)
2092 ret = blk_end_request(req, -EIO,
2093 blk_rq_cur_bytes(req));
2094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Per Forlinee8a43a2011-07-01 18:55:33 +02002096 start_new_req:
2097 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002098 if (mmc_card_removed(card)) {
2099 rqc->cmd_flags |= REQ_QUIET;
2100 blk_end_request_all(rqc, -EIO);
2101 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002102 /*
2103 * If current request is packed, it needs to put back.
2104 */
2105 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2106 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2107
Seungwon Jeon7a819022013-01-22 19:48:07 +09002108 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2109 mmc_start_req(card->host,
2110 &mq->mqrq_cur->mmc_active, NULL);
2111 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002112 }
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
Adrian Hunterbd788c92010-08-11 14:17:47 -07002117static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2118{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002119 int ret;
2120 struct mmc_blk_data *md = mq->data;
2121 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002122 struct mmc_host *host = card->host;
2123 unsigned long flags;
Ray Juif662ae42013-10-26 11:03:44 -07002124 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002125
Per Forlinee8a43a2011-07-01 18:55:33 +02002126 if (req && !mq->mqrq_prev->req)
2127 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002128 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002129
Andrei Warkentin371a6892011-04-11 18:10:25 -05002130 ret = mmc_blk_part_switch(card, md);
2131 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002132 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302133 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002134 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002135 ret = 0;
2136 goto out;
2137 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002138
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002139 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Ray Juif662ae42013-10-26 11:03:44 -07002140 if (cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002141 /* complete ongoing async transfer before issuing discard */
2142 if (card->host->areq)
2143 mmc_blk_issue_rw_rq(mq, NULL);
Lukas Czerner5204d002014-06-18 13:18:07 +02002144 if (req->cmd_flags & REQ_SECURE)
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002145 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002146 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002147 ret = mmc_blk_issue_discard_rq(mq, req);
Ray Juif662ae42013-10-26 11:03:44 -07002148 } else if (cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09002149 /* complete ongoing async transfer before issuing flush */
2150 if (card->host->areq)
2151 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002152 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002153 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002154 if (!req && host->areq) {
2155 spin_lock_irqsave(&host->context_info.lock, flags);
2156 host->context_info.is_waiting_last_req = true;
2157 spin_unlock_irqrestore(&host->context_info.lock, flags);
2158 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002159 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07002160 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002161
Andrei Warkentin371a6892011-04-11 18:10:25 -05002162out:
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002163 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Ray Juif662ae42013-10-26 11:03:44 -07002164 (cmd_flags & MMC_REQ_SPECIAL_MASK))
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09002165 /*
2166 * Release host when there are no more requests
2167 * and after special request(discard, flush) is done.
2168 * In case sepecial request, there is no reentry to
2169 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2170 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002171 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002172 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07002173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Russell Kinga6f6c962006-01-03 22:38:44 +00002175static inline int mmc_blk_readonly(struct mmc_card *card)
2176{
2177 return mmc_card_readonly(card) ||
2178 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2179}
2180
Andrei Warkentin371a6892011-04-11 18:10:25 -05002181static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2182 struct device *parent,
2183 sector_t size,
2184 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002185 const char *subname,
2186 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct mmc_blk_data *md;
2189 int devidx, ret;
2190
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002191 devidx = find_first_zero_bit(dev_use, max_devices);
2192 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return ERR_PTR(-ENOSPC);
2194 __set_bit(devidx, dev_use);
2195
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002196 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002197 if (!md) {
2198 ret = -ENOMEM;
2199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002201
Russell Kinga6f6c962006-01-03 22:38:44 +00002202 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002203 * !subname implies we are creating main mmc_blk_data that will be
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002204 * associated with mmc_card with dev_set_drvdata. Due to device
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002205 * partitions, devidx will not coincide with a per-physical card
2206 * index anymore so we keep track of a name index.
2207 */
2208 if (!subname) {
2209 md->name_idx = find_first_zero_bit(name_use, max_devices);
2210 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002211 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002212 md->name_idx = ((struct mmc_blk_data *)
2213 dev_to_disk(parent)->private_data)->name_idx;
2214
Johan Rudholmadd710e2011-12-02 08:51:06 +01002215 md->area_type = area_type;
2216
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002217 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002218 * Set the read-only status based on the supported commands
2219 * and the write protect switch.
2220 */
2221 md->read_only = mmc_blk_readonly(card);
2222
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002223 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002224 if (md->disk == NULL) {
2225 ret = -ENOMEM;
2226 goto err_kfree;
2227 }
2228
2229 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002230 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002231 md->usage = 1;
2232
Adrian Hunterd09408a2011-06-23 13:40:28 +03002233 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002234 if (ret)
2235 goto err_putdisk;
2236
Russell Kinga6f6c962006-01-03 22:38:44 +00002237 md->queue.issue_fn = mmc_blk_issue_rq;
2238 md->queue.data = md;
2239
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002240 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002241 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002242 md->disk->fops = &mmc_bdops;
2243 md->disk->private_data = md;
2244 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002245 md->disk->driverfs_dev = parent;
2246 set_disk_ro(md->disk, md->read_only || default_ro);
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02002247 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02002248 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002249
2250 /*
2251 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2252 *
2253 * - be set for removable media with permanent block devices
2254 * - be unset for removable block devices with permanent media
2255 *
2256 * Since MMC block devices clearly fall under the second
2257 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2258 * should use the block device creation/destruction hotplug
2259 * messages to tell when the card is present.
2260 */
2261
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002262 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Asaf Vertzfe821912015-01-20 18:33:35 +02002263 "mmcblk%u%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002264
Saugata Dasa5075eb2012-05-17 16:32:21 +05302265 if (mmc_card_mmc(card))
2266 blk_queue_logical_block_size(md->queue.queue,
2267 card->ext_csd.data_sector_size);
2268 else
2269 blk_queue_logical_block_size(md->queue.queue, 512);
2270
Andrei Warkentin371a6892011-04-11 18:10:25 -05002271 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002272
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002273 if (mmc_host_cmd23(card->host)) {
2274 if (mmc_card_mmc(card) ||
2275 (mmc_card_sd(card) &&
2276 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2277 md->flags |= MMC_BLK_CMD23;
2278 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002279
2280 if (mmc_card_mmc(card) &&
2281 md->flags & MMC_BLK_CMD23 &&
2282 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2283 card->ext_csd.rel_sectors)) {
2284 md->flags |= MMC_BLK_REL_WR;
2285 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2286 }
2287
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002288 if (mmc_card_mmc(card) &&
2289 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2290 (md->flags & MMC_BLK_CMD23) &&
2291 card->ext_csd.packed_event_en) {
2292 if (!mmc_packed_init(&md->queue, card))
2293 md->flags |= MMC_BLK_PACKED_CMD;
2294 }
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002297
2298 err_putdisk:
2299 put_disk(md->disk);
2300 err_kfree:
2301 kfree(md);
2302 out:
2303 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Andrei Warkentin371a6892011-04-11 18:10:25 -05002306static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2307{
2308 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002309
2310 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2311 /*
2312 * The EXT_CSD sector count is in number or 512 byte
2313 * sectors.
2314 */
2315 size = card->ext_csd.sectors;
2316 } else {
2317 /*
2318 * The CSD capacity field is in units of read_blkbits.
2319 * set_capacity takes units of 512 bytes.
2320 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00002321 size = (typeof(sector_t))card->csd.capacity
2322 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002323 }
2324
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01002325 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002326 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002327}
2328
2329static int mmc_blk_alloc_part(struct mmc_card *card,
2330 struct mmc_blk_data *md,
2331 unsigned int part_type,
2332 sector_t size,
2333 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002334 const char *subname,
2335 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002336{
2337 char cap_str[10];
2338 struct mmc_blk_data *part_md;
2339
2340 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002341 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002342 if (IS_ERR(part_md))
2343 return PTR_ERR(part_md);
2344 part_md->part_type = part_type;
2345 list_add(&part_md->part, &md->part);
2346
James Bottomleyb9f28d82015-03-05 18:47:01 -08002347 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05002348 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302349 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002350 part_md->disk->disk_name, mmc_card_id(card),
2351 mmc_card_name(card), part_md->part_type, cap_str);
2352 return 0;
2353}
2354
Namjae Jeone0c368d2011-10-06 23:41:38 +09002355/* MMC Physical partitions consist of two boot partitions and
2356 * up to four general purpose partitions.
2357 * For each partition enabled in EXT_CSD a block device will be allocatedi
2358 * to provide access to the partition.
2359 */
2360
Andrei Warkentin371a6892011-04-11 18:10:25 -05002361static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2362{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002363 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002364
2365 if (!mmc_card_mmc(card))
2366 return 0;
2367
Namjae Jeone0c368d2011-10-06 23:41:38 +09002368 for (idx = 0; idx < card->nr_parts; idx++) {
2369 if (card->part[idx].size) {
2370 ret = mmc_blk_alloc_part(card, md,
2371 card->part[idx].part_cfg,
2372 card->part[idx].size >> 9,
2373 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002374 card->part[idx].name,
2375 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002376 if (ret)
2377 return ret;
2378 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002379 }
2380
2381 return ret;
2382}
2383
Andrei Warkentin371a6892011-04-11 18:10:25 -05002384static void mmc_blk_remove_req(struct mmc_blk_data *md)
2385{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002386 struct mmc_card *card;
2387
Andrei Warkentin371a6892011-04-11 18:10:25 -05002388 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002389 /*
2390 * Flush remaining requests and free queues. It
2391 * is freeing the queue that stops new requests
2392 * from being accepted.
2393 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02002394 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07002395 mmc_cleanup_queue(&md->queue);
2396 if (md->flags & MMC_BLK_PACKED_CMD)
2397 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002398 if (md->disk->flags & GENHD_FL_UP) {
2399 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002400 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2401 card->ext_csd.boot_ro_lockable)
2402 device_remove_file(disk_to_dev(md->disk),
2403 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002404
Andrei Warkentin371a6892011-04-11 18:10:25 -05002405 del_gendisk(md->disk);
2406 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002407 mmc_blk_put(md);
2408 }
2409}
2410
2411static void mmc_blk_remove_parts(struct mmc_card *card,
2412 struct mmc_blk_data *md)
2413{
2414 struct list_head *pos, *q;
2415 struct mmc_blk_data *part_md;
2416
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002417 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002418 list_for_each_safe(pos, q, &md->part) {
2419 part_md = list_entry(pos, struct mmc_blk_data, part);
2420 list_del(pos);
2421 mmc_blk_remove_req(part_md);
2422 }
2423}
2424
2425static int mmc_add_disk(struct mmc_blk_data *md)
2426{
2427 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002428 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002429
2430 add_disk(md->disk);
2431 md->force_ro.show = force_ro_show;
2432 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302433 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002434 md->force_ro.attr.name = "force_ro";
2435 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2436 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2437 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002438 goto force_ro_fail;
2439
2440 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2441 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002442 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002443
2444 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2445 mode = S_IRUGO;
2446 else
2447 mode = S_IRUGO | S_IWUSR;
2448
2449 md->power_ro_lock.show = power_ro_lock_show;
2450 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002451 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002452 md->power_ro_lock.attr.mode = mode;
2453 md->power_ro_lock.attr.name =
2454 "ro_lock_until_next_power_on";
2455 ret = device_create_file(disk_to_dev(md->disk),
2456 &md->power_ro_lock);
2457 if (ret)
2458 goto power_ro_lock_fail;
2459 }
2460 return ret;
2461
2462power_ro_lock_fail:
2463 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2464force_ro_fail:
2465 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002466
2467 return ret;
2468}
2469
Chris Ballc59d4472011-11-11 22:01:43 -05002470#define CID_MANFID_SANDISK 0x2
2471#define CID_MANFID_TOSHIBA 0x11
2472#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09002473#define CID_MANFID_SAMSUNG 0x15
Shawn Linb5b4ff02015-08-12 13:08:32 +08002474#define CID_MANFID_KINGSTON 0x70
Chris Ballc59d4472011-11-11 22:01:43 -05002475
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002476static const struct mmc_fixup blk_fixups[] =
2477{
Chris Ballc59d4472011-11-11 22:01:43 -05002478 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2479 MMC_QUIRK_INAND_CMD38),
2480 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2481 MMC_QUIRK_INAND_CMD38),
2482 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2483 MMC_QUIRK_INAND_CMD38),
2484 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2485 MMC_QUIRK_INAND_CMD38),
2486 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2487 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002488
2489 /*
2490 * Some MMC cards experience performance degradation with CMD23
2491 * instead of CMD12-bounded multiblock transfers. For now we'll
2492 * black list what's bad...
2493 * - Certain Toshiba cards.
2494 *
2495 * N.B. This doesn't affect SD cards.
2496 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08002497 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2498 MMC_QUIRK_BLK_NO_CMD23),
2499 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2500 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002501 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002502 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002503 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002504 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002505 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002506 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002507
2508 /*
2509 * Some Micron MMC cards needs longer data read timeout than
2510 * indicated in CSD.
2511 */
Chris Ballc59d4472011-11-11 22:01:43 -05002512 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002513 MMC_QUIRK_LONG_READ_TIME),
2514
Ian Chen3550ccd2012-08-29 15:05:36 +09002515 /*
2516 * On these Samsung MoviNAND parts, performing secure erase or
2517 * secure trim can result in unrecoverable corruption due to a
2518 * firmware bug.
2519 */
2520 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2521 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2522 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2523 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2524 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2525 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2526 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2527 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2528 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2530 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2531 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2532 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2533 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2534 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2535 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2536
Shawn Linb5b4ff02015-08-12 13:08:32 +08002537 /*
2538 * On Some Kingston eMMCs, performing trim can result in
2539 * unrecoverable data conrruption occasionally due to a firmware bug.
2540 */
2541 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2542 MMC_QUIRK_TRIM_BROKEN),
2543 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2544 MMC_QUIRK_TRIM_BROKEN),
2545
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002546 END_FIXUP
2547};
2548
Ulf Hansson96541ba2015-04-14 13:06:12 +02002549static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002551 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002552 char cap_str[10];
2553
Pierre Ossman912490d2005-05-21 10:27:02 +01002554 /*
2555 * Check that the card supports the command class(es) we need.
2556 */
2557 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return -ENODEV;
2559
Lukas Czerner5204d002014-06-18 13:18:07 +02002560 mmc_fixup_device(card, blk_fixups);
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 md = mmc_blk_alloc(card);
2563 if (IS_ERR(md))
2564 return PTR_ERR(md);
2565
James Bottomleyb9f28d82015-03-05 18:47:01 -08002566 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002567 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302568 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002570 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Andrei Warkentin371a6892011-04-11 18:10:25 -05002572 if (mmc_blk_alloc_parts(card, md))
2573 goto out;
2574
Ulf Hansson96541ba2015-04-14 13:06:12 +02002575 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002576
Andrei Warkentin371a6892011-04-11 18:10:25 -05002577 if (mmc_add_disk(md))
2578 goto out;
2579
2580 list_for_each_entry(part_md, &md->part, part) {
2581 if (mmc_add_disk(part_md))
2582 goto out;
2583 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002584
2585 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2586 pm_runtime_use_autosuspend(&card->dev);
2587
2588 /*
2589 * Don't enable runtime PM for SD-combo cards here. Leave that
2590 * decision to be taken during the SDIO init sequence instead.
2591 */
2592 if (card->type != MMC_TYPE_SD_COMBO) {
2593 pm_runtime_set_active(&card->dev);
2594 pm_runtime_enable(&card->dev);
2595 }
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return 0;
2598
2599 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002600 mmc_blk_remove_parts(card, md);
2601 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Ulf Hansson96541ba2015-04-14 13:06:12 +02002605static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002607 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Andrei Warkentin371a6892011-04-11 18:10:25 -05002609 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002610 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002611 mmc_claim_host(card->host);
2612 mmc_blk_part_switch(card, md);
2613 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002614 if (card->type != MMC_TYPE_SD_COMBO)
2615 pm_runtime_disable(&card->dev);
2616 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002617 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02002618 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
Ulf Hansson96541ba2015-04-14 13:06:12 +02002621static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002623 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02002624 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (md) {
2627 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002628 list_for_each_entry(part_md, &md->part, part) {
2629 mmc_queue_suspend(&part_md->queue);
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 return 0;
2633}
2634
Ulf Hansson96541ba2015-04-14 13:06:12 +02002635static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02002636{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002637 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002638}
2639
Ulf Hansson0967edc2014-10-06 11:29:42 +02002640#ifdef CONFIG_PM_SLEEP
2641static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02002642{
Ulf Hansson96541ba2015-04-14 13:06:12 +02002643 struct mmc_card *card = mmc_dev_to_card(dev);
2644
2645 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02002646}
2647
Ulf Hansson0967edc2014-10-06 11:29:42 +02002648static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002650 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02002651 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002654 /*
2655 * Resume involves the card going into idle state,
2656 * so current partition is always the main one.
2657 */
2658 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002660 list_for_each_entry(part_md, &md->part, part) {
2661 mmc_queue_resume(&part_md->queue);
2662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664 return 0;
2665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666#endif
2667
Ulf Hansson0967edc2014-10-06 11:29:42 +02002668static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2669
Ulf Hansson96541ba2015-04-14 13:06:12 +02002670static struct mmc_driver mmc_driver = {
2671 .drv = {
2672 .name = "mmcblk",
2673 .pm = &mmc_blk_pm_ops,
2674 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 .probe = mmc_blk_probe,
2676 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02002677 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678};
2679
2680static int __init mmc_blk_init(void)
2681{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002682 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002684 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2685 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2686
Ben Hutchingsa26eba62014-11-06 03:35:09 +00002687 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002688
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002689 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2690 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002693 res = mmc_register_driver(&mmc_driver);
2694 if (res)
2695 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002697 return 0;
2698 out2:
2699 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 out:
2701 return res;
2702}
2703
2704static void __exit mmc_blk_exit(void)
2705{
2706 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002707 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710module_init(mmc_blk_init);
2711module_exit(mmc_blk_exit);
2712
2713MODULE_LICENSE("GPL");
2714MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2715