]> err.no Git - linux-2.6/blobdiff - include/linux/scatterlist.h
Merge branch 'for-2.6.24' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerp...
[linux-2.6] / include / linux / scatterlist.h
index c6136e8a7f58fcb561ffc042b501e7a9f554c1a1..25973504414823b7876d2f34256ea1f103018ddb 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _LINUX_SCATTERLIST_H
 #define _LINUX_SCATTERLIST_H
 
+#include <asm/types.h>
 #include <asm/scatterlist.h>
 #include <linux/mm.h>
 #include <linux/string.h>
  *
  */
 
+#define SG_MAGIC       0x87654321
+
+/**
+ * sg_assign_page - Assign a given page to an SG entry
+ * @sg:                    SG entry
+ * @page:          The page
+ *
+ * Description:
+ *   Assign page to sg entry. Also see sg_set_page(), the most commonly used
+ *   variant.
+ *
+ **/
+static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
+{
+       unsigned long page_link = sg->page_link & 0x3;
+
+       /*
+        * In order for the low bit stealing approach to work, pages
+        * must be aligned at a 32-bit boundary as a minimum.
+        */
+       BUG_ON((unsigned long) page & 0x03);
+#ifdef CONFIG_DEBUG_SG
+       BUG_ON(sg->sg_magic != SG_MAGIC);
+#endif
+       sg->page_link = page_link | (unsigned long) page;
+}
+
 /**
  * sg_set_page - Set sg entry to point at given page
  * @sg:                 SG entry
  * @page:       The page
+ * @len:        Length of data
+ * @offset:     Offset into page
  *
  * Description:
  *   Use this function to set an sg entry pointing at a page, never assign
  *   to an sg entry.
  *
  **/
-static inline void sg_set_page(struct scatterlist *sg, struct page *page)
+static inline void sg_set_page(struct scatterlist *sg, struct page *page,
+                              unsigned int len, unsigned int offset)
 {
-       unsigned long page_link = sg->page_link & 0x3;
-
-       sg->page_link = page_link | (unsigned long) page;
+       sg_assign_page(sg, page);
+       sg->offset = offset;
+       sg->length = len;
 }
 
 #define sg_page(sg)    ((struct page *) ((sg)->page_link & ~0x3))
@@ -54,9 +85,7 @@ static inline void sg_set_page(struct scatterlist *sg, struct page *page)
 static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
                              unsigned int buflen)
 {
-       sg_set_page(sg, virt_to_page(buf));
-       sg->offset = offset_in_page(buf);
-       sg->length = buflen;
+       sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
 }
 
 /*
@@ -81,6 +110,9 @@ static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
  **/
 static inline struct scatterlist *sg_next(struct scatterlist *sg)
 {
+#ifdef CONFIG_DEBUG_SG
+       BUG_ON(sg->sg_magic != SG_MAGIC);
+#endif
        if (sg_is_last(sg))
                return NULL;
 
@@ -118,11 +150,15 @@ static inline struct scatterlist *sg_last(struct scatterlist *sgl,
        struct scatterlist *ret = &sgl[nents - 1];
 #else
        struct scatterlist *sg, *ret = NULL;
-       int i;
+       unsigned int i;
 
        for_each_sg(sgl, sg, nents, i)
                ret = sg;
 
+#endif
+#ifdef CONFIG_DEBUG_SG
+       BUG_ON(sgl[0].sg_magic != SG_MAGIC);
+       BUG_ON(!sg_is_last(ret));
 #endif
        return ret;
 }
@@ -143,26 +179,55 @@ static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
 #ifndef ARCH_HAS_SG_CHAIN
        BUG();
 #endif
-       prv[prv_nents - 1].page_link = (unsigned long) sgl | 0x01;
+       /*
+        * Set lowest bit to indicate a link pointer, and make sure to clear
+        * the termination bit if it happens to be set.
+        */
+       prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
 }
 
 /**
  * sg_mark_end - Mark the end of the scatterlist
- * @sgl:       Scatterlist
- * @nents:     Number of entries in sgl
+ * @sg:                 SG entryScatterlist
  *
  * Description:
- *   Marks the last entry as the termination point for sg_next()
+ *   Marks the passed in sg entry as the termination point for the sg
+ *   table. A call to sg_next() on this entry will return NULL.
  *
  **/
-static inline void sg_mark_end(struct scatterlist *sgl, unsigned int nents)
+static inline void sg_mark_end(struct scatterlist *sg)
 {
-       sgl[nents - 1].page_link = 0x02;
+#ifdef CONFIG_DEBUG_SG
+       BUG_ON(sg->sg_magic != SG_MAGIC);
+#endif
+       /*
+        * Set termination bit, clear potential chain bit
+        */
+       sg->page_link |= 0x02;
+       sg->page_link &= ~0x01;
 }
 
-static inline void __sg_mark_end(struct scatterlist *sg)
+/**
+ * sg_init_table - Initialize SG table
+ * @sgl:          The SG table
+ * @nents:        Number of entries in table
+ *
+ * Notes:
+ *   If this is part of a chained sg table, sg_mark_end() should be
+ *   used only on the last table part.
+ *
+ **/
+static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
 {
-       sg->page_link |= 0x02;
+       memset(sgl, 0, sizeof(*sgl) * nents);
+#ifdef CONFIG_DEBUG_SG
+       {
+               unsigned int i;
+               for (i = 0; i < nents; i++)
+                       sgl[i].sg_magic = SG_MAGIC;
+       }
+#endif
+       sg_mark_end(&sgl[nents - 1]);
 }
 
 /**
@@ -179,27 +244,10 @@ static inline void __sg_mark_end(struct scatterlist *sg)
 static inline void sg_init_one(struct scatterlist *sg, const void *buf,
                               unsigned int buflen)
 {
-       memset(sg, 0, sizeof(*sg));
-       sg_mark_end(sg, 1);
+       sg_init_table(sg, 1);
        sg_set_buf(sg, buf, buflen);
 }
 
-/**
- * sg_init_table - Initialize SG table
- * @sgl:          The SG table
- * @nents:        Number of entries in table
- *
- * Notes:
- *   If this is part of a chained sg table, sg_mark_end() should be
- *   used only on the last table part.
- *
- **/
-static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
-{
-       memset(sgl, 0, sizeof(*sgl) * nents);
-       sg_mark_end(sgl, nents);
-}
-
 /**
  * sg_phys - Return physical address of an sg entry
  * @sg:             SG entry
@@ -210,7 +258,7 @@ static inline void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  *   on the sg page.
  *
  **/
-static inline unsigned long sg_phys(struct scatterlist *sg)
+static inline dma_addr_t sg_phys(struct scatterlist *sg)
 {
        return page_to_phys(sg_page(sg)) + sg->offset;
 }