]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_scsi.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / common / cmd_scsi.c
index 31ea78845ceb5ac8e42f4e1d723d4550187f35d9..cbc107ea5014fd8f36b5f6b42885ec44ba6dfb36 100644 (file)
@@ -2,26 +2,7 @@
  * (C) Copyright 2001
  * Denis Peter, MPL AG Switzerland
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- *
- *
- *
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
@@ -29,6 +10,7 @@
  */
 #include <common.h>
 #include <command.h>
+#include <inttypes.h>
 #include <asm/processor.h>
 #include <scsi.h>
 #include <image.h>
@@ -72,7 +54,6 @@ static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
  *  forward declerations of some Setup Routines
  */
 void scsi_setup_test_unit_ready(ccb * pccb);
-void scsi_setup_read_capacity(ccb * pccb);
 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
 static void scsi_setup_write_ext(ccb *pccb, unsigned long start,
@@ -81,8 +62,11 @@ void scsi_setup_inquiry(ccb * pccb);
 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
 
 
-static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer);
-static ulong scsi_write(int device, ulong blknr,
+static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
+                             unsigned long *blksz);
+static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
+                      void *buffer);
+static ulong scsi_write(int device, lbaint_t blknr,
                        lbaint_t blkcnt, const void *buffer);
 
 
@@ -93,7 +77,8 @@ static ulong scsi_write(int device, ulong blknr,
 void scsi_scan(int mode)
 {
        unsigned char i,perq,modi,lun;
-       unsigned long capacity,blksz;
+       lbaint_t capacity;
+       unsigned long blksz;
        ccb* pccb=(ccb *)&tempccb;
 
        if(mode==1) {
@@ -104,11 +89,13 @@ void scsi_scan(int mode)
                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].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;
@@ -123,7 +110,7 @@ void scsi_scan(int mode)
                        pccb->pdata=(unsigned char *)&tempbuff;
                        pccb->datalen=512;
                        scsi_setup_inquiry(pccb);
-                       if(scsi_exec(pccb)!=TRUE) {
+                       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 */
@@ -137,7 +124,7 @@ void scsi_scan(int mode)
                                continue; /* skip unknown devices */
                        }
                        if((modi&0x80)==0x80) /* drive is removable */
-                               scsi_dev_desc[scsi_max_devs].removable=TRUE;
+                               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);
@@ -150,26 +137,22 @@ void scsi_scan(int mode)
 
                        pccb->datalen=0;
                        scsi_setup_test_unit_ready(pccb);
-                       if(scsi_exec(pccb)!=TRUE) {
-                               if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
+                       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;
                        }
-                       pccb->datalen=8;
-                       scsi_setup_read_capacity(pccb);
-                       if(scsi_exec(pccb)!=TRUE) {
+                       if (scsi_read_capacity(pccb, &capacity, &blksz)) {
                                scsi_print_error(pccb);
                                continue;
                        }
-                       capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
-                                       ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
-                       blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
-                               ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
                        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:
@@ -184,6 +167,16 @@ removable:
                scsi_curr_dev=0;
        else
                scsi_curr_dev = -1;
+
+       printf("Found %d device(s).\n", scsi_max_devs);
+#ifndef CONFIG_SPL_BUILD
+       setenv_ulong("scsidevs", scsi_max_devs);
+#endif
+}
+
+int scsi_get_disk_count(void)
+{
+       return scsi_max_devs;
 }
 
 #ifdef CONFIG_PCI
@@ -364,7 +357,8 @@ int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
 
-static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
+static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
+                      void *buffer)
 {
        lbaint_t start, blks;
        uintptr_t buf_addr;
@@ -398,9 +392,9 @@ static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
                        blks=0;
                }
                debug("scsi_read_ext: startblk " LBAF
-                     ", blccnt %x buffer %lx\n",
+                     ", blccnt %x buffer %" PRIXPTR "\n",
                      start, smallblks, buf_addr);
-               if(scsi_exec(pccb)!=TRUE) {
+               if (scsi_exec(pccb) != true) {
                        scsi_print_error(pccb);
                        blkcnt-=blks;
                        break;
@@ -408,7 +402,7 @@ static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
                buf_addr+=pccb->datalen;
        } while(blks!=0);
        debug("scsi_read_ext: end startblk " LBAF
-             ", blccnt %x buffer %lx\n", start, smallblks, buf_addr);
+             ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
        return(blkcnt);
 }
 
