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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
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.
57 #include "state_machine.h"
58 #include "remote_device.h"
59 #include "scic_sds_request.h"
60 #include "scic_sds_smp_request.h"
62 #include "scu_completion_codes.h"
63 #include "scu_task_context.h"
66 static void scu_smp_request_construct_task_context(
67 struct scic_sds_request
*sci_req
,
68 struct smp_req
*smp_req
);
70 void scic_sds_smp_request_assign_buffers(struct scic_sds_request
*sci_req
)
72 if (sci_req
->was_tag_assigned_by_user
== false)
73 sci_req
->task_context_buffer
= &sci_req
->tc
;
77 * This function will fill in the SCU Task Context for a SMP request. The
78 * following important settings are utilized: -# task_type ==
79 * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
80 * (i.e. non-raw frame) is being utilized to perform task management. -#
81 * control_frame == 1. This ensures that the proper endianess is set so
82 * that the bytes are transmitted in the right order for a smp request frame.
83 * @sci_req: This parameter specifies the smp request object being
88 scu_smp_request_construct_task_context(struct scic_sds_request
*sci_req
,
89 struct smp_req
*smp_req
)
92 struct scic_sds_controller
*scic
;
93 struct scic_sds_remote_device
*sci_dev
;
94 struct scic_sds_port
*sci_port
;
95 struct scu_task_context
*task_context
;
96 ssize_t word_cnt
= sizeof(struct smp_req
) / sizeof(u32
);
98 /* byte swap the smp request. */
99 sci_swab32_cpy(&sci_req
->smp
.cmd
, smp_req
,
102 task_context
= scic_sds_request_get_task_context(sci_req
);
104 scic
= scic_sds_request_get_controller(sci_req
);
105 sci_dev
= scic_sds_request_get_device(sci_req
);
106 sci_port
= scic_sds_request_get_port(sci_req
);
109 * Fill in the TC with the its required data
112 task_context
->priority
= 0;
113 task_context
->initiator_request
= 1;
114 task_context
->connection_rate
= sci_dev
->connection_rate
;
115 task_context
->protocol_engine_index
=
116 scic_sds_controller_get_protocol_engine_group(scic
);
117 task_context
->logical_port_index
= scic_sds_port_get_index(sci_port
);
118 task_context
->protocol_type
= SCU_TASK_CONTEXT_PROTOCOL_SMP
;
119 task_context
->abort
= 0;
120 task_context
->valid
= SCU_TASK_CONTEXT_VALID
;
121 task_context
->context_type
= SCU_TASK_CONTEXT_TYPE
;
124 task_context
->remote_node_index
= sci_dev
->rnc
.remote_node_index
;
125 task_context
->command_code
= 0;
126 task_context
->task_type
= SCU_TASK_TYPE_SMP_REQUEST
;
129 task_context
->link_layer_control
= 0;
130 task_context
->do_not_dma_ssp_good_response
= 1;
131 task_context
->strict_ordering
= 0;
132 task_context
->control_frame
= 1;
133 task_context
->timeout_enable
= 0;
134 task_context
->block_guard_enable
= 0;
137 task_context
->address_modifier
= 0;
140 task_context
->ssp_command_iu_length
= smp_req
->req_len
;
143 task_context
->transfer_length_bytes
= 0;
146 * 18h ~ 30h, protocol specific
147 * since commandIU has been build by framework at this point, we just
148 * copy the frist DWord from command IU to this location. */
149 memcpy(&task_context
->type
.smp
, &sci_req
->smp
.cmd
, sizeof(u32
));
153 * "For SMP you could program it to zero. We would prefer that way
154 * so that done code will be consistent." - Venki
156 task_context
->task_phase
= 0;
158 if (sci_req
->was_tag_assigned_by_user
) {
160 * Build the task context now since we have already read
163 sci_req
->post_context
=
164 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
165 (scic_sds_controller_get_protocol_engine_group(scic
) <<
166 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
167 (scic_sds_port_get_index(sci_port
) <<
168 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
) |
169 scic_sds_io_tag_get_index(sci_req
->io_tag
));
172 * Build the task context now since we have already read
174 * I/O tag index is not assigned because we have to wait
175 * until we get a TCi.
177 sci_req
->post_context
=
178 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
179 (scic_sds_controller_get_protocol_engine_group(scic
) <<
180 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
181 (scic_sds_port_get_index(sci_port
) <<
182 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
));
186 * Copy the physical address for the command buffer to the SCU Task
187 * Context command buffer should not contain command header.
189 dma_addr
= scic_io_request_get_dma_addr(sci_req
,
190 ((char *) &sci_req
->smp
.cmd
) +
193 task_context
->command_iu_upper
= upper_32_bits(dma_addr
);
194 task_context
->command_iu_lower
= lower_32_bits(dma_addr
);
196 /* SMP response comes as UF, so no need to set response IU address. */
197 task_context
->response_iu_upper
= 0;
198 task_context
->response_iu_lower
= 0;
202 * This function processes an unsolicited frame while the SMP request is waiting
203 * for a response frame. It will copy the response data, release the
204 * unsolicited frame, and transition the request to the
205 * SCI_BASE_REQUEST_STATE_COMPLETED state.
206 * @sci_req: This parameter specifies the request for which the
207 * unsolicited frame was received.
208 * @frame_index: This parameter indicates the unsolicited frame index that
209 * should contain the response.
211 * This function returns an indication of whether the response frame was handled
212 * successfully or not. SCI_SUCCESS Currently this value is always returned and
213 * indicates successful processing of the TC response.
215 static enum sci_status
216 scic_sds_smp_request_await_response_frame_handler(struct scic_sds_request
*sci_req
,
219 enum sci_status status
;
221 struct smp_resp
*rsp_hdr
= &sci_req
->smp
.rsp
;
222 ssize_t word_cnt
= SMP_RESP_HDR_SZ
/ sizeof(u32
);
224 status
= scic_sds_unsolicited_frame_control_get_header(
225 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
229 /* byte swap the header. */
230 sci_swab32_cpy(rsp_hdr
, frame_header
, word_cnt
);
232 if (rsp_hdr
->frame_type
== SMP_RESPONSE
) {
235 status
= scic_sds_unsolicited_frame_control_get_buffer(
236 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
240 word_cnt
= (sizeof(struct smp_req
) - SMP_RESP_HDR_SZ
) /
243 sci_swab32_cpy(((u8
*) rsp_hdr
) + SMP_RESP_HDR_SZ
,
246 scic_sds_request_set_status(
247 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
);
249 sci_base_state_machine_change_state(
250 &sci_req
->started_substate_machine
,
251 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
);
253 /* This was not a response frame why did it get forwarded? */
254 dev_err(scic_to_dev(sci_req
->owning_controller
),
255 "%s: SCIC SMP Request 0x%p received unexpected frame "
260 rsp_hdr
->frame_type
);
262 scic_sds_request_set_status(
264 SCU_TASK_DONE_SMP_FRM_TYPE_ERR
,
265 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
);
267 sci_base_state_machine_change_state(
268 &sci_req
->state_machine
,
269 SCI_BASE_REQUEST_STATE_COMPLETED
);
272 scic_sds_controller_release_frame(sci_req
->owning_controller
,
280 * This method processes an abnormal TC completion while the SMP request is
281 * waiting for a response frame. It decides what happened to the IO based
282 * on TC completion status.
283 * @sci_req: This parameter specifies the request for which the TC
284 * completion was received.
285 * @completion_code: This parameter indicates the completion status information
288 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
289 * this method always returns success.
291 static enum sci_status
scic_sds_smp_request_await_response_tc_completion_handler(
292 struct scic_sds_request
*sci_req
,
295 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
296 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
298 * In the AWAIT RESPONSE state, any TC completion is unexpected.
299 * but if the TC has success status, we complete the IO anyway. */
300 scic_sds_request_set_status(
301 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
304 sci_base_state_machine_change_state(
305 &sci_req
->state_machine
,
306 SCI_BASE_REQUEST_STATE_COMPLETED
);
309 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR
):
310 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR
):
311 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR
):
312 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR
):
314 * These status has been seen in a specific LSI expander, which sometimes
315 * is not able to send smp response within 2 ms. This causes our hardware
316 * break the connection and set TC completion with one of these SMP_XXX_XX_ERR
317 * status. For these type of error, we ask scic user to retry the request. */
318 scic_sds_request_set_status(
319 sci_req
, SCU_TASK_DONE_SMP_RESP_TO_ERR
, SCI_FAILURE_RETRY_REQUIRED
322 sci_base_state_machine_change_state(
323 &sci_req
->state_machine
,
324 SCI_BASE_REQUEST_STATE_COMPLETED
);
329 * All other completion status cause the IO to be complete. If a NAK
330 * was received, then it is up to the user to retry the request. */
331 scic_sds_request_set_status(
333 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
334 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
337 sci_base_state_machine_change_state(
338 &sci_req
->state_machine
,
339 SCI_BASE_REQUEST_STATE_COMPLETED
);
348 * This method processes the completions transport layer (TL) status to
349 * determine if the SMP request was sent successfully. If the SMP request
350 * was sent successfully, then the state for the SMP request transits to
351 * waiting for a response frame.
352 * @sci_req: This parameter specifies the request for which the TC
353 * completion was received.
354 * @completion_code: This parameter indicates the completion status information
357 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
358 * this method always returns success.
360 static enum sci_status
scic_sds_smp_request_await_tc_completion_tc_completion_handler(
361 struct scic_sds_request
*sci_req
,
364 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
365 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
366 scic_sds_request_set_status(
367 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
370 sci_base_state_machine_change_state(
371 &sci_req
->state_machine
,
372 SCI_BASE_REQUEST_STATE_COMPLETED
);
377 * All other completion status cause the IO to be complete. If a NAK
378 * was received, then it is up to the user to retry the request. */
379 scic_sds_request_set_status(
381 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
382 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
385 sci_base_state_machine_change_state(
386 &sci_req
->state_machine
,
387 SCI_BASE_REQUEST_STATE_COMPLETED
);
395 static const struct scic_sds_io_request_state_handler scic_sds_smp_request_started_substate_handler_table
[] = {
396 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
397 .abort_handler
= scic_sds_request_started_state_abort_handler
,
398 .tc_completion_handler
= scic_sds_smp_request_await_response_tc_completion_handler
,
399 .frame_handler
= scic_sds_smp_request_await_response_frame_handler
,
401 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
402 .abort_handler
= scic_sds_request_started_state_abort_handler
,
403 .tc_completion_handler
= scic_sds_smp_request_await_tc_completion_tc_completion_handler
,
408 * This method performs the actions required when entering the
409 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_RESPONSE sub-state. This
410 * includes setting the IO request state handlers for this sub-state.
411 * @object: This parameter specifies the request object for which the sub-state
412 * change is occurring.
416 static void scic_sds_smp_request_started_await_response_substate_enter(
419 struct scic_sds_request
*sci_req
= object
;
423 scic_sds_smp_request_started_substate_handler_table
,
424 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
429 * This method performs the actions required when entering the
430 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION sub-state.
431 * This includes setting the SMP request state handlers for this sub-state.
432 * @object: This parameter specifies the request object for which the sub-state
433 * change is occurring.
437 static void scic_sds_smp_request_started_await_tc_completion_substate_enter(
440 struct scic_sds_request
*sci_req
= object
;
444 scic_sds_smp_request_started_substate_handler_table
,
445 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
449 static const struct sci_base_state scic_sds_smp_request_started_substate_table
[] = {
450 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
451 .enter_state
= scic_sds_smp_request_started_await_response_substate_enter
,
453 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
454 .enter_state
= scic_sds_smp_request_started_await_tc_completion_substate_enter
,
459 * This method is called by the SCI user to build an SMP IO request.
461 * - The user must have previously called scic_io_request_construct() on the
462 * supplied IO request. Indicate if the controller successfully built the IO
463 * request. SCI_SUCCESS This value is returned if the IO request was
464 * successfully built. SCI_FAILURE_UNSUPPORTED_PROTOCOL This value is returned
465 * if the remote_device does not support the SMP protocol.
466 * SCI_FAILURE_INVALID_ASSOCIATION This value is returned if the user did not
467 * properly set the association between the SCIC IO request and the user's IO
470 enum sci_status
scic_io_request_construct_smp(struct scic_sds_request
*sci_req
)
472 struct smp_req
*smp_req
= kmalloc(sizeof(*smp_req
), GFP_KERNEL
);
475 return SCI_FAILURE_INSUFFICIENT_RESOURCES
;
477 sci_req
->protocol
= SCIC_SMP_PROTOCOL
;
478 sci_req
->has_started_substate_machine
= true;
480 /* Construct the started sub-state machine. */
481 sci_base_state_machine_construct(
482 &sci_req
->started_substate_machine
,
484 scic_sds_smp_request_started_substate_table
,
485 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
488 /* Construct the SMP SCU Task Context */
489 memcpy(smp_req
, &sci_req
->smp
.cmd
, sizeof(*smp_req
));
492 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
493 * functions under SAS 2.0, a zero request length really indicates
494 * a non-zero default length. */
495 if (smp_req
->req_len
== 0) {
496 switch (smp_req
->func
) {
498 case SMP_REPORT_PHY_ERR_LOG
:
499 case SMP_REPORT_PHY_SATA
:
500 case SMP_REPORT_ROUTE_INFO
:
501 smp_req
->req_len
= 2;
503 case SMP_CONF_ROUTE_INFO
:
504 case SMP_PHY_CONTROL
:
505 case SMP_PHY_TEST_FUNCTION
:
506 smp_req
->req_len
= 9;
508 /* Default - zero is a valid default for 2.0. */
512 scu_smp_request_construct_task_context(sci_req
, smp_req
);
514 sci_base_state_machine_change_state(&sci_req
->state_machine
,
515 SCI_BASE_REQUEST_STATE_CONSTRUCTED
);