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>
27 #include "sas_internal.h"
29 #include <scsi/scsi_transport.h>
30 #include <scsi/scsi_transport_sas.h>
31 #include "../scsi_sas_internal.h"
33 static int sas_discover_expander(struct domain_device *dev);
34 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
35 static int sas_configure_phy(struct domain_device *dev, int phy_id,
36 u8 *sas_addr, int include);
37 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
40 /* FIXME: smp needs to migrate into the sas class */
41 static ssize_t smp_portal_read(struct kobject *, struct bin_attribute *,
42 char *, loff_t, size_t);
43 static ssize_t smp_portal_write(struct kobject *, struct bin_attribute *,
44 char *, loff_t, size_t);
47 /* ---------- SMP task management ---------- */
49 static void smp_task_timedout(unsigned long _task)
51 struct sas_task *task = (void *) _task;
54 spin_lock_irqsave(&task->task_state_lock, flags);
55 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
56 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
57 spin_unlock_irqrestore(&task->task_state_lock, flags);
59 complete(&task->completion);
62 static void smp_task_done(struct sas_task *task)
64 if (!del_timer(&task->timer))
66 complete(&task->completion);
69 /* Give it some long enough timeout. In seconds. */
70 #define SMP_TIMEOUT 10
72 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
73 void *resp, int resp_size)
76 struct sas_task *task = NULL;
77 struct sas_internal *i =
78 to_sas_internal(dev->port->ha->core.shost->transportt);
80 for (retry = 0; retry < 3; retry++) {
81 task = sas_alloc_task(GFP_KERNEL);
86 task->task_proto = dev->tproto;
87 sg_init_one(&task->smp_task.smp_req, req, req_size);
88 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
90 task->task_done = smp_task_done;
92 task->timer.data = (unsigned long) task;
93 task->timer.function = smp_task_timedout;
94 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
95 add_timer(&task->timer);
97 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
100 del_timer(&task->timer);
101 SAS_DPRINTK("executing SMP task failed:%d\n", res);
105 wait_for_completion(&task->completion);
107 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
108 SAS_DPRINTK("smp task timed out or aborted\n");
109 i->dft->lldd_abort_task(task);
110 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
111 SAS_DPRINTK("SMP task aborted and not done\n");
115 if (task->task_status.resp == SAS_TASK_COMPLETE &&
116 task->task_status.stat == SAM_GOOD) {
120 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
121 "status 0x%x\n", __FUNCTION__,
122 SAS_ADDR(dev->sas_addr),
123 task->task_status.resp,
124 task->task_status.stat);
130 BUG_ON(retry == 3 && task != NULL);
137 /* ---------- Allocations ---------- */
139 static inline void *alloc_smp_req(int size)
141 u8 *p = kzalloc(size, GFP_KERNEL);
147 static inline void *alloc_smp_resp(int size)
149 return kzalloc(size, GFP_KERNEL);
152 /* ---------- Expander configuration ---------- */
154 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
157 struct expander_device *ex = &dev->ex_dev;
158 struct ex_phy *phy = &ex->ex_phy[phy_id];
159 struct smp_resp *resp = disc_resp;
160 struct discover_resp *dr = &resp->disc;
161 struct sas_rphy *rphy = dev->rphy;
162 int rediscover = (phy->phy != NULL);
165 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
167 /* FIXME: error_handling */
171 switch (resp->result) {
172 case SMP_RESP_PHY_VACANT:
173 phy->phy_state = PHY_VACANT;
176 phy->phy_state = PHY_NOT_PRESENT;
178 case SMP_RESP_FUNC_ACC:
179 phy->phy_state = PHY_EMPTY; /* do not know yet */
183 phy->phy_id = phy_id;
184 phy->attached_dev_type = dr->attached_dev_type;
185 phy->linkrate = dr->linkrate;
186 phy->attached_sata_host = dr->attached_sata_host;
187 phy->attached_sata_dev = dr->attached_sata_dev;
188 phy->attached_sata_ps = dr->attached_sata_ps;
189 phy->attached_iproto = dr->iproto << 1;
190 phy->attached_tproto = dr->tproto << 1;
191 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
192 phy->attached_phy_id = dr->attached_phy_id;
193 phy->phy_change_count = dr->change_count;
194 phy->routing_attr = dr->routing_attr;
195 phy->virtual = dr->virtual;
196 phy->last_da_index = -1;
198 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
199 phy->phy->identify.target_port_protocols = phy->attached_tproto;
200 phy->phy->identify.phy_identifier = phy_id;
201 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
202 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
203 phy->phy->minimum_linkrate = dr->pmin_linkrate;
204 phy->phy->maximum_linkrate = dr->pmax_linkrate;
205 phy->phy->negotiated_linkrate = phy->linkrate;
208 sas_phy_add(phy->phy);
210 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
211 SAS_ADDR(dev->sas_addr), phy->phy_id,
212 phy->routing_attr == TABLE_ROUTING ? 'T' :
213 phy->routing_attr == DIRECT_ROUTING ? 'D' :
214 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
215 SAS_ADDR(phy->attached_sas_addr));
220 #define DISCOVER_REQ_SIZE 16
221 #define DISCOVER_RESP_SIZE 56
223 static int sas_ex_phy_discover(struct domain_device *dev, int single)
225 struct expander_device *ex = &dev->ex_dev;
230 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
234 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
240 disc_req[1] = SMP_DISCOVER;
242 if (0 <= single && single < ex->num_phys) {
243 disc_req[9] = single;
244 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
245 disc_resp, DISCOVER_RESP_SIZE);
248 sas_set_ex_phy(dev, single, disc_resp);
252 for (i = 0; i < ex->num_phys; i++) {
254 res = smp_execute_task(dev, disc_req,
255 DISCOVER_REQ_SIZE, disc_resp,
259 sas_set_ex_phy(dev, i, disc_resp);
268 static int sas_expander_discover(struct domain_device *dev)
270 struct expander_device *ex = &dev->ex_dev;
273 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
277 res = sas_ex_phy_discover(dev, -1);
288 #define MAX_EXPANDER_PHYS 128
290 static void ex_assign_report_general(struct domain_device *dev,
291 struct smp_resp *resp)
293 struct report_general_resp *rg = &resp->rg;
295 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
296 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
297 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
298 dev->ex_dev.conf_route_table = rg->conf_route_table;
299 dev->ex_dev.configuring = rg->configuring;
300 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
303 #define RG_REQ_SIZE 8
304 #define RG_RESP_SIZE 32
306 static int sas_ex_general(struct domain_device *dev)
309 struct smp_resp *rg_resp;
313 rg_req = alloc_smp_req(RG_REQ_SIZE);
317 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
323 rg_req[1] = SMP_REPORT_GENERAL;
325 for (i = 0; i < 5; i++) {
326 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
330 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
331 SAS_ADDR(dev->sas_addr), res);
333 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
334 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
335 SAS_ADDR(dev->sas_addr), rg_resp->result);
336 res = rg_resp->result;
340 ex_assign_report_general(dev, rg_resp);
342 if (dev->ex_dev.configuring) {
343 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
344 SAS_ADDR(dev->sas_addr));
345 schedule_timeout_interruptible(5*HZ);
355 static void ex_assign_manuf_info(struct domain_device *dev, void
358 u8 *mi_resp = _mi_resp;
359 struct sas_rphy *rphy = dev->rphy;
360 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
362 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
363 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
364 memcpy(edev->product_rev, mi_resp + 36,
365 SAS_EXPANDER_PRODUCT_REV_LEN);
367 if (mi_resp[8] & 1) {
368 memcpy(edev->component_vendor_id, mi_resp + 40,
369 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
370 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
371 edev->component_revision_id = mi_resp[50];
375 #define MI_REQ_SIZE 8
376 #define MI_RESP_SIZE 64
378 static int sas_ex_manuf_info(struct domain_device *dev)
384 mi_req = alloc_smp_req(MI_REQ_SIZE);
388 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
394 mi_req[1] = SMP_REPORT_MANUF_INFO;
396 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
398 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
399 SAS_ADDR(dev->sas_addr), res);
401 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
402 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
403 SAS_ADDR(dev->sas_addr), mi_resp[2]);
407 ex_assign_manuf_info(dev, mi_resp);
414 #define PC_REQ_SIZE 44
415 #define PC_RESP_SIZE 8
417 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
418 enum phy_func phy_func,
419 struct sas_phy_linkrates *rates)
425 pc_req = alloc_smp_req(PC_REQ_SIZE);
429 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
435 pc_req[1] = SMP_PHY_CONTROL;
437 pc_req[10]= phy_func;
439 pc_req[32] = rates->minimum_linkrate << 4;
440 pc_req[33] = rates->maximum_linkrate << 4;
443 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
450 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
452 struct expander_device *ex = &dev->ex_dev;
453 struct ex_phy *phy = &ex->ex_phy[phy_id];
455 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
456 phy->linkrate = SAS_PHY_DISABLED;
459 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
461 struct expander_device *ex = &dev->ex_dev;
464 for (i = 0; i < ex->num_phys; i++) {
465 struct ex_phy *phy = &ex->ex_phy[i];
467 if (phy->phy_state == PHY_VACANT ||
468 phy->phy_state == PHY_NOT_PRESENT)
471 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
472 sas_ex_disable_phy(dev, i);
476 static int sas_dev_present_in_domain(struct asd_sas_port *port,
479 struct domain_device *dev;
481 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
483 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
484 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
490 #define RPEL_REQ_SIZE 16
491 #define RPEL_RESP_SIZE 32
492 int sas_smp_get_phy_events(struct sas_phy *phy)
495 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
496 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
497 u8 *req = alloc_smp_req(RPEL_REQ_SIZE);
498 u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL);
503 req[1] = SMP_REPORT_PHY_ERR_LOG;
504 req[9] = phy->number;
506 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
507 resp, RPEL_RESP_SIZE);
512 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
513 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
514 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
515 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
523 #define RPS_REQ_SIZE 16
524 #define RPS_RESP_SIZE 60
526 static int sas_get_report_phy_sata(struct domain_device *dev,
528 struct smp_resp *rps_resp)
531 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
536 rps_req[1] = SMP_REPORT_PHY_SATA;
539 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
540 rps_resp, RPS_RESP_SIZE);
546 static void sas_ex_get_linkrate(struct domain_device *parent,
547 struct domain_device *child,
548 struct ex_phy *parent_phy)
550 struct expander_device *parent_ex = &parent->ex_dev;
551 struct sas_port *port;
556 port = parent_phy->port;
558 for (i = 0; i < parent_ex->num_phys; i++) {
559 struct ex_phy *phy = &parent_ex->ex_phy[i];
561 if (phy->phy_state == PHY_VACANT ||
562 phy->phy_state == PHY_NOT_PRESENT)
565 if (SAS_ADDR(phy->attached_sas_addr) ==
566 SAS_ADDR(child->sas_addr)) {
568 child->min_linkrate = min(parent->min_linkrate,
570 child->max_linkrate = max(parent->max_linkrate,
573 sas_port_add_phy(port, phy->phy);
576 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
577 child->pathways = min(child->pathways, parent->pathways);
580 static struct domain_device *sas_ex_discover_end_dev(
581 struct domain_device *parent, int phy_id)
583 struct expander_device *parent_ex = &parent->ex_dev;
584 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
585 struct domain_device *child = NULL;
586 struct sas_rphy *rphy;
589 if (phy->attached_sata_host || phy->attached_sata_ps)
592 child = kzalloc(sizeof(*child), GFP_KERNEL);
596 child->parent = parent;
597 child->port = parent->port;
598 child->iproto = phy->attached_iproto;
599 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
600 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
602 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
603 if (unlikely(!phy->port))
605 if (unlikely(sas_port_add(phy->port) != 0)) {
606 sas_port_free(phy->port);
610 sas_ex_get_linkrate(parent, child, phy);
612 if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) {
613 child->dev_type = SATA_DEV;
614 if (phy->attached_tproto & SAS_PROTO_STP)
615 child->tproto = phy->attached_tproto;
616 if (phy->attached_sata_dev)
617 child->tproto |= SATA_DEV;
618 res = sas_get_report_phy_sata(parent, phy_id,
619 &child->sata_dev.rps_resp);
621 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
622 "0x%x\n", SAS_ADDR(parent->sas_addr),
626 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
627 sizeof(struct dev_to_host_fis));
629 res = sas_discover_sata(child);
631 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
632 "%016llx:0x%x returned 0x%x\n",
633 SAS_ADDR(child->sas_addr),
634 SAS_ADDR(parent->sas_addr), phy_id, res);
637 } else if (phy->attached_tproto & SAS_PROTO_SSP) {
638 child->dev_type = SAS_END_DEV;
639 rphy = sas_end_device_alloc(phy->port);
640 /* FIXME: error handling */
643 child->tproto = phy->attached_tproto;
647 sas_fill_in_rphy(child, rphy);
649 spin_lock(&parent->port->dev_list_lock);
650 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
651 spin_unlock(&parent->port->dev_list_lock);
653 res = sas_discover_end_dev(child);
655 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
656 "at %016llx:0x%x returned 0x%x\n",
657 SAS_ADDR(child->sas_addr),
658 SAS_ADDR(parent->sas_addr), phy_id, res);
662 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
663 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
667 list_add_tail(&child->siblings, &parent_ex->children);
671 sas_rphy_free(child->rphy);
673 list_del(&child->dev_list_node);
675 sas_port_delete(phy->port);
682 /* See if this phy is part of a wide port */
683 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
685 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
688 for (i = 0; i < parent->ex_dev.num_phys; i++) {
689 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
694 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
695 SAS_ADDR_SIZE) && ephy->port) {
696 sas_port_add_phy(ephy->port, phy->phy);
697 phy->phy_state = PHY_DEVICE_DISCOVERED;
705 static struct domain_device *sas_ex_discover_expander(
706 struct domain_device *parent, int phy_id)
708 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
709 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
710 struct domain_device *child = NULL;
711 struct sas_rphy *rphy;
712 struct sas_expander_device *edev;
713 struct asd_sas_port *port;
716 if (phy->routing_attr == DIRECT_ROUTING) {
717 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
719 SAS_ADDR(parent->sas_addr), phy_id,
720 SAS_ADDR(phy->attached_sas_addr),
721 phy->attached_phy_id);
724 child = kzalloc(sizeof(*child), GFP_KERNEL);
728 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
729 /* FIXME: better error handling */
730 BUG_ON(sas_port_add(phy->port) != 0);
733 switch (phy->attached_dev_type) {
735 rphy = sas_expander_alloc(phy->port,
736 SAS_EDGE_EXPANDER_DEVICE);
739 rphy = sas_expander_alloc(phy->port,
740 SAS_FANOUT_EXPANDER_DEVICE);
743 rphy = NULL; /* shut gcc up */
748 edev = rphy_to_expander_device(rphy);
749 child->dev_type = phy->attached_dev_type;
750 child->parent = parent;
752 child->iproto = phy->attached_iproto;
753 child->tproto = phy->attached_tproto;
754 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
755 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
756 sas_ex_get_linkrate(parent, child, phy);
757 edev->level = parent_ex->level + 1;
758 parent->port->disc.max_level = max(parent->port->disc.max_level,
761 sas_fill_in_rphy(child, rphy);
764 spin_lock(&parent->port->dev_list_lock);
765 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
766 spin_unlock(&parent->port->dev_list_lock);
768 res = sas_discover_expander(child);
773 list_add_tail(&child->siblings, &parent->ex_dev.children);
777 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
779 struct expander_device *ex = &dev->ex_dev;
780 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
781 struct domain_device *child = NULL;
785 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
786 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
787 res = sas_ex_phy_discover(dev, phy_id);
792 /* Parent and domain coherency */
793 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
794 SAS_ADDR(dev->port->sas_addr))) {
795 sas_add_parent_port(dev, phy_id);
798 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
799 SAS_ADDR(dev->parent->sas_addr))) {
800 sas_add_parent_port(dev, phy_id);
801 if (ex_phy->routing_attr == TABLE_ROUTING)
802 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
806 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
807 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
809 if (ex_phy->attached_dev_type == NO_DEVICE) {
810 if (ex_phy->routing_attr == DIRECT_ROUTING) {
811 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
812 sas_configure_routing(dev, ex_phy->attached_sas_addr);
815 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
818 if (ex_phy->attached_dev_type != SAS_END_DEV &&
819 ex_phy->attached_dev_type != FANOUT_DEV &&
820 ex_phy->attached_dev_type != EDGE_DEV) {
821 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
822 "phy 0x%x\n", ex_phy->attached_dev_type,
823 SAS_ADDR(dev->sas_addr),
828 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
830 SAS_DPRINTK("configure routing for dev %016llx "
831 "reported 0x%x. Forgotten\n",
832 SAS_ADDR(ex_phy->attached_sas_addr), res);
833 sas_disable_routing(dev, ex_phy->attached_sas_addr);
837 res = sas_ex_join_wide_port(dev, phy_id);
839 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
840 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
844 switch (ex_phy->attached_dev_type) {
846 child = sas_ex_discover_end_dev(dev, phy_id);
849 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
850 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
851 "attached to ex %016llx phy 0x%x\n",
852 SAS_ADDR(ex_phy->attached_sas_addr),
853 ex_phy->attached_phy_id,
854 SAS_ADDR(dev->sas_addr),
856 sas_ex_disable_phy(dev, phy_id);
859 memcpy(dev->port->disc.fanout_sas_addr,
860 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
863 child = sas_ex_discover_expander(dev, phy_id);
872 for (i = 0; i < ex->num_phys; i++) {
873 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
874 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
877 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
878 SAS_ADDR(child->sas_addr))
879 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
886 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
888 struct expander_device *ex = &dev->ex_dev;
891 for (i = 0; i < ex->num_phys; i++) {
892 struct ex_phy *phy = &ex->ex_phy[i];
894 if (phy->phy_state == PHY_VACANT ||
895 phy->phy_state == PHY_NOT_PRESENT)
898 if ((phy->attached_dev_type == EDGE_DEV ||
899 phy->attached_dev_type == FANOUT_DEV) &&
900 phy->routing_attr == SUBTRACTIVE_ROUTING) {
902 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
910 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
912 struct expander_device *ex = &dev->ex_dev;
913 struct domain_device *child;
914 u8 sub_addr[8] = {0, };
916 list_for_each_entry(child, &ex->children, siblings) {
917 if (child->dev_type != EDGE_DEV &&
918 child->dev_type != FANOUT_DEV)
920 if (sub_addr[0] == 0) {
921 sas_find_sub_addr(child, sub_addr);
926 if (sas_find_sub_addr(child, s2) &&
927 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
929 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
930 "diverges from subtractive "
931 "boundary %016llx\n",
932 SAS_ADDR(dev->sas_addr),
933 SAS_ADDR(child->sas_addr),
937 sas_ex_disable_port(child, s2);
944 * sas_ex_discover_devices -- discover devices attached to this expander
945 * dev: pointer to the expander domain device
946 * single: if you want to do a single phy, else set to -1;
948 * Configure this expander for use with its devices and register the
949 * devices of this expander.
951 static int sas_ex_discover_devices(struct domain_device *dev, int single)
953 struct expander_device *ex = &dev->ex_dev;
954 int i = 0, end = ex->num_phys;
957 if (0 <= single && single < end) {
962 for ( ; i < end; i++) {
963 struct ex_phy *ex_phy = &ex->ex_phy[i];
965 if (ex_phy->phy_state == PHY_VACANT ||
966 ex_phy->phy_state == PHY_NOT_PRESENT ||
967 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
970 switch (ex_phy->linkrate) {
971 case SAS_PHY_DISABLED:
972 case SAS_PHY_RESET_PROBLEM:
973 case SAS_SATA_PORT_SELECTOR:
976 res = sas_ex_discover_dev(dev, i);
984 sas_check_level_subtractive_boundary(dev);
989 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
991 struct expander_device *ex = &dev->ex_dev;
993 u8 *sub_sas_addr = NULL;
995 if (dev->dev_type != EDGE_DEV)
998 for (i = 0; i < ex->num_phys; i++) {
999 struct ex_phy *phy = &ex->ex_phy[i];
1001 if (phy->phy_state == PHY_VACANT ||
1002 phy->phy_state == PHY_NOT_PRESENT)
1005 if ((phy->attached_dev_type == FANOUT_DEV ||
1006 phy->attached_dev_type == EDGE_DEV) &&
1007 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1010 sub_sas_addr = &phy->attached_sas_addr[0];
1011 else if (SAS_ADDR(sub_sas_addr) !=
1012 SAS_ADDR(phy->attached_sas_addr)) {
1014 SAS_DPRINTK("ex %016llx phy 0x%x "
1015 "diverges(%016llx) on subtractive "
1016 "boundary(%016llx). Disabled\n",
1017 SAS_ADDR(dev->sas_addr), i,
1018 SAS_ADDR(phy->attached_sas_addr),
1019 SAS_ADDR(sub_sas_addr));
1020 sas_ex_disable_phy(dev, i);
1027 static void sas_print_parent_topology_bug(struct domain_device *child,
1028 struct ex_phy *parent_phy,
1029 struct ex_phy *child_phy)
1031 static const char ra_char[] = {
1032 [DIRECT_ROUTING] = 'D',
1033 [SUBTRACTIVE_ROUTING] = 'S',
1034 [TABLE_ROUTING] = 'T',
1036 static const char *ex_type[] = {
1037 [EDGE_DEV] = "edge",
1038 [FANOUT_DEV] = "fanout",
1040 struct domain_device *parent = child->parent;
1042 sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x "
1043 "has %c:%c routing link!\n",
1045 ex_type[parent->dev_type],
1046 SAS_ADDR(parent->sas_addr),
1049 ex_type[child->dev_type],
1050 SAS_ADDR(child->sas_addr),
1053 ra_char[parent_phy->routing_attr],
1054 ra_char[child_phy->routing_attr]);
1057 static int sas_check_eeds(struct domain_device *child,
1058 struct ex_phy *parent_phy,
1059 struct ex_phy *child_phy)
1062 struct domain_device *parent = child->parent;
1064 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1066 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1067 "phy S:0x%x, while there is a fanout ex %016llx\n",
1068 SAS_ADDR(parent->sas_addr),
1070 SAS_ADDR(child->sas_addr),
1072 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1073 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1074 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1076 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1078 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1079 SAS_ADDR(parent->sas_addr)) ||
1080 (SAS_ADDR(parent->port->disc.eeds_a) ==
1081 SAS_ADDR(child->sas_addr)))
1083 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1084 SAS_ADDR(parent->sas_addr)) ||
1085 (SAS_ADDR(parent->port->disc.eeds_b) ==
1086 SAS_ADDR(child->sas_addr))))
1090 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1091 "phy 0x%x link forms a third EEDS!\n",
1092 SAS_ADDR(parent->sas_addr),
1094 SAS_ADDR(child->sas_addr),
1101 /* Here we spill over 80 columns. It is intentional.
1103 static int sas_check_parent_topology(struct domain_device *child)
1105 struct expander_device *child_ex = &child->ex_dev;
1106 struct expander_device *parent_ex;
1113 if (child->parent->dev_type != EDGE_DEV &&
1114 child->parent->dev_type != FANOUT_DEV)
1117 parent_ex = &child->parent->ex_dev;
1119 for (i = 0; i < parent_ex->num_phys; i++) {
1120 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1121 struct ex_phy *child_phy;
1123 if (parent_phy->phy_state == PHY_VACANT ||
1124 parent_phy->phy_state == PHY_NOT_PRESENT)
1127 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1130 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1132 switch (child->parent->dev_type) {
1134 if (child->dev_type == FANOUT_DEV) {
1135 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1136 child_phy->routing_attr != TABLE_ROUTING) {
1137 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1140 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1141 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1142 res = sas_check_eeds(child, parent_phy, child_phy);
1143 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1144 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1147 } else if (parent_phy->routing_attr == TABLE_ROUTING &&
1148 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1149 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1154 if (parent_phy->routing_attr != TABLE_ROUTING ||
1155 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1156 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1168 #define RRI_REQ_SIZE 16
1169 #define RRI_RESP_SIZE 44
1171 static int sas_configure_present(struct domain_device *dev, int phy_id,
1172 u8 *sas_addr, int *index, int *present)
1175 struct expander_device *ex = &dev->ex_dev;
1176 struct ex_phy *phy = &ex->ex_phy[phy_id];
1183 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1187 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1193 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1194 rri_req[9] = phy_id;
1196 for (i = 0; i < ex->max_route_indexes ; i++) {
1197 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1198 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1203 if (res == SMP_RESP_NO_INDEX) {
1204 SAS_DPRINTK("overflow of indexes: dev %016llx "
1205 "phy 0x%x index 0x%x\n",
1206 SAS_ADDR(dev->sas_addr), phy_id, i);
1208 } else if (res != SMP_RESP_FUNC_ACC) {
1209 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1210 "result 0x%x\n", __FUNCTION__,
1211 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1214 if (SAS_ADDR(sas_addr) != 0) {
1215 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1217 if ((rri_resp[12] & 0x80) == 0x80)
1222 } else if (SAS_ADDR(rri_resp+16) == 0) {
1227 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1228 phy->last_da_index < i) {
1229 phy->last_da_index = i;
1242 #define CRI_REQ_SIZE 44
1243 #define CRI_RESP_SIZE 8
1245 static int sas_configure_set(struct domain_device *dev, int phy_id,
1246 u8 *sas_addr, int index, int include)
1252 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1256 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1262 cri_req[1] = SMP_CONF_ROUTE_INFO;
1263 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1264 cri_req[9] = phy_id;
1265 if (SAS_ADDR(sas_addr) == 0 || !include)
1266 cri_req[12] |= 0x80;
1267 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1269 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1274 if (res == SMP_RESP_NO_INDEX) {
1275 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1277 SAS_ADDR(dev->sas_addr), phy_id, index);
1285 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1286 u8 *sas_addr, int include)
1292 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1295 if (include ^ present)
1296 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1302 * sas_configure_parent -- configure routing table of parent
1303 * parent: parent expander
1304 * child: child expander
1305 * sas_addr: SAS port identifier of device directly attached to child
1307 static int sas_configure_parent(struct domain_device *parent,
1308 struct domain_device *child,
1309 u8 *sas_addr, int include)
1311 struct expander_device *ex_parent = &parent->ex_dev;
1315 if (parent->parent) {
1316 res = sas_configure_parent(parent->parent, parent, sas_addr,
1322 if (ex_parent->conf_route_table == 0) {
1323 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1324 SAS_ADDR(parent->sas_addr));
1328 for (i = 0; i < ex_parent->num_phys; i++) {
1329 struct ex_phy *phy = &ex_parent->ex_phy[i];
1331 if ((phy->routing_attr == TABLE_ROUTING) &&
1332 (SAS_ADDR(phy->attached_sas_addr) ==
1333 SAS_ADDR(child->sas_addr))) {
1334 res = sas_configure_phy(parent, i, sas_addr, include);
1344 * sas_configure_routing -- configure routing
1345 * dev: expander device
1346 * sas_addr: port identifier of device directly attached to the expander device
1348 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1351 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1355 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1358 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1363 #define SMP_BIN_ATTR_NAME "smp_portal"
1365 static void sas_ex_smp_hook(struct domain_device *dev)
1367 struct expander_device *ex_dev = &dev->ex_dev;
1368 struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr;
1370 memset(bin_attr, 0, sizeof(*bin_attr));
1372 bin_attr->attr.name = SMP_BIN_ATTR_NAME;
1373 bin_attr->attr.mode = 0600;
1376 bin_attr->private = NULL;
1377 bin_attr->read = smp_portal_read;
1378 bin_attr->write= smp_portal_write;
1379 bin_attr->mmap = NULL;
1381 ex_dev->smp_portal_pid = -1;
1382 init_MUTEX(&ex_dev->smp_sema);
1387 * sas_discover_expander -- expander discovery
1388 * @ex: pointer to expander domain device
1390 * See comment in sas_discover_sata().
1392 static int sas_discover_expander(struct domain_device *dev)
1396 res = sas_notify_lldd_dev_found(dev);
1400 res = sas_ex_general(dev);
1403 res = sas_ex_manuf_info(dev);
1407 res = sas_expander_discover(dev);
1409 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1410 SAS_ADDR(dev->sas_addr), res);
1414 sas_check_ex_subtractive_boundary(dev);
1415 res = sas_check_parent_topology(dev);
1420 sas_notify_lldd_dev_gone(dev);
1424 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1427 struct domain_device *dev;
1429 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1430 if (dev->dev_type == EDGE_DEV ||
1431 dev->dev_type == FANOUT_DEV) {
1432 struct sas_expander_device *ex =
1433 rphy_to_expander_device(dev->rphy);
1435 if (level == ex->level)
1436 res = sas_ex_discover_devices(dev, -1);
1438 res = sas_ex_discover_devices(port->port_dev, -1);
1446 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1452 level = port->disc.max_level;
1453 res = sas_ex_level_discovery(port, level);
1455 } while (level < port->disc.max_level);
1460 int sas_discover_root_expander(struct domain_device *dev)
1463 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1465 res = sas_rphy_add(dev->rphy);
1469 ex->level = dev->port->disc.max_level; /* 0 */
1470 res = sas_discover_expander(dev);
1474 sas_ex_bfs_disc(dev->port);
1479 sas_rphy_remove(dev->rphy);
1484 /* ---------- Domain revalidation ---------- */
1486 static int sas_get_phy_discover(struct domain_device *dev,
1487 int phy_id, struct smp_resp *disc_resp)
1492 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1496 disc_req[1] = SMP_DISCOVER;
1497 disc_req[9] = phy_id;
1499 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1500 disc_resp, DISCOVER_RESP_SIZE);
1503 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1504 res = disc_resp->result;
1512 static int sas_get_phy_change_count(struct domain_device *dev,
1513 int phy_id, int *pcc)
1516 struct smp_resp *disc_resp;
1518 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1522 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1524 *pcc = disc_resp->disc.change_count;
1530 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1531 int phy_id, u8 *attached_sas_addr)
1534 struct smp_resp *disc_resp;
1535 struct discover_resp *dr;
1537 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1540 dr = &disc_resp->disc;
1542 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1544 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1545 if (dr->attached_dev_type == 0)
1546 memset(attached_sas_addr, 0, 8);
1552 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1555 struct expander_device *ex = &dev->ex_dev;
1559 for (i = from_phy; i < ex->num_phys; i++) {
1560 int phy_change_count = 0;
1562 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1565 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1566 ex->ex_phy[i].phy_change_count = phy_change_count;
1575 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1579 struct smp_resp *rg_resp;
1581 rg_req = alloc_smp_req(RG_REQ_SIZE);
1585 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1591 rg_req[1] = SMP_REPORT_GENERAL;
1593 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1597 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1598 res = rg_resp->result;
1602 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1609 static int sas_find_bcast_dev(struct domain_device *dev,
1610 struct domain_device **src_dev)
1612 struct expander_device *ex = &dev->ex_dev;
1613 int ex_change_count = -1;
1616 res = sas_get_ex_change_count(dev, &ex_change_count);
1619 if (ex_change_count != -1 &&
1620 ex_change_count != ex->ex_change_count) {
1622 ex->ex_change_count = ex_change_count;
1624 struct domain_device *ch;
1626 list_for_each_entry(ch, &ex->children, siblings) {
1627 if (ch->dev_type == EDGE_DEV ||
1628 ch->dev_type == FANOUT_DEV) {
1629 res = sas_find_bcast_dev(ch, src_dev);
1639 static void sas_unregister_ex_tree(struct domain_device *dev)
1641 struct expander_device *ex = &dev->ex_dev;
1642 struct domain_device *child, *n;
1644 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1645 if (child->dev_type == EDGE_DEV ||
1646 child->dev_type == FANOUT_DEV)
1647 sas_unregister_ex_tree(child);
1649 sas_unregister_dev(child);
1651 sas_unregister_dev(dev);
1654 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1657 struct expander_device *ex_dev = &parent->ex_dev;
1658 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1659 struct domain_device *child, *n;
1661 list_for_each_entry_safe(child, n, &ex_dev->children, siblings) {
1662 if (SAS_ADDR(child->sas_addr) ==
1663 SAS_ADDR(phy->attached_sas_addr)) {
1664 if (child->dev_type == EDGE_DEV ||
1665 child->dev_type == FANOUT_DEV)
1666 sas_unregister_ex_tree(child);
1668 sas_unregister_dev(child);
1672 sas_disable_routing(parent, phy->attached_sas_addr);
1673 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1674 sas_port_delete_phy(phy->port, phy->phy);
1675 if (phy->port->num_phys == 0)
1676 sas_port_delete(phy->port);
1680 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1683 struct expander_device *ex_root = &root->ex_dev;
1684 struct domain_device *child;
1687 list_for_each_entry(child, &ex_root->children, siblings) {
1688 if (child->dev_type == EDGE_DEV ||
1689 child->dev_type == FANOUT_DEV) {
1690 struct sas_expander_device *ex =
1691 rphy_to_expander_device(child->rphy);
1693 if (level > ex->level)
1694 res = sas_discover_bfs_by_root_level(child,
1696 else if (level == ex->level)
1697 res = sas_ex_discover_devices(child, -1);
1703 static int sas_discover_bfs_by_root(struct domain_device *dev)
1706 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1707 int level = ex->level+1;
1709 res = sas_ex_discover_devices(dev, -1);
1713 res = sas_discover_bfs_by_root_level(dev, level);
1716 } while (level <= dev->port->disc.max_level);
1721 static int sas_discover_new(struct domain_device *dev, int phy_id)
1723 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1724 struct domain_device *child;
1727 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1728 SAS_ADDR(dev->sas_addr), phy_id);
1729 res = sas_ex_phy_discover(dev, phy_id);
1732 res = sas_ex_discover_devices(dev, phy_id);
1735 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1736 if (SAS_ADDR(child->sas_addr) ==
1737 SAS_ADDR(ex_phy->attached_sas_addr)) {
1738 if (child->dev_type == EDGE_DEV ||
1739 child->dev_type == FANOUT_DEV)
1740 res = sas_discover_bfs_by_root(child);
1748 static int sas_rediscover_dev(struct domain_device *dev, int phy_id)
1750 struct expander_device *ex = &dev->ex_dev;
1751 struct ex_phy *phy = &ex->ex_phy[phy_id];
1752 u8 attached_sas_addr[8];
1755 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1757 case SMP_RESP_NO_PHY:
1758 phy->phy_state = PHY_NOT_PRESENT;
1759 sas_unregister_devs_sas_addr(dev, phy_id);
1761 case SMP_RESP_PHY_VACANT:
1762 phy->phy_state = PHY_VACANT;
1763 sas_unregister_devs_sas_addr(dev, phy_id);
1765 case SMP_RESP_FUNC_ACC:
1769 if (SAS_ADDR(attached_sas_addr) == 0) {
1770 phy->phy_state = PHY_EMPTY;
1771 sas_unregister_devs_sas_addr(dev, phy_id);
1772 } else if (SAS_ADDR(attached_sas_addr) ==
1773 SAS_ADDR(phy->attached_sas_addr)) {
1774 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1775 SAS_ADDR(dev->sas_addr), phy_id);
1776 sas_ex_phy_discover(dev, phy_id);
1778 res = sas_discover_new(dev, phy_id);
1783 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1785 struct expander_device *ex = &dev->ex_dev;
1786 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1790 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1791 SAS_ADDR(dev->sas_addr), phy_id);
1793 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1794 for (i = 0; i < ex->num_phys; i++) {
1795 struct ex_phy *phy = &ex->ex_phy[i];
1799 if (SAS_ADDR(phy->attached_sas_addr) ==
1800 SAS_ADDR(changed_phy->attached_sas_addr)) {
1801 SAS_DPRINTK("phy%d part of wide port with "
1802 "phy%d\n", phy_id, i);
1806 res = sas_rediscover_dev(dev, phy_id);
1808 res = sas_discover_new(dev, phy_id);
1814 * sas_revalidate_domain -- revalidate the domain
1815 * @port: port to the domain of interest
1817 * NOTE: this process _must_ quit (return) as soon as any connection
1818 * errors are encountered. Connection recovery is done elsewhere.
1819 * Discover process only interrogates devices in order to discover the
1822 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1825 struct domain_device *dev = NULL;
1827 res = sas_find_bcast_dev(port_dev, &dev);
1831 struct expander_device *ex = &dev->ex_dev;
1836 res = sas_find_bcast_phy(dev, &phy_id, i);
1839 res = sas_rediscover(dev, phy_id);
1841 } while (i < ex->num_phys);
1848 /* ---------- SMP portal ---------- */
1850 static ssize_t smp_portal_write(struct kobject *kobj,
1851 struct bin_attribute *bin_attr,
1852 char *buf, loff_t offs, size_t size)
1854 struct domain_device *dev = to_dom_device(kobj);
1855 struct expander_device *ex = &dev->ex_dev;
1862 down_interruptible(&ex->smp_sema);
1865 ex->smp_req = kzalloc(size, GFP_USER);
1870 memcpy(ex->smp_req, buf, size);
1871 ex->smp_req_size = size;
1872 ex->smp_portal_pid = current->pid;
1878 static ssize_t smp_portal_read(struct kobject *kobj,
1879 struct bin_attribute *bin_attr,
1880 char *buf, loff_t offs, size_t size)
1882 struct domain_device *dev = to_dom_device(kobj);
1883 struct expander_device *ex = &dev->ex_dev;
1887 /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact
1891 down_interruptible(&ex->smp_sema);
1892 if (!ex->smp_req || ex->smp_portal_pid != current->pid)
1900 smp_resp = alloc_smp_resp(size);
1903 res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size,
1906 memcpy(buf, smp_resp, size);
1914 ex->smp_req_size = 0;
1915 ex->smp_portal_pid = -1;