static ulong scsi_read(block_dev_desc_t *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) { int device = block_dev->dev; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks = 0; ccb* pccb=(ccb *)&tempccb; device&=0xff; /* Setup device */ pccb->target=scsi_dev_desc[device].target; pccb->lun=scsi_dev_desc[device].lun; buf_addr=(unsigned long)buffer; start=blknr; blks=blkcnt; debug("\nscsi_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", device, start, blks, (unsigned long)buffer); do { pccb->pdata=(unsigned char *)buf_addr; #ifdef CONFIG_SYS_64BIT_LBA if (start > SCSI_LBA48_READ) { unsigned long blocks; blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK); pccb->datalen = scsi_dev_desc[device].blksz * blocks; scsi_setup_read16(pccb, start, blocks); start += blocks; blks -= blocks; } else #endif if (blks > SCSI_MAX_READ_BLK) { pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; smallblks=SCSI_MAX_READ_BLK; scsi_setup_read_ext(pccb,start,smallblks); start+=SCSI_MAX_READ_BLK; blks-=SCSI_MAX_READ_BLK; } else { pccb->datalen=scsi_dev_desc[device].blksz * blks; smallblks=(unsigned short) blks; scsi_setup_read_ext(pccb,start,smallblks); start+=blks; blks=0; } debug("scsi_read_ext: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); if (scsi_exec(pccb) != true) { scsi_print_error(pccb); blkcnt-=blks; break; } buf_addr+=pccb->datalen; } while(blks!=0); debug("scsi_read_ext: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); return(blkcnt); }
static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) #endif { #ifdef CONFIG_BLK struct blk_desc *block_dev = dev_get_uclass_platdata(dev); struct udevice *bdev = dev->parent; #else struct udevice *bdev = NULL; #endif lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; /* Setup device */ pccb->target = block_dev->target; pccb->lun = block_dev->lun; buf_addr = (unsigned long)buffer; start = blknr; blks = blkcnt; debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", __func__, block_dev->devnum, start, blks, (unsigned long)buffer); do { pccb->pdata = (unsigned char *)buf_addr; if (blks > SCSI_MAX_WRITE_BLK) { pccb->datalen = (block_dev->blksz * SCSI_MAX_WRITE_BLK); smallblks = SCSI_MAX_WRITE_BLK; scsi_setup_write_ext(pccb, start, smallblks); start += SCSI_MAX_WRITE_BLK; blks -= SCSI_MAX_WRITE_BLK; } else { pccb->datalen = block_dev->blksz * blks; smallblks = (unsigned short)blks; scsi_setup_write_ext(pccb, start, smallblks); start += blks; blks = 0; } debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", __func__, start, smallblks, buf_addr); if (scsi_exec(bdev, pccb)) { scsi_print_error(pccb); blkcnt -= blks; break; } buf_addr += pccb->datalen; } while (blks != 0); debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", __func__, start, smallblks, buf_addr); return blkcnt; }
static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer) { lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; ccb* pccb=(ccb *)&tempccb; device&=0xff; /* Setup device */ pccb->target=scsi_dev_desc[device].target; pccb->lun=scsi_dev_desc[device].lun; buf_addr=(unsigned long)buffer; start=blknr; blks=blkcnt; debug("\nscsi_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", device, start, blks, (unsigned long)buffer); do { pccb->pdata=(unsigned char *)buf_addr; if(blks>SCSI_MAX_READ_BLK) { pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; smallblks=SCSI_MAX_READ_BLK; scsi_setup_read_ext(pccb,start,smallblks); start+=SCSI_MAX_READ_BLK; blks-=SCSI_MAX_READ_BLK; } else { pccb->datalen=scsi_dev_desc[device].blksz * blks; smallblks=(unsigned short) blks; scsi_setup_read_ext(pccb,start,smallblks); start+=blks; blks=0; } debug("scsi_read_ext: startblk " LBAF ", blccnt %x buffer %x\n", start, smallblks, buf_addr); if(scsi_exec(pccb)!=TRUE) { scsi_print_error(pccb); blkcnt-=blks; break; } buf_addr+=pccb->datalen; } while(blks!=0); debug("scsi_read_ext: end startblk " LBAF ", blccnt %x buffer %x\n", start, smallblks, buf_addr); return(blkcnt); }
static ulong scsi_write(block_dev_desc_t *block_dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) { int device = block_dev->dev; lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks; ccb* pccb = (ccb *)&tempccb; device &= 0xff; /* Setup device */ pccb->target = scsi_dev_desc[device].target; pccb->lun = scsi_dev_desc[device].lun; buf_addr = (unsigned long)buffer; start = blknr; blks = blkcnt; debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", __func__, device, start, blks, (unsigned long)buffer); do { pccb->pdata = (unsigned char *)buf_addr; if (blks > SCSI_MAX_WRITE_BLK) { pccb->datalen = (scsi_dev_desc[device].blksz * SCSI_MAX_WRITE_BLK); smallblks = SCSI_MAX_WRITE_BLK; scsi_setup_write_ext(pccb, start, smallblks); start += SCSI_MAX_WRITE_BLK; blks -= SCSI_MAX_WRITE_BLK; } else { pccb->datalen = scsi_dev_desc[device].blksz * blks; smallblks = (unsigned short)blks; scsi_setup_write_ext(pccb, start, smallblks); start += blks; blks = 0; } debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", __func__, start, smallblks, buf_addr); if (scsi_exec(pccb) != true) { scsi_print_error(pccb); blkcnt -= blks; break; } buf_addr += pccb->datalen; } while (blks != 0); debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", __func__, start, smallblks, buf_addr); return blkcnt; }
/** * scsi_detect_dev - Detect scsi device * * @target: target id * @lun: target lun * @dev_desc: block device description * * The scsi_detect_dev detects and fills a dev_desc structure when the device is * detected. * * Return: 0 on success, error value otherwise */ static int scsi_detect_dev(struct udevice *dev, int target, int lun, struct blk_desc *dev_desc) { unsigned char perq, modi; lbaint_t capacity; unsigned long blksz; struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; pccb->target = target; pccb->lun = lun; pccb->pdata = (unsigned char *)&tempbuff; pccb->datalen = 512; scsi_setup_inquiry(pccb); if (scsi_exec(dev, pccb)) { if (pccb->contr_stat == SCSI_SEL_TIME_OUT) { /* * selection timeout => assuming no * device present */ debug("Selection timeout ID %d\n", pccb->target); return -ETIMEDOUT; } scsi_print_error(pccb); return -ENODEV; } perq = tempbuff[0]; modi = tempbuff[1]; if ((perq & 0x1f) == 0x1f) return -ENODEV; /* skip unknown devices */ if ((modi & 0x80) == 0x80) /* drive is removable */ dev_desc->removable = true; /* get info for this device */ scsi_ident_cpy((unsigned char *)dev_desc->vendor, &tempbuff[8], 8); scsi_ident_cpy((unsigned char *)dev_desc->product, &tempbuff[16], 16); scsi_ident_cpy((unsigned char *)dev_desc->revision, &tempbuff[32], 4); dev_desc->target = pccb->target; dev_desc->lun = pccb->lun; pccb->datalen = 0; scsi_setup_test_unit_ready(pccb); if (scsi_exec(dev, pccb)) { if (dev_desc->removable) { dev_desc->type = perq; goto removable; } scsi_print_error(pccb); return -EINVAL; } if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) { scsi_print_error(pccb); return -EINVAL; } dev_desc->lba = capacity; dev_desc->blksz = blksz; dev_desc->log2blksz = LOG2(dev_desc->blksz); dev_desc->type = perq; removable: return 0; }
static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) #endif { #ifdef CONFIG_BLK struct blk_desc *block_dev = dev_get_uclass_platdata(dev); struct udevice *bdev = dev->parent; #else struct udevice *bdev = NULL; #endif lbaint_t start, blks; uintptr_t buf_addr; unsigned short smallblks = 0; struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; /* Setup device */ pccb->target = block_dev->target; pccb->lun = block_dev->lun; buf_addr = (unsigned long)buffer; start = blknr; blks = blkcnt; debug("\nscsi_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", block_dev->devnum, start, blks, (unsigned long)buffer); do { pccb->pdata = (unsigned char *)buf_addr; #ifdef CONFIG_SYS_64BIT_LBA if (start > SCSI_LBA48_READ) { unsigned long blocks; blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK); pccb->datalen = block_dev->blksz * blocks; scsi_setup_read16(pccb, start, blocks); start += blocks; blks -= blocks; } else #endif if (blks > SCSI_MAX_READ_BLK) { pccb->datalen = block_dev->blksz * SCSI_MAX_READ_BLK; smallblks = SCSI_MAX_READ_BLK; scsi_setup_read_ext(pccb, start, smallblks); start += SCSI_MAX_READ_BLK; blks -= SCSI_MAX_READ_BLK; } else { pccb->datalen = block_dev->blksz * blks; smallblks = (unsigned short)blks; scsi_setup_read_ext(pccb, start, smallblks); start += blks; blks = 0; } debug("scsi_read_ext: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); if (scsi_exec(bdev, pccb)) { scsi_print_error(pccb); blkcnt -= blks; break; } buf_addr += pccb->datalen; } while (blks != 0); debug("scsi_read_ext: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); return blkcnt; }
/********************************************************************************* * (re)-scan the scsi bus and reports scsi device info * to the user if mode = 1 */ void scsi_scan(int mode) { unsigned char i,perq,modi,lun; lbaint_t capacity; unsigned long blksz; ccb* pccb=(ccb *)&tempccb; if(mode==1) { printf("scanning bus for devices...\n"); } for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { scsi_dev_desc[i].target=0xff; scsi_dev_desc[i].lun=0xff; scsi_dev_desc[i].lba=0; scsi_dev_desc[i].blksz=0; scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; scsi_dev_desc[i].vendor[0]=0; scsi_dev_desc[i].product[0]=0; scsi_dev_desc[i].revision[0]=0; scsi_dev_desc[i].removable=FALSE; scsi_dev_desc[i].if_type=IF_TYPE_SCSI; scsi_dev_desc[i].dev=i; scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; scsi_dev_desc[i].block_read=scsi_read; scsi_dev_desc[i].block_write = scsi_write; } scsi_max_devs=0; for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { pccb->target=i; for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { pccb->lun=lun; pccb->pdata=(unsigned char *)&tempbuff; pccb->datalen=512; scsi_setup_inquiry(pccb); if(scsi_exec(pccb)!=TRUE) { if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { debug ("Selection timeout ID %d\n",pccb->target); continue; /* selection timeout => assuming no device present */ } /* Device present at this target/lun * but may not be ready yet. */ scsi_print_error(pccb); continue; } perq=tempbuff[0]; modi=tempbuff[1]; if((perq & 0x1f)==0x1f) { continue; /* skip unknown devices */ } if((modi&0x80)==0x80) /* drive is removable */ scsi_dev_desc[scsi_max_devs].removable=TRUE; /* get info for this device */ scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], &tempbuff[8], 8); scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], &tempbuff[16], 16); scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], &tempbuff[32], 4); scsi_dev_desc[scsi_max_devs].target=pccb->target; scsi_dev_desc[scsi_max_devs].lun=pccb->lun; pccb->datalen=0; scsi_setup_test_unit_ready(pccb); if(scsi_exec(pccb)!=TRUE) { if(scsi_dev_desc[scsi_max_devs].removable==TRUE) { scsi_dev_desc[scsi_max_devs].type=perq; goto removable; } scsi_print_error(pccb); continue; } if (scsi_read_capacity(pccb, &capacity, &blksz)) { scsi_print_error(pccb); continue; } scsi_dev_desc[scsi_max_devs].lba=capacity; scsi_dev_desc[scsi_max_devs].blksz=blksz; scsi_dev_desc[scsi_max_devs].type=perq; init_part(&scsi_dev_desc[scsi_max_devs]); removable: if(mode==1) { printf (" Device %d: ", scsi_max_devs); dev_print(&scsi_dev_desc[scsi_max_devs]); } /* if mode */ scsi_max_devs++; } /* next LUN */ } if(scsi_max_devs>0) scsi_curr_dev=0; else scsi_curr_dev = -1; printf("Found %d device(s).\n", scsi_max_devs); }
/********************************************************************************* * (re)-scan the scsi bus and reports scsi device info * to the user if mode = 1 */ void scsi_scan(int mode) { unsigned char i,perq,modi,lun; lbaint_t capacity; unsigned long blksz; ccb* pccb=(ccb *)&tempccb; static int scsi_detected = 0; if (scsi_detected) { printf("** scsi detection can run only once - please"); printf(" reset board before running detection again **\n"); return; } scsi_detected = 1; if(mode==1) { printf("scanning bus for devices...\n"); } if (tempbuff == NULL) { tempbuff = memalign(ARCH_DMA_MINALIGN, 512); if (tempbuff == NULL) { if (mode == 1) printf("error: cannot allocate buffer\n"); return; } } for(i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++) { scsi_dev_desc[i].target=0xff; scsi_dev_desc[i].lun=0xff; scsi_dev_desc[i].lba=0; scsi_dev_desc[i].blksz=0; scsi_dev_desc[i].log2blksz = LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz)); scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; scsi_dev_desc[i].vendor[0]=0; scsi_dev_desc[i].product[0]=0; scsi_dev_desc[i].revision[0]=0; scsi_dev_desc[i].removable=FALSE; scsi_dev_desc[i].if_type=IF_TYPE_SCSI; scsi_dev_desc[i].dev=i; scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; scsi_dev_desc[i].block_read=scsi_read; scsi_dev_desc[i].block_write = scsi_write; } scsi_max_devs=0; for(i=0; i<CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) { pccb->target=i; for(lun=0; lun<CONFIG_SYS_SCSI_MAX_LUN; lun++) { pccb->lun=lun; pccb->pdata = (unsigned char *)tempbuff; pccb->datalen=512; scsi_setup_inquiry(pccb); if(scsi_exec(pccb)!=TRUE) { if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { debug ("Selection timeout ID %d\n",pccb->target); continue; /* selection timeout => assuming no device present */ } scsi_print_error(pccb); continue; } perq=tempbuff[0]; modi=tempbuff[1]; if((perq & 0x1f)==0x1f) { continue; /* skip unknown devices */ } if((modi&0x80)==0x80) /* drive is removable */ scsi_dev_desc[scsi_max_devs].removable=TRUE; /* get info for this device */ scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], &tempbuff[8], 8); scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], &tempbuff[16], 16); scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], &tempbuff[32], 4); scsi_dev_desc[scsi_max_devs].target=pccb->target; scsi_dev_desc[scsi_max_devs].lun=pccb->lun; pccb->datalen=0; scsi_setup_test_unit_ready(pccb); if(scsi_exec(pccb)!=TRUE) { if(scsi_dev_desc[scsi_max_devs].removable==TRUE) { scsi_dev_desc[scsi_max_devs].type=perq; goto removable; } scsi_print_error(pccb); continue; } if (scsi_read_capacity(pccb, &capacity, &blksz)) { scsi_print_error(pccb); continue; } scsi_dev_desc[scsi_max_devs].lba=capacity; scsi_dev_desc[scsi_max_devs].blksz=blksz; scsi_dev_desc[scsi_max_devs].log2blksz = LOG2(scsi_dev_desc[scsi_max_devs].blksz); scsi_dev_desc[scsi_max_devs].type=perq; init_part(&scsi_dev_desc[scsi_max_devs]); removable: if(mode==1) { printf (" Device %d: ", scsi_max_devs); dev_print(&scsi_dev_desc[scsi_max_devs]); } /* if mode */ scsi_max_devs++; } /* next LUN */ } if (scsi_max_devs > 0) scsi_curr_dev = 0; else scsi_curr_dev = -1; printf("Found %d device(s).\n", scsi_max_devs); setenv_ulong("scsidevs", scsi_max_devs); }