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