Commit e3d1bd2c authored by Alan Stern's avatar Alan Stern Committed by Greg Kroah-Hartman

[PATCH] USB storage: Convert datafab to use the new s-g routines

This patch updates the datafab driver to the new scatter-gather handling,
which makes it safe for systems with >1GByte of memory.
It has been tested by Eduard Hasenleithner.
parent 18f49e88
...@@ -51,7 +51,6 @@ ...@@ -51,7 +51,6 @@
*/ */
#include "transport.h" #include "transport.h"
#include "raw_bulk.h"
#include "protocol.h" #include "protocol.h"
#include "usb.h" #include "usb.h"
#include "debug.h" #include "debug.h"
...@@ -91,16 +90,14 @@ static int datafab_read_data(struct us_data *us, ...@@ -91,16 +90,14 @@ static int datafab_read_data(struct us_data *us,
struct datafab_info *info, struct datafab_info *info,
u32 sector, u32 sector,
u32 sectors, u32 sectors,
unsigned char *dest, unsigned char *buffer,
int use_sg) int use_sg)
{ {
unsigned char *command = us->iobuf; unsigned char *command = us->iobuf;
unsigned char *buffer = NULL;
unsigned char *ptr;
unsigned char thistime; unsigned char thistime;
int totallen, len, result; unsigned int totallen, alloclen;
int sg_idx = 0, sg_offset = 0; int len, result;
int rc; unsigned int sg_idx = 0, sg_offset = 0;
// we're working in LBA mode. according to the ATA spec, // we're working in LBA mode. according to the ATA spec,
// we can support up to 28-bit addressing. I don't know if Datafab // we can support up to 28-bit addressing. I don't know if Datafab
...@@ -111,23 +108,28 @@ static int datafab_read_data(struct us_data *us, ...@@ -111,23 +108,28 @@ static int datafab_read_data(struct us_data *us,
return USB_STOR_TRANSPORT_ERROR; return USB_STOR_TRANSPORT_ERROR;
if (info->lun == -1) { if (info->lun == -1) {
rc = datafab_determine_lun(us, info); result = datafab_determine_lun(us, info);
if (rc != USB_STOR_TRANSPORT_GOOD) if (result != USB_STOR_TRANSPORT_GOOD)
return rc; return result;
} }
totallen = sectors * info->ssize; totallen = sectors * info->ssize;
do { // Since we don't read more than 64 KB at a time, we have to create
// loop, never allocate or transfer more than 64k at once // a bounce buffer if the transfer uses scatter-gather.
// (min(128k, 255*info->ssize) is the real limit)
len = min_t(int, totallen, 65536);
ptr = buffer = (use_sg ? kmalloc(len, GFP_NOIO) : dest); alloclen = min(totallen, 65536u);
if (use_sg) {
buffer = kmalloc(alloclen, GFP_NOIO);
if (buffer == NULL) if (buffer == NULL)
return USB_STOR_TRANSPORT_ERROR; return USB_STOR_TRANSPORT_ERROR;
}
do {
// loop, never allocate or transfer more than 64k at once
// (min(128k, 255*info->ssize) is the real limit)
len = min(totallen, alloclen);
thistime = (len / info->ssize) & 0xff; thistime = (len / info->ssize) & 0xff;
command[0] = 0; command[0] = 0;
...@@ -135,7 +137,7 @@ static int datafab_read_data(struct us_data *us, ...@@ -135,7 +137,7 @@ static int datafab_read_data(struct us_data *us,
command[2] = sector & 0xFF; command[2] = sector & 0xFF;
command[3] = (sector >> 8) & 0xFF; command[3] = (sector >> 8) & 0xFF;
command[4] = (sector >> 16) & 0xFF; command[4] = (sector >> 16) & 0xFF;
command[5] = 0xE0 + (info->lun << 4); command[5] = 0xE0 + (info->lun << 4);
command[5] |= (sector >> 24) & 0x0F; command[5] |= (sector >> 24) & 0x0F;
command[6] = 0x20; command[6] = 0x20;
...@@ -147,24 +149,22 @@ static int datafab_read_data(struct us_data *us, ...@@ -147,24 +149,22 @@ static int datafab_read_data(struct us_data *us,
goto leave; goto leave;
// read the result // read the result
result = datafab_bulk_read(us, ptr, len); result = datafab_bulk_read(us, buffer, len);
if (result != USB_STOR_XFER_GOOD) if (result != USB_STOR_XFER_GOOD)
goto leave; goto leave;
sectors -= thistime; if (use_sg)
sector += thistime; usb_stor_access_xfer_buf(buffer, len, us->srb,
&sg_idx, &sg_offset, TO_XFER_BUF);
if (use_sg) { else
us_copy_to_sgbuf(buffer, len, dest, buffer += len;
&sg_idx, &sg_offset, use_sg);
kfree(buffer);
} else {
dest += len;
}
sector += thistime;
totallen -= len; totallen -= len;
} while (totallen > 0); } while (totallen > 0);
if (use_sg)
kfree(buffer);
return USB_STOR_TRANSPORT_GOOD; return USB_STOR_TRANSPORT_GOOD;
leave: leave:
...@@ -178,16 +178,15 @@ static int datafab_write_data(struct us_data *us, ...@@ -178,16 +178,15 @@ static int datafab_write_data(struct us_data *us,
struct datafab_info *info, struct datafab_info *info,
u32 sector, u32 sector,
u32 sectors, u32 sectors,
unsigned char *src, unsigned char *buffer,
int use_sg) int use_sg)
{ {
unsigned char *command = us->iobuf; unsigned char *command = us->iobuf;
unsigned char *reply = us->iobuf; unsigned char *reply = us->iobuf;
unsigned char *buffer = NULL;
unsigned char *ptr;
unsigned char thistime; unsigned char thistime;
int totallen, len, result, rc; unsigned int totallen, alloclen;
int sg_idx = 0, sg_offset = 0; int len, result;
unsigned int sg_idx = 0, sg_offset = 0;
// we're working in LBA mode. according to the ATA spec, // we're working in LBA mode. according to the ATA spec,
// we can support up to 28-bit addressing. I don't know if Datafab // we can support up to 28-bit addressing. I don't know if Datafab
...@@ -198,38 +197,34 @@ static int datafab_write_data(struct us_data *us, ...@@ -198,38 +197,34 @@ static int datafab_write_data(struct us_data *us,
return USB_STOR_TRANSPORT_ERROR; return USB_STOR_TRANSPORT_ERROR;
if (info->lun == -1) { if (info->lun == -1) {
rc = datafab_determine_lun(us, info); result = datafab_determine_lun(us, info);
if (rc != USB_STOR_TRANSPORT_GOOD) if (result != USB_STOR_TRANSPORT_GOOD)
return rc; return result;
} }
// If we're using scatter-gather, we have to create a new
// buffer to read all of the data in first, since a
// scatter-gather buffer could in theory start in the middle
// of a page, which would be bad. A developer who wants a
// challenge might want to write a limited-buffer
// version of this code.
totallen = sectors * info->ssize; totallen = sectors * info->ssize;
do { // Since we don't write more than 64 KB at a time, we have to create
// loop, never allocate or transfer more than 64k at once // a bounce buffer if the transfer uses scatter-gather.
// (min(128k, 255*info->ssize) is the real limit)
len = min_t(int, totallen, 65536);
// if we are using scatter-gather,
// first copy all to one big buffer
buffer = us_copy_from_sgbuf(src, len, &sg_idx, alloclen = min(totallen, 65536u);
&sg_offset, use_sg); if (use_sg) {
buffer = kmalloc(alloclen, GFP_NOIO);
if (buffer == NULL) if (buffer == NULL)
return USB_STOR_TRANSPORT_ERROR; return USB_STOR_TRANSPORT_ERROR;
}
ptr = buffer; do {
// loop, never allocate or transfer more than 64k at once
// (min(128k, 255*info->ssize) is the real limit)
len = min(totallen, alloclen);
thistime = (len / info->ssize) & 0xff; thistime = (len / info->ssize) & 0xff;
if (use_sg)
usb_stor_access_xfer_buf(buffer, len, us->srb,
&sg_idx, &sg_offset, FROM_XFER_BUF);
command[0] = 0; command[0] = 0;
command[1] = thistime; command[1] = thistime;
command[2] = sector & 0xFF; command[2] = sector & 0xFF;
...@@ -247,7 +242,7 @@ static int datafab_write_data(struct us_data *us, ...@@ -247,7 +242,7 @@ static int datafab_write_data(struct us_data *us,
goto leave; goto leave;
// send the data // send the data
result = datafab_bulk_write(us, ptr, len); result = datafab_bulk_write(us, buffer, len);
if (result != USB_STOR_XFER_GOOD) if (result != USB_STOR_XFER_GOOD)
goto leave; goto leave;
...@@ -264,17 +259,15 @@ static int datafab_write_data(struct us_data *us, ...@@ -264,17 +259,15 @@ static int datafab_write_data(struct us_data *us,
goto leave; goto leave;
} }
sectors -= thistime; if (!use_sg)
sector += thistime; buffer += len;
if (use_sg)
kfree(buffer);
else
src += len;
sector += thistime;
totallen -= len; totallen -= len;
} while (totallen > 0); } while (totallen > 0);
if (use_sg)
kfree(buffer);
return USB_STOR_TRANSPORT_GOOD; return USB_STOR_TRANSPORT_GOOD;
leave: leave:
...@@ -435,7 +428,7 @@ static int datafab_handle_mode_sense(struct us_data *us, ...@@ -435,7 +428,7 @@ static int datafab_handle_mode_sense(struct us_data *us,
// datafab reader doesn't present a SCSI interface so we // datafab reader doesn't present a SCSI interface so we
// fudge the SCSI commands... // fudge the SCSI commands...
// //
if (sense_6) if (sense_6)
param_len = srb->cmnd[4]; param_len = srb->cmnd[4];
else else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment