[SG] Update drivers to use sg helpers
[safe/jmp/linux-2.6] / drivers / mmc / card / queue.c
index aa75ac1..9203a0b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  linux/drivers/mmc/queue.c
+ *  linux/drivers/mmc/card/queue.c
  *
  *  Copyright (C) 2003 Russell King, All Rights Reserved.
  *  Copyright 2006-2007 Pierre Ossman
  */
 #include <linux/module.h>
 #include <linux/blkdev.h>
+#include <linux/freezer.h>
 #include <linux/kthread.h>
+#include <linux/scatterlist.h>
 
 #include <linux/mmc/card.h>
 #include <linux/mmc/host.h>
 #include "queue.h"
 
+#define MMC_QUEUE_BOUNCESZ     65536
+
 #define MMC_QUEUE_SUSPENDED    (1 << 0)
 
 /*
- * Prepare a MMC request.  Essentially, this means passing the
- * preparation off to the media driver.  The media driver will
- * create a mmc_io_request in req->special.
+ * 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;
-       int ret = BLKPREP_KILL;
-
-       if (blk_special_request(req)) {
-               /*
-                * Special commands already have the command
-                * blocks already setup in req->special.
-                */
-               BUG_ON(!req->special);
-
-               ret = BLKPREP_OK;
-       } else if (blk_fs_request(req) || blk_pc_request(req)) {
-               /*
-                * Block I/O requests need translating according
-                * to the protocol.
-                */
-               ret = mq->prep_fn(mq, req);
-       } else {
-               /*
-                * Everything else is invalid.
-                */
+       /*
+        * We only like normal block requests.
+        */
+       if (!blk_fs_request(req) && !blk_pc_request(req)) {
                blk_dump_rq_flags(req, "MMC bad request");
+               return BLKPREP_KILL;
        }
 
-       if (ret == BLKPREP_OK)
-               req->cmd_flags |= REQ_DONTPREP;
+       req->cmd_flags |= REQ_DONTPREP;
 
-       return ret;
+       return BLKPREP_OK;
 }
 
 static int mmc_queue_thread(void *d)
@@ -61,11 +46,7 @@ static int mmc_queue_thread(void *d)
        struct mmc_queue *mq = d;
        struct request_queue *q = mq->queue;
 
