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