cregit-Linux how code gets into the kernel

Release 4.7 drivers/mmc/card/queue.c

Directory: drivers/mmc/card
/*
 *  linux/drivers/mmc/card/queue.c
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
 *  Copyright 2006-2007 Pierre Ossman
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/blkdev.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/scatterlist.h>
#include <linux/dma-mapping.h>

#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
#include "queue.h"


#define MMC_QUEUE_BOUNCESZ	65536

/*
 * Prepare a MMC request. This just filters out odd stuff.
 */

static int mmc_prep_request(struct request_queue *q, struct request *req) { struct mmc_queue *mq = q->queuedata; /* * We only like normal block requests and discards. */ if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) { blk_dump_rq_flags(req, "MMC bad request"); return BLKPREP_KILL; } if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq))) return BLKPREP_KILL; req->cmd_flags |= REQ_DONTPREP; return BLKPREP_OK; }

Contributors

PersonTokensPropCommitsCommitProp
russell kingrussell king3642.35%114.29%
sujit reddy thummasujit reddy thumma2327.06%114.29%
adrian hunteradrian hunter1011.76%114.29%
chuanxiao dongchuanxiao dong78.24%114.29%
pierre ossmanpierre ossman44.71%114.29%
christoph hellwigchristoph hellwig44.71%114.29%
jens axboejens axboe11.18%114.29%
Total85100.00%7100.00%


static int mmc_queue_thread(void *d) { struct mmc_queue *mq = d; struct request_queue *q = mq->queue; current->flags |= PF_MEMALLOC; down(&mq->thread_sem); do { struct request *req = NULL; unsigned int cmd_flags = 0; spin_lock_irq(q->queue_lock); set_current_state(TASK_INTERRUPTIBLE); req = blk_fetch_request(q); mq->mqrq_cur->req = req; spin_unlock_irq(q->queue_lock); if (req || mq->mqrq_prev->req) { set_current_state(TASK_RUNNING); cmd_flags = req ? req->cmd_flags : 0; mq->issue_fn(mq, req); cond_resched(); if (mq->flags & MMC_QUEUE_NEW_REQUEST) { mq->flags &= ~MMC_QUEUE_NEW_REQUEST; continue; /* fetch again */ } /* * Current request becomes previous request * and vice versa. * In case of special requests, current request * has been finished. Do not assign it to previous * request. */ if (cmd_flags & MMC_REQ_SPECIAL_MASK) mq->mqrq_cur->req = NULL; mq->mqrq_prev->brq.mrq.data = NULL; mq->mqrq_prev->req = NULL; swap(mq->mqrq_prev, mq->mqrq_cur); } else { if (kthread_should_stop()) { set_current_state(TASK_RUNNING); break; } up(&mq->thread_sem); schedule(); down(&mq->thread_sem); } } while (1); up(&mq->thread_sem); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
russell kingrussell king10141.22%430.77%
seungwon jeonseungwon jeon6727.35%215.38%
per forlinper forlin4417.96%215.38%
konstantin dorfmankonstantin dorfman197.76%17.69%
juha yrjolajuha yrjola62.45%17.69%
fabian frederickfabian frederick41.63%17.69%
rabin vincentrabin vincent31.22%17.69%
tejun heotejun heo10.41%17.69%
Total245100.00%13100.00%

/* * Generic MMC request handler. This is called for any queue on a * particular host. When the host is not busy, we look for a request * on any queue on this host, and attempt to issue it. This may * not be the queue we were asked to process. */
static void mmc_request_fn(struct request_queue *q) { struct mmc_queue *mq = q->queuedata; struct request *req; unsigned long flags; struct mmc_context_info *cntx; if (!mq) { while ((req = blk_fetch_request(q)) != NULL) { req->cmd_flags |= REQ_QUIET; __blk_end_request_all(req, -EIO); } return; } cntx = &mq->card->host->context_info; if (!mq->mqrq_cur->req && mq->mqrq_prev->req) { /* * New MMC request arrived when MMC thread may be * blocked on the previous request to be complete * with no current request fetched */ spin_lock_irqsave(&cntx->lock, flags); if (cntx->is_waiting_last_req) { cntx->is_new_req = true; wake_up_interruptible(&cntx->wait); } spin_unlock_irqrestore(&cntx->lock, flags); } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req) wake_up_process(mq->thread); }

