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.
60 #define SCIC_SDS_PORT_HARD_RESET_TIMEOUT (1000)
61 #define SCU_DUMMY_INDEX (0xFFFF)
63 static void isci_port_change_state(struct isci_port *iport, enum isci_status status)
67 dev_dbg(&iport->isci_host->pdev->dev,
68 "%s: iport = %p, state = 0x%x\n",
69 __func__, iport, status);
71 /* XXX pointless lock */
72 spin_lock_irqsave(&iport->state_lock, flags);
73 iport->status = status;
74 spin_unlock_irqrestore(&iport->state_lock, flags);
78 * This function will indicate which protocols are supported by this port.
79 * @sci_port: a handle corresponding to the SAS port for which to return the
80 * supported protocols.
81 * @protocols: This parameter specifies a pointer to a data structure
82 * which the core will copy the protocol values for the port from the
83 * transmit_identification register.
86 scic_sds_port_get_protocols(struct scic_sds_port *sci_port,
87 struct scic_phy_proto *protocols)
93 for (index = 0; index < SCI_MAX_PHYS; index++) {
94 if (sci_port->phy_table[index] != NULL) {
95 scic_sds_phy_get_protocols(sci_port->phy_table[index],
102 * This method requests a list (mask) of the phys contained in the supplied SAS
104 * @sci_port: a handle corresponding to the SAS port for which to return the
107 * Return a bit mask indicating which phys are a part of this port. Each bit
108 * corresponds to a phy identifier (e.g. bit 0 = phy id 0).
110 static u32 scic_sds_port_get_phys(struct scic_sds_port *sci_port)
117 for (index = 0; index < SCI_MAX_PHYS; index++) {
118 if (sci_port->phy_table[index] != NULL) {
119 mask |= (1 << index);
127 * scic_port_get_properties() - This method simply returns the properties
128 * regarding the port, such as: physical index, protocols, sas address, etc.
129 * @port: this parameter specifies the port for which to retrieve the physical
131 * @properties: This parameter specifies the properties structure into which to
132 * copy the requested information.
134 * Indicate if the user specified a valid port. SCI_SUCCESS This value is
135 * returned if the specified port was valid. SCI_FAILURE_INVALID_PORT This
136 * value is returned if the specified port is not valid. When this value is
137 * returned, no data is copied to the properties output parameter.
139 static enum sci_status scic_port_get_properties(struct scic_sds_port *port,
140 struct scic_port_properties *prop)
142 if ((port == NULL) ||
143 (port->logical_port_index == SCIC_SDS_DUMMY_PORT))
144 return SCI_FAILURE_INVALID_PORT;
146 prop->index = port->logical_port_index;
147 prop->phy_mask = scic_sds_port_get_phys(port);
148 scic_sds_port_get_sas_address(port, &prop->local.sas_address);
149 scic_sds_port_get_protocols(port, &prop->local.protocols);
150 scic_sds_port_get_attached_sas_address(port, &prop->remote.sas_address);
155 static void isci_port_link_up(struct isci_host *isci_host,
156 struct scic_sds_port *port,
157 struct scic_sds_phy *phy)
160 struct scic_port_properties properties;
161 struct isci_phy *isci_phy = sci_phy_to_iphy(phy);
162 struct isci_port *isci_port = sci_port_to_iport(port);
163 unsigned long success = true;
165 BUG_ON(isci_phy->isci_port != NULL);
167 isci_phy->isci_port = isci_port;
169 dev_dbg(&isci_host->pdev->dev,
170 "%s: isci_port = %p\n",
171 __func__, isci_port);
173 spin_lock_irqsave(&isci_phy->sas_phy.frame_rcvd_lock, flags);
175 isci_port_change_state(isci_phy->isci_port, isci_starting);
177 scic_port_get_properties(port, &properties);
179 if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
180 u64 attached_sas_address;
182 isci_phy->sas_phy.oob_mode = SATA_OOB_MODE;
183 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
186 * For direct-attached SATA devices, the SCI core will
187 * automagically assign a SAS address to the end device
188 * for the purpose of creating a port. This SAS address
189 * will not be the same as assigned to the PHY and needs
190 * to be obtained from struct scic_port_properties properties.
192 attached_sas_address = properties.remote.sas_address.high;
193 attached_sas_address <<= 32;
194 attached_sas_address |= properties.remote.sas_address.low;
195 swab64s(&attached_sas_address);
197 memcpy(&isci_phy->sas_phy.attached_sas_addr,
198 &attached_sas_address, sizeof(attached_sas_address));
199 } else if (phy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
200 isci_phy->sas_phy.oob_mode = SAS_OOB_MODE;
201 isci_phy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
203 /* Copy the attached SAS address from the IAF */
204 memcpy(isci_phy->sas_phy.attached_sas_addr,
205 isci_phy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
207 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
211 isci_phy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(phy);
213 spin_unlock_irqrestore(&isci_phy->sas_phy.frame_rcvd_lock, flags);
215 /* Notify libsas that we have an address frame, if indeed
216 * we've found an SSP, SMP, or STP target */
218 isci_host->sas_ha.notify_port_event(&isci_phy->sas_phy,
224 * isci_port_link_down() - This function is called by the sci core when a link
226 * @isci_host: This parameter specifies the isci host object.
227 * @phy: This parameter specifies the isci phy with the active link.
228 * @port: This parameter specifies the isci port with the active link.
231 static void isci_port_link_down(struct isci_host *isci_host,
232 struct isci_phy *isci_phy,
233 struct isci_port *isci_port)
235 struct isci_remote_device *isci_device;
237 dev_dbg(&isci_host->pdev->dev,
238 "%s: isci_port = %p\n", __func__, isci_port);
242 /* check to see if this is the last phy on this port. */
243 if (isci_phy->sas_phy.port
244 && isci_phy->sas_phy.port->num_phys == 1) {
246 /* change the state for all devices on this port.
247 * The next task sent to this device will be returned
248 * as SAS_TASK_UNDELIVERED, and the scsi mid layer
249 * will remove the target
251 list_for_each_entry(isci_device,
252 &isci_port->remote_dev_list,
254 dev_dbg(&isci_host->pdev->dev,
255 "%s: isci_device = %p\n",
256 __func__, isci_device);
257 isci_remote_device_change_state(isci_device,
261 isci_port_change_state(isci_port, isci_stopping);
264 /* Notify libsas of the borken link, this will trigger calls to our
265 * isci_port_deformed and isci_dev_gone functions.
267 sas_phy_disconnected(&isci_phy->sas_phy);
268 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
269 PHYE_LOSS_OF_SIGNAL);
271 isci_phy->isci_port = NULL;
273 dev_dbg(&isci_host->pdev->dev,
274 "%s: isci_port = %p - Done\n", __func__, isci_port);
279 * isci_port_ready() - This function is called by the sci core when a link
281 * @isci_host: This parameter specifies the isci host object.
282 * @port: This parameter specifies the sci port with the active link.
285 static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
287 dev_dbg(&isci_host->pdev->dev,
288 "%s: isci_port = %p\n", __func__, isci_port);
290 complete_all(&isci_port->start_complete);
291 isci_port_change_state(isci_port, isci_ready);
296 * isci_port_not_ready() - This function is called by the sci core when a link
297 * is not ready. All remote devices on this link will be removed if they are
298 * in the stopping state.
299 * @isci_host: This parameter specifies the isci host object.
300 * @port: This parameter specifies the sci port with the active link.
303 static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
305 dev_dbg(&isci_host->pdev->dev,
306 "%s: isci_port = %p\n", __func__, isci_port);
309 static void isci_port_stop_complete(struct scic_sds_controller *scic,
310 struct scic_sds_port *sci_port,
311 enum sci_status completion_status)
313 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
317 * isci_port_hard_reset_complete() - This function is called by the sci core
318 * when the hard reset complete notification has been received.
319 * @port: This parameter specifies the sci port with the active link.
320 * @completion_status: This parameter specifies the core status for the reset
324 static void isci_port_hard_reset_complete(struct isci_port *isci_port,
325 enum sci_status completion_status)
327 dev_dbg(&isci_port->isci_host->pdev->dev,
328 "%s: isci_port = %p, completion_status=%x\n",
329 __func__, isci_port, completion_status);
331 /* Save the status of the hard reset from the port. */
332 isci_port->hard_reset_status = completion_status;
334 complete_all(&isci_port->hard_reset_complete);
337 /* This method will return a true value if the specified phy can be assigned to
338 * this port The following is a list of phys for each port that are allowed: -
339 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
340 * doesn't preclude all configurations. It merely ensures that a phy is part
341 * of the allowable set of phy identifiers for that port. For example, one
342 * could assign phy 3 to port 0 and no other phys. Please refer to
343 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
344 * phy_mask for a port can be supported. bool true if this is a valid phy
345 * assignment for the port false if this is not a valid phy assignment for the
348 bool scic_sds_port_is_valid_phy_assignment(struct scic_sds_port *sci_port,
351 /* Initialize to invalid value. */
352 u32 existing_phy_index = SCI_MAX_PHYS;
355 if ((sci_port->physical_port_index == 1) && (phy_index != 1)) {
359 if (sci_port->physical_port_index == 3 && phy_index != 3) {
364 (sci_port->physical_port_index == 2)
365 && ((phy_index == 0) || (phy_index == 1))
370 for (index = 0; index < SCI_MAX_PHYS; index++) {
371 if ((sci_port->phy_table[index] != NULL)
372 && (index != phy_index)) {
373 existing_phy_index = index;
378 * Ensure that all of the phys in the port are capable of
379 * operating at the same maximum link rate. */
381 (existing_phy_index < SCI_MAX_PHYS)
382 && (sci_port->owning_controller->user_parameters.sds1.phys[
383 phy_index].max_speed_generation !=
384 sci_port->owning_controller->user_parameters.sds1.phys[
385 existing_phy_index].max_speed_generation)
394 * @sci_port: This is the port object for which to determine if the phy mask
397 * This method will return a true value if the port's phy mask can be supported
398 * by the SCU. The following is a list of valid PHY mask configurations for
399 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
400 * - Port 3 - [3] This method returns a boolean indication specifying if the
401 * phy mask can be supported. true if this is a valid phy assignment for the
402 * port false if this is not a valid phy assignment for the port
404 static bool scic_sds_port_is_phy_mask_valid(
405 struct scic_sds_port *sci_port,
408 if (sci_port->physical_port_index == 0) {
409 if (((phy_mask & 0x0F) == 0x0F)
410 || ((phy_mask & 0x03) == 0x03)
411 || ((phy_mask & 0x01) == 0x01)
414 } else if (sci_port->physical_port_index == 1) {
415 if (((phy_mask & 0x02) == 0x02)
418 } else if (sci_port->physical_port_index == 2) {
419 if (((phy_mask & 0x0C) == 0x0C)
420 || ((phy_mask & 0x04) == 0x04)
423 } else if (sci_port->physical_port_index == 3) {
424 if (((phy_mask & 0x08) == 0x08)
434 * @sci_port: This parameter specifies the port from which to return a
437 * This method retrieves a currently active (i.e. connected) phy contained in
438 * the port. Currently, the lowest order phy that is connected is returned.
439 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
440 * returned if there are no currently active (i.e. connected to a remote end
441 * point) phys contained in the port. All other values specify a struct scic_sds_phy
442 * object that is active in the port.
444 static struct scic_sds_phy *scic_sds_port_get_a_connected_phy(
445 struct scic_sds_port *sci_port
448 struct scic_sds_phy *phy;
450 for (index = 0; index < SCI_MAX_PHYS; index++) {
452 * Ensure that the phy is both part of the port and currently
453 * connected to the remote end-point. */
454 phy = sci_port->phy_table[index];
457 && scic_sds_port_active_phy(sci_port, phy)
467 * scic_sds_port_set_phy() -
468 * @out]: port The port object to which the phy assignement is being made.
469 * @out]: phy The phy which is being assigned to the port.
471 * This method attempts to make the assignment of the phy to the port. If
472 * successful the phy is assigned to the ports phy table. bool true if the phy
473 * assignment can be made. false if the phy assignement can not be made. This
474 * is a functional test that only fails if the phy is currently assigned to a
477 static enum sci_status scic_sds_port_set_phy(
478 struct scic_sds_port *port,
479 struct scic_sds_phy *phy)
482 * Check to see if we can add this phy to a port
483 * that means that the phy is not part of a port and that the port does
484 * not already have a phy assinged to the phy index. */
486 (port->phy_table[phy->phy_index] == NULL)
487 && (phy_get_non_dummy_port(phy) == NULL)
488 && scic_sds_port_is_valid_phy_assignment(port, phy->phy_index)
491 * Phy is being added in the stopped state so we are in MPC mode
492 * make logical port index = physical port index */
493 port->logical_port_index = port->physical_port_index;
494 port->phy_table[phy->phy_index] = phy;
495 scic_sds_phy_set_port(phy, port);
504 * scic_sds_port_clear_phy() -
505 * @out]: port The port from which the phy is being cleared.
506 * @out]: phy The phy being cleared from the port.
508 * This method will clear the phy assigned to this port. This method fails if
509 * this phy is not currently assinged to this port. bool true if the phy is
510 * removed from the port. false if this phy is not assined to this port.
512 static enum sci_status scic_sds_port_clear_phy(
513 struct scic_sds_port *port,
514 struct scic_sds_phy *phy)
516 /* Make sure that this phy is part of this port */
517 if (port->phy_table[phy->phy_index] == phy &&
518 phy_get_non_dummy_port(phy) == port) {
519 struct scic_sds_controller *scic = port->owning_controller;
520 struct isci_host *ihost = scic_to_ihost(scic);
522 /* Yep it is assigned to this port so remove it */
523 scic_sds_phy_set_port(phy, &ihost->ports[SCI_MAX_PORTS].sci);
524 port->phy_table[phy->phy_index] = NULL;
533 * This method requests the SAS address for the supplied SAS port from the SCI
535 * @sci_port: a handle corresponding to the SAS port for which to return the
537 * @sas_address: This parameter specifies a pointer to a SAS address structure
538 * into which the core will copy the SAS address for the port.
541 void scic_sds_port_get_sas_address(
542 struct scic_sds_port *sci_port,
543 struct sci_sas_address *sas_address)
547 sas_address->high = 0;
548 sas_address->low = 0;
550 for (index = 0; index < SCI_MAX_PHYS; index++) {
551 if (sci_port->phy_table[index] != NULL) {
552 scic_sds_phy_get_sas_address(sci_port->phy_table[index], sas_address);
558 * This function requests the SAS address for the device directly attached to
560 * @sci_port: a handle corresponding to the SAS port for which to return the
562 * @sas_address: This parameter specifies a pointer to a SAS address structure
563 * into which the core will copy the SAS address for the device directly
564 * attached to the port.
567 void scic_sds_port_get_attached_sas_address(
568 struct scic_sds_port *sci_port,
569 struct sci_sas_address *sas_address)
571 struct scic_sds_phy *sci_phy;
574 * Ensure that the phy is both part of the port and currently
575 * connected to the remote end-point.
577 sci_phy = scic_sds_port_get_a_connected_phy(sci_port);
579 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
580 scic_sds_phy_get_attached_sas_address(sci_phy,
583 scic_sds_phy_get_sas_address(sci_phy, sas_address);
584 sas_address->low += sci_phy->phy_index;
587 sas_address->high = 0;
588 sas_address->low = 0;
593 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
595 * @sci_port: logical port on which we need to create the remote node context
596 * @rni: remote node index for this remote node context.
598 * This routine will construct a dummy remote node context data structure
599 * This structure will be posted to the hardware to work around a scheduler
600 * error in the hardware.
602 static void scic_sds_port_construct_dummy_rnc(struct scic_sds_port *sci_port, u16 rni)
604 union scu_remote_node_context *rnc;
606 rnc = &sci_port->owning_controller->remote_node_context_table[rni];
608 memset(rnc, 0, sizeof(union scu_remote_node_context));
610 rnc->ssp.remote_sas_address_hi = 0;
611 rnc->ssp.remote_sas_address_lo = 0;
613 rnc->ssp.remote_node_index = rni;
614 rnc->ssp.remote_node_port_width = 1;
615 rnc->ssp.logical_port_index = sci_port->physical_port_index;
617 rnc->ssp.nexus_loss_timer_enable = false;
618 rnc->ssp.check_bit = false;
619 rnc->ssp.is_valid = true;
620 rnc->ssp.is_remote_node_context = true;
621 rnc->ssp.function_number = 0;
622 rnc->ssp.arbitration_wait_time = 0;
626 * scic_sds_port_construct_dummy_task() - create dummy task for si workaround
627 * @sci_port The logical port on which we need to create the
628 * remote node context.
630 * @tci The remote node index for this remote node context.
632 * This routine will construct a dummy task context data structure. This
633 * structure will be posted to the hardwre to work around a scheduler error
637 static void scic_sds_port_construct_dummy_task(struct scic_sds_port *sci_port, u16 tci)
639 struct scu_task_context *task_context;
641 task_context = scic_sds_controller_get_task_context_buffer(sci_port->owning_controller, tci);
643 memset(task_context, 0, sizeof(struct scu_task_context));
645 task_context->abort = 0;
646 task_context->priority = 0;
647 task_context->initiator_request = 1;
648 task_context->connection_rate = 1;
649 task_context->protocol_engine_index = 0;
650 task_context->logical_port_index = sci_port->physical_port_index;
651 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
652 task_context->task_index = scic_sds_io_tag_get_index(tci);
653 task_context->valid = SCU_TASK_CONTEXT_VALID;
654 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
656 task_context->remote_node_index = sci_port->reserved_rni;
657 task_context->command_code = 0;
659 task_context->link_layer_control = 0;
660 task_context->do_not_dma_ssp_good_response = 1;
661 task_context->strict_ordering = 0;
662 task_context->control_frame = 0;
663 task_context->timeout_enable = 0;
664 task_context->block_guard_enable = 0;
666 task_context->address_modifier = 0;
668 task_context->task_phase = 0x01;
671 static void scic_sds_port_destroy_dummy_resources(struct scic_sds_port *sci_port)
673 struct scic_sds_controller *scic = sci_port->owning_controller;
675 if (sci_port->reserved_tci != SCU_DUMMY_INDEX)
676 scic_controller_free_io_tag(scic, sci_port->reserved_tci);
678 if (sci_port->reserved_rni != SCU_DUMMY_INDEX)
679 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
680 1, sci_port->reserved_rni);
682 sci_port->reserved_rni = SCU_DUMMY_INDEX;
683 sci_port->reserved_tci = SCU_DUMMY_INDEX;
687 * This method performs initialization of the supplied port. Initialization
688 * includes: - state machine initialization - member variable initialization
689 * - configuring the phy_mask
691 * @transport_layer_registers:
692 * @port_task_scheduler_registers:
693 * @port_configuration_regsiter:
695 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
696 * if the phy being added to the port
698 enum sci_status scic_sds_port_initialize(
699 struct scic_sds_port *sci_port,
700 void __iomem *port_task_scheduler_registers,
701 void __iomem *port_configuration_regsiter,
702 void __iomem *viit_registers)
704 sci_port->port_task_scheduler_registers = port_task_scheduler_registers;
705 sci_port->port_pe_configuration_register = port_configuration_regsiter;
706 sci_port->viit_registers = viit_registers;
713 * This method assigns the direct attached device ID for this port.
715 * @param[in] sci_port The port for which the direct attached device id is to
717 * @param[in] device_id The direct attached device ID to assign to the port.
718 * This will be the RNi for the device
720 void scic_sds_port_setup_transports(
721 struct scic_sds_port *sci_port,
726 for (index = 0; index < SCI_MAX_PHYS; index++) {
727 if (sci_port->active_phy_mask & (1 << index))
728 scic_sds_phy_setup_transport(sci_port->phy_table[index], device_id);
734 * @sci_port: This is the port on which the phy should be enabled.
735 * @sci_phy: This is the specific phy which to enable.
736 * @do_notify_user: This parameter specifies whether to inform the user (via
737 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
739 * This function will activate the phy in the port.
740 * Activation includes: - adding
741 * the phy to the port - enabling the Protocol Engine in the silicon. -
742 * notifying the user that the link is up. none
744 static void scic_sds_port_activate_phy(struct scic_sds_port *sci_port,
745 struct scic_sds_phy *sci_phy,
748 struct scic_sds_controller *scic = sci_port->owning_controller;
749 struct isci_host *ihost = scic_to_ihost(scic);
751 if (sci_phy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
752 scic_sds_phy_resume(sci_phy);
754 sci_port->active_phy_mask |= 1 << sci_phy->phy_index;
756 scic_sds_controller_clear_invalid_phy(scic, sci_phy);
758 if (do_notify_user == true)
759 isci_port_link_up(ihost, sci_port, sci_phy);
762 void scic_sds_port_deactivate_phy(struct scic_sds_port *sci_port,
763 struct scic_sds_phy *sci_phy,
766 struct scic_sds_controller *scic = scic_sds_port_get_controller(sci_port);
767 struct isci_port *iport = sci_port_to_iport(sci_port);
768 struct isci_host *ihost = scic_to_ihost(scic);
769 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
771 sci_port->active_phy_mask &= ~(1 << sci_phy->phy_index);
773 sci_phy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
775 /* Re-assign the phy back to the LP as if it were a narrow port */
776 writel(sci_phy->phy_index,
777 &sci_port->port_pe_configuration_register[sci_phy->phy_index]);
779 if (do_notify_user == true)
780 isci_port_link_down(ihost, iphy, iport);
785 * @sci_port: This is the port on which the phy should be disabled.
786 * @sci_phy: This is the specific phy which to disabled.
788 * This function will disable the phy and report that the phy is not valid for
789 * this port object. None
791 static void scic_sds_port_invalid_link_up(struct scic_sds_port *sci_port,
792 struct scic_sds_phy *sci_phy)
794 struct scic_sds_controller *scic = sci_port->owning_controller;
797 * Check to see if we have alreay reported this link as bad and if
798 * not go ahead and tell the SCI_USER that we have discovered an
801 if ((scic->invalid_phy_mask & (1 << sci_phy->phy_index)) == 0) {
802 scic_sds_controller_set_invalid_phy(scic, sci_phy);
803 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
807 static bool is_port_ready_state(enum scic_sds_port_states state)
811 case SCI_PORT_SUB_WAITING:
812 case SCI_PORT_SUB_OPERATIONAL:
813 case SCI_PORT_SUB_CONFIGURING:
820 /* flag dummy rnc hanling when exiting a ready state */
821 static void port_state_machine_change(struct scic_sds_port *sci_port,
822 enum scic_sds_port_states state)
824 struct sci_base_state_machine *sm = &sci_port->sm;
825 enum scic_sds_port_states old_state = sm->current_state_id;
827 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
828 sci_port->ready_exit = true;
830 sci_change_state(sm, state);
831 sci_port->ready_exit = false;
835 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
836 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
837 * @sci_phy: This is the struct scic_sds_phy object that has gone link up.
838 * @do_notify_user: This parameter specifies whether to inform the user (via
839 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
841 * Determine if this phy can be assigned to this
842 * port . If the phy is not a valid PHY for
843 * this port then the function will notify the user. A PHY can only be
844 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
845 * the same port. none
847 static void scic_sds_port_general_link_up_handler(struct scic_sds_port *sci_port,
848 struct scic_sds_phy *sci_phy,
851 struct sci_sas_address port_sas_address;
852 struct sci_sas_address phy_sas_address;
854 scic_sds_port_get_attached_sas_address(sci_port, &port_sas_address);
855 scic_sds_phy_get_attached_sas_address(sci_phy, &phy_sas_address);
857 /* If the SAS address of the new phy matches the SAS address of
858 * other phys in the port OR this is the first phy in the port,
859 * then activate the phy and allow it to be used for operations
862 if ((phy_sas_address.high == port_sas_address.high &&
863 phy_sas_address.low == port_sas_address.low) ||
864 sci_port->active_phy_mask == 0) {
865 struct sci_base_state_machine *sm = &sci_port->sm;
867 scic_sds_port_activate_phy(sci_port, sci_phy, do_notify_user);
868 if (sm->current_state_id == SCI_PORT_RESETTING)
869 port_state_machine_change(sci_port, SCI_PORT_READY);
871 scic_sds_port_invalid_link_up(sci_port, sci_phy);
877 * This method returns false if the port only has a single phy object assigned.
878 * If there are no phys or more than one phy then the method will return
880 * @sci_port: The port for which the wide port condition is to be checked.
882 * bool true Is returned if this is a wide ported port. false Is returned if
883 * this is a narrow port.
885 static bool scic_sds_port_is_wide(struct scic_sds_port *sci_port)
890 for (index = 0; index < SCI_MAX_PHYS; index++) {
891 if (sci_port->phy_table[index] != NULL) {
896 return phy_count != 1;
900 * This method is called by the PHY object when the link is detected. if the
901 * port wants the PHY to continue on to the link up state then the port
902 * layer must return true. If the port object returns false the phy object
903 * must halt its attempt to go link up.
904 * @sci_port: The port associated with the phy object.
905 * @sci_phy: The phy object that is trying to go link up.
907 * true if the phy object can continue to the link up condition. true Is
908 * returned if this phy can continue to the ready state. false Is returned if
909 * can not continue on to the ready state. This notification is in place for
910 * wide ports and direct attached phys. Since there are no wide ported SATA
911 * devices this could become an invalid port configuration.
913 bool scic_sds_port_link_detected(
914 struct scic_sds_port *sci_port,
915 struct scic_sds_phy *sci_phy)
917 if ((sci_port->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
918 (sci_phy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
919 scic_sds_port_is_wide(sci_port)) {
920 scic_sds_port_invalid_link_up(sci_port, sci_phy);
928 static void port_timeout(unsigned long data)
930 struct sci_timer *tmr = (struct sci_timer *)data;
931 struct scic_sds_port *sci_port = container_of(tmr, typeof(*sci_port), timer);
932 struct isci_host *ihost = scic_to_ihost(sci_port->owning_controller);
936 spin_lock_irqsave(&ihost->scic_lock, flags);
941 current_state = sci_port->sm.current_state_id;
943 if (current_state == SCI_PORT_RESETTING) {
944 /* if the port is still in the resetting state then the timeout
945 * fired before the reset completed.
947 port_state_machine_change(sci_port, SCI_PORT_FAILED);
948 } else if (current_state == SCI_PORT_STOPPED) {
949 /* if the port is stopped then the start request failed In this
950 * case stay in the stopped state.
952 dev_err(sciport_to_dev(sci_port),
953 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
956 } else if (current_state == SCI_PORT_STOPPING) {
957 /* if the port is still stopping then the stop has not completed */
958 isci_port_stop_complete(sci_port->owning_controller,
960 SCI_FAILURE_TIMEOUT);
962 /* The port is in the ready state and we have a timer
963 * reporting a timeout this should not happen.
965 dev_err(sciport_to_dev(sci_port),
966 "%s: SCIC Port 0x%p is processing a timeout operation "
967 "in state %d.\n", __func__, sci_port, current_state);
971 spin_unlock_irqrestore(&ihost->scic_lock, flags);
974 /* --------------------------------------------------------------------------- */
977 * This function updates the hardwares VIIT entry for this port.
981 static void scic_sds_port_update_viit_entry(struct scic_sds_port *sci_port)
983 struct sci_sas_address sas_address;
985 scic_sds_port_get_sas_address(sci_port, &sas_address);
987 writel(sas_address.high,
988 &sci_port->viit_registers->initiator_sas_address_hi);
989 writel(sas_address.low,
990 &sci_port->viit_registers->initiator_sas_address_lo);
992 /* This value get cleared just in case its not already cleared */
993 writel(0, &sci_port->viit_registers->reserved);
995 /* We are required to update the status register last */
996 writel(SCU_VIIT_ENTRY_ID_VIIT |
997 SCU_VIIT_IPPT_INITIATOR |
998 ((1 << sci_port->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
999 SCU_VIIT_STATUS_ALL_VALID,
1000 &sci_port->viit_registers->status);
1004 * This method returns the maximum allowed speed for data transfers on this
1005 * port. This maximum allowed speed evaluates to the maximum speed of the
1006 * slowest phy in the port.
1007 * @sci_port: This parameter specifies the port for which to retrieve the
1008 * maximum allowed speed.
1010 * This method returns the maximum negotiated speed of the slowest phy in the
1013 enum sas_linkrate scic_sds_port_get_max_allowed_speed(
1014 struct scic_sds_port *sci_port)
1017 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1018 struct scic_sds_phy *phy = NULL;
1021 * Loop through all of the phys in this port and find the phy with the
1022 * lowest maximum link rate. */
1023 for (index = 0; index < SCI_MAX_PHYS; index++) {
1024 phy = sci_port->phy_table[index];
1027 && (scic_sds_port_active_phy(sci_port, phy) == true)
1028 && (phy->max_negotiated_speed < max_allowed_speed)
1030 max_allowed_speed = phy->max_negotiated_speed;
1033 return max_allowed_speed;
1036 static void scic_port_enable_broadcast_change_notification(struct scic_sds_port *port)
1038 struct scic_sds_phy *phy;
1042 /* Loop through all of the phys to enable BCN. */
1043 for (index = 0; index < SCI_MAX_PHYS; index++) {
1044 phy = port->phy_table[index];
1047 readl(&phy->link_layer_registers->link_layer_control);
1049 /* clear the bit by writing 1. */
1050 writel(register_value,
1051 &phy->link_layer_registers->link_layer_control);
1058 * @sci_port: This is the struct scic_sds_port object to suspend.
1060 * This method will susped the port task scheduler for this port object. none
1063 scic_sds_port_suspend_port_task_scheduler(struct scic_sds_port *port)
1065 u32 pts_control_value;
1067 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1068 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1069 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1073 * scic_sds_port_post_dummy_request() - post dummy/workaround request
1074 * @sci_port: port to post task
1076 * Prevent the hardware scheduler from posting new requests to the front
1077 * of the scheduler queue causing a starvation problem for currently
1081 static void scic_sds_port_post_dummy_request(struct scic_sds_port *sci_port)
1084 struct scu_task_context *task_context;
1085 struct scic_sds_controller *scic = sci_port->owning_controller;
1086 u16 tci = sci_port->reserved_tci;
1088 task_context = scic_sds_controller_get_task_context_buffer(scic, tci);
1090 task_context->abort = 0;
1092 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1093 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1096 scic_sds_controller_post_request(scic, command);
1100 * This routine will abort the dummy request. This will alow the hardware to
1101 * power down parts of the silicon to save power.
1103 * @sci_port: The port on which the task must be aborted.
1106 static void scic_sds_port_abort_dummy_request(struct scic_sds_port *sci_port)
1108 struct scic_sds_controller *scic = sci_port->owning_controller;
1109 u16 tci = sci_port->reserved_tci;
1110 struct scu_task_context *tc;
1113 tc = scic_sds_controller_get_task_context_buffer(scic, tci);
1117 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1118 sci_port->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1121 scic_sds_controller_post_request(scic, command);
1126 * @sci_port: This is the struct scic_sds_port object to resume.
1128 * This method will resume the port task scheduler for this port object. none
1131 scic_sds_port_resume_port_task_scheduler(struct scic_sds_port *port)
1133 u32 pts_control_value;
1135 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1136 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1137 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1140 static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
1142 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1144 scic_sds_port_suspend_port_task_scheduler(sci_port);
1146 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1148 if (sci_port->active_phy_mask != 0) {
1149 /* At least one of the phys on the port is ready */
1150 port_state_machine_change(sci_port,
1151 SCI_PORT_SUB_OPERATIONAL);
1155 static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
1158 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1159 struct scic_sds_controller *scic = sci_port->owning_controller;
1160 struct isci_host *ihost = scic_to_ihost(scic);
1161 struct isci_port *iport = sci_port_to_iport(sci_port);
1163 isci_port_ready(ihost, iport);
1165 for (index = 0; index < SCI_MAX_PHYS; index++) {
1166 if (sci_port->phy_table[index]) {
1167 writel(sci_port->physical_port_index,
1168 &sci_port->port_pe_configuration_register[
1169 sci_port->phy_table[index]->phy_index]);
1173 scic_sds_port_update_viit_entry(sci_port);
1175 scic_sds_port_resume_port_task_scheduler(sci_port);
1178 * Post the dummy task for the port so the hardware can schedule
1181 scic_sds_port_post_dummy_request(sci_port);
1184 static void scic_sds_port_invalidate_dummy_remote_node(struct scic_sds_port *sci_port)
1186 struct scic_sds_controller *scic = sci_port->owning_controller;
1187 u8 phys_index = sci_port->physical_port_index;
1188 union scu_remote_node_context *rnc;
1189 u16 rni = sci_port->reserved_rni;
1192 rnc = &scic->remote_node_context_table[rni];
1194 rnc->ssp.is_valid = false;
1196 /* ensure the preceding tc abort request has reached the
1197 * controller and give it ample time to act before posting the rnc
1200 readl(&scic->smu_registers->interrupt_status); /* flush */
1203 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1204 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1206 scic_sds_controller_post_request(scic, command);
1211 * @object: This is the object which is cast to a struct scic_sds_port object.
1213 * This method will perform the actions required by the struct scic_sds_port on
1214 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
1215 * the port not ready and suspends the port task scheduler. none
1217 static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
1219 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1220 struct scic_sds_controller *scic = sci_port->owning_controller;
1221 struct isci_host *ihost = scic_to_ihost(scic);
1222 struct isci_port *iport = sci_port_to_iport(sci_port);
1225 * Kill the dummy task for this port if it has not yet posted
1226 * the hardware will treat this as a NOP and just return abort
1229 scic_sds_port_abort_dummy_request(sci_port);
1231 isci_port_not_ready(ihost, iport);
1233 if (sci_port->ready_exit)
1234 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1237 static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
1239 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1240 struct scic_sds_controller *scic = sci_port->owning_controller;
1241 struct isci_host *ihost = scic_to_ihost(scic);
1242 struct isci_port *iport = sci_port_to_iport(sci_port);
1244 if (sci_port->active_phy_mask == 0) {
1245 isci_port_not_ready(ihost, iport);
1247 port_state_machine_change(sci_port,
1248 SCI_PORT_SUB_WAITING);
1249 } else if (sci_port->started_request_count == 0)
1250 port_state_machine_change(sci_port,
1251 SCI_PORT_SUB_OPERATIONAL);
1254 static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
1256 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1258 scic_sds_port_suspend_port_task_scheduler(sci_port);
1259 if (sci_port->ready_exit)
1260 scic_sds_port_invalidate_dummy_remote_node(sci_port);
1263 enum sci_status scic_sds_port_start(struct scic_sds_port *sci_port)
1265 struct scic_sds_controller *scic = sci_port->owning_controller;
1266 enum sci_status status = SCI_SUCCESS;
1267 enum scic_sds_port_states state;
1270 state = sci_port->sm.current_state_id;
1271 if (state != SCI_PORT_STOPPED) {
1272 dev_warn(sciport_to_dev(sci_port),
1273 "%s: in wrong state: %d\n", __func__, state);
1274 return SCI_FAILURE_INVALID_STATE;
1277 if (sci_port->assigned_device_count > 0) {
1278 /* TODO This is a start failure operation because
1279 * there are still devices assigned to this port.
1280 * There must be no devices assigned to a port on a
1283 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1286 if (sci_port->reserved_rni == SCU_DUMMY_INDEX) {
1287 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1288 &scic->available_remote_nodes, 1);
1290 if (rni != SCU_DUMMY_INDEX)
1291 scic_sds_port_construct_dummy_rnc(sci_port, rni);
1293 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1294 sci_port->reserved_rni = rni;
1297 if (sci_port->reserved_tci == SCU_DUMMY_INDEX) {
1298 /* Allocate a TCI and remove the sequence nibble */
1299 u16 tci = scic_controller_allocate_io_tag(scic);
1301 if (tci != SCU_DUMMY_INDEX)
1302 scic_sds_port_construct_dummy_task(sci_port, tci);
1304 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1305 sci_port->reserved_tci = tci;
1308 if (status == SCI_SUCCESS) {
1309 phy_mask = scic_sds_port_get_phys(sci_port);
1312 * There are one or more phys assigned to this port. Make sure
1313 * the port's phy mask is in fact legal and supported by the
1316 if (scic_sds_port_is_phy_mask_valid(sci_port, phy_mask) == true) {
1317 port_state_machine_change(sci_port,
1322 status = SCI_FAILURE;
1325 if (status != SCI_SUCCESS)
1326 scic_sds_port_destroy_dummy_resources(sci_port);
1331 enum sci_status scic_sds_port_stop(struct scic_sds_port *sci_port)
1333 enum scic_sds_port_states state;
1335 state = sci_port->sm.current_state_id;
1337 case SCI_PORT_STOPPED:
1339 case SCI_PORT_SUB_WAITING:
1340 case SCI_PORT_SUB_OPERATIONAL:
1341 case SCI_PORT_SUB_CONFIGURING:
1342 case SCI_PORT_RESETTING:
1343 port_state_machine_change(sci_port,
1347 dev_warn(sciport_to_dev(sci_port),
1348 "%s: in wrong state: %d\n", __func__, state);
1349 return SCI_FAILURE_INVALID_STATE;
1353 static enum sci_status scic_port_hard_reset(struct scic_sds_port *sci_port, u32 timeout)
1355 enum sci_status status = SCI_FAILURE_INVALID_PHY;
1356 struct scic_sds_phy *selected_phy = NULL;
1357 enum scic_sds_port_states state;
1360 state = sci_port->sm.current_state_id;
1361 if (state != SCI_PORT_SUB_OPERATIONAL) {
1362 dev_warn(sciport_to_dev(sci_port),
1363 "%s: in wrong state: %d\n", __func__, state);
1364 return SCI_FAILURE_INVALID_STATE;
1367 /* Select a phy on which we can send the hard reset request. */
1368 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !selected_phy; phy_index++) {
1369 selected_phy = sci_port->phy_table[phy_index];
1371 !scic_sds_port_active_phy(sci_port, selected_phy)) {
1373 * We found a phy but it is not ready select
1376 selected_phy = NULL;
1380 /* If we have a phy then go ahead and start the reset procedure */
1383 status = scic_sds_phy_reset(selected_phy);
1385 if (status != SCI_SUCCESS)
1388 sci_mod_timer(&sci_port->timer, timeout);
1389 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1391 port_state_machine_change(sci_port,
1392 SCI_PORT_RESETTING);
1397 * scic_sds_port_add_phy() -
1398 * @sci_port: This parameter specifies the port in which the phy will be added.
1399 * @sci_phy: This parameter is the phy which is to be added to the port.
1401 * This method will add a PHY to the selected port. This method returns an
1402 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1403 * status is a failure to add the phy to the port.
1405 enum sci_status scic_sds_port_add_phy(struct scic_sds_port *sci_port,
1406 struct scic_sds_phy *sci_phy)
1408 enum sci_status status;
1409 enum scic_sds_port_states state;
1411 state = sci_port->sm.current_state_id;
1413 case SCI_PORT_STOPPED: {
1414 struct sci_sas_address port_sas_address;
1416 /* Read the port assigned SAS Address if there is one */
1417 scic_sds_port_get_sas_address(sci_port, &port_sas_address);
1419 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1420 struct sci_sas_address phy_sas_address;
1422 /* Make sure that the PHY SAS Address matches the SAS Address
1425 scic_sds_phy_get_sas_address(sci_phy, &phy_sas_address);
1427 if (port_sas_address.high != phy_sas_address.high ||
1428 port_sas_address.low != phy_sas_address.low)
1429 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1431 return scic_sds_port_set_phy(sci_port, sci_phy);
1433 case SCI_PORT_SUB_WAITING:
1434 case SCI_PORT_SUB_OPERATIONAL:
1435 status = scic_sds_port_set_phy(sci_port, sci_phy);
1437 if (status != SCI_SUCCESS)
1440 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1441 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1442 port_state_machine_change(sci_port, SCI_PORT_SUB_CONFIGURING);
1445 case SCI_PORT_SUB_CONFIGURING:
1446 status = scic_sds_port_set_phy(sci_port, sci_phy);
1448 if (status != SCI_SUCCESS)
1450 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1452 /* Re-enter the configuring state since this may be the last phy in
1455 port_state_machine_change(sci_port,
1456 SCI_PORT_SUB_CONFIGURING);
1459 dev_warn(sciport_to_dev(sci_port),
1460 "%s: in wrong state: %d\n", __func__, state);
1461 return SCI_FAILURE_INVALID_STATE;
1466 * scic_sds_port_remove_phy() -
1467 * @sci_port: This parameter specifies the port in which the phy will be added.
1468 * @sci_phy: This parameter is the phy which is to be added to the port.
1470 * This method will remove the PHY from the selected PORT. This method returns
1471 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1472 * other status is a failure to add the phy to the port.
1474 enum sci_status scic_sds_port_remove_phy(struct scic_sds_port *sci_port,
1475 struct scic_sds_phy *sci_phy)
1477 enum sci_status status;
1478 enum scic_sds_port_states state;
1480 state = sci_port->sm.current_state_id;
1483 case SCI_PORT_STOPPED:
1484 return scic_sds_port_clear_phy(sci_port, sci_phy);
1485 case SCI_PORT_SUB_OPERATIONAL:
1486 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1487 if (status != SCI_SUCCESS)
1490 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1491 sci_port->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1492 port_state_machine_change(sci_port,
1493 SCI_PORT_SUB_CONFIGURING);
1495 case SCI_PORT_SUB_CONFIGURING:
1496 status = scic_sds_port_clear_phy(sci_port, sci_phy);
1498 if (status != SCI_SUCCESS)
1500 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1502 /* Re-enter the configuring state since this may be the last phy in
1505 port_state_machine_change(sci_port,
1506 SCI_PORT_SUB_CONFIGURING);
1509 dev_warn(sciport_to_dev(sci_port),
1510 "%s: in wrong state: %d\n", __func__, state);
1511 return SCI_FAILURE_INVALID_STATE;
1515 enum sci_status scic_sds_port_link_up(struct scic_sds_port *sci_port,
1516 struct scic_sds_phy *sci_phy)
1518 enum scic_sds_port_states state;
1520 state = sci_port->sm.current_state_id;
1522 case SCI_PORT_SUB_WAITING:
1523 /* Since this is the first phy going link up for the port we
1524 * can just enable it and continue
1526 scic_sds_port_activate_phy(sci_port, sci_phy, true);
1528 port_state_machine_change(sci_port,
1529 SCI_PORT_SUB_OPERATIONAL);
1531 case SCI_PORT_SUB_OPERATIONAL:
1532 scic_sds_port_general_link_up_handler(sci_port, sci_phy, true);
1534 case SCI_PORT_RESETTING:
1535 /* TODO We should make sure that the phy that has gone
1536 * link up is the same one on which we sent the reset. It is
1537 * possible that the phy on which we sent the reset is not the
1538 * one that has gone link up and we want to make sure that
1539 * phy being reset comes back. Consider the case where a
1540 * reset is sent but before the hardware processes the reset it
1541 * get a link up on the port because of a hot plug event.
1542 * because of the reset request this phy will go link down
1543 * almost immediately.
1546 /* In the resetting state we don't notify the user regarding
1547 * link up and link down notifications.
1549 scic_sds_port_general_link_up_handler(sci_port, sci_phy, false);
1552 dev_warn(sciport_to_dev(sci_port),
1553 "%s: in wrong state: %d\n", __func__, state);
1554 return SCI_FAILURE_INVALID_STATE;
1558 enum sci_status scic_sds_port_link_down(struct scic_sds_port *sci_port,
1559 struct scic_sds_phy *sci_phy)
1561 enum scic_sds_port_states state;
1563 state = sci_port->sm.current_state_id;
1565 case SCI_PORT_SUB_OPERATIONAL:
1566 scic_sds_port_deactivate_phy(sci_port, sci_phy, true);
1568 /* If there are no active phys left in the port, then
1569 * transition the port to the WAITING state until such time
1570 * as a phy goes link up
1572 if (sci_port->active_phy_mask == 0)
1573 port_state_machine_change(sci_port,
1574 SCI_PORT_SUB_WAITING);
1576 case SCI_PORT_RESETTING:
1577 /* In the resetting state we don't notify the user regarding
1578 * link up and link down notifications. */
1579 scic_sds_port_deactivate_phy(sci_port, sci_phy, false);
1582 dev_warn(sciport_to_dev(sci_port),
1583 "%s: in wrong state: %d\n", __func__, state);
1584 return SCI_FAILURE_INVALID_STATE;
1588 enum sci_status scic_sds_port_start_io(struct scic_sds_port *sci_port,
1589 struct scic_sds_remote_device *sci_dev,
1590 struct scic_sds_request *sci_req)
1592 enum scic_sds_port_states state;
1594 state = sci_port->sm.current_state_id;
1596 case SCI_PORT_SUB_WAITING:
1597 return SCI_FAILURE_INVALID_STATE;
1598 case SCI_PORT_SUB_OPERATIONAL:
1599 sci_port->started_request_count++;
1602 dev_warn(sciport_to_dev(sci_port),
1603 "%s: in wrong state: %d\n", __func__, state);
1604 return SCI_FAILURE_INVALID_STATE;
1608 enum sci_status scic_sds_port_complete_io(struct scic_sds_port *sci_port,
1609 struct scic_sds_remote_device *sci_dev,
1610 struct scic_sds_request *sci_req)
1612 enum scic_sds_port_states state;
1614 state = sci_port->sm.current_state_id;
1616 case SCI_PORT_STOPPED:
1617 dev_warn(sciport_to_dev(sci_port),
1618 "%s: in wrong state: %d\n", __func__, state);
1619 return SCI_FAILURE_INVALID_STATE;
1620 case SCI_PORT_STOPPING:
1621 scic_sds_port_decrement_request_count(sci_port);
1623 if (sci_port->started_request_count == 0)
1624 port_state_machine_change(sci_port,
1627 case SCI_PORT_READY:
1628 case SCI_PORT_RESETTING:
1629 case SCI_PORT_FAILED:
1630 case SCI_PORT_SUB_WAITING:
1631 case SCI_PORT_SUB_OPERATIONAL:
1632 scic_sds_port_decrement_request_count(sci_port);
1634 case SCI_PORT_SUB_CONFIGURING:
1635 scic_sds_port_decrement_request_count(sci_port);
1636 if (sci_port->started_request_count == 0) {
1637 port_state_machine_change(sci_port,
1638 SCI_PORT_SUB_OPERATIONAL);
1647 * @sci_port: This is the port object which to suspend.
1649 * This method will enable the SCU Port Task Scheduler for this port object but
1650 * will leave the port task scheduler in a suspended state. none
1653 scic_sds_port_enable_port_task_scheduler(struct scic_sds_port *port)
1655 u32 pts_control_value;
1657 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1658 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1659 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1664 * @sci_port: This is the port object which to resume.
1666 * This method will disable the SCU port task scheduler for this port object.
1670 scic_sds_port_disable_port_task_scheduler(struct scic_sds_port *port)
1672 u32 pts_control_value;
1674 pts_control_value = readl(&port->port_task_scheduler_registers->control);
1675 pts_control_value &=
1676 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1677 writel(pts_control_value, &port->port_task_scheduler_registers->control);
1680 static void scic_sds_port_post_dummy_remote_node(struct scic_sds_port *sci_port)
1682 struct scic_sds_controller *scic = sci_port->owning_controller;
1683 u8 phys_index = sci_port->physical_port_index;
1684 union scu_remote_node_context *rnc;
1685 u16 rni = sci_port->reserved_rni;
1688 rnc = &scic->remote_node_context_table[rni];
1689 rnc->ssp.is_valid = true;
1691 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1692 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1694 scic_sds_controller_post_request(scic, command);
1696 /* ensure hardware has seen the post rnc command and give it
1697 * ample time to act before sending the suspend
1699 readl(&scic->smu_registers->interrupt_status); /* flush */
1702 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1703 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1705 scic_sds_controller_post_request(scic, command);
1708 static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
1710 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1712 if (sci_port->sm.previous_state_id == SCI_PORT_STOPPING) {
1714 * If we enter this state becasuse of a request to stop
1715 * the port then we want to disable the hardwares port
1716 * task scheduler. */
1717 scic_sds_port_disable_port_task_scheduler(sci_port);
1721 static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
1723 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1725 /* Enable and suspend the port task scheduler */
1726 scic_sds_port_enable_port_task_scheduler(sci_port);
1729 static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
1731 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1732 struct scic_sds_controller *scic = sci_port->owning_controller;
1733 struct isci_host *ihost = scic_to_ihost(scic);
1734 struct isci_port *iport = sci_port_to_iport(sci_port);
1737 prev_state = sci_port->sm.previous_state_id;
1738 if (prev_state == SCI_PORT_RESETTING)
1739 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1741 isci_port_not_ready(ihost, iport);
1743 /* Post and suspend the dummy remote node context for this port. */
1744 scic_sds_port_post_dummy_remote_node(sci_port);
1746 /* Start the ready substate machine */
1747 port_state_machine_change(sci_port,
1748 SCI_PORT_SUB_WAITING);
1751 static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
1753 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1755 sci_del_timer(&sci_port->timer);
1758 static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
1760 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1762 sci_del_timer(&sci_port->timer);
1764 scic_sds_port_destroy_dummy_resources(sci_port);
1767 static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
1769 struct scic_sds_port *sci_port = container_of(sm, typeof(*sci_port), sm);
1770 struct isci_port *iport = sci_port_to_iport(sci_port);
1772 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1775 /* --------------------------------------------------------------------------- */
1777 static const struct sci_base_state scic_sds_port_state_table[] = {
1778 [SCI_PORT_STOPPED] = {
1779 .enter_state = scic_sds_port_stopped_state_enter,
1780 .exit_state = scic_sds_port_stopped_state_exit
1782 [SCI_PORT_STOPPING] = {
1783 .exit_state = scic_sds_port_stopping_state_exit
1785 [SCI_PORT_READY] = {
1786 .enter_state = scic_sds_port_ready_state_enter,
1788 [SCI_PORT_SUB_WAITING] = {
1789 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1791 [SCI_PORT_SUB_OPERATIONAL] = {
1792 .enter_state = scic_sds_port_ready_substate_operational_enter,
1793 .exit_state = scic_sds_port_ready_substate_operational_exit
1795 [SCI_PORT_SUB_CONFIGURING] = {
1796 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1797 .exit_state = scic_sds_port_ready_substate_configuring_exit
1799 [SCI_PORT_RESETTING] = {
1800 .exit_state = scic_sds_port_resetting_state_exit
1802 [SCI_PORT_FAILED] = {
1803 .enter_state = scic_sds_port_failed_state_enter,
1807 void scic_sds_port_construct(struct scic_sds_port *sci_port, u8 index,
1808 struct scic_sds_controller *scic)
1810 sci_base_state_machine_construct(&sci_port->sm,
1811 scic_sds_port_state_table,
1814 sci_base_state_machine_start(&sci_port->sm);
1816 sci_port->logical_port_index = SCIC_SDS_DUMMY_PORT;
1817 sci_port->physical_port_index = index;
1818 sci_port->active_phy_mask = 0;
1819 sci_port->ready_exit = false;
1821 sci_port->owning_controller = scic;
1823 sci_port->started_request_count = 0;
1824 sci_port->assigned_device_count = 0;
1826 sci_port->reserved_rni = SCU_DUMMY_INDEX;
1827 sci_port->reserved_tci = SCU_DUMMY_INDEX;
1829 sci_init_timer(&sci_port->timer, port_timeout);
1831 sci_port->port_task_scheduler_registers = NULL;
1833 for (index = 0; index < SCI_MAX_PHYS; index++)
1834 sci_port->phy_table[index] = NULL;
1837 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1839 INIT_LIST_HEAD(&iport->remote_dev_list);
1840 INIT_LIST_HEAD(&iport->domain_dev_list);
1841 spin_lock_init(&iport->state_lock);
1842 init_completion(&iport->start_complete);
1843 iport->isci_host = ihost;
1844 isci_port_change_state(iport, isci_freed);
1848 * isci_port_get_state() - This function gets the status of the port object.
1849 * @isci_port: This parameter points to the isci_port object
1851 * status of the object as a isci_status enum.
1853 enum isci_status isci_port_get_state(
1854 struct isci_port *isci_port)
1856 return isci_port->status;
1859 static void isci_port_bc_change_received(struct isci_host *ihost,
1860 struct scic_sds_port *sci_port,
1861 struct scic_sds_phy *sci_phy)
1863 struct isci_phy *iphy = sci_phy_to_iphy(sci_phy);
1865 dev_dbg(&ihost->pdev->dev, "%s: iphy = %p, sas_phy = %p\n",
1866 __func__, iphy, &iphy->sas_phy);
1868 ihost->sas_ha.notify_port_event(&iphy->sas_phy, PORTE_BROADCAST_RCVD);
1869 scic_port_enable_broadcast_change_notification(sci_port);
1872 void scic_sds_port_broadcast_change_received(
1873 struct scic_sds_port *sci_port,
1874 struct scic_sds_phy *sci_phy)
1876 struct scic_sds_controller *scic = sci_port->owning_controller;
1877 struct isci_host *ihost = scic_to_ihost(scic);
1879 /* notify the user. */
1880 isci_port_bc_change_received(ihost, sci_port, sci_phy);
1883 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1884 struct isci_phy *iphy)
1886 unsigned long flags;
1887 enum sci_status status;
1888 int ret = TMF_RESP_FUNC_COMPLETE;
1890 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1893 init_completion(&iport->hard_reset_complete);
1895 spin_lock_irqsave(&ihost->scic_lock, flags);
1897 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1898 status = scic_port_hard_reset(&iport->sci, ISCI_PORT_RESET_TIMEOUT);
1900 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1902 if (status == SCI_SUCCESS) {
1903 wait_for_completion(&iport->hard_reset_complete);
1905 dev_dbg(&ihost->pdev->dev,
1906 "%s: iport = %p; hard reset completion\n",
1909 if (iport->hard_reset_status != SCI_SUCCESS)
1910 ret = TMF_RESP_FUNC_FAILED;
1912 ret = TMF_RESP_FUNC_FAILED;
1914 dev_err(&ihost->pdev->dev,
1915 "%s: iport = %p; scic_port_hard_reset call"
1917 __func__, iport, status);
1921 /* If the hard reset for the port has failed, consider this
1922 * the same as link failures on all phys in the port.
1924 if (ret != TMF_RESP_FUNC_COMPLETE) {
1925 dev_err(&ihost->pdev->dev,
1926 "%s: iport = %p; hard reset failed "
1927 "(0x%x) - sending link down to libsas for phy %p\n",
1928 __func__, iport, iport->hard_reset_status, iphy);
1930 isci_port_link_down(ihost, iphy, iport);
1937 * isci_port_deformed() - This function is called by libsas when a port becomes
1939 * @phy: This parameter specifies the libsas phy with the inactive port.
1942 void isci_port_deformed(struct asd_sas_phy *phy)
1944 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1948 * isci_port_formed() - This function is called by libsas when a port becomes
1950 * @phy: This parameter specifies the libsas phy with the active port.
1953 void isci_port_formed(struct asd_sas_phy *phy)
1955 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);