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 mutex_lock(&dev->ex_dev.cmd_mutex);
76 for (retry = 0; retry < 3; retry++) {
77 task = sas_alloc_task(GFP_KERNEL);
83 task->task_proto = dev->tproto;
84 sg_init_one(&task->smp_task.smp_req, req, req_size);
85 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
87 task->task_done = smp_task_done;
89 task->timer.data = (unsigned long) task;
90 task->timer.function = smp_task_timedout;
91 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
92 add_timer(&task->timer);
94 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
97 del_timer(&task->timer);
98 SAS_DPRINTK("executing SMP task failed:%d\n", res);
102 wait_for_completion(&task->completion);
104 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
105 SAS_DPRINTK("smp task timed out or aborted\n");
106 i->dft->lldd_abort_task(task);
107 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
108 SAS_DPRINTK("SMP task aborted and not done\n");
112 if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAM_STAT_GOOD) {
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
118 task->task_status.stat == SAS_DATA_UNDERRUN) {
119 /* no error, but return the number of bytes of
121 res = task->task_status.residual;
124 if (task->task_status.resp == SAS_TASK_COMPLETE &&
125 task->task_status.stat == SAS_DATA_OVERRUN) {
129 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
130 task->task_status.stat == SAS_DEVICE_UNKNOWN)
133 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
134 "status 0x%x\n", __func__,
135 SAS_ADDR(dev->sas_addr),
136 task->task_status.resp,
137 task->task_status.stat);
142 mutex_unlock(&dev->ex_dev.cmd_mutex);
144 BUG_ON(retry == 3 && task != NULL);
149 /* ---------- Allocations ---------- */
151 static inline void *alloc_smp_req(int size)
153 u8 *p = kzalloc(size, GFP_KERNEL);
159 static inline void *alloc_smp_resp(int size)
161 return kzalloc(size, GFP_KERNEL);
164 /* ---------- Expander configuration ---------- */
166 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
169 struct expander_device *ex = &dev->ex_dev;
170 struct ex_phy *phy = &ex->ex_phy[phy_id];
171 struct smp_resp *resp = disc_resp;
172 struct discover_resp *dr = &resp->disc;
173 struct sas_rphy *rphy = dev->rphy;
174 int rediscover = (phy->phy != NULL);
177 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
179 /* FIXME: error_handling */
183 switch (resp->result) {
184 case SMP_RESP_PHY_VACANT:
185 phy->phy_state = PHY_VACANT;
188 phy->phy_state = PHY_NOT_PRESENT;
190 case SMP_RESP_FUNC_ACC:
191 phy->phy_state = PHY_EMPTY; /* do not know yet */
195 phy->phy_id = phy_id;
196 phy->attached_dev_type = dr->attached_dev_type;
197 phy->linkrate = dr->linkrate;
198 phy->attached_sata_host = dr->attached_sata_host;
199 phy->attached_sata_dev = dr->attached_sata_dev;
200 phy->attached_sata_ps = dr->attached_sata_ps;
201 phy->attached_iproto = dr->iproto << 1;
202 phy->attached_tproto = dr->tproto << 1;
203 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
204 phy->attached_phy_id = dr->attached_phy_id;
205 phy->phy_change_count = dr->change_count;
206 phy->routing_attr = dr->routing_attr;
207 phy->virtual = dr->virtual;
208 phy->last_da_index = -1;
210 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
211 phy->phy->identify.device_type = phy->attached_dev_type;
212 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
213 phy->phy->identify.target_port_protocols = phy->attached_tproto;
214 phy->phy->identify.phy_identifier = phy_id;
215 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
216 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
217 phy->phy->minimum_linkrate = dr->pmin_linkrate;
218 phy->phy->maximum_linkrate = dr->pmax_linkrate;
219 phy->phy->negotiated_linkrate = phy->linkrate;
222 if (sas_phy_add(phy->phy)) {
223 sas_phy_free(phy->phy);
227 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
228 SAS_ADDR(dev->sas_addr), phy->phy_id,
229 phy->routing_attr == TABLE_ROUTING ? 'T' :
230 phy->routing_attr == DIRECT_ROUTING ? 'D' :
231 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
232 SAS_ADDR(phy->attached_sas_addr));
237 /* check if we have an existing attached ata device on this expander phy */
238 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
240 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
241 struct domain_device *dev;
242 struct sas_rphy *rphy;
247 rphy = ex_phy->port->rphy;
251 dev = sas_find_dev_by_rphy(rphy);
253 if (dev && dev_is_sata(dev))
259 #define DISCOVER_REQ_SIZE 16
260 #define DISCOVER_RESP_SIZE 56
262 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
263 u8 *disc_resp, int single)
265 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
268 disc_req[9] = single;
269 for (i = 1 ; i < 3; i++) {
270 struct discover_resp *dr;
272 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
273 disc_resp, DISCOVER_RESP_SIZE);
276 dr = &((struct smp_resp *)disc_resp)->disc;
277 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
278 SAS_ADDR_SIZE) == 0) {
279 sas_printk("Found loopback topology, just ignore it!\n");
283 /* This is detecting a failure to transmit initial
284 * dev to host FIS as described in section J.5 of
287 if (!(dr->attached_dev_type == 0 &&
288 dr->attached_sata_dev))
291 /* In order to generate the dev to host FIS, we send a
292 * link reset to the expander port. If a device was
293 * previously detected on this port we ask libata to
294 * manage the reset and link recovery.
297 sas_ata_schedule_reset(ata_dev);
300 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
301 /* Wait for the reset to trigger the negotiation */
304 sas_set_ex_phy(dev, single, disc_resp);
308 static int sas_ex_phy_discover(struct domain_device *dev, int single)
310 struct expander_device *ex = &dev->ex_dev;
315 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
319 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
325 disc_req[1] = SMP_DISCOVER;
327 if (0 <= single && single < ex->num_phys) {
328 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
332 for (i = 0; i < ex->num_phys; i++) {
333 res = sas_ex_phy_discover_helper(dev, disc_req,
345 static int sas_expander_discover(struct domain_device *dev)
347 struct expander_device *ex = &dev->ex_dev;
350 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
354 res = sas_ex_phy_discover(dev, -1);
365 #define MAX_EXPANDER_PHYS 128
367 static void ex_assign_report_general(struct domain_device *dev,
368 struct smp_resp *resp)
370 struct report_general_resp *rg = &resp->rg;
372 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
373 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
374 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
375 dev->ex_dev.t2t_supp = rg->t2t_supp;
376 dev->ex_dev.conf_route_table = rg->conf_route_table;
377 dev->ex_dev.configuring = rg->configuring;
378 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
381 #define RG_REQ_SIZE 8
382 #define RG_RESP_SIZE 32
384 static int sas_ex_general(struct domain_device *dev)
387 struct smp_resp *rg_resp;
391 rg_req = alloc_smp_req(RG_REQ_SIZE);
395 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
401 rg_req[1] = SMP_REPORT_GENERAL;
403 for (i = 0; i < 5; i++) {
404 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
408 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
409 SAS_ADDR(dev->sas_addr), res);
411 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
412 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
413 SAS_ADDR(dev->sas_addr), rg_resp->result);
414 res = rg_resp->result;
418 ex_assign_report_general(dev, rg_resp);
420 if (dev->ex_dev.configuring) {
421 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
422 SAS_ADDR(dev->sas_addr));
423 schedule_timeout_interruptible(5*HZ);
433 static void ex_assign_manuf_info(struct domain_device *dev, void
436 u8 *mi_resp = _mi_resp;
437 struct sas_rphy *rphy = dev->rphy;
438 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
440 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
441 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
442 memcpy(edev->product_rev, mi_resp + 36,
443 SAS_EXPANDER_PRODUCT_REV_LEN);
445 if (mi_resp[8] & 1) {
446 memcpy(edev->component_vendor_id, mi_resp + 40,
447 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
448 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
449 edev->component_revision_id = mi_resp[50];
453 #define MI_REQ_SIZE 8
454 #define MI_RESP_SIZE 64
456 static int sas_ex_manuf_info(struct domain_device *dev)
462 mi_req = alloc_smp_req(MI_REQ_SIZE);
466 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
472 mi_req[1] = SMP_REPORT_MANUF_INFO;
474 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
476 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
477 SAS_ADDR(dev->sas_addr), res);
479 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
480 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
481 SAS_ADDR(dev->sas_addr), mi_resp[2]);
485 ex_assign_manuf_info(dev, mi_resp);
492 #define PC_REQ_SIZE 44
493 #define PC_RESP_SIZE 8
495 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
496 enum phy_func phy_func,
497 struct sas_phy_linkrates *rates)
503 pc_req = alloc_smp_req(PC_REQ_SIZE);
507 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
513 pc_req[1] = SMP_PHY_CONTROL;
515 pc_req[10]= phy_func;
517 pc_req[32] = rates->minimum_linkrate << 4;
518 pc_req[33] = rates->maximum_linkrate << 4;
521 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
528 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
530 struct expander_device *ex = &dev->ex_dev;
531 struct ex_phy *phy = &ex->ex_phy[phy_id];
533 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
534 phy->linkrate = SAS_PHY_DISABLED;
537 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
539 struct expander_device *ex = &dev->ex_dev;
542 for (i = 0; i < ex->num_phys; i++) {
543 struct ex_phy *phy = &ex->ex_phy[i];
545 if (phy->phy_state == PHY_VACANT ||
546 phy->phy_state == PHY_NOT_PRESENT)
549 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
550 sas_ex_disable_phy(dev, i);
554 static int sas_dev_present_in_domain(struct asd_sas_port *port,
557 struct domain_device *dev;
559 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
561 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
562 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
568 #define RPEL_REQ_SIZE 16
569 #define RPEL_RESP_SIZE 32
570 int sas_smp_get_phy_events(struct sas_phy *phy)
575 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
576 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
578 req = alloc_smp_req(RPEL_REQ_SIZE);
582 resp = alloc_smp_resp(RPEL_RESP_SIZE);
588 req[1] = SMP_REPORT_PHY_ERR_LOG;
589 req[9] = phy->number;
591 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
592 resp, RPEL_RESP_SIZE);
597 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
598 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
599 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
600 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
608 #ifdef CONFIG_SCSI_SAS_ATA
610 #define RPS_REQ_SIZE 16
611 #define RPS_RESP_SIZE 60
613 static int sas_get_report_phy_sata(struct domain_device *dev,
615 struct smp_resp *rps_resp)
618 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
619 u8 *resp = (u8 *)rps_resp;
624 rps_req[1] = SMP_REPORT_PHY_SATA;
627 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
628 rps_resp, RPS_RESP_SIZE);
630 /* 0x34 is the FIS type for the D2H fis. There's a potential
631 * standards cockup here. sas-2 explicitly specifies the FIS
632 * should be encoded so that FIS type is in resp[24].
633 * However, some expanders endian reverse this. Undo the
635 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
638 for (i = 0; i < 5; i++) {
643 resp[j + 0] = resp[j + 3];
644 resp[j + 1] = resp[j + 2];
655 static void sas_ex_get_linkrate(struct domain_device *parent,
656 struct domain_device *child,
657 struct ex_phy *parent_phy)
659 struct expander_device *parent_ex = &parent->ex_dev;
660 struct sas_port *port;
665 port = parent_phy->port;
667 for (i = 0; i < parent_ex->num_phys; i++) {
668 struct ex_phy *phy = &parent_ex->ex_phy[i];
670 if (phy->phy_state == PHY_VACANT ||
671 phy->phy_state == PHY_NOT_PRESENT)
674 if (SAS_ADDR(phy->attached_sas_addr) ==
675 SAS_ADDR(child->sas_addr)) {
677 child->min_linkrate = min(parent->min_linkrate,
679 child->max_linkrate = max(parent->max_linkrate,
682 sas_port_add_phy(port, phy->phy);
685 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
686 child->pathways = min(child->pathways, parent->pathways);
689 static struct domain_device *sas_ex_discover_end_dev(
690 struct domain_device *parent, int phy_id)
692 struct expander_device *parent_ex = &parent->ex_dev;
693 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
694 struct domain_device *child = NULL;
695 struct sas_rphy *rphy;
698 if (phy->attached_sata_host || phy->attached_sata_ps)
701 child = sas_alloc_device();
705 kref_get(&parent->kref);
706 child->parent = parent;
707 child->port = parent->port;
708 child->iproto = phy->attached_iproto;
709 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
710 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
712 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
713 if (unlikely(!phy->port))
715 if (unlikely(sas_port_add(phy->port) != 0)) {
716 sas_port_free(phy->port);
720 sas_ex_get_linkrate(parent, child, phy);
722 #ifdef CONFIG_SCSI_SAS_ATA
723 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
724 child->dev_type = SATA_DEV;
725 if (phy->attached_tproto & SAS_PROTOCOL_STP)
726 child->tproto = phy->attached_tproto;
727 if (phy->attached_sata_dev)
728 child->tproto |= SATA_DEV;
729 res = sas_get_report_phy_sata(parent, phy_id,
730 &child->sata_dev.rps_resp);
732 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
733 "0x%x\n", SAS_ADDR(parent->sas_addr),
737 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
738 sizeof(struct dev_to_host_fis));
740 rphy = sas_end_device_alloc(phy->port);
748 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
750 res = sas_discover_sata(child);
752 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
753 "%016llx:0x%x returned 0x%x\n",
754 SAS_ADDR(child->sas_addr),
755 SAS_ADDR(parent->sas_addr), phy_id, res);
760 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
761 child->dev_type = SAS_END_DEV;
762 rphy = sas_end_device_alloc(phy->port);
763 /* FIXME: error handling */
766 child->tproto = phy->attached_tproto;
770 sas_fill_in_rphy(child, rphy);
772 spin_lock_irq(&parent->port->dev_list_lock);
773 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
774 spin_unlock_irq(&parent->port->dev_list_lock);
776 res = sas_discover_end_dev(child);
778 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
779 "at %016llx:0x%x returned 0x%x\n",
780 SAS_ADDR(child->sas_addr),
781 SAS_ADDR(parent->sas_addr), phy_id, res);
785 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
786 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
791 list_add_tail(&child->siblings, &parent_ex->children);
795 sas_rphy_free(child->rphy);
798 list_del(&child->disco_list_node);
799 spin_lock_irq(&parent->port->dev_list_lock);
800 list_del(&child->dev_list_node);
801 spin_unlock_irq(&parent->port->dev_list_lock);
803 sas_port_delete(phy->port);
806 sas_put_device(child);
810 /* See if this phy is part of a wide port */
811 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
813 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
816 for (i = 0; i < parent->ex_dev.num_phys; i++) {
817 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
822 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
823 SAS_ADDR_SIZE) && ephy->port) {
824 sas_port_add_phy(ephy->port, phy->phy);
825 phy->port = ephy->port;
826 phy->phy_state = PHY_DEVICE_DISCOVERED;
834 static struct domain_device *sas_ex_discover_expander(
835 struct domain_device *parent, int phy_id)
837 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
838 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
839 struct domain_device *child = NULL;
840 struct sas_rphy *rphy;
841 struct sas_expander_device *edev;
842 struct asd_sas_port *port;
845 if (phy->routing_attr == DIRECT_ROUTING) {
846 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
848 SAS_ADDR(parent->sas_addr), phy_id,
849 SAS_ADDR(phy->attached_sas_addr),
850 phy->attached_phy_id);
853 child = sas_alloc_device();
857 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
858 /* FIXME: better error handling */
859 BUG_ON(sas_port_add(phy->port) != 0);
862 switch (phy->attached_dev_type) {
864 rphy = sas_expander_alloc(phy->port,
865 SAS_EDGE_EXPANDER_DEVICE);
868 rphy = sas_expander_alloc(phy->port,
869 SAS_FANOUT_EXPANDER_DEVICE);
872 rphy = NULL; /* shut gcc up */
877 edev = rphy_to_expander_device(rphy);
878 child->dev_type = phy->attached_dev_type;
879 kref_get(&parent->kref);
880 child->parent = parent;
882 child->iproto = phy->attached_iproto;
883 child->tproto = phy->attached_tproto;
884 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
885 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
886 sas_ex_get_linkrate(parent, child, phy);
887 edev->level = parent_ex->level + 1;
888 parent->port->disc.max_level = max(parent->port->disc.max_level,
891 sas_fill_in_rphy(child, rphy);
894 spin_lock_irq(&parent->port->dev_list_lock);
895 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
896 spin_unlock_irq(&parent->port->dev_list_lock);
898 res = sas_discover_expander(child);
900 spin_lock_irq(&parent->port->dev_list_lock);
901 list_del(&child->dev_list_node);
902 spin_unlock_irq(&parent->port->dev_list_lock);
903 sas_put_device(child);
906 list_add_tail(&child->siblings, &parent->ex_dev.children);
910 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
912 struct expander_device *ex = &dev->ex_dev;
913 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
914 struct domain_device *child = NULL;
918 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
919 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
920 res = sas_ex_phy_discover(dev, phy_id);
925 /* Parent and domain coherency */
926 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
927 SAS_ADDR(dev->port->sas_addr))) {
928 sas_add_parent_port(dev, phy_id);
931 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
932 SAS_ADDR(dev->parent->sas_addr))) {
933 sas_add_parent_port(dev, phy_id);
934 if (ex_phy->routing_attr == TABLE_ROUTING)
935 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
939 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
940 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
942 if (ex_phy->attached_dev_type == NO_DEVICE) {
943 if (ex_phy->routing_attr == DIRECT_ROUTING) {
944 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
945 sas_configure_routing(dev, ex_phy->attached_sas_addr);
948 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
951 if (ex_phy->attached_dev_type != SAS_END_DEV &&
952 ex_phy->attached_dev_type != FANOUT_DEV &&
953 ex_phy->attached_dev_type != EDGE_DEV) {
954 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
955 "phy 0x%x\n", ex_phy->attached_dev_type,
956 SAS_ADDR(dev->sas_addr),
961 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
963 SAS_DPRINTK("configure routing for dev %016llx "
964 "reported 0x%x. Forgotten\n",
965 SAS_ADDR(ex_phy->attached_sas_addr), res);
966 sas_disable_routing(dev, ex_phy->attached_sas_addr);
970 res = sas_ex_join_wide_port(dev, phy_id);
972 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
973 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
977 switch (ex_phy->attached_dev_type) {
979 child = sas_ex_discover_end_dev(dev, phy_id);
982 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
983 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
984 "attached to ex %016llx phy 0x%x\n",
985 SAS_ADDR(ex_phy->attached_sas_addr),
986 ex_phy->attached_phy_id,
987 SAS_ADDR(dev->sas_addr),
989 sas_ex_disable_phy(dev, phy_id);
992 memcpy(dev->port->disc.fanout_sas_addr,
993 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
996 child = sas_ex_discover_expander(dev, phy_id);
1005 for (i = 0; i < ex->num_phys; i++) {
1006 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1007 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1010 * Due to races, the phy might not get added to the
1011 * wide port, so we add the phy to the wide port here.
1013 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1014 SAS_ADDR(child->sas_addr)) {
1015 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1016 res = sas_ex_join_wide_port(dev, i);
1018 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1019 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1028 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1030 struct expander_device *ex = &dev->ex_dev;
1033 for (i = 0; i < ex->num_phys; i++) {
1034 struct ex_phy *phy = &ex->ex_phy[i];
1036 if (phy->phy_state == PHY_VACANT ||
1037 phy->phy_state == PHY_NOT_PRESENT)
1040 if ((phy->attached_dev_type == EDGE_DEV ||
1041 phy->attached_dev_type == FANOUT_DEV) &&
1042 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1044 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1052 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1054 struct expander_device *ex = &dev->ex_dev;
1055 struct domain_device *child;
1056 u8 sub_addr[8] = {0, };
1058 list_for_each_entry(child, &ex->children, siblings) {
1059 if (child->dev_type != EDGE_DEV &&
1060 child->dev_type != FANOUT_DEV)
1062 if (sub_addr[0] == 0) {
1063 sas_find_sub_addr(child, sub_addr);
1068 if (sas_find_sub_addr(child, s2) &&
1069 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1071 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1072 "diverges from subtractive "
1073 "boundary %016llx\n",
1074 SAS_ADDR(dev->sas_addr),
1075 SAS_ADDR(child->sas_addr),
1077 SAS_ADDR(sub_addr));
1079 sas_ex_disable_port(child, s2);
1086 * sas_ex_discover_devices -- discover devices attached to this expander
1087 * dev: pointer to the expander domain device
1088 * single: if you want to do a single phy, else set to -1;
1090 * Configure this expander for use with its devices and register the
1091 * devices of this expander.
1093 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1095 struct expander_device *ex = &dev->ex_dev;
1096 int i = 0, end = ex->num_phys;
1099 if (0 <= single && single < end) {
1104 for ( ; i < end; i++) {
1105 struct ex_phy *ex_phy = &ex->ex_phy[i];
1107 if (ex_phy->phy_state == PHY_VACANT ||
1108 ex_phy->phy_state == PHY_NOT_PRESENT ||
1109 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1112 switch (ex_phy->linkrate) {
1113 case SAS_PHY_DISABLED:
1114 case SAS_PHY_RESET_PROBLEM:
1115 case SAS_SATA_PORT_SELECTOR:
1118 res = sas_ex_discover_dev(dev, i);
1126 sas_check_level_subtractive_boundary(dev);
1131 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1133 struct expander_device *ex = &dev->ex_dev;
1135 u8 *sub_sas_addr = NULL;
1137 if (dev->dev_type != EDGE_DEV)
1140 for (i = 0; i < ex->num_phys; i++) {
1141 struct ex_phy *phy = &ex->ex_phy[i];
1143 if (phy->phy_state == PHY_VACANT ||
1144 phy->phy_state == PHY_NOT_PRESENT)
1147 if ((phy->attached_dev_type == FANOUT_DEV ||
1148 phy->attached_dev_type == EDGE_DEV) &&
1149 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1152 sub_sas_addr = &phy->attached_sas_addr[0];
1153 else if (SAS_ADDR(sub_sas_addr) !=
1154 SAS_ADDR(phy->attached_sas_addr)) {
1156 SAS_DPRINTK("ex %016llx phy 0x%x "
1157 "diverges(%016llx) on subtractive "
1158 "boundary(%016llx). Disabled\n",
1159 SAS_ADDR(dev->sas_addr), i,
1160 SAS_ADDR(phy->attached_sas_addr),
1161 SAS_ADDR(sub_sas_addr));
1162 sas_ex_disable_phy(dev, i);
1169 static void sas_print_parent_topology_bug(struct domain_device *child,
1170 struct ex_phy *parent_phy,
1171 struct ex_phy *child_phy)
1173 static const char ra_char[] = {
1174 [DIRECT_ROUTING] = 'D',
1175 [SUBTRACTIVE_ROUTING] = 'S',
1176 [TABLE_ROUTING] = 'T',
1178 static const char *ex_type[] = {
1179 [EDGE_DEV] = "edge",
1180 [FANOUT_DEV] = "fanout",
1182 struct domain_device *parent = child->parent;
1184 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1185 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
1187 ex_type[parent->dev_type],
1188 SAS_ADDR(parent->sas_addr),
1189 parent->ex_dev.t2t_supp,
1192 ex_type[child->dev_type],
1193 SAS_ADDR(child->sas_addr),
1194 child->ex_dev.t2t_supp,
1197 ra_char[parent_phy->routing_attr],
1198 ra_char[child_phy->routing_attr]);
1201 static int sas_check_eeds(struct domain_device *child,
1202 struct ex_phy *parent_phy,
1203 struct ex_phy *child_phy)
1206 struct domain_device *parent = child->parent;
1208 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1210 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1211 "phy S:0x%x, while there is a fanout ex %016llx\n",
1212 SAS_ADDR(parent->sas_addr),
1214 SAS_ADDR(child->sas_addr),
1216 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1217 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1218 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1220 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1222 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1223 SAS_ADDR(parent->sas_addr)) ||
1224 (SAS_ADDR(parent->port->disc.eeds_a) ==
1225 SAS_ADDR(child->sas_addr)))
1227 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1228 SAS_ADDR(parent->sas_addr)) ||
1229 (SAS_ADDR(parent->port->disc.eeds_b) ==
1230 SAS_ADDR(child->sas_addr))))
1234 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1235 "phy 0x%x link forms a third EEDS!\n",
1236 SAS_ADDR(parent->sas_addr),
1238 SAS_ADDR(child->sas_addr),
1245 /* Here we spill over 80 columns. It is intentional.
1247 static int sas_check_parent_topology(struct domain_device *child)
1249 struct expander_device *child_ex = &child->ex_dev;
1250 struct expander_device *parent_ex;
1257 if (child->parent->dev_type != EDGE_DEV &&
1258 child->parent->dev_type != FANOUT_DEV)
1261 parent_ex = &child->parent->ex_dev;
1263 for (i = 0; i < parent_ex->num_phys; i++) {
1264 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1265 struct ex_phy *child_phy;
1267 if (parent_phy->phy_state == PHY_VACANT ||
1268 parent_phy->phy_state == PHY_NOT_PRESENT)
1271 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1274 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1276 switch (child->parent->dev_type) {
1278 if (child->dev_type == FANOUT_DEV) {
1279 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1280 child_phy->routing_attr != TABLE_ROUTING) {
1281 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1284 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1285 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1286 res = sas_check_eeds(child, parent_phy, child_phy);
1287 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1288 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1291 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1292 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1293 (child_phy->routing_attr == TABLE_ROUTING &&
1294 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1297 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1303 if (parent_phy->routing_attr != TABLE_ROUTING ||
1304 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1305 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1317 #define RRI_REQ_SIZE 16
1318 #define RRI_RESP_SIZE 44
1320 static int sas_configure_present(struct domain_device *dev, int phy_id,
1321 u8 *sas_addr, int *index, int *present)
1324 struct expander_device *ex = &dev->ex_dev;
1325 struct ex_phy *phy = &ex->ex_phy[phy_id];
1332 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1336 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1342 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1343 rri_req[9] = phy_id;
1345 for (i = 0; i < ex->max_route_indexes ; i++) {
1346 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1347 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1352 if (res == SMP_RESP_NO_INDEX) {
1353 SAS_DPRINTK("overflow of indexes: dev %016llx "
1354 "phy 0x%x index 0x%x\n",
1355 SAS_ADDR(dev->sas_addr), phy_id, i);
1357 } else if (res != SMP_RESP_FUNC_ACC) {
1358 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1359 "result 0x%x\n", __func__,
1360 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1363 if (SAS_ADDR(sas_addr) != 0) {
1364 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1366 if ((rri_resp[12] & 0x80) == 0x80)
1371 } else if (SAS_ADDR(rri_resp+16) == 0) {
1376 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1377 phy->last_da_index < i) {
1378 phy->last_da_index = i;
1391 #define CRI_REQ_SIZE 44
1392 #define CRI_RESP_SIZE 8
1394 static int sas_configure_set(struct domain_device *dev, int phy_id,
1395 u8 *sas_addr, int index, int include)
1401 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1405 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1411 cri_req[1] = SMP_CONF_ROUTE_INFO;
1412 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1413 cri_req[9] = phy_id;
1414 if (SAS_ADDR(sas_addr) == 0 || !include)
1415 cri_req[12] |= 0x80;
1416 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1418 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1423 if (res == SMP_RESP_NO_INDEX) {
1424 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1426 SAS_ADDR(dev->sas_addr), phy_id, index);
1434 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1435 u8 *sas_addr, int include)
1441 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1444 if (include ^ present)
1445 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1451 * sas_configure_parent -- configure routing table of parent
1452 * parent: parent expander
1453 * child: child expander
1454 * sas_addr: SAS port identifier of device directly attached to child
1456 static int sas_configure_parent(struct domain_device *parent,
1457 struct domain_device *child,
1458 u8 *sas_addr, int include)
1460 struct expander_device *ex_parent = &parent->ex_dev;
1464 if (parent->parent) {
1465 res = sas_configure_parent(parent->parent, parent, sas_addr,
1471 if (ex_parent->conf_route_table == 0) {
1472 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1473 SAS_ADDR(parent->sas_addr));
1477 for (i = 0; i < ex_parent->num_phys; i++) {
1478 struct ex_phy *phy = &ex_parent->ex_phy[i];
1480 if ((phy->routing_attr == TABLE_ROUTING) &&
1481 (SAS_ADDR(phy->attached_sas_addr) ==
1482 SAS_ADDR(child->sas_addr))) {
1483 res = sas_configure_phy(parent, i, sas_addr, include);
1493 * sas_configure_routing -- configure routing
1494 * dev: expander device
1495 * sas_addr: port identifier of device directly attached to the expander device
1497 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1500 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1504 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1507 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1512 * sas_discover_expander -- expander discovery
1513 * @ex: pointer to expander domain device
1515 * See comment in sas_discover_sata().
1517 static int sas_discover_expander(struct domain_device *dev)
1521 res = sas_notify_lldd_dev_found(dev);
1525 res = sas_ex_general(dev);
1528 res = sas_ex_manuf_info(dev);
1532 res = sas_expander_discover(dev);
1534 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1535 SAS_ADDR(dev->sas_addr), res);
1539 sas_check_ex_subtractive_boundary(dev);
1540 res = sas_check_parent_topology(dev);
1545 sas_notify_lldd_dev_gone(dev);
1549 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1552 struct domain_device *dev;
1554 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1555 if (dev->dev_type == EDGE_DEV ||
1556 dev->dev_type == FANOUT_DEV) {
1557 struct sas_expander_device *ex =
1558 rphy_to_expander_device(dev->rphy);
1560 if (level == ex->level)
1561 res = sas_ex_discover_devices(dev, -1);
1563 res = sas_ex_discover_devices(port->port_dev, -1);
1571 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1577 level = port->disc.max_level;
1578 res = sas_ex_level_discovery(port, level);
1580 } while (level < port->disc.max_level);
1585 int sas_discover_root_expander(struct domain_device *dev)
1588 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1590 res = sas_rphy_add(dev->rphy);
1594 ex->level = dev->port->disc.max_level; /* 0 */
1595 res = sas_discover_expander(dev);
1599 sas_ex_bfs_disc(dev->port);
1604 sas_rphy_remove(dev->rphy);
1609 /* ---------- Domain revalidation ---------- */
1611 static int sas_get_phy_discover(struct domain_device *dev,
1612 int phy_id, struct smp_resp *disc_resp)
1617 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1621 disc_req[1] = SMP_DISCOVER;
1622 disc_req[9] = phy_id;
1624 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1625 disc_resp, DISCOVER_RESP_SIZE);
1628 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1629 res = disc_resp->result;
1637 static int sas_get_phy_change_count(struct domain_device *dev,
1638 int phy_id, int *pcc)
1641 struct smp_resp *disc_resp;
1643 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1647 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1649 *pcc = disc_resp->disc.change_count;
1655 int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1656 u8 *attached_sas_addr)
1659 struct smp_resp *disc_resp;
1660 struct discover_resp *dr;
1662 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1665 dr = &disc_resp->disc;
1667 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1669 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1670 if (dr->attached_dev_type == 0)
1671 memset(attached_sas_addr, 0, 8);
1677 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1678 int from_phy, bool update)
1680 struct expander_device *ex = &dev->ex_dev;
1684 for (i = from_phy; i < ex->num_phys; i++) {
1685 int phy_change_count = 0;
1687 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1690 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1692 ex->ex_phy[i].phy_change_count =
1702 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1706 struct smp_resp *rg_resp;
1708 rg_req = alloc_smp_req(RG_REQ_SIZE);
1712 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1718 rg_req[1] = SMP_REPORT_GENERAL;
1720 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1724 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1725 res = rg_resp->result;
1729 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1736 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1737 * @dev:domain device to be detect.
1738 * @src_dev: the device which originated BROADCAST(CHANGE).
1740 * Add self-configuration expander suport. Suppose two expander cascading,
1741 * when the first level expander is self-configuring, hotplug the disks in
1742 * second level expander, BROADCAST(CHANGE) will not only be originated
1743 * in the second level expander, but also be originated in the first level
1744 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1745 * expander changed count in two level expanders will all increment at least
1746 * once, but the phy which chang count has changed is the source device which
1750 static int sas_find_bcast_dev(struct domain_device *dev,
1751 struct domain_device **src_dev)
1753 struct expander_device *ex = &dev->ex_dev;
1754 int ex_change_count = -1;
1757 struct domain_device *ch;
1759 res = sas_get_ex_change_count(dev, &ex_change_count);
1762 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1763 /* Just detect if this expander phys phy change count changed,
1764 * in order to determine if this expander originate BROADCAST,
1765 * and do not update phy change count field in our structure.
1767 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1770 ex->ex_change_count = ex_change_count;
1771 SAS_DPRINTK("Expander phy change count has changed\n");
1774 SAS_DPRINTK("Expander phys DID NOT change\n");
1776 list_for_each_entry(ch, &ex->children, siblings) {
1777 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1778 res = sas_find_bcast_dev(ch, src_dev);
1787 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1789 struct expander_device *ex = &dev->ex_dev;
1790 struct domain_device *child, *n;
1792 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1793 set_bit(SAS_DEV_GONE, &child->state);
1794 if (child->dev_type == EDGE_DEV ||
1795 child->dev_type == FANOUT_DEV)
1796 sas_unregister_ex_tree(port, child);
1798 sas_unregister_dev(port, child);
1800 sas_unregister_dev(port, dev);
1803 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1804 int phy_id, bool last)
1806 struct expander_device *ex_dev = &parent->ex_dev;
1807 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1808 struct domain_device *child, *n;
1810 list_for_each_entry_safe(child, n,
1811 &ex_dev->children, siblings) {
1812 if (SAS_ADDR(child->sas_addr) ==
1813 SAS_ADDR(phy->attached_sas_addr)) {
1814 set_bit(SAS_DEV_GONE, &child->state);
1815 if (child->dev_type == EDGE_DEV ||
1816 child->dev_type == FANOUT_DEV)
1817 sas_unregister_ex_tree(parent->port, child);
1819 sas_unregister_dev(parent->port, child);
1823 set_bit(SAS_DEV_GONE, &parent->state);
1824 sas_disable_routing(parent, phy->attached_sas_addr);
1826 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1828 sas_port_delete_phy(phy->port, phy->phy);
1829 if (phy->port->num_phys == 0)
1830 sas_port_delete(phy->port);
1835 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1838 struct expander_device *ex_root = &root->ex_dev;
1839 struct domain_device *child;
1842 list_for_each_entry(child, &ex_root->children, siblings) {
1843 if (child->dev_type == EDGE_DEV ||
1844 child->dev_type == FANOUT_DEV) {
1845 struct sas_expander_device *ex =
1846 rphy_to_expander_device(child->rphy);
1848 if (level > ex->level)
1849 res = sas_discover_bfs_by_root_level(child,
1851 else if (level == ex->level)
1852 res = sas_ex_discover_devices(child, -1);
1858 static int sas_discover_bfs_by_root(struct domain_device *dev)
1861 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1862 int level = ex->level+1;
1864 res = sas_ex_discover_devices(dev, -1);
1868 res = sas_discover_bfs_by_root_level(dev, level);
1871 } while (level <= dev->port->disc.max_level);
1876 static int sas_discover_new(struct domain_device *dev, int phy_id)
1878 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1879 struct domain_device *child;
1883 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1884 SAS_ADDR(dev->sas_addr), phy_id);
1885 res = sas_ex_phy_discover(dev, phy_id);
1888 /* to support the wide port inserted */
1889 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1890 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1893 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1894 SAS_ADDR(ex_phy->attached_sas_addr)) {
1900 sas_ex_join_wide_port(dev, phy_id);
1903 res = sas_ex_discover_devices(dev, phy_id);
1906 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1907 if (SAS_ADDR(child->sas_addr) ==
1908 SAS_ADDR(ex_phy->attached_sas_addr)) {
1909 if (child->dev_type == EDGE_DEV ||
1910 child->dev_type == FANOUT_DEV)
1911 res = sas_discover_bfs_by_root(child);
1919 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1921 struct expander_device *ex = &dev->ex_dev;
1922 struct ex_phy *phy = &ex->ex_phy[phy_id];
1923 u8 attached_sas_addr[8];
1926 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1928 case SMP_RESP_NO_PHY:
1929 phy->phy_state = PHY_NOT_PRESENT;
1930 sas_unregister_devs_sas_addr(dev, phy_id, last);
1932 case SMP_RESP_PHY_VACANT:
1933 phy->phy_state = PHY_VACANT;
1934 sas_unregister_devs_sas_addr(dev, phy_id, last);
1936 case SMP_RESP_FUNC_ACC:
1940 if (SAS_ADDR(attached_sas_addr) == 0) {
1941 phy->phy_state = PHY_EMPTY;
1942 sas_unregister_devs_sas_addr(dev, phy_id, last);
1943 } else if (SAS_ADDR(attached_sas_addr) ==
1944 SAS_ADDR(phy->attached_sas_addr)) {
1945 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1946 SAS_ADDR(dev->sas_addr), phy_id);
1947 sas_ex_phy_discover(dev, phy_id);
1949 res = sas_discover_new(dev, phy_id);
1955 * sas_rediscover - revalidate the domain.
1956 * @dev:domain device to be detect.
1957 * @phy_id: the phy id will be detected.
1959 * NOTE: this process _must_ quit (return) as soon as any connection
1960 * errors are encountered. Connection recovery is done elsewhere.
1961 * Discover process only interrogates devices in order to discover the
1962 * domain.For plugging out, we un-register the device only when it is
1963 * the last phy in the port, for other phys in this port, we just delete it
1964 * from the port.For inserting, we do discovery when it is the
1965 * first phy,for other phys in this port, we add it to the port to
1966 * forming the wide-port.
1968 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1970 struct expander_device *ex = &dev->ex_dev;
1971 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1974 bool last = true; /* is this the last phy of the port */
1976 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1977 SAS_ADDR(dev->sas_addr), phy_id);
1979 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1980 for (i = 0; i < ex->num_phys; i++) {
1981 struct ex_phy *phy = &ex->ex_phy[i];
1985 if (SAS_ADDR(phy->attached_sas_addr) ==
1986 SAS_ADDR(changed_phy->attached_sas_addr)) {
1987 SAS_DPRINTK("phy%d part of wide port with "
1988 "phy%d\n", phy_id, i);
1993 res = sas_rediscover_dev(dev, phy_id, last);
1995 res = sas_discover_new(dev, phy_id);
2000 * sas_revalidate_domain -- revalidate the domain
2001 * @port: port to the domain of interest
2003 * NOTE: this process _must_ quit (return) as soon as any connection
2004 * errors are encountered. Connection recovery is done elsewhere.
2005 * Discover process only interrogates devices in order to discover the
2008 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2011 struct domain_device *dev = NULL;
2013 res = sas_find_bcast_dev(port_dev, &dev);
2017 struct expander_device *ex = &dev->ex_dev;
2022 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2025 res = sas_rediscover(dev, phy_id);
2027 } while (i < ex->num_phys);
2033 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2034 struct request *req)
2036 struct domain_device *dev;
2038 struct request *rsp = req->next_rq;
2041 printk("%s: space for a smp response is missing\n",
2046 /* no rphy means no smp target support (ie aic94xx host) */
2048 return sas_smp_host_handler(shost, req, rsp);
2050 type = rphy->identify.device_type;
2052 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2053 type != SAS_FANOUT_EXPANDER_DEVICE) {
2054 printk("%s: can we send a smp request to a device?\n",
2059 dev = sas_find_dev_by_rphy(rphy);
2061 printk("%s: fail to find a domain_device?\n", __func__);
2065 /* do we need to support multiple segments? */
2066 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2067 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2068 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2069 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2073 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2074 bio_data(rsp->bio), blk_rq_bytes(rsp));
2076 /* positive number is the untransferred residual */
2077 rsp->resid_len = ret;
2080 } else if (ret == 0) {