]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/mmc/card/queue.c
block: add a separate operation type for secure erase
[mirror_ubuntu-bionic-kernel.git] / drivers / mmc / card / queue.c
1 /*
2 * linux/drivers/mmc/card/queue.c
3 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2006-2007 Pierre Ossman
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/freezer.h>
16 #include <linux/kthread.h>
17 #include <linux/scatterlist.h>
18 #include <linux/dma-mapping.h>
19
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/host.h>
22 #include "queue.h"
23
24 #define MMC_QUEUE_BOUNCESZ 65536
25
26 /*
27 * Prepare a MMC request. This just filters out odd stuff.
28 */
29 static 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 blk_dump_rq_flags(req, "MMC bad request");
38 return BLKPREP_KILL;
39 }
40
41 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
42 return BLKPREP_KILL;
43
44 req->cmd_flags |= REQ_DONTPREP;
45
46 return BLKPREP_OK;
47 }
48
49 static int mmc_queue_thread(void *d)
50 {
51 struct mmc_queue *mq = d;
52 struct request_queue *q = mq->queue;
53
54 current->flags |= PF_MEMALLOC;
55
56 down(&mq->thread_sem);
57 do {
58 struct request *req = NULL;
59
60 spin_lock_irq(q->queue_lock);
61 set_current_state(TASK_INTERRUPTIBLE);
62 req = blk_fetch_request(q);
63 mq->mqrq_cur->req = req;
64 spin_unlock_irq(q->queue_lock);
65
66 if (req || mq->mqrq_prev->req) {
67 set_current_state(TASK_RUNNING);
68 mq->issue_fn(mq, req);
69 cond_resched();
70 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
71 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
72 continue; /* fetch again */
73 }
74
75 /*
76 * Current request becomes previous request
77 * and vice versa.
78 * In case of special requests, current request
79 * has been finished. Do not assign it to previous
80 * request.
81 */
82 if (mmc_req_is_special(req))
83 mq->mqrq_cur->req = NULL;
84
85 mq->mqrq_prev->brq.mrq.data = NULL;
86 mq->mqrq_prev->req = NULL;
87 swap(mq->mqrq_prev, mq->mqrq_cur);
88 } else {
89 if (kthread_should_stop()) {
90 set_current_state(TASK_RUNNING);
91 break;
92 }
93 up(&mq->thread_sem);
94 schedule();
95 down(&mq->thread_sem);
96 }
97 } while (1);
98 up(&mq->thread_sem);
99
100 return 0;
101 }
102
103 /*
104 * Generic MMC request handler. This is called for any queue on a
105 * particular host. When the host is not busy, we look for a request
106 * on any queue on this host, and attempt to issue it. This may
107 * not be the queue we were asked to process.
108 */
109 static void mmc_request_fn(struct request_queue *q)
110 {
111 struct mmc_queue *mq = q->queuedata;
112 struct request *req;
113 unsigned long flags;
114 struct mmc_context_info *cntx;
115
116 if (!mq) {
117 while ((req = blk_fetch_request(q)) != NULL) {
118 req->cmd_flags |= REQ_QUIET;
119 __blk_end_request_all(req, -EIO);
120 }
121 return;
122 }
123
124 cntx = &mq->card->host->context_info;
125 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
126 /*
127 * New MMC request arrived when MMC thread may be
128 * blocked on the previous request to be complete
129 * with no current request fetched
130 */
131 spin_lock_irqsave(&cntx->lock, flags);
132 if (cntx->is_waiting_last_req) {
133 cntx->is_new_req = true;
134 wake_up_interruptible(&cntx->wait);
135 }
136 spin_unlock_irqrestore(&cntx->lock, flags);
137 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
138 wake_up_process(mq->thread);
139 }
140
141 static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
142 {
143 struct scatterlist *sg;
144
145 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
146 if (!sg)
147 *err = -ENOMEM;
148 else {
149 *err = 0;
150 sg_init_table(sg, sg_len);
151 }
152
153 return sg;
154 }
155
156 static void mmc_queue_setup_discard(struct request_queue *q,
157 struct mmc_card *card)
158 {
159 unsigned max_discard;
160
161 max_discard = mmc_calc_max_discard(card);
162 if (!max_discard)
163 return;
164
165 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
166 blk_queue_max_discard_sectors(q, max_discard);
167 if (card->erased_byte == 0 && !mmc_can_discard(card))
168 q->limits.discard_zeroes_data = 1;
169 q->limits.discard_granularity = card->pref_erase << 9;
170 /* granularity must not be greater than max. discard */
171 if (card->pref_erase > max_discard)
172 q->limits.discard_granularity = 0;
173 if (mmc_can_secure_erase_trim(card))
174 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
175 }
176
177 /**
178 * mmc_init_queue - initialise a queue structure.
179 * @mq: mmc queue
180 * @card: mmc card to attach this queue
181 * @lock: queue lock
182 * @subname: partition subname
183 *
184 * Initialise a MMC card request queue.
185 */
186 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
187 spinlock_t *lock, const char *subname)
188 {
189 struct mmc_host *host = card->host;
190 u64 limit = BLK_BOUNCE_HIGH;
191 int ret;
192 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
193 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
194
195 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
196 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
197
198 mq->card = card;
199 mq->queue = blk_init_queue(mmc_request_fn, lock);
200 if (!mq->queue)
201 return -ENOMEM;
202
203 mq->mqrq_cur = mqrq_cur;
204 mq->mqrq_prev = mqrq_prev;
205 mq->queue->queuedata = mq;
206
207 blk_queue_prep_rq(mq->queue, mmc_prep_request);
208 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
209 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
210 if (mmc_can_erase(card))
211 mmc_queue_setup_discard(mq->queue, card);
212
213 #ifdef CONFIG_MMC_BLOCK_BOUNCE
214 if (host->max_segs == 1) {
215 unsigned int bouncesz;
216
217 bouncesz = MMC_QUEUE_BOUNCESZ;
218
219 if (bouncesz > host->max_req_size)
220 bouncesz = host->max_req_size;
221 if (bouncesz > host->max_seg_size)
222 bouncesz = host->max_seg_size;
223 if (bouncesz > (host->max_blk_count * 512))
224 bouncesz = host->max_blk_count * 512;
225
226 if (bouncesz > 512) {
227 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
228 if (!mqrq_cur->bounce_buf) {
229 pr_warn("%s: unable to allocate bounce cur buffer\n",
230 mmc_card_name(card));
231 } else {
232 mqrq_prev->bounce_buf =
233 kmalloc(bouncesz, GFP_KERNEL);
234 if (!mqrq_prev->bounce_buf) {
235 pr_warn("%s: unable to allocate bounce prev buffer\n",
236 mmc_card_name(card));
237 kfree(mqrq_cur->bounce_buf);
238 mqrq_cur->bounce_buf = NULL;
239 }
240 }
241 }
242
243 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
244 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
245 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
246 blk_queue_max_segments(mq->queue, bouncesz / 512);
247 blk_queue_max_segment_size(mq->queue, bouncesz);
248
249 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
250 if (ret)
251 goto cleanup_queue;
252
253 mqrq_cur->bounce_sg =
254 mmc_alloc_sg(bouncesz / 512, &ret);
255 if (ret)
256 goto cleanup_queue;
257
258 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
259 if (ret)
260 goto cleanup_queue;
261
262 mqrq_prev->bounce_sg =
263 mmc_alloc_sg(bouncesz / 512, &ret);
264 if (ret)
265 goto cleanup_queue;
266 }
267 }
268 #endif
269
270 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
271 blk_queue_bounce_limit(mq->queue, limit);
272 blk_queue_max_hw_sectors(mq->queue,
273 min(host->max_blk_count, host->max_req_size / 512));
274 blk_queue_max_segments(mq->queue, host->max_segs);
275 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
276
277 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
278 if (ret)
279 goto cleanup_queue;
280
281
282 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
283 if (ret)
284 goto cleanup_queue;
285 }
286
287 sema_init(&mq->thread_sem, 1);
288
289 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
290 host->index, subname ? subname : "");
291
292 if (IS_ERR(mq->thread)) {
293 ret = PTR_ERR(mq->thread);
294 goto free_bounce_sg;
295 }
296
297 return 0;
298 free_bounce_sg:
299 kfree(mqrq_cur->bounce_sg);
300 mqrq_cur->bounce_sg = NULL;
301 kfree(mqrq_prev->bounce_sg);
302 mqrq_prev->bounce_sg = NULL;
303
304 cleanup_queue:
305 kfree(mqrq_cur->sg);
306 mqrq_cur->sg = NULL;
307 kfree(mqrq_cur->bounce_buf);
308 mqrq_cur->bounce_buf = NULL;
309
310 kfree(mqrq_prev->sg);
311 mqrq_prev->sg = NULL;
312 kfree(mqrq_prev->bounce_buf);
313 mqrq_prev->bounce_buf = NULL;
314
315 blk_cleanup_queue(mq->queue);
316 return ret;
317 }
318
319 void mmc_cleanup_queue(struct mmc_queue *mq)
320 {
321 struct request_queue *q = mq->queue;
322 unsigned long flags;
323 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
324 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
325
326 /* Make sure the queue isn't suspended, as that will deadlock */
327 mmc_queue_resume(mq);
328
329 /* Then terminate our worker thread */
330 kthread_stop(mq->thread);
331
332 /* Empty the queue */
333 spin_lock_irqsave(q->queue_lock, flags);
334 q->queuedata = NULL;
335 blk_start_queue(q);
336 spin_unlock_irqrestore(q->queue_lock, flags);
337
338 kfree(mqrq_cur->bounce_sg);
339 mqrq_cur->bounce_sg = NULL;
340
341 kfree(mqrq_cur->sg);
342 mqrq_cur->sg = NULL;
343
344 kfree(mqrq_cur->bounce_buf);
345 mqrq_cur->bounce_buf = NULL;
346
347 kfree(mqrq_prev->bounce_sg);
348 mqrq_prev->bounce_sg = NULL;
349
350 kfree(mqrq_prev->sg);
351 mqrq_prev->sg = NULL;
352
353 kfree(mqrq_prev->bounce_buf);
354 mqrq_prev->bounce_buf = NULL;
355
356 mq->card = NULL;
357 }
358 EXPORT_SYMBOL(mmc_cleanup_queue);
359
360 int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card)
361 {
362 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
363 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
364 int ret = 0;
365
366
367 mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
368 if (!mqrq_cur->packed) {
369 pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n",
370 mmc_card_name(card));
371 ret = -ENOMEM;
372 goto out;
373 }
374
375 mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
376 if (!mqrq_prev->packed) {
377 pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n",
378 mmc_card_name(card));
379 kfree(mqrq_cur->packed);
380 mqrq_cur->packed = NULL;
381 ret = -ENOMEM;
382 goto out;
383 }
384
385 INIT_LIST_HEAD(&mqrq_cur->packed->list);
386 INIT_LIST_HEAD(&mqrq_prev->packed->list);
387
388 out:
389 return ret;
390 }
391
392 void mmc_packed_clean(struct mmc_queue *mq)
393 {
394 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
395 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
396
397 kfree(mqrq_cur->packed);
398 mqrq_cur->packed = NULL;
399 kfree(mqrq_prev->packed);
400 mqrq_prev->packed = NULL;
401 }
402
403 /**
404 * mmc_queue_suspend - suspend a MMC request queue
405 * @mq: MMC queue to suspend
406 *
407 * Stop the block request queue, and wait for our thread to
408 * complete any outstanding requests. This ensures that we
409 * won't suspend while a request is being processed.
410 */
411 void mmc_queue_suspend(struct mmc_queue *mq)
412 {
413 struct request_queue *q = mq->queue;
414 unsigned long flags;
415
416 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
417 mq->flags |= MMC_QUEUE_SUSPENDED;
418
419 spin_lock_irqsave(q->queue_lock, flags);
420 blk_stop_queue(q);
421 spin_unlock_irqrestore(q->queue_lock, flags);
422
423 down(&mq->thread_sem);
424 }
425 }
426
427 /**
428 * mmc_queue_resume - resume a previously suspended MMC request queue
429 * @mq: MMC queue to resume
430 */
431 void mmc_queue_resume(struct mmc_queue *mq)
432 {
433 struct request_queue *q = mq->queue;
434 unsigned long flags;
435
436 if (mq->flags & MMC_QUEUE_SUSPENDED) {
437 mq->flags &= ~MMC_QUEUE_SUSPENDED;
438
439 up(&mq->thread_sem);
440
441 spin_lock_irqsave(q->queue_lock, flags);
442 blk_start_queue(q);
443 spin_unlock_irqrestore(q->queue_lock, flags);
444 }
445 }
446
447 static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
448 struct mmc_packed *packed,
449 struct scatterlist *sg,
450 enum mmc_packed_type cmd_type)
451 {
452 struct scatterlist *__sg = sg;
453 unsigned int sg_len = 0;
454 struct request *req;
455
456 if (mmc_packed_wr(cmd_type)) {
457 unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512;
458 unsigned int max_seg_sz = queue_max_segment_size(mq->queue);
459 unsigned int len, remain, offset = 0;
460 u8 *buf = (u8 *)packed->cmd_hdr;
461
462 remain = hdr_sz;
463 do {
464 len = min(remain, max_seg_sz);
465 sg_set_buf(__sg, buf + offset, len);
466 offset += len;
467 remain -= len;
468 sg_unmark_end(__sg++);
469 sg_len++;
470 } while (remain);
471 }
472
473 list_for_each_entry(req, &packed->list, queuelist) {
474 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
475 __sg = sg + (sg_len - 1);
476 sg_unmark_end(__sg++);
477 }
478 sg_mark_end(sg + (sg_len - 1));
479 return sg_len;
480 }
481
482 /*
483 * Prepare the sg list(s) to be handed of to the host driver
484 */
485 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
486 {
487 unsigned int sg_len;
488 size_t buflen;
489 struct scatterlist *sg;
490 enum mmc_packed_type cmd_type;
491 int i;
492
493 cmd_type = mqrq->cmd_type;
494
495 if (!mqrq->bounce_buf) {
496 if (mmc_packed_cmd(cmd_type))
497 return mmc_queue_packed_map_sg(mq, mqrq->packed,
498 mqrq->sg, cmd_type);
499 else
500 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
501 }
502
503 BUG_ON(!mqrq->bounce_sg);
504
505 if (mmc_packed_cmd(cmd_type))
506 sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed,
507 mqrq->bounce_sg, cmd_type);
508 else
509 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
510
511 mqrq->bounce_sg_len = sg_len;
512
513 buflen = 0;
514 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
515 buflen += sg->length;
516
517 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
518
519 return 1;
520 }
521
522 /*
523 * If writing, bounce the data to the buffer before the request
524 * is sent to the host driver
525 */
526 void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
527 {
528 if (!mqrq->bounce_buf)
529 return;
530
531 if (rq_data_dir(mqrq->req) != WRITE)
532 return;
533
534 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
535 mqrq->bounce_buf, mqrq->sg[0].length);
536 }
537
538 /*
539 * If reading, bounce the data from the buffer after the request
540 * has been handled by the host driver
541 */
542 void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
543 {
544 if (!mqrq->bounce_buf)
545 return;
546
547 if (rq_data_dir(mqrq->req) != READ)
548 return;
549
550 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
551 mqrq->bounce_buf, mqrq->sg[0].length);
552 }