]>
Commit | Line | Data |
---|---|---|
7c45b226 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * Block driver for media (i.e., flash cards) | |
4 | * | |
5 | * Copyright 2002 Hewlett-Packard Company | |
979ce720 | 6 | * Copyright 2005-2008 Pierre Ossman |
1da177e4 LT |
7 | * |
8 | * Use consistent with the GNU GPL is permitted, | |
9 | * provided that this copyright notice is | |
10 | * preserved in its entirety in all copies and derived works. | |
11 | * | |
12 | * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, | |
13 | * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS | |
14 | * FITNESS FOR ANY PARTICULAR PURPOSE. | |
15 | * | |
16 | * Many thanks to Alessandro Rubini and Jonathan Corbet! | |
17 | * | |
18 | * Author: Andrew Christian | |
19 | * 28 May 2002 | |
20 | */ | |
21 | #include <linux/moduleparam.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/init.h> | |
24 | ||
1da177e4 LT |
25 | #include <linux/kernel.h> |
26 | #include <linux/fs.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
1da177e4 LT |
28 | #include <linux/errno.h> |
29 | #include <linux/hdreg.h> | |
30 | #include <linux/kdev_t.h> | |
edb25572 | 31 | #include <linux/kref.h> |
1da177e4 | 32 | #include <linux/blkdev.h> |
97548575 | 33 | #include <linux/cdev.h> |
a621aaed | 34 | #include <linux/mutex.h> |
ec5a19dd | 35 | #include <linux/scatterlist.h> |
a7bbb573 | 36 | #include <linux/string_helpers.h> |
cb87ea28 JC |
37 | #include <linux/delay.h> |
38 | #include <linux/capability.h> | |
39 | #include <linux/compat.h> | |
e94cfef6 | 40 | #include <linux/pm_runtime.h> |
b10fa99e | 41 | #include <linux/idr.h> |
627c3ccf | 42 | #include <linux/debugfs.h> |
1da177e4 | 43 | |
cb87ea28 | 44 | #include <linux/mmc/ioctl.h> |
1da177e4 | 45 | #include <linux/mmc/card.h> |
385e3227 | 46 | #include <linux/mmc/host.h> |
da7fbe58 PO |
47 | #include <linux/mmc/mmc.h> |
48 | #include <linux/mmc/sd.h> | |
1da177e4 | 49 | |
7c0f6ba6 | 50 | #include <linux/uaccess.h> |
1da177e4 | 51 | |
98ac2162 | 52 | #include "queue.h" |
48ab086d | 53 | #include "block.h" |
55244c56 | 54 | #include "core.h" |
4facdde1 | 55 | #include "card.h" |
93f1c150 | 56 | #include "crypto.h" |
5857b29b | 57 | #include "host.h" |
4facdde1 | 58 | #include "bus.h" |
55244c56 | 59 | #include "mmc_ops.h" |
28fc64af | 60 | #include "quirks.h" |
55244c56 | 61 | #include "sd_ops.h" |
1da177e4 | 62 | |
6b0b6285 | 63 | MODULE_ALIAS("mmc:block"); |
5e71b7a6 OJ |
64 | #ifdef MODULE_PARAM_PREFIX |
65 | #undef MODULE_PARAM_PREFIX | |
66 | #endif | |
67 | #define MODULE_PARAM_PREFIX "mmcblk." | |
68 | ||
6b7a363d AH |
69 | /* |
70 | * Set a 10 second timeout for polling write request busy state. Note, mmc core | |
71 | * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10 | |
72 | * second software timer to timeout the whole request, so 10 seconds should be | |
73 | * ample. | |
74 | */ | |
75 | #define MMC_BLK_TIMEOUT_MS (10 * 1000) | |
775a9362 | 76 | #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) |
a0e95766 | 77 | #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8) |
6a7a6b45 | 78 | |
5e71b7a6 | 79 | static DEFINE_MUTEX(block_mutex); |
6b0b6285 | 80 | |
1da177e4 | 81 | /* |
5e71b7a6 OJ |
82 | * The defaults come from config options but can be overriden by module |
83 | * or bootarg options. | |
1da177e4 | 84 | */ |
5e71b7a6 | 85 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
1dff3144 | 86 | |
5e71b7a6 OJ |
87 | /* |
88 | * We've only got one major, so number of mmcblk devices is | |
a26eba61 | 89 | * limited to (1 << 20) / number of minors per device. It is also |
b10fa99e | 90 | * limited by the MAX_DEVICES below. |
5e71b7a6 OJ |
91 | */ |
92 | static int max_devices; | |
93 | ||
a26eba61 BH |
94 | #define MAX_DEVICES 256 |
95 | ||
b10fa99e | 96 | static DEFINE_IDA(mmc_blk_ida); |
97548575 | 97 | static DEFINE_IDA(mmc_rpmb_ida); |
1da177e4 | 98 | |
6966e609 UH |
99 | struct mmc_blk_busy_data { |
100 | struct mmc_card *card; | |
101 | u32 status; | |
102 | }; | |
103 | ||
1da177e4 LT |
104 | /* |
105 | * There is one mmc_blk_data per slot. | |
106 | */ | |
107 | struct mmc_blk_data { | |
307d8e6f | 108 | struct device *parent; |
1da177e4 LT |
109 | struct gendisk *disk; |
110 | struct mmc_queue queue; | |
371a689f | 111 | struct list_head part; |
97548575 | 112 | struct list_head rpmbs; |
1da177e4 | 113 | |
d0c97cfb AW |
114 | unsigned int flags; |
115 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ | |
116 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ | |
117 | ||
edb25572 | 118 | struct kref kref; |
a6f6c96b | 119 | unsigned int read_only; |
371a689f | 120 | unsigned int part_type; |
67716327 AH |
121 | unsigned int reset_done; |
122 | #define MMC_BLK_READ BIT(0) | |
123 | #define MMC_BLK_WRITE BIT(1) | |
124 | #define MMC_BLK_DISCARD BIT(2) | |
125 | #define MMC_BLK_SECDISCARD BIT(3) | |
1e8e55b6 | 126 | #define MMC_BLK_CQE_RECOVERY BIT(4) |
f7b6fc32 | 127 | #define MMC_BLK_TRIM BIT(5) |
371a689f AW |
128 | |
129 | /* | |
130 | * Only set in main mmc_blk_data associated | |
fc95e30b | 131 | * with mmc_card with dev_set_drvdata, and keeps |
371a689f AW |
132 | * track of the current selected device partition. |
133 | */ | |
134 | unsigned int part_curr; | |
406e1480 | 135 | #define MMC_BLK_PART_INVALID UINT_MAX /* Unknown partition active */ |
add710ea | 136 | int area_type; |
f9f0da98 AH |
137 | |
138 | /* debugfs files (only in main mmc_blk_data) */ | |
139 | struct dentry *status_dentry; | |
140 | struct dentry *ext_csd_dentry; | |
1da177e4 LT |
141 | }; |
142 | ||
97548575 LW |
143 | /* Device type for RPMB character devices */ |
144 | static dev_t mmc_rpmb_devt; | |
145 | ||
146 | /* Bus type for RPMB character devices */ | |
147 | static struct bus_type mmc_rpmb_bus_type = { | |
148 | .name = "mmc_rpmb", | |
149 | }; | |
150 | ||
151 | /** | |
152 | * struct mmc_rpmb_data - special RPMB device type for these areas | |
153 | * @dev: the device for the RPMB area | |
154 | * @chrdev: character device for the RPMB area | |
155 | * @id: unique device ID number | |
156 | * @part_index: partition index (0 on first) | |
157 | * @md: parent MMC block device | |
158 | * @node: list item, so we can put this device on a list | |
159 | */ | |
160 | struct mmc_rpmb_data { | |
161 | struct device dev; | |
162 | struct cdev chrdev; | |
163 | int id; | |
164 | unsigned int part_index; | |
165 | struct mmc_blk_data *md; | |
166 | struct list_head node; | |
167 | }; | |
168 | ||
a621aaed | 169 | static DEFINE_MUTEX(open_lock); |
1da177e4 | 170 | |
5e71b7a6 OJ |
171 | module_param(perdev_minors, int, 0444); |
172 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); | |
173 | ||
8d1e977d | 174 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
1f797edc | 175 | unsigned int part_type); |
511ce378 BW |
176 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
177 | struct mmc_card *card, | |
b3fa3e6d | 178 | int recovery_mode, |
511ce378 BW |
179 | struct mmc_queue *mq); |
180 | static void mmc_blk_hsq_req_done(struct mmc_request *mrq); | |
568898cb | 181 | static int mmc_spi_err_check(struct mmc_card *card); |
f19c5a73 | 182 | static int mmc_blk_busy_cb(void *cb_data, bool *busy); |
cdf8a6fb | 183 | |
1da177e4 LT |
184 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
185 | { | |
186 | struct mmc_blk_data *md; | |
187 | ||
a621aaed | 188 | mutex_lock(&open_lock); |
1da177e4 | 189 | md = disk->private_data; |
edb25572 | 190 | if (md && !kref_get_unless_zero(&md->kref)) |
1da177e4 | 191 | md = NULL; |
a621aaed | 192 | mutex_unlock(&open_lock); |
1da177e4 LT |
193 | |
194 | return md; | |
195 | } | |
196 | ||
371a689f AW |
197 | static inline int mmc_get_devidx(struct gendisk *disk) |
198 | { | |
382c55f8 | 199 | int devidx = disk->first_minor / perdev_minors; |
371a689f AW |
200 | return devidx; |
201 | } | |
202 | ||
edb25572 | 203 | static void mmc_blk_kref_release(struct kref *ref) |
1da177e4 | 204 | { |
edb25572 SB |
205 | struct mmc_blk_data *md = container_of(ref, struct mmc_blk_data, kref); |
206 | int devidx; | |
249cda33 | 207 | |
edb25572 SB |
208 | devidx = mmc_get_devidx(md->disk); |
209 | ida_simple_remove(&mmc_blk_ida, devidx); | |
210 | ||
211 | mutex_lock(&open_lock); | |
212 | md->disk->private_data = NULL; | |
a621aaed | 213 | mutex_unlock(&open_lock); |
edb25572 SB |
214 | |
215 | put_disk(md->disk); | |
216 | kfree(md); | |
217 | } | |
218 | ||
219 | static void mmc_blk_put(struct mmc_blk_data *md) | |
220 | { | |
221 | kref_put(&md->kref, mmc_blk_kref_release); | |
1da177e4 LT |
222 | } |
223 | ||
add710ea JR |
224 | static ssize_t power_ro_lock_show(struct device *dev, |
225 | struct device_attribute *attr, char *buf) | |
226 | { | |
227 | int ret; | |
228 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
229 | struct mmc_card *card = md->queue.card; | |
230 | int locked = 0; | |
231 | ||
232 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) | |
233 | locked = 2; | |
234 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) | |
235 | locked = 1; | |
236 | ||
237 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); | |
238 | ||
9098f84c TW |
239 | mmc_blk_put(md); |
240 | ||
add710ea JR |
241 | return ret; |
242 | } | |
243 | ||
244 | static ssize_t power_ro_lock_store(struct device *dev, | |
245 | struct device_attribute *attr, const char *buf, size_t count) | |
246 | { | |
247 | int ret; | |
248 | struct mmc_blk_data *md, *part_md; | |
0493f6fe LW |
249 | struct mmc_queue *mq; |
250 | struct request *req; | |
add710ea JR |
251 | unsigned long set; |
252 | ||
253 | if (kstrtoul(buf, 0, &set)) | |
254 | return -EINVAL; | |
255 | ||
256 | if (set != 1) | |
257 | return count; | |
258 | ||
259 | md = mmc_blk_get(dev_to_disk(dev)); | |
0493f6fe | 260 | mq = &md->queue; |
add710ea | 261 | |
0493f6fe | 262 | /* Dispatch locking to the block layer */ |
0bf6d96c | 263 | req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_OUT, 0); |
fb8e456e AH |
264 | if (IS_ERR(req)) { |
265 | count = PTR_ERR(req); | |
266 | goto out_put; | |
267 | } | |
0493f6fe | 268 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP; |
003fb0a5 | 269 | req_to_mmc_queue_req(req)->drv_op_result = -EIO; |
b84ba30b | 270 | blk_execute_rq(req, false); |
0493f6fe | 271 | ret = req_to_mmc_queue_req(req)->drv_op_result; |
0bf6d96c | 272 | blk_mq_free_request(req); |
add710ea JR |
273 | |
274 | if (!ret) { | |
275 | pr_info("%s: Locking boot partition ro until next power on\n", | |
276 | md->disk->disk_name); | |
277 | set_disk_ro(md->disk, 1); | |
278 | ||
279 | list_for_each_entry(part_md, &md->part, part) | |
280 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { | |
281 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); | |
282 | set_disk_ro(part_md->disk, 1); | |
283 | } | |
284 | } | |
fb8e456e | 285 | out_put: |
add710ea JR |
286 | mmc_blk_put(md); |
287 | return count; | |
288 | } | |
289 | ||
29e6a5e0 CH |
290 | static DEVICE_ATTR(ro_lock_until_next_power_on, 0, |
291 | power_ro_lock_show, power_ro_lock_store); | |
292 | ||
371a689f AW |
293 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
294 | char *buf) | |
295 | { | |
296 | int ret; | |
297 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
298 | ||
0031a98a | 299 | ret = snprintf(buf, PAGE_SIZE, "%d\n", |
371a689f AW |
300 | get_disk_ro(dev_to_disk(dev)) ^ |
301 | md->read_only); | |
302 | mmc_blk_put(md); | |
303 | return ret; | |
304 | } | |
305 | ||
306 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, | |
307 | const char *buf, size_t count) | |
308 | { | |
309 | int ret; | |
310 | char *end; | |
311 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | |
312 | unsigned long set = simple_strtoul(buf, &end, 0); | |
313 | if (end == buf) { | |
314 | ret = -EINVAL; | |
315 | goto out; | |
316 | } | |
317 | ||
318 | set_disk_ro(dev_to_disk(dev), set || md->read_only); | |
319 | ret = count; | |
320 | out: | |
321 | mmc_blk_put(md); | |
322 | return ret; | |
323 | } | |
324 | ||
29e6a5e0 CH |
325 | static DEVICE_ATTR(force_ro, 0644, force_ro_show, force_ro_store); |
326 | ||
327 | static struct attribute *mmc_disk_attrs[] = { | |
328 | &dev_attr_force_ro.attr, | |
329 | &dev_attr_ro_lock_until_next_power_on.attr, | |
330 | NULL, | |
331 | }; | |
332 | ||
333 | static umode_t mmc_disk_attrs_is_visible(struct kobject *kobj, | |
334 | struct attribute *a, int n) | |
335 | { | |
0e8bb666 | 336 | struct device *dev = kobj_to_dev(kobj); |
29e6a5e0 CH |
337 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
338 | umode_t mode = a->mode; | |
339 | ||
340 | if (a == &dev_attr_ro_lock_until_next_power_on.attr && | |
341 | (md->area_type & MMC_BLK_DATA_AREA_BOOT) && | |
342 | md->queue.card->ext_csd.boot_ro_lockable) { | |
343 | mode = S_IRUGO; | |
344 | if (!(md->queue.card->ext_csd.boot_ro_lock & | |
345 | EXT_CSD_BOOT_WP_B_PWR_WP_DIS)) | |
346 | mode |= S_IWUSR; | |
347 | } | |
348 | ||
349 | mmc_blk_put(md); | |
350 | return mode; | |
351 | } | |
352 | ||
353 | static const struct attribute_group mmc_disk_attr_group = { | |
354 | .is_visible = mmc_disk_attrs_is_visible, | |
355 | .attrs = mmc_disk_attrs, | |
356 | }; | |
357 | ||
358 | static const struct attribute_group *mmc_disk_attr_groups[] = { | |
359 | &mmc_disk_attr_group, | |
360 | NULL, | |
361 | }; | |
362 | ||
05bdb996 | 363 | static int mmc_blk_open(struct gendisk *disk, blk_mode_t mode) |
1da177e4 | 364 | { |
d32e2bf8 | 365 | struct mmc_blk_data *md = mmc_blk_get(disk); |
1da177e4 LT |
366 | int ret = -ENXIO; |
367 | ||
2a48fc0a | 368 | mutex_lock(&block_mutex); |
1da177e4 | 369 | if (md) { |
1da177e4 | 370 | ret = 0; |
05bdb996 | 371 | if ((mode & BLK_OPEN_WRITE) && md->read_only) { |
70bb0896 | 372 | mmc_blk_put(md); |
a00fc090 | 373 | ret = -EROFS; |
70bb0896 | 374 | } |
1da177e4 | 375 | } |
2a48fc0a | 376 | mutex_unlock(&block_mutex); |
1da177e4 LT |
377 | |
378 | return ret; | |
379 | } | |
380 | ||
ae220766 | 381 | static void mmc_blk_release(struct gendisk *disk) |
1da177e4 | 382 | { |
a5a1561f | 383 | struct mmc_blk_data *md = disk->private_data; |
1da177e4 | 384 | |
2a48fc0a | 385 | mutex_lock(&block_mutex); |
1da177e4 | 386 | mmc_blk_put(md); |
2a48fc0a | 387 | mutex_unlock(&block_mutex); |
1da177e4 LT |
388 | } |
389 | ||
390 | static int | |
a885c8c4 | 391 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 392 | { |
a885c8c4 CH |
393 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
394 | geo->heads = 4; | |
395 | geo->sectors = 16; | |
396 | return 0; | |
1da177e4 LT |
397 | } |
398 | ||
cb87ea28 JC |
399 | struct mmc_blk_ioc_data { |
400 | struct mmc_ioc_cmd ic; | |
401 | unsigned char *buf; | |
402 | u64 buf_bytes; | |
97548575 | 403 | struct mmc_rpmb_data *rpmb; |
cb87ea28 JC |
404 | }; |
405 | ||
406 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( | |
407 | struct mmc_ioc_cmd __user *user) | |
408 | { | |
409 | struct mmc_blk_ioc_data *idata; | |
410 | int err; | |
411 | ||
1ff8950c | 412 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); |
cb87ea28 JC |
413 | if (!idata) { |
414 | err = -ENOMEM; | |
aea253ec | 415 | goto out; |
cb87ea28 JC |
416 | } |
417 | ||
418 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { | |
419 | err = -EFAULT; | |
aea253ec | 420 | goto idata_err; |
cb87ea28 JC |
421 | } |
422 | ||
423 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; | |
424 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { | |
425 | err = -EOVERFLOW; | |
aea253ec | 426 | goto idata_err; |
cb87ea28 JC |
427 | } |
428 | ||
bfe5b1b1 VV |
429 | if (!idata->buf_bytes) { |
430 | idata->buf = NULL; | |
4d6144de | 431 | return idata; |
bfe5b1b1 | 432 | } |
4d6144de | 433 | |
97a0c313 ME |
434 | idata->buf = memdup_user((void __user *)(unsigned long) |
435 | idata->ic.data_ptr, idata->buf_bytes); | |
436 | if (IS_ERR(idata->buf)) { | |
437 | err = PTR_ERR(idata->buf); | |
aea253ec | 438 | goto idata_err; |
cb87ea28 JC |
439 | } |
440 | ||
cb87ea28 JC |
441 | return idata; |
442 | ||
aea253ec | 443 | idata_err: |
cb87ea28 | 444 | kfree(idata); |
aea253ec | 445 | out: |
cb87ea28 | 446 | return ERR_PTR(err); |
cb87ea28 JC |
447 | } |
448 | ||
a5f5774c JH |
449 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, |
450 | struct mmc_blk_ioc_data *idata) | |
451 | { | |
452 | struct mmc_ioc_cmd *ic = &idata->ic; | |
453 | ||
454 | if (copy_to_user(&(ic_ptr->response), ic->response, | |
455 | sizeof(ic->response))) | |
456 | return -EFAULT; | |
457 | ||
458 | if (!idata->ic.write_flag) { | |
459 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, | |
460 | idata->buf, idata->buf_bytes)) | |
461 | return -EFAULT; | |
462 | } | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
a5f5774c JH |
467 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, |
468 | struct mmc_blk_ioc_data *idata) | |
cb87ea28 | 469 | { |
a44f7cb9 | 470 | struct mmc_command cmd = {}, sbc = {}; |
c7836d15 MY |
471 | struct mmc_data data = {}; |
472 | struct mmc_request mrq = {}; | |
cb87ea28 | 473 | struct scatterlist sg; |
f19c5a73 | 474 | bool r1b_resp; |
51f5b305 | 475 | unsigned int busy_timeout_ms; |
cb87ea28 | 476 | int err; |
97548575 | 477 | unsigned int target_part; |
cb87ea28 | 478 | |
a5f5774c JH |
479 | if (!card || !md || !idata) |
480 | return -EINVAL; | |
cb87ea28 | 481 | |
97548575 LW |
482 | /* |
483 | * The RPMB accesses comes in from the character device, so we | |
484 | * need to target these explicitly. Else we just target the | |
485 | * partition type for the block device the ioctl() was issued | |
486 | * on. | |
487 | */ | |
488 | if (idata->rpmb) { | |
489 | /* Support multiple RPMB partitions */ | |
490 | target_part = idata->rpmb->part_index; | |
491 | target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB; | |
492 | } else { | |
493 | target_part = md->part_type; | |
494 | } | |
8d1e977d | 495 | |
4d6144de JR |
496 | cmd.opcode = idata->ic.opcode; |
497 | cmd.arg = idata->ic.arg; | |
498 | cmd.flags = idata->ic.flags; | |
499 | ||
500 | if (idata->buf_bytes) { | |
501 | data.sg = &sg; | |
502 | data.sg_len = 1; | |
503 | data.blksz = idata->ic.blksz; | |
504 | data.blocks = idata->ic.blocks; | |
505 | ||
506 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); | |
507 | ||
508 | if (idata->ic.write_flag) | |
509 | data.flags = MMC_DATA_WRITE; | |
510 | else | |
511 | data.flags = MMC_DATA_READ; | |
512 | ||
513 | /* data.flags must already be set before doing this. */ | |
514 | mmc_set_data_timeout(&data, card); | |
515 | ||
516 | /* Allow overriding the timeout_ns for empirical tuning. */ | |
517 | if (idata->ic.data_timeout_ns) | |
518 | data.timeout_ns = idata->ic.data_timeout_ns; | |
519 | ||
4d6144de JR |
520 | mrq.data = &data; |
521 | } | |
522 | ||
523 | mrq.cmd = &cmd; | |
524 | ||
97548575 | 525 | err = mmc_blk_part_switch(card, target_part); |
8d1e977d | 526 | if (err) |
a5f5774c | 527 | return err; |
8d1e977d | 528 | |
cb87ea28 JC |
529 | if (idata->ic.is_acmd) { |
530 | err = mmc_app_cmd(card->host, card); | |
531 | if (err) | |
a5f5774c | 532 | return err; |
cb87ea28 JC |
533 | } |
534 | ||
97548575 | 535 | if (idata->rpmb) { |
a44f7cb9 WS |
536 | sbc.opcode = MMC_SET_BLOCK_COUNT; |
537 | /* | |
538 | * We don't do any blockcount validation because the max size | |
539 | * may be increased by a future standard. We just copy the | |
540 | * 'Reliable Write' bit here. | |
541 | */ | |
542 | sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31)); | |
543 | sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; | |
544 | mrq.sbc = &sbc; | |
8d1e977d LP |
545 | } |
546 | ||
a82e484e | 547 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
55c2b8b9 | 548 | (cmd.opcode == MMC_SWITCH)) |
4f111d04 | 549 | return mmc_sanitize(card, idata->ic.cmd_timeout_ms); |
775a9362 | 550 | |
51f5b305 UH |
551 | /* If it's an R1B response we need some more preparations. */ |
552 | busy_timeout_ms = idata->ic.cmd_timeout_ms ? : MMC_BLK_TIMEOUT_MS; | |
553 | r1b_resp = (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B; | |
554 | if (r1b_resp) | |
f19c5a73 | 555 | mmc_prepare_busy_cmd(card->host, &cmd, busy_timeout_ms); |
51f5b305 | 556 | |
cb87ea28 | 557 | mmc_wait_for_req(card->host, &mrq); |
e72a55f2 | 558 | memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp)); |
cb87ea28 JC |
559 | |
560 | if (cmd.error) { | |
561 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", | |
562 | __func__, cmd.error); | |
a5f5774c | 563 | return cmd.error; |
cb87ea28 JC |
564 | } |
565 | if (data.error) { | |
566 | dev_err(mmc_dev(card->host), "%s: data error %d\n", | |
567 | __func__, data.error); | |
a5f5774c | 568 | return data.error; |
cb87ea28 JC |
569 | } |
570 | ||
a0e95766 BS |
571 | /* |
572 | * Make sure the cache of the PARTITION_CONFIG register and | |
573 | * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write | |
574 | * changed it successfully. | |
575 | */ | |
576 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) && | |
577 | (cmd.opcode == MMC_SWITCH)) { | |
578 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); | |
579 | u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg); | |
580 | ||
581 | /* | |
582 | * Update cache so the next mmc_blk_part_switch call operates | |
583 | * on up-to-date data. | |
584 | */ | |
585 | card->ext_csd.part_config = value; | |
586 | main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK; | |
587 | } | |
588 | ||
aea0440a AA |
589 | /* |
590 | * Make sure to update CACHE_CTRL in case it was changed. The cache | |
591 | * will get turned back on if the card is re-initialized, e.g. | |
592 | * suspend/resume or hw reset in recovery. | |
593 | */ | |
594 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) && | |
595 | (cmd.opcode == MMC_SWITCH)) { | |
596 | u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1; | |
597 | ||
598 | card->ext_csd.cache_ctrl = value; | |
599 | } | |
600 | ||
cb87ea28 JC |
601 | /* |
602 | * According to the SD specs, some commands require a delay after | |
603 | * issuing the command. | |
604 | */ | |
605 | if (idata->ic.postsleep_min_us) | |
606 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); | |
607 | ||
568898cb CL |
608 | if (mmc_host_is_spi(card->host)) { |
609 | if (idata->ic.write_flag || r1b_resp || cmd.flags & MMC_RSP_SPI_BUSY) | |
610 | return mmc_spi_err_check(card); | |
611 | return err; | |
612 | } | |
f19c5a73 UH |
613 | |
614 | /* | |
615 | * Ensure RPMB, writes and R1B responses are completed by polling with | |
616 | * CMD13. Note that, usually we don't need to poll when using HW busy | |
617 | * detection, but here it's needed since some commands may indicate the | |
618 | * error through the R1 status bits. | |
619 | */ | |
620 | if (idata->rpmb || idata->ic.write_flag || r1b_resp) { | |
621 | struct mmc_blk_busy_data cb_data = { | |
622 | .card = card, | |
623 | }; | |
624 | ||
625 | err = __mmc_poll_for_busy(card->host, 0, busy_timeout_ms, | |
626 | &mmc_blk_busy_cb, &cb_data); | |
627 | ||
628 | idata->ic.response[0] = cb_data.status; | |
629 | } | |
8d1e977d | 630 | |
a5f5774c JH |
631 | return err; |
632 | } | |
633 | ||
2fe20bae | 634 | static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md, |
97548575 LW |
635 | struct mmc_ioc_cmd __user *ic_ptr, |
636 | struct mmc_rpmb_data *rpmb) | |
a5f5774c JH |
637 | { |
638 | struct mmc_blk_ioc_data *idata; | |
3ecd8cf2 | 639 | struct mmc_blk_ioc_data *idatas[1]; |
614f0388 | 640 | struct mmc_queue *mq; |
a5f5774c | 641 | struct mmc_card *card; |
b093410c | 642 | int err = 0, ioc_err = 0; |
614f0388 | 643 | struct request *req; |
a5f5774c JH |
644 | |
645 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); | |
646 | if (IS_ERR(idata)) | |
647 | return PTR_ERR(idata); | |
97548575 LW |
648 | /* This will be NULL on non-RPMB ioctl():s */ |
649 | idata->rpmb = rpmb; | |
a5f5774c | 650 | |
a5f5774c JH |
651 | card = md->queue.card; |
652 | if (IS_ERR(card)) { | |
653 | err = PTR_ERR(card); | |
654 | goto cmd_done; | |
655 | } | |
656 | ||
614f0388 LW |
657 | /* |
658 | * Dispatch the ioctl() into the block request queue. | |
659 | */ | |
660 | mq = &md->queue; | |
0bf6d96c | 661 | req = blk_mq_alloc_request(mq->queue, |
ff005a06 | 662 | idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
fb8e456e AH |
663 | if (IS_ERR(req)) { |
664 | err = PTR_ERR(req); | |
665 | goto cmd_done; | |
666 | } | |
3ecd8cf2 | 667 | idatas[0] = idata; |
97548575 LW |
668 | req_to_mmc_queue_req(req)->drv_op = |
669 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; | |
003fb0a5 | 670 | req_to_mmc_queue_req(req)->drv_op_result = -EIO; |
69f7599e | 671 | req_to_mmc_queue_req(req)->drv_op_data = idatas; |
3ecd8cf2 | 672 | req_to_mmc_queue_req(req)->ioc_count = 1; |
b84ba30b | 673 | blk_execute_rq(req, false); |
0493f6fe | 674 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
b093410c | 675 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
0bf6d96c | 676 | blk_mq_free_request(req); |
a5f5774c | 677 | |
cb87ea28 | 678 | cmd_done: |
cb87ea28 JC |
679 | kfree(idata->buf); |
680 | kfree(idata); | |
b093410c | 681 | return ioc_err ? ioc_err : err; |
cb87ea28 JC |
682 | } |
683 | ||
2fe20bae | 684 | static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md, |
97548575 LW |
685 | struct mmc_ioc_multi_cmd __user *user, |
686 | struct mmc_rpmb_data *rpmb) | |
a5f5774c JH |
687 | { |
688 | struct mmc_blk_ioc_data **idata = NULL; | |
689 | struct mmc_ioc_cmd __user *cmds = user->cmds; | |
690 | struct mmc_card *card; | |
3ecd8cf2 | 691 | struct mmc_queue *mq; |
103da066 | 692 | int err = 0, ioc_err = 0; |
a5f5774c | 693 | __u64 num_of_cmds; |
103da066 | 694 | unsigned int i, n; |
3ecd8cf2 | 695 | struct request *req; |
a5f5774c JH |
696 | |
697 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, | |
698 | sizeof(num_of_cmds))) | |
699 | return -EFAULT; | |
700 | ||
aab2ee03 GU |
701 | if (!num_of_cmds) |
702 | return 0; | |
703 | ||
a5f5774c JH |
704 | if (num_of_cmds > MMC_IOC_MAX_CMDS) |
705 | return -EINVAL; | |
706 | ||
103da066 SS |
707 | n = num_of_cmds; |
708 | idata = kcalloc(n, sizeof(*idata), GFP_KERNEL); | |
a5f5774c JH |
709 | if (!idata) |
710 | return -ENOMEM; | |
711 | ||
103da066 | 712 | for (i = 0; i < n; i++) { |
a5f5774c JH |
713 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); |
714 | if (IS_ERR(idata[i])) { | |
715 | err = PTR_ERR(idata[i]); | |
103da066 | 716 | n = i; |
a5f5774c JH |
717 | goto cmd_err; |
718 | } | |
97548575 LW |
719 | /* This will be NULL on non-RPMB ioctl():s */ |
720 | idata[i]->rpmb = rpmb; | |
a5f5774c JH |
721 | } |
722 | ||
a5f5774c JH |
723 | card = md->queue.card; |
724 | if (IS_ERR(card)) { | |
725 | err = PTR_ERR(card); | |
2fe20bae | 726 | goto cmd_err; |
a5f5774c JH |
727 | } |
728 | ||
a5f5774c | 729 | |
3ecd8cf2 LW |
730 | /* |
731 | * Dispatch the ioctl()s into the block request queue. | |
732 | */ | |
733 | mq = &md->queue; | |
0bf6d96c | 734 | req = blk_mq_alloc_request(mq->queue, |
ff005a06 | 735 | idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
fb8e456e AH |
736 | if (IS_ERR(req)) { |
737 | err = PTR_ERR(req); | |
738 | goto cmd_err; | |
739 | } | |
97548575 LW |
740 | req_to_mmc_queue_req(req)->drv_op = |
741 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; | |
003fb0a5 | 742 | req_to_mmc_queue_req(req)->drv_op_result = -EIO; |
69f7599e | 743 | req_to_mmc_queue_req(req)->drv_op_data = idata; |
103da066 | 744 | req_to_mmc_queue_req(req)->ioc_count = n; |
b84ba30b | 745 | blk_execute_rq(req, false); |
0493f6fe | 746 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
a5f5774c JH |
747 | |
748 | /* copy to user if data and response */ | |
103da066 | 749 | for (i = 0; i < n && !err; i++) |
a5f5774c | 750 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
a5f5774c | 751 | |
0bf6d96c | 752 | blk_mq_free_request(req); |
3ecd8cf2 | 753 | |
a5f5774c | 754 | cmd_err: |
103da066 | 755 | for (i = 0; i < n; i++) { |
a5f5774c JH |
756 | kfree(idata[i]->buf); |
757 | kfree(idata[i]); | |
758 | } | |
759 | kfree(idata); | |
b093410c | 760 | return ioc_err ? ioc_err : err; |
a5f5774c JH |
761 | } |
762 | ||
61fe0e2b LW |
763 | static int mmc_blk_check_blkdev(struct block_device *bdev) |
764 | { | |
765 | /* | |
766 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | |
767 | * whole block device, not on a partition. This prevents overspray | |
768 | * between sibling partitions. | |
769 | */ | |
fa01b1e9 | 770 | if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev)) |
61fe0e2b LW |
771 | return -EPERM; |
772 | return 0; | |
773 | } | |
774 | ||
05bdb996 | 775 | static int mmc_blk_ioctl(struct block_device *bdev, blk_mode_t mode, |
cb87ea28 JC |
776 | unsigned int cmd, unsigned long arg) |
777 | { | |
2fe20bae | 778 | struct mmc_blk_data *md; |
61fe0e2b LW |
779 | int ret; |
780 | ||
a5f5774c JH |
781 | switch (cmd) { |
782 | case MMC_IOC_CMD: | |
61fe0e2b LW |
783 | ret = mmc_blk_check_blkdev(bdev); |
784 | if (ret) | |
785 | return ret; | |
2fe20bae LW |
786 | md = mmc_blk_get(bdev->bd_disk); |
787 | if (!md) | |
788 | return -EINVAL; | |
789 | ret = mmc_blk_ioctl_cmd(md, | |
97548575 LW |
790 | (struct mmc_ioc_cmd __user *)arg, |
791 | NULL); | |
2fe20bae LW |
792 | mmc_blk_put(md); |
793 | return ret; | |
a5f5774c | 794 | case MMC_IOC_MULTI_CMD: |
61fe0e2b LW |
795 | ret = mmc_blk_check_blkdev(bdev); |
796 | if (ret) | |
797 | return ret; | |
2fe20bae LW |
798 | md = mmc_blk_get(bdev->bd_disk); |
799 | if (!md) | |
800 | return -EINVAL; | |
801 | ret = mmc_blk_ioctl_multi_cmd(md, | |
97548575 LW |
802 | (struct mmc_ioc_multi_cmd __user *)arg, |
803 | NULL); | |
2fe20bae LW |
804 | mmc_blk_put(md); |
805 | return ret; | |
a5f5774c JH |
806 | default: |
807 | return -EINVAL; | |
808 | } | |
cb87ea28 JC |
809 | } |
810 | ||
811 | #ifdef CONFIG_COMPAT | |
05bdb996 | 812 | static int mmc_blk_compat_ioctl(struct block_device *bdev, blk_mode_t mode, |
cb87ea28 JC |
813 | unsigned int cmd, unsigned long arg) |
814 | { | |
815 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); | |
816 | } | |
817 | #endif | |
818 | ||
dc913385 DO |
819 | static int mmc_blk_alternative_gpt_sector(struct gendisk *disk, |
820 | sector_t *sector) | |
821 | { | |
822 | struct mmc_blk_data *md; | |
823 | int ret; | |
824 | ||
825 | md = mmc_blk_get(disk); | |
826 | if (!md) | |
827 | return -EINVAL; | |
828 | ||
829 | if (md->queue.card) | |
830 | ret = mmc_card_alternative_gpt_sector(md->queue.card, sector); | |
831 | else | |
832 | ret = -ENODEV; | |
833 | ||
834 | mmc_blk_put(md); | |
835 | ||
836 | return ret; | |
837 | } | |
838 | ||
83d5cde4 | 839 | static const struct block_device_operations mmc_bdops = { |
a5a1561f AV |
840 | .open = mmc_blk_open, |
841 | .release = mmc_blk_release, | |
a885c8c4 | 842 | .getgeo = mmc_blk_getgeo, |
1da177e4 | 843 | .owner = THIS_MODULE, |
cb87ea28 JC |
844 | .ioctl = mmc_blk_ioctl, |
845 | #ifdef CONFIG_COMPAT | |
846 | .compat_ioctl = mmc_blk_compat_ioctl, | |
847 | #endif | |
dc913385 | 848 | .alternative_gpt_sector = mmc_blk_alternative_gpt_sector, |
1da177e4 LT |
849 | }; |
850 | ||
025e3d5f AH |
851 | static int mmc_blk_part_switch_pre(struct mmc_card *card, |
852 | unsigned int part_type) | |
853 | { | |
854 | int ret = 0; | |
855 | ||
856 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | |
857 | if (card->ext_csd.cmdq_en) { | |
858 | ret = mmc_cmdq_disable(card); | |
859 | if (ret) | |
860 | return ret; | |
861 | } | |
862 | mmc_retune_pause(card->host); | |
863 | } | |
864 | ||
865 | return ret; | |
866 | } | |
867 | ||
868 | static int mmc_blk_part_switch_post(struct mmc_card *card, | |
869 | unsigned int part_type) | |
870 | { | |
871 | int ret = 0; | |
872 | ||
873 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | |
874 | mmc_retune_unpause(card->host); | |
875 | if (card->reenable_cmdq && !card->ext_csd.cmdq_en) | |
876 | ret = mmc_cmdq_enable(card); | |
877 | } | |
878 | ||
879 | return ret; | |
880 | } | |
881 | ||
371a689f | 882 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
1f797edc | 883 | unsigned int part_type) |
371a689f | 884 | { |
025e3d5f | 885 | int ret = 0; |
fc95e30b | 886 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
0d7d85ca | 887 | |
1f797edc | 888 | if (main_md->part_curr == part_type) |
371a689f AW |
889 | return 0; |
890 | ||
891 | if (mmc_card_mmc(card)) { | |
0d7d85ca AH |
892 | u8 part_config = card->ext_csd.part_config; |
893 | ||
1f797edc | 894 | ret = mmc_blk_part_switch_pre(card, part_type); |
025e3d5f AH |
895 | if (ret) |
896 | return ret; | |
57da0c04 | 897 | |
0d7d85ca | 898 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
1f797edc | 899 | part_config |= part_type; |
371a689f AW |
900 | |
901 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
0d7d85ca | 902 | EXT_CSD_PART_CONFIG, part_config, |
371a689f | 903 | card->ext_csd.part_time); |
57da0c04 | 904 | if (ret) { |
1f797edc | 905 | mmc_blk_part_switch_post(card, part_type); |
371a689f | 906 | return ret; |
57da0c04 | 907 | } |
0d7d85ca AH |
908 | |
909 | card->ext_csd.part_config = part_config; | |
57da0c04 | 910 | |
025e3d5f | 911 | ret = mmc_blk_part_switch_post(card, main_md->part_curr); |
67716327 | 912 | } |
371a689f | 913 | |
1f797edc | 914 | main_md->part_curr = part_type; |
025e3d5f | 915 | return ret; |
371a689f AW |
916 | } |
917 | ||
169f03a0 | 918 | static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks) |
ec5a19dd PO |
919 | { |
920 | int err; | |
051913da BD |
921 | u32 result; |
922 | __be32 *blocks; | |
ec5a19dd | 923 | |
c7836d15 MY |
924 | struct mmc_request mrq = {}; |
925 | struct mmc_command cmd = {}; | |
926 | struct mmc_data data = {}; | |
ec5a19dd PO |
927 | |
928 | struct scatterlist sg; | |
929 | ||
13433b9e | 930 | err = mmc_app_cmd(card->host, card); |
7213d175 | 931 | if (err) |
169f03a0 | 932 | return err; |
ec5a19dd PO |
933 | |
934 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; | |
935 | cmd.arg = 0; | |
7213d175 | 936 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
ec5a19dd | 937 | |
ec5a19dd PO |
938 | data.blksz = 4; |
939 | data.blocks = 1; | |
940 | data.flags = MMC_DATA_READ; | |
941 | data.sg = &sg; | |
942 | data.sg_len = 1; | |
d380443c | 943 | mmc_set_data_timeout(&data, card); |
ec5a19dd | 944 | |
ec5a19dd PO |
945 | mrq.cmd = &cmd; |
946 | mrq.data = &data; | |
947 | ||
051913da BD |
948 | blocks = kmalloc(4, GFP_KERNEL); |
949 | if (!blocks) | |
169f03a0 | 950 | return -ENOMEM; |
051913da BD |
951 | |
952 | sg_init_one(&sg, blocks, 4); | |
ec5a19dd PO |
953 | |
954 | mmc_wait_for_req(card->host, &mrq); | |
955 | ||
051913da BD |
956 | result = ntohl(*blocks); |
957 | kfree(blocks); | |
958 | ||
17b0429d | 959 | if (cmd.error || data.error) |
169f03a0 LW |
960 | return -EIO; |
961 | ||
962 | *written_blocks = result; | |
ec5a19dd | 963 | |
169f03a0 | 964 | return 0; |
ec5a19dd PO |
965 | } |
966 | ||
92c0a0cc AH |
967 | static unsigned int mmc_blk_clock_khz(struct mmc_host *host) |
968 | { | |
969 | if (host->actual_clock) | |
970 | return host->actual_clock / 1000; | |
971 | ||
972 | /* Clock may be subject to a divisor, fudge it by a factor of 2. */ | |
973 | if (host->ios.clock) | |
974 | return host->ios.clock / 2000; | |
975 | ||
976 | /* How can there be no clock */ | |
977 | WARN_ON_ONCE(1); | |
978 | return 100; /* 100 kHz is minimum possible value */ | |
979 | } | |
980 | ||
981 | static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host, | |
982 | struct mmc_data *data) | |
983 | { | |
984 | unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000); | |
985 | unsigned int khz; | |
986 | ||
987 | if (data->timeout_clks) { | |
988 | khz = mmc_blk_clock_khz(host); | |
989 | ms += DIV_ROUND_UP(data->timeout_clks, khz); | |
990 | } | |
991 | ||
992 | return ms; | |
993 | } | |
994 | ||
406e1480 CL |
995 | /* |
996 | * Attempts to reset the card and get back to the requested partition. | |
997 | * Therefore any error here must result in cancelling the block layer | |
998 | * request, it must not be reattempted without going through the mmc_blk | |
999 | * partition sanity checks. | |
1000 | */ | |
67716327 AH |
1001 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
1002 | int type) | |
1003 | { | |
1004 | int err; | |
406e1480 | 1005 | struct mmc_blk_data *main_md = dev_get_drvdata(&host->card->dev); |
67716327 AH |
1006 | |
1007 | if (md->reset_done & type) | |
1008 | return -EEXIST; | |
1009 | ||
1010 | md->reset_done |= type; | |
b71597ed | 1011 | err = mmc_hw_reset(host->card); |
406e1480 CL |
1012 | /* |
1013 | * A successful reset will leave the card in the main partition, but | |
1014 | * upon failure it might not be, so set it to MMC_BLK_PART_INVALID | |
1015 | * in that case. | |
1016 | */ | |
1017 | main_md->part_curr = err ? MMC_BLK_PART_INVALID : main_md->part_type; | |
1018 | if (err) | |
1019 | return err; | |
67716327 | 1020 | /* Ensure we switch back to the correct partition */ |
406e1480 CL |
1021 | if (mmc_blk_part_switch(host->card, md->part_type)) |
1022 | /* | |
1023 | * We have failed to get back into the correct | |
1024 | * partition, so we need to abort the whole request. | |
1025 | */ | |
1026 | return -ENODEV; | |
1027 | return 0; | |
67716327 AH |
1028 | } |
1029 | ||
1030 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | |
1031 | { | |
1032 | md->reset_done &= ~type; | |
1033 | } | |
1034 | ||
5ec12396 LW |
1035 | /* |
1036 | * The non-block commands come back from the block layer after it queued it and | |
1037 | * processed it with all other requests and then they get issued in this | |
1038 | * function. | |
1039 | */ | |
1040 | static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) | |
1041 | { | |
1042 | struct mmc_queue_req *mq_rq; | |
1043 | struct mmc_card *card = mq->card; | |
1044 | struct mmc_blk_data *md = mq->blkdata; | |
69f7599e | 1045 | struct mmc_blk_ioc_data **idata; |
97548575 | 1046 | bool rpmb_ioctl; |
627c3ccf LW |
1047 | u8 **ext_csd; |
1048 | u32 status; | |
0493f6fe | 1049 | int ret; |
5ec12396 LW |
1050 | int i; |
1051 | ||
1052 | mq_rq = req_to_mmc_queue_req(req); | |
97548575 | 1053 | rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB); |
5ec12396 LW |
1054 | |
1055 | switch (mq_rq->drv_op) { | |
1056 | case MMC_DRV_OP_IOCTL: | |
70b52f09 BH |
1057 | if (card->ext_csd.cmdq_en) { |
1058 | ret = mmc_cmdq_disable(card); | |
1059 | if (ret) | |
1060 | break; | |
1061 | } | |
1062 | fallthrough; | |
97548575 | 1063 | case MMC_DRV_OP_IOCTL_RPMB: |
69f7599e | 1064 | idata = mq_rq->drv_op_data; |
7432b49b | 1065 | for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) { |
69f7599e | 1066 | ret = __mmc_blk_ioctl_cmd(card, md, idata[i]); |
0493f6fe | 1067 | if (ret) |
5ec12396 LW |
1068 | break; |
1069 | } | |
5ec12396 | 1070 | /* Always switch back to main area after RPMB access */ |
97548575 LW |
1071 | if (rpmb_ioctl) |
1072 | mmc_blk_part_switch(card, 0); | |
70b52f09 BH |
1073 | else if (card->reenable_cmdq && !card->ext_csd.cmdq_en) |
1074 | mmc_cmdq_enable(card); | |
0493f6fe LW |
1075 | break; |
1076 | case MMC_DRV_OP_BOOT_WP: | |
1077 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | |
1078 | card->ext_csd.boot_ro_lock | | |
1079 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | |
1080 | card->ext_csd.part_time); | |
1081 | if (ret) | |
1082 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", | |
1083 | md->disk->disk_name, ret); | |
1084 | else | |
1085 | card->ext_csd.boot_ro_lock |= | |
1086 | EXT_CSD_BOOT_WP_B_PWR_WP_EN; | |
5ec12396 | 1087 | break; |
627c3ccf LW |
1088 | case MMC_DRV_OP_GET_CARD_STATUS: |
1089 | ret = mmc_send_status(card, &status); | |
1090 | if (!ret) | |
1091 | ret = status; | |
1092 | break; | |
1093 | case MMC_DRV_OP_GET_EXT_CSD: | |
1094 | ext_csd = mq_rq->drv_op_data; | |
1095 | ret = mmc_get_ext_csd(card, ext_csd); | |
1096 | break; | |
5ec12396 | 1097 | default: |
0493f6fe LW |
1098 | pr_err("%s: unknown driver specific operation\n", |
1099 | md->disk->disk_name); | |
1100 | ret = -EINVAL; | |
5ec12396 LW |
1101 | break; |
1102 | } | |
0493f6fe | 1103 | mq_rq->drv_op_result = ret; |
0fbfd125 | 1104 | blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
5ec12396 LW |
1105 | } |
1106 | ||
f7b6fc32 VW |
1107 | static void mmc_blk_issue_erase_rq(struct mmc_queue *mq, struct request *req, |
1108 | int type, unsigned int erase_arg) | |
bd788c96 | 1109 | { |
7db3028e | 1110 | struct mmc_blk_data *md = mq->blkdata; |
bd788c96 | 1111 | struct mmc_card *card = md->queue.card; |
01904ff7 | 1112 | unsigned int from, nr; |
f7b6fc32 | 1113 | int err = 0; |
2a842aca | 1114 | blk_status_t status = BLK_STS_OK; |
bd788c96 | 1115 | |
bd788c96 | 1116 | if (!mmc_can_erase(card)) { |
2a842aca | 1117 | status = BLK_STS_NOTSUPP; |
8cb6ed17 | 1118 | goto fail; |
bd788c96 AH |
1119 | } |
1120 | ||
1121 | from = blk_rq_pos(req); | |
1122 | nr = blk_rq_sectors(req); | |
1123 | ||
164b50b3 GU |
1124 | do { |
1125 | err = 0; | |
1126 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | |
1127 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1128 | INAND_CMD38_ARG_EXT_CSD, | |
f7b6fc32 | 1129 | erase_arg == MMC_TRIM_ARG ? |
164b50b3 GU |
1130 | INAND_CMD38_ARG_TRIM : |
1131 | INAND_CMD38_ARG_ERASE, | |
ad91619a | 1132 | card->ext_csd.generic_cmd6_time); |
164b50b3 GU |
1133 | } |
1134 | if (!err) | |
f7b6fc32 | 1135 | err = mmc_erase(card, from, nr, erase_arg); |
164b50b3 | 1136 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); |
2a842aca CH |
1137 | if (err) |
1138 | status = BLK_STS_IOERR; | |
1139 | else | |
67716327 | 1140 | mmc_blk_reset_success(md, type); |
8cb6ed17 | 1141 | fail: |
0fbfd125 | 1142 | blk_mq_end_request(req, status); |
bd788c96 AH |
1143 | } |
1144 | ||
f7b6fc32 VW |
1145 | static void mmc_blk_issue_trim_rq(struct mmc_queue *mq, struct request *req) |
1146 | { | |
1147 | mmc_blk_issue_erase_rq(mq, req, MMC_BLK_TRIM, MMC_TRIM_ARG); | |
1148 | } | |
1149 | ||
1150 | static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) | |
1151 | { | |
1152 | struct mmc_blk_data *md = mq->blkdata; | |
1153 | struct mmc_card *card = md->queue.card; | |
07d2872b | 1154 | unsigned int arg = card->erase_arg; |
f7b6fc32 | 1155 | |
07d2872b AA |
1156 | if (mmc_card_broken_sd_discard(card)) |
1157 | arg = SD_ERASE_ARG; | |
1158 | ||
1159 | mmc_blk_issue_erase_rq(mq, req, MMC_BLK_DISCARD, arg); | |
f7b6fc32 VW |
1160 | } |
1161 | ||
df061588 | 1162 | static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
49804548 AH |
1163 | struct request *req) |
1164 | { | |
7db3028e | 1165 | struct mmc_blk_data *md = mq->blkdata; |
49804548 | 1166 | struct mmc_card *card = md->queue.card; |
775a9362 | 1167 | unsigned int from, nr, arg; |
67716327 | 1168 | int err = 0, type = MMC_BLK_SECDISCARD; |
2a842aca | 1169 | blk_status_t status = BLK_STS_OK; |
49804548 | 1170 | |
775a9362 | 1171 | if (!(mmc_can_secure_erase_trim(card))) { |
2a842aca | 1172 | status = BLK_STS_NOTSUPP; |
49804548 AH |
1173 | goto out; |
1174 | } | |
1175 | ||
28302812 AH |
1176 | from = blk_rq_pos(req); |
1177 | nr = blk_rq_sectors(req); | |
1178 | ||
775a9362 ME |
1179 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
1180 | arg = MMC_SECURE_TRIM1_ARG; | |
1181 | else | |
1182 | arg = MMC_SECURE_ERASE_ARG; | |
d9ddd629 | 1183 | |
67716327 | 1184 | retry: |
6a7a6b45 AW |
1185 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1186 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1187 | INAND_CMD38_ARG_EXT_CSD, | |
1188 | arg == MMC_SECURE_TRIM1_ARG ? | |
1189 | INAND_CMD38_ARG_SECTRIM1 : | |
1190 | INAND_CMD38_ARG_SECERASE, | |
ad91619a | 1191 | card->ext_csd.generic_cmd6_time); |
6a7a6b45 | 1192 | if (err) |
28302812 | 1193 | goto out_retry; |
6a7a6b45 | 1194 | } |
28302812 | 1195 | |
49804548 | 1196 | err = mmc_erase(card, from, nr, arg); |
28302812 AH |
1197 | if (err == -EIO) |
1198 | goto out_retry; | |
2a842aca CH |
1199 | if (err) { |
1200 | status = BLK_STS_IOERR; | |
28302812 | 1201 | goto out; |
2a842aca | 1202 | } |
28302812 AH |
1203 | |
1204 | if (arg == MMC_SECURE_TRIM1_ARG) { | |
6a7a6b45 AW |
1205 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
1206 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
1207 | INAND_CMD38_ARG_EXT_CSD, | |
1208 | INAND_CMD38_ARG_SECTRIM2, | |
ad91619a | 1209 | card->ext_csd.generic_cmd6_time); |
6a7a6b45 | 1210 | if (err) |
28302812 | 1211 | goto out_retry; |
6a7a6b45 | 1212 | } |
28302812 | 1213 | |
49804548 | 1214 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
28302812 AH |
1215 | if (err == -EIO) |
1216 | goto out_retry; | |
2a842aca CH |
1217 | if (err) { |
1218 | status = BLK_STS_IOERR; | |
28302812 | 1219 | goto out; |
2a842aca | 1220 | } |
6a7a6b45 | 1221 | } |
28302812 | 1222 | |
28302812 AH |
1223 | out_retry: |
1224 | if (err && !mmc_blk_reset(md, card->host, type)) | |
67716327 AH |
1225 | goto retry; |
1226 | if (!err) | |
1227 | mmc_blk_reset_success(md, type); | |
28302812 | 1228 | out: |
0fbfd125 | 1229 | blk_mq_end_request(req, status); |
49804548 AH |
1230 | } |
1231 | ||
df061588 | 1232 | static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
f4c5522b | 1233 | { |
7db3028e | 1234 | struct mmc_blk_data *md = mq->blkdata; |
881d1c25 SJ |
1235 | struct mmc_card *card = md->queue.card; |
1236 | int ret = 0; | |
1237 | ||
8ae11ede | 1238 | ret = mmc_flush_cache(card->host); |
0fbfd125 | 1239 | blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
f4c5522b AW |
1240 | } |
1241 | ||
1242 | /* | |
1243 | * Reformat current write as a reliable write, supporting | |
1244 | * both legacy and the enhanced reliable write MMC cards. | |
1245 | * In each transfer we'll handle only as much as a single | |
1246 | * reliable write can handle, thus finish the request in | |
1247 | * partial completions. | |
1248 | */ | |
d0c97cfb AW |
1249 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
1250 | struct mmc_card *card, | |
1251 | struct request *req) | |
f4c5522b | 1252 | { |
f4c5522b AW |
1253 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
1254 | /* Legacy mode imposes restrictions on transfers. */ | |
9cb38f7a | 1255 | if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) |
f4c5522b AW |
1256 | brq->data.blocks = 1; |
1257 | ||
1258 | if (brq->data.blocks > card->ext_csd.rel_sectors) | |
1259 | brq->data.blocks = card->ext_csd.rel_sectors; | |
1260 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | |
1261 | brq->data.blocks = 1; | |
1262 | } | |
f4c5522b AW |
1263 | } |
1264 | ||
f47a1fe3 AH |
1265 | #define CMD_ERRORS_EXCL_OOR \ |
1266 | (R1_ADDRESS_ERROR | /* Misaligned address */ \ | |
4c2b8f26 RKAL |
1267 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
1268 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ | |
a04e6bae | 1269 | R1_CARD_ECC_FAILED | /* Card ECC failed */ \ |
4c2b8f26 RKAL |
1270 | R1_CC_ERROR | /* Card controller error */ \ |
1271 | R1_ERROR) /* General/unknown error */ | |
1272 | ||
f47a1fe3 AH |
1273 | #define CMD_ERRORS \ |
1274 | (CMD_ERRORS_EXCL_OOR | \ | |
1275 | R1_OUT_OF_RANGE) /* Command argument out of range */ \ | |
1276 | ||
d83c2dba | 1277 | static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) |
a04e6bae | 1278 | { |
d83c2dba | 1279 | u32 val; |
a04e6bae | 1280 | |
d83c2dba SL |
1281 | /* |
1282 | * Per the SD specification(physical layer version 4.10)[1], | |
1283 | * section 4.3.3, it explicitly states that "When the last | |
1284 | * block of user area is read using CMD18, the host should | |
1285 | * ignore OUT_OF_RANGE error that may occur even the sequence | |
1286 | * is correct". And JESD84-B51 for eMMC also has a similar | |
1287 | * statement on section 6.8.3. | |
1288 | * | |
1289 | * Multiple block read/write could be done by either predefined | |
1290 | * method, namely CMD23, or open-ending mode. For open-ending mode, | |
1291 | * we should ignore the OUT_OF_RANGE error as it's normal behaviour. | |
1292 | * | |
1293 | * However the spec[1] doesn't tell us whether we should also | |
1294 | * ignore that for predefined method. But per the spec[1], section | |
1295 | * 4.15 Set Block Count Command, it says"If illegal block count | |
1296 | * is set, out of range error will be indicated during read/write | |
1297 | * operation (For example, data transfer is stopped at user area | |
1298 | * boundary)." In another word, we could expect a out of range error | |
1299 | * in the response for the following CMD18/25. And if argument of | |
1300 | * CMD23 + the argument of CMD18/25 exceed the max number of blocks, | |
1301 | * we could also expect to get a -ETIMEDOUT or any error number from | |
1302 | * the host drivers due to missing data response(for write)/data(for | |
1303 | * read), as the cards will stop the data transfer by itself per the | |
1304 | * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. | |
1305 | */ | |
1306 | ||
1307 | if (!brq->stop.error) { | |
1308 | bool oor_with_open_end; | |
1309 | /* If there is no error yet, check R1 response */ | |
1310 | ||
1311 | val = brq->stop.resp[0] & CMD_ERRORS; | |
1312 | oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; | |
1313 | ||
1314 | if (val && !oor_with_open_end) | |
1315 | brq->stop.error = -EIO; | |
1316 | } | |
a04e6bae WS |
1317 | } |
1318 | ||
ca5717f7 | 1319 | static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, |
b3fa3e6d | 1320 | int recovery_mode, bool *do_rel_wr_p, |
d3377c01 | 1321 | bool *do_data_tag_p) |
1da177e4 | 1322 | { |
ca5717f7 AH |
1323 | struct mmc_blk_data *md = mq->blkdata; |
1324 | struct mmc_card *card = md->queue.card; | |
54d49d77 | 1325 | struct mmc_blk_request *brq = &mqrq->brq; |
67e69d52 | 1326 | struct request *req = mmc_queue_req_to_req(mqrq); |
d3377c01 | 1327 | bool do_rel_wr, do_data_tag; |
1da177e4 | 1328 | |
f4c5522b AW |
1329 | /* |
1330 | * Reliable writes are used to implement Forced Unit Access and | |
d3df0465 | 1331 | * are supported only on MMCs. |
f4c5522b | 1332 | */ |
d3377c01 AH |
1333 | do_rel_wr = (req->cmd_flags & REQ_FUA) && |
1334 | rq_data_dir(req) == WRITE && | |
1335 | (md->flags & MMC_BLK_REL_WR); | |
f4c5522b | 1336 | |
54d49d77 | 1337 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
ca5717f7 | 1338 | |
93f1c150 EB |
1339 | mmc_crypto_prepare_req(mqrq); |
1340 | ||
54d49d77 | 1341 | brq->mrq.data = &brq->data; |
93482b3d | 1342 | brq->mrq.tag = req->tag; |
1da177e4 | 1343 | |
54d49d77 PF |
1344 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
1345 | brq->stop.arg = 0; | |
ca5717f7 AH |
1346 | |
1347 | if (rq_data_dir(req) == READ) { | |
1348 | brq->data.flags = MMC_DATA_READ; | |
1349 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1350 | } else { | |
1351 | brq->data.flags = MMC_DATA_WRITE; | |
1352 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1353 | } | |
1354 | ||
1355 | brq->data.blksz = 512; | |
54d49d77 | 1356 | brq->data.blocks = blk_rq_sectors(req); |
93482b3d AH |
1357 | brq->data.blk_addr = blk_rq_pos(req); |
1358 | ||
1359 | /* | |
1360 | * The command queue supports 2 priorities: "high" (1) and "simple" (0). | |
1361 | * The eMMC will give "high" priority tasks priority over "simple" | |
1362 | * priority tasks. Here we always set "simple" priority by not setting | |
1363 | * MMC_DATA_PRIO. | |
1364 | */ | |
6a79e391 | 1365 | |
54d49d77 PF |
1366 | /* |
1367 | * The block layer doesn't support all sector count | |
1368 | * restrictions, so we need to be prepared for too big | |
1369 | * requests. | |
1370 | */ | |
1371 | if (brq->data.blocks > card->host->max_blk_count) | |
1372 | brq->data.blocks = card->host->max_blk_count; | |
1da177e4 | 1373 | |
2bf22b39 | 1374 | if (brq->data.blocks > 1) { |
41591b38 CB |
1375 | /* |
1376 | * Some SD cards in SPI mode return a CRC error or even lock up | |
1377 | * completely when trying to read the last block using a | |
1378 | * multiblock read command. | |
1379 | */ | |
1380 | if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) && | |
1381 | (blk_rq_pos(req) + blk_rq_sectors(req) == | |
1382 | get_capacity(md->disk))) | |
1383 | brq->data.blocks--; | |
1384 | ||
2bf22b39 | 1385 | /* |
b3fa3e6d | 1386 | * After a read error, we redo the request one (native) sector |
2bf22b39 PW |
1387 | * at a time in order to accurately determine which |
1388 | * sectors can be read successfully. | |
1389 | */ | |
b3fa3e6d CL |
1390 | if (recovery_mode) |
1391 | brq->data.blocks = queue_physical_block_size(mq->queue) >> 9; | |
2bf22b39 | 1392 | |
2e47e842 KM |
1393 | /* |
1394 | * Some controllers have HW issues while operating | |
1395 | * in multiple I/O mode | |
1396 | */ | |
1397 | if (card->host->ops->multi_io_quirk) | |
1398 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | |
1399 | (rq_data_dir(req) == READ) ? | |
1400 | MMC_DATA_READ : MMC_DATA_WRITE, | |
1401 | brq->data.blocks); | |
2bf22b39 | 1402 | } |
d0c97cfb | 1403 | |
93482b3d | 1404 | if (do_rel_wr) { |
ca5717f7 | 1405 | mmc_apply_rel_rw(brq, card, req); |
93482b3d AH |
1406 | brq->data.flags |= MMC_DATA_REL_WR; |
1407 | } | |
ca5717f7 AH |
1408 | |
1409 | /* | |
1410 | * Data tag is used only during writing meta data to speed | |
1411 | * up write and any subsequent read of this meta data | |
1412 | */ | |
d3377c01 AH |
1413 | do_data_tag = card->ext_csd.data_tag_unit_size && |
1414 | (req->cmd_flags & REQ_META) && | |
1415 | (rq_data_dir(req) == WRITE) && | |
1416 | ((brq->data.blocks * brq->data.blksz) >= | |
1417 | card->ext_csd.data_tag_unit_size); | |
ca5717f7 | 1418 | |
93482b3d AH |
1419 | if (do_data_tag) |
1420 | brq->data.flags |= MMC_DATA_DAT_TAG; | |
1421 | ||
ca5717f7 AH |
1422 | mmc_set_data_timeout(&brq->data, card); |
1423 | ||
1424 | brq->data.sg = mqrq->sg; | |
1425 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | |
1426 | ||
1427 | /* | |
1428 | * Adjust the sg list so it is the same size as the | |
1429 | * request. | |
1430 | */ | |
1431 | if (brq->data.blocks != blk_rq_sectors(req)) { | |
1432 | int i, data_size = brq->data.blocks << 9; | |
1433 | struct scatterlist *sg; | |
1434 | ||
1435 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | |
1436 | data_size -= sg->length; | |
1437 | if (data_size <= 0) { | |
1438 | sg->length += data_size; | |
1439 | i++; | |
1440 | break; | |
1441 | } | |
1442 | } | |
1443 | brq->data.sg_len = i; | |
1444 | } | |
1445 | ||
d3377c01 AH |
1446 | if (do_rel_wr_p) |
1447 | *do_rel_wr_p = do_rel_wr; | |
1448 | ||
1449 | if (do_data_tag_p) | |
1450 | *do_data_tag_p = do_data_tag; | |
ca5717f7 AH |
1451 | } |
1452 | ||
1e8e55b6 AH |
1453 | #define MMC_CQE_RETRIES 2 |
1454 | ||
1455 | static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req) | |
1456 | { | |
1457 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1458 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1459 | struct request_queue *q = req->q; | |
1460 | struct mmc_host *host = mq->card->host; | |
e6bfb1bf | 1461 | enum mmc_issue_type issue_type = mmc_issue_type(mq, req); |
1e8e55b6 AH |
1462 | unsigned long flags; |
1463 | bool put_card; | |
1464 | int err; | |
1465 | ||
1466 | mmc_cqe_post_req(host, mrq); | |
1467 | ||
1468 | if (mrq->cmd && mrq->cmd->error) | |
1469 | err = mrq->cmd->error; | |
1470 | else if (mrq->data && mrq->data->error) | |
1471 | err = mrq->data->error; | |
1472 | else | |
1473 | err = 0; | |
1474 | ||
1475 | if (err) { | |
1476 | if (mqrq->retries++ < MMC_CQE_RETRIES) | |
1477 | blk_mq_requeue_request(req, true); | |
1478 | else | |
1479 | blk_mq_end_request(req, BLK_STS_IOERR); | |
1480 | } else if (mrq->data) { | |
1481 | if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered)) | |
1482 | blk_mq_requeue_request(req, true); | |
1483 | else | |
1484 | __blk_mq_end_request(req, BLK_STS_OK); | |
174925d3 AH |
1485 | } else if (mq->in_recovery) { |
1486 | blk_mq_requeue_request(req, true); | |
1e8e55b6 AH |
1487 | } else { |
1488 | blk_mq_end_request(req, BLK_STS_OK); | |
1489 | } | |
1490 | ||
f5d72c5c | 1491 | spin_lock_irqsave(&mq->lock, flags); |
1e8e55b6 | 1492 | |
e6bfb1bf | 1493 | mq->in_flight[issue_type] -= 1; |
1e8e55b6 AH |
1494 | |
1495 | put_card = (mmc_tot_in_flight(mq) == 0); | |
1496 | ||
1497 | mmc_cqe_check_busy(mq); | |
1498 | ||
f5d72c5c | 1499 | spin_unlock_irqrestore(&mq->lock, flags); |
1e8e55b6 AH |
1500 | |
1501 | if (!mq->cqe_busy) | |
1502 | blk_mq_run_hw_queues(q, true); | |
1503 | ||
1504 | if (put_card) | |
1505 | mmc_put_card(mq->card, &mq->ctx); | |
1506 | } | |
1507 | ||
1508 | void mmc_blk_cqe_recovery(struct mmc_queue *mq) | |
1509 | { | |
1510 | struct mmc_card *card = mq->card; | |
1511 | struct mmc_host *host = card->host; | |
1512 | int err; | |
1513 | ||
1514 | pr_debug("%s: CQE recovery start\n", mmc_hostname(host)); | |
1515 | ||
1516 | err = mmc_cqe_recovery(host); | |
1517 | if (err) | |
1518 | mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY); | |
a051246b | 1519 | mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY); |
1e8e55b6 AH |
1520 | |
1521 | pr_debug("%s: CQE recovery done\n", mmc_hostname(host)); | |
1522 | } | |
1523 | ||
1524 | static void mmc_blk_cqe_req_done(struct mmc_request *mrq) | |
1525 | { | |
1526 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
1527 | brq.mrq); | |
1528 | struct request *req = mmc_queue_req_to_req(mqrq); | |
1529 | struct request_queue *q = req->q; | |
1530 | struct mmc_queue *mq = q->queuedata; | |
1531 | ||
1532 | /* | |
1533 | * Block layer timeouts race with completions which means the normal | |
1534 | * completion path cannot be used during recovery. | |
1535 | */ | |
1536 | if (mq->in_recovery) | |
1537 | mmc_blk_cqe_complete_rq(mq, req); | |
15f73f5b | 1538 | else if (likely(!blk_should_fake_timeout(req->q))) |
1e8e55b6 AH |
1539 | blk_mq_complete_request(req); |
1540 | } | |
1541 | ||
1542 | static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq) | |
1543 | { | |
1544 | mrq->done = mmc_blk_cqe_req_done; | |
1545 | mrq->recovery_notifier = mmc_cqe_recovery_notifier; | |
1546 | ||
1547 | return mmc_cqe_start_req(host, mrq); | |
1548 | } | |
1549 | ||
1550 | static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq, | |
1551 | struct request *req) | |
1552 | { | |
1553 | struct mmc_blk_request *brq = &mqrq->brq; | |
1554 | ||
1555 | memset(brq, 0, sizeof(*brq)); | |
1556 | ||
1557 | brq->mrq.cmd = &brq->cmd; | |
1558 | brq->mrq.tag = req->tag; | |
1559 | ||
1560 | return &brq->mrq; | |
1561 | } | |
1562 | ||
1563 | static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req) | |
1564 | { | |
1565 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1566 | struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req); | |
1567 | ||
1568 | mrq->cmd->opcode = MMC_SWITCH; | |
1569 | mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
1570 | (EXT_CSD_FLUSH_CACHE << 16) | | |
1571 | (1 << 8) | | |
1572 | EXT_CSD_CMD_SET_NORMAL; | |
1573 | mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B; | |
1574 | ||
1575 | return mmc_blk_cqe_start_req(mq->card->host, mrq); | |
1576 | } | |
1577 | ||
511ce378 BW |
1578 | static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req) |
1579 | { | |
1580 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1581 | struct mmc_host *host = mq->card->host; | |
1582 | int err; | |
1583 | ||
1584 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); | |
1585 | mqrq->brq.mrq.done = mmc_blk_hsq_req_done; | |
1586 | mmc_pre_req(host, &mqrq->brq.mrq); | |
1587 | ||
1588 | err = mmc_cqe_start_req(host, &mqrq->brq.mrq); | |
1589 | if (err) | |
1590 | mmc_post_req(host, &mqrq->brq.mrq, err); | |
1591 | ||
1592 | return err; | |
1593 | } | |
1594 | ||
1e8e55b6 AH |
1595 | static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req) |
1596 | { | |
1597 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
511ce378 BW |
1598 | struct mmc_host *host = mq->card->host; |
1599 | ||
1600 | if (host->hsq_enabled) | |
1601 | return mmc_blk_hsq_issue_rw_rq(mq, req); | |
1e8e55b6 AH |
1602 | |
1603 | mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL); | |
1604 | ||
1605 | return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq); | |
1606 | } | |
1607 | ||
ca5717f7 AH |
1608 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
1609 | struct mmc_card *card, | |
b3fa3e6d | 1610 | int recovery_mode, |
ca5717f7 AH |
1611 | struct mmc_queue *mq) |
1612 | { | |
1613 | u32 readcmd, writecmd; | |
1614 | struct mmc_blk_request *brq = &mqrq->brq; | |
67e69d52 | 1615 | struct request *req = mmc_queue_req_to_req(mqrq); |
ca5717f7 AH |
1616 | struct mmc_blk_data *md = mq->blkdata; |
1617 | bool do_rel_wr, do_data_tag; | |
1618 | ||
b3fa3e6d | 1619 | mmc_blk_data_prep(mq, mqrq, recovery_mode, &do_rel_wr, &do_data_tag); |
ca5717f7 AH |
1620 | |
1621 | brq->mrq.cmd = &brq->cmd; | |
1622 | ||
1623 | brq->cmd.arg = blk_rq_pos(req); | |
1624 | if (!mmc_card_blockaddr(card)) | |
1625 | brq->cmd.arg <<= 9; | |
1626 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | |
1627 | ||
54d49d77 PF |
1628 | if (brq->data.blocks > 1 || do_rel_wr) { |
1629 | /* SPI multiblock writes terminate using a special | |
1630 | * token, not a STOP_TRANSMISSION request. | |
d0c97cfb | 1631 | */ |
54d49d77 PF |
1632 | if (!mmc_host_is_spi(card->host) || |
1633 | rq_data_dir(req) == READ) | |
1634 | brq->mrq.stop = &brq->stop; | |
1635 | readcmd = MMC_READ_MULTIPLE_BLOCK; | |
1636 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | |
1637 | } else { | |
1638 | brq->mrq.stop = NULL; | |
1639 | readcmd = MMC_READ_SINGLE_BLOCK; | |
1640 | writecmd = MMC_WRITE_BLOCK; | |
1641 | } | |
ca5717f7 | 1642 | brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; |
4265900e | 1643 | |
54d49d77 PF |
1644 | /* |
1645 | * Pre-defined multi-block transfers are preferable to | |
1646 | * open ended-ones (and necessary for reliable writes). | |
1647 | * However, it is not sufficient to just send CMD23, | |
1648 | * and avoid the final CMD12, as on an error condition | |
1649 | * CMD12 (stop) needs to be sent anyway. This, coupled | |
1650 | * with Auto-CMD23 enhancements provided by some | |
1651 | * hosts, means that the complexity of dealing | |
1652 | * with this is best left to the host. If CMD23 is | |
1653 | * supported by card and host, we'll fill sbc in and let | |
1654 | * the host deal with handling it correctly. This means | |
1655 | * that for hosts that don't expose MMC_CAP_CMD23, no | |
1656 | * change of behavior will be observed. | |
1657 | * | |
1658 | * N.B: Some MMC cards experience perf degradation. | |
1659 | * We'll avoid using CMD23-bounded multiblock writes for | |
1660 | * these, while retaining features like reliable writes. | |
1661 | */ | |
4265900e SD |
1662 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
1663 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | |
1664 | do_data_tag)) { | |
54d49d77 PF |
1665 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
1666 | brq->sbc.arg = brq->data.blocks | | |
4265900e SD |
1667 | (do_rel_wr ? (1 << 31) : 0) | |
1668 | (do_data_tag ? (1 << 29) : 0); | |
54d49d77 PF |
1669 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1670 | brq->mrq.sbc = &brq->sbc; | |
1671 | } | |
54d49d77 | 1672 | } |
6a79e391 | 1673 | |
81196976 | 1674 | #define MMC_MAX_RETRIES 5 |
7eb43d53 | 1675 | #define MMC_DATA_RETRIES 2 |
81196976 AH |
1676 | #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1) |
1677 | ||
7eb43d53 AH |
1678 | static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout) |
1679 | { | |
1680 | struct mmc_command cmd = { | |
1681 | .opcode = MMC_STOP_TRANSMISSION, | |
1682 | .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC, | |
1683 | /* Some hosts wait for busy anyway, so provide a busy timeout */ | |
1684 | .busy_timeout = timeout, | |
1685 | }; | |
1686 | ||
1687 | return mmc_wait_for_cmd(card->host, &cmd, 5); | |
1688 | } | |
1689 | ||
1690 | static int mmc_blk_fix_state(struct mmc_card *card, struct request *req) | |
1691 | { | |
1692 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1693 | struct mmc_blk_request *brq = &mqrq->brq; | |
1694 | unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data); | |
1695 | int err; | |
1696 | ||
1697 | mmc_retune_hold_now(card->host); | |
1698 | ||
1699 | mmc_blk_send_stop(card, timeout); | |
1700 | ||
972d5084 | 1701 | err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO); |
7eb43d53 AH |
1702 | |
1703 | mmc_retune_release(card->host); | |
1704 | ||
1705 | return err; | |
1706 | } | |
1707 | ||
81196976 AH |
1708 | #define MMC_READ_SINGLE_RETRIES 2 |
1709 | ||
b3fa3e6d | 1710 | /* Single (native) sector read during recovery */ |
81196976 AH |
1711 | static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req) |
1712 | { | |
1713 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1714 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
1715 | struct mmc_card *card = mq->card; | |
1716 | struct mmc_host *host = card->host; | |
1717 | blk_status_t error = BLK_STS_OK; | |
b3fa3e6d | 1718 | size_t bytes_per_read = queue_physical_block_size(mq->queue); |
81196976 AH |
1719 | |
1720 | do { | |
1721 | u32 status; | |
1722 | int err; | |
54309fde | 1723 | int retries = 0; |
81196976 | 1724 | |
54309fde CL |
1725 | while (retries++ <= MMC_READ_SINGLE_RETRIES) { |
1726 | mmc_blk_rw_rq_prep(mqrq, card, 1, mq); | |
81196976 | 1727 | |
54309fde | 1728 | mmc_wait_for_req(host, mrq); |
81196976 | 1729 | |
54309fde | 1730 | err = mmc_send_status(card, &status); |
81196976 AH |
1731 | if (err) |
1732 | goto error_exit; | |
81196976 | 1733 | |
54309fde CL |
1734 | if (!mmc_host_is_spi(host) && |
1735 | !mmc_ready_for_data(status)) { | |
1736 | err = mmc_blk_fix_state(card, req); | |
1737 | if (err) | |
1738 | goto error_exit; | |
1739 | } | |
81196976 | 1740 | |
54309fde CL |
1741 | if (!mrq->cmd->error) |
1742 | break; | |
1743 | } | |
81196976 AH |
1744 | |
1745 | if (mrq->cmd->error || | |
1746 | mrq->data->error || | |
1747 | (!mmc_host_is_spi(host) && | |
1748 | (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS))) | |
1749 | error = BLK_STS_IOERR; | |
1750 | else | |
1751 | error = BLK_STS_OK; | |
1752 | ||
b3fa3e6d | 1753 | } while (blk_update_request(req, error, bytes_per_read)); |
81196976 AH |
1754 | |
1755 | return; | |
1756 | ||
1757 | error_exit: | |
1758 | mrq->data->bytes_xfered = 0; | |
b3fa3e6d | 1759 | blk_update_request(req, BLK_STS_IOERR, bytes_per_read); |
81196976 AH |
1760 | /* Let it try the remaining request again */ |
1761 | if (mqrq->retries > MMC_MAX_RETRIES - 1) | |
1762 | mqrq->retries = MMC_MAX_RETRIES - 1; | |
1763 | } | |
1764 | ||
7eb43d53 AH |
1765 | static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq) |
1766 | { | |
1767 | return !!brq->mrq.sbc; | |
1768 | } | |
1769 | ||
1770 | static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq) | |
1771 | { | |
1772 | return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR; | |
1773 | } | |
1774 | ||
1775 | /* | |
1776 | * Check for errors the host controller driver might not have seen such as | |
1777 | * response mode errors or invalid card state. | |
1778 | */ | |
1779 | static bool mmc_blk_status_error(struct request *req, u32 status) | |
1780 | { | |
1781 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1782 | struct mmc_blk_request *brq = &mqrq->brq; | |
1783 | struct mmc_queue *mq = req->q->queuedata; | |
1784 | u32 stop_err_bits; | |
1785 | ||
1786 | if (mmc_host_is_spi(mq->card->host)) | |
aa950144 | 1787 | return false; |
7eb43d53 AH |
1788 | |
1789 | stop_err_bits = mmc_blk_stop_err_bits(brq); | |
1790 | ||
1791 | return brq->cmd.resp[0] & CMD_ERRORS || | |
1792 | brq->stop.resp[0] & stop_err_bits || | |
1793 | status & stop_err_bits || | |
40c96853 | 1794 | (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status)); |
7eb43d53 AH |
1795 | } |
1796 | ||
1797 | static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq) | |
1798 | { | |
1799 | return !brq->sbc.error && !brq->cmd.error && | |
1800 | !(brq->cmd.resp[0] & CMD_ERRORS); | |
1801 | } | |
1802 | ||
1803 | /* | |
1804 | * Requests are completed by mmc_blk_mq_complete_rq() which sets simple | |
1805 | * policy: | |
1806 | * 1. A request that has transferred at least some data is considered | |
1807 | * successful and will be requeued if there is remaining data to | |
1808 | * transfer. | |
1809 | * 2. Otherwise the number of retries is incremented and the request | |
1810 | * will be requeued if there are remaining retries. | |
1811 | * 3. Otherwise the request will be errored out. | |
1812 | * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and | |
1813 | * mqrq->retries. So there are only 4 possible actions here: | |
1814 | * 1. do not accept the bytes_xfered value i.e. set it to zero | |
1815 | * 2. change mqrq->retries to determine the number of retries | |
1816 | * 3. try to reset the card | |
1817 | * 4. read one sector at a time | |
1818 | */ | |
81196976 AH |
1819 | static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req) |
1820 | { | |
1821 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
1822 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
1823 | struct mmc_blk_request *brq = &mqrq->brq; | |
1824 | struct mmc_blk_data *md = mq->blkdata; | |
1825 | struct mmc_card *card = mq->card; | |
7eb43d53 AH |
1826 | u32 status; |
1827 | u32 blocks; | |
1828 | int err; | |
81196976 | 1829 | |
7eb43d53 AH |
1830 | /* |
1831 | * Some errors the host driver might not have seen. Set the number of | |
1832 | * bytes transferred to zero in that case. | |
1833 | */ | |
1834 | err = __mmc_send_status(card, &status, 0); | |
1835 | if (err || mmc_blk_status_error(req, status)) | |
1836 | brq->data.bytes_xfered = 0; | |
81196976 AH |
1837 | |
1838 | mmc_retune_release(card->host); | |
1839 | ||
1840 | /* | |
7eb43d53 AH |
1841 | * Try again to get the status. This also provides an opportunity for |
1842 | * re-tuning. | |
81196976 | 1843 | */ |
7eb43d53 AH |
1844 | if (err) |
1845 | err = __mmc_send_status(card, &status, 0); | |
81196976 | 1846 | |
7eb43d53 AH |
1847 | /* |
1848 | * Nothing more to do after the number of bytes transferred has been | |
1849 | * updated and there is no card. | |
1850 | */ | |
1851 | if (err && mmc_detect_card_removed(card->host)) | |
1852 | return; | |
81196976 | 1853 | |
7eb43d53 AH |
1854 | /* Try to get back to "tran" state */ |
1855 | if (!mmc_host_is_spi(mq->card->host) && | |
40c96853 | 1856 | (err || !mmc_ready_for_data(status))) |
7eb43d53 AH |
1857 | err = mmc_blk_fix_state(mq->card, req); |
1858 | ||
1859 | /* | |
1860 | * Special case for SD cards where the card might record the number of | |
1861 | * blocks written. | |
1862 | */ | |
1863 | if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) && | |
1864 | rq_data_dir(req) == WRITE) { | |
1865 | if (mmc_sd_num_wr_blocks(card, &blocks)) | |
1866 | brq->data.bytes_xfered = 0; | |
1867 | else | |
1868 | brq->data.bytes_xfered = blocks << 9; | |
81196976 | 1869 | } |
7eb43d53 AH |
1870 | |
1871 | /* Reset if the card is in a bad state */ | |
1872 | if (!mmc_host_is_spi(mq->card->host) && | |
1873 | err && mmc_blk_reset(md, card->host, type)) { | |
f3fa33ac | 1874 | pr_err("%s: recovery failed!\n", req->q->disk->disk_name); |
81196976 | 1875 | mqrq->retries = MMC_NO_RETRIES; |
7eb43d53 AH |
1876 | return; |
1877 | } | |
1878 | ||
1879 | /* | |
1880 | * If anything was done, just return and if there is anything remaining | |
1881 | * on the request it will get requeued. | |
1882 | */ | |
1883 | if (brq->data.bytes_xfered) | |
1884 | return; | |
1885 | ||
1886 | /* Reset before last retry */ | |
406e1480 CL |
1887 | if (mqrq->retries + 1 == MMC_MAX_RETRIES && |
1888 | mmc_blk_reset(md, card->host, type)) | |
1889 | return; | |
7eb43d53 AH |
1890 | |
1891 | /* Command errors fail fast, so use all MMC_MAX_RETRIES */ | |
1892 | if (brq->sbc.error || brq->cmd.error) | |
1893 | return; | |
1894 | ||
1895 | /* Reduce the remaining retries for data errors */ | |
1896 | if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) { | |
1897 | mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES; | |
1898 | return; | |
1899 | } | |
1900 | ||
b3fa3e6d CL |
1901 | if (rq_data_dir(req) == READ && brq->data.blocks > |
1902 | queue_physical_block_size(mq->queue) >> 9) { | |
1903 | /* Read one (native) sector at a time */ | |
7eb43d53 AH |
1904 | mmc_blk_read_single(mq, req); |
1905 | return; | |
81196976 AH |
1906 | } |
1907 | } | |
1908 | ||
10f21df4 AH |
1909 | static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq) |
1910 | { | |
1911 | mmc_blk_eval_resp_error(brq); | |
1912 | ||
1913 | return brq->sbc.error || brq->cmd.error || brq->stop.error || | |
1914 | brq->data.error || brq->cmd.resp[0] & CMD_ERRORS; | |
1915 | } | |
1916 | ||
5d435933 CL |
1917 | static int mmc_spi_err_check(struct mmc_card *card) |
1918 | { | |
1919 | u32 status = 0; | |
1920 | int err; | |
1921 | ||
1922 | /* | |
1923 | * SPI does not have a TRAN state we have to wait on, instead the | |
1924 | * card is ready again when it no longer holds the line LOW. | |
1925 | * We still have to ensure two things here before we know the write | |
1926 | * was successful: | |
1927 | * 1. The card has not disconnected during busy and we actually read our | |
1928 | * own pull-up, thinking it was still connected, so ensure it | |
1929 | * still responds. | |
1930 | * 2. Check for any error bits, in particular R1_SPI_IDLE to catch a | |
1931 | * just reconnected card after being disconnected during busy. | |
1932 | */ | |
1933 | err = __mmc_send_status(card, &status, 0); | |
1934 | if (err) | |
1935 | return err; | |
1936 | /* All R1 and R2 bits of SPI are errors in our case */ | |
1937 | if (status) | |
1938 | return -EIO; | |
1939 | return 0; | |
1940 | } | |
1941 | ||
6966e609 UH |
1942 | static int mmc_blk_busy_cb(void *cb_data, bool *busy) |
1943 | { | |
1944 | struct mmc_blk_busy_data *data = cb_data; | |
1945 | u32 status = 0; | |
1946 | int err; | |
1947 | ||
1948 | err = mmc_send_status(data->card, &status); | |
1949 | if (err) | |
1950 | return err; | |
1951 | ||
1952 | /* Accumulate response error bits. */ | |
1953 | data->status |= status; | |
1954 | ||
1955 | *busy = !mmc_ready_for_data(status); | |
1956 | return 0; | |
1957 | } | |
1958 | ||
88a51646 AH |
1959 | static int mmc_blk_card_busy(struct mmc_card *card, struct request *req) |
1960 | { | |
1961 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
6966e609 | 1962 | struct mmc_blk_busy_data cb_data; |
88a51646 AH |
1963 | int err; |
1964 | ||
5d435933 | 1965 | if (rq_data_dir(req) == READ) |
88a51646 AH |
1966 | return 0; |
1967 | ||
5d435933 CL |
1968 | if (mmc_host_is_spi(card->host)) { |
1969 | err = mmc_spi_err_check(card); | |
1970 | if (err) | |
1971 | mqrq->brq.data.bytes_xfered = 0; | |
1972 | return err; | |
1973 | } | |
1974 | ||
6966e609 UH |
1975 | cb_data.card = card; |
1976 | cb_data.status = 0; | |
1760fdb6 | 1977 | err = __mmc_poll_for_busy(card->host, 0, MMC_BLK_TIMEOUT_MS, |
2ebbdace | 1978 | &mmc_blk_busy_cb, &cb_data); |
88a51646 | 1979 | |
f47a1fe3 AH |
1980 | /* |
1981 | * Do not assume data transferred correctly if there are any error bits | |
1982 | * set. | |
1983 | */ | |
6966e609 | 1984 | if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) { |
f47a1fe3 | 1985 | mqrq->brq.data.bytes_xfered = 0; |
88a51646 AH |
1986 | err = err ? err : -EIO; |
1987 | } | |
1988 | ||
f47a1fe3 | 1989 | /* Copy the exception bit so it will be seen later on */ |
6966e609 | 1990 | if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT) |
f47a1fe3 AH |
1991 | mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT; |
1992 | ||
88a51646 AH |
1993 | return err; |
1994 | } | |
1995 | ||
10f21df4 AH |
1996 | static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq, |
1997 | struct request *req) | |
1998 | { | |
1999 | int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | |
2000 | ||
2001 | mmc_blk_reset_success(mq->blkdata, type); | |
2002 | } | |
2003 | ||
81196976 AH |
2004 | static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req) |
2005 | { | |
2006 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2007 | unsigned int nr_bytes = mqrq->brq.data.bytes_xfered; | |
2008 | ||
2009 | if (nr_bytes) { | |
2010 | if (blk_update_request(req, BLK_STS_OK, nr_bytes)) | |
2011 | blk_mq_requeue_request(req, true); | |
2012 | else | |
2013 | __blk_mq_end_request(req, BLK_STS_OK); | |
2014 | } else if (!blk_rq_bytes(req)) { | |
2015 | __blk_mq_end_request(req, BLK_STS_IOERR); | |
2016 | } else if (mqrq->retries++ < MMC_MAX_RETRIES) { | |
2017 | blk_mq_requeue_request(req, true); | |
2018 | } else { | |
2019 | if (mmc_card_removed(mq->card)) | |
2020 | req->rq_flags |= RQF_QUIET; | |
2021 | blk_mq_end_request(req, BLK_STS_IOERR); | |
2022 | } | |
2023 | } | |
2024 | ||
2025 | static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq, | |
2026 | struct mmc_queue_req *mqrq) | |
2027 | { | |
2028 | return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) && | |
2029 | (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT || | |
2030 | mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT); | |
2031 | } | |
2032 | ||
2033 | static void mmc_blk_urgent_bkops(struct mmc_queue *mq, | |
2034 | struct mmc_queue_req *mqrq) | |
2035 | { | |
2036 | if (mmc_blk_urgent_bkops_needed(mq, mqrq)) | |
0c204979 | 2037 | mmc_run_bkops(mq->card); |
81196976 AH |
2038 | } |
2039 | ||
511ce378 BW |
2040 | static void mmc_blk_hsq_req_done(struct mmc_request *mrq) |
2041 | { | |
2042 | struct mmc_queue_req *mqrq = | |
2043 | container_of(mrq, struct mmc_queue_req, brq.mrq); | |
2044 | struct request *req = mmc_queue_req_to_req(mqrq); | |
2045 | struct request_queue *q = req->q; | |
2046 | struct mmc_queue *mq = q->queuedata; | |
2047 | struct mmc_host *host = mq->card->host; | |
2048 | unsigned long flags; | |
2049 | ||
2050 | if (mmc_blk_rq_error(&mqrq->brq) || | |
2051 | mmc_blk_urgent_bkops_needed(mq, mqrq)) { | |
2052 | spin_lock_irqsave(&mq->lock, flags); | |
2053 | mq->recovery_needed = true; | |
2054 | mq->recovery_req = req; | |
2055 | spin_unlock_irqrestore(&mq->lock, flags); | |
2056 | ||
2057 | host->cqe_ops->cqe_recovery_start(host); | |
2058 | ||
2059 | schedule_work(&mq->recovery_work); | |
2060 | return; | |
2061 | } | |
2062 | ||
2063 | mmc_blk_rw_reset_success(mq, req); | |
2064 | ||
2065 | /* | |
2066 | * Block layer timeouts race with completions which means the normal | |
2067 | * completion path cannot be used during recovery. | |
2068 | */ | |
2069 | if (mq->in_recovery) | |
2070 | mmc_blk_cqe_complete_rq(mq, req); | |
15f73f5b | 2071 | else if (likely(!blk_should_fake_timeout(req->q))) |
511ce378 BW |
2072 | blk_mq_complete_request(req); |
2073 | } | |
2074 | ||
81196976 AH |
2075 | void mmc_blk_mq_complete(struct request *req) |
2076 | { | |
2077 | struct mmc_queue *mq = req->q->queuedata; | |
407a1c57 | 2078 | struct mmc_host *host = mq->card->host; |
81196976 | 2079 | |
407a1c57 | 2080 | if (host->cqe_enabled) |
1e8e55b6 | 2081 | mmc_blk_cqe_complete_rq(mq, req); |
15f73f5b | 2082 | else if (likely(!blk_should_fake_timeout(req->q))) |
1e8e55b6 | 2083 | mmc_blk_mq_complete_rq(mq, req); |
81196976 AH |
2084 | } |
2085 | ||
2086 | static void mmc_blk_mq_poll_completion(struct mmc_queue *mq, | |
2087 | struct request *req) | |
2088 | { | |
2089 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
88a51646 | 2090 | struct mmc_host *host = mq->card->host; |
81196976 | 2091 | |
88a51646 AH |
2092 | if (mmc_blk_rq_error(&mqrq->brq) || |
2093 | mmc_blk_card_busy(mq->card, req)) { | |
2094 | mmc_blk_mq_rw_recovery(mq, req); | |
2095 | } else { | |
2096 | mmc_blk_rw_reset_success(mq, req); | |
2097 | mmc_retune_release(host); | |
2098 | } | |
81196976 AH |
2099 | |
2100 | mmc_blk_urgent_bkops(mq, mqrq); | |
2101 | } | |
2102 | ||
4b430d4a | 2103 | static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, enum mmc_issue_type issue_type) |
81196976 | 2104 | { |
81196976 AH |
2105 | unsigned long flags; |
2106 | bool put_card; | |
2107 | ||
f5d72c5c | 2108 | spin_lock_irqsave(&mq->lock, flags); |
81196976 | 2109 | |
4b430d4a | 2110 | mq->in_flight[issue_type] -= 1; |
81196976 AH |
2111 | |
2112 | put_card = (mmc_tot_in_flight(mq) == 0); | |
2113 | ||
f5d72c5c | 2114 | spin_unlock_irqrestore(&mq->lock, flags); |
81196976 AH |
2115 | |
2116 | if (put_card) | |
2117 | mmc_put_card(mq->card, &mq->ctx); | |
2118 | } | |
2119 | ||
639d3531 SAS |
2120 | static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req, |
2121 | bool can_sleep) | |
81196976 | 2122 | { |
4b430d4a | 2123 | enum mmc_issue_type issue_type = mmc_issue_type(mq, req); |
81196976 AH |
2124 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); |
2125 | struct mmc_request *mrq = &mqrq->brq.mrq; | |
2126 | struct mmc_host *host = mq->card->host; | |
2127 | ||
2128 | mmc_post_req(host, mrq, 0); | |
2129 | ||
10f21df4 AH |
2130 | /* |
2131 | * Block layer timeouts race with completions which means the normal | |
2132 | * completion path cannot be used during recovery. | |
2133 | */ | |
639d3531 | 2134 | if (mq->in_recovery) { |
10f21df4 | 2135 | mmc_blk_mq_complete_rq(mq, req); |
639d3531 SAS |
2136 | } else if (likely(!blk_should_fake_timeout(req->q))) { |
2137 | if (can_sleep) | |
2138 | blk_mq_complete_request_direct(req, mmc_blk_mq_complete); | |
2139 | else | |
2140 | blk_mq_complete_request(req); | |
2141 | } | |
81196976 | 2142 | |
4b430d4a | 2143 | mmc_blk_mq_dec_in_flight(mq, issue_type); |
81196976 AH |
2144 | } |
2145 | ||
10f21df4 AH |
2146 | void mmc_blk_mq_recovery(struct mmc_queue *mq) |
2147 | { | |
2148 | struct request *req = mq->recovery_req; | |
2149 | struct mmc_host *host = mq->card->host; | |
2150 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2151 | ||
2152 | mq->recovery_req = NULL; | |
2153 | mq->rw_wait = false; | |
2154 | ||
2155 | if (mmc_blk_rq_error(&mqrq->brq)) { | |
2156 | mmc_retune_hold_now(host); | |
2157 | mmc_blk_mq_rw_recovery(mq, req); | |
2158 | } | |
2159 | ||
2160 | mmc_blk_urgent_bkops(mq, mqrq); | |
2161 | ||
639d3531 | 2162 | mmc_blk_mq_post_req(mq, req, true); |
10f21df4 AH |
2163 | } |
2164 | ||
81196976 AH |
2165 | static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq, |
2166 | struct request **prev_req) | |
2167 | { | |
10f21df4 AH |
2168 | if (mmc_host_done_complete(mq->card->host)) |
2169 | return; | |
2170 | ||
81196976 AH |
2171 | mutex_lock(&mq->complete_lock); |
2172 | ||
2173 | if (!mq->complete_req) | |
2174 | goto out_unlock; | |
2175 | ||
2176 | mmc_blk_mq_poll_completion(mq, mq->complete_req); | |
2177 | ||
2178 | if (prev_req) | |
2179 | *prev_req = mq->complete_req; | |
2180 | else | |
639d3531 | 2181 | mmc_blk_mq_post_req(mq, mq->complete_req, true); |
81196976 AH |
2182 | |
2183 | mq->complete_req = NULL; | |
2184 | ||
2185 | out_unlock: | |
2186 | mutex_unlock(&mq->complete_lock); | |
2187 | } | |
2188 | ||
2189 | void mmc_blk_mq_complete_work(struct work_struct *work) | |
2190 | { | |
2191 | struct mmc_queue *mq = container_of(work, struct mmc_queue, | |
2192 | complete_work); | |
2193 | ||
2194 | mmc_blk_mq_complete_prev_req(mq, NULL); | |
2195 | } | |
2196 | ||
2197 | static void mmc_blk_mq_req_done(struct mmc_request *mrq) | |
2198 | { | |
2199 | struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, | |
2200 | brq.mrq); | |
2201 | struct request *req = mmc_queue_req_to_req(mqrq); | |
2202 | struct request_queue *q = req->q; | |
2203 | struct mmc_queue *mq = q->queuedata; | |
10f21df4 | 2204 | struct mmc_host *host = mq->card->host; |
81196976 | 2205 | unsigned long flags; |
81196976 | 2206 | |
10f21df4 AH |
2207 | if (!mmc_host_done_complete(host)) { |
2208 | bool waiting; | |
81196976 | 2209 | |
10f21df4 AH |
2210 | /* |
2211 | * We cannot complete the request in this context, so record | |
2212 | * that there is a request to complete, and that a following | |
2213 | * request does not need to wait (although it does need to | |
2214 | * complete complete_req first). | |
2215 | */ | |
f5d72c5c | 2216 | spin_lock_irqsave(&mq->lock, flags); |
10f21df4 AH |
2217 | mq->complete_req = req; |
2218 | mq->rw_wait = false; | |
2219 | waiting = mq->waiting; | |
f5d72c5c | 2220 | spin_unlock_irqrestore(&mq->lock, flags); |
10f21df4 AH |
2221 | |
2222 | /* | |
2223 | * If 'waiting' then the waiting task will complete this | |
2224 | * request, otherwise queue a work to do it. Note that | |
2225 | * complete_work may still race with the dispatch of a following | |
2226 | * request. | |
2227 | */ | |
2228 | if (waiting) | |
2229 | wake_up(&mq->wait); | |
2230 | else | |
dcf6e2e3 | 2231 | queue_work(mq->card->complete_wq, &mq->complete_work); |
10f21df4 AH |
2232 | |
2233 | return; | |
2234 | } | |
2235 | ||
2236 | /* Take the recovery path for errors or urgent background operations */ | |
2237 | if (mmc_blk_rq_error(&mqrq->brq) || | |
2238 | mmc_blk_urgent_bkops_needed(mq, mqrq)) { | |
f5d72c5c | 2239 | spin_lock_irqsave(&mq->lock, flags); |
10f21df4 AH |
2240 | mq->recovery_needed = true; |
2241 | mq->recovery_req = req; | |
f5d72c5c | 2242 | spin_unlock_irqrestore(&mq->lock, flags); |
81196976 | 2243 | wake_up(&mq->wait); |
10f21df4 AH |
2244 | schedule_work(&mq->recovery_work); |
2245 | return; | |
2246 | } | |
2247 | ||
2248 | mmc_blk_rw_reset_success(mq, req); | |
2249 | ||
2250 | mq->rw_wait = false; | |
2251 | wake_up(&mq->wait); | |
2252 | ||
639d3531 SAS |
2253 | /* context unknown */ |
2254 | mmc_blk_mq_post_req(mq, req, false); | |
81196976 AH |
2255 | } |
2256 | ||
2257 | static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err) | |
2258 | { | |
81196976 AH |
2259 | unsigned long flags; |
2260 | bool done; | |
2261 | ||
2262 | /* | |
10f21df4 AH |
2263 | * Wait while there is another request in progress, but not if recovery |
2264 | * is needed. Also indicate whether there is a request waiting to start. | |
81196976 | 2265 | */ |
f5d72c5c | 2266 | spin_lock_irqsave(&mq->lock, flags); |
10f21df4 AH |
2267 | if (mq->recovery_needed) { |
2268 | *err = -EBUSY; | |
2269 | done = true; | |
2270 | } else { | |
2271 | done = !mq->rw_wait; | |
2272 | } | |
81196976 | 2273 | mq->waiting = !done; |
f5d72c5c | 2274 | spin_unlock_irqrestore(&mq->lock, flags); |
81196976 AH |
2275 | |
2276 | return done; | |
2277 | } | |
2278 | ||
2279 | static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req) | |
2280 | { | |
2281 | int err = 0; | |
2282 | ||
2283 | wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err)); | |
2284 | ||
2285 | /* Always complete the previous request if there is one */ | |
2286 | mmc_blk_mq_complete_prev_req(mq, prev_req); | |
2287 | ||
2288 | return err; | |
2289 | } | |
2290 | ||
2291 | static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq, | |
2292 | struct request *req) | |
2293 | { | |
2294 | struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); | |
2295 | struct mmc_host *host = mq->card->host; | |
2296 | struct request *prev_req = NULL; | |
2297 | int err = 0; | |
2298 | ||
2299 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); | |
2300 | ||
2301 | mqrq->brq.mrq.done = mmc_blk_mq_req_done; | |
2302 | ||
2303 | mmc_pre_req(host, &mqrq->brq.mrq); | |
2304 | ||
2305 | err = mmc_blk_rw_wait(mq, &prev_req); | |
2306 | if (err) | |
2307 | goto out_post_req; | |
2308 | ||
2309 | mq->rw_wait = true; | |
2310 | ||
2311 | err = mmc_start_request(host, &mqrq->brq.mrq); | |
2312 | ||
2313 | if (prev_req) | |
639d3531 | 2314 | mmc_blk_mq_post_req(mq, prev_req, true); |
81196976 | 2315 | |
10f21df4 | 2316 | if (err) |
81196976 | 2317 | mq->rw_wait = false; |
10f21df4 AH |
2318 | |
2319 | /* Release re-tuning here where there is no synchronization required */ | |
2320 | if (err || mmc_host_done_complete(host)) | |
81196976 | 2321 | mmc_retune_release(host); |
81196976 AH |
2322 | |
2323 | out_post_req: | |
2324 | if (err) | |
2325 | mmc_post_req(host, &mqrq->brq.mrq, err); | |
2326 | ||
2327 | return err; | |
2328 | } | |
2329 | ||
2330 | static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host) | |
2331 | { | |
407a1c57 | 2332 | if (host->cqe_enabled) |
1e8e55b6 AH |
2333 | return host->cqe_ops->cqe_wait_for_idle(host); |
2334 | ||
81196976 AH |
2335 | return mmc_blk_rw_wait(mq, NULL); |
2336 | } | |
2337 | ||
2338 | enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req) | |
2339 | { | |
2340 | struct mmc_blk_data *md = mq->blkdata; | |
2341 | struct mmc_card *card = md->queue.card; | |
2342 | struct mmc_host *host = card->host; | |
2343 | int ret; | |
2344 | ||
2345 | ret = mmc_blk_part_switch(card, md->part_type); | |
2346 | if (ret) | |
2347 | return MMC_REQ_FAILED_TO_START; | |
2348 | ||
2349 | switch (mmc_issue_type(mq, req)) { | |
2350 | case MMC_ISSUE_SYNC: | |
2351 | ret = mmc_blk_wait_for_idle(mq, host); | |
2352 | if (ret) | |
2353 | return MMC_REQ_BUSY; | |
2354 | switch (req_op(req)) { | |
2355 | case REQ_OP_DRV_IN: | |
2356 | case REQ_OP_DRV_OUT: | |
2357 | mmc_blk_issue_drv_op(mq, req); | |
2358 | break; | |
2359 | case REQ_OP_DISCARD: | |
2360 | mmc_blk_issue_discard_rq(mq, req); | |
2361 | break; | |
2362 | case REQ_OP_SECURE_ERASE: | |
2363 | mmc_blk_issue_secdiscard_rq(mq, req); | |
2364 | break; | |
f7b6fc32 VW |
2365 | case REQ_OP_WRITE_ZEROES: |
2366 | mmc_blk_issue_trim_rq(mq, req); | |
2367 | break; | |
81196976 AH |
2368 | case REQ_OP_FLUSH: |
2369 | mmc_blk_issue_flush(mq, req); | |
2370 | break; | |
2371 | default: | |
2372 | WARN_ON_ONCE(1); | |
2373 | return MMC_REQ_FAILED_TO_START; | |
2374 | } | |
2375 | return MMC_REQ_FINISHED; | |
1e8e55b6 | 2376 | case MMC_ISSUE_DCMD: |
81196976 AH |
2377 | case MMC_ISSUE_ASYNC: |
2378 | switch (req_op(req)) { | |
1e8e55b6 | 2379 | case REQ_OP_FLUSH: |
97fce126 AA |
2380 | if (!mmc_cache_enabled(host)) { |
2381 | blk_mq_end_request(req, BLK_STS_OK); | |
2382 | return MMC_REQ_FINISHED; | |
2383 | } | |
1e8e55b6 AH |
2384 | ret = mmc_blk_cqe_issue_flush(mq, req); |
2385 | break; | |
81196976 | 2386 | case REQ_OP_WRITE: |
ed9009ad BH |
2387 | card->written_flag = true; |
2388 | fallthrough; | |
2389 | case REQ_OP_READ: | |
407a1c57 | 2390 | if (host->cqe_enabled) |
1e8e55b6 AH |
2391 | ret = mmc_blk_cqe_issue_rw_rq(mq, req); |
2392 | else | |
2393 | ret = mmc_blk_mq_issue_rw_rq(mq, req); | |
81196976 AH |
2394 | break; |
2395 | default: | |
2396 | WARN_ON_ONCE(1); | |
2397 | ret = -EINVAL; | |
2398 | } | |
2399 | if (!ret) | |
2400 | return MMC_REQ_STARTED; | |
2401 | return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START; | |
2402 | default: | |
2403 | WARN_ON_ONCE(1); | |
2404 | return MMC_REQ_FAILED_TO_START; | |
2405 | } | |
2406 | } | |
2407 | ||
a6f6c96b RK |
2408 | static inline int mmc_blk_readonly(struct mmc_card *card) |
2409 | { | |
2410 | return mmc_card_readonly(card) || | |
2411 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | |
2412 | } | |
2413 | ||
371a689f AW |
2414 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
2415 | struct device *parent, | |
2416 | sector_t size, | |
2417 | bool default_ro, | |
add710ea | 2418 | const char *subname, |
a94dcfce CH |
2419 | int area_type, |
2420 | unsigned int part_type) | |
1da177e4 LT |
2421 | { |
2422 | struct mmc_blk_data *md; | |
2423 | int devidx, ret; | |
ce999ed1 | 2424 | char cap_str[10]; |
08ebf903 MW |
2425 | bool cache_enabled = false; |
2426 | bool fua_enabled = false; | |
1da177e4 | 2427 | |
a04848c7 | 2428 | devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); |
e7b42769 SL |
2429 | if (devidx < 0) { |
2430 | /* | |
2431 | * We get -ENOSPC because there are no more any available | |
2432 | * devidx. The reason may be that, either userspace haven't yet | |
2433 | * unmounted the partitions, which postpones mmc_blk_release() | |
2434 | * from being called, or the device has more partitions than | |
2435 | * what we support. | |
2436 | */ | |
2437 | if (devidx == -ENOSPC) | |
2438 | dev_err(mmc_dev(card->host), | |
2439 | "no more device IDs available\n"); | |
2440 | ||
a04848c7 | 2441 | return ERR_PTR(devidx); |
e7b42769 | 2442 | } |
1da177e4 | 2443 | |
dd00cc48 | 2444 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
a6f6c96b RK |
2445 | if (!md) { |
2446 | ret = -ENOMEM; | |
2447 | goto out; | |
2448 | } | |
1da177e4 | 2449 | |
add710ea JR |
2450 | md->area_type = area_type; |
2451 | ||
a6f6c96b RK |
2452 | /* |
2453 | * Set the read-only status based on the supported commands | |
2454 | * and the write protect switch. | |
2455 | */ | |
2456 | md->read_only = mmc_blk_readonly(card); | |
1da177e4 | 2457 | |
607d968a CH |
2458 | md->disk = mmc_init_queue(&md->queue, card); |
2459 | if (IS_ERR(md->disk)) { | |
2460 | ret = PTR_ERR(md->disk); | |
a6f6c96b RK |
2461 | goto err_kfree; |
2462 | } | |
1da177e4 | 2463 | |
371a689f | 2464 | INIT_LIST_HEAD(&md->part); |
97548575 | 2465 | INIT_LIST_HEAD(&md->rpmbs); |
edb25572 SB |
2466 | kref_init(&md->kref); |
2467 | ||
7db3028e | 2468 | md->queue.blkdata = md; |
a94dcfce | 2469 | md->part_type = part_type; |
d2b18394 | 2470 | |
fe6b4c88 | 2471 | md->disk->major = MMC_BLOCK_MAJOR; |
1033d103 | 2472 | md->disk->minors = perdev_minors; |
5e71b7a6 | 2473 | md->disk->first_minor = devidx * perdev_minors; |
a6f6c96b RK |
2474 | md->disk->fops = &mmc_bdops; |
2475 | md->disk->private_data = md; | |
307d8e6f | 2476 | md->parent = parent; |
371a689f | 2477 | set_disk_ro(md->disk, md->read_only || default_ro); |
f5b4d71f | 2478 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) |
79b0f79a | 2479 | md->disk->flags |= GENHD_FL_NO_PART; |
a6f6c96b RK |
2480 | |
2481 | /* | |
2482 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | |
2483 | * | |
2484 | * - be set for removable media with permanent block devices | |
2485 | * - be unset for removable block devices with permanent media | |
2486 | * | |
2487 | * Since MMC block devices clearly fall under the second | |
2488 | * case, we do not set GENHD_FL_REMOVABLE. Userspace | |
2489 | * should use the block device creation/destruction hotplug | |
2490 | * messages to tell when the card is present. | |
2491 | */ | |
2492 | ||
f06c9153 | 2493 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
9aaf3437 | 2494 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
a6f6c96b | 2495 | |
371a689f | 2496 | set_capacity(md->disk, size); |
d0c97cfb | 2497 | |
f0d89972 | 2498 | if (mmc_host_cmd23(card->host)) { |
0ed50abb DG |
2499 | if ((mmc_card_mmc(card) && |
2500 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || | |
f0d89972 AW |
2501 | (mmc_card_sd(card) && |
2502 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | |
2503 | md->flags |= MMC_BLK_CMD23; | |
2504 | } | |
d0c97cfb | 2505 | |
08ebf903 | 2506 | if (md->flags & MMC_BLK_CMD23 && |
d0c97cfb AW |
2507 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
2508 | card->ext_csd.rel_sectors)) { | |
2509 | md->flags |= MMC_BLK_REL_WR; | |
08ebf903 MW |
2510 | fua_enabled = true; |
2511 | cache_enabled = true; | |
d0c97cfb | 2512 | } |
08ebf903 MW |
2513 | if (mmc_cache_enabled(card->host)) |
2514 | cache_enabled = true; | |
2515 | ||
2516 | blk_queue_write_cache(md->queue.queue, cache_enabled, fua_enabled); | |
d0c97cfb | 2517 | |
ce999ed1 UH |
2518 | string_get_size((u64)size, 512, STRING_UNITS_2, |
2519 | cap_str, sizeof(cap_str)); | |
78ce88e7 | 2520 | pr_info("%s: %s %s %s%s\n", |
ce999ed1 | 2521 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
78ce88e7 | 2522 | cap_str, md->read_only ? " (ro)" : ""); |
ce999ed1 | 2523 | |
a94dcfce CH |
2524 | /* used in ->open, must be set before add_disk: */ |
2525 | if (area_type == MMC_BLK_DATA_AREA_MAIN) | |
2526 | dev_set_drvdata(&card->dev, md); | |
9c1aaec4 LC |
2527 | ret = device_add_disk(md->parent, md->disk, mmc_disk_attr_groups); |
2528 | if (ret) | |
bf14fad1 | 2529 | goto err_put_disk; |
371a689f AW |
2530 | return md; |
2531 | ||
bf14fad1 ML |
2532 | err_put_disk: |
2533 | put_disk(md->disk); | |
9c1aaec4 | 2534 | blk_mq_free_tag_set(&md->queue.tag_set); |
371a689f AW |
2535 | err_kfree: |
2536 | kfree(md); | |
2537 | out: | |
a04848c7 | 2538 | ida_simple_remove(&mmc_blk_ida, devidx); |
371a689f AW |
2539 | return ERR_PTR(ret); |
2540 | } | |
2541 | ||
2542 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | |
2543 | { | |
2544 | sector_t size; | |
a6f6c96b | 2545 | |
85a18ad9 PO |
2546 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
2547 | /* | |
2548 | * The EXT_CSD sector count is in number or 512 byte | |
2549 | * sectors. | |
2550 | */ | |
371a689f | 2551 | size = card->ext_csd.sectors; |
85a18ad9 PO |
2552 | } else { |
2553 | /* | |
2554 | * The CSD capacity field is in units of read_blkbits. | |
2555 | * set_capacity takes units of 512 bytes. | |
2556 | */ | |
087de9ed KM |
2557 | size = (typeof(sector_t))card->csd.capacity |
2558 | << (card->csd.read_blkbits - 9); | |
85a18ad9 | 2559 | } |
371a689f | 2560 | |
7a30f2af | 2561 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
a94dcfce | 2562 | MMC_BLK_DATA_AREA_MAIN, 0); |
371a689f | 2563 | } |
a6f6c96b | 2564 | |
371a689f AW |
2565 | static int mmc_blk_alloc_part(struct mmc_card *card, |
2566 | struct mmc_blk_data *md, | |
2567 | unsigned int part_type, | |
2568 | sector_t size, | |
2569 | bool default_ro, | |
add710ea JR |
2570 | const char *subname, |
2571 | int area_type) | |
371a689f | 2572 | { |
371a689f AW |
2573 | struct mmc_blk_data *part_md; |
2574 | ||
2575 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | |
a94dcfce | 2576 | subname, area_type, part_type); |
371a689f AW |
2577 | if (IS_ERR(part_md)) |
2578 | return PTR_ERR(part_md); | |
371a689f AW |
2579 | list_add(&part_md->part, &md->part); |
2580 | ||
371a689f AW |
2581 | return 0; |
2582 | } | |
2583 | ||
97548575 LW |
2584 | /** |
2585 | * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev | |
2586 | * @filp: the character device file | |
2587 | * @cmd: the ioctl() command | |
2588 | * @arg: the argument from userspace | |
2589 | * | |
2590 | * This will essentially just redirect the ioctl()s coming in over to | |
2591 | * the main block device spawning the RPMB character device. | |
2592 | */ | |
2593 | static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, | |
2594 | unsigned long arg) | |
2595 | { | |
2596 | struct mmc_rpmb_data *rpmb = filp->private_data; | |
2597 | int ret; | |
2598 | ||
2599 | switch (cmd) { | |
2600 | case MMC_IOC_CMD: | |
2601 | ret = mmc_blk_ioctl_cmd(rpmb->md, | |
2602 | (struct mmc_ioc_cmd __user *)arg, | |
2603 | rpmb); | |
2604 | break; | |
2605 | case MMC_IOC_MULTI_CMD: | |
2606 | ret = mmc_blk_ioctl_multi_cmd(rpmb->md, | |
2607 | (struct mmc_ioc_multi_cmd __user *)arg, | |
2608 | rpmb); | |
2609 | break; | |
2610 | default: | |
2611 | ret = -EINVAL; | |
2612 | break; | |
2613 | } | |
2614 | ||
b25b750d | 2615 | return ret; |
97548575 LW |
2616 | } |
2617 | ||
2618 | #ifdef CONFIG_COMPAT | |
2619 | static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, | |
2620 | unsigned long arg) | |
2621 | { | |
2622 | return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); | |
2623 | } | |
2624 | #endif | |
2625 | ||
2626 | static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) | |
2627 | { | |
2628 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
2629 | struct mmc_rpmb_data, chrdev); | |
2630 | ||
2631 | get_device(&rpmb->dev); | |
2632 | filp->private_data = rpmb; | |
1c87f735 | 2633 | mmc_blk_get(rpmb->md->disk); |
97548575 LW |
2634 | |
2635 | return nonseekable_open(inode, filp); | |
2636 | } | |
2637 | ||
2638 | static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) | |
2639 | { | |
2640 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, | |
2641 | struct mmc_rpmb_data, chrdev); | |
2642 | ||
1c87f735 | 2643 | mmc_blk_put(rpmb->md); |
202500d2 | 2644 | put_device(&rpmb->dev); |
97548575 LW |
2645 | |
2646 | return 0; | |
2647 | } | |
2648 | ||
2649 | static const struct file_operations mmc_rpmb_fileops = { | |
2650 | .release = mmc_rpmb_chrdev_release, | |
2651 | .open = mmc_rpmb_chrdev_open, | |
2652 | .owner = THIS_MODULE, | |
2653 | .llseek = no_llseek, | |
2654 | .unlocked_ioctl = mmc_rpmb_ioctl, | |
2655 | #ifdef CONFIG_COMPAT | |
2656 | .compat_ioctl = mmc_rpmb_ioctl_compat, | |
2657 | #endif | |
2658 | }; | |
2659 | ||
1c87f735 LW |
2660 | static void mmc_blk_rpmb_device_release(struct device *dev) |
2661 | { | |
2662 | struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); | |
2663 | ||
2664 | ida_simple_remove(&mmc_rpmb_ida, rpmb->id); | |
2665 | kfree(rpmb); | |
2666 | } | |
97548575 LW |
2667 | |
2668 | static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, | |
2669 | struct mmc_blk_data *md, | |
2670 | unsigned int part_index, | |
2671 | sector_t size, | |
2672 | const char *subname) | |
2673 | { | |
2674 | int devidx, ret; | |
2675 | char rpmb_name[DISK_NAME_LEN]; | |
2676 | char cap_str[10]; | |
2677 | struct mmc_rpmb_data *rpmb; | |
2678 | ||
2679 | /* This creates the minor number for the RPMB char device */ | |
2680 | devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL); | |
2681 | if (devidx < 0) | |
2682 | return devidx; | |
2683 | ||
2684 | rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); | |
1c87f735 LW |
2685 | if (!rpmb) { |
2686 | ida_simple_remove(&mmc_rpmb_ida, devidx); | |
97548575 | 2687 | return -ENOMEM; |
1c87f735 | 2688 | } |
97548575 LW |
2689 | |
2690 | snprintf(rpmb_name, sizeof(rpmb_name), | |
2691 | "mmcblk%u%s", card->host->index, subname ? subname : ""); | |
2692 | ||
2693 | rpmb->id = devidx; | |
2694 | rpmb->part_index = part_index; | |
2695 | rpmb->dev.init_name = rpmb_name; | |
2696 | rpmb->dev.bus = &mmc_rpmb_bus_type; | |
2697 | rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); | |
2698 | rpmb->dev.parent = &card->dev; | |
1c87f735 | 2699 | rpmb->dev.release = mmc_blk_rpmb_device_release; |
97548575 LW |
2700 | device_initialize(&rpmb->dev); |
2701 | dev_set_drvdata(&rpmb->dev, rpmb); | |
2702 | rpmb->md = md; | |
2703 | ||
2704 | cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); | |
2705 | rpmb->chrdev.owner = THIS_MODULE; | |
2706 | ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); | |
2707 | if (ret) { | |
2708 | pr_err("%s: could not add character device\n", rpmb_name); | |
1c87f735 | 2709 | goto out_put_device; |
97548575 LW |
2710 | } |
2711 | ||
2712 | list_add(&rpmb->node, &md->rpmbs); | |
2713 | ||
2714 | string_get_size((u64)size, 512, STRING_UNITS_2, | |
2715 | cap_str, sizeof(cap_str)); | |
2716 | ||
ce999ed1 UH |
2717 | pr_info("%s: %s %s %s, chardev (%d:%d)\n", |
2718 | rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str, | |
97548575 LW |
2719 | MAJOR(mmc_rpmb_devt), rpmb->id); |
2720 | ||
2721 | return 0; | |
2722 | ||
1c87f735 LW |
2723 | out_put_device: |
2724 | put_device(&rpmb->dev); | |
97548575 LW |
2725 | return ret; |
2726 | } | |
2727 | ||
2728 | static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) | |
1c87f735 | 2729 | |
97548575 LW |
2730 | { |
2731 | cdev_device_del(&rpmb->chrdev, &rpmb->dev); | |
1c87f735 | 2732 | put_device(&rpmb->dev); |
97548575 LW |
2733 | } |
2734 | ||
e0c368d5 NJ |
2735 | /* MMC Physical partitions consist of two boot partitions and |
2736 | * up to four general purpose partitions. | |
2737 | * For each partition enabled in EXT_CSD a block device will be allocatedi | |
2738 | * to provide access to the partition. | |
2739 | */ | |
2740 | ||
371a689f AW |
2741 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
2742 | { | |
97548575 | 2743 | int idx, ret; |
371a689f AW |
2744 | |
2745 | if (!mmc_card_mmc(card)) | |
2746 | return 0; | |
2747 | ||
e0c368d5 | 2748 | for (idx = 0; idx < card->nr_parts; idx++) { |
97548575 LW |
2749 | if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { |
2750 | /* | |
2751 | * RPMB partitions does not provide block access, they | |
2752 | * are only accessed using ioctl():s. Thus create | |
2753 | * special RPMB block devices that do not have a | |
2754 | * backing block queue for these. | |
2755 | */ | |
2756 | ret = mmc_blk_alloc_rpmb_part(card, md, | |
2757 | card->part[idx].part_cfg, | |
2758 | card->part[idx].size >> 9, | |
2759 | card->part[idx].name); | |
2760 | if (ret) | |
2761 | return ret; | |
2762 | } else if (card->part[idx].size) { | |
e0c368d5 NJ |
2763 | ret = mmc_blk_alloc_part(card, md, |
2764 | card->part[idx].part_cfg, | |
2765 | card->part[idx].size >> 9, | |
2766 | card->part[idx].force_ro, | |
add710ea JR |
2767 | card->part[idx].name, |
2768 | card->part[idx].area_type); | |
e0c368d5 NJ |
2769 | if (ret) |
2770 | return ret; | |
2771 | } | |
371a689f AW |
2772 | } |
2773 | ||
97548575 | 2774 | return 0; |
1da177e4 LT |
2775 | } |
2776 | ||
371a689f AW |
2777 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
2778 | { | |
a94dcfce CH |
2779 | /* |
2780 | * Flush remaining requests and free queues. It is freeing the queue | |
2781 | * that stops new requests from being accepted. | |
2782 | */ | |
2783 | del_gendisk(md->disk); | |
2784 | mmc_cleanup_queue(&md->queue); | |
2785 | mmc_blk_put(md); | |
371a689f AW |
2786 | } |
2787 | ||
2788 | static void mmc_blk_remove_parts(struct mmc_card *card, | |
2789 | struct mmc_blk_data *md) | |
2790 | { | |
2791 | struct list_head *pos, *q; | |
2792 | struct mmc_blk_data *part_md; | |
97548575 | 2793 | struct mmc_rpmb_data *rpmb; |
371a689f | 2794 | |
97548575 LW |
2795 | /* Remove RPMB partitions */ |
2796 | list_for_each_safe(pos, q, &md->rpmbs) { | |
2797 | rpmb = list_entry(pos, struct mmc_rpmb_data, node); | |
2798 | list_del(pos); | |
2799 | mmc_blk_remove_rpmb_part(rpmb); | |
2800 | } | |
2801 | /* Remove block partitions */ | |
371a689f AW |
2802 | list_for_each_safe(pos, q, &md->part) { |
2803 | part_md = list_entry(pos, struct mmc_blk_data, part); | |
2804 | list_del(pos); | |
2805 | mmc_blk_remove_req(part_md); | |
2806 | } | |
2807 | } | |
2808 | ||
627c3ccf LW |
2809 | #ifdef CONFIG_DEBUG_FS |
2810 | ||
2811 | static int mmc_dbg_card_status_get(void *data, u64 *val) | |
2812 | { | |
2813 | struct mmc_card *card = data; | |
2814 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
2815 | struct mmc_queue *mq = &md->queue; | |
2816 | struct request *req; | |
2817 | int ret; | |
2818 | ||
2819 | /* Ask the block layer about the card status */ | |
0bf6d96c | 2820 | req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0); |
fb8e456e AH |
2821 | if (IS_ERR(req)) |
2822 | return PTR_ERR(req); | |
627c3ccf | 2823 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; |
003fb0a5 | 2824 | req_to_mmc_queue_req(req)->drv_op_result = -EIO; |
b84ba30b | 2825 | blk_execute_rq(req, false); |
627c3ccf LW |
2826 | ret = req_to_mmc_queue_req(req)->drv_op_result; |
2827 | if (ret >= 0) { | |
2828 | *val = ret; | |
2829 | ret = 0; | |
2830 | } | |
0bf6d96c | 2831 | blk_mq_free_request(req); |
627c3ccf LW |
2832 | |
2833 | return ret; | |
2834 | } | |
f6a3d9d9 Y |
2835 | DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, |
2836 | NULL, "%08llx\n"); | |
627c3ccf LW |
2837 | |
2838 | /* That is two digits * 512 + 1 for newline */ | |
2839 | #define EXT_CSD_STR_LEN 1025 | |
2840 | ||
2841 | static int mmc_ext_csd_open(struct inode *inode, struct file *filp) | |
2842 | { | |
2843 | struct mmc_card *card = inode->i_private; | |
2844 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | |
2845 | struct mmc_queue *mq = &md->queue; | |
2846 | struct request *req; | |
2847 | char *buf; | |
2848 | ssize_t n = 0; | |
2849 | u8 *ext_csd; | |
2850 | int err, i; | |
2851 | ||
2852 | buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); | |
2853 | if (!buf) | |
2854 | return -ENOMEM; | |
2855 | ||
2856 | /* Ask the block layer for the EXT CSD */ | |
0bf6d96c | 2857 | req = blk_mq_alloc_request(mq->queue, REQ_OP_DRV_IN, 0); |
fb8e456e AH |
2858 | if (IS_ERR(req)) { |
2859 | err = PTR_ERR(req); | |
2860 | goto out_free; | |
2861 | } | |
627c3ccf | 2862 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; |
003fb0a5 | 2863 | req_to_mmc_queue_req(req)->drv_op_result = -EIO; |
627c3ccf | 2864 | req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; |
b84ba30b | 2865 | blk_execute_rq(req, false); |
627c3ccf | 2866 | err = req_to_mmc_queue_req(req)->drv_op_result; |
0bf6d96c | 2867 | blk_mq_free_request(req); |
627c3ccf LW |
2868 | if (err) { |
2869 | pr_err("FAILED %d\n", err); | |
2870 | goto out_free; | |
2871 | } | |
2872 | ||
2873 | for (i = 0; i < 512; i++) | |
2874 | n += sprintf(buf + n, "%02x", ext_csd[i]); | |
2875 | n += sprintf(buf + n, "\n"); | |
2876 | ||
2877 | if (n != EXT_CSD_STR_LEN) { | |
2878 | err = -EINVAL; | |
0be55579 | 2879 | kfree(ext_csd); |
627c3ccf LW |
2880 | goto out_free; |
2881 | } | |
2882 | ||
2883 | filp->private_data = buf; | |
2884 | kfree(ext_csd); | |
2885 | return 0; | |
2886 | ||
2887 | out_free: | |
2888 | kfree(buf); | |
2889 | return err; | |
2890 | } | |
2891 | ||
2892 | static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, | |
2893 | size_t cnt, loff_t *ppos) | |
2894 | { | |
2895 | char *buf = filp->private_data; | |
2896 | ||
2897 | return simple_read_from_buffer(ubuf, cnt, ppos, | |
2898 | buf, EXT_CSD_STR_LEN); | |
2899 | } | |
2900 | ||
2901 | static int mmc_ext_csd_release(struct inode *inode, struct file *file) | |
2902 | { | |
2903 | kfree(file->private_data); | |
2904 | return 0; | |
2905 | } | |
2906 | ||
2907 | static const struct file_operations mmc_dbg_ext_csd_fops = { | |
2908 | .open = mmc_ext_csd_open, | |
2909 | .read = mmc_ext_csd_read, | |
2910 | .release = mmc_ext_csd_release, | |
2911 | .llseek = default_llseek, | |
2912 | }; | |
2913 | ||
584f5488 | 2914 | static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf LW |
2915 | { |
2916 | struct dentry *root; | |
2917 | ||
2918 | if (!card->debugfs_root) | |
584f5488 | 2919 | return; |
627c3ccf LW |
2920 | |
2921 | root = card->debugfs_root; | |
2922 | ||
2923 | if (mmc_card_mmc(card) || mmc_card_sd(card)) { | |
f9f0da98 | 2924 | md->status_dentry = |
f6a3d9d9 Y |
2925 | debugfs_create_file_unsafe("status", 0400, root, |
2926 | card, | |
2927 | &mmc_dbg_card_status_fops); | |
627c3ccf LW |
2928 | } |
2929 | ||
2930 | if (mmc_card_mmc(card)) { | |
f9f0da98 AH |
2931 | md->ext_csd_dentry = |
2932 | debugfs_create_file("ext_csd", S_IRUSR, root, card, | |
2933 | &mmc_dbg_ext_csd_fops); | |
627c3ccf | 2934 | } |
627c3ccf LW |
2935 | } |
2936 | ||
f9f0da98 AH |
2937 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
2938 | struct mmc_blk_data *md) | |
2939 | { | |
2940 | if (!card->debugfs_root) | |
2941 | return; | |
2942 | ||
584f5488 YF |
2943 | debugfs_remove(md->status_dentry); |
2944 | md->status_dentry = NULL; | |
f9f0da98 | 2945 | |
584f5488 YF |
2946 | debugfs_remove(md->ext_csd_dentry); |
2947 | md->ext_csd_dentry = NULL; | |
f9f0da98 | 2948 | } |
627c3ccf LW |
2949 | |
2950 | #else | |
2951 | ||
584f5488 | 2952 | static void mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md) |
627c3ccf | 2953 | { |
627c3ccf LW |
2954 | } |
2955 | ||
f9f0da98 AH |
2956 | static void mmc_blk_remove_debugfs(struct mmc_card *card, |
2957 | struct mmc_blk_data *md) | |
2958 | { | |
2959 | } | |
2960 | ||
627c3ccf LW |
2961 | #endif /* CONFIG_DEBUG_FS */ |
2962 | ||
96541bac | 2963 | static int mmc_blk_probe(struct mmc_card *card) |
1da177e4 | 2964 | { |
a94dcfce | 2965 | struct mmc_blk_data *md; |
6f1d3247 | 2966 | int ret = 0; |
a7bbb573 | 2967 | |
912490db PO |
2968 | /* |
2969 | * Check that the card supports the command class(es) we need. | |
2970 | */ | |
2971 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | |
1da177e4 LT |
2972 | return -ENODEV; |
2973 | ||
8c7cdbf9 | 2974 | mmc_fixup_device(card, mmc_blk_fixups); |
5204d00f | 2975 | |
dcf6e2e3 ZH |
2976 | card->complete_wq = alloc_workqueue("mmc_complete", |
2977 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); | |
1d848c28 | 2978 | if (!card->complete_wq) { |
dcf6e2e3 ZH |
2979 | pr_err("Failed to create mmc completion workqueue"); |
2980 | return -ENOMEM; | |
2981 | } | |
2982 | ||
1da177e4 | 2983 | md = mmc_blk_alloc(card); |
6f1d3247 UH |
2984 | if (IS_ERR(md)) { |
2985 | ret = PTR_ERR(md); | |
2986 | goto out_free; | |
2987 | } | |
1da177e4 | 2988 | |
6f1d3247 UH |
2989 | ret = mmc_blk_alloc_parts(card, md); |
2990 | if (ret) | |
371a689f AW |
2991 | goto out; |
2992 | ||
627c3ccf | 2993 | /* Add two debugfs entries */ |
f9f0da98 | 2994 | mmc_blk_add_debugfs(card, md); |
627c3ccf | 2995 | |
e94cfef6 UH |
2996 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
2997 | pm_runtime_use_autosuspend(&card->dev); | |
2998 | ||
2999 | /* | |
3000 | * Don't enable runtime PM for SD-combo cards here. Leave that | |
3001 | * decision to be taken during the SDIO init sequence instead. | |
3002 | */ | |
3beb0ab5 | 3003 | if (!mmc_card_sd_combo(card)) { |
e94cfef6 UH |
3004 | pm_runtime_set_active(&card->dev); |
3005 | pm_runtime_enable(&card->dev); | |
3006 | } | |
3007 | ||
1da177e4 LT |
3008 | return 0; |
3009 | ||
6f1d3247 | 3010 | out: |
371a689f AW |
3011 | mmc_blk_remove_parts(card, md); |
3012 | mmc_blk_remove_req(md); | |
6f1d3247 UH |
3013 | out_free: |
3014 | destroy_workqueue(card->complete_wq); | |
3015 | return ret; | |
1da177e4 LT |
3016 | } |
3017 | ||
96541bac | 3018 | static void mmc_blk_remove(struct mmc_card *card) |
1da177e4 | 3019 | { |
96541bac | 3020 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 | 3021 | |
f9f0da98 | 3022 | mmc_blk_remove_debugfs(card, md); |
371a689f | 3023 | mmc_blk_remove_parts(card, md); |
e94cfef6 | 3024 | pm_runtime_get_sync(&card->dev); |
65f9e20e SL |
3025 | if (md->part_curr != md->part_type) { |
3026 | mmc_claim_host(card->host); | |
3027 | mmc_blk_part_switch(card, md->part_type); | |
3028 | mmc_release_host(card->host); | |
3029 | } | |
3beb0ab5 | 3030 | if (!mmc_card_sd_combo(card)) |
e94cfef6 UH |
3031 | pm_runtime_disable(&card->dev); |
3032 | pm_runtime_put_noidle(&card->dev); | |
371a689f | 3033 | mmc_blk_remove_req(md); |
dcf6e2e3 | 3034 | destroy_workqueue(card->complete_wq); |
1da177e4 LT |
3035 | } |
3036 | ||
96541bac | 3037 | static int _mmc_blk_suspend(struct mmc_card *card) |
1da177e4 | 3038 | { |
371a689f | 3039 | struct mmc_blk_data *part_md; |
96541bac | 3040 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
1da177e4 LT |
3041 | |
3042 | if (md) { | |
3043 | mmc_queue_suspend(&md->queue); | |
371a689f AW |
3044 | list_for_each_entry(part_md, &md->part, part) { |
3045 | mmc_queue_suspend(&part_md->queue); | |
3046 | } | |
1da177e4 LT |
3047 | } |
3048 | return 0; | |
3049 | } | |
3050 | ||
96541bac | 3051 | static void mmc_blk_shutdown(struct mmc_card *card) |
76287748 | 3052 | { |
96541bac | 3053 | _mmc_blk_suspend(card); |
76287748 UH |
3054 | } |
3055 | ||
0967edc6 UH |
3056 | #ifdef CONFIG_PM_SLEEP |
3057 | static int mmc_blk_suspend(struct device *dev) | |
76287748 | 3058 | { |
96541bac UH |
3059 | struct mmc_card *card = mmc_dev_to_card(dev); |
3060 | ||
3061 | return _mmc_blk_suspend(card); | |
76287748 UH |
3062 | } |
3063 | ||
0967edc6 | 3064 | static int mmc_blk_resume(struct device *dev) |
1da177e4 | 3065 | { |
371a689f | 3066 | struct mmc_blk_data *part_md; |
fc95e30b | 3067 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
1da177e4 LT |
3068 | |
3069 | if (md) { | |
371a689f AW |
3070 | /* |
3071 | * Resume involves the card going into idle state, | |
3072 | * so current partition is always the main one. | |
3073 | */ | |
3074 | md->part_curr = md->part_type; | |
1da177e4 | 3075 | mmc_queue_resume(&md->queue); |
371a689f AW |
3076 | list_for_each_entry(part_md, &md->part, part) { |
3077 | mmc_queue_resume(&part_md->queue); | |
3078 | } | |
1da177e4 LT |
3079 | } |
3080 | return 0; | |
3081 | } | |
1da177e4 LT |
3082 | #endif |
3083 | ||
0967edc6 UH |
3084 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
3085 | ||
96541bac UH |
3086 | static struct mmc_driver mmc_driver = { |
3087 | .drv = { | |
3088 | .name = "mmcblk", | |
3089 | .pm = &mmc_blk_pm_ops, | |
3090 | }, | |
1da177e4 LT |
3091 | .probe = mmc_blk_probe, |
3092 | .remove = mmc_blk_remove, | |
76287748 | 3093 | .shutdown = mmc_blk_shutdown, |
1da177e4 LT |
3094 | }; |
3095 | ||
3096 | static int __init mmc_blk_init(void) | |
3097 | { | |
9d4e98e9 | 3098 | int res; |
1da177e4 | 3099 | |
97548575 LW |
3100 | res = bus_register(&mmc_rpmb_bus_type); |
3101 | if (res < 0) { | |
3102 | pr_err("mmcblk: could not register RPMB bus type\n"); | |
3103 | return res; | |
3104 | } | |
3105 | res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); | |
3106 | if (res < 0) { | |
3107 | pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); | |
3108 | goto out_bus_unreg; | |
3109 | } | |
3110 | ||
5e71b7a6 OJ |
3111 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
3112 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | |
3113 | ||
a26eba61 | 3114 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
5e71b7a6 | 3115 | |
fe6b4c88 PO |
3116 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
3117 | if (res) | |
97548575 | 3118 | goto out_chrdev_unreg; |
1da177e4 | 3119 | |
9d4e98e9 AM |
3120 | res = mmc_register_driver(&mmc_driver); |
3121 | if (res) | |
97548575 | 3122 | goto out_blkdev_unreg; |
1da177e4 | 3123 | |
9d4e98e9 | 3124 | return 0; |
97548575 LW |
3125 | |
3126 | out_blkdev_unreg: | |
9d4e98e9 | 3127 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 LW |
3128 | out_chrdev_unreg: |
3129 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); | |
3130 | out_bus_unreg: | |
3131 | bus_unregister(&mmc_rpmb_bus_type); | |
1da177e4 LT |
3132 | return res; |
3133 | } | |
3134 | ||
3135 | static void __exit mmc_blk_exit(void) | |
3136 | { | |
3137 | mmc_unregister_driver(&mmc_driver); | |
fe6b4c88 | 3138 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
97548575 | 3139 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); |
d0a0852b | 3140 | bus_unregister(&mmc_rpmb_bus_type); |
1da177e4 LT |
3141 | } |
3142 | ||
3143 | module_init(mmc_blk_init); | |
3144 | module_exit(mmc_blk_exit); | |
3145 | ||
3146 | MODULE_LICENSE("GPL"); | |
3147 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | |
3148 |