sas_ata.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 || stat->stat == SAM_GOOD) {
  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. } else if (stat->stat != SAM_STAT_GOOD) {
  95. ac = sas_to_ata_err(stat);
  96. if (ac) {
  97. SAS_DPRINTK("%s: SAS error %x\n", __FUNCTION__,
  98. stat->stat);
  99. /* We saw a SAS error. Send a vague error. */
  100. qc->err_mask = ac;
  101. dev->sata_dev.tf.feature = 0x04; /* status err */
  102. dev->sata_dev.tf.command = ATA_ERR;
  103. }
  104. }
  105. qc->lldd_task = NULL;
  106. ata_qc_complete(qc);
  107. spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
  108. qc_already_gone:
  109. list_del_init(&task->list);
  110. sas_free_task(task);
  111. }
  112. static unsigned int sas_ata_qc_issue(struct ata_queued_cmd *qc)
  113. {
  114. int res;
  115. struct sas_task *task;
  116. struct domain_device *dev = qc->ap->private_data;
  117. struct sas_ha_struct *sas_ha = dev->port->ha;
  118. struct Scsi_Host *host = sas_ha->core.shost;
  119. struct sas_internal *i = to_sas_internal(host->transportt);
  120. struct scatterlist *sg;
  121. unsigned int num = 0;
  122. unsigned int xfer = 0;
  123. task = sas_alloc_task(GFP_ATOMIC);
  124. if (!task)
  125. return AC_ERR_SYSTEM;
  126. task->dev = dev;
  127. task->task_proto = SAS_PROTOCOL_STP;
  128. task->task_done = sas_ata_task_done;
  129. if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  130. qc->tf.command == ATA_CMD_FPDMA_READ) {
  131. /* Need to zero out the tag libata assigned us */
  132. qc->tf.nsect = 0;
  133. }
  134. ata_tf_to_fis(&qc->tf, (u8*)&task->ata_task.fis, 0);
  135. task->uldd_task = qc;
  136. if (is_atapi_taskfile(&qc->tf)) {
  137. memcpy(task->ata_task.atapi_packet, qc->cdb, qc->dev->cdb_len);
  138. task->total_xfer_len = qc->nbytes + qc->pad_len;
  139. task->num_scatter = qc->pad_len ? qc->n_elem + 1 : qc->n_elem;
  140. } else {
  141. ata_for_each_sg(sg, qc) {
  142. num++;
  143. xfer += sg->length;
  144. }
  145. task->total_xfer_len = xfer;
  146. task->num_scatter = num;
  147. }
  148. task->data_dir = qc->dma_dir;
  149. task->scatter = qc->__sg;
  150. task->ata_task.retry_count = 1;
  151. task->task_state_flags = SAS_TASK_STATE_PENDING;
  152. qc->lldd_task = task;
  153. switch (qc->tf.protocol) {
  154. case ATA_PROT_NCQ:
  155. task->ata_task.use_ncq = 1;
  156. /* fall through */
  157. case ATA_PROT_ATAPI_DMA:
  158. case ATA_PROT_DMA:
  159. task->ata_task.dma_xfer = 1;
  160. break;
  161. }
  162. if (sas_ha->lldd_max_execute_num < 2)
  163. res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
  164. else
  165. res = sas_queue_up(task);
  166. /* Examine */
  167. if (res) {
  168. SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
  169. sas_free_task(task);
  170. return AC_ERR_SYSTEM;
  171. }
  172. return 0;
  173. }
  174. static u8 sas_ata_check_status(struct ata_port *ap)
  175. {
  176. struct domain_device *dev = ap->private_data;
  177. return dev->sata_dev.tf.command;
  178. }
  179. static void sas_ata_phy_reset(struct ata_port *ap)
  180. {
  181. struct domain_device *dev = ap->private_data;
  182. struct sas_internal *i =
  183. to_sas_internal(dev->port->ha->core.shost->transportt);
  184. int res = 0;
  185. if (i->dft->lldd_I_T_nexus_reset)
  186. res = i->dft->lldd_I_T_nexus_reset(dev);
  187. if (res)
  188. SAS_DPRINTK("%s: Unable to reset I T nexus?\n", __FUNCTION__);
  189. switch (dev->sata_dev.command_set) {
  190. case ATA_COMMAND_SET:
  191. SAS_DPRINTK("%s: Found ATA device.\n", __FUNCTION__);
  192. ap->device[0].class = ATA_DEV_ATA;
  193. break;
  194. case ATAPI_COMMAND_SET:
  195. SAS_DPRINTK("%s: Found ATAPI device.\n", __FUNCTION__);
  196. ap->device[0].class = ATA_DEV_ATAPI;
  197. break;
  198. default:
  199. SAS_DPRINTK("%s: Unknown SATA command set: %d.\n",
  200. __FUNCTION__,
  201. dev->sata_dev.command_set);
  202. ap->device[0].class = ATA_DEV_ATA;
  203. break;
  204. }
  205. ap->cbl = ATA_CBL_SATA;
  206. }
  207. static void sas_ata_post_internal(struct ata_queued_cmd *qc)
  208. {
  209. if (qc->flags & ATA_QCFLAG_FAILED)
  210. qc->err_mask |= AC_ERR_OTHER;
  211. if (qc->err_mask) {
  212. /*
  213. * Find the sas_task and kill it. By this point,
  214. * libata has decided to kill the qc, so we needn't
  215. * bother with sas_ata_task_done. But we still
  216. * ought to abort the task.
  217. */
  218. struct sas_task *task = qc->lldd_task;
  219. struct domain_device *dev = qc->ap->private_data;
  220. qc->lldd_task = NULL;
  221. if (task) {
  222. task->uldd_task = NULL;
  223. __sas_task_abort(task);
  224. }
  225. sas_phy_reset(dev->port->phy, 1);
  226. }
  227. }
  228. static void sas_ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  229. {
  230. struct domain_device *dev = ap->private_data;
  231. memcpy(tf, &dev->sata_dev.tf, sizeof (*tf));
  232. }
  233. static void sas_ata_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  234. u32 val)
  235. {
  236. struct domain_device *dev = ap->private_data;
  237. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  238. switch (sc_reg_in) {
  239. case SCR_STATUS:
  240. dev->sata_dev.sstatus = val;
  241. break;
  242. case SCR_CONTROL:
  243. dev->sata_dev.scontrol = val;
  244. break;
  245. case SCR_ERROR:
  246. dev->sata_dev.serror = val;
  247. break;
  248. case SCR_ACTIVE:
  249. dev->sata_dev.ap->sactive = val;
  250. break;
  251. }
  252. }
  253. static u32 sas_ata_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  254. {
  255. struct domain_device *dev = ap->private_data;
  256. SAS_DPRINTK("STUB %s\n", __FUNCTION__);
  257. switch (sc_reg_in) {
  258. case SCR_STATUS:
  259. return dev->sata_dev.sstatus;
  260. case SCR_CONTROL:
  261. return dev->sata_dev.scontrol;
  262. case SCR_ERROR:
  263. return dev->sata_dev.serror;
  264. case SCR_ACTIVE:
  265. return dev->sata_dev.ap->sactive;
  266. default:
  267. return 0xffffffffU;
  268. }
  269. }
  270. static struct ata_port_operations sas_sata_ops = {
  271. .port_disable = ata_port_disable,
  272. .check_status = sas_ata_check_status,
  273. .check_altstatus = sas_ata_check_status,
  274. .dev_select = ata_noop_dev_select,
  275. .phy_reset = sas_ata_phy_reset,
  276. .post_internal_cmd = sas_ata_post_internal,
  277. .tf_read = sas_ata_tf_read,
  278. .qc_prep = ata_noop_qc_prep,
  279. .qc_issue = sas_ata_qc_issue,
  280. .port_start = ata_sas_port_start,
  281. .port_stop = ata_sas_port_stop,
  282. .scr_read = sas_ata_scr_read,
  283. .scr_write = sas_ata_scr_write
  284. };
  285. static struct ata_port_info sata_port_info = {
  286. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  287. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA | ATA_FLAG_NCQ,
  288. .pio_mask = 0x1f, /* PIO0-4 */
  289. .mwdma_mask = 0x07, /* MWDMA0-2 */
  290. .udma_mask = ATA_UDMA6,
  291. .port_ops = &sas_sata_ops
  292. };
  293. int sas_ata_init_host_and_port(struct domain_device *found_dev,
  294. struct scsi_target *starget)
  295. {
  296. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  297. struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
  298. struct ata_port *ap;
  299. ata_host_init(&found_dev->sata_dev.ata_host,
  300. &ha->pcidev->dev,
  301. sata_port_info.flags,
  302. &sas_sata_ops);
  303. ap = ata_sas_port_alloc(&found_dev->sata_dev.ata_host,
  304. &sata_port_info,
  305. shost);
  306. if (!ap) {
  307. SAS_DPRINTK("ata_sas_port_alloc failed.\n");
  308. return -ENODEV;
  309. }
  310. ap->private_data = found_dev;
  311. ap->cbl = ATA_CBL_SATA;
  312. ap->scsi_host = shost;
  313. found_dev->sata_dev.ap = ap;
  314. return 0;
  315. }