1 // SPDX-License-Identifier: GPL-2.0
3 * Serial Attached SCSI (SAS) Expander discovery and configuration
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
8 * This file is licensed under GPLv2.
11 #include <linux/scatterlist.h>
12 #include <linux/blkdev.h>
13 #include <linux/slab.h>
14 #include <linux/unaligned.h>
16 #include "sas_internal.h"
18 #include <scsi/sas_ata.h>
19 #include <scsi/scsi_transport.h>
20 #include <scsi/scsi_transport_sas.h>
21 #include "scsi_sas_internal.h"
23 static int sas_discover_expander(struct domain_device *dev);
24 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
25 static int sas_configure_phy(struct domain_device *dev, int phy_id,
26 u8 *sas_addr, int include);
27 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr);
29 static void sas_port_add_ex_phy(struct sas_port *port, struct ex_phy *ex_phy)
31 sas_port_add_phy(port, ex_phy->phy);
33 ex_phy->phy_state = PHY_DEVICE_DISCOVERED;
36 static void sas_ex_add_parent_port(struct domain_device *dev, int phy_id)
38 struct expander_device *ex = &dev->ex_dev;
39 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
41 if (!ex->parent_port) {
42 ex->parent_port = sas_port_alloc(&dev->rphy->dev, phy_id);
43 /* FIXME: error handling */
44 BUG_ON(!ex->parent_port);
45 BUG_ON(sas_port_add(ex->parent_port));
46 sas_port_mark_backlink(ex->parent_port);
48 sas_port_add_ex_phy(ex->parent_port, ex_phy);
51 /* ---------- SMP task management ---------- */
53 /* Give it some long enough timeout. In seconds. */
54 #define SMP_TIMEOUT 10
56 static int smp_execute_task_sg(struct domain_device *dev,
57 struct scatterlist *req, struct scatterlist *resp)
60 struct sas_task *task = NULL;
61 struct sas_internal *i =
62 to_sas_internal(dev->port->ha->shost->transportt);
63 struct sas_ha_struct *ha = dev->port->ha;
65 pm_runtime_get_sync(ha->dev);
66 mutex_lock(&dev->ex_dev.cmd_mutex);
67 for (retry = 0; retry < 3; retry++) {
68 if (test_bit(SAS_DEV_GONE, &dev->state)) {
73 task = sas_alloc_slow_task(GFP_KERNEL);
79 task->task_proto = dev->tproto;
80 task->smp_task.smp_req = *req;
81 task->smp_task.smp_resp = *resp;
83 task->task_done = sas_task_internal_done;
85 task->slow_task->timer.function = sas_task_internal_timedout;
86 task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
87 add_timer(&task->slow_task->timer);
89 res = i->dft->lldd_execute_task(task, GFP_KERNEL);
92 del_timer_sync(&task->slow_task->timer);
93 pr_notice("executing SMP task failed:%d\n", res);
97 wait_for_completion(&task->slow_task->completion);
99 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
100 pr_notice("smp task timed out or aborted\n");
101 i->dft->lldd_abort_task(task);
102 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
103 pr_notice("SMP task aborted and not done\n");
107 if (task->task_status.resp == SAS_TASK_COMPLETE &&
108 task->task_status.stat == SAS_SAM_STAT_GOOD) {
112 if (task->task_status.resp == SAS_TASK_COMPLETE &&
113 task->task_status.stat == SAS_DATA_UNDERRUN) {
114 /* no error, but return the number of bytes of
116 res = task->task_status.residual;
119 if (task->task_status.resp == SAS_TASK_COMPLETE &&
120 task->task_status.stat == SAS_DATA_OVERRUN) {
124 if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
125 task->task_status.stat == SAS_DEVICE_UNKNOWN)
128 pr_notice("%s: task to dev %016llx response: 0x%x status 0x%x\n",
130 SAS_ADDR(dev->sas_addr),
131 task->task_status.resp,
132 task->task_status.stat);
137 mutex_unlock(&dev->ex_dev.cmd_mutex);
138 pm_runtime_put_sync(ha->dev);
140 BUG_ON(retry == 3 && task != NULL);
145 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
146 void *resp, int resp_size)
148 struct scatterlist req_sg;
149 struct scatterlist resp_sg;
151 sg_init_one(&req_sg, req, req_size);
152 sg_init_one(&resp_sg, resp, resp_size);
153 return smp_execute_task_sg(dev, &req_sg, &resp_sg);
156 /* ---------- Allocations ---------- */
158 static inline void *alloc_smp_req(int size)
160 u8 *p = kzalloc(ALIGN(size, ARCH_DMA_MINALIGN), GFP_KERNEL);
166 static inline void *alloc_smp_resp(int size)
168 return kzalloc(size, GFP_KERNEL);
171 static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
173 switch (phy->routing_attr) {
175 if (dev->ex_dev.t2t_supp)
181 case SUBTRACTIVE_ROUTING:
188 static enum sas_device_type to_dev_type(struct discover_resp *dr)
190 /* This is detecting a failure to transmit initial dev to host
191 * FIS as described in section J.5 of sas-2 r16
193 if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev &&
194 dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
195 return SAS_SATA_PENDING;
197 return dr->attached_dev_type;
200 static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
201 struct smp_disc_resp *disc_resp)
203 enum sas_device_type dev_type;
204 enum sas_linkrate linkrate;
205 u8 sas_addr[SAS_ADDR_SIZE];
206 struct discover_resp *dr = &disc_resp->disc;
207 struct sas_ha_struct *ha = dev->port->ha;
208 struct expander_device *ex = &dev->ex_dev;
209 struct ex_phy *phy = &ex->ex_phy[phy_id];
210 struct sas_rphy *rphy = dev->rphy;
211 bool new_phy = !phy->phy;
215 if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
217 phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
219 /* FIXME: error_handling */
223 switch (disc_resp->result) {
224 case SMP_RESP_PHY_VACANT:
225 phy->phy_state = PHY_VACANT;
228 phy->phy_state = PHY_NOT_PRESENT;
230 case SMP_RESP_FUNC_ACC:
231 phy->phy_state = PHY_EMPTY; /* do not know yet */
235 /* check if anything important changed to squelch debug */
236 dev_type = phy->attached_dev_type;
237 linkrate = phy->linkrate;
238 memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
240 /* Handle vacant phy - rest of dr data is not valid so skip it */
241 if (phy->phy_state == PHY_VACANT) {
242 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
243 phy->attached_dev_type = SAS_PHY_UNUSED;
244 if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
245 phy->phy_id = phy_id;
251 phy->attached_dev_type = to_dev_type(dr);
252 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
254 phy->phy_id = phy_id;
255 phy->linkrate = dr->linkrate;
256 phy->attached_sata_host = dr->attached_sata_host;
257 phy->attached_sata_dev = dr->attached_sata_dev;
258 phy->attached_sata_ps = dr->attached_sata_ps;
259 phy->attached_iproto = dr->iproto << 1;
260 phy->attached_tproto = dr->tproto << 1;
261 /* help some expanders that fail to zero sas_address in the 'no
264 if (phy->attached_dev_type == SAS_PHY_UNUSED)
265 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
267 memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
268 phy->attached_phy_id = dr->attached_phy_id;
269 phy->phy_change_count = dr->change_count;
270 phy->routing_attr = dr->routing_attr;
271 phy->virtual = dr->virtual;
272 phy->last_da_index = -1;
274 phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
275 phy->phy->identify.device_type = dr->attached_dev_type;
276 phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
277 phy->phy->identify.target_port_protocols = phy->attached_tproto;
278 if (!phy->attached_tproto && dr->attached_sata_dev)
279 phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
280 phy->phy->identify.phy_identifier = phy_id;
281 phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
282 phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
283 phy->phy->minimum_linkrate = dr->pmin_linkrate;
284 phy->phy->maximum_linkrate = dr->pmax_linkrate;
285 phy->phy->negotiated_linkrate = phy->linkrate;
286 phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
290 if (sas_phy_add(phy->phy)) {
291 sas_phy_free(phy->phy);
296 switch (phy->attached_dev_type) {
297 case SAS_SATA_PENDING:
298 type = "stp pending";
304 if (phy->attached_iproto) {
305 if (phy->attached_tproto)
306 type = "host+target";
310 if (dr->attached_sata_dev)
316 case SAS_EDGE_EXPANDER_DEVICE:
317 case SAS_FANOUT_EXPANDER_DEVICE:
324 /* this routine is polled by libata error recovery so filter
325 * unimportant messages
327 if (new_phy || phy->attached_dev_type != dev_type ||
328 phy->linkrate != linkrate ||
329 SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
334 /* if the attached device type changed and ata_eh is active,
335 * make sure we run revalidation when eh completes (see:
336 * sas_enable_revalidation)
338 if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
339 set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);
341 pr_debug("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
342 test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
343 SAS_ADDR(dev->sas_addr), phy->phy_id,
344 sas_route_char(dev, phy), phy->linkrate,
345 SAS_ADDR(phy->attached_sas_addr), type);
348 /* check if we have an existing attached ata device on this expander phy */
349 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
351 struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
352 struct domain_device *dev;
353 struct sas_rphy *rphy;
358 rphy = ex_phy->port->rphy;
362 dev = sas_find_dev_by_rphy(rphy);
364 if (dev && dev_is_sata(dev))
370 #define DISCOVER_REQ_SIZE 16
371 #define DISCOVER_RESP_SIZE sizeof(struct smp_disc_resp)
373 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
374 struct smp_disc_resp *disc_resp,
377 struct discover_resp *dr = &disc_resp->disc;
380 disc_req[9] = single;
382 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
383 disc_resp, DISCOVER_RESP_SIZE);
386 if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
387 pr_notice("Found loopback topology, just ignore it!\n");
390 sas_set_ex_phy(dev, single, disc_resp);
394 int sas_ex_phy_discover(struct domain_device *dev, int single)
396 struct expander_device *ex = &dev->ex_dev;
399 struct smp_disc_resp *disc_resp;
401 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
405 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
411 disc_req[1] = SMP_DISCOVER;
413 if (0 <= single && single < ex->num_phys) {
414 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
418 for (i = 0; i < ex->num_phys; i++) {
419 res = sas_ex_phy_discover_helper(dev, disc_req,
431 static int sas_expander_discover(struct domain_device *dev)
433 struct expander_device *ex = &dev->ex_dev;
436 ex->ex_phy = kcalloc(ex->num_phys, sizeof(*ex->ex_phy), GFP_KERNEL);
440 res = sas_ex_phy_discover(dev, -1);
451 #define MAX_EXPANDER_PHYS 128
453 #define RG_REQ_SIZE 8
454 #define RG_RESP_SIZE sizeof(struct smp_rg_resp)
456 static int sas_ex_general(struct domain_device *dev)
459 struct smp_rg_resp *rg_resp;
460 struct report_general_resp *rg;
464 rg_req = alloc_smp_req(RG_REQ_SIZE);
468 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
474 rg_req[1] = SMP_REPORT_GENERAL;
476 for (i = 0; i < 5; i++) {
477 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
481 pr_notice("RG to ex %016llx failed:0x%x\n",
482 SAS_ADDR(dev->sas_addr), res);
484 } else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
485 pr_debug("RG:ex %016llx returned SMP result:0x%x\n",
486 SAS_ADDR(dev->sas_addr), rg_resp->result);
487 res = rg_resp->result;
492 dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
493 dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
494 dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
495 dev->ex_dev.t2t_supp = rg->t2t_supp;
496 dev->ex_dev.conf_route_table = rg->conf_route_table;
497 dev->ex_dev.configuring = rg->configuring;
498 memcpy(dev->ex_dev.enclosure_logical_id,
499 rg->enclosure_logical_id, 8);
501 if (dev->ex_dev.configuring) {
502 pr_debug("RG: ex %016llx self-configuring...\n",
503 SAS_ADDR(dev->sas_addr));
504 schedule_timeout_interruptible(5*HZ);
514 static void ex_assign_manuf_info(struct domain_device *dev, void
517 u8 *mi_resp = _mi_resp;
518 struct sas_rphy *rphy = dev->rphy;
519 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
521 memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
522 memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
523 memcpy(edev->product_rev, mi_resp + 36,
524 SAS_EXPANDER_PRODUCT_REV_LEN);
526 if (mi_resp[8] & 1) {
527 memcpy(edev->component_vendor_id, mi_resp + 40,
528 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
529 edev->component_id = mi_resp[48] << 8 | mi_resp[49];
530 edev->component_revision_id = mi_resp[50];
534 #define MI_REQ_SIZE 8
535 #define MI_RESP_SIZE 64
537 static int sas_ex_manuf_info(struct domain_device *dev)
543 mi_req = alloc_smp_req(MI_REQ_SIZE);
547 mi_resp = alloc_smp_resp(MI_RESP_SIZE);
553 mi_req[1] = SMP_REPORT_MANUF_INFO;
555 res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp, MI_RESP_SIZE);
557 pr_notice("MI: ex %016llx failed:0x%x\n",
558 SAS_ADDR(dev->sas_addr), res);
560 } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
561 pr_debug("MI ex %016llx returned SMP result:0x%x\n",
562 SAS_ADDR(dev->sas_addr), mi_resp[2]);
566 ex_assign_manuf_info(dev, mi_resp);
573 #define PC_REQ_SIZE 44
574 #define PC_RESP_SIZE 8
576 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
577 enum phy_func phy_func,
578 struct sas_phy_linkrates *rates)
584 pc_req = alloc_smp_req(PC_REQ_SIZE);
588 pc_resp = alloc_smp_resp(PC_RESP_SIZE);
594 pc_req[1] = SMP_PHY_CONTROL;
596 pc_req[10] = phy_func;
598 pc_req[32] = rates->minimum_linkrate << 4;
599 pc_req[33] = rates->maximum_linkrate << 4;
602 res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp, PC_RESP_SIZE);
604 pr_err("ex %016llx phy%02d PHY control failed: %d\n",
605 SAS_ADDR(dev->sas_addr), phy_id, res);
606 } else if (pc_resp[2] != SMP_RESP_FUNC_ACC) {
607 pr_err("ex %016llx phy%02d PHY control failed: function result 0x%x\n",
608 SAS_ADDR(dev->sas_addr), phy_id, pc_resp[2]);
616 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
618 struct expander_device *ex = &dev->ex_dev;
619 struct ex_phy *phy = &ex->ex_phy[phy_id];
621 sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
622 phy->linkrate = SAS_PHY_DISABLED;
625 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
627 struct expander_device *ex = &dev->ex_dev;
630 for (i = 0; i < ex->num_phys; i++) {
631 struct ex_phy *phy = &ex->ex_phy[i];
633 if (phy->phy_state == PHY_VACANT ||
634 phy->phy_state == PHY_NOT_PRESENT)
637 if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
638 sas_ex_disable_phy(dev, i);
642 static int sas_dev_present_in_domain(struct asd_sas_port *port,
645 struct domain_device *dev;
647 if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
649 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
650 if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
656 #define RPEL_REQ_SIZE 16
657 #define RPEL_RESP_SIZE 32
658 int sas_smp_get_phy_events(struct sas_phy *phy)
663 struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
664 struct domain_device *dev = sas_find_dev_by_rphy(rphy);
666 req = alloc_smp_req(RPEL_REQ_SIZE);
670 resp = alloc_smp_resp(RPEL_RESP_SIZE);
676 req[1] = SMP_REPORT_PHY_ERR_LOG;
677 req[9] = phy->number;
679 res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
680 resp, RPEL_RESP_SIZE);
685 phy->invalid_dword_count = get_unaligned_be32(&resp[12]);
686 phy->running_disparity_error_count = get_unaligned_be32(&resp[16]);
687 phy->loss_of_dword_sync_count = get_unaligned_be32(&resp[20]);
688 phy->phy_reset_problem_count = get_unaligned_be32(&resp[24]);
697 #ifdef CONFIG_SCSI_SAS_ATA
699 #define RPS_REQ_SIZE 16
700 #define RPS_RESP_SIZE sizeof(struct smp_rps_resp)
702 int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
703 struct smp_rps_resp *rps_resp)
706 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
707 u8 *resp = (u8 *)rps_resp;
712 rps_req[1] = SMP_REPORT_PHY_SATA;
715 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
716 rps_resp, RPS_RESP_SIZE);
718 /* 0x34 is the FIS type for the D2H fis. There's a potential
719 * standards cockup here. sas-2 explicitly specifies the FIS
720 * should be encoded so that FIS type is in resp[24].
721 * However, some expanders endian reverse this. Undo the
723 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
726 for (i = 0; i < 5; i++) {
731 resp[j + 0] = resp[j + 3];
732 resp[j + 1] = resp[j + 2];
743 static void sas_ex_get_linkrate(struct domain_device *parent,
744 struct domain_device *child,
745 struct ex_phy *parent_phy)
747 struct expander_device *parent_ex = &parent->ex_dev;
748 struct sas_port *port;
753 port = parent_phy->port;
755 for (i = 0; i < parent_ex->num_phys; i++) {
756 struct ex_phy *phy = &parent_ex->ex_phy[i];
758 if (phy->phy_state == PHY_VACANT ||
759 phy->phy_state == PHY_NOT_PRESENT)
762 if (sas_phy_match_dev_addr(child, phy)) {
763 child->min_linkrate = min(parent->min_linkrate,
765 child->max_linkrate = max(parent->max_linkrate,
768 sas_port_add_phy(port, phy->phy);
771 child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
772 child->pathways = min(child->pathways, parent->pathways);
775 static int sas_ex_add_dev(struct domain_device *parent, struct ex_phy *phy,
776 struct domain_device *child, int phy_id)
778 struct sas_rphy *rphy;
781 child->dev_type = SAS_END_DEVICE;
782 rphy = sas_end_device_alloc(phy->port);
786 child->tproto = phy->attached_tproto;
790 get_device(&rphy->dev);
791 rphy->identify.phy_identifier = phy_id;
792 sas_fill_in_rphy(child, rphy);
794 list_add_tail(&child->disco_list_node, &parent->port->disco_list);
796 res = sas_notify_lldd_dev_found(child);
798 pr_notice("notify lldd for device %016llx at %016llx:%02d returned 0x%x\n",
799 SAS_ADDR(child->sas_addr),
800 SAS_ADDR(parent->sas_addr), phy_id, res);
801 sas_rphy_free(child->rphy);
802 list_del(&child->disco_list_node);
809 static struct domain_device *sas_ex_discover_end_dev(
810 struct domain_device *parent, int phy_id)
812 struct expander_device *parent_ex = &parent->ex_dev;
813 struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
814 struct domain_device *child = NULL;
817 if (phy->attached_sata_host || phy->attached_sata_ps)
820 child = sas_alloc_device();
824 kref_get(&parent->kref);
825 child->parent = parent;
826 child->port = parent->port;
827 child->iproto = phy->attached_iproto;
828 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
829 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
831 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
832 if (unlikely(!phy->port))
834 if (unlikely(sas_port_add(phy->port) != 0)) {
835 sas_port_free(phy->port);
839 sas_ex_get_linkrate(parent, child, phy);
840 sas_device_set_phy(child, phy->port);
842 if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
843 res = sas_ata_add_dev(parent, phy, child, phy_id);
844 } else if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
845 res = sas_ex_add_dev(parent, phy, child, phy_id);
847 pr_notice("target proto 0x%x at %016llx:0x%x not handled\n",
848 phy->attached_tproto, SAS_ADDR(parent->sas_addr),
856 list_add_tail(&child->siblings, &parent_ex->children);
860 sas_port_delete(phy->port);
863 sas_put_device(child);
867 /* See if this phy is part of a wide port */
868 static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
870 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
873 for (i = 0; i < parent->ex_dev.num_phys; i++) {
874 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
879 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
880 SAS_ADDR_SIZE) && ephy->port) {
881 sas_port_add_ex_phy(ephy->port, phy);
889 static struct domain_device *sas_ex_discover_expander(
890 struct domain_device *parent, int phy_id)
892 struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
893 struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
894 struct domain_device *child = NULL;
895 struct sas_rphy *rphy;
896 struct sas_expander_device *edev;
897 struct asd_sas_port *port;
900 if (phy->routing_attr == DIRECT_ROUTING) {
901 pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
902 SAS_ADDR(parent->sas_addr), phy_id,
903 SAS_ADDR(phy->attached_sas_addr),
904 phy->attached_phy_id);
907 child = sas_alloc_device();
911 phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
912 /* FIXME: better error handling */
913 BUG_ON(sas_port_add(phy->port) != 0);
916 switch (phy->attached_dev_type) {
917 case SAS_EDGE_EXPANDER_DEVICE:
918 rphy = sas_expander_alloc(phy->port,
919 SAS_EDGE_EXPANDER_DEVICE);
921 case SAS_FANOUT_EXPANDER_DEVICE:
922 rphy = sas_expander_alloc(phy->port,
923 SAS_FANOUT_EXPANDER_DEVICE);
926 rphy = NULL; /* shut gcc up */
931 get_device(&rphy->dev);
932 edev = rphy_to_expander_device(rphy);
933 child->dev_type = phy->attached_dev_type;
934 kref_get(&parent->kref);
935 child->parent = parent;
937 child->iproto = phy->attached_iproto;
938 child->tproto = phy->attached_tproto;
939 memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
940 sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
941 sas_ex_get_linkrate(parent, child, phy);
942 edev->level = parent_ex->level + 1;
943 parent->port->disc.max_level = max(parent->port->disc.max_level,
946 sas_fill_in_rphy(child, rphy);
949 spin_lock_irq(&parent->port->dev_list_lock);
950 list_add_tail(&child->dev_list_node, &parent->port->dev_list);
951 spin_unlock_irq(&parent->port->dev_list_lock);
953 res = sas_discover_expander(child);
955 sas_rphy_delete(rphy);
956 spin_lock_irq(&parent->port->dev_list_lock);
957 list_del(&child->dev_list_node);
958 spin_unlock_irq(&parent->port->dev_list_lock);
959 sas_put_device(child);
960 sas_port_delete(phy->port);
964 list_add_tail(&child->siblings, &parent->ex_dev.children);
968 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
970 struct expander_device *ex = &dev->ex_dev;
971 struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
972 struct domain_device *child = NULL;
976 if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
977 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
978 res = sas_ex_phy_discover(dev, phy_id);
983 /* Parent and domain coherency */
984 if (!dev->parent && sas_phy_match_port_addr(dev->port, ex_phy)) {
985 sas_ex_add_parent_port(dev, phy_id);
988 if (dev->parent && sas_phy_match_dev_addr(dev->parent, ex_phy)) {
989 sas_ex_add_parent_port(dev, phy_id);
990 if (ex_phy->routing_attr == TABLE_ROUTING)
991 sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
995 if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
996 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
998 if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) {
999 if (ex_phy->routing_attr == DIRECT_ROUTING) {
1000 memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1001 sas_configure_routing(dev, ex_phy->attached_sas_addr);
1004 } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
1007 if (ex_phy->attached_dev_type != SAS_END_DEVICE &&
1008 ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE &&
1009 ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE &&
1010 ex_phy->attached_dev_type != SAS_SATA_PENDING) {
1011 pr_warn("unknown device type(0x%x) attached to ex %016llx phy%02d\n",
1012 ex_phy->attached_dev_type,
1013 SAS_ADDR(dev->sas_addr),
1018 res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1020 pr_notice("configure routing for dev %016llx reported 0x%x. Forgotten\n",
1021 SAS_ADDR(ex_phy->attached_sas_addr), res);
1022 sas_disable_routing(dev, ex_phy->attached_sas_addr);
1026 if (sas_ex_join_wide_port(dev, phy_id)) {
1027 pr_debug("Attaching ex phy%02d to wide port %016llx\n",
1028 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1032 switch (ex_phy->attached_dev_type) {
1033 case SAS_END_DEVICE:
1034 case SAS_SATA_PENDING:
1035 child = sas_ex_discover_end_dev(dev, phy_id);
1037 case SAS_FANOUT_EXPANDER_DEVICE:
1038 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1039 pr_debug("second fanout expander %016llx phy%02d attached to ex %016llx phy%02d\n",
1040 SAS_ADDR(ex_phy->attached_sas_addr),
1041 ex_phy->attached_phy_id,
1042 SAS_ADDR(dev->sas_addr),
1044 sas_ex_disable_phy(dev, phy_id);
1047 memcpy(dev->port->disc.fanout_sas_addr,
1048 ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1050 case SAS_EDGE_EXPANDER_DEVICE:
1051 child = sas_ex_discover_expander(dev, phy_id);
1058 pr_notice("ex %016llx phy%02d failed to discover\n",
1059 SAS_ADDR(dev->sas_addr), phy_id);
1063 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1065 struct expander_device *ex = &dev->ex_dev;
1068 for (i = 0; i < ex->num_phys; i++) {
1069 struct ex_phy *phy = &ex->ex_phy[i];
1071 if (phy->phy_state == PHY_VACANT ||
1072 phy->phy_state == PHY_NOT_PRESENT)
1075 if (dev_is_expander(phy->attached_dev_type) &&
1076 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1078 memcpy(sub_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
1086 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1088 struct expander_device *ex = &dev->ex_dev;
1089 struct domain_device *child;
1090 u8 sub_addr[SAS_ADDR_SIZE] = {0, };
1092 list_for_each_entry(child, &ex->children, siblings) {
1093 if (!dev_is_expander(child->dev_type))
1095 if (sub_addr[0] == 0) {
1096 sas_find_sub_addr(child, sub_addr);
1099 u8 s2[SAS_ADDR_SIZE];
1101 if (sas_find_sub_addr(child, s2) &&
1102 (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1104 pr_notice("ex %016llx->%016llx-?->%016llx diverges from subtractive boundary %016llx\n",
1105 SAS_ADDR(dev->sas_addr),
1106 SAS_ADDR(child->sas_addr),
1108 SAS_ADDR(sub_addr));
1110 sas_ex_disable_port(child, s2);
1117 * sas_ex_discover_devices - discover devices attached to this expander
1118 * @dev: pointer to the expander domain device
1119 * @single: if you want to do a single phy, else set to -1;
1121 * Configure this expander for use with its devices and register the
1122 * devices of this expander.
1124 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1126 struct expander_device *ex = &dev->ex_dev;
1127 int i = 0, end = ex->num_phys;
1130 if (0 <= single && single < end) {
1135 for ( ; i < end; i++) {
1136 struct ex_phy *ex_phy = &ex->ex_phy[i];
1138 if (ex_phy->phy_state == PHY_VACANT ||
1139 ex_phy->phy_state == PHY_NOT_PRESENT ||
1140 ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1143 switch (ex_phy->linkrate) {
1144 case SAS_PHY_DISABLED:
1145 case SAS_PHY_RESET_PROBLEM:
1146 case SAS_SATA_PORT_SELECTOR:
1149 res = sas_ex_discover_dev(dev, i);
1157 sas_check_level_subtractive_boundary(dev);
1162 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1164 struct expander_device *ex = &dev->ex_dev;
1166 u8 *sub_sas_addr = NULL;
1168 if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE)
1171 for (i = 0; i < ex->num_phys; i++) {
1172 struct ex_phy *phy = &ex->ex_phy[i];
1174 if (phy->phy_state == PHY_VACANT ||
1175 phy->phy_state == PHY_NOT_PRESENT)
1178 if (dev_is_expander(phy->attached_dev_type) &&
1179 phy->routing_attr == SUBTRACTIVE_ROUTING) {
1182 sub_sas_addr = &phy->attached_sas_addr[0];
1183 else if (SAS_ADDR(sub_sas_addr) !=
1184 SAS_ADDR(phy->attached_sas_addr)) {
1186 pr_notice("ex %016llx phy%02d diverges(%016llx) on subtractive boundary(%016llx). Disabled\n",
1187 SAS_ADDR(dev->sas_addr), i,
1188 SAS_ADDR(phy->attached_sas_addr),
1189 SAS_ADDR(sub_sas_addr));
1190 sas_ex_disable_phy(dev, i);
1197 static void sas_print_parent_topology_bug(struct domain_device *child,
1198 struct ex_phy *parent_phy,
1199 struct ex_phy *child_phy)
1201 static const char *ex_type[] = {
1202 [SAS_EDGE_EXPANDER_DEVICE] = "edge",
1203 [SAS_FANOUT_EXPANDER_DEVICE] = "fanout",
1205 struct domain_device *parent = child->parent;
1207 pr_notice("%s ex %016llx phy%02d <--> %s ex %016llx phy%02d has %c:%c routing link!\n",
1208 ex_type[parent->dev_type],
1209 SAS_ADDR(parent->sas_addr),
1212 ex_type[child->dev_type],
1213 SAS_ADDR(child->sas_addr),
1216 sas_route_char(parent, parent_phy),
1217 sas_route_char(child, child_phy));
1220 static bool sas_eeds_valid(struct domain_device *parent,
1221 struct domain_device *child)
1223 struct sas_discovery *disc = &parent->port->disc;
1225 return (SAS_ADDR(disc->eeds_a) == SAS_ADDR(parent->sas_addr) ||
1226 SAS_ADDR(disc->eeds_a) == SAS_ADDR(child->sas_addr)) &&
1227 (SAS_ADDR(disc->eeds_b) == SAS_ADDR(parent->sas_addr) ||
1228 SAS_ADDR(disc->eeds_b) == SAS_ADDR(child->sas_addr));
1231 static int sas_check_eeds(struct domain_device *child,
1232 struct ex_phy *parent_phy,
1233 struct ex_phy *child_phy)
1236 struct domain_device *parent = child->parent;
1237 struct sas_discovery *disc = &parent->port->disc;
1239 if (SAS_ADDR(disc->fanout_sas_addr) != 0) {
1241 pr_warn("edge ex %016llx phy S:%02d <--> edge ex %016llx phy S:%02d, while there is a fanout ex %016llx\n",
1242 SAS_ADDR(parent->sas_addr),
1244 SAS_ADDR(child->sas_addr),
1246 SAS_ADDR(disc->fanout_sas_addr));
1247 } else if (SAS_ADDR(disc->eeds_a) == 0) {
1248 memcpy(disc->eeds_a, parent->sas_addr, SAS_ADDR_SIZE);
1249 memcpy(disc->eeds_b, child->sas_addr, SAS_ADDR_SIZE);
1250 } else if (!sas_eeds_valid(parent, child)) {
1252 pr_warn("edge ex %016llx phy%02d <--> edge ex %016llx phy%02d link forms a third EEDS!\n",
1253 SAS_ADDR(parent->sas_addr),
1255 SAS_ADDR(child->sas_addr),
1262 static int sas_check_edge_expander_topo(struct domain_device *child,
1263 struct ex_phy *parent_phy)
1265 struct expander_device *child_ex = &child->ex_dev;
1266 struct expander_device *parent_ex = &child->parent->ex_dev;
1267 struct ex_phy *child_phy;
1269 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1271 if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
1272 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1273 child_phy->routing_attr != TABLE_ROUTING)
1275 } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1276 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING)
1277 return sas_check_eeds(child, parent_phy, child_phy);
1278 else if (child_phy->routing_attr != TABLE_ROUTING)
1280 } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1281 if (child_phy->routing_attr != SUBTRACTIVE_ROUTING &&
1282 (child_phy->routing_attr != TABLE_ROUTING ||
1283 !child_ex->t2t_supp || !parent_ex->t2t_supp))
1289 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1293 static int sas_check_fanout_expander_topo(struct domain_device *child,
1294 struct ex_phy *parent_phy)
1296 struct expander_device *child_ex = &child->ex_dev;
1297 struct ex_phy *child_phy;
1299 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1301 if (parent_phy->routing_attr == TABLE_ROUTING &&
1302 child_phy->routing_attr == SUBTRACTIVE_ROUTING)
1305 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1310 static int sas_check_parent_topology(struct domain_device *child)
1312 struct expander_device *parent_ex;
1319 if (!dev_is_expander(child->parent->dev_type))
1322 parent_ex = &child->parent->ex_dev;
1324 for (i = 0; i < parent_ex->num_phys; i++) {
1325 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1327 if (parent_phy->phy_state == PHY_VACANT ||
1328 parent_phy->phy_state == PHY_NOT_PRESENT)
1331 if (!sas_phy_match_dev_addr(child, parent_phy))
1334 switch (child->parent->dev_type) {
1335 case SAS_EDGE_EXPANDER_DEVICE:
1336 if (sas_check_edge_expander_topo(child, parent_phy))
1339 case SAS_FANOUT_EXPANDER_DEVICE:
1340 if (sas_check_fanout_expander_topo(child, parent_phy))
1351 #define RRI_REQ_SIZE 16
1352 #define RRI_RESP_SIZE 44
1354 static int sas_configure_present(struct domain_device *dev, int phy_id,
1355 u8 *sas_addr, int *index, int *present)
1358 struct expander_device *ex = &dev->ex_dev;
1359 struct ex_phy *phy = &ex->ex_phy[phy_id];
1366 rri_req = alloc_smp_req(RRI_REQ_SIZE);
1370 rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1376 rri_req[1] = SMP_REPORT_ROUTE_INFO;
1377 rri_req[9] = phy_id;
1379 for (i = 0; i < ex->max_route_indexes ; i++) {
1380 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1381 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1386 if (res == SMP_RESP_NO_INDEX) {
1387 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1388 SAS_ADDR(dev->sas_addr), phy_id, i);
1390 } else if (res != SMP_RESP_FUNC_ACC) {
1391 pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n",
1392 __func__, SAS_ADDR(dev->sas_addr), phy_id,
1396 if (SAS_ADDR(sas_addr) != 0) {
1397 if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1399 if ((rri_resp[12] & 0x80) == 0x80)
1404 } else if (SAS_ADDR(rri_resp+16) == 0) {
1409 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1410 phy->last_da_index < i) {
1411 phy->last_da_index = i;
1424 #define CRI_REQ_SIZE 44
1425 #define CRI_RESP_SIZE 8
1427 static int sas_configure_set(struct domain_device *dev, int phy_id,
1428 u8 *sas_addr, int index, int include)
1434 cri_req = alloc_smp_req(CRI_REQ_SIZE);
1438 cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1444 cri_req[1] = SMP_CONF_ROUTE_INFO;
1445 *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1446 cri_req[9] = phy_id;
1447 if (SAS_ADDR(sas_addr) == 0 || !include)
1448 cri_req[12] |= 0x80;
1449 memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1451 res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1456 if (res == SMP_RESP_NO_INDEX) {
1457 pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1458 SAS_ADDR(dev->sas_addr), phy_id, index);
1466 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1467 u8 *sas_addr, int include)
1473 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1476 if (include ^ present)
1477 return sas_configure_set(dev, phy_id, sas_addr, index,
1484 * sas_configure_parent - configure routing table of parent
1485 * @parent: parent expander
1486 * @child: child expander
1487 * @sas_addr: SAS port identifier of device directly attached to child
1488 * @include: whether or not to include @child in the expander routing table
1490 static int sas_configure_parent(struct domain_device *parent,
1491 struct domain_device *child,
1492 u8 *sas_addr, int include)
1494 struct expander_device *ex_parent = &parent->ex_dev;
1498 if (parent->parent) {
1499 res = sas_configure_parent(parent->parent, parent, sas_addr,
1505 if (ex_parent->conf_route_table == 0) {
1506 pr_debug("ex %016llx has self-configuring routing table\n",
1507 SAS_ADDR(parent->sas_addr));
1511 for (i = 0; i < ex_parent->num_phys; i++) {
1512 struct ex_phy *phy = &ex_parent->ex_phy[i];
1514 if ((phy->routing_attr == TABLE_ROUTING) &&
1515 sas_phy_match_dev_addr(child, phy)) {
1516 res = sas_configure_phy(parent, i, sas_addr, include);
1526 * sas_configure_routing - configure routing
1527 * @dev: expander device
1528 * @sas_addr: port identifier of device directly attached to the expander device
1530 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1533 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1537 static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr)
1540 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1545 * sas_discover_expander - expander discovery
1546 * @dev: pointer to expander domain device
1548 * See comment in sas_discover_sata().
1550 static int sas_discover_expander(struct domain_device *dev)
1554 res = sas_notify_lldd_dev_found(dev);
1558 res = sas_ex_general(dev);
1561 res = sas_ex_manuf_info(dev);
1565 res = sas_expander_discover(dev);
1567 pr_warn("expander %016llx discovery failed(0x%x)\n",
1568 SAS_ADDR(dev->sas_addr), res);
1572 sas_check_ex_subtractive_boundary(dev);
1573 res = sas_check_parent_topology(dev);
1578 sas_notify_lldd_dev_gone(dev);
1582 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1585 struct domain_device *dev;
1587 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1588 if (dev_is_expander(dev->dev_type)) {
1589 struct sas_expander_device *ex =
1590 rphy_to_expander_device(dev->rphy);
1592 if (level == ex->level)
1593 res = sas_ex_discover_devices(dev, -1);
1595 res = sas_ex_discover_devices(port->port_dev, -1);
1603 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1609 level = port->disc.max_level;
1610 res = sas_ex_level_discovery(port, level);
1612 } while (level < port->disc.max_level);
1617 int sas_discover_root_expander(struct domain_device *dev)
1620 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1622 res = sas_rphy_add(dev->rphy);
1626 ex->level = dev->port->disc.max_level; /* 0 */
1627 res = sas_discover_expander(dev);
1631 sas_ex_bfs_disc(dev->port);
1636 sas_rphy_remove(dev->rphy);
1641 /* ---------- Domain revalidation ---------- */
1643 static void sas_get_sas_addr_and_dev_type(struct smp_disc_resp *disc_resp,
1645 enum sas_device_type *type)
1647 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, SAS_ADDR_SIZE);
1648 *type = to_dev_type(&disc_resp->disc);
1649 if (*type == SAS_PHY_UNUSED)
1650 memset(sas_addr, 0, SAS_ADDR_SIZE);
1653 static int sas_get_phy_discover(struct domain_device *dev,
1654 int phy_id, struct smp_disc_resp *disc_resp)
1659 disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1663 disc_req[1] = SMP_DISCOVER;
1664 disc_req[9] = phy_id;
1666 res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1667 disc_resp, DISCOVER_RESP_SIZE);
1670 if (disc_resp->result != SMP_RESP_FUNC_ACC)
1671 res = disc_resp->result;
1677 static int sas_get_phy_change_count(struct domain_device *dev,
1678 int phy_id, int *pcc)
1681 struct smp_disc_resp *disc_resp;
1683 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1687 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1689 *pcc = disc_resp->disc.change_count;
1695 int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1696 u8 *sas_addr, enum sas_device_type *type)
1699 struct smp_disc_resp *disc_resp;
1701 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1705 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1707 sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, type);
1712 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1713 int from_phy, bool update)
1715 struct expander_device *ex = &dev->ex_dev;
1719 for (i = from_phy; i < ex->num_phys; i++) {
1720 int phy_change_count = 0;
1722 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1724 case SMP_RESP_PHY_VACANT:
1725 case SMP_RESP_NO_PHY:
1727 case SMP_RESP_FUNC_ACC:
1733 if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1735 ex->ex_phy[i].phy_change_count =
1744 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1748 struct smp_rg_resp *rg_resp;
1750 rg_req = alloc_smp_req(RG_REQ_SIZE);
1754 rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1760 rg_req[1] = SMP_REPORT_GENERAL;
1762 res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1766 if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1767 res = rg_resp->result;
1771 *ecc = be16_to_cpu(rg_resp->rg.change_count);
1778 * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE).
1779 * @dev:domain device to be detect.
1780 * @src_dev: the device which originated BROADCAST(CHANGE).
1782 * Add self-configuration expander support. Suppose two expander cascading,
1783 * when the first level expander is self-configuring, hotplug the disks in
1784 * second level expander, BROADCAST(CHANGE) will not only be originated
1785 * in the second level expander, but also be originated in the first level
1786 * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1787 * expander changed count in two level expanders will all increment at least
1788 * once, but the phy which chang count has changed is the source device which
1792 static int sas_find_bcast_dev(struct domain_device *dev,
1793 struct domain_device **src_dev)
1795 struct expander_device *ex = &dev->ex_dev;
1796 int ex_change_count = -1;
1799 struct domain_device *ch;
1801 res = sas_get_ex_change_count(dev, &ex_change_count);
1804 if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1805 /* Just detect if this expander phys phy change count changed,
1806 * in order to determine if this expander originate BROADCAST,
1807 * and do not update phy change count field in our structure.
1809 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1812 ex->ex_change_count = ex_change_count;
1813 pr_info("ex %016llx phy%02d change count has changed\n",
1814 SAS_ADDR(dev->sas_addr), phy_id);
1817 pr_info("ex %016llx phys DID NOT change\n",
1818 SAS_ADDR(dev->sas_addr));
1820 list_for_each_entry(ch, &ex->children, siblings) {
1821 if (dev_is_expander(ch->dev_type)) {
1822 res = sas_find_bcast_dev(ch, src_dev);
1831 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1833 struct expander_device *ex = &dev->ex_dev;
1834 struct domain_device *child, *n;
1836 list_for_each_entry_safe(child, n, &ex->children, siblings) {
1837 set_bit(SAS_DEV_GONE, &child->state);
1838 if (dev_is_expander(child->dev_type))
1839 sas_unregister_ex_tree(port, child);
1841 sas_unregister_dev(port, child);
1843 sas_unregister_dev(port, dev);
1846 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1847 int phy_id, bool last)
1849 struct expander_device *ex_dev = &parent->ex_dev;
1850 struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1851 struct domain_device *child, *n, *found = NULL;
1853 list_for_each_entry_safe(child, n,
1854 &ex_dev->children, siblings) {
1855 if (sas_phy_match_dev_addr(child, phy)) {
1856 set_bit(SAS_DEV_GONE, &child->state);
1857 if (dev_is_expander(child->dev_type))
1858 sas_unregister_ex_tree(parent->port, child);
1860 sas_unregister_dev(parent->port, child);
1865 sas_disable_routing(parent, phy->attached_sas_addr);
1867 memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1869 sas_port_delete_phy(phy->port, phy->phy);
1870 sas_device_set_phy(found, phy->port);
1871 if (phy->port->num_phys == 0) {
1872 list_add_tail(&phy->port->del_list,
1873 &parent->port->sas_port_del_list);
1874 if (ex_dev->parent_port == phy->port)
1875 ex_dev->parent_port = NULL;
1881 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1884 struct expander_device *ex_root = &root->ex_dev;
1885 struct domain_device *child;
1888 list_for_each_entry(child, &ex_root->children, siblings) {
1889 if (dev_is_expander(child->dev_type)) {
1890 struct sas_expander_device *ex =
1891 rphy_to_expander_device(child->rphy);
1893 if (level > ex->level)
1894 res = sas_discover_bfs_by_root_level(child,
1896 else if (level == ex->level)
1897 res = sas_ex_discover_devices(child, -1);
1903 static int sas_discover_bfs_by_root(struct domain_device *dev)
1906 struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1907 int level = ex->level+1;
1909 res = sas_ex_discover_devices(dev, -1);
1913 res = sas_discover_bfs_by_root_level(dev, level);
1916 } while (level <= dev->port->disc.max_level);
1921 static int sas_discover_new(struct domain_device *dev, int phy_id)
1923 struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1924 struct domain_device *child;
1927 pr_debug("ex %016llx phy%02d new device attached\n",
1928 SAS_ADDR(dev->sas_addr), phy_id);
1929 res = sas_ex_phy_discover(dev, phy_id);
1933 if (sas_ex_join_wide_port(dev, phy_id))
1936 res = sas_ex_discover_devices(dev, phy_id);
1939 list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1940 if (sas_phy_match_dev_addr(child, ex_phy)) {
1941 if (dev_is_expander(child->dev_type))
1942 res = sas_discover_bfs_by_root(child);
1949 static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old)
1954 /* treat device directed resets as flutter, if we went
1955 * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery
1957 if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) ||
1958 (old == SAS_END_DEVICE && new == SAS_SATA_PENDING))
1964 static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
1965 bool last, int sibling)
1967 struct expander_device *ex = &dev->ex_dev;
1968 struct ex_phy *phy = &ex->ex_phy[phy_id];
1969 enum sas_device_type type = SAS_PHY_UNUSED;
1970 struct smp_disc_resp *disc_resp;
1971 u8 sas_addr[SAS_ADDR_SIZE];
1976 sprintf(msg, ", part of a wide port with phy%02d", sibling);
1978 pr_debug("ex %016llx rediscovering phy%02d%s\n",
1979 SAS_ADDR(dev->sas_addr), phy_id, msg);
1981 memset(sas_addr, 0, SAS_ADDR_SIZE);
1982 disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1986 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1988 case SMP_RESP_NO_PHY:
1989 phy->phy_state = PHY_NOT_PRESENT;
1990 sas_unregister_devs_sas_addr(dev, phy_id, last);
1992 case SMP_RESP_PHY_VACANT:
1993 phy->phy_state = PHY_VACANT;
1994 sas_unregister_devs_sas_addr(dev, phy_id, last);
1996 case SMP_RESP_FUNC_ACC:
2005 sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, &type);
2007 if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
2008 phy->phy_state = PHY_EMPTY;
2009 sas_unregister_devs_sas_addr(dev, phy_id, last);
2011 * Even though the PHY is empty, for convenience we update
2012 * the PHY info, like negotiated linkrate.
2015 sas_set_ex_phy(dev, phy_id, disc_resp);
2017 } else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2018 dev_type_flutter(type, phy->attached_dev_type)) {
2019 struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2022 sas_ex_phy_discover(dev, phy_id);
2024 if (ata_dev && phy->attached_dev_type == SAS_SATA_PENDING)
2025 action = ", needs recovery";
2026 pr_debug("ex %016llx phy%02d broadcast flutter%s\n",
2027 SAS_ADDR(dev->sas_addr), phy_id, action);
2031 /* we always have to delete the old device when we went here */
2032 pr_info("ex %016llx phy%02d replace %016llx\n",
2033 SAS_ADDR(dev->sas_addr), phy_id,
2034 SAS_ADDR(phy->attached_sas_addr));
2035 sas_unregister_devs_sas_addr(dev, phy_id, last);
2037 res = sas_discover_new(dev, phy_id);
2044 * sas_rediscover - revalidate the domain.
2045 * @dev:domain device to be detect.
2046 * @phy_id: the phy id will be detected.
2048 * NOTE: this process _must_ quit (return) as soon as any connection
2049 * errors are encountered. Connection recovery is done elsewhere.
2050 * Discover process only interrogates devices in order to discover the
2051 * domain.For plugging out, we un-register the device only when it is
2052 * the last phy in the port, for other phys in this port, we just delete it
2053 * from the port.For inserting, we do discovery when it is the
2054 * first phy,for other phys in this port, we add it to the port to
2055 * forming the wide-port.
2057 static int sas_rediscover(struct domain_device *dev, const int phy_id)
2059 struct expander_device *ex = &dev->ex_dev;
2060 struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2063 bool last = true; /* is this the last phy of the port */
2065 pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n",
2066 SAS_ADDR(dev->sas_addr), phy_id);
2068 if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2069 for (i = 0; i < ex->num_phys; i++) {
2070 struct ex_phy *phy = &ex->ex_phy[i];
2074 if (sas_phy_addr_match(phy, changed_phy)) {
2079 res = sas_rediscover_dev(dev, phy_id, last, i);
2081 res = sas_discover_new(dev, phy_id);
2086 * sas_ex_revalidate_domain - revalidate the domain
2087 * @port_dev: port domain device.
2089 * NOTE: this process _must_ quit (return) as soon as any connection
2090 * errors are encountered. Connection recovery is done elsewhere.
2091 * Discover process only interrogates devices in order to discover the
2094 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2097 struct domain_device *dev = NULL;
2099 res = sas_find_bcast_dev(port_dev, &dev);
2100 if (res == 0 && dev) {
2101 struct expander_device *ex = &dev->ex_dev;
2106 res = sas_find_bcast_phy(dev, &phy_id, i, true);
2109 res = sas_rediscover(dev, phy_id);
2111 } while (i < ex->num_phys);
2116 int sas_find_attached_phy_id(struct expander_device *ex_dev,
2117 struct domain_device *dev)
2122 for (phy_id = 0; phy_id < ex_dev->num_phys; phy_id++) {
2123 phy = &ex_dev->ex_phy[phy_id];
2124 if (sas_phy_match_dev_addr(dev, phy))
2130 EXPORT_SYMBOL_GPL(sas_find_attached_phy_id);
2132 void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2133 struct sas_rphy *rphy)
2135 struct domain_device *dev;
2136 unsigned int rcvlen = 0;
2139 /* no rphy means no smp target support (ie aic94xx host) */
2141 return sas_smp_host_handler(job, shost);
2143 switch (rphy->identify.device_type) {
2144 case SAS_EDGE_EXPANDER_DEVICE:
2145 case SAS_FANOUT_EXPANDER_DEVICE:
2148 pr_err("%s: can we send a smp request to a device?\n",
2153 dev = sas_find_dev_by_rphy(rphy);
2155 pr_err("%s: fail to find a domain_device?\n", __func__);
2159 /* do we need to support multiple segments? */
2160 if (job->request_payload.sg_cnt > 1 ||
2161 job->reply_payload.sg_cnt > 1) {
2162 pr_info("%s: multiple segments req %u, rsp %u\n",
2163 __func__, job->request_payload.payload_len,
2164 job->reply_payload.payload_len);
2168 ret = smp_execute_task_sg(dev, job->request_payload.sg_list,
2169 job->reply_payload.sg_list);
2171 /* bsg_job_done() requires the length received */
2172 rcvlen = job->reply_payload.payload_len - ret;
2177 bsg_job_done(job, ret, rcvlen);