Contributors

PersonTokensPropCommitsCommitProp
konstantin dorfmankonstantin dorfman8148.80%18.33%
pierre ossmanpierre ossman3018.07%18.33%
russell kingrussell king2917.47%18.33%
per forlinper forlin95.42%216.67%
adrian hunteradrian hunter84.82%18.33%
jens axboejens axboe21.20%18.33%
tejun heotejun heo21.20%216.67%
christoph hellwigchristoph hellwig21.20%18.33%
kiyoshi uedakiyoshi ueda21.20%18.33%
venkatraman sathiyamoorthyvenkatraman sathiyamoorthy10.60%18.33%
Total166100.00%12100.00%


static struct scatterlist *mmc_alloc_sg(int sg_len, int *err) { struct scatterlist *sg; sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL); if (!sg) *err = -ENOMEM; else { *err = 0; sg_init_table(sg, sg_len); } return sg; }

Contributors

PersonTokensPropCommitsCommitProp
per forlinper forlin6398.44%150.00%
venkatraman sathiyamoorthyvenkatraman sathiyamoorthy11.56%150.00%
Total64100.00%2100.00%


static void mmc_queue_setup_discard(struct request_queue *q, struct mmc_card *card) { unsigned max_discard; max_discard = mmc_calc_max_discard(card); if (!max_discard) return; queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); blk_queue_max_discard_sectors(q, max_discard); if (card->erased_byte == 0 && !mmc_can_discard(card)) q->limits.discard_zeroes_data = 1; q->limits.discard_granularity = card->pref_erase << 9; /* granularity must not be greater than max. discard */ if (card->pref_erase > max_discard) q->limits.discard_granularity = 0; if (mmc_can_secure_erase_trim(card)) queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q); }

Contributors

PersonTokensPropCommitsCommitProp
adrian hunteradrian hunter10796.40%266.67%
jens axboejens axboe43.60%133.33%
Total111100.00%3100.00%