@@ -419,7 +413,7 @@ static ulong scsi_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
 /* Almost the maximum amount of the scsi_ext command.. */
 #define SCSI_MAX_WRITE_BLK 0xFFFF
 
-static ulong scsi_write(int device, ulong blknr,
+static ulong scsi_write(int device, lbaint_t blknr,
                        lbaint_t blkcnt, const void *buffer)
 {
        lbaint_t start, blks;
@@ -452,16 +446,16 @@ static ulong scsi_write(int device, ulong blknr,
                        start += blks;
                        blks = 0;
                }
-               debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
+               debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
                      __func__, start, smallblks, buf_addr);
-               if (scsi_exec(pccb) != TRUE) {
+               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 %lx\n",
+       debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
              __func__, start, smallblks, buf_addr);
        return blkcnt;
 }
@@ -506,6 +500,67 @@ void scsi_trim_trail (unsigned char *str, unsigned int len)
        }
 }
 
+int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz)
+{
+       *capacity = 0;
+
+       memset(pccb->cmd, 0, sizeof(pccb->cmd));
+       pccb->cmd[0] = SCSI_RD_CAPAC10;
+       pccb->cmd[1] = pccb->lun << 5;
+       pccb->cmdlen = 10;
+       pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
+
+       pccb->datalen = 8;
+       if (scsi_exec(pccb) != true)
+               return 1;
+
+       *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
+                   ((lbaint_t)pccb->pdata[1] << 16) |
+                   ((lbaint_t)pccb->pdata[2] << 8)  |
+                   ((lbaint_t)pccb->pdata[3]);
+
+       if (*capacity != 0xffffffff) {
+               /* Read capacity (10) was sufficient for this drive. */
+               *blksz = ((unsigned long)pccb->pdata[4] << 24) |
+                        ((unsigned long)pccb->pdata[5] << 16) |
+                        ((unsigned long)pccb->pdata[6] << 8)  |
+                        ((unsigned long)pccb->pdata[7]);
+               return 0;
+       }
+
+       /* Read capacity (10) was insufficient. Use read capacity (16). */
+
+       memset(pccb->cmd, 0, sizeof(pccb->cmd));
+       pccb->cmd[0] = SCSI_RD_CAPAC16;
+       pccb->cmd[1] = 0x10;
+       pccb->cmdlen = 16;
+       pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
+
+       pccb->datalen = 16;
+       if (scsi_exec(pccb) != true)
+               return 1;
+
+       *capacity = ((uint64_t)pccb->pdata[0] << 56) |
+                   ((uint64_t)pccb->pdata[1] << 48) |
+                   ((uint64_t)pccb->pdata[2] << 40) |
+                   ((uint64_t)pccb->pdata[3] << 32) |
+                   ((uint64_t)pccb->pdata[4] << 24) |
+                   ((uint64_t)pccb->pdata[5] << 16) |
+                   ((uint64_t)pccb->pdata[6] << 8)  |
+                   ((uint64_t)pccb->pdata[7]);
+
+       *blksz = ((uint64_t)pccb->pdata[8]  << 56) |
+                ((uint64_t)pccb->pdata[9]  << 48) |
+                ((uint64_t)pccb->pdata[10] << 40) |
+                ((uint64_t)pccb->pdata[11] << 32) |
+                ((uint64_t)pccb->pdata[12] << 24) |
+                ((uint64_t)pccb->pdata[13] << 16) |
+                ((uint64_t)pccb->pdata[14] << 8)  |
+                ((uint64_t)pccb->pdata[15]);
+
+       return 0;
+}
+
 
 /************************************************************************************
  * Some setup (fill-in) routines
@@ -522,23 +577,6 @@ void scsi_setup_test_unit_ready(ccb * pccb)
        pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
 }
 
-void scsi_setup_read_capacity(ccb * pccb)
-{
-       pccb->cmd[0]=SCSI_RD_CAPAC;
-       pccb->cmd[1]=pccb->lun<<5;
-       pccb->cmd[2]=0;
-       pccb->cmd[3]=0;
-       pccb->cmd[4]=0;
-       pccb->cmd[5]=0;
-       pccb->cmd[6]=0;
-       pccb->cmd[7]=0;
-       pccb->cmd[8]=0;
-       pccb->cmd[9]=0;
-       pccb->cmdlen=10;
-       pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
-
-}
-
 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
 {
        pccb->cmd[0]=SCSI_READ10;