blob: 9e429b036fd4b3d2b17f81cabb1de121455eb03b [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>
Sujit Reddy Thumma55291992014-12-09 20:40:16 +020033#include <linux/bitops.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020034#include <linux/string_helpers.h>
John Calixtocb87ea22011-04-26 18:56:29 -040035#include <linux/delay.h>
36#include <linux/capability.h>
37#include <linux/compat.h>
Ulf Hanssone94cfef2013-05-02 14:02:38 +020038#include <linux/pm_runtime.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020039#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
John Calixtocb87ea22011-04-26 18:56:29 -040041#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020043#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010044#include <linux/mmc/mmc.h>
45#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
48
Pierre Ossman98ac2162006-12-23 20:03:02 +010049#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080050#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000052MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040053#ifdef MODULE_PARAM_PREFIX
54#undef MODULE_PARAM_PREFIX
55#endif
56#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010057
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050058#define INAND_CMD38_ARG_EXT_CSD 113
59#define INAND_CMD38_ARG_ERASE 0x00
60#define INAND_CMD38_ARG_TRIM 0x01
61#define INAND_CMD38_ARG_SECERASE 0x80
62#define INAND_CMD38_ARG_SECTRIM1 0x81
63#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020064#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030065#define MMC_SANITIZE_REQ_TIMEOUT 240000
66#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050067
Luca Porziod3df0462015-11-06 15:12:26 +000068#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090069 (rq_data_dir(req) == WRITE))
70#define PACKED_CMD_VER 0x01
71#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030072#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090073
Maya Erezf93ca0a2014-12-09 23:34:41 +020074#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020075#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
76 do { \
77 if (stats->enabled) \
78 stats->pack_stop_reason[reason]++; \
79 } while (0)
80
Lee Susman841fd132013-04-23 17:59:26 +030081#define PCKD_TRGR_INIT_MEAN_POTEN 17
82#define PCKD_TRGR_POTEN_LOWER_BOUND 5
83#define PCKD_TRGR_URGENT_PENALTY 2
84#define PCKD_TRGR_LOWER_BOUND 5
85#define PCKD_TRGR_PRECISION_MULTIPLIER 100
86
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020087static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040088
89/*
90 * The defaults come from config options but can be overriden by module
91 * or bootarg options.
92 */
93static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
94
95/*
96 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +000097 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +020098 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -040099 */
100static int max_devices;
101
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000102#define MAX_DEVICES 256
103
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200104static DEFINE_IDA(mmc_blk_ida);
105static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * There is one mmc_blk_data per slot.
109 */
110struct mmc_blk_data {
111 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700112 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct gendisk *disk;
114 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500115 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500117 unsigned int flags;
118#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
119#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900120#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000123 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500124 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300125 unsigned int reset_done;
126#define MMC_BLK_READ BIT(0)
127#define MMC_BLK_WRITE BIT(1)
128#define MMC_BLK_DISCARD BIT(2)
129#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200130#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500131
132 /*
133 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200134 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500135 * track of the current selected device partition.
136 */
137 unsigned int part_curr;
138 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100139 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200140 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200141 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100142 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Arjan van de Vena621aae2006-01-12 18:43:35 +0000145static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900147enum {
148 MMC_PACKED_NR_IDX = -1,
149 MMC_PACKED_NR_ZERO,
150 MMC_PACKED_NR_SINGLE,
151};
152
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400153module_param(perdev_minors, int, 0444);
154MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
155
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200156static inline int mmc_blk_part_switch(struct mmc_card *card,
157 struct mmc_blk_data *md);
158static int get_card_status(struct mmc_card *card, u32 *status, int retries);
159
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900160static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
161{
162 struct mmc_packed *packed = mqrq->packed;
163
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900164 mqrq->cmd_type = MMC_PACKED_NONE;
165 packed->nr_entries = MMC_PACKED_NR_ZERO;
166 packed->idx_failure = MMC_PACKED_NR_IDX;
167 packed->retries = 0;
168 packed->blocks = 0;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
172{
173 struct mmc_blk_data *md;
174
Arjan van de Vena621aae2006-01-12 18:43:35 +0000175 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 md = disk->private_data;
177 if (md && md->usage == 0)
178 md = NULL;
179 if (md)
180 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000181 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 return md;
184}
185
Andrei Warkentin371a6892011-04-11 18:10:25 -0500186static inline int mmc_get_devidx(struct gendisk *disk)
187{
Colin Cross382c55f2015-10-22 10:00:41 -0700188 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500189 return devidx;
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void mmc_blk_put(struct mmc_blk_data *md)
193{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000194 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 md->usage--;
196 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500197 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800198 blk_cleanup_queue(md->queue.queue);
199
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200200 spin_lock(&mmc_blk_lock);
201 ida_remove(&mmc_blk_ida, devidx);
202 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(md);
206 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000207 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Johan Rudholmadd710e2011-12-02 08:51:06 +0100210static ssize_t power_ro_lock_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
212{
213 int ret;
214 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200215 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100216 int locked = 0;
217
Asutosh Das507d9a72014-12-09 10:15:53 +0200218 if (!md)
219 return -EINVAL;
220
221 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100222 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
223 locked = 2;
224 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
225 locked = 1;
226
227 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
228
Tomas Winkler9098f842015-07-16 15:50:45 +0200229 mmc_blk_put(md);
230
Johan Rudholmadd710e2011-12-02 08:51:06 +0100231 return ret;
232}
233
234static ssize_t power_ro_lock_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
236{
237 int ret;
238 struct mmc_blk_data *md, *part_md;
239 struct mmc_card *card;
240 unsigned long set;
241
242 if (kstrtoul(buf, 0, &set))
243 return -EINVAL;
244
245 if (set != 1)
246 return count;
247
248 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200249 if (!md)
250 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100251 card = md->queue.card;
252
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200253 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100254
255 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
256 card->ext_csd.boot_ro_lock |
257 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
258 card->ext_csd.part_time);
259 if (ret)
260 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
261 else
262 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
263
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200264 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100265
266 if (!ret) {
267 pr_info("%s: Locking boot partition ro until next power on\n",
268 md->disk->disk_name);
269 set_disk_ro(md->disk, 1);
270
271 list_for_each_entry(part_md, &md->part, part)
272 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
273 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
274 set_disk_ro(part_md->disk, 1);
275 }
276 }
277
278 mmc_blk_put(md);
279 return count;
280}
281
Andrei Warkentin371a6892011-04-11 18:10:25 -0500282static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
283 char *buf)
284{
285 int ret;
286 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
287
Asutosh Das507d9a72014-12-09 10:15:53 +0200288 if (!md)
289 return -EINVAL;
290
Baruch Siach0031a982014-09-22 10:12:51 +0300291 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500292 get_disk_ro(dev_to_disk(dev)) ^
293 md->read_only);
294 mmc_blk_put(md);
295 return ret;
296}
297
298static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
299 const char *buf, size_t count)
300{
301 int ret;
302 char *end;
303 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
304 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200305
306 if (!md)
307 return -EINVAL;
308
Andrei Warkentin371a6892011-04-11 18:10:25 -0500309 if (end == buf) {
310 ret = -EINVAL;
311 goto out;
312 }
313
314 set_disk_ro(dev_to_disk(dev), set || md->read_only);
315 ret = count;
316out:
317 mmc_blk_put(md);
318 return ret;
319}
320
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200321static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200322no_pack_for_random_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
326 int ret;
327
Asutosh Das507d9a72014-12-09 10:15:53 +0200328 if (!md)
329 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200330 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
331
332 mmc_blk_put(md);
333 return ret;
334}
335
336static ssize_t
337no_pack_for_random_store(struct device *dev,
338 struct device_attribute *attr,
339 const char *buf, size_t count)
340{
341 int value;
342 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200343 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200344 int ret = count;
345
Asutosh Das507d9a72014-12-09 10:15:53 +0200346 if (!md)
347 return -EINVAL;
348
349 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200350 if (!card) {
351 ret = -EINVAL;
352 goto exit;
353 }
354
355 sscanf(buf, "%d", &value);
356
357 if (value < 0) {
358 pr_err("%s: value %d is not valid. old value remains = %d",
359 mmc_hostname(card->host), value,
360 md->queue.no_pack_for_random);
361 ret = -EINVAL;
362 goto exit;
363 }
364
365 md->queue.no_pack_for_random = (value > 0) ? true : false;
366
367 pr_debug("%s: no_pack_for_random: new value = %d",
368 mmc_hostname(card->host),
369 md->queue.no_pack_for_random);
370
371exit:
372 mmc_blk_put(md);
373 return ret;
374}
375
376static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200377num_wr_reqs_to_start_packing_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
380 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
381 int num_wr_reqs_to_start_packing;
382 int ret;
383
Asutosh Das507d9a72014-12-09 10:15:53 +0200384 if (!md)
385 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200386 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
387
388 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
389
390 mmc_blk_put(md);
391 return ret;
392}
393
394static ssize_t
395num_wr_reqs_to_start_packing_store(struct device *dev,
396 struct device_attribute *attr,
397 const char *buf, size_t count)
398{
399 int value;
400 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200401 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200402 int ret = count;
403
Asutosh Das507d9a72014-12-09 10:15:53 +0200404 if (!md)
405 return -EINVAL;
406
407 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200408 if (!card) {
409 ret = -EINVAL;
410 goto exit;
411 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200412
413 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200414
Yaniv Gardi42399822014-12-04 00:26:23 +0200415 if (value >= 0) {
416 md->queue.num_wr_reqs_to_start_packing =
417 min_t(int, value, (int)card->ext_csd.max_packed_writes);
418
419 pr_debug("%s: trigger to pack: new value = %d",
420 mmc_hostname(card->host),
421 md->queue.num_wr_reqs_to_start_packing);
422 } else {
423 pr_err("%s: value %d is not valid. old value remains = %d",
424 mmc_hostname(card->host), value,
425 md->queue.num_wr_reqs_to_start_packing);
426 ret = -EINVAL;
427 }
428
429exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200430 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200431 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200432}
433
Mark Salyzyn6904e432016-01-28 11:12:25 -0800434#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
435
436static int max_read_speed, max_write_speed, cache_size = 4;
437
438module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
439MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
440module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
441MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
442module_param(cache_size, int, S_IRUSR | S_IRGRP);
443MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
444
445/*
446 * helper macros and expectations:
447 * size - unsigned long number of bytes
448 * jiffies - unsigned long HZ timestamp difference
449 * speed - unsigned KB/s transfer rate
450 */
451#define size_and_speed_to_jiffies(size, speed) \
452 ((size) * HZ / (speed) / 1024UL)
453#define jiffies_and_speed_to_size(jiffies, speed) \
454 (((speed) * (jiffies) * 1024UL) / HZ)
455#define jiffies_and_size_to_speed(jiffies, size) \
456 ((size) * HZ / (jiffies) / 1024UL)
457
458/* Limits to report warning */
459/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
460#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
461#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
462
463#define speed_valid(speed) ((speed) > 0)
464
465static const char off[] = "off\n";
466
467static int max_speed_show(int speed, char *buf)
468{
469 if (speed)
470 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
471 else
472 return scnprintf(buf, PAGE_SIZE, off);
473}
474
475static int max_speed_store(const char *buf, struct request_queue *q)
476{
477 unsigned int limit, set = 0;
478
479 if (!strncasecmp(off, buf, sizeof(off) - 2))
480 return set;
481 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
482 return -EINVAL;
483 if (set == 0)
484 return set;
485 limit = MAX_SPEED(q);
486 if (set > limit)
487 pr_warn("max speed %u ineffective above %u\n", set, limit);
488 limit = MIN_SPEED(q);
489 if (set < limit)
490 pr_warn("max speed %u painful below %u\n", set, limit);
491 return set;
492}
493
494static ssize_t max_write_speed_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
497 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
498 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
499
500 mmc_blk_put(md);
501 return ret;
502}
503
504static ssize_t max_write_speed_store(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
509 int set = max_speed_store(buf, md->queue.queue);
510
511 if (set < 0) {
512 mmc_blk_put(md);
513 return set;
514 }
515
516 atomic_set(&md->queue.max_write_speed, set);
517 mmc_blk_put(md);
518 return count;
519}
520
521static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
522 max_write_speed_show, max_write_speed_store);
523
524static ssize_t max_read_speed_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
528 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
529
530 mmc_blk_put(md);
531 return ret;
532}
533
534static ssize_t max_read_speed_store(struct device *dev,
535 struct device_attribute *attr,
536 const char *buf, size_t count)
537{
538 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
539 int set = max_speed_store(buf, md->queue.queue);
540
541 if (set < 0) {
542 mmc_blk_put(md);
543 return set;
544 }
545
546 atomic_set(&md->queue.max_read_speed, set);
547 mmc_blk_put(md);
548 return count;
549}
550
551static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
552 max_read_speed_show, max_read_speed_store);
553
554static ssize_t cache_size_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
558 struct mmc_queue *mq = &md->queue;
559 int cache_size = atomic_read(&mq->cache_size);
560 int ret;
561
562 if (!cache_size)
563 ret = scnprintf(buf, PAGE_SIZE, off);
564 else {
565 int speed = atomic_read(&mq->max_write_speed);
566
567 if (!speed_valid(speed))
568 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
569 else { /* We accept race between cache_jiffies and cache_used */
570 unsigned long size = jiffies_and_speed_to_size(
571 jiffies - mq->cache_jiffies, speed);
572 long used = atomic_long_read(&mq->cache_used);
573
574 if (size >= used)
575 size = 0;
576 else
577 size = (used - size) * 100 / cache_size
578 / 1024UL / 1024UL;
579
580 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
581 cache_size, size);
582 }
583 }
584
585 mmc_blk_put(md);
586 return ret;
587}
588
589static ssize_t cache_size_store(struct device *dev,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
592{
593 struct mmc_blk_data *md;
594 unsigned int set = 0;
595
596 if (strncasecmp(off, buf, sizeof(off) - 2)
597 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
598 return -EINVAL;
599
600 md = mmc_blk_get(dev_to_disk(dev));
601 atomic_set(&md->queue.cache_size, set);
602 mmc_blk_put(md);
603 return count;
604}
605
606static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
607 cache_size_show, cache_size_store);
608
609/* correct for write-back */
610static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
611{
612 long used = 0;
613 int speed = atomic_read(&mq->max_write_speed);
614
615 if (speed_valid(speed)) {
616 unsigned long size = jiffies_and_speed_to_size(
617 waitfor - mq->cache_jiffies, speed);
618 used = atomic_long_read(&mq->cache_used);
619
620 if (size >= used)
621 used = 0;
622 else
623 used -= size;
624 }
625
626 atomic_long_set(&mq->cache_used, used);
627 mq->cache_jiffies = waitfor;
628
629 return used;
630}
631
632static void mmc_blk_simulate_delay(
633 struct mmc_queue *mq,
634 struct request *req,
635 unsigned long waitfor)
636{
637 int max_speed;
638
639 if (!req)
640 return;
641
642 max_speed = (rq_data_dir(req) == READ)
643 ? atomic_read(&mq->max_read_speed)
644 : atomic_read(&mq->max_write_speed);
645 if (speed_valid(max_speed)) {
646 unsigned long bytes = blk_rq_bytes(req);
647
648 if (rq_data_dir(req) != READ) {
649 int cache_size = atomic_read(&mq->cache_size);
650
651 if (cache_size) {
652 unsigned long size = cache_size * 1024L * 1024L;
653 long used = mmc_blk_cache_used(mq, waitfor);
654
655 used += bytes;
656 atomic_long_set(&mq->cache_used, used);
657 bytes = 0;
658 if (used > size)
659 bytes = used - size;
660 }
661 }
662 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
663 if (time_is_after_jiffies(waitfor)) {
664 long msecs = jiffies_to_msecs(waitfor - jiffies);
665
666 if (likely(msecs > 0))
667 msleep(msecs);
668 }
669 }
670}
671
672#else
673
674#define mmc_blk_simulate_delay(mq, req, waitfor)
675
676#endif
677
Al Viroa5a15612008-03-02 10:33:30 -0500678static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Al Viroa5a15612008-03-02 10:33:30 -0500680 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret = -ENXIO;
682
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200683 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (md) {
685 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500686 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700688
Al Viroa5a15612008-03-02 10:33:30 -0500689 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700690 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700691 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200694 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return ret;
697}
698
Al Virodb2a1442013-05-05 21:52:57 -0400699static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Al Viroa5a15612008-03-02 10:33:30 -0500701 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200703 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200705 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800709mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800711 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
712 geo->heads = 4;
713 geo->sectors = 16;
714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
John Calixtocb87ea22011-04-26 18:56:29 -0400717struct mmc_blk_ioc_data {
718 struct mmc_ioc_cmd ic;
719 unsigned char *buf;
720 u64 buf_bytes;
721};
722
723static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
724 struct mmc_ioc_cmd __user *user)
725{
726 struct mmc_blk_ioc_data *idata;
727 int err;
728
yalin wang1ff89502015-11-12 19:27:11 +0800729 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400730 if (!idata) {
731 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400732 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400733 }
734
735 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
736 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400737 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400738 }
739
740 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
741 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
742 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400743 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400744 }
745
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300746 if (!idata->buf_bytes) {
747 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100748 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300749 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100750
yalin wang1ff89502015-11-12 19:27:11 +0800751 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400752 if (!idata->buf) {
753 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400754 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400755 }
756
757 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
758 idata->ic.data_ptr, idata->buf_bytes)) {
759 err = -EFAULT;
760 goto copy_err;
761 }
762
763 return idata;
764
765copy_err:
766 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400767idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400768 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400769out:
John Calixtocb87ea22011-04-26 18:56:29 -0400770 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400771}
772
Jon Huntera5f57742015-09-22 10:27:53 +0100773static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
774 struct mmc_blk_ioc_data *idata)
775{
776 struct mmc_ioc_cmd *ic = &idata->ic;
777
778 if (copy_to_user(&(ic_ptr->response), ic->response,
779 sizeof(ic->response)))
780 return -EFAULT;
781
782 if (!idata->ic.write_flag) {
783 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
784 idata->buf, idata->buf_bytes))
785 return -EFAULT;
786 }
787
788 return 0;
789}
790
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200791static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
792 u32 retries_max)
793{
794 int err;
795 u32 retry_count = 0;
796
797 if (!status || !retries_max)
798 return -EINVAL;
799
800 do {
801 err = get_card_status(card, status, 5);
802 if (err)
803 break;
804
805 if (!R1_STATUS(*status) &&
806 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
807 break; /* RPMB programming operation complete */
808
809 /*
810 * Rechedule to give the MMC device a chance to continue
811 * processing the previous command without being polled too
812 * frequently.
813 */
814 usleep_range(1000, 5000);
815 } while (++retry_count < retries_max);
816
817 if (retry_count == retries_max)
818 err = -EPERM;
819
820 return err;
821}
822
Maya Erez775a9362013-04-18 15:41:55 +0300823static int ioctl_do_sanitize(struct mmc_card *card)
824{
825 int err;
826
Ulf Hanssona2d10862013-12-16 14:37:26 +0100827 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300828 pr_warn("%s: %s - SANITIZE is not supported\n",
829 mmc_hostname(card->host), __func__);
830 err = -EOPNOTSUPP;
831 goto out;
832 }
833
834 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
835 mmc_hostname(card->host), __func__);
836
837 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
838 EXT_CSD_SANITIZE_START, 1,
839 MMC_SANITIZE_REQ_TIMEOUT);
840
841 if (err)
842 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
843 mmc_hostname(card->host), __func__, err);
844
845 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
846 __func__);
847out:
848 return err;
849}
850
Jon Huntera5f57742015-09-22 10:27:53 +0100851static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
852 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400853{
John Calixtocb87ea22011-04-26 18:56:29 -0400854 struct mmc_command cmd = {0};
855 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530856 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400857 struct scatterlist sg;
858 int err;
859
Jon Huntera5f57742015-09-22 10:27:53 +0100860 if (!card || !md || !idata)
861 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400862
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100863 cmd.opcode = idata->ic.opcode;
864 cmd.arg = idata->ic.arg;
865 cmd.flags = idata->ic.flags;
866
867 if (idata->buf_bytes) {
868 data.sg = &sg;
869 data.sg_len = 1;
870 data.blksz = idata->ic.blksz;
871 data.blocks = idata->ic.blocks;
872
873 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
874
875 if (idata->ic.write_flag)
876 data.flags = MMC_DATA_WRITE;
877 else
878 data.flags = MMC_DATA_READ;
879
880 /* data.flags must already be set before doing this. */
881 mmc_set_data_timeout(&data, card);
882
883 /* Allow overriding the timeout_ns for empirical tuning. */
884 if (idata->ic.data_timeout_ns)
885 data.timeout_ns = idata->ic.data_timeout_ns;
886
887 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
888 /*
889 * Pretend this is a data transfer and rely on the
890 * host driver to compute timeout. When all host
891 * drivers support cmd.cmd_timeout for R1B, this
892 * can be changed to:
893 *
894 * mrq.data = NULL;
895 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
896 */
897 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
898 }
899
900 mrq.data = &data;
901 }
902
903 mrq.cmd = &cmd;
904
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200905 err = mmc_blk_part_switch(card, md);
906 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100907 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200908
John Calixtocb87ea22011-04-26 18:56:29 -0400909 if (idata->ic.is_acmd) {
910 err = mmc_app_cmd(card->host, card);
911 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100912 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400913 }
914
Yaniv Gardia82e4842013-06-05 14:13:08 +0300915 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
916 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300917 err = ioctl_do_sanitize(card);
918
919 if (err)
920 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
921 __func__, err);
922
Jon Huntera5f57742015-09-22 10:27:53 +0100923 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300924 }
925
John Calixtocb87ea22011-04-26 18:56:29 -0400926 mmc_wait_for_req(card->host, &mrq);
927
928 if (cmd.error) {
929 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
930 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100931 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400932 }
933 if (data.error) {
934 dev_err(mmc_dev(card->host), "%s: data error %d\n",
935 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100936 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400937 }
938
939 /*
940 * According to the SD specs, some commands require a delay after
941 * issuing the command.
942 */
943 if (idata->ic.postsleep_min_us)
944 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
945
Jon Huntera5f57742015-09-22 10:27:53 +0100946 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400947
Krishna Kondae6711632014-12-04 15:20:57 +0200948 return err;
949}
950
951struct mmc_blk_ioc_rpmb_data {
952 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
953};
954
955static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
956 struct mmc_ioc_rpmb __user *user)
957{
958 struct mmc_blk_ioc_rpmb_data *idata;
959 int err, i;
960
961 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
962 if (!idata) {
963 err = -ENOMEM;
964 goto out;
965 }
966
967 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
968 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
969 if (IS_ERR(idata->data[i])) {
970 err = PTR_ERR(idata->data[i]);
971 goto copy_err;
972 }
973 }
974
975 return idata;
976
977copy_err:
978 while (--i >= 0) {
979 kfree(idata->data[i]->buf);
980 kfree(idata->data[i]);
981 }
982 kfree(idata);
983out:
984 return ERR_PTR(err);
985}
986
987static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
988 struct mmc_ioc_rpmb __user *ic_ptr)
989{
990 struct mmc_blk_ioc_rpmb_data *idata;
991 struct mmc_blk_data *md;
992 struct mmc_card *card;
993 struct mmc_command cmd = {0};
994 struct mmc_data data = {0};
995 struct mmc_request mrq = {NULL};
996 struct scatterlist sg;
997 int err = 0, i = 0;
998 u32 status = 0;
999
1000 /* The caller must have CAP_SYS_RAWIO */
1001 if (!capable(CAP_SYS_RAWIO))
1002 return -EPERM;
1003
1004 md = mmc_blk_get(bdev->bd_disk);
1005 /* make sure this is a rpmb partition */
1006 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1007 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001008 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001009 }
1010
1011 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1012 if (IS_ERR(idata)) {
1013 err = PTR_ERR(idata);
1014 goto cmd_done;
1015 }
1016
1017 card = md->queue.card;
1018 if (IS_ERR(card)) {
1019 err = PTR_ERR(card);
1020 goto idata_free;
1021 }
1022
Maya Erezdd669562015-02-12 20:37:31 +02001023 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001024
1025 err = mmc_blk_part_switch(card, md);
1026 if (err)
1027 goto cmd_rel_host;
1028
1029 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1030 struct mmc_blk_ioc_data *curr_data;
1031 struct mmc_ioc_cmd *curr_cmd;
1032
1033 curr_data = idata->data[i];
1034 curr_cmd = &curr_data->ic;
1035 if (!curr_cmd->opcode)
1036 break;
1037
1038 cmd.opcode = curr_cmd->opcode;
1039 cmd.arg = curr_cmd->arg;
1040 cmd.flags = curr_cmd->flags;
1041
1042 if (curr_data->buf_bytes) {
1043 data.sg = &sg;
1044 data.sg_len = 1;
1045 data.blksz = curr_cmd->blksz;
1046 data.blocks = curr_cmd->blocks;
1047
1048 sg_init_one(data.sg, curr_data->buf,
1049 curr_data->buf_bytes);
1050
1051 if (curr_cmd->write_flag)
1052 data.flags = MMC_DATA_WRITE;
1053 else
1054 data.flags = MMC_DATA_READ;
1055
1056 /* data.flags must already be set before doing this. */
1057 mmc_set_data_timeout(&data, card);
1058
1059 /*
1060 * Allow overriding the timeout_ns for empirical tuning.
1061 */
1062 if (curr_cmd->data_timeout_ns)
1063 data.timeout_ns = curr_cmd->data_timeout_ns;
1064
1065 mrq.data = &data;
1066 }
1067
1068 mrq.cmd = &cmd;
1069
1070 err = mmc_set_blockcount(card, data.blocks,
1071 curr_cmd->write_flag & (1 << 31));
1072 if (err)
1073 goto cmd_rel_host;
1074
1075 mmc_wait_for_req(card->host, &mrq);
1076
1077 if (cmd.error) {
1078 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1079 __func__, cmd.error);
1080 err = cmd.error;
1081 goto cmd_rel_host;
1082 }
1083 if (data.error) {
1084 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1085 __func__, data.error);
1086 err = data.error;
1087 goto cmd_rel_host;
1088 }
1089
1090 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1091 sizeof(cmd.resp))) {
1092 err = -EFAULT;
1093 goto cmd_rel_host;
1094 }
1095
1096 if (!curr_cmd->write_flag) {
1097 if (copy_to_user((void __user *)(unsigned long)
1098 curr_cmd->data_ptr,
1099 curr_data->buf,
1100 curr_data->buf_bytes)) {
1101 err = -EFAULT;
1102 goto cmd_rel_host;
1103 }
1104 }
1105
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001106 /*
1107 * Ensure RPMB command has completed by polling CMD13
1108 * "Send Status".
1109 */
1110 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1111 if (err)
1112 dev_err(mmc_dev(card->host),
1113 "%s: Card Status=0x%08X, error %d\n",
1114 __func__, status, err);
1115 }
1116
Krishna Kondae6711632014-12-04 15:20:57 +02001117cmd_rel_host:
1118 mmc_put_card(card);
1119
1120idata_free:
1121 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1122 kfree(idata->data[i]->buf);
1123 kfree(idata->data[i]);
1124 }
1125 kfree(idata);
1126
1127cmd_done:
1128 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001129 return err;
1130}
1131
1132static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1133 struct mmc_ioc_cmd __user *ic_ptr)
1134{
1135 struct mmc_blk_ioc_data *idata;
1136 struct mmc_blk_data *md;
1137 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001138 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001139
Shawn Lin83c742c2016-03-16 18:15:47 +08001140 /*
1141 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1142 * whole block device, not on a partition. This prevents overspray
1143 * between sibling partitions.
1144 */
1145 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1146 return -EPERM;
1147
Jon Huntera5f57742015-09-22 10:27:53 +01001148 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301149 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001150 return PTR_ERR(idata);
1151
1152 md = mmc_blk_get(bdev->bd_disk);
1153 if (!md) {
1154 err = -EINVAL;
1155 goto cmd_err;
1156 }
1157
1158 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301159 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001160 err = PTR_ERR(card);
1161 goto cmd_done;
1162 }
1163
1164 mmc_get_card(card);
1165
Grant Grundlerb0934102015-09-23 18:30:33 -07001166 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001167
Adrian Hunter3c866562016-05-04 14:38:12 +03001168 /* Always switch back to main area after RPMB access */
1169 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1170 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1171
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001172 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001173
Grant Grundlerb0934102015-09-23 18:30:33 -07001174 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001175
John Calixtocb87ea22011-04-26 18:56:29 -04001176cmd_done:
1177 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001178cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001179 kfree(idata->buf);
1180 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001181 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001182}
1183
Jon Huntera5f57742015-09-22 10:27:53 +01001184static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1185 struct mmc_ioc_multi_cmd __user *user)
1186{
1187 struct mmc_blk_ioc_data **idata = NULL;
1188 struct mmc_ioc_cmd __user *cmds = user->cmds;
1189 struct mmc_card *card;
1190 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001191 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001192 __u64 num_of_cmds;
1193
Shawn Lin83c742c2016-03-16 18:15:47 +08001194 /*
1195 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1196 * whole block device, not on a partition. This prevents overspray
1197 * between sibling partitions.
1198 */
1199 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1200 return -EPERM;
1201
Jon Huntera5f57742015-09-22 10:27:53 +01001202 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1203 sizeof(num_of_cmds)))
1204 return -EFAULT;
1205
1206 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1207 return -EINVAL;
1208
1209 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1210 if (!idata)
1211 return -ENOMEM;
1212
1213 for (i = 0; i < num_of_cmds; i++) {
1214 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1215 if (IS_ERR(idata[i])) {
1216 err = PTR_ERR(idata[i]);
1217 num_of_cmds = i;
1218 goto cmd_err;
1219 }
1220 }
1221
1222 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001223 if (!md) {
1224 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001225 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001226 }
Jon Huntera5f57742015-09-22 10:27:53 +01001227
1228 card = md->queue.card;
1229 if (IS_ERR(card)) {
1230 err = PTR_ERR(card);
1231 goto cmd_done;
1232 }
1233
1234 mmc_get_card(card);
1235
Grant Grundlerb0934102015-09-23 18:30:33 -07001236 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1237 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001238
Adrian Hunter3c866562016-05-04 14:38:12 +03001239 /* Always switch back to main area after RPMB access */
1240 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1241 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1242
Jon Huntera5f57742015-09-22 10:27:53 +01001243 mmc_put_card(card);
1244
1245 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001246 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001247 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001248
1249cmd_done:
1250 mmc_blk_put(md);
1251cmd_err:
1252 for (i = 0; i < num_of_cmds; i++) {
1253 kfree(idata[i]->buf);
1254 kfree(idata[i]);
1255 }
1256 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001257 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001258}
1259
John Calixtocb87ea22011-04-26 18:56:29 -04001260static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1261 unsigned int cmd, unsigned long arg)
1262{
Jon Huntera5f57742015-09-22 10:27:53 +01001263 switch (cmd) {
1264 case MMC_IOC_CMD:
1265 return mmc_blk_ioctl_cmd(bdev,
1266 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001267 case MMC_IOC_RPMB_CMD:
1268 return mmc_blk_ioctl_rpmb_cmd(bdev,
1269 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001270 case MMC_IOC_MULTI_CMD:
1271 return mmc_blk_ioctl_multi_cmd(bdev,
1272 (struct mmc_ioc_multi_cmd __user *)arg);
1273 default:
1274 return -EINVAL;
1275 }
John Calixtocb87ea22011-04-26 18:56:29 -04001276}
1277
1278#ifdef CONFIG_COMPAT
1279static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1280 unsigned int cmd, unsigned long arg)
1281{
1282 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1283}
1284#endif
1285
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001286static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001287 .open = mmc_blk_open,
1288 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001289 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001291 .ioctl = mmc_blk_ioctl,
1292#ifdef CONFIG_COMPAT
1293 .compat_ioctl = mmc_blk_compat_ioctl,
1294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295};
1296
Andrei Warkentin371a6892011-04-11 18:10:25 -05001297static inline int mmc_blk_part_switch(struct mmc_card *card,
1298 struct mmc_blk_data *md)
1299{
1300 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001301 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001302
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001303 if ((main_md->part_curr == md->part_type) &&
1304 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001305 return 0;
1306
1307 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001308 u8 part_config = card->ext_csd.part_config;
1309
Adrian Hunter57da0c02016-05-04 14:38:13 +03001310 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1311 mmc_retune_pause(card->host);
1312
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001313 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1314 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001315
1316 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001317 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001318 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001319 if (ret) {
1320 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1321 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001322 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001323 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001324
1325 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001326 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001327
1328 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1329 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001330 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001331
1332 main_md->part_curr = md->part_type;
1333 return 0;
1334}
1335
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001336static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1337{
1338 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001339 u32 result;
1340 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001341
Venkatraman Sad5fd972011-08-25 00:30:50 +05301342 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001343 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001344 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001345
1346 struct scatterlist sg;
1347
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001348 cmd.opcode = MMC_APP_CMD;
1349 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001350 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001351
1352 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001353 if (err)
1354 return (u32)-1;
1355 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001356 return (u32)-1;
1357
1358 memset(&cmd, 0, sizeof(struct mmc_command));
1359
1360 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1361 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001362 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001363
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001364 data.blksz = 4;
1365 data.blocks = 1;
1366 data.flags = MMC_DATA_READ;
1367 data.sg = &sg;
1368 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301369 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001370
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001371 mrq.cmd = &cmd;
1372 mrq.data = &data;
1373
Ben Dooks051913d2009-06-08 23:33:57 +01001374 blocks = kmalloc(4, GFP_KERNEL);
1375 if (!blocks)
1376 return (u32)-1;
1377
1378 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001379
1380 mmc_wait_for_req(card->host, &mrq);
1381
Ben Dooks051913d2009-06-08 23:33:57 +01001382 result = ntohl(*blocks);
1383 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001384
Ben Dooks051913d2009-06-08 23:33:57 +01001385 if (cmd.error || data.error)
1386 result = (u32)-1;
1387
1388 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001389}
1390
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001391static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001392{
Chris Ball1278dba2011-04-13 23:40:30 -04001393 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001394 int err;
1395
Adrian Hunter504f1912008-10-16 12:55:25 +03001396 cmd.opcode = MMC_SEND_STATUS;
1397 if (!mmc_host_is_spi(card->host))
1398 cmd.arg = card->rca << 16;
1399 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001400 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1401 if (err == 0)
1402 *status = cmd.resp[0];
1403 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001404}
1405
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001406static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001407 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001408{
1409 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1410 int err = 0;
1411 u32 status;
1412
1413 do {
1414 err = get_card_status(card, &status, 5);
1415 if (err) {
1416 pr_err("%s: error %d requesting status\n",
1417 req->rq_disk->disk_name, err);
1418 return err;
1419 }
1420
1421 if (status & R1_ERROR) {
1422 pr_err("%s: %s: error sending status cmd, status %#x\n",
1423 req->rq_disk->disk_name, __func__, status);
1424 *gen_err = 1;
1425 }
1426
Ulf Hansson95a91292014-01-29 13:11:27 +01001427 /* We may rely on the host hw to handle busy detection.*/
1428 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1429 hw_busy_detect)
1430 break;
1431
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001432 /*
1433 * Timeout if the device never becomes ready for data and never
1434 * leaves the program state.
1435 */
1436 if (time_after(jiffies, timeout)) {
1437 pr_err("%s: Card stuck in programming state! %s %s\n",
1438 mmc_hostname(card->host),
1439 req->rq_disk->disk_name, __func__);
1440 return -ETIMEDOUT;
1441 }
1442
1443 /*
1444 * Some cards mishandle the status bits,
1445 * so make sure to check both the busy
1446 * indication and the card state.
1447 */
1448 } while (!(status & R1_READY_FOR_DATA) ||
1449 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1450
1451 return err;
1452}
1453
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001454static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1455 struct request *req, int *gen_err, u32 *stop_status)
1456{
1457 struct mmc_host *host = card->host;
1458 struct mmc_command cmd = {0};
1459 int err;
1460 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1461
1462 /*
1463 * Normally we use R1B responses for WRITE, but in cases where the host
1464 * has specified a max_busy_timeout we need to validate it. A failure
1465 * means we need to prevent the host from doing hw busy detection, which
1466 * is done by converting to a R1 response instead.
1467 */
1468 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1469 use_r1b_resp = false;
1470
1471 cmd.opcode = MMC_STOP_TRANSMISSION;
1472 if (use_r1b_resp) {
1473 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1474 cmd.busy_timeout = timeout_ms;
1475 } else {
1476 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1477 }
1478
1479 err = mmc_wait_for_cmd(host, &cmd, 5);
1480 if (err)
1481 return err;
1482
1483 *stop_status = cmd.resp[0];
1484
1485 /* No need to check card status in case of READ. */
1486 if (rq_data_dir(req) == READ)
1487 return 0;
1488
1489 if (!mmc_host_is_spi(host) &&
1490 (*stop_status & R1_ERROR)) {
1491 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1492 req->rq_disk->disk_name, __func__, *stop_status);
1493 *gen_err = 1;
1494 }
1495
1496 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1497}
1498
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301499#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001500#define ERR_RETRY 2
1501#define ERR_ABORT 1
1502#define ERR_CONTINUE 0
1503
1504static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1505 bool status_valid, u32 status)
1506{
1507 switch (error) {
1508 case -EILSEQ:
1509 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001510 pr_err_ratelimited(
1511 "%s: response CRC error sending %s command, card status %#x\n",
1512 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001513 name, status);
1514 return ERR_RETRY;
1515
1516 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001517 pr_err_ratelimited(
1518 "%s: timed out sending %s command, card status %#x\n",
1519 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001520
1521 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301522 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001523 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301524 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001525 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301526 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001527
1528 /*
1529 * If it was a r/w cmd crc error, or illegal command
1530 * (eg, issued in wrong state) then retry - we should
1531 * have corrected the state problem above.
1532 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301533 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001534 pr_err_ratelimited(
1535 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301536 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001537 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301538 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001539
1540 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001541 pr_err_ratelimited(
1542 "%s: not retrying timeout\n",
1543 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001544 return ERR_ABORT;
1545
1546 default:
1547 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001548 pr_err_ratelimited(
1549 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001550 req->rq_disk->disk_name, error, status);
1551 return ERR_ABORT;
1552 }
1553}
1554
1555/*
1556 * Initial r/w and stop cmd error recovery.
1557 * We don't know whether the card received the r/w cmd or not, so try to
1558 * restore things back to a sane state. Essentially, we do this as follows:
1559 * - Obtain card status. If the first attempt to obtain card status fails,
1560 * the status word will reflect the failed status cmd, not the failed
1561 * r/w cmd. If we fail to obtain card status, it suggests we can no
1562 * longer communicate with the card.
1563 * - Check the card state. If the card received the cmd but there was a
1564 * transient problem with the response, it might still be in a data transfer
1565 * mode. Try to send it a stop command. If this fails, we can't recover.
1566 * - If the r/w cmd failed due to a response CRC error, it was probably
1567 * transient, so retry the cmd.
1568 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1569 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1570 * illegal cmd, retry.
1571 * Otherwise we don't understand what happened, so abort.
1572 */
1573static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001574 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001575{
1576 bool prev_cmd_status_valid = true;
1577 u32 status, stop_status = 0;
1578 int err, retry;
1579
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301580 if (mmc_card_removed(card))
1581 return ERR_NOMEDIUM;
1582
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001583 /*
1584 * Try to get card status which indicates both the card state
1585 * and why there was no response. If the first attempt fails,
1586 * we can't be sure the returned status is for the r/w command.
1587 */
1588 for (retry = 2; retry >= 0; retry--) {
1589 err = get_card_status(card, &status, 0);
1590 if (!err)
1591 break;
1592
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001593 /* Re-tune if needed */
1594 mmc_retune_recheck(card->host);
1595
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001596 prev_cmd_status_valid = false;
1597 pr_err("%s: error %d sending status command, %sing\n",
1598 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1599 }
1600
1601 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301602 if (err) {
1603 /* Check if the card is removed */
1604 if (mmc_detect_card_removed(card->host))
1605 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001606 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301607 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001608
Adrian Hunter67716322011-08-29 16:42:15 +03001609 /* Flag ECC errors */
1610 if ((status & R1_CARD_ECC_FAILED) ||
1611 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1612 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1613 *ecc_err = 1;
1614
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001615 /* Flag General errors */
1616 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1617 if ((status & R1_ERROR) ||
1618 (brq->stop.resp[0] & R1_ERROR)) {
1619 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1620 req->rq_disk->disk_name, __func__,
1621 brq->stop.resp[0], status);
1622 *gen_err = 1;
1623 }
1624
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001625 /*
1626 * Check the current card state. If it is in some data transfer
1627 * mode, tell it to stop (and hopefully transition back to TRAN.)
1628 */
1629 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1630 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001631 err = send_stop(card,
1632 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1633 req, gen_err, &stop_status);
1634 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001635 pr_err("%s: error %d sending stop command\n",
1636 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001637 /*
1638 * If the stop cmd also timed out, the card is probably
1639 * not present, so abort. Other errors are bad news too.
1640 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001641 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001642 }
1643
Adrian Hunter67716322011-08-29 16:42:15 +03001644 if (stop_status & R1_CARD_ECC_FAILED)
1645 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001646 }
1647
1648 /* Check for set block count errors */
1649 if (brq->sbc.error)
1650 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1651 prev_cmd_status_valid, status);
1652
1653 /* Check for r/w command errors */
1654 if (brq->cmd.error)
1655 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1656 prev_cmd_status_valid, status);
1657
Adrian Hunter67716322011-08-29 16:42:15 +03001658 /* Data errors */
1659 if (!brq->stop.error)
1660 return ERR_CONTINUE;
1661
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001662 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001663 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001664 req->rq_disk->disk_name, brq->stop.error,
1665 brq->cmd.resp[0], status);
1666
1667 /*
1668 * Subsitute in our own stop status as this will give the error
1669 * state which happened during the execution of the r/w command.
1670 */
1671 if (stop_status) {
1672 brq->stop.resp[0] = stop_status;
1673 brq->stop.error = 0;
1674 }
1675 return ERR_CONTINUE;
1676}
1677
Adrian Hunter67716322011-08-29 16:42:15 +03001678static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1679 int type)
1680{
1681 int err;
1682
1683 if (md->reset_done & type)
1684 return -EEXIST;
1685
1686 md->reset_done |= type;
1687 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301688 if (err && err != -EOPNOTSUPP) {
1689 /* We failed to reset so we need to abort the request */
1690 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1691 __func__, err);
1692 return -ENODEV;
1693 }
1694
Adrian Hunter67716322011-08-29 16:42:15 +03001695 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301696 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001697 struct mmc_blk_data *main_md =
1698 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001699 int part_err;
1700
1701 main_md->part_curr = main_md->part_type;
1702 part_err = mmc_blk_part_switch(host->card, md);
1703 if (part_err) {
1704 /*
1705 * We have failed to get back into the correct
1706 * partition, so we need to abort the whole request.
1707 */
1708 return -ENODEV;
1709 }
1710 }
1711 return err;
1712}
1713
1714static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1715{
1716 md->reset_done &= ~type;
1717}
1718
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001719int mmc_access_rpmb(struct mmc_queue *mq)
1720{
1721 struct mmc_blk_data *md = mq->data;
1722 /*
1723 * If this is a RPMB partition access, return ture
1724 */
1725 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1726 return true;
1727
1728 return false;
1729}
1730
Adrian Hunterbd788c92010-08-11 14:17:47 -07001731static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1732{
1733 struct mmc_blk_data *md = mq->data;
1734 struct mmc_card *card = md->queue.card;
1735 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001736 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001737
Adrian Hunterbd788c92010-08-11 14:17:47 -07001738 if (!mmc_can_erase(card)) {
1739 err = -EOPNOTSUPP;
1740 goto out;
1741 }
1742
1743 from = blk_rq_pos(req);
1744 nr = blk_rq_sectors(req);
1745
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001746 if (mmc_can_discard(card))
1747 arg = MMC_DISCARD_ARG;
1748 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001749 arg = MMC_TRIM_ARG;
1750 else
1751 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001752retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001753 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1754 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1755 INAND_CMD38_ARG_EXT_CSD,
1756 arg == MMC_TRIM_ARG ?
1757 INAND_CMD38_ARG_TRIM :
1758 INAND_CMD38_ARG_ERASE,
1759 0);
1760 if (err)
1761 goto out;
1762 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001763 err = mmc_erase(card, from, nr, arg);
1764out:
Adrian Hunter67716322011-08-29 16:42:15 +03001765 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1766 goto retry;
1767 if (!err)
1768 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301769 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001770
Adrian Hunterbd788c92010-08-11 14:17:47 -07001771 return err ? 0 : 1;
1772}
1773
Adrian Hunter49804542010-08-11 14:17:50 -07001774static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1775 struct request *req)
1776{
1777 struct mmc_blk_data *md = mq->data;
1778 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001779 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001780 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001781
Maya Erez775a9362013-04-18 15:41:55 +03001782 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001783 err = -EOPNOTSUPP;
1784 goto out;
1785 }
1786
1787 from = blk_rq_pos(req);
1788 nr = blk_rq_sectors(req);
1789
Maya Erez775a9362013-04-18 15:41:55 +03001790 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1791 arg = MMC_SECURE_TRIM1_ARG;
1792 else
1793 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001794
Adrian Hunter67716322011-08-29 16:42:15 +03001795retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001796 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1797 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1798 INAND_CMD38_ARG_EXT_CSD,
1799 arg == MMC_SECURE_TRIM1_ARG ?
1800 INAND_CMD38_ARG_SECTRIM1 :
1801 INAND_CMD38_ARG_SECERASE,
1802 0);
1803 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001804 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001805 }
Adrian Hunter28302812012-04-05 14:45:48 +03001806
Adrian Hunter49804542010-08-11 14:17:50 -07001807 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001808 if (err == -EIO)
1809 goto out_retry;
1810 if (err)
1811 goto out;
1812
1813 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001814 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1815 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1816 INAND_CMD38_ARG_EXT_CSD,
1817 INAND_CMD38_ARG_SECTRIM2,
1818 0);
1819 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001820 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001821 }
Adrian Hunter28302812012-04-05 14:45:48 +03001822
Adrian Hunter49804542010-08-11 14:17:50 -07001823 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001824 if (err == -EIO)
1825 goto out_retry;
1826 if (err)
1827 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001828 }
Adrian Hunter28302812012-04-05 14:45:48 +03001829
Adrian Hunter28302812012-04-05 14:45:48 +03001830out_retry:
1831 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001832 goto retry;
1833 if (!err)
1834 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001835out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301836 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001837
Adrian Hunter49804542010-08-11 14:17:50 -07001838 return err ? 0 : 1;
1839}
1840
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001841static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1842{
1843 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001844 struct mmc_card *card = md->queue.card;
1845 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001846
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001847 ret = mmc_flush_cache(card);
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001848 if (ret == -ENODEV) {
1849 pr_err("%s: %s: restart mmc card",
1850 req->rq_disk->disk_name, __func__);
1851 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
1852 pr_err("%s: %s: fail to restart mmc",
1853 req->rq_disk->disk_name, __func__);
1854 else
1855 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
1856 }
1857
1858 if (ret) {
1859 pr_err("%s: %s: notify flush error to upper layers",
1860 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001861 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001862 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001863
Mark Salyzyn6904e432016-01-28 11:12:25 -08001864#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1865 else if (atomic_read(&mq->cache_size)) {
1866 long used = mmc_blk_cache_used(mq, jiffies);
1867
1868 if (used) {
1869 int speed = atomic_read(&mq->max_write_speed);
1870
1871 if (speed_valid(speed)) {
1872 unsigned long msecs = jiffies_to_msecs(
1873 size_and_speed_to_jiffies(
1874 used, speed));
1875 if (msecs)
1876 msleep(msecs);
1877 }
1878 }
1879 }
1880#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301881 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001882
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001883 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001884}
1885
1886/*
1887 * Reformat current write as a reliable write, supporting
1888 * both legacy and the enhanced reliable write MMC cards.
1889 * In each transfer we'll handle only as much as a single
1890 * reliable write can handle, thus finish the request in
1891 * partial completions.
1892 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001893static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1894 struct mmc_card *card,
1895 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001896{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001897 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1898 /* Legacy mode imposes restrictions on transfers. */
1899 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1900 brq->data.blocks = 1;
1901
1902 if (brq->data.blocks > card->ext_csd.rel_sectors)
1903 brq->data.blocks = card->ext_csd.rel_sectors;
1904 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1905 brq->data.blocks = 1;
1906 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001907}
1908
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001909#define CMD_ERRORS \
1910 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1911 R1_ADDRESS_ERROR | /* Misaligned address */ \
1912 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1913 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1914 R1_CC_ERROR | /* Card controller error */ \
1915 R1_ERROR) /* General/unknown error */
1916
Per Forlinee8a43a2011-07-01 18:55:33 +02001917static int mmc_blk_err_check(struct mmc_card *card,
1918 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001919{
Per Forlinee8a43a2011-07-01 18:55:33 +02001920 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1921 mmc_active);
1922 struct mmc_blk_request *brq = &mq_mrq->brq;
1923 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001924 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001925 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001926
1927 /*
1928 * sbc.error indicates a problem with the set block count
1929 * command. No data will have been transferred.
1930 *
1931 * cmd.error indicates a problem with the r/w command. No
1932 * data will have been transferred.
1933 *
1934 * stop.error indicates a problem with the stop command. Data
1935 * may have been transferred, or may still be transferring.
1936 */
Adrian Hunter67716322011-08-29 16:42:15 +03001937 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1938 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001939 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001940 case ERR_RETRY:
1941 return MMC_BLK_RETRY;
1942 case ERR_ABORT:
1943 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301944 case ERR_NOMEDIUM:
1945 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001946 case ERR_CONTINUE:
1947 break;
1948 }
1949 }
1950
1951 /*
1952 * Check for errors relating to the execution of the
1953 * initial command - such as address errors. No data
1954 * has been transferred.
1955 */
1956 if (brq->cmd.resp[0] & CMD_ERRORS) {
1957 pr_err("%s: r/w command failed, status = %#x\n",
1958 req->rq_disk->disk_name, brq->cmd.resp[0]);
1959 return MMC_BLK_ABORT;
1960 }
1961
1962 /*
1963 * Everything else is either success, or a data error of some
1964 * kind. If it was a write, we may have transitioned to
1965 * program mode, which we have to wait for it to complete.
1966 */
1967 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001968 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001969
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001970 /* Check stop command response */
1971 if (brq->stop.resp[0] & R1_ERROR) {
1972 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1973 req->rq_disk->disk_name, __func__,
1974 brq->stop.resp[0]);
1975 gen_err = 1;
1976 }
1977
Ulf Hansson95a91292014-01-29 13:11:27 +01001978 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1979 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001980 if (err)
1981 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001982 }
1983
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001984 /* if general error occurs, retry the write operation. */
1985 if (gen_err) {
1986 pr_warn("%s: retrying write for general error\n",
1987 req->rq_disk->disk_name);
1988 return MMC_BLK_RETRY;
1989 }
1990
Per Forlind78d4a82011-07-01 18:55:30 +02001991 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001992 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001993 pr_debug("%s: retrying because a re-tune was needed\n",
1994 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001995 brq->retune_retry_done = 1;
1996 return MMC_BLK_RETRY;
1997 }
Per Forlind78d4a82011-07-01 18:55:30 +02001998 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1999 req->rq_disk->disk_name, brq->data.error,
2000 (unsigned)blk_rq_pos(req),
2001 (unsigned)blk_rq_sectors(req),
2002 brq->cmd.resp[0], brq->stop.resp[0]);
2003
2004 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002005 if (ecc_err)
2006 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002007 return MMC_BLK_DATA_ERR;
2008 } else {
2009 return MMC_BLK_CMD_ERR;
2010 }
2011 }
2012
Adrian Hunter67716322011-08-29 16:42:15 +03002013 if (!brq->data.bytes_xfered)
2014 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002015
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002016 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2017 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2018 return MMC_BLK_PARTIAL;
2019 else
2020 return MMC_BLK_SUCCESS;
2021 }
2022
Adrian Hunter67716322011-08-29 16:42:15 +03002023 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2024 return MMC_BLK_PARTIAL;
2025
2026 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002027}
2028
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002029static int mmc_blk_packed_err_check(struct mmc_card *card,
2030 struct mmc_async_req *areq)
2031{
2032 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2033 mmc_active);
2034 struct request *req = mq_rq->req;
2035 struct mmc_packed *packed = mq_rq->packed;
2036 int err, check, status;
2037 u8 *ext_csd;
2038
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002039 packed->retries--;
2040 check = mmc_blk_err_check(card, areq);
2041 err = get_card_status(card, &status, 0);
2042 if (err) {
2043 pr_err("%s: error %d sending status command\n",
2044 req->rq_disk->disk_name, err);
2045 return MMC_BLK_ABORT;
2046 }
2047
2048 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002049 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002050 if (err) {
2051 pr_err("%s: error %d sending ext_csd\n",
2052 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002053 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002054 }
2055
2056 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2057 EXT_CSD_PACKED_FAILURE) &&
2058 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2059 EXT_CSD_PACKED_GENERIC_ERROR)) {
2060 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2061 EXT_CSD_PACKED_INDEXED_ERROR) {
2062 packed->idx_failure =
2063 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2064 check = MMC_BLK_PARTIAL;
2065 }
2066 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2067 "failure index: %d\n",
2068 req->rq_disk->disk_name, packed->nr_entries,
2069 packed->blocks, packed->idx_failure);
2070 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002071 kfree(ext_csd);
2072 }
2073
2074 return check;
2075}
2076
Per Forlin54d49d72011-07-01 18:55:29 +02002077static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2078 struct mmc_card *card,
2079 int disable_multi,
2080 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Per Forlin54d49d72011-07-01 18:55:29 +02002082 u32 readcmd, writecmd;
2083 struct mmc_blk_request *brq = &mqrq->brq;
2084 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302086 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002088 /*
2089 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002090 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002091 */
Luca Porziod3df0462015-11-06 15:12:26 +00002092 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002093 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002094 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002095
Per Forlin54d49d72011-07-01 18:55:29 +02002096 memset(brq, 0, sizeof(struct mmc_blk_request));
2097 brq->mrq.cmd = &brq->cmd;
2098 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Per Forlin54d49d72011-07-01 18:55:29 +02002100 brq->cmd.arg = blk_rq_pos(req);
2101 if (!mmc_card_blockaddr(card))
2102 brq->cmd.arg <<= 9;
2103 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2104 brq->data.blksz = 512;
2105 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2106 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002107 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Asutosh Dasf0665412012-07-27 18:10:19 +05302109 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002110 /*
2111 * The block layer doesn't support all sector count
2112 * restrictions, so we need to be prepared for too big
2113 * requests.
2114 */
2115 if (brq->data.blocks > card->host->max_blk_count)
2116 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002118 if (brq->data.blocks > 1) {
2119 /*
2120 * After a read error, we redo the request one sector
2121 * at a time in order to accurately determine which
2122 * sectors can be read successfully.
2123 */
2124 if (disable_multi)
2125 brq->data.blocks = 1;
2126
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002127 /*
2128 * Some controllers have HW issues while operating
2129 * in multiple I/O mode
2130 */
2131 if (card->host->ops->multi_io_quirk)
2132 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2133 (rq_data_dir(req) == READ) ?
2134 MMC_DATA_READ : MMC_DATA_WRITE,
2135 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002136 }
Per Forlin54d49d72011-07-01 18:55:29 +02002137
2138 if (brq->data.blocks > 1 || do_rel_wr) {
2139 /* SPI multiblock writes terminate using a special
2140 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002141 */
Per Forlin54d49d72011-07-01 18:55:29 +02002142 if (!mmc_host_is_spi(card->host) ||
2143 rq_data_dir(req) == READ)
2144 brq->mrq.stop = &brq->stop;
2145 readcmd = MMC_READ_MULTIPLE_BLOCK;
2146 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2147 } else {
2148 brq->mrq.stop = NULL;
2149 readcmd = MMC_READ_SINGLE_BLOCK;
2150 writecmd = MMC_WRITE_BLOCK;
2151 }
2152 if (rq_data_dir(req) == READ) {
2153 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002154 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002155 if (brq->mrq.stop)
2156 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2157 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002158 } else {
2159 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002160 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002161 if (brq->mrq.stop)
2162 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2163 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002164 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002165
Per Forlin54d49d72011-07-01 18:55:29 +02002166 if (do_rel_wr)
2167 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002168
Per Forlin54d49d72011-07-01 18:55:29 +02002169 /*
Saugata Das42659002011-12-21 13:09:17 +05302170 * Data tag is used only during writing meta data to speed
2171 * up write and any subsequent read of this meta data
2172 */
2173 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2174 (req->cmd_flags & REQ_META) &&
2175 (rq_data_dir(req) == WRITE) &&
2176 ((brq->data.blocks * brq->data.blksz) >=
2177 card->ext_csd.data_tag_unit_size);
2178
2179 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002180 * Pre-defined multi-block transfers are preferable to
2181 * open ended-ones (and necessary for reliable writes).
2182 * However, it is not sufficient to just send CMD23,
2183 * and avoid the final CMD12, as on an error condition
2184 * CMD12 (stop) needs to be sent anyway. This, coupled
2185 * with Auto-CMD23 enhancements provided by some
2186 * hosts, means that the complexity of dealing
2187 * with this is best left to the host. If CMD23 is
2188 * supported by card and host, we'll fill sbc in and let
2189 * the host deal with handling it correctly. This means
2190 * that for hosts that don't expose MMC_CAP_CMD23, no
2191 * change of behavior will be observed.
2192 *
2193 * N.B: Some MMC cards experience perf degradation.
2194 * We'll avoid using CMD23-bounded multiblock writes for
2195 * these, while retaining features like reliable writes.
2196 */
Saugata Das42659002011-12-21 13:09:17 +05302197 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2198 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2199 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002200 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2201 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302202 (do_rel_wr ? (1 << 31) : 0) |
2203 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002204 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2205 brq->mrq.sbc = &brq->sbc;
2206 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002207
Per Forlin54d49d72011-07-01 18:55:29 +02002208 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002209
Per Forlin54d49d72011-07-01 18:55:29 +02002210 brq->data.sg = mqrq->sg;
2211 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002212
Per Forlin54d49d72011-07-01 18:55:29 +02002213 /*
2214 * Adjust the sg list so it is the same size as the
2215 * request.
2216 */
2217 if (brq->data.blocks != blk_rq_sectors(req)) {
2218 int i, data_size = brq->data.blocks << 9;
2219 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002220
Per Forlin54d49d72011-07-01 18:55:29 +02002221 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2222 data_size -= sg->length;
2223 if (data_size <= 0) {
2224 sg->length += data_size;
2225 i++;
2226 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002227 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002228 }
Per Forlin54d49d72011-07-01 18:55:29 +02002229 brq->data.sg_len = i;
2230 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002231
Per Forlinee8a43a2011-07-01 18:55:33 +02002232 mqrq->mmc_active.mrq = &brq->mrq;
2233 mqrq->mmc_active.err_check = mmc_blk_err_check;
2234
Per Forlin54d49d72011-07-01 18:55:29 +02002235 mmc_queue_bounce_pre(mqrq);
2236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002238static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2239 struct mmc_card *card)
2240{
2241 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2242 unsigned int max_seg_sz = queue_max_segment_size(q);
2243 unsigned int len, nr_segs = 0;
2244
2245 do {
2246 len = min(hdr_sz, max_seg_sz);
2247 hdr_sz -= len;
2248 nr_segs++;
2249 } while (hdr_sz);
2250
2251 return nr_segs;
2252}
2253
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002254/**
2255 * mmc_blk_disable_wr_packing() - disables packing mode
2256 * @mq: MMC queue.
2257 *
2258 */
2259void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2260{
2261 if (mq) {
2262 mq->wr_packing_enabled = false;
2263 mq->num_of_potential_packed_wr_reqs = 0;
2264 }
2265}
2266EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2267
Lee Susman841fd132013-04-23 17:59:26 +03002268static int get_packed_trigger(int potential, struct mmc_card *card,
2269 struct request *req, int curr_trigger)
2270{
2271 static int num_mean_elements = 1;
2272 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2273 unsigned int trigger = curr_trigger;
2274 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2275
2276 /* scale down the upper bound to 75% */
2277 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2278
2279 /*
2280 * since the most common calls for this function are with small
2281 * potential write values and since we don't want these calls to affect
2282 * the packed trigger, set a lower bound and ignore calls with
2283 * potential lower than that bound
2284 */
2285 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2286 return trigger;
2287
2288 /*
2289 * this is to prevent integer overflow in the following calculation:
2290 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2291 */
2292 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2293 num_mean_elements = 1;
2294 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2295 }
2296
2297 /*
2298 * get next mean value based on previous mean value and current
2299 * potential packed writes. Calculation is as follows:
2300 * mean_pot[i+1] =
2301 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2302 */
2303 mean_potential *= num_mean_elements;
2304 /*
2305 * add num_mean_elements so that the division of two integers doesn't
2306 * lower mean_potential too much
2307 */
2308 if (potential > mean_potential)
2309 mean_potential += num_mean_elements;
2310 mean_potential += potential;
2311 /* this is for gaining more precision when dividing two integers */
2312 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2313 /* this completes the mean calculation */
2314 mean_potential /= ++num_mean_elements;
2315 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2316
2317 /*
2318 * if current potential packed writes is greater than the mean potential
2319 * then the heuristic is that the following workload will contain many
2320 * write requests, therefore we lower the packed trigger. In the
2321 * opposite case we want to increase the trigger in order to get less
2322 * packing events.
2323 */
2324 if (potential >= mean_potential)
2325 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2326 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2327 else
2328 trigger = (trigger >= pckd_trgr_upper_bound) ?
2329 pckd_trgr_upper_bound : trigger + 1;
2330
2331 /*
2332 * an urgent read request indicates a packed list being interrupted
2333 * by this read, therefore we aim for less packing, hence the trigger
2334 * gets increased
2335 */
2336 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2337 trigger += PCKD_TRGR_URGENT_PENALTY;
2338
2339 return trigger;
2340}
2341
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002342static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2343 struct request *req)
2344{
2345 struct mmc_host *host = mq->card->host;
2346 int data_dir;
2347
2348 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2349 return;
2350
Maya Erez8e2b3c32012-12-02 13:27:15 +02002351 /* Support for the write packing on eMMC 4.5 or later */
2352 if (mq->card->ext_csd.rev <= 5)
2353 return;
2354
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002355 /*
2356 * In case the packing control is not supported by the host, it should
2357 * not have an effect on the write packing. Therefore we have to enable
2358 * the write packing
2359 */
2360 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2361 mq->wr_packing_enabled = true;
2362 return;
2363 }
2364
2365 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2366 if (mq->num_of_potential_packed_wr_reqs >
2367 mq->num_wr_reqs_to_start_packing)
2368 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002369 mq->num_wr_reqs_to_start_packing =
2370 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2371 mq->card, req,
2372 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002373 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002374 return;
2375 }
2376
2377 data_dir = rq_data_dir(req);
2378
2379 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002380 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002381 mq->num_wr_reqs_to_start_packing =
2382 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2383 mq->card, req,
2384 mq->num_wr_reqs_to_start_packing);
2385 mq->num_of_potential_packed_wr_reqs = 0;
2386 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002387 return;
2388 } else if (data_dir == WRITE) {
2389 mq->num_of_potential_packed_wr_reqs++;
2390 }
2391
2392 if (mq->num_of_potential_packed_wr_reqs >
2393 mq->num_wr_reqs_to_start_packing)
2394 mq->wr_packing_enabled = true;
2395}
2396
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002397struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2398{
2399 if (!card)
2400 return NULL;
2401
2402 return &card->wr_pack_stats;
2403}
2404EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2405
2406void mmc_blk_init_packed_statistics(struct mmc_card *card)
2407{
2408 int max_num_of_packed_reqs = 0;
2409
2410 if (!card || !card->wr_pack_stats.packing_events)
2411 return;
2412
2413 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2414
2415 spin_lock(&card->wr_pack_stats.lock);
2416 memset(card->wr_pack_stats.packing_events, 0,
2417 (max_num_of_packed_reqs + 1) *
2418 sizeof(*card->wr_pack_stats.packing_events));
2419 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2420 sizeof(card->wr_pack_stats.pack_stop_reason));
2421 card->wr_pack_stats.enabled = true;
2422 spin_unlock(&card->wr_pack_stats.lock);
2423}
2424EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2425
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002426static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2427{
2428 struct request_queue *q = mq->queue;
2429 struct mmc_card *card = mq->card;
2430 struct request *cur = req, *next = NULL;
2431 struct mmc_blk_data *md = mq->data;
2432 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2433 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2434 unsigned int req_sectors = 0, phys_segments = 0;
2435 unsigned int max_blk_count, max_phys_segs;
2436 bool put_back = true;
2437 u8 max_packed_rw = 0;
2438 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002439 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002440
Shawn Lin96e52da2016-08-26 08:49:55 +08002441 /*
2442 * We don't need to check packed for any further
2443 * operation of packed stuff as we set MMC_PACKED_NONE
2444 * and return zero for reqs if geting null packed. Also
2445 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2446 * it again when removing blk req.
2447 */
2448 if (!mqrq->packed) {
2449 md->flags &= (~MMC_BLK_PACKED_CMD);
2450 goto no_packed;
2451 }
2452
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002453 if (!(md->flags & MMC_BLK_PACKED_CMD))
2454 goto no_packed;
2455
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002456 if (!mq->wr_packing_enabled)
2457 goto no_packed;
2458
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002459 if ((rq_data_dir(cur) == WRITE) &&
2460 mmc_host_packed_wr(card->host))
2461 max_packed_rw = card->ext_csd.max_packed_writes;
2462
2463 if (max_packed_rw == 0)
2464 goto no_packed;
2465
2466 if (mmc_req_rel_wr(cur) &&
2467 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2468 goto no_packed;
2469
2470 if (mmc_large_sector(card) &&
2471 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2472 goto no_packed;
2473
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002474 if (cur->cmd_flags & REQ_FUA)
2475 goto no_packed;
2476
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002477 mmc_blk_clear_packed(mqrq);
2478
2479 max_blk_count = min(card->host->max_blk_count,
2480 card->host->max_req_size >> 9);
2481 if (unlikely(max_blk_count > 0xffff))
2482 max_blk_count = 0xffff;
2483
2484 max_phys_segs = queue_max_segments(q);
2485 req_sectors += blk_rq_sectors(cur);
2486 phys_segments += cur->nr_phys_segments;
2487
2488 if (rq_data_dir(cur) == WRITE) {
2489 req_sectors += mmc_large_sector(card) ? 8 : 1;
2490 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2491 }
2492
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002493 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002494 do {
2495 if (reqs >= max_packed_rw - 1) {
2496 put_back = false;
2497 break;
2498 }
2499
2500 spin_lock_irq(q->queue_lock);
2501 next = blk_fetch_request(q);
2502 spin_unlock_irq(q->queue_lock);
2503 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002504 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002505 put_back = false;
2506 break;
2507 }
2508
2509 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002510 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2511 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002512 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002513 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002514
Mike Christie3a5e02c2016-06-05 14:32:23 -05002515 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002516 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002517 req_op(next) == REQ_OP_FLUSH) {
2518 if (req_op(next) != REQ_OP_SECURE_ERASE)
2519 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002520 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002521 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002522
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002523 if (next->cmd_flags & REQ_FUA) {
2524 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2525 break;
2526 }
2527
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002528 if (rq_data_dir(cur) != rq_data_dir(next)) {
2529 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002530 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002531 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002532
2533 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002534 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2535 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002536 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002537 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002538
2539 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002540 if (req_sectors > max_blk_count) {
2541 if (stats->enabled)
2542 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002543 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002544 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002545
2546 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002547 if (phys_segments > max_phys_segs) {
2548 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002549 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002550 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002551
Maya Erez5a8dae12014-12-04 15:13:59 +02002552 if (mq->no_pack_for_random) {
2553 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2554 blk_rq_pos(next)) {
2555 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2556 put_back = 1;
2557 break;
2558 }
2559 }
2560
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002561 if (rq_data_dir(next) == WRITE)
2562 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002563 list_add_tail(&next->queuelist, &mqrq->packed->list);
2564 cur = next;
2565 reqs++;
2566 } while (1);
2567
2568 if (put_back) {
2569 spin_lock_irq(q->queue_lock);
2570 blk_requeue_request(q, next);
2571 spin_unlock_irq(q->queue_lock);
2572 }
2573
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002574 if (stats->enabled) {
2575 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2576 stats->packing_events[reqs + 1]++;
2577 if (reqs + 1 == max_packed_rw)
2578 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2579 }
2580
2581 spin_unlock(&stats->lock);
2582
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002583 if (reqs > 0) {
2584 list_add(&req->queuelist, &mqrq->packed->list);
2585 mqrq->packed->nr_entries = ++reqs;
2586 mqrq->packed->retries = reqs;
2587 return reqs;
2588 }
2589
2590no_packed:
2591 mqrq->cmd_type = MMC_PACKED_NONE;
2592 return 0;
2593}
2594
2595static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2596 struct mmc_card *card,
2597 struct mmc_queue *mq)
2598{
2599 struct mmc_blk_request *brq = &mqrq->brq;
2600 struct request *req = mqrq->req;
2601 struct request *prq;
2602 struct mmc_blk_data *md = mq->data;
2603 struct mmc_packed *packed = mqrq->packed;
2604 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002605 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002606 u8 hdr_blocks;
2607 u8 i = 1;
2608
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002609 mqrq->cmd_type = MMC_PACKED_WRITE;
2610 packed->blocks = 0;
2611 packed->idx_failure = MMC_PACKED_NR_IDX;
2612
2613 packed_cmd_hdr = packed->cmd_hdr;
2614 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002615 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2616 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002617 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2618
2619 /*
2620 * Argument for each entry of packed group
2621 */
2622 list_for_each_entry(prq, &packed->list, queuelist) {
2623 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2624 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2625 (prq->cmd_flags & REQ_META) &&
2626 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002627 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002628 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002629 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002630 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2631 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002632 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002633 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002634 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002635 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002636 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002637 packed->blocks += blk_rq_sectors(prq);
2638 i++;
2639 }
2640
2641 memset(brq, 0, sizeof(struct mmc_blk_request));
2642 brq->mrq.cmd = &brq->cmd;
2643 brq->mrq.data = &brq->data;
2644 brq->mrq.sbc = &brq->sbc;
2645 brq->mrq.stop = &brq->stop;
2646
2647 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2648 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2649 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2650
2651 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2652 brq->cmd.arg = blk_rq_pos(req);
2653 if (!mmc_card_blockaddr(card))
2654 brq->cmd.arg <<= 9;
2655 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2656
2657 brq->data.blksz = 512;
2658 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002659 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302660 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002661
2662 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2663 brq->stop.arg = 0;
2664 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2665
2666 mmc_set_data_timeout(&brq->data, card);
2667
2668 brq->data.sg = mqrq->sg;
2669 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2670
2671 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002672
2673 /*
2674 * This is intended for packed commands tests usage - in case these
2675 * functions are not in use the respective pointers are NULL
2676 */
2677 if (mq->err_check_fn)
2678 mqrq->mmc_active.err_check = mq->err_check_fn;
2679 else
2680 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2681
2682 if (mq->packed_test_fn)
2683 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002684
2685 mmc_queue_bounce_pre(mqrq);
2686}
2687
Adrian Hunter67716322011-08-29 16:42:15 +03002688static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2689 struct mmc_blk_request *brq, struct request *req,
2690 int ret)
2691{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002692 struct mmc_queue_req *mq_rq;
2693 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2694
Adrian Hunter67716322011-08-29 16:42:15 +03002695 /*
2696 * If this is an SD card and we're writing, we can first
2697 * mark the known good sectors as ok.
2698 *
2699 * If the card is not SD, we can still ok written sectors
2700 * as reported by the controller (which might be less than
2701 * the real number of written sectors, but never more).
2702 */
2703 if (mmc_card_sd(card)) {
2704 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302705 if (!brq->data.fault_injected) {
2706 blocks = mmc_sd_num_wr_blocks(card);
2707 if (blocks != (u32)-1)
2708 ret = blk_end_request(req, 0, blocks << 9);
2709 } else
2710 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002711 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002712 if (!mmc_packed_cmd(mq_rq->cmd_type))
2713 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002714 }
2715 return ret;
2716}
2717
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002718static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2719{
2720 struct request *prq;
2721 struct mmc_packed *packed = mq_rq->packed;
2722 int idx = packed->idx_failure, i = 0;
2723 int ret = 0;
2724
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002725 while (!list_empty(&packed->list)) {
2726 prq = list_entry_rq(packed->list.next);
2727 if (idx == i) {
2728 /* retry from error index */
2729 packed->nr_entries -= idx;
2730 mq_rq->req = prq;
2731 ret = 1;
2732
2733 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2734 list_del_init(&prq->queuelist);
2735 mmc_blk_clear_packed(mq_rq);
2736 }
2737 return ret;
2738 }
2739 list_del_init(&prq->queuelist);
2740 blk_end_request(prq, 0, blk_rq_bytes(prq));
2741 i++;
2742 }
2743
2744 mmc_blk_clear_packed(mq_rq);
2745 return ret;
2746}
2747
2748static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2749{
2750 struct request *prq;
2751 struct mmc_packed *packed = mq_rq->packed;
2752
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002753 while (!list_empty(&packed->list)) {
2754 prq = list_entry_rq(packed->list.next);
2755 list_del_init(&prq->queuelist);
2756 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2757 }
2758
2759 mmc_blk_clear_packed(mq_rq);
2760}
2761
2762static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2763 struct mmc_queue_req *mq_rq)
2764{
2765 struct request *prq;
2766 struct request_queue *q = mq->queue;
2767 struct mmc_packed *packed = mq_rq->packed;
2768
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002769 while (!list_empty(&packed->list)) {
2770 prq = list_entry_rq(packed->list.prev);
2771 if (prq->queuelist.prev != &packed->list) {
2772 list_del_init(&prq->queuelist);
2773 spin_lock_irq(q->queue_lock);
2774 blk_requeue_request(mq->queue, prq);
2775 spin_unlock_irq(q->queue_lock);
2776 } else {
2777 list_del_init(&prq->queuelist);
2778 }
2779 }
2780
2781 mmc_blk_clear_packed(mq_rq);
2782}
2783
Per Forlinee8a43a2011-07-01 18:55:33 +02002784static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002785{
2786 struct mmc_blk_data *md = mq->data;
2787 struct mmc_card *card = md->queue.card;
2788 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002789 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002790 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002791 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302792 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002793 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002794 const u8 packed_nr = 2;
2795 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002796#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2797 unsigned long waitfor = jiffies;
2798#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002799
2800 if (!rqc && !mq->mqrq_prev->req)
2801 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002802
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002803 if (rqc)
2804 reqs = mmc_blk_prep_packed_list(mq, rqc);
2805
Per Forlin54d49d72011-07-01 18:55:29 +02002806 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002807 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302808 /*
2809 * When 4KB native sector is enabled, only 8 blocks
2810 * multiple read or write is allowed
2811 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002812 if (mmc_large_sector(card) &&
2813 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302814 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2815 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002816 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302817 goto cmd_abort;
2818 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002819
2820 if (reqs >= packed_nr)
2821 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2822 card, mq);
2823 else
2824 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002825 areq = &mq->mqrq_cur->mmc_active;
2826 } else
2827 areq = NULL;
2828 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002829 if (!areq) {
2830 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002831 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02002832 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002833 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002834
Per Forlinee8a43a2011-07-01 18:55:33 +02002835 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2836 brq = &mq_rq->brq;
2837 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002838 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002839 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002840
Per Forlind78d4a82011-07-01 18:55:30 +02002841 switch (status) {
2842 case MMC_BLK_SUCCESS:
2843 case MMC_BLK_PARTIAL:
2844 /*
2845 * A block was successfully transferred.
2846 */
Adrian Hunter67716322011-08-29 16:42:15 +03002847 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002848
Mark Salyzyn6904e432016-01-28 11:12:25 -08002849 mmc_blk_simulate_delay(mq, rqc, waitfor);
2850
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002851 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2852 ret = mmc_blk_end_packed_req(mq_rq);
2853 break;
2854 } else {
2855 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002856 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002857 }
2858
Adrian Hunter67716322011-08-29 16:42:15 +03002859 /*
2860 * If the blk_end_request function returns non-zero even
2861 * though all data has been transferred and no errors
2862 * were returned by the host controller, it's a bug.
2863 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002864 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302865 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002866 __func__, blk_rq_bytes(req),
2867 brq->data.bytes_xfered);
2868 rqc = NULL;
2869 goto cmd_abort;
2870 }
Per Forlind78d4a82011-07-01 18:55:30 +02002871 break;
2872 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002873 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002874 if (mmc_blk_reset(md, card->host, type))
2875 goto cmd_abort;
2876 if (!ret)
2877 goto start_new_req;
2878 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002879 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002880 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02002881 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002882 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002883 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002884 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02002885 if (!mmc_blk_reset(md, card->host, type) &&
2886 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03002887 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002888 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002889 case MMC_BLK_DATA_ERR: {
2890 int err;
2891
2892 err = mmc_blk_reset(md, card->host, type);
2893 if (!err)
2894 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302895 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002896 }
2897 case MMC_BLK_ECC_ERR:
2898 if (brq->data.blocks > 1) {
2899 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002900 pr_warn("%s: retrying using single block read\n",
2901 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002902 disable_multi = 1;
2903 break;
2904 }
Per Forlind78d4a82011-07-01 18:55:30 +02002905 /*
2906 * After an error, we redo I/O one sector at a
2907 * time, so we only reach here after trying to
2908 * read a single sector.
2909 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302910 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002911 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002912 if (!ret)
2913 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002914 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302915 case MMC_BLK_NOMEDIUM:
2916 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002917 default:
2918 pr_err("%s: Unhandled return value (%d)",
2919 req->rq_disk->disk_name, status);
2920 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002921 }
2922
Per Forlinee8a43a2011-07-01 18:55:33 +02002923 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002924 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2925 if (!mq_rq->packed->retries)
2926 goto cmd_abort;
2927 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2928 mmc_start_req(card->host,
2929 &mq_rq->mmc_active, NULL);
2930 } else {
2931
2932 /*
2933 * In case of a incomplete request
2934 * prepare it again and resend.
2935 */
2936 mmc_blk_rw_rq_prep(mq_rq, card,
2937 disable_multi, mq);
2938 mmc_start_req(card->host,
2939 &mq_rq->mmc_active, NULL);
2940 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002941 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 } while (ret);
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return 1;
2946
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002947 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002948 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2949 mmc_blk_abort_packed_req(mq_rq);
2950 } else {
2951 if (mmc_card_removed(card))
2952 req->cmd_flags |= REQ_QUIET;
2953 while (ret)
2954 ret = blk_end_request(req, -EIO,
2955 blk_rq_cur_bytes(req));
2956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Per Forlinee8a43a2011-07-01 18:55:33 +02002958 start_new_req:
2959 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002960 if (mmc_card_removed(card)) {
2961 rqc->cmd_flags |= REQ_QUIET;
2962 blk_end_request_all(rqc, -EIO);
2963 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002964 /*
2965 * If current request is packed, it needs to put back.
2966 */
2967 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2968 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2969
Seungwon Jeon7a819022013-01-22 19:48:07 +09002970 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2971 mmc_start_req(card->host,
2972 &mq->mqrq_cur->mmc_active, NULL);
2973 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002974 }
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return 0;
2977}
2978
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002979int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002980{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002981 int ret;
2982 struct mmc_blk_data *md = mq->data;
2983 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002984 struct mmc_host *host = card->host;
2985 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002986 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002987
Per Forlinee8a43a2011-07-01 18:55:33 +02002988 if (req && !mq->mqrq_prev->req)
2989 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002990 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002991
Andrei Warkentin371a6892011-04-11 18:10:25 -05002992 ret = mmc_blk_part_switch(card, md);
2993 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002994 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302995 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002996 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002997 ret = 0;
2998 goto out;
2999 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003000
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003001 mmc_blk_write_packing_control(mq, req);
3002
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003003 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003004 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003005 /* complete ongoing async transfer before issuing discard */
3006 if (card->host->areq)
3007 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003008 ret = mmc_blk_issue_discard_rq(mq, req);
3009 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3010 /* complete ongoing async transfer before issuing secure erase*/
3011 if (card->host->areq)
3012 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003013 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3014 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3015 else
3016 ret = mmc_blk_issue_discard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05003017 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003018 /* complete ongoing async transfer before issuing flush */
3019 if (card->host->areq)
3020 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003021 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003022 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003023 if (!req && host->areq) {
3024 spin_lock_irqsave(&host->context_info.lock, flags);
3025 host->context_info.is_waiting_last_req = true;
3026 spin_unlock_irqrestore(&host->context_info.lock, flags);
3027 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003028 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003029 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003030
Andrei Warkentin371a6892011-04-11 18:10:25 -05003031out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003032 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3033 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003034 /*
3035 * Release host when there are no more requests
3036 * and after special request(discard, flush) is done.
3037 * In case sepecial request, there is no reentry to
3038 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3039 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003040 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003041 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Russell Kinga6f6c962006-01-03 22:38:44 +00003044static inline int mmc_blk_readonly(struct mmc_card *card)
3045{
3046 return mmc_card_readonly(card) ||
3047 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3048}
3049
Andrei Warkentin371a6892011-04-11 18:10:25 -05003050static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3051 struct device *parent,
3052 sector_t size,
3053 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003054 const char *subname,
3055 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct mmc_blk_data *md;
3058 int devidx, ret;
3059
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003060again:
3061 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3062 return ERR_PTR(-ENOMEM);
3063
3064 spin_lock(&mmc_blk_lock);
3065 ret = ida_get_new(&mmc_blk_ida, &devidx);
3066 spin_unlock(&mmc_blk_lock);
3067
3068 if (ret == -EAGAIN)
3069 goto again;
3070 else if (ret)
3071 return ERR_PTR(ret);
3072
3073 if (devidx >= max_devices) {
3074 ret = -ENOSPC;
3075 goto out;
3076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003078 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003079 if (!md) {
3080 ret = -ENOMEM;
3081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003083
Johan Rudholmadd710e2011-12-02 08:51:06 +01003084 md->area_type = area_type;
3085
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003086 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003087 * Set the read-only status based on the supported commands
3088 * and the write protect switch.
3089 */
3090 md->read_only = mmc_blk_readonly(card);
3091
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003092 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003093 if (md->disk == NULL) {
3094 ret = -ENOMEM;
3095 goto err_kfree;
3096 }
3097
3098 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003099 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003100 md->usage = 1;
3101
Adrian Hunterd09408a2011-06-23 13:40:28 +03003102 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003103 if (ret)
3104 goto err_putdisk;
3105
Russell Kinga6f6c962006-01-03 22:38:44 +00003106 md->queue.data = md;
3107
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003108 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003109 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003110 md->disk->fops = &mmc_bdops;
3111 md->disk->private_data = md;
3112 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003113 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003114 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003115 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003116 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003117 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003118
3119 /*
3120 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3121 *
3122 * - be set for removable media with permanent block devices
3123 * - be unset for removable block devices with permanent media
3124 *
3125 * Since MMC block devices clearly fall under the second
3126 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3127 * should use the block device creation/destruction hotplug
3128 * messages to tell when the card is present.
3129 */
3130
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003131 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003132 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003133
Saugata Dasa5075eb2012-05-17 16:32:21 +05303134 if (mmc_card_mmc(card))
3135 blk_queue_logical_block_size(md->queue.queue,
3136 card->ext_csd.data_sector_size);
3137 else
3138 blk_queue_logical_block_size(md->queue.queue, 512);
3139
Andrei Warkentin371a6892011-04-11 18:10:25 -05003140 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003141
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003142 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003143 if ((mmc_card_mmc(card) &&
3144 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003145 (mmc_card_sd(card) &&
3146 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3147 md->flags |= MMC_BLK_CMD23;
3148 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003149
3150 if (mmc_card_mmc(card) &&
3151 md->flags & MMC_BLK_CMD23 &&
3152 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3153 card->ext_csd.rel_sectors)) {
3154 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003155 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003156 }
3157
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003158 if (mmc_card_mmc(card) &&
3159 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3160 (md->flags & MMC_BLK_CMD23) &&
3161 card->ext_csd.packed_event_en) {
3162 if (!mmc_packed_init(&md->queue, card))
3163 md->flags |= MMC_BLK_PACKED_CMD;
3164 }
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003167
3168 err_putdisk:
3169 put_disk(md->disk);
3170 err_kfree:
3171 kfree(md);
3172 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003173 spin_lock(&mmc_blk_lock);
3174 ida_remove(&mmc_blk_ida, devidx);
3175 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003176 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
Andrei Warkentin371a6892011-04-11 18:10:25 -05003179static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3180{
3181 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003182
3183 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3184 /*
3185 * The EXT_CSD sector count is in number or 512 byte
3186 * sectors.
3187 */
3188 size = card->ext_csd.sectors;
3189 } else {
3190 /*
3191 * The CSD capacity field is in units of read_blkbits.
3192 * set_capacity takes units of 512 bytes.
3193 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003194 size = (typeof(sector_t))card->csd.capacity
3195 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003196 }
3197
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003198 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003199 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003200}
3201
3202static int mmc_blk_alloc_part(struct mmc_card *card,
3203 struct mmc_blk_data *md,
3204 unsigned int part_type,
3205 sector_t size,
3206 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003207 const char *subname,
3208 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003209{
3210 char cap_str[10];
3211 struct mmc_blk_data *part_md;
3212
3213 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003214 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003215 if (IS_ERR(part_md))
3216 return PTR_ERR(part_md);
3217 part_md->part_type = part_type;
3218 list_add(&part_md->part, &md->part);
3219
James Bottomleyb9f28d82015-03-05 18:47:01 -08003220 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003221 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303222 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003223 part_md->disk->disk_name, mmc_card_id(card),
3224 mmc_card_name(card), part_md->part_type, cap_str);
3225 return 0;
3226}
3227
Namjae Jeone0c368d2011-10-06 23:41:38 +09003228/* MMC Physical partitions consist of two boot partitions and
3229 * up to four general purpose partitions.
3230 * For each partition enabled in EXT_CSD a block device will be allocatedi
3231 * to provide access to the partition.
3232 */
3233
Andrei Warkentin371a6892011-04-11 18:10:25 -05003234static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3235{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003236 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003237
3238 if (!mmc_card_mmc(card))
3239 return 0;
3240
Namjae Jeone0c368d2011-10-06 23:41:38 +09003241 for (idx = 0; idx < card->nr_parts; idx++) {
3242 if (card->part[idx].size) {
3243 ret = mmc_blk_alloc_part(card, md,
3244 card->part[idx].part_cfg,
3245 card->part[idx].size >> 9,
3246 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003247 card->part[idx].name,
3248 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003249 if (ret)
3250 return ret;
3251 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003252 }
3253
3254 return ret;
3255}
3256
Andrei Warkentin371a6892011-04-11 18:10:25 -05003257static void mmc_blk_remove_req(struct mmc_blk_data *md)
3258{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003259 struct mmc_card *card;
3260
Andrei Warkentin371a6892011-04-11 18:10:25 -05003261 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003262 /*
3263 * Flush remaining requests and free queues. It
3264 * is freeing the queue that stops new requests
3265 * from being accepted.
3266 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003267 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003268 mmc_cleanup_queue(&md->queue);
3269 if (md->flags & MMC_BLK_PACKED_CMD)
3270 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003271 device_remove_file(disk_to_dev(md->disk),
3272 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003273 if (md->disk->flags & GENHD_FL_UP) {
3274 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003275 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3276 card->ext_csd.boot_ro_lockable)
3277 device_remove_file(disk_to_dev(md->disk),
3278 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003279#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3280 device_remove_file(disk_to_dev(md->disk),
3281 &dev_attr_max_write_speed);
3282 device_remove_file(disk_to_dev(md->disk),
3283 &dev_attr_max_read_speed);
3284 device_remove_file(disk_to_dev(md->disk),
3285 &dev_attr_cache_size);
3286#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003287
Andrei Warkentin371a6892011-04-11 18:10:25 -05003288 del_gendisk(md->disk);
3289 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003290 mmc_blk_put(md);
3291 }
3292}
3293
3294static void mmc_blk_remove_parts(struct mmc_card *card,
3295 struct mmc_blk_data *md)
3296{
3297 struct list_head *pos, *q;
3298 struct mmc_blk_data *part_md;
3299
3300 list_for_each_safe(pos, q, &md->part) {
3301 part_md = list_entry(pos, struct mmc_blk_data, part);
3302 list_del(pos);
3303 mmc_blk_remove_req(part_md);
3304 }
3305}
3306
3307static int mmc_add_disk(struct mmc_blk_data *md)
3308{
3309 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003310 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003311
Dan Williams307d8e62016-06-20 10:40:44 -07003312 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003313 md->force_ro.show = force_ro_show;
3314 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303315 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003316 md->force_ro.attr.name = "force_ro";
3317 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3318 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3319 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003320 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003321#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3322 atomic_set(&md->queue.max_write_speed, max_write_speed);
3323 ret = device_create_file(disk_to_dev(md->disk),
3324 &dev_attr_max_write_speed);
3325 if (ret)
3326 goto max_write_speed_fail;
3327 atomic_set(&md->queue.max_read_speed, max_read_speed);
3328 ret = device_create_file(disk_to_dev(md->disk),
3329 &dev_attr_max_read_speed);
3330 if (ret)
3331 goto max_read_speed_fail;
3332 atomic_set(&md->queue.cache_size, cache_size);
3333 atomic_long_set(&md->queue.cache_used, 0);
3334 md->queue.cache_jiffies = jiffies;
3335 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3336 if (ret)
3337 goto cache_size_fail;
3338#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003339
3340 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3341 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003342 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003343
3344 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3345 mode = S_IRUGO;
3346 else
3347 mode = S_IRUGO | S_IWUSR;
3348
3349 md->power_ro_lock.show = power_ro_lock_show;
3350 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003351 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003352 md->power_ro_lock.attr.mode = mode;
3353 md->power_ro_lock.attr.name =
3354 "ro_lock_until_next_power_on";
3355 ret = device_create_file(disk_to_dev(md->disk),
3356 &md->power_ro_lock);
3357 if (ret)
3358 goto power_ro_lock_fail;
3359 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003360
3361 md->num_wr_reqs_to_start_packing.show =
3362 num_wr_reqs_to_start_packing_show;
3363 md->num_wr_reqs_to_start_packing.store =
3364 num_wr_reqs_to_start_packing_store;
3365 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3366 md->num_wr_reqs_to_start_packing.attr.name =
3367 "num_wr_reqs_to_start_packing";
3368 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3369 ret = device_create_file(disk_to_dev(md->disk),
3370 &md->num_wr_reqs_to_start_packing);
3371 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003372 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003373
Maya Erez5a8dae12014-12-04 15:13:59 +02003374 md->no_pack_for_random.show = no_pack_for_random_show;
3375 md->no_pack_for_random.store = no_pack_for_random_store;
3376 sysfs_attr_init(&md->no_pack_for_random.attr);
3377 md->no_pack_for_random.attr.name = "no_pack_for_random";
3378 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3379 ret = device_create_file(disk_to_dev(md->disk),
3380 &md->no_pack_for_random);
3381 if (ret)
3382 goto no_pack_for_random_fails;
3383
Johan Rudholmadd710e2011-12-02 08:51:06 +01003384 return ret;
3385
Maya Erez5a8dae12014-12-04 15:13:59 +02003386no_pack_for_random_fails:
3387 device_remove_file(disk_to_dev(md->disk),
3388 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003389num_wr_reqs_to_start_packing_fail:
3390 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003391power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003392#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3393 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3394cache_size_fail:
3395 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3396max_read_speed_fail:
3397 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3398max_write_speed_fail:
3399#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003400 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3401force_ro_fail:
3402 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003403
3404 return ret;
3405}
3406
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003407static const struct mmc_fixup blk_fixups[] =
3408{
Chris Ballc59d4472011-11-11 22:01:43 -05003409 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3410 MMC_QUIRK_INAND_CMD38),
3411 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3412 MMC_QUIRK_INAND_CMD38),
3413 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3414 MMC_QUIRK_INAND_CMD38),
3415 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3416 MMC_QUIRK_INAND_CMD38),
3417 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3418 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003419
3420 /*
3421 * Some MMC cards experience performance degradation with CMD23
3422 * instead of CMD12-bounded multiblock transfers. For now we'll
3423 * black list what's bad...
3424 * - Certain Toshiba cards.
3425 *
3426 * N.B. This doesn't affect SD cards.
3427 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003428 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3429 MMC_QUIRK_BLK_NO_CMD23),
3430 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3431 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003432 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003433 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003434 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003435 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003436 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003437 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003438
3439 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003440 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003441 */
Chris Ballc59d4472011-11-11 22:01:43 -05003442 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003443 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003444 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3445 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003446
Ian Chen3550ccd2012-08-29 15:05:36 +09003447 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003448 * Some Samsung MMC cards need longer data read timeout than
3449 * indicated in CSD.
3450 */
3451 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3452 MMC_QUIRK_LONG_READ_TIME),
3453
3454 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003455 * On these Samsung MoviNAND parts, performing secure erase or
3456 * secure trim can result in unrecoverable corruption due to a
3457 * firmware bug.
3458 */
3459 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3460 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3461 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3462 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3463 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3464 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3465 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3466 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3467 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3468 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3469 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3470 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3471 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3472 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3473 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3474 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3475
Shawn Linb5b4ff02015-08-12 13:08:32 +08003476 /*
3477 * On Some Kingston eMMCs, performing trim can result in
3478 * unrecoverable data conrruption occasionally due to a firmware bug.
3479 */
3480 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3481 MMC_QUIRK_TRIM_BROKEN),
3482 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3483 MMC_QUIRK_TRIM_BROKEN),
3484
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003485 /* Some INAND MCP devices advertise incorrect timeout values */
3486 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3487 MMC_QUIRK_INAND_DATA_TIMEOUT),
3488
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003489 END_FIXUP
3490};
3491
Ulf Hansson96541ba2015-04-14 13:06:12 +02003492static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003494 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003495 char cap_str[10];
3496
Pierre Ossman912490d2005-05-21 10:27:02 +01003497 /*
3498 * Check that the card supports the command class(es) we need.
3499 */
3500 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return -ENODEV;
3502
Lukas Czerner5204d002014-06-18 13:18:07 +02003503 mmc_fixup_device(card, blk_fixups);
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 md = mmc_blk_alloc(card);
3506 if (IS_ERR(md))
3507 return PTR_ERR(md);
3508
James Bottomleyb9f28d82015-03-05 18:47:01 -08003509 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003510 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303511 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003513 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Andrei Warkentin371a6892011-04-11 18:10:25 -05003515 if (mmc_blk_alloc_parts(card, md))
3516 goto out;
3517
Ulf Hansson96541ba2015-04-14 13:06:12 +02003518 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003519
Andrei Warkentin371a6892011-04-11 18:10:25 -05003520 if (mmc_add_disk(md))
3521 goto out;
3522
3523 list_for_each_entry(part_md, &md->part, part) {
3524 if (mmc_add_disk(part_md))
3525 goto out;
3526 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003527
3528 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3529 pm_runtime_use_autosuspend(&card->dev);
3530
3531 /*
3532 * Don't enable runtime PM for SD-combo cards here. Leave that
3533 * decision to be taken during the SDIO init sequence instead.
3534 */
3535 if (card->type != MMC_TYPE_SD_COMBO) {
3536 pm_runtime_set_active(&card->dev);
3537 pm_runtime_enable(&card->dev);
3538 }
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return 0;
3541
3542 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003543 mmc_blk_remove_parts(card, md);
3544 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Ulf Hansson96541ba2015-04-14 13:06:12 +02003548static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003550 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Andrei Warkentin371a6892011-04-11 18:10:25 -05003552 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003553 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003554 mmc_claim_host(card->host);
3555 mmc_blk_part_switch(card, md);
3556 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003557 if (card->type != MMC_TYPE_SD_COMBO)
3558 pm_runtime_disable(&card->dev);
3559 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003560 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003561 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Ulf Hansson96541ba2015-04-14 13:06:12 +02003564static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003566 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003567 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303568 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303571 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303572 if (rc)
3573 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003574 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303575 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303576 if (rc)
3577 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303580 goto out;
3581
3582 out_resume:
3583 mmc_queue_resume(&md->queue);
3584 list_for_each_entry(part_md, &md->part, part) {
3585 mmc_queue_resume(&part_md->queue);
3586 }
3587 out:
3588 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Ulf Hansson96541ba2015-04-14 13:06:12 +02003591static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003592{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003593 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003594}
3595
Ulf Hansson0967edc2014-10-06 11:29:42 +02003596#ifdef CONFIG_PM_SLEEP
3597static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003598{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003599 struct mmc_card *card = mmc_dev_to_card(dev);
3600
3601 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003602}
3603
Ulf Hansson0967edc2014-10-06 11:29:42 +02003604static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003606 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003607 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003610 /*
3611 * Resume involves the card going into idle state,
3612 * so current partition is always the main one.
3613 */
3614 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003616 list_for_each_entry(part_md, &md->part, part) {
3617 mmc_queue_resume(&part_md->queue);
3618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620 return 0;
3621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622#endif
3623
Ulf Hansson0967edc2014-10-06 11:29:42 +02003624static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3625
Ulf Hansson96541ba2015-04-14 13:06:12 +02003626static struct mmc_driver mmc_driver = {
3627 .drv = {
3628 .name = "mmcblk",
3629 .pm = &mmc_blk_pm_ops,
3630 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 .probe = mmc_blk_probe,
3632 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003633 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634};
3635
3636static int __init mmc_blk_init(void)
3637{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003638 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003640 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3641 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3642
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003643 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003644
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003645 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3646 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003649 res = mmc_register_driver(&mmc_driver);
3650 if (res)
3651 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003653 return 0;
3654 out2:
3655 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 out:
3657 return res;
3658}
3659
3660static void __exit mmc_blk_exit(void)
3661{
3662 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003663 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664}
3665
3666module_init(mmc_blk_init);
3667module_exit(mmc_blk_exit);
3668
3669MODULE_LICENSE("GPL");
3670MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3671