/** * mmc_init_queue - initialise a queue structure. * @mq: mmc queue * @card: mmc card to attach this queue * @lock: queue lock * @subname: partition subname * * Initialise a MMC card request queue. */
int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock, const char *subname) { struct mmc_host *host = card->host; u64 limit = BLK_BOUNCE_HIGH; int ret; struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT; mq->card = card; mq->queue = blk_init_queue(mmc_request_fn, lock); if (!mq->queue) return -ENOMEM; mq->mqrq_cur = mqrq_cur; mq->mqrq_prev = mqrq_prev; mq->queue->queuedata = mq; blk_queue_prep_rq(mq->queue, mmc_prep_request); queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue); if (mmc_can_erase(card)) mmc_queue_setup_discard(mq->queue, card); #ifdef CONFIG_MMC_BLOCK_BOUNCE if (host->max_segs == 1) { unsigned int bouncesz; bouncesz = MMC_QUEUE_BOUNCESZ; if (bouncesz > host->max_req_size) bouncesz = host->max_req_size; if (bouncesz > host->max_seg_size) bouncesz = host->max_seg_size; if (bouncesz > (host->max_blk_count * 512)) bouncesz = host->max_blk_count * 512; if (bouncesz > 512) { mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); if (!mqrq_cur->bounce_buf) { pr_warn("%s: unable to allocate bounce cur buffer\n", mmc_card_name(card)); } else { mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); if (!mqrq_prev->bounce_buf) { pr_warn("%s: unable to allocate bounce prev buffer\n", mmc_card_name(card)); kfree(mqrq_cur->bounce_buf); mqrq_cur->bounce_buf = NULL; } } } if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) { blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); blk_queue_max_hw_sectors(mq->queue, bouncesz / 512); blk_queue_max_segments(mq->queue, bouncesz / 512); blk_queue_max_segment_size(mq->queue, bouncesz); mqrq_cur->sg = mmc_alloc_sg(1, &ret); if (ret) goto cleanup_queue; mqrq_cur->bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret); if (ret) goto cleanup_queue; mqrq_prev->sg = mmc_alloc_sg(1, &ret); if (ret) goto cleanup_queue; mqrq_prev->bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret); if (ret) goto cleanup_queue; } } #endif if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) { blk_queue_bounce_limit(mq->queue, limit); blk_queue_max_hw_sectors(mq->queue, min(host->max_blk_count, host->max_req_size / 512)); blk_queue_max_segments(mq->queue, host->max_segs); blk_queue_max_segment_size(mq->queue, host->max_seg_size); mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret); if (ret) goto cleanup_queue; mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret); if (ret) goto cleanup_queue; } sema_init(&mq->thread_sem, 1); mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s", host->index, subname ? subname : ""); if (IS_ERR(mq->thread)) { ret = PTR_ERR(mq->thread); goto free_bounce_sg; } return 0; free_bounce_sg: kfree(mqrq_cur->bounce_sg); mqrq_cur->bounce_sg = NULL; kfree(mqrq_prev->bounce_sg); mqrq_prev->bounce_sg = NULL; cleanup_queue: kfree(mqrq_cur->sg); mqrq_cur->sg = NULL; kfree(mqrq_cur->bounce_buf); mqrq_cur->bounce_buf = NULL; kfree(mqrq_prev->sg); mqrq_prev->sg = NULL; kfree(mqrq_prev->bounce_buf); mqrq_prev->bounce_buf = NULL; blk_cleanup_queue(mq->queue); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman23632.11%620.00%
per forlinper forlin21629.39%26.67%
russell kingrussell king19426.39%620.00%
adrian hunteradrian hunter283.81%413.33%
christoph hellwigchristoph hellwig162.18%13.33%
greg kroah-hartmangreg kroah-hartman91.22%13.33%
mike snitzermike snitzer91.22%13.33%
martin k. petersenmartin k. petersen70.95%310.00%
santosh shilimkarsantosh shilimkar50.68%13.33%
joe perchesjoe perches40.54%13.33%
ethan duethan du40.54%13.33%
bhuvanesh suracharibhuvanesh surachari30.41%13.33%
thomas gleixnerthomas gleixner30.41%13.33%
venkatraman sathiyamoorthyvenkatraman sathiyamoorthy10.14%13.33%
Total735100.00%30100.00%


void mmc_cleanup_queue(struct mmc_queue *mq) { struct request_queue *q = mq->queue; unsigned long flags; struct mmc_queue_req *mqrq_cur = mq->mqrq_cur; struct mmc_queue_req *mqrq_prev = mq->mqrq_prev; /* Make sure the queue isn't suspended, as that will deadlock */ mmc_queue_resume(mq); /* Then terminate our worker thread */ kthread_stop(mq->thread); /* Empty the queue */ spin_lock_irqsave(q->queue_lock, flags); q->queuedata = NULL; blk_start_queue(q); spin_unlock_irqrestore(q->queue_lock, flags); kfree(mqrq_cur->bounce_sg); mqrq_cur->bounce_sg = NULL; kfree(mqrq_cur->sg); mqrq_cur->sg = NULL; kfree(mqrq_cur->bounce_buf); mqrq_cur->bounce_buf = NULL; kfree(mqrq_prev->bounce_sg); mqrq_prev->bounce_sg = NULL; kfree(mqrq_prev->sg); mqrq_prev->sg = NULL; kfree(mqrq_prev->bounce_buf); mqrq_prev->bounce_buf = NULL; mq->card = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
per forlinper forlin6337.28%222.22%
pierre ossmanpierre ossman5130.18%222.22%
russell kingrussell king3319.53%333.33%
adrian hunteradrian hunter2011.83%111.11%
jens axboejens axboe21.18%111.11%
Total169100.00%9100.00%

EXPORT_SYMBOL(mmc_cleanup_queue);
int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card) { struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; int ret = 0; mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL); if (!mqrq_cur->packed) { pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n", mmc_card_name(card)); ret = -ENOMEM; goto out; } mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL); if (!mqrq_prev->packed) { pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n", mmc_card_name(card)); kfree(mqrq_cur->packed); mqrq_cur->packed = NULL; ret = -ENOMEM; goto out; } INIT_LIST_HEAD(&mqrq_cur->packed->list); INIT_LIST_HEAD(&mqrq_prev->packed->list); out: return ret; }

