ahci.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright (C) Freescale Semiconductor, Inc. 2006. All rights reserved.
  3. * Author: Jason Jin<Jason.jin@freescale.com>
  4. * Zhang Wei<wei.zhang@freescale.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. * with the reference on libata and ahci drvier in kernel
  25. *
  26. */
  27. #include <common.h>
  28. #ifdef CONFIG_SCSI_AHCI
  29. #include <command.h>
  30. #include <pci.h>
  31. #include <asm/processor.h>
  32. #include <asm/errno.h>
  33. #include <asm/io.h>
  34. #include <malloc.h>
  35. #include <scsi.h>
  36. #include <ata.h>
  37. #include <linux/ctype.h>
  38. #include <ahci.h>
  39. struct ahci_probe_ent *probe_ent = NULL;
  40. hd_driveid_t *ataid[AHCI_MAX_PORTS];
  41. #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
  42. static inline u32 ahci_port_base(u32 base, u32 port)
  43. {
  44. return base + 0x100 + (port * 0x80);
  45. }
  46. static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
  47. unsigned int port_idx)
  48. {
  49. base = ahci_port_base(base, port_idx);
  50. port->cmd_addr = base;
  51. port->scr_addr = base + PORT_SCR;
  52. }
  53. #define msleep(a) udelay(a * 1000)
  54. #define ssleep(a) msleep(a * 1000)
  55. static int waiting_for_cmd_completed(volatile u8 *offset,
  56. int timeout_msec,
  57. u32 sign)
  58. {
  59. int i;
  60. u32 status;
  61. for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
  62. msleep(1);
  63. return (i < timeout_msec) ? 0 : -1;
  64. }
  65. static int ahci_host_init(struct ahci_probe_ent *probe_ent)
  66. {
  67. pci_dev_t pdev = probe_ent->dev;
  68. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  69. u32 tmp, cap_save;
  70. u16 tmp16;
  71. int i, j;
  72. volatile u8 *port_mmio;
  73. unsigned short vendor;
  74. cap_save = readl(mmio + HOST_CAP);
  75. cap_save &= ((1 << 28) | (1 << 17));
  76. cap_save |= (1 << 27);
  77. /* global controller reset */
  78. tmp = readl(mmio + HOST_CTL);
  79. if ((tmp & HOST_RESET) == 0)
  80. writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
  81. /* reset must complete within 1 second, or
  82. * the hardware should be considered fried.
  83. */
  84. ssleep(1);
  85. tmp = readl(mmio + HOST_CTL);
  86. if (tmp & HOST_RESET) {
  87. debug("controller reset failed (0x%x)\n", tmp);
  88. return -1;
  89. }
  90. writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
  91. writel(cap_save, mmio + HOST_CAP);
  92. writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
  93. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  94. if (vendor == PCI_VENDOR_ID_INTEL) {
  95. u16 tmp16;
  96. pci_read_config_word(pdev, 0x92, &tmp16);
  97. tmp16 |= 0xf;
  98. pci_write_config_word(pdev, 0x92, tmp16);
  99. }
  100. probe_ent->cap = readl(mmio + HOST_CAP);
  101. probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
  102. probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
  103. debug("cap 0x%x port_map 0x%x n_ports %d\n",
  104. probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
  105. for (i = 0; i < probe_ent->n_ports; i++) {
  106. probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
  107. port_mmio = (u8 *) probe_ent->port[i].port_mmio;
  108. ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
  109. /* make sure port is not active */
  110. tmp = readl(port_mmio + PORT_CMD);
  111. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  112. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  113. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  114. PORT_CMD_FIS_RX | PORT_CMD_START);
  115. writel_with_flush(tmp, port_mmio + PORT_CMD);
  116. /* spec says 500 msecs for each bit, so
  117. * this is slightly incorrect.
  118. */
  119. msleep(500);
  120. }
  121. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  122. j = 0;
  123. while (j < 100) {
  124. msleep(10);
  125. tmp = readl(port_mmio + PORT_SCR_STAT);
  126. if ((tmp & 0xf) == 0x3)
  127. break;
  128. j++;
  129. }
  130. tmp = readl(port_mmio + PORT_SCR_ERR);
  131. debug("PORT_SCR_ERR 0x%x\n", tmp);
  132. writel(tmp, port_mmio + PORT_SCR_ERR);
  133. /* ack any pending irq events for this port */
  134. tmp = readl(port_mmio + PORT_IRQ_STAT);
  135. debug("PORT_IRQ_STAT 0x%x\n", tmp);
  136. if (tmp)
  137. writel(tmp, port_mmio + PORT_IRQ_STAT);
  138. writel(1 << i, mmio + HOST_IRQ_STAT);
  139. /* set irq mask (enables interrupts) */
  140. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  141. /*register linkup ports */
  142. tmp = readl(port_mmio + PORT_SCR_STAT);
  143. debug("Port %d status: 0x%x\n", i, tmp);
  144. if ((tmp & 0xf) == 0x03)
  145. probe_ent->link_port_map |= (0x01 << i);
  146. }
  147. tmp = readl(mmio + HOST_CTL);
  148. debug("HOST_CTL 0x%x\n", tmp);
  149. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  150. tmp = readl(mmio + HOST_CTL);
  151. debug("HOST_CTL 0x%x\n", tmp);
  152. pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
  153. tmp |= PCI_COMMAND_MASTER;
  154. pci_write_config_word(pdev, PCI_COMMAND, tmp16);
  155. return 0;
  156. }
  157. static void ahci_print_info(struct ahci_probe_ent *probe_ent)
  158. {
  159. pci_dev_t pdev = probe_ent->dev;
  160. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  161. u32 vers, cap, impl, speed;
  162. const char *speed_s;
  163. u16 cc;
  164. const char *scc_s;
  165. vers = readl(mmio + HOST_VERSION);
  166. cap = probe_ent->cap;
  167. impl = probe_ent->port_map;
  168. speed = (cap >> 20) & 0xf;
  169. if (speed == 1)
  170. speed_s = "1.5";
  171. else if (speed == 2)
  172. speed_s = "3";
  173. else
  174. speed_s = "?";
  175. pci_read_config_word(pdev, 0x0a, &cc);
  176. if (cc == 0x0101)
  177. scc_s = "IDE";
  178. else if (cc == 0x0106)
  179. scc_s = "SATA";
  180. else if (cc == 0x0104)
  181. scc_s = "RAID";
  182. else
  183. scc_s = "unknown";
  184. printf("AHCI %02x%02x.%02x%02x "
  185. "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
  186. (vers >> 24) & 0xff,
  187. (vers >> 16) & 0xff,
  188. (vers >> 8) & 0xff,
  189. vers & 0xff,
  190. ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
  191. printf("flags: "
  192. "%s%s%s%s%s%s"
  193. "%s%s%s%s%s%s%s\n",
  194. cap & (1 << 31) ? "64bit " : "",
  195. cap & (1 << 30) ? "ncq " : "",
  196. cap & (1 << 28) ? "ilck " : "",
  197. cap & (1 << 27) ? "stag " : "",
  198. cap & (1 << 26) ? "pm " : "",
  199. cap & (1 << 25) ? "led " : "",
  200. cap & (1 << 24) ? "clo " : "",
  201. cap & (1 << 19) ? "nz " : "",
  202. cap & (1 << 18) ? "only " : "",
  203. cap & (1 << 17) ? "pmp " : "",
  204. cap & (1 << 15) ? "pio " : "",
  205. cap & (1 << 14) ? "slum " : "",
  206. cap & (1 << 13) ? "part " : "");
  207. }
  208. static int ahci_init_one(pci_dev_t pdev)
  209. {
  210. u32 iobase;
  211. u16 vendor;
  212. int rc;
  213. memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
  214. probe_ent = malloc(sizeof(probe_ent));
  215. memset(probe_ent, 0, sizeof(probe_ent));
  216. probe_ent->dev = pdev;
  217. pci_read_config_dword(pdev, AHCI_PCI_BAR, &iobase);
  218. iobase &= ~0xf;
  219. probe_ent->host_flags = ATA_FLAG_SATA
  220. | ATA_FLAG_NO_LEGACY
  221. | ATA_FLAG_MMIO
  222. | ATA_FLAG_PIO_DMA
  223. | ATA_FLAG_NO_ATAPI;
  224. probe_ent->pio_mask = 0x1f;
  225. probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  226. probe_ent->mmio_base = iobase;
  227. /* Take from kernel:
  228. * JMicron-specific fixup:
  229. * make sure we're in AHCI mode
  230. */
  231. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  232. if (vendor == 0x197b)
  233. pci_write_config_byte(pdev, 0x41, 0xa1);
  234. /* initialize adapter */
  235. rc = ahci_host_init(probe_ent);
  236. if (rc)
  237. goto err_out;
  238. ahci_print_info(probe_ent);
  239. return 0;
  240. err_out:
  241. return rc;
  242. }
  243. #define MAX_DATA_BYTE_COUNT (4*1024*1024)
  244. static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
  245. {
  246. struct ahci_ioports *pp = &(probe_ent->port[port]);
  247. struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
  248. u32 sg_count;
  249. int i;
  250. sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
  251. if (sg_count > AHCI_MAX_SG) {
  252. printf("Error:Too much sg!\n");
  253. return -1;
  254. }
  255. for (i = 0; i < sg_count; i++) {
  256. ahci_sg->addr =
  257. cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
  258. ahci_sg->addr_hi = 0;
  259. ahci_sg->flags_size = cpu_to_le32(0x3fffff &
  260. (buf_len < MAX_DATA_BYTE_COUNT
  261. ? (buf_len - 1)
  262. : (MAX_DATA_BYTE_COUNT - 1)));
  263. ahci_sg++;
  264. buf_len -= MAX_DATA_BYTE_COUNT;
  265. }
  266. return sg_count;
  267. }
  268. static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
  269. {
  270. pp->cmd_slot->opts = cpu_to_le32(opts);
  271. pp->cmd_slot->status = 0;
  272. pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
  273. pp->cmd_slot->tbl_addr_hi = 0;
  274. }
  275. static void ahci_set_feature(u8 port)
  276. {
  277. struct ahci_ioports *pp = &(probe_ent->port[port]);
  278. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  279. u32 cmd_fis_len = 5; /* five dwords */
  280. u8 fis[20];
  281. /*set feature */
  282. memset(fis, 0, 20);
  283. fis[0] = 0x27;
  284. fis[1] = 1 << 7;
  285. fis[2] = ATA_CMD_SETF;
  286. fis[3] = SETFEATURES_XFER;
  287. fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
  288. memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
  289. ahci_fill_cmd_slot(pp, cmd_fis_len);
  290. writel(1, port_mmio + PORT_CMD_ISSUE);
  291. readl(port_mmio + PORT_CMD_ISSUE);
  292. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
  293. printf("set feature error!\n");
  294. }
  295. }
  296. static int ahci_port_start(u8 port)
  297. {
  298. struct ahci_ioports *pp = &(probe_ent->port[port]);
  299. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  300. u32 port_status;
  301. u32 mem;
  302. debug("Enter start port: %d\n", port);
  303. port_status = readl(port_mmio + PORT_SCR_STAT);
  304. debug("Port %d status: %x\n", port, port_status);
  305. if ((port_status & 0xf) != 0x03) {
  306. printf("No Link on this port!\n");
  307. return -1;
  308. }
  309. mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
  310. if (!mem) {
  311. free(pp);
  312. printf("No mem for table!\n");
  313. return -ENOMEM;
  314. }
  315. mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
  316. memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  317. /*
  318. * First item in chunk of DMA memory: 32-slot command table,
  319. * 32 bytes each in size
  320. */
  321. pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
  322. debug("cmd_slot = 0x%x\n", pp->cmd_slot);
  323. mem += (AHCI_CMD_SLOT_SZ + 224);
  324. /*
  325. * Second item: Received-FIS area
  326. */
  327. pp->rx_fis = mem;
  328. mem += AHCI_RX_FIS_SZ;
  329. /*
  330. * Third item: data area for storing a single command
  331. * and its scatter-gather table
  332. */
  333. pp->cmd_tbl = mem;
  334. debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
  335. mem += AHCI_CMD_TBL_HDR;
  336. pp->cmd_tbl_sg = (struct ahci_sg *)mem;
  337. writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
  338. writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
  339. writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  340. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  341. PORT_CMD_START, port_mmio + PORT_CMD);
  342. debug("Exit start port %d\n", port);
  343. return 0;
  344. }
  345. static int get_ahci_device_data(u8 port, u8 *fis, int fis_len, u8 *buf,
  346. int buf_len)
  347. {
  348. struct ahci_ioports *pp = &(probe_ent->port[port]);
  349. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  350. u32 opts;
  351. u32 port_status;
  352. int sg_count;
  353. debug("Enter get_ahci_device_data: for port %d\n", port);
  354. if (port > probe_ent->n_ports) {
  355. printf("Invaild port number %d\n", port);
  356. return -1;
  357. }
  358. port_status = readl(port_mmio + PORT_SCR_STAT);
  359. if ((port_status & 0xf) != 0x03) {
  360. debug("No Link on port %d!\n", port);
  361. return -1;
  362. }
  363. memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
  364. sg_count = ahci_fill_sg(port, buf, buf_len);
  365. opts = (fis_len >> 2) | (sg_count << 16);
  366. ahci_fill_cmd_slot(pp, opts);
  367. writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  368. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
  369. printf("timeout exit!\n");
  370. return -1;
  371. }
  372. debug("get_ahci_device_data: %d byte transferred.\n",
  373. pp->cmd_slot->status);
  374. return 0;
  375. }
  376. static char *ata_id_strcpy(u16 *target, u16 *src, int len)
  377. {
  378. int i;
  379. for (i = 0; i < len / 2; i++)
  380. target[i] = le16_to_cpu(src[i]);
  381. return (char *)target;
  382. }
  383. static void dump_ataid(hd_driveid_t *ataid)
  384. {
  385. debug("(49)ataid->capability = 0x%x\n", ataid->capability);
  386. debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
  387. debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
  388. debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
  389. debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
  390. debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
  391. debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
  392. debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
  393. debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
  394. debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
  395. debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
  396. debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
  397. debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
  398. debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
  399. debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
  400. }
  401. /*
  402. * SCSI INQUIRY command operation.
  403. */
  404. static int ata_scsiop_inquiry(ccb *pccb)
  405. {
  406. u8 hdr[] = {
  407. 0,
  408. 0,
  409. 0x5, /* claim SPC-3 version compatibility */
  410. 2,
  411. 95 - 4,
  412. };
  413. u8 fis[20];
  414. u8 *tmpid;
  415. u8 port;
  416. /* Clean ccb data buffer */
  417. memset(pccb->pdata, 0, pccb->datalen);
  418. memcpy(pccb->pdata, hdr, sizeof(hdr));
  419. if (pccb->datalen <= 35)
  420. return 0;
  421. memset(fis, 0, 20);
  422. /* Construct the FIS */
  423. fis[0] = 0x27; /* Host to device FIS. */
  424. fis[1] = 1 << 7; /* Command FIS. */
  425. fis[2] = ATA_CMD_IDENT; /* Command byte. */
  426. /* Read id from sata */
  427. port = pccb->target;
  428. if (!(tmpid = malloc(sizeof(hd_driveid_t))))
  429. return -ENOMEM;
  430. if (get_ahci_device_data(port, (u8 *) & fis, 20,
  431. tmpid, sizeof(hd_driveid_t))) {
  432. debug("scsi_ahci: SCSI inquiry command failure.\n");
  433. return -EIO;
  434. }
  435. if (ataid[port])
  436. free(ataid[port]);
  437. ataid[port] = (hd_driveid_t *) tmpid;
  438. memcpy(&pccb->pdata[8], "ATA ", 8);
  439. ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
  440. ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
  441. dump_ataid(ataid[port]);
  442. return 0;
  443. }
  444. /*
  445. * SCSI READ10 command operation.
  446. */
  447. static int ata_scsiop_read10(ccb * pccb)
  448. {
  449. u64 lba = 0;
  450. u32 len = 0;
  451. u8 fis[20];
  452. lba = (((u64) pccb->cmd[2]) << 24) | (((u64) pccb->cmd[3]) << 16)
  453. | (((u64) pccb->cmd[4]) << 8) | ((u64) pccb->cmd[5]);
  454. len = (((u32) pccb->cmd[7]) << 8) | ((u32) pccb->cmd[8]);
  455. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  456. * length 0 means transfer 0 block of data.
  457. * However, for ATA R/W commands, sector count 0 means
  458. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  459. *
  460. * WARNING: one or two older ATA drives treat 0 as 0...
  461. */
  462. if (!len)
  463. return 0;
  464. memset(fis, 0, 20);
  465. /* Construct the FIS */
  466. fis[0] = 0x27; /* Host to device FIS. */
  467. fis[1] = 1 << 7; /* Command FIS. */
  468. fis[2] = ATA_CMD_RD_DMA; /* Command byte. */
  469. /* LBA address, only support LBA28 in this driver */
  470. fis[4] = pccb->cmd[5];
  471. fis[5] = pccb->cmd[4];
  472. fis[6] = pccb->cmd[3];
  473. fis[7] = (pccb->cmd[2] & 0x0f) | 0xe0;
  474. /* Sector Count */
  475. fis[12] = pccb->cmd[8];
  476. fis[13] = pccb->cmd[7];
  477. /* Read from ahci */
  478. if (get_ahci_device_data(pccb->target, (u8 *) & fis, 20,
  479. pccb->pdata, pccb->datalen)) {
  480. debug("scsi_ahci: SCSI READ10 command failure.\n");
  481. return -EIO;
  482. }
  483. return 0;
  484. }
  485. /*
  486. * SCSI READ CAPACITY10 command operation.
  487. */
  488. static int ata_scsiop_read_capacity10(ccb *pccb)
  489. {
  490. u8 buf[8];
  491. if (!ataid[pccb->target]) {
  492. printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
  493. "\tNo ATA info!\n"
  494. "\tPlease run SCSI commmand INQUIRY firstly!\n");
  495. return -EPERM;
  496. }
  497. memset(buf, 0, 8);
  498. *(u32 *) buf = le32_to_cpu(ataid[pccb->target]->lba_capacity);
  499. buf[6] = 512 >> 8;
  500. buf[7] = 512 & 0xff;
  501. memcpy(pccb->pdata, buf, 8);
  502. return 0;
  503. }
  504. /*
  505. * SCSI TEST UNIT READY command operation.
  506. */
  507. static int ata_scsiop_test_unit_ready(ccb *pccb)
  508. {
  509. return (ataid[pccb->target]) ? 0 : -EPERM;
  510. }
  511. int scsi_exec(ccb *pccb)
  512. {
  513. int ret;
  514. switch (pccb->cmd[0]) {
  515. case SCSI_READ10:
  516. ret = ata_scsiop_read10(pccb);
  517. break;
  518. case SCSI_RD_CAPAC:
  519. ret = ata_scsiop_read_capacity10(pccb);
  520. break;
  521. case SCSI_TST_U_RDY:
  522. ret = ata_scsiop_test_unit_ready(pccb);
  523. break;
  524. case SCSI_INQUIRY:
  525. ret = ata_scsiop_inquiry(pccb);
  526. break;
  527. default:
  528. printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
  529. return FALSE;
  530. }
  531. if (ret) {
  532. debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
  533. return FALSE;
  534. }
  535. return TRUE;
  536. }
  537. void scsi_low_level_init(int busdevfunc)
  538. {
  539. int i;
  540. u32 linkmap;
  541. ahci_init_one(busdevfunc);
  542. linkmap = probe_ent->link_port_map;
  543. for (i = 0; i < CFG_SCSI_MAX_SCSI_ID; i++) {
  544. if (((linkmap >> i) & 0x01)) {
  545. if (ahci_port_start((u8) i)) {
  546. printf("Can not start port %d\n", i);
  547. continue;
  548. }
  549. ahci_set_feature((u8) i);
  550. }
  551. }
  552. }
  553. void scsi_bus_reset(void)
  554. {
  555. /*Not implement*/
  556. }
  557. void scsi_print_error(ccb * pccb)
  558. {
  559. /*The ahci error info can be read in the ahci driver*/
  560. }
  561. #endif