2 * Serial Attached SCSI (SAS) Expander discovery and configuration
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <linux/scatterlist.h>
26 #include <linux/blkdev.h>
27 #include <linux/slab.h>
29 #include "sas_internal.h"
31 #include <scsi/scsi_transport.h>
32 #include <scsi/scsi_transport_sas.h>
33 #include "../scsi_sas_internal.h"
35 static int sas_discover_expander(struct domain_device *dev);
36 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
37 static int sas_configure_phy(struct domain_device *dev, int phy_id,
38 u8 *sas_addr, int include);
39 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
41 /* ---------- SMP task management ---------- */
43 static void smp_task_timedout(unsigned long _task)
45 struct sas_task *task = (void *) _task;
48 spin_lock_irqsave(&task->task_state_lock, flags);
49 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
50 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
51 spin_unlock_irqrestore(&task->task_state_lock, flags);
53 complete(&task->completion);
56 static void smp_task_done(struct sas_task *task)
58 if (!del_timer(&task->timer))
60 complete(&task->completion);
63 /* Give it some long enough timeout. In seconds. */
64 #define SMP_TIMEOUT 10
66 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
67 void *resp, int resp_size)
70 struct sas_task *task = NULL;
71 struct sas_internal *i =
72 to_sas_internal(dev->port->ha->core.shost->transportt);
74 for (retry = 0; retry < 3; retry++) {
75 task = sas_alloc_task(GFP_KERNEL);
80 task->task_proto = dev->tproto;
81 sg_init_one(&task->smp_task.smp_req, req, req_size);
82 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
84 task->task_done = smp_task_done;
86 task->timer.data = (unsigned long) task;
87 task->timer.function = smp_task_timedout;
88 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
89 add_timer(&task->timer);
91 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
94 del_timer(&task->timer);
95 SAS_DPRINTK("executing SMP task failed:%d\n", res);
99 wait_for_completion(&task->completion);
101 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
102 SAS_DPRINTK("smp task timed out or aborted\n");
103 i->dft->lldd_abort_task(task);
104 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
105 SAS_DPRINTK("SMP task aborted and not done\n");
109 if (task->task_status.resp == SAS_TASK_COMPLETE &&
110 task->task_status.stat == SAM_STAT_GOOD) {
113 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
114 task->task_status.stat == SAS_DATA_UNDERRUN) {
115 /* no error, but return the number of bytes of
117 res = task->task_status.residual;
119 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
120 task->task_status.stat == SAS_DATA_OVERRUN) {
124 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
125 "status 0x%x\n", __func__,
126 SAS_ADDR(dev->sas_addr),
127 task->task_status.resp,
128 task->task_status.stat);
134 BUG_ON(retry == 3 && task != NULL);
141 /* ---------- Allocations ---------- */
143 static inline void *alloc_smp_req(int size)
145 u8 *p = kzalloc(size, GFP_KERNEL);
151 static inline void *alloc_smp_resp(int size)
153 return kzalloc(size, GFP_KERNEL);
156 /* ---------- Expander configuration ---------- */
158 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
161 struct expander_device *ex = &dev->ex_dev;
162 struct ex_phy *phy = &ex->ex_phy[phy_id];
163 struct smp_resp *resp = disc_resp;
164 struct discover_resp *dr = &resp->disc;
165 struct sas_rphy *rphy = dev->rphy;
166 int rediscover = (phy->phy != NULL);
169 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
171 /* FIXME: error_handling */
175 switch (resp->result) {
176 case SMP_RESP_PHY_VACANT:
177 phy->phy_state = PHY_VACANT;
180 phy->phy_state = PHY_NOT_PRESENT;
182 case SMP_RESP_FUNC_ACC:
183 phy->phy_state = PHY_EMPTY; /* do not know yet */
187 phy->phy_id = phy_id;
188 phy->attached_dev_type = dr->attached_dev_type;
189 phy->linkrate = dr->linkrate;
190 phy->attached_sata_host = dr->attached_sata_host;
191 phy->attached_sata_dev = dr->attached_sata_dev;
192 phy->attached_sata_ps = dr->attached_sata_ps;
193 phy->attached_iproto = dr->iproto << 1;
194 phy->attached_tproto = dr->tproto << 1;
195 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
196 phy->attached_phy_id = dr->attached_phy_id;
197 phy->phy_change_count = dr->change_count;
198 phy->routing_attr = dr->routing_attr;
199 phy->virtual = dr->virtual;
200 phy->last_da_index = -1;
202 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
203 phy->phy->identify.target_port_protocols = phy->attached_tproto;
204 phy->phy->identify.phy_identifier = phy_id;
205 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
206 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
207 phy->phy->minimum_linkrate = dr->pmin_linkrate;
208 phy->phy->maximum_linkrate = dr->pmax_linkrate;
209 phy->phy->negotiated_linkrate = phy->linkrate;
212 if (sas_phy_add(phy->phy)) {
213 sas_phy_free(phy->phy);
217 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
218 SAS_ADDR(dev->sas_addr), phy->phy_id,
219 phy->routing_attr == TABLE_ROUTING ? 'T' :
220 phy->routing_attr == DIRECT_ROUTING ? 'D' :
221 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
222 SAS_ADDR(phy->attached_sas_addr));
227 #define DISCOVER_REQ_SIZE 16
228 #define DISCOVER_RESP_SIZE 56
230 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
231 u8 *disc_resp, int single)
235 disc_req[9] = single;
236 for (i = 1 ; i < 3; i++) {
237 struct discover_resp *dr;
239 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
240 disc_resp, DISCOVER_RESP_SIZE);
243 /* This is detecting a failure to transmit inital
244 * dev to host FIS as described in section G.5 of
246 dr = &((struct smp_resp *)disc_resp)->disc;
247 if (!(dr->attached_dev_type == 0 &&
248 dr->attached_sata_dev))
250 /* In order to generate the dev to host FIS, we
251 * send a link reset to the expander port */
252 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
253 /* Wait for the reset to trigger the negotiation */
256 sas_set_ex_phy(dev, single, disc_resp);
260 static int sas_ex_phy_discover(struct domain_device *dev, int single)
262 struct expander_device *ex = &dev->ex_dev;
267 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
271 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
277 disc_req[1] = SMP_DISCOVER;
279 if (0 <= single && single < ex->num_phys) {
280 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
284 for (i = 0; i < ex->num_phys; i++) {
285 res = sas_ex_phy_discover_helper(dev, disc_req,
297 static int sas_expander_discover(struct domain_device *dev)
299 struct expander_device *ex = &dev->ex_dev;
302 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
306 res = sas_ex_phy_discover(dev, -1);
317 #define MAX_EXPANDER_PHYS 128
319 static void ex_assign_report_general(struct domain_device *dev,
320 struct smp_resp *resp)
322 struct report_general_resp *rg = &resp->rg;
324 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
325 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
326 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
327 dev->ex_dev.conf_route_table = rg->conf_route_table;
328 dev->ex_dev.configuring = rg->configuring;
329 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
332 #define RG_REQ_SIZE 8
333 #define RG_RESP_SIZE 32
335 static int sas_ex_general(struct domain_device *dev)
338 struct smp_resp *rg_resp;
342 rg_req = alloc_smp_req(RG_REQ_SIZE);
346 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
352 rg_req[1] = SMP_REPORT_GENERAL;
354 for (i = 0; i < 5; i++) {
355 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
359 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
360 SAS_ADDR(dev->sas_addr), res);
362 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
363 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
364 SAS_ADDR(dev->sas_addr), rg_resp->result);
365 res = rg_resp->result;
369 ex_assign_report_general(dev, rg_resp);
371 if (dev->ex_dev.configuring) {
372 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
373 SAS_ADDR(dev->sas_addr));
374 schedule_timeout_interruptible(5*HZ);
384 static void ex_assign_manuf_info(struct domain_device *dev, void
387 u8 *mi_resp = _mi_resp;
388 struct sas_rphy *rphy = dev->rphy;
389 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
391 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
392 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
393 memcpy(edev->product_rev, mi_resp + 36,
394 SAS_EXPANDER_PRODUCT_REV_LEN);
396 if (mi_resp[8] & 1) {
397 memcpy(edev->component_vendor_id, mi_resp + 40,
398 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
399 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
400 edev->component_revision_id = mi_resp[50];
404 #define MI_REQ_SIZE 8
405 #define MI_RESP_SIZE 64
407 static int sas_ex_manuf_info(struct domain_device *dev)
413 mi_req = alloc_smp_req(MI_REQ_SIZE);
417 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
423 mi_req[1] = SMP_REPORT_MANUF_INFO;
425 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
427 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
428 SAS_ADDR(dev->sas_addr), res);
430 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
431 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
432 SAS_ADDR(dev->sas_addr), mi_resp[2]);
436 ex_assign_manuf_info(dev, mi_resp);
443 #define PC_REQ_SIZE 44
444 #define PC_RESP_SIZE 8
446 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
447 enum phy_func phy_func,
448 struct sas_phy_linkrates *rates)
454 pc_req = alloc_smp_req(PC_REQ_SIZE);
458 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
464 pc_req[1] = SMP_PHY_CONTROL;
466 pc_req[10]= phy_func;
468 pc_req[32] = rates->minimum_linkrate << 4;
469 pc_req[33] = rates->maximum_linkrate << 4;
472 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
479 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
481 struct expander_device *ex = &dev->ex_dev;
482 struct ex_phy *phy = &ex->ex_phy[phy_id];
484 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
485 phy->linkrate = SAS_PHY_DISABLED;
488 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
490 struct expander_device *ex = &dev->ex_dev;
493 for (i = 0; i < ex->num_phys; i++) {
494 struct ex_phy *phy = &ex->ex_phy[i];
496 if (phy->phy_state == PHY_VACANT ||
497 phy->phy_state == PHY_NOT_PRESENT)
500 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
501 sas_ex_disable_phy(dev, i);
505 static int sas_dev_present_in_domain(struct asd_sas_port *port,
508 struct domain_device *dev;
510 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
512 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
513 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
519 #define RPEL_REQ_SIZE 16
520 #define RPEL_RESP_SIZE 32
521 int sas_smp_get_phy_events(struct sas_phy *phy)
526 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
527 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
529 req = alloc_smp_req(RPEL_REQ_SIZE);
533 resp = alloc_smp_resp(RPEL_RESP_SIZE);
539 req[1] = SMP_REPORT_PHY_ERR_LOG;
540 req[9] = phy->number;
542 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
543 resp, RPEL_RESP_SIZE);
548 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
549 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
550 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
551 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
559 #ifdef CONFIG_SCSI_SAS_ATA
561 #define RPS_REQ_SIZE 16
562 #define RPS_RESP_SIZE 60
564 static int sas_get_report_phy_sata(struct domain_device *dev,
566 struct smp_resp *rps_resp)
569 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
570 u8 *resp = (u8 *)rps_resp;
575 rps_req[1] = SMP_REPORT_PHY_SATA;
578 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
579 rps_resp, RPS_RESP_SIZE);
581 /* 0x34 is the FIS type for the D2H fis. There's a potential
582 * standards cockup here. sas-2 explicitly specifies the FIS
583 * should be encoded so that FIS type is in resp[24].
584 * However, some expanders endian reverse this. Undo the
586 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
589 for (i = 0; i < 5; i++) {
594 resp[j + 0] = resp[j + 3];
595 resp[j + 1] = resp[j + 2];
606 static void sas_ex_get_linkrate(struct domain_device *parent,
607 struct domain_device *child,
608 struct ex_phy *parent_phy)
610 struct expander_device *parent_ex = &parent->ex_dev;
611 struct sas_port *port;
616 port = parent_phy->port;
618 for (i = 0; i < parent_ex->num_phys; i++) {
619 struct ex_phy *phy = &parent_ex->ex_phy[i];
621 if (phy->phy_state == PHY_VACANT ||
622 phy->phy_state == PHY_NOT_PRESENT)
625 if (SAS_ADDR(phy->attached_sas_addr) ==
626 SAS_ADDR(child->sas_addr)) {
628 child->min_linkrate = min(parent->min_linkrate,
630 child->max_linkrate = max(parent->max_linkrate,
633 sas_port_add_phy(port, phy->phy);
636 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
637 child->pathways = min(child->pathways, parent->pathways);
640 static struct domain_device *sas_ex_discover_end_dev(
641 struct domain_device *parent, int phy_id)
643 struct expander_device *parent_ex = &parent->ex_dev;
644 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
645 struct domain_device *child = NULL;
646 struct sas_rphy *rphy;
649 if (phy->attached_sata_host || phy->attached_sata_ps)
652 child = kzalloc(sizeof(*child), GFP_KERNEL);
656 child->parent = parent;
657 child->port = parent->port;
658 child->iproto = phy->attached_iproto;
659 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
660 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
662 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
663 if (unlikely(!phy->port))
665 if (unlikely(sas_port_add(phy->port) != 0)) {
666 sas_port_free(phy->port);
670 sas_ex_get_linkrate(parent, child, phy);
672 #ifdef CONFIG_SCSI_SAS_ATA
673 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
674 child->dev_type = SATA_DEV;
675 if (phy->attached_tproto & SAS_PROTOCOL_STP)
676 child->tproto = phy->attached_tproto;
677 if (phy->attached_sata_dev)
678 child->tproto |= SATA_DEV;
679 res = sas_get_report_phy_sata(parent, phy_id,
680 &child->sata_dev.rps_resp);
682 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
683 "0x%x\n", SAS_ADDR(parent->sas_addr),
687 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
688 sizeof(struct dev_to_host_fis));
690 rphy = sas_end_device_alloc(phy->port);
698 spin_lock_irq(&parent->port->dev_list_lock);
699 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
700 spin_unlock_irq(&parent->port->dev_list_lock);
702 res = sas_discover_sata(child);
704 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
705 "%016llx:0x%x returned 0x%x\n",
706 SAS_ADDR(child->sas_addr),
707 SAS_ADDR(parent->sas_addr), phy_id, res);
712 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
713 child->dev_type = SAS_END_DEV;
714 rphy = sas_end_device_alloc(phy->port);
715 /* FIXME: error handling */
718 child->tproto = phy->attached_tproto;
722 sas_fill_in_rphy(child, rphy);
724 spin_lock_irq(&parent->port->dev_list_lock);
725 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
726 spin_unlock_irq(&parent->port->dev_list_lock);
728 res = sas_discover_end_dev(child);
730 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
731 "at %016llx:0x%x returned 0x%x\n",
732 SAS_ADDR(child->sas_addr),
733 SAS_ADDR(parent->sas_addr), phy_id, res);
737 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
738 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
743 list_add_tail(&child->siblings, &parent_ex->children);
747 sas_rphy_free(child->rphy);
749 list_del(&child->dev_list_node);
751 sas_port_delete(phy->port);
758 /* See if this phy is part of a wide port */
759 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
761 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
764 for (i = 0; i < parent->ex_dev.num_phys; i++) {
765 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
770 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
771 SAS_ADDR_SIZE) && ephy->port) {
772 sas_port_add_phy(ephy->port, phy->phy);
773 phy->port = ephy->port;
774 phy->phy_state = PHY_DEVICE_DISCOVERED;
782 static struct domain_device *sas_ex_discover_expander(
783 struct domain_device *parent, int phy_id)
785 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
786 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
787 struct domain_device *child = NULL;
788 struct sas_rphy *rphy;
789 struct sas_expander_device *edev;
790 struct asd_sas_port *port;
793 if (phy->routing_attr == DIRECT_ROUTING) {
794 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
796 SAS_ADDR(parent->sas_addr), phy_id,
797 SAS_ADDR(phy->attached_sas_addr),
798 phy->attached_phy_id);
801 child = kzalloc(sizeof(*child), GFP_KERNEL);
805 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
806 /* FIXME: better error handling */
807 BUG_ON(sas_port_add(phy->port) != 0);
810 switch (phy->attached_dev_type) {
812 rphy = sas_expander_alloc(phy->port,
813 SAS_EDGE_EXPANDER_DEVICE);
816 rphy = sas_expander_alloc(phy->port,
817 SAS_FANOUT_EXPANDER_DEVICE);
820 rphy = NULL; /* shut gcc up */
825 edev = rphy_to_expander_device(rphy);
826 child->dev_type = phy->attached_dev_type;
827 child->parent = parent;
829 child->iproto = phy->attached_iproto;
830 child->tproto = phy->attached_tproto;
831 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
832 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
833 sas_ex_get_linkrate(parent, child, phy);
834 edev->level = parent_ex->level + 1;
835 parent->port->disc.max_level = max(parent->port->disc.max_level,
838 sas_fill_in_rphy(child, rphy);
841 spin_lock_irq(&parent->port->dev_list_lock);
842 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
843 spin_unlock_irq(&parent->port->dev_list_lock);
845 res = sas_discover_expander(child);
850 list_add_tail(&child->siblings, &parent->ex_dev.children);
854 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
856 struct expander_device *ex = &dev->ex_dev;
857 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
858 struct domain_device *child = NULL;
862 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
863 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
864 res = sas_ex_phy_discover(dev, phy_id);
869 /* Parent and domain coherency */
870 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
871 SAS_ADDR(dev->port->sas_addr))) {
872 sas_add_parent_port(dev, phy_id);
875 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
876 SAS_ADDR(dev->parent->sas_addr))) {
877 sas_add_parent_port(dev, phy_id);
878 if (ex_phy->routing_attr == TABLE_ROUTING)
879 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
883 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
884 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
886 if (ex_phy->attached_dev_type == NO_DEVICE) {
887 if (ex_phy->routing_attr == DIRECT_ROUTING) {
888 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
889 sas_configure_routing(dev, ex_phy->attached_sas_addr);
892 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
895 if (ex_phy->attached_dev_type != SAS_END_DEV &&
896 ex_phy->attached_dev_type != FANOUT_DEV &&
897 ex_phy->attached_dev_type != EDGE_DEV) {
898 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
899 "phy 0x%x\n", ex_phy->attached_dev_type,
900 SAS_ADDR(dev->sas_addr),
905 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
907 SAS_DPRINTK("configure routing for dev %016llx "
908 "reported 0x%x. Forgotten\n",
909 SAS_ADDR(ex_phy->attached_sas_addr), res);
910 sas_disable_routing(dev, ex_phy->attached_sas_addr);
914 res = sas_ex_join_wide_port(dev, phy_id);
916 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
917 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
921 switch (ex_phy->attached_dev_type) {
923 child = sas_ex_discover_end_dev(dev, phy_id);
926 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
927 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
928 "attached to ex %016llx phy 0x%x\n",
929 SAS_ADDR(ex_phy->attached_sas_addr),
930 ex_phy->attached_phy_id,
931 SAS_ADDR(dev->sas_addr),
933 sas_ex_disable_phy(dev, phy_id);
936 memcpy(dev->port->disc.fanout_sas_addr,
937 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
940 child = sas_ex_discover_expander(dev, phy_id);
949 for (i = 0; i < ex->num_phys; i++) {
950 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
951 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
954 * Due to races, the phy might not get added to the
955 * wide port, so we add the phy to the wide port here.
957 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
958 SAS_ADDR(child->sas_addr)) {
959 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
960 res = sas_ex_join_wide_port(dev, i);
962 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
963 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
972 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
974 struct expander_device *ex = &dev->ex_dev;
977 for (i = 0; i < ex->num_phys; i++) {
978 struct ex_phy *phy = &ex->ex_phy[i];
980 if (phy->phy_state == PHY_VACANT ||
981 phy->phy_state == PHY_NOT_PRESENT)
984 if ((phy->attached_dev_type == EDGE_DEV ||
985 phy->attached_dev_type == FANOUT_DEV) &&
986 phy->routing_attr == SUBTRACTIVE_ROUTING) {
988 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
996 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
998 struct expander_device *ex = &dev->ex_dev;
999 struct domain_device *child;
1000 u8 sub_addr[8] = {0, };
1002 list_for_each_entry(child, &ex->children, siblings) {
1003 if (child->dev_type != EDGE_DEV &&
1004 child->dev_type != FANOUT_DEV)
1006 if (sub_addr[0] == 0) {
1007 sas_find_sub_addr(child, sub_addr);
1012 if (sas_find_sub_addr(child, s2) &&
1013 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1015 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1016 "diverges from subtractive "
1017 "boundary %016llx\n",
1018 SAS_ADDR(dev->sas_addr),
1019 SAS_ADDR(child->sas_addr),
1021 SAS_ADDR(sub_addr));
1023 sas_ex_disable_port(child, s2);
1030 * sas_ex_discover_devices -- discover devices attached to this expander
1031 * dev: pointer to the expander domain device
1032 * single: if you want to do a single phy, else set to -1;
1034 * Configure this expander for use with its devices and register the
1035 * devices of this expander.
1037 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1039 struct expander_device *ex = &dev->ex_dev;
1040 int i = 0, end = ex->num_phys;
1043 if (0 <= single && single < end) {
1048 for ( ; i < end; i++) {
1049 struct ex_phy *ex_phy = &ex->ex_phy[i];
1051 if (ex_phy->phy_state == PHY_VACANT ||
1052 ex_phy->phy_state == PHY_NOT_PRESENT ||
1053 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1056 switch (ex_phy->linkrate) {
1057 case SAS_PHY_DISABLED:
1058 case SAS_PHY_RESET_PROBLEM:
1059 case SAS_SATA_PORT_SELECTOR:
1062 res = sas_ex_discover_dev(dev, i);
1070 sas_check_level_subtractive_boundary(dev);
1075 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1077 struct expander_device *ex = &dev->ex_dev;
1079 u8 *sub_sas_addr = NULL;
1081 if (dev->dev_type != EDGE_DEV)
1084 for (i = 0; i < ex->num_phys; i++) {
1085 struct ex_phy *phy = &ex->ex_phy[i];
1087 if (phy->phy_state == PHY_VACANT ||
1088 phy->phy_state == PHY_NOT_PRESENT)
1091 if ((phy->attached_dev_type == FANOUT_DEV ||
1092 phy->attached_dev_type == EDGE_DEV) &&
1093 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1096 sub_sas_addr = &phy->attached_sas_addr[0];
1097 else if (SAS_ADDR(sub_sas_addr) !=
1098 SAS_ADDR(phy->attached_sas_addr)) {
1100 SAS_DPRINTK("ex %016llx phy 0x%x "
1101 "diverges(%016llx) on subtractive "
1102 "boundary(%016llx). Disabled\n",
1103 SAS_ADDR(dev->sas_addr), i,
1104 SAS_ADDR(phy->attached_sas_addr),
1105 SAS_ADDR(sub_sas_addr));
1106 sas_ex_disable_phy(dev, i);
1113 static void sas_print_parent_topology_bug(struct domain_device *child,
1114 struct ex_phy *parent_phy,
1115 struct ex_phy *child_phy)
1117 static const char ra_char[] = {
1118 [DIRECT_ROUTING] = 'D',
1119 [SUBTRACTIVE_ROUTING] = 'S',
1120 [TABLE_ROUTING] = 'T',
1122 static const char *ex_type[] = {
1123 [EDGE_DEV] = "edge",
1124 [FANOUT_DEV] = "fanout",
1126 struct domain_device *parent = child->parent;
1128 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1129 "has %c:%c routing link!\n",
1131 ex_type[parent->dev_type],
1132 SAS_ADDR(parent->sas_addr),
1135 ex_type[child->dev_type],
1136 SAS_ADDR(child->sas_addr),
1139 ra_char[parent_phy->routing_attr],
1140 ra_char[child_phy->routing_attr]);
1143 static int sas_check_eeds(struct domain_device *child,
1144 struct ex_phy *parent_phy,
1145 struct ex_phy *child_phy)
1148 struct domain_device *parent = child->parent;
1150 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1152 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1153 "phy S:0x%x, while there is a fanout ex %016llx\n",
1154 SAS_ADDR(parent->sas_addr),
1156 SAS_ADDR(child->sas_addr),
1158 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1159 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1160 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1162 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1164 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1165 SAS_ADDR(parent->sas_addr)) ||
1166 (SAS_ADDR(parent->port->disc.eeds_a) ==
1167 SAS_ADDR(child->sas_addr)))
1169 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1170 SAS_ADDR(parent->sas_addr)) ||
1171 (SAS_ADDR(parent->port->disc.eeds_b) ==
1172 SAS_ADDR(child->sas_addr))))
1176 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1177 "phy 0x%x link forms a third EEDS!\n",
1178 SAS_ADDR(parent->sas_addr),
1180 SAS_ADDR(child->sas_addr),
1187 /* Here we spill over 80 columns. It is intentional.
1189 static int sas_check_parent_topology(struct domain_device *child)
1191 struct expander_device *child_ex = &child->ex_dev;
1192 struct expander_device *parent_ex;
1199 if (child->parent->dev_type != EDGE_DEV &&
1200 child->parent->dev_type != FANOUT_DEV)
1203 parent_ex = &child->parent->ex_dev;
1205 for (i = 0; i < parent_ex->num_phys; i++) {
1206 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1207 struct ex_phy *child_phy;
1209 if (parent_phy->phy_state == PHY_VACANT ||
1210 parent_phy->phy_state == PHY_NOT_PRESENT)
1213 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1216 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1218 switch (child->parent->dev_type) {
1220 if (child->dev_type == FANOUT_DEV) {
1221 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1222 child_phy->routing_attr != TABLE_ROUTING) {
1223 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1226 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1227 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1228 res = sas_check_eeds(child, parent_phy, child_phy);
1229 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1230 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1233 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1234 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1235 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1240 if (parent_phy->routing_attr != TABLE_ROUTING ||
1241 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1242 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1254 #define RRI_REQ_SIZE 16
1255 #define RRI_RESP_SIZE 44
1257 static int sas_configure_present(struct domain_device *dev, int phy_id,
1258 u8 *sas_addr, int *index, int *present)
1261 struct expander_device *ex = &dev->ex_dev;
1262 struct ex_phy *phy = &ex->ex_phy[phy_id];
1269 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1273 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1279 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1280 rri_req[9] = phy_id;
1282 for (i = 0; i < ex->max_route_indexes ; i++) {
1283 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1284 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1289 if (res == SMP_RESP_NO_INDEX) {
1290 SAS_DPRINTK("overflow of indexes: dev %016llx "
1291 "phy 0x%x index 0x%x\n",
1292 SAS_ADDR(dev->sas_addr), phy_id, i);
1294 } else if (res != SMP_RESP_FUNC_ACC) {
1295 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1296 "result 0x%x\n", __func__,
1297 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1300 if (SAS_ADDR(sas_addr) != 0) {
1301 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1303 if ((rri_resp[12] & 0x80) == 0x80)
1308 } else if (SAS_ADDR(rri_resp+16) == 0) {
1313 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1314 phy->last_da_index < i) {
1315 phy->last_da_index = i;
1328 #define CRI_REQ_SIZE 44
1329 #define CRI_RESP_SIZE 8
1331 static int sas_configure_set(struct domain_device *dev, int phy_id,
1332 u8 *sas_addr, int index, int include)
1338 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1342 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1348 cri_req[1] = SMP_CONF_ROUTE_INFO;
1349 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1350 cri_req[9] = phy_id;
1351 if (SAS_ADDR(sas_addr) == 0 || !include)
1352 cri_req[12] |= 0x80;
1353 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1355 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1360 if (res == SMP_RESP_NO_INDEX) {
1361 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1363 SAS_ADDR(dev->sas_addr), phy_id, index);
1371 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1372 u8 *sas_addr, int include)
1378 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1381 if (include ^ present)
1382 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1388 * sas_configure_parent -- configure routing table of parent
1389 * parent: parent expander
1390 * child: child expander
1391 * sas_addr: SAS port identifier of device directly attached to child
1393 static int sas_configure_parent(struct domain_device *parent,
1394 struct domain_device *child,
1395 u8 *sas_addr, int include)
1397 struct expander_device *ex_parent = &parent->ex_dev;
1401 if (parent->parent) {
1402 res = sas_configure_parent(parent->parent, parent, sas_addr,
1408 if (ex_parent->conf_route_table == 0) {
1409 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1410 SAS_ADDR(parent->sas_addr));
1414 for (i = 0; i < ex_parent->num_phys; i++) {
1415 struct ex_phy *phy = &ex_parent->ex_phy[i];
1417 if ((phy->routing_attr == TABLE_ROUTING) &&
1418 (SAS_ADDR(phy->attached_sas_addr) ==
1419 SAS_ADDR(child->sas_addr))) {
1420 res = sas_configure_phy(parent, i, sas_addr, include);
1430 * sas_configure_routing -- configure routing
1431 * dev: expander device
1432 * sas_addr: port identifier of device directly attached to the expander device
1434 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1437 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1441 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1444 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1449 * sas_discover_expander -- expander discovery
1450 * @ex: pointer to expander domain device
1452 * See comment in sas_discover_sata().
1454 static int sas_discover_expander(struct domain_device *dev)
1458 res = sas_notify_lldd_dev_found(dev);
1462 res = sas_ex_general(dev);
1465 res = sas_ex_manuf_info(dev);
1469 res = sas_expander_discover(dev);
1471 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1472 SAS_ADDR(dev->sas_addr), res);
1476 sas_check_ex_subtractive_boundary(dev);
1477 res = sas_check_parent_topology(dev);
1482 sas_notify_lldd_dev_gone(dev);
1486 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1489 struct domain_device *dev;
1491 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1492 if (dev->dev_type == EDGE_DEV ||
1493 dev->dev_type == FANOUT_DEV) {
1494 struct sas_expander_device *ex =
1495 rphy_to_expander_device(dev->rphy);
1497 if (level == ex->level)
1498 res = sas_ex_discover_devices(dev, -1);
1500 res = sas_ex_discover_devices(port->port_dev, -1);
1508 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1514 level = port->disc.max_level;
1515 res = sas_ex_level_discovery(port, level);
1517 } while (level < port->disc.max_level);
1522 int sas_discover_root_expander(struct domain_device *dev)
1525 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1527 res = sas_rphy_add(dev->rphy);
1531 ex->level = dev->port->disc.max_level; /* 0 */
1532 res = sas_discover_expander(dev);
1536 sas_ex_bfs_disc(dev->port);
1541 sas_rphy_remove(dev->rphy);
1546 /* ---------- Domain revalidation ---------- */
1548 static int sas_get_phy_discover(struct domain_device *dev,
1549 int phy_id, struct smp_resp *disc_resp)
1554 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1558 disc_req[1] = SMP_DISCOVER;
1559 disc_req[9] = phy_id;
1561 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1562 disc_resp, DISCOVER_RESP_SIZE);
1565 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1566 res = disc_resp->result;
1574 static int sas_get_phy_change_count(struct domain_device *dev,
1575 int phy_id, int *pcc)
1578 struct smp_resp *disc_resp;
1580 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1584 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1586 *pcc = disc_resp->disc.change_count;
1592 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1593 int phy_id, u8 *attached_sas_addr)
1596 struct smp_resp *disc_resp;
1597 struct discover_resp *dr;
1599 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1602 dr = &disc_resp->disc;
1604 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1606 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1607 if (dr->attached_dev_type == 0)
1608 memset(attached_sas_addr, 0, 8);
1614 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1615 int from_phy, bool update)
1617 struct expander_device *ex = &dev->ex_dev;
1621 for (i = from_phy; i < ex->num_phys; i++) {
1622 int phy_change_count = 0;
1624 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1627 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1629 ex->ex_phy[i].phy_change_count =
1639 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1643 struct smp_resp *rg_resp;
1645 rg_req = alloc_smp_req(RG_REQ_SIZE);
1649 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1655 rg_req[1] = SMP_REPORT_GENERAL;
1657 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1661 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1662 res = rg_resp->result;
1666 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1673 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1674 * @dev:domain device to be detect.
1675 * @src_dev: the device which originated BROADCAST(CHANGE).
1677 * Add self-configuration expander suport. Suppose two expander cascading,
1678 * when the first level expander is self-configuring, hotplug the disks in
1679 * second level expander, BROADCAST(CHANGE) will not only be originated
1680 * in the second level expander, but also be originated in the first level
1681 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1682 * expander changed count in two level expanders will all increment at least
1683 * once, but the phy which chang count has changed is the source device which
1687 static int sas_find_bcast_dev(struct domain_device *dev,
1688 struct domain_device **src_dev)
1690 struct expander_device *ex = &dev->ex_dev;
1691 int ex_change_count = -1;
1694 struct domain_device *ch;
1696 res = sas_get_ex_change_count(dev, &ex_change_count);
1699 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1700 /* Just detect if this expander phys phy change count changed,
1701 * in order to determine if this expander originate BROADCAST,
1702 * and do not update phy change count field in our structure.
1704 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1707 ex->ex_change_count = ex_change_count;
1708 SAS_DPRINTK("Expander phy change count has changed\n");
1711 SAS_DPRINTK("Expander phys DID NOT change\n");
1713 list_for_each_entry(ch, &ex->children, siblings) {
1714 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1715 res = sas_find_bcast_dev(ch, src_dev);
1724 static void sas_unregister_ex_tree(struct domain_device *dev)
1726 struct expander_device *ex = &dev->ex_dev;
1727 struct domain_device *child, *n;
1729 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1731 if (child->dev_type == EDGE_DEV ||
1732 child->dev_type == FANOUT_DEV)
1733 sas_unregister_ex_tree(child);
1735 sas_unregister_dev(child);
1737 sas_unregister_dev(dev);
1740 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1741 int phy_id, bool last)
1743 struct expander_device *ex_dev = &parent->ex_dev;
1744 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1745 struct domain_device *child, *n;
1747 list_for_each_entry_safe(child, n,
1748 &ex_dev->children, siblings) {
1749 if (SAS_ADDR(child->sas_addr) ==
1750 SAS_ADDR(phy->attached_sas_addr)) {
1752 if (child->dev_type == EDGE_DEV ||
1753 child->dev_type == FANOUT_DEV)
1754 sas_unregister_ex_tree(child);
1756 sas_unregister_dev(child);
1761 sas_disable_routing(parent, phy->attached_sas_addr);
1763 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1764 sas_port_delete_phy(phy->port, phy->phy);
1765 if (phy->port->num_phys == 0)
1766 sas_port_delete(phy->port);
1770 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1773 struct expander_device *ex_root = &root->ex_dev;
1774 struct domain_device *child;
1777 list_for_each_entry(child, &ex_root->children, siblings) {
1778 if (child->dev_type == EDGE_DEV ||
1779 child->dev_type == FANOUT_DEV) {
1780 struct sas_expander_device *ex =
1781 rphy_to_expander_device(child->rphy);
1783 if (level > ex->level)
1784 res = sas_discover_bfs_by_root_level(child,
1786 else if (level == ex->level)
1787 res = sas_ex_discover_devices(child, -1);
1793 static int sas_discover_bfs_by_root(struct domain_device *dev)
1796 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1797 int level = ex->level+1;
1799 res = sas_ex_discover_devices(dev, -1);
1803 res = sas_discover_bfs_by_root_level(dev, level);
1806 } while (level <= dev->port->disc.max_level);
1811 static int sas_discover_new(struct domain_device *dev, int phy_id)
1813 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1814 struct domain_device *child;
1818 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1819 SAS_ADDR(dev->sas_addr), phy_id);
1820 res = sas_ex_phy_discover(dev, phy_id);
1823 /* to support the wide port inserted */
1824 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1825 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1828 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1829 SAS_ADDR(ex_phy->attached_sas_addr)) {
1835 sas_ex_join_wide_port(dev, phy_id);
1838 res = sas_ex_discover_devices(dev, phy_id);
1841 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1842 if (SAS_ADDR(child->sas_addr) ==
1843 SAS_ADDR(ex_phy->attached_sas_addr)) {
1844 if (child->dev_type == EDGE_DEV ||
1845 child->dev_type == FANOUT_DEV)
1846 res = sas_discover_bfs_by_root(child);
1854 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1856 struct expander_device *ex = &dev->ex_dev;
1857 struct ex_phy *phy = &ex->ex_phy[phy_id];
1858 u8 attached_sas_addr[8];
1861 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1863 case SMP_RESP_NO_PHY:
1864 phy->phy_state = PHY_NOT_PRESENT;
1865 sas_unregister_devs_sas_addr(dev, phy_id, last);
1867 case SMP_RESP_PHY_VACANT:
1868 phy->phy_state = PHY_VACANT;
1869 sas_unregister_devs_sas_addr(dev, phy_id, last);
1871 case SMP_RESP_FUNC_ACC:
1875 if (SAS_ADDR(attached_sas_addr) == 0) {
1876 phy->phy_state = PHY_EMPTY;
1877 sas_unregister_devs_sas_addr(dev, phy_id, last);
1878 } else if (SAS_ADDR(attached_sas_addr) ==
1879 SAS_ADDR(phy->attached_sas_addr)) {
1880 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1881 SAS_ADDR(dev->sas_addr), phy_id);
1882 sas_ex_phy_discover(dev, phy_id);
1884 res = sas_discover_new(dev, phy_id);
1890 * sas_rediscover - revalidate the domain.
1891 * @dev:domain device to be detect.
1892 * @phy_id: the phy id will be detected.
1894 * NOTE: this process _must_ quit (return) as soon as any connection
1895 * errors are encountered. Connection recovery is done elsewhere.
1896 * Discover process only interrogates devices in order to discover the
1897 * domain.For plugging out, we un-register the device only when it is
1898 * the last phy in the port, for other phys in this port, we just delete it
1899 * from the port.For inserting, we do discovery when it is the
1900 * first phy,for other phys in this port, we add it to the port to
1901 * forming the wide-port.
1903 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1905 struct expander_device *ex = &dev->ex_dev;
1906 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1909 bool last = true; /* is this the last phy of the port */
1911 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1912 SAS_ADDR(dev->sas_addr), phy_id);
1914 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1915 for (i = 0; i < ex->num_phys; i++) {
1916 struct ex_phy *phy = &ex->ex_phy[i];
1920 if (SAS_ADDR(phy->attached_sas_addr) ==
1921 SAS_ADDR(changed_phy->attached_sas_addr)) {
1922 SAS_DPRINTK("phy%d part of wide port with "
1923 "phy%d\n", phy_id, i);
1928 res = sas_rediscover_dev(dev, phy_id, last);
1930 res = sas_discover_new(dev, phy_id);
1935 * sas_revalidate_domain -- revalidate the domain
1936 * @port: port to the domain of interest
1938 * NOTE: this process _must_ quit (return) as soon as any connection
1939 * errors are encountered. Connection recovery is done elsewhere.
1940 * Discover process only interrogates devices in order to discover the
1943 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1946 struct domain_device *dev = NULL;
1948 res = sas_find_bcast_dev(port_dev, &dev);
1952 struct expander_device *ex = &dev->ex_dev;
1957 res = sas_find_bcast_phy(dev, &phy_id, i, true);
1960 res = sas_rediscover(dev, phy_id);
1962 } while (i < ex->num_phys);
1968 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1969 struct request *req)
1971 struct domain_device *dev;
1973 struct request *rsp = req->next_rq;
1976 printk("%s: space for a smp response is missing\n",
1981 /* no rphy means no smp target support (ie aic94xx host) */
1983 return sas_smp_host_handler(shost, req, rsp);
1985 type = rphy->identify.device_type;
1987 if (type != SAS_EDGE_EXPANDER_DEVICE &&
1988 type != SAS_FANOUT_EXPANDER_DEVICE) {
1989 printk("%s: can we send a smp request to a device?\n",
1994 dev = sas_find_dev_by_rphy(rphy);
1996 printk("%s: fail to find a domain_device?\n", __func__);
2000 /* do we need to support multiple segments? */
2001 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2002 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2003 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2004 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2008 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2009 bio_data(rsp->bio), blk_rq_bytes(rsp));
2011 /* positive number is the untransferred residual */
2012 rsp->resid_len = ret;
2015 } else if (ret == 0) {