Contributors

PersonTokensPropCommitsCommitProp
seungwon jeonseungwon jeon168100.00%1100.00%
Total168100.00%1100.00%


void mmc_packed_clean(struct mmc_queue *mq) { struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; kfree(mqrq_cur->packed); mqrq_cur->packed = NULL; kfree(mqrq_prev->packed); mqrq_prev->packed = NULL; }

Contributors

PersonTokensPropCommitsCommitProp
seungwon jeonseungwon jeon62100.00%1100.00%
Total62100.00%1100.00%

/** * mmc_queue_suspend - suspend a MMC request queue * @mq: MMC queue to suspend * * Stop the block request queue, and wait for our thread to * complete any outstanding requests. This ensures that we * won't suspend while a request is being processed. */
void mmc_queue_suspend(struct mmc_queue *mq) { struct request_queue *q = mq->queue; unsigned long flags; if (!(mq->flags & MMC_QUEUE_SUSPENDED)) { mq->flags |= MMC_QUEUE_SUSPENDED; spin_lock_irqsave(q->queue_lock, flags); blk_stop_queue(q); spin_unlock_irqrestore(q->queue_lock, flags); down(&mq->thread_sem); } }

Contributors

PersonTokensPropCommitsCommitProp
russell kingrussell king7197.26%150.00%
jens axboejens axboe22.74%150.00%
Total73100.00%2100.00%

/** * mmc_queue_resume - resume a previously suspended MMC request queue * @mq: MMC queue to resume */
void mmc_queue_resume(struct mmc_queue *mq) { struct request_queue *q = mq->queue; unsigned long flags; if (mq->flags & MMC_QUEUE_SUSPENDED) { mq->flags &= ~MMC_QUEUE_SUSPENDED; up(&mq->thread_sem); spin_lock_irqsave(q->queue_lock, flags); blk_start_queue(q); spin_unlock_irqrestore(q->queue_lock, flags); } }

Contributors

PersonTokensPropCommitsCommitProp
russell kingrussell king6997.18%150.00%
jens axboejens axboe22.82%150.00%
Total71100.00%2100.00%


static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq, struct mmc_packed *packed, struct scatterlist *sg, enum mmc_packed_type cmd_type) { struct scatterlist *__sg = sg; unsigned int sg_len = 0; struct request *req; if (mmc_packed_wr(cmd_type)) { unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512; unsigned int max_seg_sz = queue_max_segment_size(mq->queue); unsigned int len, remain, offset = 0; u8 *buf = (u8 *)packed->cmd_hdr; remain = hdr_sz; do { len = min(remain, max_seg_sz); sg_set_buf(__sg, buf + offset, len); offset += len; remain -= len; sg_unmark_end(__sg++); sg_len++; } while (remain); } list_for_each_entry(req, &packed->list, queuelist) { sg_len += blk_rq_map_sg(mq->queue, req, __sg); __sg = sg + (sg_len - 1); sg_unmark_end(__sg++); } sg_mark_end(sg + (sg_len - 1)); return sg_len; }

