cmd_scsi.c 14 KB

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