[SCSI] libsas: poll for ata device readiness after reset
[linux-2.6.git] / drivers / scsi / libsas / sas_expander.c
index eca83e8..32e417e 100644 (file)
 
 #include <linux/scatterlist.h>
 #include <linux/blkdev.h>
+#include <linux/slab.h>
 
 #include "sas_internal.h"
 
+#include <scsi/sas_ata.h>
 #include <scsi/scsi_transport.h>
 #include <scsi/scsi_transport_sas.h>
 #include "../scsi_sas_internal.h"
@@ -70,11 +72,13 @@ static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
        struct sas_internal *i =
                to_sas_internal(dev->port->ha->core.shost->transportt);
 
+       mutex_lock(&dev->ex_dev.cmd_mutex);
        for (retry = 0; retry < 3; retry++) {
                task = sas_alloc_task(GFP_KERNEL);
-               if (!task)
-                       return -ENOMEM;
-
+               if (!task) {
+                       res = -ENOMEM;
+                       break;
+               }
                task->dev = dev;
                task->task_proto = dev->tproto;
                sg_init_one(&task->smp_task.smp_req, req, req_size);
@@ -92,26 +96,42 @@ static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
                if (res) {
                        del_timer(&task->timer);
                        SAS_DPRINTK("executing SMP task failed:%d\n", res);
-                       goto ex_err;
+                       break;
                }
 
                wait_for_completion(&task->completion);
-               res = -ETASK;
+               res = -ECOMM;
                if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
                        SAS_DPRINTK("smp task timed out or aborted\n");
                        i->dft->lldd_abort_task(task);
                        if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
                                SAS_DPRINTK("SMP task aborted and not done\n");
-                               goto ex_err;
+                               break;
                        }
                }
                if (task->task_status.resp == SAS_TASK_COMPLETE &&
-                   task->task_status.stat == SAM_GOOD) {
+                   task->task_status.stat == SAM_STAT_GOOD) {
                        res = 0;
                        break;
-               } else {
+               }
+               if (task->task_status.resp == SAS_TASK_COMPLETE &&
+                   task->task_status.stat == SAS_DATA_UNDERRUN) {
+                       /* no error, but return the number of bytes of
+                        * underrun */
+                       res = task->task_status.residual;
+                       break;
+               }
+               if (task->task_status.resp == SAS_TASK_COMPLETE &&
+                   task->task_status.stat == SAS_DATA_OVERRUN) {
+                       res = -EMSGSIZE;
+                       break;
+               }
+               if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
+                   task->task_status.stat == SAS_DEVICE_UNKNOWN)
+                       break;
+               else {
                        SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
-                                   "status 0x%x\n", __FUNCTION__,
+                                   "status 0x%x\n", __func__,
                                    SAS_ADDR(dev->sas_addr),
                                    task->task_status.resp,
                                    task->task_status.stat);
@@ -119,11 +139,10 @@ static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
                        task = NULL;
                }
        }
-ex_err:
+       mutex_unlock(&dev->ex_dev.cmd_mutex);
+
        BUG_ON(retry == 3 && task != NULL);
-       if (task != NULL) {
-               sas_free_task(task);
-       }
+       sas_free_task(task);
        return res;
 }
 
@@ -164,10 +183,10 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
        switch (resp->result) {
        case SMP_RESP_PHY_VACANT:
                phy->phy_state = PHY_VACANT;
-               return;
+               break;
        default:
                phy->phy_state = PHY_NOT_PRESENT;
-               return;
+               break;
        case SMP_RESP_FUNC_ACC:
                phy->phy_state = PHY_EMPTY; /* do not know yet */
                break;
@@ -188,6 +207,8 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
        phy->virtual = dr->virtual;
        phy->last_da_index = -1;
 
+       phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
+       phy->phy->identify.device_type = phy->attached_dev_type;
        phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
        phy->phy->identify.target_port_protocols = phy->attached_tproto;
        phy->phy->identify.phy_identifier = phy_id;
@@ -198,7 +219,10 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
        phy->phy->negotiated_linkrate = phy->linkrate;
 
        if (!rediscover)
-               sas_phy_add(phy->phy);
+               if (sas_phy_add(phy->phy)) {
+                       sas_phy_free(phy->phy);
+                       return;
+               }
 
        SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
                    SAS_ADDR(dev->sas_addr), phy->phy_id,
@@ -210,12 +234,35 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
        return;
 }
 
