#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_disk.h>
#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>
#include "dmareg.h"
#include "scsivar.h"
#include "samachdep.h"
#ifdef SD_DEBUG
#define DPRINTF(x) printf x;
#else
#define DPRINTF(x)
#endif
struct sdminilabel {
u_short npart;
long offset[MAXPARTITIONS+1];
};
struct sd_softc {
int sc_unit;
int sc_lun;
int sc_part;
int sc_dev_bsize;
struct sdminilabel sc_pinfo;
};
#define NSD 7
#define MAXRETRIES 5
int sdprobe(char target, char lun);
int sdgetinfo(struct sd_softc *ss);
int
sdprobe(char target, char lun)
{
struct scsi_test_unit_ready cdb1;
struct scsipi_inquiry cdb2;
struct scsipi_inquiry_data inq;
int error, retries;
int count;
memset(&cdb1, 0, sizeof(cdb1));
cdb1.opcode = SCSI_TEST_UNIT_READY;
retries = 0;
do {
count = 0;
error = scsiicmd(target, lun, (u_char *)&cdb1, sizeof(cdb1), NULL, &count);
if (error == -SCSI_BUSY) {
register int N = 10000000; while (--N > 0);
}
} while ((error == -SCSI_CHECK || error == -SCSI_BUSY)
&& retries++ < MAXRETRIES);
if (error)
return error<0 ? ENODEV : error;
memset(&cdb2, 0, sizeof(cdb2));
cdb2.opcode = INQUIRY;
cdb2.length = SCSIPI_INQUIRY_LENGTH_SCSI2;
count = SCSIPI_INQUIRY_LENGTH_SCSI2;
error = scsiicmd(target, lun, (u_char *)&cdb2, sizeof(cdb2),
(char *)&inq, &count);
if (error != 0)
return error<0 ? EHER : error;
if ((inq.device & SID_TYPE) != T_DIRECT
&& (inq.device & SID_TYPE) != T_CDROM)
return EUNIT;
DPRINTF(("booting disk %s.\n", inq.vendor));
return 0;
}
int
sdgetinfo(struct sd_softc *ss)
{
struct scsipi_read_capacity_10 cdb;
struct scsipi_read_capacity_10_data cap;
struct sdminilabel *pi = &ss->sc_pinfo;
struct next68k_disklabel *label;
int error, i, blklen;
char io_buf[NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET];
int count;
int sc_blkshift = 0;
memset(&cdb, 0, sizeof(cdb));
cdb.opcode = READ_CAPACITY_10;
count = sizeof(cap);
error = scsiicmd(ss->sc_unit, ss->sc_lun, (u_char *)&cdb, sizeof(cdb),
(char *)&cap, &count);
if (error != 0)
return error<0 ? EHER : error;
blklen = (cap.length[0]<<24) + (cap.length[1]<<16)
+ (cap.length[2]<<8) + cap.length[3];
if (blklen == 0)
blklen = DEV_BSIZE;
ss->sc_dev_bsize = blklen;
ss->sc_pinfo.offset[ss->sc_part] = 0;
error = sdstrategy(ss, F_READ, NEXT68K_LABEL_SECTOR,
NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET, io_buf, (unsigned int *)&i);
if (error != 0) {
DPRINTF(("sdgetinfo: sdstrategy error %d\n", error));
return(ERDLAB);
}
label = (struct next68k_disklabel *)(io_buf+NEXT68K_LABEL_OFFSET);
if (!IS_DISKLABEL(label))
return EUNLAB;
DPRINTF(("Disk is %s (%s, %s).\n",
label->cd_label,label->cd_name, label->cd_type));
while (label->cd_secsize > blklen)
{
blklen <<= 1;
++sc_blkshift;
}
if (label->cd_secsize < blklen)
{
printf("bad label sectorsize (%d) or device blocksize (%d).\n",
label->cd_secsize, blklen>>sc_blkshift);
return ENXIO;
}
pi->npart = 0;
for(i=0; i<MAXPARTITIONS; i++) {
if (label->cd_partitions[i].cp_size > 0) {
pi->offset[pi->npart] = (label->cd_partitions[i].cp_offset
+ label->cd_front) << sc_blkshift;
}
else
pi->offset[pi->npart] = -1;
DPRINTF (("%d: [%d]=%ld\n", i, pi->npart, pi->offset[pi->npart]));
pi->npart++;
if (pi->npart == RAW_PART)
pi->npart++;
}
pi->offset[RAW_PART] = -1;
return 0;
}
int
sdopen(struct open_file *f, ...)
{
va_list ap;
int count, lun, part;
register struct sd_softc *ss;
char unit, cnt;
int error;
va_start(ap, f);
count = va_arg(ap, int);
lun = va_arg(ap, int);
part = va_arg(ap, int);
va_end(ap);
DPRINTF(("open: sd(%d,%d,%d)\n", count, lun, part));
if (lun >= NSD)
return EUNIT;
scsi_init();
for(cnt=0, unit=0; unit < NSD; unit++)
{
DPRINTF(("trying target %d lun %d.\n", unit, lun));
error = sdprobe(unit, lun);
if (error == 0)
{
if (cnt++ == count)
break;
}
else if (error != EUNIT)
return error;
}
if (unit >= NSD)
return EUNIT;
ss = alloc(sizeof(struct sd_softc));
ss->sc_unit = unit;
ss->sc_lun = lun;
ss->sc_part = part;
if ((error = sdgetinfo(ss)) != 0)
return error;
if ((unsigned char)part >= ss->sc_pinfo.npart
|| ss->sc_pinfo.offset[(int)part] == -1)
return EPART;
f->f_devdata = ss;
return 0;
}
int
sdclose(struct open_file *f)
{
register struct sd_softc *ss = f->f_devdata;
dealloc(ss, sizeof(struct sd_softc));
return 0;
}
int
sdstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
void *buf, size_t *rsize)
{
struct sd_softc *ss = devdata;
u_long blk = dblk + ss->sc_pinfo.offset[ss->sc_part];
struct scsipi_rw_10 cdb;
int error;
if (size == 0)
return 0;
if (rw != F_READ)
{
printf("sdstrategy: write not implemented.\n");
return EOPNOTSUPP;
}
*rsize = 0;
while (size > 0) {
u_long nblks;
int tsize;
if (size > MAX_DMASIZE)
tsize = MAX_DMASIZE;
else
tsize = size;
nblks = howmany(tsize, ss->sc_dev_bsize);
DPRINTF(("sdstrategy: read block %ld, %d bytes (%ld blks a %d bytes).\n",
blk, tsize, nblks, ss->sc_dev_bsize));
memset(&cdb, 0, sizeof(cdb));
cdb.opcode = READ_10;
cdb.addr[0] = (blk & 0xff000000) >> 24;
cdb.addr[1] = (blk & 0xff0000) >> 16;
cdb.addr[2] = (blk & 0xff00) >> 8;
cdb.addr[3] = blk & 0xff;
cdb.length[0] = (nblks & 0xff00) >> 8;
cdb.length[1] = nblks & 0xff;
error = scsiicmd(ss->sc_unit, ss->sc_lun,
(u_char *)&cdb, sizeof(cdb), (char *)buf + *rsize, &tsize);
if (error != 0)
{
DPRINTF(("sdstrategy: scsiicmd failed: %d = %s.\n", error, strerror(error)));
return error<0 ? EIO : error;
}
*rsize += tsize;
size -= tsize;
blk += nblks;
}
DPRINTF(("sdstrategy: read %d bytes\n", *rsize));
return 0;
}