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 if (test_bit(SAS_DEV_GONE, &dev->state)) {
82 task = sas_alloc_task(GFP_KERNEL);
88 task->task_proto = dev->tproto;
89 sg_init_one(&task->smp_task.smp_req, req, req_size);
90 sg_init_one(&task->smp_task.smp_resp, resp, resp_size);
92 task->task_done = smp_task_done;
94 task->timer.data = (unsigned long) task;
95 task->timer.function = smp_task_timedout;
96 task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
97 add_timer(&task->timer);
99 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
102 del_timer(&task->timer);
103 SAS_DPRINTK("executing SMP task failed:%d\n", res);
107 wait_for_completion(&task->completion);
109 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
110 SAS_DPRINTK("smp task timed out or aborted\n");
111 i->dft->lldd_abort_task(task);
112 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
113 SAS_DPRINTK("SMP task aborted and not done\n");
117 if (task->task_status.resp == SAS_TASK_COMPLETE &&
118 task->task_status.stat == SAM_STAT_GOOD) {
122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
123 task->task_status.stat == SAS_DATA_UNDERRUN) {
124 /* no error, but return the number of bytes of
126 res = task->task_status.residual;
129 if (task->task_status.resp == SAS_TASK_COMPLETE &&
130 task->task_status.stat == SAS_DATA_OVERRUN) {
134 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
135 task->task_status.stat == SAS_DEVICE_UNKNOWN)
138 SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
139 "status 0x%x\n", __func__,
140 SAS_ADDR(dev->sas_addr),
141 task->task_status.resp,
142 task->task_status.stat);
147 mutex_unlock(&dev->ex_dev.cmd_mutex);
149 BUG_ON(retry == 3 && task != NULL);
154 /* ---------- Allocations ---------- */
156 static inline void *alloc_smp_req(int size)
158 u8 *p = kzalloc(size, GFP_KERNEL);
164 static inline void *alloc_smp_resp(int size)
166 return kzalloc(size, GFP_KERNEL);
169 /* ---------- Expander configuration ---------- */
171 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
174 struct expander_device *ex = &dev->ex_dev;
175 struct ex_phy *phy = &ex->ex_phy[phy_id];
176 struct smp_resp *resp = disc_resp;
177 struct discover_resp *dr = &resp->disc;
178 struct sas_rphy *rphy = dev->rphy;
179 int rediscover = (phy->phy != NULL);
182 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
184 /* FIXME: error_handling */
188 switch (resp->result) {
189 case SMP_RESP_PHY_VACANT:
190 phy->phy_state = PHY_VACANT;
193 phy->phy_state = PHY_NOT_PRESENT;
195 case SMP_RESP_FUNC_ACC:
196 phy->phy_state = PHY_EMPTY; /* do not know yet */
200 phy->phy_id = phy_id;
201 phy->attached_dev_type = dr->attached_dev_type;
202 phy->linkrate = dr->linkrate;
203 phy->attached_sata_host = dr->attached_sata_host;
204 phy->attached_sata_dev = dr->attached_sata_dev;
205 phy->attached_sata_ps = dr->attached_sata_ps;
206 phy->attached_iproto = dr->iproto << 1;
207 phy->attached_tproto = dr->tproto << 1;
208 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
209 phy->attached_phy_id = dr->attached_phy_id;
210 phy->phy_change_count = dr->change_count;
211 phy->routing_attr = dr->routing_attr;
212 phy->virtual = dr->virtual;
213 phy->last_da_index = -1;
215 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
216 phy->phy->identify.device_type = phy->attached_dev_type;
217 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
218 phy->phy->identify.target_port_protocols = phy->attached_tproto;
219 phy->phy->identify.phy_identifier = phy_id;
220 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
221 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
222 phy->phy->minimum_linkrate = dr->pmin_linkrate;
223 phy->phy->maximum_linkrate = dr->pmax_linkrate;
224 phy->phy->negotiated_linkrate = phy->linkrate;
227 if (sas_phy_add(phy->phy)) {
228 sas_phy_free(phy->phy);
232 SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n",
233 SAS_ADDR(dev->sas_addr), phy->phy_id,
234 phy->routing_attr == TABLE_ROUTING ? 'T' :
235 phy->routing_attr == DIRECT_ROUTING ? 'D' :
236 phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?',
237 SAS_ADDR(phy->attached_sas_addr));
242 /* check if we have an existing attached ata device on this expander phy */
243 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
245 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
246 struct domain_device *dev;
247 struct sas_rphy *rphy;
252 rphy = ex_phy->port->rphy;
256 dev = sas_find_dev_by_rphy(rphy);
258 if (dev && dev_is_sata(dev))
264 #define DISCOVER_REQ_SIZE 16
265 #define DISCOVER_RESP_SIZE 56
267 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
268 u8 *disc_resp, int single)
270 struct domain_device *ata_dev = sas_ex_to_ata(dev, single);
273 disc_req[9] = single;
274 for (i = 1 ; i < 3; i++) {
275 struct discover_resp *dr;
277 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
278 disc_resp, DISCOVER_RESP_SIZE);
281 dr = &((struct smp_resp *)disc_resp)->disc;
282 if (memcmp(dev->sas_addr, dr->attached_sas_addr,
283 SAS_ADDR_SIZE) == 0) {
284 sas_printk("Found loopback topology, just ignore it!\n");
288 /* This is detecting a failure to transmit initial
289 * dev to host FIS as described in section J.5 of
292 if (!(dr->attached_dev_type == 0 &&
293 dr->attached_sata_dev))
296 /* In order to generate the dev to host FIS, we send a
297 * link reset to the expander port. If a device was
298 * previously detected on this port we ask libata to
299 * manage the reset and link recovery.
302 sas_ata_schedule_reset(ata_dev);
305 sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL);
306 /* Wait for the reset to trigger the negotiation */
309 sas_set_ex_phy(dev, single, disc_resp);
313 static int sas_ex_phy_discover(struct domain_device *dev, int single)
315 struct expander_device *ex = &dev->ex_dev;
320 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
324 disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE);
330 disc_req[1] = SMP_DISCOVER;
332 if (0 <= single && single < ex->num_phys) {
333 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
337 for (i = 0; i < ex->num_phys; i++) {
338 res = sas_ex_phy_discover_helper(dev, disc_req,
350 static int sas_expander_discover(struct domain_device *dev)
352 struct expander_device *ex = &dev->ex_dev;
355 ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL);
359 res = sas_ex_phy_discover(dev, -1);
370 #define MAX_EXPANDER_PHYS 128
372 static void ex_assign_report_general(struct domain_device *dev,
373 struct smp_resp *resp)
375 struct report_general_resp *rg = &resp->rg;
377 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
378 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
379 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
380 dev->ex_dev.t2t_supp = rg->t2t_supp;
381 dev->ex_dev.conf_route_table = rg->conf_route_table;
382 dev->ex_dev.configuring = rg->configuring;
383 memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
386 #define RG_REQ_SIZE 8
387 #define RG_RESP_SIZE 32
389 static int sas_ex_general(struct domain_device *dev)
392 struct smp_resp *rg_resp;
396 rg_req = alloc_smp_req(RG_REQ_SIZE);
400 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
406 rg_req[1] = SMP_REPORT_GENERAL;
408 for (i = 0; i < 5; i++) {
409 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
413 SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
414 SAS_ADDR(dev->sas_addr), res);
416 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
417 SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
418 SAS_ADDR(dev->sas_addr), rg_resp->result);
419 res = rg_resp->result;
423 ex_assign_report_general(dev, rg_resp);
425 if (dev->ex_dev.configuring) {
426 SAS_DPRINTK("RG: ex %llx self-configuring...\n",
427 SAS_ADDR(dev->sas_addr));
428 schedule_timeout_interruptible(5*HZ);
438 static void ex_assign_manuf_info(struct domain_device *dev, void
441 u8 *mi_resp = _mi_resp;
442 struct sas_rphy *rphy = dev->rphy;
443 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
445 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
446 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
447 memcpy(edev->product_rev, mi_resp + 36,
448 SAS_EXPANDER_PRODUCT_REV_LEN);
450 if (mi_resp[8] & 1) {
451 memcpy(edev->component_vendor_id, mi_resp + 40,
452 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
453 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
454 edev->component_revision_id = mi_resp[50];
458 #define MI_REQ_SIZE 8
459 #define MI_RESP_SIZE 64
461 static int sas_ex_manuf_info(struct domain_device *dev)
467 mi_req = alloc_smp_req(MI_REQ_SIZE);
471 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
477 mi_req[1] = SMP_REPORT_MANUF_INFO;
479 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
481 SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
482 SAS_ADDR(dev->sas_addr), res);
484 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
485 SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
486 SAS_ADDR(dev->sas_addr), mi_resp[2]);
490 ex_assign_manuf_info(dev, mi_resp);
497 #define PC_REQ_SIZE 44
498 #define PC_RESP_SIZE 8
500 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
501 enum phy_func phy_func,
502 struct sas_phy_linkrates *rates)
508 pc_req = alloc_smp_req(PC_REQ_SIZE);
512 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
518 pc_req[1] = SMP_PHY_CONTROL;
520 pc_req[10]= phy_func;
522 pc_req[32] = rates->minimum_linkrate << 4;
523 pc_req[33] = rates->maximum_linkrate << 4;
526 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
533 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
535 struct expander_device *ex = &dev->ex_dev;
536 struct ex_phy *phy = &ex->ex_phy[phy_id];
538 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
539 phy->linkrate = SAS_PHY_DISABLED;
542 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
544 struct expander_device *ex = &dev->ex_dev;
547 for (i = 0; i < ex->num_phys; i++) {
548 struct ex_phy *phy = &ex->ex_phy[i];
550 if (phy->phy_state == PHY_VACANT ||
551 phy->phy_state == PHY_NOT_PRESENT)
554 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
555 sas_ex_disable_phy(dev, i);
559 static int sas_dev_present_in_domain(struct asd_sas_port *port,
562 struct domain_device *dev;
564 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
566 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
567 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
573 #define RPEL_REQ_SIZE 16
574 #define RPEL_RESP_SIZE 32
575 int sas_smp_get_phy_events(struct sas_phy *phy)
580 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
581 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
583 req = alloc_smp_req(RPEL_REQ_SIZE);
587 resp = alloc_smp_resp(RPEL_RESP_SIZE);
593 req[1] = SMP_REPORT_PHY_ERR_LOG;
594 req[9] = phy->number;
596 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
597 resp, RPEL_RESP_SIZE);
602 phy->invalid_dword_count = scsi_to_u32(&resp[12]);
603 phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
604 phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
605 phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);
613 #ifdef CONFIG_SCSI_SAS_ATA
615 #define RPS_REQ_SIZE 16
616 #define RPS_RESP_SIZE 60
618 static int sas_get_report_phy_sata(struct domain_device *dev,
620 struct smp_resp *rps_resp)
623 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
624 u8 *resp = (u8 *)rps_resp;
629 rps_req[1] = SMP_REPORT_PHY_SATA;
632 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
633 rps_resp, RPS_RESP_SIZE);
635 /* 0x34 is the FIS type for the D2H fis. There's a potential
636 * standards cockup here. sas-2 explicitly specifies the FIS
637 * should be encoded so that FIS type is in resp[24].
638 * However, some expanders endian reverse this. Undo the
640 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
643 for (i = 0; i < 5; i++) {
648 resp[j + 0] = resp[j + 3];
649 resp[j + 1] = resp[j + 2];
660 static void sas_ex_get_linkrate(struct domain_device *parent,
661 struct domain_device *child,
662 struct ex_phy *parent_phy)
664 struct expander_device *parent_ex = &parent->ex_dev;
665 struct sas_port *port;
670 port = parent_phy->port;
672 for (i = 0; i < parent_ex->num_phys; i++) {
673 struct ex_phy *phy = &parent_ex->ex_phy[i];
675 if (phy->phy_state == PHY_VACANT ||
676 phy->phy_state == PHY_NOT_PRESENT)
679 if (SAS_ADDR(phy->attached_sas_addr) ==
680 SAS_ADDR(child->sas_addr)) {
682 child->min_linkrate = min(parent->min_linkrate,
684 child->max_linkrate = max(parent->max_linkrate,
687 sas_port_add_phy(port, phy->phy);
690 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
691 child->pathways = min(child->pathways, parent->pathways);
694 static struct domain_device *sas_ex_discover_end_dev(
695 struct domain_device *parent, int phy_id)
697 struct expander_device *parent_ex = &parent->ex_dev;
698 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
699 struct domain_device *child = NULL;
700 struct sas_rphy *rphy;
703 if (phy->attached_sata_host || phy->attached_sata_ps)
706 child = sas_alloc_device();
710 kref_get(&parent->kref);
711 child->parent = parent;
712 child->port = parent->port;
713 child->iproto = phy->attached_iproto;
714 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
715 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
717 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
718 if (unlikely(!phy->port))
720 if (unlikely(sas_port_add(phy->port) != 0)) {
721 sas_port_free(phy->port);
725 sas_ex_get_linkrate(parent, child, phy);
726 sas_device_set_phy(child, phy->port);
728 #ifdef CONFIG_SCSI_SAS_ATA
729 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
730 child->dev_type = SATA_DEV;
731 if (phy->attached_tproto & SAS_PROTOCOL_STP)
732 child->tproto = phy->attached_tproto;
733 if (phy->attached_sata_dev)
734 child->tproto |= SATA_DEV;
735 res = sas_get_report_phy_sata(parent, phy_id,
736 &child->sata_dev.rps_resp);
738 SAS_DPRINTK("report phy sata to %016llx:0x%x returned "
739 "0x%x\n", SAS_ADDR(parent->sas_addr),
743 memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis,
744 sizeof(struct dev_to_host_fis));
746 rphy = sas_end_device_alloc(phy->port);
754 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
756 res = sas_discover_sata(child);
758 SAS_DPRINTK("sas_discover_sata() for device %16llx at "
759 "%016llx:0x%x returned 0x%x\n",
760 SAS_ADDR(child->sas_addr),
761 SAS_ADDR(parent->sas_addr), phy_id, res);
766 if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
767 child->dev_type = SAS_END_DEV;
768 rphy = sas_end_device_alloc(phy->port);
769 /* FIXME: error handling */
772 child->tproto = phy->attached_tproto;
776 sas_fill_in_rphy(child, rphy);
778 spin_lock_irq(&parent->port->dev_list_lock);
779 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
780 spin_unlock_irq(&parent->port->dev_list_lock);
782 res = sas_discover_end_dev(child);
784 SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
785 "at %016llx:0x%x returned 0x%x\n",
786 SAS_ADDR(child->sas_addr),
787 SAS_ADDR(parent->sas_addr), phy_id, res);
791 SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
792 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
797 list_add_tail(&child->siblings, &parent_ex->children);
801 sas_rphy_free(child->rphy);
804 list_del(&child->disco_list_node);
805 spin_lock_irq(&parent->port->dev_list_lock);
806 list_del(&child->dev_list_node);
807 spin_unlock_irq(&parent->port->dev_list_lock);
809 sas_port_delete(phy->port);
812 sas_put_device(child);
816 /* See if this phy is part of a wide port */
817 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
819 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
822 for (i = 0; i < parent->ex_dev.num_phys; i++) {
823 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
828 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
829 SAS_ADDR_SIZE) && ephy->port) {
830 sas_port_add_phy(ephy->port, phy->phy);
831 phy->port = ephy->port;
832 phy->phy_state = PHY_DEVICE_DISCOVERED;
840 static struct domain_device *sas_ex_discover_expander(
841 struct domain_device *parent, int phy_id)
843 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
844 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
845 struct domain_device *child = NULL;
846 struct sas_rphy *rphy;
847 struct sas_expander_device *edev;
848 struct asd_sas_port *port;
851 if (phy->routing_attr == DIRECT_ROUTING) {
852 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
854 SAS_ADDR(parent->sas_addr), phy_id,
855 SAS_ADDR(phy->attached_sas_addr),
856 phy->attached_phy_id);
859 child = sas_alloc_device();
863 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
864 /* FIXME: better error handling */
865 BUG_ON(sas_port_add(phy->port) != 0);
868 switch (phy->attached_dev_type) {
870 rphy = sas_expander_alloc(phy->port,
871 SAS_EDGE_EXPANDER_DEVICE);
874 rphy = sas_expander_alloc(phy->port,
875 SAS_FANOUT_EXPANDER_DEVICE);
878 rphy = NULL; /* shut gcc up */
883 edev = rphy_to_expander_device(rphy);
884 child->dev_type = phy->attached_dev_type;
885 kref_get(&parent->kref);
886 child->parent = parent;
888 child->iproto = phy->attached_iproto;
889 child->tproto = phy->attached_tproto;
890 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
891 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
892 sas_ex_get_linkrate(parent, child, phy);
893 edev->level = parent_ex->level + 1;
894 parent->port->disc.max_level = max(parent->port->disc.max_level,
897 sas_fill_in_rphy(child, rphy);
900 spin_lock_irq(&parent->port->dev_list_lock);
901 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
902 spin_unlock_irq(&parent->port->dev_list_lock);
904 res = sas_discover_expander(child);
906 spin_lock_irq(&parent->port->dev_list_lock);
907 list_del(&child->dev_list_node);
908 spin_unlock_irq(&parent->port->dev_list_lock);
909 sas_put_device(child);
912 list_add_tail(&child->siblings, &parent->ex_dev.children);
916 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
918 struct expander_device *ex = &dev->ex_dev;
919 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
920 struct domain_device *child = NULL;
924 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
925 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
926 res = sas_ex_phy_discover(dev, phy_id);
931 /* Parent and domain coherency */
932 if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
933 SAS_ADDR(dev->port->sas_addr))) {
934 sas_add_parent_port(dev, phy_id);
937 if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
938 SAS_ADDR(dev->parent->sas_addr))) {
939 sas_add_parent_port(dev, phy_id);
940 if (ex_phy->routing_attr == TABLE_ROUTING)
941 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
945 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
946 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
948 if (ex_phy->attached_dev_type == NO_DEVICE) {
949 if (ex_phy->routing_attr == DIRECT_ROUTING) {
950 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
951 sas_configure_routing(dev, ex_phy->attached_sas_addr);
954 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
957 if (ex_phy->attached_dev_type != SAS_END_DEV &&
958 ex_phy->attached_dev_type != FANOUT_DEV &&
959 ex_phy->attached_dev_type != EDGE_DEV) {
960 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
961 "phy 0x%x\n", ex_phy->attached_dev_type,
962 SAS_ADDR(dev->sas_addr),
967 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
969 SAS_DPRINTK("configure routing for dev %016llx "
970 "reported 0x%x. Forgotten\n",
971 SAS_ADDR(ex_phy->attached_sas_addr), res);
972 sas_disable_routing(dev, ex_phy->attached_sas_addr);
976 res = sas_ex_join_wide_port(dev, phy_id);
978 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
979 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
983 switch (ex_phy->attached_dev_type) {
985 child = sas_ex_discover_end_dev(dev, phy_id);
988 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
989 SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
990 "attached to ex %016llx phy 0x%x\n",
991 SAS_ADDR(ex_phy->attached_sas_addr),
992 ex_phy->attached_phy_id,
993 SAS_ADDR(dev->sas_addr),
995 sas_ex_disable_phy(dev, phy_id);
998 memcpy(dev->port->disc.fanout_sas_addr,
999 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1002 child = sas_ex_discover_expander(dev, phy_id);
1011 for (i = 0; i < ex->num_phys; i++) {
1012 if (ex->ex_phy[i].phy_state == PHY_VACANT ||
1013 ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
1016 * Due to races, the phy might not get added to the
1017 * wide port, so we add the phy to the wide port here.
1019 if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1020 SAS_ADDR(child->sas_addr)) {
1021 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1022 res = sas_ex_join_wide_port(dev, i);
1024 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
1025 i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
1034 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1036 struct expander_device *ex = &dev->ex_dev;
1039 for (i = 0; i < ex->num_phys; i++) {
1040 struct ex_phy *phy = &ex->ex_phy[i];
1042 if (phy->phy_state == PHY_VACANT ||
1043 phy->phy_state == PHY_NOT_PRESENT)
1046 if ((phy->attached_dev_type == EDGE_DEV ||
1047 phy->attached_dev_type == FANOUT_DEV) &&
1048 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1050 memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1058 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1060 struct expander_device *ex = &dev->ex_dev;
1061 struct domain_device *child;
1062 u8 sub_addr[8] = {0, };
1064 list_for_each_entry(child, &ex->children, siblings) {
1065 if (child->dev_type != EDGE_DEV &&
1066 child->dev_type != FANOUT_DEV)
1068 if (sub_addr[0] == 0) {
1069 sas_find_sub_addr(child, sub_addr);
1074 if (sas_find_sub_addr(child, s2) &&
1075 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1077 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1078 "diverges from subtractive "
1079 "boundary %016llx\n",
1080 SAS_ADDR(dev->sas_addr),
1081 SAS_ADDR(child->sas_addr),
1083 SAS_ADDR(sub_addr));
1085 sas_ex_disable_port(child, s2);
1092 * sas_ex_discover_devices -- discover devices attached to this expander
1093 * dev: pointer to the expander domain device
1094 * single: if you want to do a single phy, else set to -1;
1096 * Configure this expander for use with its devices and register the
1097 * devices of this expander.
1099 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1101 struct expander_device *ex = &dev->ex_dev;
1102 int i = 0, end = ex->num_phys;
1105 if (0 <= single && single < end) {
1110 for ( ; i < end; i++) {
1111 struct ex_phy *ex_phy = &ex->ex_phy[i];
1113 if (ex_phy->phy_state == PHY_VACANT ||
1114 ex_phy->phy_state == PHY_NOT_PRESENT ||
1115 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1118 switch (ex_phy->linkrate) {
1119 case SAS_PHY_DISABLED:
1120 case SAS_PHY_RESET_PROBLEM:
1121 case SAS_SATA_PORT_SELECTOR:
1124 res = sas_ex_discover_dev(dev, i);
1132 sas_check_level_subtractive_boundary(dev);
1137 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1139 struct expander_device *ex = &dev->ex_dev;
1141 u8 *sub_sas_addr = NULL;
1143 if (dev->dev_type != EDGE_DEV)
1146 for (i = 0; i < ex->num_phys; i++) {
1147 struct ex_phy *phy = &ex->ex_phy[i];
1149 if (phy->phy_state == PHY_VACANT ||
1150 phy->phy_state == PHY_NOT_PRESENT)
1153 if ((phy->attached_dev_type == FANOUT_DEV ||
1154 phy->attached_dev_type == EDGE_DEV) &&
1155 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1158 sub_sas_addr = &phy->attached_sas_addr[0];
1159 else if (SAS_ADDR(sub_sas_addr) !=
1160 SAS_ADDR(phy->attached_sas_addr)) {
1162 SAS_DPRINTK("ex %016llx phy 0x%x "
1163 "diverges(%016llx) on subtractive "
1164 "boundary(%016llx). Disabled\n",
1165 SAS_ADDR(dev->sas_addr), i,
1166 SAS_ADDR(phy->attached_sas_addr),
1167 SAS_ADDR(sub_sas_addr));
1168 sas_ex_disable_phy(dev, i);
1175 static void sas_print_parent_topology_bug(struct domain_device *child,
1176 struct ex_phy *parent_phy,
1177 struct ex_phy *child_phy)
1179 static const char ra_char[] = {
1180 [DIRECT_ROUTING] = 'D',
1181 [SUBTRACTIVE_ROUTING] = 'S',
1182 [TABLE_ROUTING] = 'T',
1184 static const char *ex_type[] = {
1185 [EDGE_DEV] = "edge",
1186 [FANOUT_DEV] = "fanout",
1188 struct domain_device *parent = child->parent;
1190 sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1191 "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
1193 ex_type[parent->dev_type],
1194 SAS_ADDR(parent->sas_addr),
1195 parent->ex_dev.t2t_supp,
1198 ex_type[child->dev_type],
1199 SAS_ADDR(child->sas_addr),
1200 child->ex_dev.t2t_supp,
1203 ra_char[parent_phy->routing_attr],
1204 ra_char[child_phy->routing_attr]);
1207 static int sas_check_eeds(struct domain_device *child,
1208 struct ex_phy *parent_phy,
1209 struct ex_phy *child_phy)
1212 struct domain_device *parent = child->parent;
1214 if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1216 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1217 "phy S:0x%x, while there is a fanout ex %016llx\n",
1218 SAS_ADDR(parent->sas_addr),
1220 SAS_ADDR(child->sas_addr),
1222 SAS_ADDR(parent->port->disc.fanout_sas_addr));
1223 } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1224 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1226 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1228 } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1229 SAS_ADDR(parent->sas_addr)) ||
1230 (SAS_ADDR(parent->port->disc.eeds_a) ==
1231 SAS_ADDR(child->sas_addr)))
1233 ((SAS_ADDR(parent->port->disc.eeds_b) ==
1234 SAS_ADDR(parent->sas_addr)) ||
1235 (SAS_ADDR(parent->port->disc.eeds_b) ==
1236 SAS_ADDR(child->sas_addr))))
1240 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1241 "phy 0x%x link forms a third EEDS!\n",
1242 SAS_ADDR(parent->sas_addr),
1244 SAS_ADDR(child->sas_addr),
1251 /* Here we spill over 80 columns. It is intentional.
1253 static int sas_check_parent_topology(struct domain_device *child)
1255 struct expander_device *child_ex = &child->ex_dev;
1256 struct expander_device *parent_ex;
1263 if (child->parent->dev_type != EDGE_DEV &&
1264 child->parent->dev_type != FANOUT_DEV)
1267 parent_ex = &child->parent->ex_dev;
1269 for (i = 0; i < parent_ex->num_phys; i++) {
1270 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1271 struct ex_phy *child_phy;
1273 if (parent_phy->phy_state == PHY_VACANT ||
1274 parent_phy->phy_state == PHY_NOT_PRESENT)
1277 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1280 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1282 switch (child->parent->dev_type) {
1284 if (child->dev_type == FANOUT_DEV) {
1285 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1286 child_phy->routing_attr != TABLE_ROUTING) {
1287 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1290 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1291 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1292 res = sas_check_eeds(child, parent_phy, child_phy);
1293 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1294 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1297 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1298 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1299 (child_phy->routing_attr == TABLE_ROUTING &&
1300 child_ex->t2t_supp && parent_ex->t2t_supp)) {
1303 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1309 if (parent_phy->routing_attr != TABLE_ROUTING ||
1310 child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1311 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1323 #define RRI_REQ_SIZE 16
1324 #define RRI_RESP_SIZE 44
1326 static int sas_configure_present(struct domain_device *dev, int phy_id,
1327 u8 *sas_addr, int *index, int *present)
1330 struct expander_device *ex = &dev->ex_dev;
1331 struct ex_phy *phy = &ex->ex_phy[phy_id];
1338 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1342 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1348 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1349 rri_req[9] = phy_id;
1351 for (i = 0; i < ex->max_route_indexes ; i++) {
1352 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1353 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1358 if (res == SMP_RESP_NO_INDEX) {
1359 SAS_DPRINTK("overflow of indexes: dev %016llx "
1360 "phy 0x%x index 0x%x\n",
1361 SAS_ADDR(dev->sas_addr), phy_id, i);
1363 } else if (res != SMP_RESP_FUNC_ACC) {
1364 SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1365 "result 0x%x\n", __func__,
1366 SAS_ADDR(dev->sas_addr), phy_id, i, res);
1369 if (SAS_ADDR(sas_addr) != 0) {
1370 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1372 if ((rri_resp[12] & 0x80) == 0x80)
1377 } else if (SAS_ADDR(rri_resp+16) == 0) {
1382 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1383 phy->last_da_index < i) {
1384 phy->last_da_index = i;
1397 #define CRI_REQ_SIZE 44
1398 #define CRI_RESP_SIZE 8
1400 static int sas_configure_set(struct domain_device *dev, int phy_id,
1401 u8 *sas_addr, int index, int include)
1407 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1411 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1417 cri_req[1] = SMP_CONF_ROUTE_INFO;
1418 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1419 cri_req[9] = phy_id;
1420 if (SAS_ADDR(sas_addr) == 0 || !include)
1421 cri_req[12] |= 0x80;
1422 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1424 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1429 if (res == SMP_RESP_NO_INDEX) {
1430 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1432 SAS_ADDR(dev->sas_addr), phy_id, index);
1440 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1441 u8 *sas_addr, int include)
1447 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1450 if (include ^ present)
1451 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1457 * sas_configure_parent -- configure routing table of parent
1458 * parent: parent expander
1459 * child: child expander
1460 * sas_addr: SAS port identifier of device directly attached to child
1462 static int sas_configure_parent(struct domain_device *parent,
1463 struct domain_device *child,
1464 u8 *sas_addr, int include)
1466 struct expander_device *ex_parent = &parent->ex_dev;
1470 if (parent->parent) {
1471 res = sas_configure_parent(parent->parent, parent, sas_addr,
1477 if (ex_parent->conf_route_table == 0) {
1478 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1479 SAS_ADDR(parent->sas_addr));
1483 for (i = 0; i < ex_parent->num_phys; i++) {
1484 struct ex_phy *phy = &ex_parent->ex_phy[i];
1486 if ((phy->routing_attr == TABLE_ROUTING) &&
1487 (SAS_ADDR(phy->attached_sas_addr) ==
1488 SAS_ADDR(child->sas_addr))) {
1489 res = sas_configure_phy(parent, i, sas_addr, include);
1499 * sas_configure_routing -- configure routing
1500 * dev: expander device
1501 * sas_addr: port identifier of device directly attached to the expander device
1503 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1506 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1510 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1513 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1518 * sas_discover_expander -- expander discovery
1519 * @ex: pointer to expander domain device
1521 * See comment in sas_discover_sata().
1523 static int sas_discover_expander(struct domain_device *dev)
1527 res = sas_notify_lldd_dev_found(dev);
1531 res = sas_ex_general(dev);
1534 res = sas_ex_manuf_info(dev);
1538 res = sas_expander_discover(dev);
1540 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1541 SAS_ADDR(dev->sas_addr), res);
1545 sas_check_ex_subtractive_boundary(dev);
1546 res = sas_check_parent_topology(dev);
1551 sas_notify_lldd_dev_gone(dev);
1555 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1558 struct domain_device *dev;
1560 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1561 if (dev->dev_type == EDGE_DEV ||
1562 dev->dev_type == FANOUT_DEV) {
1563 struct sas_expander_device *ex =
1564 rphy_to_expander_device(dev->rphy);
1566 if (level == ex->level)
1567 res = sas_ex_discover_devices(dev, -1);
1569 res = sas_ex_discover_devices(port->port_dev, -1);
1577 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1583 level = port->disc.max_level;
1584 res = sas_ex_level_discovery(port, level);
1586 } while (level < port->disc.max_level);
1591 int sas_discover_root_expander(struct domain_device *dev)
1594 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1596 res = sas_rphy_add(dev->rphy);
1600 ex->level = dev->port->disc.max_level; /* 0 */
1601 res = sas_discover_expander(dev);
1605 sas_ex_bfs_disc(dev->port);
1610 sas_rphy_remove(dev->rphy);
1615 /* ---------- Domain revalidation ---------- */
1617 static int sas_get_phy_discover(struct domain_device *dev,
1618 int phy_id, struct smp_resp *disc_resp)
1623 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1627 disc_req[1] = SMP_DISCOVER;
1628 disc_req[9] = phy_id;
1630 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1631 disc_resp, DISCOVER_RESP_SIZE);
1634 else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1635 res = disc_resp->result;
1643 static int sas_get_phy_change_count(struct domain_device *dev,
1644 int phy_id, int *pcc)
1647 struct smp_resp *disc_resp;
1649 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1653 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1655 *pcc = disc_resp->disc.change_count;
1661 int sas_get_phy_attached_sas_addr(struct domain_device *dev, int phy_id,
1662 u8 *attached_sas_addr)
1665 struct smp_resp *disc_resp;
1666 struct discover_resp *dr;
1668 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1671 dr = &disc_resp->disc;
1673 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1675 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1676 if (dr->attached_dev_type == 0)
1677 memset(attached_sas_addr, 0, 8);
1683 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1684 int from_phy, bool update)
1686 struct expander_device *ex = &dev->ex_dev;
1690 for (i = from_phy; i < ex->num_phys; i++) {
1691 int phy_change_count = 0;
1693 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1696 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1698 ex->ex_phy[i].phy_change_count =
1708 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1712 struct smp_resp *rg_resp;
1714 rg_req = alloc_smp_req(RG_REQ_SIZE);
1718 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1724 rg_req[1] = SMP_REPORT_GENERAL;
1726 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1730 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1731 res = rg_resp->result;
1735 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1742 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1743 * @dev:domain device to be detect.
1744 * @src_dev: the device which originated BROADCAST(CHANGE).
1746 * Add self-configuration expander suport. Suppose two expander cascading,
1747 * when the first level expander is self-configuring, hotplug the disks in
1748 * second level expander, BROADCAST(CHANGE) will not only be originated
1749 * in the second level expander, but also be originated in the first level
1750 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1751 * expander changed count in two level expanders will all increment at least
1752 * once, but the phy which chang count has changed is the source device which
1756 static int sas_find_bcast_dev(struct domain_device *dev,
1757 struct domain_device **src_dev)
1759 struct expander_device *ex = &dev->ex_dev;
1760 int ex_change_count = -1;
1763 struct domain_device *ch;
1765 res = sas_get_ex_change_count(dev, &ex_change_count);
1768 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1769 /* Just detect if this expander phys phy change count changed,
1770 * in order to determine if this expander originate BROADCAST,
1771 * and do not update phy change count field in our structure.
1773 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1776 ex->ex_change_count = ex_change_count;
1777 SAS_DPRINTK("Expander phy change count has changed\n");
1780 SAS_DPRINTK("Expander phys DID NOT change\n");
1782 list_for_each_entry(ch, &ex->children, siblings) {
1783 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1784 res = sas_find_bcast_dev(ch, src_dev);
1793 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1795 struct expander_device *ex = &dev->ex_dev;
1796 struct domain_device *child, *n;
1798 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1799 set_bit(SAS_DEV_GONE, &child->state);
1800 if (child->dev_type == EDGE_DEV ||
1801 child->dev_type == FANOUT_DEV)
1802 sas_unregister_ex_tree(port, child);
1804 sas_unregister_dev(port, child);
1806 sas_unregister_dev(port, dev);
1809 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1810 int phy_id, bool last)
1812 struct expander_device *ex_dev = &parent->ex_dev;
1813 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1814 struct domain_device *child, *n, *found = NULL;
1816 list_for_each_entry_safe(child, n,
1817 &ex_dev->children, siblings) {
1818 if (SAS_ADDR(child->sas_addr) ==
1819 SAS_ADDR(phy->attached_sas_addr)) {
1820 set_bit(SAS_DEV_GONE, &child->state);
1821 if (child->dev_type == EDGE_DEV ||
1822 child->dev_type == FANOUT_DEV)
1823 sas_unregister_ex_tree(parent->port, child);
1825 sas_unregister_dev(parent->port, child);
1830 sas_disable_routing(parent, phy->attached_sas_addr);
1832 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1834 sas_port_delete_phy(phy->port, phy->phy);
1835 sas_device_set_phy(found, phy->port);
1836 if (phy->port->num_phys == 0)
1837 sas_port_delete(phy->port);
1842 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1845 struct expander_device *ex_root = &root->ex_dev;
1846 struct domain_device *child;
1849 list_for_each_entry(child, &ex_root->children, siblings) {
1850 if (child->dev_type == EDGE_DEV ||
1851 child->dev_type == FANOUT_DEV) {
1852 struct sas_expander_device *ex =
1853 rphy_to_expander_device(child->rphy);
1855 if (level > ex->level)
1856 res = sas_discover_bfs_by_root_level(child,
1858 else if (level == ex->level)
1859 res = sas_ex_discover_devices(child, -1);
1865 static int sas_discover_bfs_by_root(struct domain_device *dev)
1868 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1869 int level = ex->level+1;
1871 res = sas_ex_discover_devices(dev, -1);
1875 res = sas_discover_bfs_by_root_level(dev, level);
1878 } while (level <= dev->port->disc.max_level);
1883 static int sas_discover_new(struct domain_device *dev, int phy_id)
1885 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1886 struct domain_device *child;
1890 SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1891 SAS_ADDR(dev->sas_addr), phy_id);
1892 res = sas_ex_phy_discover(dev, phy_id);
1895 /* to support the wide port inserted */
1896 for (i = 0; i < dev->ex_dev.num_phys; i++) {
1897 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1900 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1901 SAS_ADDR(ex_phy->attached_sas_addr)) {
1907 sas_ex_join_wide_port(dev, phy_id);
1910 res = sas_ex_discover_devices(dev, phy_id);
1913 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1914 if (SAS_ADDR(child->sas_addr) ==
1915 SAS_ADDR(ex_phy->attached_sas_addr)) {
1916 if (child->dev_type == EDGE_DEV ||
1917 child->dev_type == FANOUT_DEV)
1918 res = sas_discover_bfs_by_root(child);
1926 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1928 struct expander_device *ex = &dev->ex_dev;
1929 struct ex_phy *phy = &ex->ex_phy[phy_id];
1930 u8 attached_sas_addr[8];
1933 res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1935 case SMP_RESP_NO_PHY:
1936 phy->phy_state = PHY_NOT_PRESENT;
1937 sas_unregister_devs_sas_addr(dev, phy_id, last);
1939 case SMP_RESP_PHY_VACANT:
1940 phy->phy_state = PHY_VACANT;
1941 sas_unregister_devs_sas_addr(dev, phy_id, last);
1943 case SMP_RESP_FUNC_ACC:
1947 if (SAS_ADDR(attached_sas_addr) == 0) {
1948 phy->phy_state = PHY_EMPTY;
1949 sas_unregister_devs_sas_addr(dev, phy_id, last);
1950 } else if (SAS_ADDR(attached_sas_addr) ==
1951 SAS_ADDR(phy->attached_sas_addr)) {
1952 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1953 SAS_ADDR(dev->sas_addr), phy_id);
1954 sas_ex_phy_discover(dev, phy_id);
1956 res = sas_discover_new(dev, phy_id);
1962 * sas_rediscover - revalidate the domain.
1963 * @dev:domain device to be detect.
1964 * @phy_id: the phy id will be detected.
1966 * NOTE: this process _must_ quit (return) as soon as any connection
1967 * errors are encountered. Connection recovery is done elsewhere.
1968 * Discover process only interrogates devices in order to discover the
1969 * domain.For plugging out, we un-register the device only when it is
1970 * the last phy in the port, for other phys in this port, we just delete it
1971 * from the port.For inserting, we do discovery when it is the
1972 * first phy,for other phys in this port, we add it to the port to
1973 * forming the wide-port.
1975 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1977 struct expander_device *ex = &dev->ex_dev;
1978 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1981 bool last = true; /* is this the last phy of the port */
1983 SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1984 SAS_ADDR(dev->sas_addr), phy_id);
1986 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1987 for (i = 0; i < ex->num_phys; i++) {
1988 struct ex_phy *phy = &ex->ex_phy[i];
1992 if (SAS_ADDR(phy->attached_sas_addr) ==
1993 SAS_ADDR(changed_phy->attached_sas_addr)) {
1994 SAS_DPRINTK("phy%d part of wide port with "
1995 "phy%d\n", phy_id, i);
2000 res = sas_rediscover_dev(dev, phy_id, last);
2002 res = sas_discover_new(dev, phy_id);
2007 * sas_revalidate_domain -- revalidate the domain
2008 * @port: port to the domain of interest
2010 * NOTE: this process _must_ quit (return) as soon as any connection
2011 * errors are encountered. Connection recovery is done elsewhere.
2012 * Discover process only interrogates devices in order to discover the
2015 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2018 struct domain_device *dev = NULL;
2020 res = sas_find_bcast_dev(port_dev, &dev);
2024 struct expander_device *ex = &dev->ex_dev;
2029 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2032 res = sas_rediscover(dev, phy_id);
2034 } while (i < ex->num_phys);
2040 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
2041 struct request *req)
2043 struct domain_device *dev;
2045 struct request *rsp = req->next_rq;
2048 printk("%s: space for a smp response is missing\n",
2053 /* no rphy means no smp target support (ie aic94xx host) */
2055 return sas_smp_host_handler(shost, req, rsp);
2057 type = rphy->identify.device_type;
2059 if (type != SAS_EDGE_EXPANDER_DEVICE &&
2060 type != SAS_FANOUT_EXPANDER_DEVICE) {
2061 printk("%s: can we send a smp request to a device?\n",
2066 dev = sas_find_dev_by_rphy(rphy);
2068 printk("%s: fail to find a domain_device?\n", __func__);
2072 /* do we need to support multiple segments? */
2073 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2074 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2075 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2076 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2080 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2081 bio_data(rsp->bio), blk_rq_bytes(rsp));
2083 /* positive number is the untransferred residual */
2084 rsp->resid_len = ret;
2087 } else if (ret == 0) {