LCOV - code coverage report
Current view: top level - drivers/mmc/card - block.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 952 0.0 %
Date: 2014-04-16 Functions: 0 49 0.0 %
Branches: 0 757 0.0 %

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

Generated by: LCOV version 1.9