Lines Matching +full:supports +full:- +full:cqe
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
7 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
24 #include <linux/fault-inject.h>
33 #include <linux/mmc/slot-gpio.h>
49 /* The max erase timeout, used when host->max_busy_timeout isn't specified */
84 struct mmc_command *cmd = mrq->cmd; in mmc_should_fail_request()
85 struct mmc_data *data = mrq->data; in mmc_should_fail_request()
87 -ETIMEDOUT, in mmc_should_fail_request()
88 -EILSEQ, in mmc_should_fail_request()
89 -EIO, in mmc_should_fail_request()
95 if ((cmd && cmd->error) || data->error || in mmc_should_fail_request()
96 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) in mmc_should_fail_request()
99 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)]; in mmc_should_fail_request()
100 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9; in mmc_should_fail_request()
114 if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion)) in mmc_complete_cmd()
115 complete_all(&mrq->cmd_completion); in mmc_complete_cmd()
120 if (!mrq->cap_cmd_during_tfr) in mmc_command_done()
126 mmc_hostname(host), mrq->cmd->opcode); in mmc_command_done()
131 * mmc_request_done - finish processing an MMC request
140 struct mmc_command *cmd = mrq->cmd; in mmc_request_done()
141 int err = cmd->error; in mmc_request_done()
143 /* Flag re-tuning needed on CRC errors */ in mmc_request_done()
144 if (cmd->opcode != MMC_SEND_TUNING_BLOCK && in mmc_request_done()
145 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200 && in mmc_request_done()
146 !host->retune_crc_disable && in mmc_request_done()
147 (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) || in mmc_request_done()
148 (mrq->data && mrq->data->error == -EILSEQ) || in mmc_request_done()
149 (mrq->stop && mrq->stop->error == -EILSEQ))) in mmc_request_done()
152 if (err && cmd->retries && mmc_host_is_spi(host)) { in mmc_request_done()
153 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) in mmc_request_done()
154 cmd->retries = 0; in mmc_request_done()
157 if (host->ongoing_mrq == mrq) in mmc_request_done()
158 host->ongoing_mrq = NULL; in mmc_request_done()
168 * - There was no error, OK fine then in mmc_request_done()
169 * - We are not doing some kind of retry in mmc_request_done()
170 * - The card was removed (...so just complete everything no matter in mmc_request_done()
173 if (!err || !cmd->retries || mmc_card_removed(host->card)) { in mmc_request_done()
176 if (!host->ongoing_mrq) in mmc_request_done()
177 led_trigger_event(host->led, LED_OFF); in mmc_request_done()
179 if (mrq->sbc) { in mmc_request_done()
181 mmc_hostname(host), mrq->sbc->opcode, in mmc_request_done()
182 mrq->sbc->error, in mmc_request_done()
183 mrq->sbc->resp[0], mrq->sbc->resp[1], in mmc_request_done()
184 mrq->sbc->resp[2], mrq->sbc->resp[3]); in mmc_request_done()
188 mmc_hostname(host), cmd->opcode, err, in mmc_request_done()
189 cmd->resp[0], cmd->resp[1], in mmc_request_done()
190 cmd->resp[2], cmd->resp[3]); in mmc_request_done()
192 if (mrq->data) { in mmc_request_done()
195 mrq->data->bytes_xfered, mrq->data->error); in mmc_request_done()
198 if (mrq->stop) { in mmc_request_done()
200 mmc_hostname(host), mrq->stop->opcode, in mmc_request_done()
201 mrq->stop->error, in mmc_request_done()
202 mrq->stop->resp[0], mrq->stop->resp[1], in mmc_request_done()
203 mrq->stop->resp[2], mrq->stop->resp[3]); in mmc_request_done()
207 * Request starter must handle retries - see in mmc_request_done()
210 if (mrq->done) in mmc_request_done()
211 mrq->done(mrq); in mmc_request_done()
223 mrq->cmd->error = err; in __mmc_start_request()
233 if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) && in __mmc_start_request()
234 host->ops->card_busy) { in __mmc_start_request()
237 while (host->ops->card_busy(host) && --tries) in __mmc_start_request()
241 mrq->cmd->error = -EBUSY; in __mmc_start_request()
247 if (mrq->cap_cmd_during_tfr) { in __mmc_start_request()
248 host->ongoing_mrq = mrq; in __mmc_start_request()
253 reinit_completion(&mrq->cmd_completion); in __mmc_start_request()
258 if (host->cqe_on) in __mmc_start_request()
259 host->cqe_ops->cqe_off(host); in __mmc_start_request()
261 host->ops->request(host, mrq); in __mmc_start_request()
265 bool cqe) in mmc_mrq_pr_debug() argument
267 if (mrq->sbc) { in mmc_mrq_pr_debug()
269 mmc_hostname(host), mrq->sbc->opcode, in mmc_mrq_pr_debug()
270 mrq->sbc->arg, mrq->sbc->flags); in mmc_mrq_pr_debug()
273 if (mrq->cmd) { in mmc_mrq_pr_debug()
275 mmc_hostname(host), cqe ? "CQE direct " : "", in mmc_mrq_pr_debug()
276 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags); in mmc_mrq_pr_debug()
277 } else if (cqe) { in mmc_mrq_pr_debug()
278 pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n", in mmc_mrq_pr_debug()
279 mmc_hostname(host), mrq->tag, mrq->data->blk_addr); in mmc_mrq_pr_debug()
282 if (mrq->data) { in mmc_mrq_pr_debug()
285 mmc_hostname(host), mrq->data->blksz, in mmc_mrq_pr_debug()
286 mrq->data->blocks, mrq->data->flags, in mmc_mrq_pr_debug()
287 mrq->data->timeout_ns / 1000000, in mmc_mrq_pr_debug()
288 mrq->data->timeout_clks); in mmc_mrq_pr_debug()
291 if (mrq->stop) { in mmc_mrq_pr_debug()
293 mmc_hostname(host), mrq->stop->opcode, in mmc_mrq_pr_debug()
294 mrq->stop->arg, mrq->stop->flags); in mmc_mrq_pr_debug()
303 if (mrq->cmd) { in mmc_mrq_prep()
304 mrq->cmd->error = 0; in mmc_mrq_prep()
305 mrq->cmd->mrq = mrq; in mmc_mrq_prep()
306 mrq->cmd->data = mrq->data; in mmc_mrq_prep()
308 if (mrq->sbc) { in mmc_mrq_prep()
309 mrq->sbc->error = 0; in mmc_mrq_prep()
310 mrq->sbc->mrq = mrq; in mmc_mrq_prep()
312 if (mrq->data) { in mmc_mrq_prep()
313 if (mrq->data->blksz > host->max_blk_size || in mmc_mrq_prep()
314 mrq->data->blocks > host->max_blk_count || in mmc_mrq_prep()
315 mrq->data->blocks * mrq->data->blksz > host->max_req_size) in mmc_mrq_prep()
316 return -EINVAL; in mmc_mrq_prep()
318 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) in mmc_mrq_prep()
319 sz += sg->length; in mmc_mrq_prep()
320 if (sz != mrq->data->blocks * mrq->data->blksz) in mmc_mrq_prep()
321 return -EINVAL; in mmc_mrq_prep()
323 mrq->data->error = 0; in mmc_mrq_prep()
324 mrq->data->mrq = mrq; in mmc_mrq_prep()
325 if (mrq->stop) { in mmc_mrq_prep()
326 mrq->data->stop = mrq->stop; in mmc_mrq_prep()
327 mrq->stop->error = 0; in mmc_mrq_prep()
328 mrq->stop->mrq = mrq; in mmc_mrq_prep()
339 init_completion(&mrq->cmd_completion); in mmc_start_request()
343 if (mmc_card_removed(host->card)) in mmc_start_request()
344 return -ENOMEDIUM; in mmc_start_request()
348 WARN_ON(!host->claimed); in mmc_start_request()
354 led_trigger_event(host->led, LED_FULL); in mmc_start_request()
363 complete(&mrq->completion); in mmc_wait_done()
368 struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq); in mmc_wait_ongoing_tfr_cmd()
374 if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion)) in mmc_wait_ongoing_tfr_cmd()
375 wait_for_completion(&ongoing_mrq->cmd_completion); in mmc_wait_ongoing_tfr_cmd()
384 init_completion(&mrq->completion); in __mmc_start_req()
385 mrq->done = mmc_wait_done; in __mmc_start_req()
389 mrq->cmd->error = err; in __mmc_start_req()
391 complete(&mrq->completion); in __mmc_start_req()
402 wait_for_completion(&mrq->completion); in mmc_wait_for_req_done()
404 cmd = mrq->cmd; in mmc_wait_for_req_done()
406 if (!cmd->error || !cmd->retries || in mmc_wait_for_req_done()
407 mmc_card_removed(host->card)) in mmc_wait_for_req_done()
413 mmc_hostname(host), cmd->opcode, cmd->error); in mmc_wait_for_req_done()
414 cmd->retries--; in mmc_wait_for_req_done()
415 cmd->error = 0; in mmc_wait_for_req_done()
424 * mmc_cqe_start_req - Start a CQE request.
428 * Start the request, re-tuning if needed and it is possible. Returns an error
429 * code if the request fails to start or -EBUSY if CQE is busy.
436 * CQE cannot process re-tuning commands. Caller must hold retuning in mmc_cqe_start_req()
437 * while CQE is in use. Re-tuning can happen here only when CQE has no in mmc_cqe_start_req()
438 * active requests i.e. this is the first. Note, re-tuning will call in mmc_cqe_start_req()
439 * ->cqe_off(). in mmc_cqe_start_req()
445 mrq->host = host; in mmc_cqe_start_req()
453 err = host->cqe_ops->cqe_request(host, mrq); in mmc_cqe_start_req()
462 if (mrq->cmd) { in mmc_cqe_start_req()
463 pr_debug("%s: failed to start CQE direct CMD%u, error %d\n", in mmc_cqe_start_req()
464 mmc_hostname(host), mrq->cmd->opcode, err); in mmc_cqe_start_req()
466 pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n", in mmc_cqe_start_req()
467 mmc_hostname(host), mrq->tag, err); in mmc_cqe_start_req()
474 * mmc_cqe_request_done - CQE has finished processing an MMC request
478 * CQE drivers should call this function when they have completed
485 /* Flag re-tuning needed on CRC errors */ in mmc_cqe_request_done()
486 if ((mrq->cmd && mrq->cmd->error == -EILSEQ) || in mmc_cqe_request_done()
487 (mrq->data && mrq->data->error == -EILSEQ)) in mmc_cqe_request_done()
492 if (mrq->cmd) { in mmc_cqe_request_done()
493 pr_debug("%s: CQE req done (direct CMD%u): %d\n", in mmc_cqe_request_done()
494 mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error); in mmc_cqe_request_done()
496 pr_debug("%s: CQE transfer done tag %d\n", in mmc_cqe_request_done()
497 mmc_hostname(host), mrq->tag); in mmc_cqe_request_done()
500 if (mrq->data) { in mmc_cqe_request_done()
503 mrq->data->bytes_xfered, mrq->data->error); in mmc_cqe_request_done()
506 mrq->done(mrq); in mmc_cqe_request_done()
511 * mmc_cqe_post_req - CQE post process of a completed MMC request
517 if (host->cqe_ops->cqe_post_req) in mmc_cqe_post_req()
518 host->cqe_ops->cqe_post_req(host, mrq); in mmc_cqe_post_req()
526 * mmc_cqe_recovery - Recover from CQE errors.
529 * Recovery consists of stopping CQE, stopping eMMC, discarding the queue in
530 * in eMMC, and discarding the queue in CQE. CQE must call
545 pr_warn("%s: running CQE recovery\n", mmc_hostname(host)); in mmc_cqe_recovery()
547 host->cqe_ops->cqe_recovery_start(host); in mmc_cqe_recovery()
564 host->cqe_ops->cqe_recovery_finish(host); in mmc_cqe_recovery()
573 * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
578 * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
586 return completion_done(&mrq->completion); in mmc_is_req_done()
591 * mmc_wait_for_req - start a request and wait for completion
606 if (!mrq->cap_cmd_during_tfr) in mmc_wait_for_req()
612 * mmc_wait_for_cmd - start a command and wait for completion
625 WARN_ON(!host->claimed); in mmc_wait_for_cmd()
627 memset(cmd->resp, 0, sizeof(cmd->resp)); in mmc_wait_for_cmd()
628 cmd->retries = retries; in mmc_wait_for_cmd()
631 cmd->data = NULL; in mmc_wait_for_cmd()
635 return cmd->error; in mmc_wait_for_cmd()
641 * mmc_set_data_timeout - set the timeout for a data command
656 data->timeout_ns = 1000000000; in mmc_set_data_timeout()
657 data->timeout_clks = 0; in mmc_set_data_timeout()
670 if (data->flags & MMC_DATA_WRITE) in mmc_set_data_timeout()
671 mult <<= card->csd.r2w_factor; in mmc_set_data_timeout()
673 data->timeout_ns = card->csd.taac_ns * mult; in mmc_set_data_timeout()
674 data->timeout_clks = card->csd.taac_clks * mult; in mmc_set_data_timeout()
682 timeout_us = data->timeout_ns / 1000; in mmc_set_data_timeout()
683 if (card->host->ios.clock) in mmc_set_data_timeout()
684 timeout_us += data->timeout_clks * 1000 / in mmc_set_data_timeout()
685 (card->host->ios.clock / 1000); in mmc_set_data_timeout()
687 if (data->flags & MMC_DATA_WRITE) in mmc_set_data_timeout()
704 data->timeout_ns = limit_us * 1000; in mmc_set_data_timeout()
705 data->timeout_clks = 0; in mmc_set_data_timeout()
710 data->timeout_ns = limit_us * 1000; in mmc_set_data_timeout()
719 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) { in mmc_set_data_timeout()
720 data->timeout_ns = 600000000; in mmc_set_data_timeout()
721 data->timeout_clks = 0; in mmc_set_data_timeout()
730 if (mmc_host_is_spi(card->host)) { in mmc_set_data_timeout()
731 if (data->flags & MMC_DATA_WRITE) { in mmc_set_data_timeout()
732 if (data->timeout_ns < 1000000000) in mmc_set_data_timeout()
733 data->timeout_ns = 1000000000; /* 1s */ in mmc_set_data_timeout()
735 if (data->timeout_ns < 100000000) in mmc_set_data_timeout()
736 data->timeout_ns = 100000000; /* 100ms */ in mmc_set_data_timeout()
749 return host->claimer == ctx || in mmc_ctx_matches()
750 (!ctx && task && host->claimer->task == task); in mmc_ctx_matches()
757 if (!host->claimer) { in mmc_ctx_set_claimer()
759 host->claimer = ctx; in mmc_ctx_set_claimer()
761 host->claimer = &host->default_ctx; in mmc_ctx_set_claimer()
764 host->claimer->task = task; in mmc_ctx_set_claimer()
768 * __mmc_claim_host - exclusively claim a host
775 * dereference a non-zero value then this will return prematurely with
776 * that non-zero value without acquiring the lock. Returns zero
790 add_wait_queue(&host->wq, &wait); in __mmc_claim_host()
791 spin_lock_irqsave(&host->lock, flags); in __mmc_claim_host()
795 if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task)) in __mmc_claim_host()
797 spin_unlock_irqrestore(&host->lock, flags); in __mmc_claim_host()
799 spin_lock_irqsave(&host->lock, flags); in __mmc_claim_host()
803 host->claimed = 1; in __mmc_claim_host()
805 host->claim_cnt += 1; in __mmc_claim_host()
806 if (host->claim_cnt == 1) in __mmc_claim_host()
809 wake_up(&host->wq); in __mmc_claim_host()
810 spin_unlock_irqrestore(&host->lock, flags); in __mmc_claim_host()
811 remove_wait_queue(&host->wq, &wait); in __mmc_claim_host()
821 * mmc_release_host - release a host
831 WARN_ON(!host->claimed); in mmc_release_host()
833 spin_lock_irqsave(&host->lock, flags); in mmc_release_host()
834 if (--host->claim_cnt) { in mmc_release_host()
836 spin_unlock_irqrestore(&host->lock, flags); in mmc_release_host()
838 host->claimed = 0; in mmc_release_host()
839 host->claimer->task = NULL; in mmc_release_host()
840 host->claimer = NULL; in mmc_release_host()
841 spin_unlock_irqrestore(&host->lock, flags); in mmc_release_host()
842 wake_up(&host->wq); in mmc_release_host()
844 if (host->caps & MMC_CAP_SYNC_RUNTIME_PM) in mmc_release_host()
858 pm_runtime_get_sync(&card->dev); in mmc_get_card()
859 __mmc_claim_host(card->host, ctx, NULL); in mmc_get_card()
869 struct mmc_host *host = card->host; in mmc_put_card()
871 WARN_ON(ctx && host->claimer != ctx); in mmc_put_card()
874 pm_runtime_mark_last_busy(&card->dev); in mmc_put_card()
875 pm_runtime_put_autosuspend(&card->dev); in mmc_put_card()
885 struct mmc_ios *ios = &host->ios; in mmc_set_ios()
889 mmc_hostname(host), ios->clock, ios->bus_mode, in mmc_set_ios()
890 ios->power_mode, ios->chip_select, ios->vdd, in mmc_set_ios()
891 1 << ios->bus_width, ios->timing); in mmc_set_ios()
893 host->ops->set_ios(host, ios); in mmc_set_ios()
901 host->ios.chip_select = mode; in mmc_set_chip_select()
911 WARN_ON(hz && hz < host->f_min); in mmc_set_clock()
913 if (hz > host->f_max) in mmc_set_clock()
914 hz = host->f_max; in mmc_set_clock()
916 host->ios.clock = hz; in mmc_set_clock()
922 struct mmc_host *host = card->host; in mmc_execute_tuning()
926 if (!host->ops->execute_tuning) in mmc_execute_tuning()
929 if (host->cqe_on) in mmc_execute_tuning()
930 host->cqe_ops->cqe_off(host); in mmc_execute_tuning()
937 err = host->ops->execute_tuning(host, opcode); in mmc_execute_tuning()
949 * Change the bus mode (open drain/push-pull) of a host.
953 host->ios.bus_mode = mode; in mmc_set_bus_mode()
962 host->ios.bus_width = width; in mmc_set_bus_width()
971 if (host->cqe_on) in mmc_set_initial_state()
972 host->cqe_ops->cqe_off(host); in mmc_set_initial_state()
977 host->ios.chip_select = MMC_CS_HIGH; in mmc_set_initial_state()
979 host->ios.chip_select = MMC_CS_DONTCARE; in mmc_set_initial_state()
980 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; in mmc_set_initial_state()
981 host->ios.bus_width = MMC_BUS_WIDTH_1; in mmc_set_initial_state()
982 host->ios.timing = MMC_TIMING_LEGACY; in mmc_set_initial_state()
983 host->ios.drv_type = 0; in mmc_set_initial_state()
984 host->ios.enhanced_strobe = false; in mmc_set_initial_state()
987 * Make sure we are in non-enhanced strobe mode before we in mmc_set_initial_state()
990 if ((host->caps2 & MMC_CAP2_HS400_ES) && in mmc_set_initial_state()
991 host->ops->hs400_enhanced_strobe) in mmc_set_initial_state()
992 host->ops->hs400_enhanced_strobe(host, &host->ios); in mmc_set_initial_state()
998 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1018 return -EINVAL; in mmc_vdd_to_ocrbitnum()
1024 vdd -= 1; in mmc_vdd_to_ocrbitnum()
1027 bit = (vdd - 2000) / 100 + 8; in mmc_vdd_to_ocrbitnum()
1034 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1065 mask |= 1 << vdd_max--; in mmc_vddrange_to_ocrmask()
1087 if (!host->parent || !host->parent->of_node) in mmc_of_find_child_device()
1090 for_each_child_of_node(host->parent->of_node, node) { in mmc_of_find_child_device()
1116 ocr &= host->ocr_avail; in mmc_select_voltage()
1122 if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) { in mmc_select_voltage()
1123 bit = ffs(ocr) - 1; in mmc_select_voltage()
1127 bit = fls(ocr) - 1; in mmc_select_voltage()
1129 if (bit != host->ios.vdd) in mmc_select_voltage()
1139 int old_signal_voltage = host->ios.signal_voltage; in mmc_set_signal_voltage()
1141 host->ios.signal_voltage = signal_voltage; in mmc_set_signal_voltage()
1142 if (host->ops->start_signal_voltage_switch) in mmc_set_signal_voltage()
1143 err = host->ops->start_signal_voltage_switch(host, &host->ios); in mmc_set_signal_voltage()
1146 host->ios.signal_voltage = old_signal_voltage; in mmc_set_signal_voltage()
1171 clock = host->ios.clock; in mmc_host_set_uhs_voltage()
1172 host->ios.clock = 0; in mmc_host_set_uhs_voltage()
1176 return -EAGAIN; in mmc_host_set_uhs_voltage()
1180 host->ios.clock = clock; in mmc_host_set_uhs_voltage()
1195 if (!host->ops->start_signal_voltage_switch) in mmc_set_uhs_voltage()
1196 return -EPERM; in mmc_set_uhs_voltage()
1197 if (!host->ops->card_busy) in mmc_set_uhs_voltage()
1210 return -EIO; in mmc_set_uhs_voltage()
1217 if (host->ops->card_busy && !host->ops->card_busy(host)) { in mmc_set_uhs_voltage()
1218 err = -EAGAIN; in mmc_set_uhs_voltage()
1227 err = -EAGAIN; in mmc_set_uhs_voltage()
1238 if (host->ops->card_busy && host->ops->card_busy(host)) in mmc_set_uhs_voltage()
1239 err = -EAGAIN; in mmc_set_uhs_voltage()
1256 host->ios.timing = timing; in mmc_set_timing()
1265 host->ios.drv_type = drv_type; in mmc_set_driver_type()
1272 struct mmc_host *host = card->host; in mmc_select_drive_strength()
1277 if (!host->ops->select_drive_strength) in mmc_select_drive_strength()
1281 if (host->caps & MMC_CAP_DRIVER_TYPE_A) in mmc_select_drive_strength()
1284 if (host->caps & MMC_CAP_DRIVER_TYPE_C) in mmc_select_drive_strength()
1287 if (host->caps & MMC_CAP_DRIVER_TYPE_D) in mmc_select_drive_strength()
1296 return host->ops->select_drive_strength(card, max_dtr, in mmc_select_drive_strength()
1303 * Apply power to the MMC stack. This is a two-stage process.
1315 if (host->ios.power_mode == MMC_POWER_ON) in mmc_power_up()
1320 host->ios.vdd = fls(ocr) - 1; in mmc_power_up()
1321 host->ios.power_mode = MMC_POWER_UP; in mmc_power_up()
1331 mmc_delay(host->ios.power_delay_ms); in mmc_power_up()
1335 host->ios.clock = host->f_init; in mmc_power_up()
1337 host->ios.power_mode = MMC_POWER_ON; in mmc_power_up()
1344 mmc_delay(host->ios.power_delay_ms); in mmc_power_up()
1349 if (host->ios.power_mode == MMC_POWER_OFF) in mmc_power_off()
1354 host->ios.clock = 0; in mmc_power_off()
1355 host->ios.vdd = 0; in mmc_power_off()
1357 host->ios.power_mode = MMC_POWER_OFF; in mmc_power_off()
1363 * XO-1.5, require a short delay after poweroff before the card in mmc_power_off()
1382 WARN_ON(!host->bus_dead); in __mmc_release_bus()
1384 host->bus_ops = NULL; in __mmc_release_bus()
1394 spin_lock_irqsave(&host->lock, flags); in mmc_bus_get()
1395 host->bus_refs++; in mmc_bus_get()
1396 spin_unlock_irqrestore(&host->lock, flags); in mmc_bus_get()
1407 spin_lock_irqsave(&host->lock, flags); in mmc_bus_put()
1408 host->bus_refs--; in mmc_bus_put()
1409 if ((host->bus_refs == 0) && host->bus_ops) in mmc_bus_put()
1411 spin_unlock_irqrestore(&host->lock, flags); in mmc_bus_put()
1422 WARN_ON(!host->claimed); in mmc_attach_bus()
1424 spin_lock_irqsave(&host->lock, flags); in mmc_attach_bus()
1426 WARN_ON(host->bus_ops); in mmc_attach_bus()
1427 WARN_ON(host->bus_refs); in mmc_attach_bus()
1429 host->bus_ops = ops; in mmc_attach_bus()
1430 host->bus_refs = 1; in mmc_attach_bus()
1431 host->bus_dead = 0; in mmc_attach_bus()
1433 spin_unlock_irqrestore(&host->lock, flags); in mmc_attach_bus()
1443 WARN_ON(!host->claimed); in mmc_detach_bus()
1444 WARN_ON(!host->bus_ops); in mmc_detach_bus()
1446 spin_lock_irqsave(&host->lock, flags); in mmc_detach_bus()
1448 host->bus_dead = 1; in mmc_detach_bus()
1450 spin_unlock_irqrestore(&host->lock, flags); in mmc_detach_bus()
1462 if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL)) in _mmc_detect_change()
1463 __pm_wakeup_event(host->ws, 5000); in _mmc_detect_change()
1465 host->detect_change = 1; in _mmc_detect_change()
1466 mmc_schedule_delayed_work(&host->detect, delay); in _mmc_detect_change()
1470 * mmc_detect_change - process change of state on a MMC socket
1489 if (is_power_of_2(card->erase_size)) in mmc_init_erase()
1490 card->erase_shift = ffs(card->erase_size) - 1; in mmc_init_erase()
1492 card->erase_shift = 0; in mmc_init_erase()
1498 * timeout calculations become increasingly hugely over-estimated. in mmc_init_erase()
1509 if (mmc_card_sd(card) && card->ssr.au) { in mmc_init_erase()
1510 card->pref_erase = card->ssr.au; in mmc_init_erase()
1511 card->erase_shift = ffs(card->ssr.au) - 1; in mmc_init_erase()
1512 } else if (card->erase_size) { in mmc_init_erase()
1513 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; in mmc_init_erase()
1515 card->pref_erase = 512 * 1024 / 512; in mmc_init_erase()
1517 card->pref_erase = 1024 * 1024 / 512; in mmc_init_erase()
1519 card->pref_erase = 2 * 1024 * 1024 / 512; in mmc_init_erase()
1521 card->pref_erase = 4 * 1024 * 1024 / 512; in mmc_init_erase()
1522 if (card->pref_erase < card->erase_size) in mmc_init_erase()
1523 card->pref_erase = card->erase_size; in mmc_init_erase()
1525 sz = card->pref_erase % card->erase_size; in mmc_init_erase()
1527 card->pref_erase += card->erase_size - sz; in mmc_init_erase()
1530 card->pref_erase = 0; in mmc_init_erase()
1539 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) { in mmc_mmc_erase_timeout()
1540 erase_timeout = card->ext_csd.trim_timeout; in mmc_mmc_erase_timeout()
1541 } else if (card->ext_csd.erase_group_def & 1) { in mmc_mmc_erase_timeout()
1544 erase_timeout = card->ext_csd.trim_timeout; in mmc_mmc_erase_timeout()
1546 erase_timeout = card->ext_csd.hc_erase_timeout; in mmc_mmc_erase_timeout()
1549 unsigned int mult = (10 << card->csd.r2w_factor); in mmc_mmc_erase_timeout()
1550 unsigned int timeout_clks = card->csd.taac_clks * mult; in mmc_mmc_erase_timeout()
1554 if (card->csd.taac_ns < 1000000) in mmc_mmc_erase_timeout()
1555 timeout_us = (card->csd.taac_ns * mult) / 1000; in mmc_mmc_erase_timeout()
1557 timeout_us = (card->csd.taac_ns / 1000) * mult; in mmc_mmc_erase_timeout()
1565 (card->host->ios.clock / 1000); in mmc_mmc_erase_timeout()
1580 erase_timeout *= card->ext_csd.sec_erase_mult; in mmc_mmc_erase_timeout()
1582 erase_timeout *= card->ext_csd.sec_trim_mult; in mmc_mmc_erase_timeout()
1591 if (mmc_host_is_spi(card->host) && erase_timeout < 1000) in mmc_mmc_erase_timeout()
1609 if (card->ssr.erase_timeout) { in mmc_sd_erase_timeout()
1611 erase_timeout = card->ssr.erase_timeout * qty + in mmc_sd_erase_timeout()
1612 card->ssr.erase_offset; in mmc_sd_erase_timeout()
1646 mmc_retune_hold(card->host); in mmc_do_erase()
1664 if (card->erase_shift) in mmc_do_erase()
1665 qty += ((to >> card->erase_shift) - in mmc_do_erase()
1666 (from >> card->erase_shift)) + 1; in mmc_do_erase()
1668 qty += to - from + 1; in mmc_do_erase()
1670 qty += ((to / card->erase_size) - in mmc_do_erase()
1671 (from / card->erase_size)) + 1; in mmc_do_erase()
1684 err = mmc_wait_for_cmd(card->host, &cmd, 0); in mmc_do_erase()
1688 err = -EIO; in mmc_do_erase()
1699 err = mmc_wait_for_cmd(card->host, &cmd, 0); in mmc_do_erase()
1703 err = -EIO; in mmc_do_erase()
1712 * If the host controller supports busy signalling and the timeout for in mmc_do_erase()
1719 if (!(card->host->caps & MMC_CAP_NEED_RSP_BUSY) && in mmc_do_erase()
1720 card->host->max_busy_timeout && in mmc_do_erase()
1721 busy_timeout > card->host->max_busy_timeout) { in mmc_do_erase()
1729 err = mmc_wait_for_cmd(card->host, &cmd, 0); in mmc_do_erase()
1733 err = -EIO; in mmc_do_erase()
1737 if (mmc_host_is_spi(card->host)) in mmc_do_erase()
1744 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) in mmc_do_erase()
1751 mmc_retune_release(card->host); in mmc_do_erase()
1763 * When the 'card->erase_size' is power of 2, we can use round_up/down() in mmc_align_erase_size()
1766 if (is_power_of_2(card->erase_size)) { in mmc_align_erase_size()
1769 from_new = round_up(temp, card->erase_size); in mmc_align_erase_size()
1770 rem = from_new - temp; in mmc_align_erase_size()
1773 nr_new -= rem; in mmc_align_erase_size()
1777 nr_new = round_down(nr_new, card->erase_size); in mmc_align_erase_size()
1779 rem = from_new % card->erase_size; in mmc_align_erase_size()
1781 rem = card->erase_size - rem; in mmc_align_erase_size()
1784 nr_new -= rem; in mmc_align_erase_size()
1789 rem = nr_new % card->erase_size; in mmc_align_erase_size()
1791 nr_new -= rem; in mmc_align_erase_size()
1804 * mmc_erase - erase sectors.
1818 if (!(card->csd.cmdclass & CCC_ERASE)) in mmc_erase()
1819 return -EOPNOTSUPP; in mmc_erase()
1821 if (!card->erase_size) in mmc_erase()
1822 return -EOPNOTSUPP; in mmc_erase()
1825 return -EOPNOTSUPP; in mmc_erase()
1828 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) in mmc_erase()
1829 return -EOPNOTSUPP; in mmc_erase()
1832 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) in mmc_erase()
1833 return -EOPNOTSUPP; in mmc_erase()
1836 if (from % card->erase_size || nr % card->erase_size) in mmc_erase()
1837 return -EINVAL; in mmc_erase()
1847 return -EINVAL; in mmc_erase()
1850 to -= 1; in mmc_erase()
1853 * Special case where only one erase-group fits in the timeout budget: in mmc_erase()
1854 * If the region crosses an erase-group boundary on this particular in mmc_erase()
1855 * case, we will be trimming more than one erase-group which, does not in mmc_erase()
1858 * identified by the card->eg_boundary flag. in mmc_erase()
1860 rem = card->erase_size - (from % card->erase_size); in mmc_erase()
1861 if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) { in mmc_erase()
1862 err = mmc_do_erase(card, from, from + rem - 1, arg); in mmc_erase()
1874 if (card->csd.cmdclass & CCC_ERASE && card->erase_size) in mmc_can_erase()
1882 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) && in mmc_can_trim()
1883 (!(card->quirks & MMC_QUIRK_TRIM_BROKEN))) in mmc_can_trim()
1895 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE) in mmc_can_discard()
1905 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) in mmc_can_sanitize()
1912 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) && in mmc_can_secure_erase_trim()
1913 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN)) in mmc_can_secure_erase_trim()
1922 if (!card->erase_size) in mmc_erase_group_aligned()
1924 if (from % card->erase_size || nr % card->erase_size) in mmc_erase_group_aligned()
1933 struct mmc_host *host = card->host; in mmc_do_calc_max_discard()
1936 unsigned int max_busy_timeout = host->max_busy_timeout ? in mmc_do_calc_max_discard()
1937 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS; in mmc_do_calc_max_discard()
1939 if (card->erase_shift) { in mmc_do_calc_max_discard()
1940 max_qty = UINT_MAX >> card->erase_shift; in mmc_do_calc_max_discard()
1941 min_qty = card->pref_erase >> card->erase_shift; in mmc_do_calc_max_discard()
1944 min_qty = card->pref_erase; in mmc_do_calc_max_discard()
1946 max_qty = UINT_MAX / card->erase_size; in mmc_do_calc_max_discard()
1947 min_qty = card->pref_erase / card->erase_size; in mmc_do_calc_max_discard()
1951 * We should not only use 'host->max_busy_timeout' as the limitation in mmc_do_calc_max_discard()
1956 * Here we set 'card->pref_erase' as the minimal discard sectors no in mmc_do_calc_max_discard()
1957 * matter what size of 'host->max_busy_timeout', but if the in mmc_do_calc_max_discard()
1958 * 'host->max_busy_timeout' is large enough for more discard sectors, in mmc_do_calc_max_discard()
1960 * get a balance value. In cases when the 'host->max_busy_timeout' in mmc_do_calc_max_discard()
1965 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { in mmc_do_calc_max_discard()
1984 * an erase-group boundary even if the amount of sectors is less than in mmc_do_calc_max_discard()
1985 * one erase-group. in mmc_do_calc_max_discard()
1986 * If we can only fit one erase-group in the controller timeout budget, in mmc_do_calc_max_discard()
1987 * we have to care that erase-group boundaries are not crossed by a in mmc_do_calc_max_discard()
1990 * always touch (qty + 1) erase-groups as a simple optimization. in mmc_do_calc_max_discard()
1993 card->eg_boundary = 1; in mmc_do_calc_max_discard()
1995 qty--; in mmc_do_calc_max_discard()
1998 if (card->erase_shift) in mmc_do_calc_max_discard()
1999 max_discard = qty << card->erase_shift; in mmc_do_calc_max_discard()
2003 max_discard = qty * card->erase_size; in mmc_do_calc_max_discard()
2010 struct mmc_host *host = card->host; in mmc_calc_max_discard()
2018 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) in mmc_calc_max_discard()
2019 return card->pref_erase; in mmc_calc_max_discard()
2026 } else if (max_discard < card->erase_size) { in mmc_calc_max_discard()
2030 mmc_hostname(host), max_discard, host->max_busy_timeout ? in mmc_calc_max_discard()
2031 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS); in mmc_calc_max_discard()
2053 return mmc_wait_for_cmd(card->host, &cmd, 5); in mmc_set_blocklen()
2061 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) in mmc_hw_reset_for_init()
2063 host->ops->hw_reset(host); in mmc_hw_reset_for_init()
2067 * mmc_hw_reset - reset the card in hardware
2074 * Return: 0 on success, -errno on failure
2080 if (!host->card) in mmc_hw_reset()
2081 return -EINVAL; in mmc_hw_reset()
2084 if (!host->bus_ops || host->bus_dead || !host->bus_ops->hw_reset) { in mmc_hw_reset()
2086 return -EOPNOTSUPP; in mmc_hw_reset()
2089 ret = host->bus_ops->hw_reset(host); in mmc_hw_reset()
2104 if (!host->card) in mmc_sw_reset()
2105 return -EINVAL; in mmc_sw_reset()
2108 if (!host->bus_ops || host->bus_dead || !host->bus_ops->sw_reset) { in mmc_sw_reset()
2110 return -EOPNOTSUPP; in mmc_sw_reset()
2113 ret = host->bus_ops->sw_reset(host); in mmc_sw_reset()
2126 host->f_init = freq; in mmc_rescan_try_freq()
2129 mmc_hostname(host), __func__, host->f_init); in mmc_rescan_try_freq()
2131 mmc_power_up(host, host->ocr_avail); in mmc_rescan_try_freq()
2141 * if the card is being re-initialized, just send it. CMD52 in mmc_rescan_try_freq()
2145 if (!(host->caps2 & MMC_CAP2_NO_SDIO)) in mmc_rescan_try_freq()
2150 if (!(host->caps2 & MMC_CAP2_NO_SD)) in mmc_rescan_try_freq()
2151 mmc_send_if_cond(host, host->ocr_avail); in mmc_rescan_try_freq()
2154 if (!(host->caps2 & MMC_CAP2_NO_SDIO)) in mmc_rescan_try_freq()
2158 if (!(host->caps2 & MMC_CAP2_NO_SD)) in mmc_rescan_try_freq()
2162 if (!(host->caps2 & MMC_CAP2_NO_MMC)) in mmc_rescan_try_freq()
2167 return -EIO; in mmc_rescan_try_freq()
2174 if (!host->card || mmc_card_removed(host->card)) in _mmc_detect_card_removed()
2177 ret = host->bus_ops->alive(host); in _mmc_detect_card_removed()
2186 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) { in _mmc_detect_card_removed()
2192 mmc_card_set_removed(host->card); in _mmc_detect_card_removed()
2201 struct mmc_card *card = host->card; in mmc_detect_card_removed()
2204 WARN_ON(!host->claimed); in mmc_detect_card_removed()
2217 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL)) in mmc_detect_card_removed()
2220 host->detect_change = 0; in mmc_detect_card_removed()
2223 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) { in mmc_detect_card_removed()
2228 cancel_delayed_work(&host->detect); in mmc_detect_card_removed()
2243 if (host->rescan_disable) in mmc_rescan()
2246 /* If there is a non-removable card registered, only scan once */ in mmc_rescan()
2247 if (!mmc_card_is_removable(host) && host->rescan_entered) in mmc_rescan()
2249 host->rescan_entered = 1; in mmc_rescan()
2251 if (host->trigger_card_event && host->ops->card_event) { in mmc_rescan()
2253 host->ops->card_event(host); in mmc_rescan()
2255 host->trigger_card_event = false; in mmc_rescan()
2261 if (host->bus_ops && !host->bus_dead) in mmc_rescan()
2262 host->bus_ops->detect(host); in mmc_rescan()
2264 host->detect_change = 0; in mmc_rescan()
2274 if (host->bus_ops != NULL) { in mmc_rescan()
2286 if (mmc_card_is_removable(host) && host->ops->get_cd && in mmc_rescan()
2287 host->ops->get_cd(host) == 0) { in mmc_rescan()
2295 if (freq > host->f_max) { in mmc_rescan()
2298 freq = host->f_max; in mmc_rescan()
2300 if (!mmc_rescan_try_freq(host, max(freq, host->f_min))) in mmc_rescan()
2302 if (freqs[i] <= host->f_min) in mmc_rescan()
2308 if (host->caps & MMC_CAP_NEEDS_POLL) in mmc_rescan()
2309 mmc_schedule_delayed_work(&host->detect, HZ); in mmc_rescan()
2314 host->f_init = max(min(freqs[0], host->f_max), host->f_min); in mmc_start_host()
2315 host->rescan_disable = 0; in mmc_start_host()
2317 if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) { in mmc_start_host()
2319 mmc_power_up(host, host->ocr_avail); in mmc_start_host()
2329 if (host->slot.cd_irq >= 0) { in mmc_stop_host()
2331 disable_irq(host->slot.cd_irq); in mmc_stop_host()
2334 host->rescan_disable = 1; in mmc_stop_host()
2335 cancel_delayed_work_sync(&host->detect); in mmc_stop_host()
2338 host->pm_flags = 0; in mmc_stop_host()
2341 if (host->bus_ops && !host->bus_dead) { in mmc_stop_host()
2342 /* Calling bus_ops->remove() with a claimed host can deadlock */ in mmc_stop_host()
2343 host->bus_ops->remove(host); in mmc_stop_host()
2375 spin_lock_irqsave(&host->lock, flags); in mmc_pm_notify()
2376 host->rescan_disable = 1; in mmc_pm_notify()
2377 spin_unlock_irqrestore(&host->lock, flags); in mmc_pm_notify()
2378 cancel_delayed_work_sync(&host->detect); in mmc_pm_notify()
2380 if (!host->bus_ops) in mmc_pm_notify()
2384 if (host->bus_ops->pre_suspend) in mmc_pm_notify()
2385 err = host->bus_ops->pre_suspend(host); in mmc_pm_notify()
2391 "pre_suspend failed for non-removable host: " in mmc_pm_notify()
2393 /* Avoid removing non-removable hosts */ in mmc_pm_notify()
2397 /* Calling bus_ops->remove() with a claimed host can deadlock */ in mmc_pm_notify()
2398 host->bus_ops->remove(host); in mmc_pm_notify()
2403 host->pm_flags = 0; in mmc_pm_notify()
2410 spin_lock_irqsave(&host->lock, flags); in mmc_pm_notify()
2411 host->rescan_disable = 0; in mmc_pm_notify()
2412 spin_unlock_irqrestore(&host->lock, flags); in mmc_pm_notify()
2422 host->pm_notify.notifier_call = mmc_pm_notify; in mmc_register_pm_notifier()
2423 register_pm_notifier(&host->pm_notify); in mmc_register_pm_notifier()
2428 unregister_pm_notifier(&host->pm_notify); in mmc_unregister_pm_notifier()