• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/sched/rt.h>
21 #include <uapi/linux/sched/types.h>
22 
23 #include "queue.h"
24 #include "block.h"
25 #include "core.h"
26 #include "card.h"
27 
28 /*
29  * Prepare a MMC request. This just filters out odd stuff.
30  */
mmc_prep_request(struct request_queue * q,struct request * req)31 static int mmc_prep_request(struct request_queue *q, struct request *req)
32 {
33 	struct mmc_queue *mq = q->queuedata;
34 
35 	if (mq && mmc_card_removed(mq->card))
36 		return BLKPREP_KILL;
37 
38 	req->rq_flags |= RQF_DONTPREP;
39 
40 	return BLKPREP_OK;
41 }
42 
mmc_queue_thread(void * d)43 static int mmc_queue_thread(void *d)
44 {
45 	struct mmc_queue *mq = d;
46 	struct request_queue *q = mq->queue;
47 	struct mmc_context_info *cntx = &mq->card->host->context_info;
48 	struct sched_param scheduler_params = {0};
49 
50 	scheduler_params.sched_priority = 1;
51 
52 	sched_setscheduler(current, SCHED_FIFO, &scheduler_params);
53 
54 	current->flags |= PF_MEMALLOC;
55 
56 	down(&mq->thread_sem);
57 	do {
58 		struct request *req;
59 
60 		spin_lock_irq(q->queue_lock);
61 		set_current_state(TASK_INTERRUPTIBLE);
62 		req = blk_fetch_request(q);
63 		mq->asleep = false;
64 		cntx->is_waiting_last_req = false;
65 		cntx->is_new_req = false;
66 		if (!req) {
67 			/*
68 			 * Dispatch queue is empty so set flags for
69 			 * mmc_request_fn() to wake us up.
70 			 */
71 			if (mq->qcnt)
72 				cntx->is_waiting_last_req = true;
73 			else
74 				mq->asleep = true;
75 		}
76 		spin_unlock_irq(q->queue_lock);
77 
78 		if (req || mq->qcnt) {
79 			set_current_state(TASK_RUNNING);
80 			mmc_blk_issue_rq(mq, req);
81 			cond_resched();
82 		} else {
83 			if (kthread_should_stop()) {
84 				set_current_state(TASK_RUNNING);
85 				break;
86 			}
87 			up(&mq->thread_sem);
88 			schedule();
89 			down(&mq->thread_sem);
90 		}
91 	} while (1);
92 	up(&mq->thread_sem);
93 
94 	return 0;
95 }
96 
97 /*
98  * Generic MMC request handler.  This is called for any queue on a
99  * particular host.  When the host is not busy, we look for a request
100  * on any queue on this host, and attempt to issue it.  This may
101  * not be the queue we were asked to process.
102  */
mmc_request_fn(struct request_queue * q)103 static void mmc_request_fn(struct request_queue *q)
104 {
105 	struct mmc_queue *mq = q->queuedata;
106 	struct request *req;
107 	struct mmc_context_info *cntx;
108 
109 	if (!mq) {
110 		while ((req = blk_fetch_request(q)) != NULL) {
111 			req->rq_flags |= RQF_QUIET;
112 			__blk_end_request_all(req, BLK_STS_IOERR);
113 		}
114 		return;
115 	}
116 
117 	cntx = &mq->card->host->context_info;
118 
119 	if (cntx->is_waiting_last_req) {
120 		cntx->is_new_req = true;
121 		wake_up_interruptible(&cntx->wait);
122 	}
123 
124 	if (mq->asleep)
125 		wake_up_process(mq->thread);
126 }
127 
mmc_alloc_sg(int sg_len,gfp_t gfp)128 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
129 {
130 	struct scatterlist *sg;
131 
132 	sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
133 	if (sg)
134 		sg_init_table(sg, sg_len);
135 
136 	return sg;
137 }
138 
mmc_queue_setup_discard(struct request_queue * q,struct mmc_card * card)139 static void mmc_queue_setup_discard(struct request_queue *q,
140 				    struct mmc_card *card)
141 {
142 	unsigned max_discard;
143 
144 	max_discard = mmc_calc_max_discard(card);
145 	if (!max_discard)
146 		return;
147 
148 	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
149 	blk_queue_max_discard_sectors(q, max_discard);
150 	q->limits.discard_granularity = card->pref_erase << 9;
151 	/* granularity must not be greater than max. discard */
152 	if (card->pref_erase > max_discard)
153 		q->limits.discard_granularity = 0;
154 	if (mmc_can_secure_erase_trim(card))
155 		queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
156 }
157 
158 /**
159  * mmc_init_request() - initialize the MMC-specific per-request data
160  * @q: the request queue
161  * @req: the request
162  * @gfp: memory allocation policy
163  */
mmc_init_request(struct request_queue * q,struct request * req,gfp_t gfp)164 static int mmc_init_request(struct request_queue *q, struct request *req,
165 			    gfp_t gfp)
166 {
167 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
168 	struct mmc_queue *mq = q->queuedata;
169 	struct mmc_card *card = mq->card;
170 	struct mmc_host *host = card->host;
171 
172 	mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
173 	if (!mq_rq->sg)
174 		return -ENOMEM;
175 
176 	return 0;
177 }
178 
mmc_exit_request(struct request_queue * q,struct request * req)179 static void mmc_exit_request(struct request_queue *q, struct request *req)
180 {
181 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
182 
183 	kfree(mq_rq->sg);
184 	mq_rq->sg = NULL;
185 }
186 
187 /**
188  * mmc_init_queue - initialise a queue structure.
189  * @mq: mmc queue
190  * @card: mmc card to attach this queue
191  * @lock: queue lock
192  * @subname: partition subname
193  *
194  * Initialise a MMC card request queue.
195  */
mmc_init_queue(struct mmc_queue * mq,struct mmc_card * card,spinlock_t * lock,const char * subname)196 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
197 		   spinlock_t *lock, const char *subname)
198 {
199 	struct mmc_host *host = card->host;
200 	u64 limit = BLK_BOUNCE_HIGH;
201 	int ret = -ENOMEM;
202 
203 	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
204 		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
205 
206 	mq->card = card;
207 	mq->queue = blk_alloc_queue(GFP_KERNEL);
208 	if (!mq->queue)
209 		return -ENOMEM;
210 	mq->queue->queue_lock = lock;
211 	mq->queue->request_fn = mmc_request_fn;
212 	mq->queue->init_rq_fn = mmc_init_request;
213 	mq->queue->exit_rq_fn = mmc_exit_request;
214 	mq->queue->cmd_size = sizeof(struct mmc_queue_req);
215 	mq->queue->queuedata = mq;
216 	mq->qcnt = 0;
217 	ret = blk_init_allocated_queue(mq->queue);
218 	if (ret) {
219 		blk_cleanup_queue(mq->queue);
220 		return ret;
221 	}
222 
223 	blk_queue_prep_rq(mq->queue, mmc_prep_request);
224 	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
225 	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
226 	if (mmc_can_erase(card))
227 		mmc_queue_setup_discard(mq->queue, card);
228 
229 	blk_queue_bounce_limit(mq->queue, limit);
230 	blk_queue_max_hw_sectors(mq->queue,
231 		min(host->max_blk_count, host->max_req_size / 512));
232 	blk_queue_max_segments(mq->queue, host->max_segs);
233 	blk_queue_max_segment_size(mq->queue, host->max_seg_size);
234 
235 	sema_init(&mq->thread_sem, 1);
236 
237 	mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
238 		host->index, subname ? subname : "");
239 
240 	if (IS_ERR(mq->thread)) {
241 		ret = PTR_ERR(mq->thread);
242 		goto cleanup_queue;
243 	}
244 
245 	return 0;
246 
247 cleanup_queue:
248 	blk_cleanup_queue(mq->queue);
249 	return ret;
250 }
251 
mmc_cleanup_queue(struct mmc_queue * mq)252 void mmc_cleanup_queue(struct mmc_queue *mq)
253 {
254 	struct request_queue *q = mq->queue;
255 	unsigned long flags;
256 
257 	/* Make sure the queue isn't suspended, as that will deadlock */
258 	mmc_queue_resume(mq);
259 
260 	/* Then terminate our worker thread */
261 	kthread_stop(mq->thread);
262 
263 	/* Empty the queue */
264 	spin_lock_irqsave(q->queue_lock, flags);
265 	q->queuedata = NULL;
266 	blk_start_queue(q);
267 	spin_unlock_irqrestore(q->queue_lock, flags);
268 
269 	mq->card = NULL;
270 }
271 EXPORT_SYMBOL(mmc_cleanup_queue);
272 
273 /**
274  * mmc_queue_suspend - suspend a MMC request queue
275  * @mq: MMC queue to suspend
276  *
277  * Stop the block request queue, and wait for our thread to
278  * complete any outstanding requests.  This ensures that we
279  * won't suspend while a request is being processed.
280  */
mmc_queue_suspend(struct mmc_queue * mq)281 void mmc_queue_suspend(struct mmc_queue *mq)
282 {
283 	struct request_queue *q = mq->queue;
284 	unsigned long flags;
285 
286 	if (!mq->suspended) {
287 		mq->suspended |= true;
288 
289 		spin_lock_irqsave(q->queue_lock, flags);
290 		blk_stop_queue(q);
291 		spin_unlock_irqrestore(q->queue_lock, flags);
292 
293 		down(&mq->thread_sem);
294 	}
295 }
296 
297 /**
298  * mmc_queue_resume - resume a previously suspended MMC request queue
299  * @mq: MMC queue to resume
300  */
mmc_queue_resume(struct mmc_queue * mq)301 void mmc_queue_resume(struct mmc_queue *mq)
302 {
303 	struct request_queue *q = mq->queue;
304 	unsigned long flags;
305 
306 	if (mq->suspended) {
307 		mq->suspended = false;
308 
309 		up(&mq->thread_sem);
310 
311 		spin_lock_irqsave(q->queue_lock, flags);
312 		blk_start_queue(q);
313 		spin_unlock_irqrestore(q->queue_lock, flags);
314 	}
315 }
316 
317 /*
318  * Prepare the sg list(s) to be handed of to the host driver
319  */
mmc_queue_map_sg(struct mmc_queue * mq,struct mmc_queue_req * mqrq)320 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
321 {
322 	struct request *req = mmc_queue_req_to_req(mqrq);
323 
324 	return blk_rq_map_sg(mq->queue, req, mqrq->sg);
325 }
326