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/sas_ata.h>
32 #include <scsi/scsi_transport.h>
33 #include <scsi/scsi_transport_sas.h>
34 #include "../scsi_sas_internal.h"
36 static int sas_discover_expander(struct domain_device *dev);
37 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
38 static int sas_configure_phy(struct domain_device *dev, int phy_id,
39 u8 *sas_addr, int include);
40 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
42 /* ---------- SMP task management ---------- */
44 static void smp_task_timedout(unsigned long _task)
46 struct sas_task *task = (void *) _task;
49 spin_lock_irqsave(&task->task_state_lock, flags);
50 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
51 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
52 spin_unlock_irqrestore(&task->task_state_lock, flags);
54 complete(&task->completion);
57 static void smp_task_done(struct sas_task *task)
59 if (!del_timer(&task->timer))
61 complete(&task->completion);
64 /* Give it some long enough timeout. In seconds. */
65 #define SMP_TIMEOUT 10
67 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
68 void *resp, int resp_size)
71 struct sas_task *task = NULL;
72 struct sas_internal *i =
73 to_sas_internal(dev->port->ha->core.shost->transportt);
75 for (retry = 0; retry < 3; retry++) {
76 task = sas_alloc_task(GFP_KERNEL);
81 task->task_proto = dev->tproto;
82 sg_init_one(&task->smp_task.smp_req, req, req_size);
83 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
85 task->task_done = smp_task_done;
87 task->timer.data = (unsigned long) task;
88 task->timer.function = smp_task_timedout;
89 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
90 add_timer(&task->timer);
92 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
95 del_timer(&task->timer);
96 SAS_DPRINTK("executing SMP task failed:%d\n", res);
100 wait_for_completion(&task->completion);
102 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
103 SAS_DPRINTK("smp task timed out or aborted\n");
104 i->dft->lldd_abort_task(task);
105 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
106 SAS_DPRINTK("SMP task aborted and not done\n");
110 if (task->task_status.resp == SAS_TASK_COMPLETE &&
111 task->task_status.stat == SAM_STAT_GOOD) {
114 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
115 task->task_status.stat == SAS_DATA_UNDERRUN) {
116 /* no error, but return the number of bytes of
118 res = task->task_status.residual;
120 } if (task->task_status.resp == SAS_TASK_COMPLETE &&
121 task->task_status.stat == SAS_DATA_OVERRUN) {
125 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
126 "status 0x%x\n", __func__,
127 SAS_ADDR(dev->sas_addr),
128 task->task_status.resp,
129 task->task_status.stat);
135 BUG_ON(retry == 3 && task != NULL);
142 /* ---------- Allocations ---------- */
144 static inline void *alloc_smp_req(int size)
146 u8 *p = kzalloc(size, GFP_KERNEL);
152 static inline void *alloc_smp_resp(int size)
154 return kzalloc(size, GFP_KERNEL);
157 /* ---------- Expander configuration ---------- */
159 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
162 struct expander_device *ex = &dev->ex_dev;
163 struct ex_phy *phy = &ex->ex_phy[phy_id];
164 struct smp_resp *resp = disc_resp;
165 struct discover_resp *dr = &resp->disc;
166 struct sas_rphy *rphy = dev->rphy;
167 int rediscover = (phy->phy != NULL);
170 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
172 /* FIXME: error_handling */
176 switch (resp->result) {
177 case SMP_RESP_PHY_VACANT:
178 phy->phy_state = PHY_VACANT;
181 phy->phy_state = PHY_NOT_PRESENT;
183 case SMP_RESP_FUNC_ACC:
184 phy->phy_state = PHY_EMPTY; /* do not know yet */
188 phy->phy_id = phy_id;
189 phy->attached_dev_type = dr->attached_dev_type;
190 phy->linkrate = dr->linkrate;
191 phy->attached_sata_host = dr->attached_sata_host;
192 phy->attached_sata_dev = dr->attached_sata_dev;
193 phy->attached_sata_ps = dr->attached_sata_ps;
194 phy->attached_iproto = dr->iproto << 1;
195 phy->attached_tproto = dr->tproto << 1;
196 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
197 phy->attached_phy_id = dr->attached_phy_id;
198 phy->phy_change_count = dr->change_count;
199 phy->routing_attr = dr->routing_attr;
200 phy->virtual = dr->virtual;
201 phy->last_da_index = -1;
203 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
204 phy->phy->identify.device_type = phy->attached_dev_type;
205 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
206 phy->phy->identify.target_port_protocols = phy->attached_tproto;
207 phy->phy->identify.phy_identifier = phy_id;
208 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
209 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
210 phy->phy->minimum_linkrate = dr->pmin_linkrate;
211 phy->phy->maximum_linkrate = dr->pmax_linkrate;
212 phy->phy->negotiated_linkrate = phy->linkrate;
215 if (sas_phy_add(phy->phy)) {
216 sas_phy_free(phy->phy);
220 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
221 SAS_ADDR(dev->sas_addr), phy->phy_id,
222 phy->routing_attr == TABLE_ROUTING ? 'T' :
223 phy->routing_attr == DIRECT_ROUTING ? 'D' :
224 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
225 SAS_ADDR(phy->attached_sas_addr));
230 /* check if we have an existing attached ata device on this expander phy */
231 static struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
233 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
234 struct domain_device *dev;
235 struct sas_rphy *rphy;
240 rphy = ex_phy->port->rphy;
244 dev = sas_find_dev_by_rphy(rphy);
246 if (dev && dev_is_sata(dev))
252 #define DISCOVER_REQ_SIZE 16
253 #define DISCOVER_RESP_SIZE 56
255 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
256 u8 *disc_resp, int single)
258 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
261 disc_req[9] = single;
262 for (i = 1 ; i < 3; i++) {
263 struct discover_resp *dr;
265 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
266 disc_resp, DISCOVER_RESP_SIZE);
269 dr = &((struct smp_resp *)disc_resp)->disc;
270 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
271 SAS_ADDR_SIZE) == 0) {
272 sas_printk("Found loopback topology, just ignore it!\n");
276 /* This is detecting a failure to transmit initial
277 * dev to host FIS as described in section J.5 of
280 if (!(dr->attached_dev_type == 0 &&
281 dr->attached_sata_dev))
284 /* In order to generate the dev to host FIS, we send a
285 * link reset to the expander port. If a device was
286 * previously detected on this port we ask libata to
287 * manage the reset and link recovery.
290 sas_ata_schedule_reset(ata_dev);
293 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
294 /* Wait for the reset to trigger the negotiation */
297 sas_set_ex_phy(dev, single, disc_resp);
301 static int sas_ex_phy_discover(struct domain_device *dev, int single)
303 struct expander_device *ex = &dev->ex_dev;
308 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
312 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
318 disc_req[1] = SMP_DISCOVER;
320 if (0 <= single && single < ex->num_phys) {
321 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
325 for (i = 0; i < ex->num_phys; i++) {
326 res = sas_ex_phy_discover_helper(dev, disc_req,
338 static int sas_expander_discover(struct domain_device *dev)
340 struct expander_device *ex = &dev->ex_dev;
343 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
347 res = sas_ex_phy_discover(dev, -1);
358 #define MAX_EXPANDER_PHYS 128
360 static void ex_assign_report_general(struct domain_device *dev,
361 struct smp_resp *resp)
363 struct report_general_resp *rg = &resp->rg;
365 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
366 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
367 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
368 dev->ex_dev.t2t_supp = rg->t2t_supp;
369 dev->ex_dev.conf_route_table = rg->conf_route_table;
370 dev->ex_dev.configuring = rg->configuring;
371 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
374 #define RG_REQ_SIZE 8
375 #define RG_RESP_SIZE 32
377 static int sas_ex_general(struct domain_device *dev)
380 struct smp_resp *rg_resp;
384 rg_req = alloc_smp_req(RG_REQ_SIZE);
388 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
394 rg_req[1] = SMP_REPORT_GENERAL;
396 for (i = 0; i < 5; i++) {
397 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
401 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
402 SAS_ADDR(dev->sas_addr), res);
404 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
405 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
406 SAS_ADDR(dev->sas_addr), rg_resp->result);
407 res = rg_resp->result;
411 ex_assign_report_general(dev, rg_resp);
413 if (dev->ex_dev.configuring) {
414 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
415 SAS_ADDR(dev->sas_addr));
416 schedule_timeout_interruptible(5*HZ);
426 static void ex_assign_manuf_info(struct domain_device *dev, void
429 u8 *mi_resp = _mi_resp;
430 struct sas_rphy *rphy = dev->rphy;
431 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
433 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
434 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
435 memcpy(edev->product_rev, mi_resp + 36,
436 SAS_EXPANDER_PRODUCT_REV_LEN);
438 if (mi_resp[8] & 1) {
439 memcpy(edev->component_vendor_id, mi_resp + 40,
440 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
441 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
442 edev->component_revision_id = mi_resp[50];
446 #define MI_REQ_SIZE 8
447 #define MI_RESP_SIZE 64
449 static int sas_ex_manuf_info(struct domain_device *dev)
455 mi_req = alloc_smp_req(MI_REQ_SIZE);
459 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
465 mi_req[1] = SMP_REPORT_MANUF_INFO;
467 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
469 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
470 SAS_ADDR(dev->sas_addr), res);
472 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
473 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
474 SAS_ADDR(dev->sas_addr), mi_resp[2]);
478 ex_assign_manuf_info(dev, mi_resp);
485 #define PC_REQ_SIZE 44
486 #define PC_RESP_SIZE 8
488 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
489 enum phy_func phy_func,
490 struct sas_phy_linkrates *rates)
496 pc_req = alloc_smp_req(PC_REQ_SIZE);
500 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
506 pc_req[1] = SMP_PHY_CONTROL;
508 pc_req[10]= phy_func;
510 pc_req[32] = rates->minimum_linkrate << 4;
511 pc_req[33] = rates->maximum_linkrate << 4;
514 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
521 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
523 struct expander_device *ex = &dev->ex_dev;
524 struct ex_phy *phy = &ex->ex_phy[phy_id];
526 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
527 phy->linkrate = SAS_PHY_DISABLED;
530 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
532 struct expander_device *ex = &dev->ex_dev;
535 for (i = 0; i < ex->num_phys; i++) {
536 struct ex_phy *phy = &ex->ex_phy[i];
538 if (phy->phy_state == PHY_VACANT ||
539 phy->phy_state == PHY_NOT_PRESENT)
542 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
543 sas_ex_disable_phy(dev, i);
547 static int sas_dev_present_in_domain(struct asd_sas_port *port,
550 struct domain_device *dev;
552 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
554 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
555 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
561 #define RPEL_REQ_SIZE 16
562 #define RPEL_RESP_SIZE 32
563 int sas_smp_get_phy_events(struct sas_phy *phy)
568 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
569 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
571 req = alloc_smp_req(RPEL_REQ_SIZE);
575 resp = alloc_smp_resp(RPEL_RESP_SIZE);
581 req[1] = SMP_REPORT_PHY_ERR_LOG;
582 req[9] = phy->number;
584 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
585 resp, RPEL_RESP_SIZE);
590 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
591 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
592 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
593 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
601 #ifdef CONFIG_SCSI_SAS_ATA
603 #define RPS_REQ_SIZE 16
604 #define RPS_RESP_SIZE 60
606 static int sas_get_report_phy_sata(struct domain_device *dev,
608 struct smp_resp *rps_resp)
611 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
612 u8 *resp = (u8 *)rps_resp;
617 rps_req[1] = SMP_REPORT_PHY_SATA;
620 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
621 rps_resp, RPS_RESP_SIZE);
623 /* 0x34 is the FIS type for the D2H fis. There's a potential
624 * standards cockup here. sas-2 explicitly specifies the FIS
625 * should be encoded so that FIS type is in resp[24].
626 * However, some expanders endian reverse this. Undo the
628 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
631 for (i = 0; i < 5; i++) {
636 resp[j + 0] = resp[j + 3];
637 resp[j + 1] = resp[j + 2];
648 static void sas_ex_get_linkrate(struct domain_device *parent,
649 struct domain_device *child,
650 struct ex_phy *parent_phy)
652 struct expander_device *parent_ex = &parent->ex_dev;
653 struct sas_port *port;
658 port = parent_phy->port;
660 for (i = 0; i < parent_ex->num_phys; i++) {
661 struct ex_phy *phy = &parent_ex->ex_phy[i];
663 if (phy->phy_state == PHY_VACANT ||
664 phy->phy_state == PHY_NOT_PRESENT)
667 if (SAS_ADDR(phy->attached_sas_addr) ==
668 SAS_ADDR(child->sas_addr)) {
670 child->min_linkrate = min(parent->min_linkrate,
672 child->max_linkrate = max(parent->max_linkrate,
675 sas_port_add_phy(port, phy->phy);
678 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
679 child->pathways = min(child->pathways, parent->pathways);
682 static struct domain_device *sas_ex_discover_end_dev(
683 struct domain_device *parent, int phy_id)
685 struct expander_device *parent_ex = &parent->ex_dev;
686 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
687 struct domain_device *child = NULL;
688 struct sas_rphy *rphy;
691 if (phy->attached_sata_host || phy->attached_sata_ps)
694 child = sas_alloc_device();
698 kref_get(&parent->kref);
699 child->parent = parent;
700 child->port = parent->port;
701 child->iproto = phy->attached_iproto;
702 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
703 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
705 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
706 if (unlikely(!phy->port))
708 if (unlikely(sas_port_add(phy->port) != 0)) {
709 sas_port_free(phy->port);
713 sas_ex_get_linkrate(parent, child, phy);
715 #ifdef CONFIG_SCSI_SAS_ATA
716 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
717 child->dev_type = SATA_DEV;
718 if (phy->attached_tproto & SAS_PROTOCOL_STP)
719 child->tproto = phy->attached_tproto;
720 if (phy->attached_sata_dev)
721 child->tproto |= SATA_DEV;
722 res = sas_get_report_phy_sata(parent, phy_id,
723 &child->sata_dev.rps_resp);
725 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
726 "0x%x\n", SAS_ADDR(parent->sas_addr),
730 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
731 sizeof(struct dev_to_host_fis));
733 rphy = sas_end_device_alloc(phy->port);
741 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
743 res = sas_discover_sata(child);
745 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
746 "%016llx:0x%x returned 0x%x\n",
747 SAS_ADDR(child->sas_addr),
748 SAS_ADDR(parent->sas_addr), phy_id, res);
753 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
754 child->dev_type = SAS_END_DEV;
755 rphy = sas_end_device_alloc(phy->port);
756 /* FIXME: error handling */
759 child->tproto = phy->attached_tproto;
763 sas_fill_in_rphy(child, rphy);
765 spin_lock_irq(&parent->port->dev_list_lock);
766 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
767 spin_unlock_irq(&parent->port->dev_list_lock);
769 res = sas_discover_end_dev(child);
771 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
772 "at %016llx:0x%x returned 0x%x\n",
773 SAS_ADDR(child->sas_addr),
774 SAS_ADDR(parent->sas_addr), phy_id, res);
778 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
779 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
784 list_add_tail(&child->siblings, &parent_ex->children);
788 sas_rphy_free(child->rphy);
791 list_del(&child->disco_list_node);
792 spin_lock_irq(&parent->port->dev_list_lock);
793 list_del(&child->dev_list_node);
794 spin_unlock_irq(&parent->port->dev_list_lock);
796 sas_port_delete(phy->port);
799 sas_put_device(child);
803 /* See if this phy is part of a wide port */
804 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
806 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
809 for (i = 0; i < parent->ex_dev.num_phys; i++) {
810 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
815 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
816 SAS_ADDR_SIZE) && ephy->port) {
817 sas_port_add_phy(ephy->port, phy->phy);
818 phy->port = ephy->port;
819 phy->phy_state = PHY_DEVICE_DISCOVERED;
827 static struct domain_device *sas_ex_discover_expander(
828 struct domain_device *parent, int phy_id)
830 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
831 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
832 struct domain_device *child = NULL;
833 struct sas_rphy *rphy;
834 struct sas_expander_device *edev;
835 struct asd_sas_port *port;
838 if (phy->routing_attr == DIRECT_ROUTING) {
839 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
841 SAS_ADDR(parent->sas_addr), phy_id,
842 SAS_ADDR(phy->attached_sas_addr),
843 phy->attached_phy_id);
846 child = sas_alloc_device();
850 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
851 /* FIXME: better error handling */
852 BUG_ON(sas_port_add(phy->port) != 0);
855 switch (phy->attached_dev_type) {
857 rphy = sas_expander_alloc(phy->port,
858 SAS_EDGE_EXPANDER_DEVICE);
861 rphy = sas_expander_alloc(phy->port,
862 SAS_FANOUT_EXPANDER_DEVICE);
865 rphy = NULL; /* shut gcc up */
870 edev = rphy_to_expander_device(rphy);
871 child->dev_type = phy->attached_dev_type;
872 kref_get(&parent->kref);
873 child->parent = parent;
875 child->iproto = phy->attached_iproto;
876 child->tproto = phy->attached_tproto;
877 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
878 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
879 sas_ex_get_linkrate(parent, child, phy);
880 edev->level = parent_ex->level + 1;
881 parent->port->disc.max_level = max(parent->port->disc.max_level,
884 sas_fill_in_rphy(child, rphy);
887 spin_lock_irq(&parent->port->dev_list_lock);
888 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
889 spin_unlock_irq(&parent->port->dev_list_lock);
891 res = sas_discover_expander(child);
893 spin_lock_irq(&parent->port->dev_list_lock);
894 list_del(&child->dev_list_node);
895 spin_unlock_irq(&parent->port->dev_list_lock);
896 sas_put_device(child);
899 list_add_tail(&child->siblings, &parent->ex_dev.children);
903 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
905 struct expander_device *ex = &dev->ex_dev;
906 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
907 struct domain_device *child = NULL;
911 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
912 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
913 res = sas_ex_phy_discover(dev, phy_id);
918 /* Parent and domain coherency */
919 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
920 SAS_ADDR(dev->port->sas_addr))) {
921 sas_add_parent_port(dev, phy_id);
924 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
925 SAS_ADDR(dev->parent->sas_addr))) {
926 sas_add_parent_port(dev, phy_id);
927 if (ex_phy->routing_attr == TABLE_ROUTING)
928 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
932 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
933 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
935 if (ex_phy->attached_dev_type == NO_DEVICE) {
936 if (ex_phy->routing_attr == DIRECT_ROUTING) {
937 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
938 sas_configure_routing(dev, ex_phy->attached_sas_addr);
941 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
944 if (ex_phy->attached_dev_type != SAS_END_DEV &&
945 ex_phy->attached_dev_type != FANOUT_DEV &&
946 ex_phy->attached_dev_type != EDGE_DEV) {
947 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
948 "phy 0x%x\n", ex_phy->attached_dev_type,
949 SAS_ADDR(dev->sas_addr),
954 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
956 SAS_DPRINTK("configure routing for dev %016llx "
957 "reported 0x%x. Forgotten\n",
958 SAS_ADDR(ex_phy->attached_sas_addr), res);
959 sas_disable_routing(dev, ex_phy->attached_sas_addr);
963 res = sas_ex_join_wide_port(dev, phy_id);
965 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
966 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
970 switch (ex_phy->attached_dev_type) {
972 child = sas_ex_discover_end_dev(dev, phy_id);
975 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
976 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
977 "attached to ex %016llx phy 0x%x\n",
978 SAS_ADDR(ex_phy->attached_sas_addr),
979 ex_phy->attached_phy_id,
980 SAS_ADDR(dev->sas_addr),
982 sas_ex_disable_phy(dev, phy_id);
985 memcpy(dev->port->disc.fanout_sas_addr,
986 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
989 child = sas_ex_discover_expander(dev, phy_id);
998 for (i = 0; i < ex->num_phys; i++) {
999 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1000 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1003 * Due to races, the phy might not get added to the
1004 * wide port, so we add the phy to the wide port here.
1006 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1007 SAS_ADDR(child->sas_addr)) {
1008 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1009 res = sas_ex_join_wide_port(dev, i);
1011 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1012 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1021 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1023 struct expander_device *ex = &dev->ex_dev;
1026 for (i = 0; i < ex->num_phys; i++) {
1027 struct ex_phy *phy = &ex->ex_phy[i];
1029 if (phy->phy_state == PHY_VACANT ||
1030 phy->phy_state == PHY_NOT_PRESENT)
1033 if ((phy->attached_dev_type == EDGE_DEV ||
1034 phy->attached_dev_type == FANOUT_DEV) &&
1035 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1037 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1045 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1047 struct expander_device *ex = &dev->ex_dev;
1048 struct domain_device *child;
1049 u8 sub_addr[8] = {0, };
1051 list_for_each_entry(child, &ex->children, siblings) {
1052 if (child->dev_type != EDGE_DEV &&
1053 child->dev_type != FANOUT_DEV)
1055 if (sub_addr[0] == 0) {
1056 sas_find_sub_addr(child, sub_addr);
1061 if (sas_find_sub_addr(child, s2) &&
1062 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1064 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1065 "diverges from subtractive "
1066 "boundary %016llx\n",
1067 SAS_ADDR(dev->sas_addr),
1068 SAS_ADDR(child->sas_addr),
1070 SAS_ADDR(sub_addr));
1072 sas_ex_disable_port(child, s2);
1079 * sas_ex_discover_devices -- discover devices attached to this expander
1080 * dev: pointer to the expander domain device
1081 * single: if you want to do a single phy, else set to -1;
1083 * Configure this expander for use with its devices and register the
1084 * devices of this expander.
1086 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1088 struct expander_device *ex = &dev->ex_dev;
1089 int i = 0, end = ex->num_phys;
1092 if (0 <= single && single < end) {
1097 for ( ; i < end; i++) {
1098 struct ex_phy *ex_phy = &ex->ex_phy[i];
1100 if (ex_phy->phy_state == PHY_VACANT ||
1101 ex_phy->phy_state == PHY_NOT_PRESENT ||
1102 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1105 switch (ex_phy->linkrate) {
1106 case SAS_PHY_DISABLED:
1107 case SAS_PHY_RESET_PROBLEM:
1108 case SAS_SATA_PORT_SELECTOR:
1111 res = sas_ex_discover_dev(dev, i);
1119 sas_check_level_subtractive_boundary(dev);
1124 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1126 struct expander_device *ex = &dev->ex_dev;
1128 u8 *sub_sas_addr = NULL;
1130 if (dev->dev_type != EDGE_DEV)
1133 for (i = 0; i < ex->num_phys; i++) {
1134 struct ex_phy *phy = &ex->ex_phy[i];
1136 if (phy->phy_state == PHY_VACANT ||
1137 phy->phy_state == PHY_NOT_PRESENT)
1140 if ((phy->attached_dev_type == FANOUT_DEV ||
1141 phy->attached_dev_type == EDGE_DEV) &&
1142 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1145 sub_sas_addr = &phy->attached_sas_addr[0];
1146 else if (SAS_ADDR(sub_sas_addr) !=
1147 SAS_ADDR(phy->attached_sas_addr)) {
1149 SAS_DPRINTK("ex %016llx phy 0x%x "
1150 "diverges(%016llx) on subtractive "
1151 "boundary(%016llx). Disabled\n",
1152 SAS_ADDR(dev->sas_addr), i,
1153 SAS_ADDR(phy->attached_sas_addr),
1154 SAS_ADDR(sub_sas_addr));
1155 sas_ex_disable_phy(dev, i);
1162 static void sas_print_parent_topology_bug(struct domain_device *child,
1163 struct ex_phy *parent_phy,
1164 struct ex_phy *child_phy)
1166 static const char ra_char[] = {
1167 [DIRECT_ROUTING] = 'D',
1168 [SUBTRACTIVE_ROUTING] = 'S',
1169 [TABLE_ROUTING] = 'T',
1171 static const char *ex_type[] = {
1172 [EDGE_DEV] = "edge",
1173 [FANOUT_DEV] = "fanout",
1175 struct domain_device *parent = child->parent;
1177 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1178 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
1180 ex_type[parent->dev_type],
1181 SAS_ADDR(parent->sas_addr),
1182 parent->ex_dev.t2t_supp,
1185 ex_type[child->dev_type],
1186 SAS_ADDR(child->sas_addr),
1187 child->ex_dev.t2t_supp,
1190 ra_char[parent_phy->routing_attr],
1191 ra_char[child_phy->routing_attr]);
1194 static int sas_check_eeds(struct domain_device *child,
1195 struct ex_phy *parent_phy,
1196 struct ex_phy *child_phy)
1199 struct domain_device *parent = child->parent;
1201 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1203 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1204 "phy S:0x%x, while there is a fanout ex %016llx\n",
1205 SAS_ADDR(parent->sas_addr),
1207 SAS_ADDR(child->sas_addr),
1209 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1210 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1211 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1213 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1215 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1216 SAS_ADDR(parent->sas_addr)) ||
1217 (SAS_ADDR(parent->port->disc.eeds_a) ==
1218 SAS_ADDR(child->sas_addr)))
1220 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1221 SAS_ADDR(parent->sas_addr)) ||
1222 (SAS_ADDR(parent->port->disc.eeds_b) ==
1223 SAS_ADDR(child->sas_addr))))
1227 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1228 "phy 0x%x link forms a third EEDS!\n",
1229 SAS_ADDR(parent->sas_addr),
1231 SAS_ADDR(child->sas_addr),
1238 /* Here we spill over 80 columns. It is intentional.
1240 static int sas_check_parent_topology(struct domain_device *child)
1242 struct expander_device *child_ex = &child->ex_dev;
1243 struct expander_device *parent_ex;
1250 if (child->parent->dev_type != EDGE_DEV &&
1251 child->parent->dev_type != FANOUT_DEV)
1254 parent_ex = &child->parent->ex_dev;
1256 for (i = 0; i < parent_ex->num_phys; i++) {
1257 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1258 struct ex_phy *child_phy;
1260 if (parent_phy->phy_state == PHY_VACANT ||
1261 parent_phy->phy_state == PHY_NOT_PRESENT)
1264 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1267 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1269 switch (child->parent->dev_type) {
1271 if (child->dev_type == FANOUT_DEV) {
1272 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1273 child_phy->routing_attr != TABLE_ROUTING) {
1274 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1277 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1278 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1279 res = sas_check_eeds(child, parent_phy, child_phy);
1280 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1281 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1284 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1285 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1286 (child_phy->routing_attr == TABLE_ROUTING &&
1287 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1290 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1296 if (parent_phy->routing_attr != TABLE_ROUTING ||
1297 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1298 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1310 #define RRI_REQ_SIZE 16
1311 #define RRI_RESP_SIZE 44
1313 static int sas_configure_present(struct domain_device *dev, int phy_id,
1314 u8 *sas_addr, int *index, int *present)
1317 struct expander_device *ex = &dev->ex_dev;
1318 struct ex_phy *phy = &ex->ex_phy[phy_id];
1325 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1329 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1335 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1336 rri_req[9] = phy_id;
1338 for (i = 0; i < ex->max_route_indexes ; i++) {
1339 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1340 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1345 if (res == SMP_RESP_NO_INDEX) {
1346 SAS_DPRINTK("overflow of indexes: dev %016llx "
1347 "phy 0x%x index 0x%x\n",
1348 SAS_ADDR(dev->sas_addr), phy_id, i);
1350 } else if (res != SMP_RESP_FUNC_ACC) {
1351 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1352 "result 0x%x\n", __func__,
1353 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1356 if (SAS_ADDR(sas_addr) != 0) {
1357 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1359 if ((rri_resp[12] & 0x80) == 0x80)
1364 } else if (SAS_ADDR(rri_resp+16) == 0) {
1369 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1370 phy->last_da_index < i) {
1371 phy->last_da_index = i;
1384 #define CRI_REQ_SIZE 44
1385 #define CRI_RESP_SIZE 8
1387 static int sas_configure_set(struct domain_device *dev, int phy_id,
1388 u8 *sas_addr, int index, int include)
1394 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1398 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1404 cri_req[1] = SMP_CONF_ROUTE_INFO;
1405 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1406 cri_req[9] = phy_id;
1407 if (SAS_ADDR(sas_addr) == 0 || !include)
1408 cri_req[12] |= 0x80;
1409 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1411 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1416 if (res == SMP_RESP_NO_INDEX) {
1417 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1419 SAS_ADDR(dev->sas_addr), phy_id, index);
1427 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1428 u8 *sas_addr, int include)
1434 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1437 if (include ^ present)
1438 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1444 * sas_configure_parent -- configure routing table of parent
1445 * parent: parent expander
1446 * child: child expander
1447 * sas_addr: SAS port identifier of device directly attached to child
1449 static int sas_configure_parent(struct domain_device *parent,
1450 struct domain_device *child,
1451 u8 *sas_addr, int include)
1453 struct expander_device *ex_parent = &parent->ex_dev;
1457 if (parent->parent) {
1458 res = sas_configure_parent(parent->parent, parent, sas_addr,
1464 if (ex_parent->conf_route_table == 0) {
1465 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1466 SAS_ADDR(parent->sas_addr));
1470 for (i = 0; i < ex_parent->num_phys; i++) {
1471 struct ex_phy *phy = &ex_parent->ex_phy[i];
1473 if ((phy->routing_attr == TABLE_ROUTING) &&
1474 (SAS_ADDR(phy->attached_sas_addr) ==
1475 SAS_ADDR(child->sas_addr))) {
1476 res = sas_configure_phy(parent, i, sas_addr, include);
1486 * sas_configure_routing -- configure routing
1487 * dev: expander device
1488 * sas_addr: port identifier of device directly attached to the expander device
1490 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1493 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1497 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1500 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1505 * sas_discover_expander -- expander discovery
1506 * @ex: pointer to expander domain device
1508 * See comment in sas_discover_sata().
1510 static int sas_discover_expander(struct domain_device *dev)
1514 res = sas_notify_lldd_dev_found(dev);
1518 res = sas_ex_general(dev);
1521 res = sas_ex_manuf_info(dev);
1525 res = sas_expander_discover(dev);
1527 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1528 SAS_ADDR(dev->sas_addr), res);
1532 sas_check_ex_subtractive_boundary(dev);
1533 res = sas_check_parent_topology(dev);
1538 sas_notify_lldd_dev_gone(dev);
1542 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1545 struct domain_device *dev;
1547 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1548 if (dev->dev_type == EDGE_DEV ||
1549 dev->dev_type == FANOUT_DEV) {
1550 struct sas_expander_device *ex =
1551 rphy_to_expander_device(dev->rphy);
1553 if (level == ex->level)
1554 res = sas_ex_discover_devices(dev, -1);
1556 res = sas_ex_discover_devices(port->port_dev, -1);
1564 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1570 level = port->disc.max_level;
1571 res = sas_ex_level_discovery(port, level);
1573 } while (level < port->disc.max_level);
1578 int sas_discover_root_expander(struct domain_device *dev)
1581 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1583 res = sas_rphy_add(dev->rphy);
1587 ex->level = dev->port->disc.max_level; /* 0 */
1588 res = sas_discover_expander(dev);
1592 sas_ex_bfs_disc(dev->port);
1597 sas_rphy_remove(dev->rphy);
1602 /* ---------- Domain revalidation ---------- */
1604 static int sas_get_phy_discover(struct domain_device *dev,
1605 int phy_id, struct smp_resp *disc_resp)
1610 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1614 disc_req[1] = SMP_DISCOVER;
1615 disc_req[9] = phy_id;
1617 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1618 disc_resp, DISCOVER_RESP_SIZE);
1621 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1622 res = disc_resp->result;
1630 static int sas_get_phy_change_count(struct domain_device *dev,
1631 int phy_id, int *pcc)
1634 struct smp_resp *disc_resp;
1636 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1640 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1642 *pcc = disc_resp->disc.change_count;
1648 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1649 int phy_id, u8 *attached_sas_addr)
1652 struct smp_resp *disc_resp;
1653 struct discover_resp *dr;
1655 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1658 dr = &disc_resp->disc;
1660 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1662 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1663 if (dr->attached_dev_type == 0)
1664 memset(attached_sas_addr, 0, 8);
1670 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1671 int from_phy, bool update)
1673 struct expander_device *ex = &dev->ex_dev;
1677 for (i = from_phy; i < ex->num_phys; i++) {
1678 int phy_change_count = 0;
1680 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1683 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1685 ex->ex_phy[i].phy_change_count =
1695 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1699 struct smp_resp *rg_resp;
1701 rg_req = alloc_smp_req(RG_REQ_SIZE);
1705 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1711 rg_req[1] = SMP_REPORT_GENERAL;
1713 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1717 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1718 res = rg_resp->result;
1722 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1729 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1730 * @dev:domain device to be detect.
1731 * @src_dev: the device which originated BROADCAST(CHANGE).
1733 * Add self-configuration expander suport. Suppose two expander cascading,
1734 * when the first level expander is self-configuring, hotplug the disks in
1735 * second level expander, BROADCAST(CHANGE) will not only be originated
1736 * in the second level expander, but also be originated in the first level
1737 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1738 * expander changed count in two level expanders will all increment at least
1739 * once, but the phy which chang count has changed is the source device which
1743 static int sas_find_bcast_dev(struct domain_device *dev,
1744 struct domain_device **src_dev)
1746 struct expander_device *ex = &dev->ex_dev;
1747 int ex_change_count = -1;
1750 struct domain_device *ch;
1752 res = sas_get_ex_change_count(dev, &ex_change_count);
1755 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1756 /* Just detect if this expander phys phy change count changed,
1757 * in order to determine if this expander originate BROADCAST,
1758 * and do not update phy change count field in our structure.
1760 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1763 ex->ex_change_count = ex_change_count;
1764 SAS_DPRINTK("Expander phy change count has changed\n");
1767 SAS_DPRINTK("Expander phys DID NOT change\n");
1769 list_for_each_entry(ch, &ex->children, siblings) {
1770 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1771 res = sas_find_bcast_dev(ch, src_dev);
1780 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1782 struct expander_device *ex = &dev->ex_dev;
1783 struct domain_device *child, *n;
1785 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1786 set_bit(SAS_DEV_GONE, &child->state);
1787 if (child->dev_type == EDGE_DEV ||
1788 child->dev_type == FANOUT_DEV)
1789 sas_unregister_ex_tree(port, child);
1791 sas_unregister_dev(port, child);
1793 sas_unregister_dev(port, dev);
1796 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1797 int phy_id, bool last)
1799 struct expander_device *ex_dev = &parent->ex_dev;
1800 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1801 struct domain_device *child, *n;
1803 list_for_each_entry_safe(child, n,
1804 &ex_dev->children, siblings) {
1805 if (SAS_ADDR(child->sas_addr) ==
1806 SAS_ADDR(phy->attached_sas_addr)) {
1807 set_bit(SAS_DEV_GONE, &child->state);
1808 if (child->dev_type == EDGE_DEV ||
1809 child->dev_type == FANOUT_DEV)
1810 sas_unregister_ex_tree(parent->port, child);
1812 sas_unregister_dev(parent->port, child);
1816 set_bit(SAS_DEV_GONE, &parent->state);
1817 sas_disable_routing(parent, phy->attached_sas_addr);
1819 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1821 sas_port_delete_phy(phy->port, phy->phy);
1822 if (phy->port->num_phys == 0)
1823 sas_port_delete(phy->port);
1828 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1831 struct expander_device *ex_root = &root->ex_dev;
1832 struct domain_device *child;
1835 list_for_each_entry(child, &ex_root->children, siblings) {
1836 if (child->dev_type == EDGE_DEV ||
1837 child->dev_type == FANOUT_DEV) {
1838 struct sas_expander_device *ex =
1839 rphy_to_expander_device(child->rphy);
1841 if (level > ex->level)
1842 res = sas_discover_bfs_by_root_level(child,
1844 else if (level == ex->level)
1845 res = sas_ex_discover_devices(child, -1);
1851 static int sas_discover_bfs_by_root(struct domain_device *dev)
1854 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1855 int level = ex->level+1;
1857 res = sas_ex_discover_devices(dev, -1);
1861 res = sas_discover_bfs_by_root_level(dev, level);
1864 } while (level <= dev->port->disc.max_level);
1869 static int sas_discover_new(struct domain_device *dev, int phy_id)
1871 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1872 struct domain_device *child;
1876 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1877 SAS_ADDR(dev->sas_addr), phy_id);
1878 res = sas_ex_phy_discover(dev, phy_id);
1881 /* to support the wide port inserted */
1882 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1883 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1886 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1887 SAS_ADDR(ex_phy->attached_sas_addr)) {
1893 sas_ex_join_wide_port(dev, phy_id);
1896 res = sas_ex_discover_devices(dev, phy_id);
1899 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1900 if (SAS_ADDR(child->sas_addr) ==
1901 SAS_ADDR(ex_phy->attached_sas_addr)) {
1902 if (child->dev_type == EDGE_DEV ||
1903 child->dev_type == FANOUT_DEV)
1904 res = sas_discover_bfs_by_root(child);
1912 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1914 struct expander_device *ex = &dev->ex_dev;
1915 struct ex_phy *phy = &ex->ex_phy[phy_id];
1916 u8 attached_sas_addr[8];
1919 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1921 case SMP_RESP_NO_PHY:
1922 phy->phy_state = PHY_NOT_PRESENT;
1923 sas_unregister_devs_sas_addr(dev, phy_id, last);
1925 case SMP_RESP_PHY_VACANT:
1926 phy->phy_state = PHY_VACANT;
1927 sas_unregister_devs_sas_addr(dev, phy_id, last);
1929 case SMP_RESP_FUNC_ACC:
1933 if (SAS_ADDR(attached_sas_addr) == 0) {
1934 phy->phy_state = PHY_EMPTY;
1935 sas_unregister_devs_sas_addr(dev, phy_id, last);
1936 } else if (SAS_ADDR(attached_sas_addr) ==
1937 SAS_ADDR(phy->attached_sas_addr)) {
1938 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1939 SAS_ADDR(dev->sas_addr), phy_id);
1940 sas_ex_phy_discover(dev, phy_id);
1942 res = sas_discover_new(dev, phy_id);
1948 * sas_rediscover - revalidate the domain.
1949 * @dev:domain device to be detect.
1950 * @phy_id: the phy id will be detected.
1952 * NOTE: this process _must_ quit (return) as soon as any connection
1953 * errors are encountered. Connection recovery is done elsewhere.
1954 * Discover process only interrogates devices in order to discover the
1955 * domain.For plugging out, we un-register the device only when it is
1956 * the last phy in the port, for other phys in this port, we just delete it
1957 * from the port.For inserting, we do discovery when it is the
1958 * first phy,for other phys in this port, we add it to the port to
1959 * forming the wide-port.
1961 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1963 struct expander_device *ex = &dev->ex_dev;
1964 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1967 bool last = true; /* is this the last phy of the port */
1969 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1970 SAS_ADDR(dev->sas_addr), phy_id);
1972 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1973 for (i = 0; i < ex->num_phys; i++) {
1974 struct ex_phy *phy = &ex->ex_phy[i];
1978 if (SAS_ADDR(phy->attached_sas_addr) ==
1979 SAS_ADDR(changed_phy->attached_sas_addr)) {
1980 SAS_DPRINTK("phy%d part of wide port with "
1981 "phy%d\n", phy_id, i);
1986 res = sas_rediscover_dev(dev, phy_id, last);
1988 res = sas_discover_new(dev, phy_id);
1993 * sas_revalidate_domain -- revalidate the domain
1994 * @port: port to the domain of interest
1996 * NOTE: this process _must_ quit (return) as soon as any connection
1997 * errors are encountered. Connection recovery is done elsewhere.
1998 * Discover process only interrogates devices in order to discover the
2001 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2004 struct domain_device *dev = NULL;
2006 res = sas_find_bcast_dev(port_dev, &dev);
2010 struct expander_device *ex = &dev->ex_dev;
2015 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2018 res = sas_rediscover(dev, phy_id);
2020 } while (i < ex->num_phys);
2026 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2027 struct request *req)
2029 struct domain_device *dev;
2031 struct request *rsp = req->next_rq;
2034 printk("%s: space for a smp response is missing\n",
2039 /* no rphy means no smp target support (ie aic94xx host) */
2041 return sas_smp_host_handler(shost, req, rsp);
2043 type = rphy->identify.device_type;
2045 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2046 type != SAS_FANOUT_EXPANDER_DEVICE) {
2047 printk("%s: can we send a smp request to a device?\n",
2052 dev = sas_find_dev_by_rphy(rphy);
2054 printk("%s: fail to find a domain_device?\n", __func__);
2058 /* do we need to support multiple segments? */
2059 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2060 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2061 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2062 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2066 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2067 bio_data(rsp->bio), blk_rq_bytes(rsp));
2069 /* positive number is the untransferred residual */
2070 rsp->resid_len = ret;
2073 } else if (ret == 0) {