blob: 5ec3a98910a3c83faddc0d51ac8aea30ca9d9578 [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 */
1510 pr_err("%s: %s sending %s command, card status %#x\n",
1511 req->rq_disk->disk_name, "response CRC error",
1512 name, status);
1513 return ERR_RETRY;
1514
1515 case -ETIMEDOUT:
1516 pr_err("%s: %s sending %s command, card status %#x\n",
1517 req->rq_disk->disk_name, "timed out", name, status);
1518
1519 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301520 if (!status_valid) {
1521 pr_err("%s: status not valid, retrying timeout\n",
1522 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001523 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301524 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001525
1526 /*
1527 * If it was a r/w cmd crc error, or illegal command
1528 * (eg, issued in wrong state) then retry - we should
1529 * have corrected the state problem above.
1530 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301531 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
1532 pr_err("%s: command error, retrying timeout\n",
1533 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001534 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301535 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001536
1537 /* Otherwise abort the command */
1538 return ERR_ABORT;
1539
1540 default:
1541 /* We don't understand the error code the driver gave us */
1542 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1543 req->rq_disk->disk_name, error, status);
1544 return ERR_ABORT;
1545 }
1546}
1547
1548/*
1549 * Initial r/w and stop cmd error recovery.
1550 * We don't know whether the card received the r/w cmd or not, so try to
1551 * restore things back to a sane state. Essentially, we do this as follows:
1552 * - Obtain card status. If the first attempt to obtain card status fails,
1553 * the status word will reflect the failed status cmd, not the failed
1554 * r/w cmd. If we fail to obtain card status, it suggests we can no
1555 * longer communicate with the card.
1556 * - Check the card state. If the card received the cmd but there was a
1557 * transient problem with the response, it might still be in a data transfer
1558 * mode. Try to send it a stop command. If this fails, we can't recover.
1559 * - If the r/w cmd failed due to a response CRC error, it was probably
1560 * transient, so retry the cmd.
1561 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1562 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1563 * illegal cmd, retry.
1564 * Otherwise we don't understand what happened, so abort.
1565 */
1566static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001567 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001568{
1569 bool prev_cmd_status_valid = true;
1570 u32 status, stop_status = 0;
1571 int err, retry;
1572
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301573 if (mmc_card_removed(card))
1574 return ERR_NOMEDIUM;
1575
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001576 /*
1577 * Try to get card status which indicates both the card state
1578 * and why there was no response. If the first attempt fails,
1579 * we can't be sure the returned status is for the r/w command.
1580 */
1581 for (retry = 2; retry >= 0; retry--) {
1582 err = get_card_status(card, &status, 0);
1583 if (!err)
1584 break;
1585
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001586 /* Re-tune if needed */
1587 mmc_retune_recheck(card->host);
1588
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001589 prev_cmd_status_valid = false;
1590 pr_err("%s: error %d sending status command, %sing\n",
1591 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1592 }
1593
1594 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301595 if (err) {
1596 /* Check if the card is removed */
1597 if (mmc_detect_card_removed(card->host))
1598 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001599 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301600 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001601
Adrian Hunter67716322011-08-29 16:42:15 +03001602 /* Flag ECC errors */
1603 if ((status & R1_CARD_ECC_FAILED) ||
1604 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1605 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1606 *ecc_err = 1;
1607
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001608 /* Flag General errors */
1609 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1610 if ((status & R1_ERROR) ||
1611 (brq->stop.resp[0] & R1_ERROR)) {
1612 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1613 req->rq_disk->disk_name, __func__,
1614 brq->stop.resp[0], status);
1615 *gen_err = 1;
1616 }
1617
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001618 /*
1619 * Check the current card state. If it is in some data transfer
1620 * mode, tell it to stop (and hopefully transition back to TRAN.)
1621 */
1622 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1623 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001624 err = send_stop(card,
1625 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1626 req, gen_err, &stop_status);
1627 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001628 pr_err("%s: error %d sending stop command\n",
1629 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001630 /*
1631 * If the stop cmd also timed out, the card is probably
1632 * not present, so abort. Other errors are bad news too.
1633 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001634 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001635 }
1636
Adrian Hunter67716322011-08-29 16:42:15 +03001637 if (stop_status & R1_CARD_ECC_FAILED)
1638 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001639 }
1640
1641 /* Check for set block count errors */
1642 if (brq->sbc.error)
1643 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1644 prev_cmd_status_valid, status);
1645
1646 /* Check for r/w command errors */
1647 if (brq->cmd.error)
1648 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1649 prev_cmd_status_valid, status);
1650
Adrian Hunter67716322011-08-29 16:42:15 +03001651 /* Data errors */
1652 if (!brq->stop.error)
1653 return ERR_CONTINUE;
1654
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001655 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001656 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 +01001657 req->rq_disk->disk_name, brq->stop.error,
1658 brq->cmd.resp[0], status);
1659
1660 /*
1661 * Subsitute in our own stop status as this will give the error
1662 * state which happened during the execution of the r/w command.
1663 */
1664 if (stop_status) {
1665 brq->stop.resp[0] = stop_status;
1666 brq->stop.error = 0;
1667 }
1668 return ERR_CONTINUE;
1669}
1670
Adrian Hunter67716322011-08-29 16:42:15 +03001671static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1672 int type)
1673{
1674 int err;
1675
1676 if (md->reset_done & type)
1677 return -EEXIST;
1678
1679 md->reset_done |= type;
1680 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301681 if (err && err != -EOPNOTSUPP) {
1682 /* We failed to reset so we need to abort the request */
1683 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1684 __func__, err);
1685 return -ENODEV;
1686 }
1687
Adrian Hunter67716322011-08-29 16:42:15 +03001688 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301689 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001690 struct mmc_blk_data *main_md =
1691 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001692 int part_err;
1693
1694 main_md->part_curr = main_md->part_type;
1695 part_err = mmc_blk_part_switch(host->card, md);
1696 if (part_err) {
1697 /*
1698 * We have failed to get back into the correct
1699 * partition, so we need to abort the whole request.
1700 */
1701 return -ENODEV;
1702 }
1703 }
1704 return err;
1705}
1706
1707static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1708{
1709 md->reset_done &= ~type;
1710}
1711
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001712int mmc_access_rpmb(struct mmc_queue *mq)
1713{
1714 struct mmc_blk_data *md = mq->data;
1715 /*
1716 * If this is a RPMB partition access, return ture
1717 */
1718 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1719 return true;
1720
1721 return false;
1722}
1723
Adrian Hunterbd788c92010-08-11 14:17:47 -07001724static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1725{
1726 struct mmc_blk_data *md = mq->data;
1727 struct mmc_card *card = md->queue.card;
1728 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001729 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001730
Adrian Hunterbd788c92010-08-11 14:17:47 -07001731 if (!mmc_can_erase(card)) {
1732 err = -EOPNOTSUPP;
1733 goto out;
1734 }
1735
1736 from = blk_rq_pos(req);
1737 nr = blk_rq_sectors(req);
1738
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001739 if (mmc_can_discard(card))
1740 arg = MMC_DISCARD_ARG;
1741 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001742 arg = MMC_TRIM_ARG;
1743 else
1744 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001745retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001746 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1747 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1748 INAND_CMD38_ARG_EXT_CSD,
1749 arg == MMC_TRIM_ARG ?
1750 INAND_CMD38_ARG_TRIM :
1751 INAND_CMD38_ARG_ERASE,
1752 0);
1753 if (err)
1754 goto out;
1755 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001756 err = mmc_erase(card, from, nr, arg);
1757out:
Adrian Hunter67716322011-08-29 16:42:15 +03001758 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1759 goto retry;
1760 if (!err)
1761 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301762 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001763
Adrian Hunterbd788c92010-08-11 14:17:47 -07001764 return err ? 0 : 1;
1765}
1766
Adrian Hunter49804542010-08-11 14:17:50 -07001767static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1768 struct request *req)
1769{
1770 struct mmc_blk_data *md = mq->data;
1771 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001772 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001773 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001774
Maya Erez775a9362013-04-18 15:41:55 +03001775 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001776 err = -EOPNOTSUPP;
1777 goto out;
1778 }
1779
1780 from = blk_rq_pos(req);
1781 nr = blk_rq_sectors(req);
1782
Maya Erez775a9362013-04-18 15:41:55 +03001783 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1784 arg = MMC_SECURE_TRIM1_ARG;
1785 else
1786 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001787
Adrian Hunter67716322011-08-29 16:42:15 +03001788retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001789 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1790 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1791 INAND_CMD38_ARG_EXT_CSD,
1792 arg == MMC_SECURE_TRIM1_ARG ?
1793 INAND_CMD38_ARG_SECTRIM1 :
1794 INAND_CMD38_ARG_SECERASE,
1795 0);
1796 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001797 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001798 }
Adrian Hunter28302812012-04-05 14:45:48 +03001799
Adrian Hunter49804542010-08-11 14:17:50 -07001800 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001801 if (err == -EIO)
1802 goto out_retry;
1803 if (err)
1804 goto out;
1805
1806 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001807 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1808 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1809 INAND_CMD38_ARG_EXT_CSD,
1810 INAND_CMD38_ARG_SECTRIM2,
1811 0);
1812 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001813 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001814 }
Adrian Hunter28302812012-04-05 14:45:48 +03001815
Adrian Hunter49804542010-08-11 14:17:50 -07001816 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001817 if (err == -EIO)
1818 goto out_retry;
1819 if (err)
1820 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001821 }
Adrian Hunter28302812012-04-05 14:45:48 +03001822
Adrian Hunter28302812012-04-05 14:45:48 +03001823out_retry:
1824 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001825 goto retry;
1826 if (!err)
1827 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001828out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301829 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001830
Adrian Hunter49804542010-08-11 14:17:50 -07001831 return err ? 0 : 1;
1832}
1833
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001834static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1835{
1836 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001837 struct mmc_card *card = md->queue.card;
1838 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001839
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001840 ret = mmc_flush_cache(card);
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001841 if (ret == -ENODEV) {
1842 pr_err("%s: %s: restart mmc card",
1843 req->rq_disk->disk_name, __func__);
1844 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
1845 pr_err("%s: %s: fail to restart mmc",
1846 req->rq_disk->disk_name, __func__);
1847 else
1848 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
1849 }
1850
1851 if (ret) {
1852 pr_err("%s: %s: notify flush error to upper layers",
1853 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001854 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001855 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001856
Mark Salyzyn6904e432016-01-28 11:12:25 -08001857#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1858 else if (atomic_read(&mq->cache_size)) {
1859 long used = mmc_blk_cache_used(mq, jiffies);
1860
1861 if (used) {
1862 int speed = atomic_read(&mq->max_write_speed);
1863
1864 if (speed_valid(speed)) {
1865 unsigned long msecs = jiffies_to_msecs(
1866 size_and_speed_to_jiffies(
1867 used, speed));
1868 if (msecs)
1869 msleep(msecs);
1870 }
1871 }
1872 }
1873#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301874 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001875
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001876 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001877}
1878
1879/*
1880 * Reformat current write as a reliable write, supporting
1881 * both legacy and the enhanced reliable write MMC cards.
1882 * In each transfer we'll handle only as much as a single
1883 * reliable write can handle, thus finish the request in
1884 * partial completions.
1885 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001886static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1887 struct mmc_card *card,
1888 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001889{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001890 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1891 /* Legacy mode imposes restrictions on transfers. */
1892 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1893 brq->data.blocks = 1;
1894
1895 if (brq->data.blocks > card->ext_csd.rel_sectors)
1896 brq->data.blocks = card->ext_csd.rel_sectors;
1897 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1898 brq->data.blocks = 1;
1899 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001900}
1901
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001902#define CMD_ERRORS \
1903 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1904 R1_ADDRESS_ERROR | /* Misaligned address */ \
1905 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1906 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1907 R1_CC_ERROR | /* Card controller error */ \
1908 R1_ERROR) /* General/unknown error */
1909
Per Forlinee8a43a2011-07-01 18:55:33 +02001910static int mmc_blk_err_check(struct mmc_card *card,
1911 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001912{
Per Forlinee8a43a2011-07-01 18:55:33 +02001913 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1914 mmc_active);
1915 struct mmc_blk_request *brq = &mq_mrq->brq;
1916 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001917 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001918 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001919
1920 /*
1921 * sbc.error indicates a problem with the set block count
1922 * command. No data will have been transferred.
1923 *
1924 * cmd.error indicates a problem with the r/w command. No
1925 * data will have been transferred.
1926 *
1927 * stop.error indicates a problem with the stop command. Data
1928 * may have been transferred, or may still be transferring.
1929 */
Adrian Hunter67716322011-08-29 16:42:15 +03001930 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1931 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001932 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001933 case ERR_RETRY:
1934 return MMC_BLK_RETRY;
1935 case ERR_ABORT:
1936 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301937 case ERR_NOMEDIUM:
1938 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001939 case ERR_CONTINUE:
1940 break;
1941 }
1942 }
1943
1944 /*
1945 * Check for errors relating to the execution of the
1946 * initial command - such as address errors. No data
1947 * has been transferred.
1948 */
1949 if (brq->cmd.resp[0] & CMD_ERRORS) {
1950 pr_err("%s: r/w command failed, status = %#x\n",
1951 req->rq_disk->disk_name, brq->cmd.resp[0]);
1952 return MMC_BLK_ABORT;
1953 }
1954
1955 /*
1956 * Everything else is either success, or a data error of some
1957 * kind. If it was a write, we may have transitioned to
1958 * program mode, which we have to wait for it to complete.
1959 */
1960 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001961 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001962
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001963 /* Check stop command response */
1964 if (brq->stop.resp[0] & R1_ERROR) {
1965 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1966 req->rq_disk->disk_name, __func__,
1967 brq->stop.resp[0]);
1968 gen_err = 1;
1969 }
1970
Ulf Hansson95a91292014-01-29 13:11:27 +01001971 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1972 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001973 if (err)
1974 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02001975 }
1976
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001977 /* if general error occurs, retry the write operation. */
1978 if (gen_err) {
1979 pr_warn("%s: retrying write for general error\n",
1980 req->rq_disk->disk_name);
1981 return MMC_BLK_RETRY;
1982 }
1983
Per Forlind78d4a82011-07-01 18:55:30 +02001984 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03001985 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00001986 pr_debug("%s: retrying because a re-tune was needed\n",
1987 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03001988 brq->retune_retry_done = 1;
1989 return MMC_BLK_RETRY;
1990 }
Per Forlind78d4a82011-07-01 18:55:30 +02001991 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1992 req->rq_disk->disk_name, brq->data.error,
1993 (unsigned)blk_rq_pos(req),
1994 (unsigned)blk_rq_sectors(req),
1995 brq->cmd.resp[0], brq->stop.resp[0]);
1996
1997 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03001998 if (ecc_err)
1999 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002000 return MMC_BLK_DATA_ERR;
2001 } else {
2002 return MMC_BLK_CMD_ERR;
2003 }
2004 }
2005
Adrian Hunter67716322011-08-29 16:42:15 +03002006 if (!brq->data.bytes_xfered)
2007 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002008
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002009 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2010 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2011 return MMC_BLK_PARTIAL;
2012 else
2013 return MMC_BLK_SUCCESS;
2014 }
2015
Adrian Hunter67716322011-08-29 16:42:15 +03002016 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2017 return MMC_BLK_PARTIAL;
2018
2019 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002020}
2021
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002022static int mmc_blk_packed_err_check(struct mmc_card *card,
2023 struct mmc_async_req *areq)
2024{
2025 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2026 mmc_active);
2027 struct request *req = mq_rq->req;
2028 struct mmc_packed *packed = mq_rq->packed;
2029 int err, check, status;
2030 u8 *ext_csd;
2031
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002032 packed->retries--;
2033 check = mmc_blk_err_check(card, areq);
2034 err = get_card_status(card, &status, 0);
2035 if (err) {
2036 pr_err("%s: error %d sending status command\n",
2037 req->rq_disk->disk_name, err);
2038 return MMC_BLK_ABORT;
2039 }
2040
2041 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002042 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002043 if (err) {
2044 pr_err("%s: error %d sending ext_csd\n",
2045 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002046 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002047 }
2048
2049 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2050 EXT_CSD_PACKED_FAILURE) &&
2051 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2052 EXT_CSD_PACKED_GENERIC_ERROR)) {
2053 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2054 EXT_CSD_PACKED_INDEXED_ERROR) {
2055 packed->idx_failure =
2056 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2057 check = MMC_BLK_PARTIAL;
2058 }
2059 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2060 "failure index: %d\n",
2061 req->rq_disk->disk_name, packed->nr_entries,
2062 packed->blocks, packed->idx_failure);
2063 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002064 kfree(ext_csd);
2065 }
2066
2067 return check;
2068}
2069
Per Forlin54d49d72011-07-01 18:55:29 +02002070static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2071 struct mmc_card *card,
2072 int disable_multi,
2073 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Per Forlin54d49d72011-07-01 18:55:29 +02002075 u32 readcmd, writecmd;
2076 struct mmc_blk_request *brq = &mqrq->brq;
2077 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302079 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002081 /*
2082 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002083 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002084 */
Luca Porziod3df0462015-11-06 15:12:26 +00002085 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002086 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002087 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002088
Per Forlin54d49d72011-07-01 18:55:29 +02002089 memset(brq, 0, sizeof(struct mmc_blk_request));
2090 brq->mrq.cmd = &brq->cmd;
2091 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Per Forlin54d49d72011-07-01 18:55:29 +02002093 brq->cmd.arg = blk_rq_pos(req);
2094 if (!mmc_card_blockaddr(card))
2095 brq->cmd.arg <<= 9;
2096 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2097 brq->data.blksz = 512;
2098 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2099 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002100 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Asutosh Dasf0665412012-07-27 18:10:19 +05302102 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002103 /*
2104 * The block layer doesn't support all sector count
2105 * restrictions, so we need to be prepared for too big
2106 * requests.
2107 */
2108 if (brq->data.blocks > card->host->max_blk_count)
2109 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002111 if (brq->data.blocks > 1) {
2112 /*
2113 * After a read error, we redo the request one sector
2114 * at a time in order to accurately determine which
2115 * sectors can be read successfully.
2116 */
2117 if (disable_multi)
2118 brq->data.blocks = 1;
2119
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002120 /*
2121 * Some controllers have HW issues while operating
2122 * in multiple I/O mode
2123 */
2124 if (card->host->ops->multi_io_quirk)
2125 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2126 (rq_data_dir(req) == READ) ?
2127 MMC_DATA_READ : MMC_DATA_WRITE,
2128 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002129 }
Per Forlin54d49d72011-07-01 18:55:29 +02002130
2131 if (brq->data.blocks > 1 || do_rel_wr) {
2132 /* SPI multiblock writes terminate using a special
2133 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002134 */
Per Forlin54d49d72011-07-01 18:55:29 +02002135 if (!mmc_host_is_spi(card->host) ||
2136 rq_data_dir(req) == READ)
2137 brq->mrq.stop = &brq->stop;
2138 readcmd = MMC_READ_MULTIPLE_BLOCK;
2139 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2140 } else {
2141 brq->mrq.stop = NULL;
2142 readcmd = MMC_READ_SINGLE_BLOCK;
2143 writecmd = MMC_WRITE_BLOCK;
2144 }
2145 if (rq_data_dir(req) == READ) {
2146 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002147 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002148 if (brq->mrq.stop)
2149 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2150 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002151 } else {
2152 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002153 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002154 if (brq->mrq.stop)
2155 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2156 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002157 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002158
Per Forlin54d49d72011-07-01 18:55:29 +02002159 if (do_rel_wr)
2160 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002161
Per Forlin54d49d72011-07-01 18:55:29 +02002162 /*
Saugata Das42659002011-12-21 13:09:17 +05302163 * Data tag is used only during writing meta data to speed
2164 * up write and any subsequent read of this meta data
2165 */
2166 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2167 (req->cmd_flags & REQ_META) &&
2168 (rq_data_dir(req) == WRITE) &&
2169 ((brq->data.blocks * brq->data.blksz) >=
2170 card->ext_csd.data_tag_unit_size);
2171
2172 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002173 * Pre-defined multi-block transfers are preferable to
2174 * open ended-ones (and necessary for reliable writes).
2175 * However, it is not sufficient to just send CMD23,
2176 * and avoid the final CMD12, as on an error condition
2177 * CMD12 (stop) needs to be sent anyway. This, coupled
2178 * with Auto-CMD23 enhancements provided by some
2179 * hosts, means that the complexity of dealing
2180 * with this is best left to the host. If CMD23 is
2181 * supported by card and host, we'll fill sbc in and let
2182 * the host deal with handling it correctly. This means
2183 * that for hosts that don't expose MMC_CAP_CMD23, no
2184 * change of behavior will be observed.
2185 *
2186 * N.B: Some MMC cards experience perf degradation.
2187 * We'll avoid using CMD23-bounded multiblock writes for
2188 * these, while retaining features like reliable writes.
2189 */
Saugata Das42659002011-12-21 13:09:17 +05302190 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2191 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2192 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002193 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2194 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302195 (do_rel_wr ? (1 << 31) : 0) |
2196 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002197 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2198 brq->mrq.sbc = &brq->sbc;
2199 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002200
Per Forlin54d49d72011-07-01 18:55:29 +02002201 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002202
Per Forlin54d49d72011-07-01 18:55:29 +02002203 brq->data.sg = mqrq->sg;
2204 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002205
Per Forlin54d49d72011-07-01 18:55:29 +02002206 /*
2207 * Adjust the sg list so it is the same size as the
2208 * request.
2209 */
2210 if (brq->data.blocks != blk_rq_sectors(req)) {
2211 int i, data_size = brq->data.blocks << 9;
2212 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002213
Per Forlin54d49d72011-07-01 18:55:29 +02002214 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2215 data_size -= sg->length;
2216 if (data_size <= 0) {
2217 sg->length += data_size;
2218 i++;
2219 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002220 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002221 }
Per Forlin54d49d72011-07-01 18:55:29 +02002222 brq->data.sg_len = i;
2223 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002224
Per Forlinee8a43a2011-07-01 18:55:33 +02002225 mqrq->mmc_active.mrq = &brq->mrq;
2226 mqrq->mmc_active.err_check = mmc_blk_err_check;
2227
Per Forlin54d49d72011-07-01 18:55:29 +02002228 mmc_queue_bounce_pre(mqrq);
2229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002231static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2232 struct mmc_card *card)
2233{
2234 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2235 unsigned int max_seg_sz = queue_max_segment_size(q);
2236 unsigned int len, nr_segs = 0;
2237
2238 do {
2239 len = min(hdr_sz, max_seg_sz);
2240 hdr_sz -= len;
2241 nr_segs++;
2242 } while (hdr_sz);
2243
2244 return nr_segs;
2245}
2246
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002247/**
2248 * mmc_blk_disable_wr_packing() - disables packing mode
2249 * @mq: MMC queue.
2250 *
2251 */
2252void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2253{
2254 if (mq) {
2255 mq->wr_packing_enabled = false;
2256 mq->num_of_potential_packed_wr_reqs = 0;
2257 }
2258}
2259EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2260
Lee Susman841fd132013-04-23 17:59:26 +03002261static int get_packed_trigger(int potential, struct mmc_card *card,
2262 struct request *req, int curr_trigger)
2263{
2264 static int num_mean_elements = 1;
2265 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2266 unsigned int trigger = curr_trigger;
2267 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2268
2269 /* scale down the upper bound to 75% */
2270 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2271
2272 /*
2273 * since the most common calls for this function are with small
2274 * potential write values and since we don't want these calls to affect
2275 * the packed trigger, set a lower bound and ignore calls with
2276 * potential lower than that bound
2277 */
2278 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2279 return trigger;
2280
2281 /*
2282 * this is to prevent integer overflow in the following calculation:
2283 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2284 */
2285 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2286 num_mean_elements = 1;
2287 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2288 }
2289
2290 /*
2291 * get next mean value based on previous mean value and current
2292 * potential packed writes. Calculation is as follows:
2293 * mean_pot[i+1] =
2294 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2295 */
2296 mean_potential *= num_mean_elements;
2297 /*
2298 * add num_mean_elements so that the division of two integers doesn't
2299 * lower mean_potential too much
2300 */
2301 if (potential > mean_potential)
2302 mean_potential += num_mean_elements;
2303 mean_potential += potential;
2304 /* this is for gaining more precision when dividing two integers */
2305 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2306 /* this completes the mean calculation */
2307 mean_potential /= ++num_mean_elements;
2308 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2309
2310 /*
2311 * if current potential packed writes is greater than the mean potential
2312 * then the heuristic is that the following workload will contain many
2313 * write requests, therefore we lower the packed trigger. In the
2314 * opposite case we want to increase the trigger in order to get less
2315 * packing events.
2316 */
2317 if (potential >= mean_potential)
2318 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2319 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2320 else
2321 trigger = (trigger >= pckd_trgr_upper_bound) ?
2322 pckd_trgr_upper_bound : trigger + 1;
2323
2324 /*
2325 * an urgent read request indicates a packed list being interrupted
2326 * by this read, therefore we aim for less packing, hence the trigger
2327 * gets increased
2328 */
2329 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2330 trigger += PCKD_TRGR_URGENT_PENALTY;
2331
2332 return trigger;
2333}
2334
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002335static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2336 struct request *req)
2337{
2338 struct mmc_host *host = mq->card->host;
2339 int data_dir;
2340
2341 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2342 return;
2343
Maya Erez8e2b3c32012-12-02 13:27:15 +02002344 /* Support for the write packing on eMMC 4.5 or later */
2345 if (mq->card->ext_csd.rev <= 5)
2346 return;
2347
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002348 /*
2349 * In case the packing control is not supported by the host, it should
2350 * not have an effect on the write packing. Therefore we have to enable
2351 * the write packing
2352 */
2353 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2354 mq->wr_packing_enabled = true;
2355 return;
2356 }
2357
2358 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2359 if (mq->num_of_potential_packed_wr_reqs >
2360 mq->num_wr_reqs_to_start_packing)
2361 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002362 mq->num_wr_reqs_to_start_packing =
2363 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2364 mq->card, req,
2365 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002366 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002367 return;
2368 }
2369
2370 data_dir = rq_data_dir(req);
2371
2372 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002373 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002374 mq->num_wr_reqs_to_start_packing =
2375 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2376 mq->card, req,
2377 mq->num_wr_reqs_to_start_packing);
2378 mq->num_of_potential_packed_wr_reqs = 0;
2379 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002380 return;
2381 } else if (data_dir == WRITE) {
2382 mq->num_of_potential_packed_wr_reqs++;
2383 }
2384
2385 if (mq->num_of_potential_packed_wr_reqs >
2386 mq->num_wr_reqs_to_start_packing)
2387 mq->wr_packing_enabled = true;
2388}
2389
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002390struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2391{
2392 if (!card)
2393 return NULL;
2394
2395 return &card->wr_pack_stats;
2396}
2397EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2398
2399void mmc_blk_init_packed_statistics(struct mmc_card *card)
2400{
2401 int max_num_of_packed_reqs = 0;
2402
2403 if (!card || !card->wr_pack_stats.packing_events)
2404 return;
2405
2406 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2407
2408 spin_lock(&card->wr_pack_stats.lock);
2409 memset(card->wr_pack_stats.packing_events, 0,
2410 (max_num_of_packed_reqs + 1) *
2411 sizeof(*card->wr_pack_stats.packing_events));
2412 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2413 sizeof(card->wr_pack_stats.pack_stop_reason));
2414 card->wr_pack_stats.enabled = true;
2415 spin_unlock(&card->wr_pack_stats.lock);
2416}
2417EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2418
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002419static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2420{
2421 struct request_queue *q = mq->queue;
2422 struct mmc_card *card = mq->card;
2423 struct request *cur = req, *next = NULL;
2424 struct mmc_blk_data *md = mq->data;
2425 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2426 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2427 unsigned int req_sectors = 0, phys_segments = 0;
2428 unsigned int max_blk_count, max_phys_segs;
2429 bool put_back = true;
2430 u8 max_packed_rw = 0;
2431 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002432 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002433
Shawn Lin96e52da2016-08-26 08:49:55 +08002434 /*
2435 * We don't need to check packed for any further
2436 * operation of packed stuff as we set MMC_PACKED_NONE
2437 * and return zero for reqs if geting null packed. Also
2438 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2439 * it again when removing blk req.
2440 */
2441 if (!mqrq->packed) {
2442 md->flags &= (~MMC_BLK_PACKED_CMD);
2443 goto no_packed;
2444 }
2445
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002446 if (!(md->flags & MMC_BLK_PACKED_CMD))
2447 goto no_packed;
2448
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002449 if (!mq->wr_packing_enabled)
2450 goto no_packed;
2451
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002452 if ((rq_data_dir(cur) == WRITE) &&
2453 mmc_host_packed_wr(card->host))
2454 max_packed_rw = card->ext_csd.max_packed_writes;
2455
2456 if (max_packed_rw == 0)
2457 goto no_packed;
2458
2459 if (mmc_req_rel_wr(cur) &&
2460 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2461 goto no_packed;
2462
2463 if (mmc_large_sector(card) &&
2464 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2465 goto no_packed;
2466
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002467 if (cur->cmd_flags & REQ_FUA)
2468 goto no_packed;
2469
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002470 mmc_blk_clear_packed(mqrq);
2471
2472 max_blk_count = min(card->host->max_blk_count,
2473 card->host->max_req_size >> 9);
2474 if (unlikely(max_blk_count > 0xffff))
2475 max_blk_count = 0xffff;
2476
2477 max_phys_segs = queue_max_segments(q);
2478 req_sectors += blk_rq_sectors(cur);
2479 phys_segments += cur->nr_phys_segments;
2480
2481 if (rq_data_dir(cur) == WRITE) {
2482 req_sectors += mmc_large_sector(card) ? 8 : 1;
2483 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2484 }
2485
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002486 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002487 do {
2488 if (reqs >= max_packed_rw - 1) {
2489 put_back = false;
2490 break;
2491 }
2492
2493 spin_lock_irq(q->queue_lock);
2494 next = blk_fetch_request(q);
2495 spin_unlock_irq(q->queue_lock);
2496 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002497 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002498 put_back = false;
2499 break;
2500 }
2501
2502 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002503 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2504 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002505 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002506 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002507
Mike Christie3a5e02c2016-06-05 14:32:23 -05002508 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002509 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002510 req_op(next) == REQ_OP_FLUSH) {
2511 if (req_op(next) != REQ_OP_SECURE_ERASE)
2512 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
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
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002516 if (next->cmd_flags & REQ_FUA) {
2517 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2518 break;
2519 }
2520
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002521 if (rq_data_dir(cur) != rq_data_dir(next)) {
2522 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002523 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002524 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002525
2526 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002527 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2528 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002529 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002530 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002531
2532 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002533 if (req_sectors > max_blk_count) {
2534 if (stats->enabled)
2535 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002536 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002537 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002538
2539 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002540 if (phys_segments > max_phys_segs) {
2541 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002542 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002543 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002544
Maya Erez5a8dae12014-12-04 15:13:59 +02002545 if (mq->no_pack_for_random) {
2546 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2547 blk_rq_pos(next)) {
2548 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2549 put_back = 1;
2550 break;
2551 }
2552 }
2553
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002554 if (rq_data_dir(next) == WRITE)
2555 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002556 list_add_tail(&next->queuelist, &mqrq->packed->list);
2557 cur = next;
2558 reqs++;
2559 } while (1);
2560
2561 if (put_back) {
2562 spin_lock_irq(q->queue_lock);
2563 blk_requeue_request(q, next);
2564 spin_unlock_irq(q->queue_lock);
2565 }
2566
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002567 if (stats->enabled) {
2568 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2569 stats->packing_events[reqs + 1]++;
2570 if (reqs + 1 == max_packed_rw)
2571 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2572 }
2573
2574 spin_unlock(&stats->lock);
2575
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002576 if (reqs > 0) {
2577 list_add(&req->queuelist, &mqrq->packed->list);
2578 mqrq->packed->nr_entries = ++reqs;
2579 mqrq->packed->retries = reqs;
2580 return reqs;
2581 }
2582
2583no_packed:
2584 mqrq->cmd_type = MMC_PACKED_NONE;
2585 return 0;
2586}
2587
2588static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2589 struct mmc_card *card,
2590 struct mmc_queue *mq)
2591{
2592 struct mmc_blk_request *brq = &mqrq->brq;
2593 struct request *req = mqrq->req;
2594 struct request *prq;
2595 struct mmc_blk_data *md = mq->data;
2596 struct mmc_packed *packed = mqrq->packed;
2597 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002598 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002599 u8 hdr_blocks;
2600 u8 i = 1;
2601
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002602 mqrq->cmd_type = MMC_PACKED_WRITE;
2603 packed->blocks = 0;
2604 packed->idx_failure = MMC_PACKED_NR_IDX;
2605
2606 packed_cmd_hdr = packed->cmd_hdr;
2607 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002608 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2609 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002610 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2611
2612 /*
2613 * Argument for each entry of packed group
2614 */
2615 list_for_each_entry(prq, &packed->list, queuelist) {
2616 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2617 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2618 (prq->cmd_flags & REQ_META) &&
2619 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002620 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002621 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002622 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002623 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2624 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002625 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002626 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002627 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002628 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002629 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002630 packed->blocks += blk_rq_sectors(prq);
2631 i++;
2632 }
2633
2634 memset(brq, 0, sizeof(struct mmc_blk_request));
2635 brq->mrq.cmd = &brq->cmd;
2636 brq->mrq.data = &brq->data;
2637 brq->mrq.sbc = &brq->sbc;
2638 brq->mrq.stop = &brq->stop;
2639
2640 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2641 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2642 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2643
2644 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2645 brq->cmd.arg = blk_rq_pos(req);
2646 if (!mmc_card_blockaddr(card))
2647 brq->cmd.arg <<= 9;
2648 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2649
2650 brq->data.blksz = 512;
2651 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002652 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302653 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002654
2655 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2656 brq->stop.arg = 0;
2657 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2658
2659 mmc_set_data_timeout(&brq->data, card);
2660
2661 brq->data.sg = mqrq->sg;
2662 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2663
2664 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002665
2666 /*
2667 * This is intended for packed commands tests usage - in case these
2668 * functions are not in use the respective pointers are NULL
2669 */
2670 if (mq->err_check_fn)
2671 mqrq->mmc_active.err_check = mq->err_check_fn;
2672 else
2673 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2674
2675 if (mq->packed_test_fn)
2676 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002677
2678 mmc_queue_bounce_pre(mqrq);
2679}
2680
Adrian Hunter67716322011-08-29 16:42:15 +03002681static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2682 struct mmc_blk_request *brq, struct request *req,
2683 int ret)
2684{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002685 struct mmc_queue_req *mq_rq;
2686 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2687
Adrian Hunter67716322011-08-29 16:42:15 +03002688 /*
2689 * If this is an SD card and we're writing, we can first
2690 * mark the known good sectors as ok.
2691 *
2692 * If the card is not SD, we can still ok written sectors
2693 * as reported by the controller (which might be less than
2694 * the real number of written sectors, but never more).
2695 */
2696 if (mmc_card_sd(card)) {
2697 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302698 if (!brq->data.fault_injected) {
2699 blocks = mmc_sd_num_wr_blocks(card);
2700 if (blocks != (u32)-1)
2701 ret = blk_end_request(req, 0, blocks << 9);
2702 } else
2703 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002704 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002705 if (!mmc_packed_cmd(mq_rq->cmd_type))
2706 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002707 }
2708 return ret;
2709}
2710
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002711static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2712{
2713 struct request *prq;
2714 struct mmc_packed *packed = mq_rq->packed;
2715 int idx = packed->idx_failure, i = 0;
2716 int ret = 0;
2717
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002718 while (!list_empty(&packed->list)) {
2719 prq = list_entry_rq(packed->list.next);
2720 if (idx == i) {
2721 /* retry from error index */
2722 packed->nr_entries -= idx;
2723 mq_rq->req = prq;
2724 ret = 1;
2725
2726 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2727 list_del_init(&prq->queuelist);
2728 mmc_blk_clear_packed(mq_rq);
2729 }
2730 return ret;
2731 }
2732 list_del_init(&prq->queuelist);
2733 blk_end_request(prq, 0, blk_rq_bytes(prq));
2734 i++;
2735 }
2736
2737 mmc_blk_clear_packed(mq_rq);
2738 return ret;
2739}
2740
2741static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2742{
2743 struct request *prq;
2744 struct mmc_packed *packed = mq_rq->packed;
2745
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002746 while (!list_empty(&packed->list)) {
2747 prq = list_entry_rq(packed->list.next);
2748 list_del_init(&prq->queuelist);
2749 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2750 }
2751
2752 mmc_blk_clear_packed(mq_rq);
2753}
2754
2755static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2756 struct mmc_queue_req *mq_rq)
2757{
2758 struct request *prq;
2759 struct request_queue *q = mq->queue;
2760 struct mmc_packed *packed = mq_rq->packed;
2761
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002762 while (!list_empty(&packed->list)) {
2763 prq = list_entry_rq(packed->list.prev);
2764 if (prq->queuelist.prev != &packed->list) {
2765 list_del_init(&prq->queuelist);
2766 spin_lock_irq(q->queue_lock);
2767 blk_requeue_request(mq->queue, prq);
2768 spin_unlock_irq(q->queue_lock);
2769 } else {
2770 list_del_init(&prq->queuelist);
2771 }
2772 }
2773
2774 mmc_blk_clear_packed(mq_rq);
2775}
2776
Per Forlinee8a43a2011-07-01 18:55:33 +02002777static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02002778{
2779 struct mmc_blk_data *md = mq->data;
2780 struct mmc_card *card = md->queue.card;
2781 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03002782 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02002783 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02002784 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302785 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02002786 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002787 const u8 packed_nr = 2;
2788 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08002789#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
2790 unsigned long waitfor = jiffies;
2791#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02002792
2793 if (!rqc && !mq->mqrq_prev->req)
2794 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002795
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002796 if (rqc)
2797 reqs = mmc_blk_prep_packed_list(mq, rqc);
2798
Per Forlin54d49d72011-07-01 18:55:29 +02002799 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02002800 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302801 /*
2802 * When 4KB native sector is enabled, only 8 blocks
2803 * multiple read or write is allowed
2804 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00002805 if (mmc_large_sector(card) &&
2806 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05302807 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2808 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002809 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05302810 goto cmd_abort;
2811 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002812
2813 if (reqs >= packed_nr)
2814 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
2815 card, mq);
2816 else
2817 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02002818 areq = &mq->mqrq_cur->mmc_active;
2819 } else
2820 areq = NULL;
2821 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002822 if (!areq) {
2823 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002824 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02002825 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002826 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02002827
Per Forlinee8a43a2011-07-01 18:55:33 +02002828 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
2829 brq = &mq_rq->brq;
2830 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03002831 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02002832 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02002833
Per Forlind78d4a82011-07-01 18:55:30 +02002834 switch (status) {
2835 case MMC_BLK_SUCCESS:
2836 case MMC_BLK_PARTIAL:
2837 /*
2838 * A block was successfully transferred.
2839 */
Adrian Hunter67716322011-08-29 16:42:15 +03002840 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002841
Mark Salyzyn6904e432016-01-28 11:12:25 -08002842 mmc_blk_simulate_delay(mq, rqc, waitfor);
2843
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002844 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2845 ret = mmc_blk_end_packed_req(mq_rq);
2846 break;
2847 } else {
2848 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02002849 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002850 }
2851
Adrian Hunter67716322011-08-29 16:42:15 +03002852 /*
2853 * If the blk_end_request function returns non-zero even
2854 * though all data has been transferred and no errors
2855 * were returned by the host controller, it's a bug.
2856 */
Per Forlinee8a43a2011-07-01 18:55:33 +02002857 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302858 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02002859 __func__, blk_rq_bytes(req),
2860 brq->data.bytes_xfered);
2861 rqc = NULL;
2862 goto cmd_abort;
2863 }
Per Forlind78d4a82011-07-01 18:55:30 +02002864 break;
2865 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03002866 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08002867 if (mmc_blk_reset(md, card->host, type))
2868 goto cmd_abort;
2869 if (!ret)
2870 goto start_new_req;
2871 break;
Per Forlind78d4a82011-07-01 18:55:30 +02002872 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03002873 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02002874 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002875 break;
Adrian Hunter67716322011-08-29 16:42:15 +03002876 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02002877 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02002878 if (!mmc_blk_reset(md, card->host, type) &&
2879 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03002880 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002881 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002882 case MMC_BLK_DATA_ERR: {
2883 int err;
2884
2885 err = mmc_blk_reset(md, card->host, type);
2886 if (!err)
2887 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05302888 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03002889 }
2890 case MMC_BLK_ECC_ERR:
2891 if (brq->data.blocks > 1) {
2892 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07002893 pr_warn("%s: retrying using single block read\n",
2894 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03002895 disable_multi = 1;
2896 break;
2897 }
Per Forlind78d4a82011-07-01 18:55:30 +02002898 /*
2899 * After an error, we redo I/O one sector at a
2900 * time, so we only reach here after trying to
2901 * read a single sector.
2902 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302903 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02002904 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02002905 if (!ret)
2906 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02002907 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05302908 case MMC_BLK_NOMEDIUM:
2909 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002910 default:
2911 pr_err("%s: Unhandled return value (%d)",
2912 req->rq_disk->disk_name, status);
2913 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01002914 }
2915
Per Forlinee8a43a2011-07-01 18:55:33 +02002916 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002917 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2918 if (!mq_rq->packed->retries)
2919 goto cmd_abort;
2920 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2921 mmc_start_req(card->host,
2922 &mq_rq->mmc_active, NULL);
2923 } else {
2924
2925 /*
2926 * In case of a incomplete request
2927 * prepare it again and resend.
2928 */
2929 mmc_blk_rw_rq_prep(mq_rq, card,
2930 disable_multi, mq);
2931 mmc_start_req(card->host,
2932 &mq_rq->mmc_active, NULL);
2933 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03002934 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02002935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 } while (ret);
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return 1;
2939
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01002940 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002941 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2942 mmc_blk_abort_packed_req(mq_rq);
2943 } else {
2944 if (mmc_card_removed(card))
2945 req->cmd_flags |= REQ_QUIET;
2946 while (ret)
2947 ret = blk_end_request(req, -EIO,
2948 blk_rq_cur_bytes(req));
2949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Per Forlinee8a43a2011-07-01 18:55:33 +02002951 start_new_req:
2952 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09002953 if (mmc_card_removed(card)) {
2954 rqc->cmd_flags |= REQ_QUIET;
2955 blk_end_request_all(rqc, -EIO);
2956 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002957 /*
2958 * If current request is packed, it needs to put back.
2959 */
2960 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2961 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2962
Seungwon Jeon7a819022013-01-22 19:48:07 +09002963 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2964 mmc_start_req(card->host,
2965 &mq->mqrq_cur->mmc_active, NULL);
2966 }
Per Forlinee8a43a2011-07-01 18:55:33 +02002967 }
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return 0;
2970}
2971
Linus Walleij29eb7bd2016-09-20 11:34:38 +02002972int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07002973{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002974 int ret;
2975 struct mmc_blk_data *md = mq->data;
2976 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002977 struct mmc_host *host = card->host;
2978 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06002979 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002980
Per Forlinee8a43a2011-07-01 18:55:33 +02002981 if (req && !mq->mqrq_prev->req)
2982 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002983 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02002984
Andrei Warkentin371a6892011-04-11 18:10:25 -05002985 ret = mmc_blk_part_switch(card, md);
2986 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002987 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05302988 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03002989 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05002990 ret = 0;
2991 goto out;
2992 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05002993
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002994 mmc_blk_write_packing_control(mq, req);
2995
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02002996 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05002997 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02002998 /* complete ongoing async transfer before issuing discard */
2999 if (card->host->areq)
3000 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003001 ret = mmc_blk_issue_discard_rq(mq, req);
3002 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3003 /* complete ongoing async transfer before issuing secure erase*/
3004 if (card->host->areq)
3005 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003006 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3007 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3008 else
3009 ret = mmc_blk_issue_discard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05003010 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003011 /* complete ongoing async transfer before issuing flush */
3012 if (card->host->areq)
3013 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003014 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003015 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003016 if (!req && host->areq) {
3017 spin_lock_irqsave(&host->context_info.lock, flags);
3018 host->context_info.is_waiting_last_req = true;
3019 spin_unlock_irqrestore(&host->context_info.lock, flags);
3020 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003021 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003022 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003023
Andrei Warkentin371a6892011-04-11 18:10:25 -05003024out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003025 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3026 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003027 /*
3028 * Release host when there are no more requests
3029 * and after special request(discard, flush) is done.
3030 * In case sepecial request, there is no reentry to
3031 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3032 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003033 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003034 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Russell Kinga6f6c962006-01-03 22:38:44 +00003037static inline int mmc_blk_readonly(struct mmc_card *card)
3038{
3039 return mmc_card_readonly(card) ||
3040 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3041}
3042
Andrei Warkentin371a6892011-04-11 18:10:25 -05003043static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3044 struct device *parent,
3045 sector_t size,
3046 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003047 const char *subname,
3048 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 struct mmc_blk_data *md;
3051 int devidx, ret;
3052
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003053again:
3054 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3055 return ERR_PTR(-ENOMEM);
3056
3057 spin_lock(&mmc_blk_lock);
3058 ret = ida_get_new(&mmc_blk_ida, &devidx);
3059 spin_unlock(&mmc_blk_lock);
3060
3061 if (ret == -EAGAIN)
3062 goto again;
3063 else if (ret)
3064 return ERR_PTR(ret);
3065
3066 if (devidx >= max_devices) {
3067 ret = -ENOSPC;
3068 goto out;
3069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003071 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003072 if (!md) {
3073 ret = -ENOMEM;
3074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003076
Johan Rudholmadd710e2011-12-02 08:51:06 +01003077 md->area_type = area_type;
3078
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003079 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003080 * Set the read-only status based on the supported commands
3081 * and the write protect switch.
3082 */
3083 md->read_only = mmc_blk_readonly(card);
3084
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003085 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003086 if (md->disk == NULL) {
3087 ret = -ENOMEM;
3088 goto err_kfree;
3089 }
3090
3091 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003092 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003093 md->usage = 1;
3094
Adrian Hunterd09408a2011-06-23 13:40:28 +03003095 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
Russell Kinga6f6c962006-01-03 22:38:44 +00003096 if (ret)
3097 goto err_putdisk;
3098
Russell Kinga6f6c962006-01-03 22:38:44 +00003099 md->queue.data = md;
3100
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003101 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003102 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003103 md->disk->fops = &mmc_bdops;
3104 md->disk->private_data = md;
3105 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003106 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003107 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003108 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003109 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003110 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003111
3112 /*
3113 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3114 *
3115 * - be set for removable media with permanent block devices
3116 * - be unset for removable block devices with permanent media
3117 *
3118 * Since MMC block devices clearly fall under the second
3119 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3120 * should use the block device creation/destruction hotplug
3121 * messages to tell when the card is present.
3122 */
3123
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003124 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003125 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003126
Saugata Dasa5075eb2012-05-17 16:32:21 +05303127 if (mmc_card_mmc(card))
3128 blk_queue_logical_block_size(md->queue.queue,
3129 card->ext_csd.data_sector_size);
3130 else
3131 blk_queue_logical_block_size(md->queue.queue, 512);
3132
Andrei Warkentin371a6892011-04-11 18:10:25 -05003133 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003134
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003135 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003136 if ((mmc_card_mmc(card) &&
3137 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003138 (mmc_card_sd(card) &&
3139 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3140 md->flags |= MMC_BLK_CMD23;
3141 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003142
3143 if (mmc_card_mmc(card) &&
3144 md->flags & MMC_BLK_CMD23 &&
3145 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
3146 card->ext_csd.rel_sectors)) {
3147 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003148 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003149 }
3150
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003151 if (mmc_card_mmc(card) &&
3152 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3153 (md->flags & MMC_BLK_CMD23) &&
3154 card->ext_csd.packed_event_en) {
3155 if (!mmc_packed_init(&md->queue, card))
3156 md->flags |= MMC_BLK_PACKED_CMD;
3157 }
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003160
3161 err_putdisk:
3162 put_disk(md->disk);
3163 err_kfree:
3164 kfree(md);
3165 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003166 spin_lock(&mmc_blk_lock);
3167 ida_remove(&mmc_blk_ida, devidx);
3168 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003169 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Andrei Warkentin371a6892011-04-11 18:10:25 -05003172static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3173{
3174 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003175
3176 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3177 /*
3178 * The EXT_CSD sector count is in number or 512 byte
3179 * sectors.
3180 */
3181 size = card->ext_csd.sectors;
3182 } else {
3183 /*
3184 * The CSD capacity field is in units of read_blkbits.
3185 * set_capacity takes units of 512 bytes.
3186 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003187 size = (typeof(sector_t))card->csd.capacity
3188 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003189 }
3190
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003191 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003192 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003193}
3194
3195static int mmc_blk_alloc_part(struct mmc_card *card,
3196 struct mmc_blk_data *md,
3197 unsigned int part_type,
3198 sector_t size,
3199 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003200 const char *subname,
3201 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003202{
3203 char cap_str[10];
3204 struct mmc_blk_data *part_md;
3205
3206 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003207 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003208 if (IS_ERR(part_md))
3209 return PTR_ERR(part_md);
3210 part_md->part_type = part_type;
3211 list_add(&part_md->part, &md->part);
3212
James Bottomleyb9f28d82015-03-05 18:47:01 -08003213 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003214 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303215 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003216 part_md->disk->disk_name, mmc_card_id(card),
3217 mmc_card_name(card), part_md->part_type, cap_str);
3218 return 0;
3219}
3220
Namjae Jeone0c368d2011-10-06 23:41:38 +09003221/* MMC Physical partitions consist of two boot partitions and
3222 * up to four general purpose partitions.
3223 * For each partition enabled in EXT_CSD a block device will be allocatedi
3224 * to provide access to the partition.
3225 */
3226
Andrei Warkentin371a6892011-04-11 18:10:25 -05003227static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3228{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003229 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003230
3231 if (!mmc_card_mmc(card))
3232 return 0;
3233
Namjae Jeone0c368d2011-10-06 23:41:38 +09003234 for (idx = 0; idx < card->nr_parts; idx++) {
3235 if (card->part[idx].size) {
3236 ret = mmc_blk_alloc_part(card, md,
3237 card->part[idx].part_cfg,
3238 card->part[idx].size >> 9,
3239 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003240 card->part[idx].name,
3241 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003242 if (ret)
3243 return ret;
3244 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003245 }
3246
3247 return ret;
3248}
3249
Andrei Warkentin371a6892011-04-11 18:10:25 -05003250static void mmc_blk_remove_req(struct mmc_blk_data *md)
3251{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003252 struct mmc_card *card;
3253
Andrei Warkentin371a6892011-04-11 18:10:25 -05003254 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003255 /*
3256 * Flush remaining requests and free queues. It
3257 * is freeing the queue that stops new requests
3258 * from being accepted.
3259 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003260 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003261 mmc_cleanup_queue(&md->queue);
3262 if (md->flags & MMC_BLK_PACKED_CMD)
3263 mmc_packed_clean(&md->queue);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003264 device_remove_file(disk_to_dev(md->disk),
3265 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003266 if (md->disk->flags & GENHD_FL_UP) {
3267 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003268 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3269 card->ext_csd.boot_ro_lockable)
3270 device_remove_file(disk_to_dev(md->disk),
3271 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003272#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3273 device_remove_file(disk_to_dev(md->disk),
3274 &dev_attr_max_write_speed);
3275 device_remove_file(disk_to_dev(md->disk),
3276 &dev_attr_max_read_speed);
3277 device_remove_file(disk_to_dev(md->disk),
3278 &dev_attr_cache_size);
3279#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003280
Andrei Warkentin371a6892011-04-11 18:10:25 -05003281 del_gendisk(md->disk);
3282 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003283 mmc_blk_put(md);
3284 }
3285}
3286
3287static void mmc_blk_remove_parts(struct mmc_card *card,
3288 struct mmc_blk_data *md)
3289{
3290 struct list_head *pos, *q;
3291 struct mmc_blk_data *part_md;
3292
3293 list_for_each_safe(pos, q, &md->part) {
3294 part_md = list_entry(pos, struct mmc_blk_data, part);
3295 list_del(pos);
3296 mmc_blk_remove_req(part_md);
3297 }
3298}
3299
3300static int mmc_add_disk(struct mmc_blk_data *md)
3301{
3302 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003303 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003304
Dan Williams307d8e62016-06-20 10:40:44 -07003305 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003306 md->force_ro.show = force_ro_show;
3307 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303308 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003309 md->force_ro.attr.name = "force_ro";
3310 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3311 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3312 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003313 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003314#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3315 atomic_set(&md->queue.max_write_speed, max_write_speed);
3316 ret = device_create_file(disk_to_dev(md->disk),
3317 &dev_attr_max_write_speed);
3318 if (ret)
3319 goto max_write_speed_fail;
3320 atomic_set(&md->queue.max_read_speed, max_read_speed);
3321 ret = device_create_file(disk_to_dev(md->disk),
3322 &dev_attr_max_read_speed);
3323 if (ret)
3324 goto max_read_speed_fail;
3325 atomic_set(&md->queue.cache_size, cache_size);
3326 atomic_long_set(&md->queue.cache_used, 0);
3327 md->queue.cache_jiffies = jiffies;
3328 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3329 if (ret)
3330 goto cache_size_fail;
3331#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003332
3333 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3334 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003335 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003336
3337 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3338 mode = S_IRUGO;
3339 else
3340 mode = S_IRUGO | S_IWUSR;
3341
3342 md->power_ro_lock.show = power_ro_lock_show;
3343 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003344 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003345 md->power_ro_lock.attr.mode = mode;
3346 md->power_ro_lock.attr.name =
3347 "ro_lock_until_next_power_on";
3348 ret = device_create_file(disk_to_dev(md->disk),
3349 &md->power_ro_lock);
3350 if (ret)
3351 goto power_ro_lock_fail;
3352 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003353
3354 md->num_wr_reqs_to_start_packing.show =
3355 num_wr_reqs_to_start_packing_show;
3356 md->num_wr_reqs_to_start_packing.store =
3357 num_wr_reqs_to_start_packing_store;
3358 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3359 md->num_wr_reqs_to_start_packing.attr.name =
3360 "num_wr_reqs_to_start_packing";
3361 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3362 ret = device_create_file(disk_to_dev(md->disk),
3363 &md->num_wr_reqs_to_start_packing);
3364 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003365 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003366
Maya Erez5a8dae12014-12-04 15:13:59 +02003367 md->no_pack_for_random.show = no_pack_for_random_show;
3368 md->no_pack_for_random.store = no_pack_for_random_store;
3369 sysfs_attr_init(&md->no_pack_for_random.attr);
3370 md->no_pack_for_random.attr.name = "no_pack_for_random";
3371 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3372 ret = device_create_file(disk_to_dev(md->disk),
3373 &md->no_pack_for_random);
3374 if (ret)
3375 goto no_pack_for_random_fails;
3376
Johan Rudholmadd710e2011-12-02 08:51:06 +01003377 return ret;
3378
Maya Erez5a8dae12014-12-04 15:13:59 +02003379no_pack_for_random_fails:
3380 device_remove_file(disk_to_dev(md->disk),
3381 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003382num_wr_reqs_to_start_packing_fail:
3383 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003384power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003385#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3386 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3387cache_size_fail:
3388 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3389max_read_speed_fail:
3390 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3391max_write_speed_fail:
3392#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003393 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3394force_ro_fail:
3395 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003396
3397 return ret;
3398}
3399
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003400static const struct mmc_fixup blk_fixups[] =
3401{
Chris Ballc59d4472011-11-11 22:01:43 -05003402 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3403 MMC_QUIRK_INAND_CMD38),
3404 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3405 MMC_QUIRK_INAND_CMD38),
3406 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3407 MMC_QUIRK_INAND_CMD38),
3408 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3409 MMC_QUIRK_INAND_CMD38),
3410 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3411 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003412
3413 /*
3414 * Some MMC cards experience performance degradation with CMD23
3415 * instead of CMD12-bounded multiblock transfers. For now we'll
3416 * black list what's bad...
3417 * - Certain Toshiba cards.
3418 *
3419 * N.B. This doesn't affect SD cards.
3420 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003421 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3422 MMC_QUIRK_BLK_NO_CMD23),
3423 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3424 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003425 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003426 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003427 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003428 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003429 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003430 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003431
3432 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003433 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003434 */
Chris Ballc59d4472011-11-11 22:01:43 -05003435 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003436 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003437 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3438 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003439
Ian Chen3550ccd2012-08-29 15:05:36 +09003440 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003441 * Some Samsung MMC cards need longer data read timeout than
3442 * indicated in CSD.
3443 */
3444 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3445 MMC_QUIRK_LONG_READ_TIME),
3446
3447 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003448 * On these Samsung MoviNAND parts, performing secure erase or
3449 * secure trim can result in unrecoverable corruption due to a
3450 * firmware bug.
3451 */
3452 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3453 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3454 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3455 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3456 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3457 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3458 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3459 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3460 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3461 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3462 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3463 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3464 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3465 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3466 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3467 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3468
Shawn Linb5b4ff02015-08-12 13:08:32 +08003469 /*
3470 * On Some Kingston eMMCs, performing trim can result in
3471 * unrecoverable data conrruption occasionally due to a firmware bug.
3472 */
3473 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3474 MMC_QUIRK_TRIM_BROKEN),
3475 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3476 MMC_QUIRK_TRIM_BROKEN),
3477
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003478 /* Some INAND MCP devices advertise incorrect timeout values */
3479 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3480 MMC_QUIRK_INAND_DATA_TIMEOUT),
3481
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003482 END_FIXUP
3483};
3484
Ulf Hansson96541ba2015-04-14 13:06:12 +02003485static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003487 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003488 char cap_str[10];
3489
Pierre Ossman912490d2005-05-21 10:27:02 +01003490 /*
3491 * Check that the card supports the command class(es) we need.
3492 */
3493 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return -ENODEV;
3495
Lukas Czerner5204d002014-06-18 13:18:07 +02003496 mmc_fixup_device(card, blk_fixups);
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 md = mmc_blk_alloc(card);
3499 if (IS_ERR(md))
3500 return PTR_ERR(md);
3501
James Bottomleyb9f28d82015-03-05 18:47:01 -08003502 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003503 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303504 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003506 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Andrei Warkentin371a6892011-04-11 18:10:25 -05003508 if (mmc_blk_alloc_parts(card, md))
3509 goto out;
3510
Ulf Hansson96541ba2015-04-14 13:06:12 +02003511 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003512
Andrei Warkentin371a6892011-04-11 18:10:25 -05003513 if (mmc_add_disk(md))
3514 goto out;
3515
3516 list_for_each_entry(part_md, &md->part, part) {
3517 if (mmc_add_disk(part_md))
3518 goto out;
3519 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003520
3521 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3522 pm_runtime_use_autosuspend(&card->dev);
3523
3524 /*
3525 * Don't enable runtime PM for SD-combo cards here. Leave that
3526 * decision to be taken during the SDIO init sequence instead.
3527 */
3528 if (card->type != MMC_TYPE_SD_COMBO) {
3529 pm_runtime_set_active(&card->dev);
3530 pm_runtime_enable(&card->dev);
3531 }
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return 0;
3534
3535 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05003536 mmc_blk_remove_parts(card, md);
3537 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01003538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539}
3540
Ulf Hansson96541ba2015-04-14 13:06:12 +02003541static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003543 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Andrei Warkentin371a6892011-04-11 18:10:25 -05003545 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003546 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03003547 mmc_claim_host(card->host);
3548 mmc_blk_part_switch(card, md);
3549 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003550 if (card->type != MMC_TYPE_SD_COMBO)
3551 pm_runtime_disable(&card->dev);
3552 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003553 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02003554 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
Ulf Hansson96541ba2015-04-14 13:06:12 +02003557static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003559 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02003560 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303561 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303564 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303565 if (rc)
3566 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003567 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05303568 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303569 if (rc)
3570 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05303573 goto out;
3574
3575 out_resume:
3576 mmc_queue_resume(&md->queue);
3577 list_for_each_entry(part_md, &md->part, part) {
3578 mmc_queue_resume(&part_md->queue);
3579 }
3580 out:
3581 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
Ulf Hansson96541ba2015-04-14 13:06:12 +02003584static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02003585{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003586 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003587}
3588
Ulf Hansson0967edc2014-10-06 11:29:42 +02003589#ifdef CONFIG_PM_SLEEP
3590static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02003591{
Ulf Hansson96541ba2015-04-14 13:06:12 +02003592 struct mmc_card *card = mmc_dev_to_card(dev);
3593
3594 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02003595}
3596
Ulf Hansson0967edc2014-10-06 11:29:42 +02003597static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003599 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02003600 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05003603 /*
3604 * Resume involves the card going into idle state,
3605 * so current partition is always the main one.
3606 */
3607 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003609 list_for_each_entry(part_md, &md->part, part) {
3610 mmc_queue_resume(&part_md->queue);
3611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
3613 return 0;
3614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615#endif
3616
Ulf Hansson0967edc2014-10-06 11:29:42 +02003617static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3618
Ulf Hansson96541ba2015-04-14 13:06:12 +02003619static struct mmc_driver mmc_driver = {
3620 .drv = {
3621 .name = "mmcblk",
3622 .pm = &mmc_blk_pm_ops,
3623 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 .probe = mmc_blk_probe,
3625 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02003626 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627};
3628
3629static int __init mmc_blk_init(void)
3630{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003631 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003633 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3634 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3635
Ben Hutchingsa26eba62014-11-06 03:35:09 +00003636 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003637
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003638 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3639 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003642 res = mmc_register_driver(&mmc_driver);
3643 if (res)
3644 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09003646 return 0;
3647 out2:
3648 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 out:
3650 return res;
3651}
3652
3653static void __exit mmc_blk_exit(void)
3654{
3655 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003656 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
3659module_init(mmc_blk_init);
3660module_exit(mmc_blk_exit);
3661
3662MODULE_LICENSE("GPL");
3663MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3664