#include <asm/types.h>
   
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
 
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;         /* buffer offset                 */
        dma_addr_t      dma_address;    /* dma address                   */
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+   unsigned long       sg_magic;
+#endif
     unsigned long      page_link;
     unsigned int       offset;
     dma_addr_t         dma_address;
 
 #include <linux/mm.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
        dma_addr_t dma_address;
 
 #define __ASM_CRIS_SCATTERLIST_H
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        char *  address;    /* Location data is to be transferred to */
        unsigned int length;
 
 
  * and that's it. There's no excuse for not highmem enabling YOUR driver. /jens
  */
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;         /* for highmem, page offset */
 
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;
        dma_addr_t      dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
        unsigned int length;    /* buffer length */
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long sg_magic;
+#endif
     char *  address;    /* Location data is to be transferred to, NULL for
                          * highmem page */
     unsigned long page_link;
 
 #include <linux/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
        unsigned int length;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;
        dma_addr_t      dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;
        dma_addr_t      dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
 
 
 #include <asm/dma.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
        unsigned int length;
 
 #define _ASMS390_SCATTERLIST_H
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long sg_magic;
+#endif
     unsigned long page_link;
     unsigned int offset;
     unsigned int length;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long sg_magic;
+#endif
     unsigned long page_link;
     unsigned int offset;/* for highmem, page offset */
     dma_addr_t dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long sg_magic;
+#endif
     unsigned long page_link;
     unsigned int offset;/* for highmem, page offset */
     dma_addr_t dma_address;
 
 #include <linux/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long sg_magic;
+#endif
        unsigned long page_link;
        unsigned int offset;
 
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;
 
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned        offset;
        dma_addr_t      dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long      sg_magic;
+#endif
     unsigned long      page_link;
     unsigned int       offset;
     dma_addr_t         dma_address;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+    unsigned long      sg_magic;
+#endif
     unsigned long      page_link;
     unsigned int       offset;
     unsigned int       length;
 
 #include <asm/types.h>
 
 struct scatterlist {
+#ifdef CONFIG_DEBUG_SG
+       unsigned long   sg_magic;
+#endif
        unsigned long   page_link;
        unsigned int    offset;
        dma_addr_t      dma_address;
 
  *
  */
 
+#define SG_MAGIC       0x87654321
+
 /**
  * sg_set_page - Set sg entry to point at given page
  * @sg:                 SG entry
 {
        unsigned long page_link = sg->page_link & 0x3;
 
+#ifdef CONFIG_DEBUG_SG
+       BUG_ON(sg->sg_magic != SG_MAGIC);
+#endif
        sg->page_link = page_link | (unsigned long) page;
 }
 
  **/
 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;
 
        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;
 }
                               unsigned int buflen)
 {
        memset(sg, 0, sizeof(*sg));
+#ifdef CONFIG_DEBUG_SG
+       sg->sg_magic = SG_MAGIC;
+#endif
        sg_mark_end(sg, 1);
        sg_set_buf(sg, buf, buflen);
 }
 {
        memset(sgl, 0, sizeof(*sgl) * nents);
        sg_mark_end(sgl, nents);
+#ifdef CONFIG_DEBUG_SG
+       {
+               int i;
+               for (i = 0; i < nents; i++)
+                       sgl[i].sg_magic = SG_MAGIC;
+       }
+#endif
 }
 
 /**
 
 
          If unsure, say N.
 
+config DEBUG_SG
+       bool "Debug SG table operations"
+       depends on DEBUG_KERNEL
+       help
+         Enable this to turn on checks on scatter-gather tables. This can
+         help find problems with drivers that do not properly initialize
+         their sg tables.
+
+         If unsure, say N.
+
 config FRAME_POINTER
        bool "Compile the kernel with frame pointers"
        depends on DEBUG_KERNEL && (X86 || CRIS || M68K || M68KNOMMU || FRV || UML || S390 || AVR32 || SUPERH || BFIN)