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