LCOV - code coverage report
Current view: top level - drivers/mmc/core - core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 874 5.3 %
Date: 2014-04-16 Functions: 5 79 6.3 %
Branches: 23 790 2.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *  linux/drivers/mmc/core/core.c
       3                 :            :  *
       4                 :            :  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
       5                 :            :  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
       6                 :            :  *  Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
       7                 :            :  *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
       8                 :            :  *
       9                 :            :  * This program is free software; you can redistribute it and/or modify
      10                 :            :  * it under the terms of the GNU General Public License version 2 as
      11                 :            :  * published by the Free Software Foundation.
      12                 :            :  */
      13                 :            : #include <linux/module.h>
      14                 :            : #include <linux/init.h>
      15                 :            : #include <linux/interrupt.h>
      16                 :            : #include <linux/completion.h>
      17                 :            : #include <linux/device.h>
      18                 :            : #include <linux/delay.h>
      19                 :            : #include <linux/pagemap.h>
      20                 :            : #include <linux/err.h>
      21                 :            : #include <linux/leds.h>
      22                 :            : #include <linux/scatterlist.h>
      23                 :            : #include <linux/log2.h>
      24                 :            : #include <linux/regulator/consumer.h>
      25                 :            : #include <linux/pm_runtime.h>
      26                 :            : #include <linux/pm_wakeup.h>
      27                 :            : #include <linux/suspend.h>
      28                 :            : #include <linux/fault-inject.h>
      29                 :            : #include <linux/random.h>
      30                 :            : #include <linux/slab.h>
      31                 :            : #include <linux/of.h>
      32                 :            : #include <linux/wakelock.h>
      33                 :            : 
      34                 :            : #include <trace/events/mmc.h>
      35                 :            : 
      36                 :            : #include <linux/mmc/card.h>
      37                 :            : #include <linux/mmc/host.h>
      38                 :            : #include <linux/mmc/mmc.h>
      39                 :            : #include <linux/mmc/sd.h>
      40                 :            : 
      41                 :            : #include "core.h"
      42                 :            : #include "bus.h"
      43                 :            : #include "host.h"
      44                 :            : #include "sdio_bus.h"
      45                 :            : 
      46                 :            : #include "mmc_ops.h"
      47                 :            : #include "sd_ops.h"
      48                 :            : #include "sdio_ops.h"
      49                 :            : 
      50                 :            : /* If the device is not responding */
      51                 :            : #define MMC_CORE_TIMEOUT_MS     (10 * 60 * 1000) /* 10 minute timeout */
      52                 :            : 
      53                 :            : /*
      54                 :            :  * Background operations can take a long time, depending on the housekeeping
      55                 :            :  * operations the card has to perform.
      56                 :            :  */
      57                 :            : #define MMC_BKOPS_MAX_TIMEOUT   (4 * 60 * 1000) /* max time to wait in ms */
      58                 :            : 
      59                 :            : static struct workqueue_struct *workqueue;
      60                 :            : static struct wake_lock mmc_delayed_work_wake_lock;
      61                 :            : static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
      62                 :            : 
      63                 :            : /*
      64                 :            :  * Enabling software CRCs on the data blocks can be a significant (30%)
      65                 :            :  * performance cost, and for other reasons may not always be desired.
      66                 :            :  * So we allow it it to be disabled.
      67                 :            :  */
      68                 :            : bool use_spi_crc = 1;
      69                 :            : module_param(use_spi_crc, bool, 0);
      70                 :            : 
      71                 :            : /*
      72                 :            :  * We normally treat cards as removed during suspend if they are not
      73                 :            :  * known to be on a non-removable bus, to avoid the risk of writing
      74                 :            :  * back data to a different card after resume.  Allow this to be
      75                 :            :  * overridden if necessary.
      76                 :            :  */
      77                 :            : #ifdef CONFIG_MMC_UNSAFE_RESUME
      78                 :            : bool mmc_assume_removable;
      79                 :            : #else
      80                 :            : bool mmc_assume_removable = 1;
      81                 :            : #endif
      82                 :            : EXPORT_SYMBOL(mmc_assume_removable);
      83                 :            : module_param_named(removable, mmc_assume_removable, bool, 0644);
      84                 :            : MODULE_PARM_DESC(
      85                 :            :         removable,
      86                 :            :         "MMC/SD cards are removable and may be removed during suspend");
      87                 :            : 
      88                 :            : /*
      89                 :            :  * Internal function. Schedule delayed work in the MMC work queue.
      90                 :            :  */
      91                 :          0 : static int mmc_schedule_delayed_work(struct delayed_work *work,
      92                 :            :                                      unsigned long delay)
      93                 :            : {
      94                 :            :         wake_lock(&mmc_delayed_work_wake_lock);
      95                 :      65868 :         return queue_delayed_work(workqueue, work, delay);
      96                 :            : }
      97                 :            : 
      98                 :            : /*
      99                 :            :  * Internal function. Flush all scheduled work from the MMC work queue.
     100                 :            :  */
     101                 :            : static void mmc_flush_scheduled_work(void)
     102                 :            : {
     103                 :          0 :         flush_workqueue(workqueue);
     104                 :            : }
     105                 :            : 
     106                 :            : #ifdef CONFIG_FAIL_MMC_REQUEST
     107                 :            : 
     108                 :            : /*
     109                 :            :  * Internal function. Inject random data errors.
     110                 :            :  * If mmc_data is NULL no errors are injected.
     111                 :            :  */
     112                 :            : static void mmc_should_fail_request(struct mmc_host *host,
     113                 :            :                                     struct mmc_request *mrq)
     114                 :            : {
     115                 :            :         struct mmc_command *cmd = mrq->cmd;
     116                 :            :         struct mmc_data *data = mrq->data;
     117                 :            :         static const int data_errors[] = {
     118                 :            :                 -ETIMEDOUT,
     119                 :            :                 -EILSEQ,
     120                 :            :                 -EIO,
     121                 :            :         };
     122                 :            : 
     123                 :            :         if (!data)
     124                 :            :                 return;
     125                 :            : 
     126                 :            :         if (cmd->error || data->error ||
     127                 :            :             !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
     128                 :            :                 return;
     129                 :            : 
     130                 :            :         data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
     131                 :            :         data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
     132                 :            : }
     133                 :            : 
     134                 :            : #else /* CONFIG_FAIL_MMC_REQUEST */
     135                 :            : 
     136                 :            : static inline void mmc_should_fail_request(struct mmc_host *host,
     137                 :            :                                            struct mmc_request *mrq)
     138                 :            : {
     139                 :            : }
     140                 :            : 
     141                 :            : #endif /* CONFIG_FAIL_MMC_REQUEST */
     142                 :            : 
     143                 :            : /**
     144                 :            :  *      mmc_request_done - finish processing an MMC request
     145                 :            :  *      @host: MMC host which completed request
     146                 :            :  *      @mrq: MMC request which request
     147                 :            :  *
     148                 :            :  *      MMC drivers should call this function when they have completed
     149                 :            :  *      their processing of a request.
     150                 :            :  */
     151                 :          0 : void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
     152                 :            : {
     153                 :          0 :         struct mmc_command *cmd = mrq->cmd;
     154                 :          0 :         int err = cmd->error;
     155                 :            : 
     156 [ #  # ][ #  # ]:          0 :         if (err && cmd->retries && mmc_host_is_spi(host)) {
                 [ #  # ]
     157         [ #  # ]:          0 :                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
     158                 :          0 :                         cmd->retries = 0;
     159                 :            :         }
     160                 :            : 
     161 [ #  # ][ #  # ]:          0 :         if (err && cmd->retries && !mmc_card_removed(host->card)) {
         [ #  # ][ #  # ]
     162                 :            :                 /*
     163                 :            :                  * Request starter must handle retries - see
     164                 :            :                  * mmc_wait_for_req_done().
     165                 :            :                  */
     166         [ #  # ]:          0 :                 if (mrq->done)
     167                 :          0 :                         mrq->done(mrq);
     168                 :            :         } else {
     169                 :            :                 mmc_should_fail_request(host, mrq);
     170                 :            : 
     171                 :          0 :                 led_trigger_event(host->led, LED_OFF);
     172                 :            : 
     173                 :            :                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
     174                 :            :                         mmc_hostname(host), cmd->opcode, err,
     175                 :            :                         cmd->resp[0], cmd->resp[1],
     176                 :            :                         cmd->resp[2], cmd->resp[3]);
     177                 :            : 
     178         [ #  # ]:          0 :                 if (mrq->data) {
     179                 :            :                         pr_debug("%s:     %d bytes transferred: %d\n",
     180                 :            :                                 mmc_hostname(host),
     181                 :            :                                 mrq->data->bytes_xfered, mrq->data->error);
     182                 :          0 :                         trace_mmc_blk_rw_end(cmd->opcode, cmd->arg, mrq->data);
     183                 :            :                 }
     184                 :            : 
     185                 :            :                 if (mrq->stop) {
     186                 :            :                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
     187                 :            :                                 mmc_hostname(host), mrq->stop->opcode,
     188                 :            :                                 mrq->stop->error,
     189                 :            :                                 mrq->stop->resp[0], mrq->stop->resp[1],
     190                 :            :                                 mrq->stop->resp[2], mrq->stop->resp[3]);
     191                 :            :                 }
     192                 :            : 
     193         [ #  # ]:          0 :                 if (mrq->done)
     194                 :          0 :                         mrq->done(mrq);
     195                 :            : 
     196                 :            :                 mmc_host_clk_release(host);
     197                 :            :         }
     198                 :          0 : }
     199                 :            : 
     200                 :            : EXPORT_SYMBOL(mmc_request_done);
     201                 :            : 
     202                 :            : static void
     203                 :          0 : mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
     204                 :            : {
     205                 :            : #ifdef CONFIG_MMC_DEBUG
     206                 :            :         unsigned int i, sz;
     207                 :            :         struct scatterlist *sg;
     208                 :            : #endif
     209                 :            : 
     210                 :            :         if (mrq->sbc) {
     211                 :            :                 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
     212                 :            :                          mmc_hostname(host), mrq->sbc->opcode,
     213                 :            :                          mrq->sbc->arg, mrq->sbc->flags);
     214                 :            :         }
     215                 :            : 
     216                 :            :         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
     217                 :            :                  mmc_hostname(host), mrq->cmd->opcode,
     218                 :            :                  mrq->cmd->arg, mrq->cmd->flags);
     219                 :            : 
     220                 :            :         if (mrq->data) {
     221                 :            :                 pr_debug("%s:     blksz %d blocks %d flags %08x "
     222                 :            :                         "tsac %d ms nsac %d\n",
     223                 :            :                         mmc_hostname(host), mrq->data->blksz,
     224                 :            :                         mrq->data->blocks, mrq->data->flags,
     225                 :            :                         mrq->data->timeout_ns / 1000000,
     226                 :            :                         mrq->data->timeout_clks);
     227                 :            :         }
     228                 :            : 
     229                 :            :         if (mrq->stop) {
     230                 :            :                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
     231                 :            :                          mmc_hostname(host), mrq->stop->opcode,
     232                 :            :                          mrq->stop->arg, mrq->stop->flags);
     233                 :            :         }
     234                 :            : 
     235         [ #  # ]:          0 :         WARN_ON(!host->claimed);
     236                 :            : 
     237                 :          0 :         mrq->cmd->error = 0;
     238                 :          0 :         mrq->cmd->mrq = mrq;
     239         [ #  # ]:          0 :         if (mrq->data) {
     240         [ #  # ]:          0 :                 BUG_ON(mrq->data->blksz > host->max_blk_size);
     241         [ #  # ]:          0 :                 BUG_ON(mrq->data->blocks > host->max_blk_count);
     242         [ #  # ]:          0 :                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
     243                 :            :                         host->max_req_size);
     244                 :            : 
     245                 :            : #ifdef CONFIG_MMC_DEBUG
     246                 :            :                 sz = 0;
     247                 :            :                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
     248                 :            :                         sz += sg->length;
     249                 :            :                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
     250                 :            : #endif
     251                 :            : 
     252                 :          0 :                 mrq->cmd->data = mrq->data;
     253                 :          0 :                 mrq->data->error = 0;
     254                 :          0 :                 mrq->data->mrq = mrq;
     255         [ #  # ]:          0 :                 if (mrq->stop) {
     256                 :          0 :                         mrq->data->stop = mrq->stop;
     257                 :          0 :                         mrq->stop->error = 0;
     258                 :          0 :                         mrq->stop->mrq = mrq;
     259                 :            :                 }
     260                 :            :         }
     261                 :            :         mmc_host_clk_hold(host);
     262                 :          0 :         led_trigger_event(host->led, LED_FULL);
     263                 :          0 :         host->ops->request(host, mrq);
     264                 :          0 : }
     265                 :            : 
     266                 :            : /**
     267                 :            :  *      mmc_start_bkops - start BKOPS for supported cards
     268                 :            :  *      @card: MMC card to start BKOPS
     269                 :            :  *      @form_exception: A flag to indicate if this function was
     270                 :            :  *                       called due to an exception raised by the card
     271                 :            :  *
     272                 :            :  *      Start background operations whenever requested.
     273                 :            :  *      When the urgent BKOPS bit is set in a R1 command response
     274                 :            :  *      then background operations should be started immediately.
     275                 :            : */
     276                 :          0 : void mmc_start_bkops(struct mmc_card *card, bool from_exception)
     277                 :            : {
     278                 :            :         int err;
     279                 :            :         int timeout;
     280                 :            :         bool use_busy_signal;
     281                 :            : 
     282         [ #  # ]:          0 :         BUG_ON(!card);
     283                 :            : 
     284 [ #  # ][ #  # ]:          0 :         if (!card->ext_csd.bkops_en || mmc_card_doing_bkops(card))
     285                 :            :                 return;
     286                 :            : 
     287                 :          0 :         err = mmc_read_bkops_status(card);
     288         [ #  # ]:          0 :         if (err) {
     289                 :          0 :                 pr_err("%s: Failed to read bkops status: %d\n",
     290                 :            :                        mmc_hostname(card->host), err);
     291                 :          0 :                 return;
     292                 :            :         }
     293                 :            : 
     294         [ #  # ]:          0 :         if (!card->ext_csd.raw_bkops_status)
     295                 :            :                 return;
     296                 :            : 
     297 [ #  # ][ #  # ]:          0 :         if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
     298                 :            :             from_exception)
     299                 :            :                 return;
     300                 :            : 
     301                 :          0 :         mmc_claim_host(card->host);
     302         [ #  # ]:          0 :         if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
     303                 :            :                 timeout = MMC_BKOPS_MAX_TIMEOUT;
     304                 :            :                 use_busy_signal = true;
     305                 :            :         } else {
     306                 :            :                 timeout = 0;
     307                 :            :                 use_busy_signal = false;
     308                 :            :         }
     309                 :            : 
     310                 :          0 :         err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
     311                 :            :                         EXT_CSD_BKOPS_START, 1, timeout, use_busy_signal, true);
     312         [ #  # ]:          0 :         if (err) {
     313                 :          0 :                 pr_warn("%s: Error %d starting bkops\n",
     314                 :            :                         mmc_hostname(card->host), err);
     315                 :          0 :                 goto out;
     316                 :            :         }
     317                 :            : 
     318                 :            :         /*
     319                 :            :          * For urgent bkops status (LEVEL_2 and more)
     320                 :            :          * bkops executed synchronously, otherwise
     321                 :            :          * the operation is in progress
     322                 :            :          */
     323         [ #  # ]:          0 :         if (!use_busy_signal)
     324                 :          0 :                 mmc_card_set_doing_bkops(card);
     325                 :            : out:
     326                 :          0 :         mmc_release_host(card->host);
     327                 :            : }
     328                 :            : EXPORT_SYMBOL(mmc_start_bkops);
     329                 :            : 
     330                 :            : /*
     331                 :            :  * mmc_wait_data_done() - done callback for data request
     332                 :            :  * @mrq: done data request
     333                 :            :  *
     334                 :            :  * Wakes up mmc context, passed as a callback to host controller driver
     335                 :            :  */
     336                 :          0 : static void mmc_wait_data_done(struct mmc_request *mrq)
     337                 :            : {
     338                 :          0 :         mrq->host->context_info.is_done_rcv = true;
     339                 :          0 :         wake_up_interruptible(&mrq->host->context_info.wait);
     340                 :          0 : }
     341                 :            : 
     342                 :          0 : static void mmc_wait_done(struct mmc_request *mrq)
     343                 :            : {
     344                 :          0 :         complete(&mrq->completion);
     345                 :          0 : }
     346                 :            : 
     347                 :            : /*
     348                 :            :  *__mmc_start_data_req() - starts data request
     349                 :            :  * @host: MMC host to start the request
     350                 :            :  * @mrq: data request to start
     351                 :            :  *
     352                 :            :  * Sets the done callback to be called when request is completed by the card.
     353                 :            :  * Starts data mmc request execution
     354                 :            :  */
     355                 :          0 : static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
     356                 :            : {
     357                 :          0 :         mrq->done = mmc_wait_data_done;
     358                 :          0 :         mrq->host = host;
     359 [ #  # ][ #  # ]:          0 :         if (mmc_card_removed(host->card)) {
     360                 :          0 :                 mrq->cmd->error = -ENOMEDIUM;
     361                 :            :                 mmc_wait_data_done(mrq);
     362                 :          0 :                 return -ENOMEDIUM;
     363                 :            :         }
     364                 :          0 :         mmc_start_request(host, mrq);
     365                 :            : 
     366                 :          0 :         return 0;
     367                 :            : }
     368                 :            : 
     369                 :          0 : static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
     370                 :            : {
     371                 :            :         init_completion(&mrq->completion);
     372                 :          0 :         mrq->done = mmc_wait_done;
     373 [ #  # ][ #  # ]:          0 :         if (mmc_card_removed(host->card)) {
     374                 :          0 :                 mrq->cmd->error = -ENOMEDIUM;
     375                 :          0 :                 complete(&mrq->completion);
     376                 :          0 :                 return -ENOMEDIUM;
     377                 :            :         }
     378                 :          0 :         mmc_start_request(host, mrq);
     379                 :          0 :         return 0;
     380                 :            : }
     381                 :            : 
     382                 :            : /*
     383                 :            :  * mmc_wait_for_data_req_done() - wait for request completed
     384                 :            :  * @host: MMC host to prepare the command.
     385                 :            :  * @mrq: MMC request to wait for
     386                 :            :  *
     387                 :            :  * Blocks MMC context till host controller will ack end of data request
     388                 :            :  * execution or new request notification arrives from the block layer.
     389                 :            :  * Handles command retries.
     390                 :            :  *
     391                 :            :  * Returns enum mmc_blk_status after checking errors.
     392                 :            :  */
     393                 :          0 : static int mmc_wait_for_data_req_done(struct mmc_host *host,
     394                 :            :                                       struct mmc_request *mrq,
     395                 :            :                                       struct mmc_async_req *next_req)
     396                 :            : {
     397                 :            :         struct mmc_command *cmd;
     398                 :            :         struct mmc_context_info *context_info = &host->context_info;
     399                 :            :         int err;
     400                 :            :         unsigned long flags;
     401                 :            : 
     402                 :            :         while (1) {
     403 [ #  # ][ #  # ]:          0 :                 wait_event_interruptible(context_info->wait,
         [ #  # ][ #  # ]
                 [ #  # ]
     404                 :            :                                 (context_info->is_done_rcv ||
     405                 :            :                                  context_info->is_new_req));
     406                 :          0 :                 spin_lock_irqsave(&context_info->lock, flags);
     407                 :          0 :                 context_info->is_waiting_last_req = false;
     408                 :            :                 spin_unlock_irqrestore(&context_info->lock, flags);
     409         [ #  # ]:          0 :                 if (context_info->is_done_rcv) {
     410                 :          0 :                         context_info->is_done_rcv = false;
     411                 :          0 :                         context_info->is_new_req = false;
     412                 :          0 :                         cmd = mrq->cmd;
     413                 :            : 
     414 [ #  # ][ #  # ]:          0 :                         if (!cmd->error || !cmd->retries ||
                 [ #  # ]
     415         [ #  # ]:          0 :                             mmc_card_removed(host->card)) {
     416                 :          0 :                                 err = host->areq->err_check(host->card,
     417                 :            :                                                             host->areq);
     418                 :          0 :                                 break; /* return err */
     419                 :            :                         } else {
     420                 :          0 :                                 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
     421                 :            :                                         mmc_hostname(host),
     422                 :            :                                         cmd->opcode, cmd->error);
     423                 :          0 :                                 cmd->retries--;
     424                 :          0 :                                 cmd->error = 0;
     425                 :          0 :                                 host->ops->request(host, mrq);
     426                 :          0 :                                 continue; /* wait for done/new event again */
     427                 :            :                         }
     428         [ #  # ]:          0 :                 } else if (context_info->is_new_req) {
     429                 :          0 :                         context_info->is_new_req = false;
     430         [ #  # ]:          0 :                         if (!next_req) {
     431                 :            :                                 err = MMC_BLK_NEW_REQUEST;
     432                 :            :                                 break; /* return err */
     433                 :            :                         }
     434                 :            :                 }
     435                 :            :         }
     436                 :          0 :         return err;
     437                 :            : }
     438                 :            : 
     439                 :          0 : static void mmc_wait_for_req_done(struct mmc_host *host,
     440                 :            :                                   struct mmc_request *mrq)
     441                 :            : {
     442                 :            :         struct mmc_command *cmd;
     443                 :            : 
     444                 :            :         while (1) {
     445                 :          0 :                 wait_for_completion(&mrq->completion);
     446                 :            : 
     447                 :          0 :                 cmd = mrq->cmd;
     448                 :            : 
     449                 :            :                 /*
     450                 :            :                  * If host has timed out waiting for the sanitize
     451                 :            :                  * to complete, card might be still in programming state
     452                 :            :                  * so let's try to bring the card out of programming
     453                 :            :                  * state.
     454                 :            :                  */
     455 [ #  # ][ #  # ]:          0 :                 if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
     456         [ #  # ]:          0 :                         if (!mmc_interrupt_hpi(host->card)) {
     457                 :          0 :                                 pr_warning("%s: %s: Interrupted sanitize\n",
     458                 :            :                                            mmc_hostname(host), __func__);
     459                 :          0 :                                 cmd->error = 0;
     460                 :          0 :                                 break;
     461                 :            :                         } else {
     462                 :          0 :                                 pr_err("%s: %s: Failed to interrupt sanitize\n",
     463                 :            :                                        mmc_hostname(host), __func__);
     464                 :            :                         }
     465                 :            :                 }
     466 [ #  # ][ #  # ]:          0 :                 if (!cmd->error || !cmd->retries ||
                 [ #  # ]
     467         [ #  # ]:          0 :                     mmc_card_removed(host->card))
     468                 :            :                         break;
     469                 :            : 
     470                 :            :                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
     471                 :            :                          mmc_hostname(host), cmd->opcode, cmd->error);
     472                 :          0 :                 cmd->retries--;
     473                 :          0 :                 cmd->error = 0;
     474                 :          0 :                 host->ops->request(host, mrq);
     475                 :          0 :         }
     476                 :          0 : }
     477                 :            : 
     478                 :            : /**
     479                 :            :  *      mmc_pre_req - Prepare for a new request
     480                 :            :  *      @host: MMC host to prepare command
     481                 :            :  *      @mrq: MMC request to prepare for
     482                 :            :  *      @is_first_req: true if there is no previous started request
     483                 :            :  *                     that may run in parellel to this call, otherwise false
     484                 :            :  *
     485                 :            :  *      mmc_pre_req() is called in prior to mmc_start_req() to let
     486                 :            :  *      host prepare for the new request. Preparation of a request may be
     487                 :            :  *      performed while another request is running on the host.
     488                 :            :  */
     489                 :            : static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
     490                 :            :                  bool is_first_req)
     491                 :            : {
     492         [ #  # ]:          0 :         if (host->ops->pre_req) {
     493                 :            :                 mmc_host_clk_hold(host);
     494                 :          0 :                 host->ops->pre_req(host, mrq, is_first_req);
     495                 :            :                 mmc_host_clk_release(host);
     496                 :            :         }
     497                 :            : }
     498                 :            : 
     499                 :            : /**
     500                 :            :  *      mmc_post_req - Post process a completed request
     501                 :            :  *      @host: MMC host to post process command
     502                 :            :  *      @mrq: MMC request to post process for
     503                 :            :  *      @err: Error, if non zero, clean up any resources made in pre_req
     504                 :            :  *
     505                 :            :  *      Let the host post process a completed request. Post processing of
     506                 :            :  *      a request may be performed while another reuqest is running.
     507                 :            :  */
     508                 :            : static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
     509                 :            :                          int err)
     510                 :            : {
     511 [ #  # ][ #  # ]:          0 :         if (host->ops->post_req) {
     512                 :            :                 mmc_host_clk_hold(host);
     513                 :          0 :                 host->ops->post_req(host, mrq, err);
     514                 :            :                 mmc_host_clk_release(host);
     515                 :            :         }
     516                 :            : }
     517                 :            : 
     518                 :            : /**
     519                 :            :  *      mmc_start_req - start a non-blocking request
     520                 :            :  *      @host: MMC host to start command
     521                 :            :  *      @areq: async request to start
     522                 :            :  *      @error: out parameter returns 0 for success, otherwise non zero
     523                 :            :  *
     524                 :            :  *      Start a new MMC custom command request for a host.
     525                 :            :  *      If there is on ongoing async request wait for completion
     526                 :            :  *      of that request and start the new one and return.
     527                 :            :  *      Does not wait for the new request to complete.
     528                 :            :  *
     529                 :            :  *      Returns the completed request, NULL in case of none completed.
     530                 :            :  *      Wait for the an ongoing request (previoulsy started) to complete and
     531                 :            :  *      return the completed request. If there is no ongoing request, NULL
     532                 :            :  *      is returned without waiting. NULL is not an error condition.
     533                 :            :  */
     534                 :          0 : struct mmc_async_req *mmc_start_req(struct mmc_host *host,
     535                 :            :                                     struct mmc_async_req *areq, int *error)
     536                 :            : {
     537                 :            :         int err = 0;
     538                 :            :         int start_err = 0;
     539                 :          0 :         struct mmc_async_req *data = host->areq;
     540                 :            : 
     541                 :            :         /* Prepare a new request */
     542         [ #  # ]:          0 :         if (areq)
     543                 :          0 :                 mmc_pre_req(host, areq->mrq, !host->areq);
     544                 :            : 
     545         [ #  # ]:          0 :         if (host->areq) {
     546                 :          0 :                 err = mmc_wait_for_data_req_done(host, host->areq->mrq,   areq);
     547         [ #  # ]:          0 :                 if (err == MMC_BLK_NEW_REQUEST) {
     548         [ #  # ]:          0 :                         if (error)
     549                 :          0 :                                 *error = err;
     550                 :            :                         /*
     551                 :            :                          * The previous request was not completed,
     552                 :            :                          * nothing to return
     553                 :            :                          */
     554                 :            :                         return NULL;
     555                 :            :                 }
     556                 :            :                 /*
     557                 :            :                  * Check BKOPS urgency for each R1 response
     558                 :            :                  */
     559 [ #  # ][ #  # ]:          0 :                 if (host->card && mmc_card_mmc(host->card) &&
                 [ #  # ]
     560                 :          0 :                     ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
     561         [ #  # ]:          0 :                      (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
     562                 :          0 :                     (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT))
     563                 :          0 :                         mmc_start_bkops(host->card, true);
     564                 :            :         }
     565                 :            : 
     566         [ #  # ]:          0 :         if (!err && areq) {
     567                 :          0 :                 trace_mmc_blk_rw_start(areq->mrq->cmd->opcode,
     568                 :            :                                        areq->mrq->cmd->arg,
     569                 :            :                                        areq->mrq->data);
     570                 :          0 :                 start_err = __mmc_start_data_req(host, areq->mrq);
     571                 :            :         }
     572                 :            : 
     573         [ #  # ]:          0 :         if (host->areq)
     574                 :          0 :                 mmc_post_req(host, host->areq->mrq, 0);
     575                 :            : 
     576                 :            :          /* Cancel a prepared request if it was not started. */
     577 [ #  # ][ #  # ]:          0 :         if ((err || start_err) && areq)
     578                 :          0 :                 mmc_post_req(host, areq->mrq, -EINVAL);
     579                 :            : 
     580         [ #  # ]:          0 :         if (err)
     581                 :          0 :                 host->areq = NULL;
     582                 :            :         else
     583                 :          0 :                 host->areq = areq;
     584                 :            : 
     585         [ #  # ]:          0 :         if (error)
     586                 :          0 :                 *error = err;
     587                 :          0 :         return data;
     588                 :            : }
     589                 :            : EXPORT_SYMBOL(mmc_start_req);
     590                 :            : 
     591                 :            : /**
     592                 :            :  *      mmc_wait_for_req - start a request and wait for completion
     593                 :            :  *      @host: MMC host to start command
     594                 :            :  *      @mrq: MMC request to start
     595                 :            :  *
     596                 :            :  *      Start a new MMC custom command request for a host, and wait
     597                 :            :  *      for the command to complete. Does not attempt to parse the
     598                 :            :  *      response.
     599                 :            :  */
     600                 :          0 : void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
     601                 :            : {
     602                 :          0 :         __mmc_start_req(host, mrq);
     603                 :          0 :         mmc_wait_for_req_done(host, mrq);
     604                 :          0 : }
     605                 :            : EXPORT_SYMBOL(mmc_wait_for_req);
     606                 :            : 
     607                 :            : /**
     608                 :            :  *      mmc_interrupt_hpi - Issue for High priority Interrupt
     609                 :            :  *      @card: the MMC card associated with the HPI transfer
     610                 :            :  *
     611                 :            :  *      Issued High Priority Interrupt, and check for card status
     612                 :            :  *      until out-of prg-state.
     613                 :            :  */
     614                 :          0 : int mmc_interrupt_hpi(struct mmc_card *card)
     615                 :            : {
     616                 :            :         int err;
     617                 :            :         u32 status;
     618                 :            :         unsigned long prg_wait;
     619                 :            : 
     620         [ #  # ]:          0 :         BUG_ON(!card);
     621                 :            : 
     622         [ #  # ]:          0 :         if (!card->ext_csd.hpi_en) {
     623                 :          0 :                 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
     624                 :          0 :                 return 1;
     625                 :            :         }
     626                 :            : 
     627                 :          0 :         mmc_claim_host(card->host);
     628                 :          0 :         err = mmc_send_status(card, &status);
     629         [ #  # ]:          0 :         if (err) {
     630                 :          0 :                 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
     631                 :          0 :                 goto out;
     632                 :            :         }
     633                 :            : 
     634 [ #  # ][ #  # ]:          0 :         switch (R1_CURRENT_STATE(status)) {
                 [ #  # ]
     635                 :            :         case R1_STATE_IDLE:
     636                 :            :         case R1_STATE_READY:
     637                 :            :         case R1_STATE_STBY:
     638                 :            :         case R1_STATE_TRAN:
     639                 :            :                 /*
     640                 :            :                  * In idle and transfer states, HPI is not needed and the caller
     641                 :            :                  * can issue the next intended command immediately
     642                 :            :                  */
     643                 :            :                 goto out;
     644                 :            :         case R1_STATE_PRG:
     645                 :            :                 break;
     646                 :            :         default:
     647                 :            :                 /* In all other states, it's illegal to issue HPI */
     648                 :          0 :                 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
     649                 :            :                         mmc_hostname(card->host), R1_CURRENT_STATE(status));
     650                 :            :                 err = -EINVAL;
     651                 :            :                 goto out;
     652                 :            :         }
     653                 :            : 
     654                 :          0 :         err = mmc_send_hpi_cmd(card, &status);
     655         [ #  # ]:          0 :         if (err)
     656                 :            :                 goto out;
     657                 :            : 
     658                 :          0 :         prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
     659                 :            :         do {
     660                 :          0 :                 err = mmc_send_status(card, &status);
     661                 :            : 
     662 [ #  # ][ #  # ]:          0 :                 if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
     663                 :            :                         break;
     664         [ #  # ]:          0 :                 if (time_after(jiffies, prg_wait))
     665                 :            :                         err = -ETIMEDOUT;
     666         [ #  # ]:          0 :         } while (!err);
     667                 :            : 
     668                 :            : out:
     669                 :          0 :         mmc_release_host(card->host);
     670                 :          0 :         return err;
     671                 :            : }
     672                 :            : EXPORT_SYMBOL(mmc_interrupt_hpi);
     673                 :            : 
     674                 :            : /**
     675                 :            :  *      mmc_wait_for_cmd - start a command and wait for completion
     676                 :            :  *      @host: MMC host to start command
     677                 :            :  *      @cmd: MMC command to start
     678                 :            :  *      @retries: maximum number of retries
     679                 :            :  *
     680                 :            :  *      Start a new MMC command for a host, and wait for the command
     681                 :            :  *      to complete.  Return any error that occurred while the command
     682                 :            :  *      was executing.  Do not attempt to parse the response.
     683                 :            :  */
     684                 :          0 : int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
     685                 :            : {
     686                 :          0 :         struct mmc_request mrq = {NULL};
     687                 :            : 
     688         [ #  # ]:          0 :         WARN_ON(!host->claimed);
     689                 :            : 
     690                 :          0 :         memset(cmd->resp, 0, sizeof(cmd->resp));
     691                 :          0 :         cmd->retries = retries;
     692                 :            : 
     693                 :          0 :         mrq.cmd = cmd;
     694                 :          0 :         cmd->data = NULL;
     695                 :            : 
     696                 :            :         mmc_wait_for_req(host, &mrq);
     697                 :            : 
     698                 :          0 :         return cmd->error;
     699                 :            : }
     700                 :            : 
     701                 :            : EXPORT_SYMBOL(mmc_wait_for_cmd);
     702                 :            : 
     703                 :            : /**
     704                 :            :  *      mmc_stop_bkops - stop ongoing BKOPS
     705                 :            :  *      @card: MMC card to check BKOPS
     706                 :            :  *
     707                 :            :  *      Send HPI command to stop ongoing background operations to
     708                 :            :  *      allow rapid servicing of foreground operations, e.g. read/
     709                 :            :  *      writes. Wait until the card comes out of the programming state
     710                 :            :  *      to avoid errors in servicing read/write requests.
     711                 :            :  */
     712                 :          0 : int mmc_stop_bkops(struct mmc_card *card)
     713                 :            : {
     714                 :            :         int err = 0;
     715                 :            : 
     716         [ #  # ]:          0 :         BUG_ON(!card);
     717                 :          0 :         err = mmc_interrupt_hpi(card);
     718                 :            : 
     719                 :            :         /*
     720                 :            :          * If err is EINVAL, we can't issue an HPI.
     721                 :            :          * It should complete the BKOPS.
     722                 :            :          */
     723         [ #  # ]:          0 :         if (!err || (err == -EINVAL)) {
     724                 :          0 :                 mmc_card_clr_doing_bkops(card);
     725                 :            :                 err = 0;
     726                 :            :         }
     727                 :            : 
     728                 :          0 :         return err;
     729                 :            : }
     730                 :            : EXPORT_SYMBOL(mmc_stop_bkops);
     731                 :            : 
     732                 :          0 : int mmc_read_bkops_status(struct mmc_card *card)
     733                 :            : {
     734                 :            :         int err;
     735                 :            :         u8 *ext_csd;
     736                 :            : 
     737                 :            :         /*
     738                 :            :          * In future work, we should consider storing the entire ext_csd.
     739                 :            :          */
     740                 :            :         ext_csd = kmalloc(512, GFP_KERNEL);
     741         [ #  # ]:          0 :         if (!ext_csd) {
     742                 :          0 :                 pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
     743                 :            :                        mmc_hostname(card->host));
     744                 :          0 :                 return -ENOMEM;
     745                 :            :         }
     746                 :            : 
     747                 :          0 :         mmc_claim_host(card->host);
     748                 :          0 :         err = mmc_send_ext_csd(card, ext_csd);
     749                 :          0 :         mmc_release_host(card->host);
     750         [ #  # ]:          0 :         if (err)
     751                 :            :                 goto out;
     752                 :            : 
     753                 :          0 :         card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
     754                 :          0 :         card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
     755                 :            : out:
     756                 :          0 :         kfree(ext_csd);
     757                 :          0 :         return err;
     758                 :            : }
     759                 :            : EXPORT_SYMBOL(mmc_read_bkops_status);
     760                 :            : 
     761                 :            : /**
     762                 :            :  *      mmc_set_data_timeout - set the timeout for a data command
     763                 :            :  *      @data: data phase for command
     764                 :            :  *      @card: the MMC card associated with the data transfer
     765                 :            :  *
     766                 :            :  *      Computes the data timeout parameters according to the
     767                 :            :  *      correct algorithm given the card type.
     768                 :            :  */
     769                 :          0 : void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
     770                 :            : {
     771                 :            :         unsigned int mult;
     772                 :            : 
     773                 :            :         /*
     774                 :            :          * SDIO cards only define an upper 1 s limit on access.
     775                 :            :          */
     776         [ #  # ]:          0 :         if (mmc_card_sdio(card)) {
     777                 :          0 :                 data->timeout_ns = 1000000000;
     778                 :          0 :                 data->timeout_clks = 0;
     779                 :          0 :                 return;
     780                 :            :         }
     781                 :            : 
     782                 :            :         /*
     783                 :            :          * SD cards use a 100 multiplier rather than 10
     784                 :            :          */
     785         [ #  # ]:          0 :         mult = mmc_card_sd(card) ? 100 : 10;
     786                 :            : 
     787                 :            :         /*
     788                 :            :          * Scale up the multiplier (and therefore the timeout) by
     789                 :            :          * the r2w factor for writes.
     790                 :            :          */
     791         [ #  # ]:          0 :         if (data->flags & MMC_DATA_WRITE)
     792                 :          0 :                 mult <<= card->csd.r2w_factor;
     793                 :            : 
     794                 :          0 :         data->timeout_ns = card->csd.tacc_ns * mult;
     795                 :          0 :         data->timeout_clks = card->csd.tacc_clks * mult;
     796                 :            : 
     797                 :            :         /*
     798                 :            :          * SD cards also have an upper limit on the timeout.
     799                 :            :          */
     800         [ #  # ]:          0 :         if (mmc_card_sd(card)) {
     801                 :            :                 unsigned int timeout_us, limit_us;
     802                 :            : 
     803                 :          0 :                 timeout_us = data->timeout_ns / 1000;
     804         [ #  # ]:          0 :                 if (mmc_host_clk_rate(card->host))
     805                 :          0 :                         timeout_us += data->timeout_clks * 1000 /
     806                 :          0 :                                 (mmc_host_clk_rate(card->host) / 1000);
     807                 :            : 
     808         [ #  # ]:          0 :                 if (data->flags & MMC_DATA_WRITE)
     809                 :            :                         /*
     810                 :            :                          * The MMC spec "It is strongly recommended
     811                 :            :                          * for hosts to implement more than 500ms
     812                 :            :                          * timeout value even if the card indicates
     813                 :            :                          * the 250ms maximum busy length."  Even the
     814                 :            :                          * previous value of 300ms is known to be
     815                 :            :                          * insufficient for some cards.
     816                 :            :                          */
     817                 :            :                         limit_us = 3000000;
     818                 :            :                 else
     819                 :            :                         limit_us = 100000;
     820                 :            : 
     821                 :            :                 /*
     822                 :            :                  * SDHC cards always use these fixed values.
     823                 :            :                  */
     824 [ #  # ][ #  # ]:          0 :                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
     825                 :          0 :                         data->timeout_ns = limit_us * 1000;
     826                 :          0 :                         data->timeout_clks = 0;
     827                 :            :                 }
     828                 :            :         }
     829                 :            : 
     830                 :            :         /*
     831                 :            :          * Some cards require longer data read timeout than indicated in CSD.
     832                 :            :          * Address this by setting the read timeout to a "reasonably high"
     833                 :            :          * value. For the cards tested, 300ms has proven enough. If necessary,
     834                 :            :          * this value can be increased if other problematic cards require this.
     835                 :            :          */
     836 [ #  # ][ #  # ]:          0 :         if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
     837                 :          0 :                 data->timeout_ns = 300000000;
     838                 :          0 :                 data->timeout_clks = 0;
     839                 :            :         }
     840                 :            : 
     841                 :            :         /*
     842                 :            :          * Some cards need very high timeouts if driven in SPI mode.
     843                 :            :          * The worst observed timeout was 900ms after writing a
     844                 :            :          * continuous stream of data until the internal logic
     845                 :            :          * overflowed.
     846                 :            :          */
     847         [ #  # ]:          0 :         if (mmc_host_is_spi(card->host)) {
     848         [ #  # ]:          0 :                 if (data->flags & MMC_DATA_WRITE) {
     849         [ #  # ]:          0 :                         if (data->timeout_ns < 1000000000)
     850                 :          0 :                                 data->timeout_ns = 1000000000;       /* 1s */
     851                 :            :                 } else {
     852         [ #  # ]:          0 :                         if (data->timeout_ns < 100000000)
     853                 :          0 :                                 data->timeout_ns =  100000000;       /* 100ms */
     854                 :            :                 }
     855                 :            :         }
     856                 :            : }
     857                 :            : EXPORT_SYMBOL(mmc_set_data_timeout);
     858                 :            : 
     859                 :            : /**
     860                 :            :  *      mmc_align_data_size - pads a transfer size to a more optimal value
     861                 :            :  *      @card: the MMC card associated with the data transfer
     862                 :            :  *      @sz: original transfer size
     863                 :            :  *
     864                 :            :  *      Pads the original data size with a number of extra bytes in
     865                 :            :  *      order to avoid controller bugs and/or performance hits
     866                 :            :  *      (e.g. some controllers revert to PIO for certain sizes).
     867                 :            :  *
     868                 :            :  *      Returns the improved size, which might be unmodified.
     869                 :            :  *
     870                 :            :  *      Note that this function is only relevant when issuing a
     871                 :            :  *      single scatter gather entry.
     872                 :            :  */
     873                 :          0 : unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
     874                 :            : {
     875                 :            :         /*
     876                 :            :          * FIXME: We don't have a system for the controller to tell
     877                 :            :          * the core about its problems yet, so for now we just 32-bit
     878                 :            :          * align the size.
     879                 :            :          */
     880                 :          0 :         sz = ((sz + 3) / 4) * 4;
     881                 :            : 
     882                 :          0 :         return sz;
     883                 :            : }
     884                 :            : EXPORT_SYMBOL(mmc_align_data_size);
     885                 :            : 
     886                 :            : /**
     887                 :            :  *      __mmc_claim_host - exclusively claim a host
     888                 :            :  *      @host: mmc host to claim
     889                 :            :  *      @abort: whether or not the operation should be aborted
     890                 :            :  *
     891                 :            :  *      Claim a host for a set of operations.  If @abort is non null and
     892                 :            :  *      dereference a non-zero value then this will return prematurely with
     893                 :            :  *      that non-zero value without acquiring the lock.  Returns zero
     894                 :            :  *      with the lock held otherwise.
     895                 :            :  */
     896                 :          0 : int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
     897                 :            : {
     898                 :      65868 :         DECLARE_WAITQUEUE(wait, current);
     899                 :            :         unsigned long flags;
     900                 :            :         int stop;
     901                 :            : 
     902                 :            :         might_sleep();
     903                 :            : 
     904                 :      32934 :         add_wait_queue(&host->wq, &wait);
     905                 :      32934 :         spin_lock_irqsave(&host->lock, flags);
     906                 :            :         while (1) {
     907                 :      32934 :                 set_current_state(TASK_UNINTERRUPTIBLE);
     908         [ -  + ]:      32934 :                 stop = abort ? atomic_read(abort) : 0;
     909 [ +  - ][ -  + ]:      32934 :                 if (stop || !host->claimed || host->claimer == current)
                 [ #  # ]
     910                 :            :                         break;
     911                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
     912                 :          0 :                 schedule();
     913                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
     914                 :          0 :         }
     915                 :      32934 :         set_current_state(TASK_RUNNING);
     916         [ +  - ]:      32934 :         if (!stop) {
     917                 :      32934 :                 host->claimed = 1;
     918                 :      32934 :                 host->claimer = current;
     919                 :      32934 :                 host->claim_cnt += 1;
     920                 :            :         } else
     921                 :          0 :                 wake_up(&host->wq);
     922                 :            :         spin_unlock_irqrestore(&host->lock, flags);
     923                 :      32934 :         remove_wait_queue(&host->wq, &wait);
     924 [ -  + ][ #  # ]:      32934 :         if (host->ops->enable && !stop && host->claim_cnt == 1)
                 [ #  # ]
     925                 :          0 :                 host->ops->enable(host);
     926                 :      32934 :         return stop;
     927                 :            : }
     928                 :            : 
     929                 :            : EXPORT_SYMBOL(__mmc_claim_host);
     930                 :            : 
     931                 :            : /**
     932                 :            :  *      mmc_release_host - release a host
     933                 :            :  *      @host: mmc host to release
     934                 :            :  *
     935                 :            :  *      Release a MMC host, allowing others to claim the host
     936                 :            :  *      for their operations.
     937                 :            :  */
     938                 :          0 : void mmc_release_host(struct mmc_host *host)
     939                 :            : {
     940                 :            :         unsigned long flags;
     941                 :            : 
     942         [ -  + ]:      32934 :         WARN_ON(!host->claimed);
     943                 :            : 
     944 [ -  + ][ #  # ]:      32934 :         if (host->ops->disable && host->claim_cnt == 1)
     945                 :          0 :                 host->ops->disable(host);
     946                 :            : 
     947                 :      32934 :         spin_lock_irqsave(&host->lock, flags);
     948         [ -  + ]:      32934 :         if (--host->claim_cnt) {
     949                 :            :                 /* Release for nested claim */
     950                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
     951                 :            :         } else {
     952                 :      32934 :                 host->claimed = 0;
     953                 :      32934 :                 host->claimer = NULL;
     954                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
     955                 :      32934 :                 wake_up(&host->wq);
     956                 :            :         }
     957                 :      32934 : }
     958                 :            : EXPORT_SYMBOL(mmc_release_host);
     959                 :            : 
     960                 :            : /*
     961                 :            :  * This is a helper function, which fetches a runtime pm reference for the
     962                 :            :  * card device and also claims the host.
     963                 :            :  */
     964                 :          0 : void mmc_get_card(struct mmc_card *card)
     965                 :            : {
     966                 :            :         pm_runtime_get_sync(&card->dev);
     967                 :          0 :         mmc_claim_host(card->host);
     968                 :          0 : }
     969                 :            : EXPORT_SYMBOL(mmc_get_card);
     970                 :            : 
     971                 :            : /*
     972                 :            :  * This is a helper function, which releases the host and drops the runtime
     973                 :            :  * pm reference for the card device.
     974                 :            :  */
     975                 :          0 : void mmc_put_card(struct mmc_card *card)
     976                 :            : {
     977                 :          0 :         mmc_release_host(card->host);
     978                 :            :         pm_runtime_mark_last_busy(&card->dev);
     979                 :            :         pm_runtime_put_autosuspend(&card->dev);
     980                 :          0 : }
     981                 :            : EXPORT_SYMBOL(mmc_put_card);
     982                 :            : 
     983                 :            : /*
     984                 :            :  * Internal function that does the actual ios call to the host driver,
     985                 :            :  * optionally printing some debug output.
     986                 :            :  */
     987                 :            : static inline void mmc_set_ios(struct mmc_host *host)
     988                 :            : {
     989                 :          0 :         struct mmc_ios *ios = &host->ios;
     990                 :            : 
     991                 :            :         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
     992                 :            :                 "width %u timing %u\n",
     993                 :            :                  mmc_hostname(host), ios->clock, ios->bus_mode,
     994                 :            :                  ios->power_mode, ios->chip_select, ios->vdd,
     995                 :            :                  ios->bus_width, ios->timing);
     996                 :            : 
     997         [ #  # ]:          0 :         if (ios->clock > 0)
           [ #  #  #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     998                 :          0 :                 mmc_set_ungated(host);
     999                 :          0 :         host->ops->set_ios(host, ios);
    1000                 :            : }
    1001                 :            : 
    1002                 :            : /*
    1003                 :            :  * Control chip select pin on a host.
    1004                 :            :  */
    1005                 :          0 : void mmc_set_chip_select(struct mmc_host *host, int mode)
    1006                 :            : {
    1007                 :            :         mmc_host_clk_hold(host);
    1008                 :          0 :         host->ios.chip_select = mode;
    1009                 :            :         mmc_set_ios(host);
    1010                 :            :         mmc_host_clk_release(host);
    1011                 :          0 : }
    1012                 :            : 
    1013                 :            : /*
    1014                 :            :  * Sets the host clock to the highest possible frequency that
    1015                 :            :  * is below "hz".
    1016                 :            :  */
    1017                 :          0 : static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
    1018                 :            : {
    1019         [ #  # ]:          0 :         WARN_ON(hz < host->f_min);
    1020                 :            : 
    1021         [ #  # ]:          0 :         if (hz > host->f_max)
    1022                 :            :                 hz = host->f_max;
    1023                 :            : 
    1024                 :          0 :         host->ios.clock = hz;
    1025                 :            :         mmc_set_ios(host);
    1026                 :          0 : }
    1027                 :            : 
    1028                 :          0 : void mmc_set_clock(struct mmc_host *host, unsigned int hz)
    1029                 :            : {
    1030                 :            :         mmc_host_clk_hold(host);
    1031                 :          0 :         __mmc_set_clock(host, hz);
    1032                 :            :         mmc_host_clk_release(host);
    1033                 :          0 : }
    1034                 :            : 
    1035                 :            : #ifdef CONFIG_MMC_CLKGATE
    1036                 :            : /*
    1037                 :            :  * This gates the clock by setting it to 0 Hz.
    1038                 :            :  */
    1039                 :            : void mmc_gate_clock(struct mmc_host *host)
    1040                 :            : {
    1041                 :            :         unsigned long flags;
    1042                 :            : 
    1043                 :            :         spin_lock_irqsave(&host->clk_lock, flags);
    1044                 :            :         host->clk_old = host->ios.clock;
    1045                 :            :         host->ios.clock = 0;
    1046                 :            :         host->clk_gated = true;
    1047                 :            :         spin_unlock_irqrestore(&host->clk_lock, flags);
    1048                 :            :         mmc_set_ios(host);
    1049                 :            : }
    1050                 :            : 
    1051                 :            : /*
    1052                 :            :  * This restores the clock from gating by using the cached
    1053                 :            :  * clock value.
    1054                 :            :  */
    1055                 :            : void mmc_ungate_clock(struct mmc_host *host)
    1056                 :            : {
    1057                 :            :         /*
    1058                 :            :          * We should previously have gated the clock, so the clock shall
    1059                 :            :          * be 0 here! The clock may however be 0 during initialization,
    1060                 :            :          * when some request operations are performed before setting
    1061                 :            :          * the frequency. When ungate is requested in that situation
    1062                 :            :          * we just ignore the call.
    1063                 :            :          */
    1064                 :            :         if (host->clk_old) {
    1065                 :            :                 BUG_ON(host->ios.clock);
    1066                 :            :                 /* This call will also set host->clk_gated to false */
    1067                 :            :                 __mmc_set_clock(host, host->clk_old);
    1068                 :            :         }
    1069                 :            : }
    1070                 :            : 
    1071                 :            : void mmc_set_ungated(struct mmc_host *host)
    1072                 :            : {
    1073                 :            :         unsigned long flags;
    1074                 :            : 
    1075                 :            :         /*
    1076                 :            :          * We've been given a new frequency while the clock is gated,
    1077                 :            :          * so make sure we regard this as ungating it.
    1078                 :            :          */
    1079                 :            :         spin_lock_irqsave(&host->clk_lock, flags);
    1080                 :            :         host->clk_gated = false;
    1081                 :            :         spin_unlock_irqrestore(&host->clk_lock, flags);
    1082                 :            : }
    1083                 :            : 
    1084                 :            : #else
    1085                 :          0 : void mmc_set_ungated(struct mmc_host *host)
    1086                 :            : {
    1087                 :          0 : }
    1088                 :            : #endif
    1089                 :            : 
    1090                 :            : /*
    1091                 :            :  * Change the bus mode (open drain/push-pull) of a host.
    1092                 :            :  */
    1093                 :          0 : void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
    1094                 :            : {
    1095                 :            :         mmc_host_clk_hold(host);
    1096                 :          0 :         host->ios.bus_mode = mode;
    1097                 :            :         mmc_set_ios(host);
    1098                 :            :         mmc_host_clk_release(host);
    1099                 :          0 : }
    1100                 :            : 
    1101                 :            : /*
    1102                 :            :  * Change data bus width of a host.
    1103                 :            :  */
    1104                 :          0 : void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
    1105                 :            : {
    1106                 :            :         mmc_host_clk_hold(host);
    1107                 :          0 :         host->ios.bus_width = width;
    1108                 :            :         mmc_set_ios(host);
    1109                 :            :         mmc_host_clk_release(host);
    1110                 :          0 : }
    1111                 :            : 
    1112                 :            : /**
    1113                 :            :  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
    1114                 :            :  * @vdd:        voltage (mV)
    1115                 :            :  * @low_bits:   prefer low bits in boundary cases
    1116                 :            :  *
    1117                 :            :  * This function returns the OCR bit number according to the provided @vdd
    1118                 :            :  * value. If conversion is not possible a negative errno value returned.
    1119                 :            :  *
    1120                 :            :  * Depending on the @low_bits flag the function prefers low or high OCR bits
    1121                 :            :  * on boundary voltages. For example,
    1122                 :            :  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
    1123                 :            :  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
    1124                 :            :  *
    1125                 :            :  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
    1126                 :            :  */
    1127                 :            : static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
    1128                 :            : {
    1129                 :            :         const int max_bit = ilog2(MMC_VDD_35_36);
    1130                 :            :         int bit;
    1131                 :            : 
    1132 [ #  # ][ #  # ]:          0 :         if (vdd < 1650 || vdd > 3600)
    1133                 :            :                 return -EINVAL;
    1134                 :            : 
    1135 [ #  # ][ #  # ]:          0 :         if (vdd >= 1650 && vdd <= 1950)
    1136                 :            :                 return ilog2(MMC_VDD_165_195);
    1137                 :            : 
    1138                 :            :         if (low_bits)
    1139                 :            :                 vdd -= 1;
    1140                 :            : 
    1141                 :            :         /* Base 2000 mV, step 100 mV, bit's base 8. */
    1142                 :          0 :         bit = (vdd - 2000) / 100 + 8;
    1143 [ #  # ][ #  # ]:          0 :         if (bit > max_bit)
    1144                 :            :                 return max_bit;
    1145                 :            :         return bit;
    1146                 :            : }
    1147                 :            : 
    1148                 :            : /**
    1149                 :            :  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
    1150                 :            :  * @vdd_min:    minimum voltage value (mV)
    1151                 :            :  * @vdd_max:    maximum voltage value (mV)
    1152                 :            :  *
    1153                 :            :  * This function returns the OCR mask bits according to the provided @vdd_min
    1154                 :            :  * and @vdd_max values. If conversion is not possible the function returns 0.
    1155                 :            :  *
    1156                 :            :  * Notes wrt boundary cases:
    1157                 :            :  * This function sets the OCR bits for all boundary voltages, for example
    1158                 :            :  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
    1159                 :            :  * MMC_VDD_34_35 mask.
    1160                 :            :  */
    1161                 :          0 : u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
    1162                 :            : {
    1163                 :            :         u32 mask = 0;
    1164                 :            : 
    1165         [ #  # ]:          0 :         if (vdd_max < vdd_min)
    1166                 :            :                 return 0;
    1167                 :            : 
    1168                 :            :         /* Prefer high bits for the boundary vdd_max values. */
    1169                 :            :         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
    1170         [ #  # ]:          0 :         if (vdd_max < 0)
    1171                 :            :                 return 0;
    1172                 :            : 
    1173                 :            :         /* Prefer low bits for the boundary vdd_min values. */
    1174                 :            :         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
    1175         [ #  # ]:          0 :         if (vdd_min < 0)
    1176                 :            :                 return 0;
    1177                 :            : 
    1178                 :            :         /* Fill the mask, from max bit to min bit. */
    1179         [ #  # ]:          0 :         while (vdd_max >= vdd_min)
    1180                 :          0 :                 mask |= 1 << vdd_max--;
    1181                 :            : 
    1182                 :            :         return mask;
    1183                 :            : }
    1184                 :            : EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
    1185                 :            : 
    1186                 :            : #ifdef CONFIG_OF
    1187                 :            : 
    1188                 :            : /**
    1189                 :            :  * mmc_of_parse_voltage - return mask of supported voltages
    1190                 :            :  * @np: The device node need to be parsed.
    1191                 :            :  * @mask: mask of voltages available for MMC/SD/SDIO
    1192                 :            :  *
    1193                 :            :  * 1. Return zero on success.
    1194                 :            :  * 2. Return negative errno: voltage-range is invalid.
    1195                 :            :  */
    1196                 :          0 : int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
    1197                 :            : {
    1198                 :            :         const u32 *voltage_ranges;
    1199                 :            :         int num_ranges, i;
    1200                 :            : 
    1201                 :          0 :         voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
    1202                 :          0 :         num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
    1203 [ #  # ][ #  # ]:          0 :         if (!voltage_ranges || !num_ranges) {
    1204                 :          0 :                 pr_info("%s: voltage-ranges unspecified\n", np->full_name);
    1205                 :          0 :                 return -EINVAL;
    1206                 :            :         }
    1207                 :            : 
    1208         [ #  # ]:          0 :         for (i = 0; i < num_ranges; i++) {
    1209                 :          0 :                 const int j = i * 2;
    1210                 :            :                 u32 ocr_mask;
    1211                 :            : 
    1212 [ #  # ][ #  # ]:          0 :                 ocr_mask = mmc_vddrange_to_ocrmask(
    1213                 :          0 :                                 be32_to_cpu(voltage_ranges[j]),
    1214                 :          0 :                                 be32_to_cpu(voltage_ranges[j + 1]));
    1215         [ #  # ]:          0 :                 if (!ocr_mask) {
    1216                 :          0 :                         pr_err("%s: voltage-range #%d is invalid\n",
    1217                 :            :                                 np->full_name, i);
    1218                 :          0 :                         return -EINVAL;
    1219                 :            :                 }
    1220                 :          0 :                 *mask |= ocr_mask;
    1221                 :            :         }
    1222                 :            : 
    1223                 :            :         return 0;
    1224                 :            : }
    1225                 :            : EXPORT_SYMBOL(mmc_of_parse_voltage);
    1226                 :            : 
    1227                 :            : #endif /* CONFIG_OF */
    1228                 :            : 
    1229                 :            : #ifdef CONFIG_REGULATOR
    1230                 :            : 
    1231                 :            : /**
    1232                 :            :  * mmc_regulator_get_ocrmask - return mask of supported voltages
    1233                 :            :  * @supply: regulator to use
    1234                 :            :  *
    1235                 :            :  * This returns either a negative errno, or a mask of voltages that
    1236                 :            :  * can be provided to MMC/SD/SDIO devices using the specified voltage
    1237                 :            :  * regulator.  This would normally be called before registering the
    1238                 :            :  * MMC host adapter.
    1239                 :            :  */
    1240                 :          0 : int mmc_regulator_get_ocrmask(struct regulator *supply)
    1241                 :            : {
    1242                 :            :         int                     result = 0;
    1243                 :            :         int                     count;
    1244                 :            :         int                     i;
    1245                 :            : 
    1246                 :          0 :         count = regulator_count_voltages(supply);
    1247         [ #  # ]:          0 :         if (count < 0)
    1248                 :            :                 return count;
    1249                 :            : 
    1250         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
    1251                 :            :                 int             vdd_uV;
    1252                 :            :                 int             vdd_mV;
    1253                 :            : 
    1254                 :          0 :                 vdd_uV = regulator_list_voltage(supply, i);
    1255         [ #  # ]:          0 :                 if (vdd_uV <= 0)
    1256                 :          0 :                         continue;
    1257                 :            : 
    1258                 :          0 :                 vdd_mV = vdd_uV / 1000;
    1259                 :          0 :                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
    1260                 :            :         }
    1261                 :            : 
    1262                 :            :         return result;
    1263                 :            : }
    1264                 :            : EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
    1265                 :            : 
    1266                 :            : /**
    1267                 :            :  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
    1268                 :            :  * @mmc: the host to regulate
    1269                 :            :  * @supply: regulator to use
    1270                 :            :  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
    1271                 :            :  *
    1272                 :            :  * Returns zero on success, else negative errno.
    1273                 :            :  *
    1274                 :            :  * MMC host drivers may use this to enable or disable a regulator using
    1275                 :            :  * a particular supply voltage.  This would normally be called from the
    1276                 :            :  * set_ios() method.
    1277                 :            :  */
    1278                 :          0 : int mmc_regulator_set_ocr(struct mmc_host *mmc,
    1279                 :            :                         struct regulator *supply,
    1280                 :            :                         unsigned short vdd_bit)
    1281                 :            : {
    1282                 :            :         int                     result = 0;
    1283                 :            :         int                     min_uV, max_uV;
    1284                 :            : 
    1285         [ #  # ]:          0 :         if (vdd_bit) {
    1286                 :            :                 int             tmp;
    1287                 :            :                 int             voltage;
    1288                 :            : 
    1289                 :            :                 /*
    1290                 :            :                  * REVISIT mmc_vddrange_to_ocrmask() may have set some
    1291                 :            :                  * bits this regulator doesn't quite support ... don't
    1292                 :            :                  * be too picky, most cards and regulators are OK with
    1293                 :            :                  * a 0.1V range goof (it's a small error percentage).
    1294                 :            :                  */
    1295                 :          0 :                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
    1296         [ #  # ]:          0 :                 if (tmp == 0) {
    1297                 :            :                         min_uV = 1650 * 1000;
    1298                 :            :                         max_uV = 1950 * 1000;
    1299                 :            :                 } else {
    1300                 :          0 :                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
    1301                 :          0 :                         max_uV = min_uV + 100 * 1000;
    1302                 :            :                 }
    1303                 :            : 
    1304                 :            :                 /*
    1305                 :            :                  * If we're using a fixed/static regulator, don't call
    1306                 :            :                  * regulator_set_voltage; it would fail.
    1307                 :            :                  */
    1308                 :          0 :                 voltage = regulator_get_voltage(supply);
    1309                 :            : 
    1310         [ #  # ]:          0 :                 if (!regulator_can_change_voltage(supply))
    1311                 :            :                         min_uV = max_uV = voltage;
    1312                 :            : 
    1313         [ #  # ]:          0 :                 if (voltage < 0)
    1314                 :            :                         result = voltage;
    1315         [ #  # ]:          0 :                 else if (voltage < min_uV || voltage > max_uV)
    1316                 :          0 :                         result = regulator_set_voltage(supply, min_uV, max_uV);
    1317                 :            :                 else
    1318                 :            :                         result = 0;
    1319                 :            : 
    1320 [ #  # ][ #  # ]:          0 :                 if (result == 0 && !mmc->regulator_enabled) {
    1321                 :          0 :                         result = regulator_enable(supply);
    1322         [ #  # ]:          0 :                         if (!result)
    1323                 :          0 :                                 mmc->regulator_enabled = true;
    1324                 :            :                 }
    1325         [ #  # ]:          0 :         } else if (mmc->regulator_enabled) {
    1326                 :          0 :                 result = regulator_disable(supply);
    1327         [ #  # ]:          0 :                 if (result == 0)
    1328                 :          0 :                         mmc->regulator_enabled = false;
    1329                 :            :         }
    1330                 :            : 
    1331         [ #  # ]:          0 :         if (result)
    1332                 :          0 :                 dev_err(mmc_dev(mmc),
    1333                 :            :                         "could not set regulator OCR (%d)\n", result);
    1334                 :          0 :         return result;
    1335                 :            : }
    1336                 :            : EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
    1337                 :            : 
    1338                 :          0 : int mmc_regulator_get_supply(struct mmc_host *mmc)
    1339                 :            : {
    1340                 :          0 :         struct device *dev = mmc_dev(mmc);
    1341                 :            :         struct regulator *supply;
    1342                 :            :         int ret;
    1343                 :            : 
    1344                 :          0 :         supply = devm_regulator_get(dev, "vmmc");
    1345                 :          0 :         mmc->supply.vmmc = supply;
    1346                 :          0 :         mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
    1347                 :            : 
    1348         [ #  # ]:          0 :         if (IS_ERR(supply))
    1349                 :          0 :                 return PTR_ERR(supply);
    1350                 :            : 
    1351                 :          0 :         ret = mmc_regulator_get_ocrmask(supply);
    1352         [ #  # ]:          0 :         if (ret > 0)
    1353                 :          0 :                 mmc->ocr_avail = ret;
    1354                 :            :         else
    1355                 :          0 :                 dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret);
    1356                 :            : 
    1357                 :            :         return 0;
    1358                 :            : }
    1359                 :            : EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
    1360                 :            : 
    1361                 :            : #endif /* CONFIG_REGULATOR */
    1362                 :            : 
    1363                 :            : /*
    1364                 :            :  * Mask off any voltages we don't support and select
    1365                 :            :  * the lowest voltage
    1366                 :            :  */
    1367                 :          0 : u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
    1368                 :            : {
    1369                 :            :         int bit;
    1370                 :            : 
    1371                 :            :         /*
    1372                 :            :          * Sanity check the voltages that the card claims to
    1373                 :            :          * support.
    1374                 :            :          */
    1375         [ #  # ]:          0 :         if (ocr & 0x7F) {
    1376                 :          0 :                 dev_warn(mmc_dev(host),
    1377                 :            :                 "card claims to support voltages below defined range\n");
    1378                 :          0 :                 ocr &= ~0x7F;
    1379                 :            :         }
    1380                 :            : 
    1381                 :          0 :         ocr &= host->ocr_avail;
    1382         [ #  # ]:          0 :         if (!ocr) {
    1383                 :          0 :                 dev_warn(mmc_dev(host), "no support for card's volts\n");
    1384                 :          0 :                 return 0;
    1385                 :            :         }
    1386                 :            : 
    1387         [ #  # ]:          0 :         if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
    1388                 :          0 :                 bit = ffs(ocr) - 1;
    1389                 :          0 :                 ocr &= 3 << bit;
    1390                 :          0 :                 mmc_power_cycle(host, ocr);
    1391                 :            :         } else {
    1392                 :          0 :                 bit = fls(ocr) - 1;
    1393                 :          0 :                 ocr &= 3 << bit;
    1394         [ #  # ]:          0 :                 if (bit != host->ios.vdd)
    1395                 :          0 :                         dev_warn(mmc_dev(host), "exceeding card's volts\n");
    1396                 :            :         }
    1397                 :            : 
    1398                 :          0 :         return ocr;
    1399                 :            : }
    1400                 :            : 
    1401                 :          0 : int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
    1402                 :            : {
    1403                 :            :         int err = 0;
    1404                 :          0 :         int old_signal_voltage = host->ios.signal_voltage;
    1405                 :            : 
    1406                 :          0 :         host->ios.signal_voltage = signal_voltage;
    1407         [ #  # ]:          0 :         if (host->ops->start_signal_voltage_switch) {
           [ #  #  #  # ]
                 [ #  # ]
    1408                 :            :                 mmc_host_clk_hold(host);
    1409                 :          0 :                 err = host->ops->start_signal_voltage_switch(host, &host->ios);
    1410                 :            :                 mmc_host_clk_release(host);
    1411                 :            :         }
    1412                 :            : 
    1413 [ #  # ][ #  # ]:          0 :         if (err)
         [ #  # ][ #  # ]
    1414                 :          0 :                 host->ios.signal_voltage = old_signal_voltage;
    1415                 :            : 
    1416                 :          0 :         return err;
    1417                 :            : 
    1418                 :            : }
    1419                 :            : 
    1420                 :          0 : int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
    1421                 :            : {
    1422                 :          0 :         struct mmc_command cmd = {0};
    1423                 :            :         int err = 0;
    1424                 :            :         u32 clock;
    1425                 :            : 
    1426         [ #  # ]:          0 :         BUG_ON(!host);
    1427                 :            : 
    1428                 :            :         /*
    1429                 :            :          * Send CMD11 only if the request is to switch the card to
    1430                 :            :          * 1.8V signalling.
    1431                 :            :          */
    1432         [ #  # ]:          0 :         if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
    1433                 :          0 :                 return __mmc_set_signal_voltage(host, signal_voltage);
    1434                 :            : 
    1435                 :            :         /*
    1436                 :            :          * If we cannot switch voltages, return failure so the caller
    1437                 :            :          * can continue without UHS mode
    1438                 :            :          */
    1439         [ #  # ]:          0 :         if (!host->ops->start_signal_voltage_switch)
    1440                 :            :                 return -EPERM;
    1441         [ #  # ]:          0 :         if (!host->ops->card_busy)
    1442                 :          0 :                 pr_warning("%s: cannot verify signal voltage switch\n",
    1443                 :            :                                 mmc_hostname(host));
    1444                 :            : 
    1445                 :          0 :         cmd.opcode = SD_SWITCH_VOLTAGE;
    1446                 :          0 :         cmd.arg = 0;
    1447                 :          0 :         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
    1448                 :            : 
    1449                 :          0 :         err = mmc_wait_for_cmd(host, &cmd, 0);
    1450         [ #  # ]:          0 :         if (err)
    1451                 :            :                 return err;
    1452                 :            : 
    1453 [ #  # ][ #  # ]:          0 :         if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
    1454                 :            :                 return -EIO;
    1455                 :            : 
    1456                 :            :         mmc_host_clk_hold(host);
    1457                 :            :         /*
    1458                 :            :          * The card should drive cmd and dat[0:3] low immediately
    1459                 :            :          * after the response of cmd11, but wait 1 ms to be sure
    1460                 :            :          */
    1461                 :            :         mmc_delay(1);
    1462 [ #  # ][ #  # ]:          0 :         if (host->ops->card_busy && !host->ops->card_busy(host)) {
    1463                 :            :                 err = -EAGAIN;
    1464                 :            :                 goto power_cycle;
    1465                 :            :         }
    1466                 :            :         /*
    1467                 :            :          * During a signal voltage level switch, the clock must be gated
    1468                 :            :          * for 5 ms according to the SD spec
    1469                 :            :          */
    1470                 :          0 :         clock = host->ios.clock;
    1471                 :          0 :         host->ios.clock = 0;
    1472                 :            :         mmc_set_ios(host);
    1473                 :            : 
    1474         [ #  # ]:          0 :         if (__mmc_set_signal_voltage(host, signal_voltage)) {
    1475                 :            :                 /*
    1476                 :            :                  * Voltages may not have been switched, but we've already
    1477                 :            :                  * sent CMD11, so a power cycle is required anyway
    1478                 :            :                  */
    1479                 :            :                 err = -EAGAIN;
    1480                 :            :                 goto power_cycle;
    1481                 :            :         }
    1482                 :            : 
    1483                 :            :         /* Keep clock gated for at least 5 ms */
    1484                 :            :         mmc_delay(5);
    1485                 :          0 :         host->ios.clock = clock;
    1486                 :            :         mmc_set_ios(host);
    1487                 :            : 
    1488                 :            :         /* Wait for at least 1 ms according to spec */
    1489                 :            :         mmc_delay(1);
    1490                 :            : 
    1491                 :            :         /*
    1492                 :            :          * Failure to switch is indicated by the card holding
    1493                 :            :          * dat[0:3] low
    1494                 :            :          */
    1495 [ #  # ][ #  # ]:          0 :         if (host->ops->card_busy && host->ops->card_busy(host))
    1496                 :            :                 err = -EAGAIN;
    1497                 :            : 
    1498                 :            : power_cycle:
    1499         [ #  # ]:          0 :         if (err) {
    1500                 :            :                 pr_debug("%s: Signal voltage switch failed, "
    1501                 :            :                         "power cycling card\n", mmc_hostname(host));
    1502                 :          0 :                 mmc_power_cycle(host, ocr);
    1503                 :            :         }
    1504                 :            : 
    1505                 :            :         mmc_host_clk_release(host);
    1506                 :            : 
    1507                 :          0 :         return err;
    1508                 :            : }
    1509                 :            : 
    1510                 :            : /*
    1511                 :            :  * Select timing parameters for host.
    1512                 :            :  */
    1513                 :          0 : void mmc_set_timing(struct mmc_host *host, unsigned int timing)
    1514                 :            : {
    1515                 :            :         mmc_host_clk_hold(host);
    1516                 :          0 :         host->ios.timing = timing;
    1517                 :            :         mmc_set_ios(host);
    1518                 :            :         mmc_host_clk_release(host);
    1519                 :          0 : }
    1520                 :            : 
    1521                 :            : /*
    1522                 :            :  * Select appropriate driver type for host.
    1523                 :            :  */
    1524                 :          0 : void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
    1525                 :            : {
    1526                 :            :         mmc_host_clk_hold(host);
    1527                 :          0 :         host->ios.drv_type = drv_type;
    1528                 :            :         mmc_set_ios(host);
    1529                 :            :         mmc_host_clk_release(host);
    1530                 :          0 : }
    1531                 :            : 
    1532                 :            : /*
    1533                 :            :  * Apply power to the MMC stack.  This is a two-stage process.
    1534                 :            :  * First, we enable power to the card without the clock running.
    1535                 :            :  * We then wait a bit for the power to stabilise.  Finally,
    1536                 :            :  * enable the bus drivers and clock to the card.
    1537                 :            :  *
    1538                 :            :  * We must _NOT_ enable the clock prior to power stablising.
    1539                 :            :  *
    1540                 :            :  * If a host does all the power sequencing itself, ignore the
    1541                 :            :  * initial MMC_POWER_UP stage.
    1542                 :            :  */
    1543                 :          0 : void mmc_power_up(struct mmc_host *host, u32 ocr)
    1544                 :            : {
    1545         [ #  # ]:          0 :         if (host->ios.power_mode == MMC_POWER_ON)
    1546                 :          0 :                 return;
    1547                 :            : 
    1548                 :            :         mmc_host_clk_hold(host);
    1549                 :            : 
    1550                 :          0 :         host->ios.vdd = fls(ocr) - 1;
    1551         [ #  # ]:          0 :         if (mmc_host_is_spi(host))
    1552                 :          0 :                 host->ios.chip_select = MMC_CS_HIGH;
    1553                 :            :         else
    1554                 :          0 :                 host->ios.chip_select = MMC_CS_DONTCARE;
    1555                 :          0 :         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
    1556                 :          0 :         host->ios.power_mode = MMC_POWER_UP;
    1557                 :          0 :         host->ios.bus_width = MMC_BUS_WIDTH_1;
    1558                 :          0 :         host->ios.timing = MMC_TIMING_LEGACY;
    1559                 :            :         mmc_set_ios(host);
    1560                 :            : 
    1561                 :            :         /* Set signal voltage to 3.3V */
    1562                 :            :         __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
    1563                 :            : 
    1564                 :            :         /*
    1565                 :            :          * This delay should be sufficient to allow the power supply
    1566                 :            :          * to reach the minimum voltage.
    1567                 :            :          */
    1568                 :            :         mmc_delay(10);
    1569                 :            : 
    1570                 :          0 :         host->ios.clock = host->f_init;
    1571                 :            : 
    1572                 :          0 :         host->ios.power_mode = MMC_POWER_ON;
    1573                 :            :         mmc_set_ios(host);
    1574                 :            : 
    1575                 :            :         /*
    1576                 :            :          * This delay must be at least 74 clock sizes, or 1 ms, or the
    1577                 :            :          * time required to reach a stable voltage.
    1578                 :            :          */
    1579                 :            :         mmc_delay(10);
    1580                 :            : 
    1581                 :            :         mmc_host_clk_release(host);
    1582                 :            : }
    1583                 :            : 
    1584                 :          0 : void mmc_power_off(struct mmc_host *host)
    1585                 :            : {
    1586         [ -  + ]:      32934 :         if (host->ios.power_mode == MMC_POWER_OFF)
    1587                 :      32934 :                 return;
    1588                 :            : 
    1589                 :            :         mmc_host_clk_hold(host);
    1590                 :            : 
    1591                 :          0 :         host->ios.clock = 0;
    1592                 :          0 :         host->ios.vdd = 0;
    1593                 :            : 
    1594         [ #  # ]:          0 :         if (!mmc_host_is_spi(host)) {
    1595                 :          0 :                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
    1596                 :          0 :                 host->ios.chip_select = MMC_CS_DONTCARE;
    1597                 :            :         }
    1598                 :          0 :         host->ios.power_mode = MMC_POWER_OFF;
    1599                 :          0 :         host->ios.bus_width = MMC_BUS_WIDTH_1;
    1600                 :          0 :         host->ios.timing = MMC_TIMING_LEGACY;
    1601                 :            :         mmc_set_ios(host);
    1602                 :            : 
    1603                 :            :         /*
    1604                 :            :          * Some configurations, such as the 802.11 SDIO card in the OLPC
    1605                 :            :          * XO-1.5, require a short delay after poweroff before the card
    1606                 :            :          * can be successfully turned on again.
    1607                 :            :          */
    1608                 :            :         mmc_delay(1);
    1609                 :            : 
    1610                 :            :         mmc_host_clk_release(host);
    1611                 :            : }
    1612                 :            : 
    1613                 :          0 : void mmc_power_cycle(struct mmc_host *host, u32 ocr)
    1614                 :            : {
    1615                 :          0 :         mmc_power_off(host);
    1616                 :            :         /* Wait at least 1 ms according to SD spec */
    1617                 :            :         mmc_delay(1);
    1618                 :          0 :         mmc_power_up(host, ocr);
    1619                 :          0 : }
    1620                 :            : 
    1621                 :            : /*
    1622                 :            :  * Cleanup when the last reference to the bus operator is dropped.
    1623                 :            :  */
    1624                 :          0 : static void __mmc_release_bus(struct mmc_host *host)
    1625                 :            : {
    1626         [ #  # ]:          0 :         BUG_ON(!host);
    1627         [ #  # ]:          0 :         BUG_ON(host->bus_refs);
    1628         [ #  # ]:          0 :         BUG_ON(!host->bus_dead);
    1629                 :            : 
    1630                 :          0 :         host->bus_ops = NULL;
    1631                 :          0 : }
    1632                 :            : 
    1633                 :            : /*
    1634                 :            :  * Increase reference count of bus operator
    1635                 :            :  */
    1636                 :            : static inline void mmc_bus_get(struct mmc_host *host)
    1637                 :            : {
    1638                 :            :         unsigned long flags;
    1639                 :            : 
    1640                 :      65868 :         spin_lock_irqsave(&host->lock, flags);
    1641                 :      65868 :         host->bus_refs++;
    1642                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1643                 :            : }
    1644                 :            : 
    1645                 :            : /*
    1646                 :            :  * Decrease reference count of bus operator and free it if
    1647                 :            :  * it is the last reference.
    1648                 :            :  */
    1649                 :            : static inline void mmc_bus_put(struct mmc_host *host)
    1650                 :            : {
    1651                 :            :         unsigned long flags;
    1652                 :            : 
    1653                 :      65868 :         spin_lock_irqsave(&host->lock, flags);
    1654                 :      65868 :         host->bus_refs--;
    1655         [ #  # ]:      65868 :         if ((host->bus_refs == 0) && host->bus_ops)
           [ #  #  #  # ]
           [ #  #  #  # ]
           [ #  #  #  # ]
           [ #  #  #  # ]
           [ #  #  #  # ]
           [ #  #  +  - ]
           [ -  +  #  # ]
           [ #  #  +  - ]
           [ -  +  #  # ]
                 [ #  # ]
    1656                 :          0 :                 __mmc_release_bus(host);
    1657                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1658                 :            : }
    1659                 :            : 
    1660                 :            : /*
    1661                 :            :  * Assign a mmc bus handler to a host. Only one bus handler may control a
    1662                 :            :  * host at any given time.
    1663                 :            :  */
    1664                 :          0 : void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
    1665                 :            : {
    1666                 :            :         unsigned long flags;
    1667                 :            : 
    1668         [ #  # ]:          0 :         BUG_ON(!host);
    1669         [ #  # ]:          0 :         BUG_ON(!ops);
    1670                 :            : 
    1671         [ #  # ]:          0 :         WARN_ON(!host->claimed);
    1672                 :            : 
    1673                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1674                 :            : 
    1675         [ #  # ]:          0 :         BUG_ON(host->bus_ops);
    1676         [ #  # ]:          0 :         BUG_ON(host->bus_refs);
    1677                 :            : 
    1678                 :          0 :         host->bus_ops = ops;
    1679                 :          0 :         host->bus_refs = 1;
    1680                 :          0 :         host->bus_dead = 0;
    1681                 :            : 
    1682                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1683                 :          0 : }
    1684                 :            : 
    1685                 :            : /*
    1686                 :            :  * Remove the current bus handler from a host.
    1687                 :            :  */
    1688                 :          0 : void mmc_detach_bus(struct mmc_host *host)
    1689                 :            : {
    1690                 :            :         unsigned long flags;
    1691                 :            : 
    1692         [ #  # ]:          0 :         BUG_ON(!host);
    1693                 :            : 
    1694         [ #  # ]:          0 :         WARN_ON(!host->claimed);
    1695         [ #  # ]:          0 :         WARN_ON(!host->bus_ops);
    1696                 :            : 
    1697                 :          0 :         spin_lock_irqsave(&host->lock, flags);
    1698                 :            : 
    1699                 :          0 :         host->bus_dead = 1;
    1700                 :            : 
    1701                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1702                 :            : 
    1703                 :            :         mmc_bus_put(host);
    1704                 :          0 : }
    1705                 :            : 
    1706                 :          0 : static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
    1707                 :            :                                 bool cd_irq)
    1708                 :            : {
    1709                 :            : #ifdef CONFIG_MMC_DEBUG
    1710                 :            :         unsigned long flags;
    1711                 :            :         spin_lock_irqsave(&host->lock, flags);
    1712                 :            :         WARN_ON(host->removed);
    1713                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    1714                 :            : #endif
    1715                 :            : 
    1716                 :            :         /*
    1717                 :            :          * If the device is configured as wakeup, we prevent a new sleep for
    1718                 :            :          * 5 s to give provision for user space to consume the event.
    1719                 :            :          */
    1720 [ #  # ][ #  # ]:          0 :         if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
                 [ #  # ]
    1721                 :          0 :                 device_can_wakeup(mmc_dev(host)))
    1722                 :          0 :                 pm_wakeup_event(mmc_dev(host), 5000);
    1723                 :            : 
    1724                 :          0 :         host->detect_change = 1;
    1725                 :          0 :         mmc_schedule_delayed_work(&host->detect, delay);
    1726                 :          0 : }
    1727                 :            : 
    1728                 :            : /**
    1729                 :            :  *      mmc_detect_change - process change of state on a MMC socket
    1730                 :            :  *      @host: host which changed state.
    1731                 :            :  *      @delay: optional delay to wait before detection (jiffies)
    1732                 :            :  *
    1733                 :            :  *      MMC drivers should call this when they detect a card has been
    1734                 :            :  *      inserted or removed. The MMC layer will confirm that any
    1735                 :            :  *      present card is still functional, and initialize any newly
    1736                 :            :  *      inserted.
    1737                 :            :  */
    1738                 :          0 : void mmc_detect_change(struct mmc_host *host, unsigned long delay)
    1739                 :            : {
    1740                 :          0 :         _mmc_detect_change(host, delay, true);
    1741                 :          0 : }
    1742                 :            : EXPORT_SYMBOL(mmc_detect_change);
    1743                 :            : 
    1744                 :          0 : void mmc_init_erase(struct mmc_card *card)
    1745                 :            : {
    1746                 :            :         unsigned int sz;
    1747                 :            : 
    1748         [ #  # ]:          0 :         if (is_power_of_2(card->erase_size))
    1749                 :          0 :                 card->erase_shift = ffs(card->erase_size) - 1;
    1750                 :            :         else
    1751                 :          0 :                 card->erase_shift = 0;
    1752                 :            : 
    1753                 :            :         /*
    1754                 :            :          * It is possible to erase an arbitrarily large area of an SD or MMC
    1755                 :            :          * card.  That is not desirable because it can take a long time
    1756                 :            :          * (minutes) potentially delaying more important I/O, and also the
    1757                 :            :          * timeout calculations become increasingly hugely over-estimated.
    1758                 :            :          * Consequently, 'pref_erase' is defined as a guide to limit erases
    1759                 :            :          * to that size and alignment.
    1760                 :            :          *
    1761                 :            :          * For SD cards that define Allocation Unit size, limit erases to one
    1762                 :            :          * Allocation Unit at a time.  For MMC cards that define High Capacity
    1763                 :            :          * Erase Size, whether it is switched on or not, limit to that size.
    1764                 :            :          * Otherwise just have a stab at a good value.  For modern cards it
    1765                 :            :          * will end up being 4MiB.  Note that if the value is too small, it
    1766                 :            :          * can end up taking longer to erase.
    1767                 :            :          */
    1768 [ #  # ][ #  # ]:          0 :         if (mmc_card_sd(card) && card->ssr.au) {
    1769                 :          0 :                 card->pref_erase = card->ssr.au;
    1770                 :          0 :                 card->erase_shift = ffs(card->ssr.au) - 1;
    1771         [ #  # ]:          0 :         } else if (card->ext_csd.hc_erase_size) {
    1772                 :          0 :                 card->pref_erase = card->ext_csd.hc_erase_size;
    1773                 :            :         } else {
    1774                 :          0 :                 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
    1775         [ #  # ]:          0 :                 if (sz < 128)
    1776                 :          0 :                         card->pref_erase = 512 * 1024 / 512;
    1777         [ #  # ]:          0 :                 else if (sz < 512)
    1778                 :          0 :                         card->pref_erase = 1024 * 1024 / 512;
    1779         [ #  # ]:          0 :                 else if (sz < 1024)
    1780                 :          0 :                         card->pref_erase = 2 * 1024 * 1024 / 512;
    1781                 :            :                 else
    1782                 :          0 :                         card->pref_erase = 4 * 1024 * 1024 / 512;
    1783         [ #  # ]:          0 :                 if (card->pref_erase < card->erase_size)
    1784                 :          0 :                         card->pref_erase = card->erase_size;
    1785                 :            :                 else {
    1786                 :          0 :                         sz = card->pref_erase % card->erase_size;
    1787         [ #  # ]:          0 :                         if (sz)
    1788                 :          0 :                                 card->pref_erase += card->erase_size - sz;
    1789                 :            :                 }
    1790                 :            :         }
    1791                 :          0 : }
    1792                 :            : 
    1793                 :          0 : static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
    1794                 :            :                                           unsigned int arg, unsigned int qty)
    1795                 :            : {
    1796                 :            :         unsigned int erase_timeout;
    1797                 :            : 
    1798 [ #  # ][ #  # ]:          0 :         if (arg == MMC_DISCARD_ARG ||
    1799         [ #  # ]:          0 :             (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
    1800                 :          0 :                 erase_timeout = card->ext_csd.trim_timeout;
    1801         [ #  # ]:          0 :         } else if (card->ext_csd.erase_group_def & 1) {
    1802                 :            :                 /* High Capacity Erase Group Size uses HC timeouts */
    1803         [ #  # ]:          0 :                 if (arg == MMC_TRIM_ARG)
    1804                 :          0 :                         erase_timeout = card->ext_csd.trim_timeout;
    1805                 :            :                 else
    1806                 :          0 :                         erase_timeout = card->ext_csd.hc_erase_timeout;
    1807                 :            :         } else {
    1808                 :            :                 /* CSD Erase Group Size uses write timeout */
    1809                 :          0 :                 unsigned int mult = (10 << card->csd.r2w_factor);
    1810                 :          0 :                 unsigned int timeout_clks = card->csd.tacc_clks * mult;
    1811                 :            :                 unsigned int timeout_us;
    1812                 :            : 
    1813                 :            :                 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
    1814         [ #  # ]:          0 :                 if (card->csd.tacc_ns < 1000000)
    1815                 :          0 :                         timeout_us = (card->csd.tacc_ns * mult) / 1000;
    1816                 :            :                 else
    1817                 :          0 :                         timeout_us = (card->csd.tacc_ns / 1000) * mult;
    1818                 :            : 
    1819                 :            :                 /*
    1820                 :            :                  * ios.clock is only a target.  The real clock rate might be
    1821                 :            :                  * less but not that much less, so fudge it by multiplying by 2.
    1822                 :            :                  */
    1823                 :          0 :                 timeout_clks <<= 1;
    1824                 :          0 :                 timeout_us += (timeout_clks * 1000) /
    1825                 :          0 :                               (mmc_host_clk_rate(card->host) / 1000);
    1826                 :            : 
    1827                 :          0 :                 erase_timeout = timeout_us / 1000;
    1828                 :            : 
    1829                 :            :                 /*
    1830                 :            :                  * Theoretically, the calculation could underflow so round up
    1831                 :            :                  * to 1ms in that case.
    1832                 :            :                  */
    1833         [ #  # ]:          0 :                 if (!erase_timeout)
    1834                 :            :                         erase_timeout = 1;
    1835                 :            :         }
    1836                 :            : 
    1837                 :            :         /* Multiplier for secure operations */
    1838         [ #  # ]:          0 :         if (arg & MMC_SECURE_ARGS) {
    1839         [ #  # ]:          0 :                 if (arg == MMC_SECURE_ERASE_ARG)
    1840                 :          0 :                         erase_timeout *= card->ext_csd.sec_erase_mult;
    1841                 :            :                 else
    1842                 :          0 :                         erase_timeout *= card->ext_csd.sec_trim_mult;
    1843                 :            :         }
    1844                 :            : 
    1845                 :          0 :         erase_timeout *= qty;
    1846                 :            : 
    1847                 :            :         /*
    1848                 :            :          * Ensure at least a 1 second timeout for SPI as per
    1849                 :            :          * 'mmc_set_data_timeout()'
    1850                 :            :          */
    1851 [ #  # ][ #  # ]:          0 :         if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
    1852                 :            :                 erase_timeout = 1000;
    1853                 :            : 
    1854                 :          0 :         return erase_timeout;
    1855                 :            : }
    1856                 :            : 
    1857                 :            : static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
    1858                 :            :                                          unsigned int arg,
    1859                 :            :                                          unsigned int qty)
    1860                 :            : {
    1861                 :            :         unsigned int erase_timeout;
    1862                 :            : 
    1863         [ #  # ]:          0 :         if (card->ssr.erase_timeout) {
    1864                 :            :                 /* Erase timeout specified in SD Status Register (SSR) */
    1865                 :          0 :                 erase_timeout = card->ssr.erase_timeout * qty +
    1866                 :          0 :                                 card->ssr.erase_offset;
    1867                 :            :         } else {
    1868                 :            :                 /*
    1869                 :            :                  * Erase timeout not specified in SD Status Register (SSR) so
    1870                 :            :                  * use 250ms per write block.
    1871                 :            :                  */
    1872                 :          0 :                 erase_timeout = 250 * qty;
    1873                 :            :         }
    1874                 :            : 
    1875                 :            :         /* Must not be less than 1 second */
    1876         [ #  # ]:          0 :         if (erase_timeout < 1000)
    1877                 :            :                 erase_timeout = 1000;
    1878                 :            : 
    1879                 :            :         return erase_timeout;
    1880                 :            : }
    1881                 :            : 
    1882                 :          0 : static unsigned int mmc_erase_timeout(struct mmc_card *card,
    1883                 :            :                                       unsigned int arg,
    1884                 :            :                                       unsigned int qty)
    1885                 :            : {
    1886         [ #  # ]:          0 :         if (mmc_card_sd(card))
    1887                 :          0 :                 return mmc_sd_erase_timeout(card, arg, qty);
    1888                 :            :         else
    1889                 :          0 :                 return mmc_mmc_erase_timeout(card, arg, qty);
    1890                 :            : }
    1891                 :            : 
    1892                 :          0 : static int mmc_do_erase(struct mmc_card *card, unsigned int from,
    1893                 :            :                         unsigned int to, unsigned int arg)
    1894                 :            : {
    1895                 :          0 :         struct mmc_command cmd = {0};
    1896                 :            :         unsigned int qty = 0;
    1897                 :            :         unsigned long timeout;
    1898                 :            :         unsigned int fr, nr;
    1899                 :            :         int err;
    1900                 :            : 
    1901                 :            :         fr = from;
    1902                 :          0 :         nr = to - from + 1;
    1903                 :            :         trace_mmc_blk_erase_start(arg, fr, nr);
    1904                 :            : 
    1905                 :            :         /*
    1906                 :            :          * qty is used to calculate the erase timeout which depends on how many
    1907                 :            :          * erase groups (or allocation units in SD terminology) are affected.
    1908                 :            :          * We count erasing part of an erase group as one erase group.
    1909                 :            :          * For SD, the allocation units are always a power of 2.  For MMC, the
    1910                 :            :          * erase group size is almost certainly also power of 2, but it does not
    1911                 :            :          * seem to insist on that in the JEDEC standard, so we fall back to
    1912                 :            :          * division in that case.  SD may not specify an allocation unit size,
    1913                 :            :          * in which case the timeout is based on the number of write blocks.
    1914                 :            :          *
    1915                 :            :          * Note that the timeout for secure trim 2 will only be correct if the
    1916                 :            :          * number of erase groups specified is the same as the total of all
    1917                 :            :          * preceding secure trim 1 commands.  Since the power may have been
    1918                 :            :          * lost since the secure trim 1 commands occurred, it is generally
    1919                 :            :          * impossible to calculate the secure trim 2 timeout correctly.
    1920                 :            :          */
    1921         [ #  # ]:          0 :         if (card->erase_shift)
    1922                 :          0 :                 qty += ((to >> card->erase_shift) -
    1923                 :          0 :                         (from >> card->erase_shift)) + 1;
    1924         [ #  # ]:          0 :         else if (mmc_card_sd(card))
    1925                 :            :                 qty += to - from + 1;
    1926                 :            :         else
    1927                 :          0 :                 qty += ((to / card->erase_size) -
    1928                 :          0 :                         (from / card->erase_size)) + 1;
    1929                 :            : 
    1930         [ #  # ]:          0 :         if (!mmc_card_blockaddr(card)) {
    1931                 :          0 :                 from <<= 9;
    1932                 :          0 :                 to <<= 9;
    1933                 :            :         }
    1934                 :            : 
    1935         [ #  # ]:          0 :         if (mmc_card_sd(card))
    1936                 :          0 :                 cmd.opcode = SD_ERASE_WR_BLK_START;
    1937                 :            :         else
    1938                 :          0 :                 cmd.opcode = MMC_ERASE_GROUP_START;
    1939                 :          0 :         cmd.arg = from;
    1940                 :          0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
    1941                 :          0 :         err = mmc_wait_for_cmd(card->host, &cmd, 0);
    1942         [ #  # ]:          0 :         if (err) {
    1943                 :          0 :                 pr_err("mmc_erase: group start error %d, "
    1944                 :            :                        "status %#x\n", err, cmd.resp[0]);
    1945                 :            :                 err = -EIO;
    1946                 :          0 :                 goto out;
    1947                 :            :         }
    1948                 :            : 
    1949                 :          0 :         memset(&cmd, 0, sizeof(struct mmc_command));
    1950         [ #  # ]:          0 :         if (mmc_card_sd(card))
    1951                 :          0 :                 cmd.opcode = SD_ERASE_WR_BLK_END;
    1952                 :            :         else
    1953                 :          0 :                 cmd.opcode = MMC_ERASE_GROUP_END;
    1954                 :          0 :         cmd.arg = to;
    1955                 :          0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
    1956                 :          0 :         err = mmc_wait_for_cmd(card->host, &cmd, 0);
    1957         [ #  # ]:          0 :         if (err) {
    1958                 :          0 :                 pr_err("mmc_erase: group end error %d, status %#x\n",
    1959                 :            :                        err, cmd.resp[0]);
    1960                 :            :                 err = -EIO;
    1961                 :          0 :                 goto out;
    1962                 :            :         }
    1963                 :            : 
    1964                 :          0 :         memset(&cmd, 0, sizeof(struct mmc_command));
    1965                 :          0 :         cmd.opcode = MMC_ERASE;
    1966                 :          0 :         cmd.arg = arg;
    1967                 :          0 :         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
    1968                 :          0 :         cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
    1969                 :          0 :         err = mmc_wait_for_cmd(card->host, &cmd, 0);
    1970         [ #  # ]:          0 :         if (err) {
    1971                 :          0 :                 pr_err("mmc_erase: erase error %d, status %#x\n",
    1972                 :            :                        err, cmd.resp[0]);
    1973                 :            :                 err = -EIO;
    1974                 :          0 :                 goto out;
    1975                 :            :         }
    1976                 :            : 
    1977         [ #  # ]:          0 :         if (mmc_host_is_spi(card->host))
    1978                 :            :                 goto out;
    1979                 :            : 
    1980                 :          0 :         timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
    1981                 :            :         do {
    1982                 :          0 :                 memset(&cmd, 0, sizeof(struct mmc_command));
    1983                 :          0 :                 cmd.opcode = MMC_SEND_STATUS;
    1984                 :          0 :                 cmd.arg = card->rca << 16;
    1985                 :          0 :                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
    1986                 :            :                 /* Do not retry else we can't see errors */
    1987                 :          0 :                 err = mmc_wait_for_cmd(card->host, &cmd, 0);
    1988 [ #  # ][ #  # ]:          0 :                 if (err || (cmd.resp[0] & 0xFDF92000)) {
    1989                 :          0 :                         pr_err("error %d requesting status %#x\n",
    1990                 :            :                                 err, cmd.resp[0]);
    1991                 :            :                         err = -EIO;
    1992                 :          0 :                         goto out;
    1993                 :            :                 }
    1994                 :            : 
    1995                 :            :                 /* Timeout if the device never becomes ready for data and
    1996                 :            :                  * never leaves the program state.
    1997                 :            :                  */
    1998         [ #  # ]:          0 :                 if (time_after(jiffies, timeout)) {
    1999                 :          0 :                         pr_err("%s: Card stuck in programming state! %s\n",
    2000                 :            :                                 mmc_hostname(card->host), __func__);
    2001                 :            :                         err =  -EIO;
    2002                 :          0 :                         goto out;
    2003                 :            :                 }
    2004                 :            : 
    2005         [ #  # ]:          0 :         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
    2006         [ #  # ]:          0 :                  (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
    2007                 :            : out:
    2008                 :            : 
    2009                 :            :         trace_mmc_blk_erase_end(arg, fr, nr);
    2010                 :          0 :         return err;
    2011                 :            : }
    2012                 :            : 
    2013                 :            : /**
    2014                 :            :  * mmc_erase - erase sectors.
    2015                 :            :  * @card: card to erase
    2016                 :            :  * @from: first sector to erase
    2017                 :            :  * @nr: number of sectors to erase
    2018                 :            :  * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
    2019                 :            :  *
    2020                 :            :  * Caller must claim host before calling this function.
    2021                 :            :  */
    2022                 :          0 : int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
    2023                 :            :               unsigned int arg)
    2024                 :            : {
    2025                 :            :         unsigned int rem, to = from + nr;
    2026                 :            : 
    2027 [ #  # ][ #  # ]:          0 :         if (!(card->host->caps & MMC_CAP_ERASE) ||
    2028                 :          0 :             !(card->csd.cmdclass & CCC_ERASE))
    2029                 :            :                 return -EOPNOTSUPP;
    2030                 :            : 
    2031         [ #  # ]:          0 :         if (!card->erase_size)
    2032                 :            :                 return -EOPNOTSUPP;
    2033                 :            : 
    2034 [ #  # ][ #  # ]:          0 :         if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
    2035                 :            :                 return -EOPNOTSUPP;
    2036                 :            : 
    2037 [ #  # ][ #  # ]:          0 :         if ((arg & MMC_SECURE_ARGS) &&
    2038                 :          0 :             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
    2039                 :            :                 return -EOPNOTSUPP;
    2040                 :            : 
    2041 [ #  # ][ #  # ]:          0 :         if ((arg & MMC_TRIM_ARGS) &&
    2042                 :          0 :             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
    2043                 :            :                 return -EOPNOTSUPP;
    2044                 :            : 
    2045         [ #  # ]:          0 :         if (arg == MMC_SECURE_ERASE_ARG) {
    2046 [ #  # ][ #  # ]:          0 :                 if (from % card->erase_size || nr % card->erase_size)
    2047                 :            :                         return -EINVAL;
    2048                 :            :         }
    2049                 :            : 
    2050         [ #  # ]:          0 :         if (arg == MMC_ERASE_ARG) {
    2051                 :          0 :                 rem = from % card->erase_size;
    2052         [ #  # ]:          0 :                 if (rem) {
    2053                 :          0 :                         rem = card->erase_size - rem;
    2054                 :          0 :                         from += rem;
    2055         [ #  # ]:          0 :                         if (nr > rem)
    2056                 :          0 :                                 nr -= rem;
    2057                 :            :                         else
    2058                 :            :                                 return 0;
    2059                 :            :                 }
    2060                 :          0 :                 rem = nr % card->erase_size;
    2061         [ #  # ]:          0 :                 if (rem)
    2062                 :          0 :                         nr -= rem;
    2063                 :            :         }
    2064                 :            : 
    2065         [ #  # ]:          0 :         if (nr == 0)
    2066                 :            :                 return 0;
    2067                 :            : 
    2068                 :          0 :         to = from + nr;
    2069                 :            : 
    2070         [ #  # ]:          0 :         if (to <= from)
    2071                 :            :                 return -EINVAL;
    2072                 :            : 
    2073                 :            :         /* 'from' and 'to' are inclusive */
    2074                 :          0 :         to -= 1;
    2075                 :            : 
    2076                 :          0 :         return mmc_do_erase(card, from, to, arg);
    2077                 :            : }
    2078                 :            : EXPORT_SYMBOL(mmc_erase);
    2079                 :            : 
    2080                 :          0 : int mmc_can_erase(struct mmc_card *card)
    2081                 :            : {
    2082 [ #  # ][ #  # ]:          0 :         if ((card->host->caps & MMC_CAP_ERASE) &&
         [ #  # ][ #  # ]
    2083 [ #  # ][ #  # ]:          0 :             (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
    2084                 :            :                 return 1;
    2085                 :          0 :         return 0;
    2086                 :            : }
    2087                 :            : EXPORT_SYMBOL(mmc_can_erase);
    2088                 :            : 
    2089                 :          0 : int mmc_can_trim(struct mmc_card *card)
    2090                 :            : {
    2091 [ #  # ][ #  # ]:          0 :         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
                 [ #  # ]
    2092                 :            :                 return 1;
    2093                 :          0 :         return 0;
    2094                 :            : }
    2095                 :            : EXPORT_SYMBOL(mmc_can_trim);
    2096                 :            : 
    2097                 :          0 : int mmc_can_discard(struct mmc_card *card)
    2098                 :            : {
    2099                 :            :         /*
    2100                 :            :          * As there's no way to detect the discard support bit at v4.5
    2101                 :            :          * use the s/w feature support filed.
    2102                 :            :          */
    2103         [ #  # ]:          0 :         if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
    2104                 :            :                 return 1;
    2105                 :          0 :         return 0;
    2106                 :            : }
    2107                 :            : EXPORT_SYMBOL(mmc_can_discard);
    2108                 :            : 
    2109                 :          0 : int mmc_can_sanitize(struct mmc_card *card)
    2110                 :            : {
    2111 [ #  # ][ #  # ]:          0 :         if (!mmc_can_trim(card) && !mmc_can_erase(card))
    2112                 :            :                 return 0;
    2113         [ #  # ]:          0 :         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
    2114                 :            :                 return 1;
    2115                 :          0 :         return 0;
    2116                 :            : }
    2117                 :            : EXPORT_SYMBOL(mmc_can_sanitize);
    2118                 :            : 
    2119                 :          0 : int mmc_can_secure_erase_trim(struct mmc_card *card)
    2120                 :            : {
    2121         [ #  # ]:          0 :         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
    2122                 :            :                 return 1;
    2123                 :          0 :         return 0;
    2124                 :            : }
    2125                 :            : EXPORT_SYMBOL(mmc_can_secure_erase_trim);
    2126                 :            : 
    2127                 :          0 : int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
    2128                 :            :                             unsigned int nr)
    2129                 :            : {
    2130         [ #  # ]:          0 :         if (!card->erase_size)
    2131                 :            :                 return 0;
    2132 [ #  # ][ #  # ]:          0 :         if (from % card->erase_size || nr % card->erase_size)
    2133                 :            :                 return 0;
    2134                 :          0 :         return 1;
    2135                 :            : }
    2136                 :            : EXPORT_SYMBOL(mmc_erase_group_aligned);
    2137                 :            : 
    2138                 :          0 : static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
    2139                 :            :                                             unsigned int arg)
    2140                 :            : {
    2141                 :          0 :         struct mmc_host *host = card->host;
    2142                 :            :         unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
    2143                 :            :         unsigned int last_timeout = 0;
    2144                 :            : 
    2145         [ #  # ]:          0 :         if (card->erase_shift)
    2146                 :          0 :                 max_qty = UINT_MAX >> card->erase_shift;
    2147         [ #  # ]:          0 :         else if (mmc_card_sd(card))
    2148                 :            :                 max_qty = UINT_MAX;
    2149                 :            :         else
    2150                 :          0 :                 max_qty = UINT_MAX / card->erase_size;
    2151                 :            : 
    2152                 :            :         /* Find the largest qty with an OK timeout */
    2153                 :            :         do {
    2154                 :            :                 y = 0;
    2155 [ #  # ][ #  # ]:          0 :                 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
    2156                 :          0 :                         timeout = mmc_erase_timeout(card, arg, qty + x);
    2157         [ #  # ]:          0 :                         if (timeout > host->max_discard_to)
    2158                 :            :                                 break;
    2159         [ #  # ]:          0 :                         if (timeout < last_timeout)
    2160                 :            :                                 break;
    2161                 :            :                         last_timeout = timeout;
    2162                 :            :                         y = x;
    2163                 :            :                 }
    2164                 :          0 :                 qty += y;
    2165         [ #  # ]:          0 :         } while (y);
    2166                 :            : 
    2167         [ #  # ]:          0 :         if (!qty)
    2168                 :            :                 return 0;
    2169                 :            : 
    2170         [ #  # ]:          0 :         if (qty == 1)
    2171                 :            :                 return 1;
    2172                 :            : 
    2173                 :            :         /* Convert qty to sectors */
    2174         [ #  # ]:          0 :         if (card->erase_shift)
    2175                 :          0 :                 max_discard = --qty << card->erase_shift;
    2176         [ #  # ]:          0 :         else if (mmc_card_sd(card))
    2177                 :            :                 max_discard = qty;
    2178                 :            :         else
    2179                 :          0 :                 max_discard = --qty * card->erase_size;
    2180                 :            : 
    2181                 :          0 :         return max_discard;
    2182                 :            : }
    2183                 :            : 
    2184                 :          0 : unsigned int mmc_calc_max_discard(struct mmc_card *card)
    2185                 :            : {
    2186                 :          0 :         struct mmc_host *host = card->host;
    2187                 :            :         unsigned int max_discard, max_trim;
    2188                 :            : 
    2189         [ #  # ]:          0 :         if (!host->max_discard_to)
    2190                 :            :                 return UINT_MAX;
    2191                 :            : 
    2192                 :            :         /*
    2193                 :            :          * Without erase_group_def set, MMC erase timeout depends on clock
    2194                 :            :          * frequence which can change.  In that case, the best choice is
    2195                 :            :          * just the preferred erase size.
    2196                 :            :          */
    2197 [ #  # ][ #  # ]:          0 :         if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
    2198                 :          0 :                 return card->pref_erase;
    2199                 :            : 
    2200                 :          0 :         max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
    2201         [ #  # ]:          0 :         if (mmc_can_trim(card)) {
    2202                 :          0 :                 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
    2203         [ #  # ]:          0 :                 if (max_trim < max_discard)
    2204                 :            :                         max_discard = max_trim;
    2205         [ #  # ]:          0 :         } else if (max_discard < card->erase_size) {
    2206                 :            :                 max_discard = 0;
    2207                 :            :         }
    2208                 :            :         pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
    2209                 :            :                  mmc_hostname(host), max_discard, host->max_discard_to);
    2210                 :          0 :         return max_discard;
    2211                 :            : }
    2212                 :            : EXPORT_SYMBOL(mmc_calc_max_discard);
    2213                 :            : 
    2214                 :          0 : int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
    2215                 :            : {
    2216                 :          0 :         struct mmc_command cmd = {0};
    2217                 :            : 
    2218         [ #  # ]:          0 :         if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
    2219                 :            :                 return 0;
    2220                 :            : 
    2221                 :          0 :         cmd.opcode = MMC_SET_BLOCKLEN;
    2222                 :          0 :         cmd.arg = blocklen;
    2223                 :          0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
    2224                 :          0 :         return mmc_wait_for_cmd(card->host, &cmd, 5);
    2225                 :            : }
    2226                 :            : EXPORT_SYMBOL(mmc_set_blocklen);
    2227                 :            : 
    2228                 :          0 : int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
    2229                 :            :                         bool is_rel_write)
    2230                 :            : {
    2231                 :          0 :         struct mmc_command cmd = {0};
    2232                 :            : 
    2233                 :          0 :         cmd.opcode = MMC_SET_BLOCK_COUNT;
    2234                 :          0 :         cmd.arg = blockcount & 0x0000FFFF;
    2235         [ #  # ]:          0 :         if (is_rel_write)
    2236                 :          0 :                 cmd.arg |= 1 << 31;
    2237                 :          0 :         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
    2238                 :          0 :         return mmc_wait_for_cmd(card->host, &cmd, 5);
    2239                 :            : }
    2240                 :            : EXPORT_SYMBOL(mmc_set_blockcount);
    2241                 :            : 
    2242                 :            : static void mmc_hw_reset_for_init(struct mmc_host *host)
    2243                 :            : {
    2244 [ #  # ][ #  # ]:          0 :         if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
    2245                 :            :                 return;
    2246                 :            :         mmc_host_clk_hold(host);
    2247                 :          0 :         host->ops->hw_reset(host);
    2248                 :            :         mmc_host_clk_release(host);
    2249                 :            : }
    2250                 :            : 
    2251                 :          0 : int mmc_can_reset(struct mmc_card *card)
    2252                 :            : {
    2253                 :            :         u8 rst_n_function;
    2254                 :            : 
    2255 [ #  # ][ #  # ]:          0 :         if (!mmc_card_mmc(card))
    2256                 :            :                 return 0;
    2257                 :          0 :         rst_n_function = card->ext_csd.rst_n_function;
    2258 [ #  # ][ #  # ]:          0 :         if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
    2259                 :            :                 return 0;
    2260                 :          0 :         return 1;
    2261                 :            : }
    2262                 :            : EXPORT_SYMBOL(mmc_can_reset);
    2263                 :            : 
    2264                 :          0 : static int mmc_do_hw_reset(struct mmc_host *host, int check)
    2265                 :            : {
    2266                 :          0 :         struct mmc_card *card = host->card;
    2267                 :            : 
    2268         [ #  # ]:          0 :         if (!host->bus_ops->power_restore)
    2269                 :            :                 return -EOPNOTSUPP;
    2270                 :            : 
    2271 [ #  # ][ #  # ]:          0 :         if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
    2272                 :            :                 return -EOPNOTSUPP;
    2273                 :            : 
    2274         [ #  # ]:          0 :         if (!card)
    2275                 :            :                 return -EINVAL;
    2276                 :            : 
    2277         [ #  # ]:          0 :         if (!mmc_can_reset(card))
    2278                 :            :                 return -EOPNOTSUPP;
    2279                 :            : 
    2280                 :            :         mmc_host_clk_hold(host);
    2281                 :          0 :         mmc_set_clock(host, host->f_init);
    2282                 :            : 
    2283                 :          0 :         host->ops->hw_reset(host);
    2284                 :            : 
    2285                 :            :         /* If the reset has happened, then a status command will fail */
    2286         [ #  # ]:          0 :         if (check) {
    2287                 :          0 :                 struct mmc_command cmd = {0};
    2288                 :            :                 int err;
    2289                 :            : 
    2290                 :          0 :                 cmd.opcode = MMC_SEND_STATUS;
    2291         [ #  # ]:          0 :                 if (!mmc_host_is_spi(card->host))
    2292                 :          0 :                         cmd.arg = card->rca << 16;
    2293                 :          0 :                 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
    2294                 :          0 :                 err = mmc_wait_for_cmd(card->host, &cmd, 0);
    2295         [ #  # ]:          0 :                 if (!err) {
    2296                 :            :                         mmc_host_clk_release(host);
    2297                 :          0 :                         return -ENOSYS;
    2298                 :            :                 }
    2299                 :            :         }
    2300                 :            : 
    2301                 :          0 :         host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
    2302         [ #  # ]:          0 :         if (mmc_host_is_spi(host)) {
    2303                 :          0 :                 host->ios.chip_select = MMC_CS_HIGH;
    2304                 :          0 :                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
    2305                 :            :         } else {
    2306                 :          0 :                 host->ios.chip_select = MMC_CS_DONTCARE;
    2307                 :          0 :                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
    2308                 :            :         }
    2309                 :          0 :         host->ios.bus_width = MMC_BUS_WIDTH_1;
    2310                 :          0 :         host->ios.timing = MMC_TIMING_LEGACY;
    2311                 :            :         mmc_set_ios(host);
    2312                 :            : 
    2313                 :            :         mmc_host_clk_release(host);
    2314                 :            : 
    2315                 :          0 :         return host->bus_ops->power_restore(host);
    2316                 :            : }
    2317                 :            : 
    2318                 :          0 : int mmc_hw_reset(struct mmc_host *host)
    2319                 :            : {
    2320                 :          0 :         return mmc_do_hw_reset(host, 0);
    2321                 :            : }
    2322                 :            : EXPORT_SYMBOL(mmc_hw_reset);
    2323                 :            : 
    2324                 :          0 : int mmc_hw_reset_check(struct mmc_host *host)
    2325                 :            : {
    2326                 :          0 :         return mmc_do_hw_reset(host, 1);
    2327                 :            : }
    2328                 :            : EXPORT_SYMBOL(mmc_hw_reset_check);
    2329                 :            : 
    2330                 :          0 : static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
    2331                 :            : {
    2332                 :          0 :         host->f_init = freq;
    2333                 :            : 
    2334                 :            : #ifdef CONFIG_MMC_DEBUG
    2335                 :            :         pr_info("%s: %s: trying to init card at %u Hz\n",
    2336                 :            :                 mmc_hostname(host), __func__, host->f_init);
    2337                 :            : #endif
    2338                 :          0 :         mmc_power_up(host, host->ocr_avail);
    2339                 :            : 
    2340                 :            :         /*
    2341                 :            :          * Some eMMCs (with VCCQ always on) may not be reset after power up, so
    2342                 :            :          * do a hardware reset if possible.
    2343                 :            :          */
    2344                 :            :         mmc_hw_reset_for_init(host);
    2345                 :            : 
    2346                 :            :         /*
    2347                 :            :          * sdio_reset sends CMD52 to reset card.  Since we do not know
    2348                 :            :          * if the card is being re-initialized, just send it.  CMD52
    2349                 :            :          * should be ignored by SD/eMMC cards.
    2350                 :            :          */
    2351                 :          0 :         sdio_reset(host);
    2352                 :          0 :         mmc_go_idle(host);
    2353                 :            : 
    2354                 :          0 :         mmc_send_if_cond(host, host->ocr_avail);
    2355                 :            : 
    2356                 :            :         /* Order's important: probe SDIO, then SD, then MMC */
    2357         [ #  # ]:          0 :         if (!mmc_attach_sdio(host))
    2358                 :            :                 return 0;
    2359         [ #  # ]:          0 :         if (!mmc_attach_sd(host))
    2360                 :            :                 return 0;
    2361         [ #  # ]:          0 :         if (!mmc_attach_mmc(host))
    2362                 :            :                 return 0;
    2363                 :            : 
    2364                 :          0 :         mmc_power_off(host);
    2365                 :          0 :         return -EIO;
    2366                 :            : }
    2367                 :            : 
    2368                 :          0 : int _mmc_detect_card_removed(struct mmc_host *host)
    2369                 :            : {
    2370                 :            :         int ret;
    2371                 :            : 
    2372 [ #  # ][ #  # ]:          0 :         if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
    2373                 :            :                 return 0;
    2374                 :            : 
    2375 [ #  # ][ #  # ]:          0 :         if (!host->card || mmc_card_removed(host->card))
    2376                 :            :                 return 1;
    2377                 :            : 
    2378                 :          0 :         ret = host->bus_ops->alive(host);
    2379                 :            : 
    2380                 :            :         /*
    2381                 :            :          * Card detect status and alive check may be out of sync if card is
    2382                 :            :          * removed slowly, when card detect switch changes while card/slot
    2383                 :            :          * pads are still contacted in hardware (refer to "SD Card Mechanical
    2384                 :            :          * Addendum, Appendix C: Card Detection Switch"). So reschedule a
    2385                 :            :          * detect work 200ms later for this case.
    2386                 :            :          */
    2387 [ #  # ][ #  # ]:          0 :         if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
                 [ #  # ]
    2388                 :          0 :                 mmc_detect_change(host, msecs_to_jiffies(200));
    2389                 :            :                 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
    2390                 :            :         }
    2391                 :            : 
    2392         [ #  # ]:          0 :         if (ret) {
    2393                 :          0 :                 mmc_card_set_removed(host->card);
    2394                 :            :                 pr_debug("%s: card remove detected\n", mmc_hostname(host));
    2395                 :            :         }
    2396                 :            : 
    2397                 :          0 :         return ret;
    2398                 :            : }
    2399                 :            : 
    2400                 :          0 : int mmc_detect_card_removed(struct mmc_host *host)
    2401                 :            : {
    2402                 :          0 :         struct mmc_card *card = host->card;
    2403                 :            :         int ret;
    2404                 :            : 
    2405         [ #  # ]:          0 :         WARN_ON(!host->claimed);
    2406                 :            : 
    2407         [ #  # ]:          0 :         if (!card)
    2408                 :            :                 return 1;
    2409                 :            : 
    2410 [ #  # ][ #  # ]:          0 :         ret = mmc_card_removed(card);
    2411                 :            :         /*
    2412                 :            :          * The card will be considered unchanged unless we have been asked to
    2413                 :            :          * detect a change or host requires polling to provide card detection.
    2414                 :            :          */
    2415 [ #  # ][ #  # ]:          0 :         if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
    2416                 :            :                 return ret;
    2417                 :            : 
    2418                 :          0 :         host->detect_change = 0;
    2419         [ #  # ]:          0 :         if (!ret) {
    2420                 :          0 :                 ret = _mmc_detect_card_removed(host);
    2421 [ #  # ][ #  # ]:          0 :                 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
    2422                 :            :                         /*
    2423                 :            :                          * Schedule a detect work as soon as possible to let a
    2424                 :            :                          * rescan handle the card removal.
    2425                 :            :                          */
    2426                 :          0 :                         cancel_delayed_work(&host->detect);
    2427                 :          0 :                         _mmc_detect_change(host, 0, false);
    2428                 :            :                 }
    2429                 :            :         }
    2430                 :            : 
    2431                 :          0 :         return ret;
    2432                 :            : }
    2433                 :            : EXPORT_SYMBOL(mmc_detect_card_removed);
    2434                 :            : 
    2435                 :          0 : void mmc_rescan(struct work_struct *work)
    2436                 :            : {
    2437                 :      32934 :         struct mmc_host *host =
    2438                 :            :                 container_of(work, struct mmc_host, detect.work);
    2439                 :            :         int i;
    2440                 :            :         bool extend_wakelock = false;
    2441                 :            : 
    2442         [ +  - ]:      32934 :         if (host->rescan_disable)
    2443                 :            :                 return;
    2444                 :            : 
    2445                 :            :         /* If there is a non-removable card registered, only scan once */
    2446 [ -  + ][ #  # ]:      32934 :         if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
    2447                 :            :                 return;
    2448                 :      32934 :         host->rescan_entered = 1;
    2449                 :            : 
    2450                 :            :         mmc_bus_get(host);
    2451                 :            : 
    2452                 :            :         /*
    2453                 :            :          * if there is a _removable_ card registered, check whether it is
    2454                 :            :          * still present
    2455                 :            :          */
    2456 [ -  + ][ #  # ]:      32934 :         if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
                 [ #  # ]
    2457         [ #  # ]:          0 :             && !(host->caps & MMC_CAP_NONREMOVABLE))
    2458                 :          0 :                 host->bus_ops->detect(host);
    2459                 :            : 
    2460                 :      32934 :         host->detect_change = 0;
    2461                 :            : 
    2462                 :            :         /* If the card was removed the bus will be marked
    2463                 :            :          * as dead - extend the wakelock so userspace
    2464                 :            :          * can respond */
    2465         [ -  + ]:      32934 :         if (host->bus_dead)
    2466                 :            :                 extend_wakelock = 1;
    2467                 :            : 
    2468                 :            :         /*
    2469                 :            :          * Let mmc_bus_put() free the bus/bus_ops if we've found that
    2470                 :            :          * the card is no longer present.
    2471                 :            :          */
    2472                 :            :         mmc_bus_put(host);
    2473                 :            :         mmc_bus_get(host);
    2474                 :            : 
    2475                 :            :         /* if there still is a card present, stop here */
    2476         [ -  + ]:      32934 :         if (host->bus_ops != NULL) {
    2477                 :            :                 mmc_bus_put(host);
    2478                 :            :                 goto out;
    2479                 :            :         }
    2480                 :            : 
    2481                 :            :         /*
    2482                 :            :          * Only we can add a new handler, so it's safe to
    2483                 :            :          * release the lock here.
    2484                 :            :          */
    2485                 :            :         mmc_bus_put(host);
    2486                 :            : 
    2487         [ +  - ]:      65868 :         if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
           [ +  -  +  - ]
    2488                 :      32934 :                         host->ops->get_cd(host) == 0) {
    2489                 :            :                 mmc_claim_host(host);
    2490                 :      32934 :                 mmc_power_off(host);
    2491                 :      32934 :                 mmc_release_host(host);
    2492                 :      32934 :                 goto out;
    2493                 :            :         }
    2494                 :            : 
    2495                 :            :         mmc_claim_host(host);
    2496         [ #  # ]:          0 :         for (i = 0; i < ARRAY_SIZE(freqs); i++) {
    2497         [ #  # ]:          0 :                 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) {
    2498                 :            :                         extend_wakelock = true;
    2499                 :            :                         break;
    2500                 :            :                 }
    2501         [ #  # ]:          0 :                 if (freqs[i] <= host->f_min)
    2502                 :            :                         break;
    2503                 :            :         }
    2504                 :          0 :         mmc_release_host(host);
    2505                 :            : 
    2506                 :            :  out:
    2507         [ -  + ]:      32934 :         if (extend_wakelock)
    2508                 :            :                 wake_lock_timeout(&mmc_delayed_work_wake_lock, HZ / 2);
    2509                 :            :         else
    2510                 :            :                 wake_unlock(&mmc_delayed_work_wake_lock);
    2511         [ +  - ]:      32934 :         if (host->caps & MMC_CAP_NEEDS_POLL)
    2512                 :      32934 :                 mmc_schedule_delayed_work(&host->detect, HZ);
    2513                 :            : }
    2514                 :            : 
    2515                 :          0 : void mmc_start_host(struct mmc_host *host)
    2516                 :            : {
    2517                 :          0 :         host->f_init = max(freqs[0], host->f_min);
    2518                 :          0 :         host->rescan_disable = 0;
    2519         [ #  # ]:          0 :         if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
    2520                 :          0 :                 mmc_power_off(host);
    2521                 :            :         else
    2522                 :          0 :                 mmc_power_up(host, host->ocr_avail);
    2523                 :          0 :         _mmc_detect_change(host, 0, false);
    2524                 :          0 : }
    2525                 :            : 
    2526                 :          0 : void mmc_stop_host(struct mmc_host *host)
    2527                 :            : {
    2528                 :            : #ifdef CONFIG_MMC_DEBUG
    2529                 :            :         unsigned long flags;
    2530                 :            :         spin_lock_irqsave(&host->lock, flags);
    2531                 :            :         host->removed = 1;
    2532                 :            :         spin_unlock_irqrestore(&host->lock, flags);
    2533                 :            : #endif
    2534                 :            : 
    2535                 :          0 :         host->rescan_disable = 1;
    2536                 :          0 :         cancel_delayed_work_sync(&host->detect);
    2537                 :            :         mmc_flush_scheduled_work();
    2538                 :            : 
    2539                 :            :         /* clear pm flags now and let card drivers set them as needed */
    2540                 :          0 :         host->pm_flags = 0;
    2541                 :            : 
    2542                 :            :         mmc_bus_get(host);
    2543 [ #  # ][ #  # ]:          0 :         if (host->bus_ops && !host->bus_dead) {
    2544                 :            :                 /* Calling bus_ops->remove() with a claimed host can deadlock */
    2545                 :          0 :                 host->bus_ops->remove(host);
    2546                 :            :                 mmc_claim_host(host);
    2547                 :          0 :                 mmc_detach_bus(host);
    2548                 :          0 :                 mmc_power_off(host);
    2549                 :          0 :                 mmc_release_host(host);
    2550                 :            :                 mmc_bus_put(host);
    2551                 :          0 :                 return;
    2552                 :            :         }
    2553                 :            :         mmc_bus_put(host);
    2554                 :            : 
    2555         [ #  # ]:          0 :         BUG_ON(host->card);
    2556                 :            : 
    2557                 :          0 :         mmc_power_off(host);
    2558                 :            : }
    2559                 :            : 
    2560                 :          0 : int mmc_power_save_host(struct mmc_host *host)
    2561                 :            : {
    2562                 :            :         int ret = 0;
    2563                 :            : 
    2564                 :            : #ifdef CONFIG_MMC_DEBUG
    2565                 :            :         pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
    2566                 :            : #endif
    2567                 :            : 
    2568                 :            :         mmc_bus_get(host);
    2569                 :            : 
    2570 [ #  # ][ #  # ]:          0 :         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
                 [ #  # ]
    2571                 :            :                 mmc_bus_put(host);
    2572                 :          0 :                 return -EINVAL;
    2573                 :            :         }
    2574                 :            : 
    2575         [ #  # ]:          0 :         if (host->bus_ops->power_save)
    2576                 :          0 :                 ret = host->bus_ops->power_save(host);
    2577                 :            : 
    2578                 :            :         mmc_bus_put(host);
    2579                 :            : 
    2580                 :          0 :         mmc_power_off(host);
    2581                 :            : 
    2582                 :          0 :         return ret;
    2583                 :            : }
    2584                 :            : EXPORT_SYMBOL(mmc_power_save_host);
    2585                 :            : 
    2586                 :          0 : int mmc_power_restore_host(struct mmc_host *host)
    2587                 :            : {
    2588                 :            :         int ret;
    2589                 :            : 
    2590                 :            : #ifdef CONFIG_MMC_DEBUG
    2591                 :            :         pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
    2592                 :            : #endif
    2593                 :            : 
    2594                 :            :         mmc_bus_get(host);
    2595                 :            : 
    2596 [ #  # ][ #  # ]:          0 :         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
                 [ #  # ]
    2597                 :            :                 mmc_bus_put(host);
    2598                 :          0 :                 return -EINVAL;
    2599                 :            :         }
    2600                 :            : 
    2601                 :          0 :         mmc_power_up(host, host->card->ocr);
    2602                 :          0 :         ret = host->bus_ops->power_restore(host);
    2603                 :            : 
    2604                 :            :         mmc_bus_put(host);
    2605                 :            : 
    2606                 :          0 :         return ret;
    2607                 :            : }
    2608                 :            : EXPORT_SYMBOL(mmc_power_restore_host);
    2609                 :            : 
    2610                 :            : /*
    2611                 :            :  * Flush the cache to the non-volatile storage.
    2612                 :            :  */
    2613                 :          0 : int mmc_flush_cache(struct mmc_card *card)
    2614                 :            : {
    2615                 :          0 :         struct mmc_host *host = card->host;
    2616                 :            :         int err = 0;
    2617                 :            : 
    2618         [ #  # ]:          0 :         if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
    2619                 :            :                 return err;
    2620                 :            : 
    2621 [ #  # ][ #  # ]:          0 :         if (mmc_card_mmc(card) &&
    2622         [ #  # ]:          0 :                         (card->ext_csd.cache_size > 0) &&
    2623                 :          0 :                         (card->ext_csd.cache_ctrl & 1)) {
    2624                 :          0 :                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
    2625                 :            :                                 EXT_CSD_FLUSH_CACHE, 1, 0);
    2626         [ #  # ]:          0 :                 if (err)
    2627                 :          0 :                         pr_err("%s: cache flush error %d\n",
    2628                 :            :                                         mmc_hostname(card->host), err);
    2629                 :            :         }
    2630                 :            : 
    2631                 :          0 :         return err;
    2632                 :            : }
    2633                 :            : EXPORT_SYMBOL(mmc_flush_cache);
    2634                 :            : 
    2635                 :            : /*
    2636                 :            :  * Turn the cache ON/OFF.
    2637                 :            :  * Turning the cache OFF shall trigger flushing of the data
    2638                 :            :  * to the non-volatile storage.
    2639                 :            :  * This function should be called with host claimed
    2640                 :            :  */
    2641                 :          0 : int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
    2642                 :            : {
    2643                 :          0 :         struct mmc_card *card = host->card;
    2644                 :            :         unsigned int timeout;
    2645                 :            :         int err = 0;
    2646                 :            : 
    2647 [ #  # ][ #  # ]:          0 :         if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
    2648                 :            :                         mmc_card_is_removable(host))
    2649                 :            :                 return err;
    2650                 :            : 
    2651 [ #  # ][ #  # ]:          0 :         if (card && mmc_card_mmc(card) &&
                 [ #  # ]
    2652                 :          0 :                         (card->ext_csd.cache_size > 0)) {
    2653                 :          0 :                 enable = !!enable;
    2654                 :            : 
    2655         [ #  # ]:          0 :                 if (card->ext_csd.cache_ctrl ^ enable) {
    2656         [ #  # ]:          0 :                         timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
    2657                 :          0 :                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
    2658                 :            :                                         EXT_CSD_CACHE_CTRL, enable, timeout);
    2659         [ #  # ]:          0 :                         if (err)
    2660         [ #  # ]:          0 :                                 pr_err("%s: cache %s error %d\n",
    2661                 :            :                                                 mmc_hostname(card->host),
    2662                 :            :                                                 enable ? "on" : "off",
    2663                 :            :                                                 err);
    2664                 :            :                         else
    2665                 :          0 :                                 card->ext_csd.cache_ctrl = enable;
    2666                 :            :                 }
    2667                 :            :         }
    2668                 :            : 
    2669                 :          0 :         return err;
    2670                 :            : }
    2671                 :            : EXPORT_SYMBOL(mmc_cache_ctrl);
    2672                 :            : 
    2673                 :            : #ifdef CONFIG_PM
    2674                 :            : 
    2675                 :            : /* Do the card removal on suspend if card is assumed removeable
    2676                 :            :  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
    2677                 :            :    to sync the card.
    2678                 :            : */
    2679                 :          0 : int mmc_pm_notify(struct notifier_block *notify_block,
    2680                 :            :                                         unsigned long mode, void *unused)
    2681                 :            : {
    2682 [ #  # ][ #  # ]:          0 :         struct mmc_host *host = container_of(
                 [ #  # ]
    2683                 :            :                 notify_block, struct mmc_host, pm_notify);
    2684                 :            :         unsigned long flags;
    2685                 :            :         int err = 0;
    2686                 :            : 
    2687                 :            :         switch (mode) {
    2688                 :            :         case PM_HIBERNATION_PREPARE:
    2689                 :            :         case PM_SUSPEND_PREPARE:
    2690                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
    2691                 :          0 :                 host->rescan_disable = 1;
    2692                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    2693                 :          0 :                 cancel_delayed_work_sync(&host->detect);
    2694                 :            : 
    2695         [ #  # ]:          0 :                 if (!host->bus_ops)
    2696                 :            :                         break;
    2697                 :            : 
    2698                 :            :                 /* Validate prerequisites for suspend */
    2699         [ #  # ]:          0 :                 if (host->bus_ops->pre_suspend)
    2700                 :          0 :                         err = host->bus_ops->pre_suspend(host);
    2701 [ #  # ][ #  # ]:          0 :                 if (!err && host->bus_ops->suspend)
    2702                 :            :                         break;
    2703                 :            : 
    2704                 :            :                 /* Calling bus_ops->remove() with a claimed host can deadlock */
    2705                 :          0 :                 host->bus_ops->remove(host);
    2706                 :            :                 mmc_claim_host(host);
    2707                 :          0 :                 mmc_detach_bus(host);
    2708                 :          0 :                 mmc_power_off(host);
    2709                 :          0 :                 mmc_release_host(host);
    2710                 :          0 :                 host->pm_flags = 0;
    2711                 :          0 :                 break;
    2712                 :            : 
    2713                 :            :         case PM_POST_SUSPEND:
    2714                 :            :         case PM_POST_HIBERNATION:
    2715                 :            :         case PM_POST_RESTORE:
    2716                 :            : 
    2717                 :          0 :                 spin_lock_irqsave(&host->lock, flags);
    2718                 :          0 :                 host->rescan_disable = 0;
    2719                 :            :                 spin_unlock_irqrestore(&host->lock, flags);
    2720                 :          0 :                 _mmc_detect_change(host, 0, false);
    2721                 :            : 
    2722                 :            :         }
    2723                 :            : 
    2724                 :          0 :         return 0;
    2725                 :            : }
    2726                 :            : #endif
    2727                 :            : 
    2728                 :            : /**
    2729                 :            :  * mmc_init_context_info() - init synchronization context
    2730                 :            :  * @host: mmc host
    2731                 :            :  *
    2732                 :            :  * Init struct context_info needed to implement asynchronous
    2733                 :            :  * request mechanism, used by mmc core, host driver and mmc requests
    2734                 :            :  * supplier.
    2735                 :            :  */
    2736                 :          0 : void mmc_init_context_info(struct mmc_host *host)
    2737                 :            : {
    2738                 :          0 :         spin_lock_init(&host->context_info.lock);
    2739                 :          0 :         host->context_info.is_new_req = false;
    2740                 :          0 :         host->context_info.is_done_rcv = false;
    2741                 :          0 :         host->context_info.is_waiting_last_req = false;
    2742                 :          0 :         init_waitqueue_head(&host->context_info.wait);
    2743                 :          0 : }
    2744                 :            : 
    2745                 :            : #ifdef CONFIG_MMC_EMBEDDED_SDIO
    2746                 :            : void mmc_set_embedded_sdio_data(struct mmc_host *host,
    2747                 :            :                                 struct sdio_cis *cis,
    2748                 :            :                                 struct sdio_cccr *cccr,
    2749                 :            :                                 struct sdio_embedded_func *funcs,
    2750                 :            :                                 int num_funcs)
    2751                 :            : {
    2752                 :            :         host->embedded_sdio_data.cis = cis;
    2753                 :            :         host->embedded_sdio_data.cccr = cccr;
    2754                 :            :         host->embedded_sdio_data.funcs = funcs;
    2755                 :            :         host->embedded_sdio_data.num_funcs = num_funcs;
    2756                 :            : }
    2757                 :            : 
    2758                 :            : EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
    2759                 :            : #endif
    2760                 :            : 
    2761                 :          0 : static int __init mmc_init(void)
    2762                 :            : {
    2763                 :            :         int ret;
    2764                 :            : 
    2765                 :          0 :         workqueue = alloc_ordered_workqueue("kmmcd", 0);
    2766         [ #  # ]:          0 :         if (!workqueue)
    2767                 :            :                 return -ENOMEM;
    2768                 :            : 
    2769                 :            :         wake_lock_init(&mmc_delayed_work_wake_lock, WAKE_LOCK_SUSPEND,
    2770                 :            :                        "mmc_delayed_work");
    2771                 :            : 
    2772                 :          0 :         ret = mmc_register_bus();
    2773         [ #  # ]:          0 :         if (ret)
    2774                 :            :                 goto destroy_workqueue;
    2775                 :            : 
    2776                 :          0 :         ret = mmc_register_host_class();
    2777         [ #  # ]:          0 :         if (ret)
    2778                 :            :                 goto unregister_bus;
    2779                 :            : 
    2780                 :          0 :         ret = sdio_register_bus();
    2781         [ #  # ]:          0 :         if (ret)
    2782                 :            :                 goto unregister_host_class;
    2783                 :            : 
    2784                 :            :         return 0;
    2785                 :            : 
    2786                 :            : unregister_host_class:
    2787                 :          0 :         mmc_unregister_host_class();
    2788                 :            : unregister_bus:
    2789                 :          0 :         mmc_unregister_bus();
    2790                 :            : destroy_workqueue:
    2791                 :          0 :         destroy_workqueue(workqueue);
    2792                 :            :         wake_lock_destroy(&mmc_delayed_work_wake_lock);
    2793                 :            : 
    2794                 :          0 :         return ret;
    2795                 :            : }
    2796                 :            : 
    2797                 :          0 : static void __exit mmc_exit(void)
    2798                 :            : {
    2799                 :          0 :         sdio_unregister_bus();
    2800                 :          0 :         mmc_unregister_host_class();
    2801                 :          0 :         mmc_unregister_bus();
    2802                 :          0 :         destroy_workqueue(workqueue);
    2803                 :            :         wake_lock_destroy(&mmc_delayed_work_wake_lock);
    2804                 :          0 : }
    2805                 :            : 
    2806                 :            : subsys_initcall(mmc_init);
    2807                 :            : module_exit(mmc_exit);
    2808                 :            : 
    2809                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.9