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"
60 #include "scu_completion_codes.h"
61 #include "scu_task_context.h"
64 static void scu_smp_request_construct_task_context(
65 struct scic_sds_request
*sci_req
,
66 struct smp_req
*smp_req
);
68 void scic_sds_smp_request_assign_buffers(struct scic_sds_request
*sci_req
)
70 if (sci_req
->was_tag_assigned_by_user
== false)
71 sci_req
->task_context_buffer
= &sci_req
->tc
;
75 * This function will fill in the SCU Task Context for a SMP request. The
76 * following important settings are utilized: -# task_type ==
77 * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
78 * (i.e. non-raw frame) is being utilized to perform task management. -#
79 * control_frame == 1. This ensures that the proper endianess is set so
80 * that the bytes are transmitted in the right order for a smp request frame.
81 * @sci_req: This parameter specifies the smp request object being
86 scu_smp_request_construct_task_context(struct scic_sds_request
*sci_req
,
87 struct smp_req
*smp_req
)
90 struct scic_sds_controller
*scic
;
91 struct scic_sds_remote_device
*sci_dev
;
92 struct scic_sds_port
*sci_port
;
93 struct scu_task_context
*task_context
;
94 ssize_t word_cnt
= sizeof(struct smp_req
) / sizeof(u32
);
96 /* byte swap the smp request. */
97 sci_swab32_cpy(&sci_req
->smp
.cmd
, smp_req
,
100 task_context
= scic_sds_request_get_task_context(sci_req
);
102 scic
= scic_sds_request_get_controller(sci_req
);
103 sci_dev
= scic_sds_request_get_device(sci_req
);
104 sci_port
= scic_sds_request_get_port(sci_req
);
107 * Fill in the TC with the its required data
110 task_context
->priority
= 0;
111 task_context
->initiator_request
= 1;
112 task_context
->connection_rate
= sci_dev
->connection_rate
;
113 task_context
->protocol_engine_index
=
114 scic_sds_controller_get_protocol_engine_group(scic
);
115 task_context
->logical_port_index
= scic_sds_port_get_index(sci_port
);
116 task_context
->protocol_type
= SCU_TASK_CONTEXT_PROTOCOL_SMP
;
117 task_context
->abort
= 0;
118 task_context
->valid
= SCU_TASK_CONTEXT_VALID
;
119 task_context
->context_type
= SCU_TASK_CONTEXT_TYPE
;
122 task_context
->remote_node_index
= sci_dev
->rnc
.remote_node_index
;
123 task_context
->command_code
= 0;
124 task_context
->task_type
= SCU_TASK_TYPE_SMP_REQUEST
;
127 task_context
->link_layer_control
= 0;
128 task_context
->do_not_dma_ssp_good_response
= 1;
129 task_context
->strict_ordering
= 0;
130 task_context
->control_frame
= 1;
131 task_context
->timeout_enable
= 0;
132 task_context
->block_guard_enable
= 0;
135 task_context
->address_modifier
= 0;
138 task_context
->ssp_command_iu_length
= smp_req
->req_len
;
141 task_context
->transfer_length_bytes
= 0;
144 * 18h ~ 30h, protocol specific
145 * since commandIU has been build by framework at this point, we just
146 * copy the frist DWord from command IU to this location. */
147 memcpy(&task_context
->type
.smp
, &sci_req
->smp
.cmd
, sizeof(u32
));
151 * "For SMP you could program it to zero. We would prefer that way
152 * so that done code will be consistent." - Venki
154 task_context
->task_phase
= 0;
156 if (sci_req
->was_tag_assigned_by_user
) {
158 * Build the task context now since we have already read
161 sci_req
->post_context
=
162 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
163 (scic_sds_controller_get_protocol_engine_group(scic
) <<
164 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
165 (scic_sds_port_get_index(sci_port
) <<
166 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
) |
167 scic_sds_io_tag_get_index(sci_req
->io_tag
));
170 * Build the task context now since we have already read
172 * I/O tag index is not assigned because we have to wait
173 * until we get a TCi.
175 sci_req
->post_context
=
176 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
177 (scic_sds_controller_get_protocol_engine_group(scic
) <<
178 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
179 (scic_sds_port_get_index(sci_port
) <<
180 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
));
184 * Copy the physical address for the command buffer to the SCU Task
185 * Context command buffer should not contain command header.
187 dma_addr
= scic_io_request_get_dma_addr(sci_req
,
188 ((char *) &sci_req
->smp
.cmd
) +
191 task_context
->command_iu_upper
= upper_32_bits(dma_addr
);
192 task_context
->command_iu_lower
= lower_32_bits(dma_addr
);
194 /* SMP response comes as UF, so no need to set response IU address. */
195 task_context
->response_iu_upper
= 0;
196 task_context
->response_iu_lower
= 0;
200 * This function processes an unsolicited frame while the SMP request is waiting
201 * for a response frame. It will copy the response data, release the
202 * unsolicited frame, and transition the request to the
203 * SCI_BASE_REQUEST_STATE_COMPLETED state.
204 * @sci_req: This parameter specifies the request for which the
205 * unsolicited frame was received.
206 * @frame_index: This parameter indicates the unsolicited frame index that
207 * should contain the response.
209 * This function returns an indication of whether the response frame was handled
210 * successfully or not. SCI_SUCCESS Currently this value is always returned and
211 * indicates successful processing of the TC response.
213 static enum sci_status
214 scic_sds_smp_request_await_response_frame_handler(struct scic_sds_request
*sci_req
,
217 enum sci_status status
;
219 struct smp_resp
*rsp_hdr
= &sci_req
->smp
.rsp
;
220 ssize_t word_cnt
= SMP_RESP_HDR_SZ
/ sizeof(u32
);
222 status
= scic_sds_unsolicited_frame_control_get_header(
223 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
227 /* byte swap the header. */
228 sci_swab32_cpy(rsp_hdr
, frame_header
, word_cnt
);
230 if (rsp_hdr
->frame_type
== SMP_RESPONSE
) {
233 status
= scic_sds_unsolicited_frame_control_get_buffer(
234 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
238 word_cnt
= (sizeof(struct smp_req
) - SMP_RESP_HDR_SZ
) /
241 sci_swab32_cpy(((u8
*) rsp_hdr
) + SMP_RESP_HDR_SZ
,
244 scic_sds_request_set_status(
245 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
);
247 sci_base_state_machine_change_state(
248 &sci_req
->started_substate_machine
,
249 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
);
251 /* This was not a response frame why did it get forwarded? */
252 dev_err(scic_to_dev(sci_req
->owning_controller
),
253 "%s: SCIC SMP Request 0x%p received unexpected frame "
258 rsp_hdr
->frame_type
);
260 scic_sds_request_set_status(
262 SCU_TASK_DONE_SMP_FRM_TYPE_ERR
,
263 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
);
265 sci_base_state_machine_change_state(
266 &sci_req
->state_machine
,
267 SCI_BASE_REQUEST_STATE_COMPLETED
);
270 scic_sds_controller_release_frame(sci_req
->owning_controller
,
278 * This method processes an abnormal TC completion while the SMP request is
279 * waiting for a response frame. It decides what happened to the IO based
280 * on TC completion status.
281 * @sci_req: This parameter specifies the request for which the TC
282 * completion was received.
283 * @completion_code: This parameter indicates the completion status information
286 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
287 * this method always returns success.
289 static enum sci_status
scic_sds_smp_request_await_response_tc_completion_handler(
290 struct scic_sds_request
*sci_req
,
293 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
294 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
296 * In the AWAIT RESPONSE state, any TC completion is unexpected.
297 * but if the TC has success status, we complete the IO anyway. */
298 scic_sds_request_set_status(
299 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
302 sci_base_state_machine_change_state(
303 &sci_req
->state_machine
,
304 SCI_BASE_REQUEST_STATE_COMPLETED
);
307 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR
):
308 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR
):
309 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR
):
310 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR
):
312 * These status has been seen in a specific LSI expander, which sometimes
313 * is not able to send smp response within 2 ms. This causes our hardware
314 * break the connection and set TC completion with one of these SMP_XXX_XX_ERR
315 * status. For these type of error, we ask scic user to retry the request. */
316 scic_sds_request_set_status(
317 sci_req
, SCU_TASK_DONE_SMP_RESP_TO_ERR
, SCI_FAILURE_RETRY_REQUIRED
320 sci_base_state_machine_change_state(
321 &sci_req
->state_machine
,
322 SCI_BASE_REQUEST_STATE_COMPLETED
);
327 * All other completion status cause the IO to be complete. If a NAK
328 * was received, then it is up to the user to retry the request. */
329 scic_sds_request_set_status(
331 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
332 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
335 sci_base_state_machine_change_state(
336 &sci_req
->state_machine
,
337 SCI_BASE_REQUEST_STATE_COMPLETED
);
346 * This method processes the completions transport layer (TL) status to
347 * determine if the SMP request was sent successfully. If the SMP request
348 * was sent successfully, then the state for the SMP request transits to
349 * waiting for a response frame.
350 * @sci_req: This parameter specifies the request for which the TC
351 * completion was received.
352 * @completion_code: This parameter indicates the completion status information
355 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
356 * this method always returns success.
358 static enum sci_status
scic_sds_smp_request_await_tc_completion_tc_completion_handler(
359 struct scic_sds_request
*sci_req
,
362 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
363 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
364 scic_sds_request_set_status(
365 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
368 sci_base_state_machine_change_state(
369 &sci_req
->state_machine
,
370 SCI_BASE_REQUEST_STATE_COMPLETED
);
375 * All other completion status cause the IO to be complete. If a NAK
376 * was received, then it is up to the user to retry the request. */
377 scic_sds_request_set_status(
379 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
380 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
383 sci_base_state_machine_change_state(
384 &sci_req
->state_machine
,
385 SCI_BASE_REQUEST_STATE_COMPLETED
);
393 static const struct scic_sds_io_request_state_handler scic_sds_smp_request_started_substate_handler_table
[] = {
394 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
395 .abort_handler
= scic_sds_request_started_state_abort_handler
,
396 .tc_completion_handler
= scic_sds_smp_request_await_response_tc_completion_handler
,
397 .frame_handler
= scic_sds_smp_request_await_response_frame_handler
,
399 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
400 .abort_handler
= scic_sds_request_started_state_abort_handler
,
401 .tc_completion_handler
= scic_sds_smp_request_await_tc_completion_tc_completion_handler
,
406 * This method performs the actions required when entering the
407 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_RESPONSE sub-state. This
408 * includes setting the IO request state handlers for this sub-state.
409 * @object: This parameter specifies the request object for which the sub-state
410 * change is occurring.
414 static void scic_sds_smp_request_started_await_response_substate_enter(
417 struct scic_sds_request
*sci_req
= object
;
421 scic_sds_smp_request_started_substate_handler_table
,
422 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
427 * This method performs the actions required when entering the
428 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION sub-state.
429 * This includes setting the SMP request state handlers for this sub-state.
430 * @object: This parameter specifies the request object for which the sub-state
431 * change is occurring.
435 static void scic_sds_smp_request_started_await_tc_completion_substate_enter(
438 struct scic_sds_request
*sci_req
= object
;
442 scic_sds_smp_request_started_substate_handler_table
,
443 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
447 static const struct sci_base_state scic_sds_smp_request_started_substate_table
[] = {
448 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
449 .enter_state
= scic_sds_smp_request_started_await_response_substate_enter
,
451 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
452 .enter_state
= scic_sds_smp_request_started_await_tc_completion_substate_enter
,
457 * This method is called by the SCI user to build an SMP IO request.
459 * - The user must have previously called scic_io_request_construct() on the
460 * supplied IO request. Indicate if the controller successfully built the IO
461 * request. SCI_SUCCESS This value is returned if the IO request was
462 * successfully built. SCI_FAILURE_UNSUPPORTED_PROTOCOL This value is returned
463 * if the remote_device does not support the SMP protocol.
464 * SCI_FAILURE_INVALID_ASSOCIATION This value is returned if the user did not
465 * properly set the association between the SCIC IO request and the user's IO
468 enum sci_status
scic_io_request_construct_smp(struct scic_sds_request
*sci_req
)
470 struct smp_req
*smp_req
= kmalloc(sizeof(*smp_req
), GFP_KERNEL
);
473 return SCI_FAILURE_INSUFFICIENT_RESOURCES
;
475 sci_req
->protocol
= SCIC_SMP_PROTOCOL
;
476 sci_req
->has_started_substate_machine
= true;
478 /* Construct the started sub-state machine. */
479 sci_base_state_machine_construct(
480 &sci_req
->started_substate_machine
,
482 scic_sds_smp_request_started_substate_table
,
483 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
486 /* Construct the SMP SCU Task Context */
487 memcpy(smp_req
, &sci_req
->smp
.cmd
, sizeof(*smp_req
));
490 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
491 * functions under SAS 2.0, a zero request length really indicates
492 * a non-zero default length. */
493 if (smp_req
->req_len
== 0) {
494 switch (smp_req
->func
) {
496 case SMP_REPORT_PHY_ERR_LOG
:
497 case SMP_REPORT_PHY_SATA
:
498 case SMP_REPORT_ROUTE_INFO
:
499 smp_req
->req_len
= 2;
501 case SMP_CONF_ROUTE_INFO
:
502 case SMP_PHY_CONTROL
:
503 case SMP_PHY_TEST_FUNCTION
:
504 smp_req
->req_len
= 9;
506 /* Default - zero is a valid default for 2.0. */
510 scu_smp_request_construct_task_context(sci_req
, smp_req
);
512 sci_base_state_machine_change_state(&sci_req
->state_machine
,
513 SCI_BASE_REQUEST_STATE_CONSTRUCTED
);