isci: Remove SCIC_SWAP_DWORD()
[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 + SSP_RESP_IU_MAX_SIZE \
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 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 + SSP_RESP_IU_MAX_SIZE \
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 + SSP_RESP_IU_MAX_SIZE \
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 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 + SSP_RESP_IU_MAX_SIZE \
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 sci_swab32_cpy(&cmd_iu->cdb, task->ssp_task.cdb,
366 sizeof(task->ssp_task.cdb) / sizeof(u32));
367 }
368
369 static void scic_sds_task_request_build_ssp_task_iu(struct scic_sds_request *sci_req)
370 {
371 struct ssp_task_iu *task_iu;
372 struct isci_request *ireq = sci_req->ireq;
373 struct sas_task *task = isci_request_access_task(ireq);
374 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
375
376 task_iu = sci_req->command_buffer;
377
378 memset(task_iu, 0, sizeof(struct ssp_task_iu));
379
380 memcpy(task_iu->LUN, task->ssp_task.LUN, 8);
381
382 task_iu->task_func = isci_tmf->tmf_code;
383 task_iu->task_tag =
384 (ireq->ttype == tmf_task) ?
385 isci_tmf->io_tag :
386 SCI_CONTROLLER_INVALID_IO_TAG;
387 }
388
389 /**
390 * This method is will fill in the SCU Task Context for any type of SSP request.
391 * @sci_req:
392 * @task_context:
393 *
394 */
395 static void scu_ssp_reqeust_construct_task_context(
396 struct scic_sds_request *sds_request,
397 struct scu_task_context *task_context)
398 {
399 dma_addr_t dma_addr;
400 struct scic_sds_controller *controller;
401 struct scic_sds_remote_device *target_device;
402 struct scic_sds_port *target_port;
403
404 controller = scic_sds_request_get_controller(sds_request);
405 target_device = scic_sds_request_get_device(sds_request);
406 target_port = scic_sds_request_get_port(sds_request);
407
408 /* Fill in the TC with the its required data */
409 task_context->abort = 0;
410 task_context->priority = 0;
411 task_context->initiator_request = 1;
412 task_context->connection_rate = target_device->connection_rate;
413 task_context->protocol_engine_index =
414 scic_sds_controller_get_protocol_engine_group(controller);
415 task_context->logical_port_index =
416 scic_sds_port_get_index(target_port);
417 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
418 task_context->valid = SCU_TASK_CONTEXT_VALID;
419 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
420
421 task_context->remote_node_index =
422 scic_sds_remote_device_get_index(sds_request->target_device);
423 task_context->command_code = 0;
424
425 task_context->link_layer_control = 0;
426 task_context->do_not_dma_ssp_good_response = 1;
427 task_context->strict_ordering = 0;
428 task_context->control_frame = 0;
429 task_context->timeout_enable = 0;
430 task_context->block_guard_enable = 0;
431
432 task_context->address_modifier = 0;
433
434 /* task_context->type.ssp.tag = sci_req->io_tag; */
435 task_context->task_phase = 0x01;
436
437 if (sds_request->was_tag_assigned_by_user) {
438 /*
439 * Build the task context now since we have already read
440 * the data
441 */
442 sds_request->post_context =
443 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
444 (scic_sds_controller_get_protocol_engine_group(
445 controller) <<
446 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
447 (scic_sds_port_get_index(target_port) <<
448 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
449 scic_sds_io_tag_get_index(sds_request->io_tag));
450 } else {
451 /*
452 * Build the task context now since we have already read
453 * the data
454 *
455 * I/O tag index is not assigned because we have to wait
456 * until we get a TCi
457 */
458 sds_request->post_context =
459 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
460 (scic_sds_controller_get_protocol_engine_group(
461 owning_controller) <<
462 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
463 (scic_sds_port_get_index(target_port) <<
464 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
465 }
466
467 /*
468 * Copy the physical address for the command buffer to the
469 * SCU Task Context
470 */
471 dma_addr = scic_io_request_get_dma_addr(sds_request,
472 sds_request->command_buffer);
473
474 task_context->command_iu_upper = upper_32_bits(dma_addr);
475 task_context->command_iu_lower = lower_32_bits(dma_addr);
476
477 /*
478 * Copy the physical address for the response buffer to the
479 * SCU Task Context
480 */
481 dma_addr = scic_io_request_get_dma_addr(sds_request,
482 sds_request->response_buffer);
483
484 task_context->response_iu_upper = upper_32_bits(dma_addr);
485 task_context->response_iu_lower = lower_32_bits(dma_addr);
486 }
487
488 /**
489 * This method is will fill in the SCU Task Context for a SSP IO request.
490 * @sci_req:
491 *
492 */
493 static void scu_ssp_io_request_construct_task_context(
494 struct scic_sds_request *sci_req,
495 enum dma_data_direction dir,
496 u32 len)
497 {
498 struct scu_task_context *task_context;
499
500 task_context = scic_sds_request_get_task_context(sci_req);
501
502 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
503
504 task_context->ssp_command_iu_length =
505 sizeof(struct ssp_cmd_iu) / sizeof(u32);
506 task_context->type.ssp.frame_type = SCI_SAS_COMMAND_FRAME;
507
508 switch (dir) {
509 case DMA_FROM_DEVICE:
510 case DMA_NONE:
511 default:
512 task_context->task_type = SCU_TASK_TYPE_IOREAD;
513 break;
514 case DMA_TO_DEVICE:
515 task_context->task_type = SCU_TASK_TYPE_IOWRITE;
516 break;
517 }
518
519 task_context->transfer_length_bytes = len;
520
521 if (task_context->transfer_length_bytes > 0)
522 scic_sds_request_build_sgl(sci_req);
523 }
524
525
526 /**
527 * This method will fill in the remainder of the io request object for SSP Task
528 * requests.
529 * @sci_req:
530 *
531 */
532 static void scic_sds_ssp_task_request_assign_buffers(
533 struct scic_sds_request *sci_req)
534 {
535 /* Assign all of the buffer pointers */
536 sci_req->command_buffer =
537 scic_sds_ssp_task_request_get_command_buffer(sci_req);
538 sci_req->response_buffer =
539 scic_sds_ssp_task_request_get_response_buffer(sci_req);
540 sci_req->sgl_element_pair_buffer = NULL;
541
542 if (sci_req->was_tag_assigned_by_user == false) {
543 sci_req->task_context_buffer =
544 scic_sds_ssp_task_request_get_task_context_buffer(sci_req);
545 sci_req->task_context_buffer =
546 PTR_ALIGN(sci_req->task_context_buffer, SMP_CACHE_BYTES);
547 }
548 }
549
550 /**
551 * This method will fill in the SCU Task Context for a SSP Task request. The
552 * following important settings are utilized: -# priority ==
553 * SCU_TASK_PRIORITY_HIGH. This ensures that the task request is issued
554 * ahead of other task destined for the same Remote Node. -# task_type ==
555 * SCU_TASK_TYPE_IOREAD. This simply indicates that a normal request type
556 * (i.e. non-raw frame) is being utilized to perform task management. -#
557 * control_frame == 1. This ensures that the proper endianess is set so
558 * that the bytes are transmitted in the right order for a task frame.
559 * @sci_req: This parameter specifies the task request object being
560 * constructed.
561 *
562 */
563 static void scu_ssp_task_request_construct_task_context(
564 struct scic_sds_request *sci_req)
565 {
566 struct scu_task_context *task_context;
567
568 task_context = scic_sds_request_get_task_context(sci_req);
569
570 scu_ssp_reqeust_construct_task_context(sci_req, task_context);
571
572 task_context->control_frame = 1;
573 task_context->priority = SCU_TASK_PRIORITY_HIGH;
574 task_context->task_type = SCU_TASK_TYPE_RAW_FRAME;
575 task_context->transfer_length_bytes = 0;
576 task_context->type.ssp.frame_type = SCI_SAS_TASK_FRAME;
577 task_context->ssp_command_iu_length =
578 sizeof(struct ssp_task_iu) / sizeof(u32);
579 }
580
581
582 /**
583 * This method constructs the SSP Command IU data for this ssp passthrough
584 * comand request object.
585 * @sci_req: This parameter specifies the request object for which the SSP
586 * command information unit is being built.
587 *
588 * enum sci_status, returns invalid parameter is cdb > 16
589 */
590
591
592 /**
593 * This method constructs the SATA request object.
594 * @sci_req:
595 * @sat_protocol:
596 * @transfer_length:
597 * @data_direction:
598 * @copy_rx_frame:
599 *
600 * enum sci_status
601 */
602 static enum sci_status
603 scic_io_request_construct_sata(struct scic_sds_request *sci_req,
604 u32 len,
605 enum dma_data_direction dir,
606 bool copy)
607 {
608 enum sci_status status = SCI_SUCCESS;
609 struct isci_request *ireq = sci_req->ireq;
610 struct sas_task *task = isci_request_access_task(ireq);
611
612 /* check for management protocols */
613 if (ireq->ttype == tmf_task) {
614 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
615
616 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
617 tmf->tmf_code == isci_tmf_sata_srst_low)
618 return scic_sds_stp_soft_reset_request_construct(sci_req);
619 else {
620 dev_err(scic_to_dev(sci_req->owning_controller),
621 "%s: Request 0x%p received un-handled SAT "
622 "management protocol 0x%x.\n",
623 __func__, sci_req, tmf->tmf_code);
624
625 return SCI_FAILURE;
626 }
627 }
628
629 if (!sas_protocol_ata(task->task_proto)) {
630 dev_err(scic_to_dev(sci_req->owning_controller),
631 "%s: Non-ATA protocol in SATA path: 0x%x\n",
632 __func__,
633 task->task_proto);
634 return SCI_FAILURE;
635
636 }
637
638 /* non data */
639 if (task->data_dir == DMA_NONE)
640 return scic_sds_stp_non_data_request_construct(sci_req);
641
642 /* NCQ */
643 if (task->ata_task.use_ncq)
644 return scic_sds_stp_ncq_request_construct(sci_req, len, dir);
645
646 /* DMA */
647 if (task->ata_task.dma_xfer)
648 return scic_sds_stp_udma_request_construct(sci_req, len, dir);
649 else /* PIO */
650 return scic_sds_stp_pio_request_construct(sci_req, copy);
651
652 return status;
653 }
654
655 u32 scic_io_request_get_object_size(void)
656 {
657 u32 ssp_request_size;
658 u32 stp_request_size;
659 u32 smp_request_size;
660
661 ssp_request_size = scic_sds_ssp_request_get_object_size();
662 stp_request_size = scic_sds_stp_request_get_object_size();
663 smp_request_size = scic_sds_smp_request_get_object_size();
664
665 return max(ssp_request_size, max(stp_request_size, smp_request_size));
666 }
667
668 enum sci_status scic_io_request_construct_basic_ssp(
669 struct scic_sds_request *sci_req)
670 {
671 struct isci_request *isci_request = sci_req->ireq;
672
673 sci_req->protocol = SCIC_SSP_PROTOCOL;
674
675 scu_ssp_io_request_construct_task_context(
676 sci_req,
677 isci_request_io_request_get_data_direction(isci_request),
678 isci_request_io_request_get_transfer_length(isci_request));
679
680 scic_sds_io_request_build_ssp_command_iu(sci_req);
681
682 sci_base_state_machine_change_state(&sci_req->state_machine,
683 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
684
685 return SCI_SUCCESS;
686 }
687
688
689 enum sci_status scic_task_request_construct_ssp(
690 struct scic_sds_request *sci_req)
691 {
692 /* Construct the SSP Task SCU Task Context */
693 scu_ssp_task_request_construct_task_context(sci_req);
694
695 /* Fill in the SSP Task IU */
696 scic_sds_task_request_build_ssp_task_iu(sci_req);
697
698 sci_base_state_machine_change_state(&sci_req->state_machine,
699 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
700
701 return SCI_SUCCESS;
702 }
703
704
705 enum sci_status scic_io_request_construct_basic_sata(
706 struct scic_sds_request *sci_req)
707 {
708 enum sci_status status;
709 struct scic_sds_stp_request *stp_req;
710 u32 len;
711 enum dma_data_direction dir;
712 bool copy = false;
713 struct isci_request *isci_request = sci_req->ireq;
714 struct sas_task *task = isci_request_access_task(isci_request);
715
716 stp_req = container_of(sci_req, typeof(*stp_req), parent);
717
718 sci_req->protocol = SCIC_STP_PROTOCOL;
719
720 len = isci_request_io_request_get_transfer_length(isci_request);
721 dir = isci_request_io_request_get_data_direction(isci_request);
722 copy = (task->data_dir == DMA_NONE) ? false : true;
723
724 status = scic_io_request_construct_sata(sci_req, len, dir, copy);
725
726 if (status == SCI_SUCCESS)
727 sci_base_state_machine_change_state(&sci_req->state_machine,
728 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
729
730 return status;
731 }
732
733
734 enum sci_status scic_task_request_construct_sata(
735 struct scic_sds_request *sci_req)
736 {
737 enum sci_status status = SCI_SUCCESS;
738 struct isci_request *ireq = sci_req->ireq;
739
740 /* check for management protocols */
741 if (ireq->ttype == tmf_task) {
742 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
743
744 if (tmf->tmf_code == isci_tmf_sata_srst_high ||
745 tmf->tmf_code == isci_tmf_sata_srst_low) {
746 status = scic_sds_stp_soft_reset_request_construct(sci_req);
747 } else {
748 dev_err(scic_to_dev(sci_req->owning_controller),
749 "%s: Request 0x%p received un-handled SAT "
750 "Protocol 0x%x.\n",
751 __func__, sci_req, tmf->tmf_code);
752
753 return SCI_FAILURE;
754 }
755 }
756
757 if (status == SCI_SUCCESS)
758 sci_base_state_machine_change_state(
759 &sci_req->state_machine,
760 SCI_BASE_REQUEST_STATE_CONSTRUCTED);
761
762 return status;
763 }
764
765
766 u16 scic_io_request_get_io_tag(
767 struct scic_sds_request *sci_req)
768 {
769 return sci_req->io_tag;
770 }
771
772
773 u32 scic_request_get_controller_status(
774 struct scic_sds_request *sci_req)
775 {
776 return sci_req->scu_status;
777 }
778
779
780 void *scic_io_request_get_command_iu_address(
781 struct scic_sds_request *sci_req)
782 {
783 return sci_req->command_buffer;
784 }
785
786
787 void *scic_io_request_get_response_iu_address(
788 struct scic_sds_request *sci_req)
789 {
790 return sci_req->response_buffer;
791 }
792
793
794 #define SCU_TASK_CONTEXT_SRAM 0x200000
795 u32 scic_io_request_get_number_of_bytes_transferred(
796 struct scic_sds_request *scic_sds_request)
797 {
798 struct scic_sds_controller *scic = scic_sds_request->owning_controller;
799 u32 ret_val = 0;
800
801 if (readl(&scic->smu_registers->address_modifier) == 0) {
802 void __iomem *scu_reg_base = scic->scu_registers;
803 /*
804 * get the bytes of data from the Address == BAR1 + 20002Ch + (256*TCi) where
805 * BAR1 is the scu_registers
806 * 0x20002C = 0x200000 + 0x2c
807 * = start of task context SRAM + offset of (type.ssp.data_offset)
808 * TCi is the io_tag of struct scic_sds_request */
809 ret_val = readl(scu_reg_base +
810 (SCU_TASK_CONTEXT_SRAM + offsetof(struct scu_task_context, type.ssp.data_offset)) +
811 ((sizeof(struct scu_task_context)) * scic_sds_io_tag_get_index(scic_sds_request->io_tag)));
812 }
813
814 return ret_val;
815 }
816
817
818 /*
819 * ****************************************************************************
820 * * SCIC SDS Interface Implementation
821 * **************************************************************************** */
822
823 enum sci_status
824 scic_sds_request_start(struct scic_sds_request *request)
825 {
826 if (request->device_sequence !=
827 scic_sds_remote_device_get_sequence(request->target_device))
828 return SCI_FAILURE;
829
830 if (request->state_handlers->start_handler)
831 return request->state_handlers->start_handler(request);
832
833 dev_warn(scic_to_dev(request->owning_controller),
834 "%s: SCIC IO Request requested to start while in wrong "
835 "state %d\n",
836 __func__,
837 sci_base_state_machine_get_state(&request->state_machine));
838
839 return SCI_FAILURE_INVALID_STATE;
840 }
841
842 enum sci_status
843 scic_sds_io_request_terminate(struct scic_sds_request *request)
844 {
845 if (request->state_handlers->abort_handler)
846 return request->state_handlers->abort_handler(request);
847
848 dev_warn(scic_to_dev(request->owning_controller),
849 "%s: SCIC IO Request requested to abort while in wrong "
850 "state %d\n",
851 __func__,
852 sci_base_state_machine_get_state(&request->state_machine));
853
854 return SCI_FAILURE_INVALID_STATE;
855 }
856
857 enum sci_status
858 scic_sds_io_request_complete(struct scic_sds_request *request)
859 {
860 if (request->state_handlers->complete_handler)
861 return request->state_handlers->complete_handler(request);
862
863 dev_warn(scic_to_dev(request->owning_controller),
864 "%s: SCIC IO Request requested to complete while in wrong "
865 "state %d\n",
866 __func__,
867 sci_base_state_machine_get_state(&request->state_machine));
868
869 return SCI_FAILURE_INVALID_STATE;
870 }
871
872 enum sci_status scic_sds_io_request_event_handler(
873 struct scic_sds_request *request,
874 u32 event_code)
875 {
876 if (request->state_handlers->event_handler)
877 return request->state_handlers->event_handler(request, event_code);
878
879 dev_warn(scic_to_dev(request->owning_controller),
880 "%s: SCIC IO Request given event code notification %x while "
881 "in wrong state %d\n",
882 __func__,
883 event_code,
884 sci_base_state_machine_get_state(&request->state_machine));
885
886 return SCI_FAILURE_INVALID_STATE;
887 }
888
889 enum sci_status
890 scic_sds_io_request_tc_completion(struct scic_sds_request *request, u32 completion_code)
891 {
892 if (request->state_machine.current_state_id == SCI_BASE_REQUEST_STATE_STARTED &&
893 request->has_started_substate_machine == false)
894 return scic_sds_request_started_state_tc_completion_handler(request, completion_code);
895 else if (request->state_handlers->tc_completion_handler)
896 return request->state_handlers->tc_completion_handler(request, completion_code);
897
898 dev_warn(scic_to_dev(request->owning_controller),
899 "%s: SCIC IO Request given task completion notification %x "
900 "while in wrong state %d\n",
901 __func__,
902 completion_code,
903 sci_base_state_machine_get_state(&request->state_machine));
904
905 return SCI_FAILURE_INVALID_STATE;
906
907 }
908
909
910 /**
911 *
912 * @sci_req: The SCIC_SDS_IO_REQUEST_T object for which the start
913 * operation is to be executed.
914 * @frame_index: The frame index returned by the hardware for the reqeust
915 * object.
916 *
917 * This method invokes the core state frame handler for the
918 * SCIC_SDS_IO_REQUEST_T object. enum sci_status
919 */
920 enum sci_status scic_sds_io_request_frame_handler(
921 struct scic_sds_request *request,
922 u32 frame_index)
923 {
924 if (request->state_handlers->frame_handler)
925 return request->state_handlers->frame_handler(request, frame_index);
926
927 dev_warn(scic_to_dev(request->owning_controller),
928 "%s: SCIC IO Request given unexpected frame %x while in "
929 "state %d\n",
930 __func__,
931 frame_index,
932 sci_base_state_machine_get_state(&request->state_machine));
933
934 scic_sds_controller_release_frame(request->owning_controller, frame_index);
935 return SCI_FAILURE_INVALID_STATE;
936 }
937
938 /*
939 * This function copies response data for requests returning response data
940 * instead of sense data.
941 * @sci_req: This parameter specifies the request object for which to copy
942 * the response data.
943 */
944 void scic_sds_io_request_copy_response(struct scic_sds_request *sci_req)
945 {
946 void *resp_buf;
947 u32 len;
948 struct ssp_response_iu *ssp_response;
949 struct isci_request *ireq = sci_req->ireq;
950 struct isci_tmf *isci_tmf = isci_request_access_tmf(ireq);
951
952 ssp_response = sci_req->response_buffer;
953
954 resp_buf = &isci_tmf->resp.resp_iu;
955
956 len = min_t(u32,
957 SSP_RESP_IU_MAX_SIZE,
958 be32_to_cpu(ssp_response->response_data_len));
959
960 memcpy(resp_buf, ssp_response->resp_data, len);
961 }
962
963 /*
964 * *****************************************************************************
965 * * CONSTRUCTED STATE HANDLERS
966 * ***************************************************************************** */
967
968 /*
969 * This method implements the action taken when a constructed
970 * SCIC_SDS_IO_REQUEST_T object receives a scic_sds_request_start() request.
971 * This method will, if necessary, allocate a TCi for the io request object and
972 * then will, if necessary, copy the constructed TC data into the actual TC
973 * buffer. If everything is successful the post context field is updated with
974 * the TCi so the controller can post the request to the hardware. enum sci_status
975 * SCI_SUCCESS SCI_FAILURE_INSUFFICIENT_RESOURCES
976 */
977 static enum sci_status scic_sds_request_constructed_state_start_handler(
978 struct scic_sds_request *request)
979 {
980 struct scu_task_context *task_context;
981
982 if (request->io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
983 request->io_tag =
984 scic_controller_allocate_io_tag(request->owning_controller);
985 }
986
987 /* Record the IO Tag in the request */
988 if (request->io_tag != SCI_CONTROLLER_INVALID_IO_TAG) {
989 task_context = request->task_context_buffer;
990
991 task_context->task_index = scic_sds_io_tag_get_index(request->io_tag);
992
993 switch (task_context->protocol_type) {
994 case SCU_TASK_CONTEXT_PROTOCOL_SMP:
995 case SCU_TASK_CONTEXT_PROTOCOL_SSP:
996 /* SSP/SMP Frame */
997 task_context->type.ssp.tag = request->io_tag;
998 task_context->type.ssp.target_port_transfer_tag = 0xFFFF;
999 break;
1000
1001 case SCU_TASK_CONTEXT_PROTOCOL_STP:
1002 /*
1003 * STP/SATA Frame
1004 * task_context->type.stp.ncq_tag = request->ncq_tag; */
1005 break;
1006
1007 case SCU_TASK_CONTEXT_PROTOCOL_NONE:
1008 /* / @todo When do we set no protocol type? */
1009 break;
1010
1011 default:
1012 /* This should never happen since we build the IO requests */
1013 break;
1014 }
1015
1016 /*
1017 * Check to see if we need to copy the task context buffer
1018 * or have been building into the task context buffer */
1019 if (request->was_tag_assigned_by_user == false) {
1020 scic_sds_controller_copy_task_context(
1021 request->owning_controller, request);
1022 }
1023
1024 /* Add to the post_context the io tag value */
1025 request->post_context |= scic_sds_io_tag_get_index(request->io_tag);
1026
1027 /* Everything is good go ahead and change state */
1028 sci_base_state_machine_change_state(&request->state_machine,
1029 SCI_BASE_REQUEST_STATE_STARTED);
1030
1031 return SCI_SUCCESS;
1032 }
1033
1034 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
1035 }
1036
1037 /*
1038 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1039 * object receives a scic_sds_request_terminate() request. Since the request
1040 * has not yet been posted to the hardware the request transitions to the
1041 * completed state. enum sci_status SCI_SUCCESS
1042 */
1043 static enum sci_status scic_sds_request_constructed_state_abort_handler(
1044 struct scic_sds_request *request)
1045 {
1046 /*
1047 * This request has been terminated by the user make sure that the correct
1048 * status code is returned */
1049 scic_sds_request_set_status(request,
1050 SCU_TASK_DONE_TASK_ABORT,
1051 SCI_FAILURE_IO_TERMINATED);
1052
1053 sci_base_state_machine_change_state(&request->state_machine,
1054 SCI_BASE_REQUEST_STATE_COMPLETED);
1055 return SCI_SUCCESS;
1056 }
1057
1058 /*
1059 * *****************************************************************************
1060 * * STARTED STATE HANDLERS
1061 * ***************************************************************************** */
1062
1063 /*
1064 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1065 * object receives a scic_sds_request_terminate() request. Since the request
1066 * has been posted to the hardware the io request state is changed to the
1067 * aborting state. enum sci_status SCI_SUCCESS
1068 */
1069 enum sci_status scic_sds_request_started_state_abort_handler(
1070 struct scic_sds_request *request)
1071 {
1072 if (request->has_started_substate_machine)
1073 sci_base_state_machine_stop(&request->started_substate_machine);
1074
1075 sci_base_state_machine_change_state(&request->state_machine,
1076 SCI_BASE_REQUEST_STATE_ABORTING);
1077 return SCI_SUCCESS;
1078 }
1079
1080 /*
1081 * scic_sds_request_started_state_tc_completion_handler() - This method process
1082 * TC (task context) completions for normal IO request (i.e. Task/Abort
1083 * Completions of type 0). This method will update the
1084 * SCIC_SDS_IO_REQUEST_T::status field.
1085 * @sci_req: This parameter specifies the request for which a completion
1086 * occurred.
1087 * @completion_code: This parameter specifies the completion code received from
1088 * the SCU.
1089 *
1090 */
1091 enum sci_status
1092 scic_sds_request_started_state_tc_completion_handler(
1093 struct scic_sds_request *sci_req,
1094 u32 completion_code)
1095 {
1096 u8 datapres;
1097 struct ssp_response_iu *resp_iu;
1098
1099 /*
1100 * TODO: Any SDMA return code of other than 0 is bad
1101 * decode 0x003C0000 to determine SDMA status
1102 */
1103 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1104 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1105 scic_sds_request_set_status(sci_req,
1106 SCU_TASK_DONE_GOOD,
1107 SCI_SUCCESS);
1108 break;
1109
1110 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EARLY_RESP):
1111 {
1112 /*
1113 * There are times when the SCU hardware will return an early
1114 * response because the io request specified more data than is
1115 * returned by the target device (mode pages, inquiry data,
1116 * etc.). We must check the response stats to see if this is
1117 * truly a failed request or a good request that just got
1118 * completed early.
1119 */
1120 struct ssp_response_iu *resp = sci_req->response_buffer;
1121 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1122
1123 sci_swab32_cpy(sci_req->response_buffer,
1124 sci_req->response_buffer,
1125 word_cnt);
1126
1127 if (resp->status == 0) {
1128 scic_sds_request_set_status(
1129 sci_req,
1130 SCU_TASK_DONE_GOOD,
1131 SCI_SUCCESS_IO_DONE_EARLY);
1132 } else {
1133 scic_sds_request_set_status(
1134 sci_req,
1135 SCU_TASK_DONE_CHECK_RESPONSE,
1136 SCI_FAILURE_IO_RESPONSE_VALID);
1137 }
1138 }
1139 break;
1140
1141 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CHECK_RESPONSE):
1142 {
1143 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1144
1145 sci_swab32_cpy(sci_req->response_buffer,
1146 sci_req->response_buffer,
1147 word_cnt);
1148
1149 scic_sds_request_set_status(sci_req,
1150 SCU_TASK_DONE_CHECK_RESPONSE,
1151 SCI_FAILURE_IO_RESPONSE_VALID);
1152 break;
1153 }
1154
1155 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RESP_LEN_ERR):
1156 /*
1157 * / @todo With TASK_DONE_RESP_LEN_ERR is the response frame
1158 * guaranteed to be received before this completion status is
1159 * posted?
1160 */
1161 resp_iu = sci_req->response_buffer;
1162 datapres = resp_iu->datapres;
1163
1164 if ((datapres == 0x01) || (datapres == 0x02)) {
1165 scic_sds_request_set_status(
1166 sci_req,
1167 SCU_TASK_DONE_CHECK_RESPONSE,
1168 SCI_FAILURE_IO_RESPONSE_VALID);
1169 } else
1170 scic_sds_request_set_status(
1171 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
1172 break;
1173
1174 /* only stp device gets suspended. */
1175 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_ACK_NAK_TO):
1176 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_PERR):
1177 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_ERR):
1178 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_DATA_LEN_ERR):
1179 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_ABORT_ERR):
1180 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_WD_LEN):
1181 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1182 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_RESP):
1183 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_SDBFIS):
1184 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1185 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDB_ERR):
1186 if (sci_req->protocol == SCIC_STP_PROTOCOL) {
1187 scic_sds_request_set_status(
1188 sci_req,
1189 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1190 SCU_COMPLETION_TL_STATUS_SHIFT,
1191 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
1192 } else {
1193 scic_sds_request_set_status(
1194 sci_req,
1195 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1196 SCU_COMPLETION_TL_STATUS_SHIFT,
1197 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1198 }
1199 break;
1200
1201 /* both stp/ssp device gets suspended */
1202 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LF_ERR):
1203 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_WRONG_DESTINATION):
1204 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_1):
1205 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_2):
1206 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_RESERVED_ABANDON_3):
1207 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_BAD_DESTINATION):
1208 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_ZONE_VIOLATION):
1209 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_STP_RESOURCES_BUSY):
1210 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_PROTOCOL_NOT_SUPPORTED):
1211 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_OPEN_REJECT_CONNECTION_RATE_NOT_SUPPORTED):
1212 scic_sds_request_set_status(
1213 sci_req,
1214 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1215 SCU_COMPLETION_TL_STATUS_SHIFT,
1216 SCI_FAILURE_REMOTE_DEVICE_RESET_REQUIRED);
1217 break;
1218
1219 /* neither ssp nor stp gets suspended. */
1220 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_NAK_CMD_ERR):
1221 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_XR):
1222 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_XR_IU_LEN_ERR):
1223 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SDMA_ERR):
1224 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OFFSET_ERR):
1225 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_EXCESS_DATA):
1226 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
1227 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
1228 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
1229 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
1230 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_DATA):
1231 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_OPEN_FAIL):
1232 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_VIIT_ENTRY_NV):
1233 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_IIT_ENTRY_NV):
1234 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_RNCNV_OUTBOUND):
1235 default:
1236 scic_sds_request_set_status(
1237 sci_req,
1238 SCU_GET_COMPLETION_TL_STATUS(completion_code) >>
1239 SCU_COMPLETION_TL_STATUS_SHIFT,
1240 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1241 break;
1242 }
1243
1244 /*
1245 * TODO: This is probably wrong for ACK/NAK timeout conditions
1246 */
1247
1248 /* In all cases we will treat this as the completion of the IO req. */
1249 sci_base_state_machine_change_state(
1250 &sci_req->state_machine,
1251 SCI_BASE_REQUEST_STATE_COMPLETED);
1252 return SCI_SUCCESS;
1253 }
1254
1255 /*
1256 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1257 * object receives a scic_sds_request_frame_handler() request. This method
1258 * first determines the frame type received. If this is a response frame then
1259 * the response data is copied to the io request response buffer for processing
1260 * at completion time. If the frame type is not a response buffer an error is
1261 * logged. enum sci_status SCI_SUCCESS SCI_FAILURE_INVALID_PARAMETER_VALUE
1262 */
1263 static enum sci_status
1264 scic_sds_request_started_state_frame_handler(struct scic_sds_request *sci_req,
1265 u32 frame_index)
1266 {
1267 enum sci_status status;
1268 struct sci_ssp_frame_header *frame_header;
1269
1270 status = scic_sds_unsolicited_frame_control_get_header(
1271 &(scic_sds_request_get_controller(sci_req)->uf_control),
1272 frame_index,
1273 (void **)&frame_header);
1274
1275 if (frame_header->frame_type == SCI_SAS_RESPONSE_FRAME) {
1276 struct ssp_response_iu *resp_iu;
1277 ssize_t word_cnt = SSP_RESP_IU_MAX_SIZE / sizeof(u32);
1278
1279 status = scic_sds_unsolicited_frame_control_get_buffer(
1280 &(scic_sds_request_get_controller(sci_req)->uf_control),
1281 frame_index,
1282 (void **)&resp_iu);
1283
1284 sci_swab32_cpy(sci_req->response_buffer,
1285 resp_iu, word_cnt);
1286
1287 resp_iu = sci_req->response_buffer;
1288
1289 if ((resp_iu->datapres == 0x01) ||
1290 (resp_iu->datapres == 0x02)) {
1291 scic_sds_request_set_status(
1292 sci_req,
1293 SCU_TASK_DONE_CHECK_RESPONSE,
1294 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1295 } else
1296 scic_sds_request_set_status(
1297 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS);
1298 } else {
1299 /* This was not a response frame why did it get forwarded? */
1300 dev_err(scic_to_dev(sci_req->owning_controller),
1301 "%s: SCIC IO Request 0x%p received unexpected "
1302 "frame %d type 0x%02x\n",
1303 __func__,
1304 sci_req,
1305 frame_index,
1306 frame_header->frame_type);
1307 }
1308
1309 /*
1310 * In any case we are done with this frame buffer return it to the
1311 * controller
1312 */
1313 scic_sds_controller_release_frame(
1314 sci_req->owning_controller, frame_index);
1315
1316 return SCI_SUCCESS;
1317 }
1318
1319 /*
1320 * *****************************************************************************
1321 * * COMPLETED STATE HANDLERS
1322 * ***************************************************************************** */
1323
1324
1325 /*
1326 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1327 * object receives a scic_sds_request_complete() request. This method frees up
1328 * any io request resources that have been allocated and transitions the
1329 * request to its final state. Consider stopping the state machine instead of
1330 * transitioning to the final state? enum sci_status SCI_SUCCESS
1331 */
1332 static enum sci_status scic_sds_request_completed_state_complete_handler(
1333 struct scic_sds_request *request)
1334 {
1335 if (request->was_tag_assigned_by_user != true) {
1336 scic_controller_free_io_tag(
1337 request->owning_controller, request->io_tag);
1338 }
1339
1340 if (request->saved_rx_frame_index != SCU_INVALID_FRAME_INDEX) {
1341 scic_sds_controller_release_frame(
1342 request->owning_controller, request->saved_rx_frame_index);
1343 }
1344
1345 sci_base_state_machine_change_state(&request->state_machine,
1346 SCI_BASE_REQUEST_STATE_FINAL);
1347 return SCI_SUCCESS;
1348 }
1349
1350 /*
1351 * *****************************************************************************
1352 * * ABORTING STATE HANDLERS
1353 * ***************************************************************************** */
1354
1355 /*
1356 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1357 * object receives a scic_sds_request_terminate() request. This method is the
1358 * io request aborting state abort handlers. On receipt of a multiple
1359 * terminate requests the io request will transition to the completed state.
1360 * This should not happen in normal operation. enum sci_status SCI_SUCCESS
1361 */
1362 static enum sci_status scic_sds_request_aborting_state_abort_handler(
1363 struct scic_sds_request *request)
1364 {
1365 sci_base_state_machine_change_state(&request->state_machine,
1366 SCI_BASE_REQUEST_STATE_COMPLETED);
1367 return SCI_SUCCESS;
1368 }
1369
1370 /*
1371 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1372 * object receives a scic_sds_request_task_completion() request. This method
1373 * decodes the completion type waiting for the abort task complete
1374 * notification. When the abort task complete is received the io request
1375 * transitions to the completed state. enum sci_status SCI_SUCCESS
1376 */
1377 static enum sci_status scic_sds_request_aborting_state_tc_completion_handler(
1378 struct scic_sds_request *sci_req,
1379 u32 completion_code)
1380 {
1381 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1382 case (SCU_TASK_DONE_GOOD << SCU_COMPLETION_TL_STATUS_SHIFT):
1383 case (SCU_TASK_DONE_TASK_ABORT << SCU_COMPLETION_TL_STATUS_SHIFT):
1384 scic_sds_request_set_status(
1385 sci_req, SCU_TASK_DONE_TASK_ABORT, SCI_FAILURE_IO_TERMINATED
1386 );
1387
1388 sci_base_state_machine_change_state(&sci_req->state_machine,
1389 SCI_BASE_REQUEST_STATE_COMPLETED);
1390 break;
1391
1392 default:
1393 /*
1394 * Unless we get some strange error wait for the task abort to complete
1395 * TODO: Should there be a state change for this completion? */
1396 break;
1397 }
1398
1399 return SCI_SUCCESS;
1400 }
1401
1402 /*
1403 * This method implements the action to be taken when an SCIC_SDS_IO_REQUEST_T
1404 * object receives a scic_sds_request_frame_handler() request. This method
1405 * discards the unsolicited frame since we are waiting for the abort task
1406 * completion. enum sci_status SCI_SUCCESS
1407 */
1408 static enum sci_status scic_sds_request_aborting_state_frame_handler(
1409 struct scic_sds_request *sci_req,
1410 u32 frame_index)
1411 {
1412 /* TODO: Is it even possible to get an unsolicited frame in the aborting state? */
1413
1414 scic_sds_controller_release_frame(
1415 sci_req->owning_controller, frame_index);
1416
1417 return SCI_SUCCESS;
1418 }
1419
1420 static const struct scic_sds_io_request_state_handler scic_sds_request_state_handler_table[] = {
1421 [SCI_BASE_REQUEST_STATE_INITIAL] = {
1422 },
1423 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
1424 .start_handler = scic_sds_request_constructed_state_start_handler,
1425 .abort_handler = scic_sds_request_constructed_state_abort_handler,
1426 },
1427 [SCI_BASE_REQUEST_STATE_STARTED] = {
1428 .abort_handler = scic_sds_request_started_state_abort_handler,
1429 .tc_completion_handler = scic_sds_request_started_state_tc_completion_handler,
1430 .frame_handler = scic_sds_request_started_state_frame_handler,
1431 },
1432 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
1433 .complete_handler = scic_sds_request_completed_state_complete_handler,
1434 },
1435 [SCI_BASE_REQUEST_STATE_ABORTING] = {
1436 .abort_handler = scic_sds_request_aborting_state_abort_handler,
1437 .tc_completion_handler = scic_sds_request_aborting_state_tc_completion_handler,
1438 .frame_handler = scic_sds_request_aborting_state_frame_handler,
1439 },
1440 [SCI_BASE_REQUEST_STATE_FINAL] = {
1441 },
1442 };
1443
1444 /**
1445 * scic_sds_request_initial_state_enter() -
1446 * @object: This parameter specifies the base object for which the state
1447 * transition is occurring.
1448 *
1449 * This method implements the actions taken when entering the
1450 * SCI_BASE_REQUEST_STATE_INITIAL state. This state is entered when the initial
1451 * base request is constructed. Entry into the initial state sets all handlers
1452 * for the io request object to their default handlers. none
1453 */
1454 static void scic_sds_request_initial_state_enter(void *object)
1455 {
1456 struct scic_sds_request *sci_req = object;
1457
1458 SET_STATE_HANDLER(
1459 sci_req,
1460 scic_sds_request_state_handler_table,
1461 SCI_BASE_REQUEST_STATE_INITIAL
1462 );
1463 }
1464
1465 /**
1466 * scic_sds_request_constructed_state_enter() -
1467 * @object: The io request object that is to enter the constructed state.
1468 *
1469 * This method implements the actions taken when entering the
1470 * SCI_BASE_REQUEST_STATE_CONSTRUCTED state. The method sets the state handlers
1471 * for the the constructed state. none
1472 */
1473 static void scic_sds_request_constructed_state_enter(void *object)
1474 {
1475 struct scic_sds_request *sci_req = object;
1476
1477 SET_STATE_HANDLER(
1478 sci_req,
1479 scic_sds_request_state_handler_table,
1480 SCI_BASE_REQUEST_STATE_CONSTRUCTED
1481 );
1482 }
1483
1484 /**
1485 * scic_sds_request_started_state_enter() -
1486 * @object: This parameter specifies the base object for which the state
1487 * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
1488 *
1489 * This method implements the actions taken when entering the
1490 * SCI_BASE_REQUEST_STATE_STARTED state. If the io request object type is a
1491 * SCSI Task request we must enter the started substate machine. none
1492 */
1493 static void scic_sds_request_started_state_enter(void *object)
1494 {
1495 struct scic_sds_request *sci_req = object;
1496
1497 SET_STATE_HANDLER(
1498 sci_req,
1499 scic_sds_request_state_handler_table,
1500 SCI_BASE_REQUEST_STATE_STARTED
1501 );
1502
1503 /*
1504 * Most of the request state machines have a started substate machine so
1505 * start its execution on the entry to the started state. */
1506 if (sci_req->has_started_substate_machine == true)
1507 sci_base_state_machine_start(&sci_req->started_substate_machine);
1508 }
1509
1510 /**
1511 * scic_sds_request_started_state_exit() -
1512 * @object: This parameter specifies the base object for which the state
1513 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1514 * object.
1515 *
1516 * This method implements the actions taken when exiting the
1517 * SCI_BASE_REQUEST_STATE_STARTED state. For task requests the action will be
1518 * to stop the started substate machine. none
1519 */
1520 static void scic_sds_request_started_state_exit(void *object)
1521 {
1522 struct scic_sds_request *sci_req = object;
1523
1524 if (sci_req->has_started_substate_machine == true)
1525 sci_base_state_machine_stop(&sci_req->started_substate_machine);
1526 }
1527
1528 /**
1529 * scic_sds_request_completed_state_enter() -
1530 * @object: This parameter specifies the base object for which the state
1531 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1532 * object.
1533 *
1534 * This method implements the actions taken when entering the
1535 * SCI_BASE_REQUEST_STATE_COMPLETED state. This state is entered when the
1536 * SCIC_SDS_IO_REQUEST has completed. The method will decode the request
1537 * completion status and convert it to an enum sci_status to return in the
1538 * completion callback function. none
1539 */
1540 static void scic_sds_request_completed_state_enter(void *object)
1541 {
1542 struct scic_sds_request *sci_req = object;
1543 struct scic_sds_controller *scic =
1544 scic_sds_request_get_controller(sci_req);
1545 struct isci_host *ihost = scic->ihost;
1546 struct isci_request *ireq = sci_req->ireq;
1547
1548 SET_STATE_HANDLER(sci_req,
1549 scic_sds_request_state_handler_table,
1550 SCI_BASE_REQUEST_STATE_COMPLETED);
1551
1552 /* Tell the SCI_USER that the IO request is complete */
1553 if (sci_req->is_task_management_request == false)
1554 isci_request_io_request_complete(ihost,
1555 ireq,
1556 sci_req->sci_status);
1557 else
1558 isci_task_request_complete(ihost, ireq, sci_req->sci_status);
1559 }
1560
1561 /**
1562 * scic_sds_request_aborting_state_enter() -
1563 * @object: This parameter specifies the base object for which the state
1564 * transition is occurring. This object is cast into a SCIC_SDS_IO_REQUEST
1565 * object.
1566 *
1567 * This method implements the actions taken when entering the
1568 * SCI_BASE_REQUEST_STATE_ABORTING state. none
1569 */
1570 static void scic_sds_request_aborting_state_enter(void *object)
1571 {
1572 struct scic_sds_request *sci_req = object;
1573
1574 /* Setting the abort bit in the Task Context is required by the silicon. */
1575 sci_req->task_context_buffer->abort = 1;
1576
1577 SET_STATE_HANDLER(
1578 sci_req,
1579 scic_sds_request_state_handler_table,
1580 SCI_BASE_REQUEST_STATE_ABORTING
1581 );
1582 }
1583
1584 /**
1585 * scic_sds_request_final_state_enter() -
1586 * @object: This parameter specifies the base object for which the state
1587 * transition is occurring. This is cast into a SCIC_SDS_IO_REQUEST object.
1588 *
1589 * This method implements the actions taken when entering the
1590 * SCI_BASE_REQUEST_STATE_FINAL state. The only action required is to put the
1591 * state handlers in place. none
1592 */
1593 static void scic_sds_request_final_state_enter(void *object)
1594 {
1595 struct scic_sds_request *sci_req = object;
1596
1597 SET_STATE_HANDLER(
1598 sci_req,
1599 scic_sds_request_state_handler_table,
1600 SCI_BASE_REQUEST_STATE_FINAL
1601 );
1602 }
1603
1604 static const struct sci_base_state scic_sds_request_state_table[] = {
1605 [SCI_BASE_REQUEST_STATE_INITIAL] = {
1606 .enter_state = scic_sds_request_initial_state_enter,
1607 },
1608 [SCI_BASE_REQUEST_STATE_CONSTRUCTED] = {
1609 .enter_state = scic_sds_request_constructed_state_enter,
1610 },
1611 [SCI_BASE_REQUEST_STATE_STARTED] = {
1612 .enter_state = scic_sds_request_started_state_enter,
1613 .exit_state = scic_sds_request_started_state_exit
1614 },
1615 [SCI_BASE_REQUEST_STATE_COMPLETED] = {
1616 .enter_state = scic_sds_request_completed_state_enter,
1617 },
1618 [SCI_BASE_REQUEST_STATE_ABORTING] = {
1619 .enter_state = scic_sds_request_aborting_state_enter,
1620 },
1621 [SCI_BASE_REQUEST_STATE_FINAL] = {
1622 .enter_state = scic_sds_request_final_state_enter,
1623 },
1624 };
1625
1626 static void scic_sds_general_request_construct(struct scic_sds_controller *scic,
1627 struct scic_sds_remote_device *sci_dev,
1628 u16 io_tag,
1629 void *user_io_request_object,
1630 struct scic_sds_request *sci_req)
1631 {
1632 sci_base_state_machine_construct(&sci_req->state_machine, sci_req,
1633 scic_sds_request_state_table, SCI_BASE_REQUEST_STATE_INITIAL);
1634 sci_base_state_machine_start(&sci_req->state_machine);
1635
1636 sci_req->io_tag = io_tag;
1637 sci_req->user_request = user_io_request_object;
1638 sci_req->owning_controller = scic;
1639 sci_req->target_device = sci_dev;
1640 sci_req->has_started_substate_machine = false;
1641 sci_req->protocol = SCIC_NO_PROTOCOL;
1642 sci_req->saved_rx_frame_index = SCU_INVALID_FRAME_INDEX;
1643 sci_req->device_sequence = scic_sds_remote_device_get_sequence(sci_dev);
1644
1645 sci_req->sci_status = SCI_SUCCESS;
1646 sci_req->scu_status = 0;
1647 sci_req->post_context = 0xFFFFFFFF;
1648
1649 sci_req->is_task_management_request = false;
1650
1651 if (io_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1652 sci_req->was_tag_assigned_by_user = false;
1653 sci_req->task_context_buffer = NULL;
1654 } else {
1655 sci_req->was_tag_assigned_by_user = true;
1656
1657 sci_req->task_context_buffer =
1658 scic_sds_controller_get_task_context_buffer(scic, io_tag);
1659 }
1660 }
1661
1662 enum sci_status
1663 scic_io_request_construct(struct scic_sds_controller *scic,
1664 struct scic_sds_remote_device *sci_dev,
1665 u16 io_tag,
1666 void *user_req,
1667 struct scic_sds_request *sci_req,
1668 struct scic_sds_request **new_sci_req)
1669 {
1670 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1671 enum sci_status status = SCI_SUCCESS;
1672
1673 /* Build the common part of the request */
1674 scic_sds_general_request_construct(scic,
1675 sci_dev,
1676 io_tag,
1677 user_req,
1678 sci_req);
1679
1680 if (sci_dev->rnc.remote_node_index ==
1681 SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
1682 return SCI_FAILURE_INVALID_REMOTE_DEVICE;
1683
1684 if (dev->dev_type == SAS_END_DEV)
1685 scic_sds_ssp_io_request_assign_buffers(sci_req);
1686 else if ((dev->dev_type == SATA_DEV) ||
1687 (dev->tproto & SAS_PROTOCOL_STP)) {
1688 scic_sds_stp_request_assign_buffers(sci_req);
1689 memset(sci_req->command_buffer,
1690 0,
1691 sizeof(struct host_to_dev_fis));
1692 } else if (dev_is_expander(dev)) {
1693 scic_sds_smp_request_assign_buffers(sci_req);
1694 memset(sci_req->command_buffer, 0, sizeof(struct smp_req));
1695 } else
1696 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1697
1698 if (status == SCI_SUCCESS) {
1699 memset(sci_req->task_context_buffer,
1700 0,
1701 SCI_FIELD_OFFSET(struct scu_task_context, sgl_pair_ab));
1702 *new_sci_req = sci_req;
1703 }
1704
1705 return status;
1706 }
1707
1708 enum sci_status scic_task_request_construct(struct scic_sds_controller *scic,
1709 struct scic_sds_remote_device *sci_dev,
1710 u16 io_tag,
1711 void *user_io_request_object,
1712 struct scic_sds_request *sci_req,
1713 struct scic_sds_request **new_sci_req)
1714 {
1715 struct domain_device *dev = sci_dev_to_domain(sci_dev);
1716 enum sci_status status = SCI_SUCCESS;
1717
1718 /* Build the common part of the request */
1719 scic_sds_general_request_construct(scic, sci_dev, io_tag,
1720 user_io_request_object,
1721 sci_req);
1722
1723 if (dev->dev_type == SAS_END_DEV) {
1724 scic_sds_ssp_task_request_assign_buffers(sci_req);
1725
1726 sci_req->has_started_substate_machine = true;
1727
1728 /* Construct the started sub-state machine. */
1729 sci_base_state_machine_construct(
1730 &sci_req->started_substate_machine,
1731 sci_req,
1732 scic_sds_io_request_started_task_mgmt_substate_table,
1733 SCIC_SDS_IO_REQUEST_STARTED_TASK_MGMT_SUBSTATE_AWAIT_TC_COMPLETION
1734 );
1735 } else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP))
1736 scic_sds_stp_request_assign_buffers(sci_req);
1737 else
1738 status = SCI_FAILURE_UNSUPPORTED_PROTOCOL;
1739
1740 if (status == SCI_SUCCESS) {
1741 sci_req->is_task_management_request = true;
1742 memset(sci_req->task_context_buffer, 0, sizeof(struct scu_task_context));
1743 *new_sci_req = sci_req;
1744 }
1745
1746 return status;
1747 }
This page took 0.072481 seconds and 5 git commands to generate.