isci: Adding support for phy enable and disable

Adding support for PHY_FUNC_LINK_RESET and PHY_FUNC_DISABLE. This allow the
sysfs knob enable (both 0 and 1) and link_reset to work properly.

Signed-off-by: Dave Jiang <dave.jiang@intel.com>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
diff --git a/drivers/scsi/isci/core/scic_sds_phy.c b/drivers/scsi/isci/core/scic_sds_phy.c
index ba9e557..6f00ff6 100644
--- a/drivers/scsi/isci/core/scic_sds_phy.c
+++ b/drivers/scsi/isci/core/scic_sds_phy.c
@@ -537,27 +537,25 @@
 /**
  * This method will attempt to start the phy object. This request is only valid
  *    when the phy is in the stopped state
- * @this_phy:
+ * @sci_phy:
  *
  * enum sci_status
  */
-enum sci_status scic_sds_phy_start(
-	struct scic_sds_phy *this_phy)
+enum sci_status scic_sds_phy_start(struct scic_sds_phy *sci_phy)
 {
-	return this_phy->state_handlers->parent.start_handler(&this_phy->parent);
+	return sci_phy->state_handlers->parent.start_handler(&sci_phy->parent);
 }
 
 /**
  * This method will attempt to stop the phy object.
- * @this_phy:
+ * @sci_phy:
  *
- * enum sci_status SCI_SUCCESS if the phy is going to stop SCI_INVALID_STATE if the
- * phy is not in a valid state to stop
+ * enum sci_status SCI_SUCCESS if the phy is going to stop SCI_INVALID_STATE
+ * if the phy is not in a valid state to stop
  */
-enum sci_status scic_sds_phy_stop(
-	struct scic_sds_phy *this_phy)
+enum sci_status scic_sds_phy_stop(struct scic_sds_phy *sci_phy)
 {
-	return this_phy->state_handlers->parent.stop_handler(&this_phy->parent);
+	return sci_phy->state_handlers->parent.stop_handler(&sci_phy->parent);
 }
 
 /**
@@ -2526,7 +2524,8 @@
 
 /**
  *
- * @object: This is the struct sci_base_object which is cast to a struct scic_sds_phy object.
+ * @object: This is the struct sci_base_object which is cast to a
+ * struct scic_sds_phy object.
  *
  * This method will perform the actions required by the struct scic_sds_phy on
  * entering the SCI_BASE_PHY_STATE_INITIAL. - This function sets the state
@@ -2539,7 +2538,10 @@
 
 	sci_phy = (struct scic_sds_phy *)object;
 
-	/* / @todo We need to get to the controller to place this PE in a reset state */
+	/*
+	 * @todo We need to get to the controller to place this PE in a
+	 * reset state
+	 */
 
 	scic_sds_phy_set_base_state_handlers(sci_phy, SCI_BASE_PHY_STATE_STOPPED);
 
@@ -2551,6 +2553,12 @@
 	}
 
 	scu_link_layer_stop_protocol_engine(sci_phy);
+
+	if (sci_phy->parent.state_machine.previous_state_id !=
+			SCI_BASE_PHY_STATE_INITIAL)
+		scic_sds_controller_link_down(scic_sds_phy_get_controller(sci_phy),
+					      scic_sds_phy_get_port(sci_phy),
+					      sci_phy);
 }
 
 /**
diff --git a/drivers/scsi/isci/phy.c b/drivers/scsi/isci/phy.c
index 1eefaae..decc0c0 100644
--- a/drivers/scsi/isci/phy.c
+++ b/drivers/scsi/isci/phy.c
@@ -58,6 +58,9 @@
 #include "scic_port.h"
 #include "scic_config_parameters.h"
 
+struct scic_sds_phy;
+extern enum sci_status scic_sds_phy_start(struct scic_sds_phy *sci_phy);
+extern enum sci_status scic_sds_phy_stop(struct scic_sds_phy *sci_phy);
 
 /**
  * isci_phy_init() - This function is called by the probe function to
@@ -127,42 +130,48 @@
  *
  * status, zero indicates success.
  */
-int isci_phy_control(
-	struct asd_sas_phy *phy,
-	enum phy_func func,
-	void *buf)
+int isci_phy_control(struct asd_sas_phy *sas_phy,
+		     enum phy_func func,
+		     void *buf)
 {
-	int ret            = TMF_RESP_FUNC_COMPLETE;
-	struct isci_phy *isci_phy_ptr  = (struct isci_phy *)phy->lldd_phy;
-	struct isci_port *isci_port_ptr = NULL;
+	int ret = 0;
+	struct isci_phy *iphy = sas_phy->lldd_phy;
+	struct isci_port *iport = iphy->isci_port;
+	struct isci_host *ihost = sas_phy->ha->lldd_ha;
+	unsigned long flags;
 
-	if (isci_phy_ptr != NULL)
-		isci_port_ptr = isci_phy_ptr->isci_port;
-
-	if ((isci_phy_ptr == NULL) || (isci_port_ptr == NULL)) {
-		pr_err("%s: asd_sas_phy %p: lldd_phy %p or "
-		       "isci_port %p == NULL!\n",
-		       __func__, phy, isci_phy_ptr, isci_port_ptr);
-		return TMF_RESP_FUNC_FAILED;
-	}
-
-	pr_debug("%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
-		 __func__, phy, func, buf, isci_phy_ptr, isci_port_ptr);
+	dev_dbg(&ihost->pdev->dev,
+		"%s: phy %p; func %d; buf %p; isci phy %p, port %p\n",
+		__func__, sas_phy, func, buf, iphy, iport);
 
 	switch (func) {
-	case PHY_FUNC_HARD_RESET:
+	case PHY_FUNC_DISABLE:
+		spin_lock_irqsave(&ihost->scic_lock, flags);
+		scic_sds_phy_stop(iphy->sci_phy_handle);
+		spin_unlock_irqrestore(&ihost->scic_lock, flags);
+		break;
+
 	case PHY_FUNC_LINK_RESET:
+		spin_lock_irqsave(&ihost->scic_lock, flags);
+		scic_sds_phy_stop(iphy->sci_phy_handle);
+		scic_sds_phy_start(iphy->sci_phy_handle);
+		spin_unlock_irqrestore(&ihost->scic_lock, flags);
+		break;
+
+	case PHY_FUNC_HARD_RESET:
+		if (!iport)
+			return -ENODEV;
 
 		/* Perform the port reset. */
-		ret = isci_port_perform_hard_reset(isci_port_ptr, isci_phy_ptr);
+		ret = isci_port_perform_hard_reset(iport, iphy);
 
 		break;
 
-	case PHY_FUNC_DISABLE:
 	default:
-		pr_debug("%s: phy %p; func %d NOT IMPLEMENTED!\n",
-			 __func__, phy, func);
-		ret = TMF_RESP_FUNC_FAILED;
+		dev_dbg(&ihost->pdev->dev,
+			   "%s: phy %p; func %d NOT IMPLEMENTED!\n",
+			   __func__, sas_phy, func);
+		ret = -ENOSYS;
 		break;
 	}
 	return ret;