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