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.
61 #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
62 #define SCU_DUMMY_INDEX (0xFFFF)
64 static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
68 dev_dbg(&iport->isci_host->pdev->dev,
69 "%s: iport = %p, state = 0x%x\n",
70 __func__, iport, status);
72 /* XXX pointless lock */
73 spin_lock_irqsave(&iport->state_lock, flags);
74 iport->status = status;
75 spin_unlock_irqrestore(&iport->state_lock, flags);
79 * This function will indicate which protocols are supported by this port.
80 * @sci_port: a handle corresponding to the SAS port for which to return the
81 * supported protocols.
82 * @protocols: This parameter specifies a pointer to a data structure
83 * which the core will copy the protocol values for the port from the
84 * transmit_identification register.
87 scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
88 struct scic_phy_proto *protocols)
94 for (index = 0; index < SCI_MAX_PHYS; index++) {
95 if (sci_port->phy_table[index] != NULL) {
96 scic_sds_phy_get_protocols(sci_port->phy_table[index],
103 * This method requests a list (mask) of the phys contained in the supplied SAS
105 * @sci_port: a handle corresponding to the SAS port for which to return the
108 * Return a bit mask indicating which phys are a part of this port. Each bit
109 * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
111 static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
118 for (index = 0; index < SCI_MAX_PHYS; index++) {
119 if (sci_port->phy_table[index] != NULL) {
120 mask |= (1 << index);
128 * scic_port_get_properties() - This method simply returns the properties
129 * regarding the port, such as: physical index, protocols, sas address, etc.
130 * @port: this parameter specifies the port for which to retrieve the physical
132 * @properties: This parameter specifies the properties structure into which to
133 * copy the requested information.
135 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
136 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
137 * value is returned if the specified port is not valid. When this value is
138 * returned, no data is copied to the properties output parameter.
140 static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
141 struct scic_port_properties *prop)
143 if ((port == NULL) ||
144 (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
145 return SCI_FAILURE_INVALID_PORT;
147 prop->index = port->logical_port_index;
148 prop->phy_mask = scic_sds_port_get_phys(port);
149 scic_sds_port_get_sas_address(port, &prop->local.sas_address);
150 scic_sds_port_get_protocols(port, &prop->local.protocols);
151 scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
156 static void isci_port_link_up(struct isci_host *isci_host,
157 struct scic_sds_port *port,
158 struct scic_sds_phy *phy)
161 struct scic_port_properties properties;
162 struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
163 struct isci_port *isci_port = sci_port_to_iport(port);
164 unsigned long success = true;
166 BUG_ON(isci_phy->isci_port != NULL);
168 isci_phy->isci_port = isci_port;
170 dev_dbg(&isci_host->pdev->dev,
171 "%s: isci_port = %p\n",
172 __func__, isci_port);
174 spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
176 isci_port_change_state(isci_phy->isci_port, isci_starting);
178 scic_port_get_properties(port, &properties);
180 if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
181 u64 attached_sas_address;
183 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
184 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
187 * For direct-attached SATA devices, the SCI core will
188 * automagically assign a SAS address to the end device
189 * for the purpose of creating a port. This SAS address
190 * will not be the same as assigned to the PHY and needs
191 * to be obtained from struct scic_port_properties properties.
193 attached_sas_address = properties.remote.sas_address.high;
194 attached_sas_address <<= 32;
195 attached_sas_address |= properties.remote.sas_address.low;
196 swab64s(&attached_sas_address);
198 memcpy(&isci_phy->sas_phy.attached_sas_addr,
199 &attached_sas_address, sizeof(attached_sas_address));
200 } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
201 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
202 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
204 /* Copy the attached SAS address from the IAF */
205 memcpy(isci_phy->sas_phy.attached_sas_addr,
206 isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
208 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
212 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
214 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
216 /* Notify libsas that we have an address frame, if indeed
217 * we've found an SSP, SMP, or STP target */
219 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
225 * isci_port_link_down() - This function is called by the sci core when a link
227 * @isci_host: This parameter specifies the isci host object.
228 * @phy: This parameter specifies the isci phy with the active link.
229 * @port: This parameter specifies the isci port with the active link.
232 static void isci_port_link_down(struct isci_host *isci_host,
233 struct isci_phy *isci_phy,
234 struct isci_port *isci_port)
236 struct isci_remote_device *isci_device;
238 dev_dbg(&isci_host->pdev->dev,
239 "%s: isci_port = %p\n", __func__, isci_port);
243 /* check to see if this is the last phy on this port. */
244 if (isci_phy->sas_phy.port
245 && isci_phy->sas_phy.port->num_phys == 1) {
247 /* change the state for all devices on this port.
248 * The next task sent to this device will be returned
249 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
250 * will remove the target
252 list_for_each_entry(isci_device,
253 &isci_port->remote_dev_list,
255 dev_dbg(&isci_host->pdev->dev,
256 "%s: isci_device = %p\n",
257 __func__, isci_device);
258 isci_remote_device_change_state(isci_device,
262 isci_port_change_state(isci_port, isci_stopping);
265 /* Notify libsas of the borken link, this will trigger calls to our
266 * isci_port_deformed and isci_dev_gone functions.
268 sas_phy_disconnected(&isci_phy->sas_phy);
269 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
270 PHYE_LOSS_OF_SIGNAL);
272 isci_phy->isci_port = NULL;
274 dev_dbg(&isci_host->pdev->dev,
275 "%s: isci_port = %p - Done\n", __func__, isci_port);
280 * isci_port_ready() - This function is called by the sci core when a link
282 * @isci_host: This parameter specifies the isci host object.
283 * @port: This parameter specifies the sci port with the active link.
286 static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
288 dev_dbg(&isci_host->pdev->dev,
289 "%s: isci_port = %p\n", __func__, isci_port);
291 complete_all(&isci_port->start_complete);
292 isci_port_change_state(isci_port, isci_ready);
297 * isci_port_not_ready() - This function is called by the sci core when a link
298 * is not ready. All remote devices on this link will be removed if they are
299 * in the stopping state.
300 * @isci_host: This parameter specifies the isci host object.
301 * @port: This parameter specifies the sci port with the active link.
304 static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
306 dev_dbg(&isci_host->pdev->dev,
307 "%s: isci_port = %p\n", __func__, isci_port);
310 static void isci_port_stop_complete(struct scic_sds_controller *scic,
311 struct scic_sds_port *sci_port,
312 enum sci_status completion_status)
314 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
318 * isci_port_hard_reset_complete() - This function is called by the sci core
319 * when the hard reset complete notification has been received.
320 * @port: This parameter specifies the sci port with the active link.
321 * @completion_status: This parameter specifies the core status for the reset
325 static void isci_port_hard_reset_complete(struct isci_port *isci_port,
326 enum sci_status completion_status)
328 dev_dbg(&isci_port->isci_host->pdev->dev,
329 "%s: isci_port = %p, completion_status=%x\n",
330 __func__, isci_port, completion_status);
332 /* Save the status of the hard reset from the port. */
333 isci_port->hard_reset_status = completion_status;
335 complete_all(&isci_port->hard_reset_complete);
338 /* This method will return a true value if the specified phy can be assigned to
339 * this port The following is a list of phys for each port that are allowed: -
340 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
341 * doesn't preclude all configurations. It merely ensures that a phy is part
342 * of the allowable set of phy identifiers for that port. For example, one
343 * could assign phy 3 to port 0 and no other phys. Please refer to
344 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
345 * phy_mask for a port can be supported. bool true if this is a valid phy
346 * assignment for the port false if this is not a valid phy assignment for the
349 bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
352 /* Initialize to invalid value. */
353 u32 existing_phy_index = SCI_MAX_PHYS;
356 if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
360 if (sci_port->physical_port_index == 3 && phy_index != 3) {
365 (sci_port->physical_port_index == 2)
366 && ((phy_index == 0) || (phy_index == 1))
371 for (index = 0; index < SCI_MAX_PHYS; index++) {
372 if ((sci_port->phy_table[index] != NULL)
373 && (index != phy_index)) {
374 existing_phy_index = index;
379 * Ensure that all of the phys in the port are capable of
380 * operating at the same maximum link rate. */
382 (existing_phy_index < SCI_MAX_PHYS)
383 && (sci_port->owning_controller->user_parameters.sds1.phys[
384 phy_index].max_speed_generation !=
385 sci_port->owning_controller->user_parameters.sds1.phys[
386 existing_phy_index].max_speed_generation)
395 * @sci_port: This is the port object for which to determine if the phy mask
398 * This method will return a true value if the port's phy mask can be supported
399 * by the SCU. The following is a list of valid PHY mask configurations for
400 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
401 * - Port 3 - [3] This method returns a boolean indication specifying if the
402 * phy mask can be supported. true if this is a valid phy assignment for the
403 * port false if this is not a valid phy assignment for the port
405 static bool scic_sds_port_is_phy_mask_valid(
406 struct scic_sds_port *sci_port,
409 if (sci_port->physical_port_index == 0) {
410 if (((phy_mask & 0x0F) == 0x0F)
411 || ((phy_mask & 0x03) == 0x03)
412 || ((phy_mask & 0x01) == 0x01)
415 } else if (sci_port->physical_port_index == 1) {
416 if (((phy_mask & 0x02) == 0x02)
419 } else if (sci_port->physical_port_index == 2) {
420 if (((phy_mask & 0x0C) == 0x0C)
421 || ((phy_mask & 0x04) == 0x04)
424 } else if (sci_port->physical_port_index == 3) {
425 if (((phy_mask & 0x08) == 0x08)
435 * @sci_port: This parameter specifies the port from which to return a
438 * This method retrieves a currently active (i.e. connected) phy contained in
439 * the port. Currently, the lowest order phy that is connected is returned.
440 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
441 * returned if there are no currently active (i.e. connected to a remote end
442 * point) phys contained in the port. All other values specify a struct scic_sds_phy
443 * object that is active in the port.
445 static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
446 struct scic_sds_port *sci_port
449 struct scic_sds_phy *phy;
451 for (index = 0; index < SCI_MAX_PHYS; index++) {
453 * Ensure that the phy is both part of the port and currently
454 * connected to the remote end-point. */
455 phy = sci_port->phy_table[index];
458 && scic_sds_port_active_phy(sci_port, phy)
468 * scic_sds_port_set_phy() -
469 * @out]: port The port object to which the phy assignement is being made.
470 * @out]: phy The phy which is being assigned to the port.
472 * This method attempts to make the assignment of the phy to the port. If
473 * successful the phy is assigned to the ports phy table. bool true if the phy
474 * assignment can be made. false if the phy assignement can not be made. This
475 * is a functional test that only fails if the phy is currently assigned to a
478 static enum sci_status scic_sds_port_set_phy(
479 struct scic_sds_port *port,
480 struct scic_sds_phy *phy)
483 * Check to see if we can add this phy to a port
484 * that means that the phy is not part of a port and that the port does
485 * not already have a phy assinged to the phy index. */
487 (port->phy_table[phy->phy_index] == NULL)
488 && (phy_get_non_dummy_port(phy) == NULL)
489 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
492 * Phy is being added in the stopped state so we are in MPC mode
493 * make logical port index = physical port index */
494 port->logical_port_index = port->physical_port_index;
495 port->phy_table[phy->phy_index] = phy;
496 scic_sds_phy_set_port(phy, port);
505 * scic_sds_port_clear_phy() -
506 * @out]: port The port from which the phy is being cleared.
507 * @out]: phy The phy being cleared from the port.
509 * This method will clear the phy assigned to this port. This method fails if
510 * this phy is not currently assinged to this port. bool true if the phy is
511 * removed from the port. false if this phy is not assined to this port.
513 static enum sci_status scic_sds_port_clear_phy(
514 struct scic_sds_port *port,
515 struct scic_sds_phy *phy)
517 /* Make sure that this phy is part of this port */
518 if (port->phy_table[phy->phy_index] == phy &&
519 phy_get_non_dummy_port(phy) == port) {
520 struct scic_sds_controller *scic = port->owning_controller;
521 struct isci_host *ihost = scic_to_ihost(scic);
523 /* Yep it is assigned to this port so remove it */
524 scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
525 port->phy_table[phy->phy_index] = NULL;
534 * This method requests the SAS address for the supplied SAS port from the SCI
536 * @sci_port: a handle corresponding to the SAS port for which to return the
538 * @sas_address: This parameter specifies a pointer to a SAS address structure
539 * into which the core will copy the SAS address for the port.
542 void scic_sds_port_get_sas_address(
543 struct scic_sds_port *sci_port,
544 struct sci_sas_address *sas_address)
548 sas_address->high = 0;
549 sas_address->low = 0;
551 for (index = 0; index < SCI_MAX_PHYS; index++) {
552 if (sci_port->phy_table[index] != NULL) {
553 scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
559 * This function requests the SAS address for the device directly attached to
561 * @sci_port: a handle corresponding to the SAS port for which to return the
563 * @sas_address: This parameter specifies a pointer to a SAS address structure
564 * into which the core will copy the SAS address for the device directly
565 * attached to the port.
568 void scic_sds_port_get_attached_sas_address(
569 struct scic_sds_port *sci_port,
570 struct sci_sas_address *sas_address)
572 struct scic_sds_phy *sci_phy;
575 * Ensure that the phy is both part of the port and currently
576 * connected to the remote end-point.
578 sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
580 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
581 scic_sds_phy_get_attached_sas_address(sci_phy,
584 scic_sds_phy_get_sas_address(sci_phy, sas_address);
585 sas_address->low += sci_phy->phy_index;
588 sas_address->high = 0;
589 sas_address->low = 0;
594 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
596 * @sci_port: logical port on which we need to create the remote node context
597 * @rni: remote node index for this remote node context.
599 * This routine will construct a dummy remote node context data structure
600 * This structure will be posted to the hardware to work around a scheduler
601 * error in the hardware.
603 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
605 union scu_remote_node_context *rnc;
607 rnc = &sci_port->owning_controller->remote_node_context_table[rni];
609 memset(rnc, 0, sizeof(union scu_remote_node_context));
611 rnc->ssp.remote_sas_address_hi = 0;
612 rnc->ssp.remote_sas_address_lo = 0;
614 rnc->ssp.remote_node_index = rni;
615 rnc->ssp.remote_node_port_width = 1;
616 rnc->ssp.logical_port_index = sci_port->physical_port_index;
618 rnc->ssp.nexus_loss_timer_enable = false;
619 rnc->ssp.check_bit = false;
620 rnc->ssp.is_valid = true;
621 rnc->ssp.is_remote_node_context = true;
622 rnc->ssp.function_number = 0;
623 rnc->ssp.arbitration_wait_time = 0;
627 * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
628 * @sci_port The logical port on which we need to create the
629 * remote node context.
631 * @tci The remote node index for this remote node context.
633 * This routine will construct a dummy task context data structure. This
634 * structure will be posted to the hardwre to work around a scheduler error
638 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
640 struct scu_task_context *task_context;
642 task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
644 memset(task_context, 0, sizeof(struct scu_task_context));
646 task_context->abort = 0;
647 task_context->priority = 0;
648 task_context->initiator_request = 1;
649 task_context->connection_rate = 1;
650 task_context->protocol_engine_index = 0;
651 task_context->logical_port_index = sci_port->physical_port_index;
652 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
653 task_context->task_index = scic_sds_io_tag_get_index(tci);
654 task_context->valid = SCU_TASK_CONTEXT_VALID;
655 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
657 task_context->remote_node_index = sci_port->reserved_rni;
658 task_context->command_code = 0;
660 task_context->link_layer_control = 0;
661 task_context->do_not_dma_ssp_good_response = 1;
662 task_context->strict_ordering = 0;
663 task_context->control_frame = 0;
664 task_context->timeout_enable = 0;
665 task_context->block_guard_enable = 0;
667 task_context->address_modifier = 0;
669 task_context->task_phase = 0x01;
672 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
674 struct scic_sds_controller *scic = sci_port->owning_controller;
676 if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
677 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
679 if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
680 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
681 1, sci_port->reserved_rni);
683 sci_port->reserved_rni = SCU_DUMMY_INDEX;
684 sci_port->reserved_tci = SCU_DUMMY_INDEX;
688 * This method performs initialization of the supplied port. Initialization
689 * includes: - state machine initialization - member variable initialization
690 * - configuring the phy_mask
692 * @transport_layer_registers:
693 * @port_task_scheduler_registers:
694 * @port_configuration_regsiter:
696 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
697 * if the phy being added to the port
699 enum sci_status scic_sds_port_initialize(
700 struct scic_sds_port *sci_port,
701 void __iomem *port_task_scheduler_registers,
702 void __iomem *port_configuration_regsiter,
703 void __iomem *viit_registers)
705 sci_port->port_task_scheduler_registers = port_task_scheduler_registers;
706 sci_port->port_pe_configuration_register = port_configuration_regsiter;
707 sci_port->viit_registers = viit_registers;
714 * This method assigns the direct attached device ID for this port.
716 * @param[in] sci_port The port for which the direct attached device id is to
718 * @param[in] device_id The direct attached device ID to assign to the port.
719 * This will be the RNi for the device
721 void scic_sds_port_setup_transports(
722 struct scic_sds_port *sci_port,
727 for (index = 0; index < SCI_MAX_PHYS; index++) {
728 if (sci_port->active_phy_mask & (1 << index))
729 scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
735 * @sci_port: This is the port on which the phy should be enabled.
736 * @sci_phy: This is the specific phy which to enable.
737 * @do_notify_user: This parameter specifies whether to inform the user (via
738 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
740 * This function will activate the phy in the port.
741 * Activation includes: - adding
742 * the phy to the port - enabling the Protocol Engine in the silicon. -
743 * notifying the user that the link is up. none
745 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
746 struct scic_sds_phy *sci_phy,
749 struct scic_sds_controller *scic = sci_port->owning_controller;
750 struct isci_host *ihost = scic_to_ihost(scic);
752 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
753 scic_sds_phy_resume(sci_phy);
755 sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
757 scic_sds_controller_clear_invalid_phy(scic, sci_phy);
759 if (do_notify_user == true)
760 isci_port_link_up(ihost, sci_port, sci_phy);
763 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
764 struct scic_sds_phy *sci_phy,
767 struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
768 struct isci_port *iport = sci_port_to_iport(sci_port);
769 struct isci_host *ihost = scic_to_ihost(scic);
770 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
772 sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
774 sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
776 /* Re-assign the phy back to the LP as if it were a narrow port */
777 writel(sci_phy->phy_index,
778 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
780 if (do_notify_user == true)
781 isci_port_link_down(ihost, iphy, iport);
786 * @sci_port: This is the port on which the phy should be disabled.
787 * @sci_phy: This is the specific phy which to disabled.
789 * This function will disable the phy and report that the phy is not valid for
790 * this port object. None
792 static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
793 struct scic_sds_phy *sci_phy)
795 struct scic_sds_controller *scic = sci_port->owning_controller;
798 * Check to see if we have alreay reported this link as bad and if
799 * not go ahead and tell the SCI_USER that we have discovered an
802 if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
803 scic_sds_controller_set_invalid_phy(scic, sci_phy);
804 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
808 static bool is_port_ready_state(enum scic_sds_port_states state)
811 case SCI_BASE_PORT_STATE_READY:
812 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
813 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
814 case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
821 /* flag dummy rnc hanling when exiting a ready state */
822 static void port_state_machine_change(struct scic_sds_port *sci_port,
823 enum scic_sds_port_states state)
825 struct sci_base_state_machine *sm = &sci_port->state_machine;
826 enum scic_sds_port_states old_state = sm->current_state_id;
828 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
829 sci_port->ready_exit = true;
831 sci_base_state_machine_change_state(sm, state);
832 sci_port->ready_exit = false;
836 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
837 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
838 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
839 * @do_notify_user: This parameter specifies whether to inform the user (via
840 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
842 * Determine if this phy can be assigned to this
843 * port . If the phy is not a valid PHY for
844 * this port then the function will notify the user. A PHY can only be
845 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
846 * the same port. none
848 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
849 struct scic_sds_phy *sci_phy,
852 struct sci_sas_address port_sas_address;
853 struct sci_sas_address phy_sas_address;
855 scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
856 scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
858 /* If the SAS address of the new phy matches the SAS address of
859 * other phys in the port OR this is the first phy in the port,
860 * then activate the phy and allow it to be used for operations
863 if ((phy_sas_address.high == port_sas_address.high &&
864 phy_sas_address.low == port_sas_address.low) ||
865 sci_port->active_phy_mask == 0) {
866 struct sci_base_state_machine *sm = &sci_port->state_machine;
868 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
869 if (sm->current_state_id == SCI_BASE_PORT_STATE_RESETTING)
870 port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_READY);
872 scic_sds_port_invalid_link_up(sci_port, sci_phy);
878 * This method returns false if the port only has a single phy object assigned.
879 * If there are no phys or more than one phy then the method will return
881 * @sci_port: The port for which the wide port condition is to be checked.
883 * bool true Is returned if this is a wide ported port. false Is returned if
884 * this is a narrow port.
886 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
891 for (index = 0; index < SCI_MAX_PHYS; index++) {
892 if (sci_port->phy_table[index] != NULL) {
897 return phy_count != 1;
901 * This method is called by the PHY object when the link is detected. if the
902 * port wants the PHY to continue on to the link up state then the port
903 * layer must return true. If the port object returns false the phy object
904 * must halt its attempt to go link up.
905 * @sci_port: The port associated with the phy object.
906 * @sci_phy: The phy object that is trying to go link up.
908 * true if the phy object can continue to the link up condition. true Is
909 * returned if this phy can continue to the ready state. false Is returned if
910 * can not continue on to the ready state. This notification is in place for
911 * wide ports and direct attached phys. Since there are no wide ported SATA
912 * devices this could become an invalid port configuration.
914 bool scic_sds_port_link_detected(
915 struct scic_sds_port *sci_port,
916 struct scic_sds_phy *sci_phy)
918 if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
919 (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
920 scic_sds_port_is_wide(sci_port)) {
921 scic_sds_port_invalid_link_up(sci_port, sci_phy);
929 static void port_timeout(unsigned long data)
931 struct sci_timer *tmr = (struct sci_timer *)data;
932 struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
933 struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
937 spin_lock_irqsave(&ihost->scic_lock, flags);
942 current_state = sci_base_state_machine_get_state(&sci_port->state_machine);
944 if (current_state == SCI_BASE_PORT_STATE_RESETTING) {
945 /* if the port is still in the resetting state then the timeout
946 * fired before the reset completed.
948 port_state_machine_change(sci_port, SCI_BASE_PORT_STATE_FAILED);
949 } else if (current_state == SCI_BASE_PORT_STATE_STOPPED) {
950 /* if the port is stopped then the start request failed In this
951 * case stay in the stopped state.
953 dev_err(sciport_to_dev(sci_port),
954 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
957 } else if (current_state == SCI_BASE_PORT_STATE_STOPPING) {
958 /* if the port is still stopping then the stop has not completed */
959 isci_port_stop_complete(sci_port->owning_controller,
961 SCI_FAILURE_TIMEOUT);
963 /* The port is in the ready state and we have a timer
964 * reporting a timeout this should not happen.
966 dev_err(sciport_to_dev(sci_port),
967 "%s: SCIC Port 0x%p is processing a timeout operation "
968 "in state %d.\n", __func__, sci_port, current_state);
972 spin_unlock_irqrestore(&ihost->scic_lock, flags);
975 /* --------------------------------------------------------------------------- */
978 * This function updates the hardwares VIIT entry for this port.
982 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
984 struct sci_sas_address sas_address;
986 scic_sds_port_get_sas_address(sci_port, &sas_address);
988 writel(sas_address.high,
989 &sci_port->viit_registers->initiator_sas_address_hi);
990 writel(sas_address.low,
991 &sci_port->viit_registers->initiator_sas_address_lo);
993 /* This value get cleared just in case its not already cleared */
994 writel(0, &sci_port->viit_registers->reserved);
996 /* We are required to update the status register last */
997 writel(SCU_VIIT_ENTRY_ID_VIIT |
998 SCU_VIIT_IPPT_INITIATOR |
999 ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1000 SCU_VIIT_STATUS_ALL_VALID,
1001 &sci_port->viit_registers->status);
1005 * This method returns the maximum allowed speed for data transfers on this
1006 * port. This maximum allowed speed evaluates to the maximum speed of the
1007 * slowest phy in the port.
1008 * @sci_port: This parameter specifies the port for which to retrieve the
1009 * maximum allowed speed.
1011 * This method returns the maximum negotiated speed of the slowest phy in the
1014 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1015 struct scic_sds_port *sci_port)
1018 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1019 struct scic_sds_phy *phy = NULL;
1022 * Loop through all of the phys in this port and find the phy with the
1023 * lowest maximum link rate. */
1024 for (index = 0; index < SCI_MAX_PHYS; index++) {
1025 phy = sci_port->phy_table[index];
1028 && (scic_sds_port_active_phy(sci_port, phy) == true)
1029 && (phy->max_negotiated_speed < max_allowed_speed)
1031 max_allowed_speed = phy->max_negotiated_speed;
1034 return max_allowed_speed;
1037 static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1039 struct scic_sds_phy *phy;
1043 /* Loop through all of the phys to enable BCN. */
1044 for (index = 0; index < SCI_MAX_PHYS; index++) {
1045 phy = port->phy_table[index];
1048 readl(&phy->link_layer_registers->link_layer_control);
1050 /* clear the bit by writing 1. */
1051 writel(register_value,
1052 &phy->link_layer_registers->link_layer_control);
1059 * @sci_port: This is the struct scic_sds_port object to suspend.
1061 * This method will susped the port task scheduler for this port object. none
1064 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1066 u32 pts_control_value;
1068 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1069 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1070 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1074 * scic_sds_port_post_dummy_request() - post dummy/workaround request
1075 * @sci_port: port to post task
1077 * Prevent the hardware scheduler from posting new requests to the front
1078 * of the scheduler queue causing a starvation problem for currently
1082 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1085 struct scu_task_context *task_context;
1086 struct scic_sds_controller *scic = sci_port->owning_controller;
1087 u16 tci = sci_port->reserved_tci;
1089 task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1091 task_context->abort = 0;
1093 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1094 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1097 scic_sds_controller_post_request(scic, command);
1101 * This routine will abort the dummy request. This will alow the hardware to
1102 * power down parts of the silicon to save power.
1104 * @sci_port: The port on which the task must be aborted.
1107 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1109 struct scic_sds_controller *scic = sci_port->owning_controller;
1110 u16 tci = sci_port->reserved_tci;
1111 struct scu_task_context *tc;
1114 tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1118 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1119 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1122 scic_sds_controller_post_request(scic, command);
1127 * @sci_port: This is the struct scic_sds_port object to resume.
1129 * This method will resume the port task scheduler for this port object. none
1132 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1134 u32 pts_control_value;
1136 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1137 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1138 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1141 static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
1143 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1145 scic_sds_port_suspend_port_task_scheduler(sci_port);
1147 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1149 if (sci_port->active_phy_mask != 0) {
1150 /* At least one of the phys on the port is ready */
1151 port_state_machine_change(sci_port,
1152 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1156 static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
1159 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1160 struct scic_sds_controller *scic = sci_port->owning_controller;
1161 struct isci_host *ihost = scic_to_ihost(scic);
1162 struct isci_port *iport = sci_port_to_iport(sci_port);
1164 isci_port_ready(ihost, iport);
1166 for (index = 0; index < SCI_MAX_PHYS; index++) {
1167 if (sci_port->phy_table[index]) {
1168 writel(sci_port->physical_port_index,
1169 &sci_port->port_pe_configuration_register[
1170 sci_port->phy_table[index]->phy_index]);
1174 scic_sds_port_update_viit_entry(sci_port);
1176 scic_sds_port_resume_port_task_scheduler(sci_port);
1179 * Post the dummy task for the port so the hardware can schedule
1182 scic_sds_port_post_dummy_request(sci_port);
1185 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1187 struct scic_sds_controller *scic = sci_port->owning_controller;
1188 u8 phys_index = sci_port->physical_port_index;
1189 union scu_remote_node_context *rnc;
1190 u16 rni = sci_port->reserved_rni;
1193 rnc = &scic->remote_node_context_table[rni];
1195 rnc->ssp.is_valid = false;
1197 /* ensure the preceding tc abort request has reached the
1198 * controller and give it ample time to act before posting the rnc
1201 readl(&scic->smu_registers->interrupt_status); /* flush */
1204 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1205 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1207 scic_sds_controller_post_request(scic, command);
1212 * @object: This is the object which is cast to a struct scic_sds_port object.
1214 * This method will perform the actions required by the struct scic_sds_port on
1215 * exiting the SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL. This function reports
1216 * the port not ready and suspends the port task scheduler. none
1218 static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
1220 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1221 struct scic_sds_controller *scic = sci_port->owning_controller;
1222 struct isci_host *ihost = scic_to_ihost(scic);
1223 struct isci_port *iport = sci_port_to_iport(sci_port);
1226 * Kill the dummy task for this port if it has not yet posted
1227 * the hardware will treat this as a NOP and just return abort
1230 scic_sds_port_abort_dummy_request(sci_port);
1232 isci_port_not_ready(ihost, iport);
1234 if (sci_port->ready_exit)
1235 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1238 static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
1240 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1241 struct scic_sds_controller *scic = sci_port->owning_controller;
1242 struct isci_host *ihost = scic_to_ihost(scic);
1243 struct isci_port *iport = sci_port_to_iport(sci_port);
1245 if (sci_port->active_phy_mask == 0) {
1246 isci_port_not_ready(ihost, iport);
1248 port_state_machine_change(sci_port,
1249 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1250 } else if (sci_port->started_request_count == 0)
1251 port_state_machine_change(sci_port,
1252 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1255 static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
1257 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1259 scic_sds_port_suspend_port_task_scheduler(sci_port);
1260 if (sci_port->ready_exit)
1261 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1264 enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1266 struct scic_sds_controller *scic = sci_port->owning_controller;
1267 enum sci_status status = SCI_SUCCESS;
1268 enum scic_sds_port_states state;
1271 state = sci_port->state_machine.current_state_id;
1272 if (state != SCI_BASE_PORT_STATE_STOPPED) {
1273 dev_warn(sciport_to_dev(sci_port),
1274 "%s: in wrong state: %d\n", __func__, state);
1275 return SCI_FAILURE_INVALID_STATE;
1278 if (sci_port->assigned_device_count > 0) {
1279 /* TODO This is a start failure operation because
1280 * there are still devices assigned to this port.
1281 * There must be no devices assigned to a port on a
1284 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1287 if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1288 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1289 &scic->available_remote_nodes, 1);
1291 if (rni != SCU_DUMMY_INDEX)
1292 scic_sds_port_construct_dummy_rnc(sci_port, rni);
1294 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1295 sci_port->reserved_rni = rni;
1298 if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1299 /* Allocate a TCI and remove the sequence nibble */
1300 u16 tci = scic_controller_allocate_io_tag(scic);
1302 if (tci != SCU_DUMMY_INDEX)
1303 scic_sds_port_construct_dummy_task(sci_port, tci);
1305 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1306 sci_port->reserved_tci = tci;
1309 if (status == SCI_SUCCESS) {
1310 phy_mask = scic_sds_port_get_phys(sci_port);
1313 * There are one or more phys assigned to this port. Make sure
1314 * the port's phy mask is in fact legal and supported by the
1317 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1318 port_state_machine_change(sci_port,
1319 SCI_BASE_PORT_STATE_READY);
1323 status = SCI_FAILURE;
1326 if (status != SCI_SUCCESS)
1327 scic_sds_port_destroy_dummy_resources(sci_port);
1332 enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
1334 enum scic_sds_port_states state;
1336 state = sci_port->state_machine.current_state_id;
1338 case SCI_BASE_PORT_STATE_STOPPED:
1340 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1341 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1342 case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1343 case SCI_BASE_PORT_STATE_RESETTING:
1344 port_state_machine_change(sci_port,
1345 SCI_BASE_PORT_STATE_STOPPING);
1348 dev_warn(sciport_to_dev(sci_port),
1349 "%s: in wrong state: %d\n", __func__, state);
1350 return SCI_FAILURE_INVALID_STATE;
1354 static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
1356 enum sci_status status = SCI_FAILURE_INVALID_PHY;
1357 struct scic_sds_phy *selected_phy = NULL;
1358 enum scic_sds_port_states state;
1361 state = sci_port->state_machine.current_state_id;
1362 if (state != SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL) {
1363 dev_warn(sciport_to_dev(sci_port),
1364 "%s: in wrong state: %d\n", __func__, state);
1365 return SCI_FAILURE_INVALID_STATE;
1368 /* Select a phy on which we can send the hard reset request. */
1369 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !selected_phy; phy_index++) {
1370 selected_phy = sci_port->phy_table[phy_index];
1372 !scic_sds_port_active_phy(sci_port, selected_phy)) {
1374 * We found a phy but it is not ready select
1377 selected_phy = NULL;
1381 /* If we have a phy then go ahead and start the reset procedure */
1384 status = scic_sds_phy_reset(selected_phy);
1386 if (status != SCI_SUCCESS)
1389 sci_mod_timer(&sci_port->timer, timeout);
1390 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1392 port_state_machine_change(sci_port,
1393 SCI_BASE_PORT_STATE_RESETTING);
1398 * scic_sds_port_add_phy() -
1399 * @sci_port: This parameter specifies the port in which the phy will be added.
1400 * @sci_phy: This parameter is the phy which is to be added to the port.
1402 * This method will add a PHY to the selected port. This method returns an
1403 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1404 * status is a failure to add the phy to the port.
1406 enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
1407 struct scic_sds_phy *sci_phy)
1409 enum sci_status status;
1410 enum scic_sds_port_states state;
1412 state = sci_port->state_machine.current_state_id;
1414 case SCI_BASE_PORT_STATE_STOPPED: {
1415 struct sci_sas_address port_sas_address;
1417 /* Read the port assigned SAS Address if there is one */
1418 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
1420 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1421 struct sci_sas_address phy_sas_address;
1423 /* Make sure that the PHY SAS Address matches the SAS Address
1426 scic_sds_phy_get_sas_address(sci_phy, &phy_sas_address);
1428 if (port_sas_address.high != phy_sas_address.high ||
1429 port_sas_address.low != phy_sas_address.low)
1430 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1432 return scic_sds_port_set_phy(sci_port, sci_phy);
1434 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1435 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1436 status = scic_sds_port_set_phy(sci_port, sci_phy);
1438 if (status != SCI_SUCCESS)
1441 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1442 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1443 port_state_machine_change(sci_port, SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1446 case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1447 status = scic_sds_port_set_phy(sci_port, sci_phy);
1449 if (status != SCI_SUCCESS)
1451 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1453 /* Re-enter the configuring state since this may be the last phy in
1456 port_state_machine_change(sci_port,
1457 SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1460 dev_warn(sciport_to_dev(sci_port),
1461 "%s: in wrong state: %d\n", __func__, state);
1462 return SCI_FAILURE_INVALID_STATE;
1467 * scic_sds_port_remove_phy() -
1468 * @sci_port: This parameter specifies the port in which the phy will be added.
1469 * @sci_phy: This parameter is the phy which is to be added to the port.
1471 * This method will remove the PHY from the selected PORT. This method returns
1472 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1473 * other status is a failure to add the phy to the port.
1475 enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
1476 struct scic_sds_phy *sci_phy)
1478 enum sci_status status;
1479 enum scic_sds_port_states state;
1481 state = sci_port->state_machine.current_state_id;
1484 case SCI_BASE_PORT_STATE_STOPPED:
1485 return scic_sds_port_clear_phy(sci_port, sci_phy);
1486 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1487 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1488 if (status != SCI_SUCCESS)
1491 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1492 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1493 port_state_machine_change(sci_port,
1494 SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1496 case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1497 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1499 if (status != SCI_SUCCESS)
1501 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1503 /* Re-enter the configuring state since this may be the last phy in
1506 port_state_machine_change(sci_port,
1507 SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING);
1510 dev_warn(sciport_to_dev(sci_port),
1511 "%s: in wrong state: %d\n", __func__, state);
1512 return SCI_FAILURE_INVALID_STATE;
1516 enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
1517 struct scic_sds_phy *sci_phy)
1519 enum scic_sds_port_states state;
1521 state = sci_port->state_machine.current_state_id;
1523 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1524 /* Since this is the first phy going link up for the port we
1525 * can just enable it and continue
1527 scic_sds_port_activate_phy(sci_port, sci_phy, true);
1529 port_state_machine_change(sci_port,
1530 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1532 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1533 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1535 case SCI_BASE_PORT_STATE_RESETTING:
1536 /* TODO We should make sure that the phy that has gone
1537 * link up is the same one on which we sent the reset. It is
1538 * possible that the phy on which we sent the reset is not the
1539 * one that has gone link up and we want to make sure that
1540 * phy being reset comes back. Consider the case where a
1541 * reset is sent but before the hardware processes the reset it
1542 * get a link up on the port because of a hot plug event.
1543 * because of the reset request this phy will go link down
1544 * almost immediately.
1547 /* In the resetting state we don't notify the user regarding
1548 * link up and link down notifications.
1550 scic_sds_port_general_link_up_handler(sci_port, sci_phy, false);
1553 dev_warn(sciport_to_dev(sci_port),
1554 "%s: in wrong state: %d\n", __func__, state);
1555 return SCI_FAILURE_INVALID_STATE;
1559 enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
1560 struct scic_sds_phy *sci_phy)
1562 enum scic_sds_port_states state;
1564 state = sci_port->state_machine.current_state_id;
1566 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1567 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1569 /* If there are no active phys left in the port, then
1570 * transition the port to the WAITING state until such time
1571 * as a phy goes link up
1573 if (sci_port->active_phy_mask == 0)
1574 port_state_machine_change(sci_port,
1575 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1577 case SCI_BASE_PORT_STATE_RESETTING:
1578 /* In the resetting state we don't notify the user regarding
1579 * link up and link down notifications. */
1580 scic_sds_port_deactivate_phy(sci_port, sci_phy, false);
1583 dev_warn(sciport_to_dev(sci_port),
1584 "%s: in wrong state: %d\n", __func__, state);
1585 return SCI_FAILURE_INVALID_STATE;
1589 enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
1590 struct scic_sds_remote_device *sci_dev,
1591 struct scic_sds_request *sci_req)
1593 enum scic_sds_port_states state;
1595 state = sci_port->state_machine.current_state_id;
1597 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1598 return SCI_FAILURE_INVALID_STATE;
1599 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1600 sci_port->started_request_count++;
1603 dev_warn(sciport_to_dev(sci_port),
1604 "%s: in wrong state: %d\n", __func__, state);
1605 return SCI_FAILURE_INVALID_STATE;
1609 enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
1610 struct scic_sds_remote_device *sci_dev,
1611 struct scic_sds_request *sci_req)
1613 enum scic_sds_port_states state;
1615 state = sci_port->state_machine.current_state_id;
1617 case SCI_BASE_PORT_STATE_STOPPED:
1618 dev_warn(sciport_to_dev(sci_port),
1619 "%s: in wrong state: %d\n", __func__, state);
1620 return SCI_FAILURE_INVALID_STATE;
1621 case SCI_BASE_PORT_STATE_STOPPING:
1622 scic_sds_port_decrement_request_count(sci_port);
1624 if (sci_port->started_request_count == 0)
1625 port_state_machine_change(sci_port,
1626 SCI_BASE_PORT_STATE_STOPPED);
1628 case SCI_BASE_PORT_STATE_READY:
1629 case SCI_BASE_PORT_STATE_RESETTING:
1630 case SCI_BASE_PORT_STATE_FAILED:
1631 case SCIC_SDS_PORT_READY_SUBSTATE_WAITING:
1632 case SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL:
1633 scic_sds_port_decrement_request_count(sci_port);
1635 case SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING:
1636 scic_sds_port_decrement_request_count(sci_port);
1637 if (sci_port->started_request_count == 0) {
1638 port_state_machine_change(sci_port,
1639 SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL);
1648 * @sci_port: This is the port object which to suspend.
1650 * This method will enable the SCU Port Task Scheduler for this port object but
1651 * will leave the port task scheduler in a suspended state. none
1654 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
1656 u32 pts_control_value;
1658 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1659 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1660 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1665 * @sci_port: This is the port object which to resume.
1667 * This method will disable the SCU port task scheduler for this port object.
1671 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
1673 u32 pts_control_value;
1675 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1676 pts_control_value &=
1677 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1678 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1681 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
1683 struct scic_sds_controller *scic = sci_port->owning_controller;
1684 u8 phys_index = sci_port->physical_port_index;
1685 union scu_remote_node_context *rnc;
1686 u16 rni = sci_port->reserved_rni;
1689 rnc = &scic->remote_node_context_table[rni];
1690 rnc->ssp.is_valid = true;
1692 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1693 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1695 scic_sds_controller_post_request(scic, command);
1697 /* ensure hardware has seen the post rnc command and give it
1698 * ample time to act before sending the suspend
1700 readl(&scic->smu_registers->interrupt_status); /* flush */
1703 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1704 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1706 scic_sds_controller_post_request(scic, command);
1709 static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
1711 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1713 if (sci_port->state_machine.previous_state_id == SCI_BASE_PORT_STATE_STOPPING) {
1715 * If we enter this state becasuse of a request to stop
1716 * the port then we want to disable the hardwares port
1717 * task scheduler. */
1718 scic_sds_port_disable_port_task_scheduler(sci_port);
1722 static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
1724 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1726 /* Enable and suspend the port task scheduler */
1727 scic_sds_port_enable_port_task_scheduler(sci_port);
1730 static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
1732 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1733 struct scic_sds_controller *scic = sci_port->owning_controller;
1734 struct isci_host *ihost = scic_to_ihost(scic);
1735 struct isci_port *iport = sci_port_to_iport(sci_port);
1738 prev_state = sci_port->state_machine.previous_state_id;
1739 if (prev_state == SCI_BASE_PORT_STATE_RESETTING)
1740 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1742 isci_port_not_ready(ihost, iport);
1744 /* Post and suspend the dummy remote node context for this port. */
1745 scic_sds_port_post_dummy_remote_node(sci_port);
1747 /* Start the ready substate machine */
1748 port_state_machine_change(sci_port,
1749 SCIC_SDS_PORT_READY_SUBSTATE_WAITING);
1752 static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
1754 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1756 sci_del_timer(&sci_port->timer);
1759 static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
1761 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1763 sci_del_timer(&sci_port->timer);
1765 scic_sds_port_destroy_dummy_resources(sci_port);
1768 static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
1770 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), state_machine);
1771 struct isci_port *iport = sci_port_to_iport(sci_port);
1773 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1776 /* --------------------------------------------------------------------------- */
1778 static const struct sci_base_state scic_sds_port_state_table[] = {
1779 [SCI_BASE_PORT_STATE_STOPPED] = {
1780 .enter_state = scic_sds_port_stopped_state_enter,
1781 .exit_state = scic_sds_port_stopped_state_exit
1783 [SCI_BASE_PORT_STATE_STOPPING] = {
1784 .exit_state = scic_sds_port_stopping_state_exit
1786 [SCI_BASE_PORT_STATE_READY] = {
1787 .enter_state = scic_sds_port_ready_state_enter,
1789 [SCIC_SDS_PORT_READY_SUBSTATE_WAITING] = {
1790 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1792 [SCIC_SDS_PORT_READY_SUBSTATE_OPERATIONAL] = {
1793 .enter_state = scic_sds_port_ready_substate_operational_enter,
1794 .exit_state = scic_sds_port_ready_substate_operational_exit
1796 [SCIC_SDS_PORT_READY_SUBSTATE_CONFIGURING] = {
1797 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1798 .exit_state = scic_sds_port_ready_substate_configuring_exit
1800 [SCI_BASE_PORT_STATE_RESETTING] = {
1801 .exit_state = scic_sds_port_resetting_state_exit
1803 [SCI_BASE_PORT_STATE_FAILED] = {
1804 .enter_state = scic_sds_port_failed_state_enter,
1808 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
1809 struct scic_sds_controller *scic)
1811 sci_base_state_machine_construct(&sci_port->state_machine,
1812 scic_sds_port_state_table,
1813 SCI_BASE_PORT_STATE_STOPPED);
1815 sci_base_state_machine_start(&sci_port->state_machine);
1817 sci_port->logical_port_index = SCIC_SDS_DUMMY_PORT;
1818 sci_port->physical_port_index = index;
1819 sci_port->active_phy_mask = 0;
1820 sci_port->ready_exit = false;
1822 sci_port->owning_controller = scic;
1824 sci_port->started_request_count = 0;
1825 sci_port->assigned_device_count = 0;
1827 sci_port->reserved_rni = SCU_DUMMY_INDEX;
1828 sci_port->reserved_tci = SCU_DUMMY_INDEX;
1830 sci_init_timer(&sci_port->timer, port_timeout);
1832 sci_port->port_task_scheduler_registers = NULL;
1834 for (index = 0; index < SCI_MAX_PHYS; index++)
1835 sci_port->phy_table[index] = NULL;
1838 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1840 INIT_LIST_HEAD(&iport->remote_dev_list);
1841 INIT_LIST_HEAD(&iport->domain_dev_list);
1842 spin_lock_init(&iport->state_lock);
1843 init_completion(&iport->start_complete);
1844 iport->isci_host = ihost;
1845 isci_port_change_state(iport, isci_freed);
1849 * isci_port_get_state() - This function gets the status of the port object.
1850 * @isci_port: This parameter points to the isci_port object
1852 * status of the object as a isci_status enum.
1854 enum isci_status isci_port_get_state(
1855 struct isci_port *isci_port)
1857 return isci_port->status;
1860 static void isci_port_bc_change_received(struct isci_host *ihost,
1861 struct scic_sds_port *sci_port,
1862 struct scic_sds_phy *sci_phy)
1864 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
1866 dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
1867 __func__, iphy, &iphy->sas_phy);
1869 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
1870 scic_port_enable_broadcast_change_notification(sci_port);
1873 void scic_sds_port_broadcast_change_received(
1874 struct scic_sds_port *sci_port,
1875 struct scic_sds_phy *sci_phy)
1877 struct scic_sds_controller *scic = sci_port->owning_controller;
1878 struct isci_host *ihost = scic_to_ihost(scic);
1880 /* notify the user. */
1881 isci_port_bc_change_received(ihost, sci_port, sci_phy);
1884 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1885 struct isci_phy *iphy)
1887 unsigned long flags;
1888 enum sci_status status;
1889 int ret = TMF_RESP_FUNC_COMPLETE;
1891 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1894 init_completion(&iport->hard_reset_complete);
1896 spin_lock_irqsave(&ihost->scic_lock, flags);
1898 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1899 status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
1901 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1903 if (status == SCI_SUCCESS) {
1904 wait_for_completion(&iport->hard_reset_complete);
1906 dev_dbg(&ihost->pdev->dev,
1907 "%s: iport = %p; hard reset completion\n",
1910 if (iport->hard_reset_status != SCI_SUCCESS)
1911 ret = TMF_RESP_FUNC_FAILED;
1913 ret = TMF_RESP_FUNC_FAILED;
1915 dev_err(&ihost->pdev->dev,
1916 "%s: iport = %p; scic_port_hard_reset call"
1918 __func__, iport, status);
1922 /* If the hard reset for the port has failed, consider this
1923 * the same as link failures on all phys in the port.
1925 if (ret != TMF_RESP_FUNC_COMPLETE) {
1926 dev_err(&ihost->pdev->dev,
1927 "%s: iport = %p; hard reset failed "
1928 "(0x%x) - sending link down to libsas for phy %p\n",
1929 __func__, iport, iport->hard_reset_status, iphy);
1931 isci_port_link_down(ihost, iphy, iport);
1938 * isci_port_deformed() - This function is called by libsas when a port becomes
1940 * @phy: This parameter specifies the libsas phy with the inactive port.
1943 void isci_port_deformed(struct asd_sas_phy *phy)
1945 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1949 * isci_port_formed() - This function is called by libsas when a port becomes
1951 * @phy: This parameter specifies the libsas phy with the active port.
1954 void isci_port_formed(struct asd_sas_phy *phy)
1956 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);