Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2003 Russell King, All Rights Reserved.
4 * Copyright 2006-2007 Pierre Ossman
5 */
6#include <linux/slab.h>
7#include <linux/module.h>
8#include <linux/blkdev.h>
9#include <linux/freezer.h>
10#include <linux/scatterlist.h>
11#include <linux/dma-mapping.h>
12#include <linux/backing-dev.h>
13
14#include <linux/mmc/card.h>
15#include <linux/mmc/host.h>
16
17#include "queue.h"
18#include "block.h"
19#include "core.h"
20#include "card.h"
21#include "crypto.h"
22#include "host.h"
23
24#define MMC_DMA_MAP_MERGE_SEGMENTS 512
25
26static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
27{
28 /* Allow only 1 DCMD at a time */
29 return mq->in_flight[MMC_ISSUE_DCMD];
30}
31
32void mmc_cqe_check_busy(struct mmc_queue *mq)
33{
34 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
35 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
36}
37
38static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
39{
40 return host->caps2 & MMC_CAP2_CQE_DCMD;
41}
42
43static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
44 struct request *req)
45{
46 switch (req_op(req)) {
47 case REQ_OP_DRV_IN:
48 case REQ_OP_DRV_OUT:
49 case REQ_OP_DISCARD:
50 case REQ_OP_SECURE_ERASE:
51 case REQ_OP_WRITE_ZEROES:
52 return MMC_ISSUE_SYNC;
53 case REQ_OP_FLUSH:
54 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
55 default:
56 return MMC_ISSUE_ASYNC;
57 }
58}
59
60enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
61{
62 struct mmc_host *host = mq->card->host;
63
64 if (host->cqe_enabled && !host->hsq_enabled)
65 return mmc_cqe_issue_type(host, req);
66
67 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
68 return MMC_ISSUE_ASYNC;
69
70 return MMC_ISSUE_SYNC;
71}
72
73static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
74{
75 if (!mq->recovery_needed) {
76 mq->recovery_needed = true;
77 schedule_work(&mq->recovery_work);
78 }
79}
80
81void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
82{
83 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
84 brq.mrq);
85 struct request *req = mmc_queue_req_to_req(mqrq);
86 struct request_queue *q = req->q;
87 struct mmc_queue *mq = q->queuedata;
88 unsigned long flags;
89
90 spin_lock_irqsave(&mq->lock, flags);
91 __mmc_cqe_recovery_notifier(mq);
92 spin_unlock_irqrestore(&mq->lock, flags);
93}
94
95static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
96{
97 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
98 struct mmc_request *mrq = &mqrq->brq.mrq;
99 struct mmc_queue *mq = req->q->queuedata;
100 struct mmc_host *host = mq->card->host;
101 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
102 bool recovery_needed = false;
103
104 switch (issue_type) {
105 case MMC_ISSUE_ASYNC:
106 case MMC_ISSUE_DCMD:
107 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
108 if (recovery_needed)
109 mmc_cqe_recovery_notifier(mrq);
110 return BLK_EH_RESET_TIMER;
111 }
112 /* The request has gone already */
113 return BLK_EH_DONE;
114 default:
115 /* Timeout is handled by mmc core */
116 return BLK_EH_RESET_TIMER;
117 }
118}
119
120static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req)
121{
122 struct request_queue *q = req->q;
123 struct mmc_queue *mq = q->queuedata;
124 struct mmc_card *card = mq->card;
125 struct mmc_host *host = card->host;
126 unsigned long flags;
127 bool ignore_tout;
128
129 spin_lock_irqsave(&mq->lock, flags);
130 ignore_tout = mq->recovery_needed || !host->cqe_enabled || host->hsq_enabled;
131 spin_unlock_irqrestore(&mq->lock, flags);
132
133 return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
134}
135
136static void mmc_mq_recovery_handler(struct work_struct *work)
137{
138 struct mmc_queue *mq = container_of(work, struct mmc_queue,
139 recovery_work);
140 struct request_queue *q = mq->queue;
141 struct mmc_host *host = mq->card->host;
142
143 mmc_get_card(mq->card, &mq->ctx);
144
145 mq->in_recovery = true;
146
147 if (host->cqe_enabled && !host->hsq_enabled)
148 mmc_blk_cqe_recovery(mq);
149 else
150 mmc_blk_mq_recovery(mq);
151
152 mq->in_recovery = false;
153
154 spin_lock_irq(&mq->lock);
155 mq->recovery_needed = false;
156 spin_unlock_irq(&mq->lock);
157
158 if (host->hsq_enabled)
159 host->cqe_ops->cqe_recovery_finish(host);
160
161 mmc_put_card(mq->card, &mq->ctx);
162
163 blk_mq_run_hw_queues(q, true);
164}
165
166static struct scatterlist *mmc_alloc_sg(unsigned short sg_len, gfp_t gfp)
167{
168 struct scatterlist *sg;
169
170 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
171 if (sg)
172 sg_init_table(sg, sg_len);
173
174 return sg;
175}
176
177static void mmc_queue_setup_discard(struct request_queue *q,
178 struct mmc_card *card)
179{
180 unsigned max_discard;
181
182 max_discard = mmc_calc_max_discard(card);
183 if (!max_discard)
184 return;
185
186 blk_queue_max_discard_sectors(q, max_discard);
187 q->limits.discard_granularity = card->pref_erase << 9;
188 /* granularity must not be greater than max. discard */
189 if (card->pref_erase > max_discard)
190 q->limits.discard_granularity = SECTOR_SIZE;
191 if (mmc_can_secure_erase_trim(card))
192 blk_queue_max_secure_erase_sectors(q, max_discard);
193 if (mmc_can_trim(card) && card->erased_byte == 0)
194 blk_queue_max_write_zeroes_sectors(q, max_discard);
195}
196
197static unsigned short mmc_get_max_segments(struct mmc_host *host)
198{
199 return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS :
200 host->max_segs;
201}
202
203static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
204 unsigned int hctx_idx, unsigned int numa_node)
205{
206 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
207 struct mmc_queue *mq = set->driver_data;
208 struct mmc_card *card = mq->card;
209 struct mmc_host *host = card->host;
210
211 mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), GFP_KERNEL);
212 if (!mq_rq->sg)
213 return -ENOMEM;
214
215 return 0;
216}
217
218static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
219 unsigned int hctx_idx)
220{
221 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
222
223 kfree(mq_rq->sg);
224 mq_rq->sg = NULL;
225}
226
227static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
228 const struct blk_mq_queue_data *bd)
229{
230 struct request *req = bd->rq;
231 struct request_queue *q = req->q;
232 struct mmc_queue *mq = q->queuedata;
233 struct mmc_card *card = mq->card;
234 struct mmc_host *host = card->host;
235 enum mmc_issue_type issue_type;
236 enum mmc_issued issued;
237 bool get_card, cqe_retune_ok;
238 blk_status_t ret;
239
240 if (mmc_card_removed(mq->card)) {
241 req->rq_flags |= RQF_QUIET;
242 return BLK_STS_IOERR;
243 }
244
245 issue_type = mmc_issue_type(mq, req);
246
247 spin_lock_irq(&mq->lock);
248
249 if (mq->recovery_needed || mq->busy) {
250 spin_unlock_irq(&mq->lock);
251 return BLK_STS_RESOURCE;
252 }
253
254 switch (issue_type) {
255 case MMC_ISSUE_DCMD:
256 if (mmc_cqe_dcmd_busy(mq)) {
257 mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
258 spin_unlock_irq(&mq->lock);
259 return BLK_STS_RESOURCE;
260 }
261 break;
262 case MMC_ISSUE_ASYNC:
263 /*
264 * For MMC host software queue, we only allow 2 requests in
265 * flight to avoid a long latency.
266 */
267 if (host->hsq_enabled && mq->in_flight[issue_type] > 2) {
268 spin_unlock_irq(&mq->lock);
269 return BLK_STS_RESOURCE;
270 }
271 break;
272 default:
273 /*
274 * Timeouts are handled by mmc core, and we don't have a host
275 * API to abort requests, so we can't handle the timeout anyway.
276 * However, when the timeout happens, blk_mq_complete_request()
277 * no longer works (to stop the request disappearing under us).
278 * To avoid racing with that, set a large timeout.
279 */
280 req->timeout = 600 * HZ;
281 break;
282 }
283
284 /* Parallel dispatch of requests is not supported at the moment */
285 mq->busy = true;
286
287 mq->in_flight[issue_type] += 1;
288 get_card = (mmc_tot_in_flight(mq) == 1);
289 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
290
291 spin_unlock_irq(&mq->lock);
292
293 if (!(req->rq_flags & RQF_DONTPREP)) {
294 req_to_mmc_queue_req(req)->retries = 0;
295 req->rq_flags |= RQF_DONTPREP;
296 }
297
298 if (get_card)
299 mmc_get_card(card, &mq->ctx);
300
301 if (host->cqe_enabled) {
302 host->retune_now = host->need_retune && cqe_retune_ok &&
303 !host->hold_retune;
304 }
305
306 blk_mq_start_request(req);
307
308 issued = mmc_blk_mq_issue_rq(mq, req);
309
310 switch (issued) {
311 case MMC_REQ_BUSY:
312 ret = BLK_STS_RESOURCE;
313 break;
314 case MMC_REQ_FAILED_TO_START:
315 ret = BLK_STS_IOERR;
316 break;
317 default:
318 ret = BLK_STS_OK;
319 break;
320 }
321
322 if (issued != MMC_REQ_STARTED) {
323 bool put_card = false;
324
325 spin_lock_irq(&mq->lock);
326 mq->in_flight[issue_type] -= 1;
327 if (mmc_tot_in_flight(mq) == 0)
328 put_card = true;
329 mq->busy = false;
330 spin_unlock_irq(&mq->lock);
331 if (put_card)
332 mmc_put_card(card, &mq->ctx);
333 } else {
334 WRITE_ONCE(mq->busy, false);
335 }
336
337 return ret;
338}
339
340static const struct blk_mq_ops mmc_mq_ops = {
341 .queue_rq = mmc_mq_queue_rq,
342 .init_request = mmc_mq_init_request,
343 .exit_request = mmc_mq_exit_request,
344 .complete = mmc_blk_mq_complete,
345 .timeout = mmc_mq_timed_out,
346};
347
348static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
349{
350 struct mmc_host *host = card->host;
351 unsigned block_size = 512;
352
353 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
354 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
355 if (mmc_can_erase(card))
356 mmc_queue_setup_discard(mq->queue, card);
357
358 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask)
359 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
360 blk_queue_max_hw_sectors(mq->queue,
361 min(host->max_blk_count, host->max_req_size / 512));
362 if (host->can_dma_map_merge)
363 WARN(!blk_queue_can_use_dma_map_merging(mq->queue,
364 mmc_dev(host)),
365 "merging was advertised but not possible");
366 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host));
367
368 if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) {
369 block_size = card->ext_csd.data_sector_size;
370 WARN_ON(block_size != 512 && block_size != 4096);
371 }
372
373 blk_queue_logical_block_size(mq->queue, block_size);
374 /*
375 * After blk_queue_can_use_dma_map_merging() was called with succeed,
376 * since it calls blk_queue_virt_boundary(), the mmc should not call
377 * both blk_queue_max_segment_size().
378 */
379 if (!host->can_dma_map_merge)
380 blk_queue_max_segment_size(mq->queue,
381 round_down(host->max_seg_size, block_size));
382
383 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
384
385 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
386 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
387
388 mutex_init(&mq->complete_lock);
389
390 init_waitqueue_head(&mq->wait);
391
392 mmc_crypto_setup_queue(mq->queue, host);
393}
394
395static inline bool mmc_merge_capable(struct mmc_host *host)
396{
397 return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
398}
399
400/* Set queue depth to get a reasonable value for q->nr_requests */
401#define MMC_QUEUE_DEPTH 64
402
403/**
404 * mmc_init_queue - initialise a queue structure.
405 * @mq: mmc queue
406 * @card: mmc card to attach this queue
407 *
408 * Initialise a MMC card request queue.
409 */
410struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card)
411{
412 struct mmc_host *host = card->host;
413 struct gendisk *disk;
414 int ret;
415
416 mq->card = card;
417
418 spin_lock_init(&mq->lock);
419
420 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
421 mq->tag_set.ops = &mmc_mq_ops;
422 /*
423 * The queue depth for CQE must match the hardware because the request
424 * tag is used to index the hardware queue.
425 */
426 if (host->cqe_enabled && !host->hsq_enabled)
427 mq->tag_set.queue_depth =
428 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
429 else
430 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
431 mq->tag_set.numa_node = NUMA_NO_NODE;
432 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
433 mq->tag_set.nr_hw_queues = 1;
434 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
435 mq->tag_set.driver_data = mq;
436
437 /*
438 * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops,
439 * the host->can_dma_map_merge should be set before to get max_segs
440 * from mmc_get_max_segments().
441 */
442 if (mmc_merge_capable(host) &&
443 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
444 dma_get_merge_boundary(mmc_dev(host)))
445 host->can_dma_map_merge = 1;
446 else
447 host->can_dma_map_merge = 0;
448
449 ret = blk_mq_alloc_tag_set(&mq->tag_set);
450 if (ret)
451 return ERR_PTR(ret);
452
453
454 disk = blk_mq_alloc_disk(&mq->tag_set, mq);
455 if (IS_ERR(disk)) {
456 blk_mq_free_tag_set(&mq->tag_set);
457 return disk;
458 }
459 mq->queue = disk->queue;
460
461 if (mmc_host_is_spi(host) && host->use_spi_crc)
462 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, mq->queue);
463 blk_queue_rq_timeout(mq->queue, 60 * HZ);
464
465 mmc_setup_queue(mq, card);
466 return disk;
467}
468
469void mmc_queue_suspend(struct mmc_queue *mq)
470{
471 blk_mq_quiesce_queue(mq->queue);
472
473 /*
474 * The host remains claimed while there are outstanding requests, so
475 * simply claiming and releasing here ensures there are none.
476 */
477 mmc_claim_host(mq->card->host);
478 mmc_release_host(mq->card->host);
479}
480
481void mmc_queue_resume(struct mmc_queue *mq)
482{
483 blk_mq_unquiesce_queue(mq->queue);
484}
485
486void mmc_cleanup_queue(struct mmc_queue *mq)
487{
488 struct request_queue *q = mq->queue;
489
490 /*
491 * The legacy code handled the possibility of being suspended,
492 * so do that here too.
493 */
494 if (blk_queue_quiesced(q))
495 blk_mq_unquiesce_queue(q);
496
497 /*
498 * If the recovery completes the last (and only remaining) request in
499 * the queue, and the card has been removed, we could end up here with
500 * the recovery not quite finished yet, so cancel it.
501 */
502 cancel_work_sync(&mq->recovery_work);
503
504 blk_mq_free_tag_set(&mq->tag_set);
505
506 /*
507 * A request can be completed before the next request, potentially
508 * leaving a complete_work with nothing to do. Such a work item might
509 * still be queued at this point. Flush it.
510 */
511 flush_work(&mq->complete_work);
512
513 mq->card = NULL;
514}
515
516/*
517 * Prepare the sg list(s) to be handed of to the host driver
518 */
519unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
520{
521 struct request *req = mmc_queue_req_to_req(mqrq);
522
523 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
524}
1/*
2 * Copyright (C) 2003 Russell King, All Rights Reserved.
3 * Copyright 2006-2007 Pierre Ossman
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 */
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/blkdev.h>
13#include <linux/freezer.h>
14#include <linux/kthread.h>
15#include <linux/scatterlist.h>
16#include <linux/dma-mapping.h>
17
18#include <linux/mmc/card.h>
19#include <linux/mmc/host.h>
20
21#include "queue.h"
22#include "block.h"
23
24#define MMC_QUEUE_BOUNCESZ 65536
25
26/*
27 * Prepare a MMC request. This just filters out odd stuff.
28 */
29static int mmc_prep_request(struct request_queue *q, struct request *req)
30{
31 struct mmc_queue *mq = q->queuedata;
32
33 /*
34 * We only like normal block requests and discards.
35 */
36 if (req->cmd_type != REQ_TYPE_FS && req_op(req) != REQ_OP_DISCARD &&
37 req_op(req) != REQ_OP_SECURE_ERASE) {
38 blk_dump_rq_flags(req, "MMC bad request");
39 return BLKPREP_KILL;
40 }
41
42 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
43 return BLKPREP_KILL;
44
45 req->rq_flags |= RQF_DONTPREP;
46
47 return BLKPREP_OK;
48}
49
50static int mmc_queue_thread(void *d)
51{
52 struct mmc_queue *mq = d;
53 struct request_queue *q = mq->queue;
54 struct mmc_context_info *cntx = &mq->card->host->context_info;
55
56 current->flags |= PF_MEMALLOC;
57
58 down(&mq->thread_sem);
59 do {
60 struct request *req = NULL;
61
62 spin_lock_irq(q->queue_lock);
63 set_current_state(TASK_INTERRUPTIBLE);
64 req = blk_fetch_request(q);
65 mq->asleep = false;
66 cntx->is_waiting_last_req = false;
67 cntx->is_new_req = false;
68 if (!req) {
69 /*
70 * Dispatch queue is empty so set flags for
71 * mmc_request_fn() to wake us up.
72 */
73 if (mq->mqrq_prev->req)
74 cntx->is_waiting_last_req = true;
75 else
76 mq->asleep = true;
77 }
78 mq->mqrq_cur->req = req;
79 spin_unlock_irq(q->queue_lock);
80
81 if (req || mq->mqrq_prev->req) {
82 bool req_is_special = mmc_req_is_special(req);
83
84 set_current_state(TASK_RUNNING);
85 mmc_blk_issue_rq(mq, req);
86 cond_resched();
87 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
88 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
89 continue; /* fetch again */
90 }
91
92 /*
93 * Current request becomes previous request
94 * and vice versa.
95 * In case of special requests, current request
96 * has been finished. Do not assign it to previous
97 * request.
98 */
99 if (req_is_special)
100 mq->mqrq_cur->req = NULL;
101
102 mq->mqrq_prev->brq.mrq.data = NULL;
103 mq->mqrq_prev->req = NULL;
104 swap(mq->mqrq_prev, mq->mqrq_cur);
105 } else {
106 if (kthread_should_stop()) {
107 set_current_state(TASK_RUNNING);
108 break;
109 }
110 up(&mq->thread_sem);
111 schedule();
112 down(&mq->thread_sem);
113 }
114 } while (1);
115 up(&mq->thread_sem);
116
117 return 0;
118}
119
120/*
121 * Generic MMC request handler. This is called for any queue on a
122 * particular host. When the host is not busy, we look for a request
123 * on any queue on this host, and attempt to issue it. This may
124 * not be the queue we were asked to process.
125 */
126static void mmc_request_fn(struct request_queue *q)
127{
128 struct mmc_queue *mq = q->queuedata;
129 struct request *req;
130 struct mmc_context_info *cntx;
131
132 if (!mq) {
133 while ((req = blk_fetch_request(q)) != NULL) {
134 req->rq_flags |= RQF_QUIET;
135 __blk_end_request_all(req, -EIO);
136 }
137 return;
138 }
139
140 cntx = &mq->card->host->context_info;
141
142 if (cntx->is_waiting_last_req) {
143 cntx->is_new_req = true;
144 wake_up_interruptible(&cntx->wait);
145 }
146
147 if (mq->asleep)
148 wake_up_process(mq->thread);
149}
150
151static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
152{
153 struct scatterlist *sg;
154
155 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
156 if (!sg)
157 *err = -ENOMEM;
158 else {
159 *err = 0;
160 sg_init_table(sg, sg_len);
161 }
162
163 return sg;
164}
165
166static void mmc_queue_setup_discard(struct request_queue *q,
167 struct mmc_card *card)
168{
169 unsigned max_discard;
170
171 max_discard = mmc_calc_max_discard(card);
172 if (!max_discard)
173 return;
174
175 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
176 blk_queue_max_discard_sectors(q, max_discard);
177 if (card->erased_byte == 0 && !mmc_can_discard(card))
178 q->limits.discard_zeroes_data = 1;
179 q->limits.discard_granularity = card->pref_erase << 9;
180 /* granularity must not be greater than max. discard */
181 if (card->pref_erase > max_discard)
182 q->limits.discard_granularity = 0;
183 if (mmc_can_secure_erase_trim(card))
184 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
185}
186
187#ifdef CONFIG_MMC_BLOCK_BOUNCE
188static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
189 unsigned int bouncesz)
190{
191 int i;
192
193 for (i = 0; i < mq->qdepth; i++) {
194 mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
195 if (!mq->mqrq[i].bounce_buf)
196 goto out_err;
197 }
198
199 return true;
200
201out_err:
202 while (--i >= 0) {
203 kfree(mq->mqrq[i].bounce_buf);
204 mq->mqrq[i].bounce_buf = NULL;
205 }
206 pr_warn("%s: unable to allocate bounce buffers\n",
207 mmc_card_name(mq->card));
208 return false;
209}
210
211static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
212 unsigned int bouncesz)
213{
214 int i, ret;
215
216 for (i = 0; i < mq->qdepth; i++) {
217 mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
218 if (ret)
219 return ret;
220
221 mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
222 if (ret)
223 return ret;
224 }
225
226 return 0;
227}
228#endif
229
230static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
231{
232 int i, ret;
233
234 for (i = 0; i < mq->qdepth; i++) {
235 mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
236 if (ret)
237 return ret;
238 }
239
240 return 0;
241}
242
243static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
244{
245 kfree(mqrq->bounce_sg);
246 mqrq->bounce_sg = NULL;
247
248 kfree(mqrq->sg);
249 mqrq->sg = NULL;
250
251 kfree(mqrq->bounce_buf);
252 mqrq->bounce_buf = NULL;
253}
254
255static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
256{
257 int i;
258
259 for (i = 0; i < mq->qdepth; i++)
260 mmc_queue_req_free_bufs(&mq->mqrq[i]);
261}
262
263/**
264 * mmc_init_queue - initialise a queue structure.
265 * @mq: mmc queue
266 * @card: mmc card to attach this queue
267 * @lock: queue lock
268 * @subname: partition subname
269 *
270 * Initialise a MMC card request queue.
271 */
272int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
273 spinlock_t *lock, const char *subname)
274{
275 struct mmc_host *host = card->host;
276 u64 limit = BLK_BOUNCE_HIGH;
277 bool bounce = false;
278 int ret = -ENOMEM;
279
280 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
281 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
282
283 mq->card = card;
284 mq->queue = blk_init_queue(mmc_request_fn, lock);
285 if (!mq->queue)
286 return -ENOMEM;
287
288 mq->qdepth = 2;
289 mq->mqrq = kcalloc(mq->qdepth, sizeof(struct mmc_queue_req),
290 GFP_KERNEL);
291 if (!mq->mqrq)
292 goto blk_cleanup;
293 mq->mqrq_cur = &mq->mqrq[0];
294 mq->mqrq_prev = &mq->mqrq[1];
295 mq->queue->queuedata = mq;
296
297 blk_queue_prep_rq(mq->queue, mmc_prep_request);
298 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
299 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
300 if (mmc_can_erase(card))
301 mmc_queue_setup_discard(mq->queue, card);
302
303#ifdef CONFIG_MMC_BLOCK_BOUNCE
304 if (host->max_segs == 1) {
305 unsigned int bouncesz;
306
307 bouncesz = MMC_QUEUE_BOUNCESZ;
308
309 if (bouncesz > host->max_req_size)
310 bouncesz = host->max_req_size;
311 if (bouncesz > host->max_seg_size)
312 bouncesz = host->max_seg_size;
313 if (bouncesz > (host->max_blk_count * 512))
314 bouncesz = host->max_blk_count * 512;
315
316 if (bouncesz > 512 &&
317 mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
318 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
319 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
320 blk_queue_max_segments(mq->queue, bouncesz / 512);
321 blk_queue_max_segment_size(mq->queue, bouncesz);
322
323 ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
324 if (ret)
325 goto cleanup_queue;
326 bounce = true;
327 }
328 }
329#endif
330
331 if (!bounce) {
332 blk_queue_bounce_limit(mq->queue, limit);
333 blk_queue_max_hw_sectors(mq->queue,
334 min(host->max_blk_count, host->max_req_size / 512));
335 blk_queue_max_segments(mq->queue, host->max_segs);
336 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
337
338 ret = mmc_queue_alloc_sgs(mq, host->max_segs);
339 if (ret)
340 goto cleanup_queue;
341 }
342
343 sema_init(&mq->thread_sem, 1);
344
345 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
346 host->index, subname ? subname : "");
347
348 if (IS_ERR(mq->thread)) {
349 ret = PTR_ERR(mq->thread);
350 goto cleanup_queue;
351 }
352
353 return 0;
354
355 cleanup_queue:
356 mmc_queue_reqs_free_bufs(mq);
357 kfree(mq->mqrq);
358 mq->mqrq = NULL;
359blk_cleanup:
360 blk_cleanup_queue(mq->queue);
361 return ret;
362}
363
364void mmc_cleanup_queue(struct mmc_queue *mq)
365{
366 struct request_queue *q = mq->queue;
367 unsigned long flags;
368
369 /* Make sure the queue isn't suspended, as that will deadlock */
370 mmc_queue_resume(mq);
371
372 /* Then terminate our worker thread */
373 kthread_stop(mq->thread);
374
375 /* Empty the queue */
376 spin_lock_irqsave(q->queue_lock, flags);
377 q->queuedata = NULL;
378 blk_start_queue(q);
379 spin_unlock_irqrestore(q->queue_lock, flags);
380
381 mmc_queue_reqs_free_bufs(mq);
382 kfree(mq->mqrq);
383 mq->mqrq = NULL;
384
385 mq->card = NULL;
386}
387EXPORT_SYMBOL(mmc_cleanup_queue);
388
389/**
390 * mmc_queue_suspend - suspend a MMC request queue
391 * @mq: MMC queue to suspend
392 *
393 * Stop the block request queue, and wait for our thread to
394 * complete any outstanding requests. This ensures that we
395 * won't suspend while a request is being processed.
396 */
397void mmc_queue_suspend(struct mmc_queue *mq)
398{
399 struct request_queue *q = mq->queue;
400 unsigned long flags;
401
402 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
403 mq->flags |= MMC_QUEUE_SUSPENDED;
404
405 spin_lock_irqsave(q->queue_lock, flags);
406 blk_stop_queue(q);
407 spin_unlock_irqrestore(q->queue_lock, flags);
408
409 down(&mq->thread_sem);
410 }
411}
412
413/**
414 * mmc_queue_resume - resume a previously suspended MMC request queue
415 * @mq: MMC queue to resume
416 */
417void mmc_queue_resume(struct mmc_queue *mq)
418{
419 struct request_queue *q = mq->queue;
420 unsigned long flags;
421
422 if (mq->flags & MMC_QUEUE_SUSPENDED) {
423 mq->flags &= ~MMC_QUEUE_SUSPENDED;
424
425 up(&mq->thread_sem);
426
427 spin_lock_irqsave(q->queue_lock, flags);
428 blk_start_queue(q);
429 spin_unlock_irqrestore(q->queue_lock, flags);
430 }
431}
432
433/*
434 * Prepare the sg list(s) to be handed of to the host driver
435 */
436unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
437{
438 unsigned int sg_len;
439 size_t buflen;
440 struct scatterlist *sg;
441 int i;
442
443 if (!mqrq->bounce_buf)
444 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
445
446 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
447
448 mqrq->bounce_sg_len = sg_len;
449
450 buflen = 0;
451 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
452 buflen += sg->length;
453
454 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
455
456 return 1;
457}
458
459/*
460 * If writing, bounce the data to the buffer before the request
461 * is sent to the host driver
462 */
463void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
464{
465 if (!mqrq->bounce_buf)
466 return;
467
468 if (rq_data_dir(mqrq->req) != WRITE)
469 return;
470
471 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
472 mqrq->bounce_buf, mqrq->sg[0].length);
473}
474
475/*
476 * If reading, bounce the data from the buffer after the request
477 * has been handled by the host driver
478 */
479void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
480{
481 if (!mqrq->bounce_buf)
482 return;
483
484 if (rq_data_dir(mqrq->req) != READ)
485 return;
486
487 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
488 mqrq->bounce_buf, mqrq->sg[0].length);
489}