blob: 1296c997c5be95bc895f50df454756a8566acb9b [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;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302233 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002234 mqrq->mmc_active.err_check = mmc_blk_err_check;
2235
Per Forlin54d49d72011-07-01 18:55:29 +02002236 mmc_queue_bounce_pre(mqrq);
2237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002239static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2240 struct mmc_card *card)
2241{
2242 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2243 unsigned int max_seg_sz = queue_max_segment_size(q);
2244 unsigned int len, nr_segs = 0;
2245
2246 do {
2247 len = min(hdr_sz, max_seg_sz);
2248 hdr_sz -= len;
2249 nr_segs++;
2250 } while (hdr_sz);
2251
2252 return nr_segs;
2253}
2254
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002255/**
2256 * mmc_blk_disable_wr_packing() - disables packing mode
2257 * @mq: MMC queue.
2258 *
2259 */
2260void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2261{
2262 if (mq) {
2263 mq->wr_packing_enabled = false;
2264 mq->num_of_potential_packed_wr_reqs = 0;
2265 }
2266}
2267EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2268
Lee Susman841fd132013-04-23 17:59:26 +03002269static int get_packed_trigger(int potential, struct mmc_card *card,
2270 struct request *req, int curr_trigger)
2271{
2272 static int num_mean_elements = 1;
2273 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2274 unsigned int trigger = curr_trigger;
2275 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2276
2277 /* scale down the upper bound to 75% */
2278 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2279
2280 /*
2281 * since the most common calls for this function are with small
2282 * potential write values and since we don't want these calls to affect
2283 * the packed trigger, set a lower bound and ignore calls with
2284 * potential lower than that bound
2285 */
2286 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2287 return trigger;
2288
2289 /*
2290 * this is to prevent integer overflow in the following calculation:
2291 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2292 */
2293 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2294 num_mean_elements = 1;
2295 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2296 }
2297
2298 /*
2299 * get next mean value based on previous mean value and current
2300 * potential packed writes. Calculation is as follows:
2301 * mean_pot[i+1] =
2302 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2303 */
2304 mean_potential *= num_mean_elements;
2305 /*
2306 * add num_mean_elements so that the division of two integers doesn't
2307 * lower mean_potential too much
2308 */
2309 if (potential > mean_potential)
2310 mean_potential += num_mean_elements;
2311 mean_potential += potential;
2312 /* this is for gaining more precision when dividing two integers */
2313 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2314 /* this completes the mean calculation */
2315 mean_potential /= ++num_mean_elements;
2316 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2317
2318 /*
2319 * if current potential packed writes is greater than the mean potential
2320 * then the heuristic is that the following workload will contain many
2321 * write requests, therefore we lower the packed trigger. In the
2322 * opposite case we want to increase the trigger in order to get less
2323 * packing events.
2324 */
2325 if (potential >= mean_potential)
2326 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2327 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2328 else
2329 trigger = (trigger >= pckd_trgr_upper_bound) ?
2330 pckd_trgr_upper_bound : trigger + 1;
2331
2332 /*
2333 * an urgent read request indicates a packed list being interrupted
2334 * by this read, therefore we aim for less packing, hence the trigger
2335 * gets increased
2336 */
2337 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2338 trigger += PCKD_TRGR_URGENT_PENALTY;
2339
2340 return trigger;
2341}
2342
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002343static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2344 struct request *req)
2345{
2346 struct mmc_host *host = mq->card->host;
2347 int data_dir;
2348
2349 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2350 return;
2351
Maya Erez8e2b3c32012-12-02 13:27:15 +02002352 /* Support for the write packing on eMMC 4.5 or later */
2353 if (mq->card->ext_csd.rev <= 5)
2354 return;
2355
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002356 /*
2357 * In case the packing control is not supported by the host, it should
2358 * not have an effect on the write packing. Therefore we have to enable
2359 * the write packing
2360 */
2361 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2362 mq->wr_packing_enabled = true;
2363 return;
2364 }
2365
2366 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2367 if (mq->num_of_potential_packed_wr_reqs >
2368 mq->num_wr_reqs_to_start_packing)
2369 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002370 mq->num_wr_reqs_to_start_packing =
2371 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2372 mq->card, req,
2373 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002374 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002375 return;
2376 }
2377
2378 data_dir = rq_data_dir(req);
2379
2380 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002381 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002382 mq->num_wr_reqs_to_start_packing =
2383 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2384 mq->card, req,
2385 mq->num_wr_reqs_to_start_packing);
2386 mq->num_of_potential_packed_wr_reqs = 0;
2387 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002388 return;
2389 } else if (data_dir == WRITE) {
2390 mq->num_of_potential_packed_wr_reqs++;
2391 }
2392
2393 if (mq->num_of_potential_packed_wr_reqs >
2394 mq->num_wr_reqs_to_start_packing)
2395 mq->wr_packing_enabled = true;
2396}
2397
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002398struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2399{
2400 if (!card)
2401 return NULL;
2402
2403 return &card->wr_pack_stats;
2404}
2405EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2406
2407void mmc_blk_init_packed_statistics(struct mmc_card *card)
2408{
2409 int max_num_of_packed_reqs = 0;
2410
2411 if (!card || !card->wr_pack_stats.packing_events)
2412 return;
2413
2414 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2415
2416 spin_lock(&card->wr_pack_stats.lock);
2417 memset(card->wr_pack_stats.packing_events, 0,
2418 (max_num_of_packed_reqs + 1) *
2419 sizeof(*card->wr_pack_stats.packing_events));
2420 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2421 sizeof(card->wr_pack_stats.pack_stop_reason));
2422 card->wr_pack_stats.enabled = true;
2423 spin_unlock(&card->wr_pack_stats.lock);
2424}
2425EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2426
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002427static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2428{
2429 struct request_queue *q = mq->queue;
2430 struct mmc_card *card = mq->card;
2431 struct request *cur = req, *next = NULL;
2432 struct mmc_blk_data *md = mq->data;
2433 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2434 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2435 unsigned int req_sectors = 0, phys_segments = 0;
2436 unsigned int max_blk_count, max_phys_segs;
2437 bool put_back = true;
2438 u8 max_packed_rw = 0;
2439 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002440 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002441
Shawn Lin96e52da2016-08-26 08:49:55 +08002442 /*
2443 * We don't need to check packed for any further
2444 * operation of packed stuff as we set MMC_PACKED_NONE
2445 * and return zero for reqs if geting null packed. Also
2446 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2447 * it again when removing blk req.
2448 */
2449 if (!mqrq->packed) {
2450 md->flags &= (~MMC_BLK_PACKED_CMD);
2451 goto no_packed;
2452 }
2453
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002454 if (!(md->flags & MMC_BLK_PACKED_CMD))
2455 goto no_packed;
2456
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002457 if (!mq->wr_packing_enabled)
2458 goto no_packed;
2459
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002460 if ((rq_data_dir(cur) == WRITE) &&
2461 mmc_host_packed_wr(card->host))
2462 max_packed_rw = card->ext_csd.max_packed_writes;
2463
2464 if (max_packed_rw == 0)
2465 goto no_packed;
2466
2467 if (mmc_req_rel_wr(cur) &&
2468 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2469 goto no_packed;
2470
2471 if (mmc_large_sector(card) &&
2472 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2473 goto no_packed;
2474
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002475 if (cur->cmd_flags & REQ_FUA)
2476 goto no_packed;
2477
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002478 mmc_blk_clear_packed(mqrq);
2479
2480 max_blk_count = min(card->host->max_blk_count,
2481 card->host->max_req_size >> 9);
2482 if (unlikely(max_blk_count > 0xffff))
2483 max_blk_count = 0xffff;
2484
2485 max_phys_segs = queue_max_segments(q);
2486 req_sectors += blk_rq_sectors(cur);
2487 phys_segments += cur->nr_phys_segments;
2488
2489 if (rq_data_dir(cur) == WRITE) {
2490 req_sectors += mmc_large_sector(card) ? 8 : 1;
2491 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2492 }
2493
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002494 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002495 do {
2496 if (reqs >= max_packed_rw - 1) {
2497 put_back = false;
2498 break;
2499 }
2500
2501 spin_lock_irq(q->queue_lock);
2502 next = blk_fetch_request(q);
2503 spin_unlock_irq(q->queue_lock);
2504 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002505 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002506 put_back = false;
2507 break;
2508 }
2509
2510 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002511 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2512 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002513 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002514 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002515
Mike Christie3a5e02c2016-06-05 14:32:23 -05002516 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002517 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002518 req_op(next) == REQ_OP_FLUSH) {
2519 if (req_op(next) != REQ_OP_SECURE_ERASE)
2520 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002521 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002522 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002523
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002524 if (next->cmd_flags & REQ_FUA) {
2525 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2526 break;
2527 }
2528
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002529 if (rq_data_dir(cur) != rq_data_dir(next)) {
2530 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002531 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002532 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002533
2534 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002535 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2536 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002537 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002538 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002539
2540 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002541 if (req_sectors > max_blk_count) {
2542 if (stats->enabled)
2543 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002544 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002545 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002546
2547 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002548 if (phys_segments > max_phys_segs) {
2549 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002550 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002551 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002552
Maya Erez5a8dae12014-12-04 15:13:59 +02002553 if (mq->no_pack_for_random) {
2554 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2555 blk_rq_pos(next)) {
2556 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2557 put_back = 1;
2558 break;
2559 }
2560 }
2561
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002562 if (rq_data_dir(next) == WRITE)
2563 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002564 list_add_tail(&next->queuelist, &mqrq->packed->list);
2565 cur = next;
2566 reqs++;
2567 } while (1);
2568
2569 if (put_back) {
2570 spin_lock_irq(q->queue_lock);
2571 blk_requeue_request(q, next);
2572 spin_unlock_irq(q->queue_lock);
2573 }
2574
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002575 if (stats->enabled) {
2576 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2577 stats->packing_events[reqs + 1]++;
2578 if (reqs + 1 == max_packed_rw)
2579 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2580 }
2581
2582 spin_unlock(&stats->lock);
2583
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002584 if (reqs > 0) {
2585 list_add(&req->queuelist, &mqrq->packed->list);
2586 mqrq->packed->nr_entries = ++reqs;
2587 mqrq->packed->retries = reqs;
2588 return reqs;
2589 }
2590
2591no_packed:
2592 mqrq->cmd_type = MMC_PACKED_NONE;
2593 return 0;
2594}
2595
2596static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2597 struct mmc_card *card,
2598 struct mmc_queue *mq)
2599{
2600 struct mmc_blk_request *brq = &mqrq->brq;
2601 struct request *req = mqrq->req;
2602 struct request *prq;
2603 struct mmc_blk_data *md = mq->data;
2604 struct mmc_packed *packed = mqrq->packed;
2605 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002606 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002607 u8 hdr_blocks;
2608 u8 i = 1;
2609
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002610 mqrq->cmd_type = MMC_PACKED_WRITE;
2611 packed->blocks = 0;
2612 packed->idx_failure = MMC_PACKED_NR_IDX;
2613
2614 packed_cmd_hdr = packed->cmd_hdr;
2615 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002616 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2617 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002618 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2619
2620 /*
2621 * Argument for each entry of packed group
2622 */
2623 list_for_each_entry(prq, &packed->list, queuelist) {
2624 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2625 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2626 (prq->cmd_flags & REQ_META) &&
2627 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002628 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002629 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002630 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002631 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2632 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002633 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002634 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002635 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002636 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002637 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002638 packed->blocks += blk_rq_sectors(prq);
2639 i++;
2640 }
2641
2642 memset(brq, 0, sizeof(struct mmc_blk_request));
2643 brq->mrq.cmd = &brq->cmd;
2644 brq->mrq.data = &brq->data;
2645 brq->mrq.sbc = &brq->sbc;
2646 brq->mrq.stop = &brq->stop;
2647
2648 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2649 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2650 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2651
2652 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2653 brq->cmd.arg = blk_rq_pos(req);
2654 if (!mmc_card_blockaddr(card))
2655 brq->cmd.arg <<= 9;
2656 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2657
2658 brq->data.blksz = 512;
2659 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002660 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302661 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002662
2663 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2664 brq->stop.arg = 0;
2665 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2666
2667 mmc_set_data_timeout(&brq->data, card);
2668
2669 brq->data.sg = mqrq->sg;
2670 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2671
2672 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002673
2674 /*
2675 * This is intended for packed commands tests usage - in case these
2676 * functions are not in use the respective pointers are NULL
2677 */
2678 if (mq->err_check_fn)
2679 mqrq->mmc_active.err_check = mq->err_check_fn;
2680 else
2681 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2682
2683 if (mq->packed_test_fn)
2684 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002685
2686 mmc_queue_bounce_pre(mqrq);
2687}
2688
Adrian Hunter67716322011-08-29 16:42:15 +03002689static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2690 struct mmc_blk_request *brq, struct request *req,
2691 int ret)
2692{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002693 struct mmc_queue_req *mq_rq;
2694 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2695
Adrian Hunter67716322011-08-29 16:42:15 +03002696 /*
2697 * If this is an SD card and we're writing, we can first
2698 * mark the known good sectors as ok.
2699 *
2700 * If the card is not SD, we can still ok written sectors
2701 * as reported by the controller (which might be less than
2702 * the real number of written sectors, but never more).
2703 */
2704 if (mmc_card_sd(card)) {
2705 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302706 if (!brq->data.fault_injected) {
2707 blocks = mmc_sd_num_wr_blocks(card);
2708 if (blocks != (u32)-1)
2709 ret = blk_end_request(req, 0, blocks << 9);
2710 } else
2711 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002712 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002713 if (!mmc_packed_cmd(mq_rq->cmd_type))
2714 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002715 }
2716 return ret;
2717}
2718
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002719static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2720{
2721 struct request *prq;
2722 struct mmc_packed *packed = mq_rq->packed;
2723 int idx = packed->idx_failure, i = 0;
2724 int ret = 0;
2725
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002726 while (!list_empty(&packed->list)) {
2727 prq = list_entry_rq(packed->list.next);
2728 if (idx == i) {
2729 /* retry from error index */
2730 packed->nr_entries -= idx;
2731 mq_rq->req = prq;
2732 ret = 1;
2733
2734 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2735 list_del_init(&prq->queuelist);
2736 mmc_blk_clear_packed(mq_rq);
2737 }
2738 return ret;
2739 }
2740 list_del_init(&prq->queuelist);
2741 blk_end_request(prq, 0, blk_rq_bytes(prq));
2742 i++;
2743 }
2744
2745 mmc_blk_clear_packed(mq_rq);
2746 return ret;
2747}
2748
2749static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2750{
2751 struct request *prq;
2752 struct mmc_packed *packed = mq_rq->packed;
2753
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002754 while (!list_empty(&packed->list)) {
2755 prq = list_entry_rq(packed->list.next);
2756 list_del_init(&prq->queuelist);
2757 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2758 }
2759
2760 mmc_blk_clear_packed(mq_rq);
2761}
2762
2763static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2764 struct mmc_queue_req *mq_rq)
2765{
2766 struct request *prq;
2767 struct request_queue *q = mq->queue;
2768 struct mmc_packed *packed = mq_rq->packed;
2769
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002770 while (!list_empty(&packed->list)) {
2771 prq = list_entry_rq(packed->list.prev);
2772 if (prq->queuelist.prev != &packed->list) {
2773 list_del_init(&prq->queuelist);
2774 spin_lock_irq(q->queue_lock);
2775 blk_requeue_request(mq->queue, prq);
2776 spin_unlock_irq(q->queue_lock);
2777 } else {
2778 list_del_init(&prq->queuelist);
2779 }
2780 }
2781
2782 mmc_blk_clear_packed(mq_rq);
2783}
2784
Per Forlinee8a43a2011-07-01 18:55:33 +02002785static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002786{
2787 struct mmc_blk_data *md = mq->data;
2788 struct mmc_card *card = md->queue.card;
2789 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002790 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002791 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002792 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302793 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002794 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002795 const u8 packed_nr = 2;
2796 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002797#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2798 unsigned long waitfor = jiffies;
2799#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002800
2801 if (!rqc && !mq->mqrq_prev->req)
2802 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002803
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002804 if (rqc)
2805 reqs = mmc_blk_prep_packed_list(mq, rqc);
2806
Per Forlin54d49d72011-07-01 18:55:29 +02002807 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002808 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302809 /*
2810 * When 4KB native sector is enabled, only 8 blocks
2811 * multiple read or write is allowed
2812 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002813 if (mmc_large_sector(card) &&
2814 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302815 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2816 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002817 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302818 goto cmd_abort;
2819 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002820
2821 if (reqs >= packed_nr)
2822 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2823 card, mq);
2824 else
2825 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002826 areq = &mq->mqrq_cur->mmc_active;
2827 } else
2828 areq = NULL;
2829 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002830 if (!areq) {
2831 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002832 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02002833 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002834 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002835
Per Forlinee8a43a2011-07-01 18:55:33 +02002836 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2837 brq = &mq_rq->brq;
2838 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002839 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002840 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002841
Per Forlind78d4a82011-07-01 18:55:30 +02002842 switch (status) {
2843 case MMC_BLK_SUCCESS:
2844 case MMC_BLK_PARTIAL:
2845 /*
2846 * A block was successfully transferred.
2847 */
Adrian Hunter67716322011-08-29 16:42:15 +03002848 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002849
Mark Salyzyn6904e432016-01-28 11:12:25 -08002850 mmc_blk_simulate_delay(mq, rqc, waitfor);
2851
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002852 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2853 ret = mmc_blk_end_packed_req(mq_rq);
2854 break;
2855 } else {
2856 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002857 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002858 }
2859
Adrian Hunter67716322011-08-29 16:42:15 +03002860 /*
2861 * If the blk_end_request function returns non-zero even
2862 * though all data has been transferred and no errors
2863 * were returned by the host controller, it's a bug.
2864 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002865 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302866 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002867 __func__, blk_rq_bytes(req),
2868 brq->data.bytes_xfered);
2869 rqc = NULL;
2870 goto cmd_abort;
2871 }
Per Forlind78d4a82011-07-01 18:55:30 +02002872 break;
2873 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002874 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002875 if (mmc_blk_reset(md, card->host, type))
2876 goto cmd_abort;
2877 if (!ret)
2878 goto start_new_req;
2879 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002880 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002881 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02002882 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002883 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002884 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002885 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02002886 if (!mmc_blk_reset(md, card->host, type) &&
2887 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03002888 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002889 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002890 case MMC_BLK_DATA_ERR: {
2891 int err;
2892
2893 err = mmc_blk_reset(md, card->host, type);
2894 if (!err)
2895 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302896 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002897 }
2898 case MMC_BLK_ECC_ERR:
2899 if (brq->data.blocks > 1) {
2900 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002901 pr_warn("%s: retrying using single block read\n",
2902 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002903 disable_multi = 1;
2904 break;
2905 }
Per Forlind78d4a82011-07-01 18:55:30 +02002906 /*
2907 * After an error, we redo I/O one sector at a
2908 * time, so we only reach here after trying to
2909 * read a single sector.
2910 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302911 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002912 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002913 if (!ret)
2914 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002915 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302916 case MMC_BLK_NOMEDIUM:
2917 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002918 default:
2919 pr_err("%s: Unhandled return value (%d)",
2920 req->rq_disk->disk_name, status);
2921 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002922 }
2923
Per Forlinee8a43a2011-07-01 18:55:33 +02002924 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002925 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2926 if (!mq_rq->packed->retries)
2927 goto cmd_abort;
2928 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2929 mmc_start_req(card->host,
2930 &mq_rq->mmc_active, NULL);
2931 } else {
2932
2933 /*
2934 * In case of a incomplete request
2935 * prepare it again and resend.
2936 */
2937 mmc_blk_rw_rq_prep(mq_rq, card,
2938 disable_multi, mq);
2939 mmc_start_req(card->host,
2940 &mq_rq->mmc_active, NULL);
2941 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002942 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 } while (ret);
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return 1;
2947
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002948 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002949 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2950 mmc_blk_abort_packed_req(mq_rq);
2951 } else {
2952 if (mmc_card_removed(card))
2953 req->cmd_flags |= REQ_QUIET;
2954 while (ret)
2955 ret = blk_end_request(req, -EIO,
2956 blk_rq_cur_bytes(req));
2957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Per Forlinee8a43a2011-07-01 18:55:33 +02002959 start_new_req:
2960 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002961 if (mmc_card_removed(card)) {
2962 rqc->cmd_flags |= REQ_QUIET;
2963 blk_end_request_all(rqc, -EIO);
2964 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002965 /*
2966 * If current request is packed, it needs to put back.
2967 */
2968 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2969 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2970
Seungwon Jeon7a819022013-01-22 19:48:07 +09002971 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2972 mmc_start_req(card->host,
2973 &mq->mqrq_cur->mmc_active, NULL);
2974 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002975 }
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
2978}
2979
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002980int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002981{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002982 int ret;
2983 struct mmc_blk_data *md = mq->data;
2984 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002985 struct mmc_host *host = card->host;
2986 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002987 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002988
Per Forlinee8a43a2011-07-01 18:55:33 +02002989 if (req && !mq->mqrq_prev->req)
2990 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002991 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002992
Andrei Warkentin371a6892011-04-11 18:10:25 -05002993 ret = mmc_blk_part_switch(card, md);
2994 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002995 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302996 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002997 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002998 ret = 0;
2999 goto out;
3000 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003001
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003002 mmc_blk_write_packing_control(mq, req);
3003
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003004 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003005 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003006 /* complete ongoing async transfer before issuing discard */
3007 if (card->host->areq)
3008 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003009 ret = mmc_blk_issue_discard_rq(mq, req);
3010 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3011 /* complete ongoing async transfer before issuing secure erase*/
3012 if (card->host->areq)
3013 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003014 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3015 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3016 else
3017 ret = mmc_blk_issue_discard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05003018 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003019 /* complete ongoing async transfer before issuing flush */
3020 if (card->host->areq)
3021 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003022 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003023 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003024 if (!req && host->areq) {
3025 spin_lock_irqsave(&host->context_info.lock, flags);
3026 host->context_info.is_waiting_last_req = true;
3027 spin_unlock_irqrestore(&host->context_info.lock, flags);
3028 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003029 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003030 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003031
Andrei Warkentin371a6892011-04-11 18:10:25 -05003032out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003033 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3034 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003035 /*
3036 * Release host when there are no more requests
3037 * and after special request(discard, flush) is done.
3038 * In case sepecial request, there is no reentry to
3039 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3040 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003041 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003042 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Russell Kinga6f6c962006-01-03 22:38:44 +00003045static inline int mmc_blk_readonly(struct mmc_card *card)
3046{
3047 return mmc_card_readonly(card) ||
3048 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3049}
3050
Andrei Warkentin371a6892011-04-11 18:10:25 -05003051static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3052 struct device *parent,
3053 sector_t size,
3054 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003055 const char *subname,
3056 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct mmc_blk_data *md;
3059 int devidx, ret;
3060
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003061again:
3062 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3063 return ERR_PTR(-ENOMEM);
3064
3065 spin_lock(&mmc_blk_lock);
3066 ret = ida_get_new(&mmc_blk_ida, &devidx);
3067 spin_unlock(&mmc_blk_lock);
3068
3069 if (ret == -EAGAIN)
3070 goto again;
3071 else if (ret)
3072 return ERR_PTR(ret);
3073
3074 if (devidx >= max_devices) {
3075 ret = -ENOSPC;
3076 goto out;
3077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003079 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003080 if (!md) {
3081 ret = -ENOMEM;
3082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003084
Johan Rudholmadd710e2011-12-02 08:51:06 +01003085 md->area_type = area_type;
3086
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003087 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003088 * Set the read-only status based on the supported commands
3089 * and the write protect switch.
3090 */
3091 md->read_only = mmc_blk_readonly(card);
3092
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003093 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003094 if (md->disk == NULL) {
3095 ret = -ENOMEM;
3096 goto err_kfree;
3097 }
3098
3099 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003100 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003101 md->usage = 1;
3102
Adrian Hunterd09408a2011-06-23 13:40:28 +03003103 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003104 if (ret)
3105 goto err_putdisk;
3106
Russell Kinga6f6c962006-01-03 22:38:44 +00003107 md->queue.data = md;
3108
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003109 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003110 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003111 md->disk->fops = &mmc_bdops;
3112 md->disk->private_data = md;
3113 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003114 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003115 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003116 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003117 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003118 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003119
3120 /*
3121 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3122 *
3123 * - be set for removable media with permanent block devices
3124 * - be unset for removable block devices with permanent media
3125 *
3126 * Since MMC block devices clearly fall under the second
3127 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3128 * should use the block device creation/destruction hotplug
3129 * messages to tell when the card is present.
3130 */
3131
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003132 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003133 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003134
Saugata Dasa5075eb2012-05-17 16:32:21 +05303135 if (mmc_card_mmc(card))
3136 blk_queue_logical_block_size(md->queue.queue,
3137 card->ext_csd.data_sector_size);
3138 else
3139 blk_queue_logical_block_size(md->queue.queue, 512);
3140
Andrei Warkentin371a6892011-04-11 18:10:25 -05003141 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003142
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003143 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003144 if ((mmc_card_mmc(card) &&
3145 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003146 (mmc_card_sd(card) &&
3147 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3148 md->flags |= MMC_BLK_CMD23;
3149 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003150
3151 if (mmc_card_mmc(card) &&
3152 md->flags & MMC_BLK_CMD23 &&
3153 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3154 card->ext_csd.rel_sectors)) {
3155 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003156 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003157 }
3158
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003159 if (mmc_card_mmc(card) &&
3160 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3161 (md->flags & MMC_BLK_CMD23) &&
3162 card->ext_csd.packed_event_en) {
3163 if (!mmc_packed_init(&md->queue, card))
3164 md->flags |= MMC_BLK_PACKED_CMD;
3165 }
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003168
3169 err_putdisk:
3170 put_disk(md->disk);
3171 err_kfree:
3172 kfree(md);
3173 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003174 spin_lock(&mmc_blk_lock);
3175 ida_remove(&mmc_blk_ida, devidx);
3176 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003177 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Andrei Warkentin371a6892011-04-11 18:10:25 -05003180static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3181{
3182 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003183
3184 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3185 /*
3186 * The EXT_CSD sector count is in number or 512 byte
3187 * sectors.
3188 */
3189 size = card->ext_csd.sectors;
3190 } else {
3191 /*
3192 * The CSD capacity field is in units of read_blkbits.
3193 * set_capacity takes units of 512 bytes.
3194 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003195 size = (typeof(sector_t))card->csd.capacity
3196 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003197 }
3198
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003199 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003200 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003201}
3202
3203static int mmc_blk_alloc_part(struct mmc_card *card,
3204 struct mmc_blk_data *md,
3205 unsigned int part_type,
3206 sector_t size,
3207 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003208 const char *subname,
3209 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003210{
3211 char cap_str[10];
3212 struct mmc_blk_data *part_md;
3213
3214 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003215 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003216 if (IS_ERR(part_md))
3217 return PTR_ERR(part_md);
3218 part_md->part_type = part_type;
3219 list_add(&part_md->part, &md->part);
3220
James Bottomleyb9f28d82015-03-05 18:47:01 -08003221 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003222 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303223 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003224 part_md->disk->disk_name, mmc_card_id(card),
3225 mmc_card_name(card), part_md->part_type, cap_str);
3226 return 0;
3227}
3228
Namjae Jeone0c368d2011-10-06 23:41:38 +09003229/* MMC Physical partitions consist of two boot partitions and
3230 * up to four general purpose partitions.
3231 * For each partition enabled in EXT_CSD a block device will be allocatedi
3232 * to provide access to the partition.
3233 */
3234
Andrei Warkentin371a6892011-04-11 18:10:25 -05003235static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3236{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003237 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003238
3239 if (!mmc_card_mmc(card))
3240 return 0;
3241
Namjae Jeone0c368d2011-10-06 23:41:38 +09003242 for (idx = 0; idx < card->nr_parts; idx++) {
3243 if (card->part[idx].size) {
3244 ret = mmc_blk_alloc_part(card, md,
3245 card->part[idx].part_cfg,
3246 card->part[idx].size >> 9,
3247 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003248 card->part[idx].name,
3249 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003250 if (ret)
3251 return ret;
3252 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003253 }
3254
3255 return ret;
3256}
3257
Andrei Warkentin371a6892011-04-11 18:10:25 -05003258static void mmc_blk_remove_req(struct mmc_blk_data *md)
3259{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003260 struct mmc_card *card;
3261
Andrei Warkentin371a6892011-04-11 18:10:25 -05003262 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003263 /*
3264 * Flush remaining requests and free queues. It
3265 * is freeing the queue that stops new requests
3266 * from being accepted.
3267 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003268 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003269 mmc_cleanup_queue(&md->queue);
3270 if (md->flags & MMC_BLK_PACKED_CMD)
3271 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003272 device_remove_file(disk_to_dev(md->disk),
3273 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003274 if (md->disk->flags & GENHD_FL_UP) {
3275 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003276 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3277 card->ext_csd.boot_ro_lockable)
3278 device_remove_file(disk_to_dev(md->disk),
3279 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003280#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3281 device_remove_file(disk_to_dev(md->disk),
3282 &dev_attr_max_write_speed);
3283 device_remove_file(disk_to_dev(md->disk),
3284 &dev_attr_max_read_speed);
3285 device_remove_file(disk_to_dev(md->disk),
3286 &dev_attr_cache_size);
3287#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003288
Andrei Warkentin371a6892011-04-11 18:10:25 -05003289 del_gendisk(md->disk);
3290 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003291 mmc_blk_put(md);
3292 }
3293}
3294
3295static void mmc_blk_remove_parts(struct mmc_card *card,
3296 struct mmc_blk_data *md)
3297{
3298 struct list_head *pos, *q;
3299 struct mmc_blk_data *part_md;
3300
3301 list_for_each_safe(pos, q, &md->part) {
3302 part_md = list_entry(pos, struct mmc_blk_data, part);
3303 list_del(pos);
3304 mmc_blk_remove_req(part_md);
3305 }
3306}
3307
3308static int mmc_add_disk(struct mmc_blk_data *md)
3309{
3310 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003311 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003312
Dan Williams307d8e62016-06-20 10:40:44 -07003313 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003314 md->force_ro.show = force_ro_show;
3315 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303316 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003317 md->force_ro.attr.name = "force_ro";
3318 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3319 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3320 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003321 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003322#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3323 atomic_set(&md->queue.max_write_speed, max_write_speed);
3324 ret = device_create_file(disk_to_dev(md->disk),
3325 &dev_attr_max_write_speed);
3326 if (ret)
3327 goto max_write_speed_fail;
3328 atomic_set(&md->queue.max_read_speed, max_read_speed);
3329 ret = device_create_file(disk_to_dev(md->disk),
3330 &dev_attr_max_read_speed);
3331 if (ret)
3332 goto max_read_speed_fail;
3333 atomic_set(&md->queue.cache_size, cache_size);
3334 atomic_long_set(&md->queue.cache_used, 0);
3335 md->queue.cache_jiffies = jiffies;
3336 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3337 if (ret)
3338 goto cache_size_fail;
3339#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003340
3341 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3342 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003343 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003344
3345 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3346 mode = S_IRUGO;
3347 else
3348 mode = S_IRUGO | S_IWUSR;
3349
3350 md->power_ro_lock.show = power_ro_lock_show;
3351 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003352 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003353 md->power_ro_lock.attr.mode = mode;
3354 md->power_ro_lock.attr.name =
3355 "ro_lock_until_next_power_on";
3356 ret = device_create_file(disk_to_dev(md->disk),
3357 &md->power_ro_lock);
3358 if (ret)
3359 goto power_ro_lock_fail;
3360 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003361
3362 md->num_wr_reqs_to_start_packing.show =
3363 num_wr_reqs_to_start_packing_show;
3364 md->num_wr_reqs_to_start_packing.store =
3365 num_wr_reqs_to_start_packing_store;
3366 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3367 md->num_wr_reqs_to_start_packing.attr.name =
3368 "num_wr_reqs_to_start_packing";
3369 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3370 ret = device_create_file(disk_to_dev(md->disk),
3371 &md->num_wr_reqs_to_start_packing);
3372 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003373 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003374
Maya Erez5a8dae12014-12-04 15:13:59 +02003375 md->no_pack_for_random.show = no_pack_for_random_show;
3376 md->no_pack_for_random.store = no_pack_for_random_store;
3377 sysfs_attr_init(&md->no_pack_for_random.attr);
3378 md->no_pack_for_random.attr.name = "no_pack_for_random";
3379 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3380 ret = device_create_file(disk_to_dev(md->disk),
3381 &md->no_pack_for_random);
3382 if (ret)
3383 goto no_pack_for_random_fails;
3384
Johan Rudholmadd710e2011-12-02 08:51:06 +01003385 return ret;
3386
Maya Erez5a8dae12014-12-04 15:13:59 +02003387no_pack_for_random_fails:
3388 device_remove_file(disk_to_dev(md->disk),
3389 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003390num_wr_reqs_to_start_packing_fail:
3391 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003392power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003393#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3394 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3395cache_size_fail:
3396 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3397max_read_speed_fail:
3398 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3399max_write_speed_fail:
3400#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003401 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3402force_ro_fail:
3403 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003404
3405 return ret;
3406}
3407
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003408static const struct mmc_fixup blk_fixups[] =
3409{
Chris Ballc59d4472011-11-11 22:01:43 -05003410 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3411 MMC_QUIRK_INAND_CMD38),
3412 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3413 MMC_QUIRK_INAND_CMD38),
3414 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3415 MMC_QUIRK_INAND_CMD38),
3416 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3417 MMC_QUIRK_INAND_CMD38),
3418 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3419 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003420
3421 /*
3422 * Some MMC cards experience performance degradation with CMD23
3423 * instead of CMD12-bounded multiblock transfers. For now we'll
3424 * black list what's bad...
3425 * - Certain Toshiba cards.
3426 *
3427 * N.B. This doesn't affect SD cards.
3428 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003429 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3430 MMC_QUIRK_BLK_NO_CMD23),
3431 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3432 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003433 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003434 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003435 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003436 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003437 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003438 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003439
3440 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003441 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003442 */
Chris Ballc59d4472011-11-11 22:01:43 -05003443 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003444 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003445 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3446 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003447
Ian Chen3550ccd2012-08-29 15:05:36 +09003448 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003449 * Some Samsung MMC cards need longer data read timeout than
3450 * indicated in CSD.
3451 */
3452 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3453 MMC_QUIRK_LONG_READ_TIME),
3454
3455 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003456 * On these Samsung MoviNAND parts, performing secure erase or
3457 * secure trim can result in unrecoverable corruption due to a
3458 * firmware bug.
3459 */
3460 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3461 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3462 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3463 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3464 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3465 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3466 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3467 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3468 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3469 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3470 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3471 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3472 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3473 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3474 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3475 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3476
Shawn Linb5b4ff02015-08-12 13:08:32 +08003477 /*
3478 * On Some Kingston eMMCs, performing trim can result in
3479 * unrecoverable data conrruption occasionally due to a firmware bug.
3480 */
3481 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3482 MMC_QUIRK_TRIM_BROKEN),
3483 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3484 MMC_QUIRK_TRIM_BROKEN),
3485
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003486 /* Some INAND MCP devices advertise incorrect timeout values */
3487 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3488 MMC_QUIRK_INAND_DATA_TIMEOUT),
3489
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003490 END_FIXUP
3491};
3492
Ulf Hansson96541ba2015-04-14 13:06:12 +02003493static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003495 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003496 char cap_str[10];
3497
Pierre Ossman912490d2005-05-21 10:27:02 +01003498 /*
3499 * Check that the card supports the command class(es) we need.
3500 */
3501 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return -ENODEV;
3503
Lukas Czerner5204d002014-06-18 13:18:07 +02003504 mmc_fixup_device(card, blk_fixups);
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 md = mmc_blk_alloc(card);
3507 if (IS_ERR(md))
3508 return PTR_ERR(md);
3509
James Bottomleyb9f28d82015-03-05 18:47:01 -08003510 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003511 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303512 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003514 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Andrei Warkentin371a6892011-04-11 18:10:25 -05003516 if (mmc_blk_alloc_parts(card, md))
3517 goto out;
3518
Ulf Hansson96541ba2015-04-14 13:06:12 +02003519 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003520
Andrei Warkentin371a6892011-04-11 18:10:25 -05003521 if (mmc_add_disk(md))
3522 goto out;
3523
3524 list_for_each_entry(part_md, &md->part, part) {
3525 if (mmc_add_disk(part_md))
3526 goto out;
3527 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003528
3529 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3530 pm_runtime_use_autosuspend(&card->dev);
3531
3532 /*
3533 * Don't enable runtime PM for SD-combo cards here. Leave that
3534 * decision to be taken during the SDIO init sequence instead.
3535 */
3536 if (card->type != MMC_TYPE_SD_COMBO) {
3537 pm_runtime_set_active(&card->dev);
3538 pm_runtime_enable(&card->dev);
3539 }
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 return 0;
3542
3543 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003544 mmc_blk_remove_parts(card, md);
3545 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Ulf Hansson96541ba2015-04-14 13:06:12 +02003549static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003551 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Andrei Warkentin371a6892011-04-11 18:10:25 -05003553 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003554 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003555 mmc_claim_host(card->host);
3556 mmc_blk_part_switch(card, md);
3557 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003558 if (card->type != MMC_TYPE_SD_COMBO)
3559 pm_runtime_disable(&card->dev);
3560 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003561 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003562 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Ulf Hansson96541ba2015-04-14 13:06:12 +02003565static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003567 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003568 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303569 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303572 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303573 if (rc)
3574 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003575 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303576 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303577 if (rc)
3578 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303581 goto out;
3582
3583 out_resume:
3584 mmc_queue_resume(&md->queue);
3585 list_for_each_entry(part_md, &md->part, part) {
3586 mmc_queue_resume(&part_md->queue);
3587 }
3588 out:
3589 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Ulf Hansson96541ba2015-04-14 13:06:12 +02003592static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003593{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003594 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003595}
3596
Ulf Hansson0967edc2014-10-06 11:29:42 +02003597#ifdef CONFIG_PM_SLEEP
3598static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003599{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003600 struct mmc_card *card = mmc_dev_to_card(dev);
3601
3602 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003603}
3604
Ulf Hansson0967edc2014-10-06 11:29:42 +02003605static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003607 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003608 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003611 /*
3612 * Resume involves the card going into idle state,
3613 * so current partition is always the main one.
3614 */
3615 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003617 list_for_each_entry(part_md, &md->part, part) {
3618 mmc_queue_resume(&part_md->queue);
3619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621 return 0;
3622}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623#endif
3624
Ulf Hansson0967edc2014-10-06 11:29:42 +02003625static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3626
Ulf Hansson96541ba2015-04-14 13:06:12 +02003627static struct mmc_driver mmc_driver = {
3628 .drv = {
3629 .name = "mmcblk",
3630 .pm = &mmc_blk_pm_ops,
3631 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 .probe = mmc_blk_probe,
3633 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003634 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635};
3636
3637static int __init mmc_blk_init(void)
3638{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003639 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003641 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3642 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3643
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003644 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003645
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003646 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3647 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003650 res = mmc_register_driver(&mmc_driver);
3651 if (res)
3652 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003654 return 0;
3655 out2:
3656 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 out:
3658 return res;
3659}
3660
3661static void __exit mmc_blk_exit(void)
3662{
3663 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003664 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
3667module_init(mmc_blk_init);
3668module_exit(mmc_blk_exit);
3669
3670MODULE_LICENSE("GPL");
3671MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3672