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