blob: 56998eca1a8d0212462a321e43f624aa6601c318 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
John Calixtocb87ea22011-04-26 18:56:29 -040038#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020040#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010041#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45
Pierre Ossman98ac2162006-12-23 20:03:02 +010046#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000048MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040049#ifdef MODULE_PARAM_PREFIX
50#undef MODULE_PARAM_PREFIX
51#endif
52#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010053
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050054#define INAND_CMD38_ARG_EXT_CSD 113
55#define INAND_CMD38_ARG_ERASE 0x00
56#define INAND_CMD38_ARG_TRIM 0x01
57#define INAND_CMD38_ARG_SECERASE 0x80
58#define INAND_CMD38_ARG_SECTRIM1 0x81
59#define INAND_CMD38_ARG_SECTRIM2 0x88
Trey Ramsay8fee4762012-11-16 09:31:41 -060060#define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050061
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090062#define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
63 (req->cmd_flags & REQ_META)) && \
64 (rq_data_dir(req) == WRITE))
65#define PACKED_CMD_VER 0x01
66#define PACKED_CMD_WR 0x02
67
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020068static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040069
70/*
71 * The defaults come from config options but can be overriden by module
72 * or bootarg options.
73 */
74static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
75
76/*
77 * We've only got one major, so number of mmcblk devices is
78 * limited to 256 / number of minors per device.
79 */
80static int max_devices;
81
82/* 256 minors, so at most 256 separate devices */
83static DECLARE_BITMAP(dev_use, 256);
Andrei Warkentinf06c9152011-04-21 22:46:13 -050084static DECLARE_BITMAP(name_use, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * There is one mmc_blk_data per slot.
88 */
89struct mmc_blk_data {
90 spinlock_t lock;
91 struct gendisk *disk;
92 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -050093 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050095 unsigned int flags;
96#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
97#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090098#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -050099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000101 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500102 unsigned int part_type;
Andrei Warkentinf06c9152011-04-21 22:46:13 -0500103 unsigned int name_idx;
Adrian Hunter67716322011-08-29 16:42:15 +0300104 unsigned int reset_done;
105#define MMC_BLK_READ BIT(0)
106#define MMC_BLK_WRITE BIT(1)
107#define MMC_BLK_DISCARD BIT(2)
108#define MMC_BLK_SECDISCARD BIT(3)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500109
110 /*
111 * Only set in main mmc_blk_data associated
112 * with mmc_card with mmc_set_drvdata, and keeps
113 * track of the current selected device partition.
114 */
115 unsigned int part_curr;
116 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100117 struct device_attribute power_ro_lock;
118 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Arjan van de Vena621aae2006-01-12 18:43:35 +0000121static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900123enum {
124 MMC_PACKED_NR_IDX = -1,
125 MMC_PACKED_NR_ZERO,
126 MMC_PACKED_NR_SINGLE,
127};
128
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400129module_param(perdev_minors, int, 0444);
130MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
131
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200132static inline int mmc_blk_part_switch(struct mmc_card *card,
133 struct mmc_blk_data *md);
134static int get_card_status(struct mmc_card *card, u32 *status, int retries);
135
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900136static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
137{
138 struct mmc_packed *packed = mqrq->packed;
139
140 BUG_ON(!packed);
141
142 mqrq->cmd_type = MMC_PACKED_NONE;
143 packed->nr_entries = MMC_PACKED_NR_ZERO;
144 packed->idx_failure = MMC_PACKED_NR_IDX;
145 packed->retries = 0;
146 packed->blocks = 0;
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
150{
151 struct mmc_blk_data *md;
152
Arjan van de Vena621aae2006-01-12 18:43:35 +0000153 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 md = disk->private_data;
155 if (md && md->usage == 0)
156 md = NULL;
157 if (md)
158 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000159 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return md;
162}
163
Andrei Warkentin371a6892011-04-11 18:10:25 -0500164static inline int mmc_get_devidx(struct gendisk *disk)
165{
166 int devmaj = MAJOR(disk_devt(disk));
167 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
168
169 if (!devmaj)
170 devidx = disk->first_minor / perdev_minors;
171 return devidx;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void mmc_blk_put(struct mmc_blk_data *md)
175{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000176 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 md->usage--;
178 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500179 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800180 blk_cleanup_queue(md->queue.queue);
181
David Woodhouse1dff3142007-11-21 18:45:12 +0100182 __clear_bit(devidx, dev_use);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 kfree(md);
186 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000187 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Johan Rudholmadd710e2011-12-02 08:51:06 +0100190static ssize_t power_ro_lock_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
193 int ret;
194 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
195 struct mmc_card *card = md->queue.card;
196 int locked = 0;
197
198 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
199 locked = 2;
200 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
201 locked = 1;
202
203 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
204
205 return ret;
206}
207
208static ssize_t power_ro_lock_store(struct device *dev,
209 struct device_attribute *attr, const char *buf, size_t count)
210{
211 int ret;
212 struct mmc_blk_data *md, *part_md;
213 struct mmc_card *card;
214 unsigned long set;
215
216 if (kstrtoul(buf, 0, &set))
217 return -EINVAL;
218
219 if (set != 1)
220 return count;
221
222 md = mmc_blk_get(dev_to_disk(dev));
223 card = md->queue.card;
224
225 mmc_claim_host(card->host);
226
227 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
228 card->ext_csd.boot_ro_lock |
229 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
230 card->ext_csd.part_time);
231 if (ret)
232 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
233 else
234 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
235
236 mmc_release_host(card->host);
237
238 if (!ret) {
239 pr_info("%s: Locking boot partition ro until next power on\n",
240 md->disk->disk_name);
241 set_disk_ro(md->disk, 1);
242
243 list_for_each_entry(part_md, &md->part, part)
244 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
245 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
246 set_disk_ro(part_md->disk, 1);
247 }
248 }
249
250 mmc_blk_put(md);
251 return count;
252}
253
Andrei Warkentin371a6892011-04-11 18:10:25 -0500254static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
255 char *buf)
256{
257 int ret;
258 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
259
Baruch Siach5d316a32014-09-22 10:12:51 +0300260 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500261 get_disk_ro(dev_to_disk(dev)) ^
262 md->read_only);
263 mmc_blk_put(md);
264 return ret;
265}
266
267static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
268 const char *buf, size_t count)
269{
270 int ret;
271 char *end;
272 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
273 unsigned long set = simple_strtoul(buf, &end, 0);
274 if (end == buf) {
275 ret = -EINVAL;
276 goto out;
277 }
278
279 set_disk_ro(dev_to_disk(dev), set || md->read_only);
280 ret = count;
281out:
282 mmc_blk_put(md);
283 return ret;
284}
285
Al Viroa5a15612008-03-02 10:33:30 -0500286static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Al Viroa5a15612008-03-02 10:33:30 -0500288 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int ret = -ENXIO;
290
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200291 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (md) {
293 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500294 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700296
Al Viroa5a15612008-03-02 10:33:30 -0500297 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700298 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700299 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200302 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return ret;
305}
306
Al Virodb2a1442013-05-05 21:52:57 -0400307static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Al Viroa5a15612008-03-02 10:33:30 -0500309 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200311 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200313 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800317mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800319 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
320 geo->heads = 4;
321 geo->sectors = 16;
322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
John Calixtocb87ea22011-04-26 18:56:29 -0400325struct mmc_blk_ioc_data {
326 struct mmc_ioc_cmd ic;
327 unsigned char *buf;
328 u64 buf_bytes;
329};
330
331static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
332 struct mmc_ioc_cmd __user *user)
333{
334 struct mmc_blk_ioc_data *idata;
335 int err;
336
337 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
338 if (!idata) {
339 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400340 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400341 }
342
343 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
344 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400345 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400346 }
347
348 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
349 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
350 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400351 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400352 }
353
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100354 if (!idata->buf_bytes)
355 return idata;
356
John Calixtocb87ea22011-04-26 18:56:29 -0400357 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
358 if (!idata->buf) {
359 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400360 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400361 }
362
363 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
364 idata->ic.data_ptr, idata->buf_bytes)) {
365 err = -EFAULT;
366 goto copy_err;
367 }
368
369 return idata;
370
371copy_err:
372 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400373idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400374 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400375out:
John Calixtocb87ea22011-04-26 18:56:29 -0400376 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400377}
378
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200379static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
380 u32 retries_max)
381{
382 int err;
383 u32 retry_count = 0;
384
385 if (!status || !retries_max)
386 return -EINVAL;
387
388 do {
389 err = get_card_status(card, status, 5);
390 if (err)
391 break;
392
393 if (!R1_STATUS(*status) &&
394 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
395 break; /* RPMB programming operation complete */
396
397 /*
398 * Rechedule to give the MMC device a chance to continue
399 * processing the previous command without being polled too
400 * frequently.
401 */
402 usleep_range(1000, 5000);
403 } while (++retry_count < retries_max);
404
405 if (retry_count == retries_max)
406 err = -EPERM;
407
408 return err;
409}
410
John Calixtocb87ea22011-04-26 18:56:29 -0400411static int mmc_blk_ioctl_cmd(struct block_device *bdev,
412 struct mmc_ioc_cmd __user *ic_ptr)
413{
414 struct mmc_blk_ioc_data *idata;
415 struct mmc_blk_data *md;
416 struct mmc_card *card;
417 struct mmc_command cmd = {0};
418 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530419 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400420 struct scatterlist sg;
421 int err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200422 int is_rpmb = false;
423 u32 status = 0;
John Calixtocb87ea22011-04-26 18:56:29 -0400424
425 /*
426 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
427 * whole block device, not on a partition. This prevents overspray
428 * between sibling partitions.
429 */
430 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
431 return -EPERM;
432
433 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
434 if (IS_ERR(idata))
435 return PTR_ERR(idata);
436
John Calixtocb87ea22011-04-26 18:56:29 -0400437 md = mmc_blk_get(bdev->bd_disk);
438 if (!md) {
439 err = -EINVAL;
Philippe De Swert1c02f002012-04-11 23:31:45 +0300440 goto cmd_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400441 }
442
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200443 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
444 is_rpmb = true;
445
John Calixtocb87ea22011-04-26 18:56:29 -0400446 card = md->queue.card;
447 if (IS_ERR(card)) {
448 err = PTR_ERR(card);
449 goto cmd_done;
450 }
451
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100452 cmd.opcode = idata->ic.opcode;
453 cmd.arg = idata->ic.arg;
454 cmd.flags = idata->ic.flags;
455
456 if (idata->buf_bytes) {
457 data.sg = &sg;
458 data.sg_len = 1;
459 data.blksz = idata->ic.blksz;
460 data.blocks = idata->ic.blocks;
461
462 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
463
464 if (idata->ic.write_flag)
465 data.flags = MMC_DATA_WRITE;
466 else
467 data.flags = MMC_DATA_READ;
468
469 /* data.flags must already be set before doing this. */
470 mmc_set_data_timeout(&data, card);
471
472 /* Allow overriding the timeout_ns for empirical tuning. */
473 if (idata->ic.data_timeout_ns)
474 data.timeout_ns = idata->ic.data_timeout_ns;
475
476 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
477 /*
478 * Pretend this is a data transfer and rely on the
479 * host driver to compute timeout. When all host
480 * drivers support cmd.cmd_timeout for R1B, this
481 * can be changed to:
482 *
483 * mrq.data = NULL;
484 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
485 */
486 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
487 }
488
489 mrq.data = &data;
490 }
491
492 mrq.cmd = &cmd;
493
John Calixtocb87ea22011-04-26 18:56:29 -0400494 mmc_claim_host(card->host);
495
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200496 err = mmc_blk_part_switch(card, md);
497 if (err)
498 goto cmd_rel_host;
499
John Calixtocb87ea22011-04-26 18:56:29 -0400500 if (idata->ic.is_acmd) {
501 err = mmc_app_cmd(card->host, card);
502 if (err)
503 goto cmd_rel_host;
504 }
505
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200506 if (is_rpmb) {
507 err = mmc_set_blockcount(card, data.blocks,
508 idata->ic.write_flag & (1 << 31));
509 if (err)
510 goto cmd_rel_host;
511 }
512
John Calixtocb87ea22011-04-26 18:56:29 -0400513 mmc_wait_for_req(card->host, &mrq);
514
515 if (cmd.error) {
516 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
517 __func__, cmd.error);
518 err = cmd.error;
519 goto cmd_rel_host;
520 }
521 if (data.error) {
522 dev_err(mmc_dev(card->host), "%s: data error %d\n",
523 __func__, data.error);
524 err = data.error;
525 goto cmd_rel_host;
526 }
527
528 /*
529 * According to the SD specs, some commands require a delay after
530 * issuing the command.
531 */
532 if (idata->ic.postsleep_min_us)
533 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
534
535 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
536 err = -EFAULT;
537 goto cmd_rel_host;
538 }
539
540 if (!idata->ic.write_flag) {
541 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
542 idata->buf, idata->buf_bytes)) {
543 err = -EFAULT;
544 goto cmd_rel_host;
545 }
546 }
547
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200548 if (is_rpmb) {
549 /*
550 * Ensure RPMB command has completed by polling CMD13
551 * "Send Status".
552 */
553 err = ioctl_rpmb_card_status_poll(card, &status, 5);
554 if (err)
555 dev_err(mmc_dev(card->host),
556 "%s: Card Status=0x%08X, error %d\n",
557 __func__, status, err);
558 }
559
John Calixtocb87ea22011-04-26 18:56:29 -0400560cmd_rel_host:
561 mmc_release_host(card->host);
562
563cmd_done:
564 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +0300565cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400566 kfree(idata->buf);
567 kfree(idata);
568 return err;
569}
570
571static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
572 unsigned int cmd, unsigned long arg)
573{
574 int ret = -EINVAL;
575 if (cmd == MMC_IOC_CMD)
576 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
577 return ret;
578}
579
580#ifdef CONFIG_COMPAT
581static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
582 unsigned int cmd, unsigned long arg)
583{
584 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
585}
586#endif
587
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700588static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500589 .open = mmc_blk_open,
590 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800591 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -0400593 .ioctl = mmc_blk_ioctl,
594#ifdef CONFIG_COMPAT
595 .compat_ioctl = mmc_blk_compat_ioctl,
596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597};
598
Andrei Warkentin371a6892011-04-11 18:10:25 -0500599static inline int mmc_blk_part_switch(struct mmc_card *card,
600 struct mmc_blk_data *md)
601{
602 int ret;
603 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300604
Andrei Warkentin371a6892011-04-11 18:10:25 -0500605 if (main_md->part_curr == md->part_type)
606 return 0;
607
608 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300609 u8 part_config = card->ext_csd.part_config;
610
611 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
612 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500613
614 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300615 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -0500616 card->ext_csd.part_time);
617 if (ret)
618 return ret;
Adrian Hunter0d7d85c2011-09-23 12:48:20 +0300619
620 card->ext_csd.part_config = part_config;
Adrian Hunter67716322011-08-29 16:42:15 +0300621 }
Andrei Warkentin371a6892011-04-11 18:10:25 -0500622
623 main_md->part_curr = md->part_type;
624 return 0;
625}
626
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700627static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
628{
629 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100630 u32 result;
631 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700632
Venkatraman Sad5fd972011-08-25 00:30:50 +0530633 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -0400634 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -0400635 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700636
637 struct scatterlist sg;
638
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700639 cmd.opcode = MMC_APP_CMD;
640 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700641 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700642
643 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700644 if (err)
645 return (u32)-1;
646 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700647 return (u32)-1;
648
649 memset(&cmd, 0, sizeof(struct mmc_command));
650
651 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
652 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700653 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700654
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700655 data.blksz = 4;
656 data.blocks = 1;
657 data.flags = MMC_DATA_READ;
658 data.sg = &sg;
659 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +0530660 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700661
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700662 mrq.cmd = &cmd;
663 mrq.data = &data;
664
Ben Dooks051913d2009-06-08 23:33:57 +0100665 blocks = kmalloc(4, GFP_KERNEL);
666 if (!blocks)
667 return (u32)-1;
668
669 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700670
671 mmc_wait_for_req(card->host, &mrq);
672
Ben Dooks051913d2009-06-08 23:33:57 +0100673 result = ntohl(*blocks);
674 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700675
Ben Dooks051913d2009-06-08 23:33:57 +0100676 if (cmd.error || data.error)
677 result = (u32)-1;
678
679 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700680}
681
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100682static int send_stop(struct mmc_card *card, u32 *status)
683{
684 struct mmc_command cmd = {0};
685 int err;
686
687 cmd.opcode = MMC_STOP_TRANSMISSION;
688 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
689 err = mmc_wait_for_cmd(card->host, &cmd, 5);
690 if (err == 0)
691 *status = cmd.resp[0];
692 return err;
693}
694
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100695static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +0300696{
Chris Ball1278dba2011-04-13 23:40:30 -0400697 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +0300698 int err;
699
Adrian Hunter504f1912008-10-16 12:55:25 +0300700 cmd.opcode = MMC_SEND_STATUS;
701 if (!mmc_host_is_spi(card->host))
702 cmd.arg = card->rca << 16;
703 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +0100704 err = mmc_wait_for_cmd(card->host, &cmd, retries);
705 if (err == 0)
706 *status = cmd.resp[0];
707 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +0300708}
709
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530710#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100711#define ERR_RETRY 2
712#define ERR_ABORT 1
713#define ERR_CONTINUE 0
714
715static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
716 bool status_valid, u32 status)
717{
718 switch (error) {
719 case -EILSEQ:
720 /* response crc error, retry the r/w cmd */
721 pr_err("%s: %s sending %s command, card status %#x\n",
722 req->rq_disk->disk_name, "response CRC error",
723 name, status);
724 return ERR_RETRY;
725
726 case -ETIMEDOUT:
727 pr_err("%s: %s sending %s command, card status %#x\n",
728 req->rq_disk->disk_name, "timed out", name, status);
729
730 /* If the status cmd initially failed, retry the r/w cmd */
731 if (!status_valid)
732 return ERR_RETRY;
733
734 /*
735 * If it was a r/w cmd crc error, or illegal command
736 * (eg, issued in wrong state) then retry - we should
737 * have corrected the state problem above.
738 */
739 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
740 return ERR_RETRY;
741
742 /* Otherwise abort the command */
743 return ERR_ABORT;
744
745 default:
746 /* We don't understand the error code the driver gave us */
747 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
748 req->rq_disk->disk_name, error, status);
749 return ERR_ABORT;
750 }
751}
752
753/*
754 * Initial r/w and stop cmd error recovery.
755 * We don't know whether the card received the r/w cmd or not, so try to
756 * restore things back to a sane state. Essentially, we do this as follows:
757 * - Obtain card status. If the first attempt to obtain card status fails,
758 * the status word will reflect the failed status cmd, not the failed
759 * r/w cmd. If we fail to obtain card status, it suggests we can no
760 * longer communicate with the card.
761 * - Check the card state. If the card received the cmd but there was a
762 * transient problem with the response, it might still be in a data transfer
763 * mode. Try to send it a stop command. If this fails, we can't recover.
764 * - If the r/w cmd failed due to a response CRC error, it was probably
765 * transient, so retry the cmd.
766 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
767 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
768 * illegal cmd, retry.
769 * Otherwise we don't understand what happened, so abort.
770 */
771static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +0900772 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100773{
774 bool prev_cmd_status_valid = true;
775 u32 status, stop_status = 0;
776 int err, retry;
777
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530778 if (mmc_card_removed(card))
779 return ERR_NOMEDIUM;
780
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100781 /*
782 * Try to get card status which indicates both the card state
783 * and why there was no response. If the first attempt fails,
784 * we can't be sure the returned status is for the r/w command.
785 */
786 for (retry = 2; retry >= 0; retry--) {
787 err = get_card_status(card, &status, 0);
788 if (!err)
789 break;
790
791 prev_cmd_status_valid = false;
792 pr_err("%s: error %d sending status command, %sing\n",
793 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
794 }
795
796 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530797 if (err) {
798 /* Check if the card is removed */
799 if (mmc_detect_card_removed(card->host))
800 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100801 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +0530802 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100803
Adrian Hunter67716322011-08-29 16:42:15 +0300804 /* Flag ECC errors */
805 if ((status & R1_CARD_ECC_FAILED) ||
806 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
807 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
808 *ecc_err = 1;
809
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +0900810 /* Flag General errors */
811 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
812 if ((status & R1_ERROR) ||
813 (brq->stop.resp[0] & R1_ERROR)) {
814 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
815 req->rq_disk->disk_name, __func__,
816 brq->stop.resp[0], status);
817 *gen_err = 1;
818 }
819
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100820 /*
821 * Check the current card state. If it is in some data transfer
822 * mode, tell it to stop (and hopefully transition back to TRAN.)
823 */
824 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
825 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
826 err = send_stop(card, &stop_status);
827 if (err)
828 pr_err("%s: error %d sending stop command\n",
829 req->rq_disk->disk_name, err);
830
831 /*
832 * If the stop cmd also timed out, the card is probably
833 * not present, so abort. Other errors are bad news too.
834 */
835 if (err)
836 return ERR_ABORT;
Adrian Hunter67716322011-08-29 16:42:15 +0300837 if (stop_status & R1_CARD_ECC_FAILED)
838 *ecc_err = 1;
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +0900839 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
840 if (stop_status & R1_ERROR) {
841 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
842 req->rq_disk->disk_name, __func__,
843 stop_status);
844 *gen_err = 1;
845 }
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100846 }
847
848 /* Check for set block count errors */
849 if (brq->sbc.error)
850 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
851 prev_cmd_status_valid, status);
852
853 /* Check for r/w command errors */
854 if (brq->cmd.error)
855 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
856 prev_cmd_status_valid, status);
857
Adrian Hunter67716322011-08-29 16:42:15 +0300858 /* Data errors */
859 if (!brq->stop.error)
860 return ERR_CONTINUE;
861
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +0100862 /* Now for stop errors. These aren't fatal to the transfer. */
863 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
864 req->rq_disk->disk_name, brq->stop.error,
865 brq->cmd.resp[0], status);
866
867 /*
868 * Subsitute in our own stop status as this will give the error
869 * state which happened during the execution of the r/w command.
870 */
871 if (stop_status) {
872 brq->stop.resp[0] = stop_status;
873 brq->stop.error = 0;
874 }
875 return ERR_CONTINUE;
876}
877
Adrian Hunter67716322011-08-29 16:42:15 +0300878static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
879 int type)
880{
881 int err;
882
883 if (md->reset_done & type)
884 return -EEXIST;
885
886 md->reset_done |= type;
887 err = mmc_hw_reset(host);
888 /* Ensure we switch back to the correct partition */
889 if (err != -EOPNOTSUPP) {
890 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
891 int part_err;
892
893 main_md->part_curr = main_md->part_type;
894 part_err = mmc_blk_part_switch(host->card, md);
895 if (part_err) {
896 /*
897 * We have failed to get back into the correct
898 * partition, so we need to abort the whole request.
899 */
900 return -ENODEV;
901 }
902 }
903 return err;
904}
905
906static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
907{
908 md->reset_done &= ~type;
909}
910
Chuanxiao Dong6186ada2014-08-12 12:01:30 +0800911int mmc_access_rpmb(struct mmc_queue *mq)
912{
913 struct mmc_blk_data *md = mq->data;
914 /*
915 * If this is a RPMB partition access, return ture
916 */
917 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
918 return true;
919
920 return false;
921}
922
Adrian Hunterbd788c92010-08-11 14:17:47 -0700923static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
924{
925 struct mmc_blk_data *md = mq->data;
926 struct mmc_card *card = md->queue.card;
927 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300928 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -0700929
Adrian Hunterbd788c92010-08-11 14:17:47 -0700930 if (!mmc_can_erase(card)) {
931 err = -EOPNOTSUPP;
932 goto out;
933 }
934
935 from = blk_rq_pos(req);
936 nr = blk_rq_sectors(req);
937
Kyungmin Parkb3bf9152011-10-18 09:34:04 +0900938 if (mmc_can_discard(card))
939 arg = MMC_DISCARD_ARG;
940 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -0700941 arg = MMC_TRIM_ARG;
942 else
943 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +0300944retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -0500945 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
946 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
947 INAND_CMD38_ARG_EXT_CSD,
948 arg == MMC_TRIM_ARG ?
949 INAND_CMD38_ARG_TRIM :
950 INAND_CMD38_ARG_ERASE,
951 0);
952 if (err)
953 goto out;
954 }
Adrian Hunterbd788c92010-08-11 14:17:47 -0700955 err = mmc_erase(card, from, nr, arg);
956out:
Adrian Hunter67716322011-08-29 16:42:15 +0300957 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
958 goto retry;
959 if (!err)
960 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +0530961 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -0700962
Adrian Hunterbd788c92010-08-11 14:17:47 -0700963 return err ? 0 : 1;
964}
965
Adrian Hunter49804542010-08-11 14:17:50 -0700966static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
967 struct request *req)
968{
969 struct mmc_blk_data *md = mq->data;
970 struct mmc_card *card = md->queue.card;
Adrian Hunter28302812012-04-05 14:45:48 +0300971 unsigned int from, nr, arg, trim_arg, erase_arg;
Adrian Hunter67716322011-08-29 16:42:15 +0300972 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -0700973
Kyungmin Parkd9ddd622011-10-14 14:15:48 +0900974 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -0700975 err = -EOPNOTSUPP;
976 goto out;
977 }
978
979 from = blk_rq_pos(req);
980 nr = blk_rq_sectors(req);
981
Adrian Hunter28302812012-04-05 14:45:48 +0300982 /* The sanitize operation is supported at v4.5 only */
983 if (mmc_can_sanitize(card)) {
984 erase_arg = MMC_ERASE_ARG;
985 trim_arg = MMC_TRIM_ARG;
986 } else {
987 erase_arg = MMC_SECURE_ERASE_ARG;
988 trim_arg = MMC_SECURE_TRIM1_ARG;
989 }
990
991 if (mmc_erase_group_aligned(card, from, nr))
992 arg = erase_arg;
993 else if (mmc_can_trim(card))
994 arg = trim_arg;
995 else {
996 err = -EINVAL;
997 goto out;
998 }
Adrian Hunter67716322011-08-29 16:42:15 +0300999retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001000 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1001 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1002 INAND_CMD38_ARG_EXT_CSD,
1003 arg == MMC_SECURE_TRIM1_ARG ?
1004 INAND_CMD38_ARG_SECTRIM1 :
1005 INAND_CMD38_ARG_SECERASE,
1006 0);
1007 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001008 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001009 }
Adrian Hunter28302812012-04-05 14:45:48 +03001010
Adrian Hunter49804542010-08-11 14:17:50 -07001011 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001012 if (err == -EIO)
1013 goto out_retry;
1014 if (err)
1015 goto out;
1016
1017 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001018 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1019 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1020 INAND_CMD38_ARG_EXT_CSD,
1021 INAND_CMD38_ARG_SECTRIM2,
1022 0);
1023 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001024 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001025 }
Adrian Hunter28302812012-04-05 14:45:48 +03001026
Adrian Hunter49804542010-08-11 14:17:50 -07001027 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001028 if (err == -EIO)
1029 goto out_retry;
1030 if (err)
1031 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001032 }
Adrian Hunter28302812012-04-05 14:45:48 +03001033
1034 if (mmc_can_sanitize(card))
1035 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1036 EXT_CSD_SANITIZE_START, 1, 0);
1037out_retry:
1038 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001039 goto retry;
1040 if (!err)
1041 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001042out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301043 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001044
Adrian Hunter49804542010-08-11 14:17:50 -07001045 return err ? 0 : 1;
1046}
1047
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001048static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1049{
1050 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001051 struct mmc_card *card = md->queue.card;
1052 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001053
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001054 ret = mmc_flush_cache(card);
1055 if (ret)
1056 ret = -EIO;
1057
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301058 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001059
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001060 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001061}
1062
1063/*
1064 * Reformat current write as a reliable write, supporting
1065 * both legacy and the enhanced reliable write MMC cards.
1066 * In each transfer we'll handle only as much as a single
1067 * reliable write can handle, thus finish the request in
1068 * partial completions.
1069 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001070static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1071 struct mmc_card *card,
1072 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001073{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001074 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1075 /* Legacy mode imposes restrictions on transfers. */
1076 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1077 brq->data.blocks = 1;
1078
1079 if (brq->data.blocks > card->ext_csd.rel_sectors)
1080 brq->data.blocks = card->ext_csd.rel_sectors;
1081 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1082 brq->data.blocks = 1;
1083 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001084}
1085
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001086#define CMD_ERRORS \
1087 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1088 R1_ADDRESS_ERROR | /* Misaligned address */ \
1089 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1090 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1091 R1_CC_ERROR | /* Card controller error */ \
1092 R1_ERROR) /* General/unknown error */
1093
Per Forlinee8a43a2011-07-01 18:55:33 +02001094static int mmc_blk_err_check(struct mmc_card *card,
1095 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001096{
Per Forlinee8a43a2011-07-01 18:55:33 +02001097 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1098 mmc_active);
1099 struct mmc_blk_request *brq = &mq_mrq->brq;
1100 struct request *req = mq_mrq->req;
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +09001101 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001102
1103 /*
1104 * sbc.error indicates a problem with the set block count
1105 * command. No data will have been transferred.
1106 *
1107 * cmd.error indicates a problem with the r/w command. No
1108 * data will have been transferred.
1109 *
1110 * stop.error indicates a problem with the stop command. Data
1111 * may have been transferred, or may still be transferring.
1112 */
Adrian Hunter67716322011-08-29 16:42:15 +03001113 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1114 brq->data.error) {
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +09001115 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001116 case ERR_RETRY:
1117 return MMC_BLK_RETRY;
1118 case ERR_ABORT:
1119 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301120 case ERR_NOMEDIUM:
1121 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001122 case ERR_CONTINUE:
1123 break;
1124 }
1125 }
1126
1127 /*
1128 * Check for errors relating to the execution of the
1129 * initial command - such as address errors. No data
1130 * has been transferred.
1131 */
1132 if (brq->cmd.resp[0] & CMD_ERRORS) {
1133 pr_err("%s: r/w command failed, status = %#x\n",
1134 req->rq_disk->disk_name, brq->cmd.resp[0]);
1135 return MMC_BLK_ABORT;
1136 }
1137
1138 /*
1139 * Everything else is either success, or a data error of some
1140 * kind. If it was a write, we may have transitioned to
1141 * program mode, which we have to wait for it to complete.
1142 */
1143 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1144 u32 status;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001145 unsigned long timeout;
1146
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +09001147 /* Check stop command response */
1148 if (brq->stop.resp[0] & R1_ERROR) {
1149 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1150 req->rq_disk->disk_name, __func__,
1151 brq->stop.resp[0]);
1152 gen_err = 1;
1153 }
1154
Trey Ramsay8fee4762012-11-16 09:31:41 -06001155 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
Per Forlind78d4a82011-07-01 18:55:30 +02001156 do {
1157 int err = get_card_status(card, &status, 5);
1158 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301159 pr_err("%s: error %d requesting status\n",
Per Forlind78d4a82011-07-01 18:55:30 +02001160 req->rq_disk->disk_name, err);
1161 return MMC_BLK_CMD_ERR;
1162 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06001163
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +09001164 if (status & R1_ERROR) {
1165 pr_err("%s: %s: general error sending status command, card status %#x\n",
1166 req->rq_disk->disk_name, __func__,
1167 status);
1168 gen_err = 1;
1169 }
1170
Trey Ramsay8fee4762012-11-16 09:31:41 -06001171 /* Timeout if the device never becomes ready for data
1172 * and never leaves the program state.
1173 */
1174 if (time_after(jiffies, timeout)) {
1175 pr_err("%s: Card stuck in programming state!"\
1176 " %s %s\n", mmc_hostname(card->host),
1177 req->rq_disk->disk_name, __func__);
1178
1179 return MMC_BLK_CMD_ERR;
1180 }
Per Forlind78d4a82011-07-01 18:55:30 +02001181 /*
1182 * Some cards mishandle the status bits,
1183 * so make sure to check both the busy
1184 * indication and the card state.
1185 */
1186 } while (!(status & R1_READY_FOR_DATA) ||
1187 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1188 }
1189
KOBAYASHI Yoshitake604ae792013-07-07 07:35:45 +09001190 /* if general error occurs, retry the write operation. */
1191 if (gen_err) {
1192 pr_warn("%s: retrying write for general error\n",
1193 req->rq_disk->disk_name);
1194 return MMC_BLK_RETRY;
1195 }
1196
Per Forlind78d4a82011-07-01 18:55:30 +02001197 if (brq->data.error) {
1198 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1199 req->rq_disk->disk_name, brq->data.error,
1200 (unsigned)blk_rq_pos(req),
1201 (unsigned)blk_rq_sectors(req),
1202 brq->cmd.resp[0], brq->stop.resp[0]);
1203
1204 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001205 if (ecc_err)
1206 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001207 return MMC_BLK_DATA_ERR;
1208 } else {
1209 return MMC_BLK_CMD_ERR;
1210 }
1211 }
1212
Adrian Hunter67716322011-08-29 16:42:15 +03001213 if (!brq->data.bytes_xfered)
1214 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02001215
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001216 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1217 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1218 return MMC_BLK_PARTIAL;
1219 else
1220 return MMC_BLK_SUCCESS;
1221 }
1222
Adrian Hunter67716322011-08-29 16:42:15 +03001223 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1224 return MMC_BLK_PARTIAL;
1225
1226 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02001227}
1228
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001229static int mmc_blk_packed_err_check(struct mmc_card *card,
1230 struct mmc_async_req *areq)
1231{
1232 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1233 mmc_active);
1234 struct request *req = mq_rq->req;
1235 struct mmc_packed *packed = mq_rq->packed;
1236 int err, check, status;
1237 u8 *ext_csd;
1238
1239 BUG_ON(!packed);
1240
1241 packed->retries--;
1242 check = mmc_blk_err_check(card, areq);
1243 err = get_card_status(card, &status, 0);
1244 if (err) {
1245 pr_err("%s: error %d sending status command\n",
1246 req->rq_disk->disk_name, err);
1247 return MMC_BLK_ABORT;
1248 }
1249
1250 if (status & R1_EXCEPTION_EVENT) {
1251 ext_csd = kzalloc(512, GFP_KERNEL);
1252 if (!ext_csd) {
1253 pr_err("%s: unable to allocate buffer for ext_csd\n",
1254 req->rq_disk->disk_name);
1255 return -ENOMEM;
1256 }
1257
1258 err = mmc_send_ext_csd(card, ext_csd);
1259 if (err) {
1260 pr_err("%s: error %d sending ext_csd\n",
1261 req->rq_disk->disk_name, err);
1262 check = MMC_BLK_ABORT;
1263 goto free;
1264 }
1265
1266 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1267 EXT_CSD_PACKED_FAILURE) &&
1268 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1269 EXT_CSD_PACKED_GENERIC_ERROR)) {
1270 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1271 EXT_CSD_PACKED_INDEXED_ERROR) {
1272 packed->idx_failure =
1273 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1274 check = MMC_BLK_PARTIAL;
1275 }
1276 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1277 "failure index: %d\n",
1278 req->rq_disk->disk_name, packed->nr_entries,
1279 packed->blocks, packed->idx_failure);
1280 }
1281free:
1282 kfree(ext_csd);
1283 }
1284
1285 return check;
1286}
1287
Per Forlin54d49d72011-07-01 18:55:29 +02001288static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1289 struct mmc_card *card,
1290 int disable_multi,
1291 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Per Forlin54d49d72011-07-01 18:55:29 +02001293 u32 readcmd, writecmd;
1294 struct mmc_blk_request *brq = &mqrq->brq;
1295 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05301297 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001299 /*
1300 * Reliable writes are used to implement Forced Unit Access and
1301 * REQ_META accesses, and are supported only on MMCs.
Christoph Hellwig65299a32011-08-23 14:50:29 +02001302 *
1303 * XXX: this really needs a good explanation of why REQ_META
1304 * is treated special.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001305 */
1306 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1307 (req->cmd_flags & REQ_META)) &&
1308 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001309 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001310
Per Forlin54d49d72011-07-01 18:55:29 +02001311 memset(brq, 0, sizeof(struct mmc_blk_request));
1312 brq->mrq.cmd = &brq->cmd;
1313 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Per Forlin54d49d72011-07-01 18:55:29 +02001315 brq->cmd.arg = blk_rq_pos(req);
1316 if (!mmc_card_blockaddr(card))
1317 brq->cmd.arg <<= 9;
1318 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1319 brq->data.blksz = 512;
1320 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1321 brq->stop.arg = 0;
1322 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1323 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Per Forlin54d49d72011-07-01 18:55:29 +02001325 /*
1326 * The block layer doesn't support all sector count
1327 * restrictions, so we need to be prepared for too big
1328 * requests.
1329 */
1330 if (brq->data.blocks > card->host->max_blk_count)
1331 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Paul Walmsley2bf22b32011-10-06 14:50:33 -06001333 if (brq->data.blocks > 1) {
1334 /*
1335 * After a read error, we redo the request one sector
1336 * at a time in order to accurately determine which
1337 * sectors can be read successfully.
1338 */
1339 if (disable_multi)
1340 brq->data.blocks = 1;
1341
1342 /* Some controllers can't do multiblock reads due to hw bugs */
1343 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1344 rq_data_dir(req) == READ)
1345 brq->data.blocks = 1;
1346 }
Per Forlin54d49d72011-07-01 18:55:29 +02001347
1348 if (brq->data.blocks > 1 || do_rel_wr) {
1349 /* SPI multiblock writes terminate using a special
1350 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02001351 */
Per Forlin54d49d72011-07-01 18:55:29 +02001352 if (!mmc_host_is_spi(card->host) ||
1353 rq_data_dir(req) == READ)
1354 brq->mrq.stop = &brq->stop;
1355 readcmd = MMC_READ_MULTIPLE_BLOCK;
1356 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1357 } else {
1358 brq->mrq.stop = NULL;
1359 readcmd = MMC_READ_SINGLE_BLOCK;
1360 writecmd = MMC_WRITE_BLOCK;
1361 }
1362 if (rq_data_dir(req) == READ) {
1363 brq->cmd.opcode = readcmd;
1364 brq->data.flags |= MMC_DATA_READ;
1365 } else {
1366 brq->cmd.opcode = writecmd;
1367 brq->data.flags |= MMC_DATA_WRITE;
1368 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02001369
Per Forlin54d49d72011-07-01 18:55:29 +02001370 if (do_rel_wr)
1371 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01001372
Per Forlin54d49d72011-07-01 18:55:29 +02001373 /*
Saugata Das42659002011-12-21 13:09:17 +05301374 * Data tag is used only during writing meta data to speed
1375 * up write and any subsequent read of this meta data
1376 */
1377 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1378 (req->cmd_flags & REQ_META) &&
1379 (rq_data_dir(req) == WRITE) &&
1380 ((brq->data.blocks * brq->data.blksz) >=
1381 card->ext_csd.data_tag_unit_size);
1382
1383 /*
Per Forlin54d49d72011-07-01 18:55:29 +02001384 * Pre-defined multi-block transfers are preferable to
1385 * open ended-ones (and necessary for reliable writes).
1386 * However, it is not sufficient to just send CMD23,
1387 * and avoid the final CMD12, as on an error condition
1388 * CMD12 (stop) needs to be sent anyway. This, coupled
1389 * with Auto-CMD23 enhancements provided by some
1390 * hosts, means that the complexity of dealing
1391 * with this is best left to the host. If CMD23 is
1392 * supported by card and host, we'll fill sbc in and let
1393 * the host deal with handling it correctly. This means
1394 * that for hosts that don't expose MMC_CAP_CMD23, no
1395 * change of behavior will be observed.
1396 *
1397 * N.B: Some MMC cards experience perf degradation.
1398 * We'll avoid using CMD23-bounded multiblock writes for
1399 * these, while retaining features like reliable writes.
1400 */
Saugata Das42659002011-12-21 13:09:17 +05301401 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1402 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1403 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02001404 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1405 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05301406 (do_rel_wr ? (1 << 31) : 0) |
1407 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02001408 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1409 brq->mrq.sbc = &brq->sbc;
1410 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001411
Per Forlin54d49d72011-07-01 18:55:29 +02001412 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001413
Per Forlin54d49d72011-07-01 18:55:29 +02001414 brq->data.sg = mqrq->sg;
1415 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001416
Per Forlin54d49d72011-07-01 18:55:29 +02001417 /*
1418 * Adjust the sg list so it is the same size as the
1419 * request.
1420 */
1421 if (brq->data.blocks != blk_rq_sectors(req)) {
1422 int i, data_size = brq->data.blocks << 9;
1423 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02001424
Per Forlin54d49d72011-07-01 18:55:29 +02001425 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1426 data_size -= sg->length;
1427 if (data_size <= 0) {
1428 sg->length += data_size;
1429 i++;
1430 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01001431 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001432 }
Per Forlin54d49d72011-07-01 18:55:29 +02001433 brq->data.sg_len = i;
1434 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01001435
Per Forlinee8a43a2011-07-01 18:55:33 +02001436 mqrq->mmc_active.mrq = &brq->mrq;
1437 mqrq->mmc_active.err_check = mmc_blk_err_check;
1438
Per Forlin54d49d72011-07-01 18:55:29 +02001439 mmc_queue_bounce_pre(mqrq);
1440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001442static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1443 struct mmc_card *card)
1444{
1445 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1446 unsigned int max_seg_sz = queue_max_segment_size(q);
1447 unsigned int len, nr_segs = 0;
1448
1449 do {
1450 len = min(hdr_sz, max_seg_sz);
1451 hdr_sz -= len;
1452 nr_segs++;
1453 } while (hdr_sz);
1454
1455 return nr_segs;
1456}
1457
1458static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1459{
1460 struct request_queue *q = mq->queue;
1461 struct mmc_card *card = mq->card;
1462 struct request *cur = req, *next = NULL;
1463 struct mmc_blk_data *md = mq->data;
1464 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1465 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1466 unsigned int req_sectors = 0, phys_segments = 0;
1467 unsigned int max_blk_count, max_phys_segs;
1468 bool put_back = true;
1469 u8 max_packed_rw = 0;
1470 u8 reqs = 0;
1471
1472 if (!(md->flags & MMC_BLK_PACKED_CMD))
1473 goto no_packed;
1474
1475 if ((rq_data_dir(cur) == WRITE) &&
1476 mmc_host_packed_wr(card->host))
1477 max_packed_rw = card->ext_csd.max_packed_writes;
1478
1479 if (max_packed_rw == 0)
1480 goto no_packed;
1481
1482 if (mmc_req_rel_wr(cur) &&
1483 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1484 goto no_packed;
1485
1486 if (mmc_large_sector(card) &&
1487 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1488 goto no_packed;
1489
1490 mmc_blk_clear_packed(mqrq);
1491
1492 max_blk_count = min(card->host->max_blk_count,
1493 card->host->max_req_size >> 9);
1494 if (unlikely(max_blk_count > 0xffff))
1495 max_blk_count = 0xffff;
1496
1497 max_phys_segs = queue_max_segments(q);
1498 req_sectors += blk_rq_sectors(cur);
1499 phys_segments += cur->nr_phys_segments;
1500
1501 if (rq_data_dir(cur) == WRITE) {
1502 req_sectors += mmc_large_sector(card) ? 8 : 1;
1503 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1504 }
1505
1506 do {
1507 if (reqs >= max_packed_rw - 1) {
1508 put_back = false;
1509 break;
1510 }
1511
1512 spin_lock_irq(q->queue_lock);
1513 next = blk_fetch_request(q);
1514 spin_unlock_irq(q->queue_lock);
1515 if (!next) {
1516 put_back = false;
1517 break;
1518 }
1519
1520 if (mmc_large_sector(card) &&
1521 !IS_ALIGNED(blk_rq_sectors(next), 8))
1522 break;
1523
1524 if (next->cmd_flags & REQ_DISCARD ||
1525 next->cmd_flags & REQ_FLUSH)
1526 break;
1527
1528 if (rq_data_dir(cur) != rq_data_dir(next))
1529 break;
1530
1531 if (mmc_req_rel_wr(next) &&
1532 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1533 break;
1534
1535 req_sectors += blk_rq_sectors(next);
1536 if (req_sectors > max_blk_count)
1537 break;
1538
1539 phys_segments += next->nr_phys_segments;
1540 if (phys_segments > max_phys_segs)
1541 break;
1542
1543 list_add_tail(&next->queuelist, &mqrq->packed->list);
1544 cur = next;
1545 reqs++;
1546 } while (1);
1547
1548 if (put_back) {
1549 spin_lock_irq(q->queue_lock);
1550 blk_requeue_request(q, next);
1551 spin_unlock_irq(q->queue_lock);
1552 }
1553
1554 if (reqs > 0) {
1555 list_add(&req->queuelist, &mqrq->packed->list);
1556 mqrq->packed->nr_entries = ++reqs;
1557 mqrq->packed->retries = reqs;
1558 return reqs;
1559 }
1560
1561no_packed:
1562 mqrq->cmd_type = MMC_PACKED_NONE;
1563 return 0;
1564}
1565
1566static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1567 struct mmc_card *card,
1568 struct mmc_queue *mq)
1569{
1570 struct mmc_blk_request *brq = &mqrq->brq;
1571 struct request *req = mqrq->req;
1572 struct request *prq;
1573 struct mmc_blk_data *md = mq->data;
1574 struct mmc_packed *packed = mqrq->packed;
1575 bool do_rel_wr, do_data_tag;
1576 u32 *packed_cmd_hdr;
1577 u8 hdr_blocks;
1578 u8 i = 1;
1579
1580 BUG_ON(!packed);
1581
1582 mqrq->cmd_type = MMC_PACKED_WRITE;
1583 packed->blocks = 0;
1584 packed->idx_failure = MMC_PACKED_NR_IDX;
1585
1586 packed_cmd_hdr = packed->cmd_hdr;
1587 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1588 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1589 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1590 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1591
1592 /*
1593 * Argument for each entry of packed group
1594 */
1595 list_for_each_entry(prq, &packed->list, queuelist) {
1596 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1597 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1598 (prq->cmd_flags & REQ_META) &&
1599 (rq_data_dir(prq) == WRITE) &&
1600 ((brq->data.blocks * brq->data.blksz) >=
1601 card->ext_csd.data_tag_unit_size);
1602 /* Argument of CMD23 */
1603 packed_cmd_hdr[(i * 2)] =
1604 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1605 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1606 blk_rq_sectors(prq);
1607 /* Argument of CMD18 or CMD25 */
1608 packed_cmd_hdr[((i * 2)) + 1] =
1609 mmc_card_blockaddr(card) ?
1610 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1611 packed->blocks += blk_rq_sectors(prq);
1612 i++;
1613 }
1614
1615 memset(brq, 0, sizeof(struct mmc_blk_request));
1616 brq->mrq.cmd = &brq->cmd;
1617 brq->mrq.data = &brq->data;
1618 brq->mrq.sbc = &brq->sbc;
1619 brq->mrq.stop = &brq->stop;
1620
1621 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1622 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1623 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1624
1625 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1626 brq->cmd.arg = blk_rq_pos(req);
1627 if (!mmc_card_blockaddr(card))
1628 brq->cmd.arg <<= 9;
1629 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1630
1631 brq->data.blksz = 512;
1632 brq->data.blocks = packed->blocks + hdr_blocks;
1633 brq->data.flags |= MMC_DATA_WRITE;
1634
1635 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1636 brq->stop.arg = 0;
1637 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1638
1639 mmc_set_data_timeout(&brq->data, card);
1640
1641 brq->data.sg = mqrq->sg;
1642 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1643
1644 mqrq->mmc_active.mrq = &brq->mrq;
1645 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1646
1647 mmc_queue_bounce_pre(mqrq);
1648}
1649
Adrian Hunter67716322011-08-29 16:42:15 +03001650static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1651 struct mmc_blk_request *brq, struct request *req,
1652 int ret)
1653{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001654 struct mmc_queue_req *mq_rq;
1655 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1656
Adrian Hunter67716322011-08-29 16:42:15 +03001657 /*
1658 * If this is an SD card and we're writing, we can first
1659 * mark the known good sectors as ok.
1660 *
1661 * If the card is not SD, we can still ok written sectors
1662 * as reported by the controller (which might be less than
1663 * the real number of written sectors, but never more).
1664 */
1665 if (mmc_card_sd(card)) {
1666 u32 blocks;
1667
1668 blocks = mmc_sd_num_wr_blocks(card);
1669 if (blocks != (u32)-1) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301670 ret = blk_end_request(req, 0, blocks << 9);
Adrian Hunter67716322011-08-29 16:42:15 +03001671 }
1672 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001673 if (!mmc_packed_cmd(mq_rq->cmd_type))
1674 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03001675 }
1676 return ret;
1677}
1678
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001679static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1680{
1681 struct request *prq;
1682 struct mmc_packed *packed = mq_rq->packed;
1683 int idx = packed->idx_failure, i = 0;
1684 int ret = 0;
1685
1686 BUG_ON(!packed);
1687
1688 while (!list_empty(&packed->list)) {
1689 prq = list_entry_rq(packed->list.next);
1690 if (idx == i) {
1691 /* retry from error index */
1692 packed->nr_entries -= idx;
1693 mq_rq->req = prq;
1694 ret = 1;
1695
1696 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1697 list_del_init(&prq->queuelist);
1698 mmc_blk_clear_packed(mq_rq);
1699 }
1700 return ret;
1701 }
1702 list_del_init(&prq->queuelist);
1703 blk_end_request(prq, 0, blk_rq_bytes(prq));
1704 i++;
1705 }
1706
1707 mmc_blk_clear_packed(mq_rq);
1708 return ret;
1709}
1710
1711static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1712{
1713 struct request *prq;
1714 struct mmc_packed *packed = mq_rq->packed;
1715
1716 BUG_ON(!packed);
1717
1718 while (!list_empty(&packed->list)) {
1719 prq = list_entry_rq(packed->list.next);
1720 list_del_init(&prq->queuelist);
1721 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1722 }
1723
1724 mmc_blk_clear_packed(mq_rq);
1725}
1726
1727static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1728 struct mmc_queue_req *mq_rq)
1729{
1730 struct request *prq;
1731 struct request_queue *q = mq->queue;
1732 struct mmc_packed *packed = mq_rq->packed;
1733
1734 BUG_ON(!packed);
1735
1736 while (!list_empty(&packed->list)) {
1737 prq = list_entry_rq(packed->list.prev);
1738 if (prq->queuelist.prev != &packed->list) {
1739 list_del_init(&prq->queuelist);
1740 spin_lock_irq(q->queue_lock);
1741 blk_requeue_request(mq->queue, prq);
1742 spin_unlock_irq(q->queue_lock);
1743 } else {
1744 list_del_init(&prq->queuelist);
1745 }
1746 }
1747
1748 mmc_blk_clear_packed(mq_rq);
1749}
1750
Per Forlinee8a43a2011-07-01 18:55:33 +02001751static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02001752{
1753 struct mmc_blk_data *md = mq->data;
1754 struct mmc_card *card = md->queue.card;
1755 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunter67716322011-08-29 16:42:15 +03001756 int ret = 1, disable_multi = 0, retry = 0, type;
Per Forlind78d4a82011-07-01 18:55:30 +02001757 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02001758 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301759 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02001760 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001761 const u8 packed_nr = 2;
1762 u8 reqs = 0;
Per Forlinee8a43a2011-07-01 18:55:33 +02001763
1764 if (!rqc && !mq->mqrq_prev->req)
1765 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02001766
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001767 if (rqc)
1768 reqs = mmc_blk_prep_packed_list(mq, rqc);
1769
Per Forlin54d49d72011-07-01 18:55:29 +02001770 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02001771 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05301772 /*
1773 * When 4KB native sector is enabled, only 8 blocks
1774 * multiple read or write is allowed
1775 */
1776 if ((brq->data.blocks & 0x07) &&
1777 (card->ext_csd.data_sector_size == 4096)) {
1778 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1779 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001780 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05301781 goto cmd_abort;
1782 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001783
1784 if (reqs >= packed_nr)
1785 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1786 card, mq);
1787 else
1788 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02001789 areq = &mq->mqrq_cur->mmc_active;
1790 } else
1791 areq = NULL;
1792 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001793 if (!areq) {
1794 if (status == MMC_BLK_NEW_REQUEST)
1795 mq->flags |= MMC_QUEUE_NEW_REQUEST;
Per Forlinee8a43a2011-07-01 18:55:33 +02001796 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001797 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02001798
Per Forlinee8a43a2011-07-01 18:55:33 +02001799 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1800 brq = &mq_rq->brq;
1801 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03001802 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02001803 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02001804
Per Forlind78d4a82011-07-01 18:55:30 +02001805 switch (status) {
1806 case MMC_BLK_SUCCESS:
1807 case MMC_BLK_PARTIAL:
1808 /*
1809 * A block was successfully transferred.
1810 */
Adrian Hunter67716322011-08-29 16:42:15 +03001811 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001812
1813 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1814 ret = mmc_blk_end_packed_req(mq_rq);
1815 break;
1816 } else {
1817 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02001818 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001819 }
1820
Adrian Hunter67716322011-08-29 16:42:15 +03001821 /*
1822 * If the blk_end_request function returns non-zero even
1823 * though all data has been transferred and no errors
1824 * were returned by the host controller, it's a bug.
1825 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001826 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301827 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02001828 __func__, blk_rq_bytes(req),
1829 brq->data.bytes_xfered);
1830 rqc = NULL;
1831 goto cmd_abort;
1832 }
Per Forlind78d4a82011-07-01 18:55:30 +02001833 break;
1834 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03001835 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1836 if (!mmc_blk_reset(md, card->host, type))
1837 break;
1838 goto cmd_abort;
Per Forlind78d4a82011-07-01 18:55:30 +02001839 case MMC_BLK_RETRY:
1840 if (retry++ < 5)
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001841 break;
Adrian Hunter67716322011-08-29 16:42:15 +03001842 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02001843 case MMC_BLK_ABORT:
Adrian Hunter67716322011-08-29 16:42:15 +03001844 if (!mmc_blk_reset(md, card->host, type))
1845 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001846 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03001847 case MMC_BLK_DATA_ERR: {
1848 int err;
1849
1850 err = mmc_blk_reset(md, card->host, type);
1851 if (!err)
1852 break;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001853 if (err == -ENODEV ||
1854 mmc_packed_cmd(mq_rq->cmd_type))
Adrian Hunter67716322011-08-29 16:42:15 +03001855 goto cmd_abort;
1856 /* Fall through */
1857 }
1858 case MMC_BLK_ECC_ERR:
1859 if (brq->data.blocks > 1) {
1860 /* Redo read one sector at a time */
1861 pr_warning("%s: retrying using single block read\n",
1862 req->rq_disk->disk_name);
1863 disable_multi = 1;
1864 break;
1865 }
Per Forlind78d4a82011-07-01 18:55:30 +02001866 /*
1867 * After an error, we redo I/O one sector at a
1868 * time, so we only reach here after trying to
1869 * read a single sector.
1870 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301871 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02001872 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02001873 if (!ret)
1874 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02001875 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301876 case MMC_BLK_NOMEDIUM:
1877 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001878 default:
1879 pr_err("%s: Unhandled return value (%d)",
1880 req->rq_disk->disk_name, status);
1881 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001882 }
1883
Per Forlinee8a43a2011-07-01 18:55:33 +02001884 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001885 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1886 if (!mq_rq->packed->retries)
1887 goto cmd_abort;
1888 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1889 mmc_start_req(card->host,
1890 &mq_rq->mmc_active, NULL);
1891 } else {
1892
1893 /*
1894 * In case of a incomplete request
1895 * prepare it again and resend.
1896 */
1897 mmc_blk_rw_rq_prep(mq_rq, card,
1898 disable_multi, mq);
1899 mmc_start_req(card->host,
1900 &mq_rq->mmc_active, NULL);
1901 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 } while (ret);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return 1;
1906
Russell King - ARM Linuxa01f3cc2011-06-20 20:10:28 +01001907 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001908 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1909 mmc_blk_abort_packed_req(mq_rq);
1910 } else {
1911 if (mmc_card_removed(card))
1912 req->cmd_flags |= REQ_QUIET;
1913 while (ret)
1914 ret = blk_end_request(req, -EIO,
1915 blk_rq_cur_bytes(req));
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Per Forlinee8a43a2011-07-01 18:55:33 +02001918 start_new_req:
1919 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09001920 if (mmc_card_removed(card)) {
1921 rqc->cmd_flags |= REQ_QUIET;
1922 blk_end_request_all(rqc, -EIO);
1923 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09001924 /*
1925 * If current request is packed, it needs to put back.
1926 */
1927 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1928 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1929
Seungwon Jeon7a819022013-01-22 19:48:07 +09001930 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1931 mmc_start_req(card->host,
1932 &mq->mqrq_cur->mmc_active, NULL);
1933 }
Per Forlinee8a43a2011-07-01 18:55:33 +02001934 }
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
1937}
1938
Adrian Hunterbd788c92010-08-11 14:17:47 -07001939static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1940{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001941 int ret;
1942 struct mmc_blk_data *md = mq->data;
1943 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001944 struct mmc_host *host = card->host;
1945 unsigned long flags;
Ray Jui1e063352013-10-26 11:03:44 -07001946 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001947
Per Forlinee8a43a2011-07-01 18:55:33 +02001948 if (req && !mq->mqrq_prev->req)
1949 /* claim host only for the first request */
1950 mmc_claim_host(card->host);
1951
Andrei Warkentin371a6892011-04-11 18:10:25 -05001952 ret = mmc_blk_part_switch(card, md);
1953 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001954 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301955 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001956 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001957 ret = 0;
1958 goto out;
1959 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001960
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001961 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Ray Jui1e063352013-10-26 11:03:44 -07001962 if (cmd_flags & REQ_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02001963 /* complete ongoing async transfer before issuing discard */
1964 if (card->host->areq)
1965 mmc_blk_issue_rw_rq(mq, NULL);
Ian Chen3550ccd2012-08-29 15:05:36 +09001966 if (req->cmd_flags & REQ_SECURE &&
1967 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001968 ret = mmc_blk_issue_secdiscard_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001969 else
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001970 ret = mmc_blk_issue_discard_rq(mq, req);
Ray Jui1e063352013-10-26 11:03:44 -07001971 } else if (cmd_flags & REQ_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09001972 /* complete ongoing async transfer before issuing flush */
1973 if (card->host->areq)
1974 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001975 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001976 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001977 if (!req && host->areq) {
1978 spin_lock_irqsave(&host->context_info.lock, flags);
1979 host->context_info.is_waiting_last_req = true;
1980 spin_unlock_irqrestore(&host->context_info.lock, flags);
1981 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001982 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07001983 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001984
Andrei Warkentin371a6892011-04-11 18:10:25 -05001985out:
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001986 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
Ray Jui1e063352013-10-26 11:03:44 -07001987 (cmd_flags & MMC_REQ_SPECIAL_MASK))
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09001988 /*
1989 * Release host when there are no more requests
1990 * and after special request(discard, flush) is done.
1991 * In case sepecial request, there is no reentry to
1992 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1993 */
Per Forlinee8a43a2011-07-01 18:55:33 +02001994 mmc_release_host(card->host);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05001995 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Russell Kinga6f6c962006-01-03 22:38:44 +00001998static inline int mmc_blk_readonly(struct mmc_card *card)
1999{
2000 return mmc_card_readonly(card) ||
2001 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2002}
2003
Andrei Warkentin371a6892011-04-11 18:10:25 -05002004static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2005 struct device *parent,
2006 sector_t size,
2007 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002008 const char *subname,
2009 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct mmc_blk_data *md;
2012 int devidx, ret;
2013
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002014 devidx = find_first_zero_bit(dev_use, max_devices);
2015 if (devidx >= max_devices)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return ERR_PTR(-ENOSPC);
2017 __set_bit(devidx, dev_use);
2018
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002019 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00002020 if (!md) {
2021 ret = -ENOMEM;
2022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Russell Kinga6f6c962006-01-03 22:38:44 +00002024
Russell Kinga6f6c962006-01-03 22:38:44 +00002025 /*
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002026 * !subname implies we are creating main mmc_blk_data that will be
2027 * associated with mmc_card with mmc_set_drvdata. Due to device
2028 * partitions, devidx will not coincide with a per-physical card
2029 * index anymore so we keep track of a name index.
2030 */
2031 if (!subname) {
2032 md->name_idx = find_first_zero_bit(name_use, max_devices);
2033 __set_bit(md->name_idx, name_use);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002034 } else
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002035 md->name_idx = ((struct mmc_blk_data *)
2036 dev_to_disk(parent)->private_data)->name_idx;
2037
Johan Rudholmadd710e2011-12-02 08:51:06 +01002038 md->area_type = area_type;
2039
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002040 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00002041 * Set the read-only status based on the supported commands
2042 * and the write protect switch.
2043 */
2044 md->read_only = mmc_blk_readonly(card);
2045
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002046 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00002047 if (md->disk == NULL) {
2048 ret = -ENOMEM;
2049 goto err_kfree;
2050 }
2051
2052 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002053 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00002054 md->usage = 1;
2055
Adrian Hunterd09408a2011-06-23 13:40:28 +03002056 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00002057 if (ret)
2058 goto err_putdisk;
2059
Russell Kinga6f6c962006-01-03 22:38:44 +00002060 md->queue.issue_fn = mmc_blk_issue_rq;
2061 md->queue.data = md;
2062
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002063 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002064 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00002065 md->disk->fops = &mmc_bdops;
2066 md->disk->private_data = md;
2067 md->disk->queue = md->queue.queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002068 md->disk->driverfs_dev = parent;
2069 set_disk_ro(md->disk, md->read_only || default_ro);
Loic Pallardy53d8f972012-08-06 17:12:28 +02002070 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2071 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00002072
2073 /*
2074 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2075 *
2076 * - be set for removable media with permanent block devices
2077 * - be unset for removable block devices with permanent media
2078 *
2079 * Since MMC block devices clearly fall under the second
2080 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2081 * should use the block device creation/destruction hotplug
2082 * messages to tell when the card is present.
2083 */
2084
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002085 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2086 "mmcblk%d%s", md->name_idx, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00002087
Saugata Dasa5075eb2012-05-17 16:32:21 +05302088 if (mmc_card_mmc(card))
2089 blk_queue_logical_block_size(md->queue.queue,
2090 card->ext_csd.data_sector_size);
2091 else
2092 blk_queue_logical_block_size(md->queue.queue, 512);
2093
Andrei Warkentin371a6892011-04-11 18:10:25 -05002094 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002095
Andrei Warkentinf0d89972011-05-23 15:06:38 -05002096 if (mmc_host_cmd23(card->host)) {
2097 if (mmc_card_mmc(card) ||
2098 (mmc_card_sd(card) &&
2099 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2100 md->flags |= MMC_BLK_CMD23;
2101 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002102
2103 if (mmc_card_mmc(card) &&
2104 md->flags & MMC_BLK_CMD23 &&
2105 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2106 card->ext_csd.rel_sectors)) {
2107 md->flags |= MMC_BLK_REL_WR;
2108 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2109 }
2110
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002111 if (mmc_card_mmc(card) &&
2112 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2113 (md->flags & MMC_BLK_CMD23) &&
2114 card->ext_csd.packed_event_en) {
2115 if (!mmc_packed_init(&md->queue, card))
2116 md->flags |= MMC_BLK_PACKED_CMD;
2117 }
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00002120
2121 err_putdisk:
2122 put_disk(md->disk);
2123 err_kfree:
2124 kfree(md);
2125 out:
2126 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Andrei Warkentin371a6892011-04-11 18:10:25 -05002129static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2130{
2131 sector_t size;
2132 struct mmc_blk_data *md;
2133
2134 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2135 /*
2136 * The EXT_CSD sector count is in number or 512 byte
2137 * sectors.
2138 */
2139 size = card->ext_csd.sectors;
2140 } else {
2141 /*
2142 * The CSD capacity field is in units of read_blkbits.
2143 * set_capacity takes units of 512 bytes.
2144 */
2145 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2146 }
2147
Johan Rudholmadd710e2011-12-02 08:51:06 +01002148 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2149 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002150 return md;
2151}
2152
2153static int mmc_blk_alloc_part(struct mmc_card *card,
2154 struct mmc_blk_data *md,
2155 unsigned int part_type,
2156 sector_t size,
2157 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002158 const char *subname,
2159 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05002160{
2161 char cap_str[10];
2162 struct mmc_blk_data *part_md;
2163
2164 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002165 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002166 if (IS_ERR(part_md))
2167 return PTR_ERR(part_md);
2168 part_md->part_type = part_type;
2169 list_add(&part_md->part, &md->part);
2170
2171 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2172 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302173 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05002174 part_md->disk->disk_name, mmc_card_id(card),
2175 mmc_card_name(card), part_md->part_type, cap_str);
2176 return 0;
2177}
2178
Namjae Jeone0c368d2011-10-06 23:41:38 +09002179/* MMC Physical partitions consist of two boot partitions and
2180 * up to four general purpose partitions.
2181 * For each partition enabled in EXT_CSD a block device will be allocatedi
2182 * to provide access to the partition.
2183 */
2184
Andrei Warkentin371a6892011-04-11 18:10:25 -05002185static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2186{
Namjae Jeone0c368d2011-10-06 23:41:38 +09002187 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002188
2189 if (!mmc_card_mmc(card))
2190 return 0;
2191
Namjae Jeone0c368d2011-10-06 23:41:38 +09002192 for (idx = 0; idx < card->nr_parts; idx++) {
2193 if (card->part[idx].size) {
2194 ret = mmc_blk_alloc_part(card, md,
2195 card->part[idx].part_cfg,
2196 card->part[idx].size >> 9,
2197 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01002198 card->part[idx].name,
2199 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09002200 if (ret)
2201 return ret;
2202 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002203 }
2204
2205 return ret;
2206}
2207
Andrei Warkentin371a6892011-04-11 18:10:25 -05002208static void mmc_blk_remove_req(struct mmc_blk_data *md)
2209{
Johan Rudholmadd710e2011-12-02 08:51:06 +01002210 struct mmc_card *card;
2211
Andrei Warkentin371a6892011-04-11 18:10:25 -05002212 if (md) {
Johan Rudholmadd710e2011-12-02 08:51:06 +01002213 card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002214 if (md->disk->flags & GENHD_FL_UP) {
2215 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002216 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2217 card->ext_csd.boot_ro_lockable)
2218 device_remove_file(disk_to_dev(md->disk),
2219 &md->power_ro_lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002220
2221 /* Stop new requests from getting into the queue */
2222 del_gendisk(md->disk);
2223 }
2224
2225 /* Then flush out any already in there */
2226 mmc_cleanup_queue(&md->queue);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002227 if (md->flags & MMC_BLK_PACKED_CMD)
2228 mmc_packed_clean(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002229 mmc_blk_put(md);
2230 }
2231}
2232
2233static void mmc_blk_remove_parts(struct mmc_card *card,
2234 struct mmc_blk_data *md)
2235{
2236 struct list_head *pos, *q;
2237 struct mmc_blk_data *part_md;
2238
Andrei Warkentinf06c9152011-04-21 22:46:13 -05002239 __clear_bit(md->name_idx, name_use);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002240 list_for_each_safe(pos, q, &md->part) {
2241 part_md = list_entry(pos, struct mmc_blk_data, part);
2242 list_del(pos);
2243 mmc_blk_remove_req(part_md);
2244 }
2245}
2246
2247static int mmc_add_disk(struct mmc_blk_data *md)
2248{
2249 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002250 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05002251
2252 add_disk(md->disk);
2253 md->force_ro.show = force_ro_show;
2254 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05302255 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002256 md->force_ro.attr.name = "force_ro";
2257 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2258 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2259 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01002260 goto force_ro_fail;
2261
2262 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2263 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04002264 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01002265
2266 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2267 mode = S_IRUGO;
2268 else
2269 mode = S_IRUGO | S_IWUSR;
2270
2271 md->power_ro_lock.show = power_ro_lock_show;
2272 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01002273 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01002274 md->power_ro_lock.attr.mode = mode;
2275 md->power_ro_lock.attr.name =
2276 "ro_lock_until_next_power_on";
2277 ret = device_create_file(disk_to_dev(md->disk),
2278 &md->power_ro_lock);
2279 if (ret)
2280 goto power_ro_lock_fail;
2281 }
2282 return ret;
2283
2284power_ro_lock_fail:
2285 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2286force_ro_fail:
2287 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002288
2289 return ret;
2290}
2291
Chris Ballc59d4472011-11-11 22:01:43 -05002292#define CID_MANFID_SANDISK 0x2
2293#define CID_MANFID_TOSHIBA 0x11
2294#define CID_MANFID_MICRON 0x13
Ian Chen3550ccd2012-08-29 15:05:36 +09002295#define CID_MANFID_SAMSUNG 0x15
Chris Ballc59d4472011-11-11 22:01:43 -05002296
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002297static const struct mmc_fixup blk_fixups[] =
2298{
Chris Ballc59d4472011-11-11 22:01:43 -05002299 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2300 MMC_QUIRK_INAND_CMD38),
2301 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2302 MMC_QUIRK_INAND_CMD38),
2303 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2304 MMC_QUIRK_INAND_CMD38),
2305 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2306 MMC_QUIRK_INAND_CMD38),
2307 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2308 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002309
2310 /*
2311 * Some MMC cards experience performance degradation with CMD23
2312 * instead of CMD12-bounded multiblock transfers. For now we'll
2313 * black list what's bad...
2314 * - Certain Toshiba cards.
2315 *
2316 * N.B. This doesn't affect SD cards.
2317 */
Chris Ballc59d4472011-11-11 22:01:43 -05002318 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002319 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002320 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002321 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05002322 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002323 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002324
2325 /*
2326 * Some Micron MMC cards needs longer data read timeout than
2327 * indicated in CSD.
2328 */
Chris Ballc59d4472011-11-11 22:01:43 -05002329 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002330 MMC_QUIRK_LONG_READ_TIME),
2331
Ian Chen3550ccd2012-08-29 15:05:36 +09002332 /*
2333 * On these Samsung MoviNAND parts, performing secure erase or
2334 * secure trim can result in unrecoverable corruption due to a
2335 * firmware bug.
2336 */
2337 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2338 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2339 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2340 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2341 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2342 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2343 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2344 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2345 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2346 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2347 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2348 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2349 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2350 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2351 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2352 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2353
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002354 END_FIXUP
2355};
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static int mmc_blk_probe(struct mmc_card *card)
2358{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002359 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002360 char cap_str[10];
2361
Pierre Ossman912490d2005-05-21 10:27:02 +01002362 /*
2363 * Check that the card supports the command class(es) we need.
2364 */
2365 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -ENODEV;
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 md = mmc_blk_alloc(card);
2369 if (IS_ERR(md))
2370 return PTR_ERR(md);
2371
Yi Li444122f2009-02-05 15:31:57 +08002372 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002373 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302374 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02002376 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Andrei Warkentin371a6892011-04-11 18:10:25 -05002378 if (mmc_blk_alloc_parts(card, md))
2379 goto out;
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 mmc_set_drvdata(card, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04002382 mmc_fixup_device(card, blk_fixups);
2383
Andrei Warkentin371a6892011-04-11 18:10:25 -05002384 if (mmc_add_disk(md))
2385 goto out;
2386
2387 list_for_each_entry(part_md, &md->part, part) {
2388 if (mmc_add_disk(part_md))
2389 goto out;
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return 0;
2392
2393 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05002394 mmc_blk_remove_parts(card, md);
2395 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
2399static void mmc_blk_remove(struct mmc_card *card)
2400{
2401 struct mmc_blk_data *md = mmc_get_drvdata(card);
2402
Andrei Warkentin371a6892011-04-11 18:10:25 -05002403 mmc_blk_remove_parts(card, md);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03002404 mmc_claim_host(card->host);
2405 mmc_blk_part_switch(card, md);
2406 mmc_release_host(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002407 mmc_blk_remove_req(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 mmc_set_drvdata(card, NULL);
2409}
2410
2411#ifdef CONFIG_PM
Chuanxiao Dong32d317c2012-04-11 19:54:38 +08002412static int mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002414 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 struct mmc_blk_data *md = mmc_get_drvdata(card);
2416
2417 if (md) {
2418 mmc_queue_suspend(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002419 list_for_each_entry(part_md, &md->part, part) {
2420 mmc_queue_suspend(&part_md->queue);
2421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 return 0;
2424}
2425
2426static int mmc_blk_resume(struct mmc_card *card)
2427{
Andrei Warkentin371a6892011-04-11 18:10:25 -05002428 struct mmc_blk_data *part_md;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct mmc_blk_data *md = mmc_get_drvdata(card);
2430
2431 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05002432 /*
2433 * Resume involves the card going into idle state,
2434 * so current partition is always the main one.
2435 */
2436 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05002438 list_for_each_entry(part_md, &md->part, part) {
2439 mmc_queue_resume(&part_md->queue);
2440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442 return 0;
2443}
2444#else
2445#define mmc_blk_suspend NULL
2446#define mmc_blk_resume NULL
2447#endif
2448
2449static struct mmc_driver mmc_driver = {
2450 .drv = {
2451 .name = "mmcblk",
2452 },
2453 .probe = mmc_blk_probe,
2454 .remove = mmc_blk_remove,
2455 .suspend = mmc_blk_suspend,
2456 .resume = mmc_blk_resume,
2457};
2458
2459static int __init mmc_blk_init(void)
2460{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002461 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Olof Johansson5e71b7a2010-09-17 21:19:57 -04002463 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2464 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2465
2466 max_devices = 256 / perdev_minors;
2467
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002468 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2469 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002472 res = mmc_register_driver(&mmc_driver);
2473 if (res)
2474 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09002476 return 0;
2477 out2:
2478 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 out:
2480 return res;
2481}
2482
2483static void __exit mmc_blk_exit(void)
2484{
2485 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02002486 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489module_init(mmc_blk_init);
2490module_exit(mmc_blk_exit);
2491
2492MODULE_LICENSE("GPL");
2493MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2494