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>
28 #include "sas_internal.h"
30 #include <scsi/scsi_transport.h>
31 #include <scsi/scsi_transport_sas.h>
32 #include "../scsi_sas_internal.h"
34 static int sas_discover_expander(struct domain_device *dev);
35 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
36 static int sas_configure_phy(struct domain_device *dev, int phy_id,
37 u8 *sas_addr, int include);
38 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
40 /* ---------- SMP task management ---------- */
42 static void smp_task_timedout(unsigned long _task)
44 struct sas_task *task = (void *) _task;
47 spin_lock_irqsave(&task->task_state_lock, flags);
48 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
49 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
50 spin_unlock_irqrestore(&task->task_state_lock, flags);
52 complete(&task->completion);
55 static void smp_task_done(struct sas_task *task)
57 if (!del_timer(&task->timer))
59 complete(&task->completion);
62 /* Give it some long enough timeout. In seconds. */
63 #define SMP_TIMEOUT 10
65 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
66 void *resp, int resp_size)
69 struct sas_task *task = NULL;
70 struct sas_internal *i =
71 to_sas_internal(dev->port->ha->core.shost->transportt);
73 for (retry = 0; retry < 3; retry++) {
74 task = sas_alloc_task(GFP_KERNEL);
79 task->task_proto = dev->tproto;
80 sg_init_one(&task->smp_task.smp_req, req, req_size);
81 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
83 task->task_done = smp_task_done;
85 task->timer.data = (unsigned long) task;
86 task->timer.function = smp_task_timedout;
87 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
88 add_timer(&task->timer);
90 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
93 del_timer(&task->timer);
94 SAS_DPRINTK("executing SMP task failed:%d\n", res);
98 wait_for_completion(&task->completion);
100 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
101 SAS_DPRINTK("smp task timed out or aborted\n");
102 i->dft->lldd_abort_task(task);
103 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
104 SAS_DPRINTK("SMP task aborted and not done\n");
108 if (task->task_status.resp == SAS_TASK_COMPLETE &&
109 task->task_status.stat == SAM_GOOD) {
113 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
114 "status 0x%x\n", __FUNCTION__,
115 SAS_ADDR(dev->sas_addr),
116 task->task_status.resp,
117 task->task_status.stat);
123 BUG_ON(retry == 3 && task != NULL);
130 /* ---------- Allocations ---------- */
132 static inline void *alloc_smp_req(int size)
134 u8 *p = kzalloc(size, GFP_KERNEL);
140 static inline void *alloc_smp_resp(int size)
142 return kzalloc(size, GFP_KERNEL);
145 /* ---------- Expander configuration ---------- */
147 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
150 struct expander_device *ex = &dev->ex_dev;
151 struct ex_phy *phy = &ex->ex_phy[phy_id];
152 struct smp_resp *resp = disc_resp;
153 struct discover_resp *dr = &resp->disc;
154 struct sas_rphy *rphy = dev->rphy;
155 int rediscover = (phy->phy != NULL);
158 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
160 /* FIXME: error_handling */
164 switch (resp->result) {
165 case SMP_RESP_PHY_VACANT:
166 phy->phy_state = PHY_VACANT;
169 phy->phy_state = PHY_NOT_PRESENT;
171 case SMP_RESP_FUNC_ACC:
172 phy->phy_state = PHY_EMPTY; /* do not know yet */
176 phy->phy_id = phy_id;
177 phy->attached_dev_type = dr->attached_dev_type;
178 phy->linkrate = dr->linkrate;
179 phy->attached_sata_host = dr->attached_sata_host;
180 phy->attached_sata_dev = dr->attached_sata_dev;
181 phy->attached_sata_ps = dr->attached_sata_ps;
182 phy->attached_iproto = dr->iproto << 1;
183 phy->attached_tproto = dr->tproto << 1;
184 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
185 phy->attached_phy_id = dr->attached_phy_id;
186 phy->phy_change_count = dr->change_count;
187 phy->routing_attr = dr->routing_attr;
188 phy->virtual = dr->virtual;
189 phy->last_da_index = -1;
191 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
192 phy->phy->identify.target_port_protocols = phy->attached_tproto;
193 phy->phy->identify.phy_identifier = phy_id;
194 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
195 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
196 phy->phy->minimum_linkrate = dr->pmin_linkrate;
197 phy->phy->maximum_linkrate = dr->pmax_linkrate;
198 phy->phy->negotiated_linkrate = phy->linkrate;
201 sas_phy_add(phy->phy);
203 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
204 SAS_ADDR(dev->sas_addr), phy->phy_id,
205 phy->routing_attr == TABLE_ROUTING ? 'T' :
206 phy->routing_attr == DIRECT_ROUTING ? 'D' :
207 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
208 SAS_ADDR(phy->attached_sas_addr));
213 #define DISCOVER_REQ_SIZE 16
214 #define DISCOVER_RESP_SIZE 56
216 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
217 u8 *disc_resp, int single)
221 disc_req[9] = single;
222 for (i = 1 ; i < 3; i++) {
223 struct discover_resp *dr;
225 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
226 disc_resp, DISCOVER_RESP_SIZE);
229 /* This is detecting a failure to transmit inital
230 * dev to host FIS as described in section G.5 of
232 dr = &((struct smp_resp *)disc_resp)->disc;
233 if (!(dr->attached_dev_type == 0 &&
234 dr->attached_sata_dev))
236 /* In order to generate the dev to host FIS, we
237 * send a link reset to the expander port */
238 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
239 /* Wait for the reset to trigger the negotiation */
242 sas_set_ex_phy(dev, single, disc_resp);
246 static int sas_ex_phy_discover(struct domain_device *dev, int single)
248 struct expander_device *ex = &dev->ex_dev;
253 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
257 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
263 disc_req[1] = SMP_DISCOVER;
265 if (0 <= single && single < ex->num_phys) {
266 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
270 for (i = 0; i < ex->num_phys; i++) {
271 res = sas_ex_phy_discover_helper(dev, disc_req,
283 static int sas_expander_discover(struct domain_device *dev)
285 struct expander_device *ex = &dev->ex_dev;
288 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
292 res = sas_ex_phy_discover(dev, -1);
303 #define MAX_EXPANDER_PHYS 128
305 static void ex_assign_report_general(struct domain_device *dev,
306 struct smp_resp *resp)
308 struct report_general_resp *rg = &resp->rg;
310 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
311 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
312 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
313 dev->ex_dev.conf_route_table = rg->conf_route_table;
314 dev->ex_dev.configuring = rg->configuring;
315 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
318 #define RG_REQ_SIZE 8
319 #define RG_RESP_SIZE 32
321 static int sas_ex_general(struct domain_device *dev)
324 struct smp_resp *rg_resp;
328 rg_req = alloc_smp_req(RG_REQ_SIZE);
332 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
338 rg_req[1] = SMP_REPORT_GENERAL;
340 for (i = 0; i < 5; i++) {
341 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
345 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
346 SAS_ADDR(dev->sas_addr), res);
348 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
349 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
350 SAS_ADDR(dev->sas_addr), rg_resp->result);
351 res = rg_resp->result;
355 ex_assign_report_general(dev, rg_resp);
357 if (dev->ex_dev.configuring) {
358 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
359 SAS_ADDR(dev->sas_addr));
360 schedule_timeout_interruptible(5*HZ);
370 static void ex_assign_manuf_info(struct domain_device *dev, void
373 u8 *mi_resp = _mi_resp;
374 struct sas_rphy *rphy = dev->rphy;
375 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
377 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
378 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
379 memcpy(edev->product_rev, mi_resp + 36,
380 SAS_EXPANDER_PRODUCT_REV_LEN);
382 if (mi_resp[8] & 1) {
383 memcpy(edev->component_vendor_id, mi_resp + 40,
384 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
385 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
386 edev->component_revision_id = mi_resp[50];
390 #define MI_REQ_SIZE 8
391 #define MI_RESP_SIZE 64
393 static int sas_ex_manuf_info(struct domain_device *dev)
399 mi_req = alloc_smp_req(MI_REQ_SIZE);
403 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
409 mi_req[1] = SMP_REPORT_MANUF_INFO;
411 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
413 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
414 SAS_ADDR(dev->sas_addr), res);
416 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
417 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
418 SAS_ADDR(dev->sas_addr), mi_resp[2]);
422 ex_assign_manuf_info(dev, mi_resp);
429 #define PC_REQ_SIZE 44
430 #define PC_RESP_SIZE 8
432 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
433 enum phy_func phy_func,
434 struct sas_phy_linkrates *rates)
440 pc_req = alloc_smp_req(PC_REQ_SIZE);
444 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
450 pc_req[1] = SMP_PHY_CONTROL;
452 pc_req[10]= phy_func;
454 pc_req[32] = rates->minimum_linkrate << 4;
455 pc_req[33] = rates->maximum_linkrate << 4;
458 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
465 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
467 struct expander_device *ex = &dev->ex_dev;
468 struct ex_phy *phy = &ex->ex_phy[phy_id];
470 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
471 phy->linkrate = SAS_PHY_DISABLED;
474 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
476 struct expander_device *ex = &dev->ex_dev;
479 for (i = 0; i < ex->num_phys; i++) {
480 struct ex_phy *phy = &ex->ex_phy[i];
482 if (phy->phy_state == PHY_VACANT ||
483 phy->phy_state == PHY_NOT_PRESENT)
486 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
487 sas_ex_disable_phy(dev, i);
491 static int sas_dev_present_in_domain(struct asd_sas_port *port,
494 struct domain_device *dev;
496 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
498 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
499 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
505 #define RPEL_REQ_SIZE 16
506 #define RPEL_RESP_SIZE 32
507 int sas_smp_get_phy_events(struct sas_phy *phy)
510 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
511 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
512 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
513 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
518 req[1] = SMP_REPORT_PHY_ERR_LOG;
519 req[9] = phy->number;
521 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
522 resp, RPEL_RESP_SIZE);
527 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
528 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
529 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
530 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
538 #define RPS_REQ_SIZE 16
539 #define RPS_RESP_SIZE 60
541 static int sas_get_report_phy_sata(struct domain_device *dev,
543 struct smp_resp *rps_resp)
546 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
547 u8 *resp = (u8 *)rps_resp;
552 rps_req[1] = SMP_REPORT_PHY_SATA;
555 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
556 rps_resp, RPS_RESP_SIZE);
558 /* 0x34 is the FIS type for the D2H fis. There's a potential
559 * standards cockup here. sas-2 explicitly specifies the FIS
560 * should be encoded so that FIS type is in resp[24].
561 * However, some expanders endian reverse this. Undo the
563 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
566 for (i = 0; i < 5; i++) {
571 resp[j + 0] = resp[j + 3];
572 resp[j + 1] = resp[j + 2];
582 static void sas_ex_get_linkrate(struct domain_device *parent,
583 struct domain_device *child,
584 struct ex_phy *parent_phy)
586 struct expander_device *parent_ex = &parent->ex_dev;
587 struct sas_port *port;
592 port = parent_phy->port;
594 for (i = 0; i < parent_ex->num_phys; i++) {
595 struct ex_phy *phy = &parent_ex->ex_phy[i];
597 if (phy->phy_state == PHY_VACANT ||
598 phy->phy_state == PHY_NOT_PRESENT)
601 if (SAS_ADDR(phy->attached_sas_addr) ==
602 SAS_ADDR(child->sas_addr)) {
604 child->min_linkrate = min(parent->min_linkrate,
606 child->max_linkrate = max(parent->max_linkrate,
609 sas_port_add_phy(port, phy->phy);
612 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
613 child->pathways = min(child->pathways, parent->pathways);
616 static struct domain_device *sas_ex_discover_end_dev(
617 struct domain_device *parent, int phy_id)
619 struct expander_device *parent_ex = &parent->ex_dev;
620 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
621 struct domain_device *child = NULL;
622 struct sas_rphy *rphy;
625 if (phy->attached_sata_host || phy->attached_sata_ps)
628 child = kzalloc(sizeof(*child), GFP_KERNEL);
632 child->parent = parent;
633 child->port = parent->port;
634 child->iproto = phy->attached_iproto;
635 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
636 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
638 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
639 if (unlikely(!phy->port))
641 if (unlikely(sas_port_add(phy->port) != 0)) {
642 sas_port_free(phy->port);
646 sas_ex_get_linkrate(parent, child, phy);
648 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
649 child->dev_type = SATA_DEV;
650 if (phy->attached_tproto & SAS_PROTO_STP)
651 child->tproto = phy->attached_tproto;
652 if (phy->attached_sata_dev)
653 child->tproto |= SATA_DEV;
654 res = sas_get_report_phy_sata(parent, phy_id,
655 &child->sata_dev.rps_resp);
657 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
658 "0x%x\n", SAS_ADDR(parent->sas_addr),
662 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
663 sizeof(struct dev_to_host_fis));
665 rphy = sas_end_device_alloc(phy->port);
673 spin_lock_irq(&parent->port->dev_list_lock);
674 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
675 spin_unlock_irq(&parent->port->dev_list_lock);
677 res = sas_discover_sata(child);
679 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
680 "%016llx:0x%x returned 0x%x\n",
681 SAS_ADDR(child->sas_addr),
682 SAS_ADDR(parent->sas_addr), phy_id, res);
685 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
686 child->dev_type = SAS_END_DEV;
687 rphy = sas_end_device_alloc(phy->port);
688 /* FIXME: error handling */
691 child->tproto = phy->attached_tproto;
695 sas_fill_in_rphy(child, rphy);
697 spin_lock_irq(&parent->port->dev_list_lock);
698 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
699 spin_unlock_irq(&parent->port->dev_list_lock);
701 res = sas_discover_end_dev(child);
703 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
704 "at %016llx:0x%x returned 0x%x\n",
705 SAS_ADDR(child->sas_addr),
706 SAS_ADDR(parent->sas_addr), phy_id, res);
710 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
711 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
715 list_add_tail(&child->siblings, &parent_ex->children);
719 sas_rphy_free(child->rphy);
721 list_del(&child->dev_list_node);
723 sas_port_delete(phy->port);
730 /* See if this phy is part of a wide port */
731 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
733 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
736 for (i = 0; i < parent->ex_dev.num_phys; i++) {
737 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
742 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
743 SAS_ADDR_SIZE) && ephy->port) {
744 sas_port_add_phy(ephy->port, phy->phy);
745 phy->phy_state = PHY_DEVICE_DISCOVERED;
753 static struct domain_device *sas_ex_discover_expander(
754 struct domain_device *parent, int phy_id)
756 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
757 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
758 struct domain_device *child = NULL;
759 struct sas_rphy *rphy;
760 struct sas_expander_device *edev;
761 struct asd_sas_port *port;
764 if (phy->routing_attr == DIRECT_ROUTING) {
765 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
767 SAS_ADDR(parent->sas_addr), phy_id,
768 SAS_ADDR(phy->attached_sas_addr),
769 phy->attached_phy_id);
772 child = kzalloc(sizeof(*child), GFP_KERNEL);
776 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
777 /* FIXME: better error handling */
778 BUG_ON(sas_port_add(phy->port) != 0);
781 switch (phy->attached_dev_type) {
783 rphy = sas_expander_alloc(phy->port,
784 SAS_EDGE_EXPANDER_DEVICE);
787 rphy = sas_expander_alloc(phy->port,
788 SAS_FANOUT_EXPANDER_DEVICE);
791 rphy = NULL; /* shut gcc up */
796 edev = rphy_to_expander_device(rphy);
797 child->dev_type = phy->attached_dev_type;
798 child->parent = parent;
800 child->iproto = phy->attached_iproto;
801 child->tproto = phy->attached_tproto;
802 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
803 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
804 sas_ex_get_linkrate(parent, child, phy);
805 edev->level = parent_ex->level + 1;
806 parent->port->disc.max_level = max(parent->port->disc.max_level,
809 sas_fill_in_rphy(child, rphy);
812 spin_lock_irq(&parent->port->dev_list_lock);
813 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
814 spin_unlock_irq(&parent->port->dev_list_lock);
816 res = sas_discover_expander(child);
821 list_add_tail(&child->siblings, &parent->ex_dev.children);
825 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
827 struct expander_device *ex = &dev->ex_dev;
828 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
829 struct domain_device *child = NULL;
833 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
834 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
835 res = sas_ex_phy_discover(dev, phy_id);
840 /* Parent and domain coherency */
841 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
842 SAS_ADDR(dev->port->sas_addr))) {
843 sas_add_parent_port(dev, phy_id);
846 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
847 SAS_ADDR(dev->parent->sas_addr))) {
848 sas_add_parent_port(dev, phy_id);
849 if (ex_phy->routing_attr == TABLE_ROUTING)
850 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
854 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
855 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
857 if (ex_phy->attached_dev_type == NO_DEVICE) {
858 if (ex_phy->routing_attr == DIRECT_ROUTING) {
859 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
860 sas_configure_routing(dev, ex_phy->attached_sas_addr);
863 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
866 if (ex_phy->attached_dev_type != SAS_END_DEV &&
867 ex_phy->attached_dev_type != FANOUT_DEV &&
868 ex_phy->attached_dev_type != EDGE_DEV) {
869 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
870 "phy 0x%x\n", ex_phy->attached_dev_type,
871 SAS_ADDR(dev->sas_addr),
876 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
878 SAS_DPRINTK("configure routing for dev %016llx "
879 "reported 0x%x. Forgotten\n",
880 SAS_ADDR(ex_phy->attached_sas_addr), res);
881 sas_disable_routing(dev, ex_phy->attached_sas_addr);
885 res = sas_ex_join_wide_port(dev, phy_id);
887 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
888 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
892 switch (ex_phy->attached_dev_type) {
894 child = sas_ex_discover_end_dev(dev, phy_id);
897 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
898 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
899 "attached to ex %016llx phy 0x%x\n",
900 SAS_ADDR(ex_phy->attached_sas_addr),
901 ex_phy->attached_phy_id,
902 SAS_ADDR(dev->sas_addr),
904 sas_ex_disable_phy(dev, phy_id);
907 memcpy(dev->port->disc.fanout_sas_addr,
908 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
911 child = sas_ex_discover_expander(dev, phy_id);
920 for (i = 0; i < ex->num_phys; i++) {
921 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
922 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
925 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
926 SAS_ADDR(child->sas_addr))
927 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
934 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
936 struct expander_device *ex = &dev->ex_dev;
939 for (i = 0; i < ex->num_phys; i++) {
940 struct ex_phy *phy = &ex->ex_phy[i];
942 if (phy->phy_state == PHY_VACANT ||
943 phy->phy_state == PHY_NOT_PRESENT)
946 if ((phy->attached_dev_type == EDGE_DEV ||
947 phy->attached_dev_type == FANOUT_DEV) &&
948 phy->routing_attr == SUBTRACTIVE_ROUTING) {
950 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
958 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
960 struct expander_device *ex = &dev->ex_dev;
961 struct domain_device *child;
962 u8 sub_addr[8] = {0, };
964 list_for_each_entry(child, &ex->children, siblings) {
965 if (child->dev_type != EDGE_DEV &&
966 child->dev_type != FANOUT_DEV)
968 if (sub_addr[0] == 0) {
969 sas_find_sub_addr(child, sub_addr);
974 if (sas_find_sub_addr(child, s2) &&
975 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
977 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
978 "diverges from subtractive "
979 "boundary %016llx\n",
980 SAS_ADDR(dev->sas_addr),
981 SAS_ADDR(child->sas_addr),
985 sas_ex_disable_port(child, s2);
992 * sas_ex_discover_devices -- discover devices attached to this expander
993 * dev: pointer to the expander domain device
994 * single: if you want to do a single phy, else set to -1;
996 * Configure this expander for use with its devices and register the
997 * devices of this expander.
999 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1001 struct expander_device *ex = &dev->ex_dev;
1002 int i = 0, end = ex->num_phys;
1005 if (0 <= single && single < end) {
1010 for ( ; i < end; i++) {
1011 struct ex_phy *ex_phy = &ex->ex_phy[i];
1013 if (ex_phy->phy_state == PHY_VACANT ||
1014 ex_phy->phy_state == PHY_NOT_PRESENT ||
1015 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1018 switch (ex_phy->linkrate) {
1019 case SAS_PHY_DISABLED:
1020 case SAS_PHY_RESET_PROBLEM:
1021 case SAS_SATA_PORT_SELECTOR:
1024 res = sas_ex_discover_dev(dev, i);
1032 sas_check_level_subtractive_boundary(dev);
1037 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1039 struct expander_device *ex = &dev->ex_dev;
1041 u8 *sub_sas_addr = NULL;
1043 if (dev->dev_type != EDGE_DEV)
1046 for (i = 0; i < ex->num_phys; i++) {
1047 struct ex_phy *phy = &ex->ex_phy[i];
1049 if (phy->phy_state == PHY_VACANT ||
1050 phy->phy_state == PHY_NOT_PRESENT)
1053 if ((phy->attached_dev_type == FANOUT_DEV ||
1054 phy->attached_dev_type == EDGE_DEV) &&
1055 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1058 sub_sas_addr = &phy->attached_sas_addr[0];
1059 else if (SAS_ADDR(sub_sas_addr) !=
1060 SAS_ADDR(phy->attached_sas_addr)) {
1062 SAS_DPRINTK("ex %016llx phy 0x%x "
1063 "diverges(%016llx) on subtractive "
1064 "boundary(%016llx). Disabled\n",
1065 SAS_ADDR(dev->sas_addr), i,
1066 SAS_ADDR(phy->attached_sas_addr),
1067 SAS_ADDR(sub_sas_addr));
1068 sas_ex_disable_phy(dev, i);
1075 static void sas_print_parent_topology_bug(struct domain_device *child,
1076 struct ex_phy *parent_phy,
1077 struct ex_phy *child_phy)
1079 static const char ra_char[] = {
1080 [DIRECT_ROUTING] = 'D',
1081 [SUBTRACTIVE_ROUTING] = 'S',
1082 [TABLE_ROUTING] = 'T',
1084 static const char *ex_type[] = {
1085 [EDGE_DEV] = "edge",
1086 [FANOUT_DEV] = "fanout",
1088 struct domain_device *parent = child->parent;
1090 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1091 "has %c:%c routing link!\n",
1093 ex_type[parent->dev_type],
1094 SAS_ADDR(parent->sas_addr),
1097 ex_type[child->dev_type],
1098 SAS_ADDR(child->sas_addr),
1101 ra_char[parent_phy->routing_attr],
1102 ra_char[child_phy->routing_attr]);
1105 static int sas_check_eeds(struct domain_device *child,
1106 struct ex_phy *parent_phy,
1107 struct ex_phy *child_phy)
1110 struct domain_device *parent = child->parent;
1112 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1114 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1115 "phy S:0x%x, while there is a fanout ex %016llx\n",
1116 SAS_ADDR(parent->sas_addr),
1118 SAS_ADDR(child->sas_addr),
1120 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1121 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1122 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1124 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1126 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1127 SAS_ADDR(parent->sas_addr)) ||
1128 (SAS_ADDR(parent->port->disc.eeds_a) ==
1129 SAS_ADDR(child->sas_addr)))
1131 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1132 SAS_ADDR(parent->sas_addr)) ||
1133 (SAS_ADDR(parent->port->disc.eeds_b) ==
1134 SAS_ADDR(child->sas_addr))))
1138 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1139 "phy 0x%x link forms a third EEDS!\n",
1140 SAS_ADDR(parent->sas_addr),
1142 SAS_ADDR(child->sas_addr),
1149 /* Here we spill over 80 columns. It is intentional.
1151 static int sas_check_parent_topology(struct domain_device *child)
1153 struct expander_device *child_ex = &child->ex_dev;
1154 struct expander_device *parent_ex;
1161 if (child->parent->dev_type != EDGE_DEV &&
1162 child->parent->dev_type != FANOUT_DEV)
1165 parent_ex = &child->parent->ex_dev;
1167 for (i = 0; i < parent_ex->num_phys; i++) {
1168 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1169 struct ex_phy *child_phy;
1171 if (parent_phy->phy_state == PHY_VACANT ||
1172 parent_phy->phy_state == PHY_NOT_PRESENT)
1175 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1178 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1180 switch (child->parent->dev_type) {
1182 if (child->dev_type == FANOUT_DEV) {
1183 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1184 child_phy->routing_attr != TABLE_ROUTING) {
1185 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1188 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1189 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1190 res = sas_check_eeds(child, parent_phy, child_phy);
1191 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1192 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1195 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1196 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1197 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1202 if (parent_phy->routing_attr != TABLE_ROUTING ||
1203 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1204 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1216 #define RRI_REQ_SIZE 16
1217 #define RRI_RESP_SIZE 44
1219 static int sas_configure_present(struct domain_device *dev, int phy_id,
1220 u8 *sas_addr, int *index, int *present)
1223 struct expander_device *ex = &dev->ex_dev;
1224 struct ex_phy *phy = &ex->ex_phy[phy_id];
1231 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1235 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1241 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1242 rri_req[9] = phy_id;
1244 for (i = 0; i < ex->max_route_indexes ; i++) {
1245 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1246 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1251 if (res == SMP_RESP_NO_INDEX) {
1252 SAS_DPRINTK("overflow of indexes: dev %016llx "
1253 "phy 0x%x index 0x%x\n",
1254 SAS_ADDR(dev->sas_addr), phy_id, i);
1256 } else if (res != SMP_RESP_FUNC_ACC) {
1257 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1258 "result 0x%x\n", __FUNCTION__,
1259 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1262 if (SAS_ADDR(sas_addr) != 0) {
1263 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1265 if ((rri_resp[12] & 0x80) == 0x80)
1270 } else if (SAS_ADDR(rri_resp+16) == 0) {
1275 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1276 phy->last_da_index < i) {
1277 phy->last_da_index = i;
1290 #define CRI_REQ_SIZE 44
1291 #define CRI_RESP_SIZE 8
1293 static int sas_configure_set(struct domain_device *dev, int phy_id,
1294 u8 *sas_addr, int index, int include)
1300 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1304 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1310 cri_req[1] = SMP_CONF_ROUTE_INFO;
1311 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1312 cri_req[9] = phy_id;
1313 if (SAS_ADDR(sas_addr) == 0 || !include)
1314 cri_req[12] |= 0x80;
1315 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1317 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1322 if (res == SMP_RESP_NO_INDEX) {
1323 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1325 SAS_ADDR(dev->sas_addr), phy_id, index);
1333 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1334 u8 *sas_addr, int include)
1340 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1343 if (include ^ present)
1344 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1350 * sas_configure_parent -- configure routing table of parent
1351 * parent: parent expander
1352 * child: child expander
1353 * sas_addr: SAS port identifier of device directly attached to child
1355 static int sas_configure_parent(struct domain_device *parent,
1356 struct domain_device *child,
1357 u8 *sas_addr, int include)
1359 struct expander_device *ex_parent = &parent->ex_dev;
1363 if (parent->parent) {
1364 res = sas_configure_parent(parent->parent, parent, sas_addr,
1370 if (ex_parent->conf_route_table == 0) {
1371 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1372 SAS_ADDR(parent->sas_addr));
1376 for (i = 0; i < ex_parent->num_phys; i++) {
1377 struct ex_phy *phy = &ex_parent->ex_phy[i];
1379 if ((phy->routing_attr == TABLE_ROUTING) &&
1380 (SAS_ADDR(phy->attached_sas_addr) ==
1381 SAS_ADDR(child->sas_addr))) {
1382 res = sas_configure_phy(parent, i, sas_addr, include);
1392 * sas_configure_routing -- configure routing
1393 * dev: expander device
1394 * sas_addr: port identifier of device directly attached to the expander device
1396 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1399 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1403 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1406 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1411 * sas_discover_expander -- expander discovery
1412 * @ex: pointer to expander domain device
1414 * See comment in sas_discover_sata().
1416 static int sas_discover_expander(struct domain_device *dev)
1420 res = sas_notify_lldd_dev_found(dev);
1424 res = sas_ex_general(dev);
1427 res = sas_ex_manuf_info(dev);
1431 res = sas_expander_discover(dev);
1433 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1434 SAS_ADDR(dev->sas_addr), res);
1438 sas_check_ex_subtractive_boundary(dev);
1439 res = sas_check_parent_topology(dev);
1444 sas_notify_lldd_dev_gone(dev);
1448 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1451 struct domain_device *dev;
1453 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1454 if (dev->dev_type == EDGE_DEV ||
1455 dev->dev_type == FANOUT_DEV) {
1456 struct sas_expander_device *ex =
1457 rphy_to_expander_device(dev->rphy);
1459 if (level == ex->level)
1460 res = sas_ex_discover_devices(dev, -1);
1462 res = sas_ex_discover_devices(port->port_dev, -1);
1470 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1476 level = port->disc.max_level;
1477 res = sas_ex_level_discovery(port, level);
1479 } while (level < port->disc.max_level);
1484 int sas_discover_root_expander(struct domain_device *dev)
1487 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1489 res = sas_rphy_add(dev->rphy);
1493 ex->level = dev->port->disc.max_level; /* 0 */
1494 res = sas_discover_expander(dev);
1498 sas_ex_bfs_disc(dev->port);
1503 sas_rphy_remove(dev->rphy);
1508 /* ---------- Domain revalidation ---------- */
1510 static int sas_get_phy_discover(struct domain_device *dev,
1511 int phy_id, struct smp_resp *disc_resp)
1516 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1520 disc_req[1] = SMP_DISCOVER;
1521 disc_req[9] = phy_id;
1523 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1524 disc_resp, DISCOVER_RESP_SIZE);
1527 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1528 res = disc_resp->result;
1536 static int sas_get_phy_change_count(struct domain_device *dev,
1537 int phy_id, int *pcc)
1540 struct smp_resp *disc_resp;
1542 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1546 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1548 *pcc = disc_resp->disc.change_count;
1554 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1555 int phy_id, u8 *attached_sas_addr)
1558 struct smp_resp *disc_resp;
1559 struct discover_resp *dr;
1561 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1564 dr = &disc_resp->disc;
1566 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1568 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1569 if (dr->attached_dev_type == 0)
1570 memset(attached_sas_addr, 0, 8);
1576 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1579 struct expander_device *ex = &dev->ex_dev;
1583 for (i = from_phy; i < ex->num_phys; i++) {
1584 int phy_change_count = 0;
1586 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1589 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1590 ex->ex_phy[i].phy_change_count = phy_change_count;
1599 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1603 struct smp_resp *rg_resp;
1605 rg_req = alloc_smp_req(RG_REQ_SIZE);
1609 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1615 rg_req[1] = SMP_REPORT_GENERAL;
1617 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1621 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1622 res = rg_resp->result;
1626 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1633 static int sas_find_bcast_dev(struct domain_device *dev,
1634 struct domain_device **src_dev)
1636 struct expander_device *ex = &dev->ex_dev;
1637 int ex_change_count = -1;
1640 res = sas_get_ex_change_count(dev, &ex_change_count);
1643 if (ex_change_count != -1 &&
1644 ex_change_count != ex->ex_change_count) {
1646 ex->ex_change_count = ex_change_count;
1648 struct domain_device *ch;
1650 list_for_each_entry(ch, &ex->children, siblings) {
1651 if (ch->dev_type == EDGE_DEV ||
1652 ch->dev_type == FANOUT_DEV) {
1653 res = sas_find_bcast_dev(ch, src_dev);
1663 static void sas_unregister_ex_tree(struct domain_device *dev)
1665 struct expander_device *ex = &dev->ex_dev;
1666 struct domain_device *child, *n;
1668 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1669 if (child->dev_type == EDGE_DEV ||
1670 child->dev_type == FANOUT_DEV)
1671 sas_unregister_ex_tree(child);
1673 sas_unregister_dev(child);
1675 sas_unregister_dev(dev);
1678 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1681 struct expander_device *ex_dev = &parent->ex_dev;
1682 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1683 struct domain_device *child, *n;
1685 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1686 if (SAS_ADDR(child->sas_addr) ==
1687 SAS_ADDR(phy->attached_sas_addr)) {
1688 if (child->dev_type == EDGE_DEV ||
1689 child->dev_type == FANOUT_DEV)
1690 sas_unregister_ex_tree(child);
1692 sas_unregister_dev(child);
1696 sas_disable_routing(parent, phy->attached_sas_addr);
1697 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1698 sas_port_delete_phy(phy->port, phy->phy);
1699 if (phy->port->num_phys == 0)
1700 sas_port_delete(phy->port);
1704 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1707 struct expander_device *ex_root = &root->ex_dev;
1708 struct domain_device *child;
1711 list_for_each_entry(child, &ex_root->children, siblings) {
1712 if (child->dev_type == EDGE_DEV ||
1713 child->dev_type == FANOUT_DEV) {
1714 struct sas_expander_device *ex =
1715 rphy_to_expander_device(child->rphy);
1717 if (level > ex->level)
1718 res = sas_discover_bfs_by_root_level(child,
1720 else if (level == ex->level)
1721 res = sas_ex_discover_devices(child, -1);
1727 static int sas_discover_bfs_by_root(struct domain_device *dev)
1730 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1731 int level = ex->level+1;
1733 res = sas_ex_discover_devices(dev, -1);
1737 res = sas_discover_bfs_by_root_level(dev, level);
1740 } while (level <= dev->port->disc.max_level);
1745 static int sas_discover_new(struct domain_device *dev, int phy_id)
1747 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1748 struct domain_device *child;
1751 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1752 SAS_ADDR(dev->sas_addr), phy_id);
1753 res = sas_ex_phy_discover(dev, phy_id);
1756 res = sas_ex_discover_devices(dev, phy_id);
1759 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1760 if (SAS_ADDR(child->sas_addr) ==
1761 SAS_ADDR(ex_phy->attached_sas_addr)) {
1762 if (child->dev_type == EDGE_DEV ||
1763 child->dev_type == FANOUT_DEV)
1764 res = sas_discover_bfs_by_root(child);
1772 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1774 struct expander_device *ex = &dev->ex_dev;
1775 struct ex_phy *phy = &ex->ex_phy[phy_id];
1776 u8 attached_sas_addr[8];
1779 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1781 case SMP_RESP_NO_PHY:
1782 phy->phy_state = PHY_NOT_PRESENT;
1783 sas_unregister_devs_sas_addr(dev, phy_id);
1785 case SMP_RESP_PHY_VACANT:
1786 phy->phy_state = PHY_VACANT;
1787 sas_unregister_devs_sas_addr(dev, phy_id);
1789 case SMP_RESP_FUNC_ACC:
1793 if (SAS_ADDR(attached_sas_addr) == 0) {
1794 phy->phy_state = PHY_EMPTY;
1795 sas_unregister_devs_sas_addr(dev, phy_id);
1796 } else if (SAS_ADDR(attached_sas_addr) ==
1797 SAS_ADDR(phy->attached_sas_addr)) {
1798 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1799 SAS_ADDR(dev->sas_addr), phy_id);
1800 sas_ex_phy_discover(dev, phy_id);
1802 res = sas_discover_new(dev, phy_id);
1807 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1809 struct expander_device *ex = &dev->ex_dev;
1810 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1814 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1815 SAS_ADDR(dev->sas_addr), phy_id);
1817 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1818 for (i = 0; i < ex->num_phys; i++) {
1819 struct ex_phy *phy = &ex->ex_phy[i];
1823 if (SAS_ADDR(phy->attached_sas_addr) ==
1824 SAS_ADDR(changed_phy->attached_sas_addr)) {
1825 SAS_DPRINTK("phy%d part of wide port with "
1826 "phy%d\n", phy_id, i);
1830 res = sas_rediscover_dev(dev, phy_id);
1832 res = sas_discover_new(dev, phy_id);
1838 * sas_revalidate_domain -- revalidate the domain
1839 * @port: port to the domain of interest
1841 * NOTE: this process _must_ quit (return) as soon as any connection
1842 * errors are encountered. Connection recovery is done elsewhere.
1843 * Discover process only interrogates devices in order to discover the
1846 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1849 struct domain_device *dev = NULL;
1851 res = sas_find_bcast_dev(port_dev, &dev);
1855 struct expander_device *ex = &dev->ex_dev;
1860 res = sas_find_bcast_phy(dev, &phy_id, i);
1863 res = sas_rediscover(dev, phy_id);
1865 } while (i < ex->num_phys);
1871 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1872 struct request *req)
1874 struct domain_device *dev;
1875 int ret, type = rphy->identify.device_type;
1876 struct request *rsp = req->next_rq;
1879 printk("%s: space for a smp response is missing\n",
1884 /* seems aic94xx doesn't support */
1886 printk("%s: can we send a smp request to a host?\n",
1891 if (type != SAS_EDGE_EXPANDER_DEVICE &&
1892 type != SAS_FANOUT_EXPANDER_DEVICE) {
1893 printk("%s: can we send a smp request to a device?\n",
1898 dev = sas_find_dev_by_rphy(rphy);
1900 printk("%s: fail to find a domain_device?\n", __FUNCTION__);
1904 /* do we need to support multiple segments? */
1905 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1906 printk("%s: multiple segments req %u %u, rsp %u %u\n",
1907 __FUNCTION__, req->bio->bi_vcnt, req->data_len,
1908 rsp->bio->bi_vcnt, rsp->data_len);
1912 ret = smp_execute_task(dev, bio_data(req->bio), req->data_len,
1913 bio_data(rsp->bio), rsp->data_len);