blob: 837390397c38ffc5c3ecf58bf9030ebe2aba6d16 [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>
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -070039#include <linux/ioprio.h>
Ulf Hanssonb10fa992016-04-07 14:36:46 +020040#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
John Calixtocb87ea22011-04-26 18:56:29 -040042#include <linux/mmc/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020044#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010045#include <linux/mmc/mmc.h>
46#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
49
Pierre Ossman98ac2162006-12-23 20:03:02 +010050#include "queue.h"
Baoyou Xie48ab0862016-09-30 09:37:38 +080051#include "block.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000053MODULE_ALIAS("mmc:block");
Olof Johansson5e71b7a2010-09-17 21:19:57 -040054#ifdef MODULE_PARAM_PREFIX
55#undef MODULE_PARAM_PREFIX
56#endif
57#define MODULE_PARAM_PREFIX "mmcblk."
David Woodhouse1dff3142007-11-21 18:45:12 +010058
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050059#define INAND_CMD38_ARG_EXT_CSD 113
60#define INAND_CMD38_ARG_ERASE 0x00
61#define INAND_CMD38_ARG_TRIM 0x01
62#define INAND_CMD38_ARG_SECERASE 0x80
63#define INAND_CMD38_ARG_SECTRIM1 0x81
64#define INAND_CMD38_ARG_SECTRIM2 0x88
Subhash Jadavani2fbab612014-12-04 15:16:17 +020065#define MMC_BLK_TIMEOUT_MS (30 * 1000) /* 30 sec timeout */
Maya Erez775a9362013-04-18 15:41:55 +030066#define MMC_SANITIZE_REQ_TIMEOUT 240000
67#define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
Asutosh Das02e30862015-05-20 16:52:04 +053068#define MMC_CMDQ_STOP_TIMEOUT_MS 100
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -050069
Luca Porziod3df0462015-11-06 15:12:26 +000070#define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090071 (rq_data_dir(req) == WRITE))
72#define PACKED_CMD_VER 0x01
73#define PACKED_CMD_WR 0x02
Lee Susman841fd132013-04-23 17:59:26 +030074#define PACKED_TRIGGER_MAX_ELEMENTS 5000
Seungwon Jeonce39f9d2013-02-06 17:02:46 +090075
Maya Erezf93ca0a2014-12-09 23:34:41 +020076#define MMC_BLK_MAX_RETRIES 5 /* max # of retries before aborting a command */
Tatyana Brokhman08238ce2012-10-07 10:33:13 +020077#define MMC_BLK_UPDATE_STOP_REASON(stats, reason) \
78 do { \
79 if (stats->enabled) \
80 stats->pack_stop_reason[reason]++; \
81 } while (0)
82
Asutosh Das02e30862015-05-20 16:52:04 +053083#define MAX_RETRIES 5
Lee Susman841fd132013-04-23 17:59:26 +030084#define PCKD_TRGR_INIT_MEAN_POTEN 17
85#define PCKD_TRGR_POTEN_LOWER_BOUND 5
86#define PCKD_TRGR_URGENT_PENALTY 2
87#define PCKD_TRGR_LOWER_BOUND 5
88#define PCKD_TRGR_PRECISION_MULTIPLIER 100
89
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020090static DEFINE_MUTEX(block_mutex);
Olof Johansson5e71b7a2010-09-17 21:19:57 -040091
92/*
93 * The defaults come from config options but can be overriden by module
94 * or bootarg options.
95 */
96static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
97
98/*
99 * We've only got one major, so number of mmcblk devices is
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000100 * limited to (1 << 20) / number of minors per device. It is also
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200101 * limited by the MAX_DEVICES below.
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400102 */
103static int max_devices;
104
Ben Hutchingsa26eba62014-11-06 03:35:09 +0000105#define MAX_DEVICES 256
106
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200107static DEFINE_IDA(mmc_blk_ida);
108static DEFINE_SPINLOCK(mmc_blk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * There is one mmc_blk_data per slot.
112 */
113struct mmc_blk_data {
114 spinlock_t lock;
Dan Williams307d8e62016-06-20 10:40:44 -0700115 struct device *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct gendisk *disk;
117 struct mmc_queue queue;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500118 struct list_head part;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500120 unsigned int flags;
121#define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
122#define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900123#define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700124#define MMC_BLK_CMD_QUEUE (1 << 3) /* MMC command queue support */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -0500125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +0000127 unsigned int read_only;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500128 unsigned int part_type;
Adrian Hunter67716322011-08-29 16:42:15 +0300129 unsigned int reset_done;
130#define MMC_BLK_READ BIT(0)
131#define MMC_BLK_WRITE BIT(1)
132#define MMC_BLK_DISCARD BIT(2)
133#define MMC_BLK_SECDISCARD BIT(3)
Talel Shenhar8a8e3b42015-02-11 12:58:16 +0200134#define MMC_BLK_FLUSH BIT(4)
Andrei Warkentin371a6892011-04-11 18:10:25 -0500135
136 /*
137 * Only set in main mmc_blk_data associated
Ulf Hanssonfc95e302014-10-06 14:34:09 +0200138 * with mmc_card with dev_set_drvdata, and keeps
Andrei Warkentin371a6892011-04-11 18:10:25 -0500139 * track of the current selected device partition.
140 */
141 unsigned int part_curr;
142 struct device_attribute force_ro;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100143 struct device_attribute power_ro_lock;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200144 struct device_attribute num_wr_reqs_to_start_packing;
Maya Erez5a8dae12014-12-04 15:13:59 +0200145 struct device_attribute no_pack_for_random;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100146 int area_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Arjan van de Vena621aae2006-01-12 18:43:35 +0000149static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900151enum {
152 MMC_PACKED_NR_IDX = -1,
153 MMC_PACKED_NR_ZERO,
154 MMC_PACKED_NR_SINGLE,
155};
156
Olof Johansson5e71b7a2010-09-17 21:19:57 -0400157module_param(perdev_minors, int, 0444);
158MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
159
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200160static inline int mmc_blk_part_switch(struct mmc_card *card,
161 struct mmc_blk_data *md);
162static int get_card_status(struct mmc_card *card, u32 *status, int retries);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -0700163static int mmc_blk_cmdq_switch(struct mmc_card *card,
164 struct mmc_blk_data *md, bool enable);
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200165
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900166static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
167{
168 struct mmc_packed *packed = mqrq->packed;
169
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900170 mqrq->cmd_type = MMC_PACKED_NONE;
171 packed->nr_entries = MMC_PACKED_NR_ZERO;
172 packed->idx_failure = MMC_PACKED_NR_IDX;
173 packed->retries = 0;
174 packed->blocks = 0;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
178{
179 struct mmc_blk_data *md;
180
Arjan van de Vena621aae2006-01-12 18:43:35 +0000181 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 md = disk->private_data;
183 if (md && md->usage == 0)
184 md = NULL;
185 if (md)
186 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +0000187 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return md;
190}
191
Andrei Warkentin371a6892011-04-11 18:10:25 -0500192static inline int mmc_get_devidx(struct gendisk *disk)
193{
Colin Cross382c55f2015-10-22 10:00:41 -0700194 int devidx = disk->first_minor / perdev_minors;
Andrei Warkentin371a6892011-04-11 18:10:25 -0500195 return devidx;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void mmc_blk_put(struct mmc_blk_data *md)
199{
Arjan van de Vena621aae2006-01-12 18:43:35 +0000200 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 md->usage--;
202 if (md->usage == 0) {
Andrei Warkentin371a6892011-04-11 18:10:25 -0500203 int devidx = mmc_get_devidx(md->disk);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800204 blk_cleanup_queue(md->queue.queue);
205
Ulf Hanssonb10fa992016-04-07 14:36:46 +0200206 spin_lock(&mmc_blk_lock);
207 ida_remove(&mmc_blk_ida, devidx);
208 spin_unlock(&mmc_blk_lock);
David Woodhouse1dff3142007-11-21 18:45:12 +0100209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 kfree(md);
212 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000213 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Johan Rudholmadd710e2011-12-02 08:51:06 +0100216static ssize_t power_ro_lock_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
219 int ret;
220 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200221 struct mmc_card *card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100222 int locked = 0;
223
Asutosh Das507d9a72014-12-09 10:15:53 +0200224 if (!md)
225 return -EINVAL;
226
227 card = md->queue.card;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100228 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
229 locked = 2;
230 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
231 locked = 1;
232
233 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
234
Tomas Winkler9098f842015-07-16 15:50:45 +0200235 mmc_blk_put(md);
236
Johan Rudholmadd710e2011-12-02 08:51:06 +0100237 return ret;
238}
239
240static ssize_t power_ro_lock_store(struct device *dev,
241 struct device_attribute *attr, const char *buf, size_t count)
242{
243 int ret;
244 struct mmc_blk_data *md, *part_md;
245 struct mmc_card *card;
246 unsigned long set;
247
248 if (kstrtoul(buf, 0, &set))
249 return -EINVAL;
250
251 if (set != 1)
252 return count;
253
254 md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200255 if (!md)
256 return -EINVAL;
Johan Rudholmadd710e2011-12-02 08:51:06 +0100257 card = md->queue.card;
258
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200259 mmc_get_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100260
261 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
262 card->ext_csd.boot_ro_lock |
263 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
264 card->ext_csd.part_time);
265 if (ret)
266 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
267 else
268 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
269
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200270 mmc_put_card(card);
Johan Rudholmadd710e2011-12-02 08:51:06 +0100271
272 if (!ret) {
273 pr_info("%s: Locking boot partition ro until next power on\n",
274 md->disk->disk_name);
275 set_disk_ro(md->disk, 1);
276
277 list_for_each_entry(part_md, &md->part, part)
278 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
279 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
280 set_disk_ro(part_md->disk, 1);
281 }
282 }
283
284 mmc_blk_put(md);
285 return count;
286}
287
Andrei Warkentin371a6892011-04-11 18:10:25 -0500288static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
289 char *buf)
290{
291 int ret;
292 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
293
Asutosh Das507d9a72014-12-09 10:15:53 +0200294 if (!md)
295 return -EINVAL;
296
Baruch Siach0031a982014-09-22 10:12:51 +0300297 ret = snprintf(buf, PAGE_SIZE, "%d\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -0500298 get_disk_ro(dev_to_disk(dev)) ^
299 md->read_only);
300 mmc_blk_put(md);
301 return ret;
302}
303
304static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 int ret;
308 char *end;
309 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
310 unsigned long set = simple_strtoul(buf, &end, 0);
Asutosh Das507d9a72014-12-09 10:15:53 +0200311
312 if (!md)
313 return -EINVAL;
314
Andrei Warkentin371a6892011-04-11 18:10:25 -0500315 if (end == buf) {
316 ret = -EINVAL;
317 goto out;
318 }
319
320 set_disk_ro(dev_to_disk(dev), set || md->read_only);
321 ret = count;
322out:
323 mmc_blk_put(md);
324 return ret;
325}
326
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200327static ssize_t
Maya Erez5a8dae12014-12-04 15:13:59 +0200328no_pack_for_random_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
330{
331 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
332 int ret;
333
Asutosh Das507d9a72014-12-09 10:15:53 +0200334 if (!md)
335 return -EINVAL;
Maya Erez5a8dae12014-12-04 15:13:59 +0200336 ret = snprintf(buf, PAGE_SIZE, "%d\n", md->queue.no_pack_for_random);
337
338 mmc_blk_put(md);
339 return ret;
340}
341
342static ssize_t
343no_pack_for_random_store(struct device *dev,
344 struct device_attribute *attr,
345 const char *buf, size_t count)
346{
347 int value;
348 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200349 struct mmc_card *card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200350 int ret = count;
351
Asutosh Das507d9a72014-12-09 10:15:53 +0200352 if (!md)
353 return -EINVAL;
354
355 card = md->queue.card;
Maya Erez5a8dae12014-12-04 15:13:59 +0200356 if (!card) {
357 ret = -EINVAL;
358 goto exit;
359 }
360
361 sscanf(buf, "%d", &value);
362
363 if (value < 0) {
364 pr_err("%s: value %d is not valid. old value remains = %d",
365 mmc_hostname(card->host), value,
366 md->queue.no_pack_for_random);
367 ret = -EINVAL;
368 goto exit;
369 }
370
371 md->queue.no_pack_for_random = (value > 0) ? true : false;
372
373 pr_debug("%s: no_pack_for_random: new value = %d",
374 mmc_hostname(card->host),
375 md->queue.no_pack_for_random);
376
377exit:
378 mmc_blk_put(md);
379 return ret;
380}
381
382static ssize_t
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200383num_wr_reqs_to_start_packing_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
385{
386 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
387 int num_wr_reqs_to_start_packing;
388 int ret;
389
Asutosh Das507d9a72014-12-09 10:15:53 +0200390 if (!md)
391 return -EINVAL;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200392 num_wr_reqs_to_start_packing = md->queue.num_wr_reqs_to_start_packing;
393
394 ret = snprintf(buf, PAGE_SIZE, "%d\n", num_wr_reqs_to_start_packing);
395
396 mmc_blk_put(md);
397 return ret;
398}
399
400static ssize_t
401num_wr_reqs_to_start_packing_store(struct device *dev,
402 struct device_attribute *attr,
403 const char *buf, size_t count)
404{
405 int value;
406 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
Asutosh Das507d9a72014-12-09 10:15:53 +0200407 struct mmc_card *card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200408 int ret = count;
409
Asutosh Das507d9a72014-12-09 10:15:53 +0200410 if (!md)
411 return -EINVAL;
412
413 card = md->queue.card;
Yaniv Gardi42399822014-12-04 00:26:23 +0200414 if (!card) {
415 ret = -EINVAL;
416 goto exit;
417 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200418
419 sscanf(buf, "%d", &value);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200420
Yaniv Gardi42399822014-12-04 00:26:23 +0200421 if (value >= 0) {
422 md->queue.num_wr_reqs_to_start_packing =
423 min_t(int, value, (int)card->ext_csd.max_packed_writes);
424
425 pr_debug("%s: trigger to pack: new value = %d",
426 mmc_hostname(card->host),
427 md->queue.num_wr_reqs_to_start_packing);
428 } else {
429 pr_err("%s: value %d is not valid. old value remains = %d",
430 mmc_hostname(card->host), value,
431 md->queue.num_wr_reqs_to_start_packing);
432 ret = -EINVAL;
433 }
434
435exit:
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200436 mmc_blk_put(md);
Yaniv Gardi42399822014-12-04 00:26:23 +0200437 return ret;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +0200438}
439
Mark Salyzyn6904e432016-01-28 11:12:25 -0800440#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
441
442static int max_read_speed, max_write_speed, cache_size = 4;
443
444module_param(max_read_speed, int, S_IRUSR | S_IRGRP);
445MODULE_PARM_DESC(max_read_speed, "maximum KB/s read speed 0=off");
446module_param(max_write_speed, int, S_IRUSR | S_IRGRP);
447MODULE_PARM_DESC(max_write_speed, "maximum KB/s write speed 0=off");
448module_param(cache_size, int, S_IRUSR | S_IRGRP);
449MODULE_PARM_DESC(cache_size, "MB high speed memory or SLC cache");
450
451/*
452 * helper macros and expectations:
453 * size - unsigned long number of bytes
454 * jiffies - unsigned long HZ timestamp difference
455 * speed - unsigned KB/s transfer rate
456 */
457#define size_and_speed_to_jiffies(size, speed) \
458 ((size) * HZ / (speed) / 1024UL)
459#define jiffies_and_speed_to_size(jiffies, speed) \
460 (((speed) * (jiffies) * 1024UL) / HZ)
461#define jiffies_and_size_to_speed(jiffies, size) \
462 ((size) * HZ / (jiffies) / 1024UL)
463
464/* Limits to report warning */
465/* jiffies_and_size_to_speed(10*HZ, queue_max_hw_sectors(q) * 512UL) ~ 25 */
466#define MIN_SPEED(q) 250 /* 10 times faster than a floppy disk */
467#define MAX_SPEED(q) jiffies_and_size_to_speed(1, queue_max_sectors(q) * 512UL)
468
469#define speed_valid(speed) ((speed) > 0)
470
471static const char off[] = "off\n";
472
473static int max_speed_show(int speed, char *buf)
474{
475 if (speed)
476 return scnprintf(buf, PAGE_SIZE, "%uKB/s\n", speed);
477 else
478 return scnprintf(buf, PAGE_SIZE, off);
479}
480
481static int max_speed_store(const char *buf, struct request_queue *q)
482{
483 unsigned int limit, set = 0;
484
485 if (!strncasecmp(off, buf, sizeof(off) - 2))
486 return set;
487 if (kstrtouint(buf, 0, &set) || (set > INT_MAX))
488 return -EINVAL;
489 if (set == 0)
490 return set;
491 limit = MAX_SPEED(q);
492 if (set > limit)
493 pr_warn("max speed %u ineffective above %u\n", set, limit);
494 limit = MIN_SPEED(q);
495 if (set < limit)
496 pr_warn("max speed %u painful below %u\n", set, limit);
497 return set;
498}
499
500static ssize_t max_write_speed_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
503 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
504 int ret = max_speed_show(atomic_read(&md->queue.max_write_speed), buf);
505
506 mmc_blk_put(md);
507 return ret;
508}
509
510static ssize_t max_write_speed_store(struct device *dev,
511 struct device_attribute *attr,
512 const char *buf, size_t count)
513{
514 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
515 int set = max_speed_store(buf, md->queue.queue);
516
517 if (set < 0) {
518 mmc_blk_put(md);
519 return set;
520 }
521
522 atomic_set(&md->queue.max_write_speed, set);
523 mmc_blk_put(md);
524 return count;
525}
526
527static const DEVICE_ATTR(max_write_speed, S_IRUGO | S_IWUSR,
528 max_write_speed_show, max_write_speed_store);
529
530static ssize_t max_read_speed_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
534 int ret = max_speed_show(atomic_read(&md->queue.max_read_speed), buf);
535
536 mmc_blk_put(md);
537 return ret;
538}
539
540static ssize_t max_read_speed_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
543{
544 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
545 int set = max_speed_store(buf, md->queue.queue);
546
547 if (set < 0) {
548 mmc_blk_put(md);
549 return set;
550 }
551
552 atomic_set(&md->queue.max_read_speed, set);
553 mmc_blk_put(md);
554 return count;
555}
556
557static const DEVICE_ATTR(max_read_speed, S_IRUGO | S_IWUSR,
558 max_read_speed_show, max_read_speed_store);
559
560static ssize_t cache_size_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
564 struct mmc_queue *mq = &md->queue;
565 int cache_size = atomic_read(&mq->cache_size);
566 int ret;
567
568 if (!cache_size)
569 ret = scnprintf(buf, PAGE_SIZE, off);
570 else {
571 int speed = atomic_read(&mq->max_write_speed);
572
573 if (!speed_valid(speed))
574 ret = scnprintf(buf, PAGE_SIZE, "%uMB\n", cache_size);
575 else { /* We accept race between cache_jiffies and cache_used */
576 unsigned long size = jiffies_and_speed_to_size(
577 jiffies - mq->cache_jiffies, speed);
578 long used = atomic_long_read(&mq->cache_used);
579
580 if (size >= used)
581 size = 0;
582 else
583 size = (used - size) * 100 / cache_size
584 / 1024UL / 1024UL;
585
586 ret = scnprintf(buf, PAGE_SIZE, "%uMB %lu%% used\n",
587 cache_size, size);
588 }
589 }
590
591 mmc_blk_put(md);
592 return ret;
593}
594
595static ssize_t cache_size_store(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf, size_t count)
598{
599 struct mmc_blk_data *md;
600 unsigned int set = 0;
601
602 if (strncasecmp(off, buf, sizeof(off) - 2)
603 && (kstrtouint(buf, 0, &set) || (set > INT_MAX)))
604 return -EINVAL;
605
606 md = mmc_blk_get(dev_to_disk(dev));
607 atomic_set(&md->queue.cache_size, set);
608 mmc_blk_put(md);
609 return count;
610}
611
612static const DEVICE_ATTR(cache_size, S_IRUGO | S_IWUSR,
613 cache_size_show, cache_size_store);
614
615/* correct for write-back */
616static long mmc_blk_cache_used(struct mmc_queue *mq, unsigned long waitfor)
617{
618 long used = 0;
619 int speed = atomic_read(&mq->max_write_speed);
620
621 if (speed_valid(speed)) {
622 unsigned long size = jiffies_and_speed_to_size(
623 waitfor - mq->cache_jiffies, speed);
624 used = atomic_long_read(&mq->cache_used);
625
626 if (size >= used)
627 used = 0;
628 else
629 used -= size;
630 }
631
632 atomic_long_set(&mq->cache_used, used);
633 mq->cache_jiffies = waitfor;
634
635 return used;
636}
637
638static void mmc_blk_simulate_delay(
639 struct mmc_queue *mq,
640 struct request *req,
641 unsigned long waitfor)
642{
643 int max_speed;
644
645 if (!req)
646 return;
647
648 max_speed = (rq_data_dir(req) == READ)
649 ? atomic_read(&mq->max_read_speed)
650 : atomic_read(&mq->max_write_speed);
651 if (speed_valid(max_speed)) {
652 unsigned long bytes = blk_rq_bytes(req);
653
654 if (rq_data_dir(req) != READ) {
655 int cache_size = atomic_read(&mq->cache_size);
656
657 if (cache_size) {
658 unsigned long size = cache_size * 1024L * 1024L;
659 long used = mmc_blk_cache_used(mq, waitfor);
660
661 used += bytes;
662 atomic_long_set(&mq->cache_used, used);
663 bytes = 0;
664 if (used > size)
665 bytes = used - size;
666 }
667 }
668 waitfor += size_and_speed_to_jiffies(bytes, max_speed);
669 if (time_is_after_jiffies(waitfor)) {
670 long msecs = jiffies_to_msecs(waitfor - jiffies);
671
672 if (likely(msecs > 0))
673 msleep(msecs);
674 }
675 }
676}
677
678#else
679
680#define mmc_blk_simulate_delay(mq, req, waitfor)
681
682#endif
683
Al Viroa5a15612008-03-02 10:33:30 -0500684static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Al Viroa5a15612008-03-02 10:33:30 -0500686 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int ret = -ENXIO;
688
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200689 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (md) {
691 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500692 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700694
Al Viroa5a15612008-03-02 10:33:30 -0500695 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700696 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700697 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200700 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 return ret;
703}
704
Al Virodb2a1442013-05-05 21:52:57 -0400705static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Al Viroa5a15612008-03-02 10:33:30 -0500707 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200709 mutex_lock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 mmc_blk_put(md);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200711 mutex_unlock(&block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800715mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800717 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
718 geo->heads = 4;
719 geo->sectors = 16;
720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
John Calixtocb87ea22011-04-26 18:56:29 -0400723struct mmc_blk_ioc_data {
724 struct mmc_ioc_cmd ic;
725 unsigned char *buf;
726 u64 buf_bytes;
727};
728
729static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
730 struct mmc_ioc_cmd __user *user)
731{
732 struct mmc_blk_ioc_data *idata;
733 int err;
734
yalin wang1ff89502015-11-12 19:27:11 +0800735 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400736 if (!idata) {
737 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400738 goto out;
John Calixtocb87ea22011-04-26 18:56:29 -0400739 }
740
741 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
742 err = -EFAULT;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400743 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400744 }
745
746 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
747 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
748 err = -EOVERFLOW;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400749 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400750 }
751
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300752 if (!idata->buf_bytes) {
753 idata->buf = NULL;
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100754 return idata;
Ville Viinikkabfe5b1b2016-07-08 18:27:02 +0300755 }
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100756
yalin wang1ff89502015-11-12 19:27:11 +0800757 idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
John Calixtocb87ea22011-04-26 18:56:29 -0400758 if (!idata->buf) {
759 err = -ENOMEM;
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400760 goto idata_err;
John Calixtocb87ea22011-04-26 18:56:29 -0400761 }
762
763 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
764 idata->ic.data_ptr, idata->buf_bytes)) {
765 err = -EFAULT;
766 goto copy_err;
767 }
768
769 return idata;
770
771copy_err:
772 kfree(idata->buf);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400773idata_err:
John Calixtocb87ea22011-04-26 18:56:29 -0400774 kfree(idata);
Vladimir Motykaaea253e2011-05-11 00:00:43 -0400775out:
John Calixtocb87ea22011-04-26 18:56:29 -0400776 return ERR_PTR(err);
John Calixtocb87ea22011-04-26 18:56:29 -0400777}
778
Jon Huntera5f57742015-09-22 10:27:53 +0100779static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
780 struct mmc_blk_ioc_data *idata)
781{
782 struct mmc_ioc_cmd *ic = &idata->ic;
783
784 if (copy_to_user(&(ic_ptr->response), ic->response,
785 sizeof(ic->response)))
786 return -EFAULT;
787
788 if (!idata->ic.write_flag) {
789 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
790 idata->buf, idata->buf_bytes))
791 return -EFAULT;
792 }
793
794 return 0;
795}
796
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200797static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
798 u32 retries_max)
799{
800 int err;
801 u32 retry_count = 0;
802
803 if (!status || !retries_max)
804 return -EINVAL;
805
806 do {
807 err = get_card_status(card, status, 5);
808 if (err)
809 break;
810
811 if (!R1_STATUS(*status) &&
812 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
813 break; /* RPMB programming operation complete */
814
815 /*
816 * Rechedule to give the MMC device a chance to continue
817 * processing the previous command without being polled too
818 * frequently.
819 */
820 usleep_range(1000, 5000);
821 } while (++retry_count < retries_max);
822
823 if (retry_count == retries_max)
824 err = -EPERM;
825
826 return err;
827}
828
Maya Erez775a9362013-04-18 15:41:55 +0300829static int ioctl_do_sanitize(struct mmc_card *card)
830{
831 int err;
832
Ulf Hanssona2d10862013-12-16 14:37:26 +0100833 if (!mmc_can_sanitize(card)) {
Maya Erez775a9362013-04-18 15:41:55 +0300834 pr_warn("%s: %s - SANITIZE is not supported\n",
835 mmc_hostname(card->host), __func__);
836 err = -EOPNOTSUPP;
837 goto out;
838 }
839
840 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
841 mmc_hostname(card->host), __func__);
842
843 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
844 EXT_CSD_SANITIZE_START, 1,
845 MMC_SANITIZE_REQ_TIMEOUT);
846
847 if (err)
848 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
849 mmc_hostname(card->host), __func__, err);
850
851 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
852 __func__);
853out:
854 return err;
855}
856
Jon Huntera5f57742015-09-22 10:27:53 +0100857static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
858 struct mmc_blk_ioc_data *idata)
John Calixtocb87ea22011-04-26 18:56:29 -0400859{
John Calixtocb87ea22011-04-26 18:56:29 -0400860 struct mmc_command cmd = {0};
861 struct mmc_data data = {0};
Venkatraman Sad5fd972011-08-25 00:30:50 +0530862 struct mmc_request mrq = {NULL};
John Calixtocb87ea22011-04-26 18:56:29 -0400863 struct scatterlist sg;
864 int err;
865
Jon Huntera5f57742015-09-22 10:27:53 +0100866 if (!card || !md || !idata)
867 return -EINVAL;
John Calixtocb87ea22011-04-26 18:56:29 -0400868
Johan Rudholm4d6144d2011-11-23 09:05:58 +0100869 cmd.opcode = idata->ic.opcode;
870 cmd.arg = idata->ic.arg;
871 cmd.flags = idata->ic.flags;
872
873 if (idata->buf_bytes) {
874 data.sg = &sg;
875 data.sg_len = 1;
876 data.blksz = idata->ic.blksz;
877 data.blocks = idata->ic.blocks;
878
879 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
880
881 if (idata->ic.write_flag)
882 data.flags = MMC_DATA_WRITE;
883 else
884 data.flags = MMC_DATA_READ;
885
886 /* data.flags must already be set before doing this. */
887 mmc_set_data_timeout(&data, card);
888
889 /* Allow overriding the timeout_ns for empirical tuning. */
890 if (idata->ic.data_timeout_ns)
891 data.timeout_ns = idata->ic.data_timeout_ns;
892
893 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
894 /*
895 * Pretend this is a data transfer and rely on the
896 * host driver to compute timeout. When all host
897 * drivers support cmd.cmd_timeout for R1B, this
898 * can be changed to:
899 *
900 * mrq.data = NULL;
901 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
902 */
903 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
904 }
905
906 mrq.data = &data;
907 }
908
909 mrq.cmd = &cmd;
910
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200911 err = mmc_blk_part_switch(card, md);
912 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100913 return err;
Loic Pallardy8d1e9772012-08-06 17:12:31 +0200914
John Calixtocb87ea22011-04-26 18:56:29 -0400915 if (idata->ic.is_acmd) {
916 err = mmc_app_cmd(card->host, card);
917 if (err)
Jon Huntera5f57742015-09-22 10:27:53 +0100918 return err;
John Calixtocb87ea22011-04-26 18:56:29 -0400919 }
920
Yaniv Gardia82e4842013-06-05 14:13:08 +0300921 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
922 (cmd.opcode == MMC_SWITCH)) {
Maya Erez775a9362013-04-18 15:41:55 +0300923 err = ioctl_do_sanitize(card);
924
925 if (err)
926 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
927 __func__, err);
928
Jon Huntera5f57742015-09-22 10:27:53 +0100929 return err;
Maya Erez775a9362013-04-18 15:41:55 +0300930 }
931
John Calixtocb87ea22011-04-26 18:56:29 -0400932 mmc_wait_for_req(card->host, &mrq);
933
934 if (cmd.error) {
935 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
936 __func__, cmd.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100937 return cmd.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400938 }
939 if (data.error) {
940 dev_err(mmc_dev(card->host), "%s: data error %d\n",
941 __func__, data.error);
Jon Huntera5f57742015-09-22 10:27:53 +0100942 return data.error;
John Calixtocb87ea22011-04-26 18:56:29 -0400943 }
944
945 /*
946 * According to the SD specs, some commands require a delay after
947 * issuing the command.
948 */
949 if (idata->ic.postsleep_min_us)
950 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
951
Jon Huntera5f57742015-09-22 10:27:53 +0100952 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
John Calixtocb87ea22011-04-26 18:56:29 -0400953
Krishna Kondae6711632014-12-04 15:20:57 +0200954 return err;
955}
956
957struct mmc_blk_ioc_rpmb_data {
958 struct mmc_blk_ioc_data *data[MMC_IOC_MAX_RPMB_CMD];
959};
960
961static struct mmc_blk_ioc_rpmb_data *mmc_blk_ioctl_rpmb_copy_from_user(
962 struct mmc_ioc_rpmb __user *user)
963{
964 struct mmc_blk_ioc_rpmb_data *idata;
965 int err, i;
966
967 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
968 if (!idata) {
969 err = -ENOMEM;
970 goto out;
971 }
972
973 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
974 idata->data[i] = mmc_blk_ioctl_copy_from_user(&(user->cmds[i]));
975 if (IS_ERR(idata->data[i])) {
976 err = PTR_ERR(idata->data[i]);
977 goto copy_err;
978 }
979 }
980
981 return idata;
982
983copy_err:
984 while (--i >= 0) {
985 kfree(idata->data[i]->buf);
986 kfree(idata->data[i]);
987 }
988 kfree(idata);
989out:
990 return ERR_PTR(err);
991}
992
993static int mmc_blk_ioctl_rpmb_cmd(struct block_device *bdev,
994 struct mmc_ioc_rpmb __user *ic_ptr)
995{
996 struct mmc_blk_ioc_rpmb_data *idata;
997 struct mmc_blk_data *md;
998 struct mmc_card *card;
999 struct mmc_command cmd = {0};
1000 struct mmc_data data = {0};
1001 struct mmc_request mrq = {NULL};
1002 struct scatterlist sg;
1003 int err = 0, i = 0;
1004 u32 status = 0;
1005
1006 /* The caller must have CAP_SYS_RAWIO */
1007 if (!capable(CAP_SYS_RAWIO))
1008 return -EPERM;
1009
1010 md = mmc_blk_get(bdev->bd_disk);
1011 /* make sure this is a rpmb partition */
1012 if ((!md) || (!(md->area_type & MMC_BLK_DATA_AREA_RPMB))) {
1013 err = -EINVAL;
Asutosh Das507d9a72014-12-09 10:15:53 +02001014 return err;
Krishna Kondae6711632014-12-04 15:20:57 +02001015 }
1016
1017 idata = mmc_blk_ioctl_rpmb_copy_from_user(ic_ptr);
1018 if (IS_ERR(idata)) {
1019 err = PTR_ERR(idata);
1020 goto cmd_done;
1021 }
1022
1023 card = md->queue.card;
1024 if (IS_ERR(card)) {
1025 err = PTR_ERR(card);
1026 goto idata_free;
1027 }
1028
Maya Erezdd669562015-02-12 20:37:31 +02001029 mmc_get_card(card);
Krishna Kondae6711632014-12-04 15:20:57 +02001030
1031 err = mmc_blk_part_switch(card, md);
1032 if (err)
1033 goto cmd_rel_host;
1034
1035 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1036 struct mmc_blk_ioc_data *curr_data;
1037 struct mmc_ioc_cmd *curr_cmd;
1038
1039 curr_data = idata->data[i];
1040 curr_cmd = &curr_data->ic;
1041 if (!curr_cmd->opcode)
1042 break;
1043
1044 cmd.opcode = curr_cmd->opcode;
1045 cmd.arg = curr_cmd->arg;
1046 cmd.flags = curr_cmd->flags;
1047
1048 if (curr_data->buf_bytes) {
1049 data.sg = &sg;
1050 data.sg_len = 1;
1051 data.blksz = curr_cmd->blksz;
1052 data.blocks = curr_cmd->blocks;
1053
1054 sg_init_one(data.sg, curr_data->buf,
1055 curr_data->buf_bytes);
1056
1057 if (curr_cmd->write_flag)
1058 data.flags = MMC_DATA_WRITE;
1059 else
1060 data.flags = MMC_DATA_READ;
1061
1062 /* data.flags must already be set before doing this. */
1063 mmc_set_data_timeout(&data, card);
1064
1065 /*
1066 * Allow overriding the timeout_ns for empirical tuning.
1067 */
1068 if (curr_cmd->data_timeout_ns)
1069 data.timeout_ns = curr_cmd->data_timeout_ns;
1070
1071 mrq.data = &data;
1072 }
1073
1074 mrq.cmd = &cmd;
1075
1076 err = mmc_set_blockcount(card, data.blocks,
1077 curr_cmd->write_flag & (1 << 31));
1078 if (err)
1079 goto cmd_rel_host;
1080
1081 mmc_wait_for_req(card->host, &mrq);
1082
1083 if (cmd.error) {
1084 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
1085 __func__, cmd.error);
1086 err = cmd.error;
1087 goto cmd_rel_host;
1088 }
1089 if (data.error) {
1090 dev_err(mmc_dev(card->host), "%s: data error %d\n",
1091 __func__, data.error);
1092 err = data.error;
1093 goto cmd_rel_host;
1094 }
1095
1096 if (copy_to_user(&(ic_ptr->cmds[i].response), cmd.resp,
1097 sizeof(cmd.resp))) {
1098 err = -EFAULT;
1099 goto cmd_rel_host;
1100 }
1101
1102 if (!curr_cmd->write_flag) {
1103 if (copy_to_user((void __user *)(unsigned long)
1104 curr_cmd->data_ptr,
1105 curr_data->buf,
1106 curr_data->buf_bytes)) {
1107 err = -EFAULT;
1108 goto cmd_rel_host;
1109 }
1110 }
1111
Loic Pallardy8d1e9772012-08-06 17:12:31 +02001112 /*
1113 * Ensure RPMB command has completed by polling CMD13
1114 * "Send Status".
1115 */
1116 err = ioctl_rpmb_card_status_poll(card, &status, 5);
1117 if (err)
1118 dev_err(mmc_dev(card->host),
1119 "%s: Card Status=0x%08X, error %d\n",
1120 __func__, status, err);
1121 }
1122
Krishna Kondae6711632014-12-04 15:20:57 +02001123cmd_rel_host:
1124 mmc_put_card(card);
1125
1126idata_free:
1127 for (i = 0; i < MMC_IOC_MAX_RPMB_CMD; i++) {
1128 kfree(idata->data[i]->buf);
1129 kfree(idata->data[i]);
1130 }
1131 kfree(idata);
1132
1133cmd_done:
1134 mmc_blk_put(md);
Jon Huntera5f57742015-09-22 10:27:53 +01001135 return err;
1136}
1137
1138static int mmc_blk_ioctl_cmd(struct block_device *bdev,
1139 struct mmc_ioc_cmd __user *ic_ptr)
1140{
1141 struct mmc_blk_ioc_data *idata;
1142 struct mmc_blk_data *md;
1143 struct mmc_card *card;
Grant Grundlerb0934102015-09-23 18:30:33 -07001144 int err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001145
Shawn Lin83c742c2016-03-16 18:15:47 +08001146 /*
1147 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1148 * whole block device, not on a partition. This prevents overspray
1149 * between sibling partitions.
1150 */
1151 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1152 return -EPERM;
1153
Jon Huntera5f57742015-09-22 10:27:53 +01001154 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
Asutosh Dasbbefab32013-10-07 14:53:32 +05301155 if (IS_ERR_OR_NULL(idata))
Jon Huntera5f57742015-09-22 10:27:53 +01001156 return PTR_ERR(idata);
1157
1158 md = mmc_blk_get(bdev->bd_disk);
1159 if (!md) {
1160 err = -EINVAL;
1161 goto cmd_err;
1162 }
1163
1164 card = md->queue.card;
Asutosh Dasbbefab32013-10-07 14:53:32 +05301165 if (IS_ERR_OR_NULL(card)) {
Jon Huntera5f57742015-09-22 10:27:53 +01001166 err = PTR_ERR(card);
1167 goto cmd_done;
1168 }
1169
1170 mmc_get_card(card);
1171
Grant Grundlerb0934102015-09-23 18:30:33 -07001172 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001173
Adrian Hunter3c866562016-05-04 14:38:12 +03001174 /* Always switch back to main area after RPMB access */
1175 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1176 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1177
Ulf Hanssone94cfef2013-05-02 14:02:38 +02001178 mmc_put_card(card);
John Calixtocb87ea22011-04-26 18:56:29 -04001179
Grant Grundlerb0934102015-09-23 18:30:33 -07001180 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
Jon Huntera5f57742015-09-22 10:27:53 +01001181
John Calixtocb87ea22011-04-26 18:56:29 -04001182cmd_done:
1183 mmc_blk_put(md);
Philippe De Swert1c02f002012-04-11 23:31:45 +03001184cmd_err:
John Calixtocb87ea22011-04-26 18:56:29 -04001185 kfree(idata->buf);
1186 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001187 return ioc_err ? ioc_err : err;
John Calixtocb87ea22011-04-26 18:56:29 -04001188}
1189
Jon Huntera5f57742015-09-22 10:27:53 +01001190static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
1191 struct mmc_ioc_multi_cmd __user *user)
1192{
1193 struct mmc_blk_ioc_data **idata = NULL;
1194 struct mmc_ioc_cmd __user *cmds = user->cmds;
1195 struct mmc_card *card;
1196 struct mmc_blk_data *md;
Grant Grundlerb0934102015-09-23 18:30:33 -07001197 int i, err = 0, ioc_err = 0;
Jon Huntera5f57742015-09-22 10:27:53 +01001198 __u64 num_of_cmds;
1199
Shawn Lin83c742c2016-03-16 18:15:47 +08001200 /*
1201 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
1202 * whole block device, not on a partition. This prevents overspray
1203 * between sibling partitions.
1204 */
1205 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
1206 return -EPERM;
1207
Jon Huntera5f57742015-09-22 10:27:53 +01001208 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
1209 sizeof(num_of_cmds)))
1210 return -EFAULT;
1211
1212 if (num_of_cmds > MMC_IOC_MAX_CMDS)
1213 return -EINVAL;
1214
1215 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
1216 if (!idata)
1217 return -ENOMEM;
1218
1219 for (i = 0; i < num_of_cmds; i++) {
1220 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
1221 if (IS_ERR(idata[i])) {
1222 err = PTR_ERR(idata[i]);
1223 num_of_cmds = i;
1224 goto cmd_err;
1225 }
1226 }
1227
1228 md = mmc_blk_get(bdev->bd_disk);
Olof Johanssonf00ab142016-02-09 09:34:30 -08001229 if (!md) {
1230 err = -EINVAL;
Jon Huntera5f57742015-09-22 10:27:53 +01001231 goto cmd_err;
Olof Johanssonf00ab142016-02-09 09:34:30 -08001232 }
Jon Huntera5f57742015-09-22 10:27:53 +01001233
1234 card = md->queue.card;
1235 if (IS_ERR(card)) {
1236 err = PTR_ERR(card);
1237 goto cmd_done;
1238 }
1239
1240 mmc_get_card(card);
1241
Grant Grundlerb0934102015-09-23 18:30:33 -07001242 for (i = 0; i < num_of_cmds && !ioc_err; i++)
1243 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001244
Adrian Hunter3c866562016-05-04 14:38:12 +03001245 /* Always switch back to main area after RPMB access */
1246 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1247 mmc_blk_part_switch(card, dev_get_drvdata(&card->dev));
1248
Jon Huntera5f57742015-09-22 10:27:53 +01001249 mmc_put_card(card);
1250
1251 /* copy to user if data and response */
Grant Grundlerb0934102015-09-23 18:30:33 -07001252 for (i = 0; i < num_of_cmds && !err; i++)
Jon Huntera5f57742015-09-22 10:27:53 +01001253 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
Jon Huntera5f57742015-09-22 10:27:53 +01001254
1255cmd_done:
1256 mmc_blk_put(md);
1257cmd_err:
1258 for (i = 0; i < num_of_cmds; i++) {
1259 kfree(idata[i]->buf);
1260 kfree(idata[i]);
1261 }
1262 kfree(idata);
Grant Grundlerb0934102015-09-23 18:30:33 -07001263 return ioc_err ? ioc_err : err;
Jon Huntera5f57742015-09-22 10:27:53 +01001264}
1265
John Calixtocb87ea22011-04-26 18:56:29 -04001266static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
1267 unsigned int cmd, unsigned long arg)
1268{
Jon Huntera5f57742015-09-22 10:27:53 +01001269 switch (cmd) {
1270 case MMC_IOC_CMD:
1271 return mmc_blk_ioctl_cmd(bdev,
1272 (struct mmc_ioc_cmd __user *)arg);
Krishna Kondae6711632014-12-04 15:20:57 +02001273 case MMC_IOC_RPMB_CMD:
1274 return mmc_blk_ioctl_rpmb_cmd(bdev,
1275 (struct mmc_ioc_rpmb __user *)arg);
Jon Huntera5f57742015-09-22 10:27:53 +01001276 case MMC_IOC_MULTI_CMD:
1277 return mmc_blk_ioctl_multi_cmd(bdev,
1278 (struct mmc_ioc_multi_cmd __user *)arg);
1279 default:
1280 return -EINVAL;
1281 }
John Calixtocb87ea22011-04-26 18:56:29 -04001282}
1283
1284#ifdef CONFIG_COMPAT
1285static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
1286 unsigned int cmd, unsigned long arg)
1287{
1288 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
1289}
1290#endif
1291
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001292static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -05001293 .open = mmc_blk_open,
1294 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001295 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 .owner = THIS_MODULE,
John Calixtocb87ea22011-04-26 18:56:29 -04001297 .ioctl = mmc_blk_ioctl,
1298#ifdef CONFIG_COMPAT
1299 .compat_ioctl = mmc_blk_compat_ioctl,
1300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301};
1302
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001303static int mmc_blk_cmdq_switch(struct mmc_card *card,
1304 struct mmc_blk_data *md, bool enable)
1305{
1306 int ret = 0;
1307 bool cmdq_mode = !!mmc_card_cmdq(card);
1308
1309 if (!(card->host->caps2 & MMC_CAP2_CMD_QUEUE) ||
1310 !card->ext_csd.cmdq_support ||
1311 (enable && !(md->flags & MMC_BLK_CMD_QUEUE)) ||
1312 (cmdq_mode == enable))
1313 return 0;
1314
1315 if (enable) {
1316 ret = mmc_set_blocklen(card, MMC_CARD_CMDQ_BLK_SIZE);
1317 if (ret) {
1318 pr_err("%s: failed (%d) to set block-size to %d\n",
1319 __func__, ret, MMC_CARD_CMDQ_BLK_SIZE);
1320 goto out;
1321 }
1322 }
1323
1324 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1325 EXT_CSD_CMDQ, enable,
1326 card->ext_csd.generic_cmd6_time);
1327 if (ret) {
1328 pr_err("%s: cmdq mode %sable failed %d\n",
1329 md->disk->disk_name, enable ? "en" : "dis", ret);
1330 goto out;
1331 }
1332
1333 if (enable)
1334 mmc_card_set_cmdq(card);
1335 else
1336 mmc_card_clr_cmdq(card);
1337out:
1338 return ret;
1339}
1340
Andrei Warkentin371a6892011-04-11 18:10:25 -05001341static inline int mmc_blk_part_switch(struct mmc_card *card,
1342 struct mmc_blk_data *md)
1343{
1344 int ret;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001345 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001346
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001347 if ((main_md->part_curr == md->part_type) &&
1348 (card->part_curr == md->part_type))
Andrei Warkentin371a6892011-04-11 18:10:25 -05001349 return 0;
1350
1351 if (mmc_card_mmc(card)) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001352 u8 part_config = card->ext_csd.part_config;
1353
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001354 if (md->part_type) {
1355 /* disable CQ mode for non-user data partitions */
1356 ret = mmc_blk_cmdq_switch(card, md, false);
1357 if (ret)
1358 return ret;
1359 }
1360
Adrian Hunter57da0c02016-05-04 14:38:13 +03001361 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1362 mmc_retune_pause(card->host);
1363
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001364 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1365 part_config |= md->part_type;
Andrei Warkentin371a6892011-04-11 18:10:25 -05001366
1367 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001368 EXT_CSD_PART_CONFIG, part_config,
Andrei Warkentin371a6892011-04-11 18:10:25 -05001369 card->ext_csd.part_time);
Adrian Hunter57da0c02016-05-04 14:38:13 +03001370 if (ret) {
1371 if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1372 mmc_retune_unpause(card->host);
Andrei Warkentin371a6892011-04-11 18:10:25 -05001373 return ret;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001374 }
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03001375
1376 card->ext_csd.part_config = part_config;
Oluwafemi Adeyemif952a472013-01-03 11:32:53 -08001377 card->part_curr = md->part_type;
Adrian Hunter57da0c02016-05-04 14:38:13 +03001378
1379 if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB)
1380 mmc_retune_unpause(card->host);
Adrian Hunter67716322011-08-29 16:42:15 +03001381 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05001382
1383 main_md->part_curr = md->part_type;
1384 return 0;
1385}
1386
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001387static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
1388{
1389 int err;
Ben Dooks051913d2009-06-08 23:33:57 +01001390 u32 result;
1391 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001392
Venkatraman Sad5fd972011-08-25 00:30:50 +05301393 struct mmc_request mrq = {NULL};
Chris Ball1278dba2011-04-13 23:40:30 -04001394 struct mmc_command cmd = {0};
Chris Balla61ad2b2011-04-13 23:46:05 -04001395 struct mmc_data data = {0};
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001396
1397 struct scatterlist sg;
1398
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001399 cmd.opcode = MMC_APP_CMD;
1400 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -07001401 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001402
1403 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -07001404 if (err)
1405 return (u32)-1;
1406 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001407 return (u32)-1;
1408
1409 memset(&cmd, 0, sizeof(struct mmc_command));
1410
1411 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
1412 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -07001413 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001414
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001415 data.blksz = 4;
1416 data.blocks = 1;
1417 data.flags = MMC_DATA_READ;
1418 data.sg = &sg;
1419 data.sg_len = 1;
Subhash Jadavanid3804432012-06-13 17:10:43 +05301420 mmc_set_data_timeout(&data, card);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001421
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001422 mrq.cmd = &cmd;
1423 mrq.data = &data;
1424
Ben Dooks051913d2009-06-08 23:33:57 +01001425 blocks = kmalloc(4, GFP_KERNEL);
1426 if (!blocks)
1427 return (u32)-1;
1428
1429 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001430
1431 mmc_wait_for_req(card->host, &mrq);
1432
Ben Dooks051913d2009-06-08 23:33:57 +01001433 result = ntohl(*blocks);
1434 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001435
Ben Dooks051913d2009-06-08 23:33:57 +01001436 if (cmd.error || data.error)
1437 result = (u32)-1;
1438
1439 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -07001440}
1441
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001442static int get_card_status(struct mmc_card *card, u32 *status, int retries)
Adrian Hunter504f1912008-10-16 12:55:25 +03001443{
Chris Ball1278dba2011-04-13 23:40:30 -04001444 struct mmc_command cmd = {0};
Adrian Hunter504f1912008-10-16 12:55:25 +03001445 int err;
1446
Adrian Hunter504f1912008-10-16 12:55:25 +03001447 cmd.opcode = MMC_SEND_STATUS;
1448 if (!mmc_host_is_spi(card->host))
1449 cmd.arg = card->rca << 16;
1450 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
Russell King - ARM Linux0a2d4042011-06-20 20:10:08 +01001451 err = mmc_wait_for_cmd(card->host, &cmd, retries);
1452 if (err == 0)
1453 *status = cmd.resp[0];
1454 return err;
Adrian Hunter504f1912008-10-16 12:55:25 +03001455}
1456
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001457static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
Ulf Hansson95a91292014-01-29 13:11:27 +01001458 bool hw_busy_detect, struct request *req, int *gen_err)
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001459{
1460 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
1461 int err = 0;
1462 u32 status;
1463
1464 do {
1465 err = get_card_status(card, &status, 5);
1466 if (err) {
1467 pr_err("%s: error %d requesting status\n",
1468 req->rq_disk->disk_name, err);
1469 return err;
1470 }
1471
1472 if (status & R1_ERROR) {
1473 pr_err("%s: %s: error sending status cmd, status %#x\n",
1474 req->rq_disk->disk_name, __func__, status);
1475 *gen_err = 1;
1476 }
1477
Ulf Hansson95a91292014-01-29 13:11:27 +01001478 /* We may rely on the host hw to handle busy detection.*/
1479 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
1480 hw_busy_detect)
1481 break;
1482
Ulf Hanssonc49433f2014-01-29 11:01:55 +01001483 /*
1484 * Timeout if the device never becomes ready for data and never
1485 * leaves the program state.
1486 */
1487 if (time_after(jiffies, timeout)) {
1488 pr_err("%s: Card stuck in programming state! %s %s\n",
1489 mmc_hostname(card->host),
1490 req->rq_disk->disk_name, __func__);
1491 return -ETIMEDOUT;
1492 }
1493
1494 /*
1495 * Some cards mishandle the status bits,
1496 * so make sure to check both the busy
1497 * indication and the card state.
1498 */
1499 } while (!(status & R1_READY_FOR_DATA) ||
1500 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1501
1502 return err;
1503}
1504
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001505static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
1506 struct request *req, int *gen_err, u32 *stop_status)
1507{
1508 struct mmc_host *host = card->host;
1509 struct mmc_command cmd = {0};
1510 int err;
1511 bool use_r1b_resp = rq_data_dir(req) == WRITE;
1512
1513 /*
1514 * Normally we use R1B responses for WRITE, but in cases where the host
1515 * has specified a max_busy_timeout we need to validate it. A failure
1516 * means we need to prevent the host from doing hw busy detection, which
1517 * is done by converting to a R1 response instead.
1518 */
1519 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
1520 use_r1b_resp = false;
1521
1522 cmd.opcode = MMC_STOP_TRANSMISSION;
1523 if (use_r1b_resp) {
1524 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1525 cmd.busy_timeout = timeout_ms;
1526 } else {
1527 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1528 }
1529
1530 err = mmc_wait_for_cmd(host, &cmd, 5);
1531 if (err)
1532 return err;
1533
1534 *stop_status = cmd.resp[0];
1535
1536 /* No need to check card status in case of READ. */
1537 if (rq_data_dir(req) == READ)
1538 return 0;
1539
1540 if (!mmc_host_is_spi(host) &&
1541 (*stop_status & R1_ERROR)) {
1542 pr_err("%s: %s: general error sending stop command, resp %#x\n",
1543 req->rq_disk->disk_name, __func__, *stop_status);
1544 *gen_err = 1;
1545 }
1546
1547 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
1548}
1549
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301550#define ERR_NOMEDIUM 3
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001551#define ERR_RETRY 2
1552#define ERR_ABORT 1
1553#define ERR_CONTINUE 0
1554
1555static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
1556 bool status_valid, u32 status)
1557{
1558 switch (error) {
1559 case -EILSEQ:
1560 /* response crc error, retry the r/w cmd */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001561 pr_err_ratelimited(
1562 "%s: response CRC error sending %s command, card status %#x\n",
1563 req->rq_disk->disk_name,
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001564 name, status);
1565 return ERR_RETRY;
1566
1567 case -ETIMEDOUT:
Talel Shenhar0821fe852015-01-28 14:44:57 +02001568 pr_err_ratelimited(
1569 "%s: timed out sending %s command, card status %#x\n",
1570 req->rq_disk->disk_name, name, status);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001571
1572 /* If the status cmd initially failed, retry the r/w cmd */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301573 if (!status_valid) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001574 pr_err_ratelimited("%s: status not valid, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301575 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001576 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301577 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001578
1579 /*
1580 * If it was a r/w cmd crc error, or illegal command
1581 * (eg, issued in wrong state) then retry - we should
1582 * have corrected the state problem above.
1583 */
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301584 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
Talel Shenhar0821fe852015-01-28 14:44:57 +02001585 pr_err_ratelimited(
1586 "%s: command error, retrying timeout\n",
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301587 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001588 return ERR_RETRY;
Ken Sumrallcc4d04b2016-05-10 14:53:13 +05301589 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001590
1591 /* Otherwise abort the command */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001592 pr_err_ratelimited(
1593 "%s: not retrying timeout\n",
1594 req->rq_disk->disk_name);
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001595 return ERR_ABORT;
1596
1597 default:
1598 /* We don't understand the error code the driver gave us */
Talel Shenhar0821fe852015-01-28 14:44:57 +02001599 pr_err_ratelimited(
1600 "%s: unknown error %d sending read/write command, card status %#x\n",
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001601 req->rq_disk->disk_name, error, status);
1602 return ERR_ABORT;
1603 }
1604}
1605
1606/*
1607 * Initial r/w and stop cmd error recovery.
1608 * We don't know whether the card received the r/w cmd or not, so try to
1609 * restore things back to a sane state. Essentially, we do this as follows:
1610 * - Obtain card status. If the first attempt to obtain card status fails,
1611 * the status word will reflect the failed status cmd, not the failed
1612 * r/w cmd. If we fail to obtain card status, it suggests we can no
1613 * longer communicate with the card.
1614 * - Check the card state. If the card received the cmd but there was a
1615 * transient problem with the response, it might still be in a data transfer
1616 * mode. Try to send it a stop command. If this fails, we can't recover.
1617 * - If the r/w cmd failed due to a response CRC error, it was probably
1618 * transient, so retry the cmd.
1619 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1620 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1621 * illegal cmd, retry.
1622 * Otherwise we don't understand what happened, so abort.
1623 */
1624static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001625 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001626{
1627 bool prev_cmd_status_valid = true;
1628 u32 status, stop_status = 0;
1629 int err, retry;
1630
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301631 if (mmc_card_removed(card))
1632 return ERR_NOMEDIUM;
1633
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001634 /*
1635 * Try to get card status which indicates both the card state
1636 * and why there was no response. If the first attempt fails,
1637 * we can't be sure the returned status is for the r/w command.
1638 */
1639 for (retry = 2; retry >= 0; retry--) {
1640 err = get_card_status(card, &status, 0);
1641 if (!err)
1642 break;
1643
Adrian Hunter6f398ad2015-05-07 13:10:23 +03001644 /* Re-tune if needed */
1645 mmc_retune_recheck(card->host);
1646
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001647 prev_cmd_status_valid = false;
1648 pr_err("%s: error %d sending status command, %sing\n",
1649 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1650 }
1651
1652 /* We couldn't get a response from the card. Give up. */
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301653 if (err) {
1654 /* Check if the card is removed */
1655 if (mmc_detect_card_removed(card->host))
1656 return ERR_NOMEDIUM;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001657 return ERR_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301658 }
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001659
Adrian Hunter67716322011-08-29 16:42:15 +03001660 /* Flag ECC errors */
1661 if ((status & R1_CARD_ECC_FAILED) ||
1662 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1663 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1664 *ecc_err = 1;
1665
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001666 /* Flag General errors */
1667 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1668 if ((status & R1_ERROR) ||
1669 (brq->stop.resp[0] & R1_ERROR)) {
1670 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1671 req->rq_disk->disk_name, __func__,
1672 brq->stop.resp[0], status);
1673 *gen_err = 1;
1674 }
1675
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001676 /*
1677 * Check the current card state. If it is in some data transfer
1678 * mode, tell it to stop (and hopefully transition back to TRAN.)
1679 */
1680 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1681 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001682 err = send_stop(card,
1683 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1684 req, gen_err, &stop_status);
1685 if (err) {
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001686 pr_err("%s: error %d sending stop command\n",
1687 req->rq_disk->disk_name, err);
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001688 /*
1689 * If the stop cmd also timed out, the card is probably
1690 * not present, so abort. Other errors are bad news too.
1691 */
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001692 return ERR_ABORT;
Ulf Hanssonbb5cba42014-01-14 21:31:35 +01001693 }
1694
Adrian Hunter67716322011-08-29 16:42:15 +03001695 if (stop_status & R1_CARD_ECC_FAILED)
1696 *ecc_err = 1;
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001697 }
1698
1699 /* Check for set block count errors */
1700 if (brq->sbc.error)
1701 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1702 prev_cmd_status_valid, status);
1703
1704 /* Check for r/w command errors */
1705 if (brq->cmd.error)
1706 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1707 prev_cmd_status_valid, status);
1708
Adrian Hunter67716322011-08-29 16:42:15 +03001709 /* Data errors */
1710 if (!brq->stop.error)
1711 return ERR_CONTINUE;
1712
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01001713 /* Now for stop errors. These aren't fatal to the transfer. */
Johan Rudholm5e1344e2014-09-17 09:50:42 +02001714 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 +01001715 req->rq_disk->disk_name, brq->stop.error,
1716 brq->cmd.resp[0], status);
1717
1718 /*
1719 * Subsitute in our own stop status as this will give the error
1720 * state which happened during the execution of the r/w command.
1721 */
1722 if (stop_status) {
1723 brq->stop.resp[0] = stop_status;
1724 brq->stop.error = 0;
1725 }
1726 return ERR_CONTINUE;
1727}
1728
Adrian Hunter67716322011-08-29 16:42:15 +03001729static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1730 int type)
1731{
1732 int err;
1733
1734 if (md->reset_done & type)
1735 return -EEXIST;
1736
1737 md->reset_done |= type;
1738 err = mmc_hw_reset(host);
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301739 if (err && err != -EOPNOTSUPP) {
1740 /* We failed to reset so we need to abort the request */
1741 pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host),
1742 __func__, err);
1743 return -ENODEV;
1744 }
1745
Adrian Hunter67716322011-08-29 16:42:15 +03001746 /* Ensure we switch back to the correct partition */
Sahitya Tummala943f51b2014-05-30 09:22:35 +05301747 if (host->card) {
Ulf Hanssonfc95e302014-10-06 14:34:09 +02001748 struct mmc_blk_data *main_md =
1749 dev_get_drvdata(&host->card->dev);
Adrian Hunter67716322011-08-29 16:42:15 +03001750 int part_err;
1751
1752 main_md->part_curr = main_md->part_type;
1753 part_err = mmc_blk_part_switch(host->card, md);
1754 if (part_err) {
1755 /*
1756 * We have failed to get back into the correct
1757 * partition, so we need to abort the whole request.
1758 */
1759 return -ENODEV;
1760 }
1761 }
1762 return err;
1763}
1764
1765static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1766{
1767 md->reset_done &= ~type;
1768}
1769
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +08001770int mmc_access_rpmb(struct mmc_queue *mq)
1771{
1772 struct mmc_blk_data *md = mq->data;
1773 /*
1774 * If this is a RPMB partition access, return ture
1775 */
1776 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1777 return true;
1778
1779 return false;
1780}
1781
Adrian Hunterbd788c92010-08-11 14:17:47 -07001782static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1783{
1784 struct mmc_blk_data *md = mq->data;
1785 struct mmc_card *card = md->queue.card;
1786 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001787 int err = 0, type = MMC_BLK_DISCARD;
Adrian Hunterbd788c92010-08-11 14:17:47 -07001788
Adrian Hunterbd788c92010-08-11 14:17:47 -07001789 if (!mmc_can_erase(card)) {
1790 err = -EOPNOTSUPP;
1791 goto out;
1792 }
1793
1794 from = blk_rq_pos(req);
1795 nr = blk_rq_sectors(req);
1796
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09001797 if (mmc_can_discard(card))
1798 arg = MMC_DISCARD_ARG;
1799 else if (mmc_can_trim(card))
Adrian Hunterbd788c92010-08-11 14:17:47 -07001800 arg = MMC_TRIM_ARG;
1801 else
1802 arg = MMC_ERASE_ARG;
Adrian Hunter67716322011-08-29 16:42:15 +03001803retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001804 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1805 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1806 INAND_CMD38_ARG_EXT_CSD,
1807 arg == MMC_TRIM_ARG ?
1808 INAND_CMD38_ARG_TRIM :
1809 INAND_CMD38_ARG_ERASE,
1810 0);
1811 if (err)
1812 goto out;
1813 }
Adrian Hunterbd788c92010-08-11 14:17:47 -07001814 err = mmc_erase(card, from, nr, arg);
1815out:
Adrian Hunter67716322011-08-29 16:42:15 +03001816 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1817 goto retry;
1818 if (!err)
1819 mmc_blk_reset_success(md, type);
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301820 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunterbd788c92010-08-11 14:17:47 -07001821
Adrian Hunterbd788c92010-08-11 14:17:47 -07001822 return err ? 0 : 1;
1823}
1824
Adrian Hunter49804542010-08-11 14:17:50 -07001825static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1826 struct request *req)
1827{
1828 struct mmc_blk_data *md = mq->data;
1829 struct mmc_card *card = md->queue.card;
Maya Erez775a9362013-04-18 15:41:55 +03001830 unsigned int from, nr, arg;
Adrian Hunter67716322011-08-29 16:42:15 +03001831 int err = 0, type = MMC_BLK_SECDISCARD;
Adrian Hunter49804542010-08-11 14:17:50 -07001832
Maya Erez775a9362013-04-18 15:41:55 +03001833 if (!(mmc_can_secure_erase_trim(card))) {
Adrian Hunter49804542010-08-11 14:17:50 -07001834 err = -EOPNOTSUPP;
1835 goto out;
1836 }
1837
1838 from = blk_rq_pos(req);
1839 nr = blk_rq_sectors(req);
1840
Maya Erez775a9362013-04-18 15:41:55 +03001841 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1842 arg = MMC_SECURE_TRIM1_ARG;
1843 else
1844 arg = MMC_SECURE_ERASE_ARG;
Adrian Hunter28302812012-04-05 14:45:48 +03001845
Adrian Hunter67716322011-08-29 16:42:15 +03001846retry:
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001847 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1848 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1849 INAND_CMD38_ARG_EXT_CSD,
1850 arg == MMC_SECURE_TRIM1_ARG ?
1851 INAND_CMD38_ARG_SECTRIM1 :
1852 INAND_CMD38_ARG_SECERASE,
1853 0);
1854 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001855 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001856 }
Adrian Hunter28302812012-04-05 14:45:48 +03001857
Adrian Hunter49804542010-08-11 14:17:50 -07001858 err = mmc_erase(card, from, nr, arg);
Adrian Hunter28302812012-04-05 14:45:48 +03001859 if (err == -EIO)
1860 goto out_retry;
1861 if (err)
1862 goto out;
1863
1864 if (arg == MMC_SECURE_TRIM1_ARG) {
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001865 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1866 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1867 INAND_CMD38_ARG_EXT_CSD,
1868 INAND_CMD38_ARG_SECTRIM2,
1869 0);
1870 if (err)
Adrian Hunter28302812012-04-05 14:45:48 +03001871 goto out_retry;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001872 }
Adrian Hunter28302812012-04-05 14:45:48 +03001873
Adrian Hunter49804542010-08-11 14:17:50 -07001874 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
Adrian Hunter28302812012-04-05 14:45:48 +03001875 if (err == -EIO)
1876 goto out_retry;
1877 if (err)
1878 goto out;
Andrei Warkentin6a7a6b42011-04-12 15:06:53 -05001879 }
Adrian Hunter28302812012-04-05 14:45:48 +03001880
Adrian Hunter28302812012-04-05 14:45:48 +03001881out_retry:
1882 if (err && !mmc_blk_reset(md, card->host, type))
Adrian Hunter67716322011-08-29 16:42:15 +03001883 goto retry;
1884 if (!err)
1885 mmc_blk_reset_success(md, type);
Adrian Hunter28302812012-04-05 14:45:48 +03001886out:
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301887 blk_end_request(req, err, blk_rq_bytes(req));
Adrian Hunter49804542010-08-11 14:17:50 -07001888
Adrian Hunter49804542010-08-11 14:17:50 -07001889 return err ? 0 : 1;
1890}
1891
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001892static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1893{
1894 struct mmc_blk_data *md = mq->data;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001895 struct mmc_card *card = md->queue.card;
1896 int ret = 0;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001897
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001898 ret = mmc_flush_cache(card);
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001899 if (ret == -ENODEV) {
1900 pr_err("%s: %s: restart mmc card",
1901 req->rq_disk->disk_name, __func__);
1902 if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH))
1903 pr_err("%s: %s: fail to restart mmc",
1904 req->rq_disk->disk_name, __func__);
1905 else
1906 mmc_blk_reset_success(md, MMC_BLK_FLUSH);
1907 }
1908
1909 if (ret) {
1910 pr_err("%s: %s: notify flush error to upper layers",
1911 req->rq_disk->disk_name, __func__);
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001912 ret = -EIO;
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02001913 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001914
Mark Salyzyn6904e432016-01-28 11:12:25 -08001915#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
1916 else if (atomic_read(&mq->cache_size)) {
1917 long used = mmc_blk_cache_used(mq, jiffies);
1918
1919 if (used) {
1920 int speed = atomic_read(&mq->max_write_speed);
1921
1922 if (speed_valid(speed)) {
1923 unsigned long msecs = jiffies_to_msecs(
1924 size_and_speed_to_jiffies(
1925 used, speed));
1926 if (msecs)
1927 msleep(msecs);
1928 }
1929 }
1930 }
1931#endif
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05301932 blk_end_request_all(req, ret);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001933
Seungwon Jeon881d1c22011-10-14 14:03:21 +09001934 return ret ? 0 : 1;
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001935}
1936
1937/*
1938 * Reformat current write as a reliable write, supporting
1939 * both legacy and the enhanced reliable write MMC cards.
1940 * In each transfer we'll handle only as much as a single
1941 * reliable write can handle, thus finish the request in
1942 * partial completions.
1943 */
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05001944static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1945 struct mmc_card *card,
1946 struct request *req)
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001947{
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001948 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1949 /* Legacy mode imposes restrictions on transfers. */
1950 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1951 brq->data.blocks = 1;
1952
1953 if (brq->data.blocks > card->ext_csd.rel_sectors)
1954 brq->data.blocks = card->ext_csd.rel_sectors;
1955 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1956 brq->data.blocks = 1;
1957 }
Andrei Warkentinf4c55222011-03-31 18:40:00 -05001958}
1959
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01001960#define CMD_ERRORS \
1961 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1962 R1_ADDRESS_ERROR | /* Misaligned address */ \
1963 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1964 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1965 R1_CC_ERROR | /* Card controller error */ \
1966 R1_ERROR) /* General/unknown error */
1967
Per Forlinee8a43a2011-07-01 18:55:33 +02001968static int mmc_blk_err_check(struct mmc_card *card,
1969 struct mmc_async_req *areq)
Per Forlind78d4a82011-07-01 18:55:30 +02001970{
Per Forlinee8a43a2011-07-01 18:55:33 +02001971 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1972 mmc_active);
1973 struct mmc_blk_request *brq = &mq_mrq->brq;
1974 struct request *req = mq_mrq->req;
Adrian Hunterb8360a42015-05-07 13:10:24 +03001975 int need_retune = card->host->need_retune;
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001976 int ecc_err = 0, gen_err = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02001977
1978 /*
1979 * sbc.error indicates a problem with the set block count
1980 * command. No data will have been transferred.
1981 *
1982 * cmd.error indicates a problem with the r/w command. No
1983 * data will have been transferred.
1984 *
1985 * stop.error indicates a problem with the stop command. Data
1986 * may have been transferred, or may still be transferring.
1987 */
Adrian Hunter67716322011-08-29 16:42:15 +03001988 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1989 brq->data.error) {
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09001990 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
Per Forlind78d4a82011-07-01 18:55:30 +02001991 case ERR_RETRY:
1992 return MMC_BLK_RETRY;
1993 case ERR_ABORT:
1994 return MMC_BLK_ABORT;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05301995 case ERR_NOMEDIUM:
1996 return MMC_BLK_NOMEDIUM;
Per Forlind78d4a82011-07-01 18:55:30 +02001997 case ERR_CONTINUE:
1998 break;
1999 }
2000 }
2001
2002 /*
2003 * Check for errors relating to the execution of the
2004 * initial command - such as address errors. No data
2005 * has been transferred.
2006 */
2007 if (brq->cmd.resp[0] & CMD_ERRORS) {
2008 pr_err("%s: r/w command failed, status = %#x\n",
2009 req->rq_disk->disk_name, brq->cmd.resp[0]);
2010 return MMC_BLK_ABORT;
2011 }
2012
2013 /*
2014 * Everything else is either success, or a data error of some
2015 * kind. If it was a write, we may have transitioned to
2016 * program mode, which we have to wait for it to complete.
2017 */
2018 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002019 int err;
Trey Ramsay8fee4762012-11-16 09:31:41 -06002020
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002021 /* Check stop command response */
2022 if (brq->stop.resp[0] & R1_ERROR) {
2023 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
2024 req->rq_disk->disk_name, __func__,
2025 brq->stop.resp[0]);
2026 gen_err = 1;
2027 }
2028
Ulf Hansson95a91292014-01-29 13:11:27 +01002029 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
2030 &gen_err);
Ulf Hanssonc49433f2014-01-29 11:01:55 +01002031 if (err)
2032 return MMC_BLK_CMD_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002033 }
2034
KOBAYASHI Yoshitakec8760062013-07-07 07:35:45 +09002035 /* if general error occurs, retry the write operation. */
2036 if (gen_err) {
2037 pr_warn("%s: retrying write for general error\n",
2038 req->rq_disk->disk_name);
2039 return MMC_BLK_RETRY;
2040 }
2041
Per Forlind78d4a82011-07-01 18:55:30 +02002042 if (brq->data.error) {
Adrian Hunterb8360a42015-05-07 13:10:24 +03002043 if (need_retune && !brq->retune_retry_done) {
Russell King09faf612016-01-29 09:44:00 +00002044 pr_debug("%s: retrying because a re-tune was needed\n",
2045 req->rq_disk->disk_name);
Adrian Hunterb8360a42015-05-07 13:10:24 +03002046 brq->retune_retry_done = 1;
2047 return MMC_BLK_RETRY;
2048 }
Per Forlind78d4a82011-07-01 18:55:30 +02002049 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
2050 req->rq_disk->disk_name, brq->data.error,
2051 (unsigned)blk_rq_pos(req),
2052 (unsigned)blk_rq_sectors(req),
2053 brq->cmd.resp[0], brq->stop.resp[0]);
2054
2055 if (rq_data_dir(req) == READ) {
Adrian Hunter67716322011-08-29 16:42:15 +03002056 if (ecc_err)
2057 return MMC_BLK_ECC_ERR;
Per Forlind78d4a82011-07-01 18:55:30 +02002058 return MMC_BLK_DATA_ERR;
2059 } else {
2060 return MMC_BLK_CMD_ERR;
2061 }
2062 }
2063
Adrian Hunter67716322011-08-29 16:42:15 +03002064 if (!brq->data.bytes_xfered)
2065 return MMC_BLK_RETRY;
Per Forlind78d4a82011-07-01 18:55:30 +02002066
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002067 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
2068 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
2069 return MMC_BLK_PARTIAL;
2070 else
2071 return MMC_BLK_SUCCESS;
2072 }
2073
Adrian Hunter67716322011-08-29 16:42:15 +03002074 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
2075 return MMC_BLK_PARTIAL;
2076
2077 return MMC_BLK_SUCCESS;
Per Forlind78d4a82011-07-01 18:55:30 +02002078}
2079
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002080static int mmc_blk_packed_err_check(struct mmc_card *card,
2081 struct mmc_async_req *areq)
2082{
2083 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
2084 mmc_active);
2085 struct request *req = mq_rq->req;
2086 struct mmc_packed *packed = mq_rq->packed;
2087 int err, check, status;
2088 u8 *ext_csd;
2089
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002090 packed->retries--;
2091 check = mmc_blk_err_check(card, areq);
2092 err = get_card_status(card, &status, 0);
2093 if (err) {
2094 pr_err("%s: error %d sending status command\n",
2095 req->rq_disk->disk_name, err);
2096 return MMC_BLK_ABORT;
2097 }
2098
2099 if (status & R1_EXCEPTION_EVENT) {
Ulf Hansson86817ff2014-10-17 11:39:05 +02002100 err = mmc_get_ext_csd(card, &ext_csd);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002101 if (err) {
2102 pr_err("%s: error %d sending ext_csd\n",
2103 req->rq_disk->disk_name, err);
Ulf Hansson86817ff2014-10-17 11:39:05 +02002104 return MMC_BLK_ABORT;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002105 }
2106
2107 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
2108 EXT_CSD_PACKED_FAILURE) &&
2109 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2110 EXT_CSD_PACKED_GENERIC_ERROR)) {
2111 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
2112 EXT_CSD_PACKED_INDEXED_ERROR) {
2113 packed->idx_failure =
2114 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
2115 check = MMC_BLK_PARTIAL;
2116 }
2117 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
2118 "failure index: %d\n",
2119 req->rq_disk->disk_name, packed->nr_entries,
2120 packed->blocks, packed->idx_failure);
2121 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002122 kfree(ext_csd);
2123 }
2124
2125 return check;
2126}
2127
Per Forlin54d49d72011-07-01 18:55:29 +02002128static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
2129 struct mmc_card *card,
2130 int disable_multi,
2131 struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Per Forlin54d49d72011-07-01 18:55:29 +02002133 u32 readcmd, writecmd;
2134 struct mmc_blk_request *brq = &mqrq->brq;
2135 struct request *req = mqrq->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct mmc_blk_data *md = mq->data;
Saugata Das42659002011-12-21 13:09:17 +05302137 bool do_data_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002139 /*
2140 * Reliable writes are used to implement Forced Unit Access and
Luca Porziod3df0462015-11-06 15:12:26 +00002141 * are supported only on MMCs.
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002142 */
Luca Porziod3df0462015-11-06 15:12:26 +00002143 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002144 (rq_data_dir(req) == WRITE) &&
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002145 (md->flags & MMC_BLK_REL_WR);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002146
Per Forlin54d49d72011-07-01 18:55:29 +02002147 memset(brq, 0, sizeof(struct mmc_blk_request));
2148 brq->mrq.cmd = &brq->cmd;
2149 brq->mrq.data = &brq->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Per Forlin54d49d72011-07-01 18:55:29 +02002151 brq->cmd.arg = blk_rq_pos(req);
2152 if (!mmc_card_blockaddr(card))
2153 brq->cmd.arg <<= 9;
2154 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2155 brq->data.blksz = 512;
2156 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2157 brq->stop.arg = 0;
Per Forlin54d49d72011-07-01 18:55:29 +02002158 brq->data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Asutosh Dasf0665412012-07-27 18:10:19 +05302160 brq->data.fault_injected = false;
Per Forlin54d49d72011-07-01 18:55:29 +02002161 /*
2162 * The block layer doesn't support all sector count
2163 * restrictions, so we need to be prepared for too big
2164 * requests.
2165 */
2166 if (brq->data.blocks > card->host->max_blk_count)
2167 brq->data.blocks = card->host->max_blk_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002169 if (brq->data.blocks > 1) {
2170 /*
2171 * After a read error, we redo the request one sector
2172 * at a time in order to accurately determine which
2173 * sectors can be read successfully.
2174 */
2175 if (disable_multi)
2176 brq->data.blocks = 1;
2177
Kuninori Morimoto2e47e842014-09-02 19:08:53 -07002178 /*
2179 * Some controllers have HW issues while operating
2180 * in multiple I/O mode
2181 */
2182 if (card->host->ops->multi_io_quirk)
2183 brq->data.blocks = card->host->ops->multi_io_quirk(card,
2184 (rq_data_dir(req) == READ) ?
2185 MMC_DATA_READ : MMC_DATA_WRITE,
2186 brq->data.blocks);
Paul Walmsley2bf22b32011-10-06 14:50:33 -06002187 }
Per Forlin54d49d72011-07-01 18:55:29 +02002188
2189 if (brq->data.blocks > 1 || do_rel_wr) {
2190 /* SPI multiblock writes terminate using a special
2191 * token, not a STOP_TRANSMISSION request.
Pierre Ossman548d2de2009-04-10 17:52:57 +02002192 */
Per Forlin54d49d72011-07-01 18:55:29 +02002193 if (!mmc_host_is_spi(card->host) ||
2194 rq_data_dir(req) == READ)
2195 brq->mrq.stop = &brq->stop;
2196 readcmd = MMC_READ_MULTIPLE_BLOCK;
2197 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
2198 } else {
2199 brq->mrq.stop = NULL;
2200 readcmd = MMC_READ_SINGLE_BLOCK;
2201 writecmd = MMC_WRITE_BLOCK;
2202 }
2203 if (rq_data_dir(req) == READ) {
2204 brq->cmd.opcode = readcmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002205 brq->data.flags = MMC_DATA_READ;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002206 if (brq->mrq.stop)
2207 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
2208 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002209 } else {
2210 brq->cmd.opcode = writecmd;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002211 brq->data.flags = MMC_DATA_WRITE;
Ulf Hanssonbcc3e172014-01-14 21:24:21 +01002212 if (brq->mrq.stop)
2213 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
2214 MMC_CMD_AC;
Per Forlin54d49d72011-07-01 18:55:29 +02002215 }
Pierre Ossman548d2de2009-04-10 17:52:57 +02002216
Per Forlin54d49d72011-07-01 18:55:29 +02002217 if (do_rel_wr)
2218 mmc_apply_rel_rw(brq, card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +01002219
Per Forlin54d49d72011-07-01 18:55:29 +02002220 /*
Saugata Das42659002011-12-21 13:09:17 +05302221 * Data tag is used only during writing meta data to speed
2222 * up write and any subsequent read of this meta data
2223 */
2224 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2225 (req->cmd_flags & REQ_META) &&
2226 (rq_data_dir(req) == WRITE) &&
2227 ((brq->data.blocks * brq->data.blksz) >=
2228 card->ext_csd.data_tag_unit_size);
2229
2230 /*
Per Forlin54d49d72011-07-01 18:55:29 +02002231 * Pre-defined multi-block transfers are preferable to
2232 * open ended-ones (and necessary for reliable writes).
2233 * However, it is not sufficient to just send CMD23,
2234 * and avoid the final CMD12, as on an error condition
2235 * CMD12 (stop) needs to be sent anyway. This, coupled
2236 * with Auto-CMD23 enhancements provided by some
2237 * hosts, means that the complexity of dealing
2238 * with this is best left to the host. If CMD23 is
2239 * supported by card and host, we'll fill sbc in and let
2240 * the host deal with handling it correctly. This means
2241 * that for hosts that don't expose MMC_CAP_CMD23, no
2242 * change of behavior will be observed.
2243 *
2244 * N.B: Some MMC cards experience perf degradation.
2245 * We'll avoid using CMD23-bounded multiblock writes for
2246 * these, while retaining features like reliable writes.
2247 */
Saugata Das42659002011-12-21 13:09:17 +05302248 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
2249 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
2250 do_data_tag)) {
Per Forlin54d49d72011-07-01 18:55:29 +02002251 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2252 brq->sbc.arg = brq->data.blocks |
Saugata Das42659002011-12-21 13:09:17 +05302253 (do_rel_wr ? (1 << 31) : 0) |
2254 (do_data_tag ? (1 << 29) : 0);
Per Forlin54d49d72011-07-01 18:55:29 +02002255 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2256 brq->mrq.sbc = &brq->sbc;
2257 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002258
Per Forlin54d49d72011-07-01 18:55:29 +02002259 mmc_set_data_timeout(&brq->data, card);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05002260
Per Forlin54d49d72011-07-01 18:55:29 +02002261 brq->data.sg = mqrq->sg;
2262 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
Andrei Warkentinf4c55222011-03-31 18:40:00 -05002263
Per Forlin54d49d72011-07-01 18:55:29 +02002264 /*
2265 * Adjust the sg list so it is the same size as the
2266 * request.
2267 */
2268 if (brq->data.blocks != blk_rq_sectors(req)) {
2269 int i, data_size = brq->data.blocks << 9;
2270 struct scatterlist *sg;
Pierre Ossmanb146d262007-07-24 19:16:54 +02002271
Per Forlin54d49d72011-07-01 18:55:29 +02002272 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
2273 data_size -= sg->length;
2274 if (data_size <= 0) {
2275 sg->length += data_size;
2276 i++;
2277 break;
Adrian Hunter6a79e392008-12-31 18:21:17 +01002278 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002279 }
Per Forlin54d49d72011-07-01 18:55:29 +02002280 brq->data.sg_len = i;
2281 }
Adrian Hunter6a79e392008-12-31 18:21:17 +01002282
Per Forlinee8a43a2011-07-01 18:55:33 +02002283 mqrq->mmc_active.mrq = &brq->mrq;
Sahitya Tummalac44de842015-05-08 11:12:30 +05302284 mqrq->mmc_active.mrq->req = mqrq->req;
Per Forlinee8a43a2011-07-01 18:55:33 +02002285 mqrq->mmc_active.err_check = mmc_blk_err_check;
2286
Per Forlin54d49d72011-07-01 18:55:29 +02002287 mmc_queue_bounce_pre(mqrq);
2288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002290static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
2291 struct mmc_card *card)
2292{
2293 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
2294 unsigned int max_seg_sz = queue_max_segment_size(q);
2295 unsigned int len, nr_segs = 0;
2296
2297 do {
2298 len = min(hdr_sz, max_seg_sz);
2299 hdr_sz -= len;
2300 nr_segs++;
2301 } while (hdr_sz);
2302
2303 return nr_segs;
2304}
2305
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002306/**
2307 * mmc_blk_disable_wr_packing() - disables packing mode
2308 * @mq: MMC queue.
2309 *
2310 */
2311void mmc_blk_disable_wr_packing(struct mmc_queue *mq)
2312{
2313 if (mq) {
2314 mq->wr_packing_enabled = false;
2315 mq->num_of_potential_packed_wr_reqs = 0;
2316 }
2317}
2318EXPORT_SYMBOL(mmc_blk_disable_wr_packing);
2319
Lee Susman841fd132013-04-23 17:59:26 +03002320static int get_packed_trigger(int potential, struct mmc_card *card,
2321 struct request *req, int curr_trigger)
2322{
2323 static int num_mean_elements = 1;
2324 static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2325 unsigned int trigger = curr_trigger;
2326 unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes;
2327
2328 /* scale down the upper bound to 75% */
2329 pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4;
2330
2331 /*
2332 * since the most common calls for this function are with small
2333 * potential write values and since we don't want these calls to affect
2334 * the packed trigger, set a lower bound and ignore calls with
2335 * potential lower than that bound
2336 */
2337 if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND)
2338 return trigger;
2339
2340 /*
2341 * this is to prevent integer overflow in the following calculation:
2342 * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm
2343 */
2344 if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) {
2345 num_mean_elements = 1;
2346 mean_potential = PCKD_TRGR_INIT_MEAN_POTEN;
2347 }
2348
2349 /*
2350 * get next mean value based on previous mean value and current
2351 * potential packed writes. Calculation is as follows:
2352 * mean_pot[i+1] =
2353 * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1)
2354 */
2355 mean_potential *= num_mean_elements;
2356 /*
2357 * add num_mean_elements so that the division of two integers doesn't
2358 * lower mean_potential too much
2359 */
2360 if (potential > mean_potential)
2361 mean_potential += num_mean_elements;
2362 mean_potential += potential;
2363 /* this is for gaining more precision when dividing two integers */
2364 mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER;
2365 /* this completes the mean calculation */
2366 mean_potential /= ++num_mean_elements;
2367 mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER;
2368
2369 /*
2370 * if current potential packed writes is greater than the mean potential
2371 * then the heuristic is that the following workload will contain many
2372 * write requests, therefore we lower the packed trigger. In the
2373 * opposite case we want to increase the trigger in order to get less
2374 * packing events.
2375 */
2376 if (potential >= mean_potential)
2377 trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ?
2378 PCKD_TRGR_LOWER_BOUND : trigger - 1;
2379 else
2380 trigger = (trigger >= pckd_trgr_upper_bound) ?
2381 pckd_trgr_upper_bound : trigger + 1;
2382
2383 /*
2384 * an urgent read request indicates a packed list being interrupted
2385 * by this read, therefore we aim for less packing, hence the trigger
2386 * gets increased
2387 */
2388 if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ))
2389 trigger += PCKD_TRGR_URGENT_PENALTY;
2390
2391 return trigger;
2392}
2393
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002394static void mmc_blk_write_packing_control(struct mmc_queue *mq,
2395 struct request *req)
2396{
2397 struct mmc_host *host = mq->card->host;
2398 int data_dir;
2399
2400 if (!(host->caps2 & MMC_CAP2_PACKED_WR))
2401 return;
2402
Maya Erez8e2b3c32012-12-02 13:27:15 +02002403 /* Support for the write packing on eMMC 4.5 or later */
2404 if (mq->card->ext_csd.rev <= 5)
2405 return;
2406
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002407 /*
2408 * In case the packing control is not supported by the host, it should
2409 * not have an effect on the write packing. Therefore we have to enable
2410 * the write packing
2411 */
2412 if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) {
2413 mq->wr_packing_enabled = true;
2414 return;
2415 }
2416
2417 if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) {
2418 if (mq->num_of_potential_packed_wr_reqs >
2419 mq->num_wr_reqs_to_start_packing)
2420 mq->wr_packing_enabled = true;
Lee Susman841fd132013-04-23 17:59:26 +03002421 mq->num_wr_reqs_to_start_packing =
2422 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2423 mq->card, req,
2424 mq->num_wr_reqs_to_start_packing);
Tatyana Brokhman843915a2012-10-07 10:26:27 +02002425 mq->num_of_potential_packed_wr_reqs = 0;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002426 return;
2427 }
2428
2429 data_dir = rq_data_dir(req);
2430
2431 if (data_dir == READ) {
Konstantin Dorfman225c9c72013-02-05 15:45:53 +02002432 mmc_blk_disable_wr_packing(mq);
Lee Susman841fd132013-04-23 17:59:26 +03002433 mq->num_wr_reqs_to_start_packing =
2434 get_packed_trigger(mq->num_of_potential_packed_wr_reqs,
2435 mq->card, req,
2436 mq->num_wr_reqs_to_start_packing);
2437 mq->num_of_potential_packed_wr_reqs = 0;
2438 mq->wr_packing_enabled = false;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002439 return;
2440 } else if (data_dir == WRITE) {
2441 mq->num_of_potential_packed_wr_reqs++;
2442 }
2443
2444 if (mq->num_of_potential_packed_wr_reqs >
2445 mq->num_wr_reqs_to_start_packing)
2446 mq->wr_packing_enabled = true;
2447}
2448
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002449struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card)
2450{
2451 if (!card)
2452 return NULL;
2453
2454 return &card->wr_pack_stats;
2455}
2456EXPORT_SYMBOL(mmc_blk_get_packed_statistics);
2457
2458void mmc_blk_init_packed_statistics(struct mmc_card *card)
2459{
2460 int max_num_of_packed_reqs = 0;
2461
2462 if (!card || !card->wr_pack_stats.packing_events)
2463 return;
2464
2465 max_num_of_packed_reqs = card->ext_csd.max_packed_writes;
2466
2467 spin_lock(&card->wr_pack_stats.lock);
2468 memset(card->wr_pack_stats.packing_events, 0,
2469 (max_num_of_packed_reqs + 1) *
2470 sizeof(*card->wr_pack_stats.packing_events));
2471 memset(&card->wr_pack_stats.pack_stop_reason, 0,
2472 sizeof(card->wr_pack_stats.pack_stop_reason));
2473 card->wr_pack_stats.enabled = true;
2474 spin_unlock(&card->wr_pack_stats.lock);
2475}
2476EXPORT_SYMBOL(mmc_blk_init_packed_statistics);
2477
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002478static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
2479{
2480 struct request_queue *q = mq->queue;
2481 struct mmc_card *card = mq->card;
2482 struct request *cur = req, *next = NULL;
2483 struct mmc_blk_data *md = mq->data;
2484 struct mmc_queue_req *mqrq = mq->mqrq_cur;
2485 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
2486 unsigned int req_sectors = 0, phys_segments = 0;
2487 unsigned int max_blk_count, max_phys_segs;
2488 bool put_back = true;
2489 u8 max_packed_rw = 0;
2490 u8 reqs = 0;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002491 struct mmc_wr_pack_stats *stats = &card->wr_pack_stats;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002492
Shawn Lin96e52da2016-08-26 08:49:55 +08002493 /*
2494 * We don't need to check packed for any further
2495 * operation of packed stuff as we set MMC_PACKED_NONE
2496 * and return zero for reqs if geting null packed. Also
2497 * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing
2498 * it again when removing blk req.
2499 */
2500 if (!mqrq->packed) {
2501 md->flags &= (~MMC_BLK_PACKED_CMD);
2502 goto no_packed;
2503 }
2504
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002505 if (!(md->flags & MMC_BLK_PACKED_CMD))
2506 goto no_packed;
2507
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002508 if (!mq->wr_packing_enabled)
2509 goto no_packed;
2510
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002511 if ((rq_data_dir(cur) == WRITE) &&
2512 mmc_host_packed_wr(card->host))
2513 max_packed_rw = card->ext_csd.max_packed_writes;
2514
2515 if (max_packed_rw == 0)
2516 goto no_packed;
2517
2518 if (mmc_req_rel_wr(cur) &&
2519 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
2520 goto no_packed;
2521
2522 if (mmc_large_sector(card) &&
2523 !IS_ALIGNED(blk_rq_sectors(cur), 8))
2524 goto no_packed;
2525
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002526 if (cur->cmd_flags & REQ_FUA)
2527 goto no_packed;
2528
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002529 mmc_blk_clear_packed(mqrq);
2530
2531 max_blk_count = min(card->host->max_blk_count,
2532 card->host->max_req_size >> 9);
2533 if (unlikely(max_blk_count > 0xffff))
2534 max_blk_count = 0xffff;
2535
2536 max_phys_segs = queue_max_segments(q);
2537 req_sectors += blk_rq_sectors(cur);
2538 phys_segments += cur->nr_phys_segments;
2539
2540 if (rq_data_dir(cur) == WRITE) {
2541 req_sectors += mmc_large_sector(card) ? 8 : 1;
2542 phys_segments += mmc_calc_packed_hdr_segs(q, card);
2543 }
2544
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002545 spin_lock(&stats->lock);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002546 do {
2547 if (reqs >= max_packed_rw - 1) {
2548 put_back = false;
2549 break;
2550 }
2551
2552 spin_lock_irq(q->queue_lock);
2553 next = blk_fetch_request(q);
2554 spin_unlock_irq(q->queue_lock);
2555 if (!next) {
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002556 MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002557 put_back = false;
2558 break;
2559 }
2560
2561 if (mmc_large_sector(card) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002562 !IS_ALIGNED(blk_rq_sectors(next), 8)) {
2563 MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002564 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002565 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002566
Mike Christie3a5e02c2016-06-05 14:32:23 -05002567 if (req_op(next) == REQ_OP_DISCARD ||
Adrian Hunter7afafc82016-08-16 10:59:35 +03002568 req_op(next) == REQ_OP_SECURE_ERASE ||
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002569 req_op(next) == REQ_OP_FLUSH) {
2570 if (req_op(next) != REQ_OP_SECURE_ERASE)
2571 MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002572 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002573 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002574
Konstantin Dorfman31a482d2013-02-05 16:26:19 +02002575 if (next->cmd_flags & REQ_FUA) {
2576 MMC_BLK_UPDATE_STOP_REASON(stats, FUA);
2577 break;
2578 }
2579
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002580 if (rq_data_dir(cur) != rq_data_dir(next)) {
2581 MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002582 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002583 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002584
2585 if (mmc_req_rel_wr(next) &&
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002586 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) {
2587 MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002588 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002589 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002590
2591 req_sectors += blk_rq_sectors(next);
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002592 if (req_sectors > max_blk_count) {
2593 if (stats->enabled)
2594 stats->pack_stop_reason[EXCEEDS_SECTORS]++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002595 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002596 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002597
2598 phys_segments += next->nr_phys_segments;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002599 if (phys_segments > max_phys_segs) {
2600 MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002601 break;
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002602 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002603
Maya Erez5a8dae12014-12-04 15:13:59 +02002604 if (mq->no_pack_for_random) {
2605 if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) !=
2606 blk_rq_pos(next)) {
2607 MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM);
2608 put_back = 1;
2609 break;
2610 }
2611 }
2612
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02002613 if (rq_data_dir(next) == WRITE)
2614 mq->num_of_potential_packed_wr_reqs++;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002615 list_add_tail(&next->queuelist, &mqrq->packed->list);
2616 cur = next;
2617 reqs++;
2618 } while (1);
2619
2620 if (put_back) {
2621 spin_lock_irq(q->queue_lock);
2622 blk_requeue_request(q, next);
2623 spin_unlock_irq(q->queue_lock);
2624 }
2625
Tatyana Brokhman08238ce2012-10-07 10:33:13 +02002626 if (stats->enabled) {
2627 if (reqs + 1 <= card->ext_csd.max_packed_writes)
2628 stats->packing_events[reqs + 1]++;
2629 if (reqs + 1 == max_packed_rw)
2630 MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD);
2631 }
2632
2633 spin_unlock(&stats->lock);
2634
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002635 if (reqs > 0) {
2636 list_add(&req->queuelist, &mqrq->packed->list);
2637 mqrq->packed->nr_entries = ++reqs;
2638 mqrq->packed->retries = reqs;
2639 return reqs;
2640 }
2641
2642no_packed:
2643 mqrq->cmd_type = MMC_PACKED_NONE;
2644 return 0;
2645}
2646
2647static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
2648 struct mmc_card *card,
2649 struct mmc_queue *mq)
2650{
2651 struct mmc_blk_request *brq = &mqrq->brq;
2652 struct request *req = mqrq->req;
2653 struct request *prq;
2654 struct mmc_blk_data *md = mq->data;
2655 struct mmc_packed *packed = mqrq->packed;
2656 bool do_rel_wr, do_data_tag;
Jiri Slaby3f2d2662016-10-03 10:58:28 +02002657 __le32 *packed_cmd_hdr;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002658 u8 hdr_blocks;
2659 u8 i = 1;
2660
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002661 mqrq->cmd_type = MMC_PACKED_WRITE;
2662 packed->blocks = 0;
2663 packed->idx_failure = MMC_PACKED_NR_IDX;
2664
2665 packed_cmd_hdr = packed->cmd_hdr;
2666 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002667 packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) |
2668 (PACKED_CMD_WR << 8) | PACKED_CMD_VER);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002669 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
2670
2671 /*
2672 * Argument for each entry of packed group
2673 */
2674 list_for_each_entry(prq, &packed->list, queuelist) {
2675 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
2676 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2677 (prq->cmd_flags & REQ_META) &&
2678 (rq_data_dir(prq) == WRITE) &&
Adrian Hunterd806b462016-06-10 16:22:16 +03002679 blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002680 /* Argument of CMD23 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002681 packed_cmd_hdr[(i * 2)] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002682 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
2683 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002684 blk_rq_sectors(prq));
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002685 /* Argument of CMD18 or CMD25 */
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002686 packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32(
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002687 mmc_card_blockaddr(card) ?
Taras Kondratiukf68381a2016-07-13 22:05:38 +00002688 blk_rq_pos(prq) : blk_rq_pos(prq) << 9);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002689 packed->blocks += blk_rq_sectors(prq);
2690 i++;
2691 }
2692
2693 memset(brq, 0, sizeof(struct mmc_blk_request));
2694 brq->mrq.cmd = &brq->cmd;
2695 brq->mrq.data = &brq->data;
2696 brq->mrq.sbc = &brq->sbc;
2697 brq->mrq.stop = &brq->stop;
2698
2699 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
2700 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
2701 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
2702
2703 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
2704 brq->cmd.arg = blk_rq_pos(req);
2705 if (!mmc_card_blockaddr(card))
2706 brq->cmd.arg <<= 9;
2707 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
2708
2709 brq->data.blksz = 512;
2710 brq->data.blocks = packed->blocks + hdr_blocks;
Jaehoon Chungf53f1102016-02-01 21:07:36 +09002711 brq->data.flags = MMC_DATA_WRITE;
Asutosh Dasf0665412012-07-27 18:10:19 +05302712 brq->data.fault_injected = false;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002713
2714 brq->stop.opcode = MMC_STOP_TRANSMISSION;
2715 brq->stop.arg = 0;
2716 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2717
2718 mmc_set_data_timeout(&brq->data, card);
2719
2720 brq->data.sg = mqrq->sg;
2721 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2722
2723 mqrq->mmc_active.mrq = &brq->mrq;
Tatyana Brokhman71aefb82012-10-09 13:50:56 +02002724
2725 /*
2726 * This is intended for packed commands tests usage - in case these
2727 * functions are not in use the respective pointers are NULL
2728 */
2729 if (mq->err_check_fn)
2730 mqrq->mmc_active.err_check = mq->err_check_fn;
2731 else
2732 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
2733
2734 if (mq->packed_test_fn)
2735 mq->packed_test_fn(mq->queue, mqrq);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002736
2737 mmc_queue_bounce_pre(mqrq);
2738}
2739
Adrian Hunter67716322011-08-29 16:42:15 +03002740static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
2741 struct mmc_blk_request *brq, struct request *req,
2742 int ret)
2743{
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002744 struct mmc_queue_req *mq_rq;
2745 mq_rq = container_of(brq, struct mmc_queue_req, brq);
2746
Adrian Hunter67716322011-08-29 16:42:15 +03002747 /*
2748 * If this is an SD card and we're writing, we can first
2749 * mark the known good sectors as ok.
2750 *
2751 * If the card is not SD, we can still ok written sectors
2752 * as reported by the controller (which might be less than
2753 * the real number of written sectors, but never more).
2754 */
2755 if (mmc_card_sd(card)) {
2756 u32 blocks;
Asutosh Dasf0665412012-07-27 18:10:19 +05302757 if (!brq->data.fault_injected) {
2758 blocks = mmc_sd_num_wr_blocks(card);
2759 if (blocks != (u32)-1)
2760 ret = blk_end_request(req, 0, blocks << 9);
2761 } else
2762 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002763 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002764 if (!mmc_packed_cmd(mq_rq->cmd_type))
2765 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
Adrian Hunter67716322011-08-29 16:42:15 +03002766 }
2767 return ret;
2768}
2769
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002770static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
2771{
2772 struct request *prq;
2773 struct mmc_packed *packed = mq_rq->packed;
2774 int idx = packed->idx_failure, i = 0;
2775 int ret = 0;
2776
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002777 while (!list_empty(&packed->list)) {
2778 prq = list_entry_rq(packed->list.next);
2779 if (idx == i) {
2780 /* retry from error index */
2781 packed->nr_entries -= idx;
2782 mq_rq->req = prq;
2783 ret = 1;
2784
2785 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
2786 list_del_init(&prq->queuelist);
2787 mmc_blk_clear_packed(mq_rq);
2788 }
2789 return ret;
2790 }
2791 list_del_init(&prq->queuelist);
2792 blk_end_request(prq, 0, blk_rq_bytes(prq));
2793 i++;
2794 }
2795
2796 mmc_blk_clear_packed(mq_rq);
2797 return ret;
2798}
2799
2800static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
2801{
2802 struct request *prq;
2803 struct mmc_packed *packed = mq_rq->packed;
2804
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002805 while (!list_empty(&packed->list)) {
2806 prq = list_entry_rq(packed->list.next);
2807 list_del_init(&prq->queuelist);
2808 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
2809 }
2810
2811 mmc_blk_clear_packed(mq_rq);
2812}
2813
2814static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
2815 struct mmc_queue_req *mq_rq)
2816{
2817 struct request *prq;
2818 struct request_queue *q = mq->queue;
2819 struct mmc_packed *packed = mq_rq->packed;
2820
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09002821 while (!list_empty(&packed->list)) {
2822 prq = list_entry_rq(packed->list.prev);
2823 if (prq->queuelist.prev != &packed->list) {
2824 list_del_init(&prq->queuelist);
2825 spin_lock_irq(q->queue_lock);
2826 blk_requeue_request(mq->queue, prq);
2827 spin_unlock_irq(q->queue_lock);
2828 } else {
2829 list_del_init(&prq->queuelist);
2830 }
2831 }
2832
2833 mmc_blk_clear_packed(mq_rq);
2834}
2835
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07002836static int mmc_blk_cmdq_start_req(struct mmc_host *host,
2837 struct mmc_cmdq_req *cmdq_req)
2838{
2839 struct mmc_request *mrq = &cmdq_req->mrq;
2840
2841 mrq->done = mmc_blk_cmdq_req_done;
2842 return mmc_cmdq_start_req(host, cmdq_req);
2843}
2844
Asutosh Das5238e022015-04-23 16:00:45 +05302845/* prepare for non-data commands */
2846static struct mmc_cmdq_req *mmc_cmdq_prep_dcmd(
2847 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
2848{
2849 struct request *req = mqrq->req;
2850 struct mmc_cmdq_req *cmdq_req = &mqrq->cmdq_req;
2851
2852 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
2853
2854 cmdq_req->mrq.data = NULL;
2855 cmdq_req->cmd_flags = req->cmd_flags;
2856 cmdq_req->mrq.req = mqrq->req;
2857 req->special = mqrq;
2858 cmdq_req->cmdq_req_flags |= DCMD;
2859 cmdq_req->mrq.cmdq_req = cmdq_req;
2860
2861 return &mqrq->cmdq_req;
2862}
2863
2864
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07002865#define IS_RT_CLASS_REQ(x) \
2866 (IOPRIO_PRIO_CLASS(req_get_ioprio(x)) == IOPRIO_CLASS_RT)
2867
2868static struct mmc_cmdq_req *mmc_blk_cmdq_rw_prep(
2869 struct mmc_queue_req *mqrq, struct mmc_queue *mq)
2870{
2871 struct mmc_card *card = mq->card;
2872 struct request *req = mqrq->req;
2873 struct mmc_blk_data *md = mq->data;
2874 bool do_rel_wr = mmc_req_rel_wr(req) && (md->flags & MMC_BLK_REL_WR);
2875 bool do_data_tag;
2876 bool read_dir = (rq_data_dir(req) == READ);
2877 bool prio = IS_RT_CLASS_REQ(req);
2878 struct mmc_cmdq_req *cmdq_rq = &mqrq->cmdq_req;
2879
2880 memset(&mqrq->cmdq_req, 0, sizeof(struct mmc_cmdq_req));
2881
2882 cmdq_rq->tag = req->tag;
2883 if (read_dir) {
2884 cmdq_rq->cmdq_req_flags |= DIR;
2885 cmdq_rq->data.flags = MMC_DATA_READ;
2886 } else {
2887 cmdq_rq->data.flags = MMC_DATA_WRITE;
2888 }
2889 if (prio)
2890 cmdq_rq->cmdq_req_flags |= PRIO;
2891
2892 if (do_rel_wr)
2893 cmdq_rq->cmdq_req_flags |= REL_WR;
2894
2895 cmdq_rq->data.blocks = blk_rq_sectors(req);
2896 cmdq_rq->blk_addr = blk_rq_pos(req);
2897 cmdq_rq->data.blksz = MMC_CARD_CMDQ_BLK_SIZE;
2898
2899 mmc_set_data_timeout(&cmdq_rq->data, card);
2900
2901 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
2902 (req->cmd_flags & REQ_META) &&
2903 (rq_data_dir(req) == WRITE) &&
2904 ((cmdq_rq->data.blocks * cmdq_rq->data.blksz) >=
2905 card->ext_csd.data_tag_unit_size);
2906 if (do_data_tag)
2907 cmdq_rq->cmdq_req_flags |= DAT_TAG;
2908 cmdq_rq->data.sg = mqrq->sg;
2909 cmdq_rq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
2910
2911 /*
2912 * Adjust the sg list so it is the same size as the
2913 * request.
2914 */
2915 if (cmdq_rq->data.blocks > card->host->max_blk_count)
2916 cmdq_rq->data.blocks = card->host->max_blk_count;
2917
2918 if (cmdq_rq->data.blocks != blk_rq_sectors(req)) {
2919 int i, data_size = cmdq_rq->data.blocks << 9;
2920 struct scatterlist *sg;
2921
2922 for_each_sg(cmdq_rq->data.sg, sg, cmdq_rq->data.sg_len, i) {
2923 data_size -= sg->length;
2924 if (data_size <= 0) {
2925 sg->length += data_size;
2926 i++;
2927 break;
2928 }
2929 }
2930 cmdq_rq->data.sg_len = i;
2931 }
2932
2933 mqrq->cmdq_req.cmd_flags = req->cmd_flags;
2934 mqrq->cmdq_req.mrq.req = mqrq->req;
2935 mqrq->cmdq_req.mrq.cmdq_req = &mqrq->cmdq_req;
2936 mqrq->cmdq_req.mrq.data = &mqrq->cmdq_req.data;
2937 mqrq->req->special = mqrq;
2938
2939 pr_debug("%s: %s: mrq: 0x%p req: 0x%p mqrq: 0x%p bytes to xf: %d mmc_cmdq_req: 0x%p card-addr: 0x%08x dir(r-1/w-0): %d\n",
2940 mmc_hostname(card->host), __func__, &mqrq->cmdq_req.mrq,
2941 mqrq->req, mqrq, (cmdq_rq->data.blocks * cmdq_rq->data.blksz),
2942 cmdq_rq, cmdq_rq->blk_addr,
2943 (cmdq_rq->cmdq_req_flags & DIR) ? 1 : 0);
2944
2945 return &mqrq->cmdq_req;
2946}
2947
2948static int mmc_blk_cmdq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
2949{
2950 struct mmc_queue_req *active_mqrq;
2951 struct mmc_card *card = mq->card;
2952 struct mmc_host *host = card->host;
2953 struct mmc_cmdq_req *mc_rq;
2954 int ret = 0;
2955
2956 BUG_ON((req->tag < 0) || (req->tag > card->ext_csd.cmdq_depth));
2957 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
2958
2959 active_mqrq = &mq->mqrq_cmdq[req->tag];
2960 active_mqrq->req = req;
2961
2962 mc_rq = mmc_blk_cmdq_rw_prep(active_mqrq, mq);
2963
2964 ret = mmc_blk_cmdq_start_req(card->host, mc_rq);
2965 return ret;
2966}
2967
Asutosh Das5238e022015-04-23 16:00:45 +05302968/*
2969 * Issues a flush (dcmd) request
2970 */
2971int mmc_blk_cmdq_issue_flush_rq(struct mmc_queue *mq, struct request *req)
2972{
2973 int err;
2974 struct mmc_queue_req *active_mqrq;
2975 struct mmc_card *card = mq->card;
2976 struct mmc_host *host;
2977 struct mmc_cmdq_req *cmdq_req;
2978 struct mmc_cmdq_context_info *ctx_info;
2979
2980 BUG_ON(!card);
2981 host = card->host;
2982 BUG_ON(!host);
2983 BUG_ON(req->tag > card->ext_csd.cmdq_depth);
2984 BUG_ON(test_and_set_bit(req->tag, &host->cmdq_ctx.active_reqs));
2985
2986 ctx_info = &host->cmdq_ctx;
2987
2988 set_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
2989
2990 active_mqrq = &mq->mqrq_cmdq[req->tag];
2991 active_mqrq->req = req;
2992
2993 cmdq_req = mmc_cmdq_prep_dcmd(active_mqrq, mq);
2994 cmdq_req->cmdq_req_flags |= QBR;
2995 cmdq_req->mrq.cmd = &cmdq_req->cmd;
2996 cmdq_req->tag = req->tag;
2997
2998 err = mmc_cmdq_prepare_flush(cmdq_req->mrq.cmd);
2999 if (err) {
3000 pr_err("%s: failed (%d) preparing flush req\n",
3001 mmc_hostname(host), err);
3002 return err;
3003 }
3004 err = mmc_blk_cmdq_start_req(card->host, cmdq_req);
3005 return err;
3006}
3007EXPORT_SYMBOL(mmc_blk_cmdq_issue_flush_rq);
3008
Asutosh Das02e30862015-05-20 16:52:04 +05303009static void mmc_blk_cmdq_reset(struct mmc_host *host, bool clear_all)
3010{
3011 if (!host->cmdq_ops->reset)
3012 return;
3013
3014 if (!test_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state)) {
3015 if (mmc_cmdq_halt(host, true)) {
3016 pr_err("%s: halt failed\n", mmc_hostname(host));
3017 goto reset;
3018 }
3019 }
3020
3021 if (clear_all)
3022 mmc_cmdq_discard_queue(host, 0);
3023reset:
3024 mmc_hw_reset(host);
3025 host->cmdq_ops->reset(host, true);
3026 clear_bit(CMDQ_STATE_HALT, &host->cmdq_ctx.curr_state);
3027}
3028
3029static void mmc_blk_cmdq_err(struct mmc_queue *mq)
3030{
3031 int err;
3032 int retry = 0;
3033 int gen_err;
3034 u32 status;
3035
3036 struct mmc_host *host = mq->card->host;
3037 struct mmc_request *mrq = host->err_mrq;
3038 struct mmc_card *card = mq->card;
3039 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3040
3041 err = mmc_cmdq_halt(host, true);
3042 if (err) {
3043 pr_err("halt: failed: %d\n", err);
3044 goto reset;
3045 }
3046
3047 /* RED error - Fatal: requires reset */
3048 if (mrq->cmdq_req->resp_err) {
3049 pr_crit("%s: Response error detected: Device in bad state\n",
3050 mmc_hostname(host));
3051 blk_end_request_all(mrq->req, -EIO);
3052 goto reset;
3053 }
3054
3055 if (mrq->data->error) {
3056 blk_end_request_all(mrq->req, mrq->data->error);
3057 for (; retry < MAX_RETRIES; retry++) {
3058 err = get_card_status(card, &status, 0);
3059 if (!err)
3060 break;
3061 }
3062
3063 if (err) {
3064 pr_err("%s: No response from card !!!\n",
3065 mmc_hostname(host));
3066 goto reset;
3067 }
3068
3069 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
3070 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
3071 err = send_stop(card, MMC_CMDQ_STOP_TIMEOUT_MS,
3072 mrq->req, &gen_err, &status);
3073 if (err) {
3074 pr_err("%s: error %d sending stop (%d) command\n",
3075 mrq->req->rq_disk->disk_name,
3076 err, status);
3077 goto reset;
3078 }
3079 }
3080
3081 if (mmc_cmdq_discard_queue(host, mrq->req->tag))
3082 goto reset;
3083 else
3084 goto unhalt;
3085 }
3086
3087 /* DCMD commands */
3088 if (mrq->cmd->error)
3089 blk_end_request_all(mrq->req, mrq->cmd->error);
3090
3091reset:
3092 spin_lock_irq(mq->queue->queue_lock);
3093 blk_queue_invalidate_tags(mrq->req->q);
3094 spin_unlock_irq(mq->queue->queue_lock);
3095 mmc_blk_cmdq_reset(host, true);
3096 goto out;
3097
3098unhalt:
3099 mmc_cmdq_halt(host, false);
3100
3101out:
3102 if (test_and_clear_bit(0, &ctx_info->req_starved))
3103 blk_run_queue(mrq->req->q);
3104}
3105
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003106/* invoked by block layer in softirq context */
3107void mmc_blk_cmdq_complete_rq(struct request *rq)
3108{
3109 struct mmc_queue_req *mq_rq = rq->special;
3110 struct mmc_request *mrq = &mq_rq->cmdq_req.mrq;
3111 struct mmc_host *host = mrq->host;
3112 struct mmc_cmdq_context_info *ctx_info = &host->cmdq_ctx;
3113 struct mmc_cmdq_req *cmdq_req = &mq_rq->cmdq_req;
3114 struct mmc_queue *mq = (struct mmc_queue *)rq->q->queuedata;
3115 int err = 0;
3116
3117 if (mrq->cmd && mrq->cmd->error)
3118 err = mrq->cmd->error;
3119 else if (mrq->data && mrq->data->error)
3120 err = mrq->data->error;
3121
Asutosh Das02e30862015-05-20 16:52:04 +05303122 /* clear pending request */
3123 BUG_ON(!test_and_clear_bit(cmdq_req->tag,
3124 &ctx_info->active_reqs));
3125
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003126 mmc_cmdq_post_req(host, mrq, err);
3127 if (err) {
3128 pr_err("%s: %s: txfr error: %d\n", mmc_hostname(mrq->host),
3129 __func__, err);
Asutosh Das02e30862015-05-20 16:52:04 +05303130 if (test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state)) {
3131 pr_err("%s: CQ in error state, ending current req: %d\n",
3132 __func__, err);
3133 blk_end_request_all(rq, err);
3134 } else {
3135 set_bit(CMDQ_STATE_ERR, &ctx_info->curr_state);
3136 schedule_work(&mq->cmdq_err_work);
3137 }
3138 goto out;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003139 }
3140
Asutosh Das5238e022015-04-23 16:00:45 +05303141 if (cmdq_req->cmdq_req_flags & DCMD) {
3142 clear_bit(CMDQ_STATE_DCMD_ACTIVE, &ctx_info->curr_state);
Asutosh Das02e30862015-05-20 16:52:04 +05303143 blk_end_request_all(rq, err);
Asutosh Das5238e022015-04-23 16:00:45 +05303144 goto out;
3145 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003146
3147 blk_end_request(rq, err, cmdq_req->data.bytes_xfered);
3148
Asutosh Das5238e022015-04-23 16:00:45 +05303149out:
Asutosh Das02e30862015-05-20 16:52:04 +05303150 if (!test_bit(CMDQ_STATE_ERR, &ctx_info->curr_state) &&
3151 test_and_clear_bit(0, &ctx_info->req_starved))
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003152 blk_run_queue(mq->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003153 mmc_release_host(host);
3154 return;
3155}
3156
3157/*
3158 * Complete reqs from block layer softirq context
3159 * Invoked in irq context
3160 */
3161void mmc_blk_cmdq_req_done(struct mmc_request *mrq)
3162{
3163 struct request *req = mrq->req;
3164
3165 blk_complete_request(req);
3166}
3167EXPORT_SYMBOL(mmc_blk_cmdq_req_done);
3168
Per Forlinee8a43a2011-07-01 18:55:33 +02003169static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
Per Forlin54d49d72011-07-01 18:55:29 +02003170{
3171 struct mmc_blk_data *md = mq->data;
3172 struct mmc_card *card = md->queue.card;
3173 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
Adrian Hunterb8360a42015-05-07 13:10:24 +03003174 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
Per Forlind78d4a82011-07-01 18:55:30 +02003175 enum mmc_blk_status status;
Per Forlinee8a43a2011-07-01 18:55:33 +02003176 struct mmc_queue_req *mq_rq;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303177 struct request *req = rqc;
Per Forlinee8a43a2011-07-01 18:55:33 +02003178 struct mmc_async_req *areq;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003179 const u8 packed_nr = 2;
3180 u8 reqs = 0;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003181#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3182 unsigned long waitfor = jiffies;
3183#endif
Per Forlinee8a43a2011-07-01 18:55:33 +02003184
3185 if (!rqc && !mq->mqrq_prev->req)
3186 return 0;
Per Forlin54d49d72011-07-01 18:55:29 +02003187
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003188 if (rqc)
3189 reqs = mmc_blk_prep_packed_list(mq, rqc);
3190
Per Forlin54d49d72011-07-01 18:55:29 +02003191 do {
Per Forlinee8a43a2011-07-01 18:55:33 +02003192 if (rqc) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303193 /*
3194 * When 4KB native sector is enabled, only 8 blocks
3195 * multiple read or write is allowed
3196 */
Yuan, Juntaoe87c8562016-05-13 07:59:24 +00003197 if (mmc_large_sector(card) &&
3198 !IS_ALIGNED(blk_rq_sectors(rqc), 8)) {
Saugata Dasa5075eb2012-05-17 16:32:21 +05303199 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
3200 req->rq_disk->disk_name);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003201 mq_rq = mq->mqrq_cur;
Saugata Dasa5075eb2012-05-17 16:32:21 +05303202 goto cmd_abort;
3203 }
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003204
3205 if (reqs >= packed_nr)
3206 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
3207 card, mq);
3208 else
3209 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
Per Forlinee8a43a2011-07-01 18:55:33 +02003210 areq = &mq->mqrq_cur->mmc_active;
3211 } else
3212 areq = NULL;
3213 areq = mmc_start_req(card->host, areq, (int *) &status);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003214 if (!areq) {
3215 if (status == MMC_BLK_NEW_REQUEST)
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003216 set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Per Forlinee8a43a2011-07-01 18:55:33 +02003217 return 0;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003218 }
Pierre Ossman98ccf142007-05-12 00:26:16 +02003219
Per Forlinee8a43a2011-07-01 18:55:33 +02003220 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
3221 brq = &mq_rq->brq;
3222 req = mq_rq->req;
Adrian Hunter67716322011-08-29 16:42:15 +03003223 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
Per Forlinee8a43a2011-07-01 18:55:33 +02003224 mmc_queue_bounce_post(mq_rq);
Pierre Ossman98ccf142007-05-12 00:26:16 +02003225
Per Forlind78d4a82011-07-01 18:55:30 +02003226 switch (status) {
3227 case MMC_BLK_SUCCESS:
3228 case MMC_BLK_PARTIAL:
3229 /*
3230 * A block was successfully transferred.
3231 */
Adrian Hunter67716322011-08-29 16:42:15 +03003232 mmc_blk_reset_success(md, type);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003233
Mark Salyzyn6904e432016-01-28 11:12:25 -08003234 mmc_blk_simulate_delay(mq, rqc, waitfor);
3235
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003236 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3237 ret = mmc_blk_end_packed_req(mq_rq);
3238 break;
3239 } else {
3240 ret = blk_end_request(req, 0,
Per Forlind78d4a82011-07-01 18:55:30 +02003241 brq->data.bytes_xfered);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003242 }
3243
Adrian Hunter67716322011-08-29 16:42:15 +03003244 /*
3245 * If the blk_end_request function returns non-zero even
3246 * though all data has been transferred and no errors
3247 * were returned by the host controller, it's a bug.
3248 */
Per Forlinee8a43a2011-07-01 18:55:33 +02003249 if (status == MMC_BLK_SUCCESS && ret) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303250 pr_err("%s BUG rq_tot %d d_xfer %d\n",
Per Forlinee8a43a2011-07-01 18:55:33 +02003251 __func__, blk_rq_bytes(req),
3252 brq->data.bytes_xfered);
3253 rqc = NULL;
3254 goto cmd_abort;
3255 }
Per Forlind78d4a82011-07-01 18:55:30 +02003256 break;
3257 case MMC_BLK_CMD_ERR:
Adrian Hunter67716322011-08-29 16:42:15 +03003258 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
Ding Wang29535f72015-05-18 20:14:15 +08003259 if (mmc_blk_reset(md, card->host, type))
3260 goto cmd_abort;
3261 if (!ret)
3262 goto start_new_req;
3263 break;
Per Forlind78d4a82011-07-01 18:55:30 +02003264 case MMC_BLK_RETRY:
Adrian Hunterb8360a42015-05-07 13:10:24 +03003265 retune_retry_done = brq->retune_retry_done;
Maya Erezf93ca0a2014-12-09 23:34:41 +02003266 if (retry++ < MMC_BLK_MAX_RETRIES)
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003267 break;
Adrian Hunter67716322011-08-29 16:42:15 +03003268 /* Fall through */
Per Forlind78d4a82011-07-01 18:55:30 +02003269 case MMC_BLK_ABORT:
Maya Erezf93ca0a2014-12-09 23:34:41 +02003270 if (!mmc_blk_reset(md, card->host, type) &&
3271 (retry++ < (MMC_BLK_MAX_RETRIES + 1)))
Adrian Hunter67716322011-08-29 16:42:15 +03003272 break;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003273 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003274 case MMC_BLK_DATA_ERR: {
3275 int err;
3276
3277 err = mmc_blk_reset(md, card->host, type);
3278 if (!err)
3279 break;
Sahitya Tummalad0a19842014-10-31 09:46:20 +05303280 goto cmd_abort;
Adrian Hunter67716322011-08-29 16:42:15 +03003281 }
3282 case MMC_BLK_ECC_ERR:
3283 if (brq->data.blocks > 1) {
3284 /* Redo read one sector at a time */
Joe Perches66061102014-09-12 14:56:56 -07003285 pr_warn("%s: retrying using single block read\n",
3286 req->rq_disk->disk_name);
Adrian Hunter67716322011-08-29 16:42:15 +03003287 disable_multi = 1;
3288 break;
3289 }
Per Forlind78d4a82011-07-01 18:55:30 +02003290 /*
3291 * After an error, we redo I/O one sector at a
3292 * time, so we only reach here after trying to
3293 * read a single sector.
3294 */
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303295 ret = blk_end_request(req, -EIO,
Per Forlind78d4a82011-07-01 18:55:30 +02003296 brq->data.blksz);
Per Forlinee8a43a2011-07-01 18:55:33 +02003297 if (!ret)
3298 goto start_new_req;
Per Forlind78d4a82011-07-01 18:55:30 +02003299 break;
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +05303300 case MMC_BLK_NOMEDIUM:
3301 goto cmd_abort;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003302 default:
3303 pr_err("%s: Unhandled return value (%d)",
3304 req->rq_disk->disk_name, status);
3305 goto cmd_abort;
Russell King - ARM Linux4c2b8f22011-06-20 20:10:49 +01003306 }
3307
Per Forlinee8a43a2011-07-01 18:55:33 +02003308 if (ret) {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003309 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3310 if (!mq_rq->packed->retries)
3311 goto cmd_abort;
3312 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
3313 mmc_start_req(card->host,
3314 &mq_rq->mmc_active, NULL);
3315 } else {
3316
3317 /*
3318 * In case of a incomplete request
3319 * prepare it again and resend.
3320 */
3321 mmc_blk_rw_rq_prep(mq_rq, card,
3322 disable_multi, mq);
3323 mmc_start_req(card->host,
3324 &mq_rq->mmc_active, NULL);
3325 }
Adrian Hunterb8360a42015-05-07 13:10:24 +03003326 mq_rq->brq.retune_retry_done = retune_retry_done;
Per Forlinee8a43a2011-07-01 18:55:33 +02003327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 } while (ret);
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return 1;
3331
Russell King - ARM Linuxa01f3ccf2011-06-20 20:10:28 +01003332 cmd_abort:
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003333 if (mmc_packed_cmd(mq_rq->cmd_type)) {
3334 mmc_blk_abort_packed_req(mq_rq);
3335 } else {
3336 if (mmc_card_removed(card))
3337 req->cmd_flags |= REQ_QUIET;
3338 while (ret)
3339 ret = blk_end_request(req, -EIO,
3340 blk_rq_cur_bytes(req));
3341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Per Forlinee8a43a2011-07-01 18:55:33 +02003343 start_new_req:
3344 if (rqc) {
Seungwon Jeon7a819022013-01-22 19:48:07 +09003345 if (mmc_card_removed(card)) {
3346 rqc->cmd_flags |= REQ_QUIET;
3347 blk_end_request_all(rqc, -EIO);
3348 } else {
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003349 /*
3350 * If current request is packed, it needs to put back.
3351 */
3352 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
3353 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
3354
Seungwon Jeon7a819022013-01-22 19:48:07 +09003355 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
3356 mmc_start_req(card->host,
3357 &mq->mqrq_cur->mmc_active, NULL);
3358 }
Per Forlinee8a43a2011-07-01 18:55:33 +02003359 }
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return 0;
3362}
3363
Asutosh Das8b594832015-04-23 09:55:43 +05303364static inline int mmc_blk_cmdq_part_switch(struct mmc_card *card,
3365 struct mmc_blk_data *md)
3366{
3367 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
3368 struct mmc_host *host = card->host;
3369 struct mmc_cmdq_context_info *ctx = &host->cmdq_ctx;
3370 u8 part_config = card->ext_csd.part_config;
3371
3372 if ((main_md->part_curr == md->part_type) &&
3373 (card->part_curr == md->part_type))
3374 return 0;
3375
3376 WARN_ON(!((card->host->caps2 & MMC_CAP2_CMD_QUEUE) &&
3377 card->ext_csd.cmdq_support &&
3378 (md->flags & MMC_BLK_CMD_QUEUE)));
3379
3380 if (!test_bit(CMDQ_STATE_HALT, &ctx->curr_state))
3381 WARN_ON(mmc_cmdq_halt(host, true));
3382
3383 /* disable CQ mode in card */
3384 if (mmc_card_cmdq(card)) {
3385 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3386 EXT_CSD_CMDQ, 0,
3387 card->ext_csd.generic_cmd6_time));
3388 mmc_card_clr_cmdq(card);
3389 }
3390
3391 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
3392 part_config |= md->part_type;
3393
3394 WARN_ON(mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
3395 EXT_CSD_PART_CONFIG, part_config,
3396 card->ext_csd.part_time));
3397
3398 card->ext_csd.part_config = part_config;
3399 card->part_curr = md->part_type;
3400
3401 main_md->part_curr = md->part_type;
3402
3403 WARN_ON(mmc_blk_cmdq_switch(card, md, true));
3404 WARN_ON(mmc_cmdq_halt(host, false));
3405
3406 return 0;
3407}
3408
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003409static int mmc_blk_cmdq_issue_rq(struct mmc_queue *mq, struct request *req)
3410{
3411 int ret;
3412 struct mmc_blk_data *md = mq->data;
3413 struct mmc_card *card = md->queue.card;
Asutosh Das5238e022015-04-23 16:00:45 +05303414 unsigned int cmd_flags = req ? req->cmd_flags : 0;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003415
3416 mmc_claim_host(card->host);
Asutosh Das8b594832015-04-23 09:55:43 +05303417 ret = mmc_blk_cmdq_part_switch(card, md);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003418 if (ret) {
3419 pr_err("%s: %s: partition switch failed %d\n",
3420 md->disk->disk_name, __func__, ret);
Asutosh Das5238e022015-04-23 16:00:45 +05303421 if (req)
3422 blk_end_request_all(req, ret);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003423 mmc_release_host(card->host);
3424 goto switch_failure;
3425 }
3426
Asutosh Das5238e022015-04-23 16:00:45 +05303427 if (req) {
3428 if (cmd_flags & REQ_PREFLUSH)
3429 ret = mmc_blk_cmdq_issue_flush_rq(mq, req);
3430 else
3431 ret = mmc_blk_cmdq_issue_rw_rq(mq, req);
3432 }
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003433
3434switch_failure:
3435 return ret;
3436}
3437
Linus Walleij29eb7bd2016-09-20 11:34:38 +02003438int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
Adrian Hunterbd788c92010-08-11 14:17:47 -07003439{
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003440 int ret;
3441 struct mmc_blk_data *md = mq->data;
3442 struct mmc_card *card = md->queue.card;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003443 struct mmc_host *host = card->host;
3444 unsigned long flags;
Adrian Hunter869c5542016-08-25 14:11:43 -06003445 bool req_is_special = mmc_req_is_special(req);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003446
Per Forlinee8a43a2011-07-01 18:55:33 +02003447 if (req && !mq->mqrq_prev->req)
3448 /* claim host only for the first request */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003449 mmc_get_card(card);
Per Forlinee8a43a2011-07-01 18:55:33 +02003450
Andrei Warkentin371a6892011-04-11 18:10:25 -05003451 ret = mmc_blk_part_switch(card, md);
3452 if (ret) {
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003453 if (req) {
Subhash Jadavaniecf8b5d2012-06-07 15:46:58 +05303454 blk_end_request_all(req, -EIO);
Adrian Hunter0d7d85c2011-09-23 12:48:20 +03003455 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003456 ret = 0;
3457 goto out;
3458 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003459
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003460 mmc_blk_write_packing_control(mq, req);
3461
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003462 clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags);
Mike Christiec2df40d2016-06-05 14:32:17 -05003463 if (req && req_op(req) == REQ_OP_DISCARD) {
Per Forlinee8a43a2011-07-01 18:55:33 +02003464 /* complete ongoing async transfer before issuing discard */
3465 if (card->host->areq)
3466 mmc_blk_issue_rw_rq(mq, NULL);
Christoph Hellwig288dab82016-06-09 16:00:36 +02003467 ret = mmc_blk_issue_discard_rq(mq, req);
3468 } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) {
3469 /* complete ongoing async transfer before issuing secure erase*/
3470 if (card->host->areq)
3471 mmc_blk_issue_rw_rq(mq, NULL);
Maya Erez0c0609f2014-12-09 23:31:55 +02003472 if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
3473 ret = mmc_blk_issue_secdiscard_rq(mq, req);
3474 else
3475 ret = mmc_blk_issue_discard_rq(mq, req);
Mike Christie3a5e02c2016-06-05 14:32:23 -05003476 } else if (req && req_op(req) == REQ_OP_FLUSH) {
Jaehoon Chung393f9a02011-07-13 17:02:16 +09003477 /* complete ongoing async transfer before issuing flush */
3478 if (card->host->areq)
3479 mmc_blk_issue_rw_rq(mq, NULL);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003480 ret = mmc_blk_issue_flush(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003481 } else {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05003482 if (!req && host->areq) {
3483 spin_lock_irqsave(&host->context_info.lock, flags);
3484 host->context_info.is_waiting_last_req = true;
3485 spin_unlock_irqrestore(&host->context_info.lock, flags);
3486 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003487 ret = mmc_blk_issue_rw_rq(mq, req);
Adrian Hunter49804542010-08-11 14:17:50 -07003488 }
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003489
Andrei Warkentin371a6892011-04-11 18:10:25 -05003490out:
Sujit Reddy Thumma55291992014-12-09 20:40:16 +02003491 if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) ||
3492 req_is_special)
Seungwon Jeonef3a69c72013-03-14 15:17:13 +09003493 /*
3494 * Release host when there are no more requests
3495 * and after special request(discard, flush) is done.
3496 * In case sepecial request, there is no reentry to
3497 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
3498 */
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003499 mmc_put_card(card);
Andrei Warkentin1a258db2011-04-11 18:10:24 -05003500 return ret;
Adrian Hunterbd788c92010-08-11 14:17:47 -07003501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Russell Kinga6f6c962006-01-03 22:38:44 +00003503static inline int mmc_blk_readonly(struct mmc_card *card)
3504{
3505 return mmc_card_readonly(card) ||
3506 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
3507}
3508
Andrei Warkentin371a6892011-04-11 18:10:25 -05003509static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
3510 struct device *parent,
3511 sector_t size,
3512 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003513 const char *subname,
3514 int area_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
3516 struct mmc_blk_data *md;
3517 int devidx, ret;
3518
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003519again:
3520 if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL))
3521 return ERR_PTR(-ENOMEM);
3522
3523 spin_lock(&mmc_blk_lock);
3524 ret = ida_get_new(&mmc_blk_ida, &devidx);
3525 spin_unlock(&mmc_blk_lock);
3526
3527 if (ret == -EAGAIN)
3528 goto again;
3529 else if (ret)
3530 return ERR_PTR(ret);
3531
3532 if (devidx >= max_devices) {
3533 ret = -ENOSPC;
3534 goto out;
3535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003537 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +00003538 if (!md) {
3539 ret = -ENOMEM;
3540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Russell Kinga6f6c962006-01-03 22:38:44 +00003542
Johan Rudholmadd710e2011-12-02 08:51:06 +01003543 md->area_type = area_type;
3544
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003545 /*
Russell Kinga6f6c962006-01-03 22:38:44 +00003546 * Set the read-only status based on the supported commands
3547 * and the write protect switch.
3548 */
3549 md->read_only = mmc_blk_readonly(card);
3550
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003551 md->disk = alloc_disk(perdev_minors);
Russell Kinga6f6c962006-01-03 22:38:44 +00003552 if (md->disk == NULL) {
3553 ret = -ENOMEM;
3554 goto err_kfree;
3555 }
3556
3557 spin_lock_init(&md->lock);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003558 INIT_LIST_HEAD(&md->part);
Russell Kinga6f6c962006-01-03 22:38:44 +00003559 md->usage = 1;
3560
Asutosh Das963469b2015-05-21 13:29:51 +05303561 ret = mmc_init_queue(&md->queue, card, &md->lock, subname, area_type);
Russell Kinga6f6c962006-01-03 22:38:44 +00003562 if (ret)
3563 goto err_putdisk;
3564
Russell Kinga6f6c962006-01-03 22:38:44 +00003565 md->queue.data = md;
3566
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02003567 md->disk->major = MMC_BLOCK_MAJOR;
Olof Johansson5e71b7a2010-09-17 21:19:57 -04003568 md->disk->first_minor = devidx * perdev_minors;
Russell Kinga6f6c962006-01-03 22:38:44 +00003569 md->disk->fops = &mmc_bdops;
3570 md->disk->private_data = md;
3571 md->disk->queue = md->queue.queue;
Dan Williams307d8e62016-06-20 10:40:44 -07003572 md->parent = parent;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003573 set_disk_ro(md->disk, md->read_only || default_ro);
Colin Cross382c55f2015-10-22 10:00:41 -07003574 md->disk->flags = GENHD_FL_EXT_DEVT;
Ulf Hanssonf5b4d712014-09-03 11:02:23 +02003575 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
Loic Pallardy53d8f972012-08-06 17:12:28 +02003576 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
Russell Kinga6f6c962006-01-03 22:38:44 +00003577
3578 /*
3579 * As discussed on lkml, GENHD_FL_REMOVABLE should:
3580 *
3581 * - be set for removable media with permanent block devices
3582 * - be unset for removable block devices with permanent media
3583 *
3584 * Since MMC block devices clearly fall under the second
3585 * case, we do not set GENHD_FL_REMOVABLE. Userspace
3586 * should use the block device creation/destruction hotplug
3587 * messages to tell when the card is present.
3588 */
3589
Andrei Warkentinf06c9152011-04-21 22:46:13 -05003590 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
Ulf Hansson9aaf3432016-04-06 16:12:08 +02003591 "mmcblk%u%s", card->host->index, subname ? subname : "");
Russell Kinga6f6c962006-01-03 22:38:44 +00003592
Saugata Dasa5075eb2012-05-17 16:32:21 +05303593 if (mmc_card_mmc(card))
3594 blk_queue_logical_block_size(md->queue.queue,
3595 card->ext_csd.data_sector_size);
3596 else
3597 blk_queue_logical_block_size(md->queue.queue, 512);
3598
Andrei Warkentin371a6892011-04-11 18:10:25 -05003599 set_capacity(md->disk, size);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003600
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003601 if (mmc_host_cmd23(card->host)) {
Daniel Glöckner0ed50ab2016-08-30 14:17:30 +02003602 if ((mmc_card_mmc(card) &&
3603 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
Andrei Warkentinf0d89972011-05-23 15:06:38 -05003604 (mmc_card_sd(card) &&
3605 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
3606 md->flags |= MMC_BLK_CMD23;
3607 }
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003608
3609 if (mmc_card_mmc(card) &&
3610 md->flags & MMC_BLK_CMD23 &&
3611 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
Asutosh Das5238e022015-04-23 16:00:45 +05303612 card->ext_csd.rel_sectors)) {
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003613 md->flags |= MMC_BLK_REL_WR;
Jens Axboee9d5c742016-03-30 10:17:20 -06003614 blk_queue_write_cache(md->queue.queue, true, true);
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003615 }
3616
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003617 if (card->cmdq_init) {
3618 md->flags |= MMC_BLK_CMD_QUEUE;
3619 md->queue.cmdq_complete_fn = mmc_blk_cmdq_complete_rq;
3620 md->queue.cmdq_issue_fn = mmc_blk_cmdq_issue_rq;
Asutosh Das02e30862015-05-20 16:52:04 +05303621 md->queue.cmdq_error_fn = mmc_blk_cmdq_err;
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003622 }
3623
3624 if (mmc_card_mmc(card) && !card->cmdq_init &&
Seungwon Jeonce39f9d2013-02-06 17:02:46 +09003625 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
3626 (md->flags & MMC_BLK_CMD23) &&
3627 card->ext_csd.packed_event_en) {
3628 if (!mmc_packed_init(&md->queue, card))
3629 md->flags |= MMC_BLK_PACKED_CMD;
3630 }
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +00003633
3634 err_putdisk:
3635 put_disk(md->disk);
3636 err_kfree:
3637 kfree(md);
3638 out:
Ulf Hanssonb10fa992016-04-07 14:36:46 +02003639 spin_lock(&mmc_blk_lock);
3640 ida_remove(&mmc_blk_ida, devidx);
3641 spin_unlock(&mmc_blk_lock);
Russell Kinga6f6c962006-01-03 22:38:44 +00003642 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Andrei Warkentin371a6892011-04-11 18:10:25 -05003645static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
3646{
3647 sector_t size;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003648
3649 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
3650 /*
3651 * The EXT_CSD sector count is in number or 512 byte
3652 * sectors.
3653 */
3654 size = card->ext_csd.sectors;
3655 } else {
3656 /*
3657 * The CSD capacity field is in units of read_blkbits.
3658 * set_capacity takes units of 512 bytes.
3659 */
Kuninori Morimoto087de9e2015-05-11 07:35:28 +00003660 size = (typeof(sector_t))card->csd.capacity
3661 << (card->csd.read_blkbits - 9);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003662 }
3663
Tobias Klauser7a30f2a2015-01-21 15:56:44 +01003664 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003665 MMC_BLK_DATA_AREA_MAIN);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003666}
3667
3668static int mmc_blk_alloc_part(struct mmc_card *card,
3669 struct mmc_blk_data *md,
3670 unsigned int part_type,
3671 sector_t size,
3672 bool default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003673 const char *subname,
3674 int area_type)
Andrei Warkentin371a6892011-04-11 18:10:25 -05003675{
3676 char cap_str[10];
3677 struct mmc_blk_data *part_md;
3678
3679 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003680 subname, area_type);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003681 if (IS_ERR(part_md))
3682 return PTR_ERR(part_md);
3683 part_md->part_type = part_type;
3684 list_add(&part_md->part, &md->part);
3685
James Bottomleyb9f28d82015-03-05 18:47:01 -08003686 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
Andrei Warkentin371a6892011-04-11 18:10:25 -05003687 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303688 pr_info("%s: %s %s partition %u %s\n",
Andrei Warkentin371a6892011-04-11 18:10:25 -05003689 part_md->disk->disk_name, mmc_card_id(card),
3690 mmc_card_name(card), part_md->part_type, cap_str);
3691 return 0;
3692}
3693
Namjae Jeone0c368d2011-10-06 23:41:38 +09003694/* MMC Physical partitions consist of two boot partitions and
3695 * up to four general purpose partitions.
3696 * For each partition enabled in EXT_CSD a block device will be allocatedi
3697 * to provide access to the partition.
3698 */
3699
Andrei Warkentin371a6892011-04-11 18:10:25 -05003700static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
3701{
Namjae Jeone0c368d2011-10-06 23:41:38 +09003702 int idx, ret = 0;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003703
3704 if (!mmc_card_mmc(card))
3705 return 0;
3706
Namjae Jeone0c368d2011-10-06 23:41:38 +09003707 for (idx = 0; idx < card->nr_parts; idx++) {
3708 if (card->part[idx].size) {
3709 ret = mmc_blk_alloc_part(card, md,
3710 card->part[idx].part_cfg,
3711 card->part[idx].size >> 9,
3712 card->part[idx].force_ro,
Johan Rudholmadd710e2011-12-02 08:51:06 +01003713 card->part[idx].name,
3714 card->part[idx].area_type);
Namjae Jeone0c368d2011-10-06 23:41:38 +09003715 if (ret)
3716 return ret;
3717 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003718 }
3719
3720 return ret;
3721}
3722
Andrei Warkentin371a6892011-04-11 18:10:25 -05003723static void mmc_blk_remove_req(struct mmc_blk_data *md)
3724{
Johan Rudholmadd710e2011-12-02 08:51:06 +01003725 struct mmc_card *card;
3726
Andrei Warkentin371a6892011-04-11 18:10:25 -05003727 if (md) {
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003728 /*
3729 * Flush remaining requests and free queues. It
3730 * is freeing the queue that stops new requests
3731 * from being accepted.
3732 */
Franck Jullien8efb83a2013-07-24 15:17:48 +02003733 card = md->queue.card;
Paul Taysomfdfa20c2013-06-04 14:42:40 -07003734 mmc_cleanup_queue(&md->queue);
3735 if (md->flags & MMC_BLK_PACKED_CMD)
3736 mmc_packed_clean(&md->queue);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07003737 if (md->flags & MMC_BLK_CMD_QUEUE)
3738 mmc_cmdq_clean(&md->queue, card);
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003739 device_remove_file(disk_to_dev(md->disk),
3740 &md->num_wr_reqs_to_start_packing);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003741 if (md->disk->flags & GENHD_FL_UP) {
3742 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003743 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3744 card->ext_csd.boot_ro_lockable)
3745 device_remove_file(disk_to_dev(md->disk),
3746 &md->power_ro_lock);
Mark Salyzyn6904e432016-01-28 11:12:25 -08003747#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3748 device_remove_file(disk_to_dev(md->disk),
3749 &dev_attr_max_write_speed);
3750 device_remove_file(disk_to_dev(md->disk),
3751 &dev_attr_max_read_speed);
3752 device_remove_file(disk_to_dev(md->disk),
3753 &dev_attr_cache_size);
3754#endif
Andrei Warkentin371a6892011-04-11 18:10:25 -05003755
Andrei Warkentin371a6892011-04-11 18:10:25 -05003756 del_gendisk(md->disk);
3757 }
Andrei Warkentin371a6892011-04-11 18:10:25 -05003758 mmc_blk_put(md);
3759 }
3760}
3761
3762static void mmc_blk_remove_parts(struct mmc_card *card,
3763 struct mmc_blk_data *md)
3764{
3765 struct list_head *pos, *q;
3766 struct mmc_blk_data *part_md;
3767
3768 list_for_each_safe(pos, q, &md->part) {
3769 part_md = list_entry(pos, struct mmc_blk_data, part);
3770 list_del(pos);
3771 mmc_blk_remove_req(part_md);
3772 }
3773}
3774
3775static int mmc_add_disk(struct mmc_blk_data *md)
3776{
3777 int ret;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003778 struct mmc_card *card = md->queue.card;
Andrei Warkentin371a6892011-04-11 18:10:25 -05003779
Dan Williams307d8e62016-06-20 10:40:44 -07003780 device_add_disk(md->parent, md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003781 md->force_ro.show = force_ro_show;
3782 md->force_ro.store = force_ro_store;
Rabin Vincent641c3182011-04-23 20:52:58 +05303783 sysfs_attr_init(&md->force_ro.attr);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003784 md->force_ro.attr.name = "force_ro";
3785 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
3786 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
3787 if (ret)
Johan Rudholmadd710e2011-12-02 08:51:06 +01003788 goto force_ro_fail;
Mark Salyzyn6904e432016-01-28 11:12:25 -08003789#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3790 atomic_set(&md->queue.max_write_speed, max_write_speed);
3791 ret = device_create_file(disk_to_dev(md->disk),
3792 &dev_attr_max_write_speed);
3793 if (ret)
3794 goto max_write_speed_fail;
3795 atomic_set(&md->queue.max_read_speed, max_read_speed);
3796 ret = device_create_file(disk_to_dev(md->disk),
3797 &dev_attr_max_read_speed);
3798 if (ret)
3799 goto max_read_speed_fail;
3800 atomic_set(&md->queue.cache_size, cache_size);
3801 atomic_long_set(&md->queue.cache_used, 0);
3802 md->queue.cache_jiffies = jiffies;
3803 ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3804 if (ret)
3805 goto cache_size_fail;
3806#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003807
3808 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
3809 card->ext_csd.boot_ro_lockable) {
Al Viro88187392012-03-20 06:00:24 -04003810 umode_t mode;
Johan Rudholmadd710e2011-12-02 08:51:06 +01003811
3812 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
3813 mode = S_IRUGO;
3814 else
3815 mode = S_IRUGO | S_IWUSR;
3816
3817 md->power_ro_lock.show = power_ro_lock_show;
3818 md->power_ro_lock.store = power_ro_lock_store;
Rabin Vincent00d9ac02012-02-01 16:31:56 +01003819 sysfs_attr_init(&md->power_ro_lock.attr);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003820 md->power_ro_lock.attr.mode = mode;
3821 md->power_ro_lock.attr.name =
3822 "ro_lock_until_next_power_on";
3823 ret = device_create_file(disk_to_dev(md->disk),
3824 &md->power_ro_lock);
3825 if (ret)
3826 goto power_ro_lock_fail;
3827 }
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003828
3829 md->num_wr_reqs_to_start_packing.show =
3830 num_wr_reqs_to_start_packing_show;
3831 md->num_wr_reqs_to_start_packing.store =
3832 num_wr_reqs_to_start_packing_store;
3833 sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr);
3834 md->num_wr_reqs_to_start_packing.attr.name =
3835 "num_wr_reqs_to_start_packing";
3836 md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR;
3837 ret = device_create_file(disk_to_dev(md->disk),
3838 &md->num_wr_reqs_to_start_packing);
3839 if (ret)
Maya Erez17022402014-12-04 00:15:42 +02003840 goto num_wr_reqs_to_start_packing_fail;
Tatyana Brokhmanc879b062014-12-03 23:38:06 +02003841
Maya Erez5a8dae12014-12-04 15:13:59 +02003842 md->no_pack_for_random.show = no_pack_for_random_show;
3843 md->no_pack_for_random.store = no_pack_for_random_store;
3844 sysfs_attr_init(&md->no_pack_for_random.attr);
3845 md->no_pack_for_random.attr.name = "no_pack_for_random";
3846 md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR;
3847 ret = device_create_file(disk_to_dev(md->disk),
3848 &md->no_pack_for_random);
3849 if (ret)
3850 goto no_pack_for_random_fails;
3851
Johan Rudholmadd710e2011-12-02 08:51:06 +01003852 return ret;
3853
Maya Erez5a8dae12014-12-04 15:13:59 +02003854no_pack_for_random_fails:
3855 device_remove_file(disk_to_dev(md->disk),
3856 &md->num_wr_reqs_to_start_packing);
Maya Erez17022402014-12-04 00:15:42 +02003857num_wr_reqs_to_start_packing_fail:
3858 device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock);
Johan Rudholmadd710e2011-12-02 08:51:06 +01003859power_ro_lock_fail:
Mark Salyzyn6904e432016-01-28 11:12:25 -08003860#ifdef CONFIG_MMC_SIMULATE_MAX_SPEED
3861 device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size);
3862cache_size_fail:
3863 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed);
3864max_read_speed_fail:
3865 device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed);
3866max_write_speed_fail:
3867#endif
Johan Rudholmadd710e2011-12-02 08:51:06 +01003868 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
3869force_ro_fail:
3870 del_gendisk(md->disk);
Andrei Warkentin371a6892011-04-11 18:10:25 -05003871
3872 return ret;
3873}
3874
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003875static const struct mmc_fixup blk_fixups[] =
3876{
Chris Ballc59d4472011-11-11 22:01:43 -05003877 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
3878 MMC_QUIRK_INAND_CMD38),
3879 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
3880 MMC_QUIRK_INAND_CMD38),
3881 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
3882 MMC_QUIRK_INAND_CMD38),
3883 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
3884 MMC_QUIRK_INAND_CMD38),
3885 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
3886 MMC_QUIRK_INAND_CMD38),
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003887
3888 /*
3889 * Some MMC cards experience performance degradation with CMD23
3890 * instead of CMD12-bounded multiblock transfers. For now we'll
3891 * black list what's bad...
3892 * - Certain Toshiba cards.
3893 *
3894 * N.B. This doesn't affect SD cards.
3895 */
Yangbo Lu7d70d472015-07-10 11:44:03 +08003896 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3897 MMC_QUIRK_BLK_NO_CMD23),
3898 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
3899 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003900 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003901 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003902 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003903 MMC_QUIRK_BLK_NO_CMD23),
Chris Ballc59d4472011-11-11 22:01:43 -05003904 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
Andrei Warkentind0c97cf2011-05-23 15:06:36 -05003905 MMC_QUIRK_BLK_NO_CMD23),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003906
3907 /*
Matt Gumbel32ecd322016-05-20 10:33:46 +03003908 * Some MMC cards need longer data read timeout than indicated in CSD.
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003909 */
Chris Ballc59d4472011-11-11 22:01:43 -05003910 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003911 MMC_QUIRK_LONG_READ_TIME),
Matt Gumbel32ecd322016-05-20 10:33:46 +03003912 MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
3913 MMC_QUIRK_LONG_READ_TIME),
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01003914
Ian Chen3550ccd2012-08-29 15:05:36 +09003915 /*
Guoping Yu3c984a92014-08-06 12:44:55 +08003916 * Some Samsung MMC cards need longer data read timeout than
3917 * indicated in CSD.
3918 */
3919 MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc,
3920 MMC_QUIRK_LONG_READ_TIME),
3921
3922 /*
Ian Chen3550ccd2012-08-29 15:05:36 +09003923 * On these Samsung MoviNAND parts, performing secure erase or
3924 * secure trim can result in unrecoverable corruption due to a
3925 * firmware bug.
3926 */
3927 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3928 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3929 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3930 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3931 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3932 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3933 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3934 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3935 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3936 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3937 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3938 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3939 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3940 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3941 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
3942 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
3943
Shawn Linb5b4ff02015-08-12 13:08:32 +08003944 /*
3945 * On Some Kingston eMMCs, performing trim can result in
3946 * unrecoverable data conrruption occasionally due to a firmware bug.
3947 */
3948 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3949 MMC_QUIRK_TRIM_BROKEN),
3950 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
3951 MMC_QUIRK_TRIM_BROKEN),
3952
Pratibhasagar V8d664e32014-12-03 18:26:42 +02003953 /* Some INAND MCP devices advertise incorrect timeout values */
3954 MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc,
3955 MMC_QUIRK_INAND_DATA_TIMEOUT),
3956
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003957 END_FIXUP
3958};
3959
Ulf Hansson96541ba2015-04-14 13:06:12 +02003960static int mmc_blk_probe(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Andrei Warkentin371a6892011-04-11 18:10:25 -05003962 struct mmc_blk_data *md, *part_md;
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003963 char cap_str[10];
3964
Pierre Ossman912490d2005-05-21 10:27:02 +01003965 /*
3966 * Check that the card supports the command class(es) we need.
3967 */
3968 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return -ENODEV;
3970
Lukas Czerner5204d002014-06-18 13:18:07 +02003971 mmc_fixup_device(card, blk_fixups);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 md = mmc_blk_alloc(card);
3974 if (IS_ERR(md))
3975 return PTR_ERR(md);
3976
James Bottomleyb9f28d82015-03-05 18:47:01 -08003977 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003978 cap_str, sizeof(cap_str));
Girish K Sa3c76eb2011-10-11 11:44:09 +05303979 pr_info("%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +02003981 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Andrei Warkentin371a6892011-04-11 18:10:25 -05003983 if (mmc_blk_alloc_parts(card, md))
3984 goto out;
3985
Ulf Hansson96541ba2015-04-14 13:06:12 +02003986 dev_set_drvdata(&card->dev, md);
Andrei Warkentin6f60c222011-04-11 19:11:04 -04003987
Andrei Warkentin371a6892011-04-11 18:10:25 -05003988 if (mmc_add_disk(md))
3989 goto out;
3990
3991 list_for_each_entry(part_md, &md->part, part) {
3992 if (mmc_add_disk(part_md))
3993 goto out;
3994 }
Ulf Hanssone94cfef2013-05-02 14:02:38 +02003995
3996 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
3997 pm_runtime_use_autosuspend(&card->dev);
3998
3999 /*
4000 * Don't enable runtime PM for SD-combo cards here. Leave that
4001 * decision to be taken during the SDIO init sequence instead.
4002 */
4003 if (card->type != MMC_TYPE_SD_COMBO) {
4004 pm_runtime_set_active(&card->dev);
4005 pm_runtime_enable(&card->dev);
4006 }
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return 0;
4009
4010 out:
Andrei Warkentin371a6892011-04-11 18:10:25 -05004011 mmc_blk_remove_parts(card, md);
4012 mmc_blk_remove_req(md);
Ulf Hansson5865f282012-03-22 11:47:26 +01004013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
Ulf Hansson96541ba2015-04-14 13:06:12 +02004016static void mmc_blk_remove(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004018 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Andrei Warkentin371a6892011-04-11 18:10:25 -05004020 mmc_blk_remove_parts(card, md);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004021 pm_runtime_get_sync(&card->dev);
Adrian Hunterddd6fa72011-06-23 13:40:26 +03004022 mmc_claim_host(card->host);
4023 mmc_blk_part_switch(card, md);
4024 mmc_release_host(card->host);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02004025 if (card->type != MMC_TYPE_SD_COMBO)
4026 pm_runtime_disable(&card->dev);
4027 pm_runtime_put_noidle(&card->dev);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004028 mmc_blk_remove_req(md);
Ulf Hansson96541ba2015-04-14 13:06:12 +02004029 dev_set_drvdata(&card->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Ulf Hansson96541ba2015-04-14 13:06:12 +02004032static int _mmc_blk_suspend(struct mmc_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004034 struct mmc_blk_data *part_md;
Ulf Hansson96541ba2015-04-14 13:06:12 +02004035 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304036 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038 if (md) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304039 rc = mmc_queue_suspend(&md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304040 if (rc)
4041 goto out;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004042 list_for_each_entry(part_md, &md->part, part) {
Subhash Jadavani4893b392013-06-20 18:15:50 +05304043 rc = mmc_queue_suspend(&part_md->queue, 0);
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304044 if (rc)
4045 goto out_resume;
Andrei Warkentin371a6892011-04-11 18:10:25 -05004046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
Subhash Jadavani5cd341a2013-02-26 17:32:58 +05304048 goto out;
4049
4050 out_resume:
4051 mmc_queue_resume(&md->queue);
4052 list_for_each_entry(part_md, &md->part, part) {
4053 mmc_queue_resume(&part_md->queue);
4054 }
4055 out:
4056 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Ulf Hansson96541ba2015-04-14 13:06:12 +02004059static void mmc_blk_shutdown(struct mmc_card *card)
Ulf Hansson76287742013-06-10 17:03:40 +02004060{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004061 _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004062}
4063
Ulf Hansson0967edc2014-10-06 11:29:42 +02004064#ifdef CONFIG_PM_SLEEP
4065static int mmc_blk_suspend(struct device *dev)
Ulf Hansson76287742013-06-10 17:03:40 +02004066{
Ulf Hansson96541ba2015-04-14 13:06:12 +02004067 struct mmc_card *card = mmc_dev_to_card(dev);
4068
4069 return _mmc_blk_suspend(card);
Ulf Hansson76287742013-06-10 17:03:40 +02004070}
4071
Ulf Hansson0967edc2014-10-06 11:29:42 +02004072static int mmc_blk_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
Andrei Warkentin371a6892011-04-11 18:10:25 -05004074 struct mmc_blk_data *part_md;
Ulf Hanssonfc95e302014-10-06 14:34:09 +02004075 struct mmc_blk_data *md = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077 if (md) {
Andrei Warkentin371a6892011-04-11 18:10:25 -05004078 /*
4079 * Resume involves the card going into idle state,
4080 * so current partition is always the main one.
4081 */
4082 md->part_curr = md->part_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 mmc_queue_resume(&md->queue);
Andrei Warkentin371a6892011-04-11 18:10:25 -05004084 list_for_each_entry(part_md, &md->part, part) {
4085 mmc_queue_resume(&part_md->queue);
4086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
4088 return 0;
4089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090#endif
4091
Ulf Hansson0967edc2014-10-06 11:29:42 +02004092static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
4093
Ulf Hansson96541ba2015-04-14 13:06:12 +02004094static struct mmc_driver mmc_driver = {
4095 .drv = {
4096 .name = "mmcblk",
4097 .pm = &mmc_blk_pm_ops,
4098 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 .probe = mmc_blk_probe,
4100 .remove = mmc_blk_remove,
Ulf Hansson76287742013-06-10 17:03:40 +02004101 .shutdown = mmc_blk_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102};
4103
4104static int __init mmc_blk_init(void)
4105{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004106 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004108 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
4109 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
4110
Ben Hutchingsa26eba62014-11-06 03:35:09 +00004111 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
Olof Johansson5e71b7a2010-09-17 21:19:57 -04004112
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004113 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
4114 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004117 res = mmc_register_driver(&mmc_driver);
4118 if (res)
4119 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Akinobu Mita9d4e98e2008-09-13 19:02:07 +09004121 return 0;
4122 out2:
4123 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 out:
4125 return res;
4126}
4127
4128static void __exit mmc_blk_exit(void)
4129{
4130 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +02004131 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
4134module_init(mmc_blk_init);
4135module_exit(mmc_blk_exit);
4136
4137MODULE_LICENSE("GPL");
4138MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
4139