-       /*
-        * Set iothread to ensure that we aren't put to sleep by
-        * the process freezing.  We handle suspension ourselves.
-        */
-       current->flags |= PF_MEMALLOC|PF_NOFREEZE;
+       current->flags |= PF_MEMALLOC;
 
        down(&mq->thread_sem);
        do {
@@ -103,7 +84,7 @@ static int mmc_queue_thread(void *d)
  * 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(request_queue_t *q)
+static void mmc_request(struct request_queue *q)
 {
        struct mmc_queue *mq = q->queuedata;
        struct request *req;
@@ -146,21 +127,65 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
        if (!mq->queue)
                return -ENOMEM;
 
-       blk_queue_prep_rq(mq->queue, mmc_prep_request);
-       blk_queue_bounce_limit(mq->queue, limit);
-       blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
-       blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
-       blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
-       blk_queue_max_segment_size(mq->queue, host->max_seg_size);
-
        mq->queue->queuedata = mq;
        mq->req = NULL;
 
-       mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
-                        GFP_KERNEL);
-       if (!mq->sg) {
-               ret = -ENOMEM;
-               goto cleanup_queue;
+       blk_queue_prep_rq(mq->queue, mmc_prep_request);
+
+#ifdef CONFIG_MMC_BLOCK_BOUNCE
+       if (host->max_hw_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;
+
+               mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
+               if (!mq->bounce_buf) {
+                       printk(KERN_WARNING "%s: unable to allocate "
+                               "bounce buffer\n", mmc_card_name(card));
+               } else {
+                       blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
+                       blk_queue_max_sectors(mq->queue, bouncesz / 512);
+                       blk_queue_max_phys_segments(mq->queue, bouncesz / 512);
+                       blk_queue_max_hw_segments(mq->queue, bouncesz / 512);
+                       blk_queue_max_segment_size(mq->queue, bouncesz);
+
+                       mq->sg = kmalloc(sizeof(struct scatterlist),
+                               GFP_KERNEL);
+                       if (!mq->sg) {
+                               ret = -ENOMEM;
+                               goto cleanup_queue;
+                       }
+                       sg_init_table(mq->sg, 1);
+
+                       mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
+                               bouncesz / 512, GFP_KERNEL);
+                       if (!mq->bounce_sg) {
+                               ret = -ENOMEM;
+                               goto cleanup_queue;
+                       }
+                       sg_init_table(mq->bounce_sg, bouncesz / 512);
+               }
+       }
+#endif
+
+       if (!mq->bounce_buf) {
+               blk_queue_bounce_limit(mq->queue, limit);
+               blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
+               blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
+               blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
+               blk_queue_max_segment_size(mq->queue, host->max_seg_size);
+
+               mq->sg = kzalloc(sizeof(struct scatterlist) *
+                       host->max_phys_segs, GFP_KERNEL);
+               if (!mq->sg) {
+                       ret = -ENOMEM;
+                       goto cleanup_queue;
+               }
        }
 
        init_MUTEX(&mq->thread_sem);
@@ -168,22 +193,28 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
        mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
        if (IS_ERR(mq->thread)) {
                ret = PTR_ERR(mq->thread);
-               goto free_sg;
+               goto free_bounce_sg;
        }
 
        return 0;
-
- free_sg:
-       kfree(mq->sg);
-       mq->sg = NULL;
+ free_bounce_sg:
+       if (mq->bounce_sg)
+               kfree(mq->bounce_sg);
+       mq->bounce_sg = NULL;
  cleanup_queue:
+       if (mq->sg)
+               kfree(mq->sg);
+       mq->sg = NULL;
+       if (mq->bounce_buf)
+               kfree(mq->bounce_buf);
+       mq->bounce_buf = NULL;
        blk_cleanup_queue(mq->queue);
        return ret;
 }
 
 void mmc_cleanup_queue(struct mmc_queue *mq)
 {
-       request_queue_t *q = mq->queue;
+       struct request_queue *q = mq->queue;
        unsigned long flags;
 
        /* Mark that we should start throwing out stragglers */
@@ -191,12 +222,23 @@ void mmc_cleanup_queue(struct mmc_queue *mq)
        q->queuedata = NULL;
        spin_unlock_irqrestore(q->queue_lock, flags);
 
+       /* Make sure the queue isn't suspended, as that will deadlock */
+       mmc_queue_resume(mq);
+
        /* Then terminate our worker thread */
        kthread_stop(mq->thread);
 
+       if (mq->bounce_sg)
+               kfree(mq->bounce_sg);
+       mq->bounce_sg = NULL;
+
        kfree(mq->sg);
        mq->sg = NULL;
 
+       if (mq->bounce_buf)
+               kfree(mq->bounce_buf);
+       mq->bounce_buf = NULL;
+
        blk_cleanup_queue(mq->queue);
 
        mq->card = NULL;
@@ -213,7 +255,7 @@ EXPORT_SYMBOL(mmc_cleanup_queue);
  */
 void mmc_queue_suspend(struct mmc_queue *mq)
 {
-       request_queue_t *q = mq->queue;
+       struct request_queue *q = mq->queue;
        unsigned long flags;
 
        if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
@@ -233,7 +275,7 @@ void mmc_queue_suspend(struct mmc_queue *mq)
  */
 void mmc_queue_resume(struct mmc_queue *mq)
 {
-       request_queue_t *q = mq->queue;
+       struct request_queue *q = mq->queue;
        unsigned long flags;
 
        if (mq->flags & MMC_QUEUE_SUSPENDED) {
@@ -247,3 +289,106 @@ void mmc_queue_resume(struct mmc_queue *mq)
        }
 }
 
+static void copy_sg(struct scatterlist *dst, unsigned int dst_len,
+       struct scatterlist *src, unsigned int src_len)
+{
+       unsigned int chunk;
+       char *dst_buf, *src_buf;
+       unsigned int dst_size, src_size;
+
+       dst_buf = NULL;
+       src_buf = NULL;
+       dst_size = 0;
+       src_size = 0;
+
+       while (src_len) {
+               BUG_ON(dst_len == 0);
+
+               if (dst_size == 0) {
+                       dst_buf = sg_virt(dst);
+                       dst_size = dst->length;
+               }
+
+               if (src_size == 0) {
+                       src_buf = sg_virt(dst);
+                       src_size = src->length;
+               }
+
+               chunk = min(dst_size, src_size);
+
+               memcpy(dst_buf, src_buf, chunk);
+
+               dst_buf += chunk;
+               src_buf += chunk;
+               dst_size -= chunk;
+               src_size -= chunk;
+
+               if (dst_size == 0) {
+                       dst++;
+                       dst_len--;
+               }
+
+               if (src_size == 0) {
+                       src++;
+                       src_len--;
+               }
+       }
+}
+
+unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
+{
+       unsigned int sg_len;
+
+       if (!mq->bounce_buf)
+               return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
+
+       BUG_ON(!mq->bounce_sg);
+
+       sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
+
+       mq->bounce_sg_len = sg_len;
+
+       /*
+        * Shortcut in the event we only get a single entry.
+        */
+       if (sg_len == 1) {
+               memcpy(mq->sg, mq->bounce_sg, sizeof(struct scatterlist));
+               return 1;
+       }
+
+       sg_init_one(mq->sg, mq->bounce_buf, 0);
+
+       while (sg_len) {
+               mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
+               sg_len--;
+       }
+
+       return 1;
+}
+
+void mmc_queue_bounce_pre(struct mmc_queue *mq)
+{
+       if (!mq->bounce_buf)
+               return;
+
+       if (mq->bounce_sg_len == 1)
+               return;
+       if (rq_data_dir(mq->req) != WRITE)
+               return;
+
+       copy_sg(mq->sg, 1, mq->bounce_sg, mq->bounce_sg_len);
+}
+
+void mmc_queue_bounce_post(struct mmc_queue *mq)
+{
+       if (!mq->bounce_buf)
+               return;
+
+       if (mq->bounce_sg_len == 1)
+               return;
+       if (rq_data_dir(mq->req) != READ)
+               return;
+
+       copy_sg(mq->bounce_sg, mq->bounce_sg_len, mq->sg, 1);
+}
+