08374505832e0311e587b168928250d12e6ade95
[linux-2.6-microblaze.git] / drivers / scsi / isci / sata.c
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
56 #include <scsi/sas.h>
57 #include "isci.h"
58 #include "remote_device.h"
59 #include "scic_io_request.h"
60 #include "scic_task_request.h"
61 #include "task.h"
62 #include "request.h"
63 #include "sata.h"
64 #include "intel_sat.h"
65 #include "intel_ata.h"
66
67 static u8 isci_sata_get_management_task_protocol(struct isci_tmf *tmf);
68
69
70 /**
71  * isci_sata_task_to_fis_copy() - This function gets the host_to_dev_fis from
72  *    the core and copies the fis from the task into it.
73  * @task: This parameter is a pointer to the task struct from libsas.
74  *
75  * pointer to the host_to_dev_fis from the core request object.
76  */
77 struct host_to_dev_fis *isci_sata_task_to_fis_copy(struct sas_task *task)
78 {
79         struct isci_request *request = task->lldd_task;
80         struct host_to_dev_fis *register_fis =
81                 scic_stp_io_request_get_h2d_reg_address(
82                         request->sci_request_handle
83                         );
84
85         memcpy(
86                 (u8 *)register_fis,
87                 (u8 *)&task->ata_task.fis,
88                 sizeof(struct host_to_dev_fis)
89                 );
90
91         if (!task->ata_task.device_control_reg_update)
92                 register_fis->flags |= 0x80;
93
94         register_fis->flags &= 0xF0;
95
96         return register_fis;
97 }
98
99 /**
100  * isci_sata_is_task_ncq() - This function determines if the given stp task is
101  *    a ncq request.
102  * @task: This parameter is a pointer to the task struct from libsas.
103  *
104  * true if the task is ncq
105  */
106 bool isci_sata_is_task_ncq(struct sas_task *task)
107 {
108         struct ata_queued_cmd *qc = task->uldd_task;
109
110         bool ret = (qc &&
111                     (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
112                      qc->tf.command == ATA_CMD_FPDMA_READ));
113
114         return ret;
115 }
116
117 /**
118  * isci_sata_set_ncq_tag() - This function sets the ncq tag field in the
119  *    host_to_dev_fis equal to the tag in the queue command in the task.
120  * @task: This parameter is a pointer to the task struct from libsas.
121  * @register_fis: This parameter is a pointer to the host_to_dev_fis from the
122  *    core request object.
123  *
124  */
125 void isci_sata_set_ncq_tag(
126         struct host_to_dev_fis *register_fis,
127         struct sas_task *task)
128 {
129         struct ata_queued_cmd *qc = task->uldd_task;
130         struct isci_request *request = task->lldd_task;
131
132         register_fis->sector_count = qc->tag << 3;
133         scic_stp_io_request_set_ncq_tag(request->sci_request_handle, qc->tag);
134 }
135
136 /**
137  * isci_request_process_stp_response() - This function sets the status and
138  *    response, in the task struct, from the request object for the upper layer
139  *    driver.
140  * @sas_task: This parameter is the task struct from the upper layer driver.
141  * @response_buffer: This parameter points to the response of the completed
142  *    request.
143  *
144  * none.
145  */
146 void isci_request_process_stp_response(struct sas_task *task,
147                                        void *response_buffer)
148 {
149         struct dev_to_host_fis *d2h_reg_fis = response_buffer;
150         struct task_status_struct *ts = &task->task_status;
151         struct ata_task_resp *resp = (void *)&ts->buf[0];
152
153         resp->frame_len = le16_to_cpu(*(__le16 *)(response_buffer + 6));
154         memcpy(&resp->ending_fis[0], response_buffer + 16, 24);
155         ts->buf_valid_size = sizeof(*resp);
156
157         /**
158          * If the device fault bit is set in the status register, then
159          * set the sense data and return.
160          */
161         if (d2h_reg_fis->status & ATA_STATUS_REG_DEVICE_FAULT_BIT)
162                 ts->stat = SAS_PROTO_RESPONSE;
163         else
164                 ts->stat = SAM_STAT_GOOD;
165
166         ts->resp = SAS_TASK_COMPLETE;
167 }
168
169 /**
170  * isci_sata_get_sat_protocol() - retrieve the sat protocol for the request
171  * @isci_request: ata request
172  *
173  * Note: temporary implementation until expert mode removes the callback
174  *
175  */
176 u8 isci_sata_get_sat_protocol(struct isci_request *isci_request)
177 {
178         struct sas_task *task;
179         struct domain_device *dev;
180
181         dev_dbg(&isci_request->isci_host->pdev->dev,
182                 "%s: isci_request = %p, ttype = %d\n",
183                 __func__, isci_request, isci_request->ttype);
184
185         if (tmf_task == isci_request->ttype) {
186                 struct isci_tmf *tmf = isci_request_access_tmf(isci_request);
187
188                 return isci_sata_get_management_task_protocol(tmf);
189         }
190
191         task = isci_request_access_task(isci_request);
192         dev = task->dev;
193
194         if (!sas_protocol_ata(task->task_proto)) {
195                 WARN(1, "unhandled task protocol\n");
196                 return SAT_PROTOCOL_NON_DATA;
197         }
198
199         if (task->data_dir == DMA_NONE)
200                 return SAT_PROTOCOL_NON_DATA;
201
202         /* the "_IN" protocol types are equivalent to their "_OUT"
203          * analogs as far as the core is concerned
204          */
205         if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
206                 if (task->ata_task.dma_xfer)
207                         return SAT_PROTOCOL_PACKET_DMA_DATA_IN;
208                 else
209                         return SAT_PROTOCOL_PACKET_PIO_DATA_IN;
210         }
211
212         if (task->ata_task.use_ncq)
213                 return SAT_PROTOCOL_FPDMA;
214
215         if (task->ata_task.dma_xfer)
216                 return SAT_PROTOCOL_UDMA_DATA_IN;
217         else
218                 return SAT_PROTOCOL_PIO_DATA_IN;
219 }
220
221 static u8 isci_sata_get_management_task_protocol(
222         struct isci_tmf *tmf)
223 {
224         u8 ret = 0;
225
226         pr_warn("tmf = %p, func = %d\n", tmf, tmf->tmf_code);
227
228         if ((tmf->tmf_code == isci_tmf_sata_srst_high) ||
229             (tmf->tmf_code == isci_tmf_sata_srst_low)) {
230                 pr_warn("%s: tmf->tmf_code == TMF_LU_RESET\n", __func__);
231                 ret = SAT_PROTOCOL_SOFT_RESET;
232         }
233
234         return ret;
235 }
236
237 enum sci_status isci_sata_management_task_request_build(
238         struct isci_request *isci_request)
239 {
240         struct isci_tmf *isci_tmf;
241         enum sci_status status;
242
243         if (tmf_task != isci_request->ttype)
244                 return SCI_FAILURE;
245
246         isci_tmf = isci_request_access_tmf(isci_request);
247
248         switch (isci_tmf->tmf_code) {
249
250         case isci_tmf_sata_srst_high:
251         case isci_tmf_sata_srst_low:
252         {
253                 struct host_to_dev_fis *register_fis =
254                         scic_stp_io_request_get_h2d_reg_address(
255                                 isci_request->sci_request_handle
256                                 );
257
258                 memset(register_fis, 0, sizeof(*register_fis));
259
260                 register_fis->fis_type  =  0x27;
261                 register_fis->flags     &= ~0x80;
262                 register_fis->flags     &= 0xF0;
263                 if (isci_tmf->tmf_code == isci_tmf_sata_srst_high)
264                         register_fis->control |= ATA_SRST;
265                 else
266                         register_fis->control &= ~ATA_SRST;
267                 break;
268         }
269         /* other management commnd go here... */
270         default:
271                 return SCI_FAILURE;
272         }
273
274         /* core builds the protocol specific request
275          *  based on the h2d fis.
276          */
277         status = scic_task_request_construct_sata(
278                 isci_request->sci_request_handle
279                 );
280
281         return status;
282 }
283
284 /**
285  * isci_task_send_lu_reset_sata() - This function is called by of the SAS
286  *    Domain Template functions. This is one of the Task Management functoins
287  *    called by libsas, to reset the given SAS lun. Note the assumption that
288  *    while this call is executing, no I/O will be sent by the host to the
289  *    device.
290  * @lun: This parameter specifies the lun to be reset.
291  *
292  * status, zero indicates success.
293  */
294 int isci_task_send_lu_reset_sata(
295         struct isci_host *isci_host,
296         struct isci_remote_device *isci_device,
297         u8 *lun)
298 {
299         struct isci_tmf tmf;
300         int ret = TMF_RESP_FUNC_FAILED;
301
302         /* Send the soft reset to the target */
303         #define ISCI_SRST_TIMEOUT_MS 25000 /* 25 second timeout. */
304         isci_task_build_tmf(&tmf, isci_device, isci_tmf_sata_srst_high,
305                             NULL, NULL
306                             );
307
308         ret = isci_task_execute_tmf(isci_host, &tmf, ISCI_SRST_TIMEOUT_MS);
309
310         if (ret != TMF_RESP_FUNC_COMPLETE) {
311                 dev_warn(&isci_host->pdev->dev,
312                          "%s: Assert SRST failed (%p) = %x",
313                          __func__,
314                          isci_device,
315                          ret);
316
317                 /* Return the failure so that the LUN reset is escalated
318                  * to a target reset.
319                  */
320         }
321         return ret;
322 }