+/* check if we have an existing attached ata device on this expander phy */
+struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
+{
+       struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
+       struct domain_device *dev;
+       struct sas_rphy *rphy;
+
+       if (!ex_phy->port)
+               return NULL;
+
+       rphy = ex_phy->port->rphy;
+       if (!rphy)
+               return NULL;
+
+       dev = sas_find_dev_by_rphy(rphy);
+
+       if (dev && dev_is_sata(dev))
+               return dev;
+
+       return NULL;
+}
+
 #define DISCOVER_REQ_SIZE  16
 #define DISCOVER_RESP_SIZE 56
 
 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
                                      u8 *disc_resp, int single)
 {
+       struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
        int i, res;
 
        disc_req[9] = single;
@@ -226,15 +273,30 @@ static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
                                       disc_resp, DISCOVER_RESP_SIZE);
                if (res)
                        return res;
-               /* This is detecting a failure to transmit inital
-                * dev to host FIS as described in section G.5 of
-                * sas-2 r 04b */
                dr = &((struct smp_resp *)disc_resp)->disc;
+               if (memcmp(dev->sas_addr, dr->attached_sas_addr,
+                         SAS_ADDR_SIZE) == 0) {
+                       sas_printk("Found loopback topology, just ignore it!\n");
+                       return 0;
+               }
+
+               /* This is detecting a failure to transmit initial
+                * dev to host FIS as described in section J.5 of
+                * sas-2 r16
+                */
                if (!(dr->attached_dev_type == 0 &&
                      dr->attached_sata_dev))
                        break;
-               /* In order to generate the dev to host FIS, we
-                * send a link reset to the expander port */
+
+               /* In order to generate the dev to host FIS, we send a
+                * link reset to the expander port.  If a device was
+                * previously detected on this port we ask libata to
+                * manage the reset and link recovery.
+                */
+               if (ata_dev) {
+                       sas_ata_schedule_reset(ata_dev);
+                       break;
+               }
                sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
                /* Wait for the reset to trigger the negotiation */
                msleep(500);
