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.
56 #include "intel_sas.h"
57 #include "sci_base_state_machine.h"
58 #include "scic_controller.h"
59 #include "scic_remote_device.h"
60 #include "scic_sds_controller.h"
61 #include "scic_sds_remote_device.h"
62 #include "scic_sds_request.h"
63 #include "scic_sds_smp_request.h"
64 #include "sci_environment.h"
66 #include "scu_completion_codes.h"
67 #include "scu_task_context.h"
69 static void scu_smp_request_construct_task_context(
70 struct scic_sds_request *this_request,
71 struct smp_request *smp_request);
76 * This method return the memory space required for STP PIO requests. u32
78 u32 scic_sds_smp_request_get_object_size(void)
80 return sizeof(struct scic_sds_request)
81 + sizeof(struct smp_request)
82 + sizeof(struct smp_response)
83 + sizeof(struct scu_task_context)
88 * scic_sds_smp_request_get_command_buffer() -
90 * This macro returns the address of the smp command buffer in the smp request
91 * memory. No need to cast to SMP request type.
93 #define scic_sds_smp_request_get_command_buffer(memory) \
94 (((char *)(memory)) + sizeof(struct scic_sds_request))
97 * scic_sds_smp_request_get_response_buffer() -
99 * This macro returns the address of the smp response buffer in the smp request
102 #define scic_sds_smp_request_get_response_buffer(memory) \
103 (((char *)(scic_sds_smp_request_get_command_buffer(memory))) \
104 + sizeof(struct smp_request))
107 * scic_sds_smp_request_get_task_context_buffer() -
109 * This macro returs the task context buffer for the SMP request.
111 #define scic_sds_smp_request_get_task_context_buffer(memory) \
112 ((struct scu_task_context *)(\
113 ((char *)(scic_sds_smp_request_get_response_buffer(memory))) \
114 + sizeof(struct smp_response) \
120 * This method build the remainder of the IO request object.
121 * @this_request: This parameter specifies the request object being constructed.
123 * The scic_sds_general_request_construct() must be called before this call is
127 void scic_sds_smp_request_assign_buffers(
128 struct scic_sds_request *this_request)
130 /* Assign all of the buffer pointers */
131 this_request->command_buffer =
132 scic_sds_smp_request_get_command_buffer(this_request);
133 this_request->response_buffer =
134 scic_sds_smp_request_get_response_buffer(this_request);
135 this_request->sgl_element_pair_buffer = NULL;
137 if (this_request->was_tag_assigned_by_user == false) {
138 this_request->task_context_buffer =
139 scic_sds_smp_request_get_task_context_buffer(this_request);
140 this_request->task_context_buffer =
141 PTR_ALIGN(this_request->task_context_buffer, SMP_CACHE_BYTES);
147 * This method is called by the SCI user to build an SMP pass-through IO
149 * @scic_smp_request: This parameter specifies the handle to the io request
150 * object to be built.
151 * @passthru_cb: This parameter specifies the pointer to the callback structure
152 * that contains the function pointers
154 * - The user must have previously called scic_io_request_construct() on the
155 * supplied IO request. Indicate if the controller successfully built the IO
160 * This method will fill in the SCU Task Context for a SMP request. The
161 * following important settings are utilized: -# task_type ==
162 * SCU_TASK_TYPE_SMP. This simply indicates that a normal request type
163 * (i.e. non-raw frame) is being utilized to perform task management. -#
164 * control_frame == 1. This ensures that the proper endianess is set so
165 * that the bytes are transmitted in the right order for a smp request frame.
166 * @this_request: This parameter specifies the smp request object being
170 static void scu_smp_request_construct_task_context(
171 struct scic_sds_request *sds_request,
172 struct smp_request *smp_request)
175 struct scic_sds_controller *controller;
176 struct scic_sds_remote_device *target_device;
177 struct scic_sds_port *target_port;
178 struct scu_task_context *task_context;
180 /* byte swap the smp request. */
181 scic_word_copy_with_swap(sds_request->command_buffer,
183 sizeof(struct smp_request) / sizeof(u32));
185 task_context = scic_sds_request_get_task_context(sds_request);
187 controller = scic_sds_request_get_controller(sds_request);
188 target_device = scic_sds_request_get_device(sds_request);
189 target_port = scic_sds_request_get_port(sds_request);
192 * Fill in the TC with the its required data
195 task_context->priority = 0;
196 task_context->initiator_request = 1;
197 task_context->connection_rate =
198 scic_remote_device_get_connection_rate(target_device);
199 task_context->protocol_engine_index =
200 scic_sds_controller_get_protocol_engine_group(controller);
201 task_context->logical_port_index =
202 scic_sds_port_get_index(target_port);
203 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SMP;
204 task_context->abort = 0;
205 task_context->valid = SCU_TASK_CONTEXT_VALID;
206 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
209 task_context->remote_node_index =
210 sds_request->target_device->rnc->remote_node_index;
211 task_context->command_code = 0;
212 task_context->task_type = SCU_TASK_TYPE_SMP_REQUEST;
215 task_context->link_layer_control = 0;
216 task_context->do_not_dma_ssp_good_response = 1;
217 task_context->strict_ordering = 0;
218 task_context->control_frame = 1;
219 task_context->timeout_enable = 0;
220 task_context->block_guard_enable = 0;
223 task_context->address_modifier = 0;
226 task_context->ssp_command_iu_length =
227 smp_request->header.request_length;
230 task_context->transfer_length_bytes = 0;
233 * 18h ~ 30h, protocol specific
234 * since commandIU has been build by framework at this point, we just
235 * copy the frist DWord from command IU to this location. */
236 memcpy((void *)(&task_context->type.smp),
237 sds_request->command_buffer,
242 * "For SMP you could program it to zero. We would prefer that way
243 * so that done code will be consistent." - Venki
245 task_context->task_phase = 0;
247 if (sds_request->was_tag_assigned_by_user) {
249 * Build the task context now since we have already read
252 sds_request->post_context =
253 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
254 (scic_sds_controller_get_protocol_engine_group(
256 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
257 (scic_sds_port_get_index(target_port) <<
258 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT) |
259 scic_sds_io_tag_get_index(sds_request->io_tag));
262 * Build the task context now since we have already read
264 * I/O tag index is not assigned because we have to wait
265 * until we get a TCi.
267 sds_request->post_context =
268 (SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
269 (scic_sds_controller_get_protocol_engine_group(
271 SCU_CONTEXT_COMMAND_PROTOCOL_ENGINE_GROUP_SHIFT) |
272 (scic_sds_port_get_index(target_port) <<
273 SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT));
277 * Copy the physical address for the command buffer to the SCU Task
278 * Context command buffer should not contain command header.
280 dma_addr = scic_io_request_get_dma_addr(sds_request,
282 (sds_request->command_buffer) +
285 task_context->command_iu_upper = upper_32_bits(dma_addr);
286 task_context->command_iu_lower = lower_32_bits(dma_addr);
288 /* SMP response comes as UF, so no need to set response IU address. */
289 task_context->response_iu_upper = 0;
290 task_context->response_iu_lower = 0;
294 * This method processes an unsolicited frame while the SMP request is waiting
295 * for a response frame. It will copy the response data, release the
296 * unsolicited frame, and transition the request to the
297 * SCI_BASE_REQUEST_STATE_COMPLETED state.
298 * @this_request: This parameter specifies the request for which the
299 * unsolicited frame was received.
300 * @frame_index: This parameter indicates the unsolicited frame index that
301 * should contain the response.
303 * This method returns an indication of whether the response frame was handled
304 * successfully or not. SCI_SUCCESS Currently this value is always returned and
305 * indicates successful processing of the TC response.
307 static enum sci_status scic_sds_smp_request_await_response_frame_handler(
308 struct scic_sds_request *this_request,
311 enum sci_status status;
313 struct smp_response_header *this_frame_header;
314 u8 *user_smp_buffer = this_request->response_buffer;
316 status = scic_sds_unsolicited_frame_control_get_header(
317 &(scic_sds_request_get_controller(this_request)->uf_control),
322 /* byte swap the header. */
323 scic_word_copy_with_swap(
324 (u32 *)user_smp_buffer,
326 sizeof(struct smp_response_header) / sizeof(u32)
328 this_frame_header = (struct smp_response_header *)user_smp_buffer;
330 if (this_frame_header->smp_frame_type == SMP_FRAME_TYPE_RESPONSE) {
331 void *smp_response_buffer;
333 status = scic_sds_unsolicited_frame_control_get_buffer(
334 &(scic_sds_request_get_controller(this_request)->uf_control),
339 scic_word_copy_with_swap(
340 (u32 *)(user_smp_buffer + sizeof(struct smp_response_header)),
342 sizeof(union smp_response_body) / sizeof(u32)
344 if (this_frame_header->function == SMP_FUNCTION_DISCOVER) {
345 struct smp_response *this_smp_response;
347 this_smp_response = (struct smp_response *)user_smp_buffer;
350 * Some expanders only report an attached SATA device, and
351 * not an STP target. Since the core depends on the STP
352 * target attribute to correctly build I/O, set the bit now
354 if (this_smp_response->response.discover.protocols.u.bits.attached_sata_device
355 && !this_smp_response->response.discover.protocols.u.bits.attached_stp_target) {
356 this_smp_response->response.discover.protocols.u.bits.attached_stp_target = 1;
358 dev_dbg(scic_to_dev(this_request->owning_controller),
359 "%s: scic_sds_smp_request_await_response_frame_handler(0x%p) Found SATA dev, setting STP bit.\n",
360 __func__, this_request);
365 * Don't need to copy to user space. User instead will refer to
366 * core request's response buffer. */
369 * copy the smp response to framework smp request's response buffer.
370 * scic_sds_smp_request_copy_response(this_request); */
372 scic_sds_request_set_status(
373 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
376 sci_base_state_machine_change_state(
377 &this_request->started_substate_machine,
378 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
381 /* This was not a response frame why did it get forwarded? */
382 dev_err(scic_to_dev(this_request->owning_controller),
383 "%s: SCIC SMP Request 0x%p received unexpected frame "
388 this_frame_header->smp_frame_type);
390 scic_sds_request_set_status(
392 SCU_TASK_DONE_SMP_FRM_TYPE_ERR,
393 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
396 sci_base_state_machine_change_state(
397 &this_request->state_machine,
398 SCI_BASE_REQUEST_STATE_COMPLETED
402 scic_sds_controller_release_frame(
403 this_request->owning_controller, frame_index
411 * This method processes an abnormal TC completion while the SMP request is
412 * waiting for a response frame. It decides what happened to the IO based
413 * on TC completion status.
414 * @this_request: This parameter specifies the request for which the TC
415 * completion was received.
416 * @completion_code: This parameter indicates the completion status information
419 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
420 * this method always returns success.
422 static enum sci_status scic_sds_smp_request_await_response_tc_completion_handler(
423 struct scic_sds_request *this_request,
426 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
427 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
429 * In the AWAIT RESPONSE state, any TC completion is unexpected.
430 * but if the TC has success status, we complete the IO anyway. */
431 scic_sds_request_set_status(
432 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
435 sci_base_state_machine_change_state(
436 &this_request->state_machine,
437 SCI_BASE_REQUEST_STATE_COMPLETED);
440 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_RESP_TO_ERR):
441 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_UFI_ERR):
442 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_FRM_TYPE_ERR):
443 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_SMP_LL_RX_ERR):
445 * These status has been seen in a specific LSI expander, which sometimes
446 * is not able to send smp response within 2 ms. This causes our hardware
447 * break the connection and set TC completion with one of these SMP_XXX_XX_ERR
448 * status. For these type of error, we ask scic user to retry the request. */
449 scic_sds_request_set_status(
450 this_request, SCU_TASK_DONE_SMP_RESP_TO_ERR, SCI_FAILURE_RETRY_REQUIRED
453 sci_base_state_machine_change_state(
454 &this_request->state_machine,
455 SCI_BASE_REQUEST_STATE_COMPLETED);
460 * All other completion status cause the IO to be complete. If a NAK
461 * was received, then it is up to the user to retry the request. */
462 scic_sds_request_set_status(
464 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
465 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
468 sci_base_state_machine_change_state(
469 &this_request->state_machine,
470 SCI_BASE_REQUEST_STATE_COMPLETED);
479 * This method processes the completions transport layer (TL) status to
480 * determine if the SMP request was sent successfully. If the SMP request
481 * was sent successfully, then the state for the SMP request transits to
482 * waiting for a response frame.
483 * @this_request: This parameter specifies the request for which the TC
484 * completion was received.
485 * @completion_code: This parameter indicates the completion status information
488 * Indicate if the tc completion handler was successful. SCI_SUCCESS currently
489 * this method always returns success.
491 static enum sci_status scic_sds_smp_request_await_tc_completion_tc_completion_handler(
492 struct scic_sds_request *this_request,
495 switch (SCU_GET_COMPLETION_TL_STATUS(completion_code)) {
496 case SCU_MAKE_COMPLETION_STATUS(SCU_TASK_DONE_GOOD):
497 scic_sds_request_set_status(
498 this_request, SCU_TASK_DONE_GOOD, SCI_SUCCESS
501 sci_base_state_machine_change_state(
502 &this_request->state_machine,
503 SCI_BASE_REQUEST_STATE_COMPLETED);
508 * All other completion status cause the IO to be complete. If a NAK
509 * was received, then it is up to the user to retry the request. */
510 scic_sds_request_set_status(
512 SCU_NORMALIZE_COMPLETION_STATUS(completion_code),
513 SCI_FAILURE_CONTROLLER_SPECIFIC_IO_ERR
516 sci_base_state_machine_change_state(
517 &this_request->state_machine,
518 SCI_BASE_REQUEST_STATE_COMPLETED);
526 static const struct scic_sds_io_request_state_handler scic_sds_smp_request_started_substate_handler_table[] = {
527 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = {
528 .abort_handler = scic_sds_request_started_state_abort_handler,
529 .tc_completion_handler = scic_sds_smp_request_await_response_tc_completion_handler,
530 .frame_handler = scic_sds_smp_request_await_response_frame_handler,
532 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = {
533 .abort_handler = scic_sds_request_started_state_abort_handler,
534 .tc_completion_handler = scic_sds_smp_request_await_tc_completion_tc_completion_handler,
539 * This method performs the actions required when entering the
540 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_RESPONSE sub-state. This
541 * includes setting the IO request state handlers for this sub-state.
542 * @object: This parameter specifies the request object for which the sub-state
543 * change is occuring.
547 static void scic_sds_smp_request_started_await_response_substate_enter(
548 struct sci_base_object *object)
550 struct scic_sds_request *this_request = (struct scic_sds_request *)object;
554 scic_sds_smp_request_started_substate_handler_table,
555 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
560 * This method performs the actions required when entering the
561 * SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION sub-state.
562 * This includes setting the SMP request state handlers for this sub-state.
563 * @object: This parameter specifies the request object for which the sub-state
564 * change is occuring.
568 static void scic_sds_smp_request_started_await_tc_completion_substate_enter(
569 struct sci_base_object *object)
571 struct scic_sds_request *this_request = (struct scic_sds_request *)object;
575 scic_sds_smp_request_started_substate_handler_table,
576 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION
580 static const struct sci_base_state scic_sds_smp_request_started_substate_table[] = {
581 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE] = {
582 .enter_state = scic_sds_smp_request_started_await_response_substate_enter,
584 [SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_TC_COMPLETION] = {
585 .enter_state = scic_sds_smp_request_started_await_tc_completion_substate_enter,
590 * This method is called by the SCI user to build an SMP IO request.
592 * - The user must have previously called scic_io_request_construct() on the
593 * supplied IO request. Indicate if the controller successfully built the IO
594 * request. SCI_SUCCESS This value is returned if the IO request was
595 * successfully built. SCI_FAILURE_UNSUPPORTED_PROTOCOL This value is returned
596 * if the remote_device does not support the SMP protocol.
597 * SCI_FAILURE_INVALID_ASSOCIATION This value is returned if the user did not
598 * properly set the association between the SCIC IO request and the user's IO
599 * request. Please refer to the sci_object_set_association() routine for more
602 enum sci_status scic_io_request_construct_smp(struct scic_sds_request *sci_req)
604 struct smp_request *smp_req = kmalloc(sizeof(*smp_req), GFP_KERNEL);
607 return SCI_FAILURE_INSUFFICIENT_RESOURCES;
609 sci_req->protocol = SCIC_SMP_PROTOCOL;
610 sci_req->has_started_substate_machine = true;
612 /* Construct the started sub-state machine. */
613 sci_base_state_machine_construct(
614 &sci_req->started_substate_machine,
616 scic_sds_smp_request_started_substate_table,
617 SCIC_SDS_SMP_REQUEST_STARTED_SUBSTATE_AWAIT_RESPONSE
620 /* Construct the SMP SCU Task Context */
621 memcpy(smp_req, sci_req->command_buffer, sizeof(*smp_req));
624 * Look at the SMP requests' header fields; for certain SAS 1.x SMP
625 * functions under SAS 2.0, a zero request length really indicates
626 * a non-zero default length. */
627 if (smp_req->header.request_length == 0) {
628 switch (smp_req->header.function) {
629 case SMP_FUNCTION_DISCOVER:
630 case SMP_FUNCTION_REPORT_PHY_ERROR_LOG:
631 case SMP_FUNCTION_REPORT_PHY_SATA:
632 case SMP_FUNCTION_REPORT_ROUTE_INFORMATION:
633 smp_req->header.request_length = 2;
635 case SMP_FUNCTION_CONFIGURE_ROUTE_INFORMATION:
636 case SMP_FUNCTION_PHY_CONTROL:
637 case SMP_FUNCTION_PHY_TEST:
638 smp_req->header.request_length = 9;
640 /* Default - zero is a valid default for 2.0. */
644 scu_smp_request_construct_task_context(sci_req, smp_req);
646 sci_base_state_machine_change_state(&sci_req->state_machine,
647 SCI_BASE_REQUEST_STATE_CONSTRUCTED);