aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Axboe <jens.axboe@oracle.com>2007-10-16 06:29:34 -0400
committerJens Axboe <jens.axboe@oracle.com>2007-10-16 06:29:34 -0400
commit3eed13fd933dbb81db12f7cdec6de9268c4443b5 (patch)
treec16913706acffd4a0b29ec12cd68906b708c9a8a
parenta39d113936370ba524fa9e34d6954c3625c8aa64 (diff)
parent2c941a204070ab32d92d40318a3196a7fb994c00 (diff)
Merge branch 'sglist-arch' into for-linus
-rw-r--r--Documentation/DMA-mapping.txt2
-rw-r--r--arch/ia64/hp/common/sba_iommu.c14
-rw-r--r--arch/ia64/hp/sim/simscsi.c1
-rw-r--r--arch/ia64/sn/pci/pci_dma.c11
-rw-r--r--arch/powerpc/kernel/dma_64.c5
-rw-r--r--arch/powerpc/kernel/ibmebus.c11
-rw-r--r--arch/powerpc/kernel/iommu.c23
-rw-r--r--arch/powerpc/platforms/ps3/system-bus.c7
-rw-r--r--arch/sparc/kernel/ioport.c25
-rw-r--r--arch/sparc/mm/io-unit.c12
-rw-r--r--arch/sparc/mm/iommu.c10
-rw-r--r--arch/sparc/mm/sun4c.c10
-rw-r--r--arch/sparc64/kernel/iommu.c39
-rw-r--r--arch/sparc64/kernel/pci_sun4v.c32
-rw-r--r--arch/x86/kernel/pci-calgary_64.c24
-rw-r--r--arch/x86/kernel/pci-gart_64.c65
-rw-r--r--arch/x86/kernel/pci-nommu_64.c5
-rw-r--r--block/ll_rw_blk.c41
-rw-r--r--crypto/digest.c2
-rw-r--r--crypto/scatterwalk.c2
-rw-r--r--crypto/scatterwalk.h2
-rw-r--r--drivers/ata/libata-core.c35
-rw-r--r--drivers/ata/libata-scsi.c2
-rw-r--r--drivers/block/cciss.c1
-rw-r--r--drivers/ide/cris/ide-cris.c3
-rw-r--r--drivers/ide/ide-dma.c2
-rw-r--r--drivers/ide/ide-io.c3
-rw-r--r--drivers/ide/ide-probe.c2
-rw-r--r--drivers/ide/ide-taskfile.c18
-rw-r--r--drivers/ide/mips/au1xxx-ide.c2
-rw-r--r--drivers/ide/pci/sgiioc4.c3
-rw-r--r--drivers/ide/ppc/pmac.c2
-rw-r--r--drivers/infiniband/hw/ipath/ipath_dma.c10
-rw-r--r--drivers/infiniband/ulp/iser/iser_memory.c75
-rw-r--r--drivers/message/fusion/mptscsih.c6
-rw-r--r--drivers/mmc/card/queue.c6
-rw-r--r--drivers/s390/scsi/zfcp_def.h1
-rw-r--r--drivers/s390/scsi/zfcp_qdio.c6
-rw-r--r--drivers/scsi/3w-9xxx.c1
-rw-r--r--drivers/scsi/3w-xxxx.c1
-rw-r--r--drivers/scsi/BusLogic.c1
-rw-r--r--drivers/scsi/NCR53c406a.c3
-rw-r--r--drivers/scsi/a100u2w.c1
-rw-r--r--drivers/scsi/aacraid/linit.c1
-rw-r--r--drivers/scsi/aha1542.c32
-rw-r--r--drivers/scsi/aha1740.c1
-rw-r--r--drivers/scsi/aic7xxx/aic79xx_osm.c1
-rw-r--r--drivers/scsi/aic7xxx/aic7xxx_osm.c1
-rw-r--r--drivers/scsi/aic7xxx_old.c1
-rw-r--r--drivers/scsi/aic94xx/aic94xx_task.c6
-rw-r--r--drivers/scsi/arcmsr/arcmsr_hba.c1
-rw-r--r--drivers/scsi/dc395x.c1
-rw-r--r--drivers/scsi/dpt_i2o.c1
-rw-r--r--drivers/scsi/eata.c3
-rw-r--r--drivers/scsi/hosts.c1
-rw-r--r--drivers/scsi/hptiop.c1
-rw-r--r--drivers/scsi/ibmmca.c1
-rw-r--r--drivers/scsi/ibmvscsi/ibmvscsi.c1
-rw-r--r--drivers/scsi/ide-scsi.c32
-rw-r--r--drivers/scsi/initio.c1
-rw-r--r--drivers/scsi/ips.c14
-rw-r--r--drivers/scsi/lpfc/lpfc_scsi.c2
-rw-r--r--drivers/scsi/mac53c94.c1
-rw-r--r--drivers/scsi/megaraid.c1
-rw-r--r--drivers/scsi/megaraid/megaraid_mbox.c1
-rw-r--r--drivers/scsi/megaraid/megaraid_sas.c1
-rw-r--r--drivers/scsi/mesh.c1
-rw-r--r--drivers/scsi/nsp32.c1
-rw-r--r--drivers/scsi/pcmcia/sym53c500_cs.c1
-rw-r--r--drivers/scsi/qla1280.c70
-rw-r--r--drivers/scsi/qla2xxx/qla_os.c2
-rw-r--r--drivers/scsi/qla4xxx/ql4_os.c1
-rw-r--r--drivers/scsi/qlogicfas.c1
-rw-r--r--drivers/scsi/qlogicpti.c15
-rw-r--r--drivers/scsi/scsi_debug.c30
-rw-r--r--drivers/scsi/scsi_lib.c238
-rw-r--r--drivers/scsi/scsi_tgt_lib.c4
-rw-r--r--drivers/scsi/sg.c16
-rw-r--r--drivers/scsi/stex.c1
-rw-r--r--drivers/scsi/sym53c416.c1
-rw-r--r--drivers/scsi/sym53c8xx_2/sym_glue.c1
-rw-r--r--drivers/scsi/u14-34f.c3
-rw-r--r--drivers/scsi/ultrastor.c1
-rw-r--r--drivers/scsi/wd7000.c1
-rw-r--r--drivers/usb/storage/alauda.c16
-rw-r--r--drivers/usb/storage/datafab.c10
-rw-r--r--drivers/usb/storage/jumpshot.c10
-rw-r--r--drivers/usb/storage/protocol.c20
-rw-r--r--drivers/usb/storage/protocol.h2
-rw-r--r--drivers/usb/storage/sddr09.c16
-rw-r--r--drivers/usb/storage/sddr55.c16
-rw-r--r--drivers/usb/storage/shuttle_usbat.c17
-rw-r--r--include/asm-ia64/dma-mapping.h2
-rw-r--r--include/asm-ia64/scatterlist.h2
-rw-r--r--include/asm-powerpc/dma-mapping.h158
-rw-r--r--include/asm-powerpc/scatterlist.h2
-rw-r--r--include/asm-sparc/scatterlist.h2
-rw-r--r--include/asm-sparc64/scatterlist.h2
-rw-r--r--include/asm-x86/dma-mapping_32.h13
-rw-r--r--include/asm-x86/dma-mapping_64.h3
-rw-r--r--include/asm-x86/scatterlist_32.h2
-rw-r--r--include/asm-x86/scatterlist_64.h2
-rw-r--r--include/linux/i2o.h3
-rw-r--r--include/linux/ide.h2
-rw-r--r--include/linux/libata.h16
-rw-r--r--include/linux/scatterlist.h84
-rw-r--r--include/scsi/scsi.h7
-rw-r--r--include/scsi/scsi_cmnd.h7
-rw-r--r--include/scsi/scsi_host.h13
-rw-r--r--lib/swiotlb.c19
110 files changed, 870 insertions, 597 deletions
diff --git a/Documentation/DMA-mapping.txt b/Documentation/DMA-mapping.txt
index 8efe12d74268..3c8ae020b6a7 100644
--- a/Documentation/DMA-mapping.txt
+++ b/Documentation/DMA-mapping.txt
@@ -514,7 +514,7 @@ With scatterlists, you map a region gathered from several regions by:
514 int i, count = pci_map_sg(dev, sglist, nents, direction); 514 int i, count = pci_map_sg(dev, sglist, nents, direction);
515 struct scatterlist *sg; 515 struct scatterlist *sg;
516 516
517 for (i = 0, sg = sglist; i < count; i++, sg++) { 517 for_each_sg(sglist, sg, count, i) {
518 hw_address[i] = sg_dma_address(sg); 518 hw_address[i] = sg_dma_address(sg);
519 hw_len[i] = sg_dma_len(sg); 519 hw_len[i] = sg_dma_len(sg);
520 } 520 }
diff --git a/arch/ia64/hp/common/sba_iommu.c b/arch/ia64/hp/common/sba_iommu.c
index e980e7aa2306..4338f4123f31 100644
--- a/arch/ia64/hp/common/sba_iommu.c
+++ b/arch/ia64/hp/common/sba_iommu.c
@@ -396,7 +396,7 @@ sba_dump_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
396 printk(KERN_DEBUG " %d : DMA %08lx/%05x CPU %p\n", nents, 396 printk(KERN_DEBUG " %d : DMA %08lx/%05x CPU %p\n", nents,
397 startsg->dma_address, startsg->dma_length, 397 startsg->dma_address, startsg->dma_length,
398 sba_sg_address(startsg)); 398 sba_sg_address(startsg));
399 startsg++; 399 startsg = sg_next(startsg);
400 } 400 }
401} 401}
402 402
@@ -409,7 +409,7 @@ sba_check_sg( struct ioc *ioc, struct scatterlist *startsg, int nents)
409 while (the_nents-- > 0) { 409 while (the_nents-- > 0) {
410 if (sba_sg_address(the_sg) == 0x0UL) 410 if (sba_sg_address(the_sg) == 0x0UL)
411 sba_dump_sg(NULL, startsg, nents); 411 sba_dump_sg(NULL, startsg, nents);
412 the_sg++; 412 the_sg = sg_next(the_sg);
413 } 413 }
414} 414}
415 415
@@ -1201,7 +1201,7 @@ sba_fill_pdir(
1201 u32 pide = startsg->dma_address & ~PIDE_FLAG; 1201 u32 pide = startsg->dma_address & ~PIDE_FLAG;
1202 dma_offset = (unsigned long) pide & ~iovp_mask; 1202 dma_offset = (unsigned long) pide & ~iovp_mask;
1203 startsg->dma_address = 0; 1203 startsg->dma_address = 0;
1204 dma_sg++; 1204 dma_sg = sg_next(dma_sg);
1205 dma_sg->dma_address = pide | ioc->ibase; 1205 dma_sg->dma_address = pide | ioc->ibase;
1206 pdirp = &(ioc->pdir_base[pide >> iovp_shift]); 1206 pdirp = &(ioc->pdir_base[pide >> iovp_shift]);
1207 n_mappings++; 1207 n_mappings++;
@@ -1228,7 +1228,7 @@ sba_fill_pdir(
1228 pdirp++; 1228 pdirp++;
1229 } while (cnt > 0); 1229 } while (cnt > 0);
1230 } 1230 }
1231 startsg++; 1231 startsg = sg_next(startsg);
1232 } 1232 }
1233 /* force pdir update */ 1233 /* force pdir update */
1234 wmb(); 1234 wmb();
@@ -1297,7 +1297,7 @@ sba_coalesce_chunks( struct ioc *ioc,
1297 while (--nents > 0) { 1297 while (--nents > 0) {
1298 unsigned long vaddr; /* tmp */ 1298 unsigned long vaddr; /* tmp */
1299 1299
1300 startsg++; 1300 startsg = sg_next(startsg);
1301 1301
1302 /* PARANOID */ 1302 /* PARANOID */
1303 startsg->dma_address = startsg->dma_length = 0; 1303 startsg->dma_address = startsg->dma_length = 0;
@@ -1407,7 +1407,7 @@ int sba_map_sg(struct device *dev, struct scatterlist *sglist, int nents, int di
1407#ifdef ALLOW_IOV_BYPASS_SG 1407#ifdef ALLOW_IOV_BYPASS_SG
1408 ASSERT(to_pci_dev(dev)->dma_mask); 1408 ASSERT(to_pci_dev(dev)->dma_mask);
1409 if (likely((ioc->dma_mask & ~to_pci_dev(dev)->dma_mask) == 0)) { 1409 if (likely((ioc->dma_mask & ~to_pci_dev(dev)->dma_mask) == 0)) {
1410 for (sg = sglist ; filled < nents ; filled++, sg++){ 1410 for_each_sg(sglist, sg, nents, filled) {
1411 sg->dma_length = sg->length; 1411 sg->dma_length = sg->length;
1412 sg->dma_address = virt_to_phys(sba_sg_address(sg)); 1412 sg->dma_address = virt_to_phys(sba_sg_address(sg));
1413 } 1413 }
@@ -1501,7 +1501,7 @@ void sba_unmap_sg (struct device *dev, struct scatterlist *sglist, int nents, in
1501 while (nents && sglist->dma_length) { 1501 while (nents && sglist->dma_length) {
1502 1502
1503 sba_unmap_single(dev, sglist->dma_address, sglist->dma_length, dir); 1503 sba_unmap_single(dev, sglist->dma_address, sglist->dma_length, dir);
1504 sglist++; 1504 sglist = sg_next(sglist);
1505 nents--; 1505 nents--;
1506 } 1506 }
1507 1507
diff --git a/arch/ia64/hp/sim/simscsi.c b/arch/ia64/hp/sim/simscsi.c
index d62fa76e5a7d..a3a558a06757 100644
--- a/arch/ia64/hp/sim/simscsi.c
+++ b/arch/ia64/hp/sim/simscsi.c
@@ -360,6 +360,7 @@ static struct scsi_host_template driver_template = {
360 .max_sectors = 1024, 360 .max_sectors = 1024,
361 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN, 361 .cmd_per_lun = SIMSCSI_REQ_QUEUE_LEN,
362 .use_clustering = DISABLE_CLUSTERING, 362 .use_clustering = DISABLE_CLUSTERING,
363 .use_sg_chaining = ENABLE_SG_CHAINING,
363}; 364};
364 365
365static int __init 366static int __init
diff --git a/arch/ia64/sn/pci/pci_dma.c b/arch/ia64/sn/pci/pci_dma.c
index d79ddacfba2d..ecd8a52b9b9e 100644
--- a/arch/ia64/sn/pci/pci_dma.c
+++ b/arch/ia64/sn/pci/pci_dma.c
@@ -218,16 +218,17 @@ EXPORT_SYMBOL(sn_dma_unmap_single);
218 * 218 *
219 * Unmap a set of streaming mode DMA translations. 219 * Unmap a set of streaming mode DMA translations.
220 */ 220 */
221void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sg, 221void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sgl,
222 int nhwentries, int direction) 222 int nhwentries, int direction)
223{ 223{
224 int i; 224 int i;
225 struct pci_dev *pdev = to_pci_dev(dev); 225 struct pci_dev *pdev = to_pci_dev(dev);
226 struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev); 226 struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
227 struct scatterlist *sg;
227 228
228 BUG_ON(dev->bus != &pci_bus_type); 229 BUG_ON(dev->bus != &pci_bus_type);
229 230
230 for (i = 0; i < nhwentries; i++, sg++) { 231 for_each_sg(sgl, sg, nhwentries, i) {
231 provider->dma_unmap(pdev, sg->dma_address, direction); 232 provider->dma_unmap(pdev, sg->dma_address, direction);
232 sg->dma_address = (dma_addr_t) NULL; 233 sg->dma_address = (dma_addr_t) NULL;
233 sg->dma_length = 0; 234 sg->dma_length = 0;
@@ -244,11 +245,11 @@ EXPORT_SYMBOL(sn_dma_unmap_sg);
244 * 245 *
245 * Maps each entry of @sg for DMA. 246 * Maps each entry of @sg for DMA.
246 */ 247 */
247int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries, 248int sn_dma_map_sg(struct device *dev, struct scatterlist *sgl, int nhwentries,
248 int direction) 249 int direction)
249{ 250{
250 unsigned long phys_addr; 251 unsigned long phys_addr;
251 struct scatterlist *saved_sg = sg; 252 struct scatterlist *saved_sg = sgl, *sg;
252 struct pci_dev *pdev = to_pci_dev(dev); 253 struct pci_dev *pdev = to_pci_dev(dev);
253 struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev); 254 struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
254 int i; 255 int i;
@@ -258,7 +259,7 @@ int sn_dma_map_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
258 /* 259 /*
259 * Setup a DMA address for each entry in the scatterlist. 260 * Setup a DMA address for each entry in the scatterlist.
260 */ 261 */
261 for (i = 0; i < nhwentries; i++, sg++) { 262 for_each_sg(sgl, sg, nhwentries, i) {
262 phys_addr = SG_ENT_PHYS_ADDRESS(sg); 263 phys_addr = SG_ENT_PHYS_ADDRESS(sg);
263 sg->dma_address = provider->dma_map(pdev, 264 sg->dma_address = provider->dma_map(pdev,
264 phys_addr, sg->length, 265 phys_addr, sg->length,
diff --git a/arch/powerpc/kernel/dma_64.c b/arch/powerpc/kernel/dma_64.c
index 7b0e754383cf..9001104b56b0 100644
--- a/arch/powerpc/kernel/dma_64.c
+++ b/arch/powerpc/kernel/dma_64.c
@@ -154,12 +154,13 @@ static void dma_direct_unmap_single(struct device *dev, dma_addr_t dma_addr,
154{ 154{
155} 155}
156 156
157static int dma_direct_map_sg(struct device *dev, struct scatterlist *sg, 157static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
158 int nents, enum dma_data_direction direction) 158 int nents, enum dma_data_direction direction)
159{ 159{
160 struct scatterlist *sg;
160 int i; 161 int i;
161 162
162 for (i = 0; i < nents; i++, sg++) { 163 for_each_sg(sgl, sg, nents, i) {
163 sg->dma_address = (page_to_phys(sg->page) + sg->offset) | 164 sg->dma_address = (page_to_phys(sg->page) + sg->offset) |
164 dma_direct_offset; 165 dma_direct_offset;
165 sg->dma_length = sg->length; 166 sg->dma_length = sg->length;
diff --git a/arch/powerpc/kernel/ibmebus.c b/arch/powerpc/kernel/ibmebus.c
index 53bf64623bd8..2e16ca5778a3 100644
--- a/arch/powerpc/kernel/ibmebus.c
+++ b/arch/powerpc/kernel/ibmebus.c
@@ -87,15 +87,16 @@ static void ibmebus_unmap_single(struct device *dev,
87} 87}
88 88
89static int ibmebus_map_sg(struct device *dev, 89static int ibmebus_map_sg(struct device *dev,
90 struct scatterlist *sg, 90 struct scatterlist *sgl,
91 int nents, enum dma_data_direction direction) 91 int nents, enum dma_data_direction direction)
92{ 92{
93 struct scatterlist *sg;
93 int i; 94 int i;
94 95
95 for (i = 0; i < nents; i++) { 96 for_each_sg(sgl, sg, nents, i) {
96 sg[i].dma_address = (dma_addr_t)page_address(sg[i].page) 97 sg->dma_address = (dma_addr_t)page_address(sg->page)
97 + sg[i].offset; 98 + sg->offset;
98 sg[i].dma_length = sg[i].length; 99 sg->dma_length = sg->length;
99 } 100 }
100 101
101 return nents; 102 return nents;
diff --git a/arch/powerpc/kernel/iommu.c b/arch/powerpc/kernel/iommu.c
index e4ec6eee81a8..306a6f75b6c5 100644
--- a/arch/powerpc/kernel/iommu.c
+++ b/arch/powerpc/kernel/iommu.c
@@ -277,7 +277,7 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
277 dma_addr_t dma_next = 0, dma_addr; 277 dma_addr_t dma_next = 0, dma_addr;
278 unsigned long flags; 278 unsigned long flags;
279 struct scatterlist *s, *outs, *segstart; 279 struct scatterlist *s, *outs, *segstart;
280 int outcount, incount; 280 int outcount, incount, i;
281 unsigned long handle; 281 unsigned long handle;
282 282
283 BUG_ON(direction == DMA_NONE); 283 BUG_ON(direction == DMA_NONE);
@@ -297,7 +297,7 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
297 297
298 spin_lock_irqsave(&(tbl->it_lock), flags); 298 spin_lock_irqsave(&(tbl->it_lock), flags);
299 299
300 for (s = outs; nelems; nelems--, s++) { 300 for_each_sg(sglist, s, nelems, i) {
301 unsigned long vaddr, npages, entry, slen; 301 unsigned long vaddr, npages, entry, slen;
302 302
303 slen = s->length; 303 slen = s->length;
@@ -341,7 +341,8 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
341 if (novmerge || (dma_addr != dma_next)) { 341 if (novmerge || (dma_addr != dma_next)) {
342 /* Can't merge: create a new segment */ 342 /* Can't merge: create a new segment */
343 segstart = s; 343 segstart = s;
344 outcount++; outs++; 344 outcount++;
345 outs = sg_next(outs);
345 DBG(" can't merge, new segment.\n"); 346 DBG(" can't merge, new segment.\n");
346 } else { 347 } else {
347 outs->dma_length += s->length; 348 outs->dma_length += s->length;
@@ -374,7 +375,7 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
374 * next entry of the sglist if we didn't fill the list completely 375 * next entry of the sglist if we didn't fill the list completely
375 */ 376 */
376 if (outcount < incount) { 377 if (outcount < incount) {
377 outs++; 378 outs = sg_next(outs);
378 outs->dma_address = DMA_ERROR_CODE; 379 outs->dma_address = DMA_ERROR_CODE;
379 outs->dma_length = 0; 380 outs->dma_length = 0;
380 } 381 }
@@ -385,7 +386,7 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
385 return outcount; 386 return outcount;
386 387
387 failure: 388 failure:
388 for (s = &sglist[0]; s <= outs; s++) { 389 for_each_sg(sglist, s, nelems, i) {
389 if (s->dma_length != 0) { 390 if (s->dma_length != 0) {
390 unsigned long vaddr, npages; 391 unsigned long vaddr, npages;
391 392
@@ -395,6 +396,8 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
395 s->dma_address = DMA_ERROR_CODE; 396 s->dma_address = DMA_ERROR_CODE;
396 s->dma_length = 0; 397 s->dma_length = 0;
397 } 398 }
399 if (s == outs)
400 break;
398 } 401 }
399 spin_unlock_irqrestore(&(tbl->it_lock), flags); 402 spin_unlock_irqrestore(&(tbl->it_lock), flags);
400 return 0; 403 return 0;
@@ -404,6 +407,7 @@ int iommu_map_sg(struct iommu_table *tbl, struct scatterlist *sglist,
404void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist, 407void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
405 int nelems, enum dma_data_direction direction) 408 int nelems, enum dma_data_direction direction)
406{ 409{
410 struct scatterlist *sg;
407 unsigned long flags; 411 unsigned long flags;
408 412
409 BUG_ON(direction == DMA_NONE); 413 BUG_ON(direction == DMA_NONE);
@@ -413,15 +417,16 @@ void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
413 417
414 spin_lock_irqsave(&(tbl->it_lock), flags); 418 spin_lock_irqsave(&(tbl->it_lock), flags);
415 419
420 sg = sglist;
416 while (nelems--) { 421 while (nelems--) {
417 unsigned int npages; 422 unsigned int npages;
418 dma_addr_t dma_handle = sglist->dma_address; 423 dma_addr_t dma_handle = sg->dma_address;
419 424
420 if (sglist->dma_length == 0) 425 if (sg->dma_length == 0)
421 break; 426 break;
422 npages = iommu_num_pages(dma_handle,sglist->dma_length); 427 npages = iommu_num_pages(dma_handle, sg->dma_length);
423 __iommu_free(tbl, dma_handle, npages); 428 __iommu_free(tbl, dma_handle, npages);
424 sglist++; 429 sg = sg_next(sg);
425 } 430 }
426 431
427 /* Flush/invalidate TLBs if necessary. As for iommu_free(), we 432 /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
diff --git a/arch/powerpc/platforms/ps3/system-bus.c b/arch/powerpc/platforms/ps3/system-bus.c
index 190ff4b59a55..07e64b48e7fc 100644
--- a/arch/powerpc/platforms/ps3/system-bus.c
+++ b/arch/powerpc/platforms/ps3/system-bus.c
@@ -616,17 +616,18 @@ static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
616 } 616 }
617} 617}
618 618
619static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sg, int nents, 619static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl,
620 enum dma_data_direction direction) 620 int nents, enum dma_data_direction direction)
621{ 621{
622#if defined(CONFIG_PS3_DYNAMIC_DMA) 622#if defined(CONFIG_PS3_DYNAMIC_DMA)
623 BUG_ON("do"); 623 BUG_ON("do");
624 return -EPERM; 624 return -EPERM;
625#else 625#else
626 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); 626 struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev);
627 struct scatterlist *sg;
627 int i; 628 int i;
628 629
629 for (i = 0; i < nents; i++, sg++) { 630 for_each_sg(sgl, sg, nents, i) {
630 int result = ps3_dma_map(dev->d_region, 631 int result = ps3_dma_map(dev->d_region,
631 page_to_phys(sg->page) + sg->offset, sg->length, 632 page_to_phys(sg->page) + sg->offset, sg->length,
632 &sg->dma_address, 0); 633 &sg->dma_address, 0);
diff --git a/arch/sparc/kernel/ioport.c b/arch/sparc/kernel/ioport.c
index 62182d2d7b0d..9c3ed88853f3 100644
--- a/arch/sparc/kernel/ioport.c
+++ b/arch/sparc/kernel/ioport.c
@@ -35,6 +35,7 @@
35#include <linux/slab.h> 35#include <linux/slab.h>
36#include <linux/pci.h> /* struct pci_dev */ 36#include <linux/pci.h> /* struct pci_dev */
37#include <linux/proc_fs.h> 37#include <linux/proc_fs.h>
38#include <linux/scatterlist.h>
38 39
39#include <asm/io.h> 40#include <asm/io.h>
40#include <asm/vaddrs.h> 41#include <asm/vaddrs.h>
@@ -717,19 +718,19 @@ void pci_unmap_page(struct pci_dev *hwdev,
717 * Device ownership issues as mentioned above for pci_map_single are 718 * Device ownership issues as mentioned above for pci_map_single are
718 * the same here. 719 * the same here.
719 */ 720 */
720int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, 721int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
721 int direction) 722 int direction)
722{ 723{
724 struct scatterlist *sg;
723 int n; 725 int n;
724 726
725 BUG_ON(direction == PCI_DMA_NONE); 727 BUG_ON(direction == PCI_DMA_NONE);
726 /* IIep is write-through, not flushing. */ 728 /* IIep is write-through, not flushing. */
727 for (n = 0; n < nents; n++) { 729 for_each_sg(sgl, sg, nents, n) {
728 BUG_ON(page_address(sg->page) == NULL); 730 BUG_ON(page_address(sg->page) == NULL);
729 sg->dvma_address = 731 sg->dvma_address =
730 virt_to_phys(page_address(sg->page)) + sg->offset; 732 virt_to_phys(page_address(sg->page)) + sg->offset;
731 sg->dvma_length = sg->length; 733 sg->dvma_length = sg->length;
732 sg++;
733 } 734 }
734 return nents; 735 return nents;
735} 736}
@@ -738,19 +739,19 @@ int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
738 * Again, cpu read rules concerning calls here are the same as for 739 * Again, cpu read rules concerning calls here are the same as for
739 * pci_unmap_single() above. 740 * pci_unmap_single() above.
740 */ 741 */
741void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, 742void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
742 int direction) 743 int direction)
743{ 744{
745 struct scatterlist *sg;
744 int n; 746 int n;
745 747
746 BUG_ON(direction == PCI_DMA_NONE); 748 BUG_ON(direction == PCI_DMA_NONE);
747 if (direction != PCI_DMA_TODEVICE) { 749 if (direction != PCI_DMA_TODEVICE) {
748 for (n = 0; n < nents; n++) { 750 for_each_sg(sgl, sg, nents, n) {
749 BUG_ON(page_address(sg->page) == NULL); 751 BUG_ON(page_address(sg->page) == NULL);
750 mmu_inval_dma_area( 752 mmu_inval_dma_area(
751 (unsigned long) page_address(sg->page), 753 (unsigned long) page_address(sg->page),
752 (sg->length + PAGE_SIZE-1) & PAGE_MASK); 754 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
753 sg++;
754 } 755 }
755 } 756 }
756} 757}
@@ -789,34 +790,34 @@ void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t
789 * The same as pci_dma_sync_single_* but for a scatter-gather list, 790 * The same as pci_dma_sync_single_* but for a scatter-gather list,
790 * same rules and usage. 791 * same rules and usage.
791 */ 792 */
792void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) 793void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
793{ 794{
795 struct scatterlist *sg;
794 int n; 796 int n;
795 797
796 BUG_ON(direction == PCI_DMA_NONE); 798 BUG_ON(direction == PCI_DMA_NONE);
797 if (direction != PCI_DMA_TODEVICE) { 799 if (direction != PCI_DMA_TODEVICE) {
798 for (n = 0; n < nents; n++) { 800 for_each_sg(sgl, sg, nents, n) {
799 BUG_ON(page_address(sg->page) == NULL); 801 BUG_ON(page_address(sg->page) == NULL);
800 mmu_inval_dma_area( 802 mmu_inval_dma_area(
801 (unsigned long) page_address(sg->page), 803 (unsigned long) page_address(sg->page),
802 (sg->length + PAGE_SIZE-1) & PAGE_MASK); 804 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
803 sg++;
804 } 805 }
805 } 806 }
806} 807}
807 808
808void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) 809void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
809{ 810{
811 struct scatterlist *sg;
810 int n; 812 int n;
811 813
812 BUG_ON(direction == PCI_DMA_NONE); 814 BUG_ON(direction == PCI_DMA_NONE);
813 if (direction != PCI_DMA_TODEVICE) { 815 if (direction != PCI_DMA_TODEVICE) {
814 for (n = 0; n < nents; n++) { 816 for_each_sg(sgl, sg, nents, n) {
815 BUG_ON(page_address(sg->page) == NULL); 817 BUG_ON(page_address(sg->page) == NULL);
816 mmu_inval_dma_area( 818 mmu_inval_dma_area(
817 (unsigned long) page_address(sg->page), 819 (unsigned long) page_address(sg->page),
818 (sg->length + PAGE_SIZE-1) & PAGE_MASK); 820 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
819 sg++;
820 } 821 }
821 } 822 }
822} 823}
diff --git a/arch/sparc/mm/io-unit.c b/arch/sparc/mm/io-unit.c
index 7c89893b1fe8..375b4db63704 100644
--- a/arch/sparc/mm/io-unit.c
+++ b/arch/sparc/mm/io-unit.c
@@ -11,8 +11,8 @@
11#include <linux/mm.h> 11#include <linux/mm.h>
12#include <linux/highmem.h> /* pte_offset_map => kmap_atomic */ 12#include <linux/highmem.h> /* pte_offset_map => kmap_atomic */
13#include <linux/bitops.h> 13#include <linux/bitops.h>
14#include <linux/scatterlist.h>
14 15
15#include <asm/scatterlist.h>
16#include <asm/pgalloc.h> 16#include <asm/pgalloc.h>
17#include <asm/pgtable.h> 17#include <asm/pgtable.h>
18#include <asm/sbus.h> 18#include <asm/sbus.h>
@@ -144,8 +144,9 @@ static void iounit_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus
144 spin_lock_irqsave(&iounit->lock, flags); 144 spin_lock_irqsave(&iounit->lock, flags);
145 while (sz != 0) { 145 while (sz != 0) {
146 --sz; 146 --sz;
147 sg[sz].dvma_address = iounit_get_area(iounit, (unsigned long)page_address(sg[sz].page) + sg[sz].offset, sg[sz].length); 147 sg->dvma_address = iounit_get_area(iounit, (unsigned long)page_address(sg->page) + sg->offset, sg->length);
148 sg[sz].dvma_length = sg[sz].length; 148 sg->dvma_length = sg->length;
149 sg = sg_next(sg);
149 } 150 }
150 spin_unlock_irqrestore(&iounit->lock, flags); 151 spin_unlock_irqrestore(&iounit->lock, flags);
151} 152}
@@ -173,11 +174,12 @@ static void iounit_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_
173 spin_lock_irqsave(&iounit->lock, flags); 174 spin_lock_irqsave(&iounit->lock, flags);
174 while (sz != 0) { 175 while (sz != 0) {
175 --sz; 176 --sz;
176 len = ((sg[sz].dvma_address & ~PAGE_MASK) + sg[sz].length + (PAGE_SIZE-1)) >> PAGE_SHIFT; 177 len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
177 vaddr = (sg[sz].dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT; 178 vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
178 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); 179 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
179 for (len += vaddr; vaddr < len; vaddr++) 180 for (len += vaddr; vaddr < len; vaddr++)
180 clear_bit(vaddr, iounit->bmap); 181 clear_bit(vaddr, iounit->bmap);
182 sg = sg_next(sg);
181 } 183 }
182 spin_unlock_irqrestore(&iounit->lock, flags); 184 spin_unlock_irqrestore(&iounit->lock, flags);
183} 185}
diff --git a/arch/sparc/mm/iommu.c b/arch/sparc/mm/iommu.c
index 52e907af9d29..283656d9f6ea 100644
--- a/arch/sparc/mm/iommu.c
+++ b/arch/sparc/mm/iommu.c
@@ -12,8 +12,8 @@
12#include <linux/mm.h> 12#include <linux/mm.h>
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/highmem.h> /* pte_offset_map => kmap_atomic */ 14#include <linux/highmem.h> /* pte_offset_map => kmap_atomic */
15#include <linux/scatterlist.h>
15 16
16#include <asm/scatterlist.h>
17#include <asm/pgalloc.h> 17#include <asm/pgalloc.h>
18#include <asm/pgtable.h> 18#include <asm/pgtable.h>
19#include <asm/sbus.h> 19#include <asm/sbus.h>
@@ -240,7 +240,7 @@ static void iommu_get_scsi_sgl_noflush(struct scatterlist *sg, int sz, struct sb
240 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT; 240 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
241 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset; 241 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
242 sg->dvma_length = (__u32) sg->length; 242 sg->dvma_length = (__u32) sg->length;
243 sg++; 243 sg = sg_next(sg);
244 } 244 }
245} 245}
246 246
@@ -254,7 +254,7 @@ static void iommu_get_scsi_sgl_gflush(struct scatterlist *sg, int sz, struct sbu
254 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT; 254 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
255 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset; 255 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
256 sg->dvma_length = (__u32) sg->length; 256 sg->dvma_length = (__u32) sg->length;
257 sg++; 257 sg = sg_next(sg);
258 } 258 }
259} 259}
260 260
@@ -285,7 +285,7 @@ static void iommu_get_scsi_sgl_pflush(struct scatterlist *sg, int sz, struct sbu
285 285
286 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset; 286 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
287 sg->dvma_length = (__u32) sg->length; 287 sg->dvma_length = (__u32) sg->length;
288 sg++; 288 sg = sg_next(sg);
289 } 289 }
290} 290}
291 291
@@ -325,7 +325,7 @@ static void iommu_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_b
325 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT; 325 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
326 iommu_release_one(sg->dvma_address & PAGE_MASK, n, sbus); 326 iommu_release_one(sg->dvma_address & PAGE_MASK, n, sbus);
327 sg->dvma_address = 0x21212121; 327 sg->dvma_address = 0x21212121;
328 sg++; 328 sg = sg_next(sg);
329 } 329 }
330} 330}
331 331
diff --git a/arch/sparc/mm/sun4c.c b/arch/sparc/mm/sun4c.c
index 005a3e72d4f2..ee6708fc4492 100644
--- a/arch/sparc/mm/sun4c.c
+++ b/arch/sparc/mm/sun4c.c
@@ -17,8 +17,8 @@
17#include <linux/highmem.h> 17#include <linux/highmem.h>
18#include <linux/fs.h> 18#include <linux/fs.h>
19#include <linux/seq_file.h> 19#include <linux/seq_file.h>
20#include <linux/scatterlist.h>
20 21
21#include <asm/scatterlist.h>
22#include <asm/page.h> 22#include <asm/page.h>
23#include <asm/pgalloc.h> 23#include <asm/pgalloc.h>
24#include <asm/pgtable.h> 24#include <asm/pgtable.h>
@@ -1228,8 +1228,9 @@ static void sun4c_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *
1228{ 1228{
1229 while (sz != 0) { 1229 while (sz != 0) {
1230 --sz; 1230 --sz;
1231 sg[sz].dvma_address = (__u32)sun4c_lockarea(page_address(sg[sz].page) + sg[sz].offset, sg[sz].length); 1231 sg->dvma_address = (__u32)sun4c_lockarea(page_address(sg->page) + sg->offset, sg->length);
1232 sg[sz].dvma_length = sg[sz].length; 1232 sg->dvma_length = sg->length;
1233 sg = sg_next(sg);
1233 } 1234 }
1234} 1235}
1235 1236
@@ -1244,7 +1245,8 @@ static void sun4c_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_b
1244{ 1245{
1245 while (sz != 0) { 1246 while (sz != 0) {
1246 --sz; 1247 --sz;
1247 sun4c_unlockarea((char *)sg[sz].dvma_address, sg[sz].length); 1248 sun4c_unlockarea((char *)sg->dvma_address, sg->length);
1249 sg = sg_next(sg);
1248 } 1250 }
1249} 1251}
1250 1252
diff --git a/arch/sparc64/kernel/iommu.c b/arch/sparc64/kernel/iommu.c
index b35a62167e9c..db3ffcf7a120 100644
--- a/arch/sparc64/kernel/iommu.c
+++ b/arch/sparc64/kernel/iommu.c
@@ -10,6 +10,7 @@
10#include <linux/device.h> 10#include <linux/device.h>
11#include <linux/dma-mapping.h> 11#include <linux/dma-mapping.h>
12#include <linux/errno.h> 12#include <linux/errno.h>
13#include <linux/scatterlist.h>
13 14
14#ifdef CONFIG_PCI 15#ifdef CONFIG_PCI
15#include <linux/pci.h> 16#include <linux/pci.h>
@@ -480,7 +481,7 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
480 unsigned long iopte_protection) 481 unsigned long iopte_protection)
481{ 482{
482 struct scatterlist *dma_sg = sg; 483 struct scatterlist *dma_sg = sg;
483 struct scatterlist *sg_end = sg + nelems; 484 struct scatterlist *sg_end = sg_last(sg, nelems);
484 int i; 485 int i;
485 486
486 for (i = 0; i < nused; i++) { 487 for (i = 0; i < nused; i++) {
@@ -515,7 +516,7 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
515 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL))); 516 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
516 break; 517 break;
517 } 518 }
518 sg++; 519 sg = sg_next(sg);
519 } 520 }
520 521
521 pteval = iopte_protection | (pteval & IOPTE_PAGE); 522 pteval = iopte_protection | (pteval & IOPTE_PAGE);
@@ -528,24 +529,24 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
528 } 529 }
529 530
530 pteval = (pteval & IOPTE_PAGE) + len; 531 pteval = (pteval & IOPTE_PAGE) + len;
531 sg++; 532 sg = sg_next(sg);
532 533
533 /* Skip over any tail mappings we've fully mapped, 534 /* Skip over any tail mappings we've fully mapped,
534 * adjusting pteval along the way. Stop when we 535 * adjusting pteval along the way. Stop when we
535 * detect a page crossing event. 536 * detect a page crossing event.
536 */ 537 */
537 while (sg < sg_end && 538 while (sg != sg_end &&
538 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL && 539 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
539 (pteval == SG_ENT_PHYS_ADDRESS(sg)) && 540 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
540 ((pteval ^ 541 ((pteval ^
541 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) { 542 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
542 pteval += sg->length; 543 pteval += sg->length;
543 sg++; 544 sg = sg_next(sg);
544 } 545 }
545 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL) 546 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
546 pteval = ~0UL; 547 pteval = ~0UL;
547 } while (dma_npages != 0); 548 } while (dma_npages != 0);
548 dma_sg++; 549 dma_sg = sg_next(dma_sg);
549 } 550 }
550} 551}
551 552
@@ -606,7 +607,7 @@ static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
606 sgtmp = sglist; 607 sgtmp = sglist;
607 while (used && sgtmp->dma_length) { 608 while (used && sgtmp->dma_length) {
608 sgtmp->dma_address += dma_base; 609 sgtmp->dma_address += dma_base;
609 sgtmp++; 610 sgtmp = sg_next(sgtmp);
610 used--; 611 used--;
611 } 612 }
612 used = nelems - used; 613 used = nelems - used;
@@ -642,6 +643,7 @@ static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
642 struct strbuf *strbuf; 643 struct strbuf *strbuf;
643 iopte_t *base; 644 iopte_t *base;
644 unsigned long flags, ctx, i, npages; 645 unsigned long flags, ctx, i, npages;
646 struct scatterlist *sg, *sgprv;
645 u32 bus_addr; 647 u32 bus_addr;
646 648
647 if (unlikely(direction == DMA_NONE)) { 649 if (unlikely(direction == DMA_NONE)) {
@@ -654,11 +656,14 @@ static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
654 656
655 bus_addr = sglist->dma_address & IO_PAGE_MASK; 657 bus_addr = sglist->dma_address & IO_PAGE_MASK;
656 658
657 for (i = 1; i < nelems; i++) 659 sgprv = NULL;
658 if (sglist[i].dma_length == 0) 660 for_each_sg(sglist, sg, nelems, i) {
661 if (sg->dma_length == 0)
659 break; 662 break;
660 i--; 663 sgprv = sg;
661 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - 664 }
665
666 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
662 bus_addr) >> IO_PAGE_SHIFT; 667 bus_addr) >> IO_PAGE_SHIFT;
663 668
664 base = iommu->page_table + 669 base = iommu->page_table +
@@ -730,6 +735,7 @@ static void dma_4u_sync_sg_for_cpu(struct device *dev,
730 struct iommu *iommu; 735 struct iommu *iommu;
731 struct strbuf *strbuf; 736 struct strbuf *strbuf;
732 unsigned long flags, ctx, npages, i; 737 unsigned long flags, ctx, npages, i;
738 struct scatterlist *sg, *sgprv;
733 u32 bus_addr; 739 u32 bus_addr;
734 740
735 iommu = dev->archdata.iommu; 741 iommu = dev->archdata.iommu;
@@ -753,11 +759,14 @@ static void dma_4u_sync_sg_for_cpu(struct device *dev,
753 759
754 /* Step 2: Kick data out of streaming buffers. */ 760 /* Step 2: Kick data out of streaming buffers. */
755 bus_addr = sglist[0].dma_address & IO_PAGE_MASK; 761 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
756 for(i = 1; i < nelems; i++) 762 sgprv = NULL;
757 if (!sglist[i].dma_length) 763 for_each_sg(sglist, sg, nelems, i) {
764 if (sg->dma_length == 0)
758 break; 765 break;
759 i--; 766 sgprv = sg;
760 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) 767 }
768
769 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
761 - bus_addr) >> IO_PAGE_SHIFT; 770 - bus_addr) >> IO_PAGE_SHIFT;
762 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 771 strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
763 772
diff --git a/arch/sparc64/kernel/pci_sun4v.c b/arch/sparc64/kernel/pci_sun4v.c
index 95de1444ee67..cacacfae5451 100644
--- a/arch/sparc64/kernel/pci_sun4v.c
+++ b/arch/sparc64/kernel/pci_sun4v.c
@@ -13,6 +13,7 @@
13#include <linux/irq.h> 13#include <linux/irq.h>
14#include <linux/msi.h> 14#include <linux/msi.h>
15#include <linux/log2.h> 15#include <linux/log2.h>
16#include <linux/scatterlist.h>
16 17
17#include <asm/iommu.h> 18#include <asm/iommu.h>
18#include <asm/irq.h> 19#include <asm/irq.h>
@@ -373,7 +374,7 @@ static inline long fill_sg(long entry, struct device *dev,
373 int nused, int nelems, unsigned long prot) 374 int nused, int nelems, unsigned long prot)
374{ 375{
375 struct scatterlist *dma_sg = sg; 376 struct scatterlist *dma_sg = sg;
376 struct scatterlist *sg_end = sg + nelems; 377 struct scatterlist *sg_end = sg_last(sg, nelems);
377 unsigned long flags; 378 unsigned long flags;
378 int i; 379 int i;
379 380
@@ -413,7 +414,7 @@ static inline long fill_sg(long entry, struct device *dev,
413 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL))); 414 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
414 break; 415 break;
415 } 416 }
416 sg++; 417 sg = sg_next(sg);
417 } 418 }
418 419
419 pteval = (pteval & IOPTE_PAGE); 420 pteval = (pteval & IOPTE_PAGE);
@@ -431,24 +432,25 @@ static inline long fill_sg(long entry, struct device *dev,
431 } 432 }
432 433
433 pteval = (pteval & IOPTE_PAGE) + len; 434 pteval = (pteval & IOPTE_PAGE) + len;
434 sg++; 435 sg = sg_next(sg);
435 436
436 /* Skip over any tail mappings we've fully mapped, 437 /* Skip over any tail mappings we've fully mapped,
437 * adjusting pteval along the way. Stop when we 438 * adjusting pteval along the way. Stop when we
438 * detect a page crossing event. 439 * detect a page crossing event.
439 */ 440 */
440 while (sg < sg_end && 441 while ((pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
441 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
442 (pteval == SG_ENT_PHYS_ADDRESS(sg)) && 442 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
443 ((pteval ^ 443 ((pteval ^
444 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) { 444 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
445 pteval += sg->length; 445 pteval += sg->length;
446 sg++; 446 if (sg == sg_end)
447 break;
448 sg = sg_next(sg);
447 } 449 }
448 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL) 450 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
449 pteval = ~0UL; 451 pteval = ~0UL;
450 } while (dma_npages != 0); 452 } while (dma_npages != 0);
451 dma_sg++; 453 dma_sg = sg_next(dma_sg);
452 } 454 }
453 455
454 if (unlikely(iommu_batch_end() < 0L)) 456 if (unlikely(iommu_batch_end() < 0L))
@@ -510,7 +512,7 @@ static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
510 sgtmp = sglist; 512 sgtmp = sglist;
511 while (used && sgtmp->dma_length) { 513 while (used && sgtmp->dma_length) {
512 sgtmp->dma_address += dma_base; 514 sgtmp->dma_address += dma_base;
513 sgtmp++; 515 sgtmp = sg_next(sgtmp);
514 used--; 516 used--;
515 } 517 }
516 used = nelems - used; 518 used = nelems - used;
@@ -545,6 +547,7 @@ static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
545 struct pci_pbm_info *pbm; 547 struct pci_pbm_info *pbm;
546 struct iommu *iommu; 548 struct iommu *iommu;
547 unsigned long flags, i, npages; 549 unsigned long flags, i, npages;
550 struct scatterlist *sg, *sgprv;
548 long entry; 551 long entry;
549 u32 devhandle, bus_addr; 552 u32 devhandle, bus_addr;
550 553
@@ -558,12 +561,15 @@ static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
558 devhandle = pbm->devhandle; 561 devhandle = pbm->devhandle;
559 562
560 bus_addr = sglist->dma_address & IO_PAGE_MASK; 563 bus_addr = sglist->dma_address & IO_PAGE_MASK;
561 564 sgprv = NULL;
562 for (i = 1; i < nelems; i++) 565 for_each_sg(sglist, sg, nelems, i) {
563 if (sglist[i].dma_length == 0) 566 if (sg->dma_length == 0)
564 break; 567 break;
565 i--; 568
566 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - 569 sgprv = sg;
570 }
571
572 npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
567 bus_addr) >> IO_PAGE_SHIFT; 573 bus_addr) >> IO_PAGE_SHIFT;
568 574
569 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); 575 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
diff --git a/arch/x86/kernel/pci-calgary_64.c b/arch/x86/kernel/pci-calgary_64.c
index 71da01e73f03..a50b787b3bfa 100644
--- a/arch/x86/kernel/pci-calgary_64.c
+++ b/arch/x86/kernel/pci-calgary_64.c
@@ -35,6 +35,7 @@
35#include <linux/pci_ids.h> 35#include <linux/pci_ids.h>
36#include <linux/pci.h> 36#include <linux/pci.h>
37#include <linux/delay.h> 37#include <linux/delay.h>
38#include <linux/scatterlist.h>
38#include <asm/iommu.h> 39#include <asm/iommu.h>
39#include <asm/calgary.h> 40#include <asm/calgary.h>
40#include <asm/tce.h> 41#include <asm/tce.h>
@@ -384,31 +385,32 @@ static void calgary_unmap_sg(struct device *dev,
384 struct scatterlist *sglist, int nelems, int direction) 385 struct scatterlist *sglist, int nelems, int direction)
385{ 386{
386 struct iommu_table *tbl = find_iommu_table(dev); 387 struct iommu_table *tbl = find_iommu_table(dev);
388 struct scatterlist *s;
389 int i;
387 390
388 if (!translate_phb(to_pci_dev(dev))) 391 if (!translate_phb(to_pci_dev(dev)))
389 return; 392 return;
390 393
391 while (nelems--) { 394 for_each_sg(sglist, s, nelems, i) {
392 unsigned int npages; 395 unsigned int npages;
393 dma_addr_t dma = sglist->dma_address; 396 dma_addr_t dma = s->dma_address;
394 unsigned int dmalen = sglist->dma_length; 397 unsigned int dmalen = s->dma_length;
395 398
396 if (dmalen == 0) 399 if (dmalen == 0)
397 break; 400 break;
398 401
399 npages = num_dma_pages(dma, dmalen); 402 npages = num_dma_pages(dma, dmalen);
400 iommu_free(tbl, dma, npages); 403 iommu_free(tbl, dma, npages);
401 sglist++;
402 } 404 }
403} 405}
404 406
405static int calgary_nontranslate_map_sg(struct device* dev, 407static int calgary_nontranslate_map_sg(struct device* dev,
406 struct scatterlist *sg, int nelems, int direction) 408 struct scatterlist *sg, int nelems, int direction)
407{ 409{
410 struct scatterlist *s;
408 int i; 411 int i;
409 412
410 for (i = 0; i < nelems; i++ ) { 413 for_each_sg(sg, s, nelems, i) {
411 struct scatterlist *s = &sg[i];
412 BUG_ON(!s->page); 414 BUG_ON(!s->page);
413 s->dma_address = virt_to_bus(page_address(s->page) +s->offset); 415 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
414 s->dma_length = s->length; 416 s->dma_length = s->length;
@@ -420,6 +422,7 @@ static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
420 int nelems, int direction) 422 int nelems, int direction)
421{ 423{
422 struct iommu_table *tbl = find_iommu_table(dev); 424 struct iommu_table *tbl = find_iommu_table(dev);
425 struct scatterlist *s;
423 unsigned long vaddr; 426 unsigned long vaddr;
424 unsigned int npages; 427 unsigned int npages;
425 unsigned long entry; 428 unsigned long entry;
@@ -428,8 +431,7 @@ static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
428 if (!translate_phb(to_pci_dev(dev))) 431 if (!translate_phb(to_pci_dev(dev)))
429 return calgary_nontranslate_map_sg(dev, sg, nelems, direction); 432 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
430 433
431 for (i = 0; i < nelems; i++ ) { 434 for_each_sg(sg, s, nelems, i) {
432 struct scatterlist *s = &sg[i];
433 BUG_ON(!s->page); 435 BUG_ON(!s->page);
434 436
435 vaddr = (unsigned long)page_address(s->page) + s->offset; 437 vaddr = (unsigned long)page_address(s->page) + s->offset;
@@ -454,9 +456,9 @@ static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
454 return nelems; 456 return nelems;
455error: 457error:
456 calgary_unmap_sg(dev, sg, nelems, direction); 458 calgary_unmap_sg(dev, sg, nelems, direction);
457 for (i = 0; i < nelems; i++) { 459 for_each_sg(sg, s, nelems, i) {
458 sg[i].dma_address = bad_dma_address; 460 sg->dma_address = bad_dma_address;
459 sg[i].dma_length = 0; 461 sg->dma_length = 0;
460 } 462 }
461 return 0; 463 return 0;
462} 464}
diff --git a/arch/x86/kernel/pci-gart_64.c b/arch/x86/kernel/pci-gart_64.c
index 4918c575d582..cfcc84e6c350 100644
--- a/arch/x86/kernel/pci-gart_64.c
+++ b/arch/x86/kernel/pci-gart_64.c
@@ -23,6 +23,7 @@
23#include <linux/interrupt.h> 23#include <linux/interrupt.h>
24#include <linux/bitops.h> 24#include <linux/bitops.h>
25#include <linux/kdebug.h> 25#include <linux/kdebug.h>
26#include <linux/scatterlist.h>
26#include <asm/atomic.h> 27#include <asm/atomic.h>
27#include <asm/io.h> 28#include <asm/io.h>
28#include <asm/mtrr.h> 29#include <asm/mtrr.h>
@@ -278,10 +279,10 @@ static void gart_unmap_single(struct device *dev, dma_addr_t dma_addr,
278 */ 279 */
279static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir) 280static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
280{ 281{
282 struct scatterlist *s;
281 int i; 283 int i;
282 284
283 for (i = 0; i < nents; i++) { 285 for_each_sg(sg, s, nents, i) {
284 struct scatterlist *s = &sg[i];
285 if (!s->dma_length || !s->length) 286 if (!s->dma_length || !s->length)
286 break; 287 break;
287 gart_unmap_single(dev, s->dma_address, s->dma_length, dir); 288 gart_unmap_single(dev, s->dma_address, s->dma_length, dir);
@@ -292,14 +293,14 @@ static void gart_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
292static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg, 293static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
293 int nents, int dir) 294 int nents, int dir)
294{ 295{
296 struct scatterlist *s;
295 int i; 297 int i;
296 298
297#ifdef CONFIG_IOMMU_DEBUG 299#ifdef CONFIG_IOMMU_DEBUG
298 printk(KERN_DEBUG "dma_map_sg overflow\n"); 300 printk(KERN_DEBUG "dma_map_sg overflow\n");
299#endif 301#endif
300 302
301 for (i = 0; i < nents; i++ ) { 303 for_each_sg(sg, s, nents, i) {
302 struct scatterlist *s = &sg[i];
303 unsigned long addr = page_to_phys(s->page) + s->offset; 304 unsigned long addr = page_to_phys(s->page) + s->offset;
304 if (nonforced_iommu(dev, addr, s->length)) { 305 if (nonforced_iommu(dev, addr, s->length)) {
305 addr = dma_map_area(dev, addr, s->length, dir); 306 addr = dma_map_area(dev, addr, s->length, dir);
@@ -319,23 +320,23 @@ static int dma_map_sg_nonforce(struct device *dev, struct scatterlist *sg,
319} 320}
320 321
321/* Map multiple scatterlist entries continuous into the first. */ 322/* Map multiple scatterlist entries continuous into the first. */
322static int __dma_map_cont(struct scatterlist *sg, int start, int stopat, 323static int __dma_map_cont(struct scatterlist *start, int nelems,
323 struct scatterlist *sout, unsigned long pages) 324 struct scatterlist *sout, unsigned long pages)
324{ 325{
325 unsigned long iommu_start = alloc_iommu(pages); 326 unsigned long iommu_start = alloc_iommu(pages);
326 unsigned long iommu_page = iommu_start; 327 unsigned long iommu_page = iommu_start;
328 struct scatterlist *s;
327 int i; 329 int i;
328 330
329 if (iommu_start == -1) 331 if (iommu_start == -1)
330 return -1; 332 return -1;
331 333
332 for (i = start; i < stopat; i++) { 334 for_each_sg(start, s, nelems, i) {
333 struct scatterlist *s = &sg[i];
334 unsigned long pages, addr; 335 unsigned long pages, addr;
335 unsigned long phys_addr = s->dma_address; 336 unsigned long phys_addr = s->dma_address;
336 337
337 BUG_ON(i > start && s->offset); 338 BUG_ON(s != start && s->offset);
338 if (i == start) { 339 if (s == start) {
339 *sout = *s; 340 *sout = *s;
340 sout->dma_address = iommu_bus_base; 341 sout->dma_address = iommu_bus_base;
341 sout->dma_address += iommu_page*PAGE_SIZE + s->offset; 342 sout->dma_address += iommu_page*PAGE_SIZE + s->offset;
@@ -357,17 +358,17 @@ static int __dma_map_cont(struct scatterlist *sg, int start, int stopat,
357 return 0; 358 return 0;
358} 359}
359 360
360static inline int dma_map_cont(struct scatterlist *sg, int start, int stopat, 361static inline int dma_map_cont(struct scatterlist *start, int nelems,
361 struct scatterlist *sout, 362 struct scatterlist *sout,
362 unsigned long pages, int need) 363 unsigned long pages, int need)
363{ 364{
364 if (!need) { 365 if (!need) {
365 BUG_ON(stopat - start != 1); 366 BUG_ON(nelems != 1);
366 *sout = sg[start]; 367 *sout = *start;
367 sout->dma_length = sg[start].length; 368 sout->dma_length = start->length;
368 return 0; 369 return 0;
369 } 370 }
370 return __dma_map_cont(sg, start, stopat, sout, pages); 371 return __dma_map_cont(start, nelems, sout, pages);
371} 372}
372 373
373/* 374/*
@@ -381,6 +382,7 @@ int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
381 int start; 382 int start;
382 unsigned long pages = 0; 383 unsigned long pages = 0;
383 int need = 0, nextneed; 384 int need = 0, nextneed;
385 struct scatterlist *s, *ps, *start_sg, *sgmap;
384 386
385 if (nents == 0) 387 if (nents == 0)
386 return 0; 388 return 0;
@@ -390,8 +392,9 @@ int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
390 392
391 out = 0; 393 out = 0;
392 start = 0; 394 start = 0;
393 for (i = 0; i < nents; i++) { 395 start_sg = sgmap = sg;
394 struct scatterlist *s = &sg[i]; 396 ps = NULL; /* shut up gcc */
397 for_each_sg(sg, s, nents, i) {
395 dma_addr_t addr = page_to_phys(s->page) + s->offset; 398 dma_addr_t addr = page_to_phys(s->page) + s->offset;
396 s->dma_address = addr; 399 s->dma_address = addr;
397 BUG_ON(s->length == 0); 400 BUG_ON(s->length == 0);
@@ -400,29 +403,33 @@ int gart_map_sg(struct device *dev, struct scatterlist *sg, int nents, int dir)
400 403
401 /* Handle the previous not yet processed entries */ 404 /* Handle the previous not yet processed entries */
402 if (i > start) { 405 if (i > start) {
403 struct scatterlist *ps = &sg[i-1];
404 /* Can only merge when the last chunk ends on a page 406 /* Can only merge when the last chunk ends on a page
405 boundary and the new one doesn't have an offset. */ 407 boundary and the new one doesn't have an offset. */
406 if (!iommu_merge || !nextneed || !need || s->offset || 408 if (!iommu_merge || !nextneed || !need || s->offset ||
407 (ps->offset + ps->length) % PAGE_SIZE) { 409 (ps->offset + ps->length) % PAGE_SIZE) {
408 if (dma_map_cont(sg, start, i, sg+out, pages, 410 if (dma_map_cont(start_sg, i - start, sgmap,
409 need) < 0) 411 pages, need) < 0)
410 goto error; 412 goto error;
411 out++; 413 out++;
414 sgmap = sg_next(sgmap);
412 pages = 0; 415 pages = 0;
413 start = i; 416 start = i;
417 start_sg = s;
414 } 418 }
415 } 419 }
416 420
417 need = nextneed; 421 need = nextneed;
418 pages += to_pages(s->offset, s->length); 422 pages += to_pages(s->offset, s->length);
423 ps = s;
419 } 424 }
420 if (dma_map_cont(sg, start, i, sg+out, pages, need) < 0) 425 if (dma_map_cont(start_sg, i - start, sgmap, pages, need) < 0)
421 goto error; 426 goto error;
422 out++; 427 out++;
423 flush_gart(); 428 flush_gart();
424 if (out < nents) 429 if (out < nents) {
425 sg[out].dma_length = 0; 430 sgmap = sg_next(sgmap);
431 sgmap->dma_length = 0;
432 }
426 return out; 433 return out;
427 434
428error: 435error:
@@ -437,8 +444,8 @@ error:
437 if (panic_on_overflow) 444 if (panic_on_overflow)
438 panic("dma_map_sg: overflow on %lu pages\n", pages); 445 panic("dma_map_sg: overflow on %lu pages\n", pages);
439 iommu_full(dev, pages << PAGE_SHIFT, dir); 446 iommu_full(dev, pages << PAGE_SHIFT, dir);
440 for (i = 0; i < nents; i++) 447 for_each_sg(sg, s, nents, i)
441 sg[i].dma_address = bad_dma_address; 448 s->dma_address = bad_dma_address;
442 return 0; 449 return 0;
443} 450}
444 451
diff --git a/arch/x86/kernel/pci-nommu_64.c b/arch/x86/kernel/pci-nommu_64.c
index 2a34c6c025a9..e85d4360360c 100644
--- a/arch/x86/kernel/pci-nommu_64.c
+++ b/arch/x86/kernel/pci-nommu_64.c
@@ -5,6 +5,7 @@
5#include <linux/pci.h> 5#include <linux/pci.h>
6#include <linux/string.h> 6#include <linux/string.h>
7#include <linux/dma-mapping.h> 7#include <linux/dma-mapping.h>
8#include <linux/scatterlist.h>
8 9
9#include <asm/iommu.h> 10#include <asm/iommu.h>
10#include <asm/processor.h> 11#include <asm/processor.h>
@@ -57,10 +58,10 @@ static void nommu_unmap_single(struct device *dev, dma_addr_t addr,size_t size,
57static int nommu_map_sg(struct device *hwdev, struct scatterlist *sg, 58static int nommu_map_sg(struct device *hwdev, struct scatterlist *sg,
58 int nents, int direction) 59 int nents, int direction)
59{ 60{
61 struct scatterlist *s;
60 int i; 62 int i;
61 63
62 for (i = 0; i < nents; i++ ) { 64 for_each_sg(sg, s, nents, i) {
63 struct scatterlist *s = &sg[i];
64 BUG_ON(!s->page); 65 BUG_ON(!s->page);
65 s->dma_address = virt_to_bus(page_address(s->page) +s->offset); 66 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
66 if (!check_addr("map_sg", hwdev, s->dma_address, s->length)) 67 if (!check_addr("map_sg", hwdev, s->dma_address, s->length))
diff --git a/block/ll_rw_blk.c b/block/ll_rw_blk.c
index 4df7d027eb06..527bd8d4db50 100644
--- a/block/ll_rw_blk.c
+++ b/block/ll_rw_blk.c
@@ -30,6 +30,7 @@
30#include <linux/cpu.h> 30#include <linux/cpu.h>
31#include <linux/blktrace_api.h> 31#include <linux/blktrace_api.h>
32#include <linux/fault-inject.h> 32#include <linux/fault-inject.h>
33#include <linux/scatterlist.h>
33 34
34/* 35/*
35 * for max sense size 36 * for max sense size
@@ -1318,9 +1319,10 @@ static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
1318 * must make sure sg can hold rq->nr_phys_segments entries 1319 * must make sure sg can hold rq->nr_phys_segments entries
1319 */ 1320 */
1320int blk_rq_map_sg(struct request_queue *q, struct request *rq, 1321int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1321 struct scatterlist *sg) 1322 struct scatterlist *sglist)
1322{ 1323{
1323 struct bio_vec *bvec, *bvprv; 1324 struct bio_vec *bvec, *bvprv;
1325 struct scatterlist *next_sg, *sg;
1324 struct req_iterator iter; 1326 struct req_iterator iter;
1325 int nsegs, cluster; 1327 int nsegs, cluster;
1326 1328
@@ -1331,11 +1333,12 @@ int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1331 * for each bio in rq 1333 * for each bio in rq
1332 */ 1334 */
1333 bvprv = NULL; 1335 bvprv = NULL;
1336 sg = next_sg = &sglist[0];
1334 rq_for_each_segment(bvec, rq, iter) { 1337 rq_for_each_segment(bvec, rq, iter) {
1335 int nbytes = bvec->bv_len; 1338 int nbytes = bvec->bv_len;
1336 1339
1337 if (bvprv && cluster) { 1340 if (bvprv && cluster) {
1338 if (sg[nsegs - 1].length + nbytes > q->max_segment_size) 1341 if (sg->length + nbytes > q->max_segment_size)
1339 goto new_segment; 1342 goto new_segment;
1340 1343
1341 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec)) 1344 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
@@ -1343,14 +1346,15 @@ int blk_rq_map_sg(struct request_queue *q, struct request *rq,
1343 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec)) 1346 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1344 goto new_segment; 1347 goto new_segment;
1345 1348
1346 sg[nsegs - 1].length += nbytes; 1349 sg->length += nbytes;
1347 } else { 1350 } else {
1348new_segment: 1351new_segment:
1349 memset(&sg[nsegs],0,sizeof(struct scatterlist)); 1352 sg = next_sg;
1350 sg[nsegs].page = bvec->bv_page; 1353 next_sg = sg_next(sg);
1351 sg[nsegs].length = nbytes;
1352 sg[nsegs].offset = bvec->bv_offset;
1353 1354
1355 sg->page = bvec->bv_page;
1356 sg->length = nbytes;
1357 sg->offset = bvec->bv_offset;
1354 nsegs++; 1358 nsegs++;
1355 } 1359 }
1356 bvprv = bvec; 1360 bvprv = bvec;
@@ -4068,7 +4072,23 @@ static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
4068 return queue_var_show(max_hw_sectors_kb, (page)); 4072 return queue_var_show(max_hw_sectors_kb, (page));
4069} 4073}
4070 4074
4075static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
4076{
4077 return queue_var_show(q->max_phys_segments, page);
4078}
4079
4080static ssize_t queue_max_segments_store(struct request_queue *q,
4081 const char *page, size_t count)
4082{
4083 unsigned long segments;
4084 ssize_t ret = queue_var_store(&segments, page, count);
4071 4085
4086 spin_lock_irq(q->queue_lock);
4087 q->max_phys_segments = segments;
4088 spin_unlock_irq(q->queue_lock);
4089
4090 return ret;
4091}
4072static struct queue_sysfs_entry queue_requests_entry = { 4092static struct queue_sysfs_entry queue_requests_entry = {
4073 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 4093 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
4074 .show = queue_requests_show, 4094 .show = queue_requests_show,
@@ -4092,6 +4112,12 @@ static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
4092 .show = queue_max_hw_sectors_show, 4112 .show = queue_max_hw_sectors_show,
4093}; 4113};
4094 4114
4115static struct queue_sysfs_entry queue_max_segments_entry = {
4116 .attr = {.name = "max_segments", .mode = S_IRUGO | S_IWUSR },
4117 .show = queue_max_segments_show,
4118 .store = queue_max_segments_store,
4119};
4120
4095static struct queue_sysfs_entry queue_iosched_entry = { 4121static struct queue_sysfs_entry queue_iosched_entry = {
4096 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, 4122 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
4097 .show = elv_iosched_show, 4123 .show = elv_iosched_show,
@@ -4103,6 +4129,7 @@ static struct attribute *default_attrs[] = {
4103 &queue_ra_entry.attr, 4129 &queue_ra_entry.attr,
4104 &queue_max_hw_sectors_entry.attr, 4130 &queue_max_hw_sectors_entry.attr,
4105 &queue_max_sectors_entry.attr, 4131 &queue_max_sectors_entry.attr,
4132 &queue_max_segments_entry.attr,
4106 &queue_iosched_entry.attr, 4133 &queue_iosched_entry.attr,
4107 NULL, 4134 NULL,
4108}; 4135};
diff --git a/crypto/digest.c b/crypto/digest.c
index 1bf7414aeb9e..e56de6748b15 100644
--- a/crypto/digest.c
+++ b/crypto/digest.c
@@ -77,7 +77,7 @@ static int update2(struct hash_desc *desc,
77 77
78 if (!nbytes) 78 if (!nbytes)
79 break; 79 break;
80 sg = sg_next(sg); 80 sg = scatterwalk_sg_next(sg);
81 } 81 }
82 82
83 return 0; 83 return 0;
diff --git a/crypto/scatterwalk.c b/crypto/scatterwalk.c
index 3052f6507f53..d6852c33cfb7 100644
--- a/crypto/scatterwalk.c
+++ b/crypto/scatterwalk.c
@@ -62,7 +62,7 @@ static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
62 walk->offset += PAGE_SIZE - 1; 62 walk->offset += PAGE_SIZE - 1;
63 walk->offset &= PAGE_MASK; 63 walk->offset &= PAGE_MASK;
64 if (walk->offset >= walk->sg->offset + walk->sg->length) 64 if (walk->offset >= walk->sg->offset + walk->sg->length)
65 scatterwalk_start(walk, sg_next(walk->sg)); 65 scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
66 } 66 }
67} 67}
68 68
diff --git a/crypto/scatterwalk.h b/crypto/scatterwalk.h
index 500a220ad908..9c73e37a42ce 100644
--- a/crypto/scatterwalk.h
+++ b/crypto/scatterwalk.h
@@ -20,7 +20,7 @@
20 20
21#include "internal.h" 21#include "internal.h"
22 22
23static inline struct scatterlist *sg_next(struct scatterlist *sg) 23static inline struct scatterlist *scatterwalk_sg_next(struct scatterlist *sg)
24{ 24{
25 return (++sg)->length ? sg : (void *)sg->page; 25 return (++sg)->length ? sg : (void *)sg->page;
26} 26}
diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index 68699b3e7998..bbaa545ea999 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -1410,7 +1410,7 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1410 */ 1410 */
1411unsigned ata_exec_internal_sg(struct ata_device *dev, 1411unsigned ata_exec_internal_sg(struct ata_device *dev,
1412 struct ata_taskfile *tf, const u8 *cdb, 1412 struct ata_taskfile *tf, const u8 *cdb,
1413 int dma_dir, struct scatterlist *sg, 1413 int dma_dir, struct scatterlist *sgl,
1414 unsigned int n_elem, unsigned long timeout) 1414 unsigned int n_elem, unsigned long timeout)
1415{ 1415{
1416 struct ata_link *link = dev->link; 1416 struct ata_link *link = dev->link;
@@ -1472,11 +1472,12 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
1472 qc->dma_dir = dma_dir; 1472 qc->dma_dir = dma_dir;
1473 if (dma_dir != DMA_NONE) { 1473 if (dma_dir != DMA_NONE) {
1474 unsigned int i, buflen = 0; 1474 unsigned int i, buflen = 0;
1475 struct scatterlist *sg;
1475 1476
1476 for (i = 0; i < n_elem; i++) 1477 for_each_sg(sgl, sg, n_elem, i)
1477 buflen += sg[i].length; 1478 buflen += sg->length;
1478 1479
1479 ata_sg_init(qc, sg, n_elem); 1480 ata_sg_init(qc, sgl, n_elem);
1480 qc->nbytes = buflen; 1481 qc->nbytes = buflen;
1481 } 1482 }
1482 1483
@@ -4292,7 +4293,7 @@ void ata_sg_clean(struct ata_queued_cmd *qc)
4292 if (qc->n_elem) 4293 if (qc->n_elem)
4293 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir); 4294 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4294 /* restore last sg */ 4295 /* restore last sg */
4295 sg[qc->orig_n_elem - 1].length += qc->pad_len; 4296 sg_last(sg, qc->orig_n_elem)->length += qc->pad_len;
4296 if (pad_buf) { 4297 if (pad_buf) {
4297 struct scatterlist *psg = &qc->pad_sgent; 4298 struct scatterlist *psg = &qc->pad_sgent;
4298 void *addr = kmap_atomic(psg->page, KM_IRQ0); 4299 void *addr = kmap_atomic(psg->page, KM_IRQ0);
@@ -4547,6 +4548,7 @@ void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4547 qc->orig_n_elem = 1; 4548 qc->orig_n_elem = 1;
4548 qc->buf_virt = buf; 4549 qc->buf_virt = buf;
4549 qc->nbytes = buflen; 4550 qc->nbytes = buflen;
4551 qc->cursg = qc->__sg;
4550 4552
4551 sg_init_one(&qc->sgent, buf, buflen); 4553 sg_init_one(&qc->sgent, buf, buflen);
4552} 4554}
@@ -4572,6 +4574,7 @@ void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4572 qc->__sg = sg; 4574 qc->__sg = sg;
4573 qc->n_elem = n_elem; 4575 qc->n_elem = n_elem;
4574 qc->orig_n_elem = n_elem; 4576 qc->orig_n_elem = n_elem;
4577 qc->cursg = qc->__sg;
4575} 4578}
4576 4579
4577/** 4580/**
@@ -4661,7 +4664,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
4661{ 4664{
4662 struct ata_port *ap = qc->ap; 4665 struct ata_port *ap = qc->ap;
4663 struct scatterlist *sg = qc->__sg; 4666 struct scatterlist *sg = qc->__sg;
4664 struct scatterlist *lsg = &sg[qc->n_elem - 1]; 4667 struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
4665 int n_elem, pre_n_elem, dir, trim_sg = 0; 4668 int n_elem, pre_n_elem, dir, trim_sg = 0;
4666 4669
4667 VPRINTK("ENTER, ata%u\n", ap->print_id); 4670 VPRINTK("ENTER, ata%u\n", ap->print_id);
@@ -4825,7 +4828,6 @@ void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4825static void ata_pio_sector(struct ata_queued_cmd *qc) 4828static void ata_pio_sector(struct ata_queued_cmd *qc)
4826{ 4829{
4827 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); 4830 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
4828 struct scatterlist *sg = qc->__sg;
4829 struct ata_port *ap = qc->ap; 4831 struct ata_port *ap = qc->ap;
4830 struct page *page; 4832 struct page *page;
4831 unsigned int offset; 4833 unsigned int offset;
@@ -4834,8 +4836,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
4834 if (qc->curbytes == qc->nbytes - qc->sect_size) 4836 if (qc->curbytes == qc->nbytes - qc->sect_size)
4835 ap->hsm_task_state = HSM_ST_LAST; 4837 ap->hsm_task_state = HSM_ST_LAST;
4836 4838
4837 page = sg[qc->cursg].page; 4839 page = qc->cursg->page;
4838 offset = sg[qc->cursg].offset + qc->cursg_ofs; 4840 offset = qc->cursg->offset + qc->cursg_ofs;
4839 4841
4840 /* get the current page and offset */ 4842 /* get the current page and offset */
4841 page = nth_page(page, (offset >> PAGE_SHIFT)); 4843 page = nth_page(page, (offset >> PAGE_SHIFT));
@@ -4863,8 +4865,8 @@ static void ata_pio_sector(struct ata_queued_cmd *qc)
4863 qc->curbytes += qc->sect_size; 4865 qc->curbytes += qc->sect_size;
4864 qc->cursg_ofs += qc->sect_size; 4866 qc->cursg_ofs += qc->sect_size;
4865 4867
4866 if (qc->cursg_ofs == (&sg[qc->cursg])->length) { 4868 if (qc->cursg_ofs == qc->cursg->length) {
4867 qc->cursg++; 4869 qc->cursg = sg_next(qc->cursg);
4868 qc->cursg_ofs = 0; 4870 qc->cursg_ofs = 0;
4869 } 4871 }
4870} 4872}
@@ -4950,16 +4952,18 @@ static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4950{ 4952{
4951 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); 4953 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
4952 struct scatterlist *sg = qc->__sg; 4954 struct scatterlist *sg = qc->__sg;
4955 struct scatterlist *lsg = sg_last(qc->__sg, qc->n_elem);
4953 struct ata_port *ap = qc->ap; 4956 struct ata_port *ap = qc->ap;
4954 struct page *page; 4957 struct page *page;
4955 unsigned char *buf; 4958 unsigned char *buf;
4956 unsigned int offset, count; 4959 unsigned int offset, count;
4960 int no_more_sg = 0;
4957 4961
4958 if (qc->curbytes + bytes >= qc->nbytes) 4962 if (qc->curbytes + bytes >= qc->nbytes)
4959 ap->hsm_task_state = HSM_ST_LAST; 4963 ap->hsm_task_state = HSM_ST_LAST;
4960 4964
4961next_sg: 4965next_sg:
4962 if (unlikely(qc->cursg >= qc->n_elem)) { 4966 if (unlikely(no_more_sg)) {
4963 /* 4967 /*
4964 * The end of qc->sg is reached and the device expects 4968 * The end of qc->sg is reached and the device expects
4965 * more data to transfer. In order not to overrun qc->sg 4969 * more data to transfer. In order not to overrun qc->sg
@@ -4982,7 +4986,7 @@ next_sg:
4982 return; 4986 return;
4983 } 4987 }
4984 4988
4985 sg = &qc->__sg[qc->cursg]; 4989 sg = qc->cursg;
4986 4990
4987 page = sg->page; 4991 page = sg->page;
4988 offset = sg->offset + qc->cursg_ofs; 4992 offset = sg->offset + qc->cursg_ofs;
@@ -5021,7 +5025,10 @@ next_sg:
5021 qc->cursg_ofs += count; 5025 qc->cursg_ofs += count;
5022 5026
5023 if (qc->cursg_ofs == sg->length) { 5027 if (qc->cursg_ofs == sg->length) {
5024 qc->cursg++; 5028 if (qc->cursg == lsg)
5029 no_more_sg = 1;
5030
5031 qc->cursg = sg_next(qc->cursg);
5025 qc->cursg_ofs = 0; 5032 qc->cursg_ofs = 0;
5026 } 5033 }
5027 5034
diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index d63c81ed084f..ba62d534f32b 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -801,8 +801,6 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
801 801
802 ata_scsi_sdev_config(sdev); 802 ata_scsi_sdev_config(sdev);
803 803
804 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
805
806 sdev->manage_start_stop = 1; 804 sdev->manage_start_stop = 1;
807 805
808 if (dev) 806 if (dev)
diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
index a895228f3d70..3fb7e8bc436d 100644
--- a/drivers/block/cciss.c
+++ b/drivers/block/cciss.c
@@ -2569,6 +2569,7 @@ static void do_cciss_request(struct request_queue *q)
2569 (int)creq->nr_sectors); 2569 (int)creq->nr_sectors);
2570#endif /* CCISS_DEBUG */ 2570#endif /* CCISS_DEBUG */
2571 2571
2572 memset(tmp_sg, 0, sizeof(tmp_sg));
2572 seg = blk_rq_map_sg(q, creq, tmp_sg); 2573 seg = blk_rq_map_sg(q, creq, tmp_sg);
2573 2574
2574 /* get the DMA records for the setup */ 2575 /* get the DMA records for the setup */
diff --git a/drivers/ide/cris/ide-cris.c b/drivers/ide/cris/ide-cris.c
index 2b4d2a0ae5c2..c306c9f534ab 100644
--- a/drivers/ide/cris/ide-cris.c
+++ b/drivers/ide/cris/ide-cris.c
@@ -939,7 +939,8 @@ static int cris_ide_build_dmatable (ide_drive_t *drive)
939 /* group sequential buffers into one large buffer */ 939 /* group sequential buffers into one large buffer */
940 addr = page_to_phys(sg->page) + sg->offset; 940 addr = page_to_phys(sg->page) + sg->offset;
941 size = sg_dma_len(sg); 941 size = sg_dma_len(sg);
942 while (sg++, --i) { 942 while (--i) {
943 sg = sg_next(sg);
943 if ((addr + size) != page_to_phys(sg->page) + sg->offset) 944 if ((addr + size) != page_to_phys(sg->page) + sg->offset)
944 break; 945 break;
945 size += sg_dma_len(sg); 946 size += sg_dma_len(sg);
diff --git a/drivers/ide/ide-dma.c b/drivers/ide/ide-dma.c
index b453211ee0fc..a4cbbbaccde9 100644
--- a/drivers/ide/ide-dma.c
+++ b/drivers/ide/ide-dma.c
@@ -280,7 +280,7 @@ int ide_build_dmatable (ide_drive_t *drive, struct request *rq)
280 } 280 }
281 } 281 }
282 282
283 sg++; 283 sg = sg_next(sg);
284 i--; 284 i--;
285 } 285 }
286 286
diff --git a/drivers/ide/ide-io.c b/drivers/ide/ide-io.c
index f36ff5962af6..04273d3c147c 100644
--- a/drivers/ide/ide-io.c
+++ b/drivers/ide/ide-io.c
@@ -846,7 +846,8 @@ void ide_init_sg_cmd(ide_drive_t *drive, struct request *rq)
846 ide_hwif_t *hwif = drive->hwif; 846 ide_hwif_t *hwif = drive->hwif;
847 847
848 hwif->nsect = hwif->nleft = rq->nr_sectors; 848 hwif->nsect = hwif->nleft = rq->nr_sectors;
849 hwif->cursg = hwif->cursg_ofs = 0; 849 hwif->cursg_ofs = 0;
850 hwif->cursg = NULL;
850} 851}
851 852
852EXPORT_SYMBOL_GPL(ide_init_sg_cmd); 853EXPORT_SYMBOL_GPL(ide_init_sg_cmd);
diff --git a/drivers/ide/ide-probe.c b/drivers/ide/ide-probe.c
index d1011712601c..34b1fb65bc79 100644
--- a/drivers/ide/ide-probe.c
+++ b/drivers/ide/ide-probe.c
@@ -1349,7 +1349,7 @@ static int hwif_init(ide_hwif_t *hwif)
1349 if (!hwif->sg_max_nents) 1349 if (!hwif->sg_max_nents)
1350 hwif->sg_max_nents = PRD_ENTRIES; 1350 hwif->sg_max_nents = PRD_ENTRIES;
1351 1351
1352 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents, 1352 hwif->sg_table = kzalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1353 GFP_KERNEL); 1353 GFP_KERNEL);
1354 if (!hwif->sg_table) { 1354 if (!hwif->sg_table) {
1355 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name); 1355 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
diff --git a/drivers/ide/ide-taskfile.c b/drivers/ide/ide-taskfile.c
index aa06dafb74ac..2a3c8d498343 100644
--- a/drivers/ide/ide-taskfile.c
+++ b/drivers/ide/ide-taskfile.c
@@ -45,6 +45,7 @@
45#include <linux/hdreg.h> 45#include <linux/hdreg.h>
46#include <linux/ide.h> 46#include <linux/ide.h>
47#include <linux/bitops.h> 47#include <linux/bitops.h>
48#include <linux/scatterlist.h>
48 49
49#include <asm/byteorder.h> 50#include <asm/byteorder.h>
50#include <asm/irq.h> 51#include <asm/irq.h>
@@ -263,6 +264,7 @@ static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
263{ 264{
264 ide_hwif_t *hwif = drive->hwif; 265 ide_hwif_t *hwif = drive->hwif;
265 struct scatterlist *sg = hwif->sg_table; 266 struct scatterlist *sg = hwif->sg_table;
267 struct scatterlist *cursg = hwif->cursg;
266 struct page *page; 268 struct page *page;
267#ifdef CONFIG_HIGHMEM 269#ifdef CONFIG_HIGHMEM
268 unsigned long flags; 270 unsigned long flags;
@@ -270,8 +272,14 @@ static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
270 unsigned int offset; 272 unsigned int offset;
271 u8 *buf; 273 u8 *buf;
272 274
273 page = sg[hwif->cursg].page; 275 cursg = hwif->cursg;
274 offset = sg[hwif->cursg].offset + hwif->cursg_ofs * SECTOR_SIZE; 276 if (!cursg) {
277 cursg = sg;
278 hwif->cursg = sg;
279 }
280
281 page = cursg->page;
282 offset = cursg->offset + hwif->cursg_ofs * SECTOR_SIZE;
275 283
276 /* get the current page and offset */ 284 /* get the current page and offset */
277 page = nth_page(page, (offset >> PAGE_SHIFT)); 285 page = nth_page(page, (offset >> PAGE_SHIFT));
@@ -285,8 +293,8 @@ static void ide_pio_sector(ide_drive_t *drive, unsigned int write)
285 hwif->nleft--; 293 hwif->nleft--;
286 hwif->cursg_ofs++; 294 hwif->cursg_ofs++;
287 295
288 if ((hwif->cursg_ofs * SECTOR_SIZE) == sg[hwif->cursg].length) { 296 if ((hwif->cursg_ofs * SECTOR_SIZE) == cursg->length) {
289 hwif->cursg++; 297 hwif->cursg = sg_next(hwif->cursg);
290 hwif->cursg_ofs = 0; 298 hwif->cursg_ofs = 0;
291 } 299 }
292 300
@@ -367,6 +375,8 @@ static ide_startstop_t task_error(ide_drive_t *drive, struct request *rq,
367 375
368static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat) 376static void task_end_request(ide_drive_t *drive, struct request *rq, u8 stat)
369{ 377{
378 HWIF(drive)->cursg = NULL;
379
370 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) { 380 if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) {
371 ide_task_t *task = rq->special; 381 ide_task_t *task = rq->special;
372 382
diff --git a/drivers/ide/mips/au1xxx-ide.c b/drivers/ide/mips/au1xxx-ide.c
index aebde49365d1..892d08f61dc0 100644
--- a/drivers/ide/mips/au1xxx-ide.c
+++ b/drivers/ide/mips/au1xxx-ide.c
@@ -296,7 +296,7 @@ static int auide_build_dmatable(ide_drive_t *drive)
296 cur_addr += tc; 296 cur_addr += tc;
297 cur_len -= tc; 297 cur_len -= tc;
298 } 298 }
299 sg++; 299 sg = sg_next(sg);
300 i--; 300 i--;
301 } 301 }
302 302
diff --git a/drivers/ide/pci/sgiioc4.c b/drivers/ide/pci/sgiioc4.c
index 85ffaaa39b1b..c74fef6bbc91 100644
--- a/drivers/ide/pci/sgiioc4.c
+++ b/drivers/ide/pci/sgiioc4.c
@@ -29,6 +29,7 @@
29#include <linux/mm.h> 29#include <linux/mm.h>
30#include <linux/ioport.h> 30#include <linux/ioport.h>
31#include <linux/blkdev.h> 31#include <linux/blkdev.h>
32#include <linux/scatterlist.h>
32#include <linux/ioc4.h> 33#include <linux/ioc4.h>
33#include <asm/io.h> 34#include <asm/io.h>
34 35
@@ -537,7 +538,7 @@ sgiioc4_build_dma_table(ide_drive_t * drive, struct request *rq, int ddir)
537 } 538 }
538 } 539 }
539 540
540 sg++; 541 sg = sg_next(sg);
541 i--; 542 i--;
542 } 543 }
543 544
diff --git a/drivers/ide/ppc/pmac.c b/drivers/ide/ppc/pmac.c
index 7d8873839e21..9e86406bf44b 100644
--- a/drivers/ide/ppc/pmac.c
+++ b/drivers/ide/ppc/pmac.c
@@ -1539,7 +1539,7 @@ pmac_ide_build_dmatable(ide_drive_t *drive, struct request *rq)
1539 cur_len -= tc; 1539 cur_len -= tc;
1540 ++table; 1540 ++table;
1541 } 1541 }
1542 sg++; 1542 sg = sg_next(sg);
1543 i--; 1543 i--;
1544 } 1544 }
1545 1545
diff --git a/drivers/infiniband/hw/ipath/ipath_dma.c b/drivers/infiniband/hw/ipath/ipath_dma.c
index f87f003e3ef8..22709a4f8fc8 100644
--- a/drivers/infiniband/hw/ipath/ipath_dma.c
+++ b/drivers/infiniband/hw/ipath/ipath_dma.c
@@ -30,6 +30,7 @@
30 * SOFTWARE. 30 * SOFTWARE.
31 */ 31 */
32 32
33#include <linux/scatterlist.h>
33#include <rdma/ib_verbs.h> 34#include <rdma/ib_verbs.h>
34 35
35#include "ipath_verbs.h" 36#include "ipath_verbs.h"
@@ -96,17 +97,18 @@ static void ipath_dma_unmap_page(struct ib_device *dev,
96 BUG_ON(!valid_dma_direction(direction)); 97 BUG_ON(!valid_dma_direction(direction));
97} 98}
98 99
99static int ipath_map_sg(struct ib_device *dev, struct scatterlist *sg, int nents, 100static int ipath_map_sg(struct ib_device *dev, struct scatterlist *sgl,
100 enum dma_data_direction direction) 101 int nents, enum dma_data_direction direction)
101{ 102{
103 struct scatterlist *sg;
102 u64 addr; 104 u64 addr;
103 int i; 105 int i;
104 int ret = nents; 106 int ret = nents;
105 107
106 BUG_ON(!valid_dma_direction(direction)); 108 BUG_ON(!valid_dma_direction(direction));
107 109
108 for (i = 0; i < nents; i++) { 110 for_each_sg(sgl, sg, nents, i) {
109 addr = (u64) page_address(sg[i].page); 111 addr = (u64) page_address(sg->page);
110 /* TODO: handle highmem pages */ 112 /* TODO: handle highmem pages */
111 if (!addr) { 113 if (!addr) {
112 ret = 0; 114 ret = 0;
diff --git a/drivers/infiniband/ulp/iser/iser_memory.c b/drivers/infiniband/ulp/iser/iser_memory.c
index e05690e3592f..f3529b6f0a33 100644
--- a/drivers/infiniband/ulp/iser/iser_memory.c
+++ b/drivers/infiniband/ulp/iser/iser_memory.c
@@ -124,17 +124,19 @@ static int iser_start_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
124 124
125 if (cmd_dir == ISER_DIR_OUT) { 125 if (cmd_dir == ISER_DIR_OUT) {
126 /* copy the unaligned sg the buffer which is used for RDMA */ 126 /* copy the unaligned sg the buffer which is used for RDMA */
127 struct scatterlist *sg = (struct scatterlist *)data->buf; 127 struct scatterlist *sgl = (struct scatterlist *)data->buf;
128 struct scatterlist *sg;
128 int i; 129 int i;
129 char *p, *from; 130 char *p, *from;
130 131
131 for (p = mem, i = 0; i < data->size; i++) { 132 p = mem;
132 from = kmap_atomic(sg[i].page, KM_USER0); 133 for_each_sg(sgl, sg, data->size, i) {
134 from = kmap_atomic(sg->page, KM_USER0);
133 memcpy(p, 135 memcpy(p,
134 from + sg[i].offset, 136 from + sg->offset,
135 sg[i].length); 137 sg->length);
136 kunmap_atomic(from, KM_USER0); 138 kunmap_atomic(from, KM_USER0);
137 p += sg[i].length; 139 p += sg->length;
138 } 140 }
139 } 141 }
140 142
@@ -176,7 +178,7 @@ void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
176 178
177 if (cmd_dir == ISER_DIR_IN) { 179 if (cmd_dir == ISER_DIR_IN) {
178 char *mem; 180 char *mem;
179 struct scatterlist *sg; 181 struct scatterlist *sgl, *sg;
180 unsigned char *p, *to; 182 unsigned char *p, *to;
181 unsigned int sg_size; 183 unsigned int sg_size;
182 int i; 184 int i;
@@ -184,16 +186,17 @@ void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
184 /* copy back read RDMA to unaligned sg */ 186 /* copy back read RDMA to unaligned sg */
185 mem = mem_copy->copy_buf; 187 mem = mem_copy->copy_buf;
186 188
187 sg = (struct scatterlist *)iser_ctask->data[ISER_DIR_IN].buf; 189 sgl = (struct scatterlist *)iser_ctask->data[ISER_DIR_IN].buf;
188 sg_size = iser_ctask->data[ISER_DIR_IN].size; 190 sg_size = iser_ctask->data[ISER_DIR_IN].size;
189 191
190 for (p = mem, i = 0; i < sg_size; i++){ 192 p = mem;
191 to = kmap_atomic(sg[i].page, KM_SOFTIRQ0); 193 for_each_sg(sgl, sg, sg_size, i) {
192 memcpy(to + sg[i].offset, 194 to = kmap_atomic(sg->page, KM_SOFTIRQ0);
195 memcpy(to + sg->offset,
193 p, 196 p,
194 sg[i].length); 197 sg->length);
195 kunmap_atomic(to, KM_SOFTIRQ0); 198 kunmap_atomic(to, KM_SOFTIRQ0);
196 p += sg[i].length; 199 p += sg->length;
197 } 200 }
198 } 201 }
199 202
@@ -224,7 +227,8 @@ static int iser_sg_to_page_vec(struct iser_data_buf *data,
224 struct iser_page_vec *page_vec, 227 struct iser_page_vec *page_vec,
225 struct ib_device *ibdev) 228 struct ib_device *ibdev)
226{ 229{
227 struct scatterlist *sg = (struct scatterlist *)data->buf; 230 struct scatterlist *sgl = (struct scatterlist *)data->buf;
231 struct scatterlist *sg;
228 u64 first_addr, last_addr, page; 232 u64 first_addr, last_addr, page;
229 int end_aligned; 233 int end_aligned;
230 unsigned int cur_page = 0; 234 unsigned int cur_page = 0;
@@ -232,24 +236,25 @@ static int iser_sg_to_page_vec(struct iser_data_buf *data,
232 int i; 236 int i;
233 237
234 /* compute the offset of first element */ 238 /* compute the offset of first element */
235 page_vec->offset = (u64) sg[0].offset & ~MASK_4K; 239 page_vec->offset = (u64) sgl[0].offset & ~MASK_4K;
236 240
237 for (i = 0; i < data->dma_nents; i++) { 241 for_each_sg(sgl, sg, data->dma_nents, i) {
238 unsigned int dma_len = ib_sg_dma_len(ibdev, &sg[i]); 242 unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
239 243
240 total_sz += dma_len; 244 total_sz += dma_len;
241 245
242 first_addr = ib_sg_dma_address(ibdev, &sg[i]); 246 first_addr = ib_sg_dma_address(ibdev, sg);
243 last_addr = first_addr + dma_len; 247 last_addr = first_addr + dma_len;
244 248
245 end_aligned = !(last_addr & ~MASK_4K); 249 end_aligned = !(last_addr & ~MASK_4K);
246 250
247 /* continue to collect page fragments till aligned or SG ends */ 251 /* continue to collect page fragments till aligned or SG ends */
248 while (!end_aligned && (i + 1 < data->dma_nents)) { 252 while (!end_aligned && (i + 1 < data->dma_nents)) {
253 sg = sg_next(sg);
249 i++; 254 i++;
250 dma_len = ib_sg_dma_len(ibdev, &sg[i]); 255 dma_len = ib_sg_dma_len(ibdev, sg);
251 total_sz += dma_len; 256 total_sz += dma_len;
252 last_addr = ib_sg_dma_address(ibdev, &sg[i]) + dma_len; 257 last_addr = ib_sg_dma_address(ibdev, sg) + dma_len;
253 end_aligned = !(last_addr & ~MASK_4K); 258 end_aligned = !(last_addr & ~MASK_4K);
254 } 259 }
255 260
@@ -284,25 +289,26 @@ static int iser_sg_to_page_vec(struct iser_data_buf *data,
284static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data, 289static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data,
285 struct ib_device *ibdev) 290 struct ib_device *ibdev)
286{ 291{
287 struct scatterlist *sg; 292 struct scatterlist *sgl, *sg;
288 u64 end_addr, next_addr; 293 u64 end_addr, next_addr;
289 int i, cnt; 294 int i, cnt;
290 unsigned int ret_len = 0; 295 unsigned int ret_len = 0;
291 296
292 sg = (struct scatterlist *)data->buf; 297 sgl = (struct scatterlist *)data->buf;
293 298
294 for (cnt = 0, i = 0; i < data->dma_nents; i++, cnt++) { 299 cnt = 0;
300 for_each_sg(sgl, sg, data->dma_nents, i) {
295 /* iser_dbg("Checking sg iobuf [%d]: phys=0x%08lX " 301 /* iser_dbg("Checking sg iobuf [%d]: phys=0x%08lX "
296 "offset: %ld sz: %ld\n", i, 302 "offset: %ld sz: %ld\n", i,
297 (unsigned long)page_to_phys(sg[i].page), 303 (unsigned long)page_to_phys(sg->page),
298 (unsigned long)sg[i].offset, 304 (unsigned long)sg->offset,
299 (unsigned long)sg[i].length); */ 305 (unsigned long)sg->length); */
300 end_addr = ib_sg_dma_address(ibdev, &sg[i]) + 306 end_addr = ib_sg_dma_address(ibdev, sg) +
301 ib_sg_dma_len(ibdev, &sg[i]); 307 ib_sg_dma_len(ibdev, sg);
302 /* iser_dbg("Checking sg iobuf end address " 308 /* iser_dbg("Checking sg iobuf end address "
303 "0x%08lX\n", end_addr); */ 309 "0x%08lX\n", end_addr); */
304 if (i + 1 < data->dma_nents) { 310 if (i + 1 < data->dma_nents) {
305 next_addr = ib_sg_dma_address(ibdev, &sg[i+1]); 311 next_addr = ib_sg_dma_address(ibdev, sg_next(sg));
306 /* are i, i+1 fragments of the same page? */ 312 /* are i, i+1 fragments of the same page? */
307 if (end_addr == next_addr) 313 if (end_addr == next_addr)
308 continue; 314 continue;
@@ -322,15 +328,16 @@ static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data,
322static void iser_data_buf_dump(struct iser_data_buf *data, 328static void iser_data_buf_dump(struct iser_data_buf *data,
323 struct ib_device *ibdev) 329 struct ib_device *ibdev)
324{ 330{
325 struct scatterlist *sg = (struct scatterlist *)data->buf; 331 struct scatterlist *sgl = (struct scatterlist *)data->buf;
332 struct scatterlist *sg;
326 int i; 333 int i;
327 334
328 for (i = 0; i < data->dma_nents; i++) 335 for_each_sg(sgl, sg, data->dma_nents, i)
329 iser_err("sg[%d] dma_addr:0x%lX page:0x%p " 336 iser_err("sg[%d] dma_addr:0x%lX page:0x%p "
330 "off:0x%x sz:0x%x dma_len:0x%x\n", 337 "off:0x%x sz:0x%x dma_len:0x%x\n",
331 i, (unsigned long)ib_sg_dma_address(ibdev, &sg[i]), 338 i, (unsigned long)ib_sg_dma_address(ibdev, sg),
332 sg[i].page, sg[i].offset, 339 sg->page, sg->offset,
333 sg[i].length, ib_sg_dma_len(ibdev, &sg[i])); 340 sg->length, ib_sg_dma_len(ibdev, sg));
334} 341}
335 342
336static void iser_dump_page_vec(struct iser_page_vec *page_vec) 343static void iser_dump_page_vec(struct iser_page_vec *page_vec)
diff --git a/drivers/message/fusion/mptscsih.c b/drivers/message/fusion/mptscsih.c
index bdff950a54a1..3918ebf01e8d 100644
--- a/drivers/message/fusion/mptscsih.c
+++ b/drivers/message/fusion/mptscsih.c
@@ -293,7 +293,7 @@ nextSGEset:
293 for (ii=0; ii < (numSgeThisFrame-1); ii++) { 293 for (ii=0; ii < (numSgeThisFrame-1); ii++) {
294 thisxfer = sg_dma_len(sg); 294 thisxfer = sg_dma_len(sg);
295 if (thisxfer == 0) { 295 if (thisxfer == 0) {
296 sg ++; /* Get next SG element from the OS */ 296 sg = sg_next(sg); /* Get next SG element from the OS */
297 sg_done++; 297 sg_done++;
298 continue; 298 continue;
299 } 299 }
@@ -301,7 +301,7 @@ nextSGEset:
301 v2 = sg_dma_address(sg); 301 v2 = sg_dma_address(sg);
302 mptscsih_add_sge(psge, sgflags | thisxfer, v2); 302 mptscsih_add_sge(psge, sgflags | thisxfer, v2);
303 303
304 sg++; /* Get next SG element from the OS */ 304 sg = sg_next(sg); /* Get next SG element from the OS */
305 psge += (sizeof(u32) + sizeof(dma_addr_t)); 305 psge += (sizeof(u32) + sizeof(dma_addr_t));
306 sgeOffset += (sizeof(u32) + sizeof(dma_addr_t)); 306 sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
307 sg_done++; 307 sg_done++;
@@ -322,7 +322,7 @@ nextSGEset:
322 v2 = sg_dma_address(sg); 322 v2 = sg_dma_address(sg);
323 mptscsih_add_sge(psge, sgflags | thisxfer, v2); 323 mptscsih_add_sge(psge, sgflags | thisxfer, v2);
324 /* 324 /*
325 sg++; 325 sg = sg_next(sg);
326 psge += (sizeof(u32) + sizeof(dma_addr_t)); 326 psge += (sizeof(u32) + sizeof(dma_addr_t));
327 */ 327 */
328 sgeOffset += (sizeof(u32) + sizeof(dma_addr_t)); 328 sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
diff --git a/drivers/mmc/card/queue.c b/drivers/mmc/card/queue.c
index b0abc7d92805..a5d0354bbbda 100644
--- a/drivers/mmc/card/queue.c
+++ b/drivers/mmc/card/queue.c
@@ -153,14 +153,14 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
153 blk_queue_max_hw_segments(mq->queue, bouncesz / 512); 153 blk_queue_max_hw_segments(mq->queue, bouncesz / 512);
154 blk_queue_max_segment_size(mq->queue, bouncesz); 154 blk_queue_max_segment_size(mq->queue, bouncesz);
155 155
156 mq->sg = kmalloc(sizeof(struct scatterlist), 156 mq->sg = kzalloc(sizeof(struct scatterlist),
157 GFP_KERNEL); 157 GFP_KERNEL);
158 if (!mq->sg) { 158 if (!mq->sg) {
159 ret = -ENOMEM; 159 ret = -ENOMEM;
160 goto cleanup_queue; 160 goto cleanup_queue;
161 } 161 }
162 162
163 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) * 163 mq->bounce_sg = kzalloc(sizeof(struct scatterlist) *
164 bouncesz / 512, GFP_KERNEL); 164 bouncesz / 512, GFP_KERNEL);
165 if (!mq->bounce_sg) { 165 if (!mq->bounce_sg) {
166 ret = -ENOMEM; 166 ret = -ENOMEM;
@@ -177,7 +177,7 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
177 blk_queue_max_hw_segments(mq->queue, host->max_hw_segs); 177 blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
178 blk_queue_max_segment_size(mq->queue, host->max_seg_size); 178 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
179 179
180 mq->sg = kmalloc(sizeof(struct scatterlist) * 180 mq->sg = kzalloc(sizeof(struct scatterlist) *
181 host->max_phys_segs, GFP_KERNEL); 181 host->max_phys_segs, GFP_KERNEL);
182 if (!mq->sg) { 182 if (!mq->sg) {
183 ret = -ENOMEM; 183 ret = -ENOMEM;
diff --git a/drivers/s390/scsi/zfcp_def.h b/drivers/s390/scsi/zfcp_def.h
index 16e5563e0c65..57cac7008e0b 100644
--- a/drivers/s390/scsi/zfcp_def.h
+++ b/drivers/s390/scsi/zfcp_def.h
@@ -34,6 +34,7 @@
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/mempool.h> 35#include <linux/mempool.h>
36#include <linux/syscalls.h> 36#include <linux/syscalls.h>
37#include <linux/scatterlist.h>
37#include <linux/ioctl.h> 38#include <linux/ioctl.h>
38#include <scsi/scsi.h> 39#include <scsi/scsi.h>
39#include <scsi/scsi_tcq.h> 40#include <scsi/scsi_tcq.h>
diff --git a/drivers/s390/scsi/zfcp_qdio.c b/drivers/s390/scsi/zfcp_qdio.c
index 3f105fdcf239..51d92b196ee7 100644
--- a/drivers/s390/scsi/zfcp_qdio.c
+++ b/drivers/s390/scsi/zfcp_qdio.c
@@ -590,7 +590,7 @@ zfcp_qdio_sbals_from_segment(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
590 */ 590 */
591int 591int
592zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype, 592zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
593 struct scatterlist *sg, int sg_count, int max_sbals) 593 struct scatterlist *sgl, int sg_count, int max_sbals)
594{ 594{
595 int sg_index; 595 int sg_index;
596 struct scatterlist *sg_segment; 596 struct scatterlist *sg_segment;
@@ -606,9 +606,7 @@ zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype,
606 sbale->flags |= sbtype; 606 sbale->flags |= sbtype;
607 607
608 /* process all segements of scatter-gather list */ 608 /* process all segements of scatter-gather list */
609 for (sg_index = 0, sg_segment = sg, bytes = 0; 609 for_each_sg(sgl, sg_segment, sg_count, sg_index) {
610 sg_index < sg_count;
611 sg_index++, sg_segment++) {
612 retval = zfcp_qdio_sbals_from_segment( 610 retval = zfcp_qdio_sbals_from_segment(
613 fsf_req, 611 fsf_req,
614 sbtype, 612 sbtype,
diff --git a/drivers/scsi/3w-9xxx.c b/drivers/scsi/3w-9xxx.c
index efd9d8d3a890..fb14014ee16e 100644
--- a/drivers/scsi/3w-9xxx.c
+++ b/drivers/scsi/3w-9xxx.c
@@ -1990,6 +1990,7 @@ static struct scsi_host_template driver_template = {
1990 .max_sectors = TW_MAX_SECTORS, 1990 .max_sectors = TW_MAX_SECTORS,
1991 .cmd_per_lun = TW_MAX_CMDS_PER_LUN, 1991 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
1992 .use_clustering = ENABLE_CLUSTERING, 1992 .use_clustering = ENABLE_CLUSTERING,
1993 .use_sg_chaining = ENABLE_SG_CHAINING,
1993 .shost_attrs = twa_host_attrs, 1994 .shost_attrs = twa_host_attrs,
1994 .emulated = 1 1995 .emulated = 1
1995}; 1996};
diff --git a/drivers/scsi/3w-xxxx.c b/drivers/scsi/3w-xxxx.c
index c7995fc216e8..a64153b96034 100644
--- a/drivers/scsi/3w-xxxx.c
+++ b/drivers/scsi/3w-xxxx.c
@@ -2261,6 +2261,7 @@ static struct scsi_host_template driver_template = {
2261 .max_sectors = TW_MAX_SECTORS, 2261 .max_sectors = TW_MAX_SECTORS,
2262 .cmd_per_lun = TW_MAX_CMDS_PER_LUN, 2262 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
2263 .use_clustering = ENABLE_CLUSTERING, 2263 .use_clustering = ENABLE_CLUSTERING,
2264 .use_sg_chaining = ENABLE_SG_CHAINING,
2264 .shost_attrs = tw_host_attrs, 2265 .shost_attrs = tw_host_attrs,
2265 .emulated = 1 2266 .emulated = 1
2266}; 2267};
diff --git a/drivers/scsi/BusLogic.c b/drivers/scsi/BusLogic.c
index 9b206176f717..49e1ffa4b2ff 100644
--- a/drivers/scsi/BusLogic.c
+++ b/drivers/scsi/BusLogic.c
@@ -3575,6 +3575,7 @@ static struct scsi_host_template Bus_Logic_template = {
3575 .unchecked_isa_dma = 1, 3575 .unchecked_isa_dma = 1,
3576 .max_sectors = 128, 3576 .max_sectors = 128,
3577 .use_clustering = ENABLE_CLUSTERING, 3577 .use_clustering = ENABLE_CLUSTERING,
3578 .use_sg_chaining = ENABLE_SG_CHAINING,
3578}; 3579};
3579 3580
3580/* 3581/*
diff --git a/drivers/scsi/NCR53c406a.c b/drivers/scsi/NCR53c406a.c
index eda8c48f6be7..3168a1794849 100644
--- a/drivers/scsi/NCR53c406a.c
+++ b/drivers/scsi/NCR53c406a.c
@@ -1066,7 +1066,8 @@ static struct scsi_host_template driver_template =
1066 .sg_tablesize = 32 /*SG_ALL*/ /*SG_NONE*/, 1066 .sg_tablesize = 32 /*SG_ALL*/ /*SG_NONE*/,
1067 .cmd_per_lun = 1 /* commands per lun */, 1067 .cmd_per_lun = 1 /* commands per lun */,
1068 .unchecked_isa_dma = 1 /* unchecked_isa_dma */, 1068 .unchecked_isa_dma = 1 /* unchecked_isa_dma */,
1069 .use_clustering = ENABLE_CLUSTERING 1069 .use_clustering = ENABLE_CLUSTERING,
1070 .use_sg_chaining = ENABLE_SG_CHAINING,
1070}; 1071};
1071 1072
1072#include "scsi_module.c" 1073#include "scsi_module.c"
diff --git a/drivers/scsi/a100u2w.c b/drivers/scsi/a100u2w.c
index f608d4a1d6da..d3a6d15fb77a 100644
--- a/drivers/scsi/a100u2w.c
+++ b/drivers/scsi/a100u2w.c
@@ -1071,6 +1071,7 @@ static struct scsi_host_template inia100_template = {
1071 .sg_tablesize = SG_ALL, 1071 .sg_tablesize = SG_ALL,
1072 .cmd_per_lun = 1, 1072 .cmd_per_lun = 1,
1073 .use_clustering = ENABLE_CLUSTERING, 1073 .use_clustering = ENABLE_CLUSTERING,
1074 .use_sg_chaining = ENABLE_SG_CHAINING,
1074}; 1075};
1075 1076
1076static int __devinit inia100_probe_one(struct pci_dev *pdev, 1077static int __devinit inia100_probe_one(struct pci_dev *pdev,
diff --git a/drivers/scsi/aacraid/linit.c b/drivers/scsi/aacraid/linit.c
index a7f42a17b5c7..038980be763d 100644
--- a/drivers/scsi/aacraid/linit.c
+++ b/drivers/scsi/aacraid/linit.c
@@ -944,6 +944,7 @@ static struct scsi_host_template aac_driver_template = {
944 .cmd_per_lun = AAC_NUM_IO_FIB, 944 .cmd_per_lun = AAC_NUM_IO_FIB,
945#endif 945#endif
946 .use_clustering = ENABLE_CLUSTERING, 946 .use_clustering = ENABLE_CLUSTERING,
947 .use_sg_chaining = ENABLE_SG_CHAINING,
947 .emulated = 1, 948 .emulated = 1,
948}; 949};
949 950
diff --git a/drivers/scsi/aha1542.c b/drivers/scsi/aha1542.c
index cbbfbc9f3e0f..961a1882cb7e 100644
--- a/drivers/scsi/aha1542.c
+++ b/drivers/scsi/aha1542.c
@@ -61,15 +61,15 @@ static void BAD_DMA(void *address, unsigned int length)
61} 61}
62 62
63static void BAD_SG_DMA(Scsi_Cmnd * SCpnt, 63static void BAD_SG_DMA(Scsi_Cmnd * SCpnt,
64 struct scatterlist *sgpnt, 64 struct scatterlist *sgp,
65 int nseg, 65 int nseg,
66 int badseg) 66 int badseg)
67{ 67{
68 printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n", 68 printk(KERN_CRIT "sgpnt[%d:%d] page %p/0x%llx length %u\n",
69 badseg, nseg, 69 badseg, nseg,
70 page_address(sgpnt[badseg].page) + sgpnt[badseg].offset, 70 page_address(sgp->page) + sgp->offset,
71 (unsigned long long)SCSI_SG_PA(&sgpnt[badseg]), 71 (unsigned long long)SCSI_SG_PA(sgp),
72 sgpnt[badseg].length); 72 sgp->length);
73 73
74 /* 74 /*
75 * Not safe to continue. 75 * Not safe to continue.
@@ -691,7 +691,7 @@ static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
691 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen); 691 memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
692 692
693 if (SCpnt->use_sg) { 693 if (SCpnt->use_sg) {
694 struct scatterlist *sgpnt; 694 struct scatterlist *sg;
695 struct chain *cptr; 695 struct chain *cptr;
696#ifdef DEBUG 696#ifdef DEBUG
697 unsigned char *ptr; 697 unsigned char *ptr;
@@ -699,23 +699,21 @@ static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
699 int i; 699 int i;
700 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */ 700 ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
701 SCpnt->host_scribble = kmalloc(512, GFP_KERNEL | GFP_DMA); 701 SCpnt->host_scribble = kmalloc(512, GFP_KERNEL | GFP_DMA);
702 sgpnt = (struct scatterlist *) SCpnt->request_buffer;
703 cptr = (struct chain *) SCpnt->host_scribble; 702 cptr = (struct chain *) SCpnt->host_scribble;
704 if (cptr == NULL) { 703 if (cptr == NULL) {
705 /* free the claimed mailbox slot */ 704 /* free the claimed mailbox slot */
706 HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL; 705 HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
707 return SCSI_MLQUEUE_HOST_BUSY; 706 return SCSI_MLQUEUE_HOST_BUSY;
708 } 707 }
709 for (i = 0; i < SCpnt->use_sg; i++) { 708 scsi_for_each_sg(SCpnt, sg, SCpnt->use_sg, i) {
710 if (sgpnt[i].length == 0 || SCpnt->use_sg > 16 || 709 if (sg->length == 0 || SCpnt->use_sg > 16 ||
711 (((int) sgpnt[i].offset) & 1) || (sgpnt[i].length & 1)) { 710 (((int) sg->offset) & 1) || (sg->length & 1)) {
712 unsigned char *ptr; 711 unsigned char *ptr;
713 printk(KERN_CRIT "Bad segment list supplied to aha1542.c (%d, %d)\n", SCpnt->use_sg, i); 712 printk(KERN_CRIT "Bad segment list supplied to aha1542.c (%d, %d)\n", SCpnt->use_sg, i);
714 for (i = 0; i < SCpnt->use_sg; i++) { 713 scsi_for_each_sg(SCpnt, sg, SCpnt->use_sg, i) {
715 printk(KERN_CRIT "%d: %p %d\n", i, 714 printk(KERN_CRIT "%d: %p %d\n", i,
716 (page_address(sgpnt[i].page) + 715 (page_address(sg->page) +
717 sgpnt[i].offset), 716 sg->offset), sg->length);
718 sgpnt[i].length);
719 }; 717 };
720 printk(KERN_CRIT "cptr %x: ", (unsigned int) cptr); 718 printk(KERN_CRIT "cptr %x: ", (unsigned int) cptr);
721 ptr = (unsigned char *) &cptr[i]; 719 ptr = (unsigned char *) &cptr[i];
@@ -723,10 +721,10 @@ static int aha1542_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
723 printk("%02x ", ptr[i]); 721 printk("%02x ", ptr[i]);
724 panic("Foooooooood fight!"); 722 panic("Foooooooood fight!");
725 }; 723 };
726 any2scsi(cptr[i].dataptr, SCSI_SG_PA(&sgpnt[i])); 724 any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg));
727 if (SCSI_SG_PA(&sgpnt[i]) + sgpnt[i].length - 1 > ISA_DMA_THRESHOLD) 725 if (SCSI_SG_PA(sg) + sg->length - 1 > ISA_DMA_THRESHOLD)
728 BAD_SG_DMA(SCpnt, sgpnt, SCpnt->use_sg, i); 726 BAD_SG_DMA(SCpnt, sg, SCpnt->use_sg, i);
729 any2scsi(cptr[i].datalen, sgpnt[i].length); 727 any2scsi(cptr[i].datalen, sg->length);
730 }; 728 };
731 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain)); 729 any2scsi(ccb[mbo].datalen, SCpnt->use_sg * sizeof(struct chain));
732 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr)); 730 any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
diff --git a/drivers/scsi/aha1740.c b/drivers/scsi/aha1740.c
index e4a4f3a965d9..f6722fd46008 100644
--- a/drivers/scsi/aha1740.c
+++ b/drivers/scsi/aha1740.c
@@ -563,6 +563,7 @@ static struct scsi_host_template aha1740_template = {
563 .sg_tablesize = AHA1740_SCATTER, 563 .sg_tablesize = AHA1740_SCATTER,
564 .cmd_per_lun = AHA1740_CMDLUN, 564 .cmd_per_lun = AHA1740_CMDLUN,
565 .use_clustering = ENABLE_CLUSTERING, 565 .use_clustering = ENABLE_CLUSTERING,
566 .use_sg_chaining = ENABLE_SG_CHAINING,
566 .eh_abort_handler = aha1740_eh_abort_handler, 567 .eh_abort_handler = aha1740_eh_abort_handler,
567}; 568};
568 569
diff --git a/drivers/scsi/aic7xxx/aic79xx_osm.c b/drivers/scsi/aic7xxx/aic79xx_osm.c
index a055a96e3ad3..42c0f14a262c 100644
--- a/drivers/scsi/aic7xxx/aic79xx_osm.c
+++ b/drivers/scsi/aic7xxx/aic79xx_osm.c
@@ -766,6 +766,7 @@ struct scsi_host_template aic79xx_driver_template = {
766 .max_sectors = 8192, 766 .max_sectors = 8192,
767 .cmd_per_lun = 2, 767 .cmd_per_lun = 2,
768 .use_clustering = ENABLE_CLUSTERING, 768 .use_clustering = ENABLE_CLUSTERING,
769 .use_sg_chaining = ENABLE_SG_CHAINING,
769 .slave_alloc = ahd_linux_slave_alloc, 770 .slave_alloc = ahd_linux_slave_alloc,
770 .slave_configure = ahd_linux_slave_configure, 771 .slave_configure = ahd_linux_slave_configure,
771 .target_alloc = ahd_linux_target_alloc, 772 .target_alloc = ahd_linux_target_alloc,
diff --git a/drivers/scsi/aic7xxx/aic7xxx_osm.c b/drivers/scsi/aic7xxx/aic7xxx_osm.c
index 2e9c38f2e8a6..7770befbf50c 100644
--- a/drivers/scsi/aic7xxx/aic7xxx_osm.c
+++ b/drivers/scsi/aic7xxx/aic7xxx_osm.c
@@ -747,6 +747,7 @@ struct scsi_host_template aic7xxx_driver_template = {
747 .max_sectors = 8192, 747 .max_sectors = 8192,
748 .cmd_per_lun = 2, 748 .cmd_per_lun = 2,
749 .use_clustering = ENABLE_CLUSTERING, 749 .use_clustering = ENABLE_CLUSTERING,
750 .use_sg_chaining = ENABLE_SG_CHAINING,
750 .slave_alloc = ahc_linux_slave_alloc, 751 .slave_alloc = ahc_linux_slave_alloc,
751 .slave_configure = ahc_linux_slave_configure, 752 .slave_configure = ahc_linux_slave_configure,
752 .target_alloc = ahc_linux_target_alloc, 753 .target_alloc = ahc_linux_target_alloc,
diff --git a/drivers/scsi/aic7xxx_old.c b/drivers/scsi/aic7xxx_old.c
index 1a71b0236c97..4025608d6964 100644
--- a/drivers/scsi/aic7xxx_old.c
+++ b/drivers/scsi/aic7xxx_old.c
@@ -11142,6 +11142,7 @@ static struct scsi_host_template driver_template = {
11142 .max_sectors = 2048, 11142 .max_sectors = 2048,
11143 .cmd_per_lun = 3, 11143 .cmd_per_lun = 3,
11144 .use_clustering = ENABLE_CLUSTERING, 11144 .use_clustering = ENABLE_CLUSTERING,
11145 .use_sg_chaining = ENABLE_SG_CHAINING,
11145}; 11146};
11146 11147
11147#include "scsi_module.c" 11148#include "scsi_module.c"
diff --git a/drivers/scsi/aic94xx/aic94xx_task.c b/drivers/scsi/aic94xx/aic94xx_task.c
index f2b23e01401a..ee0a98bffcd4 100644
--- a/drivers/scsi/aic94xx/aic94xx_task.c
+++ b/drivers/scsi/aic94xx/aic94xx_task.c
@@ -94,7 +94,7 @@ static inline int asd_map_scatterlist(struct sas_task *task,
94 res = -ENOMEM; 94 res = -ENOMEM;
95 goto err_unmap; 95 goto err_unmap;
96 } 96 }
97 for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) { 97 for_each_sg(task->scatter, sc, num_sg, i) {
98 struct sg_el *sg = 98 struct sg_el *sg =
99 &((struct sg_el *)ascb->sg_arr->vaddr)[i]; 99 &((struct sg_el *)ascb->sg_arr->vaddr)[i];
100 sg->bus_addr = cpu_to_le64((u64)sg_dma_address(sc)); 100 sg->bus_addr = cpu_to_le64((u64)sg_dma_address(sc));
@@ -103,7 +103,7 @@ static inline int asd_map_scatterlist(struct sas_task *task,
103 sg->flags |= ASD_SG_EL_LIST_EOL; 103 sg->flags |= ASD_SG_EL_LIST_EOL;
104 } 104 }
105 105
106 for (sc = task->scatter, i = 0; i < 2; i++, sc++) { 106 for_each_sg(task->scatter, sc, 2, i) {
107 sg_arr[i].bus_addr = 107 sg_arr[i].bus_addr =
108 cpu_to_le64((u64)sg_dma_address(sc)); 108 cpu_to_le64((u64)sg_dma_address(sc));
109 sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc)); 109 sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
@@ -115,7 +115,7 @@ static inline int asd_map_scatterlist(struct sas_task *task,
115 sg_arr[2].bus_addr=cpu_to_le64((u64)ascb->sg_arr->dma_handle); 115 sg_arr[2].bus_addr=cpu_to_le64((u64)ascb->sg_arr->dma_handle);
116 } else { 116 } else {
117 int i; 117 int i;
118 for (sc = task->scatter, i = 0; i < num_sg; i++, sc++) { 118 for_each_sg(task->scatter, sc, num_sg, i) {
119 sg_arr[i].bus_addr = 119 sg_arr[i].bus_addr =
120 cpu_to_le64((u64)sg_dma_address(sc)); 120 cpu_to_le64((u64)sg_dma_address(sc));
121 sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc)); 121 sg_arr[i].size = cpu_to_le32((u32)sg_dma_len(sc));
diff --git a/drivers/scsi/arcmsr/arcmsr_hba.c b/drivers/scsi/arcmsr/arcmsr_hba.c
index cfcf40159eab..f81777586b8f 100644
--- a/drivers/scsi/arcmsr/arcmsr_hba.c
+++ b/drivers/scsi/arcmsr/arcmsr_hba.c
@@ -122,6 +122,7 @@ static struct scsi_host_template arcmsr_scsi_host_template = {
122 .max_sectors = ARCMSR_MAX_XFER_SECTORS, 122 .max_sectors = ARCMSR_MAX_XFER_SECTORS,
123 .cmd_per_lun = ARCMSR_MAX_CMD_PERLUN, 123 .cmd_per_lun = ARCMSR_MAX_CMD_PERLUN,
124 .use_clustering = ENABLE_CLUSTERING, 124 .use_clustering = ENABLE_CLUSTERING,
125 .use_sg_chaining = ENABLE_SG_CHAINING,
125 .shost_attrs = arcmsr_host_attrs, 126 .shost_attrs = arcmsr_host_attrs,
126}; 127};
127#ifdef CONFIG_SCSI_ARCMSR_AER 128#ifdef CONFIG_SCSI_ARCMSR_AER
diff --git a/drivers/scsi/dc395x.c b/drivers/scsi/dc395x.c
index 1591824cf4b3..fd42d4789202 100644
--- a/drivers/scsi/dc395x.c
+++ b/drivers/scsi/dc395x.c
@@ -4765,6 +4765,7 @@ static struct scsi_host_template dc395x_driver_template = {
4765 .eh_bus_reset_handler = dc395x_eh_bus_reset, 4765 .eh_bus_reset_handler = dc395x_eh_bus_reset,
4766 .unchecked_isa_dma = 0, 4766 .unchecked_isa_dma = 0,
4767 .use_clustering = DISABLE_CLUSTERING, 4767 .use_clustering = DISABLE_CLUSTERING,
4768 .use_sg_chaining = ENABLE_SG_CHAINING,
4768}; 4769};
4769 4770
4770 4771
diff --git a/drivers/scsi/dpt_i2o.c b/drivers/scsi/dpt_i2o.c
index bea9d659af15..8258506ba7d7 100644
--- a/drivers/scsi/dpt_i2o.c
+++ b/drivers/scsi/dpt_i2o.c
@@ -3295,6 +3295,7 @@ static struct scsi_host_template adpt_template = {
3295 .this_id = 7, 3295 .this_id = 7,
3296 .cmd_per_lun = 1, 3296 .cmd_per_lun = 1,
3297 .use_clustering = ENABLE_CLUSTERING, 3297 .use_clustering = ENABLE_CLUSTERING,
3298 .use_sg_chaining = ENABLE_SG_CHAINING,
3298}; 3299};
3299 3300
3300static s32 adpt_scsi_register(adpt_hba* pHba) 3301static s32 adpt_scsi_register(adpt_hba* pHba)
diff --git a/drivers/scsi/eata.c b/drivers/scsi/eata.c
index ec2233114bc9..7ead5210de96 100644
--- a/drivers/scsi/eata.c
+++ b/drivers/scsi/eata.c
@@ -523,7 +523,8 @@ static struct scsi_host_template driver_template = {
523 .slave_configure = eata2x_slave_configure, 523 .slave_configure = eata2x_slave_configure,
524 .this_id = 7, 524 .this_id = 7,
525 .unchecked_isa_dma = 1, 525 .unchecked_isa_dma = 1,
526 .use_clustering = ENABLE_CLUSTERING 526 .use_clustering = ENABLE_CLUSTERING,
527 .use_sg_chaining = ENABLE_SG_CHAINING,
527}; 528};
528 529
529#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD) 530#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
diff --git a/drivers/scsi/hosts.c b/drivers/scsi/hosts.c
index adc9559cb6f4..112ab6abe62b 100644
--- a/drivers/scsi/hosts.c
+++ b/drivers/scsi/hosts.c
@@ -343,6 +343,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
343 shost->use_clustering = sht->use_clustering; 343 shost->use_clustering = sht->use_clustering;
344 shost->ordered_tag = sht->ordered_tag; 344 shost->ordered_tag = sht->ordered_tag;
345 shost->active_mode = sht->supported_mode; 345 shost->active_mode = sht->supported_mode;
346 shost->use_sg_chaining = sht->use_sg_chaining;
346 347
347 if (sht->max_host_blocked) 348 if (sht->max_host_blocked)
348 shost->max_host_blocked = sht->max_host_blocked; 349 shost->max_host_blocked = sht->max_host_blocked;
diff --git a/drivers/scsi/hptiop.c b/drivers/scsi/hptiop.c
index 8b384fa7f048..8515054cdf70 100644
--- a/drivers/scsi/hptiop.c
+++ b/drivers/scsi/hptiop.c
@@ -655,6 +655,7 @@ static struct scsi_host_template driver_template = {
655 .unchecked_isa_dma = 0, 655 .unchecked_isa_dma = 0,
656 .emulated = 0, 656 .emulated = 0,
657 .use_clustering = ENABLE_CLUSTERING, 657 .use_clustering = ENABLE_CLUSTERING,
658 .use_sg_chaining = ENABLE_SG_CHAINING,
658 .proc_name = driver_name, 659 .proc_name = driver_name,
659 .shost_attrs = hptiop_attrs, 660 .shost_attrs = hptiop_attrs,
660 .this_id = -1, 661 .this_id = -1,
diff --git a/drivers/scsi/ibmmca.c b/drivers/scsi/ibmmca.c
index 1a924e9b0271..714e6273a70d 100644
--- a/drivers/scsi/ibmmca.c
+++ b/drivers/scsi/ibmmca.c
@@ -1501,6 +1501,7 @@ static struct scsi_host_template ibmmca_driver_template = {
1501 .sg_tablesize = 16, 1501 .sg_tablesize = 16,
1502 .cmd_per_lun = 1, 1502 .cmd_per_lun = 1,
1503 .use_clustering = ENABLE_CLUSTERING, 1503 .use_clustering = ENABLE_CLUSTERING,
1504 .use_sg_chaining = ENABLE_SG_CHAINING,
1504}; 1505};
1505 1506
1506static int ibmmca_probe(struct device *dev) 1507static int ibmmca_probe(struct device *dev)
diff --git a/drivers/scsi/ibmvscsi/ibmvscsi.c b/drivers/scsi/ibmvscsi/ibmvscsi.c
index cda0cc3d182f..22d91ee173c5 100644
--- a/drivers/scsi/ibmvscsi/ibmvscsi.c
+++ b/drivers/scsi/ibmvscsi/ibmvscsi.c
@@ -1548,6 +1548,7 @@ static struct scsi_host_template driver_template = {
1548 .this_id = -1, 1548 .this_id = -1,
1549 .sg_tablesize = SG_ALL, 1549 .sg_tablesize = SG_ALL,
1550 .use_clustering = ENABLE_CLUSTERING, 1550 .use_clustering = ENABLE_CLUSTERING,
1551 .use_sg_chaining = ENABLE_SG_CHAINING,
1551 .shost_attrs = ibmvscsi_attrs, 1552 .shost_attrs = ibmvscsi_attrs,
1552}; 1553};
1553 1554
diff --git a/drivers/scsi/ide-scsi.c b/drivers/scsi/ide-scsi.c
index d81bb076a15a..d297f64cd432 100644
--- a/drivers/scsi/ide-scsi.c
+++ b/drivers/scsi/ide-scsi.c
@@ -70,6 +70,7 @@ typedef struct idescsi_pc_s {
70 u8 *buffer; /* Data buffer */ 70 u8 *buffer; /* Data buffer */
71 u8 *current_position; /* Pointer into the above buffer */ 71 u8 *current_position; /* Pointer into the above buffer */
72 struct scatterlist *sg; /* Scatter gather table */ 72 struct scatterlist *sg; /* Scatter gather table */
73 struct scatterlist *last_sg; /* Last sg element */
73 int b_count; /* Bytes transferred from current entry */ 74 int b_count; /* Bytes transferred from current entry */
74 struct scsi_cmnd *scsi_cmd; /* SCSI command */ 75 struct scsi_cmnd *scsi_cmd; /* SCSI command */
75 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */ 76 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
@@ -173,12 +174,6 @@ static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigne
173 char *buf; 174 char *buf;
174 175
175 while (bcount) { 176 while (bcount) {
176 if (pc->sg - scsi_sglist(pc->scsi_cmd) >
177 scsi_sg_count(pc->scsi_cmd)) {
178 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
179 idescsi_discard_data (drive, bcount);
180 return;
181 }
182 count = min(pc->sg->length - pc->b_count, bcount); 177 count = min(pc->sg->length - pc->b_count, bcount);
183 if (PageHighMem(pc->sg->page)) { 178 if (PageHighMem(pc->sg->page)) {
184 unsigned long flags; 179 unsigned long flags;
@@ -197,10 +192,17 @@ static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigne
197 } 192 }
198 bcount -= count; pc->b_count += count; 193 bcount -= count; pc->b_count += count;
199 if (pc->b_count == pc->sg->length) { 194 if (pc->b_count == pc->sg->length) {
200 pc->sg++; 195 if (pc->sg == pc->last_sg)
196 break;
197 pc->sg = sg_next(pc->sg);
201 pc->b_count = 0; 198 pc->b_count = 0;
202 } 199 }
203 } 200 }
201
202 if (bcount) {
203 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
204 idescsi_discard_data (drive, bcount);
205 }
204} 206}
205 207
206static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount) 208static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
@@ -209,12 +211,6 @@ static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsign
209 char *buf; 211 char *buf;
210 212
211 while (bcount) { 213 while (bcount) {
212 if (pc->sg - scsi_sglist(pc->scsi_cmd) >
213 scsi_sg_count(pc->scsi_cmd)) {
214 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
215 idescsi_output_zeros (drive, bcount);
216 return;
217 }
218 count = min(pc->sg->length - pc->b_count, bcount); 214 count = min(pc->sg->length - pc->b_count, bcount);
219 if (PageHighMem(pc->sg->page)) { 215 if (PageHighMem(pc->sg->page)) {
220 unsigned long flags; 216 unsigned long flags;
@@ -233,10 +229,17 @@ static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsign
233 } 229 }
234 bcount -= count; pc->b_count += count; 230 bcount -= count; pc->b_count += count;
235 if (pc->b_count == pc->sg->length) { 231 if (pc->b_count == pc->sg->length) {
236 pc->sg++; 232 if (pc->sg == pc->last_sg)
233 break;
234 pc->sg = sg_next(pc->sg);
237 pc->b_count = 0; 235 pc->b_count = 0;
238 } 236 }
239 } 237 }
238
239 if (bcount) {
240 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
241 idescsi_output_zeros (drive, bcount);
242 }
240} 243}
241 244
242static void hexdump(u8 *x, int len) 245static void hexdump(u8 *x, int len)
@@ -804,6 +807,7 @@ static int idescsi_queue (struct scsi_cmnd *cmd,
804 memcpy (pc->c, cmd->cmnd, cmd->cmd_len); 807 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
805 pc->buffer = NULL; 808 pc->buffer = NULL;
806 pc->sg = scsi_sglist(cmd); 809 pc->sg = scsi_sglist(cmd);
810 pc->last_sg = sg_last(pc->sg, cmd->use_sg);
807 pc->b_count = 0; 811 pc->b_count = 0;
808 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd); 812 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
809 pc->scsi_cmd = cmd; 813 pc->scsi_cmd = cmd;
diff --git a/drivers/scsi/initio.c b/drivers/scsi/initio.c
index d9dfb69ae031..22d40fd5845b 100644
--- a/drivers/scsi/initio.c
+++ b/drivers/scsi/initio.c
@@ -2831,6 +2831,7 @@ static struct scsi_host_template initio_template = {
2831 .sg_tablesize = SG_ALL, 2831 .sg_tablesize = SG_ALL,
2832 .cmd_per_lun = 1, 2832 .cmd_per_lun = 1,
2833 .use_clustering = ENABLE_CLUSTERING, 2833 .use_clustering = ENABLE_CLUSTERING,
2834 .use_sg_chaining = ENABLE_SG_CHAINING,
2834}; 2835};
2835 2836
2836static int initio_probe_one(struct pci_dev *pdev, 2837static int initio_probe_one(struct pci_dev *pdev,
diff --git a/drivers/scsi/ips.c b/drivers/scsi/ips.c
index 2ed099e2c20d..edaac2714c5a 100644
--- a/drivers/scsi/ips.c
+++ b/drivers/scsi/ips.c
@@ -3252,7 +3252,7 @@ ips_done(ips_ha_t * ha, ips_scb_t * scb)
3252 */ 3252 */
3253 if ((scb->breakup) || (scb->sg_break)) { 3253 if ((scb->breakup) || (scb->sg_break)) {
3254 struct scatterlist *sg; 3254 struct scatterlist *sg;
3255 int sg_dma_index, ips_sg_index = 0; 3255 int i, sg_dma_index, ips_sg_index = 0;
3256 3256
3257 /* we had a data breakup */ 3257 /* we had a data breakup */
3258 scb->data_len = 0; 3258 scb->data_len = 0;
@@ -3261,20 +3261,22 @@ ips_done(ips_ha_t * ha, ips_scb_t * scb)
3261 3261
3262 /* Spin forward to last dma chunk */ 3262 /* Spin forward to last dma chunk */
3263 sg_dma_index = scb->breakup; 3263 sg_dma_index = scb->breakup;
3264 for (i = 0; i < scb->breakup; i++)
3265 sg = sg_next(sg);
3264 3266
3265 /* Take care of possible partial on last chunk */ 3267 /* Take care of possible partial on last chunk */
3266 ips_fill_scb_sg_single(ha, 3268 ips_fill_scb_sg_single(ha,
3267 sg_dma_address(&sg[sg_dma_index]), 3269 sg_dma_address(sg),
3268 scb, ips_sg_index++, 3270 scb, ips_sg_index++,
3269 sg_dma_len(&sg[sg_dma_index])); 3271 sg_dma_len(sg));
3270 3272
3271 for (; sg_dma_index < scsi_sg_count(scb->scsi_cmd); 3273 for (; sg_dma_index < scsi_sg_count(scb->scsi_cmd);
3272 sg_dma_index++) { 3274 sg_dma_index++, sg = sg_next(sg)) {
3273 if (ips_fill_scb_sg_single 3275 if (ips_fill_scb_sg_single
3274 (ha, 3276 (ha,
3275 sg_dma_address(&sg[sg_dma_index]), 3277 sg_dma_address(sg),
3276 scb, ips_sg_index++, 3278 scb, ips_sg_index++,
3277 sg_dma_len(&sg[sg_dma_index])) < 0) 3279 sg_dma_len(sg)) < 0)
3278 break; 3280 break;
3279 } 3281 }
3280 3282
diff --git a/drivers/scsi/lpfc/lpfc_scsi.c b/drivers/scsi/lpfc/lpfc_scsi.c
index cd674938ccd5..c0755565fae9 100644
--- a/drivers/scsi/lpfc/lpfc_scsi.c
+++ b/drivers/scsi/lpfc/lpfc_scsi.c
@@ -1438,6 +1438,7 @@ struct scsi_host_template lpfc_template = {
1438 .scan_finished = lpfc_scan_finished, 1438 .scan_finished = lpfc_scan_finished,
1439 .this_id = -1, 1439 .this_id = -1,
1440 .sg_tablesize = LPFC_SG_SEG_CNT, 1440 .sg_tablesize = LPFC_SG_SEG_CNT,
1441 .use_sg_chaining = ENABLE_SG_CHAINING,
1441 .cmd_per_lun = LPFC_CMD_PER_LUN, 1442 .cmd_per_lun = LPFC_CMD_PER_LUN,
1442 .use_clustering = ENABLE_CLUSTERING, 1443 .use_clustering = ENABLE_CLUSTERING,
1443 .shost_attrs = lpfc_hba_attrs, 1444 .shost_attrs = lpfc_hba_attrs,
@@ -1460,6 +1461,7 @@ struct scsi_host_template lpfc_vport_template = {
1460 .sg_tablesize = LPFC_SG_SEG_CNT, 1461 .sg_tablesize = LPFC_SG_SEG_CNT,
1461 .cmd_per_lun = LPFC_CMD_PER_LUN, 1462 .cmd_per_lun = LPFC_CMD_PER_LUN,
1462 .use_clustering = ENABLE_CLUSTERING, 1463 .use_clustering = ENABLE_CLUSTERING,
1464 .use_sg_chaining = ENABLE_SG_CHAINING,
1463 .shost_attrs = lpfc_vport_attrs, 1465 .shost_attrs = lpfc_vport_attrs,
1464 .max_sectors = 0xFFFF, 1466 .max_sectors = 0xFFFF,
1465}; 1467};
diff --git a/drivers/scsi/mac53c94.c b/drivers/scsi/mac53c94.c
index b12ad7c7c673..a035001f4438 100644
--- a/drivers/scsi/mac53c94.c
+++ b/drivers/scsi/mac53c94.c
@@ -402,6 +402,7 @@ static struct scsi_host_template mac53c94_template = {
402 .sg_tablesize = SG_ALL, 402 .sg_tablesize = SG_ALL,
403 .cmd_per_lun = 1, 403 .cmd_per_lun = 1,
404 .use_clustering = DISABLE_CLUSTERING, 404 .use_clustering = DISABLE_CLUSTERING,
405 .use_sg_chaining = ENABLE_SG_CHAINING,
405}; 406};
406 407
407static int mac53c94_probe(struct macio_dev *mdev, const struct of_device_id *match) 408static int mac53c94_probe(struct macio_dev *mdev, const struct of_device_id *match)
diff --git a/drivers/scsi/megaraid.c b/drivers/scsi/megaraid.c
index e7e11f282c8f..10d1aff9938a 100644
--- a/drivers/scsi/megaraid.c
+++ b/drivers/scsi/megaraid.c
@@ -4492,6 +4492,7 @@ static struct scsi_host_template megaraid_template = {
4492 .sg_tablesize = MAX_SGLIST, 4492 .sg_tablesize = MAX_SGLIST,
4493 .cmd_per_lun = DEF_CMD_PER_LUN, 4493 .cmd_per_lun = DEF_CMD_PER_LUN,
4494 .use_clustering = ENABLE_CLUSTERING, 4494 .use_clustering = ENABLE_CLUSTERING,
4495 .use_sg_chaining = ENABLE_SG_CHAINING,
4495 .eh_abort_handler = megaraid_abort, 4496 .eh_abort_handler = megaraid_abort,
4496 .eh_device_reset_handler = megaraid_reset, 4497 .eh_device_reset_handler = megaraid_reset,
4497 .eh_bus_reset_handler = megaraid_reset, 4498 .eh_bus_reset_handler = megaraid_reset,
diff --git a/drivers/scsi/megaraid/megaraid_mbox.c b/drivers/scsi/megaraid/megaraid_mbox.c
index c6a53dccc16a..e4e4c6a39ed6 100644
--- a/drivers/scsi/megaraid/megaraid_mbox.c
+++ b/drivers/scsi/megaraid/megaraid_mbox.c
@@ -361,6 +361,7 @@ static struct scsi_host_template megaraid_template_g = {
361 .eh_host_reset_handler = megaraid_reset_handler, 361 .eh_host_reset_handler = megaraid_reset_handler,
362 .change_queue_depth = megaraid_change_queue_depth, 362 .change_queue_depth = megaraid_change_queue_depth,
363 .use_clustering = ENABLE_CLUSTERING, 363 .use_clustering = ENABLE_CLUSTERING,
364 .use_sg_chaining = ENABLE_SG_CHAINING,
364 .sdev_attrs = megaraid_sdev_attrs, 365 .sdev_attrs = megaraid_sdev_attrs,
365 .shost_attrs = megaraid_shost_attrs, 366 .shost_attrs = megaraid_shost_attrs,
366}; 367};
diff --git a/drivers/scsi/megaraid/megaraid_sas.c b/drivers/scsi/megaraid/megaraid_sas.c
index ebb948c016bb..e3c5c5282203 100644
--- a/drivers/scsi/megaraid/megaraid_sas.c
+++ b/drivers/scsi/megaraid/megaraid_sas.c
@@ -1110,6 +1110,7 @@ static struct scsi_host_template megasas_template = {
1110 .eh_timed_out = megasas_reset_timer, 1110 .eh_timed_out = megasas_reset_timer,
1111 .bios_param = megasas_bios_param, 1111 .bios_param = megasas_bios_param,
1112 .use_clustering = ENABLE_CLUSTERING, 1112 .use_clustering = ENABLE_CLUSTERING,
1113 .use_sg_chaining = ENABLE_SG_CHAINING,
1113}; 1114};
1114 1115
1115/** 1116/**
diff --git a/drivers/scsi/mesh.c b/drivers/scsi/mesh.c
index 651d09b08f2a..7470ff39ab22 100644
--- a/drivers/scsi/mesh.c
+++ b/drivers/scsi/mesh.c
@@ -1843,6 +1843,7 @@ static struct scsi_host_template mesh_template = {
1843 .sg_tablesize = SG_ALL, 1843 .sg_tablesize = SG_ALL,
1844 .cmd_per_lun = 2, 1844 .cmd_per_lun = 2,
1845 .use_clustering = DISABLE_CLUSTERING, 1845 .use_clustering = DISABLE_CLUSTERING,
1846 .use_sg_chaining = ENABLE_SG_CHAINING,
1846}; 1847};
1847 1848
1848static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match) 1849static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
diff --git a/drivers/scsi/nsp32.c b/drivers/scsi/nsp32.c
index 7fed35372150..28161dc95e0d 100644
--- a/drivers/scsi/nsp32.c
+++ b/drivers/scsi/nsp32.c
@@ -281,6 +281,7 @@ static struct scsi_host_template nsp32_template = {
281 .cmd_per_lun = 1, 281 .cmd_per_lun = 1,
282 .this_id = NSP32_HOST_SCSIID, 282 .this_id = NSP32_HOST_SCSIID,
283 .use_clustering = DISABLE_CLUSTERING, 283 .use_clustering = DISABLE_CLUSTERING,
284 .use_sg_chaining = ENABLE_SG_CHAINING,
284 .eh_abort_handler = nsp32_eh_abort, 285 .eh_abort_handler = nsp32_eh_abort,
285 .eh_bus_reset_handler = nsp32_eh_bus_reset, 286 .eh_bus_reset_handler = nsp32_eh_bus_reset,
286 .eh_host_reset_handler = nsp32_eh_host_reset, 287 .eh_host_reset_handler = nsp32_eh_host_reset,
diff --git a/drivers/scsi/pcmcia/sym53c500_cs.c b/drivers/scsi/pcmcia/sym53c500_cs.c
index 961839ecfe86..190e2a7d7067 100644
--- a/drivers/scsi/pcmcia/sym53c500_cs.c
+++ b/drivers/scsi/pcmcia/sym53c500_cs.c
@@ -694,6 +694,7 @@ static struct scsi_host_template sym53c500_driver_template = {
694 .sg_tablesize = 32, 694 .sg_tablesize = 32,
695 .cmd_per_lun = 1, 695 .cmd_per_lun = 1,
696 .use_clustering = ENABLE_CLUSTERING, 696 .use_clustering = ENABLE_CLUSTERING,
697 .use_sg_chaining = ENABLE_SG_CHAINING,
697 .shost_attrs = SYM53C500_shost_attrs 698 .shost_attrs = SYM53C500_shost_attrs
698}; 699};
699 700
diff --git a/drivers/scsi/qla1280.c b/drivers/scsi/qla1280.c
index fba8aa8a81b5..76089cf55f4e 100644
--- a/drivers/scsi/qla1280.c
+++ b/drivers/scsi/qla1280.c
@@ -2775,7 +2775,7 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2775 struct device_reg __iomem *reg = ha->iobase; 2775 struct device_reg __iomem *reg = ha->iobase;
2776 struct scsi_cmnd *cmd = sp->cmd; 2776 struct scsi_cmnd *cmd = sp->cmd;
2777 cmd_a64_entry_t *pkt; 2777 cmd_a64_entry_t *pkt;
2778 struct scatterlist *sg = NULL; 2778 struct scatterlist *sg = NULL, *s;
2779 __le32 *dword_ptr; 2779 __le32 *dword_ptr;
2780 dma_addr_t dma_handle; 2780 dma_addr_t dma_handle;
2781 int status = 0; 2781 int status = 0;
@@ -2889,13 +2889,16 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2889 * Load data segments. 2889 * Load data segments.
2890 */ 2890 */
2891 if (seg_cnt) { /* If data transfer. */ 2891 if (seg_cnt) { /* If data transfer. */
2892 int remseg = seg_cnt;
2892 /* Setup packet address segment pointer. */ 2893 /* Setup packet address segment pointer. */
2893 dword_ptr = (u32 *)&pkt->dseg_0_address; 2894 dword_ptr = (u32 *)&pkt->dseg_0_address;
2894 2895
2895 if (cmd->use_sg) { /* If scatter gather */ 2896 if (cmd->use_sg) { /* If scatter gather */
2896 /* Load command entry data segments. */ 2897 /* Load command entry data segments. */
2897 for (cnt = 0; cnt < 2 && seg_cnt; cnt++, seg_cnt--) { 2898 for_each_sg(sg, s, seg_cnt, cnt) {
2898 dma_handle = sg_dma_address(sg); 2899 if (cnt == 2)
2900 break;
2901 dma_handle = sg_dma_address(s);
2899#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2) 2902#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
2900 if (ha->flags.use_pci_vchannel) 2903 if (ha->flags.use_pci_vchannel)
2901 sn_pci_set_vchan(ha->pdev, 2904 sn_pci_set_vchan(ha->pdev,
@@ -2906,12 +2909,12 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2906 cpu_to_le32(pci_dma_lo32(dma_handle)); 2909 cpu_to_le32(pci_dma_lo32(dma_handle));
2907 *dword_ptr++ = 2910 *dword_ptr++ =
2908 cpu_to_le32(pci_dma_hi32(dma_handle)); 2911 cpu_to_le32(pci_dma_hi32(dma_handle));
2909 *dword_ptr++ = cpu_to_le32(sg_dma_len(sg)); 2912 *dword_ptr++ = cpu_to_le32(sg_dma_len(s));
2910 sg++;
2911 dprintk(3, "S/G Segment phys_addr=%x %x, len=0x%x\n", 2913 dprintk(3, "S/G Segment phys_addr=%x %x, len=0x%x\n",
2912 cpu_to_le32(pci_dma_hi32(dma_handle)), 2914 cpu_to_le32(pci_dma_hi32(dma_handle)),
2913 cpu_to_le32(pci_dma_lo32(dma_handle)), 2915 cpu_to_le32(pci_dma_lo32(dma_handle)),
2914 cpu_to_le32(sg_dma_len(sg))); 2916 cpu_to_le32(sg_dma_len(sg_next(s))));
2917 remseg--;
2915 } 2918 }
2916 dprintk(5, "qla1280_64bit_start_scsi: Scatter/gather " 2919 dprintk(5, "qla1280_64bit_start_scsi: Scatter/gather "
2917 "command packet data - b %i, t %i, l %i \n", 2920 "command packet data - b %i, t %i, l %i \n",
@@ -2926,7 +2929,9 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2926 dprintk(3, "S/G Building Continuation...seg_cnt=0x%x " 2929 dprintk(3, "S/G Building Continuation...seg_cnt=0x%x "
2927 "remains\n", seg_cnt); 2930 "remains\n", seg_cnt);
2928 2931
2929 while (seg_cnt > 0) { 2932 while (remseg > 0) {
2933 /* Update sg start */
2934 sg = s;
2930 /* Adjust ring index. */ 2935 /* Adjust ring index. */
2931 ha->req_ring_index++; 2936 ha->req_ring_index++;
2932 if (ha->req_ring_index == REQUEST_ENTRY_CNT) { 2937 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
@@ -2952,9 +2957,10 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2952 (u32 *)&((struct cont_a64_entry *) pkt)->dseg_0_address; 2957 (u32 *)&((struct cont_a64_entry *) pkt)->dseg_0_address;
2953 2958
2954 /* Load continuation entry data segments. */ 2959 /* Load continuation entry data segments. */
2955 for (cnt = 0; cnt < 5 && seg_cnt; 2960 for_each_sg(sg, s, remseg, cnt) {
2956 cnt++, seg_cnt--) { 2961 if (cnt == 5)
2957 dma_handle = sg_dma_address(sg); 2962 break;
2963 dma_handle = sg_dma_address(s);
2958#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2) 2964#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
2959 if (ha->flags.use_pci_vchannel) 2965 if (ha->flags.use_pci_vchannel)
2960 sn_pci_set_vchan(ha->pdev, 2966 sn_pci_set_vchan(ha->pdev,
@@ -2966,13 +2972,13 @@ qla1280_64bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
2966 *dword_ptr++ = 2972 *dword_ptr++ =
2967 cpu_to_le32(pci_dma_hi32(dma_handle)); 2973 cpu_to_le32(pci_dma_hi32(dma_handle));
2968 *dword_ptr++ = 2974 *dword_ptr++ =
2969 cpu_to_le32(sg_dma_len(sg)); 2975 cpu_to_le32(sg_dma_len(s));
2970 dprintk(3, "S/G Segment Cont. phys_addr=%x %x, len=0x%x\n", 2976 dprintk(3, "S/G Segment Cont. phys_addr=%x %x, len=0x%x\n",
2971 cpu_to_le32(pci_dma_hi32(dma_handle)), 2977 cpu_to_le32(pci_dma_hi32(dma_handle)),
2972 cpu_to_le32(pci_dma_lo32(dma_handle)), 2978 cpu_to_le32(pci_dma_lo32(dma_handle)),
2973 cpu_to_le32(sg_dma_len(sg))); 2979 cpu_to_le32(sg_dma_len(s)));
2974 sg++;
2975 } 2980 }
2981 remseg -= cnt;
2976 dprintk(5, "qla1280_64bit_start_scsi: " 2982 dprintk(5, "qla1280_64bit_start_scsi: "
2977 "continuation packet data - b %i, t " 2983 "continuation packet data - b %i, t "
2978 "%i, l %i \n", SCSI_BUS_32(cmd), 2984 "%i, l %i \n", SCSI_BUS_32(cmd),
@@ -3062,7 +3068,7 @@ qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3062 struct device_reg __iomem *reg = ha->iobase; 3068 struct device_reg __iomem *reg = ha->iobase;
3063 struct scsi_cmnd *cmd = sp->cmd; 3069 struct scsi_cmnd *cmd = sp->cmd;
3064 struct cmd_entry *pkt; 3070 struct cmd_entry *pkt;
3065 struct scatterlist *sg = NULL; 3071 struct scatterlist *sg = NULL, *s;
3066 __le32 *dword_ptr; 3072 __le32 *dword_ptr;
3067 int status = 0; 3073 int status = 0;
3068 int cnt; 3074 int cnt;
@@ -3188,6 +3194,7 @@ qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3188 * Load data segments. 3194 * Load data segments.
3189 */ 3195 */
3190 if (seg_cnt) { 3196 if (seg_cnt) {
3197 int remseg = seg_cnt;
3191 /* Setup packet address segment pointer. */ 3198 /* Setup packet address segment pointer. */
3192 dword_ptr = &pkt->dseg_0_address; 3199 dword_ptr = &pkt->dseg_0_address;
3193 3200
@@ -3196,22 +3203,25 @@ qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3196 qla1280_dump_buffer(1, (char *)sg, 4 * 16); 3203 qla1280_dump_buffer(1, (char *)sg, 4 * 16);
3197 3204
3198 /* Load command entry data segments. */ 3205 /* Load command entry data segments. */
3199 for (cnt = 0; cnt < 4 && seg_cnt; cnt++, seg_cnt--) { 3206 for_each_sg(sg, s, seg_cnt, cnt) {
3207 if (cnt == 4)
3208 break;
3200 *dword_ptr++ = 3209 *dword_ptr++ =
3201 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))); 3210 cpu_to_le32(pci_dma_lo32(sg_dma_address(s)));
3202 *dword_ptr++ = 3211 *dword_ptr++ = cpu_to_le32(sg_dma_len(s));
3203 cpu_to_le32(sg_dma_len(sg));
3204 dprintk(3, "S/G Segment phys_addr=0x%lx, len=0x%x\n", 3212 dprintk(3, "S/G Segment phys_addr=0x%lx, len=0x%x\n",
3205 (pci_dma_lo32(sg_dma_address(sg))), 3213 (pci_dma_lo32(sg_dma_address(s))),
3206 (sg_dma_len(sg))); 3214 (sg_dma_len(s)));
3207 sg++; 3215 remseg--;
3208 } 3216 }
3209 /* 3217 /*
3210 * Build continuation packets. 3218 * Build continuation packets.
3211 */ 3219 */
3212 dprintk(3, "S/G Building Continuation" 3220 dprintk(3, "S/G Building Continuation"
3213 "...seg_cnt=0x%x remains\n", seg_cnt); 3221 "...seg_cnt=0x%x remains\n", seg_cnt);
3214 while (seg_cnt > 0) { 3222 while (remseg > 0) {
3223 /* Continue from end point */
3224 sg = s;
3215 /* Adjust ring index. */ 3225 /* Adjust ring index. */
3216 ha->req_ring_index++; 3226 ha->req_ring_index++;
3217 if (ha->req_ring_index == REQUEST_ENTRY_CNT) { 3227 if (ha->req_ring_index == REQUEST_ENTRY_CNT) {
@@ -3239,19 +3249,20 @@ qla1280_32bit_start_scsi(struct scsi_qla_host *ha, struct srb * sp)
3239 &((struct cont_entry *) pkt)->dseg_0_address; 3249 &((struct cont_entry *) pkt)->dseg_0_address;
3240 3250
3241 /* Load continuation entry data segments. */ 3251 /* Load continuation entry data segments. */
3242 for (cnt = 0; cnt < 7 && seg_cnt; 3252 for_each_sg(sg, s, remseg, cnt) {
3243 cnt++, seg_cnt--) { 3253 if (cnt == 7)
3254 break;
3244 *dword_ptr++ = 3255 *dword_ptr++ =
3245 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))); 3256 cpu_to_le32(pci_dma_lo32(sg_dma_address(s)));
3246 *dword_ptr++ = 3257 *dword_ptr++ =
3247 cpu_to_le32(sg_dma_len(sg)); 3258 cpu_to_le32(sg_dma_len(s));
3248 dprintk(1, 3259 dprintk(1,
3249 "S/G Segment Cont. phys_addr=0x%x, " 3260 "S/G Segment Cont. phys_addr=0x%x, "
3250 "len=0x%x\n", 3261 "len=0x%x\n",
3251 cpu_to_le32(pci_dma_lo32(sg_dma_address(sg))), 3262 cpu_to_le32(pci_dma_lo32(sg_dma_address(s))),
3252 cpu_to_le32(sg_dma_len(sg))); 3263 cpu_to_le32(sg_dma_len(s)));
3253 sg++;
3254 } 3264 }
3265 remseg -= cnt;
3255 dprintk(5, "qla1280_32bit_start_scsi: " 3266 dprintk(5, "qla1280_32bit_start_scsi: "
3256 "continuation packet data - " 3267 "continuation packet data - "
3257 "scsi(%i:%i:%i)\n", SCSI_BUS_32(cmd), 3268 "scsi(%i:%i:%i)\n", SCSI_BUS_32(cmd),
@@ -4248,6 +4259,7 @@ static struct scsi_host_template qla1280_driver_template = {
4248 .sg_tablesize = SG_ALL, 4259 .sg_tablesize = SG_ALL,
4249 .cmd_per_lun = 1, 4260 .cmd_per_lun = 1,
4250 .use_clustering = ENABLE_CLUSTERING, 4261 .use_clustering = ENABLE_CLUSTERING,
4262 .use_sg_chaining = ENABLE_SG_CHAINING,
4251}; 4263};
4252 4264
4253 4265
diff --git a/drivers/scsi/qla2xxx/qla_os.c b/drivers/scsi/qla2xxx/qla_os.c
index a6bb8d0ecf13..0351d380c2d7 100644
--- a/drivers/scsi/qla2xxx/qla_os.c
+++ b/drivers/scsi/qla2xxx/qla_os.c
@@ -132,6 +132,7 @@ struct scsi_host_template qla2x00_driver_template = {
132 .this_id = -1, 132 .this_id = -1,
133 .cmd_per_lun = 3, 133 .cmd_per_lun = 3,
134 .use_clustering = ENABLE_CLUSTERING, 134 .use_clustering = ENABLE_CLUSTERING,
135 .use_sg_chaining = ENABLE_SG_CHAINING,
135 .sg_tablesize = SG_ALL, 136 .sg_tablesize = SG_ALL,
136 137
137 /* 138 /*
@@ -163,6 +164,7 @@ struct scsi_host_template qla24xx_driver_template = {
163 .this_id = -1, 164 .this_id = -1,
164 .cmd_per_lun = 3, 165 .cmd_per_lun = 3,
165 .use_clustering = ENABLE_CLUSTERING, 166 .use_clustering = ENABLE_CLUSTERING,
167 .use_sg_chaining = ENABLE_SG_CHAINING,
166 .sg_tablesize = SG_ALL, 168 .sg_tablesize = SG_ALL,
167 169
168 .max_sectors = 0xFFFF, 170 .max_sectors = 0xFFFF,
diff --git a/drivers/scsi/qla4xxx/ql4_os.c b/drivers/scsi/qla4xxx/ql4_os.c
index b1d565c12c5b..03b68d4f3bd0 100644
--- a/drivers/scsi/qla4xxx/ql4_os.c
+++ b/drivers/scsi/qla4xxx/ql4_os.c
@@ -94,6 +94,7 @@ static struct scsi_host_template qla4xxx_driver_template = {
94 .this_id = -1, 94 .this_id = -1,
95 .cmd_per_lun = 3, 95 .cmd_per_lun = 3,
96 .use_clustering = ENABLE_CLUSTERING, 96 .use_clustering = ENABLE_CLUSTERING,
97 .use_sg_chaining = ENABLE_SG_CHAINING,
97 .sg_tablesize = SG_ALL, 98 .sg_tablesize = SG_ALL,
98 99
99 .max_sectors = 0xFFFF, 100 .max_sectors = 0xFFFF,
diff --git a/drivers/scsi/qlogicfas.c b/drivers/scsi/qlogicfas.c
index 1e874f1fb5c6..1769f965eedf 100644
--- a/drivers/scsi/qlogicfas.c
+++ b/drivers/scsi/qlogicfas.c
@@ -197,6 +197,7 @@ static struct scsi_host_template qlogicfas_driver_template = {
197 .sg_tablesize = SG_ALL, 197 .sg_tablesize = SG_ALL,
198 .cmd_per_lun = 1, 198 .cmd_per_lun = 1,
199 .use_clustering = DISABLE_CLUSTERING, 199 .use_clustering = DISABLE_CLUSTERING,
200 .use_sg_chaining = ENABLE_SG_CHAINING,
200}; 201};
201 202
202static __init int qlogicfas_init(void) 203static __init int qlogicfas_init(void)
diff --git a/drivers/scsi/qlogicpti.c b/drivers/scsi/qlogicpti.c
index e93f80316a19..7a2e7986b038 100644
--- a/drivers/scsi/qlogicpti.c
+++ b/drivers/scsi/qlogicpti.c
@@ -868,7 +868,7 @@ static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
868 struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr) 868 struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
869{ 869{
870 struct dataseg *ds; 870 struct dataseg *ds;
871 struct scatterlist *sg; 871 struct scatterlist *sg, *s;
872 int i, n; 872 int i, n;
873 873
874 if (Cmnd->use_sg) { 874 if (Cmnd->use_sg) {
@@ -884,11 +884,12 @@ static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
884 n = sg_count; 884 n = sg_count;
885 if (n > 4) 885 if (n > 4)
886 n = 4; 886 n = 4;
887 for (i = 0; i < n; i++, sg++) { 887 for_each_sg(sg, s, n, i) {
888 ds[i].d_base = sg_dma_address(sg); 888 ds[i].d_base = sg_dma_address(s);
889 ds[i].d_count = sg_dma_len(sg); 889 ds[i].d_count = sg_dma_len(s);
890 } 890 }
891 sg_count -= 4; 891 sg_count -= 4;
892 sg = s;
892 while (sg_count > 0) { 893 while (sg_count > 0) {
893 struct Continuation_Entry *cont; 894 struct Continuation_Entry *cont;
894 895
@@ -907,9 +908,9 @@ static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
907 n = sg_count; 908 n = sg_count;
908 if (n > 7) 909 if (n > 7)
909 n = 7; 910 n = 7;
910 for (i = 0; i < n; i++, sg++) { 911 for_each_sg(sg, s, n, i) {
911 ds[i].d_base = sg_dma_address(sg); 912 ds[i].d_base = sg_dma_address(s);
912 ds[i].d_count = sg_dma_len(sg); 913 ds[i].d_count = sg_dma_len(s);
913 } 914 }
914 sg_count -= n; 915 sg_count -= n;
915 } 916 }
diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c
index 4947dfe625a6..72ee4c9cfb1a 100644
--- a/drivers/scsi/scsi_debug.c
+++ b/drivers/scsi/scsi_debug.c
@@ -38,6 +38,7 @@
38#include <linux/proc_fs.h> 38#include <linux/proc_fs.h>
39#include <linux/vmalloc.h> 39#include <linux/vmalloc.h>
40#include <linux/moduleparam.h> 40#include <linux/moduleparam.h>
41#include <linux/scatterlist.h>
41 42
42#include <linux/blkdev.h> 43#include <linux/blkdev.h>
43#include "scsi.h" 44#include "scsi.h"
@@ -600,7 +601,7 @@ static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
600 int k, req_len, act_len, len, active; 601 int k, req_len, act_len, len, active;
601 void * kaddr; 602 void * kaddr;
602 void * kaddr_off; 603 void * kaddr_off;
603 struct scatterlist * sgpnt; 604 struct scatterlist * sg;
604 605
605 if (0 == scp->request_bufflen) 606 if (0 == scp->request_bufflen)
606 return 0; 607 return 0;
@@ -619,16 +620,16 @@ static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
619 scp->resid = req_len - act_len; 620 scp->resid = req_len - act_len;
620 return 0; 621 return 0;
621 } 622 }
622 sgpnt = (struct scatterlist *)scp->request_buffer;
623 active = 1; 623 active = 1;
624 for (k = 0, req_len = 0, act_len = 0; k < scp->use_sg; ++k, ++sgpnt) { 624 req_len = act_len = 0;
625 scsi_for_each_sg(scp, sg, scp->use_sg, k) {
625 if (active) { 626 if (active) {
626 kaddr = (unsigned char *) 627 kaddr = (unsigned char *)
627 kmap_atomic(sgpnt->page, KM_USER0); 628 kmap_atomic(sg->page, KM_USER0);
628 if (NULL == kaddr) 629 if (NULL == kaddr)
629 return (DID_ERROR << 16); 630 return (DID_ERROR << 16);
630 kaddr_off = (unsigned char *)kaddr + sgpnt->offset; 631 kaddr_off = (unsigned char *)kaddr + sg->offset;
631 len = sgpnt->length; 632 len = sg->length;
632 if ((req_len + len) > arr_len) { 633 if ((req_len + len) > arr_len) {
633 active = 0; 634 active = 0;
634 len = arr_len - req_len; 635 len = arr_len - req_len;
@@ -637,7 +638,7 @@ static int fill_from_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
637 kunmap_atomic(kaddr, KM_USER0); 638 kunmap_atomic(kaddr, KM_USER0);
638 act_len += len; 639 act_len += len;
639 } 640 }
640 req_len += sgpnt->length; 641 req_len += sg->length;
641 } 642 }
642 if (scp->resid) 643 if (scp->resid)
643 scp->resid -= act_len; 644 scp->resid -= act_len;
@@ -653,7 +654,7 @@ static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
653 int k, req_len, len, fin; 654 int k, req_len, len, fin;
654 void * kaddr; 655 void * kaddr;
655 void * kaddr_off; 656 void * kaddr_off;
656 struct scatterlist * sgpnt; 657 struct scatterlist * sg;
657 658
658 if (0 == scp->request_bufflen) 659 if (0 == scp->request_bufflen)
659 return 0; 660 return 0;
@@ -668,13 +669,14 @@ static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
668 memcpy(arr, scp->request_buffer, len); 669 memcpy(arr, scp->request_buffer, len);
669 return len; 670 return len;
670 } 671 }
671 sgpnt = (struct scatterlist *)scp->request_buffer; 672 sg = scsi_sglist(scp);
672 for (k = 0, req_len = 0, fin = 0; k < scp->use_sg; ++k, ++sgpnt) { 673 req_len = fin = 0;
673 kaddr = (unsigned char *)kmap_atomic(sgpnt->page, KM_USER0); 674 for (k = 0; k < scp->use_sg; ++k, sg = sg_next(sg)) {
675 kaddr = (unsigned char *)kmap_atomic(sg->page, KM_USER0);
674 if (NULL == kaddr) 676 if (NULL == kaddr)
675 return -1; 677 return -1;
676 kaddr_off = (unsigned char *)kaddr + sgpnt->offset; 678 kaddr_off = (unsigned char *)kaddr + sg->offset;
677 len = sgpnt->length; 679 len = sg->length;
678 if ((req_len + len) > max_arr_len) { 680 if ((req_len + len) > max_arr_len) {
679 len = max_arr_len - req_len; 681 len = max_arr_len - req_len;
680 fin = 1; 682 fin = 1;
@@ -683,7 +685,7 @@ static int fetch_to_dev_buffer(struct scsi_cmnd * scp, unsigned char * arr,
683 kunmap_atomic(kaddr, KM_USER0); 685 kunmap_atomic(kaddr, KM_USER0);
684 if (fin) 686 if (fin)
685 return req_len + len; 687 return req_len + len;
686 req_len += sgpnt->length; 688 req_len += sg->length;
687 } 689 }
688 return req_len; 690 return req_len;
689} 691}
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 207f1aa08869..aac8a02cbe80 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -17,6 +17,7 @@
17#include <linux/pci.h> 17#include <linux/pci.h>
18#include <linux/delay.h> 18#include <linux/delay.h>
19#include <linux/hardirq.h> 19#include <linux/hardirq.h>
20#include <linux/scatterlist.h>
20 21
21#include <scsi/scsi.h> 22#include <scsi/scsi.h>
22#include <scsi/scsi_cmnd.h> 23#include <scsi/scsi_cmnd.h>
@@ -33,35 +34,34 @@
33#define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools) 34#define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
34#define SG_MEMPOOL_SIZE 2 35#define SG_MEMPOOL_SIZE 2
35 36
37/*
38 * The maximum number of SG segments that we will put inside a scatterlist
39 * (unless chaining is used). Should ideally fit inside a single page, to
40 * avoid a higher order allocation.
41 */
42#define SCSI_MAX_SG_SEGMENTS 128
43
36struct scsi_host_sg_pool { 44struct scsi_host_sg_pool {
37 size_t size; 45 size_t size;
38 char *name; 46 char *name;
39 struct kmem_cache *slab; 47 struct kmem_cache *slab;
40 mempool_t *pool; 48 mempool_t *pool;
41}; 49};
42 50
43#if (SCSI_MAX_PHYS_SEGMENTS < 32) 51#define SP(x) { x, "sgpool-" #x }
44#error SCSI_MAX_PHYS_SEGMENTS is too small
45#endif
46
47#define SP(x) { x, "sgpool-" #x }
48static struct scsi_host_sg_pool scsi_sg_pools[] = { 52static struct scsi_host_sg_pool scsi_sg_pools[] = {
49 SP(8), 53 SP(8),
50 SP(16), 54 SP(16),
55#if (SCSI_MAX_SG_SEGMENTS > 16)
51 SP(32), 56 SP(32),
52#if (SCSI_MAX_PHYS_SEGMENTS > 32) 57#if (SCSI_MAX_SG_SEGMENTS > 32)
53 SP(64), 58 SP(64),
54#if (SCSI_MAX_PHYS_SEGMENTS > 64) 59#if (SCSI_MAX_SG_SEGMENTS > 64)
55 SP(128), 60 SP(128),
56#if (SCSI_MAX_PHYS_SEGMENTS > 128)
57 SP(256),
58#if (SCSI_MAX_PHYS_SEGMENTS > 256)
59#error SCSI_MAX_PHYS_SEGMENTS is too large
60#endif
61#endif 61#endif
62#endif 62#endif
63#endif 63#endif
64}; 64};
65#undef SP 65#undef SP
66 66
67static void scsi_run_queue(struct request_queue *q); 67static void scsi_run_queue(struct request_queue *q);
@@ -289,14 +289,16 @@ static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
289 struct request_queue *q = rq->q; 289 struct request_queue *q = rq->q;
290 int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT; 290 int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
291 unsigned int data_len = bufflen, len, bytes, off; 291 unsigned int data_len = bufflen, len, bytes, off;
292 struct scatterlist *sg;
292 struct page *page; 293 struct page *page;
293 struct bio *bio = NULL; 294 struct bio *bio = NULL;
294 int i, err, nr_vecs = 0; 295 int i, err, nr_vecs = 0;
295 296
296 for (i = 0; i < nsegs; i++) { 297 for_each_sg(sgl, sg, nsegs, i) {
297 page = sgl[i].page; 298 page = sg->page;
298 off = sgl[i].offset; 299 off = sg->offset;
299 len = sgl[i].length; 300 len = sg->length;
301 data_len += len;
300 302
301 while (len > 0 && data_len > 0) { 303 while (len > 0 && data_len > 0) {
302 /* 304 /*
@@ -695,56 +697,170 @@ static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
695 return NULL; 697 return NULL;
696} 698}
697 699
698struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask) 700/*
699{ 701 * Like SCSI_MAX_SG_SEGMENTS, but for archs that have sg chaining. This limit
700 struct scsi_host_sg_pool *sgp; 702 * is totally arbitrary, a setting of 2048 will get you at least 8mb ios.
701 struct scatterlist *sgl; 703 */
704#define SCSI_MAX_SG_CHAIN_SEGMENTS 2048
702 705
703 BUG_ON(!cmd->use_sg); 706static inline unsigned int scsi_sgtable_index(unsigned short nents)
707{
708 unsigned int index;
704 709
705 switch (cmd->use_sg) { 710 switch (nents) {
706 case 1 ... 8: 711 case 1 ... 8:
707 cmd->sglist_len = 0; 712 index = 0;
708 break; 713 break;
709 case 9 ... 16: 714 case 9 ... 16:
710 cmd->sglist_len = 1; 715 index = 1;
711 break; 716 break;
717#if (SCSI_MAX_SG_SEGMENTS > 16)
712 case 17 ... 32: 718 case 17 ... 32:
713 cmd->sglist_len = 2; 719 index = 2;
714 break; 720 break;
715#if (SCSI_MAX_PHYS_SEGMENTS > 32) 721#if (SCSI_MAX_SG_SEGMENTS > 32)
716 case 33 ... 64: 722 case 33 ... 64:
717 cmd->sglist_len = 3; 723 index = 3;
718 break; 724 break;
719#if (SCSI_MAX_PHYS_SEGMENTS > 64) 725#if (SCSI_MAX_SG_SEGMENTS > 64)
720 case 65 ... 128: 726 case 65 ... 128:
721 cmd->sglist_len = 4; 727 index = 4;
722 break;
723#if (SCSI_MAX_PHYS_SEGMENTS > 128)
724 case 129 ... 256:
725 cmd->sglist_len = 5;
726 break; 728 break;
727#endif 729#endif
728#endif 730#endif
729#endif 731#endif
730 default: 732 default:
731 return NULL; 733 printk(KERN_ERR "scsi: bad segment count=%d\n", nents);
734 BUG();
732 } 735 }
733 736
734 sgp = scsi_sg_pools + cmd->sglist_len; 737 return index;
735 sgl = mempool_alloc(sgp->pool, gfp_mask); 738}
736 return sgl; 739
740struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
741{
742 struct scsi_host_sg_pool *sgp;
743 struct scatterlist *sgl, *prev, *ret;
744 unsigned int index;
745 int this, left;
746
747 BUG_ON(!cmd->use_sg);
748
749 left = cmd->use_sg;
750 ret = prev = NULL;
751 do {
752 this = left;
753 if (this > SCSI_MAX_SG_SEGMENTS) {
754 this = SCSI_MAX_SG_SEGMENTS - 1;
755 index = SG_MEMPOOL_NR - 1;
756 } else
757 index = scsi_sgtable_index(this);
758
759 left -= this;
760
761 sgp = scsi_sg_pools + index;
762
763 sgl = mempool_alloc(sgp->pool, gfp_mask);
764 if (unlikely(!sgl))
765 goto enomem;
766
767 memset(sgl, 0, sizeof(*sgl) * sgp->size);
768
769 /*
770 * first loop through, set initial index and return value
771 */
772 if (!ret)
773 ret = sgl;
774
775 /*
776 * chain previous sglist, if any. we know the previous
777 * sglist must be the biggest one, or we would not have
778 * ended up doing another loop.
779 */
780 if (prev)
781 sg_chain(prev, SCSI_MAX_SG_SEGMENTS, sgl);
782
783 /*
784 * don't allow subsequent mempool allocs to sleep, it would
785 * violate the mempool principle.
786 */
787 gfp_mask &= ~__GFP_WAIT;
788 gfp_mask |= __GFP_HIGH;
789 prev = sgl;
790 } while (left);
791
792 /*
793 * ->use_sg may get modified after dma mapping has potentially
794 * shrunk the number of segments, so keep a copy of it for free.
795 */
796 cmd->__use_sg = cmd->use_sg;
797 return ret;
798enomem:
799 if (ret) {
800 /*
801 * Free entries chained off ret. Since we were trying to
802 * allocate another sglist, we know that all entries are of
803 * the max size.
804 */
805 sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1;
806 prev = ret;
807 ret = &ret[SCSI_MAX_SG_SEGMENTS - 1];
808
809 while ((sgl = sg_chain_ptr(ret)) != NULL) {
810 ret = &sgl[SCSI_MAX_SG_SEGMENTS - 1];
811 mempool_free(sgl, sgp->pool);
812 }
813
814 mempool_free(prev, sgp->pool);
815 }
816 return NULL;
737} 817}
738 818
739EXPORT_SYMBOL(scsi_alloc_sgtable); 819EXPORT_SYMBOL(scsi_alloc_sgtable);
740 820
741void scsi_free_sgtable(struct scatterlist *sgl, int index) 821void scsi_free_sgtable(struct scsi_cmnd *cmd)
742{ 822{
823 struct scatterlist *sgl = cmd->request_buffer;
743 struct scsi_host_sg_pool *sgp; 824 struct scsi_host_sg_pool *sgp;
744 825
745 BUG_ON(index >= SG_MEMPOOL_NR); 826 /*
827 * if this is the biggest size sglist, check if we have
828 * chained parts we need to free
829 */
830 if (cmd->__use_sg > SCSI_MAX_SG_SEGMENTS) {
831 unsigned short this, left;
832 struct scatterlist *next;
833 unsigned int index;
834
835 left = cmd->__use_sg - (SCSI_MAX_SG_SEGMENTS - 1);
836 next = sg_chain_ptr(&sgl[SCSI_MAX_SG_SEGMENTS - 1]);
837 while (left && next) {
838 sgl = next;
839 this = left;
840 if (this > SCSI_MAX_SG_SEGMENTS) {
841 this = SCSI_MAX_SG_SEGMENTS - 1;
842 index = SG_MEMPOOL_NR - 1;
843 } else
844 index = scsi_sgtable_index(this);
845
846 left -= this;
847
848 sgp = scsi_sg_pools + index;
849
850 if (left)
851 next = sg_chain_ptr(&sgl[sgp->size - 1]);
852
853 mempool_free(sgl, sgp->pool);
854 }
855
856 /*
857 * Restore original, will be freed below
858 */
859 sgl = cmd->request_buffer;
860 sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1;
861 } else
862 sgp = scsi_sg_pools + scsi_sgtable_index(cmd->__use_sg);
746 863
747 sgp = scsi_sg_pools + index;
748 mempool_free(sgl, sgp->pool); 864 mempool_free(sgl, sgp->pool);
749} 865}
750 866
@@ -770,7 +886,7 @@ EXPORT_SYMBOL(scsi_free_sgtable);
770static void scsi_release_buffers(struct scsi_cmnd *cmd) 886static void scsi_release_buffers(struct scsi_cmnd *cmd)
771{ 887{
772 if (cmd->use_sg) 888 if (cmd->use_sg)
773 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); 889 scsi_free_sgtable(cmd);
774 890
775 /* 891 /*
776 * Zero these out. They now point to freed memory, and it is 892 * Zero these out. They now point to freed memory, and it is
@@ -984,7 +1100,6 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
984static int scsi_init_io(struct scsi_cmnd *cmd) 1100static int scsi_init_io(struct scsi_cmnd *cmd)
985{ 1101{
986 struct request *req = cmd->request; 1102 struct request *req = cmd->request;
987 struct scatterlist *sgpnt;
988 int count; 1103 int count;
989 1104
990 /* 1105 /*
@@ -997,14 +1112,13 @@ static int scsi_init_io(struct scsi_cmnd *cmd)
997 /* 1112 /*
998 * If sg table allocation fails, requeue request later. 1113 * If sg table allocation fails, requeue request later.
999 */ 1114 */
1000 sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC); 1115 cmd->request_buffer = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
1001 if (unlikely(!sgpnt)) { 1116 if (unlikely(!cmd->request_buffer)) {
1002 scsi_unprep_request(req); 1117 scsi_unprep_request(req);
1003 return BLKPREP_DEFER; 1118 return BLKPREP_DEFER;
1004 } 1119 }
1005 1120
1006 req->buffer = NULL; 1121 req->buffer = NULL;
1007 cmd->request_buffer = (char *) sgpnt;
1008 if (blk_pc_request(req)) 1122 if (blk_pc_request(req))
1009 cmd->request_bufflen = req->data_len; 1123 cmd->request_bufflen = req->data_len;
1010 else 1124 else
@@ -1529,8 +1643,25 @@ struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
1529 if (!q) 1643 if (!q)
1530 return NULL; 1644 return NULL;
1531 1645
1646 /*
1647 * this limit is imposed by hardware restrictions
1648 */
1532 blk_queue_max_hw_segments(q, shost->sg_tablesize); 1649 blk_queue_max_hw_segments(q, shost->sg_tablesize);
1533 blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS); 1650
1651 /*
1652 * In the future, sg chaining support will be mandatory and this
1653 * ifdef can then go away. Right now we don't have all archs
1654 * converted, so better keep it safe.
1655 */
1656#ifdef ARCH_HAS_SG_CHAIN
1657 if (shost->use_sg_chaining)
1658 blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
1659 else
1660 blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);
1661#else
1662 blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS);
1663#endif
1664
1534 blk_queue_max_sectors(q, shost->max_sectors); 1665 blk_queue_max_sectors(q, shost->max_sectors);
1535 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); 1666 blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
1536 blk_queue_segment_boundary(q, shost->dma_boundary); 1667 blk_queue_segment_boundary(q, shost->dma_boundary);
@@ -2193,18 +2324,19 @@ EXPORT_SYMBOL_GPL(scsi_target_unblock);
2193 * 2324 *
2194 * Returns virtual address of the start of the mapped page 2325 * Returns virtual address of the start of the mapped page
2195 */ 2326 */
2196void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count, 2327void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
2197 size_t *offset, size_t *len) 2328 size_t *offset, size_t *len)
2198{ 2329{
2199 int i; 2330 int i;
2200 size_t sg_len = 0, len_complete = 0; 2331 size_t sg_len = 0, len_complete = 0;
2332 struct scatterlist *sg;
2201 struct page *page; 2333 struct page *page;
2202 2334
2203 WARN_ON(!irqs_disabled()); 2335 WARN_ON(!irqs_disabled());
2204 2336
2205 for (i = 0; i < sg_count; i++) { 2337 for_each_sg(sgl, sg, sg_count, i) {
2206 len_complete = sg_len; /* Complete sg-entries */ 2338 len_complete = sg_len; /* Complete sg-entries */
2207 sg_len += sg[i].length; 2339 sg_len += sg->length;
2208 if (sg_len > *offset) 2340 if (sg_len > *offset)
2209 break; 2341 break;
2210 } 2342 }
@@ -2218,10 +2350,10 @@ void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
2218 } 2350 }
2219 2351
2220 /* Offset starting from the beginning of first page in this sg-entry */ 2352 /* Offset starting from the beginning of first page in this sg-entry */
2221 *offset = *offset - len_complete + sg[i].offset; 2353 *offset = *offset - len_complete + sg->offset;
2222 2354
2223 /* Assumption: contiguous pages can be accessed as "page + i" */ 2355 /* Assumption: contiguous pages can be accessed as "page + i" */
2224 page = nth_page(sg[i].page, (*offset >> PAGE_SHIFT)); 2356 page = nth_page(sg->page, (*offset >> PAGE_SHIFT));
2225 *offset &= ~PAGE_MASK; 2357 *offset &= ~PAGE_MASK;
2226 2358
2227 /* Bytes in this sg-entry from *offset to the end of the page */ 2359 /* Bytes in this sg-entry from *offset to the end of the page */
diff --git a/drivers/scsi/scsi_tgt_lib.c b/drivers/scsi/scsi_tgt_lib.c
index 66c692ffa305..a91761c3645f 100644
--- a/drivers/scsi/scsi_tgt_lib.c
+++ b/drivers/scsi/scsi_tgt_lib.c
@@ -332,7 +332,7 @@ static void scsi_tgt_cmd_done(struct scsi_cmnd *cmd)
332 scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag); 332 scsi_tgt_uspace_send_status(cmd, tcmd->itn_id, tcmd->tag);
333 333
334 if (cmd->request_buffer) 334 if (cmd->request_buffer)
335 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); 335 scsi_free_sgtable(cmd);
336 336
337 queue_work(scsi_tgtd, &tcmd->work); 337 queue_work(scsi_tgtd, &tcmd->work);
338} 338}
@@ -373,7 +373,7 @@ static int scsi_tgt_init_cmd(struct scsi_cmnd *cmd, gfp_t gfp_mask)
373 } 373 }
374 374
375 eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg); 375 eprintk("cmd %p cnt %d\n", cmd, cmd->use_sg);
376 scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); 376 scsi_free_sgtable(cmd);
377 return -EINVAL; 377 return -EINVAL;
378} 378}
379 379
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index f6f5fc7d0cee..7238b2dfc497 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -1165,7 +1165,7 @@ sg_vma_nopage(struct vm_area_struct *vma, unsigned long addr, int *type)
1165 sg = rsv_schp->buffer; 1165 sg = rsv_schp->buffer;
1166 sa = vma->vm_start; 1166 sa = vma->vm_start;
1167 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end); 1167 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1168 ++k, ++sg) { 1168 ++k, sg = sg_next(sg)) {
1169 len = vma->vm_end - sa; 1169 len = vma->vm_end - sa;
1170 len = (len < sg->length) ? len : sg->length; 1170 len = (len < sg->length) ? len : sg->length;
1171 if (offset < len) { 1171 if (offset < len) {
@@ -1209,7 +1209,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
1209 sa = vma->vm_start; 1209 sa = vma->vm_start;
1210 sg = rsv_schp->buffer; 1210 sg = rsv_schp->buffer;
1211 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end); 1211 for (k = 0; (k < rsv_schp->k_use_sg) && (sa < vma->vm_end);
1212 ++k, ++sg) { 1212 ++k, sg = sg_next(sg)) {
1213 len = vma->vm_end - sa; 1213 len = vma->vm_end - sa;
1214 len = (len < sg->length) ? len : sg->length; 1214 len = (len < sg->length) ? len : sg->length;
1215 sa += len; 1215 sa += len;
@@ -1840,7 +1840,7 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1840 } 1840 }
1841 for (k = 0, sg = schp->buffer, rem_sz = blk_size; 1841 for (k = 0, sg = schp->buffer, rem_sz = blk_size;
1842 (rem_sz > 0) && (k < mx_sc_elems); 1842 (rem_sz > 0) && (k < mx_sc_elems);
1843 ++k, rem_sz -= ret_sz, ++sg) { 1843 ++k, rem_sz -= ret_sz, sg = sg_next(sg)) {
1844 1844
1845 num = (rem_sz > scatter_elem_sz_prev) ? 1845 num = (rem_sz > scatter_elem_sz_prev) ?
1846 scatter_elem_sz_prev : rem_sz; 1846 scatter_elem_sz_prev : rem_sz;
@@ -1913,7 +1913,7 @@ sg_write_xfer(Sg_request * srp)
1913 if (res) 1913 if (res)
1914 return res; 1914 return res;
1915 1915
1916 for (; p; ++sg, ksglen = sg->length, 1916 for (; p; sg = sg_next(sg), ksglen = sg->length,
1917 p = page_address(sg->page)) { 1917 p = page_address(sg->page)) {
1918 if (usglen <= 0) 1918 if (usglen <= 0)
1919 break; 1919 break;
@@ -1992,7 +1992,7 @@ sg_remove_scat(Sg_scatter_hold * schp)
1992 int k; 1992 int k;
1993 1993
1994 for (k = 0; (k < schp->k_use_sg) && sg->page; 1994 for (k = 0; (k < schp->k_use_sg) && sg->page;
1995 ++k, ++sg) { 1995 ++k, sg = sg_next(sg)) {
1996 SCSI_LOG_TIMEOUT(5, printk( 1996 SCSI_LOG_TIMEOUT(5, printk(
1997 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n", 1997 "sg_remove_scat: k=%d, pg=0x%p, len=%d\n",
1998 k, sg->page, sg->length)); 1998 k, sg->page, sg->length));
@@ -2045,7 +2045,7 @@ sg_read_xfer(Sg_request * srp)
2045 if (res) 2045 if (res)
2046 return res; 2046 return res;
2047 2047
2048 for (; p; ++sg, ksglen = sg->length, 2048 for (; p; sg = sg_next(sg), ksglen = sg->length,
2049 p = page_address(sg->page)) { 2049 p = page_address(sg->page)) {
2050 if (usglen <= 0) 2050 if (usglen <= 0)
2051 break; 2051 break;
@@ -2092,7 +2092,7 @@ sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
2092 if ((!outp) || (num_read_xfer <= 0)) 2092 if ((!outp) || (num_read_xfer <= 0))
2093 return 0; 2093 return 0;
2094 2094
2095 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, ++sg) { 2095 for (k = 0; (k < schp->k_use_sg) && sg->page; ++k, sg = sg_next(sg)) {
2096 num = sg->length; 2096 num = sg->length;
2097 if (num > num_read_xfer) { 2097 if (num > num_read_xfer) {
2098 if (__copy_to_user(outp, page_address(sg->page), 2098 if (__copy_to_user(outp, page_address(sg->page),
@@ -2142,7 +2142,7 @@ sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
2142 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size)); 2142 SCSI_LOG_TIMEOUT(4, printk("sg_link_reserve: size=%d\n", size));
2143 rem = size; 2143 rem = size;
2144 2144
2145 for (k = 0; k < rsv_schp->k_use_sg; ++k, ++sg) { 2145 for (k = 0; k < rsv_schp->k_use_sg; ++k, sg = sg_next(sg)) {
2146 num = sg->length; 2146 num = sg->length;
2147 if (rem <= num) { 2147 if (rem <= num) {
2148 sfp->save_scat_len = num; 2148 sfp->save_scat_len = num;
diff --git a/drivers/scsi/stex.c b/drivers/scsi/stex.c
index 72f6d8015358..e3fab3a6aed7 100644
--- a/drivers/scsi/stex.c
+++ b/drivers/scsi/stex.c
@@ -1123,6 +1123,7 @@ static struct scsi_host_template driver_template = {
1123 .this_id = -1, 1123 .this_id = -1,
1124 .sg_tablesize = ST_MAX_SG, 1124 .sg_tablesize = ST_MAX_SG,
1125 .cmd_per_lun = ST_CMD_PER_LUN, 1125 .cmd_per_lun = ST_CMD_PER_LUN,
1126 .use_sg_chaining = ENABLE_SG_CHAINING,
1126}; 1127};
1127 1128
1128static int stex_set_dma_mask(struct pci_dev * pdev) 1129static int stex_set_dma_mask(struct pci_dev * pdev)
diff --git a/drivers/scsi/sym53c416.c b/drivers/scsi/sym53c416.c
index 92bfaeafe30d..8befab7e9839 100644
--- a/drivers/scsi/sym53c416.c
+++ b/drivers/scsi/sym53c416.c
@@ -854,5 +854,6 @@ static struct scsi_host_template driver_template = {
854 .cmd_per_lun = 1, 854 .cmd_per_lun = 1,
855 .unchecked_isa_dma = 1, 855 .unchecked_isa_dma = 1,
856 .use_clustering = ENABLE_CLUSTERING, 856 .use_clustering = ENABLE_CLUSTERING,
857 .use_sg_chaining = ENABLE_SG_CHAINING,
857}; 858};
858#include "scsi_module.c" 859#include "scsi_module.c"
diff --git a/drivers/scsi/sym53c8xx_2/sym_glue.c b/drivers/scsi/sym53c8xx_2/sym_glue.c
index 3db22325ea2c..db03c4c8ec1e 100644
--- a/drivers/scsi/sym53c8xx_2/sym_glue.c
+++ b/drivers/scsi/sym53c8xx_2/sym_glue.c
@@ -1808,6 +1808,7 @@ static struct scsi_host_template sym2_template = {
1808 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler, 1808 .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
1809 .this_id = 7, 1809 .this_id = 7,
1810 .use_clustering = ENABLE_CLUSTERING, 1810 .use_clustering = ENABLE_CLUSTERING,
1811 .use_sg_chaining = ENABLE_SG_CHAINING,
1811 .max_sectors = 0xFFFF, 1812 .max_sectors = 0xFFFF,
1812#ifdef SYM_LINUX_PROC_INFO_SUPPORT 1813#ifdef SYM_LINUX_PROC_INFO_SUPPORT
1813 .proc_info = sym53c8xx_proc_info, 1814 .proc_info = sym53c8xx_proc_info,
diff --git a/drivers/scsi/u14-34f.c b/drivers/scsi/u14-34f.c
index fc9f51818e8f..7edd6ceb13b2 100644
--- a/drivers/scsi/u14-34f.c
+++ b/drivers/scsi/u14-34f.c
@@ -450,7 +450,8 @@ static struct scsi_host_template driver_template = {
450 .slave_configure = u14_34f_slave_configure, 450 .slave_configure = u14_34f_slave_configure,
451 .this_id = 7, 451 .this_id = 7,
452 .unchecked_isa_dma = 1, 452 .unchecked_isa_dma = 1,
453 .use_clustering = ENABLE_CLUSTERING 453 .use_clustering = ENABLE_CLUSTERING,
454 .use_sg_chaining = ENABLE_SG_CHAINING,
454 }; 455 };
455 456
456#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD) 457#if !defined(__BIG_ENDIAN_BITFIELD) && !defined(__LITTLE_ENDIAN_BITFIELD)
diff --git a/drivers/scsi/ultrastor.c b/drivers/scsi/ultrastor.c
index c08235d5afc9..ea72bbeb8f9d 100644
--- a/drivers/scsi/ultrastor.c
+++ b/drivers/scsi/ultrastor.c
@@ -1197,5 +1197,6 @@ static struct scsi_host_template driver_template = {
1197 .cmd_per_lun = ULTRASTOR_MAX_CMDS_PER_LUN, 1197 .cmd_per_lun = ULTRASTOR_MAX_CMDS_PER_LUN,
1198 .unchecked_isa_dma = 1, 1198 .unchecked_isa_dma = 1,
1199 .use_clustering = ENABLE_CLUSTERING, 1199 .use_clustering = ENABLE_CLUSTERING,
1200 .use_sg_chaining = ENABLE_SG_CHAINING,
1200}; 1201};
1201#include "scsi_module.c" 1202#include "scsi_module.c"
diff --git a/drivers/scsi/wd7000.c b/drivers/scsi/wd7000.c
index d6fd4259c56b..255c611e78b8 100644
--- a/drivers/scsi/wd7000.c
+++ b/drivers/scsi/wd7000.c
@@ -1671,6 +1671,7 @@ static struct scsi_host_template driver_template = {
1671 .cmd_per_lun = 1, 1671 .cmd_per_lun = 1,
1672 .unchecked_isa_dma = 1, 1672 .unchecked_isa_dma = 1,
1673 .use_clustering = ENABLE_CLUSTERING, 1673 .use_clustering = ENABLE_CLUSTERING,
1674 .use_sg_chaining = ENABLE_SG_CHAINING,
1674}; 1675};
1675 1676
1676#include "scsi_module.c" 1677#include "scsi_module.c"
diff --git a/drivers/usb/storage/alauda.c b/drivers/usb/storage/alauda.c
index 4d3cbb12b713..8d3711a7ff06 100644
--- a/drivers/usb/storage/alauda.c
+++ b/drivers/usb/storage/alauda.c
@@ -798,12 +798,13 @@ static int alauda_read_data(struct us_data *us, unsigned long address,
798{ 798{
799 unsigned char *buffer; 799 unsigned char *buffer;
800 u16 lba, max_lba; 800 u16 lba, max_lba;
801 unsigned int page, len, index, offset; 801 unsigned int page, len, offset;
802 unsigned int blockshift = MEDIA_INFO(us).blockshift; 802 unsigned int blockshift = MEDIA_INFO(us).blockshift;
803 unsigned int pageshift = MEDIA_INFO(us).pageshift; 803 unsigned int pageshift = MEDIA_INFO(us).pageshift;
804 unsigned int blocksize = MEDIA_INFO(us).blocksize; 804 unsigned int blocksize = MEDIA_INFO(us).blocksize;
805 unsigned int pagesize = MEDIA_INFO(us).pagesize; 805 unsigned int pagesize = MEDIA_INFO(us).pagesize;
806 unsigned int uzonesize = MEDIA_INFO(us).uzonesize; 806 unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
807 struct scatterlist *sg;
807 int result; 808 int result;
808 809
809 /* 810 /*
@@ -827,7 +828,8 @@ static int alauda_read_data(struct us_data *us, unsigned long address,
827 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift); 828 max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
828 829
829 result = USB_STOR_TRANSPORT_GOOD; 830 result = USB_STOR_TRANSPORT_GOOD;
830 index = offset = 0; 831 offset = 0;
832 sg = NULL;
831 833
832 while (sectors > 0) { 834 while (sectors > 0) {
833 unsigned int zone = lba / uzonesize; /* integer division */ 835 unsigned int zone = lba / uzonesize; /* integer division */
@@ -873,7 +875,7 @@ static int alauda_read_data(struct us_data *us, unsigned long address,
873 875
874 /* Store the data in the transfer buffer */ 876 /* Store the data in the transfer buffer */
875 usb_stor_access_xfer_buf(buffer, len, us->srb, 877 usb_stor_access_xfer_buf(buffer, len, us->srb,
876 &index, &offset, TO_XFER_BUF); 878 &sg, &offset, TO_XFER_BUF);
877 879
878 page = 0; 880 page = 0;
879 lba++; 881 lba++;
@@ -891,11 +893,12 @@ static int alauda_write_data(struct us_data *us, unsigned long address,
891 unsigned int sectors) 893 unsigned int sectors)
892{ 894{
893 unsigned char *buffer, *blockbuffer; 895 unsigned char *buffer, *blockbuffer;
894 unsigned int page, len, index, offset; 896 unsigned int page, len, offset;
895 unsigned int blockshift = MEDIA_INFO(us).blockshift; 897 unsigned int blockshift = MEDIA_INFO(us).blockshift;
896 unsigned int pageshift = MEDIA_INFO(us).pageshift; 898 unsigned int pageshift = MEDIA_INFO(us).pageshift;
897 unsigned int blocksize = MEDIA_INFO(us).blocksize; 899 unsigned int blocksize = MEDIA_INFO(us).blocksize;
898 unsigned int pagesize = MEDIA_INFO(us).pagesize; 900 unsigned int pagesize = MEDIA_INFO(us).pagesize;
901 struct scatterlist *sg;
899 u16 lba, max_lba; 902 u16 lba, max_lba;
900 int result; 903 int result;
901 904
@@ -929,7 +932,8 @@ static int alauda_write_data(struct us_data *us, unsigned long address,
929 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift); 932 max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
930 933
931 result = USB_STOR_TRANSPORT_GOOD; 934 result = USB_STOR_TRANSPORT_GOOD;
932 index = offset = 0; 935 offset = 0;
936 sg = NULL;
933 937
934 while (sectors > 0) { 938 while (sectors > 0) {
935 /* Write as many sectors as possible in this block */ 939 /* Write as many sectors as possible in this block */
@@ -946,7 +950,7 @@ static int alauda_write_data(struct us_data *us, unsigned long address,
946 950
947 /* Get the data from the transfer buffer */ 951 /* Get the data from the transfer buffer */
948 usb_stor_access_xfer_buf(buffer, len, us->srb, 952 usb_stor_access_xfer_buf(buffer, len, us->srb,
949 &index, &offset, FROM_XFER_BUF); 953 &sg, &offset, FROM_XFER_BUF);
950 954
951 result = alauda_write_lba(us, lba, page, pages, buffer, 955 result = alauda_write_lba(us, lba, page, pages, buffer,
952 blockbuffer); 956 blockbuffer);
diff --git a/drivers/usb/storage/datafab.c b/drivers/usb/storage/datafab.c
index c87ad1bae1d6..579e9f52053a 100644
--- a/drivers/usb/storage/datafab.c
+++ b/drivers/usb/storage/datafab.c
@@ -98,7 +98,8 @@ static int datafab_read_data(struct us_data *us,
98 unsigned char thistime; 98 unsigned char thistime;
99 unsigned int totallen, alloclen; 99 unsigned int totallen, alloclen;
100 int len, result; 100 int len, result;
101 unsigned int sg_idx = 0, sg_offset = 0; 101 unsigned int sg_offset = 0;
102 struct scatterlist *sg = NULL;
102 103
103 // we're working in LBA mode. according to the ATA spec, 104 // we're working in LBA mode. according to the ATA spec,
104 // we can support up to 28-bit addressing. I don't know if Datafab 105 // we can support up to 28-bit addressing. I don't know if Datafab
@@ -155,7 +156,7 @@ static int datafab_read_data(struct us_data *us,
155 156
156 // Store the data in the transfer buffer 157 // Store the data in the transfer buffer
157 usb_stor_access_xfer_buf(buffer, len, us->srb, 158 usb_stor_access_xfer_buf(buffer, len, us->srb,
158 &sg_idx, &sg_offset, TO_XFER_BUF); 159 &sg, &sg_offset, TO_XFER_BUF);
159 160
160 sector += thistime; 161 sector += thistime;
161 totallen -= len; 162 totallen -= len;
@@ -181,7 +182,8 @@ static int datafab_write_data(struct us_data *us,
181 unsigned char thistime; 182 unsigned char thistime;
182 unsigned int totallen, alloclen; 183 unsigned int totallen, alloclen;
183 int len, result; 184 int len, result;
184 unsigned int sg_idx = 0, sg_offset = 0; 185 unsigned int sg_offset = 0;
186 struct scatterlist *sg = NULL;
185 187
186 // we're working in LBA mode. according to the ATA spec, 188 // we're working in LBA mode. according to the ATA spec,
187 // we can support up to 28-bit addressing. I don't know if Datafab 189 // we can support up to 28-bit addressing. I don't know if Datafab
@@ -217,7 +219,7 @@ static int datafab_write_data(struct us_data *us,
217 219
218 // Get the data from the transfer buffer 220 // Get the data from the transfer buffer
219 usb_stor_access_xfer_buf(buffer, len, us->srb, 221 usb_stor_access_xfer_buf(buffer, len, us->srb,
220 &sg_idx, &sg_offset, FROM_XFER_BUF); 222 &sg, &sg_offset, FROM_XFER_BUF);
221 223
222 command[0] = 0; 224 command[0] = 0;
223 command[1] = thistime; 225 command[1] = thistime;
diff --git a/drivers/usb/storage/jumpshot.c b/drivers/usb/storage/jumpshot.c
index 003fcf545888..61097cbb1585 100644
--- a/drivers/usb/storage/jumpshot.c
+++ b/drivers/usb/storage/jumpshot.c
@@ -119,7 +119,8 @@ static int jumpshot_read_data(struct us_data *us,
119 unsigned char thistime; 119 unsigned char thistime;
120 unsigned int totallen, alloclen; 120 unsigned int totallen, alloclen;
121 int len, result; 121 int len, result;
122 unsigned int sg_idx = 0, sg_offset = 0; 122 unsigned int sg_offset = 0;
123 struct scatterlist *sg = NULL;
123 124
124 // we're working in LBA mode. according to the ATA spec, 125 // we're working in LBA mode. according to the ATA spec,
125 // we can support up to 28-bit addressing. I don't know if Jumpshot 126 // we can support up to 28-bit addressing. I don't know if Jumpshot
@@ -170,7 +171,7 @@ static int jumpshot_read_data(struct us_data *us,
170 171
171 // Store the data in the transfer buffer 172 // Store the data in the transfer buffer
172 usb_stor_access_xfer_buf(buffer, len, us->srb, 173 usb_stor_access_xfer_buf(buffer, len, us->srb,
173 &sg_idx, &sg_offset, TO_XFER_BUF); 174 &sg, &sg_offset, TO_XFER_BUF);
174 175
175 sector += thistime; 176 sector += thistime;
176 totallen -= len; 177 totallen -= len;
@@ -195,7 +196,8 @@ static int jumpshot_write_data(struct us_data *us,
195 unsigned char thistime; 196 unsigned char thistime;
196 unsigned int totallen, alloclen; 197 unsigned int totallen, alloclen;
197 int len, result, waitcount; 198 int len, result, waitcount;
198 unsigned int sg_idx = 0, sg_offset = 0; 199 unsigned int sg_offset = 0;
200 struct scatterlist *sg = NULL;
199 201
200 // we're working in LBA mode. according to the ATA spec, 202 // we're working in LBA mode. according to the ATA spec,
201 // we can support up to 28-bit addressing. I don't know if Jumpshot 203 // we can support up to 28-bit addressing. I don't know if Jumpshot
@@ -225,7 +227,7 @@ static int jumpshot_write_data(struct us_data *us,
225 227
226 // Get the data from the transfer buffer 228 // Get the data from the transfer buffer
227 usb_stor_access_xfer_buf(buffer, len, us->srb, 229 usb_stor_access_xfer_buf(buffer, len, us->srb,
228 &sg_idx, &sg_offset, FROM_XFER_BUF); 230 &sg, &sg_offset, FROM_XFER_BUF);
229 231
230 command[0] = 0; 232 command[0] = 0;
231 command[1] = thistime; 233 command[1] = thistime;
diff --git a/drivers/usb/storage/protocol.c b/drivers/usb/storage/protocol.c
index 9ad30428d2dd..cc8f7c52c729 100644
--- a/drivers/usb/storage/protocol.c
+++ b/drivers/usb/storage/protocol.c
@@ -157,7 +157,7 @@ void usb_stor_transparent_scsi_command(struct scsi_cmnd *srb,
157 * pick up from where this one left off. */ 157 * pick up from where this one left off. */
158 158
159unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 159unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
160 unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index, 160 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
161 unsigned int *offset, enum xfer_buf_dir dir) 161 unsigned int *offset, enum xfer_buf_dir dir)
162{ 162{
163 unsigned int cnt; 163 unsigned int cnt;
@@ -184,16 +184,17 @@ unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
184 * located in high memory -- then kmap() will map it to a temporary 184 * located in high memory -- then kmap() will map it to a temporary
185 * position in the kernel's virtual address space. */ 185 * position in the kernel's virtual address space. */
186 } else { 186 } else {
187 struct scatterlist *sg = 187 struct scatterlist *sg = *sgptr;
188 (struct scatterlist *) srb->request_buffer 188
189 + *index; 189 if (!sg)
190 sg = (struct scatterlist *) srb->request_buffer;
190 191
191 /* This loop handles a single s-g list entry, which may 192 /* This loop handles a single s-g list entry, which may
192 * include multiple pages. Find the initial page structure 193 * include multiple pages. Find the initial page structure
193 * and the starting offset within the page, and update 194 * and the starting offset within the page, and update
194 * the *offset and *index values for the next loop. */ 195 * the *offset and *index values for the next loop. */
195 cnt = 0; 196 cnt = 0;
196 while (cnt < buflen && *index < srb->use_sg) { 197 while (cnt < buflen) {
197 struct page *page = sg->page + 198 struct page *page = sg->page +
198 ((sg->offset + *offset) >> PAGE_SHIFT); 199 ((sg->offset + *offset) >> PAGE_SHIFT);
199 unsigned int poff = 200 unsigned int poff =
@@ -209,8 +210,7 @@ unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
209 210
210 /* Transfer continues to next s-g entry */ 211 /* Transfer continues to next s-g entry */
211 *offset = 0; 212 *offset = 0;
212 ++*index; 213 sg = sg_next(sg);
213 ++sg;
214 } 214 }
215 215
216 /* Transfer the data for all the pages in this 216 /* Transfer the data for all the pages in this
@@ -234,6 +234,7 @@ unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
234 sglen -= plen; 234 sglen -= plen;
235 } 235 }
236 } 236 }
237 *sgptr = sg;
237 } 238 }
238 239
239 /* Return the amount actually transferred */ 240 /* Return the amount actually transferred */
@@ -245,9 +246,10 @@ unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
245void usb_stor_set_xfer_buf(unsigned char *buffer, 246void usb_stor_set_xfer_buf(unsigned char *buffer,
246 unsigned int buflen, struct scsi_cmnd *srb) 247 unsigned int buflen, struct scsi_cmnd *srb)
247{ 248{
248 unsigned int index = 0, offset = 0; 249 unsigned int offset = 0;
250 struct scatterlist *sg = NULL;
249 251
250 usb_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset, 252 usb_stor_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
251 TO_XFER_BUF); 253 TO_XFER_BUF);
252 if (buflen < srb->request_bufflen) 254 if (buflen < srb->request_bufflen)
253 srb->resid = srb->request_bufflen - buflen; 255 srb->resid = srb->request_bufflen - buflen;
diff --git a/drivers/usb/storage/protocol.h b/drivers/usb/storage/protocol.h
index 845bed4b8031..8737a36891ca 100644
--- a/drivers/usb/storage/protocol.h
+++ b/drivers/usb/storage/protocol.h
@@ -52,7 +52,7 @@ extern void usb_stor_transparent_scsi_command(struct scsi_cmnd*,
52enum xfer_buf_dir {TO_XFER_BUF, FROM_XFER_BUF}; 52enum xfer_buf_dir {TO_XFER_BUF, FROM_XFER_BUF};
53 53
54extern unsigned int usb_stor_access_xfer_buf(unsigned char *buffer, 54extern unsigned int usb_stor_access_xfer_buf(unsigned char *buffer,
55 unsigned int buflen, struct scsi_cmnd *srb, unsigned int *index, 55 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **,
56 unsigned int *offset, enum xfer_buf_dir dir); 56 unsigned int *offset, enum xfer_buf_dir dir);
57 57
58extern void usb_stor_set_xfer_buf(unsigned char *buffer, 58extern void usb_stor_set_xfer_buf(unsigned char *buffer,
diff --git a/drivers/usb/storage/sddr09.c b/drivers/usb/storage/sddr09.c
index b2ed2a3e6fca..b12202c5da2d 100644
--- a/drivers/usb/storage/sddr09.c
+++ b/drivers/usb/storage/sddr09.c
@@ -705,7 +705,8 @@ sddr09_read_data(struct us_data *us,
705 unsigned char *buffer; 705 unsigned char *buffer;
706 unsigned int lba, maxlba, pba; 706 unsigned int lba, maxlba, pba;
707 unsigned int page, pages; 707 unsigned int page, pages;
708 unsigned int len, index, offset; 708 unsigned int len, offset;
709 struct scatterlist *sg;
709 int result; 710 int result;
710 711
711 // Figure out the initial LBA and page 712 // Figure out the initial LBA and page
@@ -730,7 +731,8 @@ sddr09_read_data(struct us_data *us,
730 // contiguous LBA's. Another exercise left to the student. 731 // contiguous LBA's. Another exercise left to the student.
731 732
732 result = 0; 733 result = 0;
733 index = offset = 0; 734 offset = 0;
735 sg = NULL;
734 736
735 while (sectors > 0) { 737 while (sectors > 0) {
736 738
@@ -777,7 +779,7 @@ sddr09_read_data(struct us_data *us,
777 779
778 // Store the data in the transfer buffer 780 // Store the data in the transfer buffer
779 usb_stor_access_xfer_buf(buffer, len, us->srb, 781 usb_stor_access_xfer_buf(buffer, len, us->srb,
780 &index, &offset, TO_XFER_BUF); 782 &sg, &offset, TO_XFER_BUF);
781 783
782 page = 0; 784 page = 0;
783 lba++; 785 lba++;
@@ -931,7 +933,8 @@ sddr09_write_data(struct us_data *us,
931 unsigned int pagelen, blocklen; 933 unsigned int pagelen, blocklen;
932 unsigned char *blockbuffer; 934 unsigned char *blockbuffer;
933 unsigned char *buffer; 935 unsigned char *buffer;
934 unsigned int len, index, offset; 936 unsigned int len, offset;
937 struct scatterlist *sg;
935 int result; 938 int result;
936 939
937 // Figure out the initial LBA and page 940 // Figure out the initial LBA and page
@@ -968,7 +971,8 @@ sddr09_write_data(struct us_data *us,
968 } 971 }
969 972
970 result = 0; 973 result = 0;
971 index = offset = 0; 974 offset = 0;
975 sg = NULL;
972 976
973 while (sectors > 0) { 977 while (sectors > 0) {
974 978
@@ -987,7 +991,7 @@ sddr09_write_data(struct us_data *us,
987 991
988 // Get the data from the transfer buffer 992 // Get the data from the transfer buffer
989 usb_stor_access_xfer_buf(buffer, len, us->srb, 993 usb_stor_access_xfer_buf(buffer, len, us->srb,
990 &index, &offset, FROM_XFER_BUF); 994 &sg, &offset, FROM_XFER_BUF);
991 995
992 result = sddr09_write_lba(us, lba, page, pages, 996 result = sddr09_write_lba(us, lba, page, pages,
993 buffer, blockbuffer); 997 buffer, blockbuffer);
diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index 0b1b5b59ca7b..d43a3415e12f 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -167,7 +167,8 @@ static int sddr55_read_data(struct us_data *us,
167 unsigned long address; 167 unsigned long address;
168 168
169 unsigned short pages; 169 unsigned short pages;
170 unsigned int len, index, offset; 170 unsigned int len, offset;
171 struct scatterlist *sg;
171 172
172 // Since we only read in one block at a time, we have to create 173 // Since we only read in one block at a time, we have to create
173 // a bounce buffer and move the data a piece at a time between the 174 // a bounce buffer and move the data a piece at a time between the
@@ -178,7 +179,8 @@ static int sddr55_read_data(struct us_data *us,
178 buffer = kmalloc(len, GFP_NOIO); 179 buffer = kmalloc(len, GFP_NOIO);
179 if (buffer == NULL) 180 if (buffer == NULL)
180 return USB_STOR_TRANSPORT_ERROR; /* out of memory */ 181 return USB_STOR_TRANSPORT_ERROR; /* out of memory */
181 index = offset = 0; 182 offset = 0;
183 sg = NULL;
182 184
183 while (sectors>0) { 185 while (sectors>0) {
184 186
@@ -255,7 +257,7 @@ static int sddr55_read_data(struct us_data *us,
255 257
256 // Store the data in the transfer buffer 258 // Store the data in the transfer buffer
257 usb_stor_access_xfer_buf(buffer, len, us->srb, 259 usb_stor_access_xfer_buf(buffer, len, us->srb,
258 &index, &offset, TO_XFER_BUF); 260 &sg, &offset, TO_XFER_BUF);
259 261
260 page = 0; 262 page = 0;
261 lba++; 263 lba++;
@@ -287,7 +289,8 @@ static int sddr55_write_data(struct us_data *us,
287 289
288 unsigned short pages; 290 unsigned short pages;
289 int i; 291 int i;
290 unsigned int len, index, offset; 292 unsigned int len, offset;
293 struct scatterlist *sg;
291 294
292 /* check if we are allowed to write */ 295 /* check if we are allowed to write */
293 if (info->read_only || info->force_read_only) { 296 if (info->read_only || info->force_read_only) {
@@ -304,7 +307,8 @@ static int sddr55_write_data(struct us_data *us,
304 buffer = kmalloc(len, GFP_NOIO); 307 buffer = kmalloc(len, GFP_NOIO);
305 if (buffer == NULL) 308 if (buffer == NULL)
306 return USB_STOR_TRANSPORT_ERROR; 309 return USB_STOR_TRANSPORT_ERROR;
307 index = offset = 0; 310 offset = 0;
311 sg = NULL;
308 312
309 while (sectors > 0) { 313 while (sectors > 0) {
310 314
@@ -322,7 +326,7 @@ static int sddr55_write_data(struct us_data *us,
322 326
323 // Get the data from the transfer buffer 327 // Get the data from the transfer buffer
324 usb_stor_access_xfer_buf(buffer, len, us->srb, 328 usb_stor_access_xfer_buf(buffer, len, us->srb,
325 &index, &offset, FROM_XFER_BUF); 329 &sg, &offset, FROM_XFER_BUF);
326 330
327 US_DEBUGP("Write %02X pages, to PBA %04X" 331 US_DEBUGP("Write %02X pages, to PBA %04X"
328 " (LBA %04X) page %02X\n", 332 " (LBA %04X) page %02X\n",
diff --git a/drivers/usb/storage/shuttle_usbat.c b/drivers/usb/storage/shuttle_usbat.c
index 17ca4d73577b..cb22a9ad1694 100644
--- a/drivers/usb/storage/shuttle_usbat.c
+++ b/drivers/usb/storage/shuttle_usbat.c
@@ -993,7 +993,8 @@ static int usbat_flash_read_data(struct us_data *us,
993 unsigned char thistime; 993 unsigned char thistime;
994 unsigned int totallen, alloclen; 994 unsigned int totallen, alloclen;
995 int len, result; 995 int len, result;
996 unsigned int sg_idx = 0, sg_offset = 0; 996 unsigned int sg_offset = 0;
997 struct scatterlist *sg = NULL;
997 998
998 result = usbat_flash_check_media(us, info); 999 result = usbat_flash_check_media(us, info);
999 if (result != USB_STOR_TRANSPORT_GOOD) 1000 if (result != USB_STOR_TRANSPORT_GOOD)
@@ -1047,7 +1048,7 @@ static int usbat_flash_read_data(struct us_data *us,
1047 1048
1048 /* Store the data in the transfer buffer */ 1049 /* Store the data in the transfer buffer */
1049 usb_stor_access_xfer_buf(buffer, len, us->srb, 1050 usb_stor_access_xfer_buf(buffer, len, us->srb,
1050 &sg_idx, &sg_offset, TO_XFER_BUF); 1051 &sg, &sg_offset, TO_XFER_BUF);
1051 1052
1052 sector += thistime; 1053 sector += thistime;
1053 totallen -= len; 1054 totallen -= len;
@@ -1083,7 +1084,8 @@ static int usbat_flash_write_data(struct us_data *us,
1083 unsigned char thistime; 1084 unsigned char thistime;
1084 unsigned int totallen, alloclen; 1085 unsigned int totallen, alloclen;
1085 int len, result; 1086 int len, result;
1086 unsigned int sg_idx = 0, sg_offset = 0; 1087 unsigned int sg_offset = 0;
1088 struct scatterlist *sg = NULL;
1087 1089
1088 result = usbat_flash_check_media(us, info); 1090 result = usbat_flash_check_media(us, info);
1089 if (result != USB_STOR_TRANSPORT_GOOD) 1091 if (result != USB_STOR_TRANSPORT_GOOD)
@@ -1122,7 +1124,7 @@ static int usbat_flash_write_data(struct us_data *us,
1122 1124
1123 /* Get the data from the transfer buffer */ 1125 /* Get the data from the transfer buffer */
1124 usb_stor_access_xfer_buf(buffer, len, us->srb, 1126 usb_stor_access_xfer_buf(buffer, len, us->srb,
1125 &sg_idx, &sg_offset, FROM_XFER_BUF); 1127 &sg, &sg_offset, FROM_XFER_BUF);
1126 1128
1127 /* ATA command 0x30 (WRITE SECTORS) */ 1129 /* ATA command 0x30 (WRITE SECTORS) */
1128 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30); 1130 usbat_pack_ata_sector_cmd(command, thistime, sector, 0x30);
@@ -1162,8 +1164,8 @@ static int usbat_hp8200e_handle_read10(struct us_data *us,
1162 unsigned char *buffer; 1164 unsigned char *buffer;
1163 unsigned int len; 1165 unsigned int len;
1164 unsigned int sector; 1166 unsigned int sector;
1165 unsigned int sg_segment = 0;
1166 unsigned int sg_offset = 0; 1167 unsigned int sg_offset = 0;
1168 struct scatterlist *sg = NULL;
1167 1169
1168 US_DEBUGP("handle_read10: transfersize %d\n", 1170 US_DEBUGP("handle_read10: transfersize %d\n",
1169 srb->transfersize); 1171 srb->transfersize);
@@ -1220,9 +1222,6 @@ static int usbat_hp8200e_handle_read10(struct us_data *us,
1220 sector |= short_pack(data[7+5], data[7+4]); 1222 sector |= short_pack(data[7+5], data[7+4]);
1221 transferred = 0; 1223 transferred = 0;
1222 1224
1223 sg_segment = 0; /* for keeping track of where we are in */
1224 sg_offset = 0; /* the scatter/gather list */
1225
1226 while (transferred != srb->request_bufflen) { 1225 while (transferred != srb->request_bufflen) {
1227 1226
1228 if (len > srb->request_bufflen - transferred) 1227 if (len > srb->request_bufflen - transferred)
@@ -1255,7 +1254,7 @@ static int usbat_hp8200e_handle_read10(struct us_data *us,
1255 1254
1256 /* Store the data in the transfer buffer */ 1255 /* Store the data in the transfer buffer */
1257 usb_stor_access_xfer_buf(buffer, len, srb, 1256 usb_stor_access_xfer_buf(buffer, len, srb,
1258 &sg_segment, &sg_offset, TO_XFER_BUF); 1257 &sg, &sg_offset, TO_XFER_BUF);
1259 1258
1260 /* Update the amount transferred and the sector number */ 1259 /* Update the amount transferred and the sector number */
1261 1260
diff --git a/include/asm-ia64/dma-mapping.h b/include/asm-ia64/dma-mapping.h
index 3ca6d5c14b2e..f1735a22d0ea 100644
--- a/include/asm-ia64/dma-mapping.h
+++ b/include/asm-ia64/dma-mapping.h
@@ -6,7 +6,7 @@
6 * David Mosberger-Tang <davidm@hpl.hp.com> 6 * David Mosberger-Tang <davidm@hpl.hp.com>
7 */ 7 */
8#include <asm/machvec.h> 8#include <asm/machvec.h>
9#include <asm/scatterlist.h> 9#include <linux/scatterlist.h>
10 10
11#define dma_alloc_coherent platform_dma_alloc_coherent 11#define dma_alloc_coherent platform_dma_alloc_coherent
12/* coherent mem. is cheap */ 12/* coherent mem. is cheap */
diff --git a/include/asm-ia64/scatterlist.h b/include/asm-ia64/scatterlist.h
index a452ea24205a..7d5234d50312 100644
--- a/include/asm-ia64/scatterlist.h
+++ b/include/asm-ia64/scatterlist.h
@@ -30,4 +30,6 @@ struct scatterlist {
30#define sg_dma_len(sg) ((sg)->dma_length) 30#define sg_dma_len(sg) ((sg)->dma_length)
31#define sg_dma_address(sg) ((sg)->dma_address) 31#define sg_dma_address(sg) ((sg)->dma_address)
32 32
33#define ARCH_HAS_SG_CHAIN
34
33#endif /* _ASM_IA64_SCATTERLIST_H */ 35#endif /* _ASM_IA64_SCATTERLIST_H */
diff --git a/include/asm-powerpc/dma-mapping.h b/include/asm-powerpc/dma-mapping.h
index d05891608f74..2af321f36aba 100644
--- a/include/asm-powerpc/dma-mapping.h
+++ b/include/asm-powerpc/dma-mapping.h
@@ -6,149 +6,6 @@
6 */ 6 */
7#ifndef _ASM_DMA_MAPPING_H 7#ifndef _ASM_DMA_MAPPING_H
8#define _ASM_DMA_MAPPING_H 8#define _ASM_DMA_MAPPING_H
9#ifdef __KERNEL__
10
11#include <linux/types.h>
12#include <linux/cache.h>
13/* need struct page definitions */
14#include <linux/mm.h>
15#include <asm/scatterlist.h>
16#include <asm/io.h>
17
18#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
19
20#ifdef CONFIG_NOT_COHERENT_CACHE
21/*
22 * DMA-consistent mapping functions for PowerPCs that don't support
23 * cache snooping. These allocate/free a region of uncached mapped
24 * memory space for use with DMA devices. Alternatively, you could
25 * allocate the space "normally" and use the cache management functions
26 * to ensure it is consistent.
27 */
28extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp);
29extern void __dma_free_coherent(size_t size, void *vaddr);
30extern void __dma_sync(void *vaddr, size_t size, int direction);
31extern void __dma_sync_page(struct page *page, unsigned long offset,
32 size_t size, int direction);
33
34#else /* ! CONFIG_NOT_COHERENT_CACHE */
35/*
36 * Cache coherent cores.
37 */
38
39#define __dma_alloc_coherent(gfp, size, handle) NULL
40#define __dma_free_coherent(size, addr) ((void)0)
41#define __dma_sync(addr, size, rw) ((void)0)
42#define __dma_sync_page(pg, off, sz, rw) ((void)0)
43
44#endif /* ! CONFIG_NOT_COHERENT_CACHE */
45
46#ifdef CONFIG_PPC64
47/*
48 * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
49 */
50struct dma_mapping_ops {
51 void * (*alloc_coherent)(struct device *dev, size_t size,
52 dma_addr_t *dma_handle, gfp_t flag);
53 void (*free_coherent)(struct device *dev, size_t size,
54 void *vaddr, dma_addr_t dma_handle);
55 dma_addr_t (*map_single)(struct device *dev, void *ptr,
56 size_t size, enum dma_data_direction direction);
57 void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
58 size_t size, enum dma_data_direction direction);
59 int (*map_sg)(struct device *dev, struct scatterlist *sg,
60 int nents, enum dma_data_direction direction);
61 void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
62 int nents, enum dma_data_direction direction);
63 int (*dma_supported)(struct device *dev, u64 mask);
64 int (*set_dma_mask)(struct device *dev, u64 dma_mask);
65};
66
67static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
68{
69 /* We don't handle the NULL dev case for ISA for now. We could
70 * do it via an out of line call but it is not needed for now. The
71 * only ISA DMA device we support is the floppy and we have a hack
72 * in the floppy driver directly to get a device for us.
73 */
74 if (unlikely(dev == NULL || dev->archdata.dma_ops == NULL))
75 return NULL;
76 return dev->archdata.dma_ops;
77}
78
79static inline int dma_supported(struct device *dev, u64 mask)
80{
81 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
82
83 if (unlikely(dma_ops == NULL))
84 return 0;
85 if (dma_ops->dma_supported == NULL)
86 return 1;
87 return dma_ops->dma_supported(dev, mask);
88}
89
90static inline int dma_set_mask(struct device *dev, u64 dma_mask)
91{
92 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
93
94 if (unlikely(dma_ops == NULL))
95 return -EIO;
96 if (dma_ops->set_dma_mask != NULL)
97 return dma_ops->set_dma_mask(dev, dma_mask);
98 if (!dev->dma_mask || !dma_supported(dev, dma_mask))
99 return -EIO;
100 *dev->dma_mask = dma_mask;
101 return 0;
102}
103
104static inline void *dma_alloc_coherent(struct device *dev, size_t size,
105 dma_addr_t *dma_handle, gfp_t flag)
106{
107 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
108
109 BUG_ON(!dma_ops);
110 return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
111}
112
113static inline void dma_free_coherent(struct device *dev, size_t size,
114 void *cpu_addr, dma_addr_t dma_handle)
115{
116 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
117
118 BUG_ON(!dma_ops);
119 dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
120}
121
122static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
123 size_t size,
124 enum dma_data_direction direction)
125{
126 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
127
128 BUG_ON(!dma_ops);
129 return dma_ops->map_single(dev, cpu_addr, size, direction);
130}
131
132static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
133 size_t size,
134 enum dma_data_direction direction)
135{
136 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
137
138 BUG_ON(!dma_ops);
139 dma_ops->unmap_single(dev, dma_addr, size, direction);
140}
141
142static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
143 unsigned long offset, size_t size,
144 enum dma_data_direction direction)
145{
146 struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
147
148 BUG_ON(!dma_ops);
149 return dma_ops->map_single(dev, page_address(page) + offset, size,
150 direction);
151}
152 9
153static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address, 10static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
154 size_t size, 11 size_t size,
@@ -276,14 +133,15 @@ static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
276} 133}
277 134
278static inline int 135static inline int
279dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 136dma_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
280 enum dma_data_direction direction) 137 enum dma_data_direction direction)
281{ 138{
139 struct scatterlist *sg;
282 int i; 140 int i;
283 141
284 BUG_ON(direction == DMA_NONE); 142 BUG_ON(direction == DMA_NONE);
285 143
286 for (i = 0; i < nents; i++, sg++) { 144 for_each_sg(sgl, sg, nents, i) {
287 BUG_ON(!sg->page); 145 BUG_ON(!sg->page);
288 __dma_sync_page(sg->page, sg->offset, sg->length, direction); 146 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
289 sg->dma_address = page_to_bus(sg->page) + sg->offset; 147 sg->dma_address = page_to_bus(sg->page) + sg->offset;
@@ -318,26 +176,28 @@ static inline void dma_sync_single_for_device(struct device *dev,
318} 176}
319 177
320static inline void dma_sync_sg_for_cpu(struct device *dev, 178static inline void dma_sync_sg_for_cpu(struct device *dev,
321 struct scatterlist *sg, int nents, 179 struct scatterlist *sgl, int nents,
322 enum dma_data_direction direction) 180 enum dma_data_direction direction)
323{ 181{
182 struct scatterlist *sg;
324 int i; 183 int i;
325 184
326 BUG_ON(direction == DMA_NONE); 185 BUG_ON(direction == DMA_NONE);
327 186
328 for (i = 0; i < nents; i++, sg++) 187 for_each_sg(sgl, sg, nents, i)
329 __dma_sync_page(sg->page, sg->offset, sg->length, direction); 188 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
330} 189}
331 190
332static inline void dma_sync_sg_for_device(struct device *dev, 191static inline void dma_sync_sg_for_device(struct device *dev,
333 struct scatterlist *sg, int nents, 192 struct scatterlist *sgl, int nents,
334 enum dma_data_direction direction) 193 enum dma_data_direction direction)
335{ 194{
195 struct scatterlist *sg;
336 int i; 196 int i;
337 197
338 BUG_ON(direction == DMA_NONE); 198 BUG_ON(direction == DMA_NONE);
339 199
340 for (i = 0; i < nents; i++, sg++) 200 for_each_sg(sgl, sg, nents, i)
341 __dma_sync_page(sg->page, sg->offset, sg->length, direction); 201 __dma_sync_page(sg->page, sg->offset, sg->length, direction);
342} 202}
343 203
diff --git a/include/asm-powerpc/scatterlist.h b/include/asm-powerpc/scatterlist.h
index 8c992d1491d4..b075f619c3b7 100644
--- a/include/asm-powerpc/scatterlist.h
+++ b/include/asm-powerpc/scatterlist.h
@@ -41,5 +41,7 @@ struct scatterlist {
41#define ISA_DMA_THRESHOLD (~0UL) 41#define ISA_DMA_THRESHOLD (~0UL)
42#endif 42#endif
43 43
44#define ARCH_HAS_SG_CHAIN
45
44#endif /* __KERNEL__ */ 46#endif /* __KERNEL__ */
45#endif /* _ASM_POWERPC_SCATTERLIST_H */ 47#endif /* _ASM_POWERPC_SCATTERLIST_H */
diff --git a/include/asm-sparc/scatterlist.h b/include/asm-sparc/scatterlist.h
index a4fcf9ac9649..4055af90ad7e 100644
--- a/include/asm-sparc/scatterlist.h
+++ b/include/asm-sparc/scatterlist.h
@@ -19,4 +19,6 @@ struct scatterlist {
19 19
20#define ISA_DMA_THRESHOLD (~0UL) 20#define ISA_DMA_THRESHOLD (~0UL)
21 21
22#define ARCH_HAS_SG_CHAIN
23
22#endif /* !(_SPARC_SCATTERLIST_H) */ 24#endif /* !(_SPARC_SCATTERLIST_H) */
diff --git a/include/asm-sparc64/scatterlist.h b/include/asm-sparc64/scatterlist.h
index 048fdb40e81d..703c5bbe6c8c 100644
--- a/include/asm-sparc64/scatterlist.h
+++ b/include/asm-sparc64/scatterlist.h
@@ -20,4 +20,6 @@ struct scatterlist {
20 20
21#define ISA_DMA_THRESHOLD (~0UL) 21#define ISA_DMA_THRESHOLD (~0UL)
22 22
23#define ARCH_HAS_SG_CHAIN
24
23#endif /* !(_SPARC64_SCATTERLIST_H) */ 25#endif /* !(_SPARC64_SCATTERLIST_H) */
diff --git a/include/asm-x86/dma-mapping_32.h b/include/asm-x86/dma-mapping_32.h
index f1d72d177f68..6a2d26cb5da6 100644
--- a/include/asm-x86/dma-mapping_32.h
+++ b/include/asm-x86/dma-mapping_32.h
@@ -2,10 +2,10 @@
2#define _ASM_I386_DMA_MAPPING_H 2#define _ASM_I386_DMA_MAPPING_H
3 3
4#include <linux/mm.h> 4#include <linux/mm.h>
5#include <linux/scatterlist.h>
5 6
6#include <asm/cache.h> 7#include <asm/cache.h>
7#include <asm/io.h> 8#include <asm/io.h>
8#include <asm/scatterlist.h>
9#include <asm/bug.h> 9#include <asm/bug.h>
10 10
11#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f) 11#define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
@@ -35,18 +35,19 @@ dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
35} 35}
36 36
37static inline int 37static inline int
38dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 38dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
39 enum dma_data_direction direction) 39 enum dma_data_direction direction)
40{ 40{
41 struct scatterlist *sg;
41 int i; 42 int i;
42 43
43 BUG_ON(!valid_dma_direction(direction)); 44 BUG_ON(!valid_dma_direction(direction));
44 WARN_ON(nents == 0 || sg[0].length == 0); 45 WARN_ON(nents == 0 || sglist[0].length == 0);
45 46
46 for (i = 0; i < nents; i++ ) { 47 for_each_sg(sglist, sg, nents, i) {
47 BUG_ON(!sg[i].page); 48 BUG_ON(!sg->page);
48 49
49 sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset; 50 sg->dma_address = page_to_phys(sg->page) + sg->offset;
50 } 51 }
51 52
52 flush_write_buffers(); 53 flush_write_buffers();
diff --git a/include/asm-x86/dma-mapping_64.h b/include/asm-x86/dma-mapping_64.h
index 6897e2a436e5..ecd0f6125ba3 100644
--- a/include/asm-x86/dma-mapping_64.h
+++ b/include/asm-x86/dma-mapping_64.h
@@ -6,8 +6,7 @@
6 * documentation. 6 * documentation.
7 */ 7 */
8 8
9 9#include <linux/scatterlist.h>
10#include <asm/scatterlist.h>
11#include <asm/io.h> 10#include <asm/io.h>
12#include <asm/swiotlb.h> 11#include <asm/swiotlb.h>
13 12
diff --git a/include/asm-x86/scatterlist_32.h b/include/asm-x86/scatterlist_32.h
index d7e45a8f1aae..bd5164aa8f63 100644
--- a/include/asm-x86/scatterlist_32.h
+++ b/include/asm-x86/scatterlist_32.h
@@ -10,6 +10,8 @@ struct scatterlist {
10 unsigned int length; 10 unsigned int length;
11}; 11};
12 12
13#define ARCH_HAS_SG_CHAIN
14
13/* These macros should be used after a pci_map_sg call has been done 15/* These macros should be used after a pci_map_sg call has been done
14 * to get bus addresses of each of the SG entries and their lengths. 16 * to get bus addresses of each of the SG entries and their lengths.
15 * You should only work with the number of sg entries pci_map_sg 17 * You should only work with the number of sg entries pci_map_sg
diff --git a/include/asm-x86/scatterlist_64.h b/include/asm-x86/scatterlist_64.h
index eaf7ada27e14..ef3986ba4b79 100644
--- a/include/asm-x86/scatterlist_64.h
+++ b/include/asm-x86/scatterlist_64.h
@@ -11,6 +11,8 @@ struct scatterlist {
11 unsigned int dma_length; 11 unsigned int dma_length;
12}; 12};
13 13
14#define ARCH_HAS_SG_CHAIN
15
14#define ISA_DMA_THRESHOLD (0x00ffffff) 16#define ISA_DMA_THRESHOLD (0x00ffffff)
15 17
16/* These macros should be used after a pci_map_sg call has been done 18/* These macros should be used after a pci_map_sg call has been done
diff --git a/include/linux/i2o.h b/include/linux/i2o.h
index 9752307d16ba..7da5b98d90e6 100644
--- a/include/linux/i2o.h
+++ b/include/linux/i2o.h
@@ -32,6 +32,7 @@
32#include <linux/workqueue.h> /* work_struct */ 32#include <linux/workqueue.h> /* work_struct */
33#include <linux/mempool.h> 33#include <linux/mempool.h>
34#include <linux/mutex.h> 34#include <linux/mutex.h>
35#include <linux/scatterlist.h>
35 36
36#include <asm/io.h> 37#include <asm/io.h>
37#include <asm/semaphore.h> /* Needed for MUTEX init macros */ 38#include <asm/semaphore.h> /* Needed for MUTEX init macros */
@@ -837,7 +838,7 @@ static inline int i2o_dma_map_sg(struct i2o_controller *c,
837 if ((sizeof(dma_addr_t) > 4) && c->pae_support) 838 if ((sizeof(dma_addr_t) > 4) && c->pae_support)
838 *mptr++ = cpu_to_le32(i2o_dma_high(sg_dma_address(sg))); 839 *mptr++ = cpu_to_le32(i2o_dma_high(sg_dma_address(sg)));
839#endif 840#endif
840 sg++; 841 sg = sg_next(sg);
841 } 842 }
842 *sg_ptr = mptr; 843 *sg_ptr = mptr;
843 844
diff --git a/include/linux/ide.h b/include/linux/ide.h
index 234fa3df24f6..30a1931466a6 100644
--- a/include/linux/ide.h
+++ b/include/linux/ide.h
@@ -772,7 +772,7 @@ typedef struct hwif_s {
772 772
773 unsigned int nsect; 773 unsigned int nsect;
774 unsigned int nleft; 774 unsigned int nleft;
775 unsigned int cursg; 775 struct scatterlist *cursg;
776 unsigned int cursg_ofs; 776 unsigned int cursg_ofs;
777 777
778 int rqsize; /* max sectors per request */ 778 int rqsize; /* max sectors per request */
diff --git a/include/linux/libata.h b/include/linux/libata.h
index 229a9ff9f924..377e6d4d9be3 100644
--- a/include/linux/libata.h
+++ b/include/linux/libata.h
@@ -29,7 +29,7 @@
29#include <linux/delay.h> 29#include <linux/delay.h>
30#include <linux/interrupt.h> 30#include <linux/interrupt.h>
31#include <linux/dma-mapping.h> 31#include <linux/dma-mapping.h>
32#include <asm/scatterlist.h> 32#include <linux/scatterlist.h>
33#include <linux/io.h> 33#include <linux/io.h>
34#include <linux/ata.h> 34#include <linux/ata.h>
35#include <linux/workqueue.h> 35#include <linux/workqueue.h>
@@ -416,6 +416,7 @@ struct ata_queued_cmd {
416 unsigned long flags; /* ATA_QCFLAG_xxx */ 416 unsigned long flags; /* ATA_QCFLAG_xxx */
417 unsigned int tag; 417 unsigned int tag;
418 unsigned int n_elem; 418 unsigned int n_elem;
419 unsigned int n_iter;
419 unsigned int orig_n_elem; 420 unsigned int orig_n_elem;
420 421
421 int dma_dir; 422 int dma_dir;
@@ -426,7 +427,7 @@ struct ata_queued_cmd {
426 unsigned int nbytes; 427 unsigned int nbytes;
427 unsigned int curbytes; 428 unsigned int curbytes;
428 429
429 unsigned int cursg; 430 struct scatterlist *cursg;
430 unsigned int cursg_ofs; 431 unsigned int cursg_ofs;
431 432
432 struct scatterlist sgent; 433 struct scatterlist sgent;
@@ -1043,7 +1044,7 @@ ata_sg_is_last(struct scatterlist *sg, struct ata_queued_cmd *qc)
1043 return 1; 1044 return 1;
1044 if (qc->pad_len) 1045 if (qc->pad_len)
1045 return 0; 1046 return 0;
1046 if (((sg - qc->__sg) + 1) == qc->n_elem) 1047 if (qc->n_iter == qc->n_elem)
1047 return 1; 1048 return 1;
1048 return 0; 1049 return 0;
1049} 1050}
@@ -1051,6 +1052,7 @@ ata_sg_is_last(struct scatterlist *sg, struct ata_queued_cmd *qc)
1051static inline struct scatterlist * 1052static inline struct scatterlist *
1052ata_qc_first_sg(struct ata_queued_cmd *qc) 1053ata_qc_first_sg(struct ata_queued_cmd *qc)
1053{ 1054{
1055 qc->n_iter = 0;
1054 if (qc->n_elem) 1056 if (qc->n_elem)
1055 return qc->__sg; 1057 return qc->__sg;
1056 if (qc->pad_len) 1058 if (qc->pad_len)
@@ -1063,8 +1065,8 @@ ata_qc_next_sg(struct scatterlist *sg, struct ata_queued_cmd *qc)
1063{ 1065{
1064 if (sg == &qc->pad_sgent) 1066 if (sg == &qc->pad_sgent)
1065 return NULL; 1067 return NULL;
1066 if (++sg - qc->__sg < qc->n_elem) 1068 if (++qc->n_iter < qc->n_elem)
1067 return sg; 1069 return sg_next(sg);
1068 if (qc->pad_len) 1070 if (qc->pad_len)
1069 return &qc->pad_sgent; 1071 return &qc->pad_sgent;
1070 return NULL; 1072 return NULL;
@@ -1309,9 +1311,11 @@ static inline void ata_qc_reinit(struct ata_queued_cmd *qc)
1309 qc->dma_dir = DMA_NONE; 1311 qc->dma_dir = DMA_NONE;
1310 qc->__sg = NULL; 1312 qc->__sg = NULL;
1311 qc->flags = 0; 1313 qc->flags = 0;
1312 qc->cursg = qc->cursg_ofs = 0; 1314 qc->cursg = NULL;
1315 qc->cursg_ofs = 0;
1313 qc->nbytes = qc->curbytes = 0; 1316 qc->nbytes = qc->curbytes = 0;
1314 qc->n_elem = 0; 1317 qc->n_elem = 0;
1318 qc->n_iter = 0;
1315 qc->err_mask = 0; 1319 qc->err_mask = 0;
1316 qc->pad_len = 0; 1320 qc->pad_len = 0;
1317 qc->sect_size = ATA_SECT_SIZE; 1321 qc->sect_size = ATA_SECT_SIZE;
diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h
index 4efbd9c445f5..2dc7464cce52 100644
--- a/include/linux/scatterlist.h
+++ b/include/linux/scatterlist.h
@@ -20,4 +20,88 @@ static inline void sg_init_one(struct scatterlist *sg, const void *buf,
20 sg_set_buf(sg, buf, buflen); 20 sg_set_buf(sg, buf, buflen);
21} 21}
22 22
23/*
24 * We overload the LSB of the page pointer to indicate whether it's
25 * a valid sg entry, or whether it points to the start of a new scatterlist.
26 * Those low bits are there for everyone! (thanks mason :-)
27 */
28#define sg_is_chain(sg) ((unsigned long) (sg)->page & 0x01)
29#define sg_chain_ptr(sg) \
30 ((struct scatterlist *) ((unsigned long) (sg)->page & ~0x01))
31
32/**
33 * sg_next - return the next scatterlist entry in a list
34 * @sg: The current sg entry
35 *
36 * Usually the next entry will be @sg@ + 1, but if this sg element is part
37 * of a chained scatterlist, it could jump to the start of a new
38 * scatterlist array.
39 *
40 * Note that the caller must ensure that there are further entries after
41 * the current entry, this function will NOT return NULL for an end-of-list.
42 *
43 */
44static inline struct scatterlist *sg_next(struct scatterlist *sg)
45{
46 sg++;
47
48 if (unlikely(sg_is_chain(sg)))
49 sg = sg_chain_ptr(sg);
50
51 return sg;
52}
53
54/*
55 * Loop over each sg element, following the pointer to a new list if necessary
56 */
57#define for_each_sg(sglist, sg, nr, __i) \
58 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
59
60/**
61 * sg_last - return the last scatterlist entry in a list
62 * @sgl: First entry in the scatterlist
63 * @nents: Number of entries in the scatterlist
64 *
65 * Should only be used casually, it (currently) scan the entire list
66 * to get the last entry.
67 *
68 * Note that the @sgl@ pointer passed in need not be the first one,
69 * the important bit is that @nents@ denotes the number of entries that
70 * exist from @sgl@.
71 *
72 */
73static inline struct scatterlist *sg_last(struct scatterlist *sgl,
74 unsigned int nents)
75{
76#ifndef ARCH_HAS_SG_CHAIN
77 struct scatterlist *ret = &sgl[nents - 1];
78#else
79 struct scatterlist *sg, *ret = NULL;
80 int i;
81
82 for_each_sg(sgl, sg, nents, i)
83 ret = sg;
84
85#endif
86 return ret;
87}
88
89/**
90 * sg_chain - Chain two sglists together
91 * @prv: First scatterlist
92 * @prv_nents: Number of entries in prv
93 * @sgl: Second scatterlist
94 *
95 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
96 *
97 */
98static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
99 struct scatterlist *sgl)
100{
101#ifndef ARCH_HAS_SG_CHAIN
102 BUG();
103#endif
104 prv[prv_nents - 1].page = (struct page *) ((unsigned long) sgl | 0x01);
105}
106
23#endif /* _LINUX_SCATTERLIST_H */ 107#endif /* _LINUX_SCATTERLIST_H */
diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h
index 9f8f80ab0c8b..702fcfeb37f1 100644
--- a/include/scsi/scsi.h
+++ b/include/scsi/scsi.h
@@ -11,13 +11,6 @@
11#include <linux/types.h> 11#include <linux/types.h>
12 12
13/* 13/*
14 * The maximum sg list length SCSI can cope with
15 * (currently must be a power of 2 between 32 and 256)
16 */
17#define SCSI_MAX_PHYS_SEGMENTS MAX_PHYS_SEGMENTS
18
19
20/*
21 * SCSI command lengths 14 * SCSI command lengths
22 */ 15 */
23 16
diff --git a/include/scsi/scsi_cmnd.h b/include/scsi/scsi_cmnd.h
index 65ab5145a09b..3f47e522a1ec 100644
--- a/include/scsi/scsi_cmnd.h
+++ b/include/scsi/scsi_cmnd.h
@@ -5,6 +5,7 @@
5#include <linux/list.h> 5#include <linux/list.h>
6#include <linux/types.h> 6#include <linux/types.h>
7#include <linux/timer.h> 7#include <linux/timer.h>
8#include <linux/scatterlist.h>
8 9
9struct request; 10struct request;
10struct scatterlist; 11struct scatterlist;
@@ -68,7 +69,7 @@ struct scsi_cmnd {
68 69
69 /* These elements define the operation we ultimately want to perform */ 70 /* These elements define the operation we ultimately want to perform */
70 unsigned short use_sg; /* Number of pieces of scatter-gather */ 71 unsigned short use_sg; /* Number of pieces of scatter-gather */
71 unsigned short sglist_len; /* size of malloc'd scatter-gather list */ 72 unsigned short __use_sg;
72 73
73 unsigned underflow; /* Return error if less than 74 unsigned underflow; /* Return error if less than
74 this amount is transferred */ 75 this amount is transferred */
@@ -128,7 +129,7 @@ extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
128extern void scsi_kunmap_atomic_sg(void *virt); 129extern void scsi_kunmap_atomic_sg(void *virt);
129 130
130extern struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *, gfp_t); 131extern struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *, gfp_t);
131extern void scsi_free_sgtable(struct scatterlist *, int); 132extern void scsi_free_sgtable(struct scsi_cmnd *);
132 133
133extern int scsi_dma_map(struct scsi_cmnd *cmd); 134extern int scsi_dma_map(struct scsi_cmnd *cmd);
134extern void scsi_dma_unmap(struct scsi_cmnd *cmd); 135extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
@@ -148,6 +149,6 @@ static inline int scsi_get_resid(struct scsi_cmnd *cmd)
148} 149}
149 150
150#define scsi_for_each_sg(cmd, sg, nseg, __i) \ 151#define scsi_for_each_sg(cmd, sg, nseg, __i) \
151 for (__i = 0, sg = scsi_sglist(cmd); __i < (nseg); __i++, (sg)++) 152 for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
152 153
153#endif /* _SCSI_SCSI_CMND_H */ 154#endif /* _SCSI_SCSI_CMND_H */
diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h
index 7d210cd6c38d..0fd4746ee39d 100644
--- a/include/scsi/scsi_host.h
+++ b/include/scsi/scsi_host.h
@@ -39,6 +39,9 @@ struct blk_queue_tags;
39#define DISABLE_CLUSTERING 0 39#define DISABLE_CLUSTERING 0
40#define ENABLE_CLUSTERING 1 40#define ENABLE_CLUSTERING 1
41 41
42#define DISABLE_SG_CHAINING 0
43#define ENABLE_SG_CHAINING 1
44
42enum scsi_eh_timer_return { 45enum scsi_eh_timer_return {
43 EH_NOT_HANDLED, 46 EH_NOT_HANDLED,
44 EH_HANDLED, 47 EH_HANDLED,
@@ -443,6 +446,15 @@ struct scsi_host_template {
443 unsigned ordered_tag:1; 446 unsigned ordered_tag:1;
444 447
445 /* 448 /*
449 * true if the low-level driver can support sg chaining. this
450 * will be removed eventually when all the drivers are
451 * converted to support sg chaining.
452 *
453 * Status: OBSOLETE
454 */
455 unsigned use_sg_chaining:1;
456
457 /*
446 * Countdown for host blocking with no commands outstanding 458 * Countdown for host blocking with no commands outstanding
447 */ 459 */
448 unsigned int max_host_blocked; 460 unsigned int max_host_blocked;
@@ -586,6 +598,7 @@ struct Scsi_Host {
586 unsigned unchecked_isa_dma:1; 598 unsigned unchecked_isa_dma:1;
587 unsigned use_clustering:1; 599 unsigned use_clustering:1;
588 unsigned use_blk_tcq:1; 600 unsigned use_blk_tcq:1;
601 unsigned use_sg_chaining:1;
589 602
590 /* 603 /*
591 * Host has requested that no further requests come through for the 604 * Host has requested that no further requests come through for the
diff --git a/lib/swiotlb.c b/lib/swiotlb.c
index 30c1400e749e..c419ecf334c3 100644
--- a/lib/swiotlb.c
+++ b/lib/swiotlb.c
@@ -677,16 +677,17 @@ swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
677 * same here. 677 * same here.
678 */ 678 */
679int 679int
680swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems, 680swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
681 int dir) 681 int dir)
682{ 682{
683 struct scatterlist *sg;
683 void *addr; 684 void *addr;
684 dma_addr_t dev_addr; 685 dma_addr_t dev_addr;
685 int i; 686 int i;
686 687
687 BUG_ON(dir == DMA_NONE); 688 BUG_ON(dir == DMA_NONE);
688 689
689 for (i = 0; i < nelems; i++, sg++) { 690 for_each_sg(sgl, sg, nelems, i) {
690 addr = SG_ENT_VIRT_ADDRESS(sg); 691 addr = SG_ENT_VIRT_ADDRESS(sg);
691 dev_addr = virt_to_bus(addr); 692 dev_addr = virt_to_bus(addr);
692 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) { 693 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
@@ -696,7 +697,7 @@ swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
696 to do proper error handling. */ 697 to do proper error handling. */
697 swiotlb_full(hwdev, sg->length, dir, 0); 698 swiotlb_full(hwdev, sg->length, dir, 0);
698 swiotlb_unmap_sg(hwdev, sg - i, i, dir); 699 swiotlb_unmap_sg(hwdev, sg - i, i, dir);
699 sg[0].dma_length = 0; 700 sgl[0].dma_length = 0;
700 return 0; 701 return 0;
701 } 702 }
702 sg->dma_address = virt_to_bus(map); 703 sg->dma_address = virt_to_bus(map);
@@ -712,19 +713,21 @@ swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
712 * concerning calls here are the same as for swiotlb_unmap_single() above. 713 * concerning calls here are the same as for swiotlb_unmap_single() above.
713 */ 714 */
714void 715void
715swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems, 716swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
716 int dir) 717 int dir)
717{ 718{
719 struct scatterlist *sg;
718 int i; 720 int i;
719 721
720 BUG_ON(dir == DMA_NONE); 722 BUG_ON(dir == DMA_NONE);
721 723
722 for (i = 0; i < nelems; i++, sg++) 724 for_each_sg(sgl, sg, nelems, i) {
723 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg)) 725 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
724 unmap_single(hwdev, bus_to_virt(sg->dma_address), 726 unmap_single(hwdev, bus_to_virt(sg->dma_address),
725 sg->dma_length, dir); 727 sg->dma_length, dir);
726 else if (dir == DMA_FROM_DEVICE) 728 else if (dir == DMA_FROM_DEVICE)
727 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length); 729 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
730 }
728} 731}
729 732
730/* 733/*
@@ -735,19 +738,21 @@ swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
735 * and usage. 738 * and usage.
736 */ 739 */
737static void 740static void
738swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sg, 741swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
739 int nelems, int dir, int target) 742 int nelems, int dir, int target)
740{ 743{
744 struct scatterlist *sg;
741 int i; 745 int i;
742 746
743 BUG_ON(dir == DMA_NONE); 747 BUG_ON(dir == DMA_NONE);
744 748
745 for (i = 0; i < nelems; i++, sg++) 749 for_each_sg(sgl, sg, nelems, i) {
746 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg)) 750 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
747 sync_single(hwdev, bus_to_virt(sg->dma_address), 751 sync_single(hwdev, bus_to_virt(sg->dma_address),
748 sg->dma_length, dir, target); 752 sg->dma_length, dir, target);
749 else if (dir == DMA_FROM_DEVICE) 753 else if (dir == DMA_FROM_DEVICE)
750 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length); 754 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
755 }
751} 756}
752 757
753void 758void