@@ -310,6 +372,7 @@ static void ex_assign_report_general(struct domain_device *dev,
        dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
        dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
        dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
+       dev->ex_dev.t2t_supp = rg->t2t_supp;
        dev->ex_dev.conf_route_table = rg->conf_route_table;
        dev->ex_dev.configuring = rg->configuring;
        memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
@@ -507,14 +570,21 @@ static int sas_dev_present_in_domain(struct asd_sas_port *port,
 int sas_smp_get_phy_events(struct sas_phy *phy)
 {
        int res;
+       u8 *req;
+       u8 *resp;
        struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
        struct domain_device *dev = sas_find_dev_by_rphy(rphy);
-       u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
-       u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
 
-       if (!resp)
+       req = alloc_smp_req(RPEL_REQ_SIZE);
+       if (!req)
                return -ENOMEM;
 
+       resp = alloc_smp_resp(RPEL_RESP_SIZE);
+       if (!resp) {
+               kfree(req);
+               return -ENOMEM;
+       }
+
        req[1] = SMP_REPORT_PHY_ERR_LOG;
        req[9] = phy->number;
 
@@ -535,6 +605,8 @@ int sas_smp_get_phy_events(struct sas_phy *phy)
 
 }
 
+#ifdef CONFIG_SCSI_SAS_ATA
+
 #define RPS_REQ_SIZE  16
 #define RPS_RESP_SIZE 60
 
@@ -578,6 +650,7 @@ static int sas_get_report_phy_sata(struct domain_device *dev,
        kfree(rps_req);
        return res;
 }
+#endif
 
 static void sas_ex_get_linkrate(struct domain_device *parent,
                                       struct domain_device *child,
@@ -625,10 +698,11 @@ static struct domain_device *sas_ex_discover_end_dev(
        if (phy->attached_sata_host || phy->attached_sata_ps)
                return NULL;
 
-       child = kzalloc(sizeof(*child), GFP_KERNEL);
+       child = sas_alloc_device();
        if (!child)
                return NULL;
 
+       kref_get(&parent->kref);
        child->parent = parent;
        child->port   = parent->port;
        child->iproto = phy->attached_iproto;
@@ -645,9 +719,10 @@ static struct domain_device *sas_ex_discover_end_dev(
        }
        sas_ex_get_linkrate(parent, child, phy);
 
-       if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
+#ifdef CONFIG_SCSI_SAS_ATA
+       if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
                child->dev_type = SATA_DEV;
-               if (phy->attached_tproto & SAS_PROTO_STP)
+               if (phy->attached_tproto & SAS_PROTOCOL_STP)
                        child->tproto = phy->attached_tproto;
                if (phy->attached_sata_dev)
                        child->tproto |= SATA_DEV;
@@ -670,9 +745,7 @@ static struct domain_device *sas_ex_discover_end_dev(
 
                child->rphy = rphy;
 
-               spin_lock_irq(&parent->port->dev_list_lock);
-               list_add_tail(&child->dev_list_node, &parent->port->dev_list);
-               spin_unlock_irq(&parent->port->dev_list_lock);
+               list_add_tail(&child->disco_list_node, &parent->port->disco_list);
 
                res = sas_discover_sata(child);
                if (res) {
@@ -682,7 +755,9 @@ static struct domain_device *sas_ex_discover_end_dev(
                                    SAS_ADDR(parent->sas_addr), phy_id, res);
                        goto out_list_del;
                }
-       } else if (phy->attached_tproto & SAS_PROTO_SSP) {
+       } else
+#endif
+         if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
                child->dev_type = SAS_END_DEV;
                rphy = sas_end_device_alloc(phy->port);
                /* FIXME: error handling */
@@ -710,6 +785,7 @@ static struct domain_device *sas_ex_discover_end_dev(
                SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
                            phy->attached_tproto, SAS_ADDR(parent->sas_addr),
                            phy_id);
+               goto out_free;
        }
 
        list_add_tail(&child->siblings, &parent_ex->children);
@@ -718,12 +794,16 @@ static struct domain_device *sas_ex_discover_end_dev(
  out_list_del:
        sas_rphy_free(child->rphy);
        child->rphy = NULL;
+
+       list_del(&child->disco_list_node);
+       spin_lock_irq(&parent->port->dev_list_lock);
        list_del(&child->dev_list_node);
+       spin_unlock_irq(&parent->port->dev_list_lock);
  out_free:
        sas_port_delete(phy->port);
  out_err:
        phy->port = NULL;
-       kfree(child);
+       sas_put_device(child);
        return NULL;
 }
 
@@ -742,6 +822,7 @@ static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
                if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
                            SAS_ADDR_SIZE) && ephy->port) {
                        sas_port_add_phy(ephy->port, phy->phy);
+                       phy->port = ephy->port;
                        phy->phy_state = PHY_DEVICE_DISCOVERED;
                        return 0;
                }
@@ -769,7 +850,7 @@ static struct domain_device *sas_ex_discover_expander(
                            phy->attached_phy_id);
                return NULL;
        }
-       child = kzalloc(sizeof(*child), GFP_KERNEL);
+       child = sas_alloc_device();
        if (!child)
                return NULL;
 
