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