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