cmd_scsi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. *
  24. *
  25. */
  26. /*
  27. * SCSI support.
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <asm/processor.h>
  32. #include <scsi.h>
  33. #include <image.h>
  34. #include <pci.h>
  35. #ifdef CONFIG_SCSI_SYM53C8XX
  36. #define SCSI_VEND_ID 0x1000
  37. #ifndef CONFIG_SCSI_DEV_ID
  38. #define SCSI_DEV_ID 0x0001
  39. #else
  40. #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
  41. #endif
  42. #elif defined CONFIG_SATA_ULI5288
  43. #define SCSI_VEND_ID 0x10b9
  44. #define SCSI_DEV_ID 0x5288
  45. #elif !defined(CONFIG_SCSI_AHCI_PLAT)
  46. #error no scsi device defined
  47. #endif
  48. static ccb tempccb; /* temporary scsi command buffer */
  49. static unsigned char tempbuff[512]; /* temporary data buffer */
  50. static int scsi_max_devs; /* number of highest available scsi device */
  51. static int scsi_curr_dev; /* current device */
  52. static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
  53. /********************************************************************************
  54. * forward declerations of some Setup Routines
  55. */
  56. void scsi_setup_test_unit_ready(ccb * pccb);
  57. void scsi_setup_read_capacity(ccb * pccb);
  58. void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
  59. void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
  60. void scsi_setup_inquiry(ccb * pccb);
  61. void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
  62. ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer);
  63. /*********************************************************************************
  64. * (re)-scan the scsi bus and reports scsi device info
  65. * to the user if mode = 1
  66. */
  67. void scsi_scan(int mode)
  68. {
  69. unsigned char i,perq,modi,lun;
  70. unsigned long capacity,blksz;
  71. ccb* pccb=(ccb *)&tempccb;
  72. if(mode==1) {
  73. printf("scanning bus for devices...\n");
  74. }
  75. for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
  76. scsi_dev_desc[i].target=0xff;
  77. scsi_dev_desc[i].lun=0xff;
  78. scsi_dev_desc[i].lba=0;
  79. scsi_dev_desc[i].blksz=0;
  80. scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
  81. scsi_dev_desc[i].vendor[0]=0;
  82. scsi_dev_desc[i].product[0]=0;
  83. scsi_dev_desc[i].revision[0]=0;
  84. scsi_dev_desc[i].removable=FALSE;
  85. scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
  86. scsi_dev_desc[i].dev=i;
  87. scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  88. scsi_dev_desc[i].block_read=scsi_read;
  89. }
  90. scsi_max_devs=0;
  91. for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
  92. pccb->target=i;
  93. for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
  94. pccb->lun=lun;
  95. pccb->pdata=(unsigned char *)&tempbuff;
  96. pccb->datalen=512;
  97. scsi_setup_inquiry(pccb);
  98. if(scsi_exec(pccb)!=TRUE) {
  99. if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
  100. debug ("Selection timeout ID %d\n",pccb->target);
  101. continue; /* selection timeout => assuming no device present */
  102. }
  103. scsi_print_error(pccb);
  104. continue;
  105. }
  106. perq=tempbuff[0];
  107. modi=tempbuff[1];
  108. if((perq & 0x1f)==0x1f) {
  109. continue; /* skip unknown devices */
  110. }
  111. if((modi&0x80)==0x80) /* drive is removable */
  112. scsi_dev_desc[scsi_max_devs].removable=TRUE;
  113. /* get info for this device */
  114. scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
  115. &tempbuff[8], 8);
  116. scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
  117. &tempbuff[16], 16);
  118. scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
  119. &tempbuff[32], 4);
  120. scsi_dev_desc[scsi_max_devs].target=pccb->target;
  121. scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
  122. pccb->datalen=0;
  123. scsi_setup_test_unit_ready(pccb);
  124. if(scsi_exec(pccb)!=TRUE) {
  125. if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
  126. scsi_dev_desc[scsi_max_devs].type=perq;
  127. goto removable;
  128. }
  129. scsi_print_error(pccb);
  130. continue;
  131. }
  132. pccb->datalen=8;
  133. scsi_setup_read_capacity(pccb);
  134. if(scsi_exec(pccb)!=TRUE) {
  135. scsi_print_error(pccb);
  136. continue;
  137. }
  138. capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
  139. ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
  140. blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
  141. ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
  142. scsi_dev_desc[scsi_max_devs].lba=capacity;
  143. scsi_dev_desc[scsi_max_devs].blksz=blksz;
  144. scsi_dev_desc[scsi_max_devs].type=perq;
  145. init_part(&scsi_dev_desc[scsi_max_devs]);
  146. removable:
  147. if(mode==1) {
  148. printf (" Device %d: ", scsi_max_devs);
  149. dev_print(&scsi_dev_desc[scsi_max_devs]);
  150. } /* if mode */
  151. scsi_max_devs++;
  152. } /* next LUN */
  153. }
  154. if(scsi_max_devs>0)
  155. scsi_curr_dev=0;
  156. else
  157. scsi_curr_dev = -1;
  158. }
  159. #ifdef CONFIG_PCI
  160. void scsi_init(void)
  161. {
  162. int busdevfunc;
  163. busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
  164. if(busdevfunc==-1) {
  165. printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
  166. return;
  167. }
  168. #ifdef DEBUG
  169. else {
  170. printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",SCSI_VEND_ID,SCSI_DEV_ID,(busdevfunc>>16)&0xFF,(busdevfunc>>11)&0x1F,(busdevfunc>>8)&0x7);
  171. }
  172. #endif
  173. scsi_low_level_init(busdevfunc);
  174. scsi_scan(1);
  175. }
  176. #endif
  177. #ifdef CONFIG_PARTITIONS
  178. block_dev_desc_t * scsi_get_dev(int dev)
  179. {
  180. return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
  181. }
  182. #endif
  183. /******************************************************************************
  184. * scsi boot command intepreter. Derived from diskboot
  185. */
  186. int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  187. {
  188. return common_diskboot(cmdtp, "scsi", argc, argv);
  189. }
  190. /*********************************************************************************
  191. * scsi command intepreter
  192. */
  193. int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  194. {
  195. switch (argc) {
  196. case 0:
  197. case 1:
  198. return CMD_RET_USAGE;
  199. case 2:
  200. if (strncmp(argv[1],"res",3) == 0) {
  201. printf("\nReset SCSI\n");
  202. scsi_bus_reset();
  203. scsi_scan(1);
  204. return 0;
  205. }
  206. if (strncmp(argv[1],"inf",3) == 0) {
  207. int i;
  208. for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
  209. if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
  210. continue; /* list only known devices */
  211. printf ("SCSI dev. %d: ", i);
  212. dev_print(&scsi_dev_desc[i]);
  213. }
  214. return 0;
  215. }
  216. if (strncmp(argv[1],"dev",3) == 0) {
  217. if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
  218. printf("\nno SCSI devices available\n");
  219. return 1;
  220. }
  221. printf ("\n Device %d: ", scsi_curr_dev);
  222. dev_print(&scsi_dev_desc[scsi_curr_dev]);
  223. return 0;
  224. }
  225. if (strncmp(argv[1],"scan",4) == 0) {
  226. scsi_scan(1);
  227. return 0;
  228. }
  229. if (strncmp(argv[1],"part",4) == 0) {
  230. int dev, ok;
  231. for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
  232. if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
  233. ok++;
  234. if (dev)
  235. printf("\n");
  236. debug ("print_part of %x\n",dev);
  237. print_part(&scsi_dev_desc[dev]);
  238. }
  239. }
  240. if (!ok)
  241. printf("\nno SCSI devices available\n");
  242. return 1;
  243. }
  244. return CMD_RET_USAGE;
  245. case 3:
  246. if (strncmp(argv[1],"dev",3) == 0) {
  247. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  248. printf ("\nSCSI device %d: ", dev);
  249. if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
  250. printf("unknown device\n");
  251. return 1;
  252. }
  253. printf ("\n Device %d: ", dev);
  254. dev_print(&scsi_dev_desc[dev]);
  255. if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
  256. return 1;
  257. }
  258. scsi_curr_dev = dev;
  259. printf("... is now current device\n");
  260. return 0;
  261. }
  262. if (strncmp(argv[1],"part",4) == 0) {
  263. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  264. if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
  265. print_part(&scsi_dev_desc[dev]);
  266. }
  267. else {
  268. printf ("\nSCSI device %d not available\n", dev);
  269. }
  270. return 1;
  271. }
  272. return CMD_RET_USAGE;
  273. default:
  274. /* at least 4 args */
  275. if (strcmp(argv[1],"read") == 0) {
  276. ulong addr = simple_strtoul(argv[2], NULL, 16);
  277. ulong blk = simple_strtoul(argv[3], NULL, 16);
  278. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  279. ulong n;
  280. printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
  281. scsi_curr_dev, blk, cnt);
  282. n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
  283. printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
  284. return 0;
  285. }
  286. } /* switch */
  287. return CMD_RET_USAGE;
  288. }
  289. /****************************************************************************************
  290. * scsi_read
  291. */
  292. #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
  293. ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
  294. {
  295. ulong start,blks, buf_addr;
  296. unsigned short smallblks;
  297. ccb* pccb=(ccb *)&tempccb;
  298. device&=0xff;
  299. /* Setup device
  300. */
  301. pccb->target=scsi_dev_desc[device].target;
  302. pccb->lun=scsi_dev_desc[device].lun;
  303. buf_addr=(unsigned long)buffer;
  304. start=blknr;
  305. blks=blkcnt;
  306. debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
  307. do {
  308. pccb->pdata=(unsigned char *)buf_addr;
  309. if(blks>SCSI_MAX_READ_BLK) {
  310. pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
  311. smallblks=SCSI_MAX_READ_BLK;
  312. scsi_setup_read_ext(pccb,start,smallblks);
  313. start+=SCSI_MAX_READ_BLK;
  314. blks-=SCSI_MAX_READ_BLK;
  315. }
  316. else {
  317. pccb->datalen=scsi_dev_desc[device].blksz * blks;
  318. smallblks=(unsigned short) blks;
  319. scsi_setup_read_ext(pccb,start,smallblks);
  320. start+=blks;
  321. blks=0;
  322. }
  323. debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
  324. if(scsi_exec(pccb)!=TRUE) {
  325. scsi_print_error(pccb);
  326. blkcnt-=blks;
  327. break;
  328. }
  329. buf_addr+=pccb->datalen;
  330. } while(blks!=0);
  331. debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
  332. return(blkcnt);
  333. }
  334. /* copy src to dest, skipping leading and trailing blanks
  335. * and null terminate the string
  336. */
  337. void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
  338. {
  339. int start,end;
  340. start=0;
  341. while(start<len) {
  342. if(src[start]!=' ')
  343. break;
  344. start++;
  345. }
  346. end=len-1;
  347. while(end>start) {
  348. if(src[end]!=' ')
  349. break;
  350. end--;
  351. }
  352. for( ; start<=end; start++) {
  353. *dest++=src[start];
  354. }
  355. *dest='\0';
  356. }
  357. /* Trim trailing blanks, and NUL-terminate string
  358. */
  359. void scsi_trim_trail (unsigned char *str, unsigned int len)
  360. {
  361. unsigned char *p = str + len - 1;
  362. while (len-- > 0) {
  363. *p-- = '\0';
  364. if (*p != ' ') {
  365. return;
  366. }
  367. }
  368. }
  369. /************************************************************************************
  370. * Some setup (fill-in) routines
  371. */
  372. void scsi_setup_test_unit_ready(ccb * pccb)
  373. {
  374. pccb->cmd[0]=SCSI_TST_U_RDY;
  375. pccb->cmd[1]=pccb->lun<<5;
  376. pccb->cmd[2]=0;
  377. pccb->cmd[3]=0;
  378. pccb->cmd[4]=0;
  379. pccb->cmd[5]=0;
  380. pccb->cmdlen=6;
  381. pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
  382. }
  383. void scsi_setup_read_capacity(ccb * pccb)
  384. {
  385. pccb->cmd[0]=SCSI_RD_CAPAC;
  386. pccb->cmd[1]=pccb->lun<<5;
  387. pccb->cmd[2]=0;
  388. pccb->cmd[3]=0;
  389. pccb->cmd[4]=0;
  390. pccb->cmd[5]=0;
  391. pccb->cmd[6]=0;
  392. pccb->cmd[7]=0;
  393. pccb->cmd[8]=0;
  394. pccb->cmd[9]=0;
  395. pccb->cmdlen=10;
  396. pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
  397. }
  398. void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
  399. {
  400. pccb->cmd[0]=SCSI_READ10;
  401. pccb->cmd[1]=pccb->lun<<5;
  402. pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
  403. pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
  404. pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
  405. pccb->cmd[5]=((unsigned char) (start))&0xff;
  406. pccb->cmd[6]=0;
  407. pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
  408. pccb->cmd[8]=(unsigned char) blocks & 0xff;
  409. pccb->cmd[6]=0;
  410. pccb->cmdlen=10;
  411. pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
  412. debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
  413. pccb->cmd[0],pccb->cmd[1],
  414. pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
  415. pccb->cmd[7],pccb->cmd[8]);
  416. }
  417. void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
  418. {
  419. pccb->cmd[0]=SCSI_READ6;
  420. pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
  421. pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
  422. pccb->cmd[3]=((unsigned char) (start))&0xff;
  423. pccb->cmd[4]=(unsigned char) blocks & 0xff;
  424. pccb->cmd[5]=0;
  425. pccb->cmdlen=6;
  426. pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
  427. debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
  428. pccb->cmd[0],pccb->cmd[1],
  429. pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
  430. }
  431. void scsi_setup_inquiry(ccb * pccb)
  432. {
  433. pccb->cmd[0]=SCSI_INQUIRY;
  434. pccb->cmd[1]=pccb->lun<<5;
  435. pccb->cmd[2]=0;
  436. pccb->cmd[3]=0;
  437. if(pccb->datalen>255)
  438. pccb->cmd[4]=255;
  439. else
  440. pccb->cmd[4]=(unsigned char)pccb->datalen;
  441. pccb->cmd[5]=0;
  442. pccb->cmdlen=6;
  443. pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
  444. }
  445. U_BOOT_CMD(
  446. scsi, 5, 1, do_scsi,
  447. "SCSI sub-system",
  448. "reset - reset SCSI controller\n"
  449. "scsi info - show available SCSI devices\n"
  450. "scsi scan - (re-)scan SCSI bus\n"
  451. "scsi device [dev] - show or set current device\n"
  452. "scsi part [dev] - print partition table of one or all SCSI devices\n"
  453. "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
  454. " to memory address `addr'"
  455. );
  456. U_BOOT_CMD(
  457. scsiboot, 3, 1, do_scsiboot,
  458. "boot from SCSI device",
  459. "loadAddr dev:part"
  460. );