@@ -795,6 +876,7 @@ static struct domain_device *sas_ex_discover_expander(
        child->rphy = rphy;
        edev = rphy_to_expander_device(rphy);
        child->dev_type = phy->attached_dev_type;
+       kref_get(&parent->kref);
        child->parent = parent;
        child->port = port;
        child->iproto = phy->attached_iproto;
@@ -815,7 +897,10 @@ static struct domain_device *sas_ex_discover_expander(
 
        res = sas_discover_expander(child);
        if (res) {
-               kfree(child);
+               spin_lock_irq(&parent->port->dev_list_lock);
+               list_del(&child->dev_list_node);
+               spin_unlock_irq(&parent->port->dev_list_lock);
+               sas_put_device(child);
                return NULL;
        }
        list_add_tail(&child->siblings, &parent->ex_dev.children);
@@ -921,10 +1006,19 @@ static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
                        if (ex->ex_phy[i].phy_state == PHY_VACANT ||
                            ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
                                continue;
-
+                       /*
+                        * Due to races, the phy might not get added to the
+                        * wide port, so we add the phy to the wide port here.
+                        */
                        if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
-                           SAS_ADDR(child->sas_addr))
+                           SAS_ADDR(child->sas_addr)) {
                                ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
+                               res = sas_ex_join_wide_port(dev, i);
+                               if (!res)
+                                       SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
+                                                   i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
+
+                       }
                }
        }
 
@@ -1087,15 +1181,17 @@ static void sas_print_parent_topology_bug(struct domain_device *child,
        };
        struct domain_device *parent = child->parent;
 
-       sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
-                  "has %c:%c routing link!\n",
+       sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
+                  "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
 
                   ex_type[parent->dev_type],
                   SAS_ADDR(parent->sas_addr),
+                  parent->ex_dev.t2t_supp,
                   parent_phy->phy_id,
 
                   ex_type[child->dev_type],
                   SAS_ADDR(child->sas_addr),
+                  child->ex_dev.t2t_supp,
                   child_phy->phy_id,
 
                   ra_char[parent_phy->routing_attr],
@@ -1192,10 +1288,15 @@ static int sas_check_parent_topology(struct domain_device *child)
                                        sas_print_parent_topology_bug(child, parent_phy, child_phy);
                                        res = -ENODEV;
                                }
-                       } else if (parent_phy->routing_attr == TABLE_ROUTING &&
-                                  child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
-                               sas_print_parent_topology_bug(child, parent_phy, child_phy);
-                               res = -ENODEV;
+                       } else if (parent_phy->routing_attr == TABLE_ROUTING) {
+                               if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
+                                   (child_phy->routing_attr == TABLE_ROUTING &&
+                                    child_ex->t2t_supp && parent_ex->t2t_supp)) {
+                                       /* All good */;
+                               } else {
+                                       sas_print_parent_topology_bug(child, parent_phy, child_phy);
+                                       res = -ENODEV;
+                               }
                        }
                        break;
                case FANOUT_DEV:
@@ -1255,7 +1356,7 @@ static int sas_configure_present(struct domain_device *dev, int phy_id,
                        goto out;
                } else if (res != SMP_RESP_FUNC_ACC) {
                        SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
-                                   "result 0x%x\n", __FUNCTION__,
+                                   "result 0x%x\n", __func__,
                                    SAS_ADDR(dev->sas_addr), phy_id, i, res);
                        goto out;
                }
@@ -1551,8 +1652,8 @@ static int sas_get_phy_change_count(struct domain_device *dev,
        return res;
 }
 
-static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
-                                        int phy_id, u8 *attached_sas_addr)
+int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
+                                 u8 *attached_sas_addr)
 {
        int res;
        struct smp_resp *disc_resp;
@@ -1574,7 +1675,7 @@ static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
 }
 
 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
-                             int from_phy)
+                             int from_phy, bool update)
 {
        struct expander_device *ex = &dev->ex_dev;
        int res = 0;
@@ -1587,7 +1688,9 @@ static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
                if (res)
                        goto out;
                else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
-                       ex->ex_phy[i].phy_change_count = phy_change_count;
+                       if (update)
+                               ex->ex_phy[i].phy_change_count =
+                                       phy_change_count;
                        *phy_id = i;
                        return 0;
                }
