sas_ata.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Support for SATA devices on Serial Attached SCSI (SAS) controllers
  3. *
  4. * Copyright (C) 2006 IBM Corporation
  5. *
  6. * Written by: Darrick J. Wong <djwong@us.ibm.com>, IBM Corporation
  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 the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * 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, MA 02111-1307
  21. * USA
  22. */
  23. #include <scsi/sas_ata.h>
  24. #include "sas_internal.h"
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_tcq.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_transport.h>
  30. #include <scsi/scsi_transport_sas.h>
  31. #include "../scsi_sas_internal.h"
  32. static enum ata_completion_errors sas_to_ata_err(struct task_status_struct *ts)
  33. {
  34. /* Cheesy attempt to translate SAS errors into ATA. Hah! */
  35. /* transport error */
  36. if (ts->resp == SAS_TASK_UNDELIVERED)
  37. return AC_ERR_ATA_BUS;
  38. /* ts->resp == SAS_TASK_COMPLETE */
  39. /* task delivered, what happened afterwards? */
  40. switch (ts->stat) {
  41. case SAS_DEV_NO_RESPONSE:
  42. return AC_ERR_TIMEOUT;
  43. case SAS_INTERRUPTED:
  44. case SAS_PHY_DOWN:
  45. case SAS_NAK_R_ERR:
  46. return AC_ERR_ATA_BUS;
  47. case SAS_DATA_UNDERRUN:
  48. /*
  49. * Some programs that use the taskfile interface
  50. * (smartctl in particular) can cause underrun
  51. * problems. Ignore these errors, perhaps at our
  52. * peril.
  53. */
  54. return 0;
  55. case SAS_DATA_OVERRUN:
  56. case SAS_QUEUE_FULL:
  57. case SAS_DEVICE_UNKNOWN:
  58. case SAS_SG_ERR:
  59. return AC_ERR_INVALID;
  60. case SAM_CHECK_COND:
  61. case SAS_OPEN_TO:
  62. case SAS_OPEN_REJECT:
  63. SAS_DPRINTK("%s: Saw error %d. What to do?\n",
  64. __FUNCTION__, ts->stat);
  65. return AC_ERR_OTHER;
  66. case SAS_ABORTED_TASK:
  67. return AC_ERR_DEV;
  68. case SAS_PROTO_RESPONSE:
  69. /* This means the ending_fis has the error
  70. * value; return 0 here to collect it */
  71. return 0;
  72. default:
  73. return 0;
  74. }
  75. }
  76. static void sas_ata_task_done(struct sas_task *task)
  77. {
  78. struct ata_queued_cmd *qc = task->uldd_task;
  79. struct domain_device *dev;
  80. struct task_status_struct *stat = &task->task_status;
  81. struct ata_task_resp *resp = (struct ata_task_resp *)stat->buf;
  82. enum ata_completion_errors ac;
  83. unsigned long flags;
  84. if (!qc)
  85. goto qc_already_gone;
  86. dev = qc->ap->private_data;
  87. spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
  88. if (stat->stat == SAS_PROTO_RESPONSE) {
  89. ata_tf_from_fis(resp->ending_fis, &dev->sata_dev.tf);
  90. qc->err_mask |= ac_err_mask(dev->sata_dev.tf.command);
  91. dev->sata_dev.sstatus = resp->sstatus;
  92. dev->sata_dev.serror = resp->serror;
  93. dev->sata_dev.scontrol = resp->scontrol;
  94. dev->sata_dev.ap->sactive = resp->sactive;
  95. } else if (stat->stat != SAM_STAT_GOOD) {
  96. ac = sas_to_ata_err(stat);
  97. if (ac) {
  98. SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
  99. stat->stat);
  100. /* We saw a SAS error. Send a vague error. */
  101. qc->err_mask = ac;
  102. dev->sata_dev.tf.feature = 0x04; /* status err */
  103. dev->sata_dev.tf.command = ATA_ERR;
  104. }
  105. }
  106. qc->lldd_task = NULL;
  107. ata_qc_complete(qc);
  108. spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
  109. qc_already_gone:
  110. list_del_init(&task->list);
  111. sas_free_task(task);
  112. }
  113. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  114. {
  115. int res;
  116. struct sas_task *task;
  117. struct domain_device *dev = qc->ap->private_data;
  118. struct sas_ha_struct *sas_ha = dev->port->ha;
  119. struct Scsi_Host *host = sas_ha->core.shost;
  120. struct sas_internal *i = to_sas_internal(host->transportt);
  121. struct scatterlist *sg;
  122. unsigned int num = 0;
  123. unsigned int xfer = 0;
  124. task = sas_alloc_task(GFP_ATOMIC);
  125. if (!task)
  126. return AC_ERR_SYSTEM;
  127. task->dev = dev;
  128. task->task_proto = SAS_PROTOCOL_STP;
  129. task->task_done = sas_ata_task_done;
  130. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  131. qc->tf.command == ATA_CMD_FPDMA_READ) {
  132. /* Need to zero out the tag libata assigned us */
  133. qc->tf.nsect = 0;
  134. }
  135. ata_tf_to_fis(&qc->tf, (u8*)&task->ata_task.fis, 0);
  136. task->uldd_task = qc;
  137. if (is_atapi_taskfile(&qc->tf)) {
  138. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  139. task->total_xfer_len = qc->nbytes + qc->pad_len;
  140. task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
  141. } else {
  142. ata_for_each_sg(sg, qc) {
  143. num++;
  144. xfer += sg->length;
  145. }
  146. task->total_xfer_len = xfer;
  147. task->num_scatter = num;
  148. }
  149. task->data_dir = qc->dma_dir;
  150. task->scatter = qc->__sg;
  151. task->ata_task.retry_count = 1;
  152. task->task_state_flags = SAS_TASK_STATE_PENDING;
  153. qc->lldd_task = task;
  154. switch (qc->tf.protocol) {
  155. case ATA_PROT_NCQ:
  156. task->ata_task.use_ncq = 1;
  157. /* fall through */
  158. case ATA_PROT_ATAPI_DMA:
  159. case ATA_PROT_DMA:
  160. task->ata_task.dma_xfer = 1;
  161. break;
  162. }
  163. if (sas_ha->lldd_max_execute_num < 2)
  164. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  165. else
  166. res = sas_queue_up(task);
  167. /* Examine */
  168. if (res) {
  169. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  170. sas_free_task(task);
  171. return AC_ERR_SYSTEM;
  172. }
  173. return 0;
  174. }
  175. static u8 sas_ata_check_status(struct ata_port *ap)
  176. {
  177. struct domain_device *dev = ap->private_data;
  178. return dev->sata_dev.tf.command;
  179. }
  180. static void sas_ata_phy_reset(struct ata_port *ap)
  181. {
  182. struct domain_device *dev = ap->private_data;
  183. struct sas_internal *i =
  184. to_sas_internal(dev->port->ha->core.shost->transportt);
  185. int res = 0;
  186. if (i->dft->lldd_I_T_nexus_reset)
  187. res = i->dft->lldd_I_T_nexus_reset(dev);
  188. if (res)
  189. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
  190. switch (dev->sata_dev.command_set) {
  191. case ATA_COMMAND_SET:
  192. SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
  193. ap->device[0].class = ATA_DEV_ATA;
  194. break;
  195. case ATAPI_COMMAND_SET:
  196. SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
  197. ap->device[0].class = ATA_DEV_ATAPI;
  198. break;
  199. default:
  200. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  201. __FUNCTION__,
  202. dev->sata_dev.command_set);
  203. ap->device[0].class = ATA_DEV_ATA;
  204. break;
  205. }
  206. ap->cbl = ATA_CBL_SATA;
  207. }
  208. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  209. {
  210. if (qc->flags & ATA_QCFLAG_FAILED)
  211. qc->err_mask |= AC_ERR_OTHER;
  212. if (qc->err_mask) {
  213. /*
  214. * Find the sas_task and kill it. By this point,
  215. * libata has decided to kill the qc, so we needn't
  216. * bother with sas_ata_task_done. But we still
  217. * ought to abort the task.
  218. */
  219. struct sas_task *task = qc->lldd_task;
  220. struct domain_device *dev = qc->ap->private_data;
  221. qc->lldd_task = NULL;
  222. if (task) {
  223. task->uldd_task = NULL;
  224. __sas_task_abort(task);
  225. }
  226. sas_phy_reset(dev->port->phy, 1);
  227. }
  228. }
  229. static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  230. {
  231. struct domain_device *dev = ap->private_data;
  232. memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
  233. }
  234. static void sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  235. u32 val)
  236. {
  237. struct domain_device *dev = ap->private_data;
  238. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  239. switch (sc_reg_in) {
  240. case SCR_STATUS:
  241. dev->sata_dev.sstatus = val;
  242. break;
  243. case SCR_CONTROL:
  244. dev->sata_dev.scontrol = val;
  245. break;
  246. case SCR_ERROR:
  247. dev->sata_dev.serror = val;
  248. break;
  249. case SCR_ACTIVE:
  250. dev->sata_dev.ap->sactive = val;
  251. break;
  252. }
  253. }
  254. static u32 sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  255. {
  256. struct domain_device *dev = ap->private_data;
  257. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  258. switch (sc_reg_in) {
  259. case SCR_STATUS:
  260. return dev->sata_dev.sstatus;
  261. case SCR_CONTROL:
  262. return dev->sata_dev.scontrol;
  263. case SCR_ERROR:
  264. return dev->sata_dev.serror;
  265. case SCR_ACTIVE:
  266. return dev->sata_dev.ap->sactive;
  267. default:
  268. return 0xffffffffU;
  269. }
  270. }
  271. static struct ata_port_operations sas_sata_ops = {
  272. .port_disable = ata_port_disable,
  273. .check_status = sas_ata_check_status,
  274. .check_altstatus = sas_ata_check_status,
  275. .dev_select = ata_noop_dev_select,
  276. .phy_reset = sas_ata_phy_reset,
  277. .post_internal_cmd = sas_ata_post_internal,
  278. .tf_read = sas_ata_tf_read,
  279. .qc_prep = ata_noop_qc_prep,
  280. .qc_issue = sas_ata_qc_issue,
  281. .port_start = ata_sas_port_start,
  282. .port_stop = ata_sas_port_stop,
  283. .scr_read = sas_ata_scr_read,
  284. .scr_write = sas_ata_scr_write
  285. };
  286. static struct ata_port_info sata_port_info = {
  287. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  288. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  289. .pio_mask = 0x1f, /* PIO0-4 */
  290. .mwdma_mask = 0x07, /* MWDMA0-2 */
  291. .udma_mask = ATA_UDMA6,
  292. .port_ops = &sas_sata_ops
  293. };
  294. int sas_ata_init_host_and_port(struct domain_device *found_dev,
  295. struct scsi_target *starget)
  296. {
  297. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  298. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  299. struct ata_port *ap;
  300. ata_host_init(&found_dev->sata_dev.ata_host,
  301. &ha->pcidev->dev,
  302. sata_port_info.flags,
  303. &sas_sata_ops);
  304. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  305. &sata_port_info,
  306. shost);
  307. if (!ap) {
  308. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  309. return -ENODEV;
  310. }
  311. ap->private_data = found_dev;
  312. ap->cbl = ATA_CBL_SATA;
  313. ap->scsi_host = shost;
  314. found_dev->sata_dev.ap = ap;
  315. return 0;
  316. }