LCOV - code coverage report
Current view: top level - drivers/mmc/core - core.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 888 5.1 %
Date: 2014-02-18 Functions: 4 79 5.1 %
Branches: 22 812 2.7 %

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

Generated by: LCOV version 1.9