sata.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  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., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "isci.h"
  56. #include "remote_device.h"
  57. #include "scic_io_request.h"
  58. #include "scic_task_request.h"
  59. #include "task.h"
  60. #include "request.h"
  61. #include "sata.h"
  62. #include "intel_sat.h"
  63. #include "intel_ata.h"
  64. static u8 isci_sata_get_management_task_protocol(struct isci_tmf *tmf);
  65. /**
  66. * isci_sata_task_to_fis_copy() - This function gets the host_to_dev_fis from
  67. * the core and copies the fis from the task into it.
  68. * @task: This parameter is a pointer to the task struct from libsas.
  69. *
  70. * pointer to the host_to_dev_fis from the core request object.
  71. */
  72. struct host_to_dev_fis *isci_sata_task_to_fis_copy(struct sas_task *task)
  73. {
  74. struct isci_request *request = task->lldd_task;
  75. struct host_to_dev_fis *register_fis =
  76. scic_stp_io_request_get_h2d_reg_address(
  77. request->sci_request_handle
  78. );
  79. memcpy(
  80. (u8 *)register_fis,
  81. (u8 *)&task->ata_task.fis,
  82. sizeof(struct host_to_dev_fis)
  83. );
  84. if (!task->ata_task.device_control_reg_update)
  85. register_fis->flags |= 0x80;
  86. register_fis->flags &= 0xF0;
  87. return register_fis;
  88. }
  89. /**
  90. * isci_sata_is_task_ncq() - This function determines if the given stp task is
  91. * a ncq request.
  92. * @task: This parameter is a pointer to the task struct from libsas.
  93. *
  94. * true if the task is ncq
  95. */
  96. bool isci_sata_is_task_ncq(struct sas_task *task)
  97. {
  98. struct ata_queued_cmd *qc = task->uldd_task;
  99. bool ret = (qc &&
  100. (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
  101. qc->tf.command == ATA_CMD_FPDMA_READ));
  102. return ret;
  103. }
  104. /**
  105. * isci_sata_set_ncq_tag() - This function sets the ncq tag field in the
  106. * host_to_dev_fis equal to the tag in the queue command in the task.
  107. * @task: This parameter is a pointer to the task struct from libsas.
  108. * @register_fis: This parameter is a pointer to the host_to_dev_fis from the
  109. * core request object.
  110. *
  111. */
  112. void isci_sata_set_ncq_tag(
  113. struct host_to_dev_fis *register_fis,
  114. struct sas_task *task)
  115. {
  116. struct ata_queued_cmd *qc = task->uldd_task;
  117. struct isci_request *request = task->lldd_task;
  118. register_fis->sector_count = qc->tag << 3;
  119. scic_stp_io_request_set_ncq_tag(request->sci_request_handle, qc->tag);
  120. }
  121. /**
  122. * isci_request_process_stp_response() - This function sets the status and
  123. * response, in the task struct, from the request object for the upper layer
  124. * driver.
  125. * @sas_task: This parameter is the task struct from the upper layer driver.
  126. * @response_buffer: This parameter points to the response of the completed
  127. * request.
  128. *
  129. * none.
  130. */
  131. void isci_request_process_stp_response(
  132. struct sas_task *task,
  133. void *response_buffer)
  134. {
  135. struct sata_fis_reg_d2h *d2h_reg_fis = (struct sata_fis_reg_d2h *)response_buffer;
  136. struct task_status_struct *ts = &task->task_status;
  137. struct ata_task_resp *resp = (void *)&ts->buf[0];
  138. resp->frame_len = le16_to_cpu(*(__le16 *)(response_buffer + 6));
  139. memcpy(&resp->ending_fis[0], response_buffer + 16, 24);
  140. ts->buf_valid_size = sizeof(*resp);
  141. /**
  142. * If the device fault bit is set in the status register, then
  143. * set the sense data and return.
  144. */
  145. if (d2h_reg_fis->status & ATA_STATUS_REG_DEVICE_FAULT_BIT)
  146. ts->stat = SAS_PROTO_RESPONSE;
  147. else
  148. ts->stat = SAM_STAT_GOOD;
  149. ts->resp = SAS_TASK_COMPLETE;
  150. }
  151. /**
  152. * isci_sata_get_sat_protocol() - retrieve the sat protocol for the request
  153. * @isci_request: ata request
  154. *
  155. * Note: temporary implementation until expert mode removes the callback
  156. *
  157. */
  158. u8 isci_sata_get_sat_protocol(struct isci_request *isci_request)
  159. {
  160. struct sas_task *task;
  161. struct domain_device *dev;
  162. dev_dbg(&isci_request->isci_host->pdev->dev,
  163. "%s: isci_request = %p, ttype = %d\n",
  164. __func__, isci_request, isci_request->ttype);
  165. if (tmf_task == isci_request->ttype) {
  166. struct isci_tmf *tmf = isci_request_access_tmf(isci_request);
  167. return isci_sata_get_management_task_protocol(tmf);
  168. }
  169. task = isci_request_access_task(isci_request);
  170. dev = task->dev;
  171. if (!sas_protocol_ata(task->task_proto)) {
  172. WARN(1, "unhandled task protocol\n");
  173. return SAT_PROTOCOL_NON_DATA;
  174. }
  175. if (task->data_dir == DMA_NONE)
  176. return SAT_PROTOCOL_NON_DATA;
  177. /* the "_IN" protocol types are equivalent to their "_OUT"
  178. * analogs as far as the core is concerned
  179. */
  180. if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
  181. if (task->ata_task.dma_xfer)
  182. return SAT_PROTOCOL_PACKET_DMA_DATA_IN;
  183. else
  184. return SAT_PROTOCOL_PACKET_PIO_DATA_IN;
  185. }
  186. if (task->ata_task.use_ncq)
  187. return SAT_PROTOCOL_FPDMA;
  188. if (task->ata_task.dma_xfer)
  189. return SAT_PROTOCOL_UDMA_DATA_IN;
  190. else
  191. return SAT_PROTOCOL_PIO_DATA_IN;
  192. }
  193. static u8 isci_sata_get_management_task_protocol(
  194. struct isci_tmf *tmf)
  195. {
  196. u8 ret = 0;
  197. pr_warn("tmf = %p, func = %d\n", tmf, tmf->tmf_code);
  198. if ((tmf->tmf_code == isci_tmf_sata_srst_high) ||
  199. (tmf->tmf_code == isci_tmf_sata_srst_low)) {
  200. pr_warn("%s: tmf->tmf_code == TMF_LU_RESET\n", __func__);
  201. ret = SAT_PROTOCOL_SOFT_RESET;
  202. }
  203. return ret;
  204. }
  205. enum sci_status isci_sata_management_task_request_build(
  206. struct isci_request *isci_request)
  207. {
  208. struct isci_tmf *isci_tmf;
  209. enum sci_status status;
  210. if (tmf_task != isci_request->ttype)
  211. return SCI_FAILURE;
  212. isci_tmf = isci_request_access_tmf(isci_request);
  213. switch (isci_tmf->tmf_code) {
  214. case isci_tmf_sata_srst_high:
  215. case isci_tmf_sata_srst_low:
  216. {
  217. struct host_to_dev_fis *register_fis =
  218. scic_stp_io_request_get_h2d_reg_address(
  219. isci_request->sci_request_handle
  220. );
  221. memset(register_fis, 0, sizeof(*register_fis));
  222. register_fis->fis_type = 0x27;
  223. register_fis->flags &= ~0x80;
  224. register_fis->flags &= 0xF0;
  225. if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
  226. register_fis->control |= ATA_SRST;
  227. else
  228. register_fis->control &= ~ATA_SRST;
  229. break;
  230. }
  231. /* other management commnd go here... */
  232. default:
  233. return SCI_FAILURE;
  234. }
  235. /* core builds the protocol specific request
  236. * based on the h2d fis.
  237. */
  238. status = scic_task_request_construct_sata(
  239. isci_request->sci_request_handle
  240. );
  241. return status;
  242. }
  243. /**
  244. * isci_task_send_lu_reset_sata() - This function is called by of the SAS
  245. * Domain Template functions. This is one of the Task Management functoins
  246. * called by libsas, to reset the given SAS lun. Note the assumption that
  247. * while this call is executing, no I/O will be sent by the host to the
  248. * device.
  249. * @lun: This parameter specifies the lun to be reset.
  250. *
  251. * status, zero indicates success.
  252. */
  253. int isci_task_send_lu_reset_sata(
  254. struct isci_host *isci_host,
  255. struct isci_remote_device *isci_device,
  256. u8 *lun)
  257. {
  258. struct isci_tmf tmf;
  259. int ret = TMF_RESP_FUNC_FAILED;
  260. /* Send the soft reset to the target */
  261. #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
  262. isci_task_build_tmf(&tmf, isci_device, isci_tmf_sata_srst_high,
  263. NULL, NULL
  264. );
  265. ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_SRST_TIMEOUT_MS);
  266. if (ret != TMF_RESP_FUNC_COMPLETE) {
  267. dev_warn(&isci_host->pdev->dev,
  268. "%s: Assert SRST failed (%p) = %x",
  269. __func__,
  270. isci_device,
  271. ret);
  272. /* Return the failure so that the LUN reset is escalated
  273. * to a target reset.
  274. */
  275. }
  276. return ret;
  277. }