block.c revision a01f3ccf845067de32189f8a8e85d22c381f93b9
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
38#include <linux/mmc/ioctl.h>
39#include <linux/mmc/card.h>
40#include <linux/mmc/host.h>
41#include <linux/mmc/mmc.h>
42#include <linux/mmc/sd.h>
43
44#include <asm/system.h>
45#include <asm/uaccess.h>
46
47#include "queue.h"
48
49MODULE_ALIAS("mmc:block");
50#ifdef MODULE_PARAM_PREFIX
51#undef MODULE_PARAM_PREFIX
52#endif
53#define MODULE_PARAM_PREFIX "mmcblk."
54
55#define INAND_CMD38_ARG_EXT_CSD  113
56#define INAND_CMD38_ARG_ERASE    0x00
57#define INAND_CMD38_ARG_TRIM     0x01
58#define INAND_CMD38_ARG_SECERASE 0x80
59#define INAND_CMD38_ARG_SECTRIM1 0x81
60#define INAND_CMD38_ARG_SECTRIM2 0x88
61
62static DEFINE_MUTEX(block_mutex);
63
64/*
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
67 */
68static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
69
70/*
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
73 */
74static int max_devices;
75
76/* 256 minors, so at most 256 separate devices */
77static DECLARE_BITMAP(dev_use, 256);
78static DECLARE_BITMAP(name_use, 256);
79
80/*
81 * There is one mmc_blk_data per slot.
82 */
83struct mmc_blk_data {
84	spinlock_t	lock;
85	struct gendisk	*disk;
86	struct mmc_queue queue;
87	struct list_head part;
88
89	unsigned int	flags;
90#define MMC_BLK_CMD23	(1 << 0)	/* Can do SET_BLOCK_COUNT for multiblock */
91#define MMC_BLK_REL_WR	(1 << 1)	/* MMC Reliable write support */
92
93	unsigned int	usage;
94	unsigned int	read_only;
95	unsigned int	part_type;
96	unsigned int	name_idx;
97
98	/*
99	 * Only set in main mmc_blk_data associated
100	 * with mmc_card with mmc_set_drvdata, and keeps
101	 * track of the current selected device partition.
102	 */
103	unsigned int	part_curr;
104	struct device_attribute force_ro;
105};
106
107static DEFINE_MUTEX(open_lock);
108
109module_param(perdev_minors, int, 0444);
110MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
111
112static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
113{
114	struct mmc_blk_data *md;
115
116	mutex_lock(&open_lock);
117	md = disk->private_data;
118	if (md && md->usage == 0)
119		md = NULL;
120	if (md)
121		md->usage++;
122	mutex_unlock(&open_lock);
123
124	return md;
125}
126
127static inline int mmc_get_devidx(struct gendisk *disk)
128{
129	int devmaj = MAJOR(disk_devt(disk));
130	int devidx = MINOR(disk_devt(disk)) / perdev_minors;
131
132	if (!devmaj)
133		devidx = disk->first_minor / perdev_minors;
134	return devidx;
135}
136
137static void mmc_blk_put(struct mmc_blk_data *md)
138{
139	mutex_lock(&open_lock);
140	md->usage--;
141	if (md->usage == 0) {
142		int devidx = mmc_get_devidx(md->disk);
143		blk_cleanup_queue(md->queue.queue);
144
145		__clear_bit(devidx, dev_use);
146
147		put_disk(md->disk);
148		kfree(md);
149	}
150	mutex_unlock(&open_lock);
151}
152
153static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
154			     char *buf)
155{
156	int ret;
157	struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
158
159	ret = snprintf(buf, PAGE_SIZE, "%d",
160		       get_disk_ro(dev_to_disk(dev)) ^
161		       md->read_only);
162	mmc_blk_put(md);
163	return ret;
164}
165
166static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167			      const char *buf, size_t count)
168{
169	int ret;
170	char *end;
171	struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172	unsigned long set = simple_strtoul(buf, &end, 0);
173	if (end == buf) {
174		ret = -EINVAL;
175		goto out;
176	}
177
178	set_disk_ro(dev_to_disk(dev), set || md->read_only);
179	ret = count;
180out:
181	mmc_blk_put(md);
182	return ret;
183}
184
185static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
186{
187	struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
188	int ret = -ENXIO;
189
190	mutex_lock(&block_mutex);
191	if (md) {
192		if (md->usage == 2)
193			check_disk_change(bdev);
194		ret = 0;
195
196		if ((mode & FMODE_WRITE) && md->read_only) {
197			mmc_blk_put(md);
198			ret = -EROFS;
199		}
200	}
201	mutex_unlock(&block_mutex);
202
203	return ret;
204}
205
206static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
207{
208	struct mmc_blk_data *md = disk->private_data;
209
210	mutex_lock(&block_mutex);
211	mmc_blk_put(md);
212	mutex_unlock(&block_mutex);
213	return 0;
214}
215
216static int
217mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
218{
219	geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
220	geo->heads = 4;
221	geo->sectors = 16;
222	return 0;
223}
224
225struct mmc_blk_ioc_data {
226	struct mmc_ioc_cmd ic;
227	unsigned char *buf;
228	u64 buf_bytes;
229};
230
231static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232	struct mmc_ioc_cmd __user *user)
233{
234	struct mmc_blk_ioc_data *idata;
235	int err;
236
237	idata = kzalloc(sizeof(*idata), GFP_KERNEL);
238	if (!idata) {
239		err = -ENOMEM;
240		goto out;
241	}
242
243	if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
244		err = -EFAULT;
245		goto idata_err;
246	}
247
248	idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249	if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
250		err = -EOVERFLOW;
251		goto idata_err;
252	}
253
254	idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
255	if (!idata->buf) {
256		err = -ENOMEM;
257		goto idata_err;
258	}
259
260	if (copy_from_user(idata->buf, (void __user *)(unsigned long)
261					idata->ic.data_ptr, idata->buf_bytes)) {
262		err = -EFAULT;
263		goto copy_err;
264	}
265
266	return idata;
267
268copy_err:
269	kfree(idata->buf);
270idata_err:
271	kfree(idata);
272out:
273	return ERR_PTR(err);
274}
275
276static int mmc_blk_ioctl_cmd(struct block_device *bdev,
277	struct mmc_ioc_cmd __user *ic_ptr)
278{
279	struct mmc_blk_ioc_data *idata;
280	struct mmc_blk_data *md;
281	struct mmc_card *card;
282	struct mmc_command cmd = {0};
283	struct mmc_data data = {0};
284	struct mmc_request mrq = {0};
285	struct scatterlist sg;
286	int err;
287
288	/*
289	 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
290	 * whole block device, not on a partition.  This prevents overspray
291	 * between sibling partitions.
292	 */
293	if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
294		return -EPERM;
295
296	idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
297	if (IS_ERR(idata))
298		return PTR_ERR(idata);
299
300	cmd.opcode = idata->ic.opcode;
301	cmd.arg = idata->ic.arg;
302	cmd.flags = idata->ic.flags;
303
304	data.sg = &sg;
305	data.sg_len = 1;
306	data.blksz = idata->ic.blksz;
307	data.blocks = idata->ic.blocks;
308
309	sg_init_one(data.sg, idata->buf, idata->buf_bytes);
310
311	if (idata->ic.write_flag)
312		data.flags = MMC_DATA_WRITE;
313	else
314		data.flags = MMC_DATA_READ;
315
316	mrq.cmd = &cmd;
317	mrq.data = &data;
318
319	md = mmc_blk_get(bdev->bd_disk);
320	if (!md) {
321		err = -EINVAL;
322		goto cmd_done;
323	}
324
325	card = md->queue.card;
326	if (IS_ERR(card)) {
327		err = PTR_ERR(card);
328		goto cmd_done;
329	}
330
331	mmc_claim_host(card->host);
332
333	if (idata->ic.is_acmd) {
334		err = mmc_app_cmd(card->host, card);
335		if (err)
336			goto cmd_rel_host;
337	}
338
339	/* data.flags must already be set before doing this. */
340	mmc_set_data_timeout(&data, card);
341	/* Allow overriding the timeout_ns for empirical tuning. */
342	if (idata->ic.data_timeout_ns)
343		data.timeout_ns = idata->ic.data_timeout_ns;
344
345	if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
346		/*
347		 * Pretend this is a data transfer and rely on the host driver
348		 * to compute timeout.  When all host drivers support
349		 * cmd.cmd_timeout for R1B, this can be changed to:
350		 *
351		 *     mrq.data = NULL;
352		 *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
353		 */
354		data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
355	}
356
357	mmc_wait_for_req(card->host, &mrq);
358
359	if (cmd.error) {
360		dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
361						__func__, cmd.error);
362		err = cmd.error;
363		goto cmd_rel_host;
364	}
365	if (data.error) {
366		dev_err(mmc_dev(card->host), "%s: data error %d\n",
367						__func__, data.error);
368		err = data.error;
369		goto cmd_rel_host;
370	}
371
372	/*
373	 * According to the SD specs, some commands require a delay after
374	 * issuing the command.
375	 */
376	if (idata->ic.postsleep_min_us)
377		usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
378
379	if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
380		err = -EFAULT;
381		goto cmd_rel_host;
382	}
383
384	if (!idata->ic.write_flag) {
385		if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
386						idata->buf, idata->buf_bytes)) {
387			err = -EFAULT;
388			goto cmd_rel_host;
389		}
390	}
391
392cmd_rel_host:
393	mmc_release_host(card->host);
394
395cmd_done:
396	mmc_blk_put(md);
397	kfree(idata->buf);
398	kfree(idata);
399	return err;
400}
401
402static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
403	unsigned int cmd, unsigned long arg)
404{
405	int ret = -EINVAL;
406	if (cmd == MMC_IOC_CMD)
407		ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
408	return ret;
409}
410
411#ifdef CONFIG_COMPAT
412static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
413	unsigned int cmd, unsigned long arg)
414{
415	return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
416}
417#endif
418
419static const struct block_device_operations mmc_bdops = {
420	.open			= mmc_blk_open,
421	.release		= mmc_blk_release,
422	.getgeo			= mmc_blk_getgeo,
423	.owner			= THIS_MODULE,
424	.ioctl			= mmc_blk_ioctl,
425#ifdef CONFIG_COMPAT
426	.compat_ioctl		= mmc_blk_compat_ioctl,
427#endif
428};
429
430struct mmc_blk_request {
431	struct mmc_request	mrq;
432	struct mmc_command	sbc;
433	struct mmc_command	cmd;
434	struct mmc_command	stop;
435	struct mmc_data		data;
436};
437
438static inline int mmc_blk_part_switch(struct mmc_card *card,
439				      struct mmc_blk_data *md)
440{
441	int ret;
442	struct mmc_blk_data *main_md = mmc_get_drvdata(card);
443	if (main_md->part_curr == md->part_type)
444		return 0;
445
446	if (mmc_card_mmc(card)) {
447		card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
448		card->ext_csd.part_config |= md->part_type;
449
450		ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
451				 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
452				 card->ext_csd.part_time);
453		if (ret)
454			return ret;
455}
456
457	main_md->part_curr = md->part_type;
458	return 0;
459}
460
461static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
462{
463	int err;
464	u32 result;
465	__be32 *blocks;
466
467	struct mmc_request mrq = {0};
468	struct mmc_command cmd = {0};
469	struct mmc_data data = {0};
470	unsigned int timeout_us;
471
472	struct scatterlist sg;
473
474	cmd.opcode = MMC_APP_CMD;
475	cmd.arg = card->rca << 16;
476	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
477
478	err = mmc_wait_for_cmd(card->host, &cmd, 0);
479	if (err)
480		return (u32)-1;
481	if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
482		return (u32)-1;
483
484	memset(&cmd, 0, sizeof(struct mmc_command));
485
486	cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
487	cmd.arg = 0;
488	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
489
490	data.timeout_ns = card->csd.tacc_ns * 100;
491	data.timeout_clks = card->csd.tacc_clks * 100;
492
493	timeout_us = data.timeout_ns / 1000;
494	timeout_us += data.timeout_clks * 1000 /
495		(card->host->ios.clock / 1000);
496
497	if (timeout_us > 100000) {
498		data.timeout_ns = 100000000;
499		data.timeout_clks = 0;
500	}
501
502	data.blksz = 4;
503	data.blocks = 1;
504	data.flags = MMC_DATA_READ;
505	data.sg = &sg;
506	data.sg_len = 1;
507
508	mrq.cmd = &cmd;
509	mrq.data = &data;
510
511	blocks = kmalloc(4, GFP_KERNEL);
512	if (!blocks)
513		return (u32)-1;
514
515	sg_init_one(&sg, blocks, 4);
516
517	mmc_wait_for_req(card->host, &mrq);
518
519	result = ntohl(*blocks);
520	kfree(blocks);
521
522	if (cmd.error || data.error)
523		result = (u32)-1;
524
525	return result;
526}
527
528static int send_stop(struct mmc_card *card, u32 *status)
529{
530	struct mmc_command cmd = {0};
531	int err;
532
533	cmd.opcode = MMC_STOP_TRANSMISSION;
534	cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
535	err = mmc_wait_for_cmd(card->host, &cmd, 5);
536	if (err == 0)
537		*status = cmd.resp[0];
538	return err;
539}
540
541static int get_card_status(struct mmc_card *card, u32 *status, int retries)
542{
543	struct mmc_command cmd = {0};
544	int err;
545
546	cmd.opcode = MMC_SEND_STATUS;
547	if (!mmc_host_is_spi(card->host))
548		cmd.arg = card->rca << 16;
549	cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
550	err = mmc_wait_for_cmd(card->host, &cmd, retries);
551	if (err == 0)
552		*status = cmd.resp[0];
553	return err;
554}
555
556#define ERR_RETRY	2
557#define ERR_ABORT	1
558#define ERR_CONTINUE	0
559
560static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
561	bool status_valid, u32 status)
562{
563	switch (error) {
564	case -EILSEQ:
565		/* response crc error, retry the r/w cmd */
566		pr_err("%s: %s sending %s command, card status %#x\n",
567			req->rq_disk->disk_name, "response CRC error",
568			name, status);
569		return ERR_RETRY;
570
571	case -ETIMEDOUT:
572		pr_err("%s: %s sending %s command, card status %#x\n",
573			req->rq_disk->disk_name, "timed out", name, status);
574
575		/* If the status cmd initially failed, retry the r/w cmd */
576		if (!status_valid)
577			return ERR_RETRY;
578
579		/*
580		 * If it was a r/w cmd crc error, or illegal command
581		 * (eg, issued in wrong state) then retry - we should
582		 * have corrected the state problem above.
583		 */
584		if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
585			return ERR_RETRY;
586
587		/* Otherwise abort the command */
588		return ERR_ABORT;
589
590	default:
591		/* We don't understand the error code the driver gave us */
592		pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
593		       req->rq_disk->disk_name, error, status);
594		return ERR_ABORT;
595	}
596}
597
598/*
599 * Initial r/w and stop cmd error recovery.
600 * We don't know whether the card received the r/w cmd or not, so try to
601 * restore things back to a sane state.  Essentially, we do this as follows:
602 * - Obtain card status.  If the first attempt to obtain card status fails,
603 *   the status word will reflect the failed status cmd, not the failed
604 *   r/w cmd.  If we fail to obtain card status, it suggests we can no
605 *   longer communicate with the card.
606 * - Check the card state.  If the card received the cmd but there was a
607 *   transient problem with the response, it might still be in a data transfer
608 *   mode.  Try to send it a stop command.  If this fails, we can't recover.
609 * - If the r/w cmd failed due to a response CRC error, it was probably
610 *   transient, so retry the cmd.
611 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
612 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
613 *   illegal cmd, retry.
614 * Otherwise we don't understand what happened, so abort.
615 */
616static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
617	struct mmc_blk_request *brq)
618{
619	bool prev_cmd_status_valid = true;
620	u32 status, stop_status = 0;
621	int err, retry;
622
623	/*
624	 * Try to get card status which indicates both the card state
625	 * and why there was no response.  If the first attempt fails,
626	 * we can't be sure the returned status is for the r/w command.
627	 */
628	for (retry = 2; retry >= 0; retry--) {
629		err = get_card_status(card, &status, 0);
630		if (!err)
631			break;
632
633		prev_cmd_status_valid = false;
634		pr_err("%s: error %d sending status command, %sing\n",
635		       req->rq_disk->disk_name, err, retry ? "retry" : "abort");
636	}
637
638	/* We couldn't get a response from the card.  Give up. */
639	if (err)
640		return ERR_ABORT;
641
642	/*
643	 * Check the current card state.  If it is in some data transfer
644	 * mode, tell it to stop (and hopefully transition back to TRAN.)
645	 */
646	if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
647	    R1_CURRENT_STATE(status) == R1_STATE_RCV) {
648		err = send_stop(card, &stop_status);
649		if (err)
650			pr_err("%s: error %d sending stop command\n",
651			       req->rq_disk->disk_name, err);
652
653		/*
654		 * If the stop cmd also timed out, the card is probably
655		 * not present, so abort.  Other errors are bad news too.
656		 */
657		if (err)
658			return ERR_ABORT;
659	}
660
661	/* Check for set block count errors */
662	if (brq->sbc.error)
663		return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
664				prev_cmd_status_valid, status);
665
666	/* Check for r/w command errors */
667	if (brq->cmd.error)
668		return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
669				prev_cmd_status_valid, status);
670
671	/* Now for stop errors.  These aren't fatal to the transfer. */
672	pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
673	       req->rq_disk->disk_name, brq->stop.error,
674	       brq->cmd.resp[0], status);
675
676	/*
677	 * Subsitute in our own stop status as this will give the error
678	 * state which happened during the execution of the r/w command.
679	 */
680	if (stop_status) {
681		brq->stop.resp[0] = stop_status;
682		brq->stop.error = 0;
683	}
684	return ERR_CONTINUE;
685}
686
687static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
688{
689	struct mmc_blk_data *md = mq->data;
690	struct mmc_card *card = md->queue.card;
691	unsigned int from, nr, arg;
692	int err = 0;
693
694	if (!mmc_can_erase(card)) {
695		err = -EOPNOTSUPP;
696		goto out;
697	}
698
699	from = blk_rq_pos(req);
700	nr = blk_rq_sectors(req);
701
702	if (mmc_can_trim(card))
703		arg = MMC_TRIM_ARG;
704	else
705		arg = MMC_ERASE_ARG;
706
707	if (card->quirks & MMC_QUIRK_INAND_CMD38) {
708		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
709				 INAND_CMD38_ARG_EXT_CSD,
710				 arg == MMC_TRIM_ARG ?
711				 INAND_CMD38_ARG_TRIM :
712				 INAND_CMD38_ARG_ERASE,
713				 0);
714		if (err)
715			goto out;
716	}
717	err = mmc_erase(card, from, nr, arg);
718out:
719	spin_lock_irq(&md->lock);
720	__blk_end_request(req, err, blk_rq_bytes(req));
721	spin_unlock_irq(&md->lock);
722
723	return err ? 0 : 1;
724}
725
726static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
727				       struct request *req)
728{
729	struct mmc_blk_data *md = mq->data;
730	struct mmc_card *card = md->queue.card;
731	unsigned int from, nr, arg;
732	int err = 0;
733
734	if (!mmc_can_secure_erase_trim(card)) {
735		err = -EOPNOTSUPP;
736		goto out;
737	}
738
739	from = blk_rq_pos(req);
740	nr = blk_rq_sectors(req);
741
742	if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
743		arg = MMC_SECURE_TRIM1_ARG;
744	else
745		arg = MMC_SECURE_ERASE_ARG;
746
747	if (card->quirks & MMC_QUIRK_INAND_CMD38) {
748		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
749				 INAND_CMD38_ARG_EXT_CSD,
750				 arg == MMC_SECURE_TRIM1_ARG ?
751				 INAND_CMD38_ARG_SECTRIM1 :
752				 INAND_CMD38_ARG_SECERASE,
753				 0);
754		if (err)
755			goto out;
756	}
757	err = mmc_erase(card, from, nr, arg);
758	if (!err && arg == MMC_SECURE_TRIM1_ARG) {
759		if (card->quirks & MMC_QUIRK_INAND_CMD38) {
760			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
761					 INAND_CMD38_ARG_EXT_CSD,
762					 INAND_CMD38_ARG_SECTRIM2,
763					 0);
764			if (err)
765				goto out;
766		}
767		err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
768	}
769out:
770	spin_lock_irq(&md->lock);
771	__blk_end_request(req, err, blk_rq_bytes(req));
772	spin_unlock_irq(&md->lock);
773
774	return err ? 0 : 1;
775}
776
777static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
778{
779	struct mmc_blk_data *md = mq->data;
780
781	/*
782	 * No-op, only service this because we need REQ_FUA for reliable
783	 * writes.
784	 */
785	spin_lock_irq(&md->lock);
786	__blk_end_request_all(req, 0);
787	spin_unlock_irq(&md->lock);
788
789	return 1;
790}
791
792/*
793 * Reformat current write as a reliable write, supporting
794 * both legacy and the enhanced reliable write MMC cards.
795 * In each transfer we'll handle only as much as a single
796 * reliable write can handle, thus finish the request in
797 * partial completions.
798 */
799static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
800				    struct mmc_card *card,
801				    struct request *req)
802{
803	if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
804		/* Legacy mode imposes restrictions on transfers. */
805		if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
806			brq->data.blocks = 1;
807
808		if (brq->data.blocks > card->ext_csd.rel_sectors)
809			brq->data.blocks = card->ext_csd.rel_sectors;
810		else if (brq->data.blocks < card->ext_csd.rel_sectors)
811			brq->data.blocks = 1;
812	}
813}
814
815static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
816{
817	struct mmc_blk_data *md = mq->data;
818	struct mmc_card *card = md->queue.card;
819	struct mmc_blk_request brq;
820	int ret = 1, disable_multi = 0, retry = 0;
821
822	/*
823	 * Reliable writes are used to implement Forced Unit Access and
824	 * REQ_META accesses, and are supported only on MMCs.
825	 */
826	bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
827			  (req->cmd_flags & REQ_META)) &&
828		(rq_data_dir(req) == WRITE) &&
829		(md->flags & MMC_BLK_REL_WR);
830
831	do {
832		u32 readcmd, writecmd;
833
834		memset(&brq, 0, sizeof(struct mmc_blk_request));
835		brq.mrq.cmd = &brq.cmd;
836		brq.mrq.data = &brq.data;
837
838		brq.cmd.arg = blk_rq_pos(req);
839		if (!mmc_card_blockaddr(card))
840			brq.cmd.arg <<= 9;
841		brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
842		brq.data.blksz = 512;
843		brq.stop.opcode = MMC_STOP_TRANSMISSION;
844		brq.stop.arg = 0;
845		brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
846		brq.data.blocks = blk_rq_sectors(req);
847
848		/*
849		 * The block layer doesn't support all sector count
850		 * restrictions, so we need to be prepared for too big
851		 * requests.
852		 */
853		if (brq.data.blocks > card->host->max_blk_count)
854			brq.data.blocks = card->host->max_blk_count;
855
856		/*
857		 * After a read error, we redo the request one sector at a time
858		 * in order to accurately determine which sectors can be read
859		 * successfully.
860		 */
861		if (disable_multi && brq.data.blocks > 1)
862			brq.data.blocks = 1;
863
864		if (brq.data.blocks > 1 || do_rel_wr) {
865			/* SPI multiblock writes terminate using a special
866			 * token, not a STOP_TRANSMISSION request.
867			 */
868			if (!mmc_host_is_spi(card->host) ||
869			    rq_data_dir(req) == READ)
870				brq.mrq.stop = &brq.stop;
871			readcmd = MMC_READ_MULTIPLE_BLOCK;
872			writecmd = MMC_WRITE_MULTIPLE_BLOCK;
873		} else {
874			brq.mrq.stop = NULL;
875			readcmd = MMC_READ_SINGLE_BLOCK;
876			writecmd = MMC_WRITE_BLOCK;
877		}
878		if (rq_data_dir(req) == READ) {
879			brq.cmd.opcode = readcmd;
880			brq.data.flags |= MMC_DATA_READ;
881		} else {
882			brq.cmd.opcode = writecmd;
883			brq.data.flags |= MMC_DATA_WRITE;
884		}
885
886		if (do_rel_wr)
887			mmc_apply_rel_rw(&brq, card, req);
888
889		/*
890		 * Pre-defined multi-block transfers are preferable to
891		 * open ended-ones (and necessary for reliable writes).
892		 * However, it is not sufficient to just send CMD23,
893		 * and avoid the final CMD12, as on an error condition
894		 * CMD12 (stop) needs to be sent anyway. This, coupled
895		 * with Auto-CMD23 enhancements provided by some
896		 * hosts, means that the complexity of dealing
897		 * with this is best left to the host. If CMD23 is
898		 * supported by card and host, we'll fill sbc in and let
899		 * the host deal with handling it correctly. This means
900		 * that for hosts that don't expose MMC_CAP_CMD23, no
901		 * change of behavior will be observed.
902		 *
903		 * N.B: Some MMC cards experience perf degradation.
904		 * We'll avoid using CMD23-bounded multiblock writes for
905		 * these, while retaining features like reliable writes.
906		 */
907
908		if ((md->flags & MMC_BLK_CMD23) &&
909		    mmc_op_multi(brq.cmd.opcode) &&
910		    (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
911			brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
912			brq.sbc.arg = brq.data.blocks |
913				(do_rel_wr ? (1 << 31) : 0);
914			brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
915			brq.mrq.sbc = &brq.sbc;
916		}
917
918		mmc_set_data_timeout(&brq.data, card);
919
920		brq.data.sg = mq->sg;
921		brq.data.sg_len = mmc_queue_map_sg(mq);
922
923		/*
924		 * Adjust the sg list so it is the same size as the
925		 * request.
926		 */
927		if (brq.data.blocks != blk_rq_sectors(req)) {
928			int i, data_size = brq.data.blocks << 9;
929			struct scatterlist *sg;
930
931			for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
932				data_size -= sg->length;
933				if (data_size <= 0) {
934					sg->length += data_size;
935					i++;
936					break;
937				}
938			}
939			brq.data.sg_len = i;
940		}
941
942		mmc_queue_bounce_pre(mq);
943
944		mmc_wait_for_req(card->host, &brq.mrq);
945
946		mmc_queue_bounce_post(mq);
947
948		/*
949		 * sbc.error indicates a problem with the set block count
950		 * command.  No data will have been transferred.
951		 *
952		 * cmd.error indicates a problem with the r/w command.  No
953		 * data will have been transferred.
954		 *
955		 * stop.error indicates a problem with the stop command.  Data
956		 * may have been transferred, or may still be transferring.
957		 */
958		if (brq.sbc.error || brq.cmd.error || brq.stop.error) {
959			switch (mmc_blk_cmd_recovery(card, req, &brq)) {
960			case ERR_RETRY:
961				if (retry++ < 5)
962					continue;
963			case ERR_ABORT:
964				goto cmd_abort;
965			case ERR_CONTINUE:
966				break;
967			}
968		}
969
970		if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
971			u32 status;
972			do {
973				int err = get_card_status(card, &status, 5);
974				if (err) {
975					printk(KERN_ERR "%s: error %d requesting status\n",
976					       req->rq_disk->disk_name, err);
977					goto cmd_err;
978				}
979				/*
980				 * Some cards mishandle the status bits,
981				 * so make sure to check both the busy
982				 * indication and the card state.
983				 */
984			} while (!(status & R1_READY_FOR_DATA) ||
985				 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
986		}
987
988		if (brq.data.error) {
989			pr_err("%s: error %d transferring data, sector %u nr %u, cmd response %#x card status %#x\n",
990				req->rq_disk->disk_name, brq.data.error,
991				(unsigned)blk_rq_pos(req),
992				(unsigned)blk_rq_sectors(req),
993				brq.cmd.resp[0], brq.stop.resp[0]);
994
995			if (rq_data_dir(req) == READ) {
996				if (brq.data.blocks > 1) {
997					/* Redo read one sector at a time */
998					pr_warning("%s: retrying using single block read\n",
999						req->rq_disk->disk_name);
1000					disable_multi = 1;
1001					continue;
1002				}
1003
1004				/*
1005				 * After an error, we redo I/O one sector at a
1006				 * time, so we only reach here after trying to
1007				 * read a single sector.
1008				 */
1009				spin_lock_irq(&md->lock);
1010				ret = __blk_end_request(req, -EIO, brq.data.blksz);
1011				spin_unlock_irq(&md->lock);
1012				continue;
1013			} else {
1014				goto cmd_err;
1015			}
1016		}
1017
1018		/*
1019		 * A block was successfully transferred.
1020		 */
1021		spin_lock_irq(&md->lock);
1022		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1023		spin_unlock_irq(&md->lock);
1024	} while (ret);
1025
1026	return 1;
1027
1028 cmd_err:
1029 	/*
1030 	 * If this is an SD card and we're writing, we can first
1031 	 * mark the known good sectors as ok.
1032 	 *
1033	 * If the card is not SD, we can still ok written sectors
1034	 * as reported by the controller (which might be less than
1035	 * the real number of written sectors, but never more).
1036	 */
1037	if (mmc_card_sd(card)) {
1038		u32 blocks;
1039
1040		blocks = mmc_sd_num_wr_blocks(card);
1041		if (blocks != (u32)-1) {
1042			spin_lock_irq(&md->lock);
1043			ret = __blk_end_request(req, 0, blocks << 9);
1044			spin_unlock_irq(&md->lock);
1045		}
1046	} else {
1047		spin_lock_irq(&md->lock);
1048		ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1049		spin_unlock_irq(&md->lock);
1050	}
1051
1052 cmd_abort:
1053	spin_lock_irq(&md->lock);
1054	while (ret)
1055		ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1056	spin_unlock_irq(&md->lock);
1057
1058	return 0;
1059}
1060
1061static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1062{
1063	int ret;
1064	struct mmc_blk_data *md = mq->data;
1065	struct mmc_card *card = md->queue.card;
1066
1067	mmc_claim_host(card->host);
1068	ret = mmc_blk_part_switch(card, md);
1069	if (ret) {
1070		ret = 0;
1071		goto out;
1072	}
1073
1074	if (req->cmd_flags & REQ_DISCARD) {
1075		if (req->cmd_flags & REQ_SECURE)
1076			ret = mmc_blk_issue_secdiscard_rq(mq, req);
1077		else
1078			ret = mmc_blk_issue_discard_rq(mq, req);
1079	} else if (req->cmd_flags & REQ_FLUSH) {
1080		ret = mmc_blk_issue_flush(mq, req);
1081	} else {
1082		ret = mmc_blk_issue_rw_rq(mq, req);
1083	}
1084
1085out:
1086	mmc_release_host(card->host);
1087	return ret;
1088}
1089
1090static inline int mmc_blk_readonly(struct mmc_card *card)
1091{
1092	return mmc_card_readonly(card) ||
1093	       !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1094}
1095
1096static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1097					      struct device *parent,
1098					      sector_t size,
1099					      bool default_ro,
1100					      const char *subname)
1101{
1102	struct mmc_blk_data *md;
1103	int devidx, ret;
1104
1105	devidx = find_first_zero_bit(dev_use, max_devices);
1106	if (devidx >= max_devices)
1107		return ERR_PTR(-ENOSPC);
1108	__set_bit(devidx, dev_use);
1109
1110	md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1111	if (!md) {
1112		ret = -ENOMEM;
1113		goto out;
1114	}
1115
1116	/*
1117	 * !subname implies we are creating main mmc_blk_data that will be
1118	 * associated with mmc_card with mmc_set_drvdata. Due to device
1119	 * partitions, devidx will not coincide with a per-physical card
1120	 * index anymore so we keep track of a name index.
1121	 */
1122	if (!subname) {
1123		md->name_idx = find_first_zero_bit(name_use, max_devices);
1124		__set_bit(md->name_idx, name_use);
1125	}
1126	else
1127		md->name_idx = ((struct mmc_blk_data *)
1128				dev_to_disk(parent)->private_data)->name_idx;
1129
1130	/*
1131	 * Set the read-only status based on the supported commands
1132	 * and the write protect switch.
1133	 */
1134	md->read_only = mmc_blk_readonly(card);
1135
1136	md->disk = alloc_disk(perdev_minors);
1137	if (md->disk == NULL) {
1138		ret = -ENOMEM;
1139		goto err_kfree;
1140	}
1141
1142	spin_lock_init(&md->lock);
1143	INIT_LIST_HEAD(&md->part);
1144	md->usage = 1;
1145
1146	ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1147	if (ret)
1148		goto err_putdisk;
1149
1150	md->queue.issue_fn = mmc_blk_issue_rq;
1151	md->queue.data = md;
1152
1153	md->disk->major	= MMC_BLOCK_MAJOR;
1154	md->disk->first_minor = devidx * perdev_minors;
1155	md->disk->fops = &mmc_bdops;
1156	md->disk->private_data = md;
1157	md->disk->queue = md->queue.queue;
1158	md->disk->driverfs_dev = parent;
1159	set_disk_ro(md->disk, md->read_only || default_ro);
1160
1161	/*
1162	 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1163	 *
1164	 * - be set for removable media with permanent block devices
1165	 * - be unset for removable block devices with permanent media
1166	 *
1167	 * Since MMC block devices clearly fall under the second
1168	 * case, we do not set GENHD_FL_REMOVABLE.  Userspace
1169	 * should use the block device creation/destruction hotplug
1170	 * messages to tell when the card is present.
1171	 */
1172
1173	snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1174		 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1175
1176	blk_queue_logical_block_size(md->queue.queue, 512);
1177	set_capacity(md->disk, size);
1178
1179	if (mmc_host_cmd23(card->host)) {
1180		if (mmc_card_mmc(card) ||
1181		    (mmc_card_sd(card) &&
1182		     card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1183			md->flags |= MMC_BLK_CMD23;
1184	}
1185
1186	if (mmc_card_mmc(card) &&
1187	    md->flags & MMC_BLK_CMD23 &&
1188	    ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1189	     card->ext_csd.rel_sectors)) {
1190		md->flags |= MMC_BLK_REL_WR;
1191		blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1192	}
1193
1194	return md;
1195
1196 err_putdisk:
1197	put_disk(md->disk);
1198 err_kfree:
1199	kfree(md);
1200 out:
1201	return ERR_PTR(ret);
1202}
1203
1204static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1205{
1206	sector_t size;
1207	struct mmc_blk_data *md;
1208
1209	if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1210		/*
1211		 * The EXT_CSD sector count is in number or 512 byte
1212		 * sectors.
1213		 */
1214		size = card->ext_csd.sectors;
1215	} else {
1216		/*
1217		 * The CSD capacity field is in units of read_blkbits.
1218		 * set_capacity takes units of 512 bytes.
1219		 */
1220		size = card->csd.capacity << (card->csd.read_blkbits - 9);
1221	}
1222
1223	md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1224	return md;
1225}
1226
1227static int mmc_blk_alloc_part(struct mmc_card *card,
1228			      struct mmc_blk_data *md,
1229			      unsigned int part_type,
1230			      sector_t size,
1231			      bool default_ro,
1232			      const char *subname)
1233{
1234	char cap_str[10];
1235	struct mmc_blk_data *part_md;
1236
1237	part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1238				    subname);
1239	if (IS_ERR(part_md))
1240		return PTR_ERR(part_md);
1241	part_md->part_type = part_type;
1242	list_add(&part_md->part, &md->part);
1243
1244	string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1245			cap_str, sizeof(cap_str));
1246	printk(KERN_INFO "%s: %s %s partition %u %s\n",
1247	       part_md->disk->disk_name, mmc_card_id(card),
1248	       mmc_card_name(card), part_md->part_type, cap_str);
1249	return 0;
1250}
1251
1252static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1253{
1254	int ret = 0;
1255
1256	if (!mmc_card_mmc(card))
1257		return 0;
1258
1259	if (card->ext_csd.boot_size) {
1260		ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1261					 card->ext_csd.boot_size >> 9,
1262					 true,
1263					 "boot0");
1264		if (ret)
1265			return ret;
1266		ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1267					 card->ext_csd.boot_size >> 9,
1268					 true,
1269					 "boot1");
1270		if (ret)
1271			return ret;
1272	}
1273
1274	return ret;
1275}
1276
1277static int
1278mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1279{
1280	int err;
1281
1282	mmc_claim_host(card->host);
1283	err = mmc_set_blocklen(card, 512);
1284	mmc_release_host(card->host);
1285
1286	if (err) {
1287		printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1288			md->disk->disk_name, err);
1289		return -EINVAL;
1290	}
1291
1292	return 0;
1293}
1294
1295static void mmc_blk_remove_req(struct mmc_blk_data *md)
1296{
1297	if (md) {
1298		if (md->disk->flags & GENHD_FL_UP) {
1299			device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1300
1301			/* Stop new requests from getting into the queue */
1302			del_gendisk(md->disk);
1303		}
1304
1305		/* Then flush out any already in there */
1306		mmc_cleanup_queue(&md->queue);
1307		mmc_blk_put(md);
1308	}
1309}
1310
1311static void mmc_blk_remove_parts(struct mmc_card *card,
1312				 struct mmc_blk_data *md)
1313{
1314	struct list_head *pos, *q;
1315	struct mmc_blk_data *part_md;
1316
1317	__clear_bit(md->name_idx, name_use);
1318	list_for_each_safe(pos, q, &md->part) {
1319		part_md = list_entry(pos, struct mmc_blk_data, part);
1320		list_del(pos);
1321		mmc_blk_remove_req(part_md);
1322	}
1323}
1324
1325static int mmc_add_disk(struct mmc_blk_data *md)
1326{
1327	int ret;
1328
1329	add_disk(md->disk);
1330	md->force_ro.show = force_ro_show;
1331	md->force_ro.store = force_ro_store;
1332	sysfs_attr_init(&md->force_ro.attr);
1333	md->force_ro.attr.name = "force_ro";
1334	md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1335	ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1336	if (ret)
1337		del_gendisk(md->disk);
1338
1339	return ret;
1340}
1341
1342static const struct mmc_fixup blk_fixups[] =
1343{
1344	MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1345	MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1346	MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1347	MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1348	MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1349
1350	/*
1351	 * Some MMC cards experience performance degradation with CMD23
1352	 * instead of CMD12-bounded multiblock transfers. For now we'll
1353	 * black list what's bad...
1354	 * - Certain Toshiba cards.
1355	 *
1356	 * N.B. This doesn't affect SD cards.
1357	 */
1358	MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1359		  MMC_QUIRK_BLK_NO_CMD23),
1360	MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1361		  MMC_QUIRK_BLK_NO_CMD23),
1362	MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1363		  MMC_QUIRK_BLK_NO_CMD23),
1364	END_FIXUP
1365};
1366
1367static int mmc_blk_probe(struct mmc_card *card)
1368{
1369	struct mmc_blk_data *md, *part_md;
1370	int err;
1371	char cap_str[10];
1372
1373	/*
1374	 * Check that the card supports the command class(es) we need.
1375	 */
1376	if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1377		return -ENODEV;
1378
1379	md = mmc_blk_alloc(card);
1380	if (IS_ERR(md))
1381		return PTR_ERR(md);
1382
1383	err = mmc_blk_set_blksize(md, card);
1384	if (err)
1385		goto out;
1386
1387	string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1388			cap_str, sizeof(cap_str));
1389	printk(KERN_INFO "%s: %s %s %s %s\n",
1390		md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1391		cap_str, md->read_only ? "(ro)" : "");
1392
1393	if (mmc_blk_alloc_parts(card, md))
1394		goto out;
1395
1396	mmc_set_drvdata(card, md);
1397	mmc_fixup_device(card, blk_fixups);
1398
1399	if (mmc_add_disk(md))
1400		goto out;
1401
1402	list_for_each_entry(part_md, &md->part, part) {
1403		if (mmc_add_disk(part_md))
1404			goto out;
1405	}
1406	return 0;
1407
1408 out:
1409	mmc_blk_remove_parts(card, md);
1410	mmc_blk_remove_req(md);
1411	return err;
1412}
1413
1414static void mmc_blk_remove(struct mmc_card *card)
1415{
1416	struct mmc_blk_data *md = mmc_get_drvdata(card);
1417
1418	mmc_blk_remove_parts(card, md);
1419	mmc_claim_host(card->host);
1420	mmc_blk_part_switch(card, md);
1421	mmc_release_host(card->host);
1422	mmc_blk_remove_req(md);
1423	mmc_set_drvdata(card, NULL);
1424}
1425
1426#ifdef CONFIG_PM
1427static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1428{
1429	struct mmc_blk_data *part_md;
1430	struct mmc_blk_data *md = mmc_get_drvdata(card);
1431
1432	if (md) {
1433		mmc_queue_suspend(&md->queue);
1434		list_for_each_entry(part_md, &md->part, part) {
1435			mmc_queue_suspend(&part_md->queue);
1436		}
1437	}
1438	return 0;
1439}
1440
1441static int mmc_blk_resume(struct mmc_card *card)
1442{
1443	struct mmc_blk_data *part_md;
1444	struct mmc_blk_data *md = mmc_get_drvdata(card);
1445
1446	if (md) {
1447		mmc_blk_set_blksize(md, card);
1448
1449		/*
1450		 * Resume involves the card going into idle state,
1451		 * so current partition is always the main one.
1452		 */
1453		md->part_curr = md->part_type;
1454		mmc_queue_resume(&md->queue);
1455		list_for_each_entry(part_md, &md->part, part) {
1456			mmc_queue_resume(&part_md->queue);
1457		}
1458	}
1459	return 0;
1460}
1461#else
1462#define	mmc_blk_suspend	NULL
1463#define mmc_blk_resume	NULL
1464#endif
1465
1466static struct mmc_driver mmc_driver = {
1467	.drv		= {
1468		.name	= "mmcblk",
1469	},
1470	.probe		= mmc_blk_probe,
1471	.remove		= mmc_blk_remove,
1472	.suspend	= mmc_blk_suspend,
1473	.resume		= mmc_blk_resume,
1474};
1475
1476static int __init mmc_blk_init(void)
1477{
1478	int res;
1479
1480	if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1481		pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1482
1483	max_devices = 256 / perdev_minors;
1484
1485	res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1486	if (res)
1487		goto out;
1488
1489	res = mmc_register_driver(&mmc_driver);
1490	if (res)
1491		goto out2;
1492
1493	return 0;
1494 out2:
1495	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1496 out:
1497	return res;
1498}
1499
1500static void __exit mmc_blk_exit(void)
1501{
1502	mmc_unregister_driver(&mmc_driver);
1503	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1504}
1505
1506module_init(mmc_blk_init);
1507module_exit(mmc_blk_exit);
1508
1509MODULE_LICENSE("GPL");
1510MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1511
1512