Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Block driver for media (i.e., flash cards) | |
3 | * | |
4 | * Copyright 2002 Hewlett-Packard Company | |
979ce720 | 5 | * Copyright 2005-2008 Pierre Ossman |
1da177e4 LT |
6 | * |
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 | ||
1da177e4 LT |
24 | #include <linux/kernel.h> |
25 | #include <linux/fs.h> | |
5a0e3ad6 | 26 | #include <linux/slab.h> |
1da177e4 LT |
27 | #include <linux/errno.h> |
28 | #include <linux/hdreg.h> | |
29 | #include <linux/kdev_t.h> | |
30 | #include <linux/blkdev.h> | |
a621aaed | 31 | #include <linux/mutex.h> |
ec5a19dd | 32 | #include <linux/scatterlist.h> |
a7bbb573 | 33 | #include <linux/string_helpers.h> |
cb87ea28 JC |
34 | #include <linux/delay.h> |
35 | #include <linux/capability.h> | |
36 | #include <linux/compat.h> | |
e94cfef6 | 37 | #include <linux/pm_runtime.h> |
1da177e4 | 38 | |
cb87ea28 | 39 | #include <linux/mmc/ioctl.h> |
1da177e4 | 40 | #include <linux/mmc/card.h> |
385e3227 | 41 | #include <linux/mmc/host.h> |
da7fbe58 PO |
42 | #include <linux/mmc/mmc.h> |
43 | #include <linux/mmc/sd.h> | |
1da177e4 | 44 | |
1da177e4 LT |
45 | #include <asm/uaccess.h> |
46 | ||
98ac2162 | 47 | #include "queue.h" |
1da177e4 | 48 | |
6b0b6285 | 49 | MODULE_ALIAS("mmc:block"); |
5e71b7a6 OJ |
50 | #ifdef MODULE_PARAM_PREFIX |
51 | #undef MODULE_PARAM_PREFIX | |
52 | #endif | |
53 | #define MODULE_PARAM_PREFIX "mmcblk." | |
54 | ||
6a7a6b45 AW |
55 | #define INAND_CMD38_ARG_EXT_CSD 113 |
56 | #define INAND_CMD38_ARG_ERASE 0x00 | |
57 | #define INAND_CMD38_ARG_TRIM 0x01 | |
58 | #define INAND_CMD38_ARG_SECERASE 0x80 | |
59 | #define INAND_CMD38_ARG_SECTRIM1 0x81 | |
60 | #define INAND_CMD38_ARG_SECTRIM2 0x88 | |
8fee476b | 61 | #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
775a9362 ME |
62 | #define MMC_SANITIZE_REQ_TIMEOUT 240000 |
63 | #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) | |
6a7a6b45 | 64 | |
d3df0465 | 65 | #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \ |
ce39f9d1 SJ |
66 | (rq_data_dir(req) == WRITE)) |
67 | #define PACKED_CMD_VER 0x01 | |
68 | #define PACKED_CMD_WR 0x02 | |
69 | ||
5e71b7a6 | 70 | static DEFINE_MUTEX(block_mutex); |
6b0b6285 | 71 | |
1da177e4 | 72 | /* |
5e71b7a6 OJ |
73 | * The defaults come from config options but can be overriden by module |
74 | * or bootarg options. | |
1da177e4 | 75 | */ |
5e71b7a6 | 76 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
1dff3144 | 77 | |
5e71b7a6 OJ |
78 | /* |
79 | * We've only got one major, so number of mmcblk devices is | |
a26eba61 BH |
80 | * limited to (1 << 20) / number of minors per device. It is also |
81 | * currently limited by the size of the static bitmaps below. | |
5e71b7a6 OJ |
82 | */ |
83 | static int max_devices; | |
84 | ||
a26eba61 BH |
85 | #define MAX_DEVICES 256 |
86 | ||
87 | /* TODO: Replace these with struct ida */ | |
88 | static DECLARE_BITMAP(dev_use, MAX_DEVICES); | |
1da177e4 | 89 | |
1da177e4 LT |
90 | /* |
91 | * There is one mmc_blk_data per slot. | |
92 | */ | |
93 | struct mmc_blk_data { | |
94 | spinlock_t lock; | |
95 | struct gendisk *disk; | |
96 | struct mmc_queue queue; | |
371a689f | 97 | struct list_head part; |
1da177e4 | 98 | |
d0c97cfb AW |
99 | unsigned int flags; |
100 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ | |
101 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ | |
ce39f9d1 | 102 | #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */ |
d0c97cfb | 103 | |
1da177e4 | 104 | unsigned int usage; |
a6f6c96b | 105 | unsigned int read_only; |
371a689f | 106 | unsigned int part_type; |
67716327 AH |
107 | unsigned int reset_done; |
108 | #define MMC_BLK_READ BIT(0) | |
109 | #define MMC_BLK_WRITE BIT(1) | |
110 | #define MMC_BLK_DISCARD BIT(2) | |
111 | #define MMC_BLK_SECDISCARD BIT(3) | |
371a689f AW |
112 | |
113 | /* | |
114 | * Only set in main mmc_blk_data associated | |
fc95e30b | 115 | * with mmc_card with dev_set_drvdata, and keeps |
371a689f AW |
116 | * track of the current selected device partition. |
117 | */ | |
118 | unsigned int part_curr; | |
119 | struct device_attribute force_ro; | |
add710ea JR |
120 | struct device_attribute power_ro_lock; |
121 | int area_type; | |
1da177e4 LT |
122 | }; |
123 | ||
a621aaed | 124 | static DEFINE_MUTEX(open_lock); |
1da177e4 | 125 | |
ce39f9d1 SJ |
126 | enum { |
127 | MMC_PACKED_NR_IDX = -1, | |
128 | MMC_PACKED_NR_ZERO, | |
129 | MMC_PACKED_NR_SINGLE, | |
130 | }; | |
131 | ||
5e71b7a6 OJ |
132 | module_param(perdev_minors, int, 0444); |
133 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); | |
134 | ||
8d1e977d LP |
135 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
136 | struct mmc_blk_data *md); | |
137 | static int get_card_status(struct mmc_card *card, u32 *status, int retries); | |
138 | ||
ce39f9d1 SJ |
139 | static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq) |
140 | { | |
141 | struct mmc_packed *packed = mqrq->packed; | |
142 | ||
143 | BUG_ON(!packed); | |
144 | ||
145 | mqrq->cmd_type = MMC_PACKED_NONE; | |
146 | packed->nr_entries = MMC_PACKED_NR_ZERO; | |
147 | packed->idx_failure = MMC_PACKED_NR_IDX; | |
148 | packed->retries = 0; | |
149 | packed->blocks = 0; | |
150 | } | |
151 | ||
1da177e4 LT |
152 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
153 | { | |
154 | struct mmc_blk_data *md; | |
155 | ||
a621aaed | 156 | mutex_lock(&open_lock); |
1da177e4 LT |
157 | md = disk->private_data; |
158 | if (md && md->usage == 0) | |
159 | md = NULL; | |
160 | if (md) | |
161 | md->usage++; | |
a621aaed | 162 | mutex_unlock(&open_lock); |
1da177e4 LT |
163 | |
164 | return md; | |
165 | } | |
166 | ||
371a689f AW |
167 | static inline int mmc_get_devidx(struct gendisk *disk) |
168 | { | |
382c55f8 | 169 | int devidx = disk->first_minor / perdev_minors; |
371a689f AW |
170 | return devidx; |
171 | } | |
172 | ||
1da177e4 LT |
173 | static void mmc_blk_put(struct mmc_blk_data *md) |
174 | { | |
a621aaed | 175 | mutex_lock(&open_lock); |
1da177e4 LT |
176 | md->usage--; |
177 | if (md->usage == 0) { | |
371a689f | 178 | int devidx = mmc_get_devidx(md->disk); |
5fa83ce2 AH |
179 | blk_cleanup_queue(md->queue.queue); |
180 | ||
1dff3144 DW |
181 | __clear_bit(devidx, dev_use); |
182 | ||
1da177e4 | 183 | put_disk(md->disk); |
1da177e4 LT |
184 | kfree(md); |
185 | } | |
a621aaed | 186 | mutex_unlock(&open_lock); |
1da177e4 LT |
187 | } |
188 | ||
add710ea JR |
189 | static ssize_t power_ro_lock_show(struct device *dev, |
190 | struct device_attribute *attr, char *buf) | |
191 | { | |
192 | int ret; | |
193 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
194 | struct mmc_card *card = md->queue.card; | |
195 | int locked = 0; | |
196 | ||
197 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) | |
198 | locked = 2; | |
199 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) | |
200 | locked = 1; | |
201 | ||
202 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); | |
203 | ||
9098f84c TW |
204 | mmc_blk_put(md); |
205 | ||
add710ea JR |
206 | return ret; |
207 | } | |
208 | ||
209 | static ssize_t power_ro_lock_store(struct device *dev, | |
210 | struct device_attribute *attr, const char *buf, size_t count) | |
211 | { | |
212 | int ret; | |
213 | struct mmc_blk_data *md, *part_md; | |
214 | struct mmc_card *card; | |
215 | unsigned long set; | |
216 | ||
217 | if (kstrtoul(buf, 0, &set)) | |
218 | return -EINVAL; | |
219 | ||
220 | if (set != 1) | |
221 | return count; | |
222 | ||
223 | md = mmc_blk_get(dev_to_disk(dev)); | |
224 | card = md->queue.card; | |
225 | ||
e94cfef6 | 226 | mmc_get_card(card); |
add710ea JR |
227 | |
228 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | |
229 | card->ext_csd.boot_ro_lock | | |
230 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | |
231 | card->ext_csd.part_time); | |
232 | if (ret) | |
233 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret); | |
234 | else | |
235 | card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN; | |
236 | ||
e94cfef6 | 237 | mmc_put_card(card); |
add710ea JR |
238 | |
239 | if (!ret) { | |
240 | pr_info("%s: Locking boot partition ro until next power on\n", | |
241 | md->disk->disk_name); | |
242 | set_disk_ro(md->disk, 1); | |
243 | ||
244 | list_for_each_entry(part_md, &md->part, part) | |
245 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { | |
246 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); | |
247 | set_disk_ro(part_md->disk, 1); | |
248 | } | |
249 | } | |
250 | ||
251 | mmc_blk_put(md); | |
252 | return count; | |
253 | } | |
254 | ||
371a689f AW |
255 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
256 | char *buf) | |
257 | { | |
258 | int ret; | |
259 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
260 | ||
0031a98a | 261 | ret = snprintf(buf, PAGE_SIZE, "%d\n", |
371a689f AW |
262 | get_disk_ro(dev_to_disk(dev)) ^ |
263 | md->read_only); | |
264 | mmc_blk_put(md); | |
265 | return ret; | |
266 | } | |
267 | ||
268 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, | |
269 | const char *buf, size_t count) | |
270 | { | |
271 | int ret; | |
272 | char *end; | |
273 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
274 | unsigned long set = simple_strtoul(buf, &end, 0); | |
275 | if (end == buf) { | |
276 | ret = -EINVAL; | |
277 | goto out; | |
278 | } | |
279 | ||
280 | set_disk_ro(dev_to_disk(dev), set || md->read_only); | |
281 | ret = count; | |
282 | out: | |
283 | mmc_blk_put(md); | |
284 | return ret; | |
285 | } | |
286 | ||
a5a1561f | 287 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 288 | { |
a5a1561f | 289 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
1da177e4 LT |
290 | int ret = -ENXIO; |
291 | ||
2a48fc0a | 292 | mutex_lock(&block_mutex); |
1da177e4 LT |
293 | if (md) { |
294 | if (md->usage == 2) | |
a5a1561f | 295 | check_disk_change(bdev); |
1da177e4 | 296 | ret = 0; |
a00fc090 | 297 | |
a5a1561f | 298 | if ((mode & FMODE_WRITE) && md->read_only) { |
70bb0896 | 299 | mmc_blk_put(md); |
a00fc090 | 300 | ret = -EROFS; |
70bb0896 | 301 | } |
1da177e4 | 302 | } |
2a48fc0a | 303 | mutex_unlock(&block_mutex); |
1da177e4 LT |
304 | |
305 | return ret; | |
306 | } | |
307 | ||
db2a144b | 308 | static void mmc_blk_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 309 | { |
a5a1561f | 310 | struct mmc_blk_data *md = disk->private_data; |
1da177e4 | 311 | |
2a48fc0a | 312 | mutex_lock(&block_mutex); |
1da177e4 | 313 | mmc_blk_put(md); |
2a48fc0a | 314 | mutex_unlock(&block_mutex); |
1da177e4 LT |
315 | } |
316 | ||
317 | static int | |
a885c8c4 | 318 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 319 | { |
a885c8c4 CH |
320 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
321 | geo->heads = 4; | |
322 | geo->sectors = 16; | |
323 | return 0; | |
1da177e4 LT |
324 | } |
325 | ||
cb87ea28 JC |
326 | struct mmc_blk_ioc_data { |
327 | struct mmc_ioc_cmd ic; | |
328 | unsigned char *buf; | |
329 | u64 buf_bytes; | |
330 | }; | |
331 | ||
332 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( | |
333 | struct mmc_ioc_cmd __user *user) | |
334 | { | |
335 | struct mmc_blk_ioc_data *idata; | |
336 | int err; | |
337 | ||
1ff8950c | 338 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); |
cb87ea28 JC |
339 | if (!idata) { |
340 | err = -ENOMEM; | |
aea253ec | 341 | goto out; |
cb87ea28 JC |
342 | } |
343 | ||
344 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { | |
345 | err = -EFAULT; | |
aea253ec | 346 | goto idata_err; |
cb87ea28 JC |
347 | } |
348 | ||
349 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; | |
350 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { | |
351 | err = -EOVERFLOW; | |
aea253ec | 352 | goto idata_err; |
cb87ea28 JC |
353 | } |
354 | ||
4d6144de JR |
355 | if (!idata->buf_bytes) |
356 | return idata; | |
357 | ||
1ff8950c | 358 | idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL); |
cb87ea28 JC |
359 | if (!idata->buf) { |
360 | err = -ENOMEM; | |
aea253ec | 361 | goto idata_err; |
cb87ea28 JC |
362 | } |
363 | ||
364 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) | |
365 | idata->ic.data_ptr, idata->buf_bytes)) { | |
366 | err = -EFAULT; | |
367 | goto copy_err; | |
368 | } | |
369 | ||
370 | return idata; | |
371 | ||
372 | copy_err: | |
373 | kfree(idata->buf); | |
aea253ec | 374 | idata_err: |
cb87ea28 | 375 | kfree(idata); |
aea253ec | 376 | out: |
cb87ea28 | 377 | return ERR_PTR(err); |
cb87ea28 JC |
378 | } |
379 | ||
a5f5774c JH |
380 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, |
381 | struct mmc_blk_ioc_data *idata) | |
382 | { | |
383 | struct mmc_ioc_cmd *ic = &idata->ic; | |
384 | ||
385 | if (copy_to_user(&(ic_ptr->response), ic->response, | |
386 | sizeof(ic->response))) | |
387 | return -EFAULT; | |
388 | ||
389 | if (!idata->ic.write_flag) { | |
390 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, | |
391 | idata->buf, idata->buf_bytes)) | |
392 | return -EFAULT; | |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
397 | ||
8d1e977d LP |
398 | static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status, |
399 | u32 retries_max) | |
400 | { | |
401 | int err; | |
402 | u32 retry_count = 0; | |
403 | ||
404 | if (!status || !retries_max) | |
405 | return -EINVAL; | |
406 | ||
407 | do { | |
408 | err = get_card_status(card, status, 5); | |
409 | if (err) | |
410 | break; | |
411 | ||
412 | if (!R1_STATUS(*status) && | |
413 | (R1_CURRENT_STATE(*status) != R1_STATE_PRG)) | |
414 | break; /* RPMB programming operation complete */ | |
415 | ||
416 | /* | |
417 | * Rechedule to give the MMC device a chance to continue | |
418 | * processing the previous command without being polled too | |
419 | * frequently. | |
420 | */ | |
421 | usleep_range(1000, 5000); | |
422 | } while (++retry_count < retries_max); | |
423 | ||
424 | if (retry_count == retries_max) | |
425 | err = -EPERM; | |
426 | ||
427 | return err; | |
428 | } | |
429 | ||
775a9362 ME |
430 | static int ioctl_do_sanitize(struct mmc_card *card) |
431 | { | |
432 | int err; | |
433 | ||
a2d1086d | 434 | if (!mmc_can_sanitize(card)) { |
775a9362 ME |
435 | pr_warn("%s: %s - SANITIZE is not supported\n", |
436 | mmc_hostname(card->host), __func__); | |
437 | err = -EOPNOTSUPP; | |
438 | goto out; | |
439 | } | |
440 | ||
441 | pr_debug("%s: %s - SANITIZE IN PROGRESS...\n", | |
442 | mmc_hostname(card->host), __func__); | |
443 | ||
444 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
445 | EXT_CSD_SANITIZE_START, 1, | |
446 | MMC_SANITIZE_REQ_TIMEOUT); | |
447 | ||
448 | if (err) | |
449 | pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n", | |
450 | mmc_hostname(card->host), __func__, err); | |
451 | ||
452 | pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host), | |
453 | __func__); | |
454 | out: | |
455 | return err; | |
456 | } | |
457 | ||
a5f5774c JH |
458 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, |
459 | struct mmc_blk_ioc_data *idata) | |
cb87ea28 | 460 | { |
cb87ea28 JC |
461 | struct mmc_command cmd = {0}; |
462 | struct mmc_data data = {0}; | |
ad5fd972 | 463 | struct mmc_request mrq = {NULL}; |
cb87ea28 JC |
464 | struct scatterlist sg; |
465 | int err; | |
8d1e977d LP |
466 | int is_rpmb = false; |
467 | u32 status = 0; | |
cb87ea28 | 468 | |
a5f5774c JH |
469 | if (!card || !md || !idata) |
470 | return -EINVAL; | |
cb87ea28 | 471 | |
8d1e977d LP |
472 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) |
473 | is_rpmb = true; | |
474 | ||
4d6144de JR |
475 | cmd.opcode = idata->ic.opcode; |
476 | cmd.arg = idata->ic.arg; | |
477 | cmd.flags = idata->ic.flags; | |
478 | ||
479 | if (idata->buf_bytes) { | |
480 | data.sg = &sg; | |
481 | data.sg_len = 1; | |
482 | data.blksz = idata->ic.blksz; | |
483 | data.blocks = idata->ic.blocks; | |
484 | ||
485 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); | |
486 | ||
487 | if (idata->ic.write_flag) | |
488 | data.flags = MMC_DATA_WRITE; | |
489 | else | |
490 | data.flags = MMC_DATA_READ; | |
491 | ||
492 | /* data.flags must already be set before doing this. */ | |
493 | mmc_set_data_timeout(&data, card); | |
494 | ||
495 | /* Allow overriding the timeout_ns for empirical tuning. */ | |
496 | if (idata->ic.data_timeout_ns) | |
497 | data.timeout_ns = idata->ic.data_timeout_ns; | |
498 | ||
499 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { | |
500 | /* | |
501 | * Pretend this is a data transfer and rely on the | |
502 | * host driver to compute timeout. When all host | |
503 | * drivers support cmd.cmd_timeout for R1B, this | |
504 | * can be changed to: | |
505 | * | |
506 | * mrq.data = NULL; | |
507 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; | |
508 | */ | |
509 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; | |
510 | } | |
511 | ||
512 | mrq.data = &data; | |
513 | } | |
514 | ||
515 | mrq.cmd = &cmd; | |
516 | ||
8d1e977d LP |
517 | err = mmc_blk_part_switch(card, md); |
518 | if (err) | |
a5f5774c | 519 | return err; |
8d1e977d | 520 | |
cb87ea28 JC |
521 | if (idata->ic.is_acmd) { |
522 | err = mmc_app_cmd(card->host, card); | |
523 | if (err) | |
a5f5774c | 524 | return err; |
cb87ea28 JC |
525 | } |
526 | ||
8d1e977d LP |
527 | if (is_rpmb) { |
528 | err = mmc_set_blockcount(card, data.blocks, | |
529 | idata->ic.write_flag & (1 << 31)); | |
530 | if (err) | |
a5f5774c | 531 | return err; |
8d1e977d LP |
532 | } |
533 | ||
a82e484e YG |
534 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
535 | (cmd.opcode == MMC_SWITCH)) { | |
775a9362 ME |
536 | err = ioctl_do_sanitize(card); |
537 | ||
538 | if (err) | |
539 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", | |
540 | __func__, err); | |
541 | ||
a5f5774c | 542 | return err; |
775a9362 ME |
543 | } |
544 | ||
cb87ea28 JC |
545 | mmc_wait_for_req(card->host, &mrq); |
546 | ||
547 | if (cmd.error) { | |
548 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", | |
549 | __func__, cmd.error); | |
a5f5774c | 550 | return cmd.error; |
cb87ea28 JC |
551 | } |
552 | if (data.error) { | |
553 | dev_err(mmc_dev(card->host), "%s: data error %d\n", | |
554 | __func__, data.error); | |
a5f5774c | 555 | return data.error; |
cb87ea28 JC |
556 | } |
557 | ||
558 | /* | |
559 | * According to the SD specs, some commands require a delay after | |
560 | * issuing the command. | |
561 | */ | |
562 | if (idata->ic.postsleep_min_us) | |
563 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); | |
564 | ||
a5f5774c | 565 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); |
cb87ea28 | 566 | |
8d1e977d LP |
567 | if (is_rpmb) { |
568 | /* | |
569 | * Ensure RPMB command has completed by polling CMD13 | |
570 | * "Send Status". | |
571 | */ | |
572 | err = ioctl_rpmb_card_status_poll(card, &status, 5); | |
573 | if (err) | |
574 | dev_err(mmc_dev(card->host), | |
575 | "%s: Card Status=0x%08X, error %d\n", | |
576 | __func__, status, err); | |
577 | } | |
578 | ||
a5f5774c JH |
579 | return err; |
580 | } | |
581 | ||
582 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, | |
583 | struct mmc_ioc_cmd __user *ic_ptr) | |
584 | { | |
585 | struct mmc_blk_ioc_data *idata; | |
586 | struct mmc_blk_data *md; | |
587 | struct mmc_card *card; | |
b093410c | 588 | int err = 0, ioc_err = 0; |
a5f5774c | 589 | |
83c742c3 SL |
590 | /* |
591 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
592 | * whole block device, not on a partition. This prevents overspray | |
593 | * between sibling partitions. | |
594 | */ | |
595 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | |
596 | return -EPERM; | |
597 | ||
a5f5774c JH |
598 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
599 | if (IS_ERR(idata)) | |
600 | return PTR_ERR(idata); | |
601 | ||
602 | md = mmc_blk_get(bdev->bd_disk); | |
603 | if (!md) { | |
604 | err = -EINVAL; | |
605 | goto cmd_err; | |
606 | } | |
607 | ||
608 | card = md->queue.card; | |
609 | if (IS_ERR(card)) { | |
610 | err = PTR_ERR(card); | |
611 | goto cmd_done; | |
612 | } | |
613 | ||
614 | mmc_get_card(card); | |
615 | ||
b093410c | 616 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata); |
a5f5774c | 617 | |
e94cfef6 | 618 | mmc_put_card(card); |
cb87ea28 | 619 | |
b093410c | 620 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
a5f5774c | 621 | |
cb87ea28 JC |
622 | cmd_done: |
623 | mmc_blk_put(md); | |
1c02f000 | 624 | cmd_err: |
cb87ea28 JC |
625 | kfree(idata->buf); |
626 | kfree(idata); | |
b093410c | 627 | return ioc_err ? ioc_err : err; |
cb87ea28 JC |
628 | } |
629 | ||
a5f5774c JH |
630 | static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, |
631 | struct mmc_ioc_multi_cmd __user *user) | |
632 | { | |
633 | struct mmc_blk_ioc_data **idata = NULL; | |
634 | struct mmc_ioc_cmd __user *cmds = user->cmds; | |
635 | struct mmc_card *card; | |
636 | struct mmc_blk_data *md; | |
b093410c | 637 | int i, err = 0, ioc_err = 0; |
a5f5774c JH |
638 | __u64 num_of_cmds; |
639 | ||
83c742c3 SL |
640 | /* |
641 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
642 | * whole block device, not on a partition. This prevents overspray | |
643 | * between sibling partitions. | |
644 | */ | |
645 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | |
646 | return -EPERM; | |
647 | ||
a5f5774c JH |
648 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, |
649 | sizeof(num_of_cmds))) | |
650 | return -EFAULT; | |
651 | ||
652 | if (num_of_cmds > MMC_IOC_MAX_CMDS) | |
653 | return -EINVAL; | |
654 | ||
655 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); | |
656 | if (!idata) | |
657 | return -ENOMEM; | |
658 | ||
659 | for (i = 0; i < num_of_cmds; i++) { | |
660 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); | |
661 | if (IS_ERR(idata[i])) { | |
662 | err = PTR_ERR(idata[i]); | |
663 | num_of_cmds = i; | |
664 | goto cmd_err; | |
665 | } | |
666 | } | |
667 | ||
668 | md = mmc_blk_get(bdev->bd_disk); | |
f00ab14c OJ |
669 | if (!md) { |
670 | err = -EINVAL; | |
a5f5774c | 671 | goto cmd_err; |
f00ab14c | 672 | } |
a5f5774c JH |
673 | |
674 | card = md->queue.card; | |
675 | if (IS_ERR(card)) { | |
676 | err = PTR_ERR(card); | |
677 | goto cmd_done; | |
678 | } | |
679 | ||
680 | mmc_get_card(card); | |
681 | ||
b093410c GG |
682 | for (i = 0; i < num_of_cmds && !ioc_err; i++) |
683 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]); | |
a5f5774c JH |
684 | |
685 | mmc_put_card(card); | |
686 | ||
687 | /* copy to user if data and response */ | |
b093410c | 688 | for (i = 0; i < num_of_cmds && !err; i++) |
a5f5774c | 689 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
a5f5774c JH |
690 | |
691 | cmd_done: | |
692 | mmc_blk_put(md); | |
693 | cmd_err: | |
694 | for (i = 0; i < num_of_cmds; i++) { | |
695 | kfree(idata[i]->buf); | |
696 | kfree(idata[i]); | |
697 | } | |
698 | kfree(idata); | |
b093410c | 699 | return ioc_err ? ioc_err : err; |
a5f5774c JH |
700 | } |
701 | ||
cb87ea28 JC |
702 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
703 | unsigned int cmd, unsigned long arg) | |
704 | { | |
a5f5774c JH |
705 | switch (cmd) { |
706 | case MMC_IOC_CMD: | |
707 | return mmc_blk_ioctl_cmd(bdev, | |
708 | (struct mmc_ioc_cmd __user *)arg); | |
709 | case MMC_IOC_MULTI_CMD: | |
710 | return mmc_blk_ioctl_multi_cmd(bdev, | |
711 | (struct mmc_ioc_multi_cmd __user *)arg); | |
712 | default: | |
713 | return -EINVAL; | |
714 | } | |
cb87ea28 JC |
715 | } |
716 | ||
717 | #ifdef CONFIG_COMPAT | |
718 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, | |
719 | unsigned int cmd, unsigned long arg) | |
720 | { | |
721 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); | |
722 | } | |
723 | #endif | |
724 | ||
83d5cde4 | 725 | static const struct block_device_operations mmc_bdops = { |
a5a1561f AV |
726 | .open = mmc_blk_open, |
727 | .release = mmc_blk_release, | |
a885c8c4 | 728 | .getgeo = mmc_blk_getgeo, |
1da177e4 | 729 | .owner = THIS_MODULE, |
cb87ea28 JC |
730 | .ioctl = mmc_blk_ioctl, |
731 | #ifdef CONFIG_COMPAT | |
732 | .compat_ioctl = mmc_blk_compat_ioctl, | |
733 | #endif | |
1da177e4 LT |
734 | }; |
735 | ||
371a689f AW |
736 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
737 | struct mmc_blk_data *md) | |
738 | { | |
739 | int ret; | |
fc95e30b | 740 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
0d7d85ca | 741 | |
371a689f AW |
742 | if (main_md->part_curr == md->part_type) |
743 | return 0; | |
744 | ||
745 | if (mmc_card_mmc(card)) { | |
0d7d85ca AH |
746 | u8 part_config = card->ext_csd.part_config; |
747 | ||
748 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; | |
749 | part_config |= md->part_type; | |
371a689f AW |
750 | |
751 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
0d7d85ca | 752 | EXT_CSD_PART_CONFIG, part_config, |
371a689f AW |
753 | card->ext_csd.part_time); |
754 | if (ret) | |
755 | return ret; | |
0d7d85ca AH |
756 | |
757 | card->ext_csd.part_config = part_config; | |
67716327 | 758 | } |
371a689f AW |
759 | |
760 | main_md->part_curr = md->part_type; | |
761 | return 0; | |
762 | } | |
763 | ||
ec5a19dd PO |
764 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
765 | { | |
766 | int err; | |
051913da BD |
767 | u32 result; |
768 | __be32 *blocks; | |
ec5a19dd | 769 | |
ad5fd972 | 770 | struct mmc_request mrq = {NULL}; |
1278dba1 | 771 | struct mmc_command cmd = {0}; |
a61ad2b4 | 772 | struct mmc_data data = {0}; |
ec5a19dd PO |
773 | |
774 | struct scatterlist sg; | |
775 | ||
ec5a19dd PO |
776 | cmd.opcode = MMC_APP_CMD; |
777 | cmd.arg = card->rca << 16; | |
7213d175 | 778 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
ec5a19dd PO |
779 | |
780 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
7213d175 DB |
781 | if (err) |
782 | return (u32)-1; | |
783 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) | |
ec5a19dd PO |
784 | return (u32)-1; |
785 | ||
786 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
787 | ||
788 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; | |
789 | cmd.arg = 0; | |
7213d175 | 790 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
ec5a19dd | 791 | |
ec5a19dd PO |
792 | data.blksz = 4; |
793 | data.blocks = 1; | |
794 | data.flags = MMC_DATA_READ; | |
795 | data.sg = &sg; | |
796 | data.sg_len = 1; | |
d380443c | 797 | mmc_set_data_timeout(&data, card); |
ec5a19dd | 798 | |
ec5a19dd PO |
799 | mrq.cmd = &cmd; |
800 | mrq.data = &data; | |
801 | ||
051913da BD |
802 | blocks = kmalloc(4, GFP_KERNEL); |
803 | if (!blocks) | |
804 | return (u32)-1; | |
805 | ||
806 | sg_init_one(&sg, blocks, 4); | |
ec5a19dd PO |
807 | |
808 | mmc_wait_for_req(card->host, &mrq); | |
809 | ||
051913da BD |
810 | result = ntohl(*blocks); |
811 | kfree(blocks); | |
812 | ||
17b0429d | 813 | if (cmd.error || data.error) |
051913da | 814 | result = (u32)-1; |
ec5a19dd | 815 | |
051913da | 816 | return result; |
ec5a19dd PO |
817 | } |
818 | ||
0a2d4048 | 819 | static int get_card_status(struct mmc_card *card, u32 *status, int retries) |
504f191f | 820 | { |
1278dba1 | 821 | struct mmc_command cmd = {0}; |
504f191f AH |
822 | int err; |
823 | ||
504f191f AH |
824 | cmd.opcode = MMC_SEND_STATUS; |
825 | if (!mmc_host_is_spi(card->host)) | |
826 | cmd.arg = card->rca << 16; | |
827 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; | |
0a2d4048 RKAL |
828 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
829 | if (err == 0) | |
830 | *status = cmd.resp[0]; | |
831 | return err; | |
504f191f AH |
832 | } |
833 | ||
c49433fb | 834 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, |
95a91298 | 835 | bool hw_busy_detect, struct request *req, int *gen_err) |
c49433fb UH |
836 | { |
837 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); | |
838 | int err = 0; | |
839 | u32 status; | |
840 | ||
841 | do { | |
842 | err = get_card_status(card, &status, 5); | |
843 | if (err) { | |
844 | pr_err("%s: error %d requesting status\n", | |
845 | req->rq_disk->disk_name, err); | |
846 | return err; | |
847 | } | |
848 | ||
849 | if (status & R1_ERROR) { | |
850 | pr_err("%s: %s: error sending status cmd, status %#x\n", | |
851 | req->rq_disk->disk_name, __func__, status); | |
852 | *gen_err = 1; | |
853 | } | |
854 | ||
95a91298 UH |
855 | /* We may rely on the host hw to handle busy detection.*/ |
856 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && | |
857 | hw_busy_detect) | |
858 | break; | |
859 | ||
c49433fb UH |
860 | /* |
861 | * Timeout if the device never becomes ready for data and never | |
862 | * leaves the program state. | |
863 | */ | |
864 | if (time_after(jiffies, timeout)) { | |
865 | pr_err("%s: Card stuck in programming state! %s %s\n", | |
866 | mmc_hostname(card->host), | |
867 | req->rq_disk->disk_name, __func__); | |
868 | return -ETIMEDOUT; | |
869 | } | |
870 | ||
871 | /* | |
872 | * Some cards mishandle the status bits, | |
873 | * so make sure to check both the busy | |
874 | * indication and the card state. | |
875 | */ | |
876 | } while (!(status & R1_READY_FOR_DATA) || | |
877 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); | |
878 | ||
879 | return err; | |
880 | } | |
881 | ||
bb5cba40 UH |
882 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
883 | struct request *req, int *gen_err, u32 *stop_status) | |
884 | { | |
885 | struct mmc_host *host = card->host; | |
886 | struct mmc_command cmd = {0}; | |
887 | int err; | |
888 | bool use_r1b_resp = rq_data_dir(req) == WRITE; | |
889 | ||
890 | /* | |
891 | * Normally we use R1B responses for WRITE, but in cases where the host | |
892 | * has specified a max_busy_timeout we need to validate it. A failure | |
893 | * means we need to prevent the host from doing hw busy detection, which | |
894 | * is done by converting to a R1 response instead. | |
895 | */ | |
896 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) | |
897 | use_r1b_resp = false; | |
898 | ||
899 | cmd.opcode = MMC_STOP_TRANSMISSION; | |
900 | if (use_r1b_resp) { | |
901 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
902 | cmd.busy_timeout = timeout_ms; | |
903 | } else { | |
904 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
905 | } | |
906 | ||
907 | err = mmc_wait_for_cmd(host, &cmd, 5); | |
908 | if (err) | |
909 | return err; | |
910 | ||
911 | *stop_status = cmd.resp[0]; | |
912 | ||
913 | /* No need to check card status in case of READ. */ | |
914 | if (rq_data_dir(req) == READ) | |
915 | return 0; | |
916 | ||
917 | if (!mmc_host_is_spi(host) && | |
918 | (*stop_status & R1_ERROR)) { | |
919 | pr_err("%s: %s: general error sending stop command, resp %#x\n", | |
920 | req->rq_disk->disk_name, __func__, *stop_status); | |
921 | *gen_err = 1; | |
922 | } | |
923 | ||
924 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); | |
925 | } | |
926 | ||
a8ad82cc | 927 | #define ERR_NOMEDIUM 3 |
a01f3ccf RKAL |
928 | #define ERR_RETRY 2 |
929 | #define ERR_ABORT 1 | |
930 | #define ERR_CONTINUE 0 | |
931 | ||
932 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, | |
933 | bool status_valid, u32 status) | |
934 | { | |
935 | switch (error) { | |
936 | case -EILSEQ: | |
937 | /* response crc error, retry the r/w cmd */ | |
938 | pr_err("%s: %s sending %s command, card status %#x\n", | |
939 | req->rq_disk->disk_name, "response CRC error", | |
940 | name, status); | |
941 | return ERR_RETRY; | |
942 | ||
943 | case -ETIMEDOUT: | |
944 | pr_err("%s: %s sending %s command, card status %#x\n", | |
945 | req->rq_disk->disk_name, "timed out", name, status); | |
946 | ||
947 | /* If the status cmd initially failed, retry the r/w cmd */ | |
948 | if (!status_valid) | |
949 | return ERR_RETRY; | |
950 | ||
951 | /* | |
952 | * If it was a r/w cmd crc error, or illegal command | |
953 | * (eg, issued in wrong state) then retry - we should | |
954 | * have corrected the state problem above. | |
955 | */ | |
956 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) | |
957 | return ERR_RETRY; | |
958 | ||
959 | /* Otherwise abort the command */ | |
960 | return ERR_ABORT; | |
961 | ||
962 | default: | |
963 | /* We don't understand the error code the driver gave us */ | |
964 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", | |
965 | req->rq_disk->disk_name, error, status); | |
966 | return ERR_ABORT; | |
967 | } | |
968 | } | |
969 | ||
970 | /* | |
971 | * Initial r/w and stop cmd error recovery. | |
972 | * We don't know whether the card received the r/w cmd or not, so try to | |
973 | * restore things back to a sane state. Essentially, we do this as follows: | |
974 | * - Obtain card status. If the first attempt to obtain card status fails, | |
975 | * the status word will reflect the failed status cmd, not the failed | |
976 | * r/w cmd. If we fail to obtain card status, it suggests we can no | |
977 | * longer communicate with the card. | |
978 | * - Check the card state. If the card received the cmd but there was a | |
979 | * transient problem with the response, it might still be in a data transfer | |
980 | * mode. Try to send it a stop command. If this fails, we can't recover. | |
981 | * - If the r/w cmd failed due to a response CRC error, it was probably | |
982 | * transient, so retry the cmd. | |
983 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. | |
984 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or | |
985 | * illegal cmd, retry. | |
986 | * Otherwise we don't understand what happened, so abort. | |
987 | */ | |
988 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, | |
c8760069 | 989 | struct mmc_blk_request *brq, int *ecc_err, int *gen_err) |
a01f3ccf RKAL |
990 | { |
991 | bool prev_cmd_status_valid = true; | |
992 | u32 status, stop_status = 0; | |
993 | int err, retry; | |
994 | ||
a8ad82cc SRT |
995 | if (mmc_card_removed(card)) |
996 | return ERR_NOMEDIUM; | |
997 | ||
a01f3ccf RKAL |
998 | /* |
999 | * Try to get card status which indicates both the card state | |
1000 | * and why there was no response. If the first attempt fails, | |
1001 | * we can't be sure the returned status is for the r/w command. | |
1002 | */ | |
1003 | for (retry = 2; retry >= 0; retry--) { | |
1004 | err = get_card_status(card, &status, 0); | |
1005 | if (!err) | |
1006 | break; | |
1007 | ||
6f398ad2 AH |
1008 | /* Re-tune if needed */ |
1009 | mmc_retune_recheck(card->host); | |
1010 | ||
a01f3ccf RKAL |
1011 | prev_cmd_status_valid = false; |
1012 | pr_err("%s: error %d sending status command, %sing\n", | |
1013 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); | |
1014 | } | |
1015 | ||
1016 | /* We couldn't get a response from the card. Give up. */ | |
a8ad82cc SRT |
1017 | if (err) { |
1018 | /* Check if the card is removed */ | |
1019 | if (mmc_detect_card_removed(card->host)) | |
1020 | return ERR_NOMEDIUM; | |
a01f3ccf | 1021 | return ERR_ABORT; |
a8ad82cc | 1022 | } |
a01f3ccf | 1023 | |
67716327 AH |
1024 | /* Flag ECC errors */ |
1025 | if ((status & R1_CARD_ECC_FAILED) || | |
1026 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || | |
1027 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) | |
1028 | *ecc_err = 1; | |
1029 | ||
c8760069 KY |
1030 | /* Flag General errors */ |
1031 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) | |
1032 | if ((status & R1_ERROR) || | |
1033 | (brq->stop.resp[0] & R1_ERROR)) { | |
1034 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", | |
1035 | req->rq_disk->disk_name, __func__, | |
1036 | brq->stop.resp[0], status); | |
1037 | *gen_err = 1; | |
1038 | } | |
1039 | ||
a01f3ccf RKAL |
1040 | /* |
1041 | * Check the current card state. If it is in some data transfer | |
1042 | * mode, tell it to stop (and hopefully transition back to TRAN.) | |
1043 | */ | |
1044 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || | |
1045 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { | |
bb5cba40 UH |
1046 | err = send_stop(card, |
1047 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), | |
1048 | req, gen_err, &stop_status); | |
1049 | if (err) { | |
a01f3ccf RKAL |
1050 | pr_err("%s: error %d sending stop command\n", |
1051 | req->rq_disk->disk_name, err); | |
bb5cba40 UH |
1052 | /* |
1053 | * If the stop cmd also timed out, the card is probably | |
1054 | * not present, so abort. Other errors are bad news too. | |
1055 | */ | |
a01f3ccf | 1056 | return ERR_ABORT; |
bb5cba40 UH |
1057 | } |
1058 | ||
67716327 AH |
1059 | if (stop_status & R1_CARD_ECC_FAILED) |
1060 | *ecc_err = 1; | |
a01f3ccf RKAL |
1061 | } |
1062 | ||
1063 | /* Check for set block count errors */ | |
1064 | if (brq->sbc.error) | |
1065 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, | |
1066 | prev_cmd_status_valid, status); | |
1067 | ||
1068 | /* Check for r/w command errors */ | |
1069 | if (brq->cmd.error) | |
1070 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, | |
1071 | prev_cmd_status_valid, status); | |
1072 | ||
67716327 AH |
1073 | /* Data errors */ |
1074 | if (!brq->stop.error) | |
1075 | return ERR_CONTINUE; | |
1076 | ||
a01f3ccf | 1077 | /* Now for stop errors. These aren't fatal to the transfer. */ |
5e1344eb | 1078 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
a01f3ccf RKAL |
1079 | req->rq_disk->disk_name, brq->stop.error, |
1080 | brq->cmd.resp[0], status); | |
1081 | ||
1082 | /* | |
1083 | * Subsitute in our own stop status as this will give the error | |
1084 | * state which happened during the execution of the r/w command. | |
1085 | */ | |
1086 | if (stop_status) { | |
1087 | brq->stop.resp[0] = stop_status; | |
1088 | brq->stop.error = 0; | |
1089 | } | |
1090 | return ERR_CONTINUE; | |
1091 | } | |
1092 | ||
67716327 AH |
1093 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
1094 | int type) | |
1095 | { | |
1096 | int err; | |
1097 | ||
1098 | if (md->reset_done & type) | |
1099 | return -EEXIST; | |
1100 | ||
1101 | md->reset_done |= type; | |
1102 | err = mmc_hw_reset(host); | |
1103 | /* Ensure we switch back to the correct partition */ | |
1104 | if (err != -EOPNOTSUPP) { | |
fc95e30b UH |
1105 | struct mmc_blk_data *main_md = |
1106 | dev_get_drvdata(&host->card->dev); | |
67716327 AH |
1107 | int part_err; |
1108 | ||
1109 | main_md->part_curr = main_md->part_type; | |
1110 | part_err = mmc_blk_part_switch(host->card, md); | |
1111 | if (part_err) { | |
1112 | /* | |
1113 | * We have failed to get back into the correct | |
1114 | * partition, so we need to abort the whole request. | |
1115 | */ | |
1116 | return -ENODEV; | |
1117 | } | |
1118 | } | |
1119 | return err; | |
1120 | } | |
1121 | ||
1122 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | |
1123 | { | |
1124 | md->reset_done &= ~type; | |
1125 | } | |
1126 | ||
4e93b9a6 CD |
1127 | int mmc_access_rpmb(struct mmc_queue *mq) |
1128 | { | |
1129 | struct mmc_blk_data *md = mq->data; | |
1130 | /* | |
1131 | * If this is a RPMB partition access, return ture | |
1132 | */ | |
1133 | if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) | |
1134 | return true; | |
1135 | ||
1136 | return false; | |
1137 | } | |
1138 | ||
bd788c96 AH |
1139 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
1140 | { | |
1141 | struct mmc_blk_data *md = mq->data; | |
1142 | struct mmc_card *card = md->queue.card; | |
1143 | unsigned int from, nr, arg; | |
67716327 | 1144 | int err = 0, type = MMC_BLK_DISCARD; |
bd788c96 | 1145 | |
bd788c96 AH |
1146 | if (!mmc_can_erase(card)) { |
1147 | err = -EOPNOTSUPP; | |
1148 | goto out; | |
1149 | } | |
1150 | ||
1151 | from = blk_rq_pos(req); | |
1152 | nr = blk_rq_sectors(req); | |
1153 | ||
b3bf9153 KP |
1154 | if (mmc_can_discard(card)) |
1155 | arg = MMC_DISCARD_ARG; | |
1156 | else if (mmc_can_trim(card)) | |
bd788c96 AH |
1157 | arg = MMC_TRIM_ARG; |
1158 | else | |
1159 | arg = MMC_ERASE_ARG; | |
67716327 | 1160 | retry: |
6a7a6b45 AW |
1161 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1162 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1163 | INAND_CMD38_ARG_EXT_CSD, | |
1164 | arg == MMC_TRIM_ARG ? | |
1165 | INAND_CMD38_ARG_TRIM : | |
1166 | INAND_CMD38_ARG_ERASE, | |
1167 | 0); | |
1168 | if (err) | |
1169 | goto out; | |
1170 | } | |
bd788c96 AH |
1171 | err = mmc_erase(card, from, nr, arg); |
1172 | out: | |
67716327 AH |
1173 | if (err == -EIO && !mmc_blk_reset(md, card->host, type)) |
1174 | goto retry; | |
1175 | if (!err) | |
1176 | mmc_blk_reset_success(md, type); | |
ecf8b5d0 | 1177 | blk_end_request(req, err, blk_rq_bytes(req)); |
bd788c96 | 1178 | |
bd788c96 AH |
1179 | return err ? 0 : 1; |
1180 | } | |
1181 | ||
49804548 AH |
1182 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
1183 | struct request *req) | |
1184 | { | |
1185 | struct mmc_blk_data *md = mq->data; | |
1186 | struct mmc_card *card = md->queue.card; | |
775a9362 | 1187 | unsigned int from, nr, arg; |
67716327 | 1188 | int err = 0, type = MMC_BLK_SECDISCARD; |
49804548 | 1189 | |
775a9362 | 1190 | if (!(mmc_can_secure_erase_trim(card))) { |
49804548 AH |
1191 | err = -EOPNOTSUPP; |
1192 | goto out; | |
1193 | } | |
1194 | ||
28302812 AH |
1195 | from = blk_rq_pos(req); |
1196 | nr = blk_rq_sectors(req); | |
1197 | ||
775a9362 ME |
1198 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
1199 | arg = MMC_SECURE_TRIM1_ARG; | |
1200 | else | |
1201 | arg = MMC_SECURE_ERASE_ARG; | |
d9ddd629 | 1202 | |
67716327 | 1203 | retry: |
6a7a6b45 AW |
1204 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1205 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1206 | INAND_CMD38_ARG_EXT_CSD, | |
1207 | arg == MMC_SECURE_TRIM1_ARG ? | |
1208 | INAND_CMD38_ARG_SECTRIM1 : | |
1209 | INAND_CMD38_ARG_SECERASE, | |
1210 | 0); | |
1211 | if (err) | |
28302812 | 1212 | goto out_retry; |
6a7a6b45 | 1213 | } |
28302812 | 1214 | |
49804548 | 1215 | err = mmc_erase(card, from, nr, arg); |
28302812 AH |
1216 | if (err == -EIO) |
1217 | goto out_retry; | |
1218 | if (err) | |
1219 | goto out; | |
1220 | ||
1221 | if (arg == MMC_SECURE_TRIM1_ARG) { | |
6a7a6b45 AW |
1222 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1223 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1224 | INAND_CMD38_ARG_EXT_CSD, | |
1225 | INAND_CMD38_ARG_SECTRIM2, | |
1226 | 0); | |
1227 | if (err) | |
28302812 | 1228 | goto out_retry; |
6a7a6b45 | 1229 | } |
28302812 | 1230 | |
49804548 | 1231 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
28302812 AH |
1232 | if (err == -EIO) |
1233 | goto out_retry; | |
1234 | if (err) | |
1235 | goto out; | |
6a7a6b45 | 1236 | } |
28302812 | 1237 | |
28302812 AH |
1238 | out_retry: |
1239 | if (err && !mmc_blk_reset(md, card->host, type)) | |
67716327 AH |
1240 | goto retry; |
1241 | if (!err) | |
1242 | mmc_blk_reset_success(md, type); | |
28302812 | 1243 | out: |
ecf8b5d0 | 1244 | blk_end_request(req, err, blk_rq_bytes(req)); |
49804548 | 1245 | |
49804548 AH |
1246 | return err ? 0 : 1; |
1247 | } | |
1248 | ||
f4c5522b AW |
1249 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
1250 | { | |
1251 | struct mmc_blk_data *md = mq->data; | |
881d1c25 SJ |
1252 | struct mmc_card *card = md->queue.card; |
1253 | int ret = 0; | |
1254 | ||
1255 | ret = mmc_flush_cache(card); | |
1256 | if (ret) | |
1257 | ret = -EIO; | |
f4c5522b | 1258 | |
ecf8b5d0 | 1259 | blk_end_request_all(req, ret); |
f4c5522b | 1260 | |
881d1c25 | 1261 | return ret ? 0 : 1; |
f4c5522b AW |
1262 | } |
1263 | ||
1264 | /* | |
1265 | * Reformat current write as a reliable write, supporting | |
1266 | * both legacy and the enhanced reliable write MMC cards. | |
1267 | * In each transfer we'll handle only as much as a single | |
1268 | * reliable write can handle, thus finish the request in | |
1269 | * partial completions. | |
1270 | */ | |
d0c97cfb AW |
1271 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
1272 | struct mmc_card *card, | |
1273 | struct request *req) | |
f4c5522b | 1274 | { |
f4c5522b AW |
1275 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
1276 | /* Legacy mode imposes restrictions on transfers. */ | |
1277 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) | |
1278 | brq->data.blocks = 1; | |
1279 | ||
1280 | if (brq->data.blocks > card->ext_csd.rel_sectors) | |
1281 | brq->data.blocks = card->ext_csd.rel_sectors; | |
1282 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | |
1283 | brq->data.blocks = 1; | |
1284 | } | |
f4c5522b AW |
1285 | } |
1286 | ||
4c2b8f26 RKAL |
1287 | #define CMD_ERRORS \ |
1288 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ | |
1289 | R1_ADDRESS_ERROR | /* Misaligned address */ \ | |
1290 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ | |
1291 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ | |
1292 | R1_CC_ERROR | /* Card controller error */ \ | |
1293 | R1_ERROR) /* General/unknown error */ | |
1294 | ||
ee8a43a5 PF |
1295 | static int mmc_blk_err_check(struct mmc_card *card, |
1296 | struct mmc_async_req *areq) | |
d78d4a8a | 1297 | { |
ee8a43a5 PF |
1298 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
1299 | mmc_active); | |
1300 | struct mmc_blk_request *brq = &mq_mrq->brq; | |
1301 | struct request *req = mq_mrq->req; | |
b8360a49 | 1302 | int need_retune = card->host->need_retune; |
c8760069 | 1303 | int ecc_err = 0, gen_err = 0; |
d78d4a8a PF |
1304 | |
1305 | /* | |
1306 | * sbc.error indicates a problem with the set block count | |
1307 | * command. No data will have been transferred. | |
1308 | * | |
1309 | * cmd.error indicates a problem with the r/w command. No | |
1310 | * data will have been transferred. | |
1311 | * | |
1312 | * stop.error indicates a problem with the stop command. Data | |
1313 | * may have been transferred, or may still be transferring. | |
1314 | */ | |
67716327 AH |
1315 | if (brq->sbc.error || brq->cmd.error || brq->stop.error || |
1316 | brq->data.error) { | |
c8760069 | 1317 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
d78d4a8a PF |
1318 | case ERR_RETRY: |
1319 | return MMC_BLK_RETRY; | |
1320 | case ERR_ABORT: | |
1321 | return MMC_BLK_ABORT; | |
a8ad82cc SRT |
1322 | case ERR_NOMEDIUM: |
1323 | return MMC_BLK_NOMEDIUM; | |
d78d4a8a PF |
1324 | case ERR_CONTINUE: |
1325 | break; | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | /* | |
1330 | * Check for errors relating to the execution of the | |
1331 | * initial command - such as address errors. No data | |
1332 | * has been transferred. | |
1333 | */ | |
1334 | if (brq->cmd.resp[0] & CMD_ERRORS) { | |
1335 | pr_err("%s: r/w command failed, status = %#x\n", | |
1336 | req->rq_disk->disk_name, brq->cmd.resp[0]); | |
1337 | return MMC_BLK_ABORT; | |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * Everything else is either success, or a data error of some | |
1342 | * kind. If it was a write, we may have transitioned to | |
1343 | * program mode, which we have to wait for it to complete. | |
1344 | */ | |
1345 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { | |
c49433fb | 1346 | int err; |
8fee476b | 1347 | |
c8760069 KY |
1348 | /* Check stop command response */ |
1349 | if (brq->stop.resp[0] & R1_ERROR) { | |
1350 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", | |
1351 | req->rq_disk->disk_name, __func__, | |
1352 | brq->stop.resp[0]); | |
1353 | gen_err = 1; | |
1354 | } | |
1355 | ||
95a91298 UH |
1356 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
1357 | &gen_err); | |
c49433fb UH |
1358 | if (err) |
1359 | return MMC_BLK_CMD_ERR; | |
d78d4a8a PF |
1360 | } |
1361 | ||
c8760069 KY |
1362 | /* if general error occurs, retry the write operation. */ |
1363 | if (gen_err) { | |
1364 | pr_warn("%s: retrying write for general error\n", | |
1365 | req->rq_disk->disk_name); | |
1366 | return MMC_BLK_RETRY; | |
1367 | } | |
1368 | ||
d78d4a8a | 1369 | if (brq->data.error) { |
b8360a49 | 1370 | if (need_retune && !brq->retune_retry_done) { |
09faf61d RK |
1371 | pr_debug("%s: retrying because a re-tune was needed\n", |
1372 | req->rq_disk->disk_name); | |
b8360a49 AH |
1373 | brq->retune_retry_done = 1; |
1374 | return MMC_BLK_RETRY; | |
1375 | } | |
d78d4a8a PF |
1376 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
1377 | req->rq_disk->disk_name, brq->data.error, | |
1378 | (unsigned)blk_rq_pos(req), | |
1379 | (unsigned)blk_rq_sectors(req), | |
1380 | brq->cmd.resp[0], brq->stop.resp[0]); | |
1381 | ||
1382 | if (rq_data_dir(req) == READ) { | |
67716327 AH |
1383 | if (ecc_err) |
1384 | return MMC_BLK_ECC_ERR; | |
d78d4a8a PF |
1385 | return MMC_BLK_DATA_ERR; |
1386 | } else { | |
1387 | return MMC_BLK_CMD_ERR; | |
1388 | } | |
1389 | } | |
1390 | ||
67716327 AH |
1391 | if (!brq->data.bytes_xfered) |
1392 | return MMC_BLK_RETRY; | |
d78d4a8a | 1393 | |
ce39f9d1 SJ |
1394 | if (mmc_packed_cmd(mq_mrq->cmd_type)) { |
1395 | if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered)) | |
1396 | return MMC_BLK_PARTIAL; | |
1397 | else | |
1398 | return MMC_BLK_SUCCESS; | |
1399 | } | |
1400 | ||
67716327 AH |
1401 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
1402 | return MMC_BLK_PARTIAL; | |
1403 | ||
1404 | return MMC_BLK_SUCCESS; | |
d78d4a8a PF |
1405 | } |
1406 | ||
ce39f9d1 SJ |
1407 | static int mmc_blk_packed_err_check(struct mmc_card *card, |
1408 | struct mmc_async_req *areq) | |
1409 | { | |
1410 | struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req, | |
1411 | mmc_active); | |
1412 | struct request *req = mq_rq->req; | |
1413 | struct mmc_packed *packed = mq_rq->packed; | |
1414 | int err, check, status; | |
1415 | u8 *ext_csd; | |
1416 | ||
1417 | BUG_ON(!packed); | |
1418 | ||
1419 | packed->retries--; | |
1420 | check = mmc_blk_err_check(card, areq); | |
1421 | err = get_card_status(card, &status, 0); | |
1422 | if (err) { | |
1423 | pr_err("%s: error %d sending status command\n", | |
1424 | req->rq_disk->disk_name, err); | |
1425 | return MMC_BLK_ABORT; | |
1426 | } | |
1427 | ||
1428 | if (status & R1_EXCEPTION_EVENT) { | |
86817ffb | 1429 | err = mmc_get_ext_csd(card, &ext_csd); |
ce39f9d1 SJ |
1430 | if (err) { |
1431 | pr_err("%s: error %d sending ext_csd\n", | |
1432 | req->rq_disk->disk_name, err); | |
86817ffb | 1433 | return MMC_BLK_ABORT; |
ce39f9d1 SJ |
1434 | } |
1435 | ||
1436 | if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] & | |
1437 | EXT_CSD_PACKED_FAILURE) && | |
1438 | (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & | |
1439 | EXT_CSD_PACKED_GENERIC_ERROR)) { | |
1440 | if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & | |
1441 | EXT_CSD_PACKED_INDEXED_ERROR) { | |
1442 | packed->idx_failure = | |
1443 | ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1; | |
1444 | check = MMC_BLK_PARTIAL; | |
1445 | } | |
1446 | pr_err("%s: packed cmd failed, nr %u, sectors %u, " | |
1447 | "failure index: %d\n", | |
1448 | req->rq_disk->disk_name, packed->nr_entries, | |
1449 | packed->blocks, packed->idx_failure); | |
1450 | } | |
ce39f9d1 SJ |
1451 | kfree(ext_csd); |
1452 | } | |
1453 | ||
1454 | return check; | |
1455 | } | |
1456 | ||
54d49d77 PF |
1457 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
1458 | struct mmc_card *card, | |
1459 | int disable_multi, | |
1460 | struct mmc_queue *mq) | |
1da177e4 | 1461 | { |
54d49d77 PF |
1462 | u32 readcmd, writecmd; |
1463 | struct mmc_blk_request *brq = &mqrq->brq; | |
1464 | struct request *req = mqrq->req; | |
1da177e4 | 1465 | struct mmc_blk_data *md = mq->data; |
4265900e | 1466 | bool do_data_tag; |
1da177e4 | 1467 | |
f4c5522b AW |
1468 | /* |
1469 | * Reliable writes are used to implement Forced Unit Access and | |
d3df0465 | 1470 | * are supported only on MMCs. |
f4c5522b | 1471 | */ |
d3df0465 | 1472 | bool do_rel_wr = (req->cmd_flags & REQ_FUA) && |
f4c5522b | 1473 | (rq_data_dir(req) == WRITE) && |
d0c97cfb | 1474 | (md->flags & MMC_BLK_REL_WR); |
f4c5522b | 1475 | |
54d49d77 PF |
1476 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
1477 | brq->mrq.cmd = &brq->cmd; | |
1478 | brq->mrq.data = &brq->data; | |
1da177e4 | 1479 | |
54d49d77 PF |
1480 | brq->cmd.arg = blk_rq_pos(req); |
1481 | if (!mmc_card_blockaddr(card)) | |
1482 | brq->cmd.arg <<= 9; | |
1483 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1484 | brq->data.blksz = 512; | |
1485 | brq->stop.opcode = MMC_STOP_TRANSMISSION; | |
1486 | brq->stop.arg = 0; | |
54d49d77 | 1487 | brq->data.blocks = blk_rq_sectors(req); |
6a79e391 | 1488 | |
54d49d77 PF |
1489 | /* |
1490 | * The block layer doesn't support all sector count | |
1491 | * restrictions, so we need to be prepared for too big | |
1492 | * requests. | |
1493 | */ | |
1494 | if (brq->data.blocks > card->host->max_blk_count) | |
1495 | brq->data.blocks = card->host->max_blk_count; | |
1da177e4 | 1496 | |
2bf22b39 PW |
1497 | if (brq->data.blocks > 1) { |
1498 | /* | |
1499 | * After a read error, we redo the request one sector | |
1500 | * at a time in order to accurately determine which | |
1501 | * sectors can be read successfully. | |
1502 | */ | |
1503 | if (disable_multi) | |
1504 | brq->data.blocks = 1; | |
1505 | ||
2e47e842 KM |
1506 | /* |
1507 | * Some controllers have HW issues while operating | |
1508 | * in multiple I/O mode | |
1509 | */ | |
1510 | if (card->host->ops->multi_io_quirk) | |
1511 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | |
1512 | (rq_data_dir(req) == READ) ? | |
1513 | MMC_DATA_READ : MMC_DATA_WRITE, | |
1514 | brq->data.blocks); | |
2bf22b39 | 1515 | } |
d0c97cfb | 1516 | |
54d49d77 PF |
1517 | if (brq->data.blocks > 1 || do_rel_wr) { |
1518 | /* SPI multiblock writes terminate using a special | |
1519 | * token, not a STOP_TRANSMISSION request. | |
d0c97cfb | 1520 | */ |
54d49d77 PF |
1521 | if (!mmc_host_is_spi(card->host) || |
1522 | rq_data_dir(req) == READ) | |
1523 | brq->mrq.stop = &brq->stop; | |
1524 | readcmd = MMC_READ_MULTIPLE_BLOCK; | |
1525 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | |
1526 | } else { | |
1527 | brq->mrq.stop = NULL; | |
1528 | readcmd = MMC_READ_SINGLE_BLOCK; | |
1529 | writecmd = MMC_WRITE_BLOCK; | |
1530 | } | |
1531 | if (rq_data_dir(req) == READ) { | |
1532 | brq->cmd.opcode = readcmd; | |
f53f1102 | 1533 | brq->data.flags = MMC_DATA_READ; |
bcc3e172 UH |
1534 | if (brq->mrq.stop) |
1535 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | | |
1536 | MMC_CMD_AC; | |
54d49d77 PF |
1537 | } else { |
1538 | brq->cmd.opcode = writecmd; | |
f53f1102 | 1539 | brq->data.flags = MMC_DATA_WRITE; |
bcc3e172 UH |
1540 | if (brq->mrq.stop) |
1541 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | | |
1542 | MMC_CMD_AC; | |
54d49d77 | 1543 | } |
d0c97cfb | 1544 | |
54d49d77 PF |
1545 | if (do_rel_wr) |
1546 | mmc_apply_rel_rw(brq, card, req); | |
f4c5522b | 1547 | |
4265900e SD |
1548 | /* |
1549 | * Data tag is used only during writing meta data to speed | |
1550 | * up write and any subsequent read of this meta data | |
1551 | */ | |
1552 | do_data_tag = (card->ext_csd.data_tag_unit_size) && | |
1553 | (req->cmd_flags & REQ_META) && | |
1554 | (rq_data_dir(req) == WRITE) && | |
1555 | ((brq->data.blocks * brq->data.blksz) >= | |
1556 | card->ext_csd.data_tag_unit_size); | |
1557 | ||
54d49d77 PF |
1558 | /* |
1559 | * Pre-defined multi-block transfers are preferable to | |
1560 | * open ended-ones (and necessary for reliable writes). | |
1561 | * However, it is not sufficient to just send CMD23, | |
1562 | * and avoid the final CMD12, as on an error condition | |
1563 | * CMD12 (stop) needs to be sent anyway. This, coupled | |
1564 | * with Auto-CMD23 enhancements provided by some | |
1565 | * hosts, means that the complexity of dealing | |
1566 | * with this is best left to the host. If CMD23 is | |
1567 | * supported by card and host, we'll fill sbc in and let | |
1568 | * the host deal with handling it correctly. This means | |
1569 | * that for hosts that don't expose MMC_CAP_CMD23, no | |
1570 | * change of behavior will be observed. | |
1571 | * | |
1572 | * N.B: Some MMC cards experience perf degradation. | |
1573 | * We'll avoid using CMD23-bounded multiblock writes for | |
1574 | * these, while retaining features like reliable writes. | |
1575 | */ | |
4265900e SD |
1576 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
1577 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | |
1578 | do_data_tag)) { | |
54d49d77 PF |
1579 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
1580 | brq->sbc.arg = brq->data.blocks | | |
4265900e SD |
1581 | (do_rel_wr ? (1 << 31) : 0) | |
1582 | (do_data_tag ? (1 << 29) : 0); | |
54d49d77 PF |
1583 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1584 | brq->mrq.sbc = &brq->sbc; | |
1585 | } | |
98ccf149 | 1586 | |
54d49d77 PF |
1587 | mmc_set_data_timeout(&brq->data, card); |
1588 | ||
1589 | brq->data.sg = mqrq->sg; | |
1590 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1591 | ||
1592 | /* | |
1593 | * Adjust the sg list so it is the same size as the | |
1594 | * request. | |
1595 | */ | |
1596 | if (brq->data.blocks != blk_rq_sectors(req)) { | |
1597 | int i, data_size = brq->data.blocks << 9; | |
1598 | struct scatterlist *sg; | |
1599 | ||
1600 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | |
1601 | data_size -= sg->length; | |
1602 | if (data_size <= 0) { | |
1603 | sg->length += data_size; | |
1604 | i++; | |
1605 | break; | |
6a79e391 | 1606 | } |
6a79e391 | 1607 | } |
54d49d77 PF |
1608 | brq->data.sg_len = i; |
1609 | } | |
1610 | ||
ee8a43a5 PF |
1611 | mqrq->mmc_active.mrq = &brq->mrq; |
1612 | mqrq->mmc_active.err_check = mmc_blk_err_check; | |
1613 | ||
54d49d77 PF |
1614 | mmc_queue_bounce_pre(mqrq); |
1615 | } | |
6a79e391 | 1616 | |
ce39f9d1 SJ |
1617 | static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q, |
1618 | struct mmc_card *card) | |
1619 | { | |
1620 | unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512; | |
1621 | unsigned int max_seg_sz = queue_max_segment_size(q); | |
1622 | unsigned int len, nr_segs = 0; | |
1623 | ||
1624 | do { | |
1625 | len = min(hdr_sz, max_seg_sz); | |
1626 | hdr_sz -= len; | |
1627 | nr_segs++; | |
1628 | } while (hdr_sz); | |
1629 | ||
1630 | return nr_segs; | |
1631 | } | |
1632 | ||
1633 | static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req) | |
1634 | { | |
1635 | struct request_queue *q = mq->queue; | |
1636 | struct mmc_card *card = mq->card; | |
1637 | struct request *cur = req, *next = NULL; | |
1638 | struct mmc_blk_data *md = mq->data; | |
1639 | struct mmc_queue_req *mqrq = mq->mqrq_cur; | |
1640 | bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN; | |
1641 | unsigned int req_sectors = 0, phys_segments = 0; | |
1642 | unsigned int max_blk_count, max_phys_segs; | |
1643 | bool put_back = true; | |
1644 | u8 max_packed_rw = 0; | |
1645 | u8 reqs = 0; | |
1646 | ||
1647 | if (!(md->flags & MMC_BLK_PACKED_CMD)) | |
1648 | goto no_packed; | |
1649 | ||
1650 | if ((rq_data_dir(cur) == WRITE) && | |
1651 | mmc_host_packed_wr(card->host)) | |
1652 | max_packed_rw = card->ext_csd.max_packed_writes; | |
1653 | ||
1654 | if (max_packed_rw == 0) | |
1655 | goto no_packed; | |
1656 | ||
1657 | if (mmc_req_rel_wr(cur) && | |
1658 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) | |
1659 | goto no_packed; | |
1660 | ||
1661 | if (mmc_large_sector(card) && | |
1662 | !IS_ALIGNED(blk_rq_sectors(cur), 8)) | |
1663 | goto no_packed; | |
1664 | ||
1665 | mmc_blk_clear_packed(mqrq); | |
1666 | ||
1667 | max_blk_count = min(card->host->max_blk_count, | |
1668 | card->host->max_req_size >> 9); | |
1669 | if (unlikely(max_blk_count > 0xffff)) | |
1670 | max_blk_count = 0xffff; | |
1671 | ||
1672 | max_phys_segs = queue_max_segments(q); | |
1673 | req_sectors += blk_rq_sectors(cur); | |
1674 | phys_segments += cur->nr_phys_segments; | |
1675 | ||
1676 | if (rq_data_dir(cur) == WRITE) { | |
1677 | req_sectors += mmc_large_sector(card) ? 8 : 1; | |
1678 | phys_segments += mmc_calc_packed_hdr_segs(q, card); | |
1679 | } | |
1680 | ||
1681 | do { | |
1682 | if (reqs >= max_packed_rw - 1) { | |
1683 | put_back = false; | |
1684 | break; | |
1685 | } | |
1686 | ||
1687 | spin_lock_irq(q->queue_lock); | |
1688 | next = blk_fetch_request(q); | |
1689 | spin_unlock_irq(q->queue_lock); | |
1690 | if (!next) { | |
1691 | put_back = false; | |
1692 | break; | |
1693 | } | |
1694 | ||
1695 | if (mmc_large_sector(card) && | |
1696 | !IS_ALIGNED(blk_rq_sectors(next), 8)) | |
1697 | break; | |
1698 | ||
1699 | if (next->cmd_flags & REQ_DISCARD || | |
1700 | next->cmd_flags & REQ_FLUSH) | |
1701 | break; | |
1702 | ||
1703 | if (rq_data_dir(cur) != rq_data_dir(next)) | |
1704 | break; | |
1705 | ||
1706 | if (mmc_req_rel_wr(next) && | |
1707 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) | |
1708 | break; | |
1709 | ||
1710 | req_sectors += blk_rq_sectors(next); | |
1711 | if (req_sectors > max_blk_count) | |
1712 | break; | |
1713 | ||
1714 | phys_segments += next->nr_phys_segments; | |
1715 | if (phys_segments > max_phys_segs) | |
1716 | break; | |
1717 | ||
1718 | list_add_tail(&next->queuelist, &mqrq->packed->list); | |
1719 | cur = next; | |
1720 | reqs++; | |
1721 | } while (1); | |
1722 | ||
1723 | if (put_back) { | |
1724 | spin_lock_irq(q->queue_lock); | |
1725 | blk_requeue_request(q, next); | |
1726 | spin_unlock_irq(q->queue_lock); | |
1727 | } | |
1728 | ||
1729 | if (reqs > 0) { | |
1730 | list_add(&req->queuelist, &mqrq->packed->list); | |
1731 | mqrq->packed->nr_entries = ++reqs; | |
1732 | mqrq->packed->retries = reqs; | |
1733 | return reqs; | |
1734 | } | |
1735 | ||
1736 | no_packed: | |
1737 | mqrq->cmd_type = MMC_PACKED_NONE; | |
1738 | return 0; | |
1739 | } | |
1740 | ||
1741 | static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq, | |
1742 | struct mmc_card *card, | |
1743 | struct mmc_queue *mq) | |
1744 | { | |
1745 | struct mmc_blk_request *brq = &mqrq->brq; | |
1746 | struct request *req = mqrq->req; | |
1747 | struct request *prq; | |
1748 | struct mmc_blk_data *md = mq->data; | |
1749 | struct mmc_packed *packed = mqrq->packed; | |
1750 | bool do_rel_wr, do_data_tag; | |
1751 | u32 *packed_cmd_hdr; | |
1752 | u8 hdr_blocks; | |
1753 | u8 i = 1; | |
1754 | ||
1755 | BUG_ON(!packed); | |
1756 | ||
1757 | mqrq->cmd_type = MMC_PACKED_WRITE; | |
1758 | packed->blocks = 0; | |
1759 | packed->idx_failure = MMC_PACKED_NR_IDX; | |
1760 | ||
1761 | packed_cmd_hdr = packed->cmd_hdr; | |
1762 | memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr)); | |
1763 | packed_cmd_hdr[0] = (packed->nr_entries << 16) | | |
1764 | (PACKED_CMD_WR << 8) | PACKED_CMD_VER; | |
1765 | hdr_blocks = mmc_large_sector(card) ? 8 : 1; | |
1766 | ||
1767 | /* | |
1768 | * Argument for each entry of packed group | |
1769 | */ | |
1770 | list_for_each_entry(prq, &packed->list, queuelist) { | |
1771 | do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR); | |
1772 | do_data_tag = (card->ext_csd.data_tag_unit_size) && | |
1773 | (prq->cmd_flags & REQ_META) && | |
1774 | (rq_data_dir(prq) == WRITE) && | |
1775 | ((brq->data.blocks * brq->data.blksz) >= | |
1776 | card->ext_csd.data_tag_unit_size); | |
1777 | /* Argument of CMD23 */ | |
1778 | packed_cmd_hdr[(i * 2)] = | |
1779 | (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) | | |
1780 | (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) | | |
1781 | blk_rq_sectors(prq); | |
1782 | /* Argument of CMD18 or CMD25 */ | |
1783 | packed_cmd_hdr[((i * 2)) + 1] = | |
1784 | mmc_card_blockaddr(card) ? | |
1785 | blk_rq_pos(prq) : blk_rq_pos(prq) << 9; | |
1786 | packed->blocks += blk_rq_sectors(prq); | |
1787 | i++; | |
1788 | } | |
1789 | ||
1790 | memset(brq, 0, sizeof(struct mmc_blk_request)); | |
1791 | brq->mrq.cmd = &brq->cmd; | |
1792 | brq->mrq.data = &brq->data; | |
1793 | brq->mrq.sbc = &brq->sbc; | |
1794 | brq->mrq.stop = &brq->stop; | |
1795 | ||
1796 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; | |
1797 | brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks); | |
1798 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; | |
1799 | ||
1800 | brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; | |
1801 | brq->cmd.arg = blk_rq_pos(req); | |
1802 | if (!mmc_card_blockaddr(card)) | |
1803 | brq->cmd.arg <<= 9; | |
1804 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1805 | ||
1806 | brq->data.blksz = 512; | |
1807 | brq->data.blocks = packed->blocks + hdr_blocks; | |
f53f1102 | 1808 | brq->data.flags = MMC_DATA_WRITE; |
ce39f9d1 SJ |
1809 | |
1810 | brq->stop.opcode = MMC_STOP_TRANSMISSION; | |
1811 | brq->stop.arg = 0; | |
1812 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1813 | ||
1814 | mmc_set_data_timeout(&brq->data, card); | |
1815 | ||
1816 | brq->data.sg = mqrq->sg; | |
1817 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1818 | ||
1819 | mqrq->mmc_active.mrq = &brq->mrq; | |
1820 | mqrq->mmc_active.err_check = mmc_blk_packed_err_check; | |
1821 | ||
1822 | mmc_queue_bounce_pre(mqrq); | |
1823 | } | |
1824 | ||
67716327 AH |
1825 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
1826 | struct mmc_blk_request *brq, struct request *req, | |
1827 | int ret) | |
1828 | { | |
ce39f9d1 SJ |
1829 | struct mmc_queue_req *mq_rq; |
1830 | mq_rq = container_of(brq, struct mmc_queue_req, brq); | |
1831 | ||
67716327 AH |
1832 | /* |
1833 | * If this is an SD card and we're writing, we can first | |
1834 | * mark the known good sectors as ok. | |
1835 | * | |
1836 | * If the card is not SD, we can still ok written sectors | |
1837 | * as reported by the controller (which might be less than | |
1838 | * the real number of written sectors, but never more). | |
1839 | */ | |
1840 | if (mmc_card_sd(card)) { | |
1841 | u32 blocks; | |
1842 | ||
1843 | blocks = mmc_sd_num_wr_blocks(card); | |
1844 | if (blocks != (u32)-1) { | |
ecf8b5d0 | 1845 | ret = blk_end_request(req, 0, blocks << 9); |
67716327 AH |
1846 | } |
1847 | } else { | |
ce39f9d1 SJ |
1848 | if (!mmc_packed_cmd(mq_rq->cmd_type)) |
1849 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); | |
67716327 AH |
1850 | } |
1851 | return ret; | |
1852 | } | |
1853 | ||
ce39f9d1 SJ |
1854 | static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq) |
1855 | { | |
1856 | struct request *prq; | |
1857 | struct mmc_packed *packed = mq_rq->packed; | |
1858 | int idx = packed->idx_failure, i = 0; | |
1859 | int ret = 0; | |
1860 | ||
1861 | BUG_ON(!packed); | |
1862 | ||
1863 | while (!list_empty(&packed->list)) { | |
1864 | prq = list_entry_rq(packed->list.next); | |
1865 | if (idx == i) { | |
1866 | /* retry from error index */ | |
1867 | packed->nr_entries -= idx; | |
1868 | mq_rq->req = prq; | |
1869 | ret = 1; | |
1870 | ||
1871 | if (packed->nr_entries == MMC_PACKED_NR_SINGLE) { | |
1872 | list_del_init(&prq->queuelist); | |
1873 | mmc_blk_clear_packed(mq_rq); | |
1874 | } | |
1875 | return ret; | |
1876 | } | |
1877 | list_del_init(&prq->queuelist); | |
1878 | blk_end_request(prq, 0, blk_rq_bytes(prq)); | |
1879 | i++; | |
1880 | } | |
1881 | ||
1882 | mmc_blk_clear_packed(mq_rq); | |
1883 | return ret; | |
1884 | } | |
1885 | ||
1886 | static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq) | |
1887 | { | |
1888 | struct request *prq; | |
1889 | struct mmc_packed *packed = mq_rq->packed; | |
1890 | ||
1891 | BUG_ON(!packed); | |
1892 | ||
1893 | while (!list_empty(&packed->list)) { | |
1894 | prq = list_entry_rq(packed->list.next); | |
1895 | list_del_init(&prq->queuelist); | |
1896 | blk_end_request(prq, -EIO, blk_rq_bytes(prq)); | |
1897 | } | |
1898 | ||
1899 | mmc_blk_clear_packed(mq_rq); | |
1900 | } | |
1901 | ||
1902 | static void mmc_blk_revert_packed_req(struct mmc_queue *mq, | |
1903 | struct mmc_queue_req *mq_rq) | |
1904 | { | |
1905 | struct request *prq; | |
1906 | struct request_queue *q = mq->queue; | |
1907 | struct mmc_packed *packed = mq_rq->packed; | |
1908 | ||
1909 | BUG_ON(!packed); | |
1910 | ||
1911 | while (!list_empty(&packed->list)) { | |
1912 | prq = list_entry_rq(packed->list.prev); | |
1913 | if (prq->queuelist.prev != &packed->list) { | |
1914 | list_del_init(&prq->queuelist); | |
1915 | spin_lock_irq(q->queue_lock); | |
1916 | blk_requeue_request(mq->queue, prq); | |
1917 | spin_unlock_irq(q->queue_lock); | |
1918 | } else { | |
1919 | list_del_init(&prq->queuelist); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | mmc_blk_clear_packed(mq_rq); | |
1924 | } | |
1925 | ||
ee8a43a5 | 1926 | static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc) |
54d49d77 PF |
1927 | { |
1928 | struct mmc_blk_data *md = mq->data; | |
1929 | struct mmc_card *card = md->queue.card; | |
1930 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; | |
b8360a49 | 1931 | int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0; |
d78d4a8a | 1932 | enum mmc_blk_status status; |
ee8a43a5 | 1933 | struct mmc_queue_req *mq_rq; |
a5075eb9 | 1934 | struct request *req = rqc; |
ee8a43a5 | 1935 | struct mmc_async_req *areq; |
ce39f9d1 SJ |
1936 | const u8 packed_nr = 2; |
1937 | u8 reqs = 0; | |
1da177e4 | 1938 | |
ee8a43a5 PF |
1939 | if (!rqc && !mq->mqrq_prev->req) |
1940 | return 0; | |
98ccf149 | 1941 | |
ce39f9d1 SJ |
1942 | if (rqc) |
1943 | reqs = mmc_blk_prep_packed_list(mq, rqc); | |
1944 | ||
ee8a43a5 PF |
1945 | do { |
1946 | if (rqc) { | |
a5075eb9 SD |
1947 | /* |
1948 | * When 4KB native sector is enabled, only 8 blocks | |
1949 | * multiple read or write is allowed | |
1950 | */ | |
1951 | if ((brq->data.blocks & 0x07) && | |
1952 | (card->ext_csd.data_sector_size == 4096)) { | |
1953 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", | |
1954 | req->rq_disk->disk_name); | |
ce39f9d1 | 1955 | mq_rq = mq->mqrq_cur; |
a5075eb9 SD |
1956 | goto cmd_abort; |
1957 | } | |
ce39f9d1 SJ |
1958 | |
1959 | if (reqs >= packed_nr) | |
1960 | mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur, | |
1961 | card, mq); | |
1962 | else | |
1963 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); | |
ee8a43a5 PF |
1964 | areq = &mq->mqrq_cur->mmc_active; |
1965 | } else | |
1966 | areq = NULL; | |
1967 | areq = mmc_start_req(card->host, areq, (int *) &status); | |
2220eedf KD |
1968 | if (!areq) { |
1969 | if (status == MMC_BLK_NEW_REQUEST) | |
1970 | mq->flags |= MMC_QUEUE_NEW_REQUEST; | |
ee8a43a5 | 1971 | return 0; |
2220eedf | 1972 | } |
ee8a43a5 PF |
1973 | |
1974 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); | |
1975 | brq = &mq_rq->brq; | |
1976 | req = mq_rq->req; | |
67716327 | 1977 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
ee8a43a5 | 1978 | mmc_queue_bounce_post(mq_rq); |
98ccf149 | 1979 | |
d78d4a8a PF |
1980 | switch (status) { |
1981 | case MMC_BLK_SUCCESS: | |
1982 | case MMC_BLK_PARTIAL: | |
1983 | /* | |
1984 | * A block was successfully transferred. | |
1985 | */ | |
67716327 | 1986 | mmc_blk_reset_success(md, type); |
ce39f9d1 SJ |
1987 | |
1988 | if (mmc_packed_cmd(mq_rq->cmd_type)) { | |
1989 | ret = mmc_blk_end_packed_req(mq_rq); | |
1990 | break; | |
1991 | } else { | |
1992 | ret = blk_end_request(req, 0, | |
d78d4a8a | 1993 | brq->data.bytes_xfered); |
ce39f9d1 SJ |
1994 | } |
1995 | ||
67716327 AH |
1996 | /* |
1997 | * If the blk_end_request function returns non-zero even | |
1998 | * though all data has been transferred and no errors | |
1999 | * were returned by the host controller, it's a bug. | |
2000 | */ | |
ee8a43a5 | 2001 | if (status == MMC_BLK_SUCCESS && ret) { |
a3c76eb9 | 2002 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
ee8a43a5 PF |
2003 | __func__, blk_rq_bytes(req), |
2004 | brq->data.bytes_xfered); | |
2005 | rqc = NULL; | |
2006 | goto cmd_abort; | |
2007 | } | |
d78d4a8a PF |
2008 | break; |
2009 | case MMC_BLK_CMD_ERR: | |
67716327 | 2010 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
29535f7b DW |
2011 | if (mmc_blk_reset(md, card->host, type)) |
2012 | goto cmd_abort; | |
2013 | if (!ret) | |
2014 | goto start_new_req; | |
2015 | break; | |
d78d4a8a | 2016 | case MMC_BLK_RETRY: |
b8360a49 | 2017 | retune_retry_done = brq->retune_retry_done; |
d78d4a8a | 2018 | if (retry++ < 5) |
a01f3ccf | 2019 | break; |
67716327 | 2020 | /* Fall through */ |
d78d4a8a | 2021 | case MMC_BLK_ABORT: |
67716327 AH |
2022 | if (!mmc_blk_reset(md, card->host, type)) |
2023 | break; | |
4c2b8f26 | 2024 | goto cmd_abort; |
67716327 AH |
2025 | case MMC_BLK_DATA_ERR: { |
2026 | int err; | |
2027 | ||
2028 | err = mmc_blk_reset(md, card->host, type); | |
2029 | if (!err) | |
2030 | break; | |
ce39f9d1 SJ |
2031 | if (err == -ENODEV || |
2032 | mmc_packed_cmd(mq_rq->cmd_type)) | |
67716327 AH |
2033 | goto cmd_abort; |
2034 | /* Fall through */ | |
2035 | } | |
2036 | case MMC_BLK_ECC_ERR: | |
2037 | if (brq->data.blocks > 1) { | |
2038 | /* Redo read one sector at a time */ | |
6606110d JP |
2039 | pr_warn("%s: retrying using single block read\n", |
2040 | req->rq_disk->disk_name); | |
67716327 AH |
2041 | disable_multi = 1; |
2042 | break; | |
2043 | } | |
d78d4a8a PF |
2044 | /* |
2045 | * After an error, we redo I/O one sector at a | |
2046 | * time, so we only reach here after trying to | |
2047 | * read a single sector. | |
2048 | */ | |
ecf8b5d0 | 2049 | ret = blk_end_request(req, -EIO, |
d78d4a8a | 2050 | brq->data.blksz); |
ee8a43a5 PF |
2051 | if (!ret) |
2052 | goto start_new_req; | |
d78d4a8a | 2053 | break; |
a8ad82cc SRT |
2054 | case MMC_BLK_NOMEDIUM: |
2055 | goto cmd_abort; | |
2220eedf KD |
2056 | default: |
2057 | pr_err("%s: Unhandled return value (%d)", | |
2058 | req->rq_disk->disk_name, status); | |
2059 | goto cmd_abort; | |
4c2b8f26 RKAL |
2060 | } |
2061 | ||
ee8a43a5 | 2062 | if (ret) { |
ce39f9d1 SJ |
2063 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
2064 | if (!mq_rq->packed->retries) | |
2065 | goto cmd_abort; | |
2066 | mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq); | |
2067 | mmc_start_req(card->host, | |
2068 | &mq_rq->mmc_active, NULL); | |
2069 | } else { | |
2070 | ||
2071 | /* | |
2072 | * In case of a incomplete request | |
2073 | * prepare it again and resend. | |
2074 | */ | |
2075 | mmc_blk_rw_rq_prep(mq_rq, card, | |
2076 | disable_multi, mq); | |
2077 | mmc_start_req(card->host, | |
2078 | &mq_rq->mmc_active, NULL); | |
2079 | } | |
b8360a49 | 2080 | mq_rq->brq.retune_retry_done = retune_retry_done; |
ee8a43a5 | 2081 | } |
1da177e4 LT |
2082 | } while (ret); |
2083 | ||
1da177e4 LT |
2084 | return 1; |
2085 | ||
a01f3ccf | 2086 | cmd_abort: |
ce39f9d1 SJ |
2087 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
2088 | mmc_blk_abort_packed_req(mq_rq); | |
2089 | } else { | |
2090 | if (mmc_card_removed(card)) | |
2091 | req->cmd_flags |= REQ_QUIET; | |
2092 | while (ret) | |
2093 | ret = blk_end_request(req, -EIO, | |
2094 | blk_rq_cur_bytes(req)); | |
2095 | } | |
1da177e4 | 2096 | |
ee8a43a5 PF |
2097 | start_new_req: |
2098 | if (rqc) { | |
7a81902f SJ |
2099 | if (mmc_card_removed(card)) { |
2100 | rqc->cmd_flags |= REQ_QUIET; | |
2101 | blk_end_request_all(rqc, -EIO); | |
2102 | } else { | |
ce39f9d1 SJ |
2103 | /* |
2104 | * If current request is packed, it needs to put back. | |
2105 | */ | |
2106 | if (mmc_packed_cmd(mq->mqrq_cur->cmd_type)) | |
2107 | mmc_blk_revert_packed_req(mq, mq->mqrq_cur); | |
2108 | ||
7a81902f SJ |
2109 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
2110 | mmc_start_req(card->host, | |
2111 | &mq->mqrq_cur->mmc_active, NULL); | |
2112 | } | |
ee8a43a5 PF |
2113 | } |
2114 | ||
1da177e4 LT |
2115 | return 0; |
2116 | } | |
2117 | ||
bd788c96 AH |
2118 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
2119 | { | |
1a258db6 AW |
2120 | int ret; |
2121 | struct mmc_blk_data *md = mq->data; | |
2122 | struct mmc_card *card = md->queue.card; | |
2220eedf KD |
2123 | struct mmc_host *host = card->host; |
2124 | unsigned long flags; | |
f662ae48 | 2125 | unsigned int cmd_flags = req ? req->cmd_flags : 0; |
1a258db6 | 2126 | |
ee8a43a5 PF |
2127 | if (req && !mq->mqrq_prev->req) |
2128 | /* claim host only for the first request */ | |
e94cfef6 | 2129 | mmc_get_card(card); |
ee8a43a5 | 2130 | |
371a689f AW |
2131 | ret = mmc_blk_part_switch(card, md); |
2132 | if (ret) { | |
0d7d85ca | 2133 | if (req) { |
ecf8b5d0 | 2134 | blk_end_request_all(req, -EIO); |
0d7d85ca | 2135 | } |
371a689f AW |
2136 | ret = 0; |
2137 | goto out; | |
2138 | } | |
1a258db6 | 2139 | |
2220eedf | 2140 | mq->flags &= ~MMC_QUEUE_NEW_REQUEST; |
f662ae48 | 2141 | if (cmd_flags & REQ_DISCARD) { |
ee8a43a5 PF |
2142 | /* complete ongoing async transfer before issuing discard */ |
2143 | if (card->host->areq) | |
2144 | mmc_blk_issue_rw_rq(mq, NULL); | |
5204d00f | 2145 | if (req->cmd_flags & REQ_SECURE) |
1a258db6 | 2146 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
49804548 | 2147 | else |
1a258db6 | 2148 | ret = mmc_blk_issue_discard_rq(mq, req); |
f662ae48 | 2149 | } else if (cmd_flags & REQ_FLUSH) { |
393f9a08 JC |
2150 | /* complete ongoing async transfer before issuing flush */ |
2151 | if (card->host->areq) | |
2152 | mmc_blk_issue_rw_rq(mq, NULL); | |
1a258db6 | 2153 | ret = mmc_blk_issue_flush(mq, req); |
49804548 | 2154 | } else { |
2220eedf KD |
2155 | if (!req && host->areq) { |
2156 | spin_lock_irqsave(&host->context_info.lock, flags); | |
2157 | host->context_info.is_waiting_last_req = true; | |
2158 | spin_unlock_irqrestore(&host->context_info.lock, flags); | |
2159 | } | |
1a258db6 | 2160 | ret = mmc_blk_issue_rw_rq(mq, req); |
49804548 | 2161 | } |
1a258db6 | 2162 | |
371a689f | 2163 | out: |
ef3a69c7 | 2164 | if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || |
f662ae48 | 2165 | (cmd_flags & MMC_REQ_SPECIAL_MASK)) |
ef3a69c7 SJ |
2166 | /* |
2167 | * Release host when there are no more requests | |
2168 | * and after special request(discard, flush) is done. | |
2169 | * In case sepecial request, there is no reentry to | |
2170 | * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'. | |
2171 | */ | |
e94cfef6 | 2172 | mmc_put_card(card); |
1a258db6 | 2173 | return ret; |
bd788c96 | 2174 | } |
1da177e4 | 2175 | |
a6f6c96b RK |
2176 | static inline int mmc_blk_readonly(struct mmc_card *card) |
2177 | { | |
2178 | return mmc_card_readonly(card) || | |
2179 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | |
2180 | } | |
2181 | ||
371a689f AW |
2182 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
2183 | struct device *parent, | |
2184 | sector_t size, | |
2185 | bool default_ro, | |
add710ea JR |
2186 | const char *subname, |
2187 | int area_type) | |
1da177e4 LT |
2188 | { |
2189 | struct mmc_blk_data *md; | |
2190 | int devidx, ret; | |
2191 | ||
5e71b7a6 OJ |
2192 | devidx = find_first_zero_bit(dev_use, max_devices); |
2193 | if (devidx >= max_devices) | |
1da177e4 LT |
2194 | return ERR_PTR(-ENOSPC); |
2195 | __set_bit(devidx, dev_use); | |
2196 | ||
dd00cc48 | 2197 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
a6f6c96b RK |
2198 | if (!md) { |
2199 | ret = -ENOMEM; | |
2200 | goto out; | |
2201 | } | |
1da177e4 | 2202 | |
add710ea JR |
2203 | md->area_type = area_type; |
2204 | ||
a6f6c96b RK |
2205 | /* |
2206 | * Set the read-only status based on the supported commands | |
2207 | * and the write protect switch. | |
2208 | */ | |
2209 | md->read_only = mmc_blk_readonly(card); | |
1da177e4 | 2210 | |
5e71b7a6 | 2211 | md->disk = alloc_disk(perdev_minors); |
a6f6c96b RK |
2212 | if (md->disk == NULL) { |
2213 | ret = -ENOMEM; | |
2214 | goto err_kfree; | |
2215 | } | |
1da177e4 | 2216 | |
a6f6c96b | 2217 | spin_lock_init(&md->lock); |
371a689f | 2218 | INIT_LIST_HEAD(&md->part); |
a6f6c96b | 2219 | md->usage = 1; |
1da177e4 | 2220 | |
d09408ad | 2221 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
a6f6c96b RK |
2222 | if (ret) |
2223 | goto err_putdisk; | |
1da177e4 | 2224 | |
a6f6c96b RK |
2225 | md->queue.issue_fn = mmc_blk_issue_rq; |
2226 | md->queue.data = md; | |
d2b18394 | 2227 | |
fe6b4c88 | 2228 | md->disk->major = MMC_BLOCK_MAJOR; |
5e71b7a6 | 2229 | md->disk->first_minor = devidx * perdev_minors; |
a6f6c96b RK |
2230 | md->disk->fops = &mmc_bdops; |
2231 | md->disk->private_data = md; | |
2232 | md->disk->queue = md->queue.queue; | |
371a689f AW |
2233 | md->disk->driverfs_dev = parent; |
2234 | set_disk_ro(md->disk, md->read_only || default_ro); | |
382c55f8 | 2235 | md->disk->flags = GENHD_FL_EXT_DEVT; |
f5b4d71f | 2236 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) |
53d8f974 | 2237 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
a6f6c96b RK |
2238 | |
2239 | /* | |
2240 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | |
2241 | * | |
2242 | * - be set for removable media with permanent block devices | |
2243 | * - be unset for removable block devices with permanent media | |
2244 | * | |
2245 | * Since MMC block devices clearly fall under the second | |
2246 | * case, we do not set GENHD_FL_REMOVABLE. Userspace | |
2247 | * should use the block device creation/destruction hotplug | |
2248 | * messages to tell when the card is present. | |
2249 | */ | |
2250 | ||
f06c9153 | 2251 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
9aaf3437 | 2252 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
a6f6c96b | 2253 | |
a5075eb9 SD |
2254 | if (mmc_card_mmc(card)) |
2255 | blk_queue_logical_block_size(md->queue.queue, | |
2256 | card->ext_csd.data_sector_size); | |
2257 | else | |
2258 | blk_queue_logical_block_size(md->queue.queue, 512); | |
2259 | ||
371a689f | 2260 | set_capacity(md->disk, size); |
d0c97cfb | 2261 | |
f0d89972 AW |
2262 | if (mmc_host_cmd23(card->host)) { |
2263 | if (mmc_card_mmc(card) || | |
2264 | (mmc_card_sd(card) && | |
2265 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | |
2266 | md->flags |= MMC_BLK_CMD23; | |
2267 | } | |
d0c97cfb AW |
2268 | |
2269 | if (mmc_card_mmc(card) && | |
2270 | md->flags & MMC_BLK_CMD23 && | |
2271 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || | |
2272 | card->ext_csd.rel_sectors)) { | |
2273 | md->flags |= MMC_BLK_REL_WR; | |
2274 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); | |
2275 | } | |
2276 | ||
ce39f9d1 SJ |
2277 | if (mmc_card_mmc(card) && |
2278 | (area_type == MMC_BLK_DATA_AREA_MAIN) && | |
2279 | (md->flags & MMC_BLK_CMD23) && | |
2280 | card->ext_csd.packed_event_en) { | |
2281 | if (!mmc_packed_init(&md->queue, card)) | |
2282 | md->flags |= MMC_BLK_PACKED_CMD; | |
2283 | } | |
2284 | ||
371a689f AW |
2285 | return md; |
2286 | ||
2287 | err_putdisk: | |
2288 | put_disk(md->disk); | |
2289 | err_kfree: | |
2290 | kfree(md); | |
2291 | out: | |
2292 | return ERR_PTR(ret); | |
2293 | } | |
2294 | ||
2295 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | |
2296 | { | |
2297 | sector_t size; | |
a6f6c96b | 2298 | |
85a18ad9 PO |
2299 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
2300 | /* | |
2301 | * The EXT_CSD sector count is in number or 512 byte | |
2302 | * sectors. | |
2303 | */ | |
371a689f | 2304 | size = card->ext_csd.sectors; |
85a18ad9 PO |
2305 | } else { |
2306 | /* | |
2307 | * The CSD capacity field is in units of read_blkbits. | |
2308 | * set_capacity takes units of 512 bytes. | |
2309 | */ | |
087de9ed KM |
2310 | size = (typeof(sector_t))card->csd.capacity |
2311 | << (card->csd.read_blkbits - 9); | |
85a18ad9 | 2312 | } |
371a689f | 2313 | |
7a30f2af | 2314 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
add710ea | 2315 | MMC_BLK_DATA_AREA_MAIN); |
371a689f | 2316 | } |
a6f6c96b | 2317 | |
371a689f AW |
2318 | static int mmc_blk_alloc_part(struct mmc_card *card, |
2319 | struct mmc_blk_data *md, | |
2320 | unsigned int part_type, | |
2321 | sector_t size, | |
2322 | bool default_ro, | |
add710ea JR |
2323 | const char *subname, |
2324 | int area_type) | |
371a689f AW |
2325 | { |
2326 | char cap_str[10]; | |
2327 | struct mmc_blk_data *part_md; | |
2328 | ||
2329 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | |
add710ea | 2330 | subname, area_type); |
371a689f AW |
2331 | if (IS_ERR(part_md)) |
2332 | return PTR_ERR(part_md); | |
2333 | part_md->part_type = part_type; | |
2334 | list_add(&part_md->part, &md->part); | |
2335 | ||
b9f28d86 | 2336 | string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2, |
371a689f | 2337 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 2338 | pr_info("%s: %s %s partition %u %s\n", |
371a689f AW |
2339 | part_md->disk->disk_name, mmc_card_id(card), |
2340 | mmc_card_name(card), part_md->part_type, cap_str); | |
2341 | return 0; | |
2342 | } | |
2343 | ||
e0c368d5 NJ |
2344 | /* MMC Physical partitions consist of two boot partitions and |
2345 | * up to four general purpose partitions. | |
2346 | * For each partition enabled in EXT_CSD a block device will be allocatedi | |
2347 | * to provide access to the partition. | |
2348 | */ | |
2349 | ||
371a689f AW |
2350 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
2351 | { | |
e0c368d5 | 2352 | int idx, ret = 0; |
371a689f AW |
2353 | |
2354 | if (!mmc_card_mmc(card)) | |
2355 | return 0; | |
2356 | ||
e0c368d5 NJ |
2357 | for (idx = 0; idx < card->nr_parts; idx++) { |
2358 | if (card->part[idx].size) { | |
2359 | ret = mmc_blk_alloc_part(card, md, | |
2360 | card->part[idx].part_cfg, | |
2361 | card->part[idx].size >> 9, | |
2362 | card->part[idx].force_ro, | |
add710ea JR |
2363 | card->part[idx].name, |
2364 | card->part[idx].area_type); | |
e0c368d5 NJ |
2365 | if (ret) |
2366 | return ret; | |
2367 | } | |
371a689f AW |
2368 | } |
2369 | ||
2370 | return ret; | |
1da177e4 LT |
2371 | } |
2372 | ||
371a689f AW |
2373 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
2374 | { | |
add710ea JR |
2375 | struct mmc_card *card; |
2376 | ||
371a689f | 2377 | if (md) { |
fdfa20c1 PT |
2378 | /* |
2379 | * Flush remaining requests and free queues. It | |
2380 | * is freeing the queue that stops new requests | |
2381 | * from being accepted. | |
2382 | */ | |
8efb83a2 | 2383 | card = md->queue.card; |
fdfa20c1 PT |
2384 | mmc_cleanup_queue(&md->queue); |
2385 | if (md->flags & MMC_BLK_PACKED_CMD) | |
2386 | mmc_packed_clean(&md->queue); | |
371a689f AW |
2387 | if (md->disk->flags & GENHD_FL_UP) { |
2388 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
add710ea JR |
2389 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
2390 | card->ext_csd.boot_ro_lockable) | |
2391 | device_remove_file(disk_to_dev(md->disk), | |
2392 | &md->power_ro_lock); | |
371a689f | 2393 | |
371a689f AW |
2394 | del_gendisk(md->disk); |
2395 | } | |
371a689f AW |
2396 | mmc_blk_put(md); |
2397 | } | |
2398 | } | |
2399 | ||
2400 | static void mmc_blk_remove_parts(struct mmc_card *card, | |
2401 | struct mmc_blk_data *md) | |
2402 | { | |
2403 | struct list_head *pos, *q; | |
2404 | struct mmc_blk_data *part_md; | |
2405 | ||
2406 | list_for_each_safe(pos, q, &md->part) { | |
2407 | part_md = list_entry(pos, struct mmc_blk_data, part); | |
2408 | list_del(pos); | |
2409 | mmc_blk_remove_req(part_md); | |
2410 | } | |
2411 | } | |
2412 | ||
2413 | static int mmc_add_disk(struct mmc_blk_data *md) | |
2414 | { | |
2415 | int ret; | |
add710ea | 2416 | struct mmc_card *card = md->queue.card; |
371a689f AW |
2417 | |
2418 | add_disk(md->disk); | |
2419 | md->force_ro.show = force_ro_show; | |
2420 | md->force_ro.store = force_ro_store; | |
641c3187 | 2421 | sysfs_attr_init(&md->force_ro.attr); |
371a689f AW |
2422 | md->force_ro.attr.name = "force_ro"; |
2423 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; | |
2424 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); | |
2425 | if (ret) | |
add710ea JR |
2426 | goto force_ro_fail; |
2427 | ||
2428 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | |
2429 | card->ext_csd.boot_ro_lockable) { | |
88187398 | 2430 | umode_t mode; |
add710ea JR |
2431 | |
2432 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) | |
2433 | mode = S_IRUGO; | |
2434 | else | |
2435 | mode = S_IRUGO | S_IWUSR; | |
2436 | ||
2437 | md->power_ro_lock.show = power_ro_lock_show; | |
2438 | md->power_ro_lock.store = power_ro_lock_store; | |
00d9ac08 | 2439 | sysfs_attr_init(&md->power_ro_lock.attr); |
add710ea JR |
2440 | md->power_ro_lock.attr.mode = mode; |
2441 | md->power_ro_lock.attr.name = | |
2442 | "ro_lock_until_next_power_on"; | |
2443 | ret = device_create_file(disk_to_dev(md->disk), | |
2444 | &md->power_ro_lock); | |
2445 | if (ret) | |
2446 | goto power_ro_lock_fail; | |
2447 | } | |
2448 | return ret; | |
2449 | ||
2450 | power_ro_lock_fail: | |
2451 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | |
2452 | force_ro_fail: | |
2453 | del_gendisk(md->disk); | |
371a689f AW |
2454 | |
2455 | return ret; | |
2456 | } | |
2457 | ||
c59d4473 CB |
2458 | #define CID_MANFID_SANDISK 0x2 |
2459 | #define CID_MANFID_TOSHIBA 0x11 | |
2460 | #define CID_MANFID_MICRON 0x13 | |
3550ccdb | 2461 | #define CID_MANFID_SAMSUNG 0x15 |
b5b4ff0a | 2462 | #define CID_MANFID_KINGSTON 0x70 |
c59d4473 | 2463 | |
6f60c222 AW |
2464 | static const struct mmc_fixup blk_fixups[] = |
2465 | { | |
c59d4473 CB |
2466 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
2467 | MMC_QUIRK_INAND_CMD38), | |
2468 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2469 | MMC_QUIRK_INAND_CMD38), | |
2470 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2471 | MMC_QUIRK_INAND_CMD38), | |
2472 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2473 | MMC_QUIRK_INAND_CMD38), | |
2474 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, | |
2475 | MMC_QUIRK_INAND_CMD38), | |
d0c97cfb AW |
2476 | |
2477 | /* | |
2478 | * Some MMC cards experience performance degradation with CMD23 | |
2479 | * instead of CMD12-bounded multiblock transfers. For now we'll | |
2480 | * black list what's bad... | |
2481 | * - Certain Toshiba cards. | |
2482 | * | |
2483 | * N.B. This doesn't affect SD cards. | |
2484 | */ | |
7d70d476 YL |
2485 | MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
2486 | MMC_QUIRK_BLK_NO_CMD23), | |
2487 | MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, | |
2488 | MMC_QUIRK_BLK_NO_CMD23), | |
c59d4473 | 2489 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2490 | MMC_QUIRK_BLK_NO_CMD23), |
c59d4473 | 2491 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2492 | MMC_QUIRK_BLK_NO_CMD23), |
c59d4473 | 2493 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
d0c97cfb | 2494 | MMC_QUIRK_BLK_NO_CMD23), |
6de5fc9c SNX |
2495 | |
2496 | /* | |
2497 | * Some Micron MMC cards needs longer data read timeout than | |
2498 | * indicated in CSD. | |
2499 | */ | |
c59d4473 | 2500 | MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc, |
6de5fc9c SNX |
2501 | MMC_QUIRK_LONG_READ_TIME), |
2502 | ||
3550ccdb IC |
2503 | /* |
2504 | * On these Samsung MoviNAND parts, performing secure erase or | |
2505 | * secure trim can result in unrecoverable corruption due to a | |
2506 | * firmware bug. | |
2507 | */ | |
2508 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2509 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2510 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2511 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2512 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2513 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2514 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2515 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2516 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2517 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2518 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2519 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2520 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2521 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2522 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, | |
2523 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), | |
2524 | ||
b5b4ff0a SL |
2525 | /* |
2526 | * On Some Kingston eMMCs, performing trim can result in | |
2527 | * unrecoverable data conrruption occasionally due to a firmware bug. | |
2528 | */ | |
2529 | MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, | |
2530 | MMC_QUIRK_TRIM_BROKEN), | |
2531 | MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, | |
2532 | MMC_QUIRK_TRIM_BROKEN), | |
2533 | ||
6f60c222 AW |
2534 | END_FIXUP |
2535 | }; | |
2536 | ||
96541bac | 2537 | static int mmc_blk_probe(struct mmc_card *card) |
1da177e4 | 2538 | { |
371a689f | 2539 | struct mmc_blk_data *md, *part_md; |
a7bbb573 PO |
2540 | char cap_str[10]; |
2541 | ||
912490db PO |
2542 | /* |
2543 | * Check that the card supports the command class(es) we need. | |
2544 | */ | |
2545 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | |
1da177e4 LT |
2546 | return -ENODEV; |
2547 | ||
5204d00f LC |
2548 | mmc_fixup_device(card, blk_fixups); |
2549 | ||
1da177e4 LT |
2550 | md = mmc_blk_alloc(card); |
2551 | if (IS_ERR(md)) | |
2552 | return PTR_ERR(md); | |
2553 | ||
b9f28d86 | 2554 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
a7bbb573 | 2555 | cap_str, sizeof(cap_str)); |
a3c76eb9 | 2556 | pr_info("%s: %s %s %s %s\n", |
1da177e4 | 2557 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
a7bbb573 | 2558 | cap_str, md->read_only ? "(ro)" : ""); |
1da177e4 | 2559 | |
371a689f AW |
2560 | if (mmc_blk_alloc_parts(card, md)) |
2561 | goto out; | |
2562 | ||
96541bac | 2563 | dev_set_drvdata(&card->dev, md); |
6f60c222 | 2564 | |
371a689f AW |
2565 | if (mmc_add_disk(md)) |
2566 | goto out; | |
2567 | ||
2568 | list_for_each_entry(part_md, &md->part, part) { | |
2569 | if (mmc_add_disk(part_md)) | |
2570 | goto out; | |
2571 | } | |
e94cfef6 UH |
2572 | |
2573 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); | |
2574 | pm_runtime_use_autosuspend(&card->dev); | |
2575 | ||
2576 | /* | |
2577 | * Don't enable runtime PM for SD-combo cards here. Leave that | |
2578 | * decision to be taken during the SDIO init sequence instead. | |
2579 | */ | |
2580 | if (card->type != MMC_TYPE_SD_COMBO) { | |
2581 | pm_runtime_set_active(&card->dev); | |
2582 | pm_runtime_enable(&card->dev); | |
2583 | } | |
2584 | ||
1da177e4 LT |
2585 | return 0; |
2586 | ||
2587 | out: | |
371a689f AW |
2588 | mmc_blk_remove_parts(card, md); |
2589 | mmc_blk_remove_req(md); | |
5865f287 | 2590 | return 0; |
1da177e4 LT |
2591 | } |
2592 | ||
96541bac | 2593 | static void mmc_blk_remove(struct mmc_card *card) |
1da177e4 | 2594 | { |
96541bac | 2595 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 | 2596 | |
371a689f | 2597 | mmc_blk_remove_parts(card, md); |
e94cfef6 | 2598 | pm_runtime_get_sync(&card->dev); |
ddd6fa7e AH |
2599 | mmc_claim_host(card->host); |
2600 | mmc_blk_part_switch(card, md); | |
2601 | mmc_release_host(card->host); | |
e94cfef6 UH |
2602 | if (card->type != MMC_TYPE_SD_COMBO) |
2603 | pm_runtime_disable(&card->dev); | |
2604 | pm_runtime_put_noidle(&card->dev); | |
371a689f | 2605 | mmc_blk_remove_req(md); |
96541bac | 2606 | dev_set_drvdata(&card->dev, NULL); |
1da177e4 LT |
2607 | } |
2608 | ||
96541bac | 2609 | static int _mmc_blk_suspend(struct mmc_card *card) |
1da177e4 | 2610 | { |
371a689f | 2611 | struct mmc_blk_data *part_md; |
96541bac | 2612 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 LT |
2613 | |
2614 | if (md) { | |
2615 | mmc_queue_suspend(&md->queue); | |
371a689f AW |
2616 | list_for_each_entry(part_md, &md->part, part) { |
2617 | mmc_queue_suspend(&part_md->queue); | |
2618 | } | |
1da177e4 LT |
2619 | } |
2620 | return 0; | |
2621 | } | |
2622 | ||
96541bac | 2623 | static void mmc_blk_shutdown(struct mmc_card *card) |
76287748 | 2624 | { |
96541bac | 2625 | _mmc_blk_suspend(card); |
76287748 UH |
2626 | } |
2627 | ||
0967edc6 UH |
2628 | #ifdef CONFIG_PM_SLEEP |
2629 | static int mmc_blk_suspend(struct device *dev) | |
76287748 | 2630 | { |
96541bac UH |
2631 | struct mmc_card *card = mmc_dev_to_card(dev); |
2632 | ||
2633 | return _mmc_blk_suspend(card); | |
76287748 UH |
2634 | } |
2635 | ||
0967edc6 | 2636 | static int mmc_blk_resume(struct device *dev) |
1da177e4 | 2637 | { |
371a689f | 2638 | struct mmc_blk_data *part_md; |
fc95e30b | 2639 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
1da177e4 LT |
2640 | |
2641 | if (md) { | |
371a689f AW |
2642 | /* |
2643 | * Resume involves the card going into idle state, | |
2644 | * so current partition is always the main one. | |
2645 | */ | |
2646 | md->part_curr = md->part_type; | |
1da177e4 | 2647 | mmc_queue_resume(&md->queue); |
371a689f AW |
2648 | list_for_each_entry(part_md, &md->part, part) { |
2649 | mmc_queue_resume(&part_md->queue); | |
2650 | } | |
1da177e4 LT |
2651 | } |
2652 | return 0; | |
2653 | } | |
1da177e4 LT |
2654 | #endif |
2655 | ||
0967edc6 UH |
2656 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
2657 | ||
96541bac UH |
2658 | static struct mmc_driver mmc_driver = { |
2659 | .drv = { | |
2660 | .name = "mmcblk", | |
2661 | .pm = &mmc_blk_pm_ops, | |
2662 | }, | |
1da177e4 LT |
2663 | .probe = mmc_blk_probe, |
2664 | .remove = mmc_blk_remove, | |
76287748 | 2665 | .shutdown = mmc_blk_shutdown, |
1da177e4 LT |
2666 | }; |
2667 | ||
2668 | static int __init mmc_blk_init(void) | |
2669 | { | |
9d4e98e9 | 2670 | int res; |
1da177e4 | 2671 | |
5e71b7a6 OJ |
2672 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
2673 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | |
2674 | ||
a26eba61 | 2675 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
5e71b7a6 | 2676 | |
fe6b4c88 PO |
2677 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
2678 | if (res) | |
1da177e4 | 2679 | goto out; |
1da177e4 | 2680 | |
9d4e98e9 AM |
2681 | res = mmc_register_driver(&mmc_driver); |
2682 | if (res) | |
2683 | goto out2; | |
1da177e4 | 2684 | |
9d4e98e9 AM |
2685 | return 0; |
2686 | out2: | |
2687 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); | |
1da177e4 LT |
2688 | out: |
2689 | return res; | |
2690 | } | |
2691 | ||
2692 | static void __exit mmc_blk_exit(void) | |
2693 | { | |
2694 | mmc_unregister_driver(&mmc_driver); | |
fe6b4c88 | 2695 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
1da177e4 LT |
2696 | } |
2697 | ||
2698 | module_init(mmc_blk_init); | |
2699 | module_exit(mmc_blk_exit); | |
2700 | ||
2701 | MODULE_LICENSE("GPL"); | |
2702 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | |
2703 |