int scsionline(SDunit* unit) { int ok; static SDreq *r; static uchar *p; if((r = sdmalloc(r, sizeof(SDreq))) == nil) return 0; if((p = sdmalloc(p, 8)) == nil) return 0; ok = 0; r->unit = unit; r->lun = 0; /* ??? */ for(;;){ /* * Read-capacity is mandatory for DA, WORM, CD-ROM and * MO. It may return 'not ready' if type DA is not * spun up, type MO or type CD-ROM are not loaded or just * plain slow getting their act together after a reset. */ r->write = 0; memset(r->cmd, 0, sizeof(r->cmd)); r->cmd[0] = 0x25; r->cmd[1] = r->lun<<5; r->clen = 10; r->data = p; r->dlen = 8; r->flags = 0; r->status = ~0; // cgascreenputs("F", 1); switch(scsirio(r)){ default: break; case 0: unit->sectors = (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3]; /* * Read-capacity returns the LBA of the last sector, * therefore the number of sectors must be incremented. */ unit->sectors++; unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7]; ok = 1; break; case 2: continue; } break; } return ok; }
static long sdrio(SDreq* r, void* a, long n) { void *data; if(n >= SDmaxio || n < 0) error(Etoobig); data = nil; if(n){ if((data = sdmalloc(n)) == nil) error(Enomem); if(r->write) memmove(data, a, n); } r->data = data; r->dlen = n; if(waserror()){ sdfree(data); r->data = nil; nexterror(); } if(r->unit->dev->ifc->rio(r) != SDok) error(Eio); if(!r->write && r->rlen > 0) memmove(a, data, r->rlen); sdfree(data); r->data = nil; poperror(); return r->rlen; }
int scsiexec(SDunit* unit, int write, uchar* cmd, int clen, void* data, int* dlen) { static SDreq *r; int status; if((r = sdmalloc(r, sizeof(SDreq))) == nil) return SDmalloc; r->unit = unit; r->lun = cmd[1]>>5; /* ??? */ r->write = write; memmove(r->cmd, cmd, clen); r->clen = clen; r->data = data; if(dlen) r->dlen = *dlen; r->flags = 0; r->status = ~0; /* * Call the device-specific I/O routine. * There should be no calls to 'error()' below this * which percolate back up. */ // cgascreenputs("D", 1); switch(status = unit->dev->ifc->rio(r)){ case SDok: if(dlen) *dlen = r->rlen; /*FALLTHROUGH*/ case SDcheck: /*FALLTHROUGH*/ default: /* * It's more complicated than this. There are conditions * which are 'ok' but for which the returned status code * is not 'SDok'. * Also, not all conditions require a reqsense, might * need to do a reqsense here and make it available to the * caller somehow. * * MaƱana. */ break; } return status; }
static void mmcswitchfunc(SDio *io, int arg) { uchar *buf; int n; u32int r[4]; n = Funcbytes; buf = sdmalloc(n); if(waserror()){ print("mmcswitchfunc error\n"); sdfree(buf); nexterror(); } io->iosetup(0, buf, n, 1); io->cmd(SWITCH_FUNC, arg, r); io->io(0, buf, n); sdfree(buf); poperror(); }
int scsiverify(SDunit* unit) { SDreq *r; int i, status; uchar *inquiry; if((r = malloc(sizeof(SDreq))) == nil) return 0; if((inquiry = sdmalloc(sizeof(unit->inquiry))) == nil){ free(r); return 0; } r->unit = unit; r->lun = 0; /* ??? */ memset(unit->inquiry, 0, sizeof(unit->inquiry)); r->write = 0; r->cmd[0] = 0x12; r->cmd[1] = r->lun<<5; r->cmd[4] = sizeof(unit->inquiry)-1; r->clen = 6; r->data = inquiry; r->dlen = sizeof(unit->inquiry)-1; r->flags = 0; r->status = ~0; if(unit->dev->ifc->rio(r) != SDok){ free(r); return 0; } memmove(unit->inquiry, inquiry, r->dlen); free(inquiry); SET(status); for(i = 0; i < 3; i++){ while((status = scsitest(r)) == SDbusy) ; if(status == SDok || status != SDcheck) break; if(!(r->flags & SDvalidsense)) break; if((r->sense[2] & 0x0F) != 0x02) continue; /* * Unit is 'not ready'. * If it is in the process of becoming ready or needs * an initialising command, set status so it will be spun-up * below. * If there's no medium, that's OK too, but don't * try to spin it up. */ if(r->sense[12] == 0x04){ if(r->sense[13] == 0x02 || r->sense[13] == 0x01){ status = SDok; break; } } if(r->sense[12] == 0x3A) break; } if(status == SDok){ /* * Try to ensure a direct-access device is spinning. * Don't wait for completion, ignore the result. */ if((unit->inquiry[0] & 0x1F) == 0){ memset(r->cmd, 0, sizeof(r->cmd)); r->write = 0; r->cmd[0] = 0x1B; r->cmd[1] = (r->lun<<5)|0x01; r->cmd[4] = 1; r->clen = 6; r->data = nil; r->dlen = 0; r->flags = 0; r->status = ~0; unit->dev->ifc->rio(r); } } free(r); if(status == SDok || status == SDcheck) return 1; return 0; }
int scsionline(SDunit* unit) { SDreq *r; uchar *p; int ok, retries; if((r = malloc(sizeof(SDreq))) == nil) return 0; if((p = sdmalloc(8)) == nil){ free(r); return 0; } ok = 0; r->unit = unit; r->lun = 0; /* ??? */ for(retries = 0; retries < 10; retries++){ /* * Read-capacity is mandatory for DA, WORM, CD-ROM and * MO. It may return 'not ready' if type DA is not * spun up, type MO or type CD-ROM are not loaded or just * plain slow getting their act together after a reset. */ r->write = 0; memset(r->cmd, 0, sizeof(r->cmd)); r->cmd[0] = 0x25; r->cmd[1] = r->lun<<5; r->clen = 10; r->data = p; r->dlen = 8; r->flags = 0; r->status = ~0; switch(scsirio(r)){ default: break; case 0: unit->sectors = (p[0]<<24)|(p[1]<<16)|(p[2]<<8)|p[3]; unit->secsize = (p[4]<<24)|(p[5]<<16)|(p[6]<<8)|p[7]; /* * Some ATAPI CD readers lie about the block size. * Since we don't read audio via this interface * it's okay to always fudge this. */ if(unit->secsize == 2352) unit->secsize = 2048; /* * Devices with removable media may return 0 sectors * when they have empty media (e.g. sata dvd writers); * if so, keep the count zero. * * Read-capacity returns the LBA of the last sector, * therefore the number of sectors must be incremented. */ if(unit->sectors != 0) unit->sectors++; ok = 1; break; case 1: ok = 1; break; case 2: continue; } break; } free(p); free(r); if(ok) return ok+retries; else return 0; }
static long sdbio(Chan* c, int write, char* a, long len, uvlong off) { int nchange; long l; uchar *b; SDpart *pp; SDunit *unit; SDev *sdev; ulong max, nb, offset; uvlong bno; sdev = sdgetdev(DEV(c->qid)); if(sdev == nil){ decref(&sdev->r); error(Enonexist); } unit = sdev->unit[UNIT(c->qid)]; if(unit == nil) error(Enonexist); nchange = 0; qlock(&unit->ctl); while(waserror()){ /* notification of media change; go around again */ if(strcmp(up->env->errstr, Eio) == 0 && unit->sectors == 0 && nchange++ == 0){ sdinitpart(unit); continue; } /* other errors; give up */ qunlock(&unit->ctl); decref(&sdev->r); nexterror(); } pp = &unit->part[PART(c->qid)]; if(unit->vers+pp->vers != c->qid.vers) error(Echange); /* * Check the request is within bounds. * Removeable drives are locked throughout the I/O * in case the media changes unexpectedly. * Non-removeable drives are not locked during the I/O * to allow the hardware to optimise if it can; this is * a little fast and loose. * It's assumed that non-removeable media parameters * (sectors, secsize) can't change once the drive has * been brought online. */ bno = (off/unit->secsize) + pp->start; nb = ((off+len+unit->secsize-1)/unit->secsize) + pp->start - bno; max = SDmaxio/unit->secsize; if(nb > max) nb = max; if(bno+nb > pp->end) nb = pp->end - bno; if(bno >= pp->end || nb == 0){ if(write) error(Eio); qunlock(&unit->ctl); decref(&sdev->r); poperror(); return 0; } if(!(unit->inquiry[1] & SDinq1removable)){ qunlock(&unit->ctl); poperror(); } b = sdmalloc(nb*unit->secsize); if(b == nil) error(Enomem); if(waserror()){ sdfree(b); if(!(unit->inquiry[1] & SDinq1removable)) decref(&sdev->r); /* gadverdamme! */ nexterror(); } offset = off%unit->secsize; if(offset+len > nb*unit->secsize) len = nb*unit->secsize - offset; if(write){ if(offset || (len%unit->secsize)){ l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno); if(l < 0) error(Eio); if(l < (nb*unit->secsize)){ nb = l/unit->secsize; l = nb*unit->secsize - offset; if(len > l) len = l; } } memmove(b+offset, a, len); l = unit->dev->ifc->bio(unit, 0, 1, b, nb, bno); if(l < 0) error(Eio); if(l < offset) len = 0; else if(len > l - offset) len = l - offset; } else{ l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno); if(l < 0) error(Eio); if(l < offset) len = 0; else if(len > l - offset) len = l - offset; memmove(a, b+offset, len); } sdfree(b); poperror(); if(unit->inquiry[1] & SDinq1removable){ qunlock(&unit->ctl); poperror(); } decref(&sdev->r); return len; }
static long sdrio(SDreq* r, void* a, long n) { char *errstr; int rv; void *data; SDunit *u; int (*f)(SDreq*); if(n >= SDmaxio || n < 0) error(Etoobig); u = r->unit; if(u->haversense && r->cmd[0] == 0x03){ u->haversense = 0; r->rlen = sizeof u->rsense; if(r->rlen > n) r->rlen = n; memmove(a, u->rsense, r->rlen); r->status = SDok; return r->rlen; } data = nil; while(n > 0 && (data = sdmalloc(n)) == nil){ if(!waserror()){ resrcwait("no memory for sdrio"); poperror(); } } if(waserror()){ sdfree(data); r->data = nil; nexterror(); } if(r->write && n > 0) memmove(data, a, n); r->data = data; r->dlen = n; if(r->proto == SData){ f = u->dev->ifc->ataio; errstr = Enoata; }else{ f = u->dev->ifc->rio; errstr = Enoscsi; } if(f == nil) error(errstr); rv = f(r); if(r->flags & SDvalidsense){ memmove(u->rsense, r->sense, sizeof u->rsense); u->haversense = 1; } if(rv != SDok) error(Eio); if(!r->write && r->rlen > 0) memmove(a, data, r->rlen); poperror(); sdfree(data); r->data = nil; return r->rlen; }
long scsibio(SDunit* unit, int lun, int write, void* data, long nb, long bno) { static SDreq *r; long rlen; if((r = sdmalloc(r, sizeof(SDreq))) == nil) return SDmalloc; r->unit = unit; r->lun = lun; again: r->write = write; if(write == 0) r->cmd[0] = 0x28; else r->cmd[0] = 0x2A; r->cmd[1] = (lun<<5); r->cmd[2] = bno>>24; r->cmd[3] = bno>>16; r->cmd[4] = bno>>8; r->cmd[5] = bno; r->cmd[6] = 0; r->cmd[7] = nb>>8; r->cmd[8] = nb; r->cmd[9] = 0; r->clen = 10; r->data = data; r->dlen = nb*unit->secsize; r->flags = 0; r->status = ~0; // cgascreenputs("E", 1); switch(scsirio(r)){ default: rlen = -1; break; case 0: rlen = r->rlen; break; case 2: rlen = -1; if(!(r->flags & SDvalidsense)) break; switch(r->sense[2] & 0x0F){ default: break; case 0x06: /* check condition */ /* * Check for a removeable media change. * If so, mark it and zap the geometry info * to force an online request. */ if(r->sense[12] != 0x28 || r->sense[13] != 0) break; if(unit->inquiry[1] & 0x80){ unit->sectors = 0; } break; case 0x02: /* not ready */ /* * If unit is becoming ready, * rather than not not ready, try again. */ if(r->sense[12] == 0x04 && r->sense[13] == 0x01) goto again; break; } break; } return rlen; }