118
150
mutex_unlock(&open_lock);
153
static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
157
struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
159
ret = snprintf(buf, PAGE_SIZE, "%d",
160
get_disk_ro(dev_to_disk(dev)) ^
166
static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167
const char *buf, size_t count)
171
struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172
unsigned long set = simple_strtoul(buf, &end, 0);
178
set_disk_ro(dev_to_disk(dev), set || md->read_only);
121
185
static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
123
187
struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
225
struct mmc_blk_ioc_data {
226
struct mmc_ioc_cmd ic;
231
static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232
struct mmc_ioc_cmd __user *user)
234
struct mmc_blk_ioc_data *idata;
237
idata = kzalloc(sizeof(*idata), GFP_KERNEL);
243
if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
248
idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249
if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
254
idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
260
if (copy_from_user(idata->buf, (void __user *)(unsigned long)
261
idata->ic.data_ptr, idata->buf_bytes)) {
276
static int mmc_blk_ioctl_cmd(struct block_device *bdev,
277
struct mmc_ioc_cmd __user *ic_ptr)
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;
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.
293
if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
296
idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
298
return PTR_ERR(idata);
300
cmd.opcode = idata->ic.opcode;
301
cmd.arg = idata->ic.arg;
302
cmd.flags = idata->ic.flags;
306
data.blksz = idata->ic.blksz;
307
data.blocks = idata->ic.blocks;
309
sg_init_one(data.sg, idata->buf, idata->buf_bytes);
311
if (idata->ic.write_flag)
312
data.flags = MMC_DATA_WRITE;
314
data.flags = MMC_DATA_READ;
319
md = mmc_blk_get(bdev->bd_disk);
325
card = md->queue.card;
331
mmc_claim_host(card->host);
333
if (idata->ic.is_acmd) {
334
err = mmc_app_cmd(card->host, card);
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;
345
if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
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:
352
* cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
354
data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
357
mmc_wait_for_req(card->host, &mrq);
360
dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
361
__func__, cmd.error);
366
dev_err(mmc_dev(card->host), "%s: data error %d\n",
367
__func__, data.error);
373
* According to the SD specs, some commands require a delay after
374
* issuing the command.
376
if (idata->ic.postsleep_min_us)
377
usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
379
if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
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)) {
393
mmc_release_host(card->host);
402
static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
403
unsigned int cmd, unsigned long arg)
406
if (cmd == MMC_IOC_CMD)
407
ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
412
static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
413
unsigned int cmd, unsigned long arg)
415
return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
161
419
static const struct block_device_operations mmc_bdops = {
162
420
.open = mmc_blk_open,
163
421
.release = mmc_blk_release,
164
422
.getgeo = mmc_blk_getgeo,
165
423
.owner = THIS_MODULE,
424
.ioctl = mmc_blk_ioctl,
426
.compat_ioctl = mmc_blk_compat_ioctl,
168
430
struct mmc_blk_request {
169
431
struct mmc_request mrq;
432
struct mmc_command sbc;
170
433
struct mmc_command cmd;
171
434
struct mmc_command stop;
172
435
struct mmc_data data;
438
static inline int mmc_blk_part_switch(struct mmc_card *card,
439
struct mmc_blk_data *md)
442
struct mmc_blk_data *main_md = mmc_get_drvdata(card);
443
if (main_md->part_curr == md->part_type)
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;
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);
457
main_md->part_curr = md->part_type;
175
461
static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
181
struct mmc_request mrq;
182
struct mmc_command cmd;
183
struct mmc_data data;
467
struct mmc_request mrq = {0};
468
struct mmc_command cmd = {0};
469
struct mmc_data data = {0};
184
470
unsigned int timeout_us;
186
472
struct scatterlist sg;
188
memset(&cmd, 0, sizeof(struct mmc_command));
190
474
cmd.opcode = MMC_APP_CMD;
191
475
cmd.arg = card->rca << 16;
192
476
cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
319
602
arg = MMC_SECURE_ERASE_ARG;
604
if (card->quirks & MMC_QUIRK_INAND_CMD38) {
605
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
606
INAND_CMD38_ARG_EXT_CSD,
607
arg == MMC_SECURE_TRIM1_ARG ?
608
INAND_CMD38_ARG_SECTRIM1 :
609
INAND_CMD38_ARG_SECERASE,
321
614
err = mmc_erase(card, from, nr, arg);
322
if (!err && arg == MMC_SECURE_TRIM1_ARG)
615
if (!err && arg == MMC_SECURE_TRIM1_ARG) {
616
if (card->quirks & MMC_QUIRK_INAND_CMD38) {
617
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
618
INAND_CMD38_ARG_EXT_CSD,
619
INAND_CMD38_ARG_SECTRIM2,
323
624
err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
325
627
spin_lock_irq(&md->lock);
326
628
__blk_end_request(req, err, blk_rq_bytes(req));
327
629
spin_unlock_irq(&md->lock);
329
mmc_release_host(card->host);
331
631
return err ? 0 : 1;
634
static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
636
struct mmc_blk_data *md = mq->data;
639
* No-op, only service this because we need REQ_FUA for reliable
642
spin_lock_irq(&md->lock);
643
__blk_end_request_all(req, 0);
644
spin_unlock_irq(&md->lock);
650
* Reformat current write as a reliable write, supporting
651
* both legacy and the enhanced reliable write MMC cards.
652
* In each transfer we'll handle only as much as a single
653
* reliable write can handle, thus finish the request in
654
* partial completions.
656
static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
657
struct mmc_card *card,
660
if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
661
/* Legacy mode imposes restrictions on transfers. */
662
if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
663
brq->data.blocks = 1;
665
if (brq->data.blocks > card->ext_csd.rel_sectors)
666
brq->data.blocks = card->ext_csd.rel_sectors;
667
else if (brq->data.blocks < card->ext_csd.rel_sectors)
668
brq->data.blocks = 1;
334
672
static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
336
674
struct mmc_blk_data *md = mq->data;
396
741
brq.data.flags |= MMC_DATA_WRITE;
745
mmc_apply_rel_rw(&brq, card, req);
748
* Pre-defined multi-block transfers are preferable to
749
* open ended-ones (and necessary for reliable writes).
750
* However, it is not sufficient to just send CMD23,
751
* and avoid the final CMD12, as on an error condition
752
* CMD12 (stop) needs to be sent anyway. This, coupled
753
* with Auto-CMD23 enhancements provided by some
754
* hosts, means that the complexity of dealing
755
* with this is best left to the host. If CMD23 is
756
* supported by card and host, we'll fill sbc in and let
757
* the host deal with handling it correctly. This means
758
* that for hosts that don't expose MMC_CAP_CMD23, no
759
* change of behavior will be observed.
761
* N.B: Some MMC cards experience perf degradation.
762
* We'll avoid using CMD23-bounded multiblock writes for
763
* these, while retaining features like reliable writes.
766
if ((md->flags & MMC_BLK_CMD23) &&
767
mmc_op_multi(brq.cmd.opcode) &&
768
(do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
769
brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
770
brq.sbc.arg = brq.data.blocks |
771
(do_rel_wr ? (1 << 31) : 0);
772
brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
773
brq.mrq.sbc = &brq.sbc;
399
776
mmc_set_data_timeout(&brq.data, card);
401
778
brq.data.sg = mq->sg;
561
942
static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
945
struct mmc_blk_data *md = mq->data;
946
struct mmc_card *card = md->queue.card;
948
mmc_claim_host(card->host);
949
ret = mmc_blk_part_switch(card, md);
563
955
if (req->cmd_flags & REQ_DISCARD) {
564
956
if (req->cmd_flags & REQ_SECURE)
565
return mmc_blk_issue_secdiscard_rq(mq, req);
957
ret = mmc_blk_issue_secdiscard_rq(mq, req);
567
return mmc_blk_issue_discard_rq(mq, req);
959
ret = mmc_blk_issue_discard_rq(mq, req);
960
} else if (req->cmd_flags & REQ_FLUSH) {
961
ret = mmc_blk_issue_flush(mq, req);
569
return mmc_blk_issue_rw_rq(mq, req);
963
ret = mmc_blk_issue_rw_rq(mq, req);
967
mmc_release_host(card->host);
573
971
static inline int mmc_blk_readonly(struct mmc_card *card)
638
1054
snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1055
"mmcblk%d%s", md->name_idx, subname ? subname : "");
641
1057
blk_queue_logical_block_size(md->queue.queue, 512);
1058
set_capacity(md->disk, size);
1060
if (mmc_host_cmd23(card->host)) {
1061
if (mmc_card_mmc(card) ||
1062
(mmc_card_sd(card) &&
1063
card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1064
md->flags |= MMC_BLK_CMD23;
1067
if (mmc_card_mmc(card) &&
1068
md->flags & MMC_BLK_CMD23 &&
1069
((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1070
card->ext_csd.rel_sectors)) {
1071
md->flags |= MMC_BLK_REL_WR;
1072
blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1082
return ERR_PTR(ret);
1085
static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1088
struct mmc_blk_data *md;
643
1090
if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
645
1092
* The EXT_CSD sector count is in number or 512 byte
648
set_capacity(md->disk, card->ext_csd.sectors);
1095
size = card->ext_csd.sectors;
651
1098
* The CSD capacity field is in units of read_blkbits.
652
1099
* set_capacity takes units of 512 bytes.
654
set_capacity(md->disk,
655
card->csd.capacity << (card->csd.read_blkbits - 9));
1101
size = card->csd.capacity << (card->csd.read_blkbits - 9);
1104
md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1108
static int mmc_blk_alloc_part(struct mmc_card *card,
1109
struct mmc_blk_data *md,
1110
unsigned int part_type,
1113
const char *subname)
1116
struct mmc_blk_data *part_md;
1118
part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1120
if (IS_ERR(part_md))
1121
return PTR_ERR(part_md);
1122
part_md->part_type = part_type;
1123
list_add(&part_md->part, &md->part);
1125
string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1126
cap_str, sizeof(cap_str));
1127
printk(KERN_INFO "%s: %s %s partition %u %s\n",
1128
part_md->disk->disk_name, mmc_card_id(card),
1129
mmc_card_name(card), part_md->part_type, cap_str);
1133
static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1137
if (!mmc_card_mmc(card))
1140
if (card->ext_csd.boot_size) {
1141
ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1142
card->ext_csd.boot_size >> 9,
1147
ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1148
card->ext_csd.boot_size >> 9,
1176
static void mmc_blk_remove_req(struct mmc_blk_data *md)
1179
if (md->disk->flags & GENHD_FL_UP) {
1180
device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1182
/* Stop new requests from getting into the queue */
1183
del_gendisk(md->disk);
1186
/* Then flush out any already in there */
1187
mmc_cleanup_queue(&md->queue);
1192
static void mmc_blk_remove_parts(struct mmc_card *card,
1193
struct mmc_blk_data *md)
1195
struct list_head *pos, *q;
1196
struct mmc_blk_data *part_md;
1198
__clear_bit(md->name_idx, name_use);
1199
list_for_each_safe(pos, q, &md->part) {
1200
part_md = list_entry(pos, struct mmc_blk_data, part);
1202
mmc_blk_remove_req(part_md);
1206
static int mmc_add_disk(struct mmc_blk_data *md)
1211
md->force_ro.show = force_ro_show;
1212
md->force_ro.store = force_ro_store;
1213
sysfs_attr_init(&md->force_ro.attr);
1214
md->force_ro.attr.name = "force_ro";
1215
md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1216
ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1218
del_gendisk(md->disk);
1223
static const struct mmc_fixup blk_fixups[] =
1225
MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1226
MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1227
MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1228
MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1229
MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1232
* Some MMC cards experience performance degradation with CMD23
1233
* instead of CMD12-bounded multiblock transfers. For now we'll
1234
* black list what's bad...
1235
* - Certain Toshiba cards.
1237
* N.B. This doesn't affect SD cards.
1239
MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1240
MMC_QUIRK_BLK_NO_CMD23),
1241
MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1242
MMC_QUIRK_BLK_NO_CMD23),
1243
MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1244
MMC_QUIRK_BLK_NO_CMD23),
685
1248
static int mmc_blk_probe(struct mmc_card *card)
687
struct mmc_blk_data *md;
1250
struct mmc_blk_data *md, *part_md;
689
1252
char cap_str[10];
724
1297
struct mmc_blk_data *md = mmc_get_drvdata(card);
727
/* Stop new requests from getting into the queue */
728
del_gendisk(md->disk);
730
/* Then flush out any already in there */
731
mmc_cleanup_queue(&md->queue);
1299
mmc_blk_remove_parts(card, md);
1300
mmc_claim_host(card->host);
1301
mmc_blk_part_switch(card, md);
1302
mmc_release_host(card->host);
1303
mmc_blk_remove_req(md);
735
1304
mmc_set_drvdata(card, NULL);
738
1307
#ifdef CONFIG_PM
739
1308
static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1310
struct mmc_blk_data *part_md;
741
1311
struct mmc_blk_data *md = mmc_get_drvdata(card);
744
1314
mmc_queue_suspend(&md->queue);
1315
list_for_each_entry(part_md, &md->part, part) {
1316
mmc_queue_suspend(&part_md->queue);
749
1322
static int mmc_blk_resume(struct mmc_card *card)
1324
struct mmc_blk_data *part_md;
751
1325
struct mmc_blk_data *md = mmc_get_drvdata(card);
754
1328
mmc_blk_set_blksize(md, card);
1331
* Resume involves the card going into idle state,
1332
* so current partition is always the main one.
1334
md->part_curr = md->part_type;
755
1335
mmc_queue_resume(&md->queue);
1336
list_for_each_entry(part_md, &md->part, part) {
1337
mmc_queue_resume(&part_md->queue);