Contributors

PersonTokensPropCommitsCommitProp
seungwon jeonseungwon jeon19797.04%150.00%
dan williamsdan williams62.96%150.00%
Total203100.00%2100.00%

/* * Prepare the sg list(s) to be handed of to the host driver */
unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq) { unsigned int sg_len; size_t buflen; struct scatterlist *sg; enum mmc_packed_type cmd_type; int i; cmd_type = mqrq->cmd_type; if (!mqrq->bounce_buf) { if (mmc_packed_cmd(cmd_type)) return mmc_queue_packed_map_sg(mq, mqrq->packed, mqrq->sg, cmd_type); else return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg); } BUG_ON(!mqrq->bounce_sg); if (mmc_packed_cmd(cmd_type)) sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed, mqrq->bounce_sg, cmd_type); else sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg); mqrq->bounce_sg_len = sg_len; buflen = 0; for_each_sg(mqrq->bounce_sg, sg, sg_len, i) buflen += sg->length; sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen); return 1; }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman10256.04%240.00%
seungwon jeonseungwon jeon6133.52%120.00%
per forlinper forlin158.24%120.00%
jens axboejens axboe42.20%120.00%
Total182100.00%5100.00%

/* * If writing, bounce the data to the buffer before the request * is sent to the host driver */
void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq) { if (!mqrq->bounce_buf) return; if (rq_data_dir(mqrq->req) != WRITE) return; sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, mqrq->bounce_buf, mqrq->sg[0].length); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman4685.19%266.67%
per forlinper forlin814.81%133.33%
Total54100.00%3100.00%

/* * If reading, bounce the data from the buffer after the request * has been handled by the host driver */
void mmc_queue_bounce_post(struct mmc_queue_req *mqrq) { if (!mqrq->bounce_buf) return; if (rq_data_dir(mqrq->req) != READ) return; sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, mqrq->bounce_buf, mqrq->sg[0].length); }

Contributors

PersonTokensPropCommitsCommitProp
pierre ossmanpierre ossman4685.19%266.67%
per forlinper forlin814.81%133.33%
Total54100.00%3100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
russell kingrussell king55522.29%914.75%
seungwon jeonseungwon jeon55522.29%34.92%
pierre ossmanpierre ossman52521.08%1016.39%
per forlinper forlin42617.11%34.92%
adrian hunteradrian hunter1746.99%69.84%
konstantin dorfmankonstantin dorfman1004.02%11.64%
christoph hellwigchristoph hellwig251.00%23.28%
sujit reddy thummasujit reddy thumma230.92%11.64%
jens axboejens axboe200.80%46.56%
mike snitzermike snitzer90.36%11.64%
greg kroah-hartmangreg kroah-hartman90.36%11.64%
santosh shilimkarsantosh shilimkar80.32%11.64%
martin k. petersenmartin k. petersen70.28%34.92%
chuanxiao dongchuanxiao dong70.28%11.64%
juha yrjolajuha yrjola60.24%11.64%
tejun heotejun heo60.24%34.92%
dan williamsdan williams60.24%11.64%
fabian frederickfabian frederick40.16%11.64%
ethan duethan du40.16%11.64%
joe perchesjoe perches40.16%11.64%
rabin vincentrabin vincent30.12%11.64%
venkatraman sathiyamoorthyvenkatraman sathiyamoorthy30.12%23.28%
thomas gleixnerthomas gleixner30.12%11.64%
bhuvanesh suracharibhuvanesh surachari30.12%11.64%
rafael j. wysockirafael j. wysocki30.12%11.64%
kiyoshi uedakiyoshi ueda20.08%11.64%
Total2490100.00%61100.00%
Directory: drivers/mmc/card
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
{% endraw %}