27 #include <linux/slab.h>
34 #include "../scsi_sas_internal.h"
44 static void smp_task_timedout(
unsigned long _task)
57 static void smp_task_done(
struct sas_task *task)
65 #define SMP_TIMEOUT 10
68 void *
resp,
int resp_size)
76 for (retry = 0; retry < 3; retry++) {
95 task->
slow_task->timer.function = smp_task_timedout;
103 SAS_DPRINTK(
"executing SMP task failed:%d\n", res);
111 i->
dft->lldd_abort_task(task);
138 SAS_DPRINTK(
"%s: task to dev %016llx response: 0x%x "
139 "status 0x%x\n", __func__,
156 static inline void *alloc_smp_req(
int size)
164 static inline void *alloc_smp_resp(
int size)
186 static enum sas_dev_type to_dev_type(
struct discover_resp *dr)
191 if (dr->attached_dev_type ==
NO_DEVICE && dr->attached_sata_dev &&
195 return dr->attached_dev_type;
204 struct discover_resp *dr = &resp->disc;
209 bool new_phy = !phy->
phy;
221 switch (resp->result) {
263 phy->
phy->identify.device_type = dr->attached_dev_type;
268 phy->
phy->identify.phy_identifier =
phy_id;
269 phy->
phy->minimum_linkrate_hw = dr->hmin_linkrate;
270 phy->
phy->maximum_linkrate_hw = dr->hmax_linkrate;
271 phy->
phy->minimum_linkrate = dr->pmin_linkrate;
272 phy->
phy->maximum_linkrate = dr->pmax_linkrate;
284 type =
"stp pending";
292 type =
"host+target";
296 if (dr->attached_sata_dev)
327 SAS_DPRINTK(
"%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
330 sas_route_char(dev, phy), phy->
linkrate,
344 rphy = ex_phy->
port->rphy;
350 if (dev && dev_is_sata(dev))
356 #define DISCOVER_REQ_SIZE 16
357 #define DISCOVER_RESP_SIZE 56
359 static int sas_ex_phy_discover_helper(
struct domain_device *dev,
u8 *disc_req,
360 u8 *disc_resp,
int single)
362 struct discover_resp *dr;
365 disc_req[9] = single;
371 dr = &((
struct smp_resp *)disc_resp)->disc;
373 sas_printk(
"Found loopback topology, just ignore it!\n");
376 sas_set_ex_phy(dev, single, disc_resp);
399 if (0 <= single && single < ex->
num_phys) {
400 res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
404 for (i = 0; i < ex->
num_phys; i++) {
405 res = sas_ex_phy_discover_helper(dev, disc_req,
437 #define MAX_EXPANDER_PHYS 128
439 static void ex_assign_report_general(
struct domain_device *dev,
442 struct report_general_resp *rg = &resp->rg;
447 dev->
ex_dev.t2t_supp = rg->t2t_supp;
448 dev->
ex_dev.conf_route_table = rg->conf_route_table;
449 dev->
ex_dev.configuring = rg->configuring;
450 memcpy(dev->
ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
453 #define RG_REQ_SIZE 8
454 #define RG_RESP_SIZE 32
475 for (i = 0; i < 5; i++) {
476 res = smp_execute_task(dev, rg_req,
RG_REQ_SIZE, rg_resp,
484 SAS_DPRINTK(
"RG:ex %016llx returned SMP result:0x%x\n",
486 res = rg_resp->result;
490 ex_assign_report_general(dev, rg_resp);
492 if (dev->
ex_dev.configuring) {
505 static void ex_assign_manuf_info(
struct domain_device *dev,
void
508 u8 *mi_resp = _mi_resp;
517 if (mi_resp[8] & 1) {
525 #define MI_REQ_SIZE 8
526 #define MI_RESP_SIZE 64
552 SAS_DPRINTK(
"MI ex %016llx returned SMP result:0x%x\n",
557 ex_assign_manuf_info(dev, mi_resp);
564 #define PC_REQ_SIZE 44
565 #define PC_RESP_SIZE 8
600 static void sas_ex_disable_phy(
struct domain_device *dev,
int phy_id)
609 static void sas_ex_disable_port(
struct domain_device *dev,
u8 *sas_addr)
614 for (i = 0; i < ex->
num_phys; i++) {
622 sas_ex_disable_phy(dev, i);
640 #define RPEL_REQ_SIZE 16
641 #define RPEL_RESP_SIZE 32
680 #ifdef CONFIG_SCSI_SAS_ATA
682 #define RPS_REQ_SIZE 16
683 #define RPS_RESP_SIZE 60
689 u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
690 u8 *resp = (
u8 *)rps_resp;
698 res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
699 rps_resp, RPS_RESP_SIZE);
706 if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
709 for (i = 0; i < 5; i++) {
714 resp[j + 0] = resp[j + 3];
715 resp[j + 1] = resp[j + 2];
728 struct ex_phy *parent_phy)
736 port = parent_phy->
port;
738 for (i = 0; i < parent_ex->
num_phys; i++) {
772 child = sas_alloc_device();
776 kref_get(&parent->
kref);
791 sas_ex_get_linkrate(parent, child, phy);
794 #ifdef CONFIG_SCSI_SAS_ATA
815 SAS_DPRINTK(
"sas_discover_sata() for device %16llx at "
816 "%016llx:0x%x returned 0x%x\n",
834 sas_fill_in_rphy(child, rphy);
840 SAS_DPRINTK(
"sas_discover_end_dev() for device %16llx "
841 "at %016llx:0x%x returned 0x%x\n",
847 SAS_DPRINTK(
"target proto 0x%x at %016llx:0x%x not handled\n",
859 spin_lock_irq(&parent->
port->dev_list_lock);
861 spin_unlock_irq(&parent->
port->dev_list_lock);
866 sas_put_device(child);
871 static bool sas_ex_join_wide_port(
struct domain_device *parent,
int phy_id)
876 for (i = 0; i < parent->
ex_dev.num_phys; i++) {
906 SAS_DPRINTK(
"ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
913 child = sas_alloc_device();
940 kref_get(&parent->
kref);
947 sas_ex_get_linkrate(parent, child, phy);
949 parent->
port->disc.max_level =
max(parent->
port->disc.max_level,
952 sas_fill_in_rphy(child, rphy);
955 spin_lock_irq(&parent->
port->dev_list_lock);
957 spin_unlock_irq(&parent->
port->dev_list_lock);
959 res = sas_discover_expander(child);
962 spin_lock_irq(&parent->
port->dev_list_lock);
964 spin_unlock_irq(&parent->
port->dev_list_lock);
965 sas_put_device(child);
972 static int sas_ex_discover_dev(
struct domain_device *dev,
int phy_id)
990 sas_add_parent_port(dev, phy_id);
995 sas_add_parent_port(dev, phy_id);
997 sas_configure_phy(dev, phy_id, dev->
port->sas_addr, 1);
1017 SAS_DPRINTK(
"unknown device type(0x%x) attached to ex %016llx "
1027 "reported 0x%x. Forgotten\n",
1033 if (sas_ex_join_wide_port(dev, phy_id)) {
1034 SAS_DPRINTK(
"Attaching ex phy%d to wide port %016llx\n",
1042 child = sas_ex_discover_end_dev(dev, phy_id);
1046 SAS_DPRINTK(
"second fanout expander %016llx phy 0x%x "
1047 "attached to ex %016llx phy 0x%x\n",
1052 sas_ex_disable_phy(dev, phy_id);
1059 child = sas_ex_discover_expander(dev, phy_id);
1068 for (i = 0; i < ex->
num_phys; i++) {
1079 if (sas_ex_join_wide_port(dev, i))
1080 SAS_DPRINTK(
"Attaching ex phy%d to wide port %016llx\n",
1095 for (i = 0; i < ex->
num_phys; i++) {
1096 struct ex_phy *phy = &ex->
ex_phy[
i];
1114 static int sas_check_level_subtractive_boundary(
struct domain_device *dev)
1118 u8 sub_addr[8] = {0, };
1124 if (sub_addr[0] == 0) {
1125 sas_find_sub_addr(child, sub_addr);
1130 if (sas_find_sub_addr(child, s2) &&
1134 "diverges from subtractive "
1135 "boundary %016llx\n",
1141 sas_ex_disable_port(child, s2);
1155 static int sas_ex_discover_devices(
struct domain_device *dev,
int single)
1161 if (0 <= single && single <
end) {
1166 for ( ; i <
end; i++) {
1167 struct ex_phy *ex_phy = &ex->
ex_phy[
i];
1180 res = sas_ex_discover_dev(dev, i);
1188 sas_check_level_subtractive_boundary(dev);
1193 static int sas_check_ex_subtractive_boundary(
struct domain_device *dev)
1202 for (i = 0; i < ex->
num_phys; i++) {
1203 struct ex_phy *phy = &ex->
ex_phy[
i];
1219 "diverges(%016llx) on subtractive "
1220 "boundary(%016llx). Disabled\n",
1224 sas_ex_disable_phy(dev, i);
1231 static void sas_print_parent_topology_bug(
struct domain_device *child,
1232 struct ex_phy *parent_phy,
1233 struct ex_phy *child_phy)
1235 static const char *ex_type[] = {
1241 sas_printk(
"%s ex %016llx phy 0x%x <--> %s ex %016llx "
1242 "phy 0x%x has %c:%c routing link!\n",
1252 sas_route_char(parent, parent_phy),
1253 sas_route_char(child, child_phy));
1257 struct ex_phy *parent_phy,
1258 struct ex_phy *child_phy)
1263 if (
SAS_ADDR(parent->
port->disc.fanout_sas_addr) != 0) {
1265 SAS_DPRINTK(
"edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1266 "phy S:0x%x, while there is a fanout ex %016llx\n",
1289 SAS_DPRINTK(
"edge ex %016llx phy 0x%x <--> edge ex %016llx "
1290 "phy 0x%x link forms a third EEDS!\n",
1302 static int sas_check_parent_topology(
struct domain_device *child)
1316 parent_ex = &child->
parent->ex_dev;
1318 for (i = 0; i < parent_ex->
num_phys; i++) {
1319 struct ex_phy *parent_phy = &parent_ex->
ex_phy[
i];
1320 struct ex_phy *child_phy;
1331 switch (child->
parent->dev_type) {
1336 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1341 res = sas_check_eeds(child, parent_phy, child_phy);
1343 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1352 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1360 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1372 #define RRI_REQ_SIZE 16
1373 #define RRI_RESP_SIZE 44
1375 static int sas_configure_present(
struct domain_device *dev,
int phy_id,
1376 u8 *sas_addr,
int *
index,
int *present)
1402 res = smp_execute_task(dev, rri_req,
RRI_REQ_SIZE, rri_resp,
1409 "phy 0x%x index 0x%x\n",
1413 SAS_DPRINTK(
"%s: dev %016llx phy 0x%x index 0x%x "
1414 "result 0x%x\n", __func__,
1421 if ((rri_resp[12] & 0x80) == 0x80)
1426 }
else if (
SAS_ADDR(rri_resp+16) == 0) {
1431 }
else if (
SAS_ADDR(rri_resp+16) == 0 &&
1446 #define CRI_REQ_SIZE 44
1447 #define CRI_RESP_SIZE 8
1449 static int sas_configure_set(
struct domain_device *dev,
int phy_id,
1450 u8 *sas_addr,
int index,
int include)
1469 if (
SAS_ADDR(sas_addr) == 0 || !include)
1470 cri_req[12] |= 0x80;
1473 res = smp_execute_task(dev, cri_req,
CRI_REQ_SIZE, cri_resp,
1479 SAS_DPRINTK(
"overflow of indexes: dev %016llx phy 0x%x "
1489 static int sas_configure_phy(
struct domain_device *dev,
int phy_id,
1490 u8 *sas_addr,
int include)
1496 res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1499 if (include ^ present)
1500 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1511 static int sas_configure_parent(
struct domain_device *parent,
1513 u8 *sas_addr,
int include)
1520 res = sas_configure_parent(parent->
parent, parent, sas_addr,
1527 SAS_DPRINTK(
"ex %016llx has self-configuring routing table\n",
1532 for (i = 0; i < ex_parent->
num_phys; i++) {
1533 struct ex_phy *phy = &ex_parent->
ex_phy[
i];
1538 res = sas_configure_phy(parent, i, sas_addr, include);
1552 static int sas_configure_routing(
struct domain_device *dev,
u8 *sas_addr)
1555 return sas_configure_parent(dev->
parent, dev, sas_addr, 1);
1559 static int sas_disable_routing(
struct domain_device *dev,
u8 *sas_addr)
1562 return sas_configure_parent(dev->
parent, dev, sas_addr, 0);
1580 res = sas_ex_general(dev);
1583 res = sas_ex_manuf_info(dev);
1587 res = sas_expander_discover(dev);
1589 SAS_DPRINTK(
"expander %016llx discovery failed(0x%x)\n",
1594 sas_check_ex_subtractive_boundary(dev);
1595 res = sas_check_parent_topology(dev);
1615 if (level == ex->
level)
1616 res = sas_ex_discover_devices(dev, -1);
1618 res = sas_ex_discover_devices(port->
port_dev, -1);
1632 level = port->
disc.max_level;
1633 res = sas_ex_level_discovery(port, level);
1635 }
while (level < port->disc.max_level);
1650 res = sas_discover_expander(dev);
1654 sas_ex_bfs_disc(dev->
port);
1667 int phy_id,
struct smp_resp *disc_resp)
1684 res = disc_resp->result;
1692 static int sas_get_phy_change_count(
struct domain_device *dev,
1693 int phy_id,
int *pcc)
1702 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1704 *pcc = disc_resp->disc.change_count;
1710 static int sas_get_phy_attached_dev(
struct domain_device *dev,
int phy_id,
1715 struct discover_resp *dr;
1720 dr = &disc_resp->disc;
1722 res = sas_get_phy_discover(dev, phy_id, disc_resp);
1724 memcpy(sas_addr, disc_resp->disc.attached_sas_addr, 8);
1725 *type = to_dev_type(dr);
1733 static int sas_find_bcast_phy(
struct domain_device *dev,
int *phy_id,
1734 int from_phy,
bool update)
1740 for (i = from_phy; i < ex->
num_phys; i++) {
1741 int phy_change_count = 0;
1743 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1754 if (phy_change_count != ex->
ex_phy[i].phy_change_count) {
1756 ex->
ex_phy[
i].phy_change_count =
1783 res = smp_execute_task(dev, rg_req,
RG_REQ_SIZE, rg_resp,
1788 res = rg_resp->result;
1822 res = sas_get_ex_change_count(dev, &ex_change_count);
1825 if (ex_change_count != -1 && ex_change_count != ex->
ex_change_count) {
1830 res = sas_find_bcast_phy(dev, &phy_id, 0,
false);
1834 SAS_DPRINTK(
"Expander phy change count has changed\n");
1841 res = sas_find_bcast_dev(ch, src_dev);
1859 sas_unregister_ex_tree(port, child);
1866 static void sas_unregister_devs_sas_addr(
struct domain_device *parent,
1867 int phy_id,
bool last)
1880 sas_unregister_ex_tree(parent->
port, child);
1893 if (phy->
port->num_phys == 0)
1899 static int sas_discover_bfs_by_root_level(
struct domain_device *root,
1912 if (level > ex->
level)
1913 res = sas_discover_bfs_by_root_level(child,
1915 else if (level == ex->
level)
1916 res = sas_ex_discover_devices(child, -1);
1922 static int sas_discover_bfs_by_root(
struct domain_device *dev)
1926 int level = ex->
level+1;
1928 res = sas_ex_discover_devices(dev, -1);
1932 res = sas_discover_bfs_by_root_level(dev, level);
1935 }
while (level <= dev->port->
disc.max_level);
1940 static int sas_discover_new(
struct domain_device *dev,
int phy_id)
1946 SAS_DPRINTK(
"ex %016llx phy%d new device attached\n",
1952 if (sas_ex_join_wide_port(dev, phy_id))
1955 res = sas_ex_discover_devices(dev, phy_id);
1963 res = sas_discover_bfs_by_root(child);
1985 static int sas_rediscover_dev(
struct domain_device *dev,
int phy_id,
bool last)
1994 res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
1998 sas_unregister_devs_sas_addr(dev, phy_id, last);
2002 sas_unregister_devs_sas_addr(dev, phy_id, last);
2014 sas_unregister_devs_sas_addr(dev, phy_id, last);
2024 action =
", needs recovery";
2025 SAS_DPRINTK(
"ex %016llx phy 0x%x broadcast flutter%s\n",
2033 SAS_DPRINTK(
"ex %016llx phy 0x%x replace %016llx\n",
2036 sas_unregister_devs_sas_addr(dev, phy_id, last);
2039 return sas_discover_new(dev, phy_id);
2056 static int sas_rediscover(
struct domain_device *dev,
const int phy_id)
2064 SAS_DPRINTK(
"ex %016llx phy%d originated BROADCAST(CHANGE)\n",
2068 for (i = 0; i < ex->
num_phys; i++) {
2069 struct ex_phy *phy = &ex->
ex_phy[
i];
2076 "phy%d\n", phy_id, i);
2081 res = sas_rediscover_dev(dev, phy_id, last);
2083 res = sas_discover_new(dev, phy_id);
2101 res = sas_find_bcast_dev(port_dev, &dev);
2102 while (res == 0 && dev) {
2108 res = sas_find_bcast_phy(dev, &phy_id, i,
true);
2111 res = sas_rediscover(dev, phy_id);
2116 res = sas_find_bcast_dev(port_dev, &dev);
2126 struct request *rsp = req->next_rq;
2129 printk(
"%s: space for a smp response is missing\n",
2142 printk(
"%s: can we send a smp request to a device?\n",
2149 printk(
"%s: fail to find a domain_device?\n", __func__);
2154 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2155 printk(
"%s: multiple segments req %u %u, rsp %u %u\n",
2156 __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2157 rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2161 ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2162 bio_data(rsp->bio), blk_rq_bytes(rsp));
2165 rsp->resid_len =
ret;
2168 }
else if (ret == 0) {