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.
58 #include "intel_sas.h"
59 #include "sci_base_state_machine.h"
60 #include "scic_controller.h"
61 #include "scic_sds_controller.h"
62 #include "remote_device.h"
63 #include "scic_sds_request.h"
64 #include "scic_sds_smp_request.h"
65 #include "sci_environment.h"
67 #include "scu_completion_codes.h"
68 #include "scu_task_context.h"
70 static void scu_smp_request_construct_task_context(
71 struct scic_sds_request
*sci_req
,
72 struct smp_req
*smp_req
);
77 * This method return the memory space required for STP PIO requests. u32
79 u32
scic_sds_smp_request_get_object_size(void)
81 return sizeof(struct scic_sds_request
)
82 + sizeof(struct smp_req
)
83 + sizeof(struct smp_resp
)
84 + sizeof(struct scu_task_context
)
89 * scic_sds_smp_request_get_command_buffer() -
91 * This macro returns the address of the smp command buffer in the smp request
92 * memory. No need to cast to SMP request type.
94 #define scic_sds_smp_request_get_command_buffer(memory) \
95 (((char *)(memory)) + sizeof(struct scic_sds_request))
98 * scic_sds_smp_request_get_response_buffer() -
100 * This macro returns the address of the smp response buffer in the smp request
103 #define scic_sds_smp_request_get_response_buffer(memory) \
104 (((char *)(scic_sds_smp_request_get_command_buffer(memory))) \
105 + sizeof(struct smp_req))
108 * scic_sds_smp_request_get_task_context_buffer() -
110 * This macro returs the task context buffer for the SMP request.
112 #define scic_sds_smp_request_get_task_context_buffer(memory) \
113 ((struct scu_task_context *)(\
114 ((char *)(scic_sds_smp_request_get_response_buffer(memory))) \
115 + sizeof(struct smp_resp) \
121 * This method build the remainder of the IO request object.
122 * @sci_req: This parameter specifies the request object being constructed.
124 * The scic_sds_general_request_construct() must be called before this call is
128 void scic_sds_smp_request_assign_buffers(
129 struct scic_sds_request
*sci_req
)
131 /* Assign all of the buffer pointers */
132 sci_req
->command_buffer
=
133 scic_sds_smp_request_get_command_buffer(sci_req
);
134 sci_req
->response_buffer
=
135 scic_sds_smp_request_get_response_buffer(sci_req
);
136 sci_req
->sgl_element_pair_buffer
= NULL
;
138 if (sci_req
->was_tag_assigned_by_user
== false) {
139 sci_req
->task_context_buffer
=
140 scic_sds_smp_request_get_task_context_buffer(sci_req
);
141 sci_req
->task_context_buffer
=
142 PTR_ALIGN(sci_req
->task_context_buffer
, SMP_CACHE_BYTES
);
148 * This function will fill in the SCU Task Context for a SMP request. The
149 * following important settings are utilized: -# task_type ==
150 * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
151 * (i.e. non-raw frame) is being utilized to perform task management. -#
152 * control_frame == 1. This ensures that the proper endianess is set so
153 * that the bytes are transmitted in the right order for a smp request frame.
154 * @sci_req: This parameter specifies the smp request object being
159 scu_smp_request_construct_task_context(struct scic_sds_request
*sci_req
,
160 struct smp_req
*smp_req
)
163 struct scic_sds_controller
*scic
;
164 struct scic_sds_remote_device
*sci_dev
;
165 struct scic_sds_port
*sci_port
;
166 struct scu_task_context
*task_context
;
167 ssize_t word_cnt
= sizeof(struct smp_req
) / sizeof(u32
);
169 /* byte swap the smp request. */
170 sci_swab32_cpy(sci_req
->command_buffer
, smp_req
,
173 task_context
= scic_sds_request_get_task_context(sci_req
);
175 scic
= scic_sds_request_get_controller(sci_req
);
176 sci_dev
= scic_sds_request_get_device(sci_req
);
177 sci_port
= scic_sds_request_get_port(sci_req
);
180 * Fill in the TC with the its required data
183 task_context
->priority
= 0;
184 task_context
->initiator_request
= 1;
185 task_context
->connection_rate
= sci_dev
->connection_rate
;
186 task_context
->protocol_engine_index
=
187 scic_sds_controller_get_protocol_engine_group(scic
);
188 task_context
->logical_port_index
= scic_sds_port_get_index(sci_port
);
189 task_context
->protocol_type
= SCU_TASK_CONTEXT_PROTOCOL_SMP
;
190 task_context
->abort
= 0;
191 task_context
->valid
= SCU_TASK_CONTEXT_VALID
;
192 task_context
->context_type
= SCU_TASK_CONTEXT_TYPE
;
195 task_context
->remote_node_index
= sci_dev
->rnc
.remote_node_index
;
196 task_context
->command_code
= 0;
197 task_context
->task_type
= SCU_TASK_TYPE_SMP_REQUEST
;
200 task_context
->link_layer_control
= 0;
201 task_context
->do_not_dma_ssp_good_response
= 1;
202 task_context
->strict_ordering
= 0;
203 task_context
->control_frame
= 1;
204 task_context
->timeout_enable
= 0;
205 task_context
->block_guard_enable
= 0;
208 task_context
->address_modifier
= 0;
211 task_context
->ssp_command_iu_length
= smp_req
->req_len
;
214 task_context
->transfer_length_bytes
= 0;
217 * 18h ~ 30h, protocol specific
218 * since commandIU has been build by framework at this point, we just
219 * copy the frist DWord from command IU to this location. */
220 memcpy((void *)(&task_context
->type
.smp
),
221 sci_req
->command_buffer
,
226 * "For SMP you could program it to zero. We would prefer that way
227 * so that done code will be consistent." - Venki
229 task_context
->task_phase
= 0;
231 if (sci_req
->was_tag_assigned_by_user
) {
233 * Build the task context now since we have already read
236 sci_req
->post_context
=
237 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
238 (scic_sds_controller_get_protocol_engine_group(scic
) <<
239 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
240 (scic_sds_port_get_index(sci_port
) <<
241 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
) |
242 scic_sds_io_tag_get_index(sci_req
->io_tag
));
245 * Build the task context now since we have already read
247 * I/O tag index is not assigned because we have to wait
248 * until we get a TCi.
250 sci_req
->post_context
=
251 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC
|
252 (scic_sds_controller_get_protocol_engine_group(scic
) <<
253 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT
) |
254 (scic_sds_port_get_index(sci_port
) <<
255 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT
));
259 * Copy the physical address for the command buffer to the SCU Task
260 * Context command buffer should not contain command header.
262 dma_addr
= scic_io_request_get_dma_addr(sci_req
,
264 (sci_req
->command_buffer
) +
267 task_context
->command_iu_upper
= upper_32_bits(dma_addr
);
268 task_context
->command_iu_lower
= lower_32_bits(dma_addr
);
270 /* SMP response comes as UF, so no need to set response IU address. */
271 task_context
->response_iu_upper
= 0;
272 task_context
->response_iu_lower
= 0;
276 * This function processes an unsolicited frame while the SMP request is waiting
277 * for a response frame. It will copy the response data, release the
278 * unsolicited frame, and transition the request to the
279 * SCI_BASE_REQUEST_STATE_COMPLETED state.
280 * @sci_req: This parameter specifies the request for which the
281 * unsolicited frame was received.
282 * @frame_index: This parameter indicates the unsolicited frame index that
283 * should contain the response.
285 * This function returns an indication of whether the response frame was handled
286 * successfully or not. SCI_SUCCESS Currently this value is always returned and
287 * indicates successful processing of the TC response.
289 static enum sci_status
290 scic_sds_smp_request_await_response_frame_handler(
291 struct scic_sds_request
*sci_req
,
294 enum sci_status status
;
296 struct smp_resp
*rsp_hdr
;
297 u8
*usr_smp_buf
= sci_req
->response_buffer
;
298 ssize_t word_cnt
= SMP_RESP_HDR_SZ
/ sizeof(u32
);
300 status
= scic_sds_unsolicited_frame_control_get_header(
301 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
305 /* byte swap the header. */
306 sci_swab32_cpy(usr_smp_buf
, frame_header
, word_cnt
);
308 rsp_hdr
= (struct smp_resp
*)usr_smp_buf
;
310 if (rsp_hdr
->frame_type
== SMP_RESPONSE
) {
313 status
= scic_sds_unsolicited_frame_control_get_buffer(
314 &(scic_sds_request_get_controller(sci_req
)->uf_control
),
318 word_cnt
= (sizeof(struct smp_req
) - SMP_RESP_HDR_SZ
) /
321 sci_swab32_cpy(usr_smp_buf
+ SMP_RESP_HDR_SZ
,
324 scic_sds_request_set_status(
325 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
);
327 sci_base_state_machine_change_state(
328 &sci_req
->started_substate_machine
,
329 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
);
331 /* This was not a response frame why did it get forwarded? */
332 dev_err(scic_to_dev(sci_req
->owning_controller
),
333 "%s: SCIC SMP Request 0x%p received unexpected frame "
338 rsp_hdr
->frame_type
);
340 scic_sds_request_set_status(
342 SCU_TASK_DONE_SMP_FRM_TYPE_ERR
,
343 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
);
345 sci_base_state_machine_change_state(
346 &sci_req
->state_machine
,
347 SCI_BASE_REQUEST_STATE_COMPLETED
);
350 scic_sds_controller_release_frame(sci_req
->owning_controller
,
358 * This method processes an abnormal TC completion while the SMP request is
359 * waiting for a response frame. It decides what happened to the IO based
360 * on TC completion status.
361 * @sci_req: This parameter specifies the request for which the TC
362 * completion was received.
363 * @completion_code: This parameter indicates the completion status information
366 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
367 * this method always returns success.
369 static enum sci_status
scic_sds_smp_request_await_response_tc_completion_handler(
370 struct scic_sds_request
*sci_req
,
373 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
374 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
376 * In the AWAIT RESPONSE state, any TC completion is unexpected.
377 * but if the TC has success status, we complete the IO anyway. */
378 scic_sds_request_set_status(
379 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
382 sci_base_state_machine_change_state(
383 &sci_req
->state_machine
,
384 SCI_BASE_REQUEST_STATE_COMPLETED
);
387 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR
):
388 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR
):
389 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR
):
390 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR
):
392 * These status has been seen in a specific LSI expander, which sometimes
393 * is not able to send smp response within 2 ms. This causes our hardware
394 * break the connection and set TC completion with one of these SMP_XXX_XX_ERR
395 * status. For these type of error, we ask scic user to retry the request. */
396 scic_sds_request_set_status(
397 sci_req
, SCU_TASK_DONE_SMP_RESP_TO_ERR
, SCI_FAILURE_RETRY_REQUIRED
400 sci_base_state_machine_change_state(
401 &sci_req
->state_machine
,
402 SCI_BASE_REQUEST_STATE_COMPLETED
);
407 * All other completion status cause the IO to be complete. If a NAK
408 * was received, then it is up to the user to retry the request. */
409 scic_sds_request_set_status(
411 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
412 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
415 sci_base_state_machine_change_state(
416 &sci_req
->state_machine
,
417 SCI_BASE_REQUEST_STATE_COMPLETED
);
426 * This method processes the completions transport layer (TL) status to
427 * determine if the SMP request was sent successfully. If the SMP request
428 * was sent successfully, then the state for the SMP request transits to
429 * waiting for a response frame.
430 * @sci_req: This parameter specifies the request for which the TC
431 * completion was received.
432 * @completion_code: This parameter indicates the completion status information
435 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
436 * this method always returns success.
438 static enum sci_status
scic_sds_smp_request_await_tc_completion_tc_completion_handler(
439 struct scic_sds_request
*sci_req
,
442 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code
)) {
443 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD
):
444 scic_sds_request_set_status(
445 sci_req
, SCU_TASK_DONE_GOOD
, SCI_SUCCESS
448 sci_base_state_machine_change_state(
449 &sci_req
->state_machine
,
450 SCI_BASE_REQUEST_STATE_COMPLETED
);
455 * All other completion status cause the IO to be complete. If a NAK
456 * was received, then it is up to the user to retry the request. */
457 scic_sds_request_set_status(
459 SCU_NORMALIZE_COMPLETION_STATUS(completion_code
),
460 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
463 sci_base_state_machine_change_state(
464 &sci_req
->state_machine
,
465 SCI_BASE_REQUEST_STATE_COMPLETED
);
473 static const struct scic_sds_io_request_state_handler scic_sds_smp_request_started_substate_handler_table
[] = {
474 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
475 .abort_handler
= scic_sds_request_started_state_abort_handler
,
476 .tc_completion_handler
= scic_sds_smp_request_await_response_tc_completion_handler
,
477 .frame_handler
= scic_sds_smp_request_await_response_frame_handler
,
479 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
480 .abort_handler
= scic_sds_request_started_state_abort_handler
,
481 .tc_completion_handler
= scic_sds_smp_request_await_tc_completion_tc_completion_handler
,
486 * This method performs the actions required when entering the
487 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_RESPONSE sub-state. This
488 * includes setting the IO request state handlers for this sub-state.
489 * @object: This parameter specifies the request object for which the sub-state
490 * change is occurring.
494 static void scic_sds_smp_request_started_await_response_substate_enter(
497 struct scic_sds_request
*sci_req
= object
;
501 scic_sds_smp_request_started_substate_handler_table
,
502 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
507 * This method performs the actions required when entering the
508 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION sub-state.
509 * This includes setting the SMP request state handlers for this sub-state.
510 * @object: This parameter specifies the request object for which the sub-state
511 * change is occurring.
515 static void scic_sds_smp_request_started_await_tc_completion_substate_enter(
518 struct scic_sds_request
*sci_req
= object
;
522 scic_sds_smp_request_started_substate_handler_table
,
523 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
527 static const struct sci_base_state scic_sds_smp_request_started_substate_table
[] = {
528 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
] = {
529 .enter_state
= scic_sds_smp_request_started_await_response_substate_enter
,
531 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
] = {
532 .enter_state
= scic_sds_smp_request_started_await_tc_completion_substate_enter
,
537 * This method is called by the SCI user to build an SMP IO request.
539 * - The user must have previously called scic_io_request_construct() on the
540 * supplied IO request. Indicate if the controller successfully built the IO
541 * request. SCI_SUCCESS This value is returned if the IO request was
542 * successfully built. SCI_FAILURE_UNSUPPORTED_PROTOCOL This value is returned
543 * if the remote_device does not support the SMP protocol.
544 * SCI_FAILURE_INVALID_ASSOCIATION This value is returned if the user did not
545 * properly set the association between the SCIC IO request and the user's IO
548 enum sci_status
scic_io_request_construct_smp(struct scic_sds_request
*sci_req
)
550 struct smp_req
*smp_req
= kmalloc(sizeof(*smp_req
), GFP_KERNEL
);
553 return SCI_FAILURE_INSUFFICIENT_RESOURCES
;
555 sci_req
->protocol
= SCIC_SMP_PROTOCOL
;
556 sci_req
->has_started_substate_machine
= true;
558 /* Construct the started sub-state machine. */
559 sci_base_state_machine_construct(
560 &sci_req
->started_substate_machine
,
562 scic_sds_smp_request_started_substate_table
,
563 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
566 /* Construct the SMP SCU Task Context */
567 memcpy(smp_req
, sci_req
->command_buffer
, sizeof(*smp_req
));
570 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
571 * functions under SAS 2.0, a zero request length really indicates
572 * a non-zero default length. */
573 if (smp_req
->req_len
== 0) {
574 switch (smp_req
->func
) {
576 case SMP_REPORT_PHY_ERR_LOG
:
577 case SMP_REPORT_PHY_SATA
:
578 case SMP_REPORT_ROUTE_INFO
:
579 smp_req
->req_len
= 2;
581 case SMP_CONF_ROUTE_INFO
:
582 case SMP_PHY_CONTROL
:
583 case SMP_PHY_TEST_FUNCTION
:
584 smp_req
->req_len
= 9;
586 /* Default - zero is a valid default for 2.0. */
590 scu_smp_request_construct_task_context(sci_req
, smp_req
);
592 sci_base_state_machine_change_state(&sci_req
->state_machine
,
593 SCI_BASE_REQUEST_STATE_CONSTRUCTED
);