@@ -1629,76 +1732,104 @@ out:
        kfree(rg_req);
        return res;
 }
+/**
+ * sas_find_bcast_dev -  find the device issue BROADCAST(CHANGE).
+ * @dev:domain device to be detect.
+ * @src_dev: the device which originated BROADCAST(CHANGE).
+ *
+ * Add self-configuration expander suport. Suppose two expander cascading,
+ * when the first level expander is self-configuring, hotplug the disks in
+ * second level expander, BROADCAST(CHANGE) will not only be originated
+ * in the second level expander, but also be originated in the first level
+ * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
+ * expander changed count in two level expanders will all increment at least
+ * once, but the phy which chang count has changed is the source device which
+ * we concerned.
+ */
 
 static int sas_find_bcast_dev(struct domain_device *dev,
                              struct domain_device **src_dev)
 {
        struct expander_device *ex = &dev->ex_dev;
        int ex_change_count = -1;
+       int phy_id = -1;
        int res;
+       struct domain_device *ch;
 
        res = sas_get_ex_change_count(dev, &ex_change_count);
        if (res)
                goto out;
-       if (ex_change_count != -1 &&
-           ex_change_count != ex->ex_change_count) {
-               *src_dev = dev;
-               ex->ex_change_count = ex_change_count;
-       } else {
-               struct domain_device *ch;
-
-               list_for_each_entry(ch, &ex->children, siblings) {
-                       if (ch->dev_type == EDGE_DEV ||
-                           ch->dev_type == FANOUT_DEV) {
-                               res = sas_find_bcast_dev(ch, src_dev);
-                               if (src_dev)
-                                       return res;
-                       }
+       if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
+               /* Just detect if this expander phys phy change count changed,
+               * in order to determine if this expander originate BROADCAST,
+               * and do not update phy change count field in our structure.
+               */
+               res = sas_find_bcast_phy(dev, &phy_id, 0, false);
+               if (phy_id != -1) {
+                       *src_dev = dev;
+                       ex->ex_change_count = ex_change_count;
+                       SAS_DPRINTK("Expander phy change count has changed\n");
+                       return res;
+               } else
+                       SAS_DPRINTK("Expander phys DID NOT change\n");
+       }
+       list_for_each_entry(ch, &ex->children, siblings) {
+               if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
+                       res = sas_find_bcast_dev(ch, src_dev);
+                       if (*src_dev)
+                               return res;
                }
        }
 out:
        return res;
 }
 
-static void sas_unregister_ex_tree(struct domain_device *dev)
+static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
 {
        struct expander_device *ex = &dev->ex_dev;
        struct domain_device *child, *n;
 
        list_for_each_entry_safe(child, n, &ex->children, siblings) {
+               set_bit(SAS_DEV_GONE, &child->state);
                if (child->dev_type == EDGE_DEV ||
                    child->dev_type == FANOUT_DEV)
-                       sas_unregister_ex_tree(child);
+                       sas_unregister_ex_tree(port, child);
                else
-                       sas_unregister_dev(child);
+                       sas_unregister_dev(port, child);
        }
-       sas_unregister_dev(dev);
+       sas_unregister_dev(port, dev);
 }
 
 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
