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 /* help some expanders that fail to zero sas_address in the 'no
198 if (phy->attached_dev_type == NO_DEVICE ||
199 phy->linkrate < SAS_LINK_RATE_1_5_GBPS)
200 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
202 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
203 phy->attached_phy_id = dr->attached_phy_id;
204 phy->phy_change_count = dr->change_count;
205 phy->routing_attr = dr->routing_attr;
206 phy->virtual = dr->virtual;
207 phy->last_da_index = -1;
209 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
210 phy->phy->identify.device_type = phy->attached_dev_type;
211 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
212 phy->phy->identify.target_port_protocols = phy->attached_tproto;
213 phy->phy->identify.phy_identifier = phy_id;
214 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
215 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
216 phy->phy->minimum_linkrate = dr->pmin_linkrate;
217 phy->phy->maximum_linkrate = dr->pmax_linkrate;
218 phy->phy->negotiated_linkrate = phy->linkrate;
221 if (sas_phy_add(phy->phy)) {
222 sas_phy_free(phy->phy);
226 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
227 SAS_ADDR(dev->sas_addr), phy->phy_id,
228 phy->routing_attr == TABLE_ROUTING ? 'T' :
229 phy->routing_attr == DIRECT_ROUTING ? 'D' :
230 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
231 SAS_ADDR(phy->attached_sas_addr));
236 #define DISCOVER_REQ_SIZE 16
237 #define DISCOVER_RESP_SIZE 56
239 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
240 u8 *disc_resp, int single)
244 disc_req[9] = single;
245 for (i = 1 ; i < 3; i++) {
246 struct discover_resp *dr;
248 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
249 disc_resp, DISCOVER_RESP_SIZE);
252 /* This is detecting a failure to transmit initial
253 * dev to host FIS as described in section G.5 of
255 dr = &((struct smp_resp *)disc_resp)->disc;
256 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
257 SAS_ADDR_SIZE) == 0) {
258 sas_printk("Found loopback topology, just ignore it!\n");
261 if (!(dr->attached_dev_type == 0 &&
262 dr->attached_sata_dev))
264 /* In order to generate the dev to host FIS, we
265 * send a link reset to the expander port */
266 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
267 /* Wait for the reset to trigger the negotiation */
270 sas_set_ex_phy(dev, single, disc_resp);
274 static int sas_ex_phy_discover(struct domain_device *dev, int single)
276 struct expander_device *ex = &dev->ex_dev;
281 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
285 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
291 disc_req[1] = SMP_DISCOVER;
293 if (0 <= single && single < ex->num_phys) {
294 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
298 for (i = 0; i < ex->num_phys; i++) {
299 res = sas_ex_phy_discover_helper(dev, disc_req,
311 static int sas_expander_discover(struct domain_device *dev)
313 struct expander_device *ex = &dev->ex_dev;
316 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
320 res = sas_ex_phy_discover(dev, -1);
331 #define MAX_EXPANDER_PHYS 128
333 static void ex_assign_report_general(struct domain_device *dev,
334 struct smp_resp *resp)
336 struct report_general_resp *rg = &resp->rg;
338 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
339 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
340 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
341 dev->ex_dev.t2t_supp = rg->t2t_supp;
342 dev->ex_dev.conf_route_table = rg->conf_route_table;
343 dev->ex_dev.configuring = rg->configuring;
344 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
347 #define RG_REQ_SIZE 8
348 #define RG_RESP_SIZE 32
350 static int sas_ex_general(struct domain_device *dev)
353 struct smp_resp *rg_resp;
357 rg_req = alloc_smp_req(RG_REQ_SIZE);
361 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
367 rg_req[1] = SMP_REPORT_GENERAL;
369 for (i = 0; i < 5; i++) {
370 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
374 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
375 SAS_ADDR(dev->sas_addr), res);
377 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
378 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
379 SAS_ADDR(dev->sas_addr), rg_resp->result);
380 res = rg_resp->result;
384 ex_assign_report_general(dev, rg_resp);
386 if (dev->ex_dev.configuring) {
387 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
388 SAS_ADDR(dev->sas_addr));
389 schedule_timeout_interruptible(5*HZ);
399 static void ex_assign_manuf_info(struct domain_device *dev, void
402 u8 *mi_resp = _mi_resp;
403 struct sas_rphy *rphy = dev->rphy;
404 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
406 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
407 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
408 memcpy(edev->product_rev, mi_resp + 36,
409 SAS_EXPANDER_PRODUCT_REV_LEN);
411 if (mi_resp[8] & 1) {
412 memcpy(edev->component_vendor_id, mi_resp + 40,
413 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
414 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
415 edev->component_revision_id = mi_resp[50];
419 #define MI_REQ_SIZE 8
420 #define MI_RESP_SIZE 64
422 static int sas_ex_manuf_info(struct domain_device *dev)
428 mi_req = alloc_smp_req(MI_REQ_SIZE);
432 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
438 mi_req[1] = SMP_REPORT_MANUF_INFO;
440 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
442 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
443 SAS_ADDR(dev->sas_addr), res);
445 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
446 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
447 SAS_ADDR(dev->sas_addr), mi_resp[2]);
451 ex_assign_manuf_info(dev, mi_resp);
458 #define PC_REQ_SIZE 44
459 #define PC_RESP_SIZE 8
461 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
462 enum phy_func phy_func,
463 struct sas_phy_linkrates *rates)
469 pc_req = alloc_smp_req(PC_REQ_SIZE);
473 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
479 pc_req[1] = SMP_PHY_CONTROL;
481 pc_req[10]= phy_func;
483 pc_req[32] = rates->minimum_linkrate << 4;
484 pc_req[33] = rates->maximum_linkrate << 4;
487 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
494 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
496 struct expander_device *ex = &dev->ex_dev;
497 struct ex_phy *phy = &ex->ex_phy[phy_id];
499 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
500 phy->linkrate = SAS_PHY_DISABLED;
503 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
505 struct expander_device *ex = &dev->ex_dev;
508 for (i = 0; i < ex->num_phys; i++) {
509 struct ex_phy *phy = &ex->ex_phy[i];
511 if (phy->phy_state == PHY_VACANT ||
512 phy->phy_state == PHY_NOT_PRESENT)
515 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
516 sas_ex_disable_phy(dev, i);
520 static int sas_dev_present_in_domain(struct asd_sas_port *port,
523 struct domain_device *dev;
525 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
527 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
528 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
534 #define RPEL_REQ_SIZE 16
535 #define RPEL_RESP_SIZE 32
536 int sas_smp_get_phy_events(struct sas_phy *phy)
541 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
542 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
544 req = alloc_smp_req(RPEL_REQ_SIZE);
548 resp = alloc_smp_resp(RPEL_RESP_SIZE);
554 req[1] = SMP_REPORT_PHY_ERR_LOG;
555 req[9] = phy->number;
557 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
558 resp, RPEL_RESP_SIZE);
563 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
564 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
565 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
566 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
574 #ifdef CONFIG_SCSI_SAS_ATA
576 #define RPS_REQ_SIZE 16
577 #define RPS_RESP_SIZE 60
579 static int sas_get_report_phy_sata(struct domain_device *dev,
581 struct smp_resp *rps_resp)
584 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
585 u8 *resp = (u8 *)rps_resp;
590 rps_req[1] = SMP_REPORT_PHY_SATA;
593 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
594 rps_resp, RPS_RESP_SIZE);
596 /* 0x34 is the FIS type for the D2H fis. There's a potential
597 * standards cockup here. sas-2 explicitly specifies the FIS
598 * should be encoded so that FIS type is in resp[24].
599 * However, some expanders endian reverse this. Undo the
601 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
604 for (i = 0; i < 5; i++) {
609 resp[j + 0] = resp[j + 3];
610 resp[j + 1] = resp[j + 2];
621 static void sas_ex_get_linkrate(struct domain_device *parent,
622 struct domain_device *child,
623 struct ex_phy *parent_phy)
625 struct expander_device *parent_ex = &parent->ex_dev;
626 struct sas_port *port;
631 port = parent_phy->port;
633 for (i = 0; i < parent_ex->num_phys; i++) {
634 struct ex_phy *phy = &parent_ex->ex_phy[i];
636 if (phy->phy_state == PHY_VACANT ||
637 phy->phy_state == PHY_NOT_PRESENT)
640 if (SAS_ADDR(phy->attached_sas_addr) ==
641 SAS_ADDR(child->sas_addr)) {
643 child->min_linkrate = min(parent->min_linkrate,
645 child->max_linkrate = max(parent->max_linkrate,
648 sas_port_add_phy(port, phy->phy);
651 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
652 child->pathways = min(child->pathways, parent->pathways);
655 static struct domain_device *sas_ex_discover_end_dev(
656 struct domain_device *parent, int phy_id)
658 struct expander_device *parent_ex = &parent->ex_dev;
659 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
660 struct domain_device *child = NULL;
661 struct sas_rphy *rphy;
664 if (phy->attached_sata_host || phy->attached_sata_ps)
667 child = kzalloc(sizeof(*child), GFP_KERNEL);
671 child->parent = parent;
672 child->port = parent->port;
673 child->iproto = phy->attached_iproto;
674 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
675 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
677 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
678 if (unlikely(!phy->port))
680 if (unlikely(sas_port_add(phy->port) != 0)) {
681 sas_port_free(phy->port);
685 sas_ex_get_linkrate(parent, child, phy);
687 #ifdef CONFIG_SCSI_SAS_ATA
688 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
689 child->dev_type = SATA_DEV;
690 if (phy->attached_tproto & SAS_PROTOCOL_STP)
691 child->tproto = phy->attached_tproto;
692 if (phy->attached_sata_dev)
693 child->tproto |= SATA_DEV;
694 res = sas_get_report_phy_sata(parent, phy_id,
695 &child->sata_dev.rps_resp);
697 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
698 "0x%x\n", SAS_ADDR(parent->sas_addr),
702 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
703 sizeof(struct dev_to_host_fis));
705 rphy = sas_end_device_alloc(phy->port);
713 spin_lock_irq(&parent->port->dev_list_lock);
714 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
715 spin_unlock_irq(&parent->port->dev_list_lock);
717 res = sas_discover_sata(child);
719 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
720 "%016llx:0x%x returned 0x%x\n",
721 SAS_ADDR(child->sas_addr),
722 SAS_ADDR(parent->sas_addr), phy_id, res);
727 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
728 child->dev_type = SAS_END_DEV;
729 rphy = sas_end_device_alloc(phy->port);
730 /* FIXME: error handling */
733 child->tproto = phy->attached_tproto;
737 sas_fill_in_rphy(child, rphy);
739 spin_lock_irq(&parent->port->dev_list_lock);
740 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
741 spin_unlock_irq(&parent->port->dev_list_lock);
743 res = sas_discover_end_dev(child);
745 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
746 "at %016llx:0x%x returned 0x%x\n",
747 SAS_ADDR(child->sas_addr),
748 SAS_ADDR(parent->sas_addr), phy_id, res);
752 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
753 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
758 list_add_tail(&child->siblings, &parent_ex->children);
762 sas_rphy_free(child->rphy);
765 spin_lock_irq(&parent->port->dev_list_lock);
766 list_del(&child->dev_list_node);
767 spin_unlock_irq(&parent->port->dev_list_lock);
769 sas_port_delete(phy->port);
776 /* See if this phy is part of a wide port */
777 static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
779 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
782 for (i = 0; i < parent->ex_dev.num_phys; i++) {
783 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
788 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
789 SAS_ADDR_SIZE) && ephy->port) {
790 sas_port_add_phy(ephy->port, phy->phy);
791 phy->port = ephy->port;
792 phy->phy_state = PHY_DEVICE_DISCOVERED;
800 static struct domain_device *sas_ex_discover_expander(
801 struct domain_device *parent, int phy_id)
803 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
804 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
805 struct domain_device *child = NULL;
806 struct sas_rphy *rphy;
807 struct sas_expander_device *edev;
808 struct asd_sas_port *port;
811 if (phy->routing_attr == DIRECT_ROUTING) {
812 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
814 SAS_ADDR(parent->sas_addr), phy_id,
815 SAS_ADDR(phy->attached_sas_addr),
816 phy->attached_phy_id);
819 child = kzalloc(sizeof(*child), GFP_KERNEL);
823 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
824 /* FIXME: better error handling */
825 BUG_ON(sas_port_add(phy->port) != 0);
828 switch (phy->attached_dev_type) {
830 rphy = sas_expander_alloc(phy->port,
831 SAS_EDGE_EXPANDER_DEVICE);
834 rphy = sas_expander_alloc(phy->port,
835 SAS_FANOUT_EXPANDER_DEVICE);
838 rphy = NULL; /* shut gcc up */
843 edev = rphy_to_expander_device(rphy);
844 child->dev_type = phy->attached_dev_type;
845 child->parent = parent;
847 child->iproto = phy->attached_iproto;
848 child->tproto = phy->attached_tproto;
849 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
850 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
851 sas_ex_get_linkrate(parent, child, phy);
852 edev->level = parent_ex->level + 1;
853 parent->port->disc.max_level = max(parent->port->disc.max_level,
856 sas_fill_in_rphy(child, rphy);
859 spin_lock_irq(&parent->port->dev_list_lock);
860 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
861 spin_unlock_irq(&parent->port->dev_list_lock);
863 res = sas_discover_expander(child);
865 spin_lock_irq(&parent->port->dev_list_lock);
866 list_del(&child->dev_list_node);
867 spin_unlock_irq(&parent->port->dev_list_lock);
871 list_add_tail(&child->siblings, &parent->ex_dev.children);
875 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
877 struct expander_device *ex = &dev->ex_dev;
878 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
879 struct domain_device *child = NULL;
883 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
884 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
885 res = sas_ex_phy_discover(dev, phy_id);
890 /* Parent and domain coherency */
891 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
892 SAS_ADDR(dev->port->sas_addr))) {
893 sas_add_parent_port(dev, phy_id);
896 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
897 SAS_ADDR(dev->parent->sas_addr))) {
898 sas_add_parent_port(dev, phy_id);
899 if (ex_phy->routing_attr == TABLE_ROUTING)
900 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
904 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
905 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
907 if (ex_phy->attached_dev_type == NO_DEVICE) {
908 if (ex_phy->routing_attr == DIRECT_ROUTING) {
909 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
910 sas_configure_routing(dev, ex_phy->attached_sas_addr);
913 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
916 if (ex_phy->attached_dev_type != SAS_END_DEV &&
917 ex_phy->attached_dev_type != FANOUT_DEV &&
918 ex_phy->attached_dev_type != EDGE_DEV) {
919 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
920 "phy 0x%x\n", ex_phy->attached_dev_type,
921 SAS_ADDR(dev->sas_addr),
926 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
928 SAS_DPRINTK("configure routing for dev %016llx "
929 "reported 0x%x. Forgotten\n",
930 SAS_ADDR(ex_phy->attached_sas_addr), res);
931 sas_disable_routing(dev, ex_phy->attached_sas_addr);
935 if (sas_ex_join_wide_port(dev, phy_id)) {
936 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
937 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
941 switch (ex_phy->attached_dev_type) {
943 child = sas_ex_discover_end_dev(dev, phy_id);
946 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
947 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
948 "attached to ex %016llx phy 0x%x\n",
949 SAS_ADDR(ex_phy->attached_sas_addr),
950 ex_phy->attached_phy_id,
951 SAS_ADDR(dev->sas_addr),
953 sas_ex_disable_phy(dev, phy_id);
956 memcpy(dev->port->disc.fanout_sas_addr,
957 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
960 child = sas_ex_discover_expander(dev, phy_id);
969 for (i = 0; i < ex->num_phys; i++) {
970 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
971 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
974 * Due to races, the phy might not get added to the
975 * wide port, so we add the phy to the wide port here.
977 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
978 SAS_ADDR(child->sas_addr)) {
979 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
980 if (sas_ex_join_wide_port(dev, i))
981 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
982 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
991 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
993 struct expander_device *ex = &dev->ex_dev;
996 for (i = 0; i < ex->num_phys; i++) {
997 struct ex_phy *phy = &ex->ex_phy[i];
999 if (phy->phy_state == PHY_VACANT ||
1000 phy->phy_state == PHY_NOT_PRESENT)
1003 if ((phy->attached_dev_type == EDGE_DEV ||
1004 phy->attached_dev_type == FANOUT_DEV) &&
1005 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1007 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1015 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1017 struct expander_device *ex = &dev->ex_dev;
1018 struct domain_device *child;
1019 u8 sub_addr[8] = {0, };
1021 list_for_each_entry(child, &ex->children, siblings) {
1022 if (child->dev_type != EDGE_DEV &&
1023 child->dev_type != FANOUT_DEV)
1025 if (sub_addr[0] == 0) {
1026 sas_find_sub_addr(child, sub_addr);
1031 if (sas_find_sub_addr(child, s2) &&
1032 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1034 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1035 "diverges from subtractive "
1036 "boundary %016llx\n",
1037 SAS_ADDR(dev->sas_addr),
1038 SAS_ADDR(child->sas_addr),
1040 SAS_ADDR(sub_addr));
1042 sas_ex_disable_port(child, s2);
1049 * sas_ex_discover_devices -- discover devices attached to this expander
1050 * dev: pointer to the expander domain device
1051 * single: if you want to do a single phy, else set to -1;
1053 * Configure this expander for use with its devices and register the
1054 * devices of this expander.
1056 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1058 struct expander_device *ex = &dev->ex_dev;
1059 int i = 0, end = ex->num_phys;
1062 if (0 <= single && single < end) {
1067 for ( ; i < end; i++) {
1068 struct ex_phy *ex_phy = &ex->ex_phy[i];
1070 if (ex_phy->phy_state == PHY_VACANT ||
1071 ex_phy->phy_state == PHY_NOT_PRESENT ||
1072 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1075 switch (ex_phy->linkrate) {
1076 case SAS_PHY_DISABLED:
1077 case SAS_PHY_RESET_PROBLEM:
1078 case SAS_SATA_PORT_SELECTOR:
1081 res = sas_ex_discover_dev(dev, i);
1089 sas_check_level_subtractive_boundary(dev);
1094 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1096 struct expander_device *ex = &dev->ex_dev;
1098 u8 *sub_sas_addr = NULL;
1100 if (dev->dev_type != EDGE_DEV)
1103 for (i = 0; i < ex->num_phys; i++) {
1104 struct ex_phy *phy = &ex->ex_phy[i];
1106 if (phy->phy_state == PHY_VACANT ||
1107 phy->phy_state == PHY_NOT_PRESENT)
1110 if ((phy->attached_dev_type == FANOUT_DEV ||
1111 phy->attached_dev_type == EDGE_DEV) &&
1112 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1115 sub_sas_addr = &phy->attached_sas_addr[0];
1116 else if (SAS_ADDR(sub_sas_addr) !=
1117 SAS_ADDR(phy->attached_sas_addr)) {
1119 SAS_DPRINTK("ex %016llx phy 0x%x "
1120 "diverges(%016llx) on subtractive "
1121 "boundary(%016llx). Disabled\n",
1122 SAS_ADDR(dev->sas_addr), i,
1123 SAS_ADDR(phy->attached_sas_addr),
1124 SAS_ADDR(sub_sas_addr));
1125 sas_ex_disable_phy(dev, i);
1132 static void sas_print_parent_topology_bug(struct domain_device *child,
1133 struct ex_phy *parent_phy,
1134 struct ex_phy *child_phy)
1136 static const char ra_char[] = {
1137 [DIRECT_ROUTING] = 'D',
1138 [SUBTRACTIVE_ROUTING] = 'S',
1139 [TABLE_ROUTING] = 'T',
1141 static const char *ex_type[] = {
1142 [EDGE_DEV] = "edge",
1143 [FANOUT_DEV] = "fanout",
1145 struct domain_device *parent = child->parent;
1147 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1148 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
1150 ex_type[parent->dev_type],
1151 SAS_ADDR(parent->sas_addr),
1152 parent->ex_dev.t2t_supp,
1155 ex_type[child->dev_type],
1156 SAS_ADDR(child->sas_addr),
1157 child->ex_dev.t2t_supp,
1160 ra_char[parent_phy->routing_attr],
1161 ra_char[child_phy->routing_attr]);
1164 static int sas_check_eeds(struct domain_device *child,
1165 struct ex_phy *parent_phy,
1166 struct ex_phy *child_phy)
1169 struct domain_device *parent = child->parent;
1171 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1173 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1174 "phy S:0x%x, while there is a fanout ex %016llx\n",
1175 SAS_ADDR(parent->sas_addr),
1177 SAS_ADDR(child->sas_addr),
1179 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1180 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1181 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1183 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1185 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1186 SAS_ADDR(parent->sas_addr)) ||
1187 (SAS_ADDR(parent->port->disc.eeds_a) ==
1188 SAS_ADDR(child->sas_addr)))
1190 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1191 SAS_ADDR(parent->sas_addr)) ||
1192 (SAS_ADDR(parent->port->disc.eeds_b) ==
1193 SAS_ADDR(child->sas_addr))))
1197 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1198 "phy 0x%x link forms a third EEDS!\n",
1199 SAS_ADDR(parent->sas_addr),
1201 SAS_ADDR(child->sas_addr),
1208 /* Here we spill over 80 columns. It is intentional.
1210 static int sas_check_parent_topology(struct domain_device *child)
1212 struct expander_device *child_ex = &child->ex_dev;
1213 struct expander_device *parent_ex;
1220 if (child->parent->dev_type != EDGE_DEV &&
1221 child->parent->dev_type != FANOUT_DEV)
1224 parent_ex = &child->parent->ex_dev;
1226 for (i = 0; i < parent_ex->num_phys; i++) {
1227 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1228 struct ex_phy *child_phy;
1230 if (parent_phy->phy_state == PHY_VACANT ||
1231 parent_phy->phy_state == PHY_NOT_PRESENT)
1234 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1237 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1239 switch (child->parent->dev_type) {
1241 if (child->dev_type == FANOUT_DEV) {
1242 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1243 child_phy->routing_attr != TABLE_ROUTING) {
1244 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1247 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1248 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1249 res = sas_check_eeds(child, parent_phy, child_phy);
1250 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1251 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1254 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1255 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1256 (child_phy->routing_attr == TABLE_ROUTING &&
1257 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1260 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1266 if (parent_phy->routing_attr != TABLE_ROUTING ||
1267 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1268 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1280 #define RRI_REQ_SIZE 16
1281 #define RRI_RESP_SIZE 44
1283 static int sas_configure_present(struct domain_device *dev, int phy_id,
1284 u8 *sas_addr, int *index, int *present)
1287 struct expander_device *ex = &dev->ex_dev;
1288 struct ex_phy *phy = &ex->ex_phy[phy_id];
1295 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1299 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1305 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1306 rri_req[9] = phy_id;
1308 for (i = 0; i < ex->max_route_indexes ; i++) {
1309 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1310 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1315 if (res == SMP_RESP_NO_INDEX) {
1316 SAS_DPRINTK("overflow of indexes: dev %016llx "
1317 "phy 0x%x index 0x%x\n",
1318 SAS_ADDR(dev->sas_addr), phy_id, i);
1320 } else if (res != SMP_RESP_FUNC_ACC) {
1321 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1322 "result 0x%x\n", __func__,
1323 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1326 if (SAS_ADDR(sas_addr) != 0) {
1327 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1329 if ((rri_resp[12] & 0x80) == 0x80)
1334 } else if (SAS_ADDR(rri_resp+16) == 0) {
1339 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1340 phy->last_da_index < i) {
1341 phy->last_da_index = i;
1354 #define CRI_REQ_SIZE 44
1355 #define CRI_RESP_SIZE 8
1357 static int sas_configure_set(struct domain_device *dev, int phy_id,
1358 u8 *sas_addr, int index, int include)
1364 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1368 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1374 cri_req[1] = SMP_CONF_ROUTE_INFO;
1375 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1376 cri_req[9] = phy_id;
1377 if (SAS_ADDR(sas_addr) == 0 || !include)
1378 cri_req[12] |= 0x80;
1379 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1381 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1386 if (res == SMP_RESP_NO_INDEX) {
1387 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1389 SAS_ADDR(dev->sas_addr), phy_id, index);
1397 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1398 u8 *sas_addr, int include)
1404 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1407 if (include ^ present)
1408 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1414 * sas_configure_parent -- configure routing table of parent
1415 * parent: parent expander
1416 * child: child expander
1417 * sas_addr: SAS port identifier of device directly attached to child
1419 static int sas_configure_parent(struct domain_device *parent,
1420 struct domain_device *child,
1421 u8 *sas_addr, int include)
1423 struct expander_device *ex_parent = &parent->ex_dev;
1427 if (parent->parent) {
1428 res = sas_configure_parent(parent->parent, parent, sas_addr,
1434 if (ex_parent->conf_route_table == 0) {
1435 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1436 SAS_ADDR(parent->sas_addr));
1440 for (i = 0; i < ex_parent->num_phys; i++) {
1441 struct ex_phy *phy = &ex_parent->ex_phy[i];
1443 if ((phy->routing_attr == TABLE_ROUTING) &&
1444 (SAS_ADDR(phy->attached_sas_addr) ==
1445 SAS_ADDR(child->sas_addr))) {
1446 res = sas_configure_phy(parent, i, sas_addr, include);
1456 * sas_configure_routing -- configure routing
1457 * dev: expander device
1458 * sas_addr: port identifier of device directly attached to the expander device
1460 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1463 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1467 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1470 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1475 * sas_discover_expander -- expander discovery
1476 * @ex: pointer to expander domain device
1478 * See comment in sas_discover_sata().
1480 static int sas_discover_expander(struct domain_device *dev)
1484 res = sas_notify_lldd_dev_found(dev);
1488 res = sas_ex_general(dev);
1491 res = sas_ex_manuf_info(dev);
1495 res = sas_expander_discover(dev);
1497 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1498 SAS_ADDR(dev->sas_addr), res);
1502 sas_check_ex_subtractive_boundary(dev);
1503 res = sas_check_parent_topology(dev);
1508 sas_notify_lldd_dev_gone(dev);
1512 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1515 struct domain_device *dev;
1517 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1518 if (dev->dev_type == EDGE_DEV ||
1519 dev->dev_type == FANOUT_DEV) {
1520 struct sas_expander_device *ex =
1521 rphy_to_expander_device(dev->rphy);
1523 if (level == ex->level)
1524 res = sas_ex_discover_devices(dev, -1);
1526 res = sas_ex_discover_devices(port->port_dev, -1);
1534 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1540 level = port->disc.max_level;
1541 res = sas_ex_level_discovery(port, level);
1543 } while (level < port->disc.max_level);
1548 int sas_discover_root_expander(struct domain_device *dev)
1551 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1553 res = sas_rphy_add(dev->rphy);
1557 ex->level = dev->port->disc.max_level; /* 0 */
1558 res = sas_discover_expander(dev);
1562 sas_ex_bfs_disc(dev->port);
1567 sas_rphy_remove(dev->rphy);
1572 /* ---------- Domain revalidation ---------- */
1574 static int sas_get_phy_discover(struct domain_device *dev,
1575 int phy_id, struct smp_resp *disc_resp)
1580 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1584 disc_req[1] = SMP_DISCOVER;
1585 disc_req[9] = phy_id;
1587 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1588 disc_resp, DISCOVER_RESP_SIZE);
1591 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1592 res = disc_resp->result;
1600 static int sas_get_phy_change_count(struct domain_device *dev,
1601 int phy_id, int *pcc)
1604 struct smp_resp *disc_resp;
1606 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1610 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1612 *pcc = disc_resp->disc.change_count;
1618 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1619 int phy_id, u8 *attached_sas_addr)
1622 struct smp_resp *disc_resp;
1623 struct discover_resp *dr;
1625 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1628 dr = &disc_resp->disc;
1630 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1632 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1633 if (dr->attached_dev_type == 0)
1634 memset(attached_sas_addr, 0, 8);
1640 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1641 int from_phy, bool update)
1643 struct expander_device *ex = &dev->ex_dev;
1647 for (i = from_phy; i < ex->num_phys; i++) {
1648 int phy_change_count = 0;
1650 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1652 case SMP_RESP_PHY_VACANT:
1653 case SMP_RESP_NO_PHY:
1655 case SMP_RESP_FUNC_ACC:
1661 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1663 ex->ex_phy[i].phy_change_count =
1672 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1676 struct smp_resp *rg_resp;
1678 rg_req = alloc_smp_req(RG_REQ_SIZE);
1682 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1688 rg_req[1] = SMP_REPORT_GENERAL;
1690 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1694 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1695 res = rg_resp->result;
1699 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1706 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1707 * @dev:domain device to be detect.
1708 * @src_dev: the device which originated BROADCAST(CHANGE).
1710 * Add self-configuration expander suport. Suppose two expander cascading,
1711 * when the first level expander is self-configuring, hotplug the disks in
1712 * second level expander, BROADCAST(CHANGE) will not only be originated
1713 * in the second level expander, but also be originated in the first level
1714 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1715 * expander changed count in two level expanders will all increment at least
1716 * once, but the phy which chang count has changed is the source device which
1720 static int sas_find_bcast_dev(struct domain_device *dev,
1721 struct domain_device **src_dev)
1723 struct expander_device *ex = &dev->ex_dev;
1724 int ex_change_count = -1;
1727 struct domain_device *ch;
1729 res = sas_get_ex_change_count(dev, &ex_change_count);
1732 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1733 /* Just detect if this expander phys phy change count changed,
1734 * in order to determine if this expander originate BROADCAST,
1735 * and do not update phy change count field in our structure.
1737 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1740 ex->ex_change_count = ex_change_count;
1741 SAS_DPRINTK("Expander phy change count has changed\n");
1744 SAS_DPRINTK("Expander phys DID NOT change\n");
1746 list_for_each_entry(ch, &ex->children, siblings) {
1747 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1748 res = sas_find_bcast_dev(ch, src_dev);
1757 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1759 struct expander_device *ex = &dev->ex_dev;
1760 struct domain_device *child, *n;
1762 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1764 if (child->dev_type == EDGE_DEV ||
1765 child->dev_type == FANOUT_DEV)
1766 sas_unregister_ex_tree(port, child);
1768 sas_unregister_dev(port, child);
1770 sas_unregister_dev(port, dev);
1773 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1774 int phy_id, bool last)
1776 struct expander_device *ex_dev = &parent->ex_dev;
1777 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1778 struct domain_device *child, *n;
1780 list_for_each_entry_safe(child, n,
1781 &ex_dev->children, siblings) {
1782 if (SAS_ADDR(child->sas_addr) ==
1783 SAS_ADDR(phy->attached_sas_addr)) {
1785 if (child->dev_type == EDGE_DEV ||
1786 child->dev_type == FANOUT_DEV)
1787 sas_unregister_ex_tree(parent->port, child);
1789 sas_unregister_dev(parent->port, child);
1794 sas_disable_routing(parent, phy->attached_sas_addr);
1796 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1798 sas_port_delete_phy(phy->port, phy->phy);
1799 if (phy->port->num_phys == 0)
1800 sas_port_delete(phy->port);
1805 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1808 struct expander_device *ex_root = &root->ex_dev;
1809 struct domain_device *child;
1812 list_for_each_entry(child, &ex_root->children, siblings) {
1813 if (child->dev_type == EDGE_DEV ||
1814 child->dev_type == FANOUT_DEV) {
1815 struct sas_expander_device *ex =
1816 rphy_to_expander_device(child->rphy);
1818 if (level > ex->level)
1819 res = sas_discover_bfs_by_root_level(child,
1821 else if (level == ex->level)
1822 res = sas_ex_discover_devices(child, -1);
1828 static int sas_discover_bfs_by_root(struct domain_device *dev)
1831 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1832 int level = ex->level+1;
1834 res = sas_ex_discover_devices(dev, -1);
1838 res = sas_discover_bfs_by_root_level(dev, level);
1841 } while (level <= dev->port->disc.max_level);
1846 static int sas_discover_new(struct domain_device *dev, int phy_id)
1848 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1849 struct domain_device *child;
1852 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1853 SAS_ADDR(dev->sas_addr), phy_id);
1854 res = sas_ex_phy_discover(dev, phy_id);
1858 if (sas_ex_join_wide_port(dev, phy_id))
1861 res = sas_ex_discover_devices(dev, phy_id);
1864 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1865 if (SAS_ADDR(child->sas_addr) ==
1866 SAS_ADDR(ex_phy->attached_sas_addr)) {
1867 if (child->dev_type == EDGE_DEV ||
1868 child->dev_type == FANOUT_DEV)
1869 res = sas_discover_bfs_by_root(child);
1876 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1878 struct expander_device *ex = &dev->ex_dev;
1879 struct ex_phy *phy = &ex->ex_phy[phy_id];
1880 u8 attached_sas_addr[8];
1883 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1885 case SMP_RESP_NO_PHY:
1886 phy->phy_state = PHY_NOT_PRESENT;
1887 sas_unregister_devs_sas_addr(dev, phy_id, last);
1889 case SMP_RESP_PHY_VACANT:
1890 phy->phy_state = PHY_VACANT;
1891 sas_unregister_devs_sas_addr(dev, phy_id, last);
1893 case SMP_RESP_FUNC_ACC:
1897 if (SAS_ADDR(attached_sas_addr) == 0) {
1898 phy->phy_state = PHY_EMPTY;
1899 sas_unregister_devs_sas_addr(dev, phy_id, last);
1900 } else if (SAS_ADDR(attached_sas_addr) ==
1901 SAS_ADDR(phy->attached_sas_addr)) {
1902 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1903 SAS_ADDR(dev->sas_addr), phy_id);
1904 sas_ex_phy_discover(dev, phy_id);
1906 res = sas_discover_new(dev, phy_id);
1912 * sas_rediscover - revalidate the domain.
1913 * @dev:domain device to be detect.
1914 * @phy_id: the phy id will be detected.
1916 * NOTE: this process _must_ quit (return) as soon as any connection
1917 * errors are encountered. Connection recovery is done elsewhere.
1918 * Discover process only interrogates devices in order to discover the
1919 * domain.For plugging out, we un-register the device only when it is
1920 * the last phy in the port, for other phys in this port, we just delete it
1921 * from the port.For inserting, we do discovery when it is the
1922 * first phy,for other phys in this port, we add it to the port to
1923 * forming the wide-port.
1925 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1927 struct expander_device *ex = &dev->ex_dev;
1928 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1931 bool last = true; /* is this the last phy of the port */
1933 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1934 SAS_ADDR(dev->sas_addr), phy_id);
1936 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1937 for (i = 0; i < ex->num_phys; i++) {
1938 struct ex_phy *phy = &ex->ex_phy[i];
1942 if (SAS_ADDR(phy->attached_sas_addr) ==
1943 SAS_ADDR(changed_phy->attached_sas_addr)) {
1944 SAS_DPRINTK("phy%d part of wide port with "
1945 "phy%d\n", phy_id, i);
1950 res = sas_rediscover_dev(dev, phy_id, last);
1952 res = sas_discover_new(dev, phy_id);
1957 * sas_revalidate_domain -- revalidate the domain
1958 * @port: port to the domain of interest
1960 * NOTE: this process _must_ quit (return) as soon as any connection
1961 * errors are encountered. Connection recovery is done elsewhere.
1962 * Discover process only interrogates devices in order to discover the
1965 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1968 struct domain_device *dev = NULL;
1970 res = sas_find_bcast_dev(port_dev, &dev);
1971 while (res == 0 && dev) {
1972 struct expander_device *ex = &dev->ex_dev;
1977 res = sas_find_bcast_phy(dev, &phy_id, i, true);
1980 res = sas_rediscover(dev, phy_id);
1982 } while (i < ex->num_phys);
1985 res = sas_find_bcast_dev(port_dev, &dev);
1990 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1991 struct request *req)
1993 struct domain_device *dev;
1995 struct request *rsp = req->next_rq;
1998 printk("%s: space for a smp response is missing\n",
2003 /* no rphy means no smp target support (ie aic94xx host) */
2005 return sas_smp_host_handler(shost, req, rsp);
2007 type = rphy->identify.device_type;
2009 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2010 type != SAS_FANOUT_EXPANDER_DEVICE) {
2011 printk("%s: can we send a smp request to a device?\n",
2016 dev = sas_find_dev_by_rphy(rphy);
2018 printk("%s: fail to find a domain_device?\n", __func__);
2022 /* do we need to support multiple segments? */
2023 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2024 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2025 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2026 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2030 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2031 bio_data(rsp->bio), blk_rq_bytes(rsp));
2033 /* positive number is the untransferred residual */
2034 rsp->resid_len = ret;
2037 } else if (ret == 0) {