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