blob: 1f314d0d71d58c481c559f2c787ce277a93bd72e [file] [log] [blame]
Dan Williams6f231dd2011-07-02 22:56:22 -07001/*
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
Dave Jiang3d2d7522012-02-10 01:18:34 -080056#include <scsi/scsi_cmnd.h>
Dan Williams6f231dd2011-07-02 22:56:22 -070057#include "isci.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070058#include "task.h"
59#include "request.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070060#include "scu_completion_codes.h"
Dan Williams5dec6f42011-05-10 02:28:49 -070061#include "scu_event_codes.h"
Dave Jiang2ec53eb2011-05-04 18:01:22 -070062#include "sas.h"
Dan Williams6f231dd2011-07-02 22:56:22 -070063
Dan Williamsd7a0ccd2012-02-10 01:18:44 -080064#undef C
65#define C(a) (#a)
66const char *req_state_name(enum sci_base_request_states state)
67{
68 static const char * const strings[] = REQUEST_STATES;
69
70 return strings[state];
71}
72#undef C
73
Dan Williams5076a1a2011-06-27 14:57:03 -070074static struct scu_sgl_element_pair *to_sgl_element_pair(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -070075 int idx)
76{
77 if (idx == 0)
Dan Williams5076a1a2011-06-27 14:57:03 -070078 return &ireq->tc->sgl_pair_ab;
Dan Williams312e0c22011-06-28 13:47:09 -070079 else if (idx == 1)
Dan Williams5076a1a2011-06-27 14:57:03 -070080 return &ireq->tc->sgl_pair_cd;
Dan Williams312e0c22011-06-28 13:47:09 -070081 else if (idx < 0)
82 return NULL;
83 else
Dan Williams5076a1a2011-06-27 14:57:03 -070084 return &ireq->sg_table[idx - 2];
Dan Williams6f231dd2011-07-02 22:56:22 -070085}
86
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070087static dma_addr_t to_sgl_element_pair_dma(struct isci_host *ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -070088 struct isci_request *ireq, u32 idx)
Dan Williams312e0c22011-06-28 13:47:09 -070089{
90 u32 offset;
91
92 if (idx == 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -070093 offset = (void *) &ireq->tc->sgl_pair_ab -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070094 (void *) &ihost->task_context_table[0];
Dan Williamsabec9122012-02-15 13:58:42 -080095 return ihost->tc_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -070096 } else if (idx == 1) {
Dan Williams5076a1a2011-06-27 14:57:03 -070097 offset = (void *) &ireq->tc->sgl_pair_cd -
Dan Williamsd9dcb4b2011-06-30 17:38:32 -070098 (void *) &ihost->task_context_table[0];
Dan Williamsabec9122012-02-15 13:58:42 -080099 return ihost->tc_dma + offset;
Dan Williams312e0c22011-06-28 13:47:09 -0700100 }
101
Dan Williams89a73012011-06-30 19:14:33 -0700102 return sci_io_request_get_dma_addr(ireq, &ireq->sg_table[idx - 2]);
Dan Williams312e0c22011-06-28 13:47:09 -0700103}
104
105static void init_sgl_element(struct scu_sgl_element *e, struct scatterlist *sg)
106{
107 e->length = sg_dma_len(sg);
108 e->address_upper = upper_32_bits(sg_dma_address(sg));
109 e->address_lower = lower_32_bits(sg_dma_address(sg));
110 e->address_modifier = 0;
111}
112
Dan Williams89a73012011-06-30 19:14:33 -0700113static void sci_request_build_sgl(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700114{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700115 struct isci_host *ihost = ireq->isci_host;
Dan Williams5076a1a2011-06-27 14:57:03 -0700116 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700117 struct scatterlist *sg = NULL;
118 dma_addr_t dma_addr;
119 u32 sg_idx = 0;
120 struct scu_sgl_element_pair *scu_sg = NULL;
121 struct scu_sgl_element_pair *prev_sg = NULL;
122
123 if (task->num_scatter > 0) {
124 sg = task->scatter;
125
126 while (sg) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700127 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williams312e0c22011-06-28 13:47:09 -0700128 init_sgl_element(&scu_sg->A, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700129 sg = sg_next(sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700130 if (sg) {
Dan Williams312e0c22011-06-28 13:47:09 -0700131 init_sgl_element(&scu_sg->B, sg);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700132 sg = sg_next(sg);
133 } else
Dan Williams312e0c22011-06-28 13:47:09 -0700134 memset(&scu_sg->B, 0, sizeof(scu_sg->B));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700135
136 if (prev_sg) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700137 dma_addr = to_sgl_element_pair_dma(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700138 ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700139 sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700140
141 prev_sg->next_pair_upper =
142 upper_32_bits(dma_addr);
143 prev_sg->next_pair_lower =
144 lower_32_bits(dma_addr);
145 }
146
147 prev_sg = scu_sg;
148 sg_idx++;
149 }
150 } else { /* handle when no sg */
Dan Williams5076a1a2011-06-27 14:57:03 -0700151 scu_sg = to_sgl_element_pair(ireq, sg_idx);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700152
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700153 dma_addr = dma_map_single(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700154 task->scatter,
155 task->total_xfer_len,
156 task->data_dir);
157
Dan Williams5076a1a2011-06-27 14:57:03 -0700158 ireq->zero_scatter_daddr = dma_addr;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700159
160 scu_sg->A.length = task->total_xfer_len;
161 scu_sg->A.address_upper = upper_32_bits(dma_addr);
162 scu_sg->A.address_lower = lower_32_bits(dma_addr);
163 }
164
165 if (scu_sg) {
166 scu_sg->next_pair_upper = 0;
167 scu_sg->next_pair_lower = 0;
168 }
169}
170
Dan Williams89a73012011-06-30 19:14:33 -0700171static void sci_io_request_build_ssp_command_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700172{
173 struct ssp_cmd_iu *cmd_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700174 struct sas_task *task = isci_request_access_task(ireq);
175
Dan Williams5076a1a2011-06-27 14:57:03 -0700176 cmd_iu = &ireq->ssp.cmd;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700177
178 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
179 cmd_iu->add_cdb_len = 0;
180 cmd_iu->_r_a = 0;
181 cmd_iu->_r_b = 0;
182 cmd_iu->en_fburst = 0; /* unsupported */
183 cmd_iu->task_prio = task->ssp_task.task_prio;
184 cmd_iu->task_attr = task->ssp_task.task_attr;
185 cmd_iu->_r_c = 0;
186
187 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
188 sizeof(task->ssp_task.cdb) / sizeof(u32));
189}
190
Dan Williams89a73012011-06-30 19:14:33 -0700191static void sci_task_request_build_ssp_task_iu(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700192{
193 struct ssp_task_iu *task_iu;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700194 struct sas_task *task = isci_request_access_task(ireq);
195 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
196
Dan Williams5076a1a2011-06-27 14:57:03 -0700197 task_iu = &ireq->ssp.tmf;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700198
199 memset(task_iu, 0, sizeof(struct ssp_task_iu));
200
201 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
202
203 task_iu->task_func = isci_tmf->tmf_code;
204 task_iu->task_tag =
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700205 (test_bit(IREQ_TMF, &ireq->flags)) ?
Dan Williamsf1f52e72011-05-10 02:28:45 -0700206 isci_tmf->io_tag :
207 SCI_CONTROLLER_INVALID_IO_TAG;
208}
209
210/**
211 * This method is will fill in the SCU Task Context for any type of SSP request.
212 * @sci_req:
213 * @task_context:
214 *
215 */
216static void scu_ssp_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700217 struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700218 struct scu_task_context *task_context)
219{
220 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700221 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700222 struct isci_port *iport;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700223
Dan Williams34a99152011-07-01 02:25:15 -0700224 idev = ireq->target_device;
225 iport = idev->owning_port;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700226
227 /* Fill in the TC with the its required data */
228 task_context->abort = 0;
229 task_context->priority = 0;
230 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700231 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -0700232 task_context->protocol_engine_index = ISCI_PEG;
233 task_context->logical_port_index = iport->physical_port_index;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700234 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
235 task_context->valid = SCU_TASK_CONTEXT_VALID;
236 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
237
Dan Williams34a99152011-07-01 02:25:15 -0700238 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700239 task_context->command_code = 0;
240
241 task_context->link_layer_control = 0;
242 task_context->do_not_dma_ssp_good_response = 1;
243 task_context->strict_ordering = 0;
244 task_context->control_frame = 0;
245 task_context->timeout_enable = 0;
246 task_context->block_guard_enable = 0;
247
248 task_context->address_modifier = 0;
249
Dan Williams5076a1a2011-06-27 14:57:03 -0700250 /* task_context->type.ssp.tag = ireq->io_tag; */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700251 task_context->task_phase = 0x01;
252
Dan Williams5076a1a2011-06-27 14:57:03 -0700253 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -0700254 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
255 (iport->physical_port_index <<
Dan Williamsffe191c2011-06-29 13:09:25 -0700256 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
257 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsf1f52e72011-05-10 02:28:45 -0700258
259 /*
260 * Copy the physical address for the command buffer to the
261 * SCU Task Context
262 */
Dan Williams89a73012011-06-30 19:14:33 -0700263 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.cmd);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700264
265 task_context->command_iu_upper = upper_32_bits(dma_addr);
266 task_context->command_iu_lower = lower_32_bits(dma_addr);
267
268 /*
269 * Copy the physical address for the response buffer to the
270 * SCU Task Context
271 */
Dan Williams89a73012011-06-30 19:14:33 -0700272 dma_addr = sci_io_request_get_dma_addr(ireq, &ireq->ssp.rsp);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700273
274 task_context->response_iu_upper = upper_32_bits(dma_addr);
275 task_context->response_iu_lower = lower_32_bits(dma_addr);
276}
277
Dave Jiang3d2d7522012-02-10 01:18:34 -0800278static u8 scu_bg_blk_size(struct scsi_device *sdp)
279{
280 switch (sdp->sector_size) {
281 case 512:
282 return 0;
283 case 1024:
284 return 1;
285 case 4096:
286 return 3;
287 default:
288 return 0xff;
289 }
290}
291
292static u32 scu_dif_bytes(u32 len, u32 sector_size)
293{
294 return (len >> ilog2(sector_size)) * 8;
295}
296
297static void scu_ssp_ireq_dif_insert(struct isci_request *ireq, u8 type, u8 op)
298{
299 struct scu_task_context *tc = ireq->tc;
300 struct scsi_cmnd *scmd = ireq->ttype_ptr.io_task_ptr->uldd_task;
301 u8 blk_sz = scu_bg_blk_size(scmd->device);
302
303 tc->block_guard_enable = 1;
304 tc->blk_prot_en = 1;
305 tc->blk_sz = blk_sz;
306 /* DIF write insert */
307 tc->blk_prot_func = 0x2;
308
309 tc->transfer_length_bytes += scu_dif_bytes(tc->transfer_length_bytes,
310 scmd->device->sector_size);
311
312 /* always init to 0, used by hw */
313 tc->interm_crc_val = 0;
314
315 tc->init_crc_seed = 0;
316 tc->app_tag_verify = 0;
317 tc->app_tag_gen = 0;
318 tc->ref_tag_seed_verify = 0;
319
320 /* always init to same as bg_blk_sz */
321 tc->UD_bytes_immed_val = scmd->device->sector_size;
322
323 tc->reserved_DC_0 = 0;
324
325 /* always init to 8 */
326 tc->DIF_bytes_immed_val = 8;
327
328 tc->reserved_DC_1 = 0;
329 tc->bgc_blk_sz = scmd->device->sector_size;
330 tc->reserved_E0_0 = 0;
331 tc->app_tag_gen_mask = 0;
332
333 /** setup block guard control **/
334 tc->bgctl = 0;
335
336 /* DIF write insert */
337 tc->bgctl_f.op = 0x2;
338
339 tc->app_tag_verify_mask = 0;
340
341 /* must init to 0 for hw */
342 tc->blk_guard_err = 0;
343
344 tc->reserved_E8_0 = 0;
345
346 if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2))
347 tc->ref_tag_seed_gen = scsi_get_lba(scmd) & 0xffffffff;
348 else if (type & SCSI_PROT_DIF_TYPE3)
349 tc->ref_tag_seed_gen = 0;
350}
351
352static void scu_ssp_ireq_dif_strip(struct isci_request *ireq, u8 type, u8 op)
353{
354 struct scu_task_context *tc = ireq->tc;
355 struct scsi_cmnd *scmd = ireq->ttype_ptr.io_task_ptr->uldd_task;
356 u8 blk_sz = scu_bg_blk_size(scmd->device);
357
358 tc->block_guard_enable = 1;
359 tc->blk_prot_en = 1;
360 tc->blk_sz = blk_sz;
361 /* DIF read strip */
362 tc->blk_prot_func = 0x1;
363
364 tc->transfer_length_bytes += scu_dif_bytes(tc->transfer_length_bytes,
365 scmd->device->sector_size);
366
367 /* always init to 0, used by hw */
368 tc->interm_crc_val = 0;
369
370 tc->init_crc_seed = 0;
371 tc->app_tag_verify = 0;
372 tc->app_tag_gen = 0;
373
374 if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2))
375 tc->ref_tag_seed_verify = scsi_get_lba(scmd) & 0xffffffff;
376 else if (type & SCSI_PROT_DIF_TYPE3)
377 tc->ref_tag_seed_verify = 0;
378
379 /* always init to same as bg_blk_sz */
380 tc->UD_bytes_immed_val = scmd->device->sector_size;
381
382 tc->reserved_DC_0 = 0;
383
384 /* always init to 8 */
385 tc->DIF_bytes_immed_val = 8;
386
387 tc->reserved_DC_1 = 0;
388 tc->bgc_blk_sz = scmd->device->sector_size;
389 tc->reserved_E0_0 = 0;
390 tc->app_tag_gen_mask = 0;
391
392 /** setup block guard control **/
393 tc->bgctl = 0;
394
395 /* DIF read strip */
396 tc->bgctl_f.crc_verify = 1;
397 tc->bgctl_f.op = 0x1;
398 if ((type & SCSI_PROT_DIF_TYPE1) || (type & SCSI_PROT_DIF_TYPE2)) {
399 tc->bgctl_f.ref_tag_chk = 1;
400 tc->bgctl_f.app_f_detect = 1;
401 } else if (type & SCSI_PROT_DIF_TYPE3)
402 tc->bgctl_f.app_ref_f_detect = 1;
403
404 tc->app_tag_verify_mask = 0;
405
406 /* must init to 0 for hw */
407 tc->blk_guard_err = 0;
408
409 tc->reserved_E8_0 = 0;
410 tc->ref_tag_seed_gen = 0;
411}
412
Dan Williamsf1f52e72011-05-10 02:28:45 -0700413/**
414 * This method is will fill in the SCU Task Context for a SSP IO request.
415 * @sci_req:
416 *
417 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700418static void scu_ssp_io_request_construct_task_context(struct isci_request *ireq,
Dan Williams312e0c22011-06-28 13:47:09 -0700419 enum dma_data_direction dir,
420 u32 len)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700421{
Dan Williams5076a1a2011-06-27 14:57:03 -0700422 struct scu_task_context *task_context = ireq->tc;
Dave Jiang3d2d7522012-02-10 01:18:34 -0800423 struct sas_task *sas_task = ireq->ttype_ptr.io_task_ptr;
424 struct scsi_cmnd *scmd = sas_task->uldd_task;
425 u8 prot_type = scsi_get_prot_type(scmd);
426 u8 prot_op = scsi_get_prot_op(scmd);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700427
Dan Williams5076a1a2011-06-27 14:57:03 -0700428 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700429
430 task_context->ssp_command_iu_length =
431 sizeof(struct ssp_cmd_iu) / sizeof(u32);
432 task_context->type.ssp.frame_type = SSP_COMMAND;
433
434 switch (dir) {
435 case DMA_FROM_DEVICE:
436 case DMA_NONE:
437 default:
438 task_context->task_type = SCU_TASK_TYPE_IOREAD;
439 break;
440 case DMA_TO_DEVICE:
441 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
442 break;
443 }
444
445 task_context->transfer_length_bytes = len;
446
447 if (task_context->transfer_length_bytes > 0)
Dan Williams89a73012011-06-30 19:14:33 -0700448 sci_request_build_sgl(ireq);
Dave Jiang3d2d7522012-02-10 01:18:34 -0800449
450 if (prot_type != SCSI_PROT_DIF_TYPE0) {
451 if (prot_op == SCSI_PROT_READ_STRIP)
452 scu_ssp_ireq_dif_strip(ireq, prot_type, prot_op);
453 else if (prot_op == SCSI_PROT_WRITE_INSERT)
454 scu_ssp_ireq_dif_insert(ireq, prot_type, prot_op);
455 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700456}
457
Dan Williamsf1f52e72011-05-10 02:28:45 -0700458/**
459 * This method will fill in the SCU Task Context for a SSP Task request. The
460 * following important settings are utilized: -# priority ==
461 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
462 * ahead of other task destined for the same Remote Node. -# task_type ==
463 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
464 * (i.e. non-raw frame) is being utilized to perform task management. -#
465 * control_frame == 1. This ensures that the proper endianess is set so
466 * that the bytes are transmitted in the right order for a task frame.
467 * @sci_req: This parameter specifies the task request object being
468 * constructed.
469 *
470 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700471static void scu_ssp_task_request_construct_task_context(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700472{
Dan Williams5076a1a2011-06-27 14:57:03 -0700473 struct scu_task_context *task_context = ireq->tc;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700474
Dan Williams5076a1a2011-06-27 14:57:03 -0700475 scu_ssp_reqeust_construct_task_context(ireq, task_context);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700476
477 task_context->control_frame = 1;
478 task_context->priority = SCU_TASK_PRIORITY_HIGH;
479 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
480 task_context->transfer_length_bytes = 0;
481 task_context->type.ssp.frame_type = SSP_TASK;
482 task_context->ssp_command_iu_length =
483 sizeof(struct ssp_task_iu) / sizeof(u32);
484}
485
Dan Williamsf1f52e72011-05-10 02:28:45 -0700486/**
Dan Williams5dec6f42011-05-10 02:28:49 -0700487 * This method is will fill in the SCU Task Context for any type of SATA
488 * request. This is called from the various SATA constructors.
489 * @sci_req: The general IO request object which is to be used in
490 * constructing the SCU task context.
491 * @task_context: The buffer pointer for the SCU task context which is being
492 * constructed.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700493 *
Dan Williams5dec6f42011-05-10 02:28:49 -0700494 * The general io request construction is complete. The buffer assignment for
495 * the command buffer is complete. none Revisit task context construction to
496 * determine what is common for SSP/SMP/STP task context structures.
Dan Williamsf1f52e72011-05-10 02:28:45 -0700497 */
Dan Williams5dec6f42011-05-10 02:28:49 -0700498static void scu_sata_reqeust_construct_task_context(
Dan Williams5076a1a2011-06-27 14:57:03 -0700499 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700500 struct scu_task_context *task_context)
501{
502 dma_addr_t dma_addr;
Dan Williams78a6f062011-06-30 16:31:37 -0700503 struct isci_remote_device *idev;
Dan Williamsffe191c2011-06-29 13:09:25 -0700504 struct isci_port *iport;
Dan Williams5dec6f42011-05-10 02:28:49 -0700505
Dan Williams34a99152011-07-01 02:25:15 -0700506 idev = ireq->target_device;
507 iport = idev->owning_port;
Dan Williams5dec6f42011-05-10 02:28:49 -0700508
509 /* Fill in the TC with the its required data */
510 task_context->abort = 0;
511 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
512 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -0700513 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -0700514 task_context->protocol_engine_index = ISCI_PEG;
515 task_context->logical_port_index = iport->physical_port_index;
Dan Williams5dec6f42011-05-10 02:28:49 -0700516 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
517 task_context->valid = SCU_TASK_CONTEXT_VALID;
518 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
519
Dan Williams34a99152011-07-01 02:25:15 -0700520 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williams5dec6f42011-05-10 02:28:49 -0700521 task_context->command_code = 0;
522
523 task_context->link_layer_control = 0;
524 task_context->do_not_dma_ssp_good_response = 1;
525 task_context->strict_ordering = 0;
526 task_context->control_frame = 0;
527 task_context->timeout_enable = 0;
528 task_context->block_guard_enable = 0;
529
530 task_context->address_modifier = 0;
531 task_context->task_phase = 0x01;
532
533 task_context->ssp_command_iu_length =
534 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
535
536 /* Set the first word of the H2D REG FIS */
Dan Williams5076a1a2011-06-27 14:57:03 -0700537 task_context->type.words[0] = *(u32 *)&ireq->stp.cmd;
Dan Williams5dec6f42011-05-10 02:28:49 -0700538
Dan Williams5076a1a2011-06-27 14:57:03 -0700539 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -0700540 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
541 (iport->physical_port_index <<
542 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
543 ISCI_TAG_TCI(ireq->io_tag));
Dan Williams5dec6f42011-05-10 02:28:49 -0700544 /*
545 * Copy the physical address for the command buffer to the SCU Task
546 * Context. We must offset the command buffer by 4 bytes because the
547 * first 4 bytes are transfered in the body of the TC.
548 */
Dan Williams89a73012011-06-30 19:14:33 -0700549 dma_addr = sci_io_request_get_dma_addr(ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -0700550 ((char *) &ireq->stp.cmd) +
Dan Williams5dec6f42011-05-10 02:28:49 -0700551 sizeof(u32));
552
553 task_context->command_iu_upper = upper_32_bits(dma_addr);
554 task_context->command_iu_lower = lower_32_bits(dma_addr);
555
556 /* SATA Requests do not have a response buffer */
557 task_context->response_iu_upper = 0;
558 task_context->response_iu_lower = 0;
559}
560
Dan Williams5076a1a2011-06-27 14:57:03 -0700561static void scu_stp_raw_request_construct_task_context(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -0700562{
Dan Williams5076a1a2011-06-27 14:57:03 -0700563 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700564
Dan Williams5076a1a2011-06-27 14:57:03 -0700565 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700566
567 task_context->control_frame = 0;
568 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
569 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
570 task_context->type.stp.fis_type = FIS_REGH2D;
571 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
572}
573
Dan Williams89a73012011-06-30 19:14:33 -0700574static enum sci_status sci_stp_pio_request_construct(struct isci_request *ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -0700575 bool copy_rx_frame)
Dan Williams5dec6f42011-05-10 02:28:49 -0700576{
Dan Williams5076a1a2011-06-27 14:57:03 -0700577 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -0700578
Dan Williams5076a1a2011-06-27 14:57:03 -0700579 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700580
Dan Williamsba7cb222011-06-27 11:56:41 -0700581 stp_req->status = 0;
582 stp_req->sgl.offset = 0;
583 stp_req->sgl.set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5dec6f42011-05-10 02:28:49 -0700584
585 if (copy_rx_frame) {
Dan Williams89a73012011-06-30 19:14:33 -0700586 sci_request_build_sgl(ireq);
Dan Williamsba7cb222011-06-27 11:56:41 -0700587 stp_req->sgl.index = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -0700588 } else {
589 /* The user does not want the data copied to the SGL buffer location */
Dan Williamsba7cb222011-06-27 11:56:41 -0700590 stp_req->sgl.index = -1;
Dan Williams5dec6f42011-05-10 02:28:49 -0700591 }
592
593 return SCI_SUCCESS;
594}
595
596/**
597 *
598 * @sci_req: This parameter specifies the request to be constructed as an
599 * optimized request.
600 * @optimized_task_type: This parameter specifies whether the request is to be
601 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
602 * value of 1 indicates NCQ.
603 *
604 * This method will perform request construction common to all types of STP
605 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
606 * returns an indication as to whether the construction was successful.
607 */
Dan Williams89a73012011-06-30 19:14:33 -0700608static void sci_stp_optimized_request_construct(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700609 u8 optimized_task_type,
610 u32 len,
611 enum dma_data_direction dir)
612{
Dan Williams5076a1a2011-06-27 14:57:03 -0700613 struct scu_task_context *task_context = ireq->tc;
Dan Williams5dec6f42011-05-10 02:28:49 -0700614
615 /* Build the STP task context structure */
Dan Williams5076a1a2011-06-27 14:57:03 -0700616 scu_sata_reqeust_construct_task_context(ireq, task_context);
Dan Williams5dec6f42011-05-10 02:28:49 -0700617
618 /* Copy over the SGL elements */
Dan Williams89a73012011-06-30 19:14:33 -0700619 sci_request_build_sgl(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700620
621 /* Copy over the number of bytes to be transfered */
622 task_context->transfer_length_bytes = len;
623
624 if (dir == DMA_TO_DEVICE) {
625 /*
626 * The difference between the DMA IN and DMA OUT request task type
627 * values are consistent with the difference between FPDMA READ
628 * and FPDMA WRITE values. Add the supplied task type parameter
629 * to this difference to set the task type properly for this
630 * DATA OUT (WRITE) case. */
631 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
632 - SCU_TASK_TYPE_DMA_IN);
633 } else {
634 /*
635 * For the DATA IN (READ) case, simply save the supplied
636 * optimized task type. */
637 task_context->task_type = optimized_task_type;
638 }
639}
640
Dan Williamsb50102d2011-09-30 18:52:19 -0700641static void sci_atapi_construct(struct isci_request *ireq)
642{
643 struct host_to_dev_fis *h2d_fis = &ireq->stp.cmd;
644 struct sas_task *task;
Dan Williams5dec6f42011-05-10 02:28:49 -0700645
Dan Williamsb50102d2011-09-30 18:52:19 -0700646 /* To simplify the implementation we take advantage of the
647 * silicon's partial acceleration of atapi protocol (dma data
648 * transfers), so we promote all commands to dma protocol. This
649 * breaks compatibility with ATA_HORKAGE_ATAPI_MOD16_DMA drives.
650 */
651 h2d_fis->features |= ATAPI_PKT_DMA;
652
653 scu_stp_raw_request_construct_task_context(ireq);
654
655 task = isci_request_access_task(ireq);
656 if (task->data_dir == DMA_NONE)
657 task->total_xfer_len = 0;
658
659 /* clear the response so we can detect arrivial of an
660 * unsolicited h2d fis
661 */
662 ireq->stp.rsp.fis_type = 0;
663}
Dan Williams5dec6f42011-05-10 02:28:49 -0700664
Dan Williamsf1f52e72011-05-10 02:28:45 -0700665static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700666sci_io_request_construct_sata(struct isci_request *ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700667 u32 len,
668 enum dma_data_direction dir,
669 bool copy)
Dan Williams6f231dd2011-07-02 22:56:22 -0700670{
Dan Williams6f231dd2011-07-02 22:56:22 -0700671 enum sci_status status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700672 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamsb50102d2011-09-30 18:52:19 -0700673 struct domain_device *dev = ireq->target_device->domain_dev;
Dan Williams6f231dd2011-07-02 22:56:22 -0700674
Dan Williamsf1f52e72011-05-10 02:28:45 -0700675 /* check for management protocols */
Jeff Skirvin3b34c162011-10-27 15:05:22 -0700676 if (test_bit(IREQ_TMF, &ireq->flags)) {
Dan Williamsf1f52e72011-05-10 02:28:45 -0700677 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700678
Dan Williams43a5ab12011-12-08 23:20:44 -0800679 dev_err(&ireq->owning_controller->pdev->dev,
680 "%s: Request 0x%p received un-handled SAT "
681 "management protocol 0x%x.\n",
682 __func__, ireq, tmf->tmf_code);
Dan Williams6f231dd2011-07-02 22:56:22 -0700683
Dan Williams43a5ab12011-12-08 23:20:44 -0800684 return SCI_FAILURE;
Dan Williams6f231dd2011-07-02 22:56:22 -0700685 }
686
Dan Williamsf1f52e72011-05-10 02:28:45 -0700687 if (!sas_protocol_ata(task->task_proto)) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700688 dev_err(&ireq->owning_controller->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700689 "%s: Non-ATA protocol in SATA path: 0x%x\n",
690 __func__,
691 task->task_proto);
Dan Williams6f231dd2011-07-02 22:56:22 -0700692 return SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700693
Dan Williams6f231dd2011-07-02 22:56:22 -0700694 }
695
Dan Williamsb50102d2011-09-30 18:52:19 -0700696 /* ATAPI */
697 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
698 task->ata_task.fis.command == ATA_CMD_PACKET) {
699 sci_atapi_construct(ireq);
700 return SCI_SUCCESS;
701 }
702
Dan Williamsf1f52e72011-05-10 02:28:45 -0700703 /* non data */
Dan Williams5dec6f42011-05-10 02:28:49 -0700704 if (task->data_dir == DMA_NONE) {
Dan Williams5076a1a2011-06-27 14:57:03 -0700705 scu_stp_raw_request_construct_task_context(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -0700706 return SCI_SUCCESS;
707 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700708
709 /* NCQ */
Dan Williams5dec6f42011-05-10 02:28:49 -0700710 if (task->ata_task.use_ncq) {
Dan Williams89a73012011-06-30 19:14:33 -0700711 sci_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700712 SCU_TASK_TYPE_FPDMAQ_READ,
713 len, dir);
714 return SCI_SUCCESS;
715 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700716
717 /* DMA */
Dan Williams5dec6f42011-05-10 02:28:49 -0700718 if (task->ata_task.dma_xfer) {
Dan Williams89a73012011-06-30 19:14:33 -0700719 sci_stp_optimized_request_construct(ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -0700720 SCU_TASK_TYPE_DMA_IN,
721 len, dir);
722 return SCI_SUCCESS;
723 } else /* PIO */
Dan Williams89a73012011-06-30 19:14:33 -0700724 return sci_stp_pio_request_construct(ireq, copy);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700725
726 return status;
727}
728
Dan Williams89a73012011-06-30 19:14:33 -0700729static enum sci_status sci_io_request_construct_basic_ssp(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700730{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700731 struct sas_task *task = isci_request_access_task(ireq);
732
Dan Williamsc79dd802012-02-01 00:44:14 -0800733 ireq->protocol = SAS_PROTOCOL_SSP;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700734
Dan Williams5076a1a2011-06-27 14:57:03 -0700735 scu_ssp_io_request_construct_task_context(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700736 task->data_dir,
737 task->total_xfer_len);
738
Dan Williams89a73012011-06-30 19:14:33 -0700739 sci_io_request_build_ssp_command_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700740
Dan Williams5076a1a2011-06-27 14:57:03 -0700741 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700742
743 return SCI_SUCCESS;
744}
745
Dan Williams89a73012011-06-30 19:14:33 -0700746enum sci_status sci_task_request_construct_ssp(
Dan Williams5076a1a2011-06-27 14:57:03 -0700747 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700748{
749 /* Construct the SSP Task SCU Task Context */
Dan Williams5076a1a2011-06-27 14:57:03 -0700750 scu_ssp_task_request_construct_task_context(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700751
752 /* Fill in the SSP Task IU */
Dan Williams89a73012011-06-30 19:14:33 -0700753 sci_task_request_build_ssp_task_iu(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700754
Dan Williams5076a1a2011-06-27 14:57:03 -0700755 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700756
757 return SCI_SUCCESS;
758}
759
Dan Williams89a73012011-06-30 19:14:33 -0700760static enum sci_status sci_io_request_construct_basic_sata(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700761{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700762 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700763 bool copy = false;
Dan Williams5076a1a2011-06-27 14:57:03 -0700764 struct sas_task *task = isci_request_access_task(ireq);
Dan Williams6f231dd2011-07-02 22:56:22 -0700765
Dan Williamsc79dd802012-02-01 00:44:14 -0800766 ireq->protocol = SAS_PROTOCOL_STP;
Dan Williams6f231dd2011-07-02 22:56:22 -0700767
Dan Williamsf1f52e72011-05-10 02:28:45 -0700768 copy = (task->data_dir == DMA_NONE) ? false : true;
Dan Williams6f231dd2011-07-02 22:56:22 -0700769
Dan Williams89a73012011-06-30 19:14:33 -0700770 status = sci_io_request_construct_sata(ireq,
Dan Williamsf1f52e72011-05-10 02:28:45 -0700771 task->total_xfer_len,
772 task->data_dir,
773 copy);
Dan Williams6f231dd2011-07-02 22:56:22 -0700774
Dan Williamsf1f52e72011-05-10 02:28:45 -0700775 if (status == SCI_SUCCESS)
Dan Williams5076a1a2011-06-27 14:57:03 -0700776 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williams6f231dd2011-07-02 22:56:22 -0700777
Dan Williamsf1f52e72011-05-10 02:28:45 -0700778 return status;
Dan Williams6f231dd2011-07-02 22:56:22 -0700779}
780
Dan Williams6f231dd2011-07-02 22:56:22 -0700781/**
Dan Williamsf1f52e72011-05-10 02:28:45 -0700782 * sci_req_tx_bytes - bytes transferred when reply underruns request
Dan Williamsb50102d2011-09-30 18:52:19 -0700783 * @ireq: request that was terminated early
Dan Williams6f231dd2011-07-02 22:56:22 -0700784 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700785#define SCU_TASK_CONTEXT_SRAM 0x200000
Dan Williams5076a1a2011-06-27 14:57:03 -0700786static u32 sci_req_tx_bytes(struct isci_request *ireq)
Dan Williams6f231dd2011-07-02 22:56:22 -0700787{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700788 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700789 u32 ret_val = 0;
Dan Williams6f231dd2011-07-02 22:56:22 -0700790
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700791 if (readl(&ihost->smu_registers->address_modifier) == 0) {
792 void __iomem *scu_reg_base = ihost->scu_registers;
Dan Williams6f231dd2011-07-02 22:56:22 -0700793
Dan Williamsf1f52e72011-05-10 02:28:45 -0700794 /* get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
795 * BAR1 is the scu_registers
796 * 0x20002C = 0x200000 + 0x2c
797 * = start of task context SRAM + offset of (type.ssp.data_offset)
Dan Williams89a73012011-06-30 19:14:33 -0700798 * TCi is the io_tag of struct sci_request
Dan Williams67ea8382011-05-08 11:47:15 -0700799 */
Dan Williamsf1f52e72011-05-10 02:28:45 -0700800 ret_val = readl(scu_reg_base +
801 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
Dan Williams5076a1a2011-06-27 14:57:03 -0700802 ((sizeof(struct scu_task_context)) * ISCI_TAG_TCI(ireq->io_tag)));
Dan Williams67ea8382011-05-08 11:47:15 -0700803 }
Dan Williams6f231dd2011-07-02 22:56:22 -0700804
Dan Williamsf1f52e72011-05-10 02:28:45 -0700805 return ret_val;
Dan Williams6f231dd2011-07-02 22:56:22 -0700806}
807
Dan Williams89a73012011-06-30 19:14:33 -0700808enum sci_status sci_request_start(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700809{
Piotr Sawickif4636a72011-05-10 23:50:32 +0000810 enum sci_base_request_states state;
Dan Williams5076a1a2011-06-27 14:57:03 -0700811 struct scu_task_context *tc = ireq->tc;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700812 struct isci_host *ihost = ireq->owning_controller;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000813
Dan Williams5076a1a2011-06-27 14:57:03 -0700814 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000815 if (state != SCI_REQ_CONSTRUCTED) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700816 dev_warn(&ihost->pdev->dev,
Piotr Sawickif4636a72011-05-10 23:50:32 +0000817 "%s: SCIC IO Request requested to start while in wrong "
818 "state %d\n", __func__, state);
819 return SCI_FAILURE_INVALID_STATE;
820 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700821
Dan Williams5076a1a2011-06-27 14:57:03 -0700822 tc->task_index = ISCI_TAG_TCI(ireq->io_tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -0700823
Dan Williams312e0c22011-06-28 13:47:09 -0700824 switch (tc->protocol_type) {
825 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
826 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
827 /* SSP/SMP Frame */
Dan Williams5076a1a2011-06-27 14:57:03 -0700828 tc->type.ssp.tag = ireq->io_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700829 tc->type.ssp.target_port_transfer_tag = 0xFFFF;
830 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000831
Dan Williams312e0c22011-06-28 13:47:09 -0700832 case SCU_TASK_CONTEXT_PROTOCOL_STP:
833 /* STP/SATA Frame
Dan Williams5076a1a2011-06-27 14:57:03 -0700834 * tc->type.stp.ncq_tag = ireq->ncq_tag;
Dan Williams312e0c22011-06-28 13:47:09 -0700835 */
836 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000837
Dan Williams312e0c22011-06-28 13:47:09 -0700838 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
839 /* / @todo When do we set no protocol type? */
840 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000841
Dan Williams312e0c22011-06-28 13:47:09 -0700842 default:
843 /* This should never happen since we build the IO
844 * requests */
845 break;
Piotr Sawickif4636a72011-05-10 23:50:32 +0000846 }
847
Dan Williams312e0c22011-06-28 13:47:09 -0700848 /* Add to the post_context the io tag value */
Dan Williams5076a1a2011-06-27 14:57:03 -0700849 ireq->post_context |= ISCI_TAG_TCI(ireq->io_tag);
Dan Williams312e0c22011-06-28 13:47:09 -0700850
851 /* Everything is good go ahead and change state */
Dan Williams5076a1a2011-06-27 14:57:03 -0700852 sci_change_state(&ireq->sm, SCI_REQ_STARTED);
Dan Williams312e0c22011-06-28 13:47:09 -0700853
854 return SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700855}
856
857enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -0700858sci_io_request_terminate(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700859{
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700860 enum sci_base_request_states state;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700861
Dan Williams5076a1a2011-06-27 14:57:03 -0700862 state = ireq->sm.current_state_id;
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700863
864 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +0000865 case SCI_REQ_CONSTRUCTED:
Jeff Skirvin726980d2012-03-08 22:41:50 -0800866 /* Set to make sure no HW terminate posting is done: */
867 set_bit(IREQ_TC_ABORT_POSTED, &ireq->flags);
Dan Williams34a99152011-07-01 02:25:15 -0700868 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
869 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -0700870 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700871 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000872 case SCI_REQ_STARTED:
873 case SCI_REQ_TASK_WAIT_TC_COMP:
874 case SCI_REQ_SMP_WAIT_RESP:
875 case SCI_REQ_SMP_WAIT_TC_COMP:
876 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
877 case SCI_REQ_STP_UDMA_WAIT_D2H:
878 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
879 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
880 case SCI_REQ_STP_PIO_WAIT_H2D:
881 case SCI_REQ_STP_PIO_WAIT_FRAME:
882 case SCI_REQ_STP_PIO_DATA_IN:
883 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williamsb50102d2011-09-30 18:52:19 -0700884 case SCI_REQ_ATAPI_WAIT_H2D:
885 case SCI_REQ_ATAPI_WAIT_PIO_SETUP:
886 case SCI_REQ_ATAPI_WAIT_D2H:
887 case SCI_REQ_ATAPI_WAIT_TC_COMP:
Jeff Skirvin726980d2012-03-08 22:41:50 -0800888 /* Fall through and change state to ABORTING... */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000889 case SCI_REQ_TASK_WAIT_TC_RESP:
Jeff Skirvin39ea2c52011-07-29 17:17:05 -0700890 /* The task frame was already confirmed to have been
891 * sent by the SCU HW. Since the state machine is
892 * now only waiting for the task response itself,
893 * abort the request and complete it immediately
894 * and don't wait for the task response.
895 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700896 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
Jeff Skirvin726980d2012-03-08 22:41:50 -0800897 /* Fall through and handle like ABORTING... */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000898 case SCI_REQ_ABORTING:
Jeff Skirvin726980d2012-03-08 22:41:50 -0800899 if (!isci_remote_device_is_safe_to_abort(ireq->target_device))
900 set_bit(IREQ_PENDING_ABORT, &ireq->flags);
901 else
902 clear_bit(IREQ_PENDING_ABORT, &ireq->flags);
903 /* If the request is only waiting on the remote device
904 * suspension, return SUCCESS so the caller will wait too.
Jeff Skirvin39ea2c52011-07-29 17:17:05 -0700905 */
Jeff Skirvin726980d2012-03-08 22:41:50 -0800906 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000907 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700908 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700909 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700910 "%s: SCIC IO Request requested to abort while in wrong "
Jeff Skirvin726980d2012-03-08 22:41:50 -0800911 "state %d\n", __func__, ireq->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700912 break;
913 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700914
915 return SCI_FAILURE_INVALID_STATE;
916}
917
Dan Williams89a73012011-06-30 19:14:33 -0700918enum sci_status sci_request_complete(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700919{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700920 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700921 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700922
Dan Williams5076a1a2011-06-27 14:57:03 -0700923 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000924 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800925 "isci: request completion from wrong state (%s)\n",
926 req_state_name(state)))
Dan Williams79e2b6b2011-05-11 08:29:56 -0700927 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700928
Dan Williams5076a1a2011-06-27 14:57:03 -0700929 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700930 sci_controller_release_frame(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700931 ireq->saved_rx_frame_index);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700932
933 /* XXX can we just stop the machine and remove the 'final' state? */
Dan Williams5076a1a2011-06-27 14:57:03 -0700934 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700935 return SCI_SUCCESS;
936}
937
Dan Williams89a73012011-06-30 19:14:33 -0700938enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700939 u32 event_code)
940{
941 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700942 struct isci_host *ihost = ireq->owning_controller;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700943
Dan Williams5076a1a2011-06-27 14:57:03 -0700944 state = ireq->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700945
Edmund Nadolskie3013702011-06-02 00:10:43 +0000946 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800947 dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %s\n",
948 __func__, event_code, req_state_name(state));
Dan Williams79e2b6b2011-05-11 08:29:56 -0700949
950 return SCI_FAILURE_INVALID_STATE;
951 }
952
953 switch (scu_get_event_specifier(event_code)) {
954 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
955 /* We are waiting for data and the SCU has R_ERR the data frame.
956 * Go back to waiting for the D2H Register FIS
957 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700958 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700959 return SCI_SUCCESS;
960 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700961 dev_err(&ihost->pdev->dev,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700962 "%s: pio request unexpected event %#x\n",
963 __func__, event_code);
964
965 /* TODO Should we fail the PIO request when we get an
966 * unexpected event?
967 */
968 return SCI_FAILURE;
969 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700970}
971
Dan Williamsf1f52e72011-05-10 02:28:45 -0700972/*
973 * This function copies response data for requests returning response data
974 * instead of sense data.
975 * @sci_req: This parameter specifies the request object for which to copy
976 * the response data.
977 */
Dan Williams89a73012011-06-30 19:14:33 -0700978static void sci_io_request_copy_response(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700979{
980 void *resp_buf;
981 u32 len;
982 struct ssp_response_iu *ssp_response;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700983 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
984
Dan Williams5076a1a2011-06-27 14:57:03 -0700985 ssp_response = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700986
987 resp_buf = &isci_tmf->resp.resp_iu;
988
989 len = min_t(u32,
990 SSP_RESP_IU_MAX_SIZE,
991 be32_to_cpu(ssp_response->response_data_len));
992
993 memcpy(resp_buf, ssp_response->resp_data, len);
994}
995
Edmund Nadolskie3013702011-06-02 00:10:43 +0000996static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700997request_started_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000998 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700999{
Dan Williamsf1f52e72011-05-10 02:28:45 -07001000 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -07001001 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001002
Dan Williamsa7e255a2011-05-11 08:27:47 -07001003 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
1004 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -07001005 */
1006 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1007 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001008 ireq->scu_status = SCU_TASK_DONE_GOOD;
1009 ireq->sci_status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001010 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -07001011 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
1012 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -07001013 * response because the io request specified more data than is
1014 * returned by the target device (mode pages, inquiry data,
1015 * etc.). We must check the response stats to see if this is
1016 * truly a failed request or a good request that just got
1017 * completed early.
1018 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001019 struct ssp_response_iu *resp = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001020 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1021
Dan Williams5076a1a2011-06-27 14:57:03 -07001022 sci_swab32_cpy(&ireq->ssp.rsp,
1023 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -07001024 word_cnt);
1025
1026 if (resp->status == 0) {
Dan Williams34a99152011-07-01 02:25:15 -07001027 ireq->scu_status = SCU_TASK_DONE_GOOD;
1028 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001029 } else {
Dan Williams34a99152011-07-01 02:25:15 -07001030 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1031 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001032 }
Dan Williamsa7e255a2011-05-11 08:27:47 -07001033 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001034 }
Dan Williamsa7e255a2011-05-11 08:27:47 -07001035 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -07001036 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1037
Dan Williams5076a1a2011-06-27 14:57:03 -07001038 sci_swab32_cpy(&ireq->ssp.rsp,
1039 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -07001040 word_cnt);
1041
Dan Williams34a99152011-07-01 02:25:15 -07001042 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1043 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001044 break;
1045 }
1046
1047 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001048 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -07001049 * guaranteed to be received before this completion status is
1050 * posted?
1051 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001052 resp_iu = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001053 datapres = resp_iu->datapres;
1054
Dan Williamsa7e255a2011-05-11 08:27:47 -07001055 if (datapres == 1 || datapres == 2) {
Dan Williams34a99152011-07-01 02:25:15 -07001056 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1057 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1058 } else {
1059 ireq->scu_status = SCU_TASK_DONE_GOOD;
1060 ireq->sci_status = SCI_SUCCESS;
1061 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07001062 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001063 /* only stp device gets suspended. */
1064 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1065 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1066 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1067 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1068 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1069 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1070 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1071 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1072 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1073 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1074 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
Dan Williamsc79dd802012-02-01 00:44:14 -08001075 if (ireq->protocol == SAS_PROTOCOL_STP) {
Dan Williams34a99152011-07-01 02:25:15 -07001076 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1077 SCU_COMPLETION_TL_STATUS_SHIFT;
1078 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001079 } else {
Dan Williams34a99152011-07-01 02:25:15 -07001080 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1081 SCU_COMPLETION_TL_STATUS_SHIFT;
1082 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001083 }
1084 break;
1085
1086 /* both stp/ssp device gets suspended */
1087 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1088 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1089 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1090 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1091 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1092 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1093 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1094 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1095 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1096 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williams34a99152011-07-01 02:25:15 -07001097 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1098 SCU_COMPLETION_TL_STATUS_SHIFT;
1099 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001100 break;
1101
1102 /* neither ssp nor stp gets suspended. */
1103 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1104 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1105 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1106 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1108 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1109 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1110 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1111 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1112 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1113 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1114 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1115 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1116 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1117 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1118 default:
Dan Williams34a99152011-07-01 02:25:15 -07001119 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1120 SCU_COMPLETION_TL_STATUS_SHIFT;
1121 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001122 break;
1123 }
1124
1125 /*
1126 * TODO: This is probably wrong for ACK/NAK timeout conditions
1127 */
1128
1129 /* In all cases we will treat this as the completion of the IO req. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001130 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001131 return SCI_SUCCESS;
1132}
1133
Edmund Nadolskie3013702011-06-02 00:10:43 +00001134static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001135request_aborting_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001136 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -07001137{
1138 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1139 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1140 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williams34a99152011-07-01 02:25:15 -07001141 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
1142 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -07001143 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001144 break;
1145
1146 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001147 /* Unless we get some strange error wait for the task abort to complete
1148 * TODO: Should there be a state change for this completion?
1149 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07001150 break;
1151 }
1152
1153 return SCI_SUCCESS;
1154}
1155
Dan Williams5076a1a2011-06-27 14:57:03 -07001156static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001157 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -07001158{
1159 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1160 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001161 ireq->scu_status = SCU_TASK_DONE_GOOD;
1162 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001163 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001164 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001165 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001166 /* Currently, the decision is to simply allow the task request
1167 * to timeout if the task IU wasn't received successfully.
1168 * There is a potential for receiving multiple task responses if
1169 * we decide to send the task IU again.
1170 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001171 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf1393032011-05-10 02:28:47 -07001172 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williams5076a1a2011-06-27 14:57:03 -07001173 "ACK/NAK timeout\n", __func__, ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001174 completion_code);
1175
Dan Williams5076a1a2011-06-27 14:57:03 -07001176 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001177 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001178 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001179 /*
1180 * All other completion status cause the IO to be complete.
1181 * If a NAK was received, then it is up to the user to retry
1182 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001183 */
Dan Williams34a99152011-07-01 02:25:15 -07001184 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1185 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001186 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001187 break;
1188 }
1189
1190 return SCI_SUCCESS;
1191}
1192
Edmund Nadolskie3013702011-06-02 00:10:43 +00001193static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001194smp_request_await_response_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001195 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001196{
1197 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1198 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001199 /* In the AWAIT RESPONSE state, any TC completion is
1200 * unexpected. but if the TC has success status, we
1201 * complete the IO anyway.
1202 */
Dan Williams34a99152011-07-01 02:25:15 -07001203 ireq->scu_status = SCU_TASK_DONE_GOOD;
1204 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001205 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001206 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001207 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1208 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1209 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1210 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001211 /* These status has been seen in a specific LSI
1212 * expander, which sometimes is not able to send smp
1213 * response within 2 ms. This causes our hardware break
1214 * the connection and set TC completion with one of
1215 * these SMP_XXX_XX_ERR status. For these type of error,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001216 * we ask ihost user to retry the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001217 */
Dan Williams34a99152011-07-01 02:25:15 -07001218 ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
1219 ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
Dan Williams5076a1a2011-06-27 14:57:03 -07001220 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001221 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001222 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001223 /* All other completion status cause the IO to be complete. If a NAK
1224 * was received, then it is up to the user to retry the request
1225 */
Dan Williams34a99152011-07-01 02:25:15 -07001226 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1227 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001228 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001229 break;
1230 }
1231
1232 return SCI_SUCCESS;
1233}
1234
Edmund Nadolskie3013702011-06-02 00:10:43 +00001235static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001236smp_request_await_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001237 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001238{
1239 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1240 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001241 ireq->scu_status = SCU_TASK_DONE_GOOD;
1242 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001243 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001244 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001245 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001246 /* All other completion status cause the IO to be
1247 * complete. If a NAK was received, then it is up to
1248 * the user to retry the request.
1249 */
Dan Williams34a99152011-07-01 02:25:15 -07001250 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1251 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001252 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001253 break;
1254 }
1255
1256 return SCI_SUCCESS;
1257}
1258
Dan Williamsba7cb222011-06-27 11:56:41 -07001259static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001260{
Dan Williams312e0c22011-06-28 13:47:09 -07001261 struct scu_sgl_element *sgl;
1262 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5076a1a2011-06-27 14:57:03 -07001263 struct isci_request *ireq = to_ireq(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001264 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001265
Dan Williams5076a1a2011-06-27 14:57:03 -07001266 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001267 if (!sgl_pair)
1268 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001269 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001270 if (sgl_pair->B.address_lower == 0 &&
1271 sgl_pair->B.address_upper == 0) {
1272 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001273 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001274 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001275 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001276 }
1277 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001278 if (sgl_pair->next_pair_lower == 0 &&
1279 sgl_pair->next_pair_upper == 0) {
1280 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001281 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001282 pio_sgl->index++;
1283 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5076a1a2011-06-27 14:57:03 -07001284 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001285 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001286 }
1287 }
1288
Dan Williams312e0c22011-06-28 13:47:09 -07001289 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001290}
1291
Edmund Nadolskie3013702011-06-02 00:10:43 +00001292static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001293stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001294 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001295{
1296 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1297 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001298 ireq->scu_status = SCU_TASK_DONE_GOOD;
1299 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001300 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001301 break;
1302
1303 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001304 /* All other completion status cause the IO to be
1305 * complete. If a NAK was received, then it is up to
1306 * the user to retry the request.
1307 */
Dan Williams34a99152011-07-01 02:25:15 -07001308 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1309 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001310 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001311 break;
1312 }
1313
1314 return SCI_SUCCESS;
1315}
1316
Dan Williams5dec6f42011-05-10 02:28:49 -07001317#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1318
1319/* transmit DATA_FIS from (current sgl + offset) for input
1320 * parameter length. current sgl and offset is alreay stored in the IO request
1321 */
Dan Williams89a73012011-06-30 19:14:33 -07001322static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
Dan Williams5076a1a2011-06-27 14:57:03 -07001323 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001324 u32 length)
1325{
Dan Williams5076a1a2011-06-27 14:57:03 -07001326 struct isci_stp_request *stp_req = &ireq->stp.req;
1327 struct scu_task_context *task_context = ireq->tc;
Dan Williams312e0c22011-06-28 13:47:09 -07001328 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001329 struct scu_sgl_element *current_sgl;
1330
1331 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1332 * for the data from current_sgl+offset for the input length
1333 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001334 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williamsba7cb222011-06-27 11:56:41 -07001335 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001336 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001337 else
Dan Williams312e0c22011-06-28 13:47:09 -07001338 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001339
1340 /* update the TC */
1341 task_context->command_iu_upper = current_sgl->address_upper;
1342 task_context->command_iu_lower = current_sgl->address_lower;
1343 task_context->transfer_length_bytes = length;
1344 task_context->type.stp.fis_type = FIS_DATA;
1345
1346 /* send the new TC out. */
Dan Williams89a73012011-06-30 19:14:33 -07001347 return sci_controller_continue_io(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001348}
1349
Dan Williams89a73012011-06-30 19:14:33 -07001350static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -07001351{
Dan Williams5076a1a2011-06-27 14:57:03 -07001352 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001353 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsb50102d2011-09-30 18:52:19 -07001354 enum sci_status status = SCI_SUCCESS;
Dan Williamsba7cb222011-06-27 11:56:41 -07001355 struct scu_sgl_element *sgl;
Dan Williamsba7cb222011-06-27 11:56:41 -07001356 u32 offset;
1357 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001358
Dan Williamsba7cb222011-06-27 11:56:41 -07001359 offset = stp_req->sgl.offset;
Dan Williams5076a1a2011-06-27 14:57:03 -07001360 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001361 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1362 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001363
Dan Williamsba7cb222011-06-27 11:56:41 -07001364 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1365 sgl = &sgl_pair->A;
1366 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001367 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001368 sgl = &sgl_pair->B;
1369 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001370 }
1371
Dan Williamsba7cb222011-06-27 11:56:41 -07001372 if (stp_req->pio_len == 0)
1373 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001374
Dan Williamsba7cb222011-06-27 11:56:41 -07001375 if (stp_req->pio_len >= len) {
Dan Williams89a73012011-06-30 19:14:33 -07001376 status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001377 if (status != SCI_SUCCESS)
1378 return status;
1379 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001380
Dan Williamsba7cb222011-06-27 11:56:41 -07001381 /* update the current sgl, offset and save for future */
1382 sgl = pio_sgl_next(stp_req);
1383 offset = 0;
1384 } else if (stp_req->pio_len < len) {
Dan Williams89a73012011-06-30 19:14:33 -07001385 sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001386
1387 /* Sgl offset will be adjusted and saved for future */
1388 offset += stp_req->pio_len;
1389 sgl->address_lower += stp_req->pio_len;
1390 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001391 }
1392
Dan Williamsba7cb222011-06-27 11:56:41 -07001393 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001394
1395 return status;
1396}
1397
1398/**
1399 *
1400 * @stp_request: The request that is used for the SGL processing.
1401 * @data_buffer: The buffer of data to be copied.
1402 * @length: The length of the data transfer.
1403 *
1404 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1405 * specified data region. enum sci_status
1406 */
1407static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001408sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williamsb50102d2011-09-30 18:52:19 -07001409 u8 *data_buf, u32 len)
Dan Williams5dec6f42011-05-10 02:28:49 -07001410{
Dan Williams5dec6f42011-05-10 02:28:49 -07001411 struct isci_request *ireq;
1412 u8 *src_addr;
1413 int copy_len;
1414 struct sas_task *task;
1415 struct scatterlist *sg;
1416 void *kaddr;
1417 int total_len = len;
1418
Dan Williams5076a1a2011-06-27 14:57:03 -07001419 ireq = to_ireq(stp_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07001420 task = isci_request_access_task(ireq);
1421 src_addr = data_buf;
1422
1423 if (task->num_scatter > 0) {
1424 sg = task->scatter;
1425
1426 while (total_len > 0) {
1427 struct page *page = sg_page(sg);
1428
1429 copy_len = min_t(int, total_len, sg_dma_len(sg));
Cong Wang77dfce02011-11-25 23:14:23 +08001430 kaddr = kmap_atomic(page);
Dan Williams5dec6f42011-05-10 02:28:49 -07001431 memcpy(kaddr + sg->offset, src_addr, copy_len);
Cong Wang77dfce02011-11-25 23:14:23 +08001432 kunmap_atomic(kaddr);
Dan Williams5dec6f42011-05-10 02:28:49 -07001433 total_len -= copy_len;
1434 src_addr += copy_len;
1435 sg = sg_next(sg);
1436 }
1437 } else {
1438 BUG_ON(task->total_xfer_len < total_len);
1439 memcpy(task->scatter, src_addr, total_len);
1440 }
1441
1442 return SCI_SUCCESS;
1443}
1444
1445/**
1446 *
1447 * @sci_req: The PIO DATA IN request that is to receive the data.
1448 * @data_buffer: The buffer to copy from.
1449 *
1450 * Copy the data buffer to the io request data region. enum sci_status
1451 */
Dan Williams89a73012011-06-30 19:14:33 -07001452static enum sci_status sci_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001453 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001454 u8 *data_buffer)
1455{
1456 enum sci_status status;
1457
1458 /*
1459 * If there is less than 1K remaining in the transfer request
1460 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001461 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams89a73012011-06-30 19:14:33 -07001462 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001463 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001464
1465 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001466 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001467 } else {
1468 /* We are transfering the whole frame so copy */
Dan Williams89a73012011-06-30 19:14:33 -07001469 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001470 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001471
1472 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001473 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001474 }
1475
1476 return status;
1477}
1478
Edmund Nadolskie3013702011-06-02 00:10:43 +00001479static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001480stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001481 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001482{
1483 enum sci_status status = SCI_SUCCESS;
1484
1485 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1486 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001487 ireq->scu_status = SCU_TASK_DONE_GOOD;
1488 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001489 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001490 break;
1491
1492 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001493 /* All other completion status cause the IO to be
1494 * complete. If a NAK was received, then it is up to
1495 * the user to retry the request.
1496 */
Dan Williams34a99152011-07-01 02:25:15 -07001497 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1498 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001499 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001500 break;
1501 }
1502
1503 return status;
1504}
1505
Edmund Nadolskie3013702011-06-02 00:10:43 +00001506static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001507pio_data_out_tx_done_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001508 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001509{
1510 enum sci_status status = SCI_SUCCESS;
1511 bool all_frames_transferred = false;
Dan Williams5076a1a2011-06-27 14:57:03 -07001512 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001513
1514 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1515 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1516 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001517 if (stp_req->pio_len != 0) {
Dan Williams89a73012011-06-30 19:14:33 -07001518 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001519 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001520 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001521 all_frames_transferred = true;
1522 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001523 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001524 /*
1525 * this will happen if the all data is written at the
1526 * first time after the pio setup fis is received
1527 */
1528 all_frames_transferred = true;
1529 }
1530
1531 /* all data transferred. */
1532 if (all_frames_transferred) {
1533 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001534 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001535 * and wait for PIO_SETUP fis / or D2H REg fis. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001536 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001537 }
1538 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001539
Dan Williams5dec6f42011-05-10 02:28:49 -07001540 default:
1541 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001542 * All other completion status cause the IO to be complete.
1543 * If a NAK was received, then it is up to the user to retry
1544 * the request.
1545 */
Dan Williams34a99152011-07-01 02:25:15 -07001546 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1547 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001548 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001549 break;
1550 }
1551
1552 return status;
1553}
1554
Dan Williams89a73012011-06-30 19:14:33 -07001555static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001556 u32 frame_index)
1557{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001558 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5dec6f42011-05-10 02:28:49 -07001559 struct dev_to_host_fis *frame_header;
1560 enum sci_status status;
1561 u32 *frame_buffer;
1562
Dan Williams89a73012011-06-30 19:14:33 -07001563 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001564 frame_index,
1565 (void **)&frame_header);
1566
1567 if ((status == SCI_SUCCESS) &&
1568 (frame_header->fis_type == FIS_REGD2H)) {
Dan Williams89a73012011-06-30 19:14:33 -07001569 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001570 frame_index,
1571 (void **)&frame_buffer);
1572
Dan Williams89a73012011-06-30 19:14:33 -07001573 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williams5dec6f42011-05-10 02:28:49 -07001574 frame_header,
1575 frame_buffer);
1576 }
1577
Dan Williams89a73012011-06-30 19:14:33 -07001578 sci_controller_release_frame(ihost, frame_index);
Dan Williams5dec6f42011-05-10 02:28:49 -07001579
1580 return status;
1581}
1582
Dan Williamsb50102d2011-09-30 18:52:19 -07001583static enum sci_status process_unsolicited_fis(struct isci_request *ireq,
1584 u32 frame_index)
1585{
1586 struct isci_host *ihost = ireq->owning_controller;
1587 enum sci_status status;
1588 struct dev_to_host_fis *frame_header;
1589 u32 *frame_buffer;
1590
1591 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1592 frame_index,
1593 (void **)&frame_header);
1594
1595 if (status != SCI_SUCCESS)
1596 return status;
1597
1598 if (frame_header->fis_type != FIS_REGD2H) {
1599 dev_err(&ireq->isci_host->pdev->dev,
1600 "%s ERROR: invalid fis type 0x%X\n",
1601 __func__, frame_header->fis_type);
1602 return SCI_FAILURE;
1603 }
1604
1605 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1606 frame_index,
1607 (void **)&frame_buffer);
1608
1609 sci_controller_copy_sata_response(&ireq->stp.rsp,
1610 (u32 *)frame_header,
1611 frame_buffer);
1612
1613 /* Frame has been decoded return it to the controller */
1614 sci_controller_release_frame(ihost, frame_index);
1615
1616 return status;
1617}
1618
1619static enum sci_status atapi_d2h_reg_frame_handler(struct isci_request *ireq,
1620 u32 frame_index)
1621{
1622 struct sas_task *task = isci_request_access_task(ireq);
1623 enum sci_status status;
1624
1625 status = process_unsolicited_fis(ireq, frame_index);
1626
1627 if (status == SCI_SUCCESS) {
1628 if (ireq->stp.rsp.status & ATA_ERR)
1629 status = SCI_IO_FAILURE_RESPONSE_VALID;
1630 } else {
1631 status = SCI_IO_FAILURE_RESPONSE_VALID;
1632 }
1633
1634 if (status != SCI_SUCCESS) {
1635 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1636 ireq->sci_status = status;
1637 } else {
1638 ireq->scu_status = SCU_TASK_DONE_GOOD;
1639 ireq->sci_status = SCI_SUCCESS;
1640 }
1641
1642 /* the d2h ufi is the end of non-data commands */
1643 if (task->data_dir == DMA_NONE)
1644 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1645
1646 return status;
1647}
1648
1649static void scu_atapi_reconstruct_raw_frame_task_context(struct isci_request *ireq)
1650{
1651 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1652 void *atapi_cdb = ireq->ttype_ptr.io_task_ptr->ata_task.atapi_packet;
1653 struct scu_task_context *task_context = ireq->tc;
1654
1655 /* fill in the SCU Task Context for a DATA fis containing CDB in Raw Frame
1656 * type. The TC for previous Packet fis was already there, we only need to
1657 * change the H2D fis content.
1658 */
1659 memset(&ireq->stp.cmd, 0, sizeof(struct host_to_dev_fis));
1660 memcpy(((u8 *)&ireq->stp.cmd + sizeof(u32)), atapi_cdb, ATAPI_CDB_LEN);
1661 memset(&(task_context->type.stp), 0, sizeof(struct stp_task_context));
1662 task_context->type.stp.fis_type = FIS_DATA;
1663 task_context->transfer_length_bytes = dev->cdb_len;
1664}
1665
1666static void scu_atapi_construct_task_context(struct isci_request *ireq)
1667{
1668 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1669 struct sas_task *task = isci_request_access_task(ireq);
1670 struct scu_task_context *task_context = ireq->tc;
1671 int cdb_len = dev->cdb_len;
1672
1673 /* reference: SSTL 1.13.4.2
1674 * task_type, sata_direction
1675 */
1676 if (task->data_dir == DMA_TO_DEVICE) {
1677 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_OUT;
1678 task_context->sata_direction = 0;
1679 } else {
1680 /* todo: for NO_DATA command, we need to send out raw frame. */
1681 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_IN;
1682 task_context->sata_direction = 1;
1683 }
1684
1685 memset(&task_context->type.stp, 0, sizeof(task_context->type.stp));
1686 task_context->type.stp.fis_type = FIS_DATA;
1687
1688 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
1689 memcpy(&ireq->stp.cmd.lbal, task->ata_task.atapi_packet, cdb_len);
1690 task_context->ssp_command_iu_length = cdb_len / sizeof(u32);
1691
1692 /* task phase is set to TX_CMD */
1693 task_context->task_phase = 0x1;
1694
1695 /* retry counter */
1696 task_context->stp_retry_count = 0;
1697
1698 /* data transfer size. */
1699 task_context->transfer_length_bytes = task->total_xfer_len;
1700
1701 /* setup sgl */
1702 sci_request_build_sgl(ireq);
1703}
1704
Edmund Nadolskie3013702011-06-02 00:10:43 +00001705enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001706sci_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001707 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001708{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001709 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5076a1a2011-06-27 14:57:03 -07001710 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001711 enum sci_base_request_states state;
1712 enum sci_status status;
1713 ssize_t word_cnt;
1714
Dan Williams5076a1a2011-06-27 14:57:03 -07001715 state = ireq->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001716 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001717 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001718 struct ssp_frame_hdr ssp_hdr;
1719 void *frame_header;
1720
Dan Williams89a73012011-06-30 19:14:33 -07001721 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001722 frame_index,
1723 &frame_header);
1724
1725 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1726 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1727
1728 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1729 struct ssp_response_iu *resp_iu;
1730 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1731
Dan Williams89a73012011-06-30 19:14:33 -07001732 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001733 frame_index,
1734 (void **)&resp_iu);
1735
Dan Williams5076a1a2011-06-27 14:57:03 -07001736 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001737
Dan Williams5076a1a2011-06-27 14:57:03 -07001738 resp_iu = &ireq->ssp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001739
1740 if (resp_iu->datapres == 0x01 ||
1741 resp_iu->datapres == 0x02) {
Dan Williams34a99152011-07-01 02:25:15 -07001742 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1743 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1744 } else {
1745 ireq->scu_status = SCU_TASK_DONE_GOOD;
1746 ireq->sci_status = SCI_SUCCESS;
1747 }
Dan Williamsd1c637c32011-05-11 08:27:47 -07001748 } else {
1749 /* not a response frame, why did it get forwarded? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001750 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001751 "%s: SCIC IO Request 0x%p received unexpected "
Dan Williams5076a1a2011-06-27 14:57:03 -07001752 "frame %d type 0x%02x\n", __func__, ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001753 frame_index, ssp_hdr.frame_type);
1754 }
1755
1756 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001757 * In any case we are done with this frame buffer return it to
1758 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001759 */
Dan Williams89a73012011-06-30 19:14:33 -07001760 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001761
1762 return SCI_SUCCESS;
1763 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001764
1765 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams89a73012011-06-30 19:14:33 -07001766 sci_io_request_copy_response(ireq);
Dan Williams5076a1a2011-06-27 14:57:03 -07001767 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams89a73012011-06-30 19:14:33 -07001768 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001769 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001770
1771 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williams54b5e3a2011-09-28 18:35:27 -07001772 struct sas_task *task = isci_request_access_task(ireq);
1773 struct scatterlist *sg = &task->smp_task.smp_resp;
1774 void *frame_header, *kaddr;
1775 u8 *rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001776
Dan Williams89a73012011-06-30 19:14:33 -07001777 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001778 frame_index,
1779 &frame_header);
Cong Wang77dfce02011-11-25 23:14:23 +08001780 kaddr = kmap_atomic(sg_page(sg));
Dan Williams54b5e3a2011-09-28 18:35:27 -07001781 rsp = kaddr + sg->offset;
1782 sci_swab32_cpy(rsp, frame_header, 1);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001783
Dan Williams54b5e3a2011-09-28 18:35:27 -07001784 if (rsp[0] == SMP_RESPONSE) {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001785 void *smp_resp;
1786
Dan Williams89a73012011-06-30 19:14:33 -07001787 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001788 frame_index,
1789 &smp_resp);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001790
Dan Williams54b5e3a2011-09-28 18:35:27 -07001791 word_cnt = (sg->length/4)-1;
1792 if (word_cnt > 0)
1793 word_cnt = min_t(unsigned int, word_cnt,
1794 SCU_UNSOLICITED_FRAME_BUFFER_SIZE/4);
1795 sci_swab32_cpy(rsp + 4, smp_resp, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001796
Dan Williams34a99152011-07-01 02:25:15 -07001797 ireq->scu_status = SCU_TASK_DONE_GOOD;
1798 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001799 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001800 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001801 /*
1802 * This was not a response frame why did it get
1803 * forwarded?
1804 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001805 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001806 "%s: SCIC SMP Request 0x%p received unexpected "
1807 "frame %d type 0x%02x\n",
1808 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -07001809 ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001810 frame_index,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001811 rsp[0]);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001812
Dan Williams34a99152011-07-01 02:25:15 -07001813 ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
1814 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001815 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001816 }
Cong Wang77dfce02011-11-25 23:14:23 +08001817 kunmap_atomic(kaddr);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001818
Dan Williams89a73012011-06-30 19:14:33 -07001819 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001820
1821 return SCI_SUCCESS;
1822 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001823
1824 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams89a73012011-06-30 19:14:33 -07001825 return sci_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001826 frame_index);
1827
1828 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001829 /* Use the general frame handler to copy the resposne data */
Dan Williams34a99152011-07-01 02:25:15 -07001830 status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001831
1832 if (status != SCI_SUCCESS)
1833 return status;
1834
Dan Williams34a99152011-07-01 02:25:15 -07001835 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1836 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1837 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001838 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001839
1840 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001841 struct dev_to_host_fis *frame_header;
1842 u32 *frame_buffer;
1843
Dan Williams89a73012011-06-30 19:14:33 -07001844 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001845 frame_index,
1846 (void **)&frame_header);
1847
1848 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001849 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001850 "%s: SCIC IO Request 0x%p could not get frame "
1851 "header for frame index %d, status %x\n",
1852 __func__,
1853 stp_req,
1854 frame_index,
1855 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001856
1857 return status;
1858 }
1859
1860 switch (frame_header->fis_type) {
1861 case FIS_REGD2H:
Dan Williams89a73012011-06-30 19:14:33 -07001862 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001863 frame_index,
1864 (void **)&frame_buffer);
1865
Dan Williams89a73012011-06-30 19:14:33 -07001866 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001867 frame_header,
1868 frame_buffer);
1869
1870 /* The command has completed with error */
Dan Williams34a99152011-07-01 02:25:15 -07001871 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1872 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001873 break;
1874
1875 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001876 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001877 "%s: IO Request:0x%p Frame Id:%d protocol "
1878 "violation occurred\n", __func__, stp_req,
1879 frame_index);
1880
Dan Williams34a99152011-07-01 02:25:15 -07001881 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1882 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001883 break;
1884 }
1885
Dan Williams5076a1a2011-06-27 14:57:03 -07001886 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001887
1888 /* Frame has been decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001889 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001890
1891 return status;
1892 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001893
1894 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001895 struct sas_task *task = isci_request_access_task(ireq);
1896 struct dev_to_host_fis *frame_header;
1897 u32 *frame_buffer;
1898
Dan Williams89a73012011-06-30 19:14:33 -07001899 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001900 frame_index,
1901 (void **)&frame_header);
1902
1903 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001904 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001905 "%s: SCIC IO Request 0x%p could not get frame "
1906 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001907 __func__, stp_req, frame_index, status);
1908 return status;
1909 }
1910
1911 switch (frame_header->fis_type) {
1912 case FIS_PIO_SETUP:
1913 /* Get from the frame buffer the PIO Setup Data */
Dan Williams89a73012011-06-30 19:14:33 -07001914 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001915 frame_index,
1916 (void **)&frame_buffer);
1917
Edmund Nadolskie3013702011-06-02 00:10:43 +00001918 /* Get the data from the PIO Setup The SCU Hardware
1919 * returns first word in the frame_header and the rest
1920 * of the data is in the frame buffer so we need to
1921 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001922 */
1923
1924 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001925 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001926
Dan Williamsba7cb222011-06-27 11:56:41 -07001927 /* status: 4th byte in the 3rd dword */
1928 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001929
Dan Williams89a73012011-06-30 19:14:33 -07001930 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001931 frame_header,
1932 frame_buffer);
1933
Dan Williams5076a1a2011-06-27 14:57:03 -07001934 ireq->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001935
1936 /* The next state is dependent on whether the
1937 * request was PIO Data-in or Data out
1938 */
1939 if (task->data_dir == DMA_FROM_DEVICE) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001940 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001941 } else if (task->data_dir == DMA_TO_DEVICE) {
1942 /* Transmit data */
Dan Williams89a73012011-06-30 19:14:33 -07001943 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001944 if (status != SCI_SUCCESS)
1945 break;
Dan Williams5076a1a2011-06-27 14:57:03 -07001946 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001947 }
1948 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001949
Dan Williamsd1c637c32011-05-11 08:27:47 -07001950 case FIS_SETDEVBITS:
Dan Williams5076a1a2011-06-27 14:57:03 -07001951 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001952 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001953
Dan Williamsd1c637c32011-05-11 08:27:47 -07001954 case FIS_REGD2H:
1955 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001956 /*
1957 * Now why is the drive sending a D2H Register
1958 * FIS when it is still busy? Do nothing since
1959 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001960 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001961 dev_dbg(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001962 "%s: SCIC PIO Request 0x%p received "
1963 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001964 "0x%x\n",
1965 __func__,
1966 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001967 frame_header->status);
1968 break;
1969 }
1970
Dan Williams89a73012011-06-30 19:14:33 -07001971 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001972 frame_index,
1973 (void **)&frame_buffer);
1974
Dan Williams89a73012011-06-30 19:14:33 -07001975 sci_controller_copy_sata_response(&ireq->stp.req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001976 frame_header,
1977 frame_buffer);
1978
Dan Williams34a99152011-07-01 02:25:15 -07001979 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1980 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07001981 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001982 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001983
Dan Williamsd1c637c32011-05-11 08:27:47 -07001984 default:
1985 /* FIXME: what do we do here? */
1986 break;
1987 }
1988
1989 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001990 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001991
1992 return status;
1993 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001994
1995 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001996 struct dev_to_host_fis *frame_header;
1997 struct sata_fis_data *frame_buffer;
1998
Dan Williams89a73012011-06-30 19:14:33 -07001999 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002000 frame_index,
2001 (void **)&frame_header);
2002
2003 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002004 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002005 "%s: SCIC IO Request 0x%p could not get frame "
2006 "header for frame index %d, status %x\n",
2007 __func__,
2008 stp_req,
2009 frame_index,
2010 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002011 return status;
2012 }
2013
2014 if (frame_header->fis_type != FIS_DATA) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002015 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002016 "%s: SCIC PIO Request 0x%p received frame %d "
2017 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00002018 "fis.\n",
2019 __func__,
2020 stp_req,
2021 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002022 frame_header->fis_type);
2023
Dan Williams34a99152011-07-01 02:25:15 -07002024 ireq->scu_status = SCU_TASK_DONE_GOOD;
2025 ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
Dan Williams5076a1a2011-06-27 14:57:03 -07002026 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002027
2028 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07002029 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002030 return status;
2031 }
2032
Dan Williamsba7cb222011-06-27 11:56:41 -07002033 if (stp_req->sgl.index < 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002034 ireq->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07002035 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07002036 } else {
Dan Williams89a73012011-06-30 19:14:33 -07002037 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002038 frame_index,
2039 (void **)&frame_buffer);
2040
Dan Williams89a73012011-06-30 19:14:33 -07002041 status = sci_stp_request_pio_data_in_copy_data(stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002042 (u8 *)frame_buffer);
2043
2044 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07002045 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002046 }
2047
2048 /* Check for the end of the transfer, are there more
2049 * bytes remaining for this data transfer
2050 */
Dan Williamsba7cb222011-06-27 11:56:41 -07002051 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07002052 return status;
2053
Dan Williamsba7cb222011-06-27 11:56:41 -07002054 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -07002055 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2056 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07002057 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002058 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -07002059 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002060 }
2061 return status;
2062 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00002063
Dan Williamsb50102d2011-09-30 18:52:19 -07002064 case SCI_REQ_ATAPI_WAIT_PIO_SETUP: {
2065 struct sas_task *task = isci_request_access_task(ireq);
2066
2067 sci_controller_release_frame(ihost, frame_index);
2068 ireq->target_device->working_request = ireq;
2069 if (task->data_dir == DMA_NONE) {
2070 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_TC_COMP);
2071 scu_atapi_reconstruct_raw_frame_task_context(ireq);
2072 } else {
2073 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2074 scu_atapi_construct_task_context(ireq);
2075 }
2076
2077 sci_controller_continue_io(ireq);
2078 return SCI_SUCCESS;
2079 }
2080 case SCI_REQ_ATAPI_WAIT_D2H:
2081 return atapi_d2h_reg_frame_handler(ireq, frame_index);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002082 case SCI_REQ_ABORTING:
2083 /*
2084 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07002085 * aborting state?
2086 */
Dan Williams89a73012011-06-30 19:14:33 -07002087 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002088 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002089
Dan Williamsd1c637c32011-05-11 08:27:47 -07002090 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002091 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002092 "%s: SCIC IO Request given unexpected frame %x while "
2093 "in state %d\n",
2094 __func__,
2095 frame_index,
2096 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002097
Dan Williams89a73012011-06-30 19:14:33 -07002098 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002099 return SCI_FAILURE_INVALID_STATE;
2100 }
2101}
2102
Dan Williams5076a1a2011-06-27 14:57:03 -07002103static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07002104 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002105{
2106 enum sci_status status = SCI_SUCCESS;
2107
2108 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2109 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07002110 ireq->scu_status = SCU_TASK_DONE_GOOD;
2111 ireq->sci_status = SCI_SUCCESS;
2112 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002113 break;
2114 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
2115 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07002116 /* We must check ther response buffer to see if the D2H
2117 * Register FIS was received before we got the TC
2118 * completion.
2119 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002120 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
Dan Williams34a99152011-07-01 02:25:15 -07002121 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2122 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2123 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002124 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07002125 /* If we have an error completion status for the
2126 * TC then we can expect a D2H register FIS from
2127 * the device so we must change state to wait
2128 * for it
2129 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002130 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07002131 }
2132 break;
2133
Dan Williamsa7e255a2011-05-11 08:27:47 -07002134 /* TODO Check to see if any of these completion status need to
2135 * wait for the device to host register fis.
2136 */
2137 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
2138 * - this comes only for B0
2139 */
Dan Williams5dec6f42011-05-10 02:28:49 -07002140 default:
2141 /* All other completion status cause the IO to be complete. */
Dan Williams34a99152011-07-01 02:25:15 -07002142 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2143 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2144 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002145 break;
2146 }
2147
2148 return status;
2149}
2150
Dan Williamsb50102d2011-09-30 18:52:19 -07002151static enum sci_status atapi_raw_completion(struct isci_request *ireq, u32 completion_code,
2152 enum sci_base_request_states next)
2153{
2154 enum sci_status status = SCI_SUCCESS;
2155
2156 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2157 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2158 ireq->scu_status = SCU_TASK_DONE_GOOD;
2159 ireq->sci_status = SCI_SUCCESS;
2160 sci_change_state(&ireq->sm, next);
2161 break;
2162 default:
2163 /* All other completion status cause the IO to be complete.
2164 * If a NAK was received, then it is up to the user to retry
2165 * the request.
2166 */
2167 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2168 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2169
2170 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2171 break;
2172 }
2173
2174 return status;
2175}
2176
2177static enum sci_status atapi_data_tc_completion_handler(struct isci_request *ireq,
2178 u32 completion_code)
2179{
2180 struct isci_remote_device *idev = ireq->target_device;
2181 struct dev_to_host_fis *d2h = &ireq->stp.rsp;
2182 enum sci_status status = SCI_SUCCESS;
2183
2184 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2185 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
2186 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2187 break;
2188
2189 case (SCU_TASK_DONE_UNEXP_FIS << SCU_COMPLETION_TL_STATUS_SHIFT): {
2190 u16 len = sci_req_tx_bytes(ireq);
2191
2192 /* likely non-error data underrrun, workaround missing
2193 * d2h frame from the controller
2194 */
2195 if (d2h->fis_type != FIS_REGD2H) {
2196 d2h->fis_type = FIS_REGD2H;
2197 d2h->flags = (1 << 6);
2198 d2h->status = 0x50;
2199 d2h->error = 0;
2200 d2h->lbal = 0;
2201 d2h->byte_count_low = len & 0xff;
2202 d2h->byte_count_high = len >> 8;
2203 d2h->device = 0xa0;
2204 d2h->lbal_exp = 0;
2205 d2h->lbam_exp = 0;
2206 d2h->lbah_exp = 0;
2207 d2h->_r_a = 0;
2208 d2h->sector_count = 0x3;
2209 d2h->sector_count_exp = 0;
2210 d2h->_r_b = 0;
2211 d2h->_r_c = 0;
2212 d2h->_r_d = 0;
2213 }
2214
2215 ireq->scu_status = SCU_TASK_DONE_GOOD;
2216 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
2217 status = ireq->sci_status;
2218
2219 /* the hw will have suspended the rnc, so complete the
2220 * request upon pending resume
2221 */
2222 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2223 break;
2224 }
2225 case (SCU_TASK_DONE_EXCESS_DATA << SCU_COMPLETION_TL_STATUS_SHIFT):
2226 /* In this case, there is no UF coming after.
2227 * compelte the IO now.
2228 */
2229 ireq->scu_status = SCU_TASK_DONE_GOOD;
2230 ireq->sci_status = SCI_SUCCESS;
2231 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2232 break;
2233
2234 default:
2235 if (d2h->fis_type == FIS_REGD2H) {
2236 /* UF received change the device state to ATAPI_ERROR */
2237 status = ireq->sci_status;
2238 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2239 } else {
2240 /* If receiving any non-sucess TC status, no UF
2241 * received yet, then an UF for the status fis
2242 * is coming after (XXX: suspect this is
2243 * actually a protocol error or a bug like the
2244 * DONE_UNEXP_FIS case)
2245 */
2246 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2247 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2248
2249 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2250 }
2251 break;
2252 }
2253
2254 return status;
2255}
2256
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002257static int sci_request_smp_completion_status_is_tx_suspend(
2258 unsigned int completion_status)
2259{
2260 switch (completion_status) {
2261 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2262 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2263 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2264 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2265 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2266 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2267 return 1;
2268 }
2269 return 0;
2270}
2271
2272static int sci_request_smp_completion_status_is_tx_rx_suspend(
2273 unsigned int completion_status)
2274{
2275 return 0; /* There are no Tx/Rx SMP suspend conditions. */
2276}
2277
2278static int sci_request_ssp_completion_status_is_tx_suspend(
2279 unsigned int completion_status)
2280{
2281 switch (completion_status) {
2282 case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2283 case SCU_TASK_DONE_LF_ERR:
2284 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2285 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2286 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2287 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2288 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2289 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2290 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2291 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2292 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2293 return 1;
2294 }
2295 return 0;
2296}
2297
2298static int sci_request_ssp_completion_status_is_tx_rx_suspend(
2299 unsigned int completion_status)
2300{
2301 return 0; /* There are no Tx/Rx SSP suspend conditions. */
2302}
2303
2304static int sci_request_stpsata_completion_status_is_tx_suspend(
2305 unsigned int completion_status)
2306{
2307 switch (completion_status) {
2308 case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2309 case SCU_TASK_DONE_LL_R_ERR:
2310 case SCU_TASK_DONE_LL_PERR:
2311 case SCU_TASK_DONE_REG_ERR:
2312 case SCU_TASK_DONE_SDB_ERR:
2313 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2314 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2315 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2316 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2317 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2318 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2319 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2320 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2321 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2322 return 1;
2323 }
2324 return 0;
2325}
2326
2327
2328static int sci_request_stpsata_completion_status_is_tx_rx_suspend(
2329 unsigned int completion_status)
2330{
2331 switch (completion_status) {
2332 case SCU_TASK_DONE_LF_ERR:
2333 case SCU_TASK_DONE_LL_SY_TERM:
2334 case SCU_TASK_DONE_LL_LF_TERM:
2335 case SCU_TASK_DONE_BREAK_RCVD:
2336 case SCU_TASK_DONE_INV_FIS_LEN:
2337 case SCU_TASK_DONE_UNEXP_FIS:
2338 case SCU_TASK_DONE_UNEXP_SDBFIS:
2339 case SCU_TASK_DONE_MAX_PLD_ERR:
2340 return 1;
2341 }
2342 return 0;
2343}
2344
2345static void sci_request_handle_suspending_completions(
2346 struct isci_request *ireq,
2347 u32 completion_code)
2348{
2349 int is_tx = 0;
2350 int is_tx_rx = 0;
2351
2352 switch (ireq->protocol) {
2353 case SAS_PROTOCOL_SMP:
2354 is_tx = sci_request_smp_completion_status_is_tx_suspend(
2355 completion_code);
2356 is_tx_rx = sci_request_smp_completion_status_is_tx_rx_suspend(
2357 completion_code);
2358 break;
2359 case SAS_PROTOCOL_SSP:
2360 is_tx = sci_request_ssp_completion_status_is_tx_suspend(
2361 completion_code);
2362 is_tx_rx = sci_request_ssp_completion_status_is_tx_rx_suspend(
2363 completion_code);
2364 break;
2365 case SAS_PROTOCOL_STP:
2366 is_tx = sci_request_stpsata_completion_status_is_tx_suspend(
2367 completion_code);
2368 is_tx_rx =
2369 sci_request_stpsata_completion_status_is_tx_rx_suspend(
2370 completion_code);
2371 break;
2372 default:
2373 dev_warn(&ireq->isci_host->pdev->dev,
2374 "%s: request %p has no valid protocol\n",
2375 __func__, ireq);
2376 break;
2377 }
2378 if (is_tx || is_tx_rx) {
2379 BUG_ON(is_tx && is_tx_rx);
2380
2381 sci_remote_node_context_suspend(
2382 &ireq->target_device->rnc,
2383 SCU_HARDWARE_SUSPENSION,
2384 (is_tx_rx) ? SCU_EVENT_TL_RNC_SUSPEND_TX_RX
2385 : SCU_EVENT_TL_RNC_SUSPEND_TX,
2386 NULL, NULL);
2387 }
2388}
2389
Dan Williamsa7e255a2011-05-11 08:27:47 -07002390enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07002391sci_io_request_tc_completion(struct isci_request *ireq,
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002392 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07002393{
2394 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002395 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002396
Dan Williams5076a1a2011-06-27 14:57:03 -07002397 state = ireq->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002398
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002399 /* Decode those completions that signal upcoming suspension events. */
2400 sci_request_handle_suspending_completions(
2401 ireq, SCU_GET_COMPLETION_TL_STATUS(completion_code));
2402
Dan Williamsa7e255a2011-05-11 08:27:47 -07002403 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002404 case SCI_REQ_STARTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002405 return request_started_state_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002406
2407 case SCI_REQ_TASK_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002408 return ssp_task_request_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002409 completion_code);
2410
2411 case SCI_REQ_SMP_WAIT_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002412 return smp_request_await_response_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002413 completion_code);
2414
2415 case SCI_REQ_SMP_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002416 return smp_request_await_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002417
2418 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002419 return stp_request_udma_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002420 completion_code);
2421
2422 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002423 return stp_request_non_data_await_h2d_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002424 completion_code);
2425
2426 case SCI_REQ_STP_PIO_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002427 return stp_request_pio_await_h2d_completion_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002428 completion_code);
2429
2430 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williams5076a1a2011-06-27 14:57:03 -07002431 return pio_data_out_tx_done_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002432
Edmund Nadolskie3013702011-06-02 00:10:43 +00002433 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -07002434 return request_aborting_state_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002435 completion_code);
2436
Dan Williamsb50102d2011-09-30 18:52:19 -07002437 case SCI_REQ_ATAPI_WAIT_H2D:
2438 return atapi_raw_completion(ireq, completion_code,
2439 SCI_REQ_ATAPI_WAIT_PIO_SETUP);
2440
2441 case SCI_REQ_ATAPI_WAIT_TC_COMP:
2442 return atapi_raw_completion(ireq, completion_code,
2443 SCI_REQ_ATAPI_WAIT_D2H);
2444
2445 case SCI_REQ_ATAPI_WAIT_D2H:
2446 return atapi_data_tc_completion_handler(ireq, completion_code);
2447
Edmund Nadolskie3013702011-06-02 00:10:43 +00002448 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08002449 dev_warn(&ihost->pdev->dev, "%s: %x in wrong state %s\n",
2450 __func__, completion_code, req_state_name(state));
Edmund Nadolskie3013702011-06-02 00:10:43 +00002451 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002452 }
2453}
2454
Dan Williams6f231dd2011-07-02 22:56:22 -07002455/**
2456 * isci_request_process_response_iu() - This function sets the status and
2457 * response iu, in the task struct, from the request object for the upper
2458 * layer driver.
2459 * @sas_task: This parameter is the task struct from the upper layer driver.
2460 * @resp_iu: This parameter points to the response iu of the completed request.
2461 * @dev: This parameter specifies the linux device struct.
2462 *
2463 * none.
2464 */
2465static void isci_request_process_response_iu(
2466 struct sas_task *task,
2467 struct ssp_response_iu *resp_iu,
2468 struct device *dev)
2469{
2470 dev_dbg(dev,
2471 "%s: resp_iu = %p "
2472 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2473 "resp_iu->response_data_len = %x, "
2474 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2475 __func__,
2476 resp_iu,
2477 resp_iu->status,
2478 resp_iu->datapres,
2479 resp_iu->response_data_len,
2480 resp_iu->sense_data_len);
2481
2482 task->task_status.stat = resp_iu->status;
2483
2484 /* libsas updates the task status fields based on the response iu. */
2485 sas_ssp_task_response(dev, task, resp_iu);
2486}
2487
2488/**
2489 * isci_request_set_open_reject_status() - This function prepares the I/O
2490 * completion for OPEN_REJECT conditions.
2491 * @request: This parameter is the completed isci_request object.
2492 * @response_ptr: This parameter specifies the service response for the I/O.
2493 * @status_ptr: This parameter specifies the exec status for the I/O.
2494 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2495 * the LLDD with respect to completing this request or forcing an abort
2496 * condition on the I/O.
2497 * @open_rej_reason: This parameter specifies the encoded reason for the
2498 * abandon-class reject.
2499 *
2500 * none.
2501 */
2502static void isci_request_set_open_reject_status(
2503 struct isci_request *request,
2504 struct sas_task *task,
2505 enum service_response *response_ptr,
2506 enum exec_status *status_ptr,
2507 enum isci_completion_selection *complete_to_host_ptr,
2508 enum sas_open_rej_reason open_rej_reason)
2509{
2510 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002511 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002512 *response_ptr = SAS_TASK_UNDELIVERED;
2513 *status_ptr = SAS_OPEN_REJECT;
2514 *complete_to_host_ptr = isci_perform_normal_io_completion;
2515 task->task_status.open_rej_reason = open_rej_reason;
2516}
2517
2518/**
2519 * isci_request_handle_controller_specific_errors() - This function decodes
2520 * controller-specific I/O completion error conditions.
2521 * @request: This parameter is the completed isci_request object.
2522 * @response_ptr: This parameter specifies the service response for the I/O.
2523 * @status_ptr: This parameter specifies the exec status for the I/O.
2524 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2525 * the LLDD with respect to completing this request or forcing an abort
2526 * condition on the I/O.
2527 *
2528 * none.
2529 */
2530static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002531 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002532 struct isci_request *request,
2533 struct sas_task *task,
2534 enum service_response *response_ptr,
2535 enum exec_status *status_ptr,
2536 enum isci_completion_selection *complete_to_host_ptr)
2537{
2538 unsigned int cstatus;
2539
Dan Williams5076a1a2011-06-27 14:57:03 -07002540 cstatus = request->scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002541
2542 dev_dbg(&request->isci_host->pdev->dev,
2543 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2544 "- controller status = 0x%x\n",
2545 __func__, request, cstatus);
2546
2547 /* Decode the controller-specific errors; most
2548 * important is to recognize those conditions in which
2549 * the target may still have a task outstanding that
2550 * must be aborted.
2551 *
2552 * Note that there are SCU completion codes being
2553 * named in the decode below for which SCIC has already
2554 * done work to handle them in a way other than as
2555 * a controller-specific completion code; these are left
2556 * in the decode below for completeness sake.
2557 */
2558 switch (cstatus) {
2559 case SCU_TASK_DONE_DMASETUP_DIRERR:
2560 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2561 case SCU_TASK_DONE_XFERCNT_ERR:
2562 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2563 if (task->task_proto == SAS_PROTOCOL_SMP) {
2564 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2565 *response_ptr = SAS_TASK_COMPLETE;
2566
2567 /* See if the device has been/is being stopped. Note
2568 * that we ignore the quiesce state, since we are
2569 * concerned about the actual device state.
2570 */
Dan Williams209fae12011-06-13 17:39:44 -07002571 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002572 *status_ptr = SAS_DEVICE_UNKNOWN;
2573 else
2574 *status_ptr = SAS_ABORTED_TASK;
2575
Dan Williams38d88792011-06-23 14:33:48 -07002576 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002577
2578 *complete_to_host_ptr =
2579 isci_perform_normal_io_completion;
2580 } else {
2581 /* Task in the target is not done. */
2582 *response_ptr = SAS_TASK_UNDELIVERED;
2583
Dan Williams209fae12011-06-13 17:39:44 -07002584 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002585 *status_ptr = SAS_DEVICE_UNKNOWN;
2586 else
2587 *status_ptr = SAM_STAT_TASK_ABORTED;
2588
Dan Williams38d88792011-06-23 14:33:48 -07002589 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002590
2591 *complete_to_host_ptr =
2592 isci_perform_error_io_completion;
2593 }
2594
2595 break;
2596
2597 case SCU_TASK_DONE_CRC_ERR:
2598 case SCU_TASK_DONE_NAK_CMD_ERR:
2599 case SCU_TASK_DONE_EXCESS_DATA:
2600 case SCU_TASK_DONE_UNEXP_FIS:
2601 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2602 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2603 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2604 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2605 /* These are conditions in which the target
2606 * has completed the task, so that no cleanup
2607 * is necessary.
2608 */
2609 *response_ptr = SAS_TASK_COMPLETE;
2610
2611 /* See if the device has been/is being stopped. Note
2612 * that we ignore the quiesce state, since we are
2613 * concerned about the actual device state.
2614 */
Dan Williams209fae12011-06-13 17:39:44 -07002615 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002616 *status_ptr = SAS_DEVICE_UNKNOWN;
2617 else
2618 *status_ptr = SAS_ABORTED_TASK;
2619
Dan Williams38d88792011-06-23 14:33:48 -07002620 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002621
2622 *complete_to_host_ptr = isci_perform_normal_io_completion;
2623 break;
2624
2625
2626 /* Note that the only open reject completion codes seen here will be
2627 * abandon-class codes; all others are automatically retried in the SCU.
2628 */
2629 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2630
2631 isci_request_set_open_reject_status(
2632 request, task, response_ptr, status_ptr,
2633 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2634 break;
2635
2636 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2637
2638 /* Note - the return of AB0 will change when
2639 * libsas implements detection of zone violations.
2640 */
2641 isci_request_set_open_reject_status(
2642 request, task, response_ptr, status_ptr,
2643 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2644 break;
2645
2646 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2647
2648 isci_request_set_open_reject_status(
2649 request, task, response_ptr, status_ptr,
2650 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2651 break;
2652
2653 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2654
2655 isci_request_set_open_reject_status(
2656 request, task, response_ptr, status_ptr,
2657 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2658 break;
2659
2660 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2661
2662 isci_request_set_open_reject_status(
2663 request, task, response_ptr, status_ptr,
2664 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2665 break;
2666
2667 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2668
2669 isci_request_set_open_reject_status(
2670 request, task, response_ptr, status_ptr,
2671 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2672 break;
2673
2674 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2675
2676 isci_request_set_open_reject_status(
2677 request, task, response_ptr, status_ptr,
2678 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2679 break;
2680
2681 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2682
2683 isci_request_set_open_reject_status(
2684 request, task, response_ptr, status_ptr,
2685 complete_to_host_ptr, SAS_OREJ_EPROTO);
2686 break;
2687
2688 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2689
2690 isci_request_set_open_reject_status(
2691 request, task, response_ptr, status_ptr,
2692 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2693 break;
2694
2695 case SCU_TASK_DONE_LL_R_ERR:
2696 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2697 case SCU_TASK_DONE_LL_PERR:
2698 case SCU_TASK_DONE_LL_SY_TERM:
2699 /* Also SCU_TASK_DONE_NAK_ERR:*/
2700 case SCU_TASK_DONE_LL_LF_TERM:
2701 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2702 case SCU_TASK_DONE_LL_ABORT_ERR:
2703 case SCU_TASK_DONE_SEQ_INV_TYPE:
2704 /* Also SCU_TASK_DONE_UNEXP_XR: */
2705 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2706 case SCU_TASK_DONE_INV_FIS_LEN:
2707 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2708 case SCU_TASK_DONE_SDMA_ERR:
2709 case SCU_TASK_DONE_OFFSET_ERR:
2710 case SCU_TASK_DONE_MAX_PLD_ERR:
2711 case SCU_TASK_DONE_LF_ERR:
2712 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2713 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2714 case SCU_TASK_DONE_UNEXP_DATA:
2715 case SCU_TASK_DONE_UNEXP_SDBFIS:
2716 case SCU_TASK_DONE_REG_ERR:
2717 case SCU_TASK_DONE_SDB_ERR:
2718 case SCU_TASK_DONE_TASK_ABORT:
2719 default:
2720 /* Task in the target is not done. */
2721 *response_ptr = SAS_TASK_UNDELIVERED;
2722 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002723
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002724 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002725 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002726
2727 *complete_to_host_ptr = isci_perform_normal_io_completion;
2728 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002729 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002730
2731 *complete_to_host_ptr = isci_perform_error_io_completion;
2732 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002733 break;
2734 }
2735}
2736
2737/**
2738 * isci_task_save_for_upper_layer_completion() - This function saves the
2739 * request for later completion to the upper layer driver.
2740 * @host: This parameter is a pointer to the host on which the the request
2741 * should be queued (either as an error or success).
2742 * @request: This parameter is the completed request.
2743 * @response: This parameter is the response code for the completed task.
2744 * @status: This parameter is the status code for the completed task.
2745 *
2746 * none.
2747 */
2748static void isci_task_save_for_upper_layer_completion(
2749 struct isci_host *host,
2750 struct isci_request *request,
2751 enum service_response response,
2752 enum exec_status status,
2753 enum isci_completion_selection task_notification_selection)
2754{
2755 struct sas_task *task = isci_request_access_task(request);
2756
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002757 task_notification_selection
2758 = isci_task_set_completion_status(task, response, status,
2759 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002760
2761 /* Tasks aborted specifically by a call to the lldd_abort_task
2762 * function should not be completed to the host in the regular path.
2763 */
2764 switch (task_notification_selection) {
2765
2766 case isci_perform_normal_io_completion:
Dan Williams6f231dd2011-07-02 22:56:22 -07002767 /* Normal notification (task_done) */
Jeff Skirvin3b34c162011-10-27 15:05:22 -07002768
Dan Williams6f231dd2011-07-02 22:56:22 -07002769 /* Add to the completed list. */
2770 list_add(&request->completed_node,
2771 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002772
2773 /* Take the request off the device's pending request list. */
2774 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002775 break;
2776
2777 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002778 /* No notification to libsas because this request is
2779 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002780 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002781 /* Wake up whatever process was waiting for this
2782 * request to complete.
2783 */
2784 WARN_ON(request->io_request_completion == NULL);
2785
2786 if (request->io_request_completion != NULL) {
2787
2788 /* Signal whoever is waiting that this
2789 * request is complete.
2790 */
2791 complete(request->io_request_completion);
2792 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002793 break;
2794
2795 case isci_perform_error_io_completion:
2796 /* Use sas_task_abort */
Dan Williams6f231dd2011-07-02 22:56:22 -07002797 /* Add to the aborted list. */
2798 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002799 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002800 break;
2801
2802 default:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002803 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002804 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002805 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002806 break;
2807 }
Jeff Skirvin3b34c162011-10-27 15:05:22 -07002808 dev_dbg(&host->pdev->dev,
2809 "%s: %d - task = %p, response=%d (%d), status=%d (%d)\n",
2810 __func__, task_notification_selection, task,
2811 (task) ? task->task_status.resp : 0, response,
2812 (task) ? task->task_status.stat : 0, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002813}
2814
Dan Williams1a878282011-07-29 17:16:40 -07002815static void isci_process_stp_response(struct sas_task *task, struct dev_to_host_fis *fis)
Dan Williams16ba7702011-07-01 10:52:55 -07002816{
Dan Williams16ba7702011-07-01 10:52:55 -07002817 struct task_status_struct *ts = &task->task_status;
2818 struct ata_task_resp *resp = (void *)&ts->buf[0];
2819
Dan Williams1a878282011-07-29 17:16:40 -07002820 resp->frame_len = sizeof(*fis);
2821 memcpy(resp->ending_fis, fis, sizeof(*fis));
Dan Williams16ba7702011-07-01 10:52:55 -07002822 ts->buf_valid_size = sizeof(*resp);
2823
Dan Williams1a878282011-07-29 17:16:40 -07002824 /* If the device fault bit is set in the status register, then
Dan Williams16ba7702011-07-01 10:52:55 -07002825 * set the sense data and return.
2826 */
Dan Williams1a878282011-07-29 17:16:40 -07002827 if (fis->status & ATA_DF)
Dan Williams16ba7702011-07-01 10:52:55 -07002828 ts->stat = SAS_PROTO_RESPONSE;
Dan Williamsb50102d2011-09-30 18:52:19 -07002829 else if (fis->status & ATA_ERR)
2830 ts->stat = SAM_STAT_CHECK_CONDITION;
Dan Williams16ba7702011-07-01 10:52:55 -07002831 else
2832 ts->stat = SAM_STAT_GOOD;
2833
2834 ts->resp = SAS_TASK_COMPLETE;
2835}
2836
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002837static void isci_request_io_request_complete(struct isci_host *ihost,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002838 struct isci_request *request,
2839 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002840{
2841 struct sas_task *task = isci_request_access_task(request);
2842 struct ssp_response_iu *resp_iu;
Dan Williams6f231dd2011-07-02 22:56:22 -07002843 unsigned long task_flags;
Jeff Skirvin0e2e2792011-10-27 15:04:50 -07002844 struct isci_remote_device *idev = request->target_device;
2845 enum service_response response = SAS_TASK_UNDELIVERED;
2846 enum exec_status status = SAS_ABORTED_TASK;
Dan Williams6f231dd2011-07-02 22:56:22 -07002847 enum isci_request_status request_status;
2848 enum isci_completion_selection complete_to_host
2849 = isci_perform_normal_io_completion;
2850
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002851 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002852 "%s: request = %p, task = %p,\n"
2853 "task->data_dir = %d completion_status = 0x%x\n",
2854 __func__,
2855 request,
2856 task,
2857 task->data_dir,
2858 completion_status);
2859
Jeff Skirvina5fde222011-03-04 14:06:42 -08002860 spin_lock(&request->state_lock);
Dan Williams34a99152011-07-01 02:25:15 -07002861 request_status = request->status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002862
2863 /* Decode the request status. Note that if the request has been
2864 * aborted by a task management function, we don't care
2865 * what the status is.
2866 */
2867 switch (request_status) {
2868
2869 case aborted:
2870 /* "aborted" indicates that the request was aborted by a task
2871 * management function, since once a task management request is
2872 * perfomed by the device, the request only completes because
2873 * of the subsequent driver terminate.
2874 *
2875 * Aborted also means an external thread is explicitly managing
2876 * this request, so that we do not complete it up the stack.
2877 *
2878 * The target is still there (since the TMF was successful).
2879 */
Dan Williams38d88792011-06-23 14:33:48 -07002880 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002881 response = SAS_TASK_COMPLETE;
2882
2883 /* See if the device has been/is being stopped. Note
2884 * that we ignore the quiesce state, since we are
2885 * concerned about the actual device state.
2886 */
Dan Williams209fae12011-06-13 17:39:44 -07002887 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002888 status = SAS_DEVICE_UNKNOWN;
2889 else
2890 status = SAS_ABORTED_TASK;
2891
2892 complete_to_host = isci_perform_aborted_io_completion;
2893 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002894
2895 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002896 break;
2897
2898 case aborting:
2899 /* aborting means that the task management function tried and
2900 * failed to abort the request. We need to note the request
2901 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2902 * target as down.
2903 *
2904 * Aborting also means an external thread is explicitly managing
2905 * this request, so that we do not complete it up the stack.
2906 */
Dan Williams38d88792011-06-23 14:33:48 -07002907 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002908 response = SAS_TASK_UNDELIVERED;
2909
Dan Williams209fae12011-06-13 17:39:44 -07002910 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002911 /* The device has been /is being stopped. Note that
2912 * we ignore the quiesce state, since we are
2913 * concerned about the actual device state.
2914 */
2915 status = SAS_DEVICE_UNKNOWN;
2916 else
2917 status = SAS_PHY_DOWN;
2918
2919 complete_to_host = isci_perform_aborted_io_completion;
2920
2921 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002922
2923 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002924 break;
2925
2926 case terminating:
2927
2928 /* This was an terminated request. This happens when
2929 * the I/O is being terminated because of an action on
2930 * the device (reset, tear down, etc.), and the I/O needs
2931 * to be completed up the stack.
2932 */
Dan Williams38d88792011-06-23 14:33:48 -07002933 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002934 response = SAS_TASK_UNDELIVERED;
2935
2936 /* See if the device has been/is being stopped. Note
2937 * that we ignore the quiesce state, since we are
2938 * concerned about the actual device state.
2939 */
Dan Williams209fae12011-06-13 17:39:44 -07002940 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002941 status = SAS_DEVICE_UNKNOWN;
2942 else
2943 status = SAS_ABORTED_TASK;
2944
Jeff Skirvina5fde222011-03-04 14:06:42 -08002945 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002946
2947 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002948
2949 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002950 break;
2951
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002952 case dead:
2953 /* This was a terminated request that timed-out during the
2954 * termination process. There is no task to complete to
2955 * libsas.
2956 */
2957 complete_to_host = isci_perform_normal_io_completion;
2958 spin_unlock(&request->state_lock);
2959 break;
2960
Dan Williams6f231dd2011-07-02 22:56:22 -07002961 default:
2962
Jeff Skirvina5fde222011-03-04 14:06:42 -08002963 /* The request is done from an SCU HW perspective. */
2964 request->status = completed;
2965
2966 spin_unlock(&request->state_lock);
2967
Dan Williams6f231dd2011-07-02 22:56:22 -07002968 /* This is an active request being completed from the core. */
2969 switch (completion_status) {
2970
2971 case SCI_IO_FAILURE_RESPONSE_VALID:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002972 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002973 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2974 __func__,
2975 request,
2976 task);
2977
2978 if (sas_protocol_ata(task->task_proto)) {
Dan Williams1a878282011-07-29 17:16:40 -07002979 isci_process_stp_response(task, &request->stp.rsp);
Dan Williams6f231dd2011-07-02 22:56:22 -07002980 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2981
2982 /* crack the iu response buffer. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002983 resp_iu = &request->ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002984 isci_request_process_response_iu(task, resp_iu,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002985 &ihost->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002986
2987 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2988
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002989 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002990 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2991 "SAS_PROTOCOL_SMP protocol\n",
2992 __func__);
2993
2994 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002995 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002996 "%s: unknown protocol\n", __func__);
2997
2998 /* use the task status set in the task struct by the
2999 * isci_request_process_response_iu call.
3000 */
Dan Williams38d88792011-06-23 14:33:48 -07003001 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003002 response = task->task_status.resp;
3003 status = task->task_status.stat;
3004 break;
3005
3006 case SCI_IO_SUCCESS:
3007 case SCI_IO_SUCCESS_IO_DONE_EARLY:
3008
3009 response = SAS_TASK_COMPLETE;
3010 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07003011 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003012
Dan Williams54b5e3a2011-09-28 18:35:27 -07003013 if (completion_status == SCI_IO_SUCCESS_IO_DONE_EARLY) {
Dan Williams6f231dd2011-07-02 22:56:22 -07003014
3015 /* This was an SSP / STP / SATA transfer.
3016 * There is a possibility that less data than
3017 * the maximum was transferred.
3018 */
Dan Williams5076a1a2011-06-27 14:57:03 -07003019 u32 transferred_length = sci_req_tx_bytes(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07003020
3021 task->task_status.residual
3022 = task->total_xfer_len - transferred_length;
3023
3024 /* If there were residual bytes, call this an
3025 * underrun.
3026 */
3027 if (task->task_status.residual != 0)
3028 status = SAS_DATA_UNDERRUN;
3029
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003030 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003031 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
3032 __func__,
3033 status);
3034
3035 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003036 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003037 "%s: SCI_IO_SUCCESS\n",
3038 __func__);
3039
3040 break;
3041
3042 case SCI_IO_FAILURE_TERMINATED:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003043 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003044 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
3045 __func__,
3046 request,
3047 task);
3048
3049 /* The request was terminated explicitly. No handling
3050 * is needed in the SCSI error handler path.
3051 */
Dan Williams38d88792011-06-23 14:33:48 -07003052 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003053 response = SAS_TASK_UNDELIVERED;
3054
3055 /* See if the device has been/is being stopped. Note
3056 * that we ignore the quiesce state, since we are
3057 * concerned about the actual device state.
3058 */
Dan Williams209fae12011-06-13 17:39:44 -07003059 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07003060 status = SAS_DEVICE_UNKNOWN;
3061 else
3062 status = SAS_ABORTED_TASK;
3063
3064 complete_to_host = isci_perform_normal_io_completion;
3065 break;
3066
3067 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
3068
3069 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07003070 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07003071 &complete_to_host);
3072
3073 break;
3074
3075 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
3076 /* This is a special case, in that the I/O completion
3077 * is telling us that the device needs a reset.
3078 * In order for the device reset condition to be
3079 * noticed, the I/O has to be handled in the error
3080 * handler. Set the reset flag and cause the
3081 * SCSI error thread to be scheduled.
3082 */
3083 spin_lock_irqsave(&task->task_state_lock, task_flags);
3084 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3085 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
3086
Jeff Skirvinaa145102011-03-07 16:40:47 -07003087 /* Fail the I/O. */
3088 response = SAS_TASK_UNDELIVERED;
3089 status = SAM_STAT_TASK_ABORTED;
3090
Dan Williams6f231dd2011-07-02 22:56:22 -07003091 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07003092 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003093 break;
3094
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003095 case SCI_FAILURE_RETRY_REQUIRED:
3096
3097 /* Fail the I/O so it can be retried. */
3098 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07003099 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003100 status = SAS_DEVICE_UNKNOWN;
3101 else
3102 status = SAS_ABORTED_TASK;
3103
3104 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07003105 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003106 break;
3107
3108
Dan Williams6f231dd2011-07-02 22:56:22 -07003109 default:
3110 /* Catch any otherwise unhandled error codes here. */
Dan Williamsa8a0a132011-07-01 12:07:25 -07003111 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003112 "%s: invalid completion code: 0x%x - "
3113 "isci_request = %p\n",
3114 __func__, completion_status, request);
3115
3116 response = SAS_TASK_UNDELIVERED;
3117
3118 /* See if the device has been/is being stopped. Note
3119 * that we ignore the quiesce state, since we are
3120 * concerned about the actual device state.
3121 */
Dan Williams209fae12011-06-13 17:39:44 -07003122 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07003123 status = SAS_DEVICE_UNKNOWN;
3124 else
3125 status = SAS_ABORTED_TASK;
3126
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003127 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07003128 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003129 complete_to_host = isci_perform_normal_io_completion;
3130 } else {
Dan Williams38d88792011-06-23 14:33:48 -07003131 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003132 complete_to_host = isci_perform_error_io_completion;
3133 }
Dan Williams6f231dd2011-07-02 22:56:22 -07003134 break;
3135 }
3136 break;
3137 }
3138
Dan Williamsddcc7e32011-06-17 10:40:43 -07003139 switch (task->task_proto) {
3140 case SAS_PROTOCOL_SSP:
3141 if (task->data_dir == DMA_NONE)
3142 break;
3143 if (task->num_scatter == 0)
3144 /* 0 indicates a single dma address */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003145 dma_unmap_single(&ihost->pdev->dev,
Dan Williamsddcc7e32011-06-17 10:40:43 -07003146 request->zero_scatter_daddr,
3147 task->total_xfer_len, task->data_dir);
3148 else /* unmap the sgl dma addresses */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003149 dma_unmap_sg(&ihost->pdev->dev, task->scatter,
Dan Williamsddcc7e32011-06-17 10:40:43 -07003150 request->num_sg_entries, task->data_dir);
3151 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07003152 case SAS_PROTOCOL_SMP: {
3153 struct scatterlist *sg = &task->smp_task.smp_req;
3154 struct smp_req *smp_req;
3155 void *kaddr;
3156
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003157 dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
Dan Williamse9bf7092011-06-16 16:59:56 -07003158
3159 /* need to swab it back in case the command buffer is re-used */
Cong Wang77dfce02011-11-25 23:14:23 +08003160 kaddr = kmap_atomic(sg_page(sg));
Dan Williamse9bf7092011-06-16 16:59:56 -07003161 smp_req = kaddr + sg->offset;
3162 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
Cong Wang77dfce02011-11-25 23:14:23 +08003163 kunmap_atomic(kaddr);
Dan Williamse9bf7092011-06-16 16:59:56 -07003164 break;
3165 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07003166 default:
3167 break;
3168 }
Dan Williams6f231dd2011-07-02 22:56:22 -07003169
3170 /* Put the completed request on the correct list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003171 isci_task_save_for_upper_layer_completion(ihost, request, response,
Dan Williams6f231dd2011-07-02 22:56:22 -07003172 status, complete_to_host
3173 );
3174
3175 /* complete the io request to the core. */
Dan Williams89a73012011-06-30 19:14:33 -07003176 sci_controller_complete_io(ihost, request->target_device, request);
Dan Williams209fae12011-06-13 17:39:44 -07003177
Dan Williams67ea8382011-05-08 11:47:15 -07003178 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07003179 * terminated again, and to cause any calls into abort
3180 * task to recognize the already completed case.
3181 */
Dan Williams38d88792011-06-23 14:33:48 -07003182 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003183}
Dan Williamsf1f52e72011-05-10 02:28:45 -07003184
Dan Williams89a73012011-06-30 19:14:33 -07003185static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003186{
Dan Williams5076a1a2011-06-27 14:57:03 -07003187 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams78a6f062011-06-30 16:31:37 -07003188 struct domain_device *dev = ireq->target_device->domain_dev;
Dan Williamsb50102d2011-09-30 18:52:19 -07003189 enum sci_base_request_states state;
Dan Williamsc72086e2011-05-10 02:28:48 -07003190 struct sas_task *task;
3191
3192 /* XXX as hch said always creating an internal sas_task for tmf
3193 * requests would simplify the driver
3194 */
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003195 task = (test_bit(IREQ_TMF, &ireq->flags)) ? NULL : isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003196
Dan Williams5dec6f42011-05-10 02:28:49 -07003197 /* all unaccelerated request types (non ssp or ncq) handled with
3198 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07003199 */
Dan Williamsc72086e2011-05-10 02:28:48 -07003200 if (!task && dev->dev_type == SAS_END_DEV) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003201 state = SCI_REQ_TASK_WAIT_TC_COMP;
Dan Williamsc72086e2011-05-10 02:28:48 -07003202 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003203 state = SCI_REQ_SMP_WAIT_RESP;
Dan Williams5dec6f42011-05-10 02:28:49 -07003204 } else if (task && sas_protocol_ata(task->task_proto) &&
3205 !task->ata_task.use_ncq) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003206 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
3207 task->ata_task.fis.command == ATA_CMD_PACKET) {
3208 state = SCI_REQ_ATAPI_WAIT_H2D;
3209 } else if (task->data_dir == DMA_NONE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003210 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williamsb50102d2011-09-30 18:52:19 -07003211 } else if (task->ata_task.dma_xfer) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003212 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williamsb50102d2011-09-30 18:52:19 -07003213 } else /* PIO */ {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003214 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williamsb50102d2011-09-30 18:52:19 -07003215 }
3216 } else {
3217 /* SSP or NCQ are fully accelerated, no substates */
3218 return;
Dan Williamsc72086e2011-05-10 02:28:48 -07003219 }
Dan Williamsb50102d2011-09-30 18:52:19 -07003220 sci_change_state(sm, state);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003221}
3222
Dan Williams89a73012011-06-30 19:14:33 -07003223static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003224{
Dan Williams5076a1a2011-06-27 14:57:03 -07003225 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003226 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003227
Dan Williamsf1f52e72011-05-10 02:28:45 -07003228 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07003229 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07003230 isci_request_io_request_complete(ihost, ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -07003231 ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003232 else
Dan Williams5076a1a2011-06-27 14:57:03 -07003233 isci_task_request_complete(ihost, ireq, ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003234}
3235
Dan Williams89a73012011-06-30 19:14:33 -07003236static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003237{
Dan Williams5076a1a2011-06-27 14:57:03 -07003238 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003239
3240 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams5076a1a2011-06-27 14:57:03 -07003241 ireq->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07003242}
3243
Dan Williams89a73012011-06-30 19:14:33 -07003244static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07003245{
Dan Williams5076a1a2011-06-27 14:57:03 -07003246 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07003247
Dan Williams34a99152011-07-01 02:25:15 -07003248 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07003249}
3250
Dan Williams89a73012011-06-30 19:14:33 -07003251static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07003252{
Dan Williams5076a1a2011-06-27 14:57:03 -07003253 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07003254
Dan Williams34a99152011-07-01 02:25:15 -07003255 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07003256}
3257
Dan Williams89a73012011-06-30 19:14:33 -07003258static const struct sci_base_state sci_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003259 [SCI_REQ_INIT] = { },
3260 [SCI_REQ_CONSTRUCTED] = { },
3261 [SCI_REQ_STARTED] = {
Dan Williams89a73012011-06-30 19:14:33 -07003262 .enter_state = sci_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003263 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003264 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07003265 .enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003266 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003267 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
3268 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07003269 .enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003270 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003271 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
3272 [SCI_REQ_STP_PIO_DATA_IN] = { },
3273 [SCI_REQ_STP_PIO_DATA_OUT] = { },
3274 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
3275 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003276 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
3277 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
3278 [SCI_REQ_SMP_WAIT_RESP] = { },
3279 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
Dan Williamsb50102d2011-09-30 18:52:19 -07003280 [SCI_REQ_ATAPI_WAIT_H2D] = { },
3281 [SCI_REQ_ATAPI_WAIT_PIO_SETUP] = { },
3282 [SCI_REQ_ATAPI_WAIT_D2H] = { },
3283 [SCI_REQ_ATAPI_WAIT_TC_COMP] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003284 [SCI_REQ_COMPLETED] = {
Dan Williams89a73012011-06-30 19:14:33 -07003285 .enter_state = sci_request_completed_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003286 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003287 [SCI_REQ_ABORTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07003288 .enter_state = sci_request_aborting_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003289 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003290 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07003291};
3292
Edmund Nadolskie3013702011-06-02 00:10:43 +00003293static void
Dan Williams89a73012011-06-30 19:14:33 -07003294sci_general_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003295 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003296 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003297{
Dan Williams89a73012011-06-30 19:14:33 -07003298 sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003299
Dan Williams78a6f062011-06-30 16:31:37 -07003300 ireq->target_device = idev;
Dan Williamsc79dd802012-02-01 00:44:14 -08003301 ireq->protocol = SAS_PROTOCOL_NONE;
Dan Williams5076a1a2011-06-27 14:57:03 -07003302 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003303
Dan Williams5076a1a2011-06-27 14:57:03 -07003304 ireq->sci_status = SCI_SUCCESS;
3305 ireq->scu_status = 0;
3306 ireq->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003307}
3308
3309static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07003310sci_io_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003311 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003312 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003313{
Dan Williams78a6f062011-06-30 16:31:37 -07003314 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003315 enum sci_status status = SCI_SUCCESS;
3316
3317 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07003318 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003319
Dan Williams78a6f062011-06-30 16:31:37 -07003320 if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003321 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3322
3323 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07003324 /* pass */;
Dan Williams11cc5182012-02-01 00:23:10 -08003325 else if (dev_is_sata(dev))
Dan Williams5076a1a2011-06-27 14:57:03 -07003326 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07003327 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07003328 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07003329 else
3330 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003331
Dan Williams5076a1a2011-06-27 14:57:03 -07003332 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07003333
3334 return status;
3335}
3336
Dan Williams89a73012011-06-30 19:14:33 -07003337enum sci_status sci_task_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003338 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003339 u16 io_tag, struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003340{
Dan Williams78a6f062011-06-30 16:31:37 -07003341 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003342 enum sci_status status = SCI_SUCCESS;
3343
3344 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07003345 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003346
Dan Williams11cc5182012-02-01 00:23:10 -08003347 if (dev->dev_type == SAS_END_DEV || dev_is_sata(dev)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07003348 set_bit(IREQ_TMF, &ireq->flags);
3349 memset(ireq->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07003350 } else
3351 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003352
3353 return status;
3354}
3355
3356static enum sci_status isci_request_ssp_request_construct(
3357 struct isci_request *request)
3358{
3359 enum sci_status status;
3360
3361 dev_dbg(&request->isci_host->pdev->dev,
3362 "%s: request = %p\n",
3363 __func__,
3364 request);
Dan Williams89a73012011-06-30 19:14:33 -07003365 status = sci_io_request_construct_basic_ssp(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003366 return status;
3367}
3368
Dan Williams16ba7702011-07-01 10:52:55 -07003369static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003370{
Dan Williams16ba7702011-07-01 10:52:55 -07003371 struct sas_task *task = isci_request_access_task(ireq);
3372 struct host_to_dev_fis *fis = &ireq->stp.cmd;
3373 struct ata_queued_cmd *qc = task->uldd_task;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003374 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003375
Dan Williams16ba7702011-07-01 10:52:55 -07003376 dev_dbg(&ireq->isci_host->pdev->dev,
3377 "%s: ireq = %p\n",
Dan Williamsf1f52e72011-05-10 02:28:45 -07003378 __func__,
Dan Williams16ba7702011-07-01 10:52:55 -07003379 ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003380
Dan Williams16ba7702011-07-01 10:52:55 -07003381 memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
3382 if (!task->ata_task.device_control_reg_update)
3383 fis->flags |= 0x80;
3384 fis->flags &= 0xF0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003385
Dan Williams16ba7702011-07-01 10:52:55 -07003386 status = sci_io_request_construct_basic_sata(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003387
Dan Williams16ba7702011-07-01 10:52:55 -07003388 if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
3389 qc->tf.command == ATA_CMD_FPDMA_READ)) {
3390 fis->sector_count = qc->tag << 3;
3391 ireq->tc->type.stp.ncq_tag = qc->tag;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003392 }
3393
3394 return status;
3395}
3396
Dan Williamse9bf7092011-06-16 16:59:56 -07003397static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07003398sci_io_request_construct_smp(struct device *dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003399 struct isci_request *ireq,
Dan Williamse9bf7092011-06-16 16:59:56 -07003400 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003401{
Dan Williamse9bf7092011-06-16 16:59:56 -07003402 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williams78a6f062011-06-30 16:31:37 -07003403 struct isci_remote_device *idev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003404 struct scu_task_context *task_context;
Dan Williamsffe191c2011-06-29 13:09:25 -07003405 struct isci_port *iport;
Dan Williamse9bf7092011-06-16 16:59:56 -07003406 struct smp_req *smp_req;
3407 void *kaddr;
3408 u8 req_len;
3409 u32 cmd;
3410
Cong Wang77dfce02011-11-25 23:14:23 +08003411 kaddr = kmap_atomic(sg_page(sg));
Dan Williamse9bf7092011-06-16 16:59:56 -07003412 smp_req = kaddr + sg->offset;
3413 /*
3414 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3415 * functions under SAS 2.0, a zero request length really indicates
3416 * a non-zero default length.
3417 */
3418 if (smp_req->req_len == 0) {
3419 switch (smp_req->func) {
3420 case SMP_DISCOVER:
3421 case SMP_REPORT_PHY_ERR_LOG:
3422 case SMP_REPORT_PHY_SATA:
3423 case SMP_REPORT_ROUTE_INFO:
3424 smp_req->req_len = 2;
3425 break;
3426 case SMP_CONF_ROUTE_INFO:
3427 case SMP_PHY_CONTROL:
3428 case SMP_PHY_TEST_FUNCTION:
3429 smp_req->req_len = 9;
3430 break;
3431 /* Default - zero is a valid default for 2.0. */
3432 }
3433 }
3434 req_len = smp_req->req_len;
3435 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3436 cmd = *(u32 *) smp_req;
Cong Wang77dfce02011-11-25 23:14:23 +08003437 kunmap_atomic(kaddr);
Dan Williamse9bf7092011-06-16 16:59:56 -07003438
3439 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3440 return SCI_FAILURE;
3441
Dan Williamsc79dd802012-02-01 00:44:14 -08003442 ireq->protocol = SAS_PROTOCOL_SMP;
Dan Williamsc72086e2011-05-10 02:28:48 -07003443
3444 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003445
Dan Williams5076a1a2011-06-27 14:57:03 -07003446 task_context = ireq->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003447
Dan Williams34a99152011-07-01 02:25:15 -07003448 idev = ireq->target_device;
3449 iport = idev->owning_port;
Dan Williamsc72086e2011-05-10 02:28:48 -07003450
3451 /*
3452 * Fill in the TC with the its required data
3453 * 00h
3454 */
3455 task_context->priority = 0;
3456 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -07003457 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -07003458 task_context->protocol_engine_index = ISCI_PEG;
3459 task_context->logical_port_index = iport->physical_port_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003460 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3461 task_context->abort = 0;
3462 task_context->valid = SCU_TASK_CONTEXT_VALID;
3463 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3464
3465 /* 04h */
Dan Williams78a6f062011-06-30 16:31:37 -07003466 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003467 task_context->command_code = 0;
3468 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3469
3470 /* 08h */
3471 task_context->link_layer_control = 0;
3472 task_context->do_not_dma_ssp_good_response = 1;
3473 task_context->strict_ordering = 0;
3474 task_context->control_frame = 1;
3475 task_context->timeout_enable = 0;
3476 task_context->block_guard_enable = 0;
3477
3478 /* 0ch */
3479 task_context->address_modifier = 0;
3480
3481 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003482 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003483
3484 /* 14h */
3485 task_context->transfer_length_bytes = 0;
3486
3487 /*
3488 * 18h ~ 30h, protocol specific
3489 * since commandIU has been build by framework at this point, we just
3490 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003491 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003492
3493 /*
3494 * 40h
3495 * "For SMP you could program it to zero. We would prefer that way
3496 * so that done code will be consistent." - Venki
3497 */
3498 task_context->task_phase = 0;
3499
Dan Williams5076a1a2011-06-27 14:57:03 -07003500 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -07003501 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3502 (iport->physical_port_index <<
3503 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3504 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003505 /*
3506 * Copy the physical address for the command buffer to the SCU Task
3507 * Context command buffer should not contain command header.
3508 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003509 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3510 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003511
3512 /* SMP response comes as UF, so no need to set response IU address. */
3513 task_context->response_iu_upper = 0;
3514 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003515
Dan Williams5076a1a2011-06-27 14:57:03 -07003516 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003517
3518 return SCI_SUCCESS;
3519}
3520
3521/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003522 * isci_smp_request_build() - This function builds the smp request.
3523 * @ireq: This parameter points to the isci_request allocated in the
3524 * request construct function.
3525 *
3526 * SCI_SUCCESS on successfull completion, or specific failure code.
3527 */
3528static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3529{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003530 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003531 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamse9bf7092011-06-16 16:59:56 -07003532 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003533
Dan Williams89a73012011-06-30 19:14:33 -07003534 status = sci_io_request_construct_smp(dev, ireq, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003535 if (status != SCI_SUCCESS)
Dan Williamsa8a0a132011-07-01 12:07:25 -07003536 dev_dbg(&ireq->isci_host->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003537 "%s: failed with status = %d\n",
3538 __func__,
3539 status);
3540
3541 return status;
3542}
3543
3544/**
3545 * isci_io_request_build() - This function builds the io request object.
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003546 * @ihost: This parameter specifies the ISCI host object
Dan Williamsf1f52e72011-05-10 02:28:45 -07003547 * @request: This parameter points to the isci_request object allocated in the
3548 * request construct function.
3549 * @sci_device: This parameter is the handle for the sci core's remote device
3550 * object that is the destination for this request.
3551 *
3552 * SCI_SUCCESS on successfull completion, or specific failure code.
3553 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003554static enum sci_status isci_io_request_build(struct isci_host *ihost,
Dan Williams312e0c22011-06-28 13:47:09 -07003555 struct isci_request *request,
Dan Williams78a6f062011-06-30 16:31:37 -07003556 struct isci_remote_device *idev)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003557{
3558 enum sci_status status = SCI_SUCCESS;
3559 struct sas_task *task = isci_request_access_task(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003560
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003561 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -07003562 "%s: idev = 0x%p; request = %p, "
Dan Williamsf1f52e72011-05-10 02:28:45 -07003563 "num_scatter = %d\n",
3564 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -07003565 idev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003566 request,
3567 task->num_scatter);
3568
3569 /* map the sgl addresses, if present.
3570 * libata does the mapping for sata devices
3571 * before we get the request.
3572 */
3573 if (task->num_scatter &&
3574 !sas_protocol_ata(task->task_proto) &&
3575 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3576
3577 request->num_sg_entries = dma_map_sg(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003578 &ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003579 task->scatter,
3580 task->num_scatter,
3581 task->data_dir
3582 );
3583
3584 if (request->num_sg_entries == 0)
3585 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3586 }
3587
Dan Williams89a73012011-06-30 19:14:33 -07003588 status = sci_io_request_construct(ihost, idev, request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003589
3590 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003591 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003592 "%s: failed request construct\n",
3593 __func__);
3594 return SCI_FAILURE;
3595 }
3596
3597 switch (task->task_proto) {
3598 case SAS_PROTOCOL_SMP:
3599 status = isci_smp_request_build(request);
3600 break;
3601 case SAS_PROTOCOL_SSP:
3602 status = isci_request_ssp_request_construct(request);
3603 break;
3604 case SAS_PROTOCOL_SATA:
3605 case SAS_PROTOCOL_STP:
3606 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3607 status = isci_request_stp_request_construct(request);
3608 break;
3609 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07003610 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003611 "%s: unknown protocol\n", __func__);
3612 return SCI_FAILURE;
3613 }
3614
3615 return SCI_SUCCESS;
3616}
3617
Dan Williamsdb056252011-06-17 14:18:39 -07003618static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003619{
Dan Williams0d0cf142011-06-13 00:51:30 -07003620 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003621
Dan Williamsdb056252011-06-17 14:18:39 -07003622 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
Dan Williams5076a1a2011-06-27 14:57:03 -07003623 ireq->io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003624 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003625 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003626 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003627 INIT_LIST_HEAD(&ireq->completed_node);
3628 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003629 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003630
Dan Williams0d0cf142011-06-13 00:51:30 -07003631 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003632}
3633
Dan Williamsdb056252011-06-17 14:18:39 -07003634static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3635 struct sas_task *task,
3636 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003637{
Dan Williams0d0cf142011-06-13 00:51:30 -07003638 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003639
Dan Williamsdb056252011-06-17 14:18:39 -07003640 ireq = isci_request_from_tag(ihost, tag);
3641 ireq->ttype_ptr.io_task_ptr = task;
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003642 clear_bit(IREQ_TMF, &ireq->flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003643 task->lldd_task = ireq;
3644
Dan Williams0d0cf142011-06-13 00:51:30 -07003645 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003646}
3647
Dan Williamsdb056252011-06-17 14:18:39 -07003648struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3649 struct isci_tmf *isci_tmf,
3650 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003651{
Dan Williams0d0cf142011-06-13 00:51:30 -07003652 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003653
Dan Williamsdb056252011-06-17 14:18:39 -07003654 ireq = isci_request_from_tag(ihost, tag);
3655 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003656 set_bit(IREQ_TMF, &ireq->flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003657
Dan Williams0d0cf142011-06-13 00:51:30 -07003658 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003659}
3660
Dan Williams209fae12011-06-13 17:39:44 -07003661int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003662 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003663{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003664 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003665 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003666 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003667 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003668
Dan Williamsf1f52e72011-05-10 02:28:45 -07003669 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003670 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003671
Dan Williamsdb056252011-06-17 14:18:39 -07003672 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003673 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003674 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003675 "%s: request_construct failed - status = 0x%x\n",
3676 __func__,
3677 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003678 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003679 }
3680
Dan Williams0d0cf142011-06-13 00:51:30 -07003681 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003682
Jeff Skirvin9274f452011-06-23 17:09:02 -07003683 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3684
3685 if (isci_task_is_ncq_recovery(task)) {
3686
3687 /* The device is in an NCQ recovery state. Issue the
3688 * request on the task side. Note that it will
3689 * complete on the I/O request side because the
3690 * request was built that way (ie.
3691 * ireq->is_task_management_request is false).
3692 */
Dan Williams89a73012011-06-30 19:14:33 -07003693 status = sci_controller_start_task(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003694 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003695 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003696 } else {
3697 status = SCI_FAILURE;
3698 }
3699 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003700 /* send the request, let the core assign the IO TAG. */
Dan Williams89a73012011-06-30 19:14:33 -07003701 status = sci_controller_start_io(ihost, idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003702 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003703 }
Dan Williams312e0c22011-06-28 13:47:09 -07003704
Dan Williamsf1f52e72011-05-10 02:28:45 -07003705 if (status != SCI_SUCCESS &&
3706 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003707 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003708 "%s: failed request start (0x%x)\n",
3709 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003710 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003711 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003712 }
3713
3714 /* Either I/O started OK, or the core has signaled that
3715 * the device needs a target reset.
3716 *
3717 * In either case, hold onto the I/O for later.
3718 *
3719 * Update it's status and add it to the list in the
3720 * remote device object.
3721 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003722 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003723
3724 if (status == SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003725 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003726 } else {
3727 /* The request did not really start in the
3728 * hardware, so clear the request handle
3729 * here so no terminations will be done.
3730 */
Dan Williams38d88792011-06-23 14:33:48 -07003731 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003732 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003733 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003734 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003735
3736 if (status ==
3737 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3738 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003739 * handler thread to work on this I/O and that
3740 * we want a device reset.
3741 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003742 spin_lock_irqsave(&task->task_state_lock, flags);
3743 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3744 spin_unlock_irqrestore(&task->task_state_lock, flags);
3745
3746 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003747 * handler thread.
3748 */
Dan Williams312d3e52011-11-17 17:59:50 -08003749 sas_task_abort(task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003750
3751 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003752 * the I/O until it is managed by the SCSI
3753 * error handler.
3754 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003755 status = SCI_SUCCESS;
3756 }
3757
Dan Williamsf1f52e72011-05-10 02:28:45 -07003758 return ret;
3759}