at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Serial Attached SCSI (SAS) Expander discovery and configuration |
| 4 | * |
| 5 | * Copyright (C) 2005 Adaptec, Inc. All rights reserved. |
| 6 | * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> |
| 7 | * |
| 8 | * This file is licensed under GPLv2. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/scatterlist.h> |
| 12 | #include <linux/blkdev.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/unaligned.h> |
| 15 | |
| 16 | #include "sas_internal.h" |
| 17 | |
| 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" |
| 22 | |
| 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); |
| 28 | |
| 29 | static void sas_port_add_ex_phy(struct sas_port *port, struct ex_phy *ex_phy) |
| 30 | { |
| 31 | sas_port_add_phy(port, ex_phy->phy); |
| 32 | ex_phy->port = port; |
| 33 | ex_phy->phy_state = PHY_DEVICE_DISCOVERED; |
| 34 | } |
| 35 | |
| 36 | static void sas_ex_add_parent_port(struct domain_device *dev, int phy_id) |
| 37 | { |
| 38 | struct expander_device *ex = &dev->ex_dev; |
| 39 | struct ex_phy *ex_phy = &ex->ex_phy[phy_id]; |
| 40 | |
| 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); |
| 47 | } |
| 48 | sas_port_add_ex_phy(ex->parent_port, ex_phy); |
| 49 | } |
| 50 | |
| 51 | /* ---------- SMP task management ---------- */ |
| 52 | |
| 53 | /* Give it some long enough timeout. In seconds. */ |
| 54 | #define SMP_TIMEOUT 10 |
| 55 | |
| 56 | static int smp_execute_task_sg(struct domain_device *dev, |
| 57 | struct scatterlist *req, struct scatterlist *resp) |
| 58 | { |
| 59 | int res, retry; |
| 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; |
| 64 | |
| 65 | /* |
| 66 | * Non-blocking: a sync resume here would deadlock against |
| 67 | * sas_drain_work() during HA resume. |
| 68 | */ |
| 69 | pm_runtime_get_noresume(ha->dev); |
| 70 | mutex_lock(&dev->ex_dev.cmd_mutex); |
| 71 | for (retry = 0; retry < 3; retry++) { |
| 72 | if (test_bit(SAS_DEV_GONE, &dev->state)) { |
| 73 | res = -ECOMM; |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | task = sas_alloc_slow_task(GFP_KERNEL); |
| 78 | if (!task) { |
| 79 | res = -ENOMEM; |
| 80 | break; |
| 81 | } |
| 82 | task->dev = dev; |
| 83 | task->task_proto = dev->tproto; |
| 84 | task->smp_task.smp_req = *req; |
| 85 | task->smp_task.smp_resp = *resp; |
| 86 | |
| 87 | task->task_done = sas_task_internal_done; |
| 88 | |
| 89 | task->slow_task->timer.function = sas_task_internal_timedout; |
| 90 | task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ; |
| 91 | add_timer(&task->slow_task->timer); |
| 92 | |
| 93 | res = i->dft->lldd_execute_task(task, GFP_KERNEL); |
| 94 | |
| 95 | if (res) { |
| 96 | timer_delete_sync(&task->slow_task->timer); |
| 97 | pr_notice("executing SMP task failed:%d\n", res); |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | wait_for_completion(&task->slow_task->completion); |
| 102 | res = -ECOMM; |
| 103 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 104 | pr_notice("smp task timed out or aborted\n"); |
| 105 | i->dft->lldd_abort_task(task); |
| 106 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
| 107 | pr_notice("SMP task aborted and not done\n"); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 112 | task->task_status.stat == SAS_SAM_STAT_GOOD) { |
| 113 | res = 0; |
| 114 | break; |
| 115 | } |
| 116 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 117 | task->task_status.stat == SAS_DATA_UNDERRUN) { |
| 118 | /* no error, but return the number of bytes of |
| 119 | * underrun */ |
| 120 | res = task->task_status.residual; |
| 121 | break; |
| 122 | } |
| 123 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 124 | task->task_status.stat == SAS_DATA_OVERRUN) { |
| 125 | res = -EMSGSIZE; |
| 126 | break; |
| 127 | } |
| 128 | if (task->task_status.resp == SAS_TASK_UNDELIVERED && |
| 129 | task->task_status.stat == SAS_DEVICE_UNKNOWN) |
| 130 | break; |
| 131 | else { |
| 132 | pr_notice("%s: task to dev %016llx response: 0x%x status 0x%x\n", |
| 133 | __func__, |
| 134 | SAS_ADDR(dev->sas_addr), |
| 135 | task->task_status.resp, |
| 136 | task->task_status.stat); |
| 137 | sas_free_task(task); |
| 138 | task = NULL; |
| 139 | } |
| 140 | } |
| 141 | mutex_unlock(&dev->ex_dev.cmd_mutex); |
| 142 | pm_runtime_put(ha->dev); |
| 143 | |
| 144 | BUG_ON(retry == 3 && task != NULL); |
| 145 | sas_free_task(task); |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | static int smp_execute_task(struct domain_device *dev, void *req, int req_size, |
| 150 | void *resp, int resp_size) |
| 151 | { |
| 152 | struct scatterlist req_sg; |
| 153 | struct scatterlist resp_sg; |
| 154 | |
| 155 | sg_init_one(&req_sg, req, req_size); |
| 156 | sg_init_one(&resp_sg, resp, resp_size); |
| 157 | return smp_execute_task_sg(dev, &req_sg, &resp_sg); |
| 158 | } |
| 159 | |
| 160 | /* ---------- Allocations ---------- */ |
| 161 | |
| 162 | static inline void *alloc_smp_req(int size) |
| 163 | { |
| 164 | u8 *p = kzalloc(ALIGN(size, ARCH_DMA_MINALIGN), GFP_KERNEL); |
| 165 | if (p) |
| 166 | p[0] = SMP_REQUEST; |
| 167 | return p; |
| 168 | } |
| 169 | |
| 170 | static inline void *alloc_smp_resp(int size) |
| 171 | { |
| 172 | return kzalloc(size, GFP_KERNEL); |
| 173 | } |
| 174 | |
| 175 | static char sas_route_char(struct domain_device *dev, struct ex_phy *phy) |
| 176 | { |
| 177 | switch (phy->routing_attr) { |
| 178 | case TABLE_ROUTING: |
| 179 | if (dev->ex_dev.t2t_supp) |
| 180 | return 'U'; |
| 181 | else |
| 182 | return 'T'; |
| 183 | case DIRECT_ROUTING: |
| 184 | return 'D'; |
| 185 | case SUBTRACTIVE_ROUTING: |
| 186 | return 'S'; |
| 187 | default: |
| 188 | return '?'; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static enum sas_device_type to_dev_type(struct discover_resp *dr) |
| 193 | { |
| 194 | /* This is detecting a failure to transmit initial dev to host |
| 195 | * FIS as described in section J.5 of sas-2 r16 |
| 196 | */ |
| 197 | if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev && |
| 198 | dr->linkrate >= SAS_LINK_RATE_1_5_GBPS) |
| 199 | return SAS_SATA_PENDING; |
| 200 | else |
| 201 | return dr->attached_dev_type; |
| 202 | } |
| 203 | |
| 204 | static void sas_set_ex_phy(struct domain_device *dev, int phy_id, |
| 205 | struct smp_disc_resp *disc_resp) |
| 206 | { |
| 207 | enum sas_device_type dev_type; |
| 208 | enum sas_linkrate linkrate; |
| 209 | u8 sas_addr[SAS_ADDR_SIZE]; |
| 210 | struct discover_resp *dr = &disc_resp->disc; |
| 211 | struct sas_ha_struct *ha = dev->port->ha; |
| 212 | struct expander_device *ex = &dev->ex_dev; |
| 213 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 214 | struct sas_rphy *rphy = dev->rphy; |
| 215 | bool new_phy = !phy->phy; |
| 216 | char *type; |
| 217 | |
| 218 | if (new_phy) { |
| 219 | if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))) |
| 220 | return; |
| 221 | phy->phy = sas_phy_alloc(&rphy->dev, phy_id); |
| 222 | |
| 223 | /* FIXME: error_handling */ |
| 224 | BUG_ON(!phy->phy); |
| 225 | } |
| 226 | |
| 227 | switch (disc_resp->result) { |
| 228 | case SMP_RESP_PHY_VACANT: |
| 229 | phy->phy_state = PHY_VACANT; |
| 230 | break; |
| 231 | default: |
| 232 | phy->phy_state = PHY_NOT_PRESENT; |
| 233 | break; |
| 234 | case SMP_RESP_FUNC_ACC: |
| 235 | phy->phy_state = PHY_EMPTY; /* do not know yet */ |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | /* check if anything important changed to squelch debug */ |
| 240 | dev_type = phy->attached_dev_type; |
| 241 | linkrate = phy->linkrate; |
| 242 | memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); |
| 243 | |
| 244 | /* Handle vacant phy - rest of dr data is not valid so skip it */ |
| 245 | if (phy->phy_state == PHY_VACANT) { |
| 246 | memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); |
| 247 | phy->attached_dev_type = SAS_PHY_UNUSED; |
| 248 | if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) { |
| 249 | phy->phy_id = phy_id; |
| 250 | goto skip; |
| 251 | } else |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | phy->attached_dev_type = to_dev_type(dr); |
| 256 | if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) |
| 257 | goto out; |
| 258 | phy->phy_id = phy_id; |
| 259 | phy->linkrate = dr->linkrate; |
| 260 | phy->attached_sata_host = dr->attached_sata_host; |
| 261 | phy->attached_sata_dev = dr->attached_sata_dev; |
| 262 | phy->attached_sata_ps = dr->attached_sata_ps; |
| 263 | phy->attached_iproto = dr->iproto << 1; |
| 264 | phy->attached_tproto = dr->tproto << 1; |
| 265 | /* help some expanders that fail to zero sas_address in the 'no |
| 266 | * device' case |
| 267 | */ |
| 268 | if (phy->attached_dev_type == SAS_PHY_UNUSED) |
| 269 | memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); |
| 270 | else |
| 271 | memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE); |
| 272 | phy->attached_phy_id = dr->attached_phy_id; |
| 273 | phy->phy_change_count = dr->change_count; |
| 274 | phy->routing_attr = dr->routing_attr; |
| 275 | phy->virtual = dr->virtual; |
| 276 | phy->last_da_index = -1; |
| 277 | |
| 278 | phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr); |
| 279 | phy->phy->identify.device_type = dr->attached_dev_type; |
| 280 | phy->phy->identify.initiator_port_protocols = phy->attached_iproto; |
| 281 | phy->phy->identify.target_port_protocols = phy->attached_tproto; |
| 282 | if (!phy->attached_tproto && dr->attached_sata_dev) |
| 283 | phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA; |
| 284 | phy->phy->identify.phy_identifier = phy_id; |
| 285 | phy->phy->minimum_linkrate_hw = dr->hmin_linkrate; |
| 286 | phy->phy->maximum_linkrate_hw = dr->hmax_linkrate; |
| 287 | phy->phy->minimum_linkrate = dr->pmin_linkrate; |
| 288 | phy->phy->maximum_linkrate = dr->pmax_linkrate; |
| 289 | phy->phy->negotiated_linkrate = phy->linkrate; |
| 290 | phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED); |
| 291 | |
| 292 | skip: |
| 293 | if (new_phy) |
| 294 | if (sas_phy_add(phy->phy)) { |
| 295 | sas_phy_free(phy->phy); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | out: |
| 300 | switch (phy->attached_dev_type) { |
| 301 | case SAS_SATA_PENDING: |
| 302 | type = "stp pending"; |
| 303 | break; |
| 304 | case SAS_PHY_UNUSED: |
| 305 | type = "no device"; |
| 306 | break; |
| 307 | case SAS_END_DEVICE: |
| 308 | if (phy->attached_iproto) { |
| 309 | if (phy->attached_tproto) |
| 310 | type = "host+target"; |
| 311 | else |
| 312 | type = "host"; |
| 313 | } else { |
| 314 | if (dr->attached_sata_dev) |
| 315 | type = "stp"; |
| 316 | else |
| 317 | type = "ssp"; |
| 318 | } |
| 319 | break; |
| 320 | case SAS_EDGE_EXPANDER_DEVICE: |
| 321 | case SAS_FANOUT_EXPANDER_DEVICE: |
| 322 | type = "smp"; |
| 323 | break; |
| 324 | default: |
| 325 | type = "unknown"; |
| 326 | } |
| 327 | |
| 328 | /* this routine is polled by libata error recovery so filter |
| 329 | * unimportant messages |
| 330 | */ |
| 331 | if (new_phy || phy->attached_dev_type != dev_type || |
| 332 | phy->linkrate != linkrate || |
| 333 | SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr)) |
| 334 | /* pass */; |
| 335 | else |
| 336 | return; |
| 337 | |
| 338 | /* if the attached device type changed and ata_eh is active, |
| 339 | * make sure we run revalidation when eh completes (see: |
| 340 | * sas_enable_revalidation) |
| 341 | */ |
| 342 | if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) |
| 343 | set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending); |
| 344 | |
| 345 | pr_debug("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n", |
| 346 | test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "", |
| 347 | SAS_ADDR(dev->sas_addr), phy->phy_id, |
| 348 | sas_route_char(dev, phy), phy->linkrate, |
| 349 | SAS_ADDR(phy->attached_sas_addr), type); |
| 350 | } |
| 351 | |
| 352 | /* Return the domain device attached to an expander phy */ |
| 353 | struct domain_device *sas_ex_phy_to_dev(struct domain_device *ex_dev, int phy_id) |
| 354 | { |
| 355 | struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id]; |
| 356 | struct sas_rphy *rphy; |
| 357 | |
| 358 | if (!ex_phy->port) |
| 359 | return NULL; |
| 360 | |
| 361 | rphy = ex_phy->port->rphy; |
| 362 | if (!rphy) |
| 363 | return NULL; |
| 364 | |
| 365 | return sas_find_dev_by_rphy(rphy); |
| 366 | } |
| 367 | |
| 368 | /* Check if we have an existing attached ata device on this expander phy */ |
| 369 | struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id) |
| 370 | { |
| 371 | struct domain_device *dev = sas_ex_phy_to_dev(ex_dev, phy_id); |
| 372 | |
| 373 | if (dev && dev_is_sata(dev)) |
| 374 | return dev; |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | #define DISCOVER_REQ_SIZE 16 |
| 380 | #define DISCOVER_RESP_SIZE sizeof(struct smp_disc_resp) |
| 381 | |
| 382 | static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req, |
| 383 | struct smp_disc_resp *disc_resp, |
| 384 | int single) |
| 385 | { |
| 386 | struct discover_resp *dr = &disc_resp->disc; |
| 387 | int res; |
| 388 | |
| 389 | disc_req[9] = single; |
| 390 | |
| 391 | res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE, |
| 392 | disc_resp, DISCOVER_RESP_SIZE); |
| 393 | if (res) |
| 394 | return res; |
| 395 | if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) { |
| 396 | pr_notice("Found loopback topology, just ignore it!\n"); |
| 397 | return 0; |
| 398 | } |
| 399 | sas_set_ex_phy(dev, single, disc_resp); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | int sas_ex_phy_discover(struct domain_device *dev, int single) |
| 404 | { |
| 405 | struct expander_device *ex = &dev->ex_dev; |
| 406 | int res = 0; |
| 407 | u8 *disc_req; |
| 408 | struct smp_disc_resp *disc_resp; |
| 409 | |
| 410 | disc_req = alloc_smp_req(DISCOVER_REQ_SIZE); |
| 411 | if (!disc_req) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); |
| 415 | if (!disc_resp) { |
| 416 | kfree(disc_req); |
| 417 | return -ENOMEM; |
| 418 | } |
| 419 | |
| 420 | disc_req[1] = SMP_DISCOVER; |
| 421 | |
| 422 | if (0 <= single && single < ex->num_phys) { |
| 423 | res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single); |
| 424 | } else { |
| 425 | int i; |
| 426 | |
| 427 | for (i = 0; i < ex->num_phys; i++) { |
| 428 | res = sas_ex_phy_discover_helper(dev, disc_req, |
| 429 | disc_resp, i); |
| 430 | if (res) |
| 431 | goto out_err; |
| 432 | } |
| 433 | } |
| 434 | out_err: |
| 435 | kfree(disc_resp); |
| 436 | kfree(disc_req); |
| 437 | return res; |
| 438 | } |
| 439 | |
| 440 | static int sas_expander_discover(struct domain_device *dev) |
| 441 | { |
| 442 | struct expander_device *ex = &dev->ex_dev; |
| 443 | int res; |
| 444 | |
| 445 | ex->ex_phy = kzalloc_objs(*ex->ex_phy, ex->num_phys); |
| 446 | if (!ex->ex_phy) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | res = sas_ex_phy_discover(dev, -1); |
| 450 | if (res) |
| 451 | goto out_err; |
| 452 | |
| 453 | return 0; |
| 454 | out_err: |
| 455 | kfree(ex->ex_phy); |
| 456 | ex->ex_phy = NULL; |
| 457 | return res; |
| 458 | } |
| 459 | |
| 460 | #define MAX_EXPANDER_PHYS 128 |
| 461 | |
| 462 | #define RG_REQ_SIZE 8 |
| 463 | #define RG_RESP_SIZE sizeof(struct smp_rg_resp) |
| 464 | |
| 465 | static int sas_ex_general(struct domain_device *dev) |
| 466 | { |
| 467 | u8 *rg_req; |
| 468 | struct smp_rg_resp *rg_resp; |
| 469 | struct report_general_resp *rg; |
| 470 | int res; |
| 471 | int i; |
| 472 | |
| 473 | rg_req = alloc_smp_req(RG_REQ_SIZE); |
| 474 | if (!rg_req) |
| 475 | return -ENOMEM; |
| 476 | |
| 477 | rg_resp = alloc_smp_resp(RG_RESP_SIZE); |
| 478 | if (!rg_resp) { |
| 479 | kfree(rg_req); |
| 480 | return -ENOMEM; |
| 481 | } |
| 482 | |
| 483 | rg_req[1] = SMP_REPORT_GENERAL; |
| 484 | |
| 485 | for (i = 0; i < 5; i++) { |
| 486 | res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp, |
| 487 | RG_RESP_SIZE); |
| 488 | |
| 489 | if (res) { |
| 490 | pr_notice("RG to ex %016llx failed:0x%x\n", |
| 491 | SAS_ADDR(dev->sas_addr), res); |
| 492 | goto out; |
| 493 | } else if (rg_resp->result != SMP_RESP_FUNC_ACC) { |
| 494 | pr_debug("RG:ex %016llx returned SMP result:0x%x\n", |
| 495 | SAS_ADDR(dev->sas_addr), rg_resp->result); |
| 496 | res = rg_resp->result; |
| 497 | goto out; |
| 498 | } |
| 499 | |
| 500 | rg = &rg_resp->rg; |
| 501 | dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count); |
| 502 | dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes); |
| 503 | dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS); |
| 504 | dev->ex_dev.t2t_supp = rg->t2t_supp; |
| 505 | dev->ex_dev.conf_route_table = rg->conf_route_table; |
| 506 | dev->ex_dev.configuring = rg->configuring; |
| 507 | memcpy(dev->ex_dev.enclosure_logical_id, |
| 508 | rg->enclosure_logical_id, 8); |
| 509 | |
| 510 | if (dev->ex_dev.configuring) { |
| 511 | pr_debug("RG: ex %016llx self-configuring...\n", |
| 512 | SAS_ADDR(dev->sas_addr)); |
| 513 | schedule_timeout_interruptible(5*HZ); |
| 514 | } else |
| 515 | break; |
| 516 | } |
| 517 | out: |
| 518 | kfree(rg_req); |
| 519 | kfree(rg_resp); |
| 520 | return res; |
| 521 | } |
| 522 | |
| 523 | static void ex_assign_manuf_info(struct domain_device *dev, void |
| 524 | *_mi_resp) |
| 525 | { |
| 526 | u8 *mi_resp = _mi_resp; |
| 527 | struct sas_rphy *rphy = dev->rphy; |
| 528 | struct sas_expander_device *edev = rphy_to_expander_device(rphy); |
| 529 | |
| 530 | memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN); |
| 531 | memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN); |
| 532 | memcpy(edev->product_rev, mi_resp + 36, |
| 533 | SAS_EXPANDER_PRODUCT_REV_LEN); |
| 534 | |
| 535 | if (mi_resp[8] & 1) { |
| 536 | memcpy(edev->component_vendor_id, mi_resp + 40, |
| 537 | SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN); |
| 538 | edev->component_id = mi_resp[48] << 8 | mi_resp[49]; |
| 539 | edev->component_revision_id = mi_resp[50]; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | #define MI_REQ_SIZE 8 |
| 544 | #define MI_RESP_SIZE 64 |
| 545 | |
| 546 | static int sas_ex_manuf_info(struct domain_device *dev) |
| 547 | { |
| 548 | u8 *mi_req; |
| 549 | u8 *mi_resp; |
| 550 | int res; |
| 551 | |
| 552 | mi_req = alloc_smp_req(MI_REQ_SIZE); |
| 553 | if (!mi_req) |
| 554 | return -ENOMEM; |
| 555 | |
| 556 | mi_resp = alloc_smp_resp(MI_RESP_SIZE); |
| 557 | if (!mi_resp) { |
| 558 | kfree(mi_req); |
| 559 | return -ENOMEM; |
| 560 | } |
| 561 | |
| 562 | mi_req[1] = SMP_REPORT_MANUF_INFO; |
| 563 | |
| 564 | res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp, MI_RESP_SIZE); |
| 565 | if (res) { |
| 566 | pr_notice("MI: ex %016llx failed:0x%x\n", |
| 567 | SAS_ADDR(dev->sas_addr), res); |
| 568 | goto out; |
| 569 | } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) { |
| 570 | pr_debug("MI ex %016llx returned SMP result:0x%x\n", |
| 571 | SAS_ADDR(dev->sas_addr), mi_resp[2]); |
| 572 | goto out; |
| 573 | } |
| 574 | |
| 575 | ex_assign_manuf_info(dev, mi_resp); |
| 576 | out: |
| 577 | kfree(mi_req); |
| 578 | kfree(mi_resp); |
| 579 | return res; |
| 580 | } |
| 581 | |
| 582 | #define PC_REQ_SIZE 44 |
| 583 | #define PC_RESP_SIZE 8 |
| 584 | |
| 585 | int sas_smp_phy_control(struct domain_device *dev, int phy_id, |
| 586 | enum phy_func phy_func, |
| 587 | struct sas_phy_linkrates *rates) |
| 588 | { |
| 589 | u8 *pc_req; |
| 590 | u8 *pc_resp; |
| 591 | int res; |
| 592 | |
| 593 | pc_req = alloc_smp_req(PC_REQ_SIZE); |
| 594 | if (!pc_req) |
| 595 | return -ENOMEM; |
| 596 | |
| 597 | pc_resp = alloc_smp_resp(PC_RESP_SIZE); |
| 598 | if (!pc_resp) { |
| 599 | kfree(pc_req); |
| 600 | return -ENOMEM; |
| 601 | } |
| 602 | |
| 603 | pc_req[1] = SMP_PHY_CONTROL; |
| 604 | pc_req[9] = phy_id; |
| 605 | pc_req[10] = phy_func; |
| 606 | if (rates) { |
| 607 | pc_req[32] = rates->minimum_linkrate << 4; |
| 608 | pc_req[33] = rates->maximum_linkrate << 4; |
| 609 | } |
| 610 | |
| 611 | res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp, PC_RESP_SIZE); |
| 612 | if (res) { |
| 613 | pr_err("ex %016llx phy%02d PHY control failed: %d\n", |
| 614 | SAS_ADDR(dev->sas_addr), phy_id, res); |
| 615 | } else if (pc_resp[2] != SMP_RESP_FUNC_ACC) { |
| 616 | pr_err("ex %016llx phy%02d PHY control failed: function result 0x%x\n", |
| 617 | SAS_ADDR(dev->sas_addr), phy_id, pc_resp[2]); |
| 618 | res = pc_resp[2]; |
| 619 | } |
| 620 | kfree(pc_resp); |
| 621 | kfree(pc_req); |
| 622 | return res; |
| 623 | } |
| 624 | |
| 625 | static void sas_ex_disable_phy(struct domain_device *dev, int phy_id) |
| 626 | { |
| 627 | struct expander_device *ex = &dev->ex_dev; |
| 628 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 629 | |
| 630 | sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL); |
| 631 | phy->linkrate = SAS_PHY_DISABLED; |
| 632 | } |
| 633 | |
| 634 | static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr) |
| 635 | { |
| 636 | struct expander_device *ex = &dev->ex_dev; |
| 637 | int i; |
| 638 | |
| 639 | for (i = 0; i < ex->num_phys; i++) { |
| 640 | struct ex_phy *phy = &ex->ex_phy[i]; |
| 641 | |
| 642 | if (phy->phy_state == PHY_VACANT || |
| 643 | phy->phy_state == PHY_NOT_PRESENT) |
| 644 | continue; |
| 645 | |
| 646 | if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr)) |
| 647 | sas_ex_disable_phy(dev, i); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | static int sas_dev_present_in_domain(struct asd_sas_port *port, |
| 652 | u8 *sas_addr) |
| 653 | { |
| 654 | struct domain_device *dev; |
| 655 | |
| 656 | if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr)) |
| 657 | return 1; |
| 658 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { |
| 659 | if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr)) |
| 660 | return 1; |
| 661 | } |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | #define RPEL_REQ_SIZE 16 |
| 666 | #define RPEL_RESP_SIZE 32 |
| 667 | int sas_smp_get_phy_events(struct sas_phy *phy) |
| 668 | { |
| 669 | int res; |
| 670 | u8 *req; |
| 671 | u8 *resp; |
| 672 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); |
| 673 | struct domain_device *dev = sas_find_dev_by_rphy(rphy); |
| 674 | |
| 675 | req = alloc_smp_req(RPEL_REQ_SIZE); |
| 676 | if (!req) |
| 677 | return -ENOMEM; |
| 678 | |
| 679 | resp = alloc_smp_resp(RPEL_RESP_SIZE); |
| 680 | if (!resp) { |
| 681 | kfree(req); |
| 682 | return -ENOMEM; |
| 683 | } |
| 684 | |
| 685 | req[1] = SMP_REPORT_PHY_ERR_LOG; |
| 686 | req[9] = phy->number; |
| 687 | |
| 688 | res = smp_execute_task(dev, req, RPEL_REQ_SIZE, |
| 689 | resp, RPEL_RESP_SIZE); |
| 690 | |
| 691 | if (res) |
| 692 | goto out; |
| 693 | |
| 694 | phy->invalid_dword_count = get_unaligned_be32(&resp[12]); |
| 695 | phy->running_disparity_error_count = get_unaligned_be32(&resp[16]); |
| 696 | phy->loss_of_dword_sync_count = get_unaligned_be32(&resp[20]); |
| 697 | phy->phy_reset_problem_count = get_unaligned_be32(&resp[24]); |
| 698 | |
| 699 | out: |
| 700 | kfree(req); |
| 701 | kfree(resp); |
| 702 | return res; |
| 703 | |
| 704 | } |
| 705 | |
| 706 | #ifdef CONFIG_SCSI_SAS_ATA |
| 707 | |
| 708 | #define RPS_REQ_SIZE 16 |
| 709 | #define RPS_RESP_SIZE sizeof(struct smp_rps_resp) |
| 710 | |
| 711 | int sas_get_report_phy_sata(struct domain_device *dev, int phy_id, |
| 712 | struct smp_rps_resp *rps_resp) |
| 713 | { |
| 714 | int res; |
| 715 | u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE); |
| 716 | u8 *resp = (u8 *)rps_resp; |
| 717 | |
| 718 | if (!rps_req) |
| 719 | return -ENOMEM; |
| 720 | |
| 721 | rps_req[1] = SMP_REPORT_PHY_SATA; |
| 722 | rps_req[9] = phy_id; |
| 723 | |
| 724 | res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE, |
| 725 | rps_resp, RPS_RESP_SIZE); |
| 726 | |
| 727 | /* 0x34 is the FIS type for the D2H fis. There's a potential |
| 728 | * standards cockup here. sas-2 explicitly specifies the FIS |
| 729 | * should be encoded so that FIS type is in resp[24]. |
| 730 | * However, some expanders endian reverse this. Undo the |
| 731 | * reversal here */ |
| 732 | if (!res && resp[27] == 0x34 && resp[24] != 0x34) { |
| 733 | int i; |
| 734 | |
| 735 | for (i = 0; i < 5; i++) { |
| 736 | int j = 24 + (i*4); |
| 737 | u8 a, b; |
| 738 | a = resp[j + 0]; |
| 739 | b = resp[j + 1]; |
| 740 | resp[j + 0] = resp[j + 3]; |
| 741 | resp[j + 1] = resp[j + 2]; |
| 742 | resp[j + 2] = b; |
| 743 | resp[j + 3] = a; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | kfree(rps_req); |
| 748 | return res; |
| 749 | } |
| 750 | #endif |
| 751 | |
| 752 | static void sas_ex_get_linkrate(struct domain_device *parent, |
| 753 | struct domain_device *child, |
| 754 | struct ex_phy *parent_phy) |
| 755 | { |
| 756 | struct expander_device *parent_ex = &parent->ex_dev; |
| 757 | struct sas_port *port; |
| 758 | int i; |
| 759 | |
| 760 | child->pathways = 0; |
| 761 | |
| 762 | port = parent_phy->port; |
| 763 | |
| 764 | for (i = 0; i < parent_ex->num_phys; i++) { |
| 765 | struct ex_phy *phy = &parent_ex->ex_phy[i]; |
| 766 | |
| 767 | if (phy->phy_state == PHY_VACANT || |
| 768 | phy->phy_state == PHY_NOT_PRESENT) |
| 769 | continue; |
| 770 | |
| 771 | if (sas_phy_match_dev_addr(child, phy)) { |
| 772 | child->min_linkrate = min(parent->min_linkrate, |
| 773 | phy->linkrate); |
| 774 | child->max_linkrate = max(parent->max_linkrate, |
| 775 | phy->linkrate); |
| 776 | child->pathways++; |
| 777 | sas_port_add_phy(port, phy->phy); |
| 778 | } |
| 779 | } |
| 780 | child->linkrate = min(parent_phy->linkrate, child->max_linkrate); |
| 781 | child->pathways = min(child->pathways, parent->pathways); |
| 782 | } |
| 783 | |
| 784 | static int sas_ex_add_dev(struct domain_device *parent, struct ex_phy *phy, |
| 785 | struct domain_device *child, int phy_id) |
| 786 | { |
| 787 | struct sas_rphy *rphy; |
| 788 | int res; |
| 789 | |
| 790 | child->dev_type = SAS_END_DEVICE; |
| 791 | rphy = sas_end_device_alloc(phy->port); |
| 792 | if (!rphy) |
| 793 | return -ENOMEM; |
| 794 | |
| 795 | child->tproto = phy->attached_tproto; |
| 796 | sas_init_dev(child); |
| 797 | |
| 798 | child->rphy = rphy; |
| 799 | get_device(&rphy->dev); |
| 800 | rphy->identify.phy_identifier = phy_id; |
| 801 | sas_fill_in_rphy(child, rphy); |
| 802 | |
| 803 | list_add_tail(&child->disco_list_node, &parent->port->disco_list); |
| 804 | |
| 805 | res = sas_notify_lldd_dev_found(child); |
| 806 | if (res) { |
| 807 | pr_notice("notify lldd for device %016llx at %016llx:%02d returned 0x%x\n", |
| 808 | SAS_ADDR(child->sas_addr), |
| 809 | SAS_ADDR(parent->sas_addr), phy_id, res); |
| 810 | sas_rphy_free(child->rphy); |
| 811 | list_del(&child->disco_list_node); |
| 812 | return res; |
| 813 | } |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static struct domain_device *sas_ex_discover_end_dev( |
| 819 | struct domain_device *parent, int phy_id) |
| 820 | { |
| 821 | struct expander_device *parent_ex = &parent->ex_dev; |
| 822 | struct ex_phy *phy = &parent_ex->ex_phy[phy_id]; |
| 823 | struct domain_device *child = NULL; |
| 824 | int res; |
| 825 | |
| 826 | if (phy->attached_sata_host || phy->attached_sata_ps) |
| 827 | return NULL; |
| 828 | |
| 829 | child = sas_alloc_device(); |
| 830 | if (!child) |
| 831 | return NULL; |
| 832 | |
| 833 | kref_get(&parent->kref); |
| 834 | child->parent = parent; |
| 835 | child->port = parent->port; |
| 836 | child->iproto = phy->attached_iproto; |
| 837 | memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); |
| 838 | sas_hash_addr(child->hashed_sas_addr, child->sas_addr); |
| 839 | if (!phy->port) { |
| 840 | phy->port = sas_port_alloc(&parent->rphy->dev, phy_id); |
| 841 | if (unlikely(!phy->port)) |
| 842 | goto out_err; |
| 843 | if (unlikely(sas_port_add(phy->port) != 0)) { |
| 844 | sas_port_free(phy->port); |
| 845 | goto out_err; |
| 846 | } |
| 847 | } |
| 848 | sas_ex_get_linkrate(parent, child, phy); |
| 849 | sas_device_set_phy(child, phy->port); |
| 850 | |
| 851 | if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) { |
| 852 | res = sas_ata_add_dev(parent, phy, child, phy_id); |
| 853 | } else if (phy->attached_tproto & SAS_PROTOCOL_SSP) { |
| 854 | res = sas_ex_add_dev(parent, phy, child, phy_id); |
| 855 | } else { |
| 856 | pr_notice("target proto 0x%x at %016llx:0x%x not handled\n", |
| 857 | phy->attached_tproto, SAS_ADDR(parent->sas_addr), |
| 858 | phy_id); |
| 859 | res = -ENODEV; |
| 860 | } |
| 861 | |
| 862 | if (res) |
| 863 | goto out_free; |
| 864 | |
| 865 | list_add_tail(&child->siblings, &parent_ex->children); |
| 866 | return child; |
| 867 | |
| 868 | out_free: |
| 869 | sas_port_delete(phy->port); |
| 870 | out_err: |
| 871 | phy->port = NULL; |
| 872 | sas_put_device(child); |
| 873 | return NULL; |
| 874 | } |
| 875 | |
| 876 | /* See if this phy is part of a wide port */ |
| 877 | static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id) |
| 878 | { |
| 879 | struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id]; |
| 880 | int i; |
| 881 | |
| 882 | for (i = 0; i < parent->ex_dev.num_phys; i++) { |
| 883 | struct ex_phy *ephy = &parent->ex_dev.ex_phy[i]; |
| 884 | |
| 885 | if (ephy == phy) |
| 886 | continue; |
| 887 | |
| 888 | if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr, |
| 889 | SAS_ADDR_SIZE) && ephy->port) { |
| 890 | sas_port_add_ex_phy(ephy->port, phy); |
| 891 | return true; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | static struct domain_device *sas_ex_discover_expander( |
| 899 | struct domain_device *parent, int phy_id) |
| 900 | { |
| 901 | struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy); |
| 902 | struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id]; |
| 903 | struct domain_device *child = NULL; |
| 904 | struct sas_rphy *rphy; |
| 905 | struct sas_expander_device *edev; |
| 906 | struct asd_sas_port *port; |
| 907 | int res; |
| 908 | |
| 909 | if (phy->routing_attr == DIRECT_ROUTING) { |
| 910 | pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n", |
| 911 | SAS_ADDR(parent->sas_addr), phy_id, |
| 912 | SAS_ADDR(phy->attached_sas_addr), |
| 913 | phy->attached_phy_id); |
| 914 | return NULL; |
| 915 | } |
| 916 | child = sas_alloc_device(); |
| 917 | if (!child) |
| 918 | return NULL; |
| 919 | |
| 920 | phy->port = sas_port_alloc(&parent->rphy->dev, phy_id); |
| 921 | /* FIXME: better error handling */ |
| 922 | BUG_ON(sas_port_add(phy->port) != 0); |
| 923 | |
| 924 | |
| 925 | switch (phy->attached_dev_type) { |
| 926 | case SAS_EDGE_EXPANDER_DEVICE: |
| 927 | rphy = sas_expander_alloc(phy->port, |
| 928 | SAS_EDGE_EXPANDER_DEVICE); |
| 929 | break; |
| 930 | case SAS_FANOUT_EXPANDER_DEVICE: |
| 931 | rphy = sas_expander_alloc(phy->port, |
| 932 | SAS_FANOUT_EXPANDER_DEVICE); |
| 933 | break; |
| 934 | default: |
| 935 | rphy = NULL; /* shut gcc up */ |
| 936 | BUG(); |
| 937 | } |
| 938 | port = parent->port; |
| 939 | child->rphy = rphy; |
| 940 | get_device(&rphy->dev); |
| 941 | edev = rphy_to_expander_device(rphy); |
| 942 | child->dev_type = phy->attached_dev_type; |
| 943 | kref_get(&parent->kref); |
| 944 | child->parent = parent; |
| 945 | child->port = port; |
| 946 | child->iproto = phy->attached_iproto; |
| 947 | child->tproto = phy->attached_tproto; |
| 948 | memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); |
| 949 | sas_hash_addr(child->hashed_sas_addr, child->sas_addr); |
| 950 | sas_ex_get_linkrate(parent, child, phy); |
| 951 | edev->level = parent_ex->level + 1; |
| 952 | parent->port->disc.max_level = max(parent->port->disc.max_level, |
| 953 | edev->level); |
| 954 | sas_init_dev(child); |
| 955 | sas_fill_in_rphy(child, rphy); |
| 956 | sas_rphy_add(rphy); |
| 957 | |
| 958 | spin_lock_irq(&parent->port->dev_list_lock); |
| 959 | list_add_tail(&child->dev_list_node, &parent->port->dev_list); |
| 960 | spin_unlock_irq(&parent->port->dev_list_lock); |
| 961 | |
| 962 | res = sas_discover_expander(child); |
| 963 | if (res) { |
| 964 | sas_rphy_delete(rphy); |
| 965 | spin_lock_irq(&parent->port->dev_list_lock); |
| 966 | list_del(&child->dev_list_node); |
| 967 | spin_unlock_irq(&parent->port->dev_list_lock); |
| 968 | sas_put_device(child); |
| 969 | sas_port_delete(phy->port); |
| 970 | phy->port = NULL; |
| 971 | return NULL; |
| 972 | } |
| 973 | list_add_tail(&child->siblings, &parent->ex_dev.children); |
| 974 | return child; |
| 975 | } |
| 976 | |
| 977 | static int sas_ex_discover_dev(struct domain_device *dev, int phy_id) |
| 978 | { |
| 979 | struct expander_device *ex = &dev->ex_dev; |
| 980 | struct ex_phy *ex_phy = &ex->ex_phy[phy_id]; |
| 981 | struct domain_device *child = NULL; |
| 982 | int res = 0; |
| 983 | |
| 984 | /* Phy state */ |
| 985 | if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) { |
| 986 | if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL)) |
| 987 | res = sas_ex_phy_discover(dev, phy_id); |
| 988 | if (res) |
| 989 | return res; |
| 990 | } |
| 991 | |
| 992 | /* Parent and domain coherency */ |
| 993 | if (!dev->parent && sas_phy_match_port_addr(dev->port, ex_phy)) { |
| 994 | sas_ex_add_parent_port(dev, phy_id); |
| 995 | return 0; |
| 996 | } |
| 997 | if (dev->parent && sas_phy_match_dev_addr(dev->parent, ex_phy)) { |
| 998 | sas_ex_add_parent_port(dev, phy_id); |
| 999 | if (ex_phy->routing_attr == TABLE_ROUTING) |
| 1000 | sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr)) |
| 1005 | sas_ex_disable_port(dev, ex_phy->attached_sas_addr); |
| 1006 | |
| 1007 | if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) { |
| 1008 | if (ex_phy->routing_attr == DIRECT_ROUTING) { |
| 1009 | memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE); |
| 1010 | sas_configure_routing(dev, ex_phy->attached_sas_addr); |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN) |
| 1014 | return 0; |
| 1015 | |
| 1016 | if (ex_phy->attached_dev_type != SAS_END_DEVICE && |
| 1017 | ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE && |
| 1018 | ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE && |
| 1019 | ex_phy->attached_dev_type != SAS_SATA_PENDING) { |
| 1020 | pr_warn("unknown device type(0x%x) attached to ex %016llx phy%02d\n", |
| 1021 | ex_phy->attached_dev_type, |
| 1022 | SAS_ADDR(dev->sas_addr), |
| 1023 | phy_id); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | res = sas_configure_routing(dev, ex_phy->attached_sas_addr); |
| 1028 | if (res) { |
| 1029 | pr_notice("configure routing for dev %016llx reported 0x%x. Forgotten\n", |
| 1030 | SAS_ADDR(ex_phy->attached_sas_addr), res); |
| 1031 | sas_disable_routing(dev, ex_phy->attached_sas_addr); |
| 1032 | return res; |
| 1033 | } |
| 1034 | |
| 1035 | if (sas_ex_join_wide_port(dev, phy_id)) { |
| 1036 | pr_debug("Attaching ex phy%02d to wide port %016llx\n", |
| 1037 | phy_id, SAS_ADDR(ex_phy->attached_sas_addr)); |
| 1038 | return res; |
| 1039 | } |
| 1040 | |
| 1041 | switch (ex_phy->attached_dev_type) { |
| 1042 | case SAS_END_DEVICE: |
| 1043 | case SAS_SATA_PENDING: |
| 1044 | child = sas_ex_discover_end_dev(dev, phy_id); |
| 1045 | break; |
| 1046 | case SAS_FANOUT_EXPANDER_DEVICE: |
| 1047 | if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) { |
| 1048 | pr_debug("second fanout expander %016llx phy%02d attached to ex %016llx phy%02d\n", |
| 1049 | SAS_ADDR(ex_phy->attached_sas_addr), |
| 1050 | ex_phy->attached_phy_id, |
| 1051 | SAS_ADDR(dev->sas_addr), |
| 1052 | phy_id); |
| 1053 | sas_ex_disable_phy(dev, phy_id); |
| 1054 | return res; |
| 1055 | } else |
| 1056 | memcpy(dev->port->disc.fanout_sas_addr, |
| 1057 | ex_phy->attached_sas_addr, SAS_ADDR_SIZE); |
| 1058 | fallthrough; |
| 1059 | case SAS_EDGE_EXPANDER_DEVICE: |
| 1060 | child = sas_ex_discover_expander(dev, phy_id); |
| 1061 | break; |
| 1062 | default: |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | if (!child) |
| 1067 | pr_notice("ex %016llx phy%02d failed to discover\n", |
| 1068 | SAS_ADDR(dev->sas_addr), phy_id); |
| 1069 | return res; |
| 1070 | } |
| 1071 | |
| 1072 | static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr) |
| 1073 | { |
| 1074 | struct expander_device *ex = &dev->ex_dev; |
| 1075 | int i; |
| 1076 | |
| 1077 | for (i = 0; i < ex->num_phys; i++) { |
| 1078 | struct ex_phy *phy = &ex->ex_phy[i]; |
| 1079 | |
| 1080 | if (phy->phy_state == PHY_VACANT || |
| 1081 | phy->phy_state == PHY_NOT_PRESENT) |
| 1082 | continue; |
| 1083 | |
| 1084 | if (dev_is_expander(phy->attached_dev_type) && |
| 1085 | phy->routing_attr == SUBTRACTIVE_ROUTING) { |
| 1086 | |
| 1087 | memcpy(sub_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); |
| 1088 | |
| 1089 | return 1; |
| 1090 | } |
| 1091 | } |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | static int sas_check_level_subtractive_boundary(struct domain_device *dev) |
| 1096 | { |
| 1097 | struct expander_device *ex = &dev->ex_dev; |
| 1098 | struct domain_device *child; |
| 1099 | u8 sub_addr[SAS_ADDR_SIZE] = {0, }; |
| 1100 | |
| 1101 | list_for_each_entry(child, &ex->children, siblings) { |
| 1102 | if (!dev_is_expander(child->dev_type)) |
| 1103 | continue; |
| 1104 | if (sub_addr[0] == 0) { |
| 1105 | sas_find_sub_addr(child, sub_addr); |
| 1106 | continue; |
| 1107 | } else { |
| 1108 | u8 s2[SAS_ADDR_SIZE]; |
| 1109 | |
| 1110 | if (sas_find_sub_addr(child, s2) && |
| 1111 | (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) { |
| 1112 | |
| 1113 | pr_notice("ex %016llx->%016llx-?->%016llx diverges from subtractive boundary %016llx\n", |
| 1114 | SAS_ADDR(dev->sas_addr), |
| 1115 | SAS_ADDR(child->sas_addr), |
| 1116 | SAS_ADDR(s2), |
| 1117 | SAS_ADDR(sub_addr)); |
| 1118 | |
| 1119 | sas_ex_disable_port(child, s2); |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | /** |
| 1126 | * sas_ex_discover_devices - discover devices attached to this expander |
| 1127 | * @dev: pointer to the expander domain device |
| 1128 | * @single: if you want to do a single phy, else set to -1; |
| 1129 | * |
| 1130 | * Configure this expander for use with its devices and register the |
| 1131 | * devices of this expander. |
| 1132 | */ |
| 1133 | static int sas_ex_discover_devices(struct domain_device *dev, int single) |
| 1134 | { |
| 1135 | struct expander_device *ex = &dev->ex_dev; |
| 1136 | int i = 0, end = ex->num_phys; |
| 1137 | int res = 0; |
| 1138 | |
| 1139 | if (0 <= single && single < end) { |
| 1140 | i = single; |
| 1141 | end = i+1; |
| 1142 | } |
| 1143 | |
| 1144 | for ( ; i < end; i++) { |
| 1145 | struct ex_phy *ex_phy = &ex->ex_phy[i]; |
| 1146 | |
| 1147 | if (ex_phy->phy_state == PHY_VACANT || |
| 1148 | ex_phy->phy_state == PHY_NOT_PRESENT || |
| 1149 | ex_phy->phy_state == PHY_DEVICE_DISCOVERED) |
| 1150 | continue; |
| 1151 | |
| 1152 | switch (ex_phy->linkrate) { |
| 1153 | case SAS_PHY_DISABLED: |
| 1154 | case SAS_PHY_RESET_PROBLEM: |
| 1155 | case SAS_SATA_PORT_SELECTOR: |
| 1156 | continue; |
| 1157 | default: |
| 1158 | res = sas_ex_discover_dev(dev, i); |
| 1159 | if (res) |
| 1160 | break; |
| 1161 | continue; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | if (!res) |
| 1166 | sas_check_level_subtractive_boundary(dev); |
| 1167 | |
| 1168 | return res; |
| 1169 | } |
| 1170 | |
| 1171 | static int sas_check_ex_subtractive_boundary(struct domain_device *dev) |
| 1172 | { |
| 1173 | struct expander_device *ex = &dev->ex_dev; |
| 1174 | int i; |
| 1175 | u8 *sub_sas_addr = NULL; |
| 1176 | |
| 1177 | if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE) |
| 1178 | return 0; |
| 1179 | |
| 1180 | for (i = 0; i < ex->num_phys; i++) { |
| 1181 | struct ex_phy *phy = &ex->ex_phy[i]; |
| 1182 | |
| 1183 | if (phy->phy_state == PHY_VACANT || |
| 1184 | phy->phy_state == PHY_NOT_PRESENT) |
| 1185 | continue; |
| 1186 | |
| 1187 | if (dev_is_expander(phy->attached_dev_type) && |
| 1188 | phy->routing_attr == SUBTRACTIVE_ROUTING) { |
| 1189 | |
| 1190 | if (!sub_sas_addr) |
| 1191 | sub_sas_addr = &phy->attached_sas_addr[0]; |
| 1192 | else if (SAS_ADDR(sub_sas_addr) != |
| 1193 | SAS_ADDR(phy->attached_sas_addr)) { |
| 1194 | |
| 1195 | pr_notice("ex %016llx phy%02d diverges(%016llx) on subtractive boundary(%016llx). Disabled\n", |
| 1196 | SAS_ADDR(dev->sas_addr), i, |
| 1197 | SAS_ADDR(phy->attached_sas_addr), |
| 1198 | SAS_ADDR(sub_sas_addr)); |
| 1199 | sas_ex_disable_phy(dev, i); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | static void sas_print_parent_topology_bug(struct domain_device *child, |
| 1207 | struct ex_phy *parent_phy, |
| 1208 | struct ex_phy *child_phy) |
| 1209 | { |
| 1210 | static const char *ex_type[] = { |
| 1211 | [SAS_EDGE_EXPANDER_DEVICE] = "edge", |
| 1212 | [SAS_FANOUT_EXPANDER_DEVICE] = "fanout", |
| 1213 | }; |
| 1214 | struct domain_device *parent = child->parent; |
| 1215 | |
| 1216 | pr_notice("%s ex %016llx phy%02d <--> %s ex %016llx phy%02d has %c:%c routing link!\n", |
| 1217 | ex_type[parent->dev_type], |
| 1218 | SAS_ADDR(parent->sas_addr), |
| 1219 | parent_phy->phy_id, |
| 1220 | |
| 1221 | ex_type[child->dev_type], |
| 1222 | SAS_ADDR(child->sas_addr), |
| 1223 | child_phy->phy_id, |
| 1224 | |
| 1225 | sas_route_char(parent, parent_phy), |
| 1226 | sas_route_char(child, child_phy)); |
| 1227 | } |
| 1228 | |
| 1229 | static bool sas_eeds_valid(struct domain_device *parent, |
| 1230 | struct domain_device *child) |
| 1231 | { |
| 1232 | struct sas_discovery *disc = &parent->port->disc; |
| 1233 | |
| 1234 | return (SAS_ADDR(disc->eeds_a) == SAS_ADDR(parent->sas_addr) || |
| 1235 | SAS_ADDR(disc->eeds_a) == SAS_ADDR(child->sas_addr)) && |
| 1236 | (SAS_ADDR(disc->eeds_b) == SAS_ADDR(parent->sas_addr) || |
| 1237 | SAS_ADDR(disc->eeds_b) == SAS_ADDR(child->sas_addr)); |
| 1238 | } |
| 1239 | |
| 1240 | static int sas_check_eeds(struct domain_device *child, |
| 1241 | struct ex_phy *parent_phy, |
| 1242 | struct ex_phy *child_phy) |
| 1243 | { |
| 1244 | int res = 0; |
| 1245 | struct domain_device *parent = child->parent; |
| 1246 | struct sas_discovery *disc = &parent->port->disc; |
| 1247 | |
| 1248 | if (SAS_ADDR(disc->fanout_sas_addr) != 0) { |
| 1249 | res = -ENODEV; |
| 1250 | pr_warn("edge ex %016llx phy S:%02d <--> edge ex %016llx phy S:%02d, while there is a fanout ex %016llx\n", |
| 1251 | SAS_ADDR(parent->sas_addr), |
| 1252 | parent_phy->phy_id, |
| 1253 | SAS_ADDR(child->sas_addr), |
| 1254 | child_phy->phy_id, |
| 1255 | SAS_ADDR(disc->fanout_sas_addr)); |
| 1256 | } else if (SAS_ADDR(disc->eeds_a) == 0) { |
| 1257 | memcpy(disc->eeds_a, parent->sas_addr, SAS_ADDR_SIZE); |
| 1258 | memcpy(disc->eeds_b, child->sas_addr, SAS_ADDR_SIZE); |
| 1259 | } else if (!sas_eeds_valid(parent, child)) { |
| 1260 | res = -ENODEV; |
| 1261 | pr_warn("edge ex %016llx phy%02d <--> edge ex %016llx phy%02d link forms a third EEDS!\n", |
| 1262 | SAS_ADDR(parent->sas_addr), |
| 1263 | parent_phy->phy_id, |
| 1264 | SAS_ADDR(child->sas_addr), |
| 1265 | child_phy->phy_id); |
| 1266 | } |
| 1267 | |
| 1268 | return res; |
| 1269 | } |
| 1270 | |
| 1271 | static int sas_check_edge_expander_topo(struct domain_device *child, |
| 1272 | struct ex_phy *parent_phy) |
| 1273 | { |
| 1274 | struct expander_device *child_ex = &child->ex_dev; |
| 1275 | struct expander_device *parent_ex = &child->parent->ex_dev; |
| 1276 | struct ex_phy *child_phy; |
| 1277 | |
| 1278 | child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id]; |
| 1279 | |
| 1280 | if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) { |
| 1281 | if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING || |
| 1282 | child_phy->routing_attr != TABLE_ROUTING) |
| 1283 | goto error; |
| 1284 | } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) { |
| 1285 | if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) |
| 1286 | return sas_check_eeds(child, parent_phy, child_phy); |
| 1287 | else if (child_phy->routing_attr != TABLE_ROUTING) |
| 1288 | goto error; |
| 1289 | } else if (parent_phy->routing_attr == TABLE_ROUTING) { |
| 1290 | if (child_phy->routing_attr != SUBTRACTIVE_ROUTING && |
| 1291 | (child_phy->routing_attr != TABLE_ROUTING || |
| 1292 | !child_ex->t2t_supp || !parent_ex->t2t_supp)) |
| 1293 | goto error; |
| 1294 | } |
| 1295 | |
| 1296 | return 0; |
| 1297 | error: |
| 1298 | sas_print_parent_topology_bug(child, parent_phy, child_phy); |
| 1299 | return -ENODEV; |
| 1300 | } |
| 1301 | |
| 1302 | static int sas_check_fanout_expander_topo(struct domain_device *child, |
| 1303 | struct ex_phy *parent_phy) |
| 1304 | { |
| 1305 | struct expander_device *child_ex = &child->ex_dev; |
| 1306 | struct ex_phy *child_phy; |
| 1307 | |
| 1308 | child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id]; |
| 1309 | |
| 1310 | if (parent_phy->routing_attr == TABLE_ROUTING && |
| 1311 | child_phy->routing_attr == SUBTRACTIVE_ROUTING) |
| 1312 | return 0; |
| 1313 | |
| 1314 | sas_print_parent_topology_bug(child, parent_phy, child_phy); |
| 1315 | |
| 1316 | return -ENODEV; |
| 1317 | } |
| 1318 | |
| 1319 | static int sas_check_parent_topology(struct domain_device *child) |
| 1320 | { |
| 1321 | struct expander_device *parent_ex; |
| 1322 | int i; |
| 1323 | int res = 0; |
| 1324 | |
| 1325 | if (!dev_parent_is_expander(child)) |
| 1326 | return 0; |
| 1327 | |
| 1328 | parent_ex = &child->parent->ex_dev; |
| 1329 | |
| 1330 | for (i = 0; i < parent_ex->num_phys; i++) { |
| 1331 | struct ex_phy *parent_phy = &parent_ex->ex_phy[i]; |
| 1332 | |
| 1333 | if (parent_phy->phy_state == PHY_VACANT || |
| 1334 | parent_phy->phy_state == PHY_NOT_PRESENT) |
| 1335 | continue; |
| 1336 | |
| 1337 | if (!sas_phy_match_dev_addr(child, parent_phy)) |
| 1338 | continue; |
| 1339 | |
| 1340 | switch (child->parent->dev_type) { |
| 1341 | case SAS_EDGE_EXPANDER_DEVICE: |
| 1342 | if (sas_check_edge_expander_topo(child, parent_phy)) |
| 1343 | res = -ENODEV; |
| 1344 | break; |
| 1345 | case SAS_FANOUT_EXPANDER_DEVICE: |
| 1346 | if (sas_check_fanout_expander_topo(child, parent_phy)) |
| 1347 | res = -ENODEV; |
| 1348 | break; |
| 1349 | default: |
| 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | return res; |
| 1355 | } |
| 1356 | |
| 1357 | #define RRI_REQ_SIZE 16 |
| 1358 | #define RRI_RESP_SIZE 44 |
| 1359 | |
| 1360 | static int sas_configure_present(struct domain_device *dev, int phy_id, |
| 1361 | u8 *sas_addr, int *index, int *present) |
| 1362 | { |
| 1363 | int i, res = 0; |
| 1364 | struct expander_device *ex = &dev->ex_dev; |
| 1365 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 1366 | u8 *rri_req; |
| 1367 | u8 *rri_resp; |
| 1368 | |
| 1369 | *present = 0; |
| 1370 | *index = 0; |
| 1371 | |
| 1372 | rri_req = alloc_smp_req(RRI_REQ_SIZE); |
| 1373 | if (!rri_req) |
| 1374 | return -ENOMEM; |
| 1375 | |
| 1376 | rri_resp = alloc_smp_resp(RRI_RESP_SIZE); |
| 1377 | if (!rri_resp) { |
| 1378 | kfree(rri_req); |
| 1379 | return -ENOMEM; |
| 1380 | } |
| 1381 | |
| 1382 | rri_req[1] = SMP_REPORT_ROUTE_INFO; |
| 1383 | rri_req[9] = phy_id; |
| 1384 | |
| 1385 | for (i = 0; i < ex->max_route_indexes ; i++) { |
| 1386 | *(__be16 *)(rri_req+6) = cpu_to_be16(i); |
| 1387 | res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp, |
| 1388 | RRI_RESP_SIZE); |
| 1389 | if (res) |
| 1390 | goto out; |
| 1391 | res = rri_resp[2]; |
| 1392 | if (res == SMP_RESP_NO_INDEX) { |
| 1393 | pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n", |
| 1394 | SAS_ADDR(dev->sas_addr), phy_id, i); |
| 1395 | goto out; |
| 1396 | } else if (res != SMP_RESP_FUNC_ACC) { |
| 1397 | pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n", |
| 1398 | __func__, SAS_ADDR(dev->sas_addr), phy_id, |
| 1399 | i, res); |
| 1400 | goto out; |
| 1401 | } |
| 1402 | if (SAS_ADDR(sas_addr) != 0) { |
| 1403 | if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) { |
| 1404 | *index = i; |
| 1405 | if ((rri_resp[12] & 0x80) == 0x80) |
| 1406 | *present = 0; |
| 1407 | else |
| 1408 | *present = 1; |
| 1409 | goto out; |
| 1410 | } else if (SAS_ADDR(rri_resp+16) == 0) { |
| 1411 | *index = i; |
| 1412 | *present = 0; |
| 1413 | goto out; |
| 1414 | } |
| 1415 | } else if (SAS_ADDR(rri_resp+16) == 0 && |
| 1416 | phy->last_da_index < i) { |
| 1417 | phy->last_da_index = i; |
| 1418 | *index = i; |
| 1419 | *present = 0; |
| 1420 | goto out; |
| 1421 | } |
| 1422 | } |
| 1423 | res = -1; |
| 1424 | out: |
| 1425 | kfree(rri_req); |
| 1426 | kfree(rri_resp); |
| 1427 | return res; |
| 1428 | } |
| 1429 | |
| 1430 | #define CRI_REQ_SIZE 44 |
| 1431 | #define CRI_RESP_SIZE 8 |
| 1432 | |
| 1433 | static int sas_configure_set(struct domain_device *dev, int phy_id, |
| 1434 | u8 *sas_addr, int index, int include) |
| 1435 | { |
| 1436 | int res; |
| 1437 | u8 *cri_req; |
| 1438 | u8 *cri_resp; |
| 1439 | |
| 1440 | cri_req = alloc_smp_req(CRI_REQ_SIZE); |
| 1441 | if (!cri_req) |
| 1442 | return -ENOMEM; |
| 1443 | |
| 1444 | cri_resp = alloc_smp_resp(CRI_RESP_SIZE); |
| 1445 | if (!cri_resp) { |
| 1446 | kfree(cri_req); |
| 1447 | return -ENOMEM; |
| 1448 | } |
| 1449 | |
| 1450 | cri_req[1] = SMP_CONF_ROUTE_INFO; |
| 1451 | *(__be16 *)(cri_req+6) = cpu_to_be16(index); |
| 1452 | cri_req[9] = phy_id; |
| 1453 | if (SAS_ADDR(sas_addr) == 0 || !include) |
| 1454 | cri_req[12] |= 0x80; |
| 1455 | memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE); |
| 1456 | |
| 1457 | res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp, |
| 1458 | CRI_RESP_SIZE); |
| 1459 | if (res) |
| 1460 | goto out; |
| 1461 | res = cri_resp[2]; |
| 1462 | if (res == SMP_RESP_NO_INDEX) { |
| 1463 | pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n", |
| 1464 | SAS_ADDR(dev->sas_addr), phy_id, index); |
| 1465 | } |
| 1466 | out: |
| 1467 | kfree(cri_req); |
| 1468 | kfree(cri_resp); |
| 1469 | return res; |
| 1470 | } |
| 1471 | |
| 1472 | static int sas_configure_phy(struct domain_device *dev, int phy_id, |
| 1473 | u8 *sas_addr, int include) |
| 1474 | { |
| 1475 | int index; |
| 1476 | int present; |
| 1477 | int res; |
| 1478 | |
| 1479 | res = sas_configure_present(dev, phy_id, sas_addr, &index, &present); |
| 1480 | if (res) |
| 1481 | return res; |
| 1482 | if (include ^ present) |
| 1483 | return sas_configure_set(dev, phy_id, sas_addr, index, |
| 1484 | include); |
| 1485 | |
| 1486 | return res; |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * sas_configure_parent - configure routing table of parent |
| 1491 | * @parent: parent expander |
| 1492 | * @child: child expander |
| 1493 | * @sas_addr: SAS port identifier of device directly attached to child |
| 1494 | * @include: whether or not to include @child in the expander routing table |
| 1495 | */ |
| 1496 | static int sas_configure_parent(struct domain_device *parent, |
| 1497 | struct domain_device *child, |
| 1498 | u8 *sas_addr, int include) |
| 1499 | { |
| 1500 | struct expander_device *ex_parent = &parent->ex_dev; |
| 1501 | int res = 0; |
| 1502 | int i; |
| 1503 | |
| 1504 | if (parent->parent) { |
| 1505 | res = sas_configure_parent(parent->parent, parent, sas_addr, |
| 1506 | include); |
| 1507 | if (res) |
| 1508 | return res; |
| 1509 | } |
| 1510 | |
| 1511 | if (ex_parent->conf_route_table == 0) { |
| 1512 | pr_debug("ex %016llx has self-configuring routing table\n", |
| 1513 | SAS_ADDR(parent->sas_addr)); |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | for (i = 0; i < ex_parent->num_phys; i++) { |
| 1518 | struct ex_phy *phy = &ex_parent->ex_phy[i]; |
| 1519 | |
| 1520 | if ((phy->routing_attr == TABLE_ROUTING) && |
| 1521 | sas_phy_match_dev_addr(child, phy)) { |
| 1522 | res = sas_configure_phy(parent, i, sas_addr, include); |
| 1523 | if (res) |
| 1524 | return res; |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | return res; |
| 1529 | } |
| 1530 | |
| 1531 | /** |
| 1532 | * sas_configure_routing - configure routing |
| 1533 | * @dev: expander device |
| 1534 | * @sas_addr: port identifier of device directly attached to the expander device |
| 1535 | */ |
| 1536 | static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr) |
| 1537 | { |
| 1538 | if (dev->parent) |
| 1539 | return sas_configure_parent(dev->parent, dev, sas_addr, 1); |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | static int sas_disable_routing(struct domain_device *dev, u8 *sas_addr) |
| 1544 | { |
| 1545 | if (dev->parent) |
| 1546 | return sas_configure_parent(dev->parent, dev, sas_addr, 0); |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * sas_discover_expander - expander discovery |
| 1552 | * @dev: pointer to expander domain device |
| 1553 | * |
| 1554 | * See comment in sas_discover_sata(). |
| 1555 | */ |
| 1556 | static int sas_discover_expander(struct domain_device *dev) |
| 1557 | { |
| 1558 | int res; |
| 1559 | |
| 1560 | res = sas_notify_lldd_dev_found(dev); |
| 1561 | if (res) |
| 1562 | return res; |
| 1563 | |
| 1564 | res = sas_ex_general(dev); |
| 1565 | if (res) |
| 1566 | goto out_err; |
| 1567 | res = sas_ex_manuf_info(dev); |
| 1568 | if (res) |
| 1569 | goto out_err; |
| 1570 | |
| 1571 | res = sas_expander_discover(dev); |
| 1572 | if (res) { |
| 1573 | pr_warn("expander %016llx discovery failed(0x%x)\n", |
| 1574 | SAS_ADDR(dev->sas_addr), res); |
| 1575 | goto out_err; |
| 1576 | } |
| 1577 | |
| 1578 | sas_check_ex_subtractive_boundary(dev); |
| 1579 | res = sas_check_parent_topology(dev); |
| 1580 | if (res) |
| 1581 | goto out_err; |
| 1582 | return 0; |
| 1583 | out_err: |
| 1584 | sas_notify_lldd_dev_gone(dev); |
| 1585 | return res; |
| 1586 | } |
| 1587 | |
| 1588 | static int sas_ex_level_discovery(struct asd_sas_port *port, const int level) |
| 1589 | { |
| 1590 | int res = 0; |
| 1591 | struct domain_device *dev; |
| 1592 | |
| 1593 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { |
| 1594 | if (dev_is_expander(dev->dev_type)) { |
| 1595 | struct sas_expander_device *ex = |
| 1596 | rphy_to_expander_device(dev->rphy); |
| 1597 | |
| 1598 | if (level == ex->level) |
| 1599 | res = sas_ex_discover_devices(dev, -1); |
| 1600 | else if (level > 0) |
| 1601 | res = sas_ex_discover_devices(port->port_dev, -1); |
| 1602 | |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return res; |
| 1607 | } |
| 1608 | |
| 1609 | static int sas_ex_bfs_disc(struct asd_sas_port *port) |
| 1610 | { |
| 1611 | int res; |
| 1612 | int level; |
| 1613 | |
| 1614 | do { |
| 1615 | level = port->disc.max_level; |
| 1616 | res = sas_ex_level_discovery(port, level); |
| 1617 | mb(); |
| 1618 | } while (level < port->disc.max_level); |
| 1619 | |
| 1620 | return res; |
| 1621 | } |
| 1622 | |
| 1623 | int sas_discover_root_expander(struct domain_device *dev) |
| 1624 | { |
| 1625 | int res; |
| 1626 | struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy); |
| 1627 | |
| 1628 | res = sas_rphy_add(dev->rphy); |
| 1629 | if (res) |
| 1630 | goto out_err; |
| 1631 | |
| 1632 | ex->level = dev->port->disc.max_level; /* 0 */ |
| 1633 | res = sas_discover_expander(dev); |
| 1634 | if (res) |
| 1635 | goto out_err2; |
| 1636 | |
| 1637 | sas_ex_bfs_disc(dev->port); |
| 1638 | |
| 1639 | return res; |
| 1640 | |
| 1641 | out_err2: |
| 1642 | sas_rphy_remove(dev->rphy); |
| 1643 | out_err: |
| 1644 | return res; |
| 1645 | } |
| 1646 | |
| 1647 | /* ---------- Domain revalidation ---------- */ |
| 1648 | |
| 1649 | static void sas_get_sas_addr_and_dev_type(struct smp_disc_resp *disc_resp, |
| 1650 | u8 *sas_addr, |
| 1651 | enum sas_device_type *type) |
| 1652 | { |
| 1653 | memcpy(sas_addr, disc_resp->disc.attached_sas_addr, SAS_ADDR_SIZE); |
| 1654 | *type = to_dev_type(&disc_resp->disc); |
| 1655 | if (*type == SAS_PHY_UNUSED) |
| 1656 | memset(sas_addr, 0, SAS_ADDR_SIZE); |
| 1657 | } |
| 1658 | |
| 1659 | static int sas_get_phy_discover(struct domain_device *dev, |
| 1660 | int phy_id, struct smp_disc_resp *disc_resp) |
| 1661 | { |
| 1662 | int res; |
| 1663 | u8 *disc_req; |
| 1664 | |
| 1665 | disc_req = alloc_smp_req(DISCOVER_REQ_SIZE); |
| 1666 | if (!disc_req) |
| 1667 | return -ENOMEM; |
| 1668 | |
| 1669 | disc_req[1] = SMP_DISCOVER; |
| 1670 | disc_req[9] = phy_id; |
| 1671 | |
| 1672 | res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE, |
| 1673 | disc_resp, DISCOVER_RESP_SIZE); |
| 1674 | if (res) |
| 1675 | goto out; |
| 1676 | if (disc_resp->result != SMP_RESP_FUNC_ACC) |
| 1677 | res = disc_resp->result; |
| 1678 | out: |
| 1679 | kfree(disc_req); |
| 1680 | return res; |
| 1681 | } |
| 1682 | |
| 1683 | static int sas_get_phy_change_count(struct domain_device *dev, |
| 1684 | int phy_id, int *pcc) |
| 1685 | { |
| 1686 | int res; |
| 1687 | struct smp_disc_resp *disc_resp; |
| 1688 | |
| 1689 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); |
| 1690 | if (!disc_resp) |
| 1691 | return -ENOMEM; |
| 1692 | |
| 1693 | res = sas_get_phy_discover(dev, phy_id, disc_resp); |
| 1694 | if (!res) |
| 1695 | *pcc = disc_resp->disc.change_count; |
| 1696 | |
| 1697 | kfree(disc_resp); |
| 1698 | return res; |
| 1699 | } |
| 1700 | |
| 1701 | int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id, |
| 1702 | u8 *sas_addr, enum sas_device_type *type) |
| 1703 | { |
| 1704 | int res; |
| 1705 | struct smp_disc_resp *disc_resp; |
| 1706 | |
| 1707 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); |
| 1708 | if (!disc_resp) |
| 1709 | return -ENOMEM; |
| 1710 | |
| 1711 | res = sas_get_phy_discover(dev, phy_id, disc_resp); |
| 1712 | if (res == 0) |
| 1713 | sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, type); |
| 1714 | kfree(disc_resp); |
| 1715 | return res; |
| 1716 | } |
| 1717 | |
| 1718 | static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id, |
| 1719 | int from_phy, bool update) |
| 1720 | { |
| 1721 | struct expander_device *ex = &dev->ex_dev; |
| 1722 | int res = 0; |
| 1723 | int i; |
| 1724 | |
| 1725 | for (i = from_phy; i < ex->num_phys; i++) { |
| 1726 | int phy_change_count = 0; |
| 1727 | |
| 1728 | res = sas_get_phy_change_count(dev, i, &phy_change_count); |
| 1729 | switch (res) { |
| 1730 | case SMP_RESP_PHY_VACANT: |
| 1731 | case SMP_RESP_NO_PHY: |
| 1732 | continue; |
| 1733 | case SMP_RESP_FUNC_ACC: |
| 1734 | break; |
| 1735 | default: |
| 1736 | return res; |
| 1737 | } |
| 1738 | |
| 1739 | if (phy_change_count != ex->ex_phy[i].phy_change_count) { |
| 1740 | if (update) |
| 1741 | ex->ex_phy[i].phy_change_count = |
| 1742 | phy_change_count; |
| 1743 | *phy_id = i; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | } |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | static int sas_get_ex_change_count(struct domain_device *dev, int *ecc) |
| 1751 | { |
| 1752 | int res; |
| 1753 | u8 *rg_req; |
| 1754 | struct smp_rg_resp *rg_resp; |
| 1755 | |
| 1756 | rg_req = alloc_smp_req(RG_REQ_SIZE); |
| 1757 | if (!rg_req) |
| 1758 | return -ENOMEM; |
| 1759 | |
| 1760 | rg_resp = alloc_smp_resp(RG_RESP_SIZE); |
| 1761 | if (!rg_resp) { |
| 1762 | kfree(rg_req); |
| 1763 | return -ENOMEM; |
| 1764 | } |
| 1765 | |
| 1766 | rg_req[1] = SMP_REPORT_GENERAL; |
| 1767 | |
| 1768 | res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp, |
| 1769 | RG_RESP_SIZE); |
| 1770 | if (res) |
| 1771 | goto out; |
| 1772 | if (rg_resp->result != SMP_RESP_FUNC_ACC) { |
| 1773 | res = rg_resp->result; |
| 1774 | goto out; |
| 1775 | } |
| 1776 | |
| 1777 | *ecc = be16_to_cpu(rg_resp->rg.change_count); |
| 1778 | out: |
| 1779 | kfree(rg_resp); |
| 1780 | kfree(rg_req); |
| 1781 | return res; |
| 1782 | } |
| 1783 | /** |
| 1784 | * sas_find_bcast_dev - find the device issue BROADCAST(CHANGE). |
| 1785 | * @dev:domain device to be detect. |
| 1786 | * @src_dev: the device which originated BROADCAST(CHANGE). |
| 1787 | * |
| 1788 | * Add self-configuration expander support. Suppose two expander cascading, |
| 1789 | * when the first level expander is self-configuring, hotplug the disks in |
| 1790 | * second level expander, BROADCAST(CHANGE) will not only be originated |
| 1791 | * in the second level expander, but also be originated in the first level |
| 1792 | * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say, |
| 1793 | * expander changed count in two level expanders will all increment at least |
| 1794 | * once, but the phy which chang count has changed is the source device which |
| 1795 | * we concerned. |
| 1796 | */ |
| 1797 | |
| 1798 | static int sas_find_bcast_dev(struct domain_device *dev, |
| 1799 | struct domain_device **src_dev) |
| 1800 | { |
| 1801 | struct expander_device *ex = &dev->ex_dev; |
| 1802 | int ex_change_count = -1; |
| 1803 | int phy_id = -1; |
| 1804 | int res; |
| 1805 | struct domain_device *ch; |
| 1806 | |
| 1807 | res = sas_get_ex_change_count(dev, &ex_change_count); |
| 1808 | if (res) |
| 1809 | goto out; |
| 1810 | if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) { |
| 1811 | /* Just detect if this expander phys phy change count changed, |
| 1812 | * in order to determine if this expander originate BROADCAST, |
| 1813 | * and do not update phy change count field in our structure. |
| 1814 | */ |
| 1815 | res = sas_find_bcast_phy(dev, &phy_id, 0, false); |
| 1816 | if (phy_id != -1) { |
| 1817 | *src_dev = dev; |
| 1818 | ex->ex_change_count = ex_change_count; |
| 1819 | pr_info("ex %016llx phy%02d change count has changed\n", |
| 1820 | SAS_ADDR(dev->sas_addr), phy_id); |
| 1821 | return res; |
| 1822 | } else |
| 1823 | pr_info("ex %016llx phys DID NOT change\n", |
| 1824 | SAS_ADDR(dev->sas_addr)); |
| 1825 | } |
| 1826 | list_for_each_entry(ch, &ex->children, siblings) { |
| 1827 | if (dev_is_expander(ch->dev_type)) { |
| 1828 | res = sas_find_bcast_dev(ch, src_dev); |
| 1829 | if (*src_dev) |
| 1830 | return res; |
| 1831 | } |
| 1832 | } |
| 1833 | out: |
| 1834 | return res; |
| 1835 | } |
| 1836 | |
| 1837 | static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev) |
| 1838 | { |
| 1839 | struct expander_device *ex = &dev->ex_dev; |
| 1840 | struct domain_device *child, *n; |
| 1841 | |
| 1842 | list_for_each_entry_safe(child, n, &ex->children, siblings) { |
| 1843 | set_bit(SAS_DEV_GONE, &child->state); |
| 1844 | if (dev_is_expander(child->dev_type)) |
| 1845 | sas_unregister_ex_tree(port, child); |
| 1846 | else |
| 1847 | sas_unregister_dev(port, child); |
| 1848 | } |
| 1849 | sas_unregister_dev(port, dev); |
| 1850 | } |
| 1851 | |
| 1852 | static void sas_unregister_devs_sas_addr(struct domain_device *parent, |
| 1853 | int phy_id, bool last) |
| 1854 | { |
| 1855 | struct expander_device *ex_dev = &parent->ex_dev; |
| 1856 | struct ex_phy *phy = &ex_dev->ex_phy[phy_id]; |
| 1857 | struct domain_device *child, *n, *found = NULL; |
| 1858 | if (last) { |
| 1859 | list_for_each_entry_safe(child, n, |
| 1860 | &ex_dev->children, siblings) { |
| 1861 | if (sas_phy_match_dev_addr(child, phy)) { |
| 1862 | set_bit(SAS_DEV_GONE, &child->state); |
| 1863 | if (dev_is_expander(child->dev_type)) |
| 1864 | sas_unregister_ex_tree(parent->port, child); |
| 1865 | else |
| 1866 | sas_unregister_dev(parent->port, child); |
| 1867 | found = child; |
| 1868 | break; |
| 1869 | } |
| 1870 | } |
| 1871 | sas_disable_routing(parent, phy->attached_sas_addr); |
| 1872 | } |
| 1873 | memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); |
| 1874 | if (phy->port) { |
| 1875 | sas_port_delete_phy(phy->port, phy->phy); |
| 1876 | sas_device_set_phy(found, phy->port); |
| 1877 | if (phy->port->num_phys == 0) { |
| 1878 | list_add_tail(&phy->port->del_list, |
| 1879 | &parent->port->sas_port_del_list); |
| 1880 | if (ex_dev->parent_port == phy->port) |
| 1881 | ex_dev->parent_port = NULL; |
| 1882 | } |
| 1883 | phy->port = NULL; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | static int sas_discover_bfs_by_root_level(struct domain_device *root, |
| 1888 | const int level) |
| 1889 | { |
| 1890 | struct expander_device *ex_root = &root->ex_dev; |
| 1891 | struct domain_device *child; |
| 1892 | int res = 0; |
| 1893 | |
| 1894 | list_for_each_entry(child, &ex_root->children, siblings) { |
| 1895 | if (dev_is_expander(child->dev_type)) { |
| 1896 | struct sas_expander_device *ex = |
| 1897 | rphy_to_expander_device(child->rphy); |
| 1898 | |
| 1899 | if (level > ex->level) |
| 1900 | res = sas_discover_bfs_by_root_level(child, |
| 1901 | level); |
| 1902 | else if (level == ex->level) |
| 1903 | res = sas_ex_discover_devices(child, -1); |
| 1904 | } |
| 1905 | } |
| 1906 | return res; |
| 1907 | } |
| 1908 | |
| 1909 | static int sas_discover_bfs_by_root(struct domain_device *dev) |
| 1910 | { |
| 1911 | int res; |
| 1912 | struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy); |
| 1913 | int level = ex->level+1; |
| 1914 | |
| 1915 | res = sas_ex_discover_devices(dev, -1); |
| 1916 | if (res) |
| 1917 | goto out; |
| 1918 | do { |
| 1919 | res = sas_discover_bfs_by_root_level(dev, level); |
| 1920 | mb(); |
| 1921 | level += 1; |
| 1922 | } while (level <= dev->port->disc.max_level); |
| 1923 | out: |
| 1924 | return res; |
| 1925 | } |
| 1926 | |
| 1927 | static int sas_discover_new(struct domain_device *dev, int phy_id) |
| 1928 | { |
| 1929 | struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id]; |
| 1930 | struct domain_device *child; |
| 1931 | int res; |
| 1932 | |
| 1933 | pr_debug("ex %016llx phy%02d new device attached\n", |
| 1934 | SAS_ADDR(dev->sas_addr), phy_id); |
| 1935 | res = sas_ex_phy_discover(dev, phy_id); |
| 1936 | if (res) |
| 1937 | return res; |
| 1938 | |
| 1939 | if (sas_ex_join_wide_port(dev, phy_id)) |
| 1940 | return 0; |
| 1941 | |
| 1942 | res = sas_ex_discover_devices(dev, phy_id); |
| 1943 | if (res) |
| 1944 | return res; |
| 1945 | list_for_each_entry(child, &dev->ex_dev.children, siblings) { |
| 1946 | if (sas_phy_match_dev_addr(child, ex_phy)) { |
| 1947 | if (dev_is_expander(child->dev_type)) |
| 1948 | res = sas_discover_bfs_by_root(child); |
| 1949 | break; |
| 1950 | } |
| 1951 | } |
| 1952 | return res; |
| 1953 | } |
| 1954 | |
| 1955 | static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old) |
| 1956 | { |
| 1957 | if (old == new) |
| 1958 | return true; |
| 1959 | |
| 1960 | /* treat device directed resets as flutter, if we went |
| 1961 | * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery |
| 1962 | */ |
| 1963 | if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) || |
| 1964 | (old == SAS_END_DEVICE && new == SAS_SATA_PENDING)) |
| 1965 | return true; |
| 1966 | |
| 1967 | return false; |
| 1968 | } |
| 1969 | |
| 1970 | static void sas_rediscover_ex_phy(struct domain_device *dev, int phy_id, |
| 1971 | bool last) |
| 1972 | { |
| 1973 | struct expander_device *ex = &dev->ex_dev; |
| 1974 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 1975 | |
| 1976 | phy->phy_change_count = -1; |
| 1977 | ex->ex_change_count = -1; |
| 1978 | sas_unregister_devs_sas_addr(dev, phy_id, last); |
| 1979 | sas_discover_event(dev->port, DISCE_REVALIDATE_DOMAIN); |
| 1980 | } |
| 1981 | |
| 1982 | static bool sas_dev_is_flutter(struct domain_device *dev, int phy_id, |
| 1983 | u8 *sas_addr, enum sas_device_type type) |
| 1984 | { |
| 1985 | struct expander_device *ex = &dev->ex_dev; |
| 1986 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 1987 | struct domain_device *child_dev; |
| 1988 | char *action = ""; |
| 1989 | int res; |
| 1990 | |
| 1991 | if (SAS_ADDR(sas_addr) != SAS_ADDR(phy->attached_sas_addr) || |
| 1992 | !dev_type_flutter(type, phy->attached_dev_type)) |
| 1993 | return false; |
| 1994 | |
| 1995 | res = sas_ex_phy_discover(dev, phy_id); |
| 1996 | if (res) |
| 1997 | return false; |
| 1998 | |
| 1999 | child_dev = sas_ex_phy_to_dev(dev, phy_id); |
| 2000 | if (!child_dev) |
| 2001 | goto out; |
| 2002 | |
| 2003 | if (dev_is_sata(child_dev) && |
| 2004 | phy->attached_dev_type == SAS_SATA_PENDING) { |
| 2005 | action = ", needs recovery"; |
| 2006 | goto out; |
| 2007 | } |
| 2008 | |
| 2009 | if (SAS_ADDR(child_dev->sas_addr) != SAS_ADDR(phy->attached_sas_addr)) { |
| 2010 | pr_info("ex %016llx phy%02d sas_addr changed from %016llx to %016llx\n", |
| 2011 | SAS_ADDR(dev->sas_addr), phy_id, |
| 2012 | SAS_ADDR(child_dev->sas_addr), |
| 2013 | SAS_ADDR(phy->attached_sas_addr)); |
| 2014 | /* |
| 2015 | * Device unregistering relies on address matching. Restore |
| 2016 | * attached_sas_addr back to the original address so that the old |
| 2017 | * device can be unregistered later |
| 2018 | */ |
| 2019 | memcpy(phy->attached_sas_addr, child_dev->sas_addr, SAS_ADDR_SIZE); |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | if (child_dev->linkrate != phy->linkrate) { |
| 2024 | pr_info("ex %016llx phy%02d linkrate changed from %d to %d\n", |
| 2025 | SAS_ADDR(dev->sas_addr), phy_id, |
| 2026 | child_dev->linkrate, phy->linkrate); |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
| 2030 | out: |
| 2031 | pr_debug("ex %016llx phy%02d broadcast flutter%s\n", |
| 2032 | SAS_ADDR(dev->sas_addr), phy_id, action); |
| 2033 | return true; |
| 2034 | } |
| 2035 | |
| 2036 | static int sas_rediscover_dev(struct domain_device *dev, int phy_id, |
| 2037 | bool last, int sibling) |
| 2038 | { |
| 2039 | struct expander_device *ex = &dev->ex_dev; |
| 2040 | struct ex_phy *phy = &ex->ex_phy[phy_id]; |
| 2041 | enum sas_device_type type = SAS_PHY_UNUSED; |
| 2042 | struct smp_disc_resp *disc_resp; |
| 2043 | u8 sas_addr[SAS_ADDR_SIZE]; |
| 2044 | char msg[80] = ""; |
| 2045 | int res; |
| 2046 | |
| 2047 | if (!last) |
| 2048 | sprintf(msg, ", part of a wide port with phy%02d", sibling); |
| 2049 | |
| 2050 | pr_debug("ex %016llx rediscovering phy%02d%s\n", |
| 2051 | SAS_ADDR(dev->sas_addr), phy_id, msg); |
| 2052 | |
| 2053 | memset(sas_addr, 0, SAS_ADDR_SIZE); |
| 2054 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); |
| 2055 | if (!disc_resp) |
| 2056 | return -ENOMEM; |
| 2057 | |
| 2058 | res = sas_get_phy_discover(dev, phy_id, disc_resp); |
| 2059 | switch (res) { |
| 2060 | case SMP_RESP_NO_PHY: |
| 2061 | phy->phy_state = PHY_NOT_PRESENT; |
| 2062 | sas_unregister_devs_sas_addr(dev, phy_id, last); |
| 2063 | goto out_free_resp; |
| 2064 | case SMP_RESP_PHY_VACANT: |
| 2065 | phy->phy_state = PHY_VACANT; |
| 2066 | sas_unregister_devs_sas_addr(dev, phy_id, last); |
| 2067 | goto out_free_resp; |
| 2068 | case SMP_RESP_FUNC_ACC: |
| 2069 | break; |
| 2070 | case -ECOMM: |
| 2071 | break; |
| 2072 | default: |
| 2073 | goto out_free_resp; |
| 2074 | } |
| 2075 | |
| 2076 | if (res == 0) |
| 2077 | sas_get_sas_addr_and_dev_type(disc_resp, sas_addr, &type); |
| 2078 | |
| 2079 | if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) { |
| 2080 | phy->phy_state = PHY_EMPTY; |
| 2081 | sas_unregister_devs_sas_addr(dev, phy_id, last); |
| 2082 | /* |
| 2083 | * Even though the PHY is empty, for convenience we update |
| 2084 | * the PHY info, like negotiated linkrate. |
| 2085 | */ |
| 2086 | if (res == 0) |
| 2087 | sas_set_ex_phy(dev, phy_id, disc_resp); |
| 2088 | goto out_free_resp; |
| 2089 | } |
| 2090 | |
| 2091 | if (sas_dev_is_flutter(dev, phy_id, sas_addr, type)) |
| 2092 | goto out_free_resp; |
| 2093 | |
| 2094 | /* we always have to delete the old device when we went here */ |
| 2095 | pr_info("ex %016llx phy%02d replace %016llx\n", |
| 2096 | SAS_ADDR(dev->sas_addr), phy_id, |
| 2097 | SAS_ADDR(phy->attached_sas_addr)); |
| 2098 | sas_rediscover_ex_phy(dev, phy_id, last); |
| 2099 | out_free_resp: |
| 2100 | kfree(disc_resp); |
| 2101 | return res; |
| 2102 | } |
| 2103 | |
| 2104 | /** |
| 2105 | * sas_rediscover - revalidate the domain. |
| 2106 | * @dev:domain device to be detect. |
| 2107 | * @phy_id: the phy id will be detected. |
| 2108 | * |
| 2109 | * NOTE: this process _must_ quit (return) as soon as any connection |
| 2110 | * errors are encountered. Connection recovery is done elsewhere. |
| 2111 | * Discover process only interrogates devices in order to discover the |
| 2112 | * domain.For plugging out, we un-register the device only when it is |
| 2113 | * the last phy in the port, for other phys in this port, we just delete it |
| 2114 | * from the port.For inserting, we do discovery when it is the |
| 2115 | * first phy,for other phys in this port, we add it to the port to |
| 2116 | * forming the wide-port. |
| 2117 | */ |
| 2118 | static int sas_rediscover(struct domain_device *dev, const int phy_id) |
| 2119 | { |
| 2120 | struct expander_device *ex = &dev->ex_dev; |
| 2121 | struct ex_phy *changed_phy = &ex->ex_phy[phy_id]; |
| 2122 | int res = 0; |
| 2123 | int i; |
| 2124 | bool last = true; /* is this the last phy of the port */ |
| 2125 | |
| 2126 | pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n", |
| 2127 | SAS_ADDR(dev->sas_addr), phy_id); |
| 2128 | |
| 2129 | if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) { |
| 2130 | for (i = 0; i < ex->num_phys; i++) { |
| 2131 | struct ex_phy *phy = &ex->ex_phy[i]; |
| 2132 | |
| 2133 | if (i == phy_id) |
| 2134 | continue; |
| 2135 | if (sas_phy_addr_match(phy, changed_phy)) { |
| 2136 | last = false; |
| 2137 | break; |
| 2138 | } |
| 2139 | } |
| 2140 | res = sas_rediscover_dev(dev, phy_id, last, i); |
| 2141 | } else |
| 2142 | res = sas_discover_new(dev, phy_id); |
| 2143 | return res; |
| 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * sas_ex_revalidate_domain - revalidate the domain |
| 2148 | * @port_dev: port domain device. |
| 2149 | * |
| 2150 | * NOTE: this process _must_ quit (return) as soon as any connection |
| 2151 | * errors are encountered. Connection recovery is done elsewhere. |
| 2152 | * Discover process only interrogates devices in order to discover the |
| 2153 | * domain. |
| 2154 | */ |
| 2155 | int sas_ex_revalidate_domain(struct domain_device *port_dev) |
| 2156 | { |
| 2157 | int res; |
| 2158 | struct domain_device *dev = NULL; |
| 2159 | |
| 2160 | res = sas_find_bcast_dev(port_dev, &dev); |
| 2161 | if (res == 0 && dev) { |
| 2162 | struct expander_device *ex = &dev->ex_dev; |
| 2163 | int i = 0, phy_id; |
| 2164 | |
| 2165 | do { |
| 2166 | phy_id = -1; |
| 2167 | res = sas_find_bcast_phy(dev, &phy_id, i, true); |
| 2168 | if (phy_id == -1) |
| 2169 | break; |
| 2170 | res = sas_rediscover(dev, phy_id); |
| 2171 | i = phy_id + 1; |
| 2172 | } while (i < ex->num_phys); |
| 2173 | } |
| 2174 | return res; |
| 2175 | } |
| 2176 | |
| 2177 | int sas_find_attached_phy_id(struct expander_device *ex_dev, |
| 2178 | struct domain_device *dev) |
| 2179 | { |
| 2180 | struct ex_phy *phy; |
| 2181 | int phy_id; |
| 2182 | |
| 2183 | for (phy_id = 0; phy_id < ex_dev->num_phys; phy_id++) { |
| 2184 | phy = &ex_dev->ex_phy[phy_id]; |
| 2185 | if (sas_phy_match_dev_addr(dev, phy)) |
| 2186 | return phy_id; |
| 2187 | } |
| 2188 | |
| 2189 | return -ENODEV; |
| 2190 | } |
| 2191 | EXPORT_SYMBOL_GPL(sas_find_attached_phy_id); |
| 2192 | |
| 2193 | void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost, |
| 2194 | struct sas_rphy *rphy) |
| 2195 | { |
| 2196 | struct domain_device *dev; |
| 2197 | unsigned int rcvlen = 0; |
| 2198 | int ret = -EINVAL; |
| 2199 | |
| 2200 | /* no rphy means no smp target support (ie aic94xx host) */ |
| 2201 | if (!rphy) |
| 2202 | return sas_smp_host_handler(job, shost); |
| 2203 | |
| 2204 | switch (rphy->identify.device_type) { |
| 2205 | case SAS_EDGE_EXPANDER_DEVICE: |
| 2206 | case SAS_FANOUT_EXPANDER_DEVICE: |
| 2207 | break; |
| 2208 | default: |
| 2209 | pr_err("%s: can we send a smp request to a device?\n", |
| 2210 | __func__); |
| 2211 | goto out; |
| 2212 | } |
| 2213 | |
| 2214 | dev = sas_find_dev_by_rphy(rphy); |
| 2215 | if (!dev) { |
| 2216 | pr_err("%s: fail to find a domain_device?\n", __func__); |
| 2217 | goto out; |
| 2218 | } |
| 2219 | |
| 2220 | /* do we need to support multiple segments? */ |
| 2221 | if (job->request_payload.sg_cnt > 1 || |
| 2222 | job->reply_payload.sg_cnt > 1) { |
| 2223 | pr_info("%s: multiple segments req %u, rsp %u\n", |
| 2224 | __func__, job->request_payload.payload_len, |
| 2225 | job->reply_payload.payload_len); |
| 2226 | goto out; |
| 2227 | } |
| 2228 | |
| 2229 | /* The host may have autosuspended, resume it here. */ |
| 2230 | ret = pm_runtime_resume_and_get(dev->port->ha->dev); |
| 2231 | if (ret) |
| 2232 | goto out; |
| 2233 | |
| 2234 | ret = smp_execute_task_sg(dev, job->request_payload.sg_list, |
| 2235 | job->reply_payload.sg_list); |
| 2236 | if (ret >= 0) { |
| 2237 | /* bsg_job_done() requires the length received */ |
| 2238 | rcvlen = job->reply_payload.payload_len - ret; |
| 2239 | ret = 0; |
| 2240 | } |
| 2241 | pm_runtime_put(dev->port->ha->dev); |
| 2242 | |
| 2243 | out: |
| 2244 | bsg_job_done(job, ret, rcvlen); |
| 2245 | } |