isci: unify isci_port and scic_sds_port
[linux-2.6/x86.git] / drivers / scsi / isci / port.c
blob04591882ee7773323eb65389b4c33cb460a23401
1 /*
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.
5 * GPL LICENSE SUMMARY
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.
24 * BSD LICENSE
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
31 * are met:
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
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 #include "isci.h"
57 #include "port.h"
58 #include "request.h"
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)
65 unsigned long flags;
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.
85 static void
86 scic_sds_port_get_protocols(struct isci_port *iport,
87 struct scic_phy_proto *protocols)
89 u8 index;
91 protocols->all = 0;
93 for (index = 0; index < SCI_MAX_PHYS; index++) {
94 if (iport->phy_table[index] != NULL) {
95 scic_sds_phy_get_protocols(iport->phy_table[index],
96 protocols);
102 * This method requests a list (mask) of the phys contained in the supplied SAS
103 * port.
104 * @sci_port: a handle corresponding to the SAS port for which to return the
105 * phy mask.
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 isci_port *iport)
112 u32 index;
113 u32 mask;
115 mask = 0;
117 for (index = 0; index < SCI_MAX_PHYS; index++) {
118 if (iport->phy_table[index] != NULL) {
119 mask |= (1 << index);
123 return mask;
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
130 * index.
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 isci_port *iport,
140 struct scic_port_properties *prop)
142 if (!iport || iport->logical_port_index == SCIC_SDS_DUMMY_PORT)
143 return SCI_FAILURE_INVALID_PORT;
145 prop->index = iport->logical_port_index;
146 prop->phy_mask = scic_sds_port_get_phys(iport);
147 scic_sds_port_get_sas_address(iport, &prop->local.sas_address);
148 scic_sds_port_get_protocols(iport, &prop->local.protocols);
149 scic_sds_port_get_attached_sas_address(iport, &prop->remote.sas_address);
151 return SCI_SUCCESS;
154 static void scic_port_bcn_enable(struct isci_port *iport)
156 struct isci_phy *iphy;
157 u32 val;
158 int i;
160 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
161 iphy = iport->phy_table[i];
162 if (!iphy)
163 continue;
164 val = readl(&iphy->link_layer_registers->link_layer_control);
165 /* clear the bit by writing 1. */
166 writel(val, &iphy->link_layer_registers->link_layer_control);
170 /* called under scic_lock to stabilize phy:port associations */
171 void isci_port_bcn_enable(struct isci_host *ihost, struct isci_port *iport)
173 int i;
175 clear_bit(IPORT_BCN_BLOCKED, &iport->flags);
176 wake_up(&ihost->eventq);
178 if (!test_and_clear_bit(IPORT_BCN_PENDING, &iport->flags))
179 return;
181 for (i = 0; i < ARRAY_SIZE(iport->phy_table); i++) {
182 struct isci_phy *iphy = iport->phy_table[i];
184 if (!iphy)
185 continue;
187 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
188 PORTE_BROADCAST_RCVD);
189 break;
193 static void isci_port_bc_change_received(struct isci_host *ihost,
194 struct isci_port *iport,
195 struct isci_phy *iphy)
197 if (iport && test_bit(IPORT_BCN_BLOCKED, &iport->flags)) {
198 dev_dbg(&ihost->pdev->dev,
199 "%s: disabled BCN; isci_phy = %p, sas_phy = %p\n",
200 __func__, iphy, &iphy->sas_phy);
201 set_bit(IPORT_BCN_PENDING, &iport->flags);
202 atomic_inc(&iport->event);
203 wake_up(&ihost->eventq);
204 } else {
205 dev_dbg(&ihost->pdev->dev,
206 "%s: isci_phy = %p, sas_phy = %p\n",
207 __func__, iphy, &iphy->sas_phy);
209 ihost->sas_ha.notify_port_event(&iphy->sas_phy,
210 PORTE_BROADCAST_RCVD);
212 scic_port_bcn_enable(iport);
215 static void isci_port_link_up(struct isci_host *isci_host,
216 struct isci_port *iport,
217 struct isci_phy *iphy)
219 unsigned long flags;
220 struct scic_port_properties properties;
221 unsigned long success = true;
223 BUG_ON(iphy->isci_port != NULL);
225 iphy->isci_port = iport;
227 dev_dbg(&isci_host->pdev->dev,
228 "%s: isci_port = %p\n",
229 __func__, iport);
231 spin_lock_irqsave(&iphy->sas_phy.frame_rcvd_lock, flags);
233 isci_port_change_state(iphy->isci_port, isci_starting);
235 scic_port_get_properties(iport, &properties);
237 if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) {
238 u64 attached_sas_address;
240 iphy->sas_phy.oob_mode = SATA_OOB_MODE;
241 iphy->sas_phy.frame_rcvd_size = sizeof(struct dev_to_host_fis);
244 * For direct-attached SATA devices, the SCI core will
245 * automagically assign a SAS address to the end device
246 * for the purpose of creating a port. This SAS address
247 * will not be the same as assigned to the PHY and needs
248 * to be obtained from struct scic_port_properties properties.
250 attached_sas_address = properties.remote.sas_address.high;
251 attached_sas_address <<= 32;
252 attached_sas_address |= properties.remote.sas_address.low;
253 swab64s(&attached_sas_address);
255 memcpy(&iphy->sas_phy.attached_sas_addr,
256 &attached_sas_address, sizeof(attached_sas_address));
257 } else if (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SAS) {
258 iphy->sas_phy.oob_mode = SAS_OOB_MODE;
259 iphy->sas_phy.frame_rcvd_size = sizeof(struct sas_identify_frame);
261 /* Copy the attached SAS address from the IAF */
262 memcpy(iphy->sas_phy.attached_sas_addr,
263 iphy->frame_rcvd.iaf.sas_addr, SAS_ADDR_SIZE);
264 } else {
265 dev_err(&isci_host->pdev->dev, "%s: unkown target\n", __func__);
266 success = false;
269 iphy->sas_phy.phy->negotiated_linkrate = sci_phy_linkrate(iphy);
271 spin_unlock_irqrestore(&iphy->sas_phy.frame_rcvd_lock, flags);
273 /* Notify libsas that we have an address frame, if indeed
274 * we've found an SSP, SMP, or STP target */
275 if (success)
276 isci_host->sas_ha.notify_port_event(&iphy->sas_phy,
277 PORTE_BYTES_DMAED);
282 * isci_port_link_down() - This function is called by the sci core when a link
283 * becomes inactive.
284 * @isci_host: This parameter specifies the isci host object.
285 * @phy: This parameter specifies the isci phy with the active link.
286 * @port: This parameter specifies the isci port with the active link.
289 static void isci_port_link_down(struct isci_host *isci_host,
290 struct isci_phy *isci_phy,
291 struct isci_port *isci_port)
293 struct isci_remote_device *isci_device;
295 dev_dbg(&isci_host->pdev->dev,
296 "%s: isci_port = %p\n", __func__, isci_port);
298 if (isci_port) {
300 /* check to see if this is the last phy on this port. */
301 if (isci_phy->sas_phy.port &&
302 isci_phy->sas_phy.port->num_phys == 1) {
303 atomic_inc(&isci_port->event);
304 isci_port_bcn_enable(isci_host, isci_port);
306 /* change the state for all devices on this port. The
307 * next task sent to this device will be returned as
308 * SAS_TASK_UNDELIVERED, and the scsi mid layer will
309 * remove the target
311 list_for_each_entry(isci_device,
312 &isci_port->remote_dev_list,
313 node) {
314 dev_dbg(&isci_host->pdev->dev,
315 "%s: isci_device = %p\n",
316 __func__, isci_device);
317 set_bit(IDEV_GONE, &isci_device->flags);
320 isci_port_change_state(isci_port, isci_stopping);
323 /* Notify libsas of the borken link, this will trigger calls to our
324 * isci_port_deformed and isci_dev_gone functions.
326 sas_phy_disconnected(&isci_phy->sas_phy);
327 isci_host->sas_ha.notify_phy_event(&isci_phy->sas_phy,
328 PHYE_LOSS_OF_SIGNAL);
330 isci_phy->isci_port = NULL;
332 dev_dbg(&isci_host->pdev->dev,
333 "%s: isci_port = %p - Done\n", __func__, isci_port);
338 * isci_port_ready() - This function is called by the sci core when a link
339 * becomes ready.
340 * @isci_host: This parameter specifies the isci host object.
341 * @port: This parameter specifies the sci port with the active link.
344 static void isci_port_ready(struct isci_host *isci_host, struct isci_port *isci_port)
346 dev_dbg(&isci_host->pdev->dev,
347 "%s: isci_port = %p\n", __func__, isci_port);
349 complete_all(&isci_port->start_complete);
350 isci_port_change_state(isci_port, isci_ready);
351 return;
355 * isci_port_not_ready() - This function is called by the sci core when a link
356 * is not ready. All remote devices on this link will be removed if they are
357 * in the stopping state.
358 * @isci_host: This parameter specifies the isci host object.
359 * @port: This parameter specifies the sci port with the active link.
362 static void isci_port_not_ready(struct isci_host *isci_host, struct isci_port *isci_port)
364 dev_dbg(&isci_host->pdev->dev,
365 "%s: isci_port = %p\n", __func__, isci_port);
368 static void isci_port_stop_complete(struct scic_sds_controller *scic,
369 struct isci_port *iport,
370 enum sci_status completion_status)
372 dev_dbg(&scic_to_ihost(scic)->pdev->dev, "Port stop complete\n");
376 * isci_port_hard_reset_complete() - This function is called by the sci core
377 * when the hard reset complete notification has been received.
378 * @port: This parameter specifies the sci port with the active link.
379 * @completion_status: This parameter specifies the core status for the reset
380 * process.
383 static void isci_port_hard_reset_complete(struct isci_port *isci_port,
384 enum sci_status completion_status)
386 dev_dbg(&isci_port->isci_host->pdev->dev,
387 "%s: isci_port = %p, completion_status=%x\n",
388 __func__, isci_port, completion_status);
390 /* Save the status of the hard reset from the port. */
391 isci_port->hard_reset_status = completion_status;
393 complete_all(&isci_port->hard_reset_complete);
396 /* This method will return a true value if the specified phy can be assigned to
397 * this port The following is a list of phys for each port that are allowed: -
398 * Port 0 - 3 2 1 0 - Port 1 - 1 - Port 2 - 3 2 - Port 3 - 3 This method
399 * doesn't preclude all configurations. It merely ensures that a phy is part
400 * of the allowable set of phy identifiers for that port. For example, one
401 * could assign phy 3 to port 0 and no other phys. Please refer to
402 * scic_sds_port_is_phy_mask_valid() for information regarding whether the
403 * phy_mask for a port can be supported. bool true if this is a valid phy
404 * assignment for the port false if this is not a valid phy assignment for the
405 * port
407 bool scic_sds_port_is_valid_phy_assignment(struct isci_port *iport,
408 u32 phy_index)
410 /* Initialize to invalid value. */
411 u32 existing_phy_index = SCI_MAX_PHYS;
412 u32 index;
414 if ((iport->physical_port_index == 1) && (phy_index != 1)) {
415 return false;
418 if (iport->physical_port_index == 3 && phy_index != 3) {
419 return false;
422 if (
423 (iport->physical_port_index == 2)
424 && ((phy_index == 0) || (phy_index == 1))
426 return false;
429 for (index = 0; index < SCI_MAX_PHYS; index++) {
430 if ((iport->phy_table[index] != NULL)
431 && (index != phy_index)) {
432 existing_phy_index = index;
437 * Ensure that all of the phys in the port are capable of
438 * operating at the same maximum link rate. */
439 if (
440 (existing_phy_index < SCI_MAX_PHYS)
441 && (iport->owning_controller->user_parameters.sds1.phys[
442 phy_index].max_speed_generation !=
443 iport->owning_controller->user_parameters.sds1.phys[
444 existing_phy_index].max_speed_generation)
446 return false;
448 return true;
453 * @sci_port: This is the port object for which to determine if the phy mask
454 * can be supported.
456 * This method will return a true value if the port's phy mask can be supported
457 * by the SCU. The following is a list of valid PHY mask configurations for
458 * each port: - Port 0 - [[3 2] 1] 0 - Port 1 - [1] - Port 2 - [[3] 2]
459 * - Port 3 - [3] This method returns a boolean indication specifying if the
460 * phy mask can be supported. true if this is a valid phy assignment for the
461 * port false if this is not a valid phy assignment for the port
463 static bool scic_sds_port_is_phy_mask_valid(
464 struct isci_port *iport,
465 u32 phy_mask)
467 if (iport->physical_port_index == 0) {
468 if (((phy_mask & 0x0F) == 0x0F)
469 || ((phy_mask & 0x03) == 0x03)
470 || ((phy_mask & 0x01) == 0x01)
471 || (phy_mask == 0))
472 return true;
473 } else if (iport->physical_port_index == 1) {
474 if (((phy_mask & 0x02) == 0x02)
475 || (phy_mask == 0))
476 return true;
477 } else if (iport->physical_port_index == 2) {
478 if (((phy_mask & 0x0C) == 0x0C)
479 || ((phy_mask & 0x04) == 0x04)
480 || (phy_mask == 0))
481 return true;
482 } else if (iport->physical_port_index == 3) {
483 if (((phy_mask & 0x08) == 0x08)
484 || (phy_mask == 0))
485 return true;
488 return false;
492 * This method retrieves a currently active (i.e. connected) phy contained in
493 * the port. Currently, the lowest order phy that is connected is returned.
494 * This method returns a pointer to a SCIS_SDS_PHY object. NULL This value is
495 * returned if there are no currently active (i.e. connected to a remote end
496 * point) phys contained in the port. All other values specify a struct scic_sds_phy
497 * object that is active in the port.
499 static struct isci_phy *scic_sds_port_get_a_connected_phy(struct isci_port *iport)
501 u32 index;
502 struct isci_phy *iphy;
504 for (index = 0; index < SCI_MAX_PHYS; index++) {
505 /* Ensure that the phy is both part of the port and currently
506 * connected to the remote end-point.
508 iphy = iport->phy_table[index];
509 if (iphy && scic_sds_port_active_phy(iport, iphy))
510 return iphy;
513 return NULL;
516 static enum sci_status scic_sds_port_set_phy(struct isci_port *iport, struct isci_phy *iphy)
518 /* Check to see if we can add this phy to a port
519 * that means that the phy is not part of a port and that the port does
520 * not already have a phy assinged to the phy index.
522 if (!iport->phy_table[iphy->phy_index] &&
523 !phy_get_non_dummy_port(iphy) &&
524 scic_sds_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
525 /* Phy is being added in the stopped state so we are in MPC mode
526 * make logical port index = physical port index
528 iport->logical_port_index = iport->physical_port_index;
529 iport->phy_table[iphy->phy_index] = iphy;
530 scic_sds_phy_set_port(iphy, iport);
532 return SCI_SUCCESS;
535 return SCI_FAILURE;
538 static enum sci_status scic_sds_port_clear_phy(struct isci_port *iport,
539 struct isci_phy *iphy)
541 /* Make sure that this phy is part of this port */
542 if (iport->phy_table[iphy->phy_index] == iphy &&
543 phy_get_non_dummy_port(iphy) == iport) {
544 struct scic_sds_controller *scic = iport->owning_controller;
545 struct isci_host *ihost = scic_to_ihost(scic);
547 /* Yep it is assigned to this port so remove it */
548 scic_sds_phy_set_port(iphy, &ihost->ports[SCI_MAX_PORTS]);
549 iport->phy_table[iphy->phy_index] = NULL;
550 return SCI_SUCCESS;
553 return SCI_FAILURE;
558 * This method requests the SAS address for the supplied SAS port from the SCI
559 * implementation.
560 * @sci_port: a handle corresponding to the SAS port for which to return the
561 * SAS address.
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 port.
566 void scic_sds_port_get_sas_address(
567 struct isci_port *iport,
568 struct sci_sas_address *sas_address)
570 u32 index;
572 sas_address->high = 0;
573 sas_address->low = 0;
575 for (index = 0; index < SCI_MAX_PHYS; index++) {
576 if (iport->phy_table[index] != NULL) {
577 scic_sds_phy_get_sas_address(iport->phy_table[index], sas_address);
583 * This function requests the SAS address for the device directly attached to
584 * this SAS port.
585 * @sci_port: a handle corresponding to the SAS port for which to return the
586 * SAS address.
587 * @sas_address: This parameter specifies a pointer to a SAS address structure
588 * into which the core will copy the SAS address for the device directly
589 * attached to the port.
592 void scic_sds_port_get_attached_sas_address(
593 struct isci_port *iport,
594 struct sci_sas_address *sas_address)
596 struct isci_phy *iphy;
599 * Ensure that the phy is both part of the port and currently
600 * connected to the remote end-point.
602 iphy = scic_sds_port_get_a_connected_phy(iport);
603 if (iphy) {
604 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA) {
605 scic_sds_phy_get_attached_sas_address(iphy,
606 sas_address);
607 } else {
608 scic_sds_phy_get_sas_address(iphy, sas_address);
609 sas_address->low += iphy->phy_index;
611 } else {
612 sas_address->high = 0;
613 sas_address->low = 0;
618 * scic_sds_port_construct_dummy_rnc() - create dummy rnc for si workaround
620 * @sci_port: logical port on which we need to create the remote node context
621 * @rni: remote node index for this remote node context.
623 * This routine will construct a dummy remote node context data structure
624 * This structure will be posted to the hardware to work around a scheduler
625 * error in the hardware.
627 static void scic_sds_port_construct_dummy_rnc(struct isci_port *iport, u16 rni)
629 union scu_remote_node_context *rnc;
631 rnc = &iport->owning_controller->remote_node_context_table[rni];
633 memset(rnc, 0, sizeof(union scu_remote_node_context));
635 rnc->ssp.remote_sas_address_hi = 0;
636 rnc->ssp.remote_sas_address_lo = 0;
638 rnc->ssp.remote_node_index = rni;
639 rnc->ssp.remote_node_port_width = 1;
640 rnc->ssp.logical_port_index = iport->physical_port_index;
642 rnc->ssp.nexus_loss_timer_enable = false;
643 rnc->ssp.check_bit = false;
644 rnc->ssp.is_valid = true;
645 rnc->ssp.is_remote_node_context = true;
646 rnc->ssp.function_number = 0;
647 rnc->ssp.arbitration_wait_time = 0;
651 * construct a dummy task context data structure. This
652 * structure will be posted to the hardwre to work around a scheduler error
653 * in the hardware.
655 static void scic_sds_port_construct_dummy_task(struct isci_port *iport, u16 tag)
657 struct scic_sds_controller *scic = iport->owning_controller;
658 struct scu_task_context *task_context;
660 task_context = &scic->task_context_table[ISCI_TAG_TCI(tag)];
661 memset(task_context, 0, sizeof(struct scu_task_context));
663 task_context->initiator_request = 1;
664 task_context->connection_rate = 1;
665 task_context->logical_port_index = iport->physical_port_index;
666 task_context->protocol_type = SCU_TASK_CONTEXT_PROTOCOL_SSP;
667 task_context->task_index = ISCI_TAG_TCI(tag);
668 task_context->valid = SCU_TASK_CONTEXT_VALID;
669 task_context->context_type = SCU_TASK_CONTEXT_TYPE;
670 task_context->remote_node_index = iport->reserved_rni;
671 task_context->do_not_dma_ssp_good_response = 1;
672 task_context->task_phase = 0x01;
675 static void scic_sds_port_destroy_dummy_resources(struct isci_port *iport)
677 struct scic_sds_controller *scic = iport->owning_controller;
679 if (iport->reserved_tag != SCI_CONTROLLER_INVALID_IO_TAG)
680 isci_free_tag(scic_to_ihost(scic), iport->reserved_tag);
682 if (iport->reserved_rni != SCU_DUMMY_INDEX)
683 scic_sds_remote_node_table_release_remote_node_index(&scic->available_remote_nodes,
684 1, iport->reserved_rni);
686 iport->reserved_rni = SCU_DUMMY_INDEX;
687 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
691 * This method performs initialization of the supplied port. Initialization
692 * includes: - state machine initialization - member variable initialization
693 * - configuring the phy_mask
694 * @sci_port:
695 * @transport_layer_registers:
696 * @port_task_scheduler_registers:
697 * @port_configuration_regsiter:
699 * enum sci_status SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION This value is returned
700 * if the phy being added to the port
702 enum sci_status scic_sds_port_initialize(
703 struct isci_port *iport,
704 void __iomem *port_task_scheduler_registers,
705 void __iomem *port_configuration_regsiter,
706 void __iomem *viit_registers)
708 iport->port_task_scheduler_registers = port_task_scheduler_registers;
709 iport->port_pe_configuration_register = port_configuration_regsiter;
710 iport->viit_registers = viit_registers;
712 return SCI_SUCCESS;
717 * This method assigns the direct attached device ID for this port.
719 * @param[in] iport The port for which the direct attached device id is to
720 * be assigned.
721 * @param[in] device_id The direct attached device ID to assign to the port.
722 * This will be the RNi for the device
724 void scic_sds_port_setup_transports(
725 struct isci_port *iport,
726 u32 device_id)
728 u8 index;
730 for (index = 0; index < SCI_MAX_PHYS; index++) {
731 if (iport->active_phy_mask & (1 << index))
732 scic_sds_phy_setup_transport(iport->phy_table[index], device_id);
738 * @sci_port: This is the port on which the phy should be enabled.
739 * @sci_phy: This is the specific phy which to enable.
740 * @do_notify_user: This parameter specifies whether to inform the user (via
741 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
743 * This function will activate the phy in the port.
744 * Activation includes: - adding
745 * the phy to the port - enabling the Protocol Engine in the silicon. -
746 * notifying the user that the link is up. none
748 static void scic_sds_port_activate_phy(struct isci_port *iport,
749 struct isci_phy *iphy,
750 bool do_notify_user)
752 struct scic_sds_controller *scic = iport->owning_controller;
753 struct isci_host *ihost = scic_to_ihost(scic);
755 if (iphy->protocol != SCIC_SDS_PHY_PROTOCOL_SATA)
756 scic_sds_phy_resume(iphy);
758 iport->active_phy_mask |= 1 << iphy->phy_index;
760 scic_sds_controller_clear_invalid_phy(scic, iphy);
762 if (do_notify_user == true)
763 isci_port_link_up(ihost, iport, iphy);
766 void scic_sds_port_deactivate_phy(struct isci_port *iport,
767 struct isci_phy *iphy,
768 bool do_notify_user)
770 struct scic_sds_controller *scic = scic_sds_port_get_controller(iport);
771 struct isci_host *ihost = scic_to_ihost(scic);
773 iport->active_phy_mask &= ~(1 << iphy->phy_index);
775 iphy->max_negotiated_speed = SAS_LINK_RATE_UNKNOWN;
777 /* Re-assign the phy back to the LP as if it were a narrow port */
778 writel(iphy->phy_index,
779 &iport->port_pe_configuration_register[iphy->phy_index]);
781 if (do_notify_user == true)
782 isci_port_link_down(ihost, iphy, iport);
787 * @sci_port: This is the port on which the phy should be disabled.
788 * @sci_phy: This is the specific phy which to disabled.
790 * This function will disable the phy and report that the phy is not valid for
791 * this port object. None
793 static void scic_sds_port_invalid_link_up(struct isci_port *iport,
794 struct isci_phy *iphy)
796 struct scic_sds_controller *scic = iport->owning_controller;
799 * Check to see if we have alreay reported this link as bad and if
800 * not go ahead and tell the SCI_USER that we have discovered an
801 * invalid link.
803 if ((scic->invalid_phy_mask & (1 << iphy->phy_index)) == 0) {
804 scic_sds_controller_set_invalid_phy(scic, iphy);
805 dev_warn(&scic_to_ihost(scic)->pdev->dev, "Invalid link up!\n");
809 static bool is_port_ready_state(enum scic_sds_port_states state)
811 switch (state) {
812 case SCI_PORT_READY:
813 case SCI_PORT_SUB_WAITING:
814 case SCI_PORT_SUB_OPERATIONAL:
815 case SCI_PORT_SUB_CONFIGURING:
816 return true;
817 default:
818 return false;
822 /* flag dummy rnc hanling when exiting a ready state */
823 static void port_state_machine_change(struct isci_port *iport,
824 enum scic_sds_port_states state)
826 struct sci_base_state_machine *sm = &iport->sm;
827 enum scic_sds_port_states old_state = sm->current_state_id;
829 if (is_port_ready_state(old_state) && !is_port_ready_state(state))
830 iport->ready_exit = true;
832 sci_change_state(sm, state);
833 iport->ready_exit = false;
837 * scic_sds_port_general_link_up_handler - phy can be assigned to port?
838 * @sci_port: scic_sds_port object for which has a phy that has gone link up.
839 * @sci_phy: This is the struct isci_phy object that has gone link up.
840 * @do_notify_user: This parameter specifies whether to inform the user (via
841 * scic_cb_port_link_up()) as to the fact that a new phy as become ready.
843 * Determine if this phy can be assigned to this
844 * port . If the phy is not a valid PHY for
845 * this port then the function will notify the user. A PHY can only be
846 * part of a port if it's attached SAS ADDRESS is the same as all other PHYs in
847 * the same port. none
849 static void scic_sds_port_general_link_up_handler(struct isci_port *iport,
850 struct isci_phy *iphy,
851 bool do_notify_user)
853 struct sci_sas_address port_sas_address;
854 struct sci_sas_address phy_sas_address;
856 scic_sds_port_get_attached_sas_address(iport, &port_sas_address);
857 scic_sds_phy_get_attached_sas_address(iphy, &phy_sas_address);
859 /* If the SAS address of the new phy matches the SAS address of
860 * other phys in the port OR this is the first phy in the port,
861 * then activate the phy and allow it to be used for operations
862 * in this port.
864 if ((phy_sas_address.high == port_sas_address.high &&
865 phy_sas_address.low == port_sas_address.low) ||
866 iport->active_phy_mask == 0) {
867 struct sci_base_state_machine *sm = &iport->sm;
869 scic_sds_port_activate_phy(iport, iphy, do_notify_user);
870 if (sm->current_state_id == SCI_PORT_RESETTING)
871 port_state_machine_change(iport, SCI_PORT_READY);
872 } else
873 scic_sds_port_invalid_link_up(iport, iphy);
879 * This method returns false if the port only has a single phy object assigned.
880 * If there are no phys or more than one phy then the method will return
881 * true.
882 * @sci_port: The port for which the wide port condition is to be checked.
884 * bool true Is returned if this is a wide ported port. false Is returned if
885 * this is a narrow port.
887 static bool scic_sds_port_is_wide(struct isci_port *iport)
889 u32 index;
890 u32 phy_count = 0;
892 for (index = 0; index < SCI_MAX_PHYS; index++) {
893 if (iport->phy_table[index] != NULL) {
894 phy_count++;
898 return phy_count != 1;
902 * This method is called by the PHY object when the link is detected. if the
903 * port wants the PHY to continue on to the link up state then the port
904 * layer must return true. If the port object returns false the phy object
905 * must halt its attempt to go link up.
906 * @sci_port: The port associated with the phy object.
907 * @sci_phy: The phy object that is trying to go link up.
909 * true if the phy object can continue to the link up condition. true Is
910 * returned if this phy can continue to the ready state. false Is returned if
911 * can not continue on to the ready state. This notification is in place for
912 * wide ports and direct attached phys. Since there are no wide ported SATA
913 * devices this could become an invalid port configuration.
915 bool scic_sds_port_link_detected(
916 struct isci_port *iport,
917 struct isci_phy *iphy)
919 if ((iport->logical_port_index != SCIC_SDS_DUMMY_PORT) &&
920 (iphy->protocol == SCIC_SDS_PHY_PROTOCOL_SATA) &&
921 scic_sds_port_is_wide(iport)) {
922 scic_sds_port_invalid_link_up(iport, iphy);
924 return false;
927 return true;
930 static void port_timeout(unsigned long data)
932 struct sci_timer *tmr = (struct sci_timer *)data;
933 struct isci_port *iport = container_of(tmr, typeof(*iport), timer);
934 struct isci_host *ihost = scic_to_ihost(iport->owning_controller);
935 unsigned long flags;
936 u32 current_state;
938 spin_lock_irqsave(&ihost->scic_lock, flags);
940 if (tmr->cancel)
941 goto done;
943 current_state = iport->sm.current_state_id;
945 if (current_state == SCI_PORT_RESETTING) {
946 /* if the port is still in the resetting state then the timeout
947 * fired before the reset completed.
949 port_state_machine_change(iport, SCI_PORT_FAILED);
950 } else if (current_state == SCI_PORT_STOPPED) {
951 /* if the port is stopped then the start request failed In this
952 * case stay in the stopped state.
954 dev_err(sciport_to_dev(iport),
955 "%s: SCIC Port 0x%p failed to stop before tiemout.\n",
956 __func__,
957 iport);
958 } else if (current_state == SCI_PORT_STOPPING) {
959 /* if the port is still stopping then the stop has not completed */
960 isci_port_stop_complete(iport->owning_controller,
961 iport,
962 SCI_FAILURE_TIMEOUT);
963 } else {
964 /* The port is in the ready state and we have a timer
965 * reporting a timeout this should not happen.
967 dev_err(sciport_to_dev(iport),
968 "%s: SCIC Port 0x%p is processing a timeout operation "
969 "in state %d.\n", __func__, iport, current_state);
972 done:
973 spin_unlock_irqrestore(&ihost->scic_lock, flags);
976 /* --------------------------------------------------------------------------- */
979 * This function updates the hardwares VIIT entry for this port.
983 static void scic_sds_port_update_viit_entry(struct isci_port *iport)
985 struct sci_sas_address sas_address;
987 scic_sds_port_get_sas_address(iport, &sas_address);
989 writel(sas_address.high,
990 &iport->viit_registers->initiator_sas_address_hi);
991 writel(sas_address.low,
992 &iport->viit_registers->initiator_sas_address_lo);
994 /* This value get cleared just in case its not already cleared */
995 writel(0, &iport->viit_registers->reserved);
997 /* We are required to update the status register last */
998 writel(SCU_VIIT_ENTRY_ID_VIIT |
999 SCU_VIIT_IPPT_INITIATOR |
1000 ((1 << iport->physical_port_index) << SCU_VIIT_ENTRY_LPVIE_SHIFT) |
1001 SCU_VIIT_STATUS_ALL_VALID,
1002 &iport->viit_registers->status);
1005 enum sas_linkrate scic_sds_port_get_max_allowed_speed(struct isci_port *iport)
1007 u16 index;
1008 struct isci_phy *iphy;
1009 enum sas_linkrate max_allowed_speed = SAS_LINK_RATE_6_0_GBPS;
1012 * Loop through all of the phys in this port and find the phy with the
1013 * lowest maximum link rate. */
1014 for (index = 0; index < SCI_MAX_PHYS; index++) {
1015 iphy = iport->phy_table[index];
1016 if (iphy && scic_sds_port_active_phy(iport, iphy) &&
1017 iphy->max_negotiated_speed < max_allowed_speed)
1018 max_allowed_speed = iphy->max_negotiated_speed;
1021 return max_allowed_speed;
1024 static void scic_sds_port_suspend_port_task_scheduler(struct isci_port *iport)
1026 u32 pts_control_value;
1028 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
1029 pts_control_value |= SCU_PTSxCR_GEN_BIT(SUSPEND);
1030 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
1034 * scic_sds_port_post_dummy_request() - post dummy/workaround request
1035 * @sci_port: port to post task
1037 * Prevent the hardware scheduler from posting new requests to the front
1038 * of the scheduler queue causing a starvation problem for currently
1039 * ongoing requests.
1042 static void scic_sds_port_post_dummy_request(struct isci_port *iport)
1044 struct scic_sds_controller *scic = iport->owning_controller;
1045 u16 tag = iport->reserved_tag;
1046 struct scu_task_context *tc;
1047 u32 command;
1049 tc = &scic->task_context_table[ISCI_TAG_TCI(tag)];
1050 tc->abort = 0;
1052 command = SCU_CONTEXT_COMMAND_REQUEST_TYPE_POST_TC |
1053 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1054 ISCI_TAG_TCI(tag);
1056 scic_sds_controller_post_request(scic, command);
1060 * This routine will abort the dummy request. This will alow the hardware to
1061 * power down parts of the silicon to save power.
1063 * @sci_port: The port on which the task must be aborted.
1066 static void scic_sds_port_abort_dummy_request(struct isci_port *iport)
1068 struct scic_sds_controller *scic = iport->owning_controller;
1069 u16 tag = iport->reserved_tag;
1070 struct scu_task_context *tc;
1071 u32 command;
1073 tc = &scic->task_context_table[ISCI_TAG_TCI(tag)];
1074 tc->abort = 1;
1076 command = SCU_CONTEXT_COMMAND_REQUEST_POST_TC_ABORT |
1077 iport->physical_port_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT |
1078 ISCI_TAG_TCI(tag);
1080 scic_sds_controller_post_request(scic, command);
1085 * @sci_port: This is the struct isci_port object to resume.
1087 * This method will resume the port task scheduler for this port object. none
1089 static void
1090 scic_sds_port_resume_port_task_scheduler(struct isci_port *iport)
1092 u32 pts_control_value;
1094 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
1095 pts_control_value &= ~SCU_PTSxCR_GEN_BIT(SUSPEND);
1096 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
1099 static void scic_sds_port_ready_substate_waiting_enter(struct sci_base_state_machine *sm)
1101 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1103 scic_sds_port_suspend_port_task_scheduler(iport);
1105 iport->not_ready_reason = SCIC_PORT_NOT_READY_NO_ACTIVE_PHYS;
1107 if (iport->active_phy_mask != 0) {
1108 /* At least one of the phys on the port is ready */
1109 port_state_machine_change(iport,
1110 SCI_PORT_SUB_OPERATIONAL);
1114 static void scic_sds_port_ready_substate_operational_enter(struct sci_base_state_machine *sm)
1116 u32 index;
1117 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1118 struct scic_sds_controller *scic = iport->owning_controller;
1119 struct isci_host *ihost = scic_to_ihost(scic);
1121 isci_port_ready(ihost, iport);
1123 for (index = 0; index < SCI_MAX_PHYS; index++) {
1124 if (iport->phy_table[index]) {
1125 writel(iport->physical_port_index,
1126 &iport->port_pe_configuration_register[
1127 iport->phy_table[index]->phy_index]);
1131 scic_sds_port_update_viit_entry(iport);
1133 scic_sds_port_resume_port_task_scheduler(iport);
1136 * Post the dummy task for the port so the hardware can schedule
1137 * io correctly
1139 scic_sds_port_post_dummy_request(iport);
1142 static void scic_sds_port_invalidate_dummy_remote_node(struct isci_port *iport)
1144 struct scic_sds_controller *scic = iport->owning_controller;
1145 u8 phys_index = iport->physical_port_index;
1146 union scu_remote_node_context *rnc;
1147 u16 rni = iport->reserved_rni;
1148 u32 command;
1150 rnc = &scic->remote_node_context_table[rni];
1152 rnc->ssp.is_valid = false;
1154 /* ensure the preceding tc abort request has reached the
1155 * controller and give it ample time to act before posting the rnc
1156 * invalidate
1158 readl(&scic->smu_registers->interrupt_status); /* flush */
1159 udelay(10);
1161 command = SCU_CONTEXT_COMMAND_POST_RNC_INVALIDATE |
1162 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1164 scic_sds_controller_post_request(scic, command);
1169 * @object: This is the object which is cast to a struct isci_port object.
1171 * This method will perform the actions required by the struct isci_port on
1172 * exiting the SCI_PORT_SUB_OPERATIONAL. This function reports
1173 * the port not ready and suspends the port task scheduler. none
1175 static void scic_sds_port_ready_substate_operational_exit(struct sci_base_state_machine *sm)
1177 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1178 struct scic_sds_controller *scic = iport->owning_controller;
1179 struct isci_host *ihost = scic_to_ihost(scic);
1182 * Kill the dummy task for this port if it has not yet posted
1183 * the hardware will treat this as a NOP and just return abort
1184 * complete.
1186 scic_sds_port_abort_dummy_request(iport);
1188 isci_port_not_ready(ihost, iport);
1190 if (iport->ready_exit)
1191 scic_sds_port_invalidate_dummy_remote_node(iport);
1194 static void scic_sds_port_ready_substate_configuring_enter(struct sci_base_state_machine *sm)
1196 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1197 struct scic_sds_controller *scic = iport->owning_controller;
1198 struct isci_host *ihost = scic_to_ihost(scic);
1200 if (iport->active_phy_mask == 0) {
1201 isci_port_not_ready(ihost, iport);
1203 port_state_machine_change(iport,
1204 SCI_PORT_SUB_WAITING);
1205 } else if (iport->started_request_count == 0)
1206 port_state_machine_change(iport,
1207 SCI_PORT_SUB_OPERATIONAL);
1210 static void scic_sds_port_ready_substate_configuring_exit(struct sci_base_state_machine *sm)
1212 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1214 scic_sds_port_suspend_port_task_scheduler(iport);
1215 if (iport->ready_exit)
1216 scic_sds_port_invalidate_dummy_remote_node(iport);
1219 enum sci_status scic_sds_port_start(struct isci_port *iport)
1221 struct scic_sds_controller *scic = iport->owning_controller;
1222 enum sci_status status = SCI_SUCCESS;
1223 enum scic_sds_port_states state;
1224 u32 phy_mask;
1226 state = iport->sm.current_state_id;
1227 if (state != SCI_PORT_STOPPED) {
1228 dev_warn(sciport_to_dev(iport),
1229 "%s: in wrong state: %d\n", __func__, state);
1230 return SCI_FAILURE_INVALID_STATE;
1233 if (iport->assigned_device_count > 0) {
1234 /* TODO This is a start failure operation because
1235 * there are still devices assigned to this port.
1236 * There must be no devices assigned to a port on a
1237 * start operation.
1239 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1242 if (iport->reserved_rni == SCU_DUMMY_INDEX) {
1243 u16 rni = scic_sds_remote_node_table_allocate_remote_node(
1244 &scic->available_remote_nodes, 1);
1246 if (rni != SCU_DUMMY_INDEX)
1247 scic_sds_port_construct_dummy_rnc(iport, rni);
1248 else
1249 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1250 iport->reserved_rni = rni;
1253 if (iport->reserved_tag == SCI_CONTROLLER_INVALID_IO_TAG) {
1254 struct isci_host *ihost = scic_to_ihost(scic);
1255 u16 tag;
1257 tag = isci_alloc_tag(ihost);
1258 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
1259 status = SCI_FAILURE_INSUFFICIENT_RESOURCES;
1260 else
1261 scic_sds_port_construct_dummy_task(iport, tag);
1262 iport->reserved_tag = tag;
1265 if (status == SCI_SUCCESS) {
1266 phy_mask = scic_sds_port_get_phys(iport);
1269 * There are one or more phys assigned to this port. Make sure
1270 * the port's phy mask is in fact legal and supported by the
1271 * silicon.
1273 if (scic_sds_port_is_phy_mask_valid(iport, phy_mask) == true) {
1274 port_state_machine_change(iport,
1275 SCI_PORT_READY);
1277 return SCI_SUCCESS;
1279 status = SCI_FAILURE;
1282 if (status != SCI_SUCCESS)
1283 scic_sds_port_destroy_dummy_resources(iport);
1285 return status;
1288 enum sci_status scic_sds_port_stop(struct isci_port *iport)
1290 enum scic_sds_port_states state;
1292 state = iport->sm.current_state_id;
1293 switch (state) {
1294 case SCI_PORT_STOPPED:
1295 return SCI_SUCCESS;
1296 case SCI_PORT_SUB_WAITING:
1297 case SCI_PORT_SUB_OPERATIONAL:
1298 case SCI_PORT_SUB_CONFIGURING:
1299 case SCI_PORT_RESETTING:
1300 port_state_machine_change(iport,
1301 SCI_PORT_STOPPING);
1302 return SCI_SUCCESS;
1303 default:
1304 dev_warn(sciport_to_dev(iport),
1305 "%s: in wrong state: %d\n", __func__, state);
1306 return SCI_FAILURE_INVALID_STATE;
1310 static enum sci_status scic_port_hard_reset(struct isci_port *iport, u32 timeout)
1312 enum sci_status status = SCI_FAILURE_INVALID_PHY;
1313 struct isci_phy *iphy = NULL;
1314 enum scic_sds_port_states state;
1315 u32 phy_index;
1317 state = iport->sm.current_state_id;
1318 if (state != SCI_PORT_SUB_OPERATIONAL) {
1319 dev_warn(sciport_to_dev(iport),
1320 "%s: in wrong state: %d\n", __func__, state);
1321 return SCI_FAILURE_INVALID_STATE;
1324 /* Select a phy on which we can send the hard reset request. */
1325 for (phy_index = 0; phy_index < SCI_MAX_PHYS && !iphy; phy_index++) {
1326 iphy = iport->phy_table[phy_index];
1327 if (iphy && !scic_sds_port_active_phy(iport, iphy)) {
1329 * We found a phy but it is not ready select
1330 * different phy
1332 iphy = NULL;
1336 /* If we have a phy then go ahead and start the reset procedure */
1337 if (!iphy)
1338 return status;
1339 status = scic_sds_phy_reset(iphy);
1341 if (status != SCI_SUCCESS)
1342 return status;
1344 sci_mod_timer(&iport->timer, timeout);
1345 iport->not_ready_reason = SCIC_PORT_NOT_READY_HARD_RESET_REQUESTED;
1347 port_state_machine_change(iport, SCI_PORT_RESETTING);
1348 return SCI_SUCCESS;
1352 * scic_sds_port_add_phy() -
1353 * @sci_port: This parameter specifies the port in which the phy will be added.
1354 * @sci_phy: This parameter is the phy which is to be added to the port.
1356 * This method will add a PHY to the selected port. This method returns an
1357 * enum sci_status. SCI_SUCCESS the phy has been added to the port. Any other
1358 * status is a failure to add the phy to the port.
1360 enum sci_status scic_sds_port_add_phy(struct isci_port *iport,
1361 struct isci_phy *iphy)
1363 enum sci_status status;
1364 enum scic_sds_port_states state;
1366 state = iport->sm.current_state_id;
1367 switch (state) {
1368 case SCI_PORT_STOPPED: {
1369 struct sci_sas_address port_sas_address;
1371 /* Read the port assigned SAS Address if there is one */
1372 scic_sds_port_get_sas_address(iport, &port_sas_address);
1374 if (port_sas_address.high != 0 && port_sas_address.low != 0) {
1375 struct sci_sas_address phy_sas_address;
1377 /* Make sure that the PHY SAS Address matches the SAS Address
1378 * for this port
1380 scic_sds_phy_get_sas_address(iphy, &phy_sas_address);
1382 if (port_sas_address.high != phy_sas_address.high ||
1383 port_sas_address.low != phy_sas_address.low)
1384 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
1386 return scic_sds_port_set_phy(iport, iphy);
1388 case SCI_PORT_SUB_WAITING:
1389 case SCI_PORT_SUB_OPERATIONAL:
1390 status = scic_sds_port_set_phy(iport, iphy);
1392 if (status != SCI_SUCCESS)
1393 return status;
1395 scic_sds_port_general_link_up_handler(iport, iphy, true);
1396 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1397 port_state_machine_change(iport, SCI_PORT_SUB_CONFIGURING);
1399 return status;
1400 case SCI_PORT_SUB_CONFIGURING:
1401 status = scic_sds_port_set_phy(iport, iphy);
1403 if (status != SCI_SUCCESS)
1404 return status;
1405 scic_sds_port_general_link_up_handler(iport, iphy, true);
1407 /* Re-enter the configuring state since this may be the last phy in
1408 * the port.
1410 port_state_machine_change(iport,
1411 SCI_PORT_SUB_CONFIGURING);
1412 return SCI_SUCCESS;
1413 default:
1414 dev_warn(sciport_to_dev(iport),
1415 "%s: in wrong state: %d\n", __func__, state);
1416 return SCI_FAILURE_INVALID_STATE;
1421 * scic_sds_port_remove_phy() -
1422 * @sci_port: This parameter specifies the port in which the phy will be added.
1423 * @sci_phy: This parameter is the phy which is to be added to the port.
1425 * This method will remove the PHY from the selected PORT. This method returns
1426 * an enum sci_status. SCI_SUCCESS the phy has been removed from the port. Any
1427 * other status is a failure to add the phy to the port.
1429 enum sci_status scic_sds_port_remove_phy(struct isci_port *iport,
1430 struct isci_phy *iphy)
1432 enum sci_status status;
1433 enum scic_sds_port_states state;
1435 state = iport->sm.current_state_id;
1437 switch (state) {
1438 case SCI_PORT_STOPPED:
1439 return scic_sds_port_clear_phy(iport, iphy);
1440 case SCI_PORT_SUB_OPERATIONAL:
1441 status = scic_sds_port_clear_phy(iport, iphy);
1442 if (status != SCI_SUCCESS)
1443 return status;
1445 scic_sds_port_deactivate_phy(iport, iphy, true);
1446 iport->not_ready_reason = SCIC_PORT_NOT_READY_RECONFIGURING;
1447 port_state_machine_change(iport,
1448 SCI_PORT_SUB_CONFIGURING);
1449 return SCI_SUCCESS;
1450 case SCI_PORT_SUB_CONFIGURING:
1451 status = scic_sds_port_clear_phy(iport, iphy);
1453 if (status != SCI_SUCCESS)
1454 return status;
1455 scic_sds_port_deactivate_phy(iport, iphy, true);
1457 /* Re-enter the configuring state since this may be the last phy in
1458 * the port
1460 port_state_machine_change(iport,
1461 SCI_PORT_SUB_CONFIGURING);
1462 return SCI_SUCCESS;
1463 default:
1464 dev_warn(sciport_to_dev(iport),
1465 "%s: in wrong state: %d\n", __func__, state);
1466 return SCI_FAILURE_INVALID_STATE;
1470 enum sci_status scic_sds_port_link_up(struct isci_port *iport,
1471 struct isci_phy *iphy)
1473 enum scic_sds_port_states state;
1475 state = iport->sm.current_state_id;
1476 switch (state) {
1477 case SCI_PORT_SUB_WAITING:
1478 /* Since this is the first phy going link up for the port we
1479 * can just enable it and continue
1481 scic_sds_port_activate_phy(iport, iphy, true);
1483 port_state_machine_change(iport,
1484 SCI_PORT_SUB_OPERATIONAL);
1485 return SCI_SUCCESS;
1486 case SCI_PORT_SUB_OPERATIONAL:
1487 scic_sds_port_general_link_up_handler(iport, iphy, true);
1488 return SCI_SUCCESS;
1489 case SCI_PORT_RESETTING:
1490 /* TODO We should make sure that the phy that has gone
1491 * link up is the same one on which we sent the reset. It is
1492 * possible that the phy on which we sent the reset is not the
1493 * one that has gone link up and we want to make sure that
1494 * phy being reset comes back. Consider the case where a
1495 * reset is sent but before the hardware processes the reset it
1496 * get a link up on the port because of a hot plug event.
1497 * because of the reset request this phy will go link down
1498 * almost immediately.
1501 /* In the resetting state we don't notify the user regarding
1502 * link up and link down notifications.
1504 scic_sds_port_general_link_up_handler(iport, iphy, false);
1505 return SCI_SUCCESS;
1506 default:
1507 dev_warn(sciport_to_dev(iport),
1508 "%s: in wrong state: %d\n", __func__, state);
1509 return SCI_FAILURE_INVALID_STATE;
1513 enum sci_status scic_sds_port_link_down(struct isci_port *iport,
1514 struct isci_phy *iphy)
1516 enum scic_sds_port_states state;
1518 state = iport->sm.current_state_id;
1519 switch (state) {
1520 case SCI_PORT_SUB_OPERATIONAL:
1521 scic_sds_port_deactivate_phy(iport, iphy, true);
1523 /* If there are no active phys left in the port, then
1524 * transition the port to the WAITING state until such time
1525 * as a phy goes link up
1527 if (iport->active_phy_mask == 0)
1528 port_state_machine_change(iport,
1529 SCI_PORT_SUB_WAITING);
1530 return SCI_SUCCESS;
1531 case SCI_PORT_RESETTING:
1532 /* In the resetting state we don't notify the user regarding
1533 * link up and link down notifications. */
1534 scic_sds_port_deactivate_phy(iport, iphy, false);
1535 return SCI_SUCCESS;
1536 default:
1537 dev_warn(sciport_to_dev(iport),
1538 "%s: in wrong state: %d\n", __func__, state);
1539 return SCI_FAILURE_INVALID_STATE;
1543 enum sci_status scic_sds_port_start_io(struct isci_port *iport,
1544 struct scic_sds_remote_device *sci_dev,
1545 struct isci_request *ireq)
1547 enum scic_sds_port_states state;
1549 state = iport->sm.current_state_id;
1550 switch (state) {
1551 case SCI_PORT_SUB_WAITING:
1552 return SCI_FAILURE_INVALID_STATE;
1553 case SCI_PORT_SUB_OPERATIONAL:
1554 iport->started_request_count++;
1555 return SCI_SUCCESS;
1556 default:
1557 dev_warn(sciport_to_dev(iport),
1558 "%s: in wrong state: %d\n", __func__, state);
1559 return SCI_FAILURE_INVALID_STATE;
1563 enum sci_status scic_sds_port_complete_io(struct isci_port *iport,
1564 struct scic_sds_remote_device *sci_dev,
1565 struct isci_request *ireq)
1567 enum scic_sds_port_states state;
1569 state = iport->sm.current_state_id;
1570 switch (state) {
1571 case SCI_PORT_STOPPED:
1572 dev_warn(sciport_to_dev(iport),
1573 "%s: in wrong state: %d\n", __func__, state);
1574 return SCI_FAILURE_INVALID_STATE;
1575 case SCI_PORT_STOPPING:
1576 scic_sds_port_decrement_request_count(iport);
1578 if (iport->started_request_count == 0)
1579 port_state_machine_change(iport,
1580 SCI_PORT_STOPPED);
1581 break;
1582 case SCI_PORT_READY:
1583 case SCI_PORT_RESETTING:
1584 case SCI_PORT_FAILED:
1585 case SCI_PORT_SUB_WAITING:
1586 case SCI_PORT_SUB_OPERATIONAL:
1587 scic_sds_port_decrement_request_count(iport);
1588 break;
1589 case SCI_PORT_SUB_CONFIGURING:
1590 scic_sds_port_decrement_request_count(iport);
1591 if (iport->started_request_count == 0) {
1592 port_state_machine_change(iport,
1593 SCI_PORT_SUB_OPERATIONAL);
1595 break;
1597 return SCI_SUCCESS;
1602 * @sci_port: This is the port object which to suspend.
1604 * This method will enable the SCU Port Task Scheduler for this port object but
1605 * will leave the port task scheduler in a suspended state. none
1607 static void
1608 scic_sds_port_enable_port_task_scheduler(struct isci_port *iport)
1610 u32 pts_control_value;
1612 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
1613 pts_control_value |= SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND);
1614 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
1619 * @sci_port: This is the port object which to resume.
1621 * This method will disable the SCU port task scheduler for this port object.
1622 * none
1624 static void
1625 scic_sds_port_disable_port_task_scheduler(struct isci_port *iport)
1627 u32 pts_control_value;
1629 pts_control_value = readl(&iport->port_task_scheduler_registers->control);
1630 pts_control_value &=
1631 ~(SCU_PTSxCR_GEN_BIT(ENABLE) | SCU_PTSxCR_GEN_BIT(SUSPEND));
1632 writel(pts_control_value, &iport->port_task_scheduler_registers->control);
1635 static void scic_sds_port_post_dummy_remote_node(struct isci_port *iport)
1637 struct scic_sds_controller *scic = iport->owning_controller;
1638 u8 phys_index = iport->physical_port_index;
1639 union scu_remote_node_context *rnc;
1640 u16 rni = iport->reserved_rni;
1641 u32 command;
1643 rnc = &scic->remote_node_context_table[rni];
1644 rnc->ssp.is_valid = true;
1646 command = SCU_CONTEXT_COMMAND_POST_RNC_32 |
1647 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1649 scic_sds_controller_post_request(scic, command);
1651 /* ensure hardware has seen the post rnc command and give it
1652 * ample time to act before sending the suspend
1654 readl(&scic->smu_registers->interrupt_status); /* flush */
1655 udelay(10);
1657 command = SCU_CONTEXT_COMMAND_POST_RNC_SUSPEND_TX_RX |
1658 phys_index << SCU_CONTEXT_COMMAND_LOGICAL_PORT_SHIFT | rni;
1660 scic_sds_controller_post_request(scic, command);
1663 static void scic_sds_port_stopped_state_enter(struct sci_base_state_machine *sm)
1665 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1667 if (iport->sm.previous_state_id == SCI_PORT_STOPPING) {
1669 * If we enter this state becasuse of a request to stop
1670 * the port then we want to disable the hardwares port
1671 * task scheduler. */
1672 scic_sds_port_disable_port_task_scheduler(iport);
1676 static void scic_sds_port_stopped_state_exit(struct sci_base_state_machine *sm)
1678 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1680 /* Enable and suspend the port task scheduler */
1681 scic_sds_port_enable_port_task_scheduler(iport);
1684 static void scic_sds_port_ready_state_enter(struct sci_base_state_machine *sm)
1686 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1687 struct scic_sds_controller *scic = iport->owning_controller;
1688 struct isci_host *ihost = scic_to_ihost(scic);
1689 u32 prev_state;
1691 prev_state = iport->sm.previous_state_id;
1692 if (prev_state == SCI_PORT_RESETTING)
1693 isci_port_hard_reset_complete(iport, SCI_SUCCESS);
1694 else
1695 isci_port_not_ready(ihost, iport);
1697 /* Post and suspend the dummy remote node context for this port. */
1698 scic_sds_port_post_dummy_remote_node(iport);
1700 /* Start the ready substate machine */
1701 port_state_machine_change(iport,
1702 SCI_PORT_SUB_WAITING);
1705 static void scic_sds_port_resetting_state_exit(struct sci_base_state_machine *sm)
1707 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1709 sci_del_timer(&iport->timer);
1712 static void scic_sds_port_stopping_state_exit(struct sci_base_state_machine *sm)
1714 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1716 sci_del_timer(&iport->timer);
1718 scic_sds_port_destroy_dummy_resources(iport);
1721 static void scic_sds_port_failed_state_enter(struct sci_base_state_machine *sm)
1723 struct isci_port *iport = container_of(sm, typeof(*iport), sm);
1725 isci_port_hard_reset_complete(iport, SCI_FAILURE_TIMEOUT);
1728 /* --------------------------------------------------------------------------- */
1730 static const struct sci_base_state scic_sds_port_state_table[] = {
1731 [SCI_PORT_STOPPED] = {
1732 .enter_state = scic_sds_port_stopped_state_enter,
1733 .exit_state = scic_sds_port_stopped_state_exit
1735 [SCI_PORT_STOPPING] = {
1736 .exit_state = scic_sds_port_stopping_state_exit
1738 [SCI_PORT_READY] = {
1739 .enter_state = scic_sds_port_ready_state_enter,
1741 [SCI_PORT_SUB_WAITING] = {
1742 .enter_state = scic_sds_port_ready_substate_waiting_enter,
1744 [SCI_PORT_SUB_OPERATIONAL] = {
1745 .enter_state = scic_sds_port_ready_substate_operational_enter,
1746 .exit_state = scic_sds_port_ready_substate_operational_exit
1748 [SCI_PORT_SUB_CONFIGURING] = {
1749 .enter_state = scic_sds_port_ready_substate_configuring_enter,
1750 .exit_state = scic_sds_port_ready_substate_configuring_exit
1752 [SCI_PORT_RESETTING] = {
1753 .exit_state = scic_sds_port_resetting_state_exit
1755 [SCI_PORT_FAILED] = {
1756 .enter_state = scic_sds_port_failed_state_enter,
1760 void scic_sds_port_construct(struct isci_port *iport, u8 index,
1761 struct scic_sds_controller *scic)
1763 sci_init_sm(&iport->sm, scic_sds_port_state_table, SCI_PORT_STOPPED);
1765 iport->logical_port_index = SCIC_SDS_DUMMY_PORT;
1766 iport->physical_port_index = index;
1767 iport->active_phy_mask = 0;
1768 iport->ready_exit = false;
1770 iport->owning_controller = scic;
1772 iport->started_request_count = 0;
1773 iport->assigned_device_count = 0;
1775 iport->reserved_rni = SCU_DUMMY_INDEX;
1776 iport->reserved_tag = SCI_CONTROLLER_INVALID_IO_TAG;
1778 sci_init_timer(&iport->timer, port_timeout);
1780 iport->port_task_scheduler_registers = NULL;
1782 for (index = 0; index < SCI_MAX_PHYS; index++)
1783 iport->phy_table[index] = NULL;
1786 void isci_port_init(struct isci_port *iport, struct isci_host *ihost, int index)
1788 INIT_LIST_HEAD(&iport->remote_dev_list);
1789 INIT_LIST_HEAD(&iport->domain_dev_list);
1790 spin_lock_init(&iport->state_lock);
1791 init_completion(&iport->start_complete);
1792 iport->isci_host = ihost;
1793 isci_port_change_state(iport, isci_freed);
1794 atomic_set(&iport->event, 0);
1798 * isci_port_get_state() - This function gets the status of the port object.
1799 * @isci_port: This parameter points to the isci_port object
1801 * status of the object as a isci_status enum.
1803 enum isci_status isci_port_get_state(
1804 struct isci_port *isci_port)
1806 return isci_port->status;
1809 void scic_sds_port_broadcast_change_received(
1810 struct isci_port *iport,
1811 struct isci_phy *iphy)
1813 struct scic_sds_controller *scic = iport->owning_controller;
1814 struct isci_host *ihost = scic_to_ihost(scic);
1816 /* notify the user. */
1817 isci_port_bc_change_received(ihost, iport, iphy);
1820 int isci_port_perform_hard_reset(struct isci_host *ihost, struct isci_port *iport,
1821 struct isci_phy *iphy)
1823 unsigned long flags;
1824 enum sci_status status;
1825 int idx, ret = TMF_RESP_FUNC_COMPLETE;
1827 dev_dbg(&ihost->pdev->dev, "%s: iport = %p\n",
1828 __func__, iport);
1830 init_completion(&iport->hard_reset_complete);
1832 spin_lock_irqsave(&ihost->scic_lock, flags);
1834 #define ISCI_PORT_RESET_TIMEOUT SCIC_SDS_SIGNATURE_FIS_TIMEOUT
1835 status = scic_port_hard_reset(iport, ISCI_PORT_RESET_TIMEOUT);
1837 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1839 if (status == SCI_SUCCESS) {
1840 wait_for_completion(&iport->hard_reset_complete);
1842 dev_dbg(&ihost->pdev->dev,
1843 "%s: iport = %p; hard reset completion\n",
1844 __func__, iport);
1846 if (iport->hard_reset_status != SCI_SUCCESS)
1847 ret = TMF_RESP_FUNC_FAILED;
1848 } else {
1849 ret = TMF_RESP_FUNC_FAILED;
1851 dev_err(&ihost->pdev->dev,
1852 "%s: iport = %p; scic_port_hard_reset call"
1853 " failed 0x%x\n",
1854 __func__, iport, status);
1858 /* If the hard reset for the port has failed, consider this
1859 * the same as link failures on all phys in the port.
1861 if (ret != TMF_RESP_FUNC_COMPLETE) {
1863 dev_err(&ihost->pdev->dev,
1864 "%s: iport = %p; hard reset failed "
1865 "(0x%x) - driving explicit link fail for all phys\n",
1866 __func__, iport, iport->hard_reset_status);
1868 /* Down all phys in the port. */
1869 spin_lock_irqsave(&ihost->scic_lock, flags);
1870 for (idx = 0; idx < SCI_MAX_PHYS; ++idx) {
1871 struct isci_phy *iphy = iport->phy_table[idx];
1873 if (!iphy)
1874 continue;
1875 scic_sds_phy_stop(iphy);
1876 scic_sds_phy_start(iphy);
1878 spin_unlock_irqrestore(&ihost->scic_lock, flags);
1880 return ret;
1884 * isci_port_deformed() - This function is called by libsas when a port becomes
1885 * inactive.
1886 * @phy: This parameter specifies the libsas phy with the inactive port.
1889 void isci_port_deformed(struct asd_sas_phy *phy)
1891 pr_debug("%s: sas_phy = %p\n", __func__, phy);
1895 * isci_port_formed() - This function is called by libsas when a port becomes
1896 * active.
1897 * @phy: This parameter specifies the libsas phy with the active port.
1900 void isci_port_formed(struct asd_sas_phy *phy)
1902 pr_debug("%s: sas_phy = %p, sas_port = %p\n", __func__, phy, phy->port);