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