-                                        int phy_id)
+                                        int phy_id, bool last)
 {
        struct expander_device *ex_dev = &parent->ex_dev;
        struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
        struct domain_device *child, *n;
-
-       list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
-               if (SAS_ADDR(child->sas_addr) ==
-                   SAS_ADDR(phy->attached_sas_addr)) {
-                       if (child->dev_type == EDGE_DEV ||
-                           child->dev_type == FANOUT_DEV)
-                               sas_unregister_ex_tree(child);
-                       else
-                               sas_unregister_dev(child);
-                       break;
+       if (last) {
+               list_for_each_entry_safe(child, n,
+                       &ex_dev->children, siblings) {
+                       if (SAS_ADDR(child->sas_addr) ==
+                           SAS_ADDR(phy->attached_sas_addr)) {
+                               set_bit(SAS_DEV_GONE, &child->state);
+                               if (child->dev_type == EDGE_DEV ||
+                                   child->dev_type == FANOUT_DEV)
+                                       sas_unregister_ex_tree(parent->port, child);
+                               else
+                                       sas_unregister_dev(parent->port, child);
+                               break;
+                       }
                }
+               set_bit(SAS_DEV_GONE, &parent->state);
+               sas_disable_routing(parent, phy->attached_sas_addr);
        }
-       sas_disable_routing(parent, phy->attached_sas_addr);
        memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
-       sas_port_delete_phy(phy->port, phy->phy);
-       if (phy->port->num_phys == 0)
-               sas_port_delete(phy->port);
-       phy->port = NULL;
+       if (phy->port) {
+               sas_port_delete_phy(phy->port, phy->phy);
+               if (phy->port->num_phys == 0)
+                       sas_port_delete(phy->port);
+               phy->port = NULL;
+       }
 }
 
 static int sas_discover_bfs_by_root_level(struct domain_device *root,
@@ -1746,15 +1877,31 @@ static int sas_discover_new(struct domain_device *dev, int phy_id)
 {
        struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
        struct domain_device *child;
-       int res;
+       bool found = false;
+       int res, i;
 
        SAS_DPRINTK("ex %016llx phy%d new device attached\n",
                    SAS_ADDR(dev->sas_addr), phy_id);
        res = sas_ex_phy_discover(dev, phy_id);
        if (res)
                goto out;
+       /* to support the wide port inserted */
+       for (i = 0; i < dev->ex_dev.num_phys; i++) {
+               struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
+               if (i == phy_id)
+                       continue;
+               if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
+                   SAS_ADDR(ex_phy->attached_sas_addr)) {
+                       found = true;
+                       break;
+               }
+       }
+       if (found) {
+               sas_ex_join_wide_port(dev, phy_id);
+               return 0;
+       }
        res = sas_ex_discover_devices(dev, phy_id);
-       if (res)
+       if (!res)
                goto out;
        list_for_each_entry(child, &dev->ex_dev.children, siblings) {
                if (SAS_ADDR(child->sas_addr) ==
@@ -1769,7 +1916,7 @@ out:
        return res;
 }
 
-static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
+static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
 {
        struct expander_device *ex = &dev->ex_dev;
        struct ex_phy *phy = &ex->ex_phy[phy_id];
@@ -1780,11 +1927,11 @@ static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
        switch (res) {
        case SMP_RESP_NO_PHY:
                phy->phy_state = PHY_NOT_PRESENT;
-               sas_unregister_devs_sas_addr(dev, phy_id);
+               sas_unregister_devs_sas_addr(dev, phy_id, last);
                goto out; break;
        case SMP_RESP_PHY_VACANT:
                phy->phy_state = PHY_VACANT;
-               sas_unregister_devs_sas_addr(dev, phy_id);
+               sas_unregister_devs_sas_addr(dev, phy_id, last);
                goto out; break;
        case SMP_RESP_FUNC_ACC:
                break;
@@ -1792,7 +1939,7 @@ static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
 
        if (SAS_ADDR(attached_sas_addr) == 0) {
                phy->phy_state = PHY_EMPTY;
-               sas_unregister_devs_sas_addr(dev, phy_id);
+               sas_unregister_devs_sas_addr(dev, phy_id, last);
        } else if (SAS_ADDR(attached_sas_addr) ==
                   SAS_ADDR(phy->attached_sas_addr)) {
                SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
@@ -1804,12 +1951,27 @@ out:
        return res;
 }
 
+/**
+ * sas_rediscover - revalidate the domain.
+ * @dev:domain device to be detect.
+ * @phy_id: the phy id will be detected.
+ *
+ * NOTE: this process _must_ quit (return) as soon as any connection
+ * errors are encountered.  Connection recovery is done elsewhere.
+ * Discover process only interrogates devices in order to discover the
+ * domain.For plugging out, we un-register the device only when it is
+ * the last phy in the port, for other phys in this port, we just delete it
+ * from the port.For inserting, we do discovery when it is the
+ * first phy,for other phys in this port, we add it to the port to
+ * forming the wide-port.
+ */
 static int sas_rediscover(struct domain_device *dev, const int phy_id)
 {
        struct expander_device *ex = &dev->ex_dev;
        struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
        int res = 0;
        int i;
+       bool last = true;       /* is this the last phy of the port */
 
        SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
                    SAS_ADDR(dev->sas_addr), phy_id);
@@ -1824,13 +1986,13 @@ static int sas_rediscover(struct domain_device *dev, const int phy_id)
                            SAS_ADDR(changed_phy->attached_sas_addr)) {
                                SAS_DPRINTK("phy%d part of wide port with "
                                            "phy%d\n", phy_id, i);
-                               goto out;
+                               last = false;
+                               break;
                        }
                }
-               res = sas_rediscover_dev(dev, phy_id);
+               res = sas_rediscover_dev(dev, phy_id, last);
        } else
                res = sas_discover_new(dev, phy_id);
-out:
        return res;
 }
 
@@ -1857,7 +2019,7 @@ int sas_ex_revalidate_domain(struct domain_device *port_dev)
 
                do {
                        phy_id = -1;
-                       res = sas_find_bcast_phy(dev, &phy_id, i);
+                       res = sas_find_bcast_phy(dev, &phy_id, i, true);
                        if (phy_id == -1)
                                break;
                        res = sas_rediscover(dev, phy_id);
@@ -1872,45 +2034,53 @@ int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
                    struct request *req)
 {
        struct domain_device *dev;
-       int ret, type = rphy->identify.device_type;
+       int ret, type;
        struct request *rsp = req->next_rq;
 
        if (!rsp) {
                printk("%s: space for a smp response is missing\n",
-                      __FUNCTION__);
+                      __func__);
                return -EINVAL;
        }
 
-       /* seems aic94xx doesn't support */
-       if (!rphy) {
-               printk("%s: can we send a smp request to a host?\n",
-                      __FUNCTION__);
-               return -EINVAL;
-       }
+       /* no rphy means no smp target support (ie aic94xx host) */
+       if (!rphy)
+               return sas_smp_host_handler(shost, req, rsp);
+
+       type = rphy->identify.device_type;
 
        if (type != SAS_EDGE_EXPANDER_DEVICE &&
            type != SAS_FANOUT_EXPANDER_DEVICE) {
                printk("%s: can we send a smp request to a device?\n",
-                      __FUNCTION__);
+                      __func__);
                return -EINVAL;
        }
 
        dev = sas_find_dev_by_rphy(rphy);
        if (!dev) {
-               printk("%s: fail to find a domain_device?\n", __FUNCTION__);
+               printk("%s: fail to find a domain_device?\n", __func__);
                return -EINVAL;
        }
 
        /* do we need to support multiple segments? */
        if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
                printk("%s: multiple segments req %u %u, rsp %u %u\n",
-                      __FUNCTION__, req->bio->bi_vcnt, req->data_len,
-                      rsp->bio->bi_vcnt, rsp->data_len);
+                      __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
+                      rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
                return -EINVAL;
        }
 
-       ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
-                              bio_data(rsp->bio), rsp->data_len);
+       ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
+                              bio_data(rsp->bio), blk_rq_bytes(rsp));
+       if (ret > 0) {
+               /* positive number is the untransferred residual */
+               rsp->resid_len = ret;
+               req->resid_len = 0;
+               ret = 0;
+       } else if (ret == 0) {
+               rsp->resid_len = 0;
+               req->resid_len = 0;
+       }
 
        return ret;
 }