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