isci: Fixup SSP command IU and task IU
[deliverable/linux.git] / drivers / scsi / isci / core / scic_sds_request.c
1 /*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 #include <scsi/sas.h>
57 #include "sas.h"
58 #include "intel_sas.h"
59 #include "scic_controller.h"
60 #include "scic_io_request.h"
61 #include "scic_sds_controller.h"
62 #include "scu_registers.h"
63 #include "scic_sds_port.h"
64 #include "remote_device.h"
65 #include "scic_sds_request.h"
66 #include "scic_sds_smp_request.h"
67 #include "scic_sds_stp_request.h"
68 #include "scic_sds_unsolicited_frame_control.h"
69 #include "sci_environment.h"
70 #include "sci_util.h"
71 #include "scu_completion_codes.h"
72 #include "scu_constants.h"
73 #include "scu_task_context.h"
74
75 /*
76 * ****************************************************************************
77 * * SCIC SDS IO REQUEST CONSTANTS
78 * **************************************************************************** */
79
80 /**
81 *
82 *
83 * We have no timer requirements for IO requests right now
84 */
85 #define SCIC_SDS_IO_REQUEST_MINIMUM_TIMER_COUNT (0)
86 #define SCIC_SDS_IO_REQUEST_MAXIMUM_TIMER_COUNT (0)
87
88 /*
89 * ****************************************************************************
90 * * SCIC SDS IO REQUEST MACROS
91 * **************************************************************************** */
92
93 /**
94 * scic_ssp_io_request_get_object_size() -
95 *
96 * This macro returns the sizeof memory required to store the an SSP IO
97 * request. This does not include the size of the SGL or SCU Task Context
98 * memory.
99 */
100 #define scic_ssp_io_request_get_object_size() \
101 (\
102 sizeof(struct ssp_cmd_iu) \
103 + sizeof(struct sci_ssp_response_iu) \
104 )
105
106 /**
107 * scic_sds_ssp_request_get_command_buffer() -
108 *
109 * This macro returns the address of the ssp command buffer in the io request
110 * memory
111 */
112 #define scic_sds_ssp_request_get_command_buffer(memory) \
113 ((struct ssp_cmd_iu *)(\
114 ((char *)(memory)) + sizeof(struct scic_sds_request) \
115 ))
116
117 /**
118 * scic_sds_ssp_request_get_response_buffer() -
119 *
120 * This macro returns the address of the ssp response buffer in the io request
121 * memory
122 */
123 #define scic_sds_ssp_request_get_response_buffer(memory) \
124 ((struct sci_ssp_response_iu *)(\
125 ((char *)(scic_sds_ssp_request_get_command_buffer(memory))) \
126 + sizeof(struct ssp_cmd_iu) \
127 ))
128
129 /**
130 * scic_sds_ssp_request_get_task_context_buffer() -
131 *
132 * This macro returns the address of the task context buffer in the io request
133 * memory
134 */
135 #define scic_sds_ssp_request_get_task_context_buffer(memory) \
136 ((struct scu_task_context *)(\
137 ((char *)(scic_sds_ssp_request_get_response_buffer(memory))) \
138 + sizeof(struct sci_ssp_response_iu) \
139 ))
140
141 /**
142 * scic_sds_ssp_request_get_sgl_element_buffer() -
143 *
144 * This macro returns the address of the sgl elment pairs in the io request
145 * memory buffer
146 */
147 #define scic_sds_ssp_request_get_sgl_element_buffer(memory) \
148 ((struct scu_sgl_element_pair *)(\
149 ((char *)(scic_sds_ssp_request_get_task_context_buffer(memory))) \
150 + sizeof(struct scu_task_context) \
151 ))
152
153
154 /**
155 * scic_ssp_task_request_get_object_size() -
156 *
157 * This macro returns the sizeof of memory required to store an SSP Task
158 * request. This does not include the size of the SCU Task Context memory.
159 */
160 #define scic_ssp_task_request_get_object_size() \
161 (\
162 sizeof(struct ssp_task_iu) \
163 + sizeof(struct sci_ssp_response_iu) \
164 )
165
166 /**
167 * scic_sds_ssp_task_request_get_command_buffer() -
168 *
169 * This macro returns the address of the ssp command buffer in the task request
170 * memory. Yes its the same as the above macro except for the name.
171 */
172 #define scic_sds_ssp_task_request_get_command_buffer(memory) \
173 ((struct ssp_task_iu *)(\
174 ((char *)(memory)) + sizeof(struct scic_sds_request) \
175 ))
176
177 /**
178 * scic_sds_ssp_task_request_get_response_buffer() -
179 *
180 * This macro returns the address of the ssp response buffer in the task
181 * request memory.
182 */
183 #define scic_sds_ssp_task_request_get_response_buffer(memory) \
184 ((struct sci_ssp_response_iu *)(\
185 ((char *)(scic_sds_ssp_task_request_get_command_buffer(memory))) \
186 + sizeof(struct ssp_task_iu) \
187 ))
188
189 /**
190 * scic_sds_ssp_task_request_get_task_context_buffer() -
191 *
192 * This macro returs the task context buffer for the SSP task request.
193 */
194 #define scic_sds_ssp_task_request_get_task_context_buffer(memory) \
195 ((struct scu_task_context *)(\
196 ((char *)(scic_sds_ssp_task_request_get_response_buffer(memory))) \
197 + sizeof(struct sci_ssp_response_iu) \
198 ))
199
200
201
202 /*
203 * ****************************************************************************
204 * * SCIC SDS IO REQUEST PRIVATE METHODS
205 * **************************************************************************** */
206
207 /**
208 *
209 *
210 * This method returns the size required to store an SSP IO request object. u32
211 */
212 static u32 scic_sds_ssp_request_get_object_size(void)
213 {
214 return sizeof(struct scic_sds_request)
215 + scic_ssp_io_request_get_object_size()
216 + sizeof(struct scu_task_context)
217 + SMP_CACHE_BYTES
218 + sizeof(struct scu_sgl_element_pair) * SCU_MAX_SGL_ELEMENT_PAIRS;
219 }
220
221 /**
222 * This method returns the sgl element pair for the specificed sgl_pair index.
223 * @sci_req: This parameter specifies the IO request for which to retrieve
224 * the Scatter-Gather List element pair.
225 * @sgl_pair_index: This parameter specifies the index into the SGL element
226 * pair to be retrieved.
227 *
228 * This method returns a pointer to an struct scu_sgl_element_pair.
229 */
230 static struct scu_sgl_element_pair *scic_sds_request_get_sgl_element_pair(
231 struct scic_sds_request *sci_req,
232 u32 sgl_pair_index
233 ) {
234 struct scu_task_context *task_context;
235
236 task_context = (struct scu_task_context *)sci_req->task_context_buffer;
237
238 if (sgl_pair_index == 0) {
239 return &task_context->sgl_pair_ab;
240 } else if (sgl_pair_index == 1) {
241 return &task_context->sgl_pair_cd;
242 }
243
244 return &sci_req->sgl_element_pair_buffer[sgl_pair_index - 2];
245 }
246
247 /**
248 * This function will build the SGL list for an IO request.
249 * @sci_req: This parameter specifies the IO request for which to build
250 * the Scatter-Gather List.
251 *
252 */
253 void scic_sds_request_build_sgl(struct scic_sds_request *sds_request)
254 {
255 struct isci_request *isci_request = sds_request->ireq;
256 struct isci_host *isci_host = isci_request->isci_host;
257 struct sas_task *task = isci_request_access_task(isci_request);
258 struct scatterlist *sg = NULL;
259 dma_addr_t dma_addr;
260 u32 sg_idx = 0;
261 struct scu_sgl_element_pair *scu_sg = NULL;
262 struct scu_sgl_element_pair *prev_sg = NULL;
263
264 if (task->num_scatter > 0) {
265 sg = task->scatter;
266
267 while (sg) {
268 scu_sg = scic_sds_request_get_sgl_element_pair(
269 sds_request,
270 sg_idx);
271
272 SCU_SGL_COPY(scu_sg->A, sg);
273
274 sg = sg_next(sg);
275
276 if (sg) {
277 SCU_SGL_COPY(scu_sg->B, sg);
278 sg = sg_next(sg);
279 } else
280 SCU_SGL_ZERO(scu_sg->B);
281
282 if (prev_sg) {
283 dma_addr =
284 scic_io_request_get_dma_addr(
285 sds_request,
286 scu_sg);
287
288 prev_sg->next_pair_upper =
289 upper_32_bits(dma_addr);
290 prev_sg->next_pair_lower =
291 lower_32_bits(dma_addr);
292 }
293
294 prev_sg = scu_sg;
295 sg_idx++;
296 }
297 } else { /* handle when no sg */
298 scu_sg = scic_sds_request_get_sgl_element_pair(sds_request,
299 sg_idx);
300
301 dma_addr = dma_map_single(&isci_host->pdev->dev,
302 task->scatter,
303 task->total_xfer_len,
304 task->data_dir);
305
306 isci_request->zero_scatter_daddr = dma_addr;
307
308 scu_sg->A.length = task->total_xfer_len;
309 scu_sg->A.address_upper = upper_32_bits(dma_addr);
310 scu_sg->A.address_lower = lower_32_bits(dma_addr);
311 }
312
313 if (scu_sg) {
314 scu_sg->next_pair_upper = 0;
315 scu_sg->next_pair_lower = 0;
316 }
317 }
318
319 /**
320 * This method build the remainder of the IO request object.
321 * @sci_req: This parameter specifies the request object being constructed.
322 *
323 * The scic_sds_general_request_construct() must be called before this call is
324 * valid. none
325 */
326 static void scic_sds_ssp_io_request_assign_buffers(
327 struct scic_sds_request *sci_req)
328 {
329 sci_req->command_buffer =
330 scic_sds_ssp_request_get_command_buffer(sci_req);
331 sci_req->response_buffer =
332 scic_sds_ssp_request_get_response_buffer(sci_req);
333 sci_req->sgl_element_pair_buffer =
334 scic_sds_ssp_request_get_sgl_element_buffer(sci_req);
335 sci_req->sgl_element_pair_buffer =
336 PTR_ALIGN(sci_req->sgl_element_pair_buffer,
337 sizeof(struct scu_sgl_element_pair));
338
339 if (sci_req->was_tag_assigned_by_user == false) {
340 sci_req->task_context_buffer =
341 scic_sds_ssp_request_get_task_context_buffer(sci_req);
342 sci_req->task_context_buffer =
343 PTR_ALIGN(sci_req->task_context_buffer,
344 SMP_CACHE_BYTES);
345 }
346 }
347
348 static void scic_sds_io_request_build_ssp_command_iu(struct scic_sds_request *sci_req)
349 {
350 struct ssp_cmd_iu *cmd_iu;
351 struct isci_request *ireq = sci_req->ireq;
352 struct sas_task *task = isci_request_access_task(ireq);
353
354 cmd_iu = sci_req->command_buffer;
355
356 memcpy(cmd_iu->LUN, task->ssp_task.LUN, 8);
357 cmd_iu->add_cdb_len = 0;
358 cmd_iu->_r_a = 0;
359 cmd_iu->_r_b = 0;
360 cmd_iu->en_fburst = 0; /* unsupported */
361 cmd_iu->task_prio = task->ssp_task.task_prio;
362 cmd_iu->task_attr = task->ssp_task.task_attr;
363 cmd_iu->_r_c = 0;
364
365 scic_word_copy_with_swap(
366 (u32 *)(&cmd_iu->cdb),
367 (u32 *)task->ssp_task.cdb,
368 sizeof(task->ssp_task.cdb) / sizeof(u32));
369 }
370
371 static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
372 {
373 struct ssp_task_iu *task_iu;
374 struct isci_request *ireq = sci_req->ireq;
375 struct sas_task *task = isci_request_access_task(ireq);
376 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
377
378 task_iu = sci_req->command_buffer;
379
380 memset(task_iu, 0, sizeof(struct ssp_task_iu));
381
382 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
383
384 task_iu->task_func = isci_tmf->tmf_code;
385 task_iu->task_tag =
386 (ireq->ttype == tmf_task) ?
387 isci_tmf->io_tag :
388 SCI_CONTROLLER_INVALID_IO_TAG;
389 }
390
391 /**
392 * This method is will fill in the SCU Task Context for any type of SSP request.
393 * @sci_req:
394 * @task_context:
395 *
396 */
397 static void scu_ssp_reqeust_construct_task_context(
398 struct scic_sds_request *sds_request,
399 struct scu_task_context *task_context)
400 {
401 dma_addr_t dma_addr;
402 struct scic_sds_controller *controller;
403 struct scic_sds_remote_device *target_device;
404 struct scic_sds_port *target_port;
405
406 controller = scic_sds_request_get_controller(sds_request);
407 target_device = scic_sds_request_get_device(sds_request);
408 target_port = scic_sds_request_get_port(sds_request);
409
410 /* Fill in the TC with the its required data */
411 task_context->abort = 0;
412 task_context->priority = 0;
413 task_context->initiator_request = 1;
414 task_context->connection_rate = target_device->connection_rate;
415 task_context->protocol_engine_index =
416 scic_sds_controller_get_protocol_engine_group(controller);
417 task_context->logical_port_index =
418 scic_sds_port_get_index(target_port);
419 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
420 task_context->valid = SCU_TASK_CONTEXT_VALID;
421 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
422
423 task_context->remote_node_index =
424 scic_sds_remote_device_get_index(sds_request->target_device);
425 task_context->command_code = 0;
426
427 task_context->link_layer_control = 0;
428 task_context->do_not_dma_ssp_good_response = 1;
429 task_context->strict_ordering = 0;
430 task_context->control_frame = 0;
431 task_context->timeout_enable = 0;
432 task_context->block_guard_enable = 0;
433
434 task_context->address_modifier = 0;
435
436 /* task_context->type.ssp.tag = sci_req->io_tag; */
437 task_context->task_phase = 0x01;
438
439 if (sds_request->was_tag_assigned_by_user) {
440 /*
441 * Build the task context now since we have already read
442 * the data
443 */
444 sds_request->post_context =
445 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
446 (scic_sds_controller_get_protocol_engine_group(
447 controller) <<
448 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
449 (scic_sds_port_get_index(target_port) <<
450 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
451 scic_sds_io_tag_get_index(sds_request->io_tag));
452 } else {
453 /*
454 * Build the task context now since we have already read
455 * the data
456 *
457 * I/O tag index is not assigned because we have to wait
458 * until we get a TCi
459 */
460 sds_request->post_context =
461 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
462 (scic_sds_controller_get_protocol_engine_group(
463 owning_controller) <<
464 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
465 (scic_sds_port_get_index(target_port) <<
466 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
467 }
468
469 /*
470 * Copy the physical address for the command buffer to the
471 * SCU Task Context
472 */
473 dma_addr = scic_io_request_get_dma_addr(sds_request,
474 sds_request->command_buffer);
475
476 task_context->command_iu_upper = upper_32_bits(dma_addr);
477 task_context->command_iu_lower = lower_32_bits(dma_addr);
478
479 /*
480 * Copy the physical address for the response buffer to the
481 * SCU Task Context
482 */
483 dma_addr = scic_io_request_get_dma_addr(sds_request,
484 sds_request->response_buffer);
485
486 task_context->response_iu_upper = upper_32_bits(dma_addr);
487 task_context->response_iu_lower = lower_32_bits(dma_addr);
488 }
489
490 /**
491 * This method is will fill in the SCU Task Context for a SSP IO request.
492 * @sci_req:
493 *
494 */
495 static void scu_ssp_io_request_construct_task_context(
496 struct scic_sds_request *sci_req,
497 enum dma_data_direction dir,
498 u32 len)
499 {
500 struct scu_task_context *task_context;
501
502 task_context = scic_sds_request_get_task_context(sci_req);
503
504 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
505
506 task_context->ssp_command_iu_length =
507 sizeof(struct ssp_cmd_iu) / sizeof(u32);
508 task_context->type.ssp.frame_type = SCI_SAS_COMMAND_FRAME;
509
510 switch (dir) {
511 case DMA_FROM_DEVICE:
512 case DMA_NONE:
513 default:
514 task_context->task_type = SCU_TASK_TYPE_IOREAD;
515 break;
516 case DMA_TO_DEVICE:
517 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
518 break;
519 }
520
521 task_context->transfer_length_bytes = len;
522
523 if (task_context->transfer_length_bytes > 0)
524 scic_sds_request_build_sgl(sci_req);
525 }
526
527
528 /**
529 * This method will fill in the remainder of the io request object for SSP Task
530 * requests.
531 * @sci_req:
532 *
533 */
534 static void scic_sds_ssp_task_request_assign_buffers(
535 struct scic_sds_request *sci_req)
536 {
537 /* Assign all of the buffer pointers */
538 sci_req->command_buffer =
539 scic_sds_ssp_task_request_get_command_buffer(sci_req);
540 sci_req->response_buffer =
541 scic_sds_ssp_task_request_get_response_buffer(sci_req);
542 sci_req->sgl_element_pair_buffer = NULL;
543
544 if (sci_req->was_tag_assigned_by_user == false) {
545 sci_req->task_context_buffer =
546 scic_sds_ssp_task_request_get_task_context_buffer(sci_req);
547 sci_req->task_context_buffer =
548 PTR_ALIGN(sci_req->task_context_buffer, SMP_CACHE_BYTES);
549 }
550 }
551
552 /**
553 * This method will fill in the SCU Task Context for a SSP Task request. The
554 * following important settings are utilized: -# priority ==
555 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
556 * ahead of other task destined for the same Remote Node. -# task_type ==
557 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
558 * (i.e. non-raw frame) is being utilized to perform task management. -#
559 * control_frame == 1. This ensures that the proper endianess is set so
560 * that the bytes are transmitted in the right order for a task frame.
561 * @sci_req: This parameter specifies the task request object being
562 * constructed.
563 *
564 */
565 static void scu_ssp_task_request_construct_task_context(
566 struct scic_sds_request *sci_req)
567 {
568 struct scu_task_context *task_context;
569
570 task_context = scic_sds_request_get_task_context(sci_req);
571
572 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
573
574 task_context->control_frame = 1;
575 task_context->priority = SCU_TASK_PRIORITY_HIGH;
576 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
577 task_context->transfer_length_bytes = 0;
578 task_context->type.ssp.frame_type = SCI_SAS_TASK_FRAME;
579 task_context->ssp_command_iu_length =
580 sizeof(struct ssp_task_iu) / sizeof(u32);
581 }
582
583
584 /**
585 * This method constructs the SSP Command IU data for this ssp passthrough
586 * comand request object.
587 * @sci_req: This parameter specifies the request object for which the SSP
588 * command information unit is being built.
589 *
590 * enum sci_status, returns invalid parameter is cdb > 16
591 */
592
593
594 /**
595 * This method constructs the SATA request object.
596 * @sci_req:
597 * @sat_protocol:
598 * @transfer_length:
599 * @data_direction:
600 * @copy_rx_frame:
601 *
602 * enum sci_status
603 */
604 static enum sci_status
605 scic_io_request_construct_sata(struct scic_sds_request *sci_req,
606 u32 len,
607 enum dma_data_direction dir,
608 bool copy)
609 {
610 enum sci_status status = SCI_SUCCESS;
611 struct isci_request *ireq = sci_req->ireq;
612 struct sas_task *task = isci_request_access_task(ireq);
613
614 /* check for management protocols */
615 if (ireq->ttype == tmf_task) {
616 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
617
618 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
619 tmf->tmf_code == isci_tmf_sata_srst_low)
620 return scic_sds_stp_soft_reset_request_construct(sci_req);
621 else {
622 dev_err(scic_to_dev(sci_req->owning_controller),
623 "%s: Request 0x%p received un-handled SAT "
624 "management protocol 0x%x.\n",
625 __func__, sci_req, tmf->tmf_code);
626
627 return SCI_FAILURE;
628 }
629 }
630
631 if (!sas_protocol_ata(task->task_proto)) {
632 dev_err(scic_to_dev(sci_req->owning_controller),
633 "%s: Non-ATA protocol in SATA path: 0x%x\n",
634 __func__,
635 task->task_proto);
636 return SCI_FAILURE;
637
638 }
639
640 /* non data */
641 if (task->data_dir == DMA_NONE)
642 return scic_sds_stp_non_data_request_construct(sci_req);
643
644 /* NCQ */
645 if (task->ata_task.use_ncq)
646 return scic_sds_stp_ncq_request_construct(sci_req, len, dir);
647
648 /* DMA */
649 if (task->ata_task.dma_xfer)
650 return scic_sds_stp_udma_request_construct(sci_req, len, dir);
651 else /* PIO */
652 return scic_sds_stp_pio_request_construct(sci_req, copy);
653
654 return status;
655 }
656
657 u32 scic_io_request_get_object_size(void)
658 {
659 u32 ssp_request_size;
660 u32 stp_request_size;
661 u32 smp_request_size;
662
663 ssp_request_size = scic_sds_ssp_request_get_object_size();
664 stp_request_size = scic_sds_stp_request_get_object_size();
665 smp_request_size = scic_sds_smp_request_get_object_size();
666
667 return max(ssp_request_size, max(stp_request_size, smp_request_size));
668 }
669
670 enum sci_status scic_io_request_construct_basic_ssp(
671 struct scic_sds_request *sci_req)
672 {
673 struct isci_request *isci_request = sci_req->ireq;
674
675 sci_req->protocol = SCIC_SSP_PROTOCOL;
676
677 scu_ssp_io_request_construct_task_context(
678 sci_req,
679 isci_request_io_request_get_data_direction(isci_request),
680 isci_request_io_request_get_transfer_length(isci_request));
681
682 scic_sds_io_request_build_ssp_command_iu(sci_req);
683
684 sci_base_state_machine_change_state(&sci_req->state_machine,
685 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
686
687 return SCI_SUCCESS;
688 }
689
690
691 enum sci_status scic_task_request_construct_ssp(
692 struct scic_sds_request *sci_req)
693 {
694 /* Construct the SSP Task SCU Task Context */
695 scu_ssp_task_request_construct_task_context(sci_req);
696
697 /* Fill in the SSP Task IU */
698 scic_sds_task_request_build_ssp_task_iu(sci_req);
699
700 sci_base_state_machine_change_state(&sci_req->state_machine,
701 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
702
703 return SCI_SUCCESS;
704 }
705
706
707 enum sci_status scic_io_request_construct_basic_sata(
708 struct scic_sds_request *sci_req)
709 {
710 enum sci_status status;
711 struct scic_sds_stp_request *stp_req;
712 u32 len;
713 enum dma_data_direction dir;
714 bool copy = false;
715 struct isci_request *isci_request = sci_req->ireq;
716 struct sas_task *task = isci_request_access_task(isci_request);
717
718 stp_req = container_of(sci_req, typeof(*stp_req), parent);
719
720 sci_req->protocol = SCIC_STP_PROTOCOL;
721
722 len = isci_request_io_request_get_transfer_length(isci_request);
723 dir = isci_request_io_request_get_data_direction(isci_request);
724 copy = (task->data_dir == DMA_NONE) ? false : true;
725
726 status = scic_io_request_construct_sata(sci_req, len, dir, copy);
727
728 if (status == SCI_SUCCESS)
729 sci_base_state_machine_change_state(&sci_req->state_machine,
730 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
731
732 return status;
733 }
734
735
736 enum sci_status scic_task_request_construct_sata(
737 struct scic_sds_request *sci_req)
738 {
739 enum sci_status status = SCI_SUCCESS;
740 struct isci_request *ireq = sci_req->ireq;
741
742 /* check for management protocols */
743 if (ireq->ttype == tmf_task) {
744 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
745
746 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
747 tmf->tmf_code == isci_tmf_sata_srst_low) {
748 status = scic_sds_stp_soft_reset_request_construct(sci_req);
749 } else {
750 dev_err(scic_to_dev(sci_req->owning_controller),
751 "%s: Request 0x%p received un-handled SAT "
752 "Protocol 0x%x.\n",
753 __func__, sci_req, tmf->tmf_code);
754
755 return SCI_FAILURE;
756 }
757 }
758
759 if (status == SCI_SUCCESS)
760 sci_base_state_machine_change_state(
761 &sci_req->state_machine,
762 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
763
764 return status;
765 }
766
767
768 u16 scic_io_request_get_io_tag(
769 struct scic_sds_request *sci_req)
770 {
771 return sci_req->io_tag;
772 }
773
774
775 u32 scic_request_get_controller_status(
776 struct scic_sds_request *sci_req)
777 {
778 return sci_req->scu_status;
779 }
780
781
782 void *scic_io_request_get_command_iu_address(
783 struct scic_sds_request *sci_req)
784 {
785 return sci_req->command_buffer;
786 }
787
788
789 void *scic_io_request_get_response_iu_address(
790 struct scic_sds_request *sci_req)
791 {
792 return sci_req->response_buffer;
793 }
794
795
796 #define SCU_TASK_CONTEXT_SRAM 0x200000
797 u32 scic_io_request_get_number_of_bytes_transferred(
798 struct scic_sds_request *scic_sds_request)
799 {
800 struct scic_sds_controller *scic = scic_sds_request->owning_controller;
801 u32 ret_val = 0;
802
803 if (readl(&scic->smu_registers->address_modifier) == 0) {
804 void __iomem *scu_reg_base = scic->scu_registers;
805 /*
806 * get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
807 * BAR1 is the scu_registers
808 * 0x20002C = 0x200000 + 0x2c
809 * = start of task context SRAM + offset of (type.ssp.data_offset)
810 * TCi is the io_tag of struct scic_sds_request */
811 ret_val = readl(scu_reg_base +
812 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
813 ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(scic_sds_request->io_tag)));
814 }
815
816 return ret_val;
817 }
818
819
820 /*
821 * ****************************************************************************
822 * * SCIC SDS Interface Implementation
823 * **************************************************************************** */
824
825 enum sci_status
826 scic_sds_request_start(struct scic_sds_request *request)
827 {
828 if (request->device_sequence !=
829 scic_sds_remote_device_get_sequence(request->target_device))
830 return SCI_FAILURE;
831
832 if (request->state_handlers->start_handler)
833 return request->state_handlers->start_handler(request);
834
835 dev_warn(scic_to_dev(request->owning_controller),
836 "%s: SCIC IO Request requested to start while in wrong "
837 "state %d\n",
838 __func__,
839 sci_base_state_machine_get_state(&request->state_machine));
840
841 return SCI_FAILURE_INVALID_STATE;
842 }
843
844 enum sci_status
845 scic_sds_io_request_terminate(struct scic_sds_request *request)
846 {
847 if (request->state_handlers->abort_handler)
848 return request->state_handlers->abort_handler(request);
849
850 dev_warn(scic_to_dev(request->owning_controller),
851 "%s: SCIC IO Request requested to abort while in wrong "
852 "state %d\n",
853 __func__,
854 sci_base_state_machine_get_state(&request->state_machine));
855
856 return SCI_FAILURE_INVALID_STATE;
857 }
858
859 enum sci_status
860 scic_sds_io_request_complete(struct scic_sds_request *request)
861 {
862 if (request->state_handlers->complete_handler)
863 return request->state_handlers->complete_handler(request);
864
865 dev_warn(scic_to_dev(request->owning_controller),
866 "%s: SCIC IO Request requested to complete while in wrong "
867 "state %d\n",
868 __func__,
869 sci_base_state_machine_get_state(&request->state_machine));
870
871 return SCI_FAILURE_INVALID_STATE;
872 }
873
874 enum sci_status scic_sds_io_request_event_handler(
875 struct scic_sds_request *request,
876 u32 event_code)
877 {
878 if (request->state_handlers->event_handler)
879 return request->state_handlers->event_handler(request, event_code);
880
881 dev_warn(scic_to_dev(request->owning_controller),
882 "%s: SCIC IO Request given event code notification %x while "
883 "in wrong state %d\n",
884 __func__,
885 event_code,
886 sci_base_state_machine_get_state(&request->state_machine));
887
888 return SCI_FAILURE_INVALID_STATE;
889 }
890
891 enum sci_status
892 scic_sds_io_request_tc_completion(struct scic_sds_request *request, u32 completion_code)
893 {
894 if (request->state_machine.current_state_id == SCI_BASE_REQUEST_STATE_STARTED &&
895 request->has_started_substate_machine == false)
896 return scic_sds_request_started_state_tc_completion_handler(request, completion_code);
897 else if (request->state_handlers->tc_completion_handler)
898 return request->state_handlers->tc_completion_handler(request, completion_code);
899
900 dev_warn(scic_to_dev(request->owning_controller),
901 "%s: SCIC IO Request given task completion notification %x "
902 "while in wrong state %d\n",
903 __func__,
904 completion_code,
905 sci_base_state_machine_get_state(&request->state_machine));
906
907 return SCI_FAILURE_INVALID_STATE;
908
909 }
910
911
912 /**
913 *
914 * @sci_req: The SCIC_SDS_IO_REQUEST_T object for which the start
915 * operation is to be executed.
916 * @frame_index: The frame index returned by the hardware for the reqeust
917 * object.
918 *
919 * This method invokes the core state frame handler for the
920 * SCIC_SDS_IO_REQUEST_T object. enum sci_status
921 */
922 enum sci_status scic_sds_io_request_frame_handler(
923 struct scic_sds_request *request,
924 u32 frame_index)
925 {
926 if (request->state_handlers->frame_handler)
927 return request->state_handlers->frame_handler(request, frame_index);
928
929 dev_warn(scic_to_dev(request->owning_controller),
930 "%s: SCIC IO Request given unexpected frame %x while in "
931 "state %d\n",
932 __func__,
933 frame_index,
934 sci_base_state_machine_get_state(&request->state_machine));
935
936 scic_sds_controller_release_frame(request->owning_controller, frame_index);
937 return SCI_FAILURE_INVALID_STATE;
938 }
939
940 /**
941 *
942 * @sci_req: The SCIC_SDS_IO_REQUEST_T object for which the task start
943 * operation is to be executed.
944 *
945 * This method invokes the core state task complete handler for the
946 * SCIC_SDS_IO_REQUEST_T object. enum sci_status
947 */
948
949 /*
950 * ****************************************************************************
951 * * SCIC SDS PROTECTED METHODS
952 * **************************************************************************** */
953
954 /**
955 * This method copies response data for requests returning response data
956 * instead of sense data.
957 * @sci_req: This parameter specifies the request object for which to copy
958 * the response data.
959 *
960 */
961 void scic_sds_io_request_copy_response(struct scic_sds_request *sds_request)
962 {
963 void *response_buffer;
964 u32 user_response_length;
965 u32 core_response_length;
966 struct sci_ssp_response_iu *ssp_response;
967 struct isci_request *isci_request = sds_request->ireq;
968
969 ssp_response =
970 (struct sci_ssp_response_iu *)sds_request->response_buffer;
971
972 response_buffer =
973 isci_task_ssp_request_get_response_data_address(
974 isci_request);
975
976 user_response_length =
977 isci_task_ssp_request_get_response_data_length(
978 isci_request);
979
980 core_response_length = sci_ssp_get_response_data_length(
981 ssp_response->response_data_length);
982
983 user_response_length = min(user_response_length, core_response_length);
984
985 memcpy(response_buffer, ssp_response->data, user_response_length);
986 }
987
988 /*
989 * *****************************************************************************
990 * * CONSTRUCTED STATE HANDLERS
991 * ***************************************************************************** */
992
993 /*
994 * This method implements the action taken when a constructed
995 * SCIC_SDS_IO_REQUEST_T object receives a scic_sds_request_start() request.
996 * This method will, if necessary, allocate a TCi for the io request object and
997 * then will, if necessary, copy the constructed TC data into the actual TC
998 * buffer. If everything is successful the post context field is updated with
999 * the TCi so the controller can post the request to the hardware. enum sci_status
1000 * SCI_SUCCESS SCI_FAILURE_INSUFFICIENT_RESOURCES
1001 */
1002 static enum sci_status scic_sds_request_constructed_state_start_handler(
1003 struct scic_sds_request *request)
1004 {
1005 struct scu_task_context *task_context;
1006
1007 if (request->io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1008 request->io_tag =
1009 scic_controller_allocate_io_tag(request->owning_controller);
1010 }
1011
1012 /* Record the IO Tag in the request */
1013 if (request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
1014 task_context = request->task_context_buffer;
1015
1016 task_context->task_index = scic_sds_io_tag_get_index(request->io_tag);
1017
1018 switch (task_context->protocol_type) {
1019 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
1020 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
1021 /* SSP/SMP Frame */
1022 task_context->type.ssp.tag = request->io_tag;
1023 task_context->type.ssp.target_port_transfer_tag = 0xFFFF;
1024 break;
1025
1026 case SCU_TASK_CONTEXT_PROTOCOL_STP:
1027 /*
1028 * STP/SATA Frame
1029 * task_context->type.stp.ncq_tag = request->ncq_tag; */
1030 break;
1031
1032 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
1033 /* / @todo When do we set no protocol type? */
1034 break;
1035
1036 default:
1037 /* This should never happen since we build the IO requests */
1038 break;
1039 }
1040
1041 /*
1042 * Check to see if we need to copy the task context buffer
1043 * or have been building into the task context buffer */
1044 if (request->was_tag_assigned_by_user == false) {
1045 scic_sds_controller_copy_task_context(
1046 request->owning_controller, request);
1047 }
1048
1049 /* Add to the post_context the io tag value */
1050 request->post_context |= scic_sds_io_tag_get_index(request->io_tag);
1051
1052 /* Everything is good go ahead and change state */
1053 sci_base_state_machine_change_state(&request->state_machine,
1054 SCI_BASE_REQUEST_STATE_STARTED);
1055
1056 return SCI_SUCCESS;
1057 }
1058
1059 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1060 }
1061
1062 /*
1063 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1064 * object receives a scic_sds_request_terminate() request. Since the request
1065 * has not yet been posted to the hardware the request transitions to the
1066 * completed state. enum sci_status SCI_SUCCESS
1067 */
1068 static enum sci_status scic_sds_request_constructed_state_abort_handler(
1069 struct scic_sds_request *request)
1070 {
1071 /*
1072 * This request has been terminated by the user make sure that the correct
1073 * status code is returned */
1074 scic_sds_request_set_status(request,
1075 SCU_TASK_DONE_TASK_ABORT,
1076 SCI_FAILURE_IO_TERMINATED);
1077
1078 sci_base_state_machine_change_state(&request->state_machine,
1079 SCI_BASE_REQUEST_STATE_COMPLETED);
1080 return SCI_SUCCESS;
1081 }
1082
1083 /*
1084 * *****************************************************************************
1085 * * STARTED STATE HANDLERS
1086 * ***************************************************************************** */
1087
1088 /*
1089 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1090 * object receives a scic_sds_request_terminate() request. Since the request
1091 * has been posted to the hardware the io request state is changed to the
1092 * aborting state. enum sci_status SCI_SUCCESS
1093 */
1094 enum sci_status scic_sds_request_started_state_abort_handler(
1095 struct scic_sds_request *request)
1096 {
1097 if (request->has_started_substate_machine)
1098 sci_base_state_machine_stop(&request->started_substate_machine);
1099
1100 sci_base_state_machine_change_state(&request->state_machine,
1101 SCI_BASE_REQUEST_STATE_ABORTING);
1102 return SCI_SUCCESS;
1103 }
1104
1105 /**
1106 * scic_sds_request_started_state_tc_completion_handler() - This method process
1107 * TC (task context) completions for normal IO request (i.e. Task/Abort
1108 * Completions of type 0). This method will update the
1109 * SCIC_SDS_IO_REQUEST_T::status field.
1110 * @sci_req: This parameter specifies the request for which a completion
1111 * occurred.
1112 * @completion_code: This parameter specifies the completion code received from
1113 * the SCU.
1114 *
1115 */
1116 enum sci_status scic_sds_request_started_state_tc_completion_handler(
1117 struct scic_sds_request *sci_req,
1118 u32 completion_code)
1119 {
1120 u8 data_present;
1121 struct sci_ssp_response_iu *response_buffer;
1122
1123 /**
1124 * @todo Any SDMA return code of other than 0 is bad
1125 * decode 0x003C0000 to determine SDMA status
1126 */
1127 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1128 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1129 scic_sds_request_set_status(
1130 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1131 );
1132 break;
1133
1134 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP):
1135 {
1136 /*
1137 * There are times when the SCU hardware will return an early response
1138 * because the io request specified more data than is returned by the
1139 * target device (mode pages, inquiry data, etc.). We must check the
1140 * response stats to see if this is truly a failed request or a good
1141 * request that just got completed early. */
1142 struct sci_ssp_response_iu *response = (struct sci_ssp_response_iu *)
1143 sci_req->response_buffer;
1144 scic_word_copy_with_swap(
1145 sci_req->response_buffer,
1146 sci_req->response_buffer,
1147 sizeof(struct sci_ssp_response_iu) / sizeof(u32)
1148 );
1149
1150 if (response->status == 0) {
1151 scic_sds_request_set_status(
1152 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS_IO_DONE_EARLY
1153 );
1154 } else {
1155 scic_sds_request_set_status(
1156 sci_req,
1157 SCU_TASK_DONE_CHECK_RESPONSE,
1158 SCI_FAILURE_IO_RESPONSE_VALID
1159 );
1160 }
1161 }
1162 break;
1163
1164 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE):
1165 scic_word_copy_with_swap(
1166 sci_req->response_buffer,
1167 sci_req->response_buffer,
1168 sizeof(struct sci_ssp_response_iu) / sizeof(u32)
1169 );
1170
1171 scic_sds_request_set_status(
1172 sci_req,
1173 SCU_TASK_DONE_CHECK_RESPONSE,
1174 SCI_FAILURE_IO_RESPONSE_VALID
1175 );
1176 break;
1177
1178 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1179 /*
1180 * / @todo With TASK_DONE_RESP_LEN_ERR is the response frame guaranteed
1181 * / to be received before this completion status is posted? */
1182 response_buffer =
1183 (struct sci_ssp_response_iu *)sci_req->response_buffer;
1184 data_present =
1185 response_buffer->data_present & SCI_SSP_RESPONSE_IU_DATA_PRESENT_MASK;
1186
1187 if ((data_present == 0x01) || (data_present == 0x02)) {
1188 scic_sds_request_set_status(
1189 sci_req,
1190 SCU_TASK_DONE_CHECK_RESPONSE,
1191 SCI_FAILURE_IO_RESPONSE_VALID
1192 );
1193 } else {
1194 scic_sds_request_set_status(
1195 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1196 );
1197 }
1198 break;
1199
1200 /* only stp device gets suspended. */
1201 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1202 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1203 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1204 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1205 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1206 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1207 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1208 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1209 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1210 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1211 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
1212 if (sci_req->protocol == SCIC_STP_PROTOCOL) {
1213 scic_sds_request_set_status(
1214 sci_req,
1215 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1216 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
1217 );
1218 } else {
1219 scic_sds_request_set_status(
1220 sci_req,
1221 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1222 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1223 );
1224 }
1225 break;
1226
1227 /* both stp/ssp device gets suspended */
1228 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1229 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1230 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1231 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1232 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1233 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1234 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1235 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1236 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1237 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1238 scic_sds_request_set_status(
1239 sci_req,
1240 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1241 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED
1242 );
1243 break;
1244
1245 /* neither ssp nor stp gets suspended. */
1246 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1247 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1248 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1249 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1250 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1251 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1252 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1253 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1254 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1255 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1256 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1257 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1258 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1259 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1260 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1261 default:
1262 scic_sds_request_set_status(
1263 sci_req,
1264 SCU_GET_COMPLETION_TL_STATUS(completion_code) >> SCU_COMPLETION_TL_STATUS_SHIFT,
1265 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1266 );
1267 break;
1268 }
1269
1270 /**
1271 * @todo This is probably wrong for ACK/NAK timeout conditions
1272 */
1273
1274 /* In all cases we will treat this as the completion of the IO request. */
1275 sci_base_state_machine_change_state(&sci_req->state_machine,
1276 SCI_BASE_REQUEST_STATE_COMPLETED);
1277 return SCI_SUCCESS;
1278 }
1279
1280 /*
1281 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1282 * object receives a scic_sds_request_frame_handler() request. This method
1283 * first determines the frame type received. If this is a response frame then
1284 * the response data is copied to the io request response buffer for processing
1285 * at completion time. If the frame type is not a response buffer an error is
1286 * logged. enum sci_status SCI_SUCCESS SCI_FAILURE_INVALID_PARAMETER_VALUE
1287 */
1288 static enum sci_status scic_sds_request_started_state_frame_handler(
1289 struct scic_sds_request *sci_req,
1290 u32 frame_index)
1291 {
1292 enum sci_status status;
1293 struct sci_ssp_frame_header *frame_header;
1294
1295 /* / @todo If this is a response frame we must record that we received it */
1296 status = scic_sds_unsolicited_frame_control_get_header(
1297 &(scic_sds_request_get_controller(sci_req)->uf_control),
1298 frame_index,
1299 (void **)&frame_header
1300 );
1301
1302 if (frame_header->frame_type == SCI_SAS_RESPONSE_FRAME) {
1303 struct sci_ssp_response_iu *response_buffer;
1304
1305 status = scic_sds_unsolicited_frame_control_get_buffer(
1306 &(scic_sds_request_get_controller(sci_req)->uf_control),
1307 frame_index,
1308 (void **)&response_buffer
1309 );
1310
1311 scic_word_copy_with_swap(
1312 sci_req->response_buffer,
1313 (u32 *)response_buffer,
1314 sizeof(struct sci_ssp_response_iu)
1315 );
1316
1317 response_buffer = (struct sci_ssp_response_iu *)sci_req->response_buffer;
1318
1319 if ((response_buffer->data_present == 0x01) ||
1320 (response_buffer->data_present == 0x02)) {
1321 scic_sds_request_set_status(
1322 sci_req,
1323 SCU_TASK_DONE_CHECK_RESPONSE,
1324 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1325 );
1326 } else
1327 scic_sds_request_set_status(
1328 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1329 );
1330 } else
1331 /* This was not a response frame why did it get forwarded? */
1332 dev_err(scic_to_dev(sci_req->owning_controller),
1333 "%s: SCIC IO Request 0x%p received unexpected "
1334 "frame %d type 0x%02x\n",
1335 __func__,
1336 sci_req,
1337 frame_index,
1338 frame_header->frame_type);
1339
1340 /*
1341 * In any case we are done with this frame buffer return it to the
1342 * controller */
1343 scic_sds_controller_release_frame(
1344 sci_req->owning_controller, frame_index
1345 );
1346
1347 return SCI_SUCCESS;
1348 }
1349
1350 /*
1351 * *****************************************************************************
1352 * * COMPLETED STATE HANDLERS
1353 * ***************************************************************************** */
1354
1355
1356 /*
1357 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1358 * object receives a scic_sds_request_complete() request. This method frees up
1359 * any io request resources that have been allocated and transitions the
1360 * request to its final state. Consider stopping the state machine instead of
1361 * transitioning to the final state? enum sci_status SCI_SUCCESS
1362 */
1363 static enum sci_status scic_sds_request_completed_state_complete_handler(
1364 struct scic_sds_request *request)
1365 {
1366 if (request->was_tag_assigned_by_user != true) {
1367 scic_controller_free_io_tag(
1368 request->owning_controller, request->io_tag);
1369 }
1370
1371 if (request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
1372 scic_sds_controller_release_frame(
1373 request->owning_controller, request->saved_rx_frame_index);
1374 }
1375
1376 sci_base_state_machine_change_state(&request->state_machine,
1377 SCI_BASE_REQUEST_STATE_FINAL);
1378 return SCI_SUCCESS;
1379 }
1380
1381 /*
1382 * *****************************************************************************
1383 * * ABORTING STATE HANDLERS
1384 * ***************************************************************************** */
1385
1386 /*
1387 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1388 * object receives a scic_sds_request_terminate() request. This method is the
1389 * io request aborting state abort handlers. On receipt of a multiple
1390 * terminate requests the io request will transition to the completed state.
1391 * This should not happen in normal operation. enum sci_status SCI_SUCCESS
1392 */
1393 static enum sci_status scic_sds_request_aborting_state_abort_handler(
1394 struct scic_sds_request *request)
1395 {
1396 sci_base_state_machine_change_state(&request->state_machine,
1397 SCI_BASE_REQUEST_STATE_COMPLETED);
1398 return SCI_SUCCESS;
1399 }
1400
1401 /*
1402 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1403 * object receives a scic_sds_request_task_completion() request. This method
1404 * decodes the completion type waiting for the abort task complete
1405 * notification. When the abort task complete is received the io request
1406 * transitions to the completed state. enum sci_status SCI_SUCCESS
1407 */
1408 static enum sci_status scic_sds_request_aborting_state_tc_completion_handler(
1409 struct scic_sds_request *sci_req,
1410 u32 completion_code)
1411 {
1412 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1413 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1414 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1415 scic_sds_request_set_status(
1416 sci_req, SCU_TASK_DONE_TASK_ABORT, SCI_FAILURE_IO_TERMINATED
1417 );
1418
1419 sci_base_state_machine_change_state(&sci_req->state_machine,
1420 SCI_BASE_REQUEST_STATE_COMPLETED);
1421 break;
1422
1423 default:
1424 /*
1425 * Unless we get some strange error wait for the task abort to complete
1426 * TODO: Should there be a state change for this completion? */
1427 break;
1428 }
1429
1430 return SCI_SUCCESS;
1431 }
1432
1433 /*
1434 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1435 * object receives a scic_sds_request_frame_handler() request. This method
1436 * discards the unsolicited frame since we are waiting for the abort task
1437 * completion. enum sci_status SCI_SUCCESS
1438 */
1439 static enum sci_status scic_sds_request_aborting_state_frame_handler(
1440 struct scic_sds_request *sci_req,
1441 u32 frame_index)
1442 {
1443 /* TODO: Is it even possible to get an unsolicited frame in the aborting state? */
1444
1445 scic_sds_controller_release_frame(
1446 sci_req->owning_controller, frame_index);
1447
1448 return SCI_SUCCESS;
1449 }
1450
1451 static const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
1452 [SCI_BASE_REQUEST_STATE_INITIAL] = {
1453 },
1454 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
1455 .start_handler = scic_sds_request_constructed_state_start_handler,
1456 .abort_handler = scic_sds_request_constructed_state_abort_handler,
1457 },
1458 [SCI_BASE_REQUEST_STATE_STARTED] = {
1459 .abort_handler = scic_sds_request_started_state_abort_handler,
1460 .tc_completion_handler = scic_sds_request_started_state_tc_completion_handler,
1461 .frame_handler = scic_sds_request_started_state_frame_handler,
1462 },
1463 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
1464 .complete_handler = scic_sds_request_completed_state_complete_handler,
1465 },
1466 [SCI_BASE_REQUEST_STATE_ABORTING] = {
1467 .abort_handler = scic_sds_request_aborting_state_abort_handler,
1468 .tc_completion_handler = scic_sds_request_aborting_state_tc_completion_handler,
1469 .frame_handler = scic_sds_request_aborting_state_frame_handler,
1470 },
1471 [SCI_BASE_REQUEST_STATE_FINAL] = {
1472 },
1473 };
1474
1475 /**
1476 * scic_sds_request_initial_state_enter() -
1477 * @object: This parameter specifies the base object for which the state
1478 * transition is occurring.
1479 *
1480 * This method implements the actions taken when entering the
1481 * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
1482 * base request is constructed. Entry into the initial state sets all handlers
1483 * for the io request object to their default handlers. none
1484 */
1485 static void scic_sds_request_initial_state_enter(void *object)
1486 {
1487 struct scic_sds_request *sci_req = object;
1488
1489 SET_STATE_HANDLER(
1490 sci_req,
1491 scic_sds_request_state_handler_table,
1492 SCI_BASE_REQUEST_STATE_INITIAL
1493 );
1494 }
1495
1496 /**
1497 * scic_sds_request_constructed_state_enter() -
1498 * @object: The io request object that is to enter the constructed state.
1499 *
1500 * This method implements the actions taken when entering the
1501 * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
1502 * for the the constructed state. none
1503 */
1504 static void scic_sds_request_constructed_state_enter(void *object)
1505 {
1506 struct scic_sds_request *sci_req = object;
1507
1508 SET_STATE_HANDLER(
1509 sci_req,
1510 scic_sds_request_state_handler_table,
1511 SCI_BASE_REQUEST_STATE_CONSTRUCTED
1512 );
1513 }
1514
1515 /**
1516 * scic_sds_request_started_state_enter() -
1517 * @object: This parameter specifies the base object for which the state
1518 * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
1519 *
1520 * This method implements the actions taken when entering the
1521 * SCI_BASE_REQUEST_STATE_STARTED state. If the io request object type is a
1522 * SCSI Task request we must enter the started substate machine. none
1523 */
1524 static void scic_sds_request_started_state_enter(void *object)
1525 {
1526 struct scic_sds_request *sci_req = object;
1527
1528 SET_STATE_HANDLER(
1529 sci_req,
1530 scic_sds_request_state_handler_table,
1531 SCI_BASE_REQUEST_STATE_STARTED
1532 );
1533
1534 /*
1535 * Most of the request state machines have a started substate machine so
1536 * start its execution on the entry to the started state. */
1537 if (sci_req->has_started_substate_machine == true)
1538 sci_base_state_machine_start(&sci_req->started_substate_machine);
1539 }
1540
1541 /**
1542 * scic_sds_request_started_state_exit() -
1543 * @object: This parameter specifies the base object for which the state
1544 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1545 * object.
1546 *
1547 * This method implements the actions taken when exiting the
1548 * SCI_BASE_REQUEST_STATE_STARTED state. For task requests the action will be
1549 * to stop the started substate machine. none
1550 */
1551 static void scic_sds_request_started_state_exit(void *object)
1552 {
1553 struct scic_sds_request *sci_req = object;
1554
1555 if (sci_req->has_started_substate_machine == true)
1556 sci_base_state_machine_stop(&sci_req->started_substate_machine);
1557 }
1558
1559 /**
1560 * scic_sds_request_completed_state_enter() -
1561 * @object: This parameter specifies the base object for which the state
1562 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1563 * object.
1564 *
1565 * This method implements the actions taken when entering the
1566 * SCI_BASE_REQUEST_STATE_COMPLETED state. This state is entered when the
1567 * SCIC_SDS_IO_REQUEST has completed. The method will decode the request
1568 * completion status and convert it to an enum sci_status to return in the
1569 * completion callback function. none
1570 */
1571 static void scic_sds_request_completed_state_enter(void *object)
1572 {
1573 struct scic_sds_request *sci_req = object;
1574 struct scic_sds_controller *scic =
1575 scic_sds_request_get_controller(sci_req);
1576 struct isci_host *ihost = scic->ihost;
1577 struct isci_request *ireq = sci_req->ireq;
1578
1579 SET_STATE_HANDLER(sci_req,
1580 scic_sds_request_state_handler_table,
1581 SCI_BASE_REQUEST_STATE_COMPLETED);
1582
1583 /* Tell the SCI_USER that the IO request is complete */
1584 if (sci_req->is_task_management_request == false)
1585 isci_request_io_request_complete(ihost,
1586 ireq,
1587 sci_req->sci_status);
1588 else
1589 isci_task_request_complete(ihost, ireq, sci_req->sci_status);
1590 }
1591
1592 /**
1593 * scic_sds_request_aborting_state_enter() -
1594 * @object: This parameter specifies the base object for which the state
1595 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1596 * object.
1597 *
1598 * This method implements the actions taken when entering the
1599 * SCI_BASE_REQUEST_STATE_ABORTING state. none
1600 */
1601 static void scic_sds_request_aborting_state_enter(void *object)
1602 {
1603 struct scic_sds_request *sci_req = object;
1604
1605 /* Setting the abort bit in the Task Context is required by the silicon. */
1606 sci_req->task_context_buffer->abort = 1;
1607
1608 SET_STATE_HANDLER(
1609 sci_req,
1610 scic_sds_request_state_handler_table,
1611 SCI_BASE_REQUEST_STATE_ABORTING
1612 );
1613 }
1614
1615 /**
1616 * scic_sds_request_final_state_enter() -
1617 * @object: This parameter specifies the base object for which the state
1618 * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
1619 *
1620 * This method implements the actions taken when entering the
1621 * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
1622 * state handlers in place. none
1623 */
1624 static void scic_sds_request_final_state_enter(void *object)
1625 {
1626 struct scic_sds_request *sci_req = object;
1627
1628 SET_STATE_HANDLER(
1629 sci_req,
1630 scic_sds_request_state_handler_table,
1631 SCI_BASE_REQUEST_STATE_FINAL
1632 );
1633 }
1634
1635 static const struct sci_base_state scic_sds_request_state_table[] = {
1636 [SCI_BASE_REQUEST_STATE_INITIAL] = {
1637 .enter_state = scic_sds_request_initial_state_enter,
1638 },
1639 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
1640 .enter_state = scic_sds_request_constructed_state_enter,
1641 },
1642 [SCI_BASE_REQUEST_STATE_STARTED] = {
1643 .enter_state = scic_sds_request_started_state_enter,
1644 .exit_state = scic_sds_request_started_state_exit
1645 },
1646 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
1647 .enter_state = scic_sds_request_completed_state_enter,
1648 },
1649 [SCI_BASE_REQUEST_STATE_ABORTING] = {
1650 .enter_state = scic_sds_request_aborting_state_enter,
1651 },
1652 [SCI_BASE_REQUEST_STATE_FINAL] = {
1653 .enter_state = scic_sds_request_final_state_enter,
1654 },
1655 };
1656
1657 static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
1658 struct scic_sds_remote_device *sci_dev,
1659 u16 io_tag,
1660 void *user_io_request_object,
1661 struct scic_sds_request *sci_req)
1662 {
1663 sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
1664 scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
1665 sci_base_state_machine_start(&sci_req->state_machine);
1666
1667 sci_req->io_tag = io_tag;
1668 sci_req->user_request = user_io_request_object;
1669 sci_req->owning_controller = scic;
1670 sci_req->target_device = sci_dev;
1671 sci_req->has_started_substate_machine = false;
1672 sci_req->protocol = SCIC_NO_PROTOCOL;
1673 sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
1674 sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
1675
1676 sci_req->sci_status = SCI_SUCCESS;
1677 sci_req->scu_status = 0;
1678 sci_req->post_context = 0xFFFFFFFF;
1679
1680 sci_req->is_task_management_request = false;
1681
1682 if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1683 sci_req->was_tag_assigned_by_user = false;
1684 sci_req->task_context_buffer = NULL;
1685 } else {
1686 sci_req->was_tag_assigned_by_user = true;
1687
1688 sci_req->task_context_buffer =
1689 scic_sds_controller_get_task_context_buffer(scic, io_tag);
1690 }
1691 }
1692
1693 enum sci_status scic_io_request_construct(struct scic_sds_controller *scic,
1694 struct scic_sds_remote_device *sci_dev,
1695 u16 io_tag,
1696 void *user_io_request_object,
1697 struct scic_sds_request *sci_req,
1698 struct scic_sds_request **new_scic_io_request_handle)
1699 {
1700 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1701 enum sci_status status = SCI_SUCCESS;
1702
1703 /* Build the common part of the request */
1704 scic_sds_general_request_construct(scic, sci_dev, io_tag,
1705 user_io_request_object, sci_req);
1706
1707 if (sci_dev->rnc.remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
1708 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
1709
1710 if (dev->dev_type == SAS_END_DEV) {
1711 scic_sds_ssp_io_request_assign_buffers(sci_req);
1712 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
1713 scic_sds_stp_request_assign_buffers(sci_req);
1714 memset(sci_req->command_buffer, 0, sizeof(struct host_to_dev_fis));
1715 } else if (dev_is_expander(dev)) {
1716 scic_sds_smp_request_assign_buffers(sci_req);
1717 memset(sci_req->command_buffer, 0, sizeof(struct smp_request));
1718 } else
1719 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1720
1721 if (status == SCI_SUCCESS) {
1722 memset(sci_req->task_context_buffer, 0,
1723 SCI_FIELD_OFFSET(struct scu_task_context, sgl_pair_ab));
1724 *new_scic_io_request_handle = sci_req;
1725 }
1726
1727 return status;
1728 }
1729
1730 enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
1731 struct scic_sds_remote_device *sci_dev,
1732 u16 io_tag,
1733 void *user_io_request_object,
1734 struct scic_sds_request *sci_req,
1735 struct scic_sds_request **new_sci_req)
1736 {
1737 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1738 enum sci_status status = SCI_SUCCESS;
1739
1740 /* Build the common part of the request */
1741 scic_sds_general_request_construct(scic, sci_dev, io_tag,
1742 user_io_request_object,
1743 sci_req);
1744
1745 if (dev->dev_type == SAS_END_DEV) {
1746 scic_sds_ssp_task_request_assign_buffers(sci_req);
1747
1748 sci_req->has_started_substate_machine = true;
1749
1750 /* Construct the started sub-state machine. */
1751 sci_base_state_machine_construct(
1752 &sci_req->started_substate_machine,
1753 sci_req,
1754 scic_sds_io_request_started_task_mgmt_substate_table,
1755 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
1756 );
1757 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1758 scic_sds_stp_request_assign_buffers(sci_req);
1759 else
1760 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1761
1762 if (status == SCI_SUCCESS) {
1763 sci_req->is_task_management_request = true;
1764 memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
1765 *new_sci_req = sci_req;
1766 }
1767
1768 return status;
1769 }
This page took 0.082657 seconds and 5 git commands to generate.