blob: 605dc68cbf7926764eff08eb27ddee20b381d6c0 [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:
Dan Williams34a99152011-07-01 02:25:15 -0700866 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
867 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -0700868 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700869 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000870 case SCI_REQ_STARTED:
871 case SCI_REQ_TASK_WAIT_TC_COMP:
872 case SCI_REQ_SMP_WAIT_RESP:
873 case SCI_REQ_SMP_WAIT_TC_COMP:
874 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
875 case SCI_REQ_STP_UDMA_WAIT_D2H:
876 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
877 case SCI_REQ_STP_NON_DATA_WAIT_D2H:
878 case SCI_REQ_STP_PIO_WAIT_H2D:
879 case SCI_REQ_STP_PIO_WAIT_FRAME:
880 case SCI_REQ_STP_PIO_DATA_IN:
881 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williamsb50102d2011-09-30 18:52:19 -0700882 case SCI_REQ_ATAPI_WAIT_H2D:
883 case SCI_REQ_ATAPI_WAIT_PIO_SETUP:
884 case SCI_REQ_ATAPI_WAIT_D2H:
885 case SCI_REQ_ATAPI_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -0700886 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700887 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000888 case SCI_REQ_TASK_WAIT_TC_RESP:
Jeff Skirvin39ea2c52011-07-29 17:17:05 -0700889 /* The task frame was already confirmed to have been
890 * sent by the SCU HW. Since the state machine is
891 * now only waiting for the task response itself,
892 * abort the request and complete it immediately
893 * and don't wait for the task response.
894 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700895 sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
896 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700897 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000898 case SCI_REQ_ABORTING:
Jeff Skirvin39ea2c52011-07-29 17:17:05 -0700899 /* If a request has a termination requested twice, return
900 * a failure indication, since HW confirmation of the first
901 * abort is still outstanding.
902 */
Edmund Nadolskie3013702011-06-02 00:10:43 +0000903 case SCI_REQ_COMPLETED:
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700904 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700905 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700906 "%s: SCIC IO Request requested to abort while in wrong "
907 "state %d\n",
908 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -0700909 ireq->sm.current_state_id);
Dan Williamsf00e6ba2011-05-10 02:39:11 -0700910 break;
911 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700912
913 return SCI_FAILURE_INVALID_STATE;
914}
915
Dan Williams89a73012011-06-30 19:14:33 -0700916enum sci_status sci_request_complete(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700917{
Dan Williams79e2b6b2011-05-11 08:29:56 -0700918 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700919 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700920
Dan Williams5076a1a2011-06-27 14:57:03 -0700921 state = ireq->sm.current_state_id;
Edmund Nadolskie3013702011-06-02 00:10:43 +0000922 if (WARN_ONCE(state != SCI_REQ_COMPLETED,
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800923 "isci: request completion from wrong state (%s)\n",
924 req_state_name(state)))
Dan Williams79e2b6b2011-05-11 08:29:56 -0700925 return SCI_FAILURE_INVALID_STATE;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700926
Dan Williams5076a1a2011-06-27 14:57:03 -0700927 if (ireq->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX)
Dan Williams89a73012011-06-30 19:14:33 -0700928 sci_controller_release_frame(ihost,
Dan Williams5076a1a2011-06-27 14:57:03 -0700929 ireq->saved_rx_frame_index);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700930
931 /* XXX can we just stop the machine and remove the 'final' state? */
Dan Williams5076a1a2011-06-27 14:57:03 -0700932 sci_change_state(&ireq->sm, SCI_REQ_FINAL);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700933 return SCI_SUCCESS;
934}
935
Dan Williams89a73012011-06-30 19:14:33 -0700936enum sci_status sci_io_request_event_handler(struct isci_request *ireq,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700937 u32 event_code)
938{
939 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700940 struct isci_host *ihost = ireq->owning_controller;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700941
Dan Williams5076a1a2011-06-27 14:57:03 -0700942 state = ireq->sm.current_state_id;
Dan Williams79e2b6b2011-05-11 08:29:56 -0700943
Edmund Nadolskie3013702011-06-02 00:10:43 +0000944 if (state != SCI_REQ_STP_PIO_DATA_IN) {
Dan Williamsd7a0ccd2012-02-10 01:18:44 -0800945 dev_warn(&ihost->pdev->dev, "%s: (%x) in wrong state %s\n",
946 __func__, event_code, req_state_name(state));
Dan Williams79e2b6b2011-05-11 08:29:56 -0700947
948 return SCI_FAILURE_INVALID_STATE;
949 }
950
951 switch (scu_get_event_specifier(event_code)) {
952 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
953 /* We are waiting for data and the SCU has R_ERR the data frame.
954 * Go back to waiting for the D2H Register FIS
955 */
Dan Williams5076a1a2011-06-27 14:57:03 -0700956 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams79e2b6b2011-05-11 08:29:56 -0700957 return SCI_SUCCESS;
958 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -0700959 dev_err(&ihost->pdev->dev,
Dan Williams79e2b6b2011-05-11 08:29:56 -0700960 "%s: pio request unexpected event %#x\n",
961 __func__, event_code);
962
963 /* TODO Should we fail the PIO request when we get an
964 * unexpected event?
965 */
966 return SCI_FAILURE;
967 }
Dan Williamsf1f52e72011-05-10 02:28:45 -0700968}
969
Dan Williamsf1f52e72011-05-10 02:28:45 -0700970/*
971 * This function copies response data for requests returning response data
972 * instead of sense data.
973 * @sci_req: This parameter specifies the request object for which to copy
974 * the response data.
975 */
Dan Williams89a73012011-06-30 19:14:33 -0700976static void sci_io_request_copy_response(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700977{
978 void *resp_buf;
979 u32 len;
980 struct ssp_response_iu *ssp_response;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700981 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
982
Dan Williams5076a1a2011-06-27 14:57:03 -0700983 ssp_response = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -0700984
985 resp_buf = &isci_tmf->resp.resp_iu;
986
987 len = min_t(u32,
988 SSP_RESP_IU_MAX_SIZE,
989 be32_to_cpu(ssp_response->response_data_len));
990
991 memcpy(resp_buf, ssp_response->resp_data, len);
992}
993
Edmund Nadolskie3013702011-06-02 00:10:43 +0000994static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -0700995request_started_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +0000996 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -0700997{
Dan Williamsf1f52e72011-05-10 02:28:45 -0700998 struct ssp_response_iu *resp_iu;
Dan Williamsa7e255a2011-05-11 08:27:47 -0700999 u8 datapres;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001000
Dan Williamsa7e255a2011-05-11 08:27:47 -07001001 /* TODO: Any SDMA return code of other than 0 is bad decode 0x003C0000
1002 * to determine SDMA status
Dan Williamsf1f52e72011-05-10 02:28:45 -07001003 */
1004 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1005 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001006 ireq->scu_status = SCU_TASK_DONE_GOOD;
1007 ireq->sci_status = SCI_SUCCESS;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001008 break;
Dan Williamsa7e255a2011-05-11 08:27:47 -07001009 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP): {
1010 /* There are times when the SCU hardware will return an early
Dan Williamsf1f52e72011-05-10 02:28:45 -07001011 * response because the io request specified more data than is
1012 * returned by the target device (mode pages, inquiry data,
1013 * etc.). We must check the response stats to see if this is
1014 * truly a failed request or a good request that just got
1015 * completed early.
1016 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001017 struct ssp_response_iu *resp = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001018 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1019
Dan Williams5076a1a2011-06-27 14:57:03 -07001020 sci_swab32_cpy(&ireq->ssp.rsp,
1021 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -07001022 word_cnt);
1023
1024 if (resp->status == 0) {
Dan Williams34a99152011-07-01 02:25:15 -07001025 ireq->scu_status = SCU_TASK_DONE_GOOD;
1026 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001027 } else {
Dan Williams34a99152011-07-01 02:25:15 -07001028 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1029 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001030 }
Dan Williamsa7e255a2011-05-11 08:27:47 -07001031 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001032 }
Dan Williamsa7e255a2011-05-11 08:27:47 -07001033 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE): {
Dan Williamsf1f52e72011-05-10 02:28:45 -07001034 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1035
Dan Williams5076a1a2011-06-27 14:57:03 -07001036 sci_swab32_cpy(&ireq->ssp.rsp,
1037 &ireq->ssp.rsp,
Dan Williamsf1f52e72011-05-10 02:28:45 -07001038 word_cnt);
1039
Dan Williams34a99152011-07-01 02:25:15 -07001040 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1041 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001042 break;
1043 }
1044
1045 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001046 /* TODO With TASK_DONE_RESP_LEN_ERR is the response frame
Dan Williamsf1f52e72011-05-10 02:28:45 -07001047 * guaranteed to be received before this completion status is
1048 * posted?
1049 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001050 resp_iu = &ireq->ssp.rsp;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001051 datapres = resp_iu->datapres;
1052
Dan Williamsa7e255a2011-05-11 08:27:47 -07001053 if (datapres == 1 || datapres == 2) {
Dan Williams34a99152011-07-01 02:25:15 -07001054 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1055 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1056 } else {
1057 ireq->scu_status = SCU_TASK_DONE_GOOD;
1058 ireq->sci_status = SCI_SUCCESS;
1059 }
Dan Williamsf1f52e72011-05-10 02:28:45 -07001060 break;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001061 /* only stp device gets suspended. */
1062 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1063 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1064 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1065 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1066 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1067 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1068 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1069 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1070 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1071 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1072 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
Dan Williamsc79dd802012-02-01 00:44:14 -08001073 if (ireq->protocol == SAS_PROTOCOL_STP) {
Dan Williams34a99152011-07-01 02:25:15 -07001074 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1075 SCU_COMPLETION_TL_STATUS_SHIFT;
1076 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001077 } else {
Dan Williams34a99152011-07-01 02:25:15 -07001078 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1079 SCU_COMPLETION_TL_STATUS_SHIFT;
1080 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001081 }
1082 break;
1083
1084 /* both stp/ssp device gets suspended */
1085 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1086 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1087 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1088 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1089 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1090 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1091 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1092 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1093 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1094 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
Dan Williams34a99152011-07-01 02:25:15 -07001095 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1096 SCU_COMPLETION_TL_STATUS_SHIFT;
1097 ireq->sci_status = SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001098 break;
1099
1100 /* neither ssp nor stp gets suspended. */
1101 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1102 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1103 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1104 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1105 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1106 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1108 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1109 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1110 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1111 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1112 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1113 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1114 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1115 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1116 default:
Dan Williams34a99152011-07-01 02:25:15 -07001117 ireq->scu_status = SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1118 SCU_COMPLETION_TL_STATUS_SHIFT;
1119 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williamsf1f52e72011-05-10 02:28:45 -07001120 break;
1121 }
1122
1123 /*
1124 * TODO: This is probably wrong for ACK/NAK timeout conditions
1125 */
1126
1127 /* In all cases we will treat this as the completion of the IO req. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001128 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001129 return SCI_SUCCESS;
1130}
1131
Edmund Nadolskie3013702011-06-02 00:10:43 +00001132static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001133request_aborting_state_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001134 u32 completion_code)
Dan Williamsf1f52e72011-05-10 02:28:45 -07001135{
1136 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1137 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1138 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
Dan Williams34a99152011-07-01 02:25:15 -07001139 ireq->scu_status = SCU_TASK_DONE_TASK_ABORT;
1140 ireq->sci_status = SCI_FAILURE_IO_TERMINATED;
Dan Williams5076a1a2011-06-27 14:57:03 -07001141 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1f52e72011-05-10 02:28:45 -07001142 break;
1143
1144 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001145 /* Unless we get some strange error wait for the task abort to complete
1146 * TODO: Should there be a state change for this completion?
1147 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07001148 break;
1149 }
1150
1151 return SCI_SUCCESS;
1152}
1153
Dan Williams5076a1a2011-06-27 14:57:03 -07001154static enum sci_status ssp_task_request_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07001155 u32 completion_code)
Dan Williamsf1393032011-05-10 02:28:47 -07001156{
1157 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1158 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001159 ireq->scu_status = SCU_TASK_DONE_GOOD;
1160 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001161 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001162 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001163 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001164 /* Currently, the decision is to simply allow the task request
1165 * to timeout if the task IU wasn't received successfully.
1166 * There is a potential for receiving multiple task responses if
1167 * we decide to send the task IU again.
1168 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001169 dev_warn(&ireq->owning_controller->pdev->dev,
Dan Williamsf1393032011-05-10 02:28:47 -07001170 "%s: TaskRequest:0x%p CompletionCode:%x - "
Dan Williams5076a1a2011-06-27 14:57:03 -07001171 "ACK/NAK timeout\n", __func__, ireq,
Dan Williamsf1393032011-05-10 02:28:47 -07001172 completion_code);
1173
Dan Williams5076a1a2011-06-27 14:57:03 -07001174 sci_change_state(&ireq->sm, SCI_REQ_TASK_WAIT_TC_RESP);
Dan Williamsf1393032011-05-10 02:28:47 -07001175 break;
Dan Williamsf1393032011-05-10 02:28:47 -07001176 default:
Edmund Nadolskie3013702011-06-02 00:10:43 +00001177 /*
1178 * All other completion status cause the IO to be complete.
1179 * If a NAK was received, then it is up to the user to retry
1180 * the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001181 */
Dan Williams34a99152011-07-01 02:25:15 -07001182 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1183 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001184 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsf1393032011-05-10 02:28:47 -07001185 break;
1186 }
1187
1188 return SCI_SUCCESS;
1189}
1190
Edmund Nadolskie3013702011-06-02 00:10:43 +00001191static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001192smp_request_await_response_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001193 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001194{
1195 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1196 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001197 /* In the AWAIT RESPONSE state, any TC completion is
1198 * unexpected. but if the TC has success status, we
1199 * complete the IO anyway.
1200 */
Dan Williams34a99152011-07-01 02:25:15 -07001201 ireq->scu_status = SCU_TASK_DONE_GOOD;
1202 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001203 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001204 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001205 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1206 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1207 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1208 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07001209 /* These status has been seen in a specific LSI
1210 * expander, which sometimes is not able to send smp
1211 * response within 2 ms. This causes our hardware break
1212 * the connection and set TC completion with one of
1213 * these SMP_XXX_XX_ERR status. For these type of error,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001214 * we ask ihost user to retry the request.
Dan Williamsa7e255a2011-05-11 08:27:47 -07001215 */
Dan Williams34a99152011-07-01 02:25:15 -07001216 ireq->scu_status = SCU_TASK_DONE_SMP_RESP_TO_ERR;
1217 ireq->sci_status = SCI_FAILURE_RETRY_REQUIRED;
Dan Williams5076a1a2011-06-27 14:57:03 -07001218 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001219 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001220 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001221 /* All other completion status cause the IO to be complete. If a NAK
1222 * was received, then it is up to the user to retry the request
1223 */
Dan Williams34a99152011-07-01 02:25:15 -07001224 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1225 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001226 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001227 break;
1228 }
1229
1230 return SCI_SUCCESS;
1231}
1232
Edmund Nadolskie3013702011-06-02 00:10:43 +00001233static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001234smp_request_await_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001235 u32 completion_code)
Dan Williamsc72086e2011-05-10 02:28:48 -07001236{
1237 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1238 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001239 ireq->scu_status = SCU_TASK_DONE_GOOD;
1240 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001241 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001242 break;
Dan Williamsc72086e2011-05-10 02:28:48 -07001243 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001244 /* All other completion status cause the IO to be
1245 * complete. If a NAK was received, then it is up to
1246 * the user to retry the request.
1247 */
Dan Williams34a99152011-07-01 02:25:15 -07001248 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1249 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001250 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsc72086e2011-05-10 02:28:48 -07001251 break;
1252 }
1253
1254 return SCI_SUCCESS;
1255}
1256
Dan Williamsba7cb222011-06-27 11:56:41 -07001257static struct scu_sgl_element *pio_sgl_next(struct isci_stp_request *stp_req)
Dan Williams5dec6f42011-05-10 02:28:49 -07001258{
Dan Williams312e0c22011-06-28 13:47:09 -07001259 struct scu_sgl_element *sgl;
1260 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5076a1a2011-06-27 14:57:03 -07001261 struct isci_request *ireq = to_ireq(stp_req);
Dan Williamsba7cb222011-06-27 11:56:41 -07001262 struct isci_stp_pio_sgl *pio_sgl = &stp_req->sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001263
Dan Williams5076a1a2011-06-27 14:57:03 -07001264 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001265 if (!sgl_pair)
1266 sgl = NULL;
Dan Williamsba7cb222011-06-27 11:56:41 -07001267 else if (pio_sgl->set == SCU_SGL_ELEMENT_PAIR_A) {
Dan Williams312e0c22011-06-28 13:47:09 -07001268 if (sgl_pair->B.address_lower == 0 &&
1269 sgl_pair->B.address_upper == 0) {
1270 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001271 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001272 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_B;
Dan Williams312e0c22011-06-28 13:47:09 -07001273 sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001274 }
1275 } else {
Dan Williams312e0c22011-06-28 13:47:09 -07001276 if (sgl_pair->next_pair_lower == 0 &&
1277 sgl_pair->next_pair_upper == 0) {
1278 sgl = NULL;
Dan Williams5dec6f42011-05-10 02:28:49 -07001279 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001280 pio_sgl->index++;
1281 pio_sgl->set = SCU_SGL_ELEMENT_PAIR_A;
Dan Williams5076a1a2011-06-27 14:57:03 -07001282 sgl_pair = to_sgl_element_pair(ireq, pio_sgl->index);
Dan Williams312e0c22011-06-28 13:47:09 -07001283 sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001284 }
1285 }
1286
Dan Williams312e0c22011-06-28 13:47:09 -07001287 return sgl;
Dan Williams5dec6f42011-05-10 02:28:49 -07001288}
1289
Edmund Nadolskie3013702011-06-02 00:10:43 +00001290static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001291stp_request_non_data_await_h2d_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001292 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001293{
1294 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1295 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001296 ireq->scu_status = SCU_TASK_DONE_GOOD;
1297 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001298 sci_change_state(&ireq->sm, SCI_REQ_STP_NON_DATA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07001299 break;
1300
1301 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001302 /* All other completion status cause the IO to be
1303 * complete. If a NAK was received, then it is up to
1304 * the user to retry the request.
1305 */
Dan Williams34a99152011-07-01 02:25:15 -07001306 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1307 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001308 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001309 break;
1310 }
1311
1312 return SCI_SUCCESS;
1313}
1314
Dan Williams5dec6f42011-05-10 02:28:49 -07001315#define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
1316
1317/* transmit DATA_FIS from (current sgl + offset) for input
1318 * parameter length. current sgl and offset is alreay stored in the IO request
1319 */
Dan Williams89a73012011-06-30 19:14:33 -07001320static enum sci_status sci_stp_request_pio_data_out_trasmit_data_frame(
Dan Williams5076a1a2011-06-27 14:57:03 -07001321 struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001322 u32 length)
1323{
Dan Williams5076a1a2011-06-27 14:57:03 -07001324 struct isci_stp_request *stp_req = &ireq->stp.req;
1325 struct scu_task_context *task_context = ireq->tc;
Dan Williams312e0c22011-06-28 13:47:09 -07001326 struct scu_sgl_element_pair *sgl_pair;
Dan Williams5dec6f42011-05-10 02:28:49 -07001327 struct scu_sgl_element *current_sgl;
1328
1329 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
1330 * for the data from current_sgl+offset for the input length
1331 */
Dan Williams5076a1a2011-06-27 14:57:03 -07001332 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williamsba7cb222011-06-27 11:56:41 -07001333 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A)
Dan Williams312e0c22011-06-28 13:47:09 -07001334 current_sgl = &sgl_pair->A;
Dan Williams5dec6f42011-05-10 02:28:49 -07001335 else
Dan Williams312e0c22011-06-28 13:47:09 -07001336 current_sgl = &sgl_pair->B;
Dan Williams5dec6f42011-05-10 02:28:49 -07001337
1338 /* update the TC */
1339 task_context->command_iu_upper = current_sgl->address_upper;
1340 task_context->command_iu_lower = current_sgl->address_lower;
1341 task_context->transfer_length_bytes = length;
1342 task_context->type.stp.fis_type = FIS_DATA;
1343
1344 /* send the new TC out. */
Dan Williams89a73012011-06-30 19:14:33 -07001345 return sci_controller_continue_io(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001346}
1347
Dan Williams89a73012011-06-30 19:14:33 -07001348static enum sci_status sci_stp_request_pio_data_out_transmit_data(struct isci_request *ireq)
Dan Williams5dec6f42011-05-10 02:28:49 -07001349{
Dan Williams5076a1a2011-06-27 14:57:03 -07001350 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams312e0c22011-06-28 13:47:09 -07001351 struct scu_sgl_element_pair *sgl_pair;
Dan Williamsb50102d2011-09-30 18:52:19 -07001352 enum sci_status status = SCI_SUCCESS;
Dan Williamsba7cb222011-06-27 11:56:41 -07001353 struct scu_sgl_element *sgl;
Dan Williamsba7cb222011-06-27 11:56:41 -07001354 u32 offset;
1355 u32 len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001356
Dan Williamsba7cb222011-06-27 11:56:41 -07001357 offset = stp_req->sgl.offset;
Dan Williams5076a1a2011-06-27 14:57:03 -07001358 sgl_pair = to_sgl_element_pair(ireq, stp_req->sgl.index);
Dan Williams312e0c22011-06-28 13:47:09 -07001359 if (WARN_ONCE(!sgl_pair, "%s: null sgl element", __func__))
1360 return SCI_FAILURE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001361
Dan Williamsba7cb222011-06-27 11:56:41 -07001362 if (stp_req->sgl.set == SCU_SGL_ELEMENT_PAIR_A) {
1363 sgl = &sgl_pair->A;
1364 len = sgl_pair->A.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001365 } else {
Dan Williamsba7cb222011-06-27 11:56:41 -07001366 sgl = &sgl_pair->B;
1367 len = sgl_pair->B.length - offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001368 }
1369
Dan Williamsba7cb222011-06-27 11:56:41 -07001370 if (stp_req->pio_len == 0)
1371 return SCI_SUCCESS;
Dan Williams5dec6f42011-05-10 02:28:49 -07001372
Dan Williamsba7cb222011-06-27 11:56:41 -07001373 if (stp_req->pio_len >= len) {
Dan Williams89a73012011-06-30 19:14:33 -07001374 status = sci_stp_request_pio_data_out_trasmit_data_frame(ireq, len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001375 if (status != SCI_SUCCESS)
1376 return status;
1377 stp_req->pio_len -= len;
Dan Williams5dec6f42011-05-10 02:28:49 -07001378
Dan Williamsba7cb222011-06-27 11:56:41 -07001379 /* update the current sgl, offset and save for future */
1380 sgl = pio_sgl_next(stp_req);
1381 offset = 0;
1382 } else if (stp_req->pio_len < len) {
Dan Williams89a73012011-06-30 19:14:33 -07001383 sci_stp_request_pio_data_out_trasmit_data_frame(ireq, stp_req->pio_len);
Dan Williamsba7cb222011-06-27 11:56:41 -07001384
1385 /* Sgl offset will be adjusted and saved for future */
1386 offset += stp_req->pio_len;
1387 sgl->address_lower += stp_req->pio_len;
1388 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001389 }
1390
Dan Williamsba7cb222011-06-27 11:56:41 -07001391 stp_req->sgl.offset = offset;
Dan Williams5dec6f42011-05-10 02:28:49 -07001392
1393 return status;
1394}
1395
1396/**
1397 *
1398 * @stp_request: The request that is used for the SGL processing.
1399 * @data_buffer: The buffer of data to be copied.
1400 * @length: The length of the data transfer.
1401 *
1402 * Copy the data from the buffer for the length specified to the IO reqeust SGL
1403 * specified data region. enum sci_status
1404 */
1405static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001406sci_stp_request_pio_data_in_copy_data_buffer(struct isci_stp_request *stp_req,
Dan Williamsb50102d2011-09-30 18:52:19 -07001407 u8 *data_buf, u32 len)
Dan Williams5dec6f42011-05-10 02:28:49 -07001408{
Dan Williams5dec6f42011-05-10 02:28:49 -07001409 struct isci_request *ireq;
1410 u8 *src_addr;
1411 int copy_len;
1412 struct sas_task *task;
1413 struct scatterlist *sg;
1414 void *kaddr;
1415 int total_len = len;
1416
Dan Williams5076a1a2011-06-27 14:57:03 -07001417 ireq = to_ireq(stp_req);
Dan Williams5dec6f42011-05-10 02:28:49 -07001418 task = isci_request_access_task(ireq);
1419 src_addr = data_buf;
1420
1421 if (task->num_scatter > 0) {
1422 sg = task->scatter;
1423
1424 while (total_len > 0) {
1425 struct page *page = sg_page(sg);
1426
1427 copy_len = min_t(int, total_len, sg_dma_len(sg));
Cong Wang77dfce02011-11-25 23:14:23 +08001428 kaddr = kmap_atomic(page);
Dan Williams5dec6f42011-05-10 02:28:49 -07001429 memcpy(kaddr + sg->offset, src_addr, copy_len);
Cong Wang77dfce02011-11-25 23:14:23 +08001430 kunmap_atomic(kaddr);
Dan Williams5dec6f42011-05-10 02:28:49 -07001431 total_len -= copy_len;
1432 src_addr += copy_len;
1433 sg = sg_next(sg);
1434 }
1435 } else {
1436 BUG_ON(task->total_xfer_len < total_len);
1437 memcpy(task->scatter, src_addr, total_len);
1438 }
1439
1440 return SCI_SUCCESS;
1441}
1442
1443/**
1444 *
1445 * @sci_req: The PIO DATA IN request that is to receive the data.
1446 * @data_buffer: The buffer to copy from.
1447 *
1448 * Copy the data buffer to the io request data region. enum sci_status
1449 */
Dan Williams89a73012011-06-30 19:14:33 -07001450static enum sci_status sci_stp_request_pio_data_in_copy_data(
Dan Williamsba7cb222011-06-27 11:56:41 -07001451 struct isci_stp_request *stp_req,
Dan Williams5dec6f42011-05-10 02:28:49 -07001452 u8 *data_buffer)
1453{
1454 enum sci_status status;
1455
1456 /*
1457 * If there is less than 1K remaining in the transfer request
1458 * copy just the data for the transfer */
Dan Williamsba7cb222011-06-27 11:56:41 -07001459 if (stp_req->pio_len < SCU_MAX_FRAME_BUFFER_SIZE) {
Dan Williams89a73012011-06-30 19:14:33 -07001460 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001461 stp_req, data_buffer, stp_req->pio_len);
Dan Williams5dec6f42011-05-10 02:28:49 -07001462
1463 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001464 stp_req->pio_len = 0;
Dan Williams5dec6f42011-05-10 02:28:49 -07001465 } else {
1466 /* We are transfering the whole frame so copy */
Dan Williams89a73012011-06-30 19:14:33 -07001467 status = sci_stp_request_pio_data_in_copy_data_buffer(
Dan Williamsba7cb222011-06-27 11:56:41 -07001468 stp_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
Dan Williams5dec6f42011-05-10 02:28:49 -07001469
1470 if (status == SCI_SUCCESS)
Dan Williamsba7cb222011-06-27 11:56:41 -07001471 stp_req->pio_len -= SCU_MAX_FRAME_BUFFER_SIZE;
Dan Williams5dec6f42011-05-10 02:28:49 -07001472 }
1473
1474 return status;
1475}
1476
Edmund Nadolskie3013702011-06-02 00:10:43 +00001477static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001478stp_request_pio_await_h2d_completion_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001479 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001480{
1481 enum sci_status status = SCI_SUCCESS;
1482
1483 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1484 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07001485 ireq->scu_status = SCU_TASK_DONE_GOOD;
1486 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001487 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001488 break;
1489
1490 default:
Dan Williamsa7e255a2011-05-11 08:27:47 -07001491 /* All other completion status cause the IO to be
1492 * complete. If a NAK was received, then it is up to
1493 * the user to retry the request.
1494 */
Dan Williams34a99152011-07-01 02:25:15 -07001495 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1496 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001497 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001498 break;
1499 }
1500
1501 return status;
1502}
1503
Edmund Nadolskie3013702011-06-02 00:10:43 +00001504static enum sci_status
Dan Williams5076a1a2011-06-27 14:57:03 -07001505pio_data_out_tx_done_tc_event(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001506 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07001507{
1508 enum sci_status status = SCI_SUCCESS;
1509 bool all_frames_transferred = false;
Dan Williams5076a1a2011-06-27 14:57:03 -07001510 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williams5dec6f42011-05-10 02:28:49 -07001511
1512 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1513 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1514 /* Transmit data */
Dan Williamsba7cb222011-06-27 11:56:41 -07001515 if (stp_req->pio_len != 0) {
Dan Williams89a73012011-06-30 19:14:33 -07001516 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williams5dec6f42011-05-10 02:28:49 -07001517 if (status == SCI_SUCCESS) {
Dan Williamsba7cb222011-06-27 11:56:41 -07001518 if (stp_req->pio_len == 0)
Dan Williams5dec6f42011-05-10 02:28:49 -07001519 all_frames_transferred = true;
1520 }
Dan Williamsba7cb222011-06-27 11:56:41 -07001521 } else if (stp_req->pio_len == 0) {
Dan Williams5dec6f42011-05-10 02:28:49 -07001522 /*
1523 * this will happen if the all data is written at the
1524 * first time after the pio setup fis is received
1525 */
1526 all_frames_transferred = true;
1527 }
1528
1529 /* all data transferred. */
1530 if (all_frames_transferred) {
1531 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001532 * Change the state to SCI_REQ_STP_PIO_DATA_IN
Dan Williams5dec6f42011-05-10 02:28:49 -07001533 * and wait for PIO_SETUP fis / or D2H REg fis. */
Dan Williams5076a1a2011-06-27 14:57:03 -07001534 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williams5dec6f42011-05-10 02:28:49 -07001535 }
1536 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001537
Dan Williams5dec6f42011-05-10 02:28:49 -07001538 default:
1539 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001540 * All other completion status cause the IO to be complete.
1541 * If a NAK was received, then it is up to the user to retry
1542 * the request.
1543 */
Dan Williams34a99152011-07-01 02:25:15 -07001544 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
1545 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001546 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07001547 break;
1548 }
1549
1550 return status;
1551}
1552
Dan Williams89a73012011-06-30 19:14:33 -07001553static enum sci_status sci_stp_request_udma_general_frame_handler(struct isci_request *ireq,
Dan Williams5dec6f42011-05-10 02:28:49 -07001554 u32 frame_index)
1555{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001556 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5dec6f42011-05-10 02:28:49 -07001557 struct dev_to_host_fis *frame_header;
1558 enum sci_status status;
1559 u32 *frame_buffer;
1560
Dan Williams89a73012011-06-30 19:14:33 -07001561 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001562 frame_index,
1563 (void **)&frame_header);
1564
1565 if ((status == SCI_SUCCESS) &&
1566 (frame_header->fis_type == FIS_REGD2H)) {
Dan Williams89a73012011-06-30 19:14:33 -07001567 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams5dec6f42011-05-10 02:28:49 -07001568 frame_index,
1569 (void **)&frame_buffer);
1570
Dan Williams89a73012011-06-30 19:14:33 -07001571 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williams5dec6f42011-05-10 02:28:49 -07001572 frame_header,
1573 frame_buffer);
1574 }
1575
Dan Williams89a73012011-06-30 19:14:33 -07001576 sci_controller_release_frame(ihost, frame_index);
Dan Williams5dec6f42011-05-10 02:28:49 -07001577
1578 return status;
1579}
1580
Dan Williamsb50102d2011-09-30 18:52:19 -07001581static enum sci_status process_unsolicited_fis(struct isci_request *ireq,
1582 u32 frame_index)
1583{
1584 struct isci_host *ihost = ireq->owning_controller;
1585 enum sci_status status;
1586 struct dev_to_host_fis *frame_header;
1587 u32 *frame_buffer;
1588
1589 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
1590 frame_index,
1591 (void **)&frame_header);
1592
1593 if (status != SCI_SUCCESS)
1594 return status;
1595
1596 if (frame_header->fis_type != FIS_REGD2H) {
1597 dev_err(&ireq->isci_host->pdev->dev,
1598 "%s ERROR: invalid fis type 0x%X\n",
1599 __func__, frame_header->fis_type);
1600 return SCI_FAILURE;
1601 }
1602
1603 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
1604 frame_index,
1605 (void **)&frame_buffer);
1606
1607 sci_controller_copy_sata_response(&ireq->stp.rsp,
1608 (u32 *)frame_header,
1609 frame_buffer);
1610
1611 /* Frame has been decoded return it to the controller */
1612 sci_controller_release_frame(ihost, frame_index);
1613
1614 return status;
1615}
1616
1617static enum sci_status atapi_d2h_reg_frame_handler(struct isci_request *ireq,
1618 u32 frame_index)
1619{
1620 struct sas_task *task = isci_request_access_task(ireq);
1621 enum sci_status status;
1622
1623 status = process_unsolicited_fis(ireq, frame_index);
1624
1625 if (status == SCI_SUCCESS) {
1626 if (ireq->stp.rsp.status & ATA_ERR)
1627 status = SCI_IO_FAILURE_RESPONSE_VALID;
1628 } else {
1629 status = SCI_IO_FAILURE_RESPONSE_VALID;
1630 }
1631
1632 if (status != SCI_SUCCESS) {
1633 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1634 ireq->sci_status = status;
1635 } else {
1636 ireq->scu_status = SCU_TASK_DONE_GOOD;
1637 ireq->sci_status = SCI_SUCCESS;
1638 }
1639
1640 /* the d2h ufi is the end of non-data commands */
1641 if (task->data_dir == DMA_NONE)
1642 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
1643
1644 return status;
1645}
1646
1647static void scu_atapi_reconstruct_raw_frame_task_context(struct isci_request *ireq)
1648{
1649 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1650 void *atapi_cdb = ireq->ttype_ptr.io_task_ptr->ata_task.atapi_packet;
1651 struct scu_task_context *task_context = ireq->tc;
1652
1653 /* fill in the SCU Task Context for a DATA fis containing CDB in Raw Frame
1654 * type. The TC for previous Packet fis was already there, we only need to
1655 * change the H2D fis content.
1656 */
1657 memset(&ireq->stp.cmd, 0, sizeof(struct host_to_dev_fis));
1658 memcpy(((u8 *)&ireq->stp.cmd + sizeof(u32)), atapi_cdb, ATAPI_CDB_LEN);
1659 memset(&(task_context->type.stp), 0, sizeof(struct stp_task_context));
1660 task_context->type.stp.fis_type = FIS_DATA;
1661 task_context->transfer_length_bytes = dev->cdb_len;
1662}
1663
1664static void scu_atapi_construct_task_context(struct isci_request *ireq)
1665{
1666 struct ata_device *dev = sas_to_ata_dev(ireq->target_device->domain_dev);
1667 struct sas_task *task = isci_request_access_task(ireq);
1668 struct scu_task_context *task_context = ireq->tc;
1669 int cdb_len = dev->cdb_len;
1670
1671 /* reference: SSTL 1.13.4.2
1672 * task_type, sata_direction
1673 */
1674 if (task->data_dir == DMA_TO_DEVICE) {
1675 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_OUT;
1676 task_context->sata_direction = 0;
1677 } else {
1678 /* todo: for NO_DATA command, we need to send out raw frame. */
1679 task_context->task_type = SCU_TASK_TYPE_PACKET_DMA_IN;
1680 task_context->sata_direction = 1;
1681 }
1682
1683 memset(&task_context->type.stp, 0, sizeof(task_context->type.stp));
1684 task_context->type.stp.fis_type = FIS_DATA;
1685
1686 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
1687 memcpy(&ireq->stp.cmd.lbal, task->ata_task.atapi_packet, cdb_len);
1688 task_context->ssp_command_iu_length = cdb_len / sizeof(u32);
1689
1690 /* task phase is set to TX_CMD */
1691 task_context->task_phase = 0x1;
1692
1693 /* retry counter */
1694 task_context->stp_retry_count = 0;
1695
1696 /* data transfer size. */
1697 task_context->transfer_length_bytes = task->total_xfer_len;
1698
1699 /* setup sgl */
1700 sci_request_build_sgl(ireq);
1701}
1702
Edmund Nadolskie3013702011-06-02 00:10:43 +00001703enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07001704sci_io_request_frame_handler(struct isci_request *ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001705 u32 frame_index)
Dan Williamsd1c637c32011-05-11 08:27:47 -07001706{
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001707 struct isci_host *ihost = ireq->owning_controller;
Dan Williams5076a1a2011-06-27 14:57:03 -07001708 struct isci_stp_request *stp_req = &ireq->stp.req;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001709 enum sci_base_request_states state;
1710 enum sci_status status;
1711 ssize_t word_cnt;
1712
Dan Williams5076a1a2011-06-27 14:57:03 -07001713 state = ireq->sm.current_state_id;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001714 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001715 case SCI_REQ_STARTED: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001716 struct ssp_frame_hdr ssp_hdr;
1717 void *frame_header;
1718
Dan Williams89a73012011-06-30 19:14:33 -07001719 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001720 frame_index,
1721 &frame_header);
1722
1723 word_cnt = sizeof(struct ssp_frame_hdr) / sizeof(u32);
1724 sci_swab32_cpy(&ssp_hdr, frame_header, word_cnt);
1725
1726 if (ssp_hdr.frame_type == SSP_RESPONSE) {
1727 struct ssp_response_iu *resp_iu;
1728 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1729
Dan Williams89a73012011-06-30 19:14:33 -07001730 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001731 frame_index,
1732 (void **)&resp_iu);
1733
Dan Williams5076a1a2011-06-27 14:57:03 -07001734 sci_swab32_cpy(&ireq->ssp.rsp, resp_iu, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001735
Dan Williams5076a1a2011-06-27 14:57:03 -07001736 resp_iu = &ireq->ssp.rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001737
1738 if (resp_iu->datapres == 0x01 ||
1739 resp_iu->datapres == 0x02) {
Dan Williams34a99152011-07-01 02:25:15 -07001740 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1741 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
1742 } else {
1743 ireq->scu_status = SCU_TASK_DONE_GOOD;
1744 ireq->sci_status = SCI_SUCCESS;
1745 }
Dan Williamsd1c637c32011-05-11 08:27:47 -07001746 } else {
1747 /* not a response frame, why did it get forwarded? */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001748 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001749 "%s: SCIC IO Request 0x%p received unexpected "
Dan Williams5076a1a2011-06-27 14:57:03 -07001750 "frame %d type 0x%02x\n", __func__, ireq,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001751 frame_index, ssp_hdr.frame_type);
1752 }
1753
1754 /*
Edmund Nadolskie3013702011-06-02 00:10:43 +00001755 * In any case we are done with this frame buffer return it to
1756 * the controller
Dan Williamsd1c637c32011-05-11 08:27:47 -07001757 */
Dan Williams89a73012011-06-30 19:14:33 -07001758 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001759
1760 return SCI_SUCCESS;
1761 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001762
1763 case SCI_REQ_TASK_WAIT_TC_RESP:
Dan Williams89a73012011-06-30 19:14:33 -07001764 sci_io_request_copy_response(ireq);
Dan Williams5076a1a2011-06-27 14:57:03 -07001765 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams89a73012011-06-30 19:14:33 -07001766 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001767 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001768
1769 case SCI_REQ_SMP_WAIT_RESP: {
Dan Williams54b5e3a2011-09-28 18:35:27 -07001770 struct sas_task *task = isci_request_access_task(ireq);
1771 struct scatterlist *sg = &task->smp_task.smp_resp;
1772 void *frame_header, *kaddr;
1773 u8 *rsp;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001774
Dan Williams89a73012011-06-30 19:14:33 -07001775 sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001776 frame_index,
1777 &frame_header);
Cong Wang77dfce02011-11-25 23:14:23 +08001778 kaddr = kmap_atomic(sg_page(sg));
Dan Williams54b5e3a2011-09-28 18:35:27 -07001779 rsp = kaddr + sg->offset;
1780 sci_swab32_cpy(rsp, frame_header, 1);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001781
Dan Williams54b5e3a2011-09-28 18:35:27 -07001782 if (rsp[0] == SMP_RESPONSE) {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001783 void *smp_resp;
1784
Dan Williams89a73012011-06-30 19:14:33 -07001785 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001786 frame_index,
1787 &smp_resp);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001788
Dan Williams54b5e3a2011-09-28 18:35:27 -07001789 word_cnt = (sg->length/4)-1;
1790 if (word_cnt > 0)
1791 word_cnt = min_t(unsigned int, word_cnt,
1792 SCU_UNSOLICITED_FRAME_BUFFER_SIZE/4);
1793 sci_swab32_cpy(rsp + 4, smp_resp, word_cnt);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001794
Dan Williams34a99152011-07-01 02:25:15 -07001795 ireq->scu_status = SCU_TASK_DONE_GOOD;
1796 ireq->sci_status = SCI_SUCCESS;
Dan Williams5076a1a2011-06-27 14:57:03 -07001797 sci_change_state(&ireq->sm, SCI_REQ_SMP_WAIT_TC_COMP);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001798 } else {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001799 /*
1800 * This was not a response frame why did it get
1801 * forwarded?
1802 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001803 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001804 "%s: SCIC SMP Request 0x%p received unexpected "
1805 "frame %d type 0x%02x\n",
1806 __func__,
Dan Williams5076a1a2011-06-27 14:57:03 -07001807 ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001808 frame_index,
Dan Williams54b5e3a2011-09-28 18:35:27 -07001809 rsp[0]);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001810
Dan Williams34a99152011-07-01 02:25:15 -07001811 ireq->scu_status = SCU_TASK_DONE_SMP_FRM_TYPE_ERR;
1812 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
Dan Williams5076a1a2011-06-27 14:57:03 -07001813 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001814 }
Cong Wang77dfce02011-11-25 23:14:23 +08001815 kunmap_atomic(kaddr);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001816
Dan Williams89a73012011-06-30 19:14:33 -07001817 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001818
1819 return SCI_SUCCESS;
1820 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001821
1822 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams89a73012011-06-30 19:14:33 -07001823 return sci_stp_request_udma_general_frame_handler(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001824 frame_index);
1825
1826 case SCI_REQ_STP_UDMA_WAIT_D2H:
Dan Williamsd1c637c32011-05-11 08:27:47 -07001827 /* Use the general frame handler to copy the resposne data */
Dan Williams34a99152011-07-01 02:25:15 -07001828 status = sci_stp_request_udma_general_frame_handler(ireq, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001829
1830 if (status != SCI_SUCCESS)
1831 return status;
1832
Dan Williams34a99152011-07-01 02:25:15 -07001833 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1834 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
1835 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001836 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001837
1838 case SCI_REQ_STP_NON_DATA_WAIT_D2H: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001839 struct dev_to_host_fis *frame_header;
1840 u32 *frame_buffer;
1841
Dan Williams89a73012011-06-30 19:14:33 -07001842 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001843 frame_index,
1844 (void **)&frame_header);
1845
1846 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001847 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001848 "%s: SCIC IO Request 0x%p could not get frame "
1849 "header for frame index %d, status %x\n",
1850 __func__,
1851 stp_req,
1852 frame_index,
1853 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001854
1855 return status;
1856 }
1857
1858 switch (frame_header->fis_type) {
1859 case FIS_REGD2H:
Dan Williams89a73012011-06-30 19:14:33 -07001860 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001861 frame_index,
1862 (void **)&frame_buffer);
1863
Dan Williams89a73012011-06-30 19:14:33 -07001864 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001865 frame_header,
1866 frame_buffer);
1867
1868 /* The command has completed with error */
Dan Williams34a99152011-07-01 02:25:15 -07001869 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1870 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001871 break;
1872
1873 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001874 dev_warn(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001875 "%s: IO Request:0x%p Frame Id:%d protocol "
1876 "violation occurred\n", __func__, stp_req,
1877 frame_index);
1878
Dan Williams34a99152011-07-01 02:25:15 -07001879 ireq->scu_status = SCU_TASK_DONE_UNEXP_FIS;
1880 ireq->sci_status = SCI_FAILURE_PROTOCOL_VIOLATION;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001881 break;
1882 }
1883
Dan Williams5076a1a2011-06-27 14:57:03 -07001884 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001885
1886 /* Frame has been decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001887 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001888
1889 return status;
1890 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001891
1892 case SCI_REQ_STP_PIO_WAIT_FRAME: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001893 struct sas_task *task = isci_request_access_task(ireq);
1894 struct dev_to_host_fis *frame_header;
1895 u32 *frame_buffer;
1896
Dan Williams89a73012011-06-30 19:14:33 -07001897 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001898 frame_index,
1899 (void **)&frame_header);
1900
1901 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001902 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00001903 "%s: SCIC IO Request 0x%p could not get frame "
1904 "header for frame index %d, status %x\n",
Dan Williamsd1c637c32011-05-11 08:27:47 -07001905 __func__, stp_req, frame_index, status);
1906 return status;
1907 }
1908
1909 switch (frame_header->fis_type) {
1910 case FIS_PIO_SETUP:
1911 /* Get from the frame buffer the PIO Setup Data */
Dan Williams89a73012011-06-30 19:14:33 -07001912 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001913 frame_index,
1914 (void **)&frame_buffer);
1915
Edmund Nadolskie3013702011-06-02 00:10:43 +00001916 /* Get the data from the PIO Setup The SCU Hardware
1917 * returns first word in the frame_header and the rest
1918 * of the data is in the frame buffer so we need to
1919 * back up one dword
Dan Williamsd1c637c32011-05-11 08:27:47 -07001920 */
1921
1922 /* transfer_count: first 16bits in the 4th dword */
Dan Williamsba7cb222011-06-27 11:56:41 -07001923 stp_req->pio_len = frame_buffer[3] & 0xffff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001924
Dan Williamsba7cb222011-06-27 11:56:41 -07001925 /* status: 4th byte in the 3rd dword */
1926 stp_req->status = (frame_buffer[2] >> 24) & 0xff;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001927
Dan Williams89a73012011-06-30 19:14:33 -07001928 sci_controller_copy_sata_response(&ireq->stp.rsp,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001929 frame_header,
1930 frame_buffer);
1931
Dan Williams5076a1a2011-06-27 14:57:03 -07001932 ireq->stp.rsp.status = stp_req->status;
Dan Williamsd1c637c32011-05-11 08:27:47 -07001933
1934 /* The next state is dependent on whether the
1935 * request was PIO Data-in or Data out
1936 */
1937 if (task->data_dir == DMA_FROM_DEVICE) {
Dan Williams5076a1a2011-06-27 14:57:03 -07001938 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_IN);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001939 } else if (task->data_dir == DMA_TO_DEVICE) {
1940 /* Transmit data */
Dan Williams89a73012011-06-30 19:14:33 -07001941 status = sci_stp_request_pio_data_out_transmit_data(ireq);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001942 if (status != SCI_SUCCESS)
1943 break;
Dan Williams5076a1a2011-06-27 14:57:03 -07001944 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_DATA_OUT);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001945 }
1946 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001947
Dan Williamsd1c637c32011-05-11 08:27:47 -07001948 case FIS_SETDEVBITS:
Dan Williams5076a1a2011-06-27 14:57:03 -07001949 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001950 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001951
Dan Williamsd1c637c32011-05-11 08:27:47 -07001952 case FIS_REGD2H:
1953 if (frame_header->status & ATA_BUSY) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00001954 /*
1955 * Now why is the drive sending a D2H Register
1956 * FIS when it is still busy? Do nothing since
1957 * we are still in the right state.
Dan Williamsd1c637c32011-05-11 08:27:47 -07001958 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07001959 dev_dbg(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001960 "%s: SCIC PIO Request 0x%p received "
1961 "D2H Register FIS with BSY status "
Edmund Nadolskie3013702011-06-02 00:10:43 +00001962 "0x%x\n",
1963 __func__,
1964 stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001965 frame_header->status);
1966 break;
1967 }
1968
Dan Williams89a73012011-06-30 19:14:33 -07001969 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001970 frame_index,
1971 (void **)&frame_buffer);
1972
Dan Williams89a73012011-06-30 19:14:33 -07001973 sci_controller_copy_sata_response(&ireq->stp.req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001974 frame_header,
1975 frame_buffer);
1976
Dan Williams34a99152011-07-01 02:25:15 -07001977 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
1978 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07001979 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001980 break;
Edmund Nadolskie3013702011-06-02 00:10:43 +00001981
Dan Williamsd1c637c32011-05-11 08:27:47 -07001982 default:
1983 /* FIXME: what do we do here? */
1984 break;
1985 }
1986
1987 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07001988 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07001989
1990 return status;
1991 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00001992
1993 case SCI_REQ_STP_PIO_DATA_IN: {
Dan Williamsd1c637c32011-05-11 08:27:47 -07001994 struct dev_to_host_fis *frame_header;
1995 struct sata_fis_data *frame_buffer;
1996
Dan Williams89a73012011-06-30 19:14:33 -07001997 status = sci_unsolicited_frame_control_get_header(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07001998 frame_index,
1999 (void **)&frame_header);
2000
2001 if (status != SCI_SUCCESS) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002002 dev_err(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002003 "%s: SCIC IO Request 0x%p could not get frame "
2004 "header for frame index %d, status %x\n",
2005 __func__,
2006 stp_req,
2007 frame_index,
2008 status);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002009 return status;
2010 }
2011
2012 if (frame_header->fis_type != FIS_DATA) {
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002013 dev_err(&ihost->pdev->dev,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002014 "%s: SCIC PIO Request 0x%p received frame %d "
2015 "with fis type 0x%02x when expecting a data "
Edmund Nadolskie3013702011-06-02 00:10:43 +00002016 "fis.\n",
2017 __func__,
2018 stp_req,
2019 frame_index,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002020 frame_header->fis_type);
2021
Dan Williams34a99152011-07-01 02:25:15 -07002022 ireq->scu_status = SCU_TASK_DONE_GOOD;
2023 ireq->sci_status = SCI_FAILURE_IO_REQUIRES_SCSI_ABORT;
Dan Williams5076a1a2011-06-27 14:57:03 -07002024 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002025
2026 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07002027 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002028 return status;
2029 }
2030
Dan Williamsba7cb222011-06-27 11:56:41 -07002031 if (stp_req->sgl.index < 0) {
Dan Williams5076a1a2011-06-27 14:57:03 -07002032 ireq->saved_rx_frame_index = frame_index;
Dan Williamsba7cb222011-06-27 11:56:41 -07002033 stp_req->pio_len = 0;
Dan Williamsd1c637c32011-05-11 08:27:47 -07002034 } else {
Dan Williams89a73012011-06-30 19:14:33 -07002035 sci_unsolicited_frame_control_get_buffer(&ihost->uf_control,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002036 frame_index,
2037 (void **)&frame_buffer);
2038
Dan Williams89a73012011-06-30 19:14:33 -07002039 status = sci_stp_request_pio_data_in_copy_data(stp_req,
Dan Williamsd1c637c32011-05-11 08:27:47 -07002040 (u8 *)frame_buffer);
2041
2042 /* Frame is decoded return it to the controller */
Dan Williams89a73012011-06-30 19:14:33 -07002043 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002044 }
2045
2046 /* Check for the end of the transfer, are there more
2047 * bytes remaining for this data transfer
2048 */
Dan Williamsba7cb222011-06-27 11:56:41 -07002049 if (status != SCI_SUCCESS || stp_req->pio_len != 0)
Dan Williamsd1c637c32011-05-11 08:27:47 -07002050 return status;
2051
Dan Williamsba7cb222011-06-27 11:56:41 -07002052 if ((stp_req->status & ATA_BUSY) == 0) {
Dan Williams34a99152011-07-01 02:25:15 -07002053 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2054 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
Dan Williams5076a1a2011-06-27 14:57:03 -07002055 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002056 } else {
Dan Williams5076a1a2011-06-27 14:57:03 -07002057 sci_change_state(&ireq->sm, SCI_REQ_STP_PIO_WAIT_FRAME);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002058 }
2059 return status;
2060 }
Edmund Nadolskie3013702011-06-02 00:10:43 +00002061
Dan Williamsb50102d2011-09-30 18:52:19 -07002062 case SCI_REQ_ATAPI_WAIT_PIO_SETUP: {
2063 struct sas_task *task = isci_request_access_task(ireq);
2064
2065 sci_controller_release_frame(ihost, frame_index);
2066 ireq->target_device->working_request = ireq;
2067 if (task->data_dir == DMA_NONE) {
2068 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_TC_COMP);
2069 scu_atapi_reconstruct_raw_frame_task_context(ireq);
2070 } else {
2071 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2072 scu_atapi_construct_task_context(ireq);
2073 }
2074
2075 sci_controller_continue_io(ireq);
2076 return SCI_SUCCESS;
2077 }
2078 case SCI_REQ_ATAPI_WAIT_D2H:
2079 return atapi_d2h_reg_frame_handler(ireq, frame_index);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002080 case SCI_REQ_ABORTING:
2081 /*
2082 * TODO: Is it even possible to get an unsolicited frame in the
Dan Williamsd1c637c32011-05-11 08:27:47 -07002083 * aborting state?
2084 */
Dan Williams89a73012011-06-30 19:14:33 -07002085 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002086 return SCI_SUCCESS;
Edmund Nadolskie3013702011-06-02 00:10:43 +00002087
Dan Williamsd1c637c32011-05-11 08:27:47 -07002088 default:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002089 dev_warn(&ihost->pdev->dev,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002090 "%s: SCIC IO Request given unexpected frame %x while "
2091 "in state %d\n",
2092 __func__,
2093 frame_index,
2094 state);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002095
Dan Williams89a73012011-06-30 19:14:33 -07002096 sci_controller_release_frame(ihost, frame_index);
Dan Williamsd1c637c32011-05-11 08:27:47 -07002097 return SCI_FAILURE_INVALID_STATE;
2098 }
2099}
2100
Dan Williams5076a1a2011-06-27 14:57:03 -07002101static enum sci_status stp_request_udma_await_tc_event(struct isci_request *ireq,
Dan Williamsa7e255a2011-05-11 08:27:47 -07002102 u32 completion_code)
Dan Williams5dec6f42011-05-10 02:28:49 -07002103{
2104 enum sci_status status = SCI_SUCCESS;
2105
2106 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2107 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
Dan Williams34a99152011-07-01 02:25:15 -07002108 ireq->scu_status = SCU_TASK_DONE_GOOD;
2109 ireq->sci_status = SCI_SUCCESS;
2110 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002111 break;
2112 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
2113 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
Dan Williamsa7e255a2011-05-11 08:27:47 -07002114 /* We must check ther response buffer to see if the D2H
2115 * Register FIS was received before we got the TC
2116 * completion.
2117 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002118 if (ireq->stp.rsp.fis_type == FIS_REGD2H) {
Dan Williams34a99152011-07-01 02:25:15 -07002119 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2120 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2121 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002122 } else {
Dan Williamsa7e255a2011-05-11 08:27:47 -07002123 /* If we have an error completion status for the
2124 * TC then we can expect a D2H register FIS from
2125 * the device so we must change state to wait
2126 * for it
2127 */
Dan Williams5076a1a2011-06-27 14:57:03 -07002128 sci_change_state(&ireq->sm, SCI_REQ_STP_UDMA_WAIT_D2H);
Dan Williams5dec6f42011-05-10 02:28:49 -07002129 }
2130 break;
2131
Dan Williamsa7e255a2011-05-11 08:27:47 -07002132 /* TODO Check to see if any of these completion status need to
2133 * wait for the device to host register fis.
2134 */
2135 /* TODO We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR
2136 * - this comes only for B0
2137 */
Dan Williams5dec6f42011-05-10 02:28:49 -07002138 default:
2139 /* All other completion status cause the IO to be complete. */
Dan Williams34a99152011-07-01 02:25:15 -07002140 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2141 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2142 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
Dan Williams5dec6f42011-05-10 02:28:49 -07002143 break;
2144 }
2145
2146 return status;
2147}
2148
Dan Williamsb50102d2011-09-30 18:52:19 -07002149static enum sci_status atapi_raw_completion(struct isci_request *ireq, u32 completion_code,
2150 enum sci_base_request_states next)
2151{
2152 enum sci_status status = SCI_SUCCESS;
2153
2154 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2155 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
2156 ireq->scu_status = SCU_TASK_DONE_GOOD;
2157 ireq->sci_status = SCI_SUCCESS;
2158 sci_change_state(&ireq->sm, next);
2159 break;
2160 default:
2161 /* All other completion status cause the IO to be complete.
2162 * If a NAK was received, then it is up to the user to retry
2163 * the request.
2164 */
2165 ireq->scu_status = SCU_NORMALIZE_COMPLETION_STATUS(completion_code);
2166 ireq->sci_status = SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR;
2167
2168 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2169 break;
2170 }
2171
2172 return status;
2173}
2174
2175static enum sci_status atapi_data_tc_completion_handler(struct isci_request *ireq,
2176 u32 completion_code)
2177{
2178 struct isci_remote_device *idev = ireq->target_device;
2179 struct dev_to_host_fis *d2h = &ireq->stp.rsp;
2180 enum sci_status status = SCI_SUCCESS;
2181
2182 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
2183 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
2184 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2185 break;
2186
2187 case (SCU_TASK_DONE_UNEXP_FIS << SCU_COMPLETION_TL_STATUS_SHIFT): {
2188 u16 len = sci_req_tx_bytes(ireq);
2189
2190 /* likely non-error data underrrun, workaround missing
2191 * d2h frame from the controller
2192 */
2193 if (d2h->fis_type != FIS_REGD2H) {
2194 d2h->fis_type = FIS_REGD2H;
2195 d2h->flags = (1 << 6);
2196 d2h->status = 0x50;
2197 d2h->error = 0;
2198 d2h->lbal = 0;
2199 d2h->byte_count_low = len & 0xff;
2200 d2h->byte_count_high = len >> 8;
2201 d2h->device = 0xa0;
2202 d2h->lbal_exp = 0;
2203 d2h->lbam_exp = 0;
2204 d2h->lbah_exp = 0;
2205 d2h->_r_a = 0;
2206 d2h->sector_count = 0x3;
2207 d2h->sector_count_exp = 0;
2208 d2h->_r_b = 0;
2209 d2h->_r_c = 0;
2210 d2h->_r_d = 0;
2211 }
2212
2213 ireq->scu_status = SCU_TASK_DONE_GOOD;
2214 ireq->sci_status = SCI_SUCCESS_IO_DONE_EARLY;
2215 status = ireq->sci_status;
2216
2217 /* the hw will have suspended the rnc, so complete the
2218 * request upon pending resume
2219 */
2220 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2221 break;
2222 }
2223 case (SCU_TASK_DONE_EXCESS_DATA << SCU_COMPLETION_TL_STATUS_SHIFT):
2224 /* In this case, there is no UF coming after.
2225 * compelte the IO now.
2226 */
2227 ireq->scu_status = SCU_TASK_DONE_GOOD;
2228 ireq->sci_status = SCI_SUCCESS;
2229 sci_change_state(&ireq->sm, SCI_REQ_COMPLETED);
2230 break;
2231
2232 default:
2233 if (d2h->fis_type == FIS_REGD2H) {
2234 /* UF received change the device state to ATAPI_ERROR */
2235 status = ireq->sci_status;
2236 sci_change_state(&idev->sm, SCI_STP_DEV_ATAPI_ERROR);
2237 } else {
2238 /* If receiving any non-sucess TC status, no UF
2239 * received yet, then an UF for the status fis
2240 * is coming after (XXX: suspect this is
2241 * actually a protocol error or a bug like the
2242 * DONE_UNEXP_FIS case)
2243 */
2244 ireq->scu_status = SCU_TASK_DONE_CHECK_RESPONSE;
2245 ireq->sci_status = SCI_FAILURE_IO_RESPONSE_VALID;
2246
2247 sci_change_state(&ireq->sm, SCI_REQ_ATAPI_WAIT_D2H);
2248 }
2249 break;
2250 }
2251
2252 return status;
2253}
2254
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002255static int sci_request_smp_completion_status_is_tx_suspend(
2256 unsigned int completion_status)
2257{
2258 switch (completion_status) {
2259 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2260 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2261 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2262 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2263 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2264 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2265 return 1;
2266 }
2267 return 0;
2268}
2269
2270static int sci_request_smp_completion_status_is_tx_rx_suspend(
2271 unsigned int completion_status)
2272{
2273 return 0; /* There are no Tx/Rx SMP suspend conditions. */
2274}
2275
2276static int sci_request_ssp_completion_status_is_tx_suspend(
2277 unsigned int completion_status)
2278{
2279 switch (completion_status) {
2280 case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2281 case SCU_TASK_DONE_LF_ERR:
2282 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2283 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2284 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2285 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2286 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2287 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2288 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2289 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2290 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2291 return 1;
2292 }
2293 return 0;
2294}
2295
2296static int sci_request_ssp_completion_status_is_tx_rx_suspend(
2297 unsigned int completion_status)
2298{
2299 return 0; /* There are no Tx/Rx SSP suspend conditions. */
2300}
2301
2302static int sci_request_stpsata_completion_status_is_tx_suspend(
2303 unsigned int completion_status)
2304{
2305 switch (completion_status) {
2306 case SCU_TASK_DONE_TX_RAW_CMD_ERR:
2307 case SCU_TASK_DONE_LL_R_ERR:
2308 case SCU_TASK_DONE_LL_PERR:
2309 case SCU_TASK_DONE_REG_ERR:
2310 case SCU_TASK_DONE_SDB_ERR:
2311 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2312 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2313 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2314 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2315 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2316 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2317 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2318 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2319 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2320 return 1;
2321 }
2322 return 0;
2323}
2324
2325
2326static int sci_request_stpsata_completion_status_is_tx_rx_suspend(
2327 unsigned int completion_status)
2328{
2329 switch (completion_status) {
2330 case SCU_TASK_DONE_LF_ERR:
2331 case SCU_TASK_DONE_LL_SY_TERM:
2332 case SCU_TASK_DONE_LL_LF_TERM:
2333 case SCU_TASK_DONE_BREAK_RCVD:
2334 case SCU_TASK_DONE_INV_FIS_LEN:
2335 case SCU_TASK_DONE_UNEXP_FIS:
2336 case SCU_TASK_DONE_UNEXP_SDBFIS:
2337 case SCU_TASK_DONE_MAX_PLD_ERR:
2338 return 1;
2339 }
2340 return 0;
2341}
2342
2343static void sci_request_handle_suspending_completions(
2344 struct isci_request *ireq,
2345 u32 completion_code)
2346{
2347 int is_tx = 0;
2348 int is_tx_rx = 0;
2349
2350 switch (ireq->protocol) {
2351 case SAS_PROTOCOL_SMP:
2352 is_tx = sci_request_smp_completion_status_is_tx_suspend(
2353 completion_code);
2354 is_tx_rx = sci_request_smp_completion_status_is_tx_rx_suspend(
2355 completion_code);
2356 break;
2357 case SAS_PROTOCOL_SSP:
2358 is_tx = sci_request_ssp_completion_status_is_tx_suspend(
2359 completion_code);
2360 is_tx_rx = sci_request_ssp_completion_status_is_tx_rx_suspend(
2361 completion_code);
2362 break;
2363 case SAS_PROTOCOL_STP:
2364 is_tx = sci_request_stpsata_completion_status_is_tx_suspend(
2365 completion_code);
2366 is_tx_rx =
2367 sci_request_stpsata_completion_status_is_tx_rx_suspend(
2368 completion_code);
2369 break;
2370 default:
2371 dev_warn(&ireq->isci_host->pdev->dev,
2372 "%s: request %p has no valid protocol\n",
2373 __func__, ireq);
2374 break;
2375 }
2376 if (is_tx || is_tx_rx) {
2377 BUG_ON(is_tx && is_tx_rx);
2378
2379 sci_remote_node_context_suspend(
2380 &ireq->target_device->rnc,
2381 SCU_HARDWARE_SUSPENSION,
2382 (is_tx_rx) ? SCU_EVENT_TL_RNC_SUSPEND_TX_RX
2383 : SCU_EVENT_TL_RNC_SUSPEND_TX,
2384 NULL, NULL);
2385 }
2386}
2387
Dan Williamsa7e255a2011-05-11 08:27:47 -07002388enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07002389sci_io_request_tc_completion(struct isci_request *ireq,
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002390 u32 completion_code)
Dan Williamsa7e255a2011-05-11 08:27:47 -07002391{
2392 enum sci_base_request_states state;
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002393 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002394
Dan Williams5076a1a2011-06-27 14:57:03 -07002395 state = ireq->sm.current_state_id;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002396
Jeff Skirvinac78ed02012-03-08 22:41:50 -08002397 /* Decode those completions that signal upcoming suspension events. */
2398 sci_request_handle_suspending_completions(
2399 ireq, SCU_GET_COMPLETION_TL_STATUS(completion_code));
2400
Dan Williamsa7e255a2011-05-11 08:27:47 -07002401 switch (state) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00002402 case SCI_REQ_STARTED:
Dan Williams5076a1a2011-06-27 14:57:03 -07002403 return request_started_state_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002404
2405 case SCI_REQ_TASK_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002406 return ssp_task_request_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002407 completion_code);
2408
2409 case SCI_REQ_SMP_WAIT_RESP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002410 return smp_request_await_response_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002411 completion_code);
2412
2413 case SCI_REQ_SMP_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002414 return smp_request_await_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002415
2416 case SCI_REQ_STP_UDMA_WAIT_TC_COMP:
Dan Williams5076a1a2011-06-27 14:57:03 -07002417 return stp_request_udma_await_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002418 completion_code);
2419
2420 case SCI_REQ_STP_NON_DATA_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002421 return stp_request_non_data_await_h2d_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002422 completion_code);
2423
2424 case SCI_REQ_STP_PIO_WAIT_H2D:
Dan Williams5076a1a2011-06-27 14:57:03 -07002425 return stp_request_pio_await_h2d_completion_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002426 completion_code);
2427
2428 case SCI_REQ_STP_PIO_DATA_OUT:
Dan Williams5076a1a2011-06-27 14:57:03 -07002429 return pio_data_out_tx_done_tc_event(ireq, completion_code);
Edmund Nadolskie3013702011-06-02 00:10:43 +00002430
Edmund Nadolskie3013702011-06-02 00:10:43 +00002431 case SCI_REQ_ABORTING:
Dan Williams5076a1a2011-06-27 14:57:03 -07002432 return request_aborting_state_tc_event(ireq,
Edmund Nadolskie3013702011-06-02 00:10:43 +00002433 completion_code);
2434
Dan Williamsb50102d2011-09-30 18:52:19 -07002435 case SCI_REQ_ATAPI_WAIT_H2D:
2436 return atapi_raw_completion(ireq, completion_code,
2437 SCI_REQ_ATAPI_WAIT_PIO_SETUP);
2438
2439 case SCI_REQ_ATAPI_WAIT_TC_COMP:
2440 return atapi_raw_completion(ireq, completion_code,
2441 SCI_REQ_ATAPI_WAIT_D2H);
2442
2443 case SCI_REQ_ATAPI_WAIT_D2H:
2444 return atapi_data_tc_completion_handler(ireq, completion_code);
2445
Edmund Nadolskie3013702011-06-02 00:10:43 +00002446 default:
Dan Williamsd7a0ccd2012-02-10 01:18:44 -08002447 dev_warn(&ihost->pdev->dev, "%s: %x in wrong state %s\n",
2448 __func__, completion_code, req_state_name(state));
Edmund Nadolskie3013702011-06-02 00:10:43 +00002449 return SCI_FAILURE_INVALID_STATE;
Dan Williamsa7e255a2011-05-11 08:27:47 -07002450 }
2451}
2452
Dan Williams6f231dd2011-07-02 22:56:22 -07002453/**
2454 * isci_request_process_response_iu() - This function sets the status and
2455 * response iu, in the task struct, from the request object for the upper
2456 * layer driver.
2457 * @sas_task: This parameter is the task struct from the upper layer driver.
2458 * @resp_iu: This parameter points to the response iu of the completed request.
2459 * @dev: This parameter specifies the linux device struct.
2460 *
2461 * none.
2462 */
2463static void isci_request_process_response_iu(
2464 struct sas_task *task,
2465 struct ssp_response_iu *resp_iu,
2466 struct device *dev)
2467{
2468 dev_dbg(dev,
2469 "%s: resp_iu = %p "
2470 "resp_iu->status = 0x%x,\nresp_iu->datapres = %d "
2471 "resp_iu->response_data_len = %x, "
2472 "resp_iu->sense_data_len = %x\nrepsonse data: ",
2473 __func__,
2474 resp_iu,
2475 resp_iu->status,
2476 resp_iu->datapres,
2477 resp_iu->response_data_len,
2478 resp_iu->sense_data_len);
2479
2480 task->task_status.stat = resp_iu->status;
2481
2482 /* libsas updates the task status fields based on the response iu. */
2483 sas_ssp_task_response(dev, task, resp_iu);
2484}
2485
2486/**
2487 * isci_request_set_open_reject_status() - This function prepares the I/O
2488 * completion for OPEN_REJECT conditions.
2489 * @request: This parameter is the completed isci_request object.
2490 * @response_ptr: This parameter specifies the service response for the I/O.
2491 * @status_ptr: This parameter specifies the exec status for the I/O.
2492 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2493 * the LLDD with respect to completing this request or forcing an abort
2494 * condition on the I/O.
2495 * @open_rej_reason: This parameter specifies the encoded reason for the
2496 * abandon-class reject.
2497 *
2498 * none.
2499 */
2500static void isci_request_set_open_reject_status(
2501 struct isci_request *request,
2502 struct sas_task *task,
2503 enum service_response *response_ptr,
2504 enum exec_status *status_ptr,
2505 enum isci_completion_selection *complete_to_host_ptr,
2506 enum sas_open_rej_reason open_rej_reason)
2507{
2508 /* Task in the target is done. */
Dan Williams38d88792011-06-23 14:33:48 -07002509 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002510 *response_ptr = SAS_TASK_UNDELIVERED;
2511 *status_ptr = SAS_OPEN_REJECT;
2512 *complete_to_host_ptr = isci_perform_normal_io_completion;
2513 task->task_status.open_rej_reason = open_rej_reason;
2514}
2515
2516/**
2517 * isci_request_handle_controller_specific_errors() - This function decodes
2518 * controller-specific I/O completion error conditions.
2519 * @request: This parameter is the completed isci_request object.
2520 * @response_ptr: This parameter specifies the service response for the I/O.
2521 * @status_ptr: This parameter specifies the exec status for the I/O.
2522 * @complete_to_host_ptr: This parameter specifies the action to be taken by
2523 * the LLDD with respect to completing this request or forcing an abort
2524 * condition on the I/O.
2525 *
2526 * none.
2527 */
2528static void isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07002529 struct isci_remote_device *idev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002530 struct isci_request *request,
2531 struct sas_task *task,
2532 enum service_response *response_ptr,
2533 enum exec_status *status_ptr,
2534 enum isci_completion_selection *complete_to_host_ptr)
2535{
2536 unsigned int cstatus;
2537
Dan Williams5076a1a2011-06-27 14:57:03 -07002538 cstatus = request->scu_status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002539
2540 dev_dbg(&request->isci_host->pdev->dev,
2541 "%s: %p SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR "
2542 "- controller status = 0x%x\n",
2543 __func__, request, cstatus);
2544
2545 /* Decode the controller-specific errors; most
2546 * important is to recognize those conditions in which
2547 * the target may still have a task outstanding that
2548 * must be aborted.
2549 *
2550 * Note that there are SCU completion codes being
2551 * named in the decode below for which SCIC has already
2552 * done work to handle them in a way other than as
2553 * a controller-specific completion code; these are left
2554 * in the decode below for completeness sake.
2555 */
2556 switch (cstatus) {
2557 case SCU_TASK_DONE_DMASETUP_DIRERR:
2558 /* Also SCU_TASK_DONE_SMP_FRM_TYPE_ERR: */
2559 case SCU_TASK_DONE_XFERCNT_ERR:
2560 /* Also SCU_TASK_DONE_SMP_UFI_ERR: */
2561 if (task->task_proto == SAS_PROTOCOL_SMP) {
2562 /* SCU_TASK_DONE_SMP_UFI_ERR == Task Done. */
2563 *response_ptr = SAS_TASK_COMPLETE;
2564
2565 /* See if the device has been/is being stopped. Note
2566 * that we ignore the quiesce state, since we are
2567 * concerned about the actual device state.
2568 */
Dan Williams209fae12011-06-13 17:39:44 -07002569 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002570 *status_ptr = SAS_DEVICE_UNKNOWN;
2571 else
2572 *status_ptr = SAS_ABORTED_TASK;
2573
Dan Williams38d88792011-06-23 14:33:48 -07002574 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002575
2576 *complete_to_host_ptr =
2577 isci_perform_normal_io_completion;
2578 } else {
2579 /* Task in the target is not done. */
2580 *response_ptr = SAS_TASK_UNDELIVERED;
2581
Dan Williams209fae12011-06-13 17:39:44 -07002582 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002583 *status_ptr = SAS_DEVICE_UNKNOWN;
2584 else
2585 *status_ptr = SAM_STAT_TASK_ABORTED;
2586
Dan Williams38d88792011-06-23 14:33:48 -07002587 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002588
2589 *complete_to_host_ptr =
2590 isci_perform_error_io_completion;
2591 }
2592
2593 break;
2594
2595 case SCU_TASK_DONE_CRC_ERR:
2596 case SCU_TASK_DONE_NAK_CMD_ERR:
2597 case SCU_TASK_DONE_EXCESS_DATA:
2598 case SCU_TASK_DONE_UNEXP_FIS:
2599 /* Also SCU_TASK_DONE_UNEXP_RESP: */
2600 case SCU_TASK_DONE_VIIT_ENTRY_NV: /* TODO - conditions? */
2601 case SCU_TASK_DONE_IIT_ENTRY_NV: /* TODO - conditions? */
2602 case SCU_TASK_DONE_RNCNV_OUTBOUND: /* TODO - conditions? */
2603 /* These are conditions in which the target
2604 * has completed the task, so that no cleanup
2605 * is necessary.
2606 */
2607 *response_ptr = SAS_TASK_COMPLETE;
2608
2609 /* See if the device has been/is being stopped. Note
2610 * that we ignore the quiesce state, since we are
2611 * concerned about the actual device state.
2612 */
Dan Williams209fae12011-06-13 17:39:44 -07002613 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002614 *status_ptr = SAS_DEVICE_UNKNOWN;
2615 else
2616 *status_ptr = SAS_ABORTED_TASK;
2617
Dan Williams38d88792011-06-23 14:33:48 -07002618 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002619
2620 *complete_to_host_ptr = isci_perform_normal_io_completion;
2621 break;
2622
2623
2624 /* Note that the only open reject completion codes seen here will be
2625 * abandon-class codes; all others are automatically retried in the SCU.
2626 */
2627 case SCU_TASK_OPEN_REJECT_WRONG_DESTINATION:
2628
2629 isci_request_set_open_reject_status(
2630 request, task, response_ptr, status_ptr,
2631 complete_to_host_ptr, SAS_OREJ_WRONG_DEST);
2632 break;
2633
2634 case SCU_TASK_OPEN_REJECT_ZONE_VIOLATION:
2635
2636 /* Note - the return of AB0 will change when
2637 * libsas implements detection of zone violations.
2638 */
2639 isci_request_set_open_reject_status(
2640 request, task, response_ptr, status_ptr,
2641 complete_to_host_ptr, SAS_OREJ_RESV_AB0);
2642 break;
2643
2644 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1:
2645
2646 isci_request_set_open_reject_status(
2647 request, task, response_ptr, status_ptr,
2648 complete_to_host_ptr, SAS_OREJ_RESV_AB1);
2649 break;
2650
2651 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2:
2652
2653 isci_request_set_open_reject_status(
2654 request, task, response_ptr, status_ptr,
2655 complete_to_host_ptr, SAS_OREJ_RESV_AB2);
2656 break;
2657
2658 case SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3:
2659
2660 isci_request_set_open_reject_status(
2661 request, task, response_ptr, status_ptr,
2662 complete_to_host_ptr, SAS_OREJ_RESV_AB3);
2663 break;
2664
2665 case SCU_TASK_OPEN_REJECT_BAD_DESTINATION:
2666
2667 isci_request_set_open_reject_status(
2668 request, task, response_ptr, status_ptr,
2669 complete_to_host_ptr, SAS_OREJ_BAD_DEST);
2670 break;
2671
2672 case SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY:
2673
2674 isci_request_set_open_reject_status(
2675 request, task, response_ptr, status_ptr,
2676 complete_to_host_ptr, SAS_OREJ_STP_NORES);
2677 break;
2678
2679 case SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED:
2680
2681 isci_request_set_open_reject_status(
2682 request, task, response_ptr, status_ptr,
2683 complete_to_host_ptr, SAS_OREJ_EPROTO);
2684 break;
2685
2686 case SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED:
2687
2688 isci_request_set_open_reject_status(
2689 request, task, response_ptr, status_ptr,
2690 complete_to_host_ptr, SAS_OREJ_CONN_RATE);
2691 break;
2692
2693 case SCU_TASK_DONE_LL_R_ERR:
2694 /* Also SCU_TASK_DONE_ACK_NAK_TO: */
2695 case SCU_TASK_DONE_LL_PERR:
2696 case SCU_TASK_DONE_LL_SY_TERM:
2697 /* Also SCU_TASK_DONE_NAK_ERR:*/
2698 case SCU_TASK_DONE_LL_LF_TERM:
2699 /* Also SCU_TASK_DONE_DATA_LEN_ERR: */
2700 case SCU_TASK_DONE_LL_ABORT_ERR:
2701 case SCU_TASK_DONE_SEQ_INV_TYPE:
2702 /* Also SCU_TASK_DONE_UNEXP_XR: */
2703 case SCU_TASK_DONE_XR_IU_LEN_ERR:
2704 case SCU_TASK_DONE_INV_FIS_LEN:
2705 /* Also SCU_TASK_DONE_XR_WD_LEN: */
2706 case SCU_TASK_DONE_SDMA_ERR:
2707 case SCU_TASK_DONE_OFFSET_ERR:
2708 case SCU_TASK_DONE_MAX_PLD_ERR:
2709 case SCU_TASK_DONE_LF_ERR:
2710 case SCU_TASK_DONE_SMP_RESP_TO_ERR: /* Escalate to dev reset? */
2711 case SCU_TASK_DONE_SMP_LL_RX_ERR:
2712 case SCU_TASK_DONE_UNEXP_DATA:
2713 case SCU_TASK_DONE_UNEXP_SDBFIS:
2714 case SCU_TASK_DONE_REG_ERR:
2715 case SCU_TASK_DONE_SDB_ERR:
2716 case SCU_TASK_DONE_TASK_ABORT:
2717 default:
2718 /* Task in the target is not done. */
2719 *response_ptr = SAS_TASK_UNDELIVERED;
2720 *status_ptr = SAM_STAT_TASK_ABORTED;
Dan Williams6f231dd2011-07-02 22:56:22 -07002721
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002722 if (task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williams38d88792011-06-23 14:33:48 -07002723 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002724
2725 *complete_to_host_ptr = isci_perform_normal_io_completion;
2726 } else {
Dan Williams38d88792011-06-23 14:33:48 -07002727 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07002728
2729 *complete_to_host_ptr = isci_perform_error_io_completion;
2730 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002731 break;
2732 }
2733}
2734
2735/**
2736 * isci_task_save_for_upper_layer_completion() - This function saves the
2737 * request for later completion to the upper layer driver.
2738 * @host: This parameter is a pointer to the host on which the the request
2739 * should be queued (either as an error or success).
2740 * @request: This parameter is the completed request.
2741 * @response: This parameter is the response code for the completed task.
2742 * @status: This parameter is the status code for the completed task.
2743 *
2744 * none.
2745 */
2746static void isci_task_save_for_upper_layer_completion(
2747 struct isci_host *host,
2748 struct isci_request *request,
2749 enum service_response response,
2750 enum exec_status status,
2751 enum isci_completion_selection task_notification_selection)
2752{
2753 struct sas_task *task = isci_request_access_task(request);
2754
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002755 task_notification_selection
2756 = isci_task_set_completion_status(task, response, status,
2757 task_notification_selection);
Dan Williams6f231dd2011-07-02 22:56:22 -07002758
2759 /* Tasks aborted specifically by a call to the lldd_abort_task
2760 * function should not be completed to the host in the regular path.
2761 */
2762 switch (task_notification_selection) {
2763
2764 case isci_perform_normal_io_completion:
Dan Williams6f231dd2011-07-02 22:56:22 -07002765 /* Normal notification (task_done) */
Jeff Skirvin3b34c162011-10-27 15:05:22 -07002766
Dan Williams6f231dd2011-07-02 22:56:22 -07002767 /* Add to the completed list. */
2768 list_add(&request->completed_node,
2769 &host->requests_to_complete);
Jeff Skirvinec6c9632011-03-04 14:06:44 -08002770
2771 /* Take the request off the device's pending request list. */
2772 list_del_init(&request->dev_node);
Dan Williams6f231dd2011-07-02 22:56:22 -07002773 break;
2774
2775 case isci_perform_aborted_io_completion:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002776 /* No notification to libsas because this request is
2777 * already in the abort path.
Dan Williams6f231dd2011-07-02 22:56:22 -07002778 */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002779 /* Wake up whatever process was waiting for this
2780 * request to complete.
2781 */
2782 WARN_ON(request->io_request_completion == NULL);
2783
2784 if (request->io_request_completion != NULL) {
2785
2786 /* Signal whoever is waiting that this
2787 * request is complete.
2788 */
2789 complete(request->io_request_completion);
2790 }
Dan Williams6f231dd2011-07-02 22:56:22 -07002791 break;
2792
2793 case isci_perform_error_io_completion:
2794 /* Use sas_task_abort */
Dan Williams6f231dd2011-07-02 22:56:22 -07002795 /* Add to the aborted list. */
2796 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002797 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002798 break;
2799
2800 default:
Jeff Skirvina5fde222011-03-04 14:06:42 -08002801 /* Add to the error to libsas list. */
Dan Williams6f231dd2011-07-02 22:56:22 -07002802 list_add(&request->completed_node,
Jeff Skirvin11b00c12011-03-04 14:06:40 -08002803 &host->requests_to_errorback);
Dan Williams6f231dd2011-07-02 22:56:22 -07002804 break;
2805 }
Jeff Skirvin3b34c162011-10-27 15:05:22 -07002806 dev_dbg(&host->pdev->dev,
2807 "%s: %d - task = %p, response=%d (%d), status=%d (%d)\n",
2808 __func__, task_notification_selection, task,
2809 (task) ? task->task_status.resp : 0, response,
2810 (task) ? task->task_status.stat : 0, status);
Dan Williams6f231dd2011-07-02 22:56:22 -07002811}
2812
Dan Williams1a878282011-07-29 17:16:40 -07002813static void isci_process_stp_response(struct sas_task *task, struct dev_to_host_fis *fis)
Dan Williams16ba7702011-07-01 10:52:55 -07002814{
Dan Williams16ba7702011-07-01 10:52:55 -07002815 struct task_status_struct *ts = &task->task_status;
2816 struct ata_task_resp *resp = (void *)&ts->buf[0];
2817
Dan Williams1a878282011-07-29 17:16:40 -07002818 resp->frame_len = sizeof(*fis);
2819 memcpy(resp->ending_fis, fis, sizeof(*fis));
Dan Williams16ba7702011-07-01 10:52:55 -07002820 ts->buf_valid_size = sizeof(*resp);
2821
Dan Williams1a878282011-07-29 17:16:40 -07002822 /* If the device fault bit is set in the status register, then
Dan Williams16ba7702011-07-01 10:52:55 -07002823 * set the sense data and return.
2824 */
Dan Williams1a878282011-07-29 17:16:40 -07002825 if (fis->status & ATA_DF)
Dan Williams16ba7702011-07-01 10:52:55 -07002826 ts->stat = SAS_PROTO_RESPONSE;
Dan Williamsb50102d2011-09-30 18:52:19 -07002827 else if (fis->status & ATA_ERR)
2828 ts->stat = SAM_STAT_CHECK_CONDITION;
Dan Williams16ba7702011-07-01 10:52:55 -07002829 else
2830 ts->stat = SAM_STAT_GOOD;
2831
2832 ts->resp = SAS_TASK_COMPLETE;
2833}
2834
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002835static void isci_request_io_request_complete(struct isci_host *ihost,
Dan Williamsf1f52e72011-05-10 02:28:45 -07002836 struct isci_request *request,
2837 enum sci_io_status completion_status)
Dan Williams6f231dd2011-07-02 22:56:22 -07002838{
2839 struct sas_task *task = isci_request_access_task(request);
2840 struct ssp_response_iu *resp_iu;
Dan Williams6f231dd2011-07-02 22:56:22 -07002841 unsigned long task_flags;
Jeff Skirvin0e2e2792011-10-27 15:04:50 -07002842 struct isci_remote_device *idev = request->target_device;
2843 enum service_response response = SAS_TASK_UNDELIVERED;
2844 enum exec_status status = SAS_ABORTED_TASK;
Dan Williams6f231dd2011-07-02 22:56:22 -07002845 enum isci_request_status request_status;
2846 enum isci_completion_selection complete_to_host
2847 = isci_perform_normal_io_completion;
2848
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002849 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002850 "%s: request = %p, task = %p,\n"
2851 "task->data_dir = %d completion_status = 0x%x\n",
2852 __func__,
2853 request,
2854 task,
2855 task->data_dir,
2856 completion_status);
2857
Jeff Skirvina5fde222011-03-04 14:06:42 -08002858 spin_lock(&request->state_lock);
Dan Williams34a99152011-07-01 02:25:15 -07002859 request_status = request->status;
Dan Williams6f231dd2011-07-02 22:56:22 -07002860
2861 /* Decode the request status. Note that if the request has been
2862 * aborted by a task management function, we don't care
2863 * what the status is.
2864 */
2865 switch (request_status) {
2866
2867 case aborted:
2868 /* "aborted" indicates that the request was aborted by a task
2869 * management function, since once a task management request is
2870 * perfomed by the device, the request only completes because
2871 * of the subsequent driver terminate.
2872 *
2873 * Aborted also means an external thread is explicitly managing
2874 * this request, so that we do not complete it up the stack.
2875 *
2876 * The target is still there (since the TMF was successful).
2877 */
Dan Williams38d88792011-06-23 14:33:48 -07002878 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002879 response = SAS_TASK_COMPLETE;
2880
2881 /* See if the device has been/is being stopped. Note
2882 * that we ignore the quiesce state, since we are
2883 * concerned about the actual device state.
2884 */
Dan Williams209fae12011-06-13 17:39:44 -07002885 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002886 status = SAS_DEVICE_UNKNOWN;
2887 else
2888 status = SAS_ABORTED_TASK;
2889
2890 complete_to_host = isci_perform_aborted_io_completion;
2891 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002892
2893 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002894 break;
2895
2896 case aborting:
2897 /* aborting means that the task management function tried and
2898 * failed to abort the request. We need to note the request
2899 * as SAS_TASK_UNDELIVERED, so that the scsi mid layer marks the
2900 * target as down.
2901 *
2902 * Aborting also means an external thread is explicitly managing
2903 * this request, so that we do not complete it up the stack.
2904 */
Dan Williams38d88792011-06-23 14:33:48 -07002905 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002906 response = SAS_TASK_UNDELIVERED;
2907
Dan Williams209fae12011-06-13 17:39:44 -07002908 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002909 /* The device has been /is being stopped. Note that
2910 * we ignore the quiesce state, since we are
2911 * concerned about the actual device state.
2912 */
2913 status = SAS_DEVICE_UNKNOWN;
2914 else
2915 status = SAS_PHY_DOWN;
2916
2917 complete_to_host = isci_perform_aborted_io_completion;
2918
2919 /* This was an aborted request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002920
2921 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002922 break;
2923
2924 case terminating:
2925
2926 /* This was an terminated request. This happens when
2927 * the I/O is being terminated because of an action on
2928 * the device (reset, tear down, etc.), and the I/O needs
2929 * to be completed up the stack.
2930 */
Dan Williams38d88792011-06-23 14:33:48 -07002931 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07002932 response = SAS_TASK_UNDELIVERED;
2933
2934 /* See if the device has been/is being stopped. Note
2935 * that we ignore the quiesce state, since we are
2936 * concerned about the actual device state.
2937 */
Dan Williams209fae12011-06-13 17:39:44 -07002938 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07002939 status = SAS_DEVICE_UNKNOWN;
2940 else
2941 status = SAS_ABORTED_TASK;
2942
Jeff Skirvina5fde222011-03-04 14:06:42 -08002943 complete_to_host = isci_perform_aborted_io_completion;
Dan Williams6f231dd2011-07-02 22:56:22 -07002944
2945 /* This was a terminated request. */
Jeff Skirvina5fde222011-03-04 14:06:42 -08002946
2947 spin_unlock(&request->state_lock);
Dan Williams6f231dd2011-07-02 22:56:22 -07002948 break;
2949
Jeff Skirvin77c852f2011-06-20 14:09:16 -07002950 case dead:
2951 /* This was a terminated request that timed-out during the
2952 * termination process. There is no task to complete to
2953 * libsas.
2954 */
2955 complete_to_host = isci_perform_normal_io_completion;
2956 spin_unlock(&request->state_lock);
2957 break;
2958
Dan Williams6f231dd2011-07-02 22:56:22 -07002959 default:
2960
Jeff Skirvina5fde222011-03-04 14:06:42 -08002961 /* The request is done from an SCU HW perspective. */
2962 request->status = completed;
2963
2964 spin_unlock(&request->state_lock);
2965
Dan Williams6f231dd2011-07-02 22:56:22 -07002966 /* This is an active request being completed from the core. */
2967 switch (completion_status) {
2968
2969 case SCI_IO_FAILURE_RESPONSE_VALID:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002970 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002971 "%s: SCI_IO_FAILURE_RESPONSE_VALID (%p/%p)\n",
2972 __func__,
2973 request,
2974 task);
2975
2976 if (sas_protocol_ata(task->task_proto)) {
Dan Williams1a878282011-07-29 17:16:40 -07002977 isci_process_stp_response(task, &request->stp.rsp);
Dan Williams6f231dd2011-07-02 22:56:22 -07002978 } else if (SAS_PROTOCOL_SSP == task->task_proto) {
2979
2980 /* crack the iu response buffer. */
Dan Williams5076a1a2011-06-27 14:57:03 -07002981 resp_iu = &request->ssp.rsp;
Dan Williams6f231dd2011-07-02 22:56:22 -07002982 isci_request_process_response_iu(task, resp_iu,
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002983 &ihost->pdev->dev);
Dan Williams6f231dd2011-07-02 22:56:22 -07002984
2985 } else if (SAS_PROTOCOL_SMP == task->task_proto) {
2986
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002987 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002988 "%s: SCI_IO_FAILURE_RESPONSE_VALID: "
2989 "SAS_PROTOCOL_SMP protocol\n",
2990 __func__);
2991
2992 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07002993 dev_err(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07002994 "%s: unknown protocol\n", __func__);
2995
2996 /* use the task status set in the task struct by the
2997 * isci_request_process_response_iu call.
2998 */
Dan Williams38d88792011-06-23 14:33:48 -07002999 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003000 response = task->task_status.resp;
3001 status = task->task_status.stat;
3002 break;
3003
3004 case SCI_IO_SUCCESS:
3005 case SCI_IO_SUCCESS_IO_DONE_EARLY:
3006
3007 response = SAS_TASK_COMPLETE;
3008 status = SAM_STAT_GOOD;
Dan Williams38d88792011-06-23 14:33:48 -07003009 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003010
Dan Williams54b5e3a2011-09-28 18:35:27 -07003011 if (completion_status == SCI_IO_SUCCESS_IO_DONE_EARLY) {
Dan Williams6f231dd2011-07-02 22:56:22 -07003012
3013 /* This was an SSP / STP / SATA transfer.
3014 * There is a possibility that less data than
3015 * the maximum was transferred.
3016 */
Dan Williams5076a1a2011-06-27 14:57:03 -07003017 u32 transferred_length = sci_req_tx_bytes(request);
Dan Williams6f231dd2011-07-02 22:56:22 -07003018
3019 task->task_status.residual
3020 = task->total_xfer_len - transferred_length;
3021
3022 /* If there were residual bytes, call this an
3023 * underrun.
3024 */
3025 if (task->task_status.residual != 0)
3026 status = SAS_DATA_UNDERRUN;
3027
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003028 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003029 "%s: SCI_IO_SUCCESS_IO_DONE_EARLY %d\n",
3030 __func__,
3031 status);
3032
3033 } else
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003034 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003035 "%s: SCI_IO_SUCCESS\n",
3036 __func__);
3037
3038 break;
3039
3040 case SCI_IO_FAILURE_TERMINATED:
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003041 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003042 "%s: SCI_IO_FAILURE_TERMINATED (%p/%p)\n",
3043 __func__,
3044 request,
3045 task);
3046
3047 /* The request was terminated explicitly. No handling
3048 * is needed in the SCSI error handler path.
3049 */
Dan Williams38d88792011-06-23 14:33:48 -07003050 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003051 response = SAS_TASK_UNDELIVERED;
3052
3053 /* See if the device has been/is being stopped. Note
3054 * that we ignore the quiesce state, since we are
3055 * concerned about the actual device state.
3056 */
Dan Williams209fae12011-06-13 17:39:44 -07003057 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07003058 status = SAS_DEVICE_UNKNOWN;
3059 else
3060 status = SAS_ABORTED_TASK;
3061
3062 complete_to_host = isci_perform_normal_io_completion;
3063 break;
3064
3065 case SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR:
3066
3067 isci_request_handle_controller_specific_errors(
Dan Williams209fae12011-06-13 17:39:44 -07003068 idev, request, task, &response, &status,
Dan Williams6f231dd2011-07-02 22:56:22 -07003069 &complete_to_host);
3070
3071 break;
3072
3073 case SCI_IO_FAILURE_REMOTE_DEVICE_RESET_REQUIRED:
3074 /* This is a special case, in that the I/O completion
3075 * is telling us that the device needs a reset.
3076 * In order for the device reset condition to be
3077 * noticed, the I/O has to be handled in the error
3078 * handler. Set the reset flag and cause the
3079 * SCSI error thread to be scheduled.
3080 */
3081 spin_lock_irqsave(&task->task_state_lock, task_flags);
3082 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3083 spin_unlock_irqrestore(&task->task_state_lock, task_flags);
3084
Jeff Skirvinaa145102011-03-07 16:40:47 -07003085 /* Fail the I/O. */
3086 response = SAS_TASK_UNDELIVERED;
3087 status = SAM_STAT_TASK_ABORTED;
3088
Dan Williams6f231dd2011-07-02 22:56:22 -07003089 complete_to_host = isci_perform_error_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07003090 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003091 break;
3092
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003093 case SCI_FAILURE_RETRY_REQUIRED:
3094
3095 /* Fail the I/O so it can be retried. */
3096 response = SAS_TASK_UNDELIVERED;
Dan Williams209fae12011-06-13 17:39:44 -07003097 if (!idev)
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003098 status = SAS_DEVICE_UNKNOWN;
3099 else
3100 status = SAS_ABORTED_TASK;
3101
3102 complete_to_host = isci_perform_normal_io_completion;
Dan Williams38d88792011-06-23 14:33:48 -07003103 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003104 break;
3105
3106
Dan Williams6f231dd2011-07-02 22:56:22 -07003107 default:
3108 /* Catch any otherwise unhandled error codes here. */
Dan Williamsa8a0a132011-07-01 12:07:25 -07003109 dev_dbg(&ihost->pdev->dev,
Dan Williams6f231dd2011-07-02 22:56:22 -07003110 "%s: invalid completion code: 0x%x - "
3111 "isci_request = %p\n",
3112 __func__, completion_status, request);
3113
3114 response = SAS_TASK_UNDELIVERED;
3115
3116 /* See if the device has been/is being stopped. Note
3117 * that we ignore the quiesce state, since we are
3118 * concerned about the actual device state.
3119 */
Dan Williams209fae12011-06-13 17:39:44 -07003120 if (!idev)
Dan Williams6f231dd2011-07-02 22:56:22 -07003121 status = SAS_DEVICE_UNKNOWN;
3122 else
3123 status = SAS_ABORTED_TASK;
3124
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003125 if (SAS_PROTOCOL_SMP == task->task_proto) {
Dan Williams38d88792011-06-23 14:33:48 -07003126 set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003127 complete_to_host = isci_perform_normal_io_completion;
3128 } else {
Dan Williams38d88792011-06-23 14:33:48 -07003129 clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
Jeff Skirvincde76fb2011-06-20 14:09:06 -07003130 complete_to_host = isci_perform_error_io_completion;
3131 }
Dan Williams6f231dd2011-07-02 22:56:22 -07003132 break;
3133 }
3134 break;
3135 }
3136
Dan Williamsddcc7e32011-06-17 10:40:43 -07003137 switch (task->task_proto) {
3138 case SAS_PROTOCOL_SSP:
3139 if (task->data_dir == DMA_NONE)
3140 break;
3141 if (task->num_scatter == 0)
3142 /* 0 indicates a single dma address */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003143 dma_unmap_single(&ihost->pdev->dev,
Dan Williamsddcc7e32011-06-17 10:40:43 -07003144 request->zero_scatter_daddr,
3145 task->total_xfer_len, task->data_dir);
3146 else /* unmap the sgl dma addresses */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003147 dma_unmap_sg(&ihost->pdev->dev, task->scatter,
Dan Williamsddcc7e32011-06-17 10:40:43 -07003148 request->num_sg_entries, task->data_dir);
3149 break;
Dan Williamse9bf7092011-06-16 16:59:56 -07003150 case SAS_PROTOCOL_SMP: {
3151 struct scatterlist *sg = &task->smp_task.smp_req;
3152 struct smp_req *smp_req;
3153 void *kaddr;
3154
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003155 dma_unmap_sg(&ihost->pdev->dev, sg, 1, DMA_TO_DEVICE);
Dan Williamse9bf7092011-06-16 16:59:56 -07003156
3157 /* need to swab it back in case the command buffer is re-used */
Cong Wang77dfce02011-11-25 23:14:23 +08003158 kaddr = kmap_atomic(sg_page(sg));
Dan Williamse9bf7092011-06-16 16:59:56 -07003159 smp_req = kaddr + sg->offset;
3160 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
Cong Wang77dfce02011-11-25 23:14:23 +08003161 kunmap_atomic(kaddr);
Dan Williamse9bf7092011-06-16 16:59:56 -07003162 break;
3163 }
Dan Williamsddcc7e32011-06-17 10:40:43 -07003164 default:
3165 break;
3166 }
Dan Williams6f231dd2011-07-02 22:56:22 -07003167
3168 /* Put the completed request on the correct list */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003169 isci_task_save_for_upper_layer_completion(ihost, request, response,
Dan Williams6f231dd2011-07-02 22:56:22 -07003170 status, complete_to_host
3171 );
3172
3173 /* complete the io request to the core. */
Dan Williams89a73012011-06-30 19:14:33 -07003174 sci_controller_complete_io(ihost, request->target_device, request);
Dan Williams209fae12011-06-13 17:39:44 -07003175
Dan Williams67ea8382011-05-08 11:47:15 -07003176 /* set terminated handle so it cannot be completed or
Dan Williams6f231dd2011-07-02 22:56:22 -07003177 * terminated again, and to cause any calls into abort
3178 * task to recognize the already completed case.
3179 */
Dan Williams38d88792011-06-23 14:33:48 -07003180 set_bit(IREQ_TERMINATED, &request->flags);
Dan Williams6f231dd2011-07-02 22:56:22 -07003181}
Dan Williamsf1f52e72011-05-10 02:28:45 -07003182
Dan Williams89a73012011-06-30 19:14:33 -07003183static void sci_request_started_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003184{
Dan Williams5076a1a2011-06-27 14:57:03 -07003185 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams78a6f062011-06-30 16:31:37 -07003186 struct domain_device *dev = ireq->target_device->domain_dev;
Dan Williamsb50102d2011-09-30 18:52:19 -07003187 enum sci_base_request_states state;
Dan Williamsc72086e2011-05-10 02:28:48 -07003188 struct sas_task *task;
3189
3190 /* XXX as hch said always creating an internal sas_task for tmf
3191 * requests would simplify the driver
3192 */
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003193 task = (test_bit(IREQ_TMF, &ireq->flags)) ? NULL : isci_request_access_task(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003194
Dan Williams5dec6f42011-05-10 02:28:49 -07003195 /* all unaccelerated request types (non ssp or ncq) handled with
3196 * substates
Dan Williamsf1393032011-05-10 02:28:47 -07003197 */
Dan Williamsc72086e2011-05-10 02:28:48 -07003198 if (!task && dev->dev_type == SAS_END_DEV) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003199 state = SCI_REQ_TASK_WAIT_TC_COMP;
Dan Williamsc72086e2011-05-10 02:28:48 -07003200 } else if (task && task->task_proto == SAS_PROTOCOL_SMP) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003201 state = SCI_REQ_SMP_WAIT_RESP;
Dan Williams5dec6f42011-05-10 02:28:49 -07003202 } else if (task && sas_protocol_ata(task->task_proto) &&
3203 !task->ata_task.use_ncq) {
Dan Williamsb50102d2011-09-30 18:52:19 -07003204 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET &&
3205 task->ata_task.fis.command == ATA_CMD_PACKET) {
3206 state = SCI_REQ_ATAPI_WAIT_H2D;
3207 } else if (task->data_dir == DMA_NONE) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003208 state = SCI_REQ_STP_NON_DATA_WAIT_H2D;
Dan Williamsb50102d2011-09-30 18:52:19 -07003209 } else if (task->ata_task.dma_xfer) {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003210 state = SCI_REQ_STP_UDMA_WAIT_TC_COMP;
Dan Williamsb50102d2011-09-30 18:52:19 -07003211 } else /* PIO */ {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003212 state = SCI_REQ_STP_PIO_WAIT_H2D;
Dan Williamsb50102d2011-09-30 18:52:19 -07003213 }
3214 } else {
3215 /* SSP or NCQ are fully accelerated, no substates */
3216 return;
Dan Williamsc72086e2011-05-10 02:28:48 -07003217 }
Dan Williamsb50102d2011-09-30 18:52:19 -07003218 sci_change_state(sm, state);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003219}
3220
Dan Williams89a73012011-06-30 19:14:33 -07003221static void sci_request_completed_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003222{
Dan Williams5076a1a2011-06-27 14:57:03 -07003223 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003224 struct isci_host *ihost = ireq->owning_controller;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003225
Dan Williamsf1f52e72011-05-10 02:28:45 -07003226 /* Tell the SCI_USER that the IO request is complete */
Dan Williams38d88792011-06-23 14:33:48 -07003227 if (!test_bit(IREQ_TMF, &ireq->flags))
Dan Williamsf1f52e72011-05-10 02:28:45 -07003228 isci_request_io_request_complete(ihost, ireq,
Dan Williams5076a1a2011-06-27 14:57:03 -07003229 ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003230 else
Dan Williams5076a1a2011-06-27 14:57:03 -07003231 isci_task_request_complete(ihost, ireq, ireq->sci_status);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003232}
3233
Dan Williams89a73012011-06-30 19:14:33 -07003234static void sci_request_aborting_state_enter(struct sci_base_state_machine *sm)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003235{
Dan Williams5076a1a2011-06-27 14:57:03 -07003236 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003237
3238 /* Setting the abort bit in the Task Context is required by the silicon. */
Dan Williams5076a1a2011-06-27 14:57:03 -07003239 ireq->tc->abort = 1;
Dan Williamsc72086e2011-05-10 02:28:48 -07003240}
3241
Dan Williams89a73012011-06-30 19:14:33 -07003242static void sci_stp_request_started_non_data_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07003243{
Dan Williams5076a1a2011-06-27 14:57:03 -07003244 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07003245
Dan Williams34a99152011-07-01 02:25:15 -07003246 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07003247}
3248
Dan Williams89a73012011-06-30 19:14:33 -07003249static void sci_stp_request_started_pio_await_h2d_completion_enter(struct sci_base_state_machine *sm)
Dan Williams5dec6f42011-05-10 02:28:49 -07003250{
Dan Williams5076a1a2011-06-27 14:57:03 -07003251 struct isci_request *ireq = container_of(sm, typeof(*ireq), sm);
Dan Williams5dec6f42011-05-10 02:28:49 -07003252
Dan Williams34a99152011-07-01 02:25:15 -07003253 ireq->target_device->working_request = ireq;
Dan Williams5dec6f42011-05-10 02:28:49 -07003254}
3255
Dan Williams89a73012011-06-30 19:14:33 -07003256static const struct sci_base_state sci_request_state_table[] = {
Edmund Nadolskie3013702011-06-02 00:10:43 +00003257 [SCI_REQ_INIT] = { },
3258 [SCI_REQ_CONSTRUCTED] = { },
3259 [SCI_REQ_STARTED] = {
Dan Williams89a73012011-06-30 19:14:33 -07003260 .enter_state = sci_request_started_state_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003261 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003262 [SCI_REQ_STP_NON_DATA_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07003263 .enter_state = sci_stp_request_started_non_data_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003264 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003265 [SCI_REQ_STP_NON_DATA_WAIT_D2H] = { },
3266 [SCI_REQ_STP_PIO_WAIT_H2D] = {
Dan Williams89a73012011-06-30 19:14:33 -07003267 .enter_state = sci_stp_request_started_pio_await_h2d_completion_enter,
Dan Williams5dec6f42011-05-10 02:28:49 -07003268 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003269 [SCI_REQ_STP_PIO_WAIT_FRAME] = { },
3270 [SCI_REQ_STP_PIO_DATA_IN] = { },
3271 [SCI_REQ_STP_PIO_DATA_OUT] = { },
3272 [SCI_REQ_STP_UDMA_WAIT_TC_COMP] = { },
3273 [SCI_REQ_STP_UDMA_WAIT_D2H] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003274 [SCI_REQ_TASK_WAIT_TC_COMP] = { },
3275 [SCI_REQ_TASK_WAIT_TC_RESP] = { },
3276 [SCI_REQ_SMP_WAIT_RESP] = { },
3277 [SCI_REQ_SMP_WAIT_TC_COMP] = { },
Dan Williamsb50102d2011-09-30 18:52:19 -07003278 [SCI_REQ_ATAPI_WAIT_H2D] = { },
3279 [SCI_REQ_ATAPI_WAIT_PIO_SETUP] = { },
3280 [SCI_REQ_ATAPI_WAIT_D2H] = { },
3281 [SCI_REQ_ATAPI_WAIT_TC_COMP] = { },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003282 [SCI_REQ_COMPLETED] = {
Dan Williams89a73012011-06-30 19:14:33 -07003283 .enter_state = sci_request_completed_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003284 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003285 [SCI_REQ_ABORTING] = {
Dan Williams89a73012011-06-30 19:14:33 -07003286 .enter_state = sci_request_aborting_state_enter,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003287 },
Edmund Nadolskie3013702011-06-02 00:10:43 +00003288 [SCI_REQ_FINAL] = { },
Dan Williamsf1f52e72011-05-10 02:28:45 -07003289};
3290
Edmund Nadolskie3013702011-06-02 00:10:43 +00003291static void
Dan Williams89a73012011-06-30 19:14:33 -07003292sci_general_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003293 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003294 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003295{
Dan Williams89a73012011-06-30 19:14:33 -07003296 sci_init_sm(&ireq->sm, sci_request_state_table, SCI_REQ_INIT);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003297
Dan Williams78a6f062011-06-30 16:31:37 -07003298 ireq->target_device = idev;
Dan Williamsc79dd802012-02-01 00:44:14 -08003299 ireq->protocol = SAS_PROTOCOL_NONE;
Dan Williams5076a1a2011-06-27 14:57:03 -07003300 ireq->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003301
Dan Williams5076a1a2011-06-27 14:57:03 -07003302 ireq->sci_status = SCI_SUCCESS;
3303 ireq->scu_status = 0;
3304 ireq->post_context = 0xFFFFFFFF;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003305}
3306
3307static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07003308sci_io_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003309 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003310 struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003311{
Dan Williams78a6f062011-06-30 16:31:37 -07003312 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003313 enum sci_status status = SCI_SUCCESS;
3314
3315 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07003316 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003317
Dan Williams78a6f062011-06-30 16:31:37 -07003318 if (idev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003319 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
3320
3321 if (dev->dev_type == SAS_END_DEV)
Dan Williamsc72086e2011-05-10 02:28:48 -07003322 /* pass */;
Dan Williams11cc5182012-02-01 00:23:10 -08003323 else if (dev_is_sata(dev))
Dan Williams5076a1a2011-06-27 14:57:03 -07003324 memset(&ireq->stp.cmd, 0, sizeof(ireq->stp.cmd));
Dan Williamsc72086e2011-05-10 02:28:48 -07003325 else if (dev_is_expander(dev))
Dan Williamse9bf7092011-06-16 16:59:56 -07003326 /* pass */;
Dan Williamsc72086e2011-05-10 02:28:48 -07003327 else
3328 return SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003329
Dan Williams5076a1a2011-06-27 14:57:03 -07003330 memset(ireq->tc, 0, offsetof(struct scu_task_context, sgl_pair_ab));
Dan Williamsf1f52e72011-05-10 02:28:45 -07003331
3332 return status;
3333}
3334
Dan Williams89a73012011-06-30 19:14:33 -07003335enum sci_status sci_task_request_construct(struct isci_host *ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003336 struct isci_remote_device *idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003337 u16 io_tag, struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003338{
Dan Williams78a6f062011-06-30 16:31:37 -07003339 struct domain_device *dev = idev->domain_dev;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003340 enum sci_status status = SCI_SUCCESS;
3341
3342 /* Build the common part of the request */
Dan Williams89a73012011-06-30 19:14:33 -07003343 sci_general_request_construct(ihost, idev, ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003344
Dan Williams11cc5182012-02-01 00:23:10 -08003345 if (dev->dev_type == SAS_END_DEV || dev_is_sata(dev)) {
Dan Williams5076a1a2011-06-27 14:57:03 -07003346 set_bit(IREQ_TMF, &ireq->flags);
3347 memset(ireq->tc, 0, sizeof(struct scu_task_context));
Dan Williamsc72086e2011-05-10 02:28:48 -07003348 } else
3349 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003350
3351 return status;
3352}
3353
3354static enum sci_status isci_request_ssp_request_construct(
3355 struct isci_request *request)
3356{
3357 enum sci_status status;
3358
3359 dev_dbg(&request->isci_host->pdev->dev,
3360 "%s: request = %p\n",
3361 __func__,
3362 request);
Dan Williams89a73012011-06-30 19:14:33 -07003363 status = sci_io_request_construct_basic_ssp(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003364 return status;
3365}
3366
Dan Williams16ba7702011-07-01 10:52:55 -07003367static enum sci_status isci_request_stp_request_construct(struct isci_request *ireq)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003368{
Dan Williams16ba7702011-07-01 10:52:55 -07003369 struct sas_task *task = isci_request_access_task(ireq);
3370 struct host_to_dev_fis *fis = &ireq->stp.cmd;
3371 struct ata_queued_cmd *qc = task->uldd_task;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003372 enum sci_status status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003373
Dan Williams16ba7702011-07-01 10:52:55 -07003374 dev_dbg(&ireq->isci_host->pdev->dev,
3375 "%s: ireq = %p\n",
Dan Williamsf1f52e72011-05-10 02:28:45 -07003376 __func__,
Dan Williams16ba7702011-07-01 10:52:55 -07003377 ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003378
Dan Williams16ba7702011-07-01 10:52:55 -07003379 memcpy(fis, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
3380 if (!task->ata_task.device_control_reg_update)
3381 fis->flags |= 0x80;
3382 fis->flags &= 0xF0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003383
Dan Williams16ba7702011-07-01 10:52:55 -07003384 status = sci_io_request_construct_basic_sata(ireq);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003385
Dan Williams16ba7702011-07-01 10:52:55 -07003386 if (qc && (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
3387 qc->tf.command == ATA_CMD_FPDMA_READ)) {
3388 fis->sector_count = qc->tag << 3;
3389 ireq->tc->type.stp.ncq_tag = qc->tag;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003390 }
3391
3392 return status;
3393}
3394
Dan Williamse9bf7092011-06-16 16:59:56 -07003395static enum sci_status
Dan Williams89a73012011-06-30 19:14:33 -07003396sci_io_request_construct_smp(struct device *dev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003397 struct isci_request *ireq,
Dan Williamse9bf7092011-06-16 16:59:56 -07003398 struct sas_task *task)
Dan Williamsc72086e2011-05-10 02:28:48 -07003399{
Dan Williamse9bf7092011-06-16 16:59:56 -07003400 struct scatterlist *sg = &task->smp_task.smp_req;
Dan Williams78a6f062011-06-30 16:31:37 -07003401 struct isci_remote_device *idev;
Dan Williamsc72086e2011-05-10 02:28:48 -07003402 struct scu_task_context *task_context;
Dan Williamsffe191c2011-06-29 13:09:25 -07003403 struct isci_port *iport;
Dan Williamse9bf7092011-06-16 16:59:56 -07003404 struct smp_req *smp_req;
3405 void *kaddr;
3406 u8 req_len;
3407 u32 cmd;
3408
Cong Wang77dfce02011-11-25 23:14:23 +08003409 kaddr = kmap_atomic(sg_page(sg));
Dan Williamse9bf7092011-06-16 16:59:56 -07003410 smp_req = kaddr + sg->offset;
3411 /*
3412 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
3413 * functions under SAS 2.0, a zero request length really indicates
3414 * a non-zero default length.
3415 */
3416 if (smp_req->req_len == 0) {
3417 switch (smp_req->func) {
3418 case SMP_DISCOVER:
3419 case SMP_REPORT_PHY_ERR_LOG:
3420 case SMP_REPORT_PHY_SATA:
3421 case SMP_REPORT_ROUTE_INFO:
3422 smp_req->req_len = 2;
3423 break;
3424 case SMP_CONF_ROUTE_INFO:
3425 case SMP_PHY_CONTROL:
3426 case SMP_PHY_TEST_FUNCTION:
3427 smp_req->req_len = 9;
3428 break;
3429 /* Default - zero is a valid default for 2.0. */
3430 }
3431 }
3432 req_len = smp_req->req_len;
3433 sci_swab32_cpy(smp_req, smp_req, sg->length / sizeof(u32));
3434 cmd = *(u32 *) smp_req;
Cong Wang77dfce02011-11-25 23:14:23 +08003435 kunmap_atomic(kaddr);
Dan Williamse9bf7092011-06-16 16:59:56 -07003436
3437 if (!dma_map_sg(dev, sg, 1, DMA_TO_DEVICE))
3438 return SCI_FAILURE;
3439
Dan Williamsc79dd802012-02-01 00:44:14 -08003440 ireq->protocol = SAS_PROTOCOL_SMP;
Dan Williamsc72086e2011-05-10 02:28:48 -07003441
3442 /* byte swap the smp request. */
Dan Williamsc72086e2011-05-10 02:28:48 -07003443
Dan Williams5076a1a2011-06-27 14:57:03 -07003444 task_context = ireq->tc;
Dan Williamsc72086e2011-05-10 02:28:48 -07003445
Dan Williams34a99152011-07-01 02:25:15 -07003446 idev = ireq->target_device;
3447 iport = idev->owning_port;
Dan Williamsc72086e2011-05-10 02:28:48 -07003448
3449 /*
3450 * Fill in the TC with the its required data
3451 * 00h
3452 */
3453 task_context->priority = 0;
3454 task_context->initiator_request = 1;
Dan Williams78a6f062011-06-30 16:31:37 -07003455 task_context->connection_rate = idev->connection_rate;
Dan Williams34a99152011-07-01 02:25:15 -07003456 task_context->protocol_engine_index = ISCI_PEG;
3457 task_context->logical_port_index = iport->physical_port_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003458 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
3459 task_context->abort = 0;
3460 task_context->valid = SCU_TASK_CONTEXT_VALID;
3461 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
3462
3463 /* 04h */
Dan Williams78a6f062011-06-30 16:31:37 -07003464 task_context->remote_node_index = idev->rnc.remote_node_index;
Dan Williamsc72086e2011-05-10 02:28:48 -07003465 task_context->command_code = 0;
3466 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
3467
3468 /* 08h */
3469 task_context->link_layer_control = 0;
3470 task_context->do_not_dma_ssp_good_response = 1;
3471 task_context->strict_ordering = 0;
3472 task_context->control_frame = 1;
3473 task_context->timeout_enable = 0;
3474 task_context->block_guard_enable = 0;
3475
3476 /* 0ch */
3477 task_context->address_modifier = 0;
3478
3479 /* 10h */
Dave Jiang77d67382011-05-25 02:21:57 +00003480 task_context->ssp_command_iu_length = req_len;
Dan Williamsc72086e2011-05-10 02:28:48 -07003481
3482 /* 14h */
3483 task_context->transfer_length_bytes = 0;
3484
3485 /*
3486 * 18h ~ 30h, protocol specific
3487 * since commandIU has been build by framework at this point, we just
3488 * copy the frist DWord from command IU to this location. */
Dan Williamse9bf7092011-06-16 16:59:56 -07003489 memcpy(&task_context->type.smp, &cmd, sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003490
3491 /*
3492 * 40h
3493 * "For SMP you could program it to zero. We would prefer that way
3494 * so that done code will be consistent." - Venki
3495 */
3496 task_context->task_phase = 0;
3497
Dan Williams5076a1a2011-06-27 14:57:03 -07003498 ireq->post_context = (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
Dan Williams34a99152011-07-01 02:25:15 -07003499 (ISCI_PEG << SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
3500 (iport->physical_port_index <<
3501 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
3502 ISCI_TAG_TCI(ireq->io_tag));
Dan Williamsc72086e2011-05-10 02:28:48 -07003503 /*
3504 * Copy the physical address for the command buffer to the SCU Task
3505 * Context command buffer should not contain command header.
3506 */
Dan Williamse9bf7092011-06-16 16:59:56 -07003507 task_context->command_iu_upper = upper_32_bits(sg_dma_address(sg));
3508 task_context->command_iu_lower = lower_32_bits(sg_dma_address(sg) + sizeof(u32));
Dan Williamsc72086e2011-05-10 02:28:48 -07003509
3510 /* SMP response comes as UF, so no need to set response IU address. */
3511 task_context->response_iu_upper = 0;
3512 task_context->response_iu_lower = 0;
Dan Williamsc72086e2011-05-10 02:28:48 -07003513
Dan Williams5076a1a2011-06-27 14:57:03 -07003514 sci_change_state(&ireq->sm, SCI_REQ_CONSTRUCTED);
Dan Williamsc72086e2011-05-10 02:28:48 -07003515
3516 return SCI_SUCCESS;
3517}
3518
3519/*
Dan Williamsf1f52e72011-05-10 02:28:45 -07003520 * isci_smp_request_build() - This function builds the smp request.
3521 * @ireq: This parameter points to the isci_request allocated in the
3522 * request construct function.
3523 *
3524 * SCI_SUCCESS on successfull completion, or specific failure code.
3525 */
3526static enum sci_status isci_smp_request_build(struct isci_request *ireq)
3527{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003528 struct sas_task *task = isci_request_access_task(ireq);
Dan Williamse9bf7092011-06-16 16:59:56 -07003529 struct device *dev = &ireq->isci_host->pdev->dev;
Dan Williamse9bf7092011-06-16 16:59:56 -07003530 enum sci_status status = SCI_FAILURE;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003531
Dan Williams89a73012011-06-30 19:14:33 -07003532 status = sci_io_request_construct_smp(dev, ireq, task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003533 if (status != SCI_SUCCESS)
Dan Williamsa8a0a132011-07-01 12:07:25 -07003534 dev_dbg(&ireq->isci_host->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003535 "%s: failed with status = %d\n",
3536 __func__,
3537 status);
3538
3539 return status;
3540}
3541
3542/**
3543 * isci_io_request_build() - This function builds the io request object.
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003544 * @ihost: This parameter specifies the ISCI host object
Dan Williamsf1f52e72011-05-10 02:28:45 -07003545 * @request: This parameter points to the isci_request object allocated in the
3546 * request construct function.
3547 * @sci_device: This parameter is the handle for the sci core's remote device
3548 * object that is the destination for this request.
3549 *
3550 * SCI_SUCCESS on successfull completion, or specific failure code.
3551 */
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003552static enum sci_status isci_io_request_build(struct isci_host *ihost,
Dan Williams312e0c22011-06-28 13:47:09 -07003553 struct isci_request *request,
Dan Williams78a6f062011-06-30 16:31:37 -07003554 struct isci_remote_device *idev)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003555{
3556 enum sci_status status = SCI_SUCCESS;
3557 struct sas_task *task = isci_request_access_task(request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003558
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003559 dev_dbg(&ihost->pdev->dev,
Dan Williams78a6f062011-06-30 16:31:37 -07003560 "%s: idev = 0x%p; request = %p, "
Dan Williamsf1f52e72011-05-10 02:28:45 -07003561 "num_scatter = %d\n",
3562 __func__,
Dan Williams78a6f062011-06-30 16:31:37 -07003563 idev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003564 request,
3565 task->num_scatter);
3566
3567 /* map the sgl addresses, if present.
3568 * libata does the mapping for sata devices
3569 * before we get the request.
3570 */
3571 if (task->num_scatter &&
3572 !sas_protocol_ata(task->task_proto) &&
3573 !(SAS_PROTOCOL_SMP & task->task_proto)) {
3574
3575 request->num_sg_entries = dma_map_sg(
Dan Williamsd9dcb4b2011-06-30 17:38:32 -07003576 &ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003577 task->scatter,
3578 task->num_scatter,
3579 task->data_dir
3580 );
3581
3582 if (request->num_sg_entries == 0)
3583 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
3584 }
3585
Dan Williams89a73012011-06-30 19:14:33 -07003586 status = sci_io_request_construct(ihost, idev, request);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003587
3588 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003589 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003590 "%s: failed request construct\n",
3591 __func__);
3592 return SCI_FAILURE;
3593 }
3594
3595 switch (task->task_proto) {
3596 case SAS_PROTOCOL_SMP:
3597 status = isci_smp_request_build(request);
3598 break;
3599 case SAS_PROTOCOL_SSP:
3600 status = isci_request_ssp_request_construct(request);
3601 break;
3602 case SAS_PROTOCOL_SATA:
3603 case SAS_PROTOCOL_STP:
3604 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
3605 status = isci_request_stp_request_construct(request);
3606 break;
3607 default:
Dan Williamsa8a0a132011-07-01 12:07:25 -07003608 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003609 "%s: unknown protocol\n", __func__);
3610 return SCI_FAILURE;
3611 }
3612
3613 return SCI_SUCCESS;
3614}
3615
Dan Williamsdb056252011-06-17 14:18:39 -07003616static struct isci_request *isci_request_from_tag(struct isci_host *ihost, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003617{
Dan Williams0d0cf142011-06-13 00:51:30 -07003618 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003619
Dan Williamsdb056252011-06-17 14:18:39 -07003620 ireq = ihost->reqs[ISCI_TAG_TCI(tag)];
Dan Williams5076a1a2011-06-27 14:57:03 -07003621 ireq->io_tag = tag;
Dan Williams0d0cf142011-06-13 00:51:30 -07003622 ireq->io_request_completion = NULL;
Dan Williams38d88792011-06-23 14:33:48 -07003623 ireq->flags = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003624 ireq->num_sg_entries = 0;
Dan Williams0d0cf142011-06-13 00:51:30 -07003625 INIT_LIST_HEAD(&ireq->completed_node);
3626 INIT_LIST_HEAD(&ireq->dev_node);
Dan Williams0d0cf142011-06-13 00:51:30 -07003627 isci_request_change_state(ireq, allocated);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003628
Dan Williams0d0cf142011-06-13 00:51:30 -07003629 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003630}
3631
Dan Williamsdb056252011-06-17 14:18:39 -07003632static struct isci_request *isci_io_request_from_tag(struct isci_host *ihost,
3633 struct sas_task *task,
3634 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003635{
Dan Williams0d0cf142011-06-13 00:51:30 -07003636 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003637
Dan Williamsdb056252011-06-17 14:18:39 -07003638 ireq = isci_request_from_tag(ihost, tag);
3639 ireq->ttype_ptr.io_task_ptr = task;
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003640 clear_bit(IREQ_TMF, &ireq->flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003641 task->lldd_task = ireq;
3642
Dan Williams0d0cf142011-06-13 00:51:30 -07003643 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003644}
3645
Dan Williamsdb056252011-06-17 14:18:39 -07003646struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
3647 struct isci_tmf *isci_tmf,
3648 u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003649{
Dan Williams0d0cf142011-06-13 00:51:30 -07003650 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003651
Dan Williamsdb056252011-06-17 14:18:39 -07003652 ireq = isci_request_from_tag(ihost, tag);
3653 ireq->ttype_ptr.tmf_task_ptr = isci_tmf;
Jeff Skirvin3b34c162011-10-27 15:05:22 -07003654 set_bit(IREQ_TMF, &ireq->flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003655
Dan Williams0d0cf142011-06-13 00:51:30 -07003656 return ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003657}
3658
Dan Williams209fae12011-06-13 17:39:44 -07003659int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
Dan Williamsdb056252011-06-17 14:18:39 -07003660 struct sas_task *task, u16 tag)
Dan Williamsf1f52e72011-05-10 02:28:45 -07003661{
Dan Williamsf1f52e72011-05-10 02:28:45 -07003662 enum sci_status status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
Dan Williams0d0cf142011-06-13 00:51:30 -07003663 struct isci_request *ireq;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003664 unsigned long flags;
Dan Williams0d0cf142011-06-13 00:51:30 -07003665 int ret = 0;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003666
Dan Williamsf1f52e72011-05-10 02:28:45 -07003667 /* do common allocation and init of request object. */
Dan Williamsdb056252011-06-17 14:18:39 -07003668 ireq = isci_io_request_from_tag(ihost, task, tag);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003669
Dan Williamsdb056252011-06-17 14:18:39 -07003670 status = isci_io_request_build(ihost, ireq, idev);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003671 if (status != SCI_SUCCESS) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003672 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003673 "%s: request_construct failed - status = 0x%x\n",
3674 __func__,
3675 status);
Dan Williamsdb056252011-06-17 14:18:39 -07003676 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003677 }
3678
Dan Williams0d0cf142011-06-13 00:51:30 -07003679 spin_lock_irqsave(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003680
Jeff Skirvin9274f452011-06-23 17:09:02 -07003681 if (test_bit(IDEV_IO_NCQERROR, &idev->flags)) {
3682
3683 if (isci_task_is_ncq_recovery(task)) {
3684
3685 /* The device is in an NCQ recovery state. Issue the
3686 * request on the task side. Note that it will
3687 * complete on the I/O request side because the
3688 * request was built that way (ie.
3689 * ireq->is_task_management_request is false).
3690 */
Dan Williams89a73012011-06-30 19:14:33 -07003691 status = sci_controller_start_task(ihost,
Dan Williams78a6f062011-06-30 16:31:37 -07003692 idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003693 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003694 } else {
3695 status = SCI_FAILURE;
3696 }
3697 } else {
Jeff Skirvin9274f452011-06-23 17:09:02 -07003698 /* send the request, let the core assign the IO TAG. */
Dan Williams89a73012011-06-30 19:14:33 -07003699 status = sci_controller_start_io(ihost, idev,
Dan Williams5076a1a2011-06-27 14:57:03 -07003700 ireq);
Jeff Skirvin9274f452011-06-23 17:09:02 -07003701 }
Dan Williams312e0c22011-06-28 13:47:09 -07003702
Dan Williamsf1f52e72011-05-10 02:28:45 -07003703 if (status != SCI_SUCCESS &&
3704 status != SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
Dan Williamsa8a0a132011-07-01 12:07:25 -07003705 dev_dbg(&ihost->pdev->dev,
Dan Williamsf1f52e72011-05-10 02:28:45 -07003706 "%s: failed request start (0x%x)\n",
3707 __func__, status);
Dan Williams0d0cf142011-06-13 00:51:30 -07003708 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsdb056252011-06-17 14:18:39 -07003709 return status;
Dan Williamsf1f52e72011-05-10 02:28:45 -07003710 }
3711
3712 /* Either I/O started OK, or the core has signaled that
3713 * the device needs a target reset.
3714 *
3715 * In either case, hold onto the I/O for later.
3716 *
3717 * Update it's status and add it to the list in the
3718 * remote device object.
3719 */
Dan Williams0d0cf142011-06-13 00:51:30 -07003720 list_add(&ireq->dev_node, &idev->reqs_in_process);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003721
3722 if (status == SCI_SUCCESS) {
Dan Williams0d0cf142011-06-13 00:51:30 -07003723 isci_request_change_state(ireq, started);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003724 } else {
3725 /* The request did not really start in the
3726 * hardware, so clear the request handle
3727 * here so no terminations will be done.
3728 */
Dan Williams38d88792011-06-23 14:33:48 -07003729 set_bit(IREQ_TERMINATED, &ireq->flags);
Dan Williams0d0cf142011-06-13 00:51:30 -07003730 isci_request_change_state(ireq, completed);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003731 }
Dan Williams0d0cf142011-06-13 00:51:30 -07003732 spin_unlock_irqrestore(&ihost->scic_lock, flags);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003733
3734 if (status ==
3735 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED) {
3736 /* Signal libsas that we need the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003737 * handler thread to work on this I/O and that
3738 * we want a device reset.
3739 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003740 spin_lock_irqsave(&task->task_state_lock, flags);
3741 task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
3742 spin_unlock_irqrestore(&task->task_state_lock, flags);
3743
3744 /* Cause this task to be scheduled in the SCSI error
Dan Williams312e0c22011-06-28 13:47:09 -07003745 * handler thread.
3746 */
Dan Williams312d3e52011-11-17 17:59:50 -08003747 sas_task_abort(task);
Dan Williamsf1f52e72011-05-10 02:28:45 -07003748
3749 /* Change the status, since we are holding
Dan Williams312e0c22011-06-28 13:47:09 -07003750 * the I/O until it is managed by the SCSI
3751 * error handler.
3752 */
Dan Williamsf1f52e72011-05-10 02:28:45 -07003753 status = SCI_SUCCESS;
3754 }
3755
Dan Williamsf1f52e72011-05-10 02:28:45 -07003756 return ret;
3757}