308f486514e99773164e85938ee54848f382c33c
[deliverable/linux.git] / drivers / scsi / isci / core / scic_sds_stp_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 "state_machine.h"
59 #include "scic_io_request.h"
60 #include "remote_device.h"
61 #include "scic_sds_request.h"
62 #include "scic_sds_stp_pio_request.h"
63 #include "scic_sds_stp_request.h"
64 #include "unsolicited_frame_control.h"
65 #include "sci_util.h"
66 #include "scu_completion_codes.h"
67 #include "scu_event_codes.h"
68 #include "scu_task_context.h"
69 #include "request.h"
70
71 void scic_sds_stp_request_assign_buffers(struct scic_sds_request *sci_req)
72 {
73 if (sci_req->was_tag_assigned_by_user == false)
74 sci_req->task_context_buffer = &sci_req->tc;
75 }
76
77 /**
78 * This method is will fill in the SCU Task Context for any type of SATA
79 * request. This is called from the various SATA constructors.
80 * @sci_req: The general IO request object which is to be used in
81 * constructing the SCU task context.
82 * @task_context: The buffer pointer for the SCU task context which is being
83 * constructed.
84 *
85 * The general io request construction is complete. The buffer assignment for
86 * the command buffer is complete. none Revisit task context construction to
87 * determine what is common for SSP/SMP/STP task context structures.
88 */
89 static void scu_sata_reqeust_construct_task_context(
90 struct scic_sds_request *sci_req,
91 struct scu_task_context *task_context)
92 {
93 dma_addr_t dma_addr;
94 struct scic_sds_controller *controller;
95 struct scic_sds_remote_device *target_device;
96 struct scic_sds_port *target_port;
97
98 controller = scic_sds_request_get_controller(sci_req);
99 target_device = scic_sds_request_get_device(sci_req);
100 target_port = scic_sds_request_get_port(sci_req);
101
102 /* Fill in the TC with the its required data */
103 task_context->abort = 0;
104 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
105 task_context->initiator_request = 1;
106 task_context->connection_rate = target_device->connection_rate;
107 task_context->protocol_engine_index =
108 scic_sds_controller_get_protocol_engine_group(controller);
109 task_context->logical_port_index =
110 scic_sds_port_get_index(target_port);
111 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_STP;
112 task_context->valid = SCU_TASK_CONTEXT_VALID;
113 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
114
115 task_context->remote_node_index =
116 scic_sds_remote_device_get_index(sci_req->target_device);
117 task_context->command_code = 0;
118
119 task_context->link_layer_control = 0;
120 task_context->do_not_dma_ssp_good_response = 1;
121 task_context->strict_ordering = 0;
122 task_context->control_frame = 0;
123 task_context->timeout_enable = 0;
124 task_context->block_guard_enable = 0;
125
126 task_context->address_modifier = 0;
127 task_context->task_phase = 0x01;
128
129 task_context->ssp_command_iu_length =
130 (sizeof(struct host_to_dev_fis) - sizeof(u32)) / sizeof(u32);
131
132 /* Set the first word of the H2D REG FIS */
133 task_context->type.words[0] = *(u32 *)&sci_req->stp.cmd;
134
135 if (sci_req->was_tag_assigned_by_user) {
136 /*
137 * Build the task context now since we have already read
138 * the data
139 */
140 sci_req->post_context =
141 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
142 (scic_sds_controller_get_protocol_engine_group(
143 controller) <<
144 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
145 (scic_sds_port_get_index(target_port) <<
146 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
147 scic_sds_io_tag_get_index(sci_req->io_tag));
148 } else {
149 /*
150 * Build the task context now since we have already read
151 * the data.
152 * I/O tag index is not assigned because we have to wait
153 * until we get a TCi.
154 */
155 sci_req->post_context =
156 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
157 (scic_sds_controller_get_protocol_engine_group(
158 controller) <<
159 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
160 (scic_sds_port_get_index(target_port) <<
161 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
162 }
163
164 /*
165 * Copy the physical address for the command buffer to the SCU Task
166 * Context. We must offset the command buffer by 4 bytes because the
167 * first 4 bytes are transfered in the body of the TC.
168 */
169 dma_addr = scic_io_request_get_dma_addr(sci_req,
170 ((char *) &sci_req->stp.cmd) +
171 sizeof(u32));
172
173 task_context->command_iu_upper = upper_32_bits(dma_addr);
174 task_context->command_iu_lower = lower_32_bits(dma_addr);
175
176 /* SATA Requests do not have a response buffer */
177 task_context->response_iu_upper = 0;
178 task_context->response_iu_lower = 0;
179 }
180
181 /**
182 *
183 * @sci_req:
184 *
185 * This method will perform any general sata request construction. What part of
186 * SATA IO request construction is general? none
187 */
188 static void scic_sds_stp_non_ncq_request_construct(
189 struct scic_sds_request *sci_req)
190 {
191 sci_req->has_started_substate_machine = true;
192 }
193
194 /**
195 *
196 * @sci_req: This parameter specifies the request to be constructed as an
197 * optimized request.
198 * @optimized_task_type: This parameter specifies whether the request is to be
199 * an UDMA request or a NCQ request. - A value of 0 indicates UDMA. - A
200 * value of 1 indicates NCQ.
201 *
202 * This method will perform request construction common to all types of STP
203 * requests that are optimized by the silicon (i.e. UDMA, NCQ). This method
204 * returns an indication as to whether the construction was successful.
205 */
206 static void scic_sds_stp_optimized_request_construct(struct scic_sds_request *sci_req,
207 u8 optimized_task_type,
208 u32 len,
209 enum dma_data_direction dir)
210 {
211 struct scu_task_context *task_context = sci_req->task_context_buffer;
212
213 /* Build the STP task context structure */
214 scu_sata_reqeust_construct_task_context(sci_req, task_context);
215
216 /* Copy over the SGL elements */
217 scic_sds_request_build_sgl(sci_req);
218
219 /* Copy over the number of bytes to be transfered */
220 task_context->transfer_length_bytes = len;
221
222 if (dir == DMA_TO_DEVICE) {
223 /*
224 * The difference between the DMA IN and DMA OUT request task type
225 * values are consistent with the difference between FPDMA READ
226 * and FPDMA WRITE values. Add the supplied task type parameter
227 * to this difference to set the task type properly for this
228 * DATA OUT (WRITE) case. */
229 task_context->task_type = optimized_task_type + (SCU_TASK_TYPE_DMA_OUT
230 - SCU_TASK_TYPE_DMA_IN);
231 } else {
232 /*
233 * For the DATA IN (READ) case, simply save the supplied
234 * optimized task type. */
235 task_context->task_type = optimized_task_type;
236 }
237 }
238
239 /**
240 *
241 * @sci_req: This parameter specifies the request to be constructed.
242 *
243 * This method will construct the STP UDMA request and its associated TC data.
244 * This method returns an indication as to whether the construction was
245 * successful. SCI_SUCCESS Currently this method always returns this value.
246 */
247 enum sci_status scic_sds_stp_ncq_request_construct(struct scic_sds_request *sci_req,
248 u32 len,
249 enum dma_data_direction dir)
250 {
251 scic_sds_stp_optimized_request_construct(sci_req,
252 SCU_TASK_TYPE_FPDMAQ_READ,
253 len, dir);
254 return SCI_SUCCESS;
255 }
256
257 /**
258 * scu_stp_raw_request_construct_task_context -
259 * @sci_req: This parameter specifies the STP request object for which to
260 * construct a RAW command frame task context.
261 * @task_context: This parameter specifies the SCU specific task context buffer
262 * to construct.
263 *
264 * This method performs the operations common to all SATA/STP requests
265 * utilizing the raw frame method. none
266 */
267 static void scu_stp_raw_request_construct_task_context(
268 struct scic_sds_stp_request *stp_req,
269 struct scu_task_context *task_context)
270 {
271 struct scic_sds_request *sci_req = to_sci_req(stp_req);
272
273 scu_sata_reqeust_construct_task_context(sci_req, task_context);
274
275 task_context->control_frame = 0;
276 task_context->priority = SCU_TASK_PRIORITY_NORMAL;
277 task_context->task_type = SCU_TASK_TYPE_SATA_RAW_FRAME;
278 task_context->type.stp.fis_type = FIS_REGH2D;
279 task_context->transfer_length_bytes = sizeof(struct host_to_dev_fis) - sizeof(u32);
280 }
281
282 void scic_stp_io_request_set_ncq_tag(
283 struct scic_sds_request *req,
284 u16 ncq_tag)
285 {
286 /**
287 * @note This could be made to return an error to the user if the user
288 * attempts to set the NCQ tag in the wrong state.
289 */
290 req->task_context_buffer->type.stp.ncq_tag = ncq_tag;
291 }
292
293 /**
294 *
295 * @sci_req:
296 *
297 * Get the next SGL element from the request. - Check on which SGL element pair
298 * we are working - if working on SLG pair element A - advance to element B -
299 * else - check to see if there are more SGL element pairs for this IO request
300 * - if there are more SGL element pairs - advance to the next pair and return
301 * element A struct scu_sgl_element*
302 */
303 static struct scu_sgl_element *scic_sds_stp_request_pio_get_next_sgl(struct scic_sds_stp_request *stp_req)
304 {
305 struct scu_sgl_element *current_sgl;
306 struct scic_sds_request *sci_req = to_sci_req(stp_req);
307 struct scic_sds_request_pio_sgl *pio_sgl = &stp_req->type.pio.request_current;
308
309 if (pio_sgl->sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
310 if (pio_sgl->sgl_pair->B.address_lower == 0 &&
311 pio_sgl->sgl_pair->B.address_upper == 0) {
312 current_sgl = NULL;
313 } else {
314 pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_B;
315 current_sgl = &pio_sgl->sgl_pair->B;
316 }
317 } else {
318 if (pio_sgl->sgl_pair->next_pair_lower == 0 &&
319 pio_sgl->sgl_pair->next_pair_upper == 0) {
320 current_sgl = NULL;
321 } else {
322 u64 phys_addr;
323
324 phys_addr = pio_sgl->sgl_pair->next_pair_upper;
325 phys_addr <<= 32;
326 phys_addr |= pio_sgl->sgl_pair->next_pair_lower;
327
328 pio_sgl->sgl_pair = scic_request_get_virt_addr(sci_req, phys_addr);
329 pio_sgl->sgl_set = SCU_SGL_ELEMENT_PAIR_A;
330 current_sgl = &pio_sgl->sgl_pair->A;
331 }
332 }
333
334 return current_sgl;
335 }
336
337 /**
338 *
339 * @sci_req:
340 * @completion_code:
341 *
342 * This method processes a TC completion. The expected TC completion is for
343 * the transmission of the H2D register FIS containing the SATA/STP non-data
344 * request. This method always successfully processes the TC completion.
345 * SCI_SUCCESS This value is always returned.
346 */
347 static enum sci_status scic_sds_stp_request_non_data_await_h2d_tc_completion_handler(
348 struct scic_sds_request *sci_req,
349 u32 completion_code)
350 {
351 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
352 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
353 scic_sds_request_set_status(
354 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
355 );
356
357 sci_base_state_machine_change_state(
358 &sci_req->started_substate_machine,
359 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
360 );
361 break;
362
363 default:
364 /*
365 * All other completion status cause the IO to be complete. If a NAK
366 * was received, then it is up to the user to retry the request. */
367 scic_sds_request_set_status(
368 sci_req,
369 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
370 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
371 );
372
373 sci_base_state_machine_change_state(
374 &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
375 break;
376 }
377
378 return SCI_SUCCESS;
379 }
380
381 /**
382 *
383 * @request: This parameter specifies the request for which a frame has been
384 * received.
385 * @frame_index: This parameter specifies the index of the frame that has been
386 * received.
387 *
388 * This method processes frames received from the target while waiting for a
389 * device to host register FIS. If a non-register FIS is received during this
390 * time, it is treated as a protocol violation from an IO perspective. Indicate
391 * if the received frame was processed successfully.
392 */
393 static enum sci_status scic_sds_stp_request_non_data_await_d2h_frame_handler(
394 struct scic_sds_request *sci_req,
395 u32 frame_index)
396 {
397 enum sci_status status;
398 struct dev_to_host_fis *frame_header;
399 u32 *frame_buffer;
400 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
401 struct scic_sds_controller *scic = sci_req->owning_controller;
402
403 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
404 frame_index,
405 (void **)&frame_header);
406
407 if (status != SCI_SUCCESS) {
408 dev_err(scic_to_dev(sci_req->owning_controller),
409 "%s: SCIC IO Request 0x%p could not get frame header "
410 "for frame index %d, status %x\n",
411 __func__, stp_req, frame_index, status);
412
413 return status;
414 }
415
416 switch (frame_header->fis_type) {
417 case FIS_REGD2H:
418 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
419 frame_index,
420 (void **)&frame_buffer);
421
422 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
423 frame_header,
424 frame_buffer);
425
426 /* The command has completed with error */
427 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_CHECK_RESPONSE,
428 SCI_FAILURE_IO_RESPONSE_VALID);
429 break;
430
431 default:
432 dev_warn(scic_to_dev(scic),
433 "%s: IO Request:0x%p Frame Id:%d protocol "
434 "violation occurred\n", __func__, stp_req,
435 frame_index);
436
437 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
438 SCI_FAILURE_PROTOCOL_VIOLATION);
439 break;
440 }
441
442 sci_base_state_machine_change_state(&sci_req->state_machine,
443 SCI_BASE_REQUEST_STATE_COMPLETED);
444
445 /* Frame has been decoded return it to the controller */
446 scic_sds_controller_release_frame(scic, frame_index);
447
448 return status;
449 }
450
451 /* --------------------------------------------------------------------------- */
452
453 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_non_data_substate_handler_table[] = {
454 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
455 .abort_handler = scic_sds_request_started_state_abort_handler,
456 .tc_completion_handler = scic_sds_stp_request_non_data_await_h2d_tc_completion_handler,
457 },
458 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
459 .abort_handler = scic_sds_request_started_state_abort_handler,
460 .frame_handler = scic_sds_stp_request_non_data_await_d2h_frame_handler,
461 }
462 };
463
464 static void scic_sds_stp_request_started_non_data_await_h2d_completion_enter(
465 void *object)
466 {
467 struct scic_sds_request *sci_req = object;
468
469 SET_STATE_HANDLER(
470 sci_req,
471 scic_sds_stp_request_started_non_data_substate_handler_table,
472 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE
473 );
474
475 scic_sds_remote_device_set_working_request(
476 sci_req->target_device, sci_req
477 );
478 }
479
480 static void scic_sds_stp_request_started_non_data_await_d2h_enter(void *object)
481 {
482 struct scic_sds_request *sci_req = object;
483
484 SET_STATE_HANDLER(
485 sci_req,
486 scic_sds_stp_request_started_non_data_substate_handler_table,
487 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE
488 );
489 }
490
491 /* --------------------------------------------------------------------------- */
492
493 static const struct sci_base_state scic_sds_stp_request_started_non_data_substate_table[] = {
494 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE] = {
495 .enter_state = scic_sds_stp_request_started_non_data_await_h2d_completion_enter,
496 },
497 [SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_D2H_SUBSTATE] = {
498 .enter_state = scic_sds_stp_request_started_non_data_await_d2h_enter,
499 },
500 };
501
502 enum sci_status scic_sds_stp_non_data_request_construct(struct scic_sds_request *sci_req)
503 {
504 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
505
506 scic_sds_stp_non_ncq_request_construct(sci_req);
507
508 /* Build the STP task context structure */
509 scu_stp_raw_request_construct_task_context(stp_req, sci_req->task_context_buffer);
510
511 sci_base_state_machine_construct(&sci_req->started_substate_machine,
512 sci_req,
513 scic_sds_stp_request_started_non_data_substate_table,
514 SCIC_SDS_STP_REQUEST_STARTED_NON_DATA_AWAIT_H2D_COMPLETION_SUBSTATE);
515
516 return SCI_SUCCESS;
517 }
518
519 #define SCU_MAX_FRAME_BUFFER_SIZE 0x400 /* 1K is the maximum SCU frame data payload */
520
521 /* transmit DATA_FIS from (current sgl + offset) for input
522 * parameter length. current sgl and offset is alreay stored in the IO request
523 */
524 static enum sci_status scic_sds_stp_request_pio_data_out_trasmit_data_frame(
525 struct scic_sds_request *sci_req,
526 u32 length)
527 {
528 struct scic_sds_controller *scic = sci_req->owning_controller;
529 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
530 struct scu_task_context *task_context;
531 struct scu_sgl_element *current_sgl;
532
533 /* Recycle the TC and reconstruct it for sending out DATA FIS containing
534 * for the data from current_sgl+offset for the input length
535 */
536 task_context = scic_sds_controller_get_task_context_buffer(scic,
537 sci_req->io_tag);
538
539 if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A)
540 current_sgl = &stp_req->type.pio.request_current.sgl_pair->A;
541 else
542 current_sgl = &stp_req->type.pio.request_current.sgl_pair->B;
543
544 /* update the TC */
545 task_context->command_iu_upper = current_sgl->address_upper;
546 task_context->command_iu_lower = current_sgl->address_lower;
547 task_context->transfer_length_bytes = length;
548 task_context->type.stp.fis_type = FIS_DATA;
549
550 /* send the new TC out. */
551 return scic_controller_continue_io(sci_req);
552 }
553
554 static enum sci_status scic_sds_stp_request_pio_data_out_transmit_data(struct scic_sds_request *sci_req)
555 {
556
557 struct scu_sgl_element *current_sgl;
558 u32 sgl_offset;
559 u32 remaining_bytes_in_current_sgl = 0;
560 enum sci_status status = SCI_SUCCESS;
561 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
562
563 sgl_offset = stp_req->type.pio.request_current.sgl_offset;
564
565 if (stp_req->type.pio.request_current.sgl_set == SCU_SGL_ELEMENT_PAIR_A) {
566 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->A);
567 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->A.length - sgl_offset;
568 } else {
569 current_sgl = &(stp_req->type.pio.request_current.sgl_pair->B);
570 remaining_bytes_in_current_sgl = stp_req->type.pio.request_current.sgl_pair->B.length - sgl_offset;
571 }
572
573
574 if (stp_req->type.pio.pio_transfer_bytes > 0) {
575 if (stp_req->type.pio.pio_transfer_bytes >= remaining_bytes_in_current_sgl) {
576 /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = remaining_bytes_in_current_sgl */
577 status = scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, remaining_bytes_in_current_sgl);
578 if (status == SCI_SUCCESS) {
579 stp_req->type.pio.pio_transfer_bytes -= remaining_bytes_in_current_sgl;
580
581 /* update the current sgl, sgl_offset and save for future */
582 current_sgl = scic_sds_stp_request_pio_get_next_sgl(stp_req);
583 sgl_offset = 0;
584 }
585 } else if (stp_req->type.pio.pio_transfer_bytes < remaining_bytes_in_current_sgl) {
586 /* recycle the TC and send the H2D Data FIS from (current sgl + sgl_offset) and length = type.pio.pio_transfer_bytes */
587 scic_sds_stp_request_pio_data_out_trasmit_data_frame(sci_req, stp_req->type.pio.pio_transfer_bytes);
588
589 if (status == SCI_SUCCESS) {
590 /* Sgl offset will be adjusted and saved for future */
591 sgl_offset += stp_req->type.pio.pio_transfer_bytes;
592 current_sgl->address_lower += stp_req->type.pio.pio_transfer_bytes;
593 stp_req->type.pio.pio_transfer_bytes = 0;
594 }
595 }
596 }
597
598 if (status == SCI_SUCCESS) {
599 stp_req->type.pio.request_current.sgl_offset = sgl_offset;
600 }
601
602 return status;
603 }
604
605 /**
606 *
607 * @stp_request: The request that is used for the SGL processing.
608 * @data_buffer: The buffer of data to be copied.
609 * @length: The length of the data transfer.
610 *
611 * Copy the data from the buffer for the length specified to the IO reqeust SGL
612 * specified data region. enum sci_status
613 */
614 static enum sci_status
615 scic_sds_stp_request_pio_data_in_copy_data_buffer(struct scic_sds_stp_request *stp_req,
616 u8 *data_buf, u32 len)
617 {
618 struct scic_sds_request *sci_req;
619 struct isci_request *ireq;
620 u8 *src_addr;
621 int copy_len;
622 struct sas_task *task;
623 struct scatterlist *sg;
624 void *kaddr;
625 int total_len = len;
626
627 sci_req = to_sci_req(stp_req);
628 ireq = sci_req_to_ireq(sci_req);
629 task = isci_request_access_task(ireq);
630 src_addr = data_buf;
631
632 if (task->num_scatter > 0) {
633 sg = task->scatter;
634
635 while (total_len > 0) {
636 struct page *page = sg_page(sg);
637
638 copy_len = min_t(int, total_len, sg_dma_len(sg));
639 kaddr = kmap_atomic(page, KM_IRQ0);
640 memcpy(kaddr + sg->offset, src_addr, copy_len);
641 kunmap_atomic(kaddr, KM_IRQ0);
642 total_len -= copy_len;
643 src_addr += copy_len;
644 sg = sg_next(sg);
645 }
646 } else {
647 BUG_ON(task->total_xfer_len < total_len);
648 memcpy(task->scatter, src_addr, total_len);
649 }
650
651 return SCI_SUCCESS;
652 }
653
654 /**
655 *
656 * @sci_req: The PIO DATA IN request that is to receive the data.
657 * @data_buffer: The buffer to copy from.
658 *
659 * Copy the data buffer to the io request data region. enum sci_status
660 */
661 static enum sci_status scic_sds_stp_request_pio_data_in_copy_data(
662 struct scic_sds_stp_request *sci_req,
663 u8 *data_buffer)
664 {
665 enum sci_status status;
666
667 /*
668 * If there is less than 1K remaining in the transfer request
669 * copy just the data for the transfer */
670 if (sci_req->type.pio.pio_transfer_bytes < SCU_MAX_FRAME_BUFFER_SIZE) {
671 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
672 sci_req, data_buffer, sci_req->type.pio.pio_transfer_bytes);
673
674 if (status == SCI_SUCCESS)
675 sci_req->type.pio.pio_transfer_bytes = 0;
676 } else {
677 /* We are transfering the whole frame so copy */
678 status = scic_sds_stp_request_pio_data_in_copy_data_buffer(
679 sci_req, data_buffer, SCU_MAX_FRAME_BUFFER_SIZE);
680
681 if (status == SCI_SUCCESS)
682 sci_req->type.pio.pio_transfer_bytes -= SCU_MAX_FRAME_BUFFER_SIZE;
683 }
684
685 return status;
686 }
687
688 /**
689 *
690 * @sci_req:
691 * @completion_code:
692 *
693 * enum sci_status
694 */
695 static enum sci_status scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler(
696 struct scic_sds_request *sci_req,
697 u32 completion_code)
698 {
699 enum sci_status status = SCI_SUCCESS;
700
701 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
702 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
703 scic_sds_request_set_status(
704 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
705 );
706
707 sci_base_state_machine_change_state(
708 &sci_req->started_substate_machine,
709 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
710 );
711 break;
712
713 default:
714 /*
715 * All other completion status cause the IO to be complete. If a NAK
716 * was received, then it is up to the user to retry the request. */
717 scic_sds_request_set_status(
718 sci_req,
719 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
720 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
721 );
722
723 sci_base_state_machine_change_state(
724 &sci_req->state_machine,
725 SCI_BASE_REQUEST_STATE_COMPLETED
726 );
727 break;
728 }
729
730 return status;
731 }
732
733 static enum sci_status scic_sds_stp_request_pio_await_frame_frame_handler(struct scic_sds_request *sci_req,
734 u32 frame_index)
735 {
736 struct scic_sds_controller *scic = sci_req->owning_controller;
737 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
738 struct isci_request *ireq = sci_req_to_ireq(sci_req);
739 struct sas_task *task = isci_request_access_task(ireq);
740 struct dev_to_host_fis *frame_header;
741 enum sci_status status;
742 u32 *frame_buffer;
743
744 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
745 frame_index,
746 (void **)&frame_header);
747
748 if (status != SCI_SUCCESS) {
749 dev_err(scic_to_dev(scic),
750 "%s: SCIC IO Request 0x%p could not get frame header "
751 "for frame index %d, status %x\n",
752 __func__, stp_req, frame_index, status);
753 return status;
754 }
755
756 switch (frame_header->fis_type) {
757 case FIS_PIO_SETUP:
758 /* Get from the frame buffer the PIO Setup Data */
759 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
760 frame_index,
761 (void **)&frame_buffer);
762
763 /* Get the data from the PIO Setup The SCU Hardware returns
764 * first word in the frame_header and the rest of the data is in
765 * the frame buffer so we need to back up one dword
766 */
767
768 /* transfer_count: first 16bits in the 4th dword */
769 stp_req->type.pio.pio_transfer_bytes = frame_buffer[3] & 0xffff;
770
771 /* ending_status: 4th byte in the 3rd dword */
772 stp_req->type.pio.ending_status = (frame_buffer[2] >> 24) & 0xff;
773
774 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
775 frame_header,
776 frame_buffer);
777
778 sci_req->stp.rsp.status = stp_req->type.pio.ending_status;
779
780 /* The next state is dependent on whether the
781 * request was PIO Data-in or Data out
782 */
783 if (task->data_dir == DMA_FROM_DEVICE) {
784 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
785 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE);
786 } else if (task->data_dir == DMA_TO_DEVICE) {
787 /* Transmit data */
788 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
789 if (status != SCI_SUCCESS)
790 break;
791 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
792 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE);
793 }
794 break;
795 case FIS_SETDEVBITS:
796 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
797 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
798 break;
799 case FIS_REGD2H:
800 if (frame_header->status & ATA_BUSY) {
801 /* Now why is the drive sending a D2H Register FIS when
802 * it is still busy? Do nothing since we are still in
803 * the right state.
804 */
805 dev_dbg(scic_to_dev(scic),
806 "%s: SCIC PIO Request 0x%p received "
807 "D2H Register FIS with BSY status "
808 "0x%x\n", __func__, stp_req,
809 frame_header->status);
810 break;
811 }
812
813 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
814 frame_index,
815 (void **)&frame_buffer);
816
817 scic_sds_controller_copy_sata_response(&sci_req->stp.req,
818 frame_header,
819 frame_buffer);
820
821 scic_sds_request_set_status(sci_req,
822 SCU_TASK_DONE_CHECK_RESPONSE,
823 SCI_FAILURE_IO_RESPONSE_VALID);
824
825 sci_base_state_machine_change_state(&sci_req->state_machine,
826 SCI_BASE_REQUEST_STATE_COMPLETED);
827 break;
828 default:
829 /* FIXME: what do we do here? */
830 break;
831 }
832
833 /* Frame is decoded return it to the controller */
834 scic_sds_controller_release_frame(scic, frame_index);
835
836 return status;
837 }
838
839 static enum sci_status scic_sds_stp_request_pio_data_in_await_data_frame_handler(struct scic_sds_request *sci_req,
840 u32 frame_index)
841 {
842 enum sci_status status;
843 struct dev_to_host_fis *frame_header;
844 struct sata_fis_data *frame_buffer;
845 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
846 struct scic_sds_controller *scic = sci_req->owning_controller;
847
848 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
849 frame_index,
850 (void **)&frame_header);
851
852 if (status != SCI_SUCCESS) {
853 dev_err(scic_to_dev(scic),
854 "%s: SCIC IO Request 0x%p could not get frame header "
855 "for frame index %d, status %x\n",
856 __func__, stp_req, frame_index, status);
857 return status;
858 }
859
860 if (frame_header->fis_type == FIS_DATA) {
861 if (stp_req->type.pio.request_current.sgl_pair == NULL) {
862 sci_req->saved_rx_frame_index = frame_index;
863 stp_req->type.pio.pio_transfer_bytes = 0;
864 } else {
865 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
866 frame_index,
867 (void **)&frame_buffer);
868
869 status = scic_sds_stp_request_pio_data_in_copy_data(stp_req,
870 (u8 *)frame_buffer);
871
872 /* Frame is decoded return it to the controller */
873 scic_sds_controller_release_frame(scic, frame_index);
874 }
875
876 /* Check for the end of the transfer, are there more
877 * bytes remaining for this data transfer
878 */
879 if (status != SCI_SUCCESS ||
880 stp_req->type.pio.pio_transfer_bytes != 0)
881 return status;
882
883 if ((stp_req->type.pio.ending_status & ATA_BUSY) == 0) {
884 scic_sds_request_set_status(sci_req,
885 SCU_TASK_DONE_CHECK_RESPONSE,
886 SCI_FAILURE_IO_RESPONSE_VALID);
887
888 sci_base_state_machine_change_state(&sci_req->state_machine,
889 SCI_BASE_REQUEST_STATE_COMPLETED);
890 } else {
891 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
892 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE);
893 }
894 } else {
895 dev_err(scic_to_dev(scic),
896 "%s: SCIC PIO Request 0x%p received frame %d "
897 "with fis type 0x%02x when expecting a data "
898 "fis.\n", __func__, stp_req, frame_index,
899 frame_header->fis_type);
900
901 scic_sds_request_set_status(sci_req,
902 SCU_TASK_DONE_GOOD,
903 SCI_FAILURE_IO_REQUIRES_SCSI_ABORT);
904
905 sci_base_state_machine_change_state(&sci_req->state_machine,
906 SCI_BASE_REQUEST_STATE_COMPLETED);
907
908 /* Frame is decoded return it to the controller */
909 scic_sds_controller_release_frame(scic, frame_index);
910 }
911
912 return status;
913 }
914
915
916 /**
917 *
918 * @sci_req:
919 * @completion_code:
920 *
921 * enum sci_status
922 */
923 static enum sci_status scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler(
924
925 struct scic_sds_request *sci_req,
926 u32 completion_code)
927 {
928 enum sci_status status = SCI_SUCCESS;
929 bool all_frames_transferred = false;
930 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
931
932 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
933 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
934 /* Transmit data */
935 if (stp_req->type.pio.pio_transfer_bytes != 0) {
936 status = scic_sds_stp_request_pio_data_out_transmit_data(sci_req);
937 if (status == SCI_SUCCESS) {
938 if (stp_req->type.pio.pio_transfer_bytes == 0)
939 all_frames_transferred = true;
940 }
941 } else if (stp_req->type.pio.pio_transfer_bytes == 0) {
942 /*
943 * this will happen if the all data is written at the
944 * first time after the pio setup fis is received
945 */
946 all_frames_transferred = true;
947 }
948
949 /* all data transferred. */
950 if (all_frames_transferred) {
951 /*
952 * Change the state to SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_FRAME_SUBSTATE
953 * and wait for PIO_SETUP fis / or D2H REg fis. */
954 sci_base_state_machine_change_state(
955 &sci_req->started_substate_machine,
956 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
957 );
958 }
959 break;
960
961 default:
962 /*
963 * All other completion status cause the IO to be complete. If a NAK
964 * was received, then it is up to the user to retry the request. */
965 scic_sds_request_set_status(
966 sci_req,
967 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
968 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
969 );
970
971 sci_base_state_machine_change_state(
972 &sci_req->state_machine,
973 SCI_BASE_REQUEST_STATE_COMPLETED
974 );
975 break;
976 }
977
978 return status;
979 }
980
981 /**
982 *
983 * @request: This is the request which is receiving the event.
984 * @event_code: This is the event code that the request on which the request is
985 * expected to take action.
986 *
987 * This method will handle any link layer events while waiting for the data
988 * frame. enum sci_status SCI_SUCCESS SCI_FAILURE
989 */
990 static enum sci_status scic_sds_stp_request_pio_data_in_await_data_event_handler(
991 struct scic_sds_request *request,
992 u32 event_code)
993 {
994 enum sci_status status;
995
996 switch (scu_get_event_specifier(event_code)) {
997 case SCU_TASK_DONE_CRC_ERR << SCU_EVENT_SPECIFIC_CODE_SHIFT:
998 /*
999 * We are waiting for data and the SCU has R_ERR the data frame.
1000 * Go back to waiting for the D2H Register FIS */
1001 sci_base_state_machine_change_state(
1002 &request->started_substate_machine,
1003 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
1004 );
1005
1006 status = SCI_SUCCESS;
1007 break;
1008
1009 default:
1010 dev_err(scic_to_dev(request->owning_controller),
1011 "%s: SCIC PIO Request 0x%p received unexpected "
1012 "event 0x%08x\n",
1013 __func__, request, event_code);
1014
1015 /* / @todo Should we fail the PIO request when we get an unexpected event? */
1016 status = SCI_FAILURE;
1017 break;
1018 }
1019
1020 return status;
1021 }
1022
1023 /* --------------------------------------------------------------------------- */
1024
1025 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_pio_substate_handler_table[] = {
1026 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
1027 .abort_handler = scic_sds_request_started_state_abort_handler,
1028 .tc_completion_handler = scic_sds_stp_request_pio_await_h2d_completion_tc_completion_handler,
1029 },
1030 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
1031 .abort_handler = scic_sds_request_started_state_abort_handler,
1032 .frame_handler = scic_sds_stp_request_pio_await_frame_frame_handler
1033 },
1034 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
1035 .abort_handler = scic_sds_request_started_state_abort_handler,
1036 .event_handler = scic_sds_stp_request_pio_data_in_await_data_event_handler,
1037 .frame_handler = scic_sds_stp_request_pio_data_in_await_data_frame_handler
1038 },
1039 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
1040 .abort_handler = scic_sds_request_started_state_abort_handler,
1041 .tc_completion_handler = scic_sds_stp_request_pio_data_out_await_data_transmit_completion_tc_completion_handler,
1042 }
1043 };
1044
1045 static void scic_sds_stp_request_started_pio_await_h2d_completion_enter(
1046 void *object)
1047 {
1048 struct scic_sds_request *sci_req = object;
1049
1050 SET_STATE_HANDLER(
1051 sci_req,
1052 scic_sds_stp_request_started_pio_substate_handler_table,
1053 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE
1054 );
1055
1056 scic_sds_remote_device_set_working_request(
1057 sci_req->target_device, sci_req);
1058 }
1059
1060 static void scic_sds_stp_request_started_pio_await_frame_enter(void *object)
1061 {
1062 struct scic_sds_request *sci_req = object;
1063
1064 SET_STATE_HANDLER(
1065 sci_req,
1066 scic_sds_stp_request_started_pio_substate_handler_table,
1067 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE
1068 );
1069 }
1070
1071 static void scic_sds_stp_request_started_pio_data_in_await_data_enter(
1072 void *object)
1073 {
1074 struct scic_sds_request *sci_req = object;
1075
1076 SET_STATE_HANDLER(
1077 sci_req,
1078 scic_sds_stp_request_started_pio_substate_handler_table,
1079 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE
1080 );
1081 }
1082
1083 static void scic_sds_stp_request_started_pio_data_out_transmit_data_enter(
1084 void *object)
1085 {
1086 struct scic_sds_request *sci_req = object;
1087
1088 SET_STATE_HANDLER(
1089 sci_req,
1090 scic_sds_stp_request_started_pio_substate_handler_table,
1091 SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE
1092 );
1093 }
1094
1095 /* --------------------------------------------------------------------------- */
1096
1097 static const struct sci_base_state scic_sds_stp_request_started_pio_substate_table[] = {
1098 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE] = {
1099 .enter_state = scic_sds_stp_request_started_pio_await_h2d_completion_enter,
1100 },
1101 [SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_FRAME_SUBSTATE] = {
1102 .enter_state = scic_sds_stp_request_started_pio_await_frame_enter,
1103 },
1104 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_IN_AWAIT_DATA_SUBSTATE] = {
1105 .enter_state = scic_sds_stp_request_started_pio_data_in_await_data_enter,
1106 },
1107 [SCIC_SDS_STP_REQUEST_STARTED_PIO_DATA_OUT_TRANSMIT_DATA_SUBSTATE] = {
1108 .enter_state = scic_sds_stp_request_started_pio_data_out_transmit_data_enter,
1109 }
1110 };
1111
1112 enum sci_status
1113 scic_sds_stp_pio_request_construct(struct scic_sds_request *sci_req,
1114 bool copy_rx_frame)
1115 {
1116 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1117 struct scic_sds_stp_pio_request *pio = &stp_req->type.pio;
1118
1119 scic_sds_stp_non_ncq_request_construct(sci_req);
1120
1121 scu_stp_raw_request_construct_task_context(stp_req,
1122 sci_req->task_context_buffer);
1123
1124 pio->current_transfer_bytes = 0;
1125 pio->ending_error = 0;
1126 pio->ending_status = 0;
1127
1128 pio->request_current.sgl_offset = 0;
1129 pio->request_current.sgl_set = SCU_SGL_ELEMENT_PAIR_A;
1130
1131 if (copy_rx_frame) {
1132 scic_sds_request_build_sgl(sci_req);
1133 /* Since the IO request copy of the TC contains the same data as
1134 * the actual TC this pointer is vaild for either.
1135 */
1136 pio->request_current.sgl_pair = &sci_req->task_context_buffer->sgl_pair_ab;
1137 } else {
1138 /* The user does not want the data copied to the SGL buffer location */
1139 pio->request_current.sgl_pair = NULL;
1140 }
1141
1142 sci_base_state_machine_construct(&sci_req->started_substate_machine,
1143 sci_req,
1144 scic_sds_stp_request_started_pio_substate_table,
1145 SCIC_SDS_STP_REQUEST_STARTED_PIO_AWAIT_H2D_COMPLETION_SUBSTATE);
1146
1147 return SCI_SUCCESS;
1148 }
1149
1150 static void scic_sds_stp_request_udma_complete_request(
1151 struct scic_sds_request *request,
1152 u32 scu_status,
1153 enum sci_status sci_status)
1154 {
1155 scic_sds_request_set_status(request, scu_status, sci_status);
1156 sci_base_state_machine_change_state(&request->state_machine,
1157 SCI_BASE_REQUEST_STATE_COMPLETED);
1158 }
1159
1160 static enum sci_status scic_sds_stp_request_udma_general_frame_handler(struct scic_sds_request *sci_req,
1161 u32 frame_index)
1162 {
1163 struct scic_sds_controller *scic = sci_req->owning_controller;
1164 struct dev_to_host_fis *frame_header;
1165 enum sci_status status;
1166 u32 *frame_buffer;
1167
1168 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1169 frame_index,
1170 (void **)&frame_header);
1171
1172 if ((status == SCI_SUCCESS) &&
1173 (frame_header->fis_type == FIS_REGD2H)) {
1174 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1175 frame_index,
1176 (void **)&frame_buffer);
1177
1178 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1179 frame_header,
1180 frame_buffer);
1181 }
1182
1183 scic_sds_controller_release_frame(scic, frame_index);
1184
1185 return status;
1186 }
1187
1188 static enum sci_status scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler(
1189 struct scic_sds_request *sci_req,
1190 u32 completion_code)
1191 {
1192 enum sci_status status = SCI_SUCCESS;
1193
1194 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1195 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1196 scic_sds_stp_request_udma_complete_request(sci_req,
1197 SCU_TASK_DONE_GOOD,
1198 SCI_SUCCESS);
1199 break;
1200 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_UNEXP_FIS):
1201 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_REG_ERR):
1202 /*
1203 * We must check ther response buffer to see if the D2H Register FIS was
1204 * received before we got the TC completion. */
1205 if (sci_req->stp.rsp.fis_type == FIS_REGD2H) {
1206 scic_sds_remote_device_suspend(sci_req->target_device,
1207 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1208
1209 scic_sds_stp_request_udma_complete_request(sci_req,
1210 SCU_TASK_DONE_CHECK_RESPONSE,
1211 SCI_FAILURE_IO_RESPONSE_VALID);
1212 } else {
1213 /*
1214 * If we have an error completion status for the TC then we can expect a
1215 * D2H register FIS from the device so we must change state to wait for it */
1216 sci_base_state_machine_change_state(&sci_req->started_substate_machine,
1217 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE);
1218 }
1219 break;
1220
1221 /*
1222 * / @todo Check to see if any of these completion status need to wait for
1223 * / the device to host register fis. */
1224 /* / @todo We can retry the command for SCU_TASK_DONE_CMD_LL_R_ERR - this comes only for B0 */
1225 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_INV_FIS_LEN):
1226 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_MAX_PLD_ERR):
1227 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_LL_R_ERR):
1228 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CMD_LL_R_ERR):
1229 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_CRC_ERR):
1230 scic_sds_remote_device_suspend(sci_req->target_device,
1231 SCU_EVENT_SPECIFIC(SCU_NORMALIZE_COMPLETION_STATUS(completion_code)));
1232 /* Fall through to the default case */
1233 default:
1234 /* All other completion status cause the IO to be complete. */
1235 scic_sds_stp_request_udma_complete_request(sci_req,
1236 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1237 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR);
1238 break;
1239 }
1240
1241 return status;
1242 }
1243
1244 static enum sci_status scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler(
1245 struct scic_sds_request *sci_req,
1246 u32 frame_index)
1247 {
1248 enum sci_status status;
1249
1250 /* Use the general frame handler to copy the resposne data */
1251 status = scic_sds_stp_request_udma_general_frame_handler(sci_req, frame_index);
1252
1253 if (status != SCI_SUCCESS)
1254 return status;
1255
1256 scic_sds_stp_request_udma_complete_request(sci_req,
1257 SCU_TASK_DONE_CHECK_RESPONSE,
1258 SCI_FAILURE_IO_RESPONSE_VALID);
1259
1260 return status;
1261 }
1262
1263 /* --------------------------------------------------------------------------- */
1264
1265 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_udma_substate_handler_table[] = {
1266 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
1267 .abort_handler = scic_sds_request_started_state_abort_handler,
1268 .tc_completion_handler = scic_sds_stp_request_udma_await_tc_completion_tc_completion_handler,
1269 .frame_handler = scic_sds_stp_request_udma_general_frame_handler,
1270 },
1271 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
1272 .abort_handler = scic_sds_request_started_state_abort_handler,
1273 .frame_handler = scic_sds_stp_request_udma_await_d2h_reg_fis_frame_handler,
1274 },
1275 };
1276
1277 static void scic_sds_stp_request_started_udma_await_tc_completion_enter(
1278 void *object)
1279 {
1280 struct scic_sds_request *sci_req = object;
1281
1282 SET_STATE_HANDLER(
1283 sci_req,
1284 scic_sds_stp_request_started_udma_substate_handler_table,
1285 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
1286 );
1287 }
1288
1289 /**
1290 *
1291 *
1292 * This state is entered when there is an TC completion failure. The hardware
1293 * received an unexpected condition while processing the IO request and now
1294 * will UF the D2H register FIS to complete the IO.
1295 */
1296 static void scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter(
1297 void *object)
1298 {
1299 struct scic_sds_request *sci_req = object;
1300
1301 SET_STATE_HANDLER(
1302 sci_req,
1303 scic_sds_stp_request_started_udma_substate_handler_table,
1304 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE
1305 );
1306 }
1307
1308 /* --------------------------------------------------------------------------- */
1309
1310 static const struct sci_base_state scic_sds_stp_request_started_udma_substate_table[] = {
1311 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE] = {
1312 .enter_state = scic_sds_stp_request_started_udma_await_tc_completion_enter,
1313 },
1314 [SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_D2H_REG_FIS_SUBSTATE] = {
1315 .enter_state = scic_sds_stp_request_started_udma_await_d2h_reg_fis_enter,
1316 },
1317 };
1318
1319 enum sci_status scic_sds_stp_udma_request_construct(struct scic_sds_request *sci_req,
1320 u32 len,
1321 enum dma_data_direction dir)
1322 {
1323 scic_sds_stp_non_ncq_request_construct(sci_req);
1324
1325 scic_sds_stp_optimized_request_construct(sci_req, SCU_TASK_TYPE_DMA_IN,
1326 len, dir);
1327
1328 sci_base_state_machine_construct(
1329 &sci_req->started_substate_machine,
1330 sci_req,
1331 scic_sds_stp_request_started_udma_substate_table,
1332 SCIC_SDS_STP_REQUEST_STARTED_UDMA_AWAIT_TC_COMPLETION_SUBSTATE
1333 );
1334
1335 return SCI_SUCCESS;
1336 }
1337
1338 /**
1339 *
1340 * @sci_req:
1341 * @completion_code:
1342 *
1343 * This method processes a TC completion. The expected TC completion is for
1344 * the transmission of the H2D register FIS containing the SATA/STP non-data
1345 * request. This method always successfully processes the TC completion.
1346 * SCI_SUCCESS This value is always returned.
1347 */
1348 static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler(
1349 struct scic_sds_request *sci_req,
1350 u32 completion_code)
1351 {
1352 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1353 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1354 scic_sds_request_set_status(
1355 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1356 );
1357
1358 sci_base_state_machine_change_state(
1359 &sci_req->started_substate_machine,
1360 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
1361 );
1362 break;
1363
1364 default:
1365 /*
1366 * All other completion status cause the IO to be complete. If a NAK
1367 * was received, then it is up to the user to retry the request. */
1368 scic_sds_request_set_status(
1369 sci_req,
1370 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1371 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1372 );
1373
1374 sci_base_state_machine_change_state(
1375 &sci_req->state_machine, SCI_BASE_REQUEST_STATE_COMPLETED);
1376 break;
1377 }
1378
1379 return SCI_SUCCESS;
1380 }
1381
1382 /**
1383 *
1384 * @sci_req:
1385 * @completion_code:
1386 *
1387 * This method processes a TC completion. The expected TC completion is for
1388 * the transmission of the H2D register FIS containing the SATA/STP non-data
1389 * request. This method always successfully processes the TC completion.
1390 * SCI_SUCCESS This value is always returned.
1391 */
1392 static enum sci_status scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler(
1393 struct scic_sds_request *sci_req,
1394 u32 completion_code)
1395 {
1396 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
1397 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
1398 scic_sds_request_set_status(
1399 sci_req, SCU_TASK_DONE_GOOD, SCI_SUCCESS
1400 );
1401
1402 sci_base_state_machine_change_state(
1403 &sci_req->started_substate_machine,
1404 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
1405 );
1406 break;
1407
1408 default:
1409 /*
1410 * All other completion status cause the IO to be complete. If a NAK
1411 * was received, then it is up to the user to retry the request. */
1412 scic_sds_request_set_status(
1413 sci_req,
1414 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
1415 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
1416 );
1417
1418 sci_base_state_machine_change_state(&sci_req->state_machine,
1419 SCI_BASE_REQUEST_STATE_COMPLETED);
1420 break;
1421 }
1422
1423 return SCI_SUCCESS;
1424 }
1425
1426 /**
1427 *
1428 * @request: This parameter specifies the request for which a frame has been
1429 * received.
1430 * @frame_index: This parameter specifies the index of the frame that has been
1431 * received.
1432 *
1433 * This method processes frames received from the target while waiting for a
1434 * device to host register FIS. If a non-register FIS is received during this
1435 * time, it is treated as a protocol violation from an IO perspective. Indicate
1436 * if the received frame was processed successfully.
1437 */
1438 static enum sci_status scic_sds_stp_request_soft_reset_await_d2h_frame_handler(
1439 struct scic_sds_request *sci_req,
1440 u32 frame_index)
1441 {
1442 enum sci_status status;
1443 struct dev_to_host_fis *frame_header;
1444 u32 *frame_buffer;
1445 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1446 struct scic_sds_controller *scic = sci_req->owning_controller;
1447
1448 status = scic_sds_unsolicited_frame_control_get_header(&scic->uf_control,
1449 frame_index,
1450 (void **)&frame_header);
1451 if (status != SCI_SUCCESS) {
1452 dev_err(scic_to_dev(scic),
1453 "%s: SCIC IO Request 0x%p could not get frame header "
1454 "for frame index %d, status %x\n",
1455 __func__, stp_req, frame_index, status);
1456 return status;
1457 }
1458
1459 switch (frame_header->fis_type) {
1460 case FIS_REGD2H:
1461 scic_sds_unsolicited_frame_control_get_buffer(&scic->uf_control,
1462 frame_index,
1463 (void **)&frame_buffer);
1464
1465 scic_sds_controller_copy_sata_response(&sci_req->stp.rsp,
1466 frame_header,
1467 frame_buffer);
1468
1469 /* The command has completed with error */
1470 scic_sds_request_set_status(sci_req,
1471 SCU_TASK_DONE_CHECK_RESPONSE,
1472 SCI_FAILURE_IO_RESPONSE_VALID);
1473 break;
1474
1475 default:
1476 dev_warn(scic_to_dev(scic),
1477 "%s: IO Request:0x%p Frame Id:%d protocol "
1478 "violation occurred\n", __func__, stp_req,
1479 frame_index);
1480
1481 scic_sds_request_set_status(sci_req, SCU_TASK_DONE_UNEXP_FIS,
1482 SCI_FAILURE_PROTOCOL_VIOLATION);
1483 break;
1484 }
1485
1486 sci_base_state_machine_change_state(&sci_req->state_machine,
1487 SCI_BASE_REQUEST_STATE_COMPLETED);
1488
1489 /* Frame has been decoded return it to the controller */
1490 scic_sds_controller_release_frame(scic, frame_index);
1491
1492 return status;
1493 }
1494
1495 /* --------------------------------------------------------------------------- */
1496
1497 static const struct scic_sds_io_request_state_handler scic_sds_stp_request_started_soft_reset_substate_handler_table[] = {
1498 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
1499 .abort_handler = scic_sds_request_started_state_abort_handler,
1500 .tc_completion_handler = scic_sds_stp_request_soft_reset_await_h2d_asserted_tc_completion_handler,
1501 },
1502 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
1503 .abort_handler = scic_sds_request_started_state_abort_handler,
1504 .tc_completion_handler = scic_sds_stp_request_soft_reset_await_h2d_diagnostic_tc_completion_handler,
1505 },
1506 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
1507 .abort_handler = scic_sds_request_started_state_abort_handler,
1508 .frame_handler = scic_sds_stp_request_soft_reset_await_d2h_frame_handler,
1509 },
1510 };
1511
1512 static void scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter(
1513 void *object)
1514 {
1515 struct scic_sds_request *sci_req = object;
1516
1517 SET_STATE_HANDLER(
1518 sci_req,
1519 scic_sds_stp_request_started_soft_reset_substate_handler_table,
1520 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE
1521 );
1522
1523 scic_sds_remote_device_set_working_request(
1524 sci_req->target_device, sci_req
1525 );
1526 }
1527
1528 static void scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter(
1529 void *object)
1530 {
1531 struct scic_sds_request *sci_req = object;
1532 struct scu_task_context *task_context;
1533 struct host_to_dev_fis *h2d_fis;
1534 enum sci_status status;
1535
1536 /* Clear the SRST bit */
1537 h2d_fis = &sci_req->stp.cmd;
1538 h2d_fis->control = 0;
1539
1540 /* Clear the TC control bit */
1541 task_context = scic_sds_controller_get_task_context_buffer(
1542 sci_req->owning_controller, sci_req->io_tag);
1543 task_context->control_frame = 0;
1544
1545 status = scic_controller_continue_io(sci_req);
1546 if (status == SCI_SUCCESS) {
1547 SET_STATE_HANDLER(
1548 sci_req,
1549 scic_sds_stp_request_started_soft_reset_substate_handler_table,
1550 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE
1551 );
1552 }
1553 }
1554
1555 static void scic_sds_stp_request_started_soft_reset_await_d2h_response_enter(
1556 void *object)
1557 {
1558 struct scic_sds_request *sci_req = object;
1559
1560 SET_STATE_HANDLER(
1561 sci_req,
1562 scic_sds_stp_request_started_soft_reset_substate_handler_table,
1563 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE
1564 );
1565 }
1566
1567 static const struct sci_base_state scic_sds_stp_request_started_soft_reset_substate_table[] = {
1568 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE] = {
1569 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_asserted_completion_enter,
1570 },
1571 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_DIAGNOSTIC_COMPLETION_SUBSTATE] = {
1572 .enter_state = scic_sds_stp_request_started_soft_reset_await_h2d_diagnostic_completion_enter,
1573 },
1574 [SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_D2H_RESPONSE_FRAME_SUBSTATE] = {
1575 .enter_state = scic_sds_stp_request_started_soft_reset_await_d2h_response_enter,
1576 },
1577 };
1578
1579 enum sci_status scic_sds_stp_soft_reset_request_construct(struct scic_sds_request *sci_req)
1580 {
1581 struct scic_sds_stp_request *stp_req = &sci_req->stp.req;
1582
1583 scic_sds_stp_non_ncq_request_construct(sci_req);
1584
1585 /* Build the STP task context structure */
1586 scu_stp_raw_request_construct_task_context(stp_req, sci_req->task_context_buffer);
1587
1588 sci_base_state_machine_construct(&sci_req->started_substate_machine,
1589 sci_req,
1590 scic_sds_stp_request_started_soft_reset_substate_table,
1591 SCIC_SDS_STP_REQUEST_STARTED_SOFT_RESET_AWAIT_H2D_ASSERTED_COMPLETION_SUBSTATE);
1592
1593 return SCI_SUCCESS;
1594 }
This page took 0.079242 seconds and 4 git commands to generate.