1 /* 2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers 3 * 4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_transport.c 5 * Copyright (C) 2012-2014 LSI Corporation 6 * Copyright (C) 2013-2014 Avago Technologies 7 * (mailto: MPT-FusionLinux.pdl@avagotech.com) 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 11 * as published by the Free Software Foundation; either version 2 12 * of the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * NO WARRANTY 20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR 21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT 22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is 24 * solely responsible for determining the appropriateness of using and 25 * distributing the Program and assumes all risks associated with its 26 * exercise of rights under this Agreement, including but not limited to 27 * the risks and costs of program errors, damage to or loss of data, 28 * programs or equipment, and unavailability or interruption of operations. 29 30 * DISCLAIMER OF LIABILITY 31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY 32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND 34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES 38 39 * You should have received a copy of the GNU General Public License 40 * along with this program; if not, write to the Free Software 41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 42 * USA. 43 */ 44 45 #include <linux/module.h> 46 #include <linux/kernel.h> 47 #include <linux/init.h> 48 #include <linux/errno.h> 49 #include <linux/sched.h> 50 #include <linux/workqueue.h> 51 #include <linux/delay.h> 52 #include <linux/pci.h> 53 54 #include <scsi/scsi.h> 55 #include <scsi/scsi_cmnd.h> 56 #include <scsi/scsi_device.h> 57 #include <scsi/scsi_host.h> 58 #include <scsi/scsi_transport_sas.h> 59 #include <scsi/scsi_dbg.h> 60 61 #include "mpt3sas_base.h" 62 63 /** 64 * _transport_sas_node_find_by_sas_address - sas node search 65 * @ioc: per adapter object 66 * @sas_address: sas address of expander or sas host 67 * Context: Calling function should acquire ioc->sas_node_lock. 68 * 69 * Search for either hba phys or expander device based on handle, then returns 70 * the sas_node object. 71 */ 72 static struct _sas_node * 73 _transport_sas_node_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc, 74 u64 sas_address) 75 { 76 if (ioc->sas_hba.sas_address == sas_address) 77 return &ioc->sas_hba; 78 else 79 return mpt3sas_scsih_expander_find_by_sas_address(ioc, 80 sas_address); 81 } 82 83 /** 84 * _transport_convert_phy_link_rate - 85 * @link_rate: link rate returned from mpt firmware 86 * 87 * Convert link_rate from mpi fusion into sas_transport form. 88 */ 89 static enum sas_linkrate 90 _transport_convert_phy_link_rate(u8 link_rate) 91 { 92 enum sas_linkrate rc; 93 94 switch (link_rate) { 95 case MPI2_SAS_NEG_LINK_RATE_1_5: 96 rc = SAS_LINK_RATE_1_5_GBPS; 97 break; 98 case MPI2_SAS_NEG_LINK_RATE_3_0: 99 rc = SAS_LINK_RATE_3_0_GBPS; 100 break; 101 case MPI2_SAS_NEG_LINK_RATE_6_0: 102 rc = SAS_LINK_RATE_6_0_GBPS; 103 break; 104 case MPI25_SAS_NEG_LINK_RATE_12_0: 105 rc = SAS_LINK_RATE_12_0_GBPS; 106 break; 107 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED: 108 rc = SAS_PHY_DISABLED; 109 break; 110 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED: 111 rc = SAS_LINK_RATE_FAILED; 112 break; 113 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR: 114 rc = SAS_SATA_PORT_SELECTOR; 115 break; 116 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS: 117 rc = SAS_PHY_RESET_IN_PROGRESS; 118 break; 119 120 default: 121 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE: 122 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE: 123 rc = SAS_LINK_RATE_UNKNOWN; 124 break; 125 } 126 return rc; 127 } 128 129 /** 130 * _transport_set_identify - set identify for phys and end devices 131 * @ioc: per adapter object 132 * @handle: device handle 133 * @identify: sas identify info 134 * 135 * Populates sas identify info. 136 * 137 * Returns 0 for success, non-zero for failure. 138 */ 139 static int 140 _transport_set_identify(struct MPT3SAS_ADAPTER *ioc, u16 handle, 141 struct sas_identify *identify) 142 { 143 Mpi2SasDevicePage0_t sas_device_pg0; 144 Mpi2ConfigReply_t mpi_reply; 145 u32 device_info; 146 u32 ioc_status; 147 148 if (ioc->shost_recovery || ioc->pci_error_recovery) { 149 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n", 150 __func__, ioc->name); 151 return -EFAULT; 152 } 153 154 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0, 155 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) { 156 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 157 ioc->name, __FILE__, __LINE__, __func__); 158 return -ENXIO; 159 } 160 161 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & 162 MPI2_IOCSTATUS_MASK; 163 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) { 164 pr_err(MPT3SAS_FMT 165 "handle(0x%04x), ioc_status(0x%04x)\nfailure at %s:%d/%s()!\n", 166 ioc->name, handle, ioc_status, 167 __FILE__, __LINE__, __func__); 168 return -EIO; 169 } 170 171 memset(identify, 0, sizeof(struct sas_identify)); 172 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo); 173 174 /* sas_address */ 175 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress); 176 177 /* phy number of the parent device this device is linked to */ 178 identify->phy_identifier = sas_device_pg0.PhyNum; 179 180 /* device_type */ 181 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) { 182 case MPI2_SAS_DEVICE_INFO_NO_DEVICE: 183 identify->device_type = SAS_PHY_UNUSED; 184 break; 185 case MPI2_SAS_DEVICE_INFO_END_DEVICE: 186 identify->device_type = SAS_END_DEVICE; 187 break; 188 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER: 189 identify->device_type = SAS_EDGE_EXPANDER_DEVICE; 190 break; 191 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER: 192 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE; 193 break; 194 } 195 196 /* initiator_port_protocols */ 197 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR) 198 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP; 199 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR) 200 identify->initiator_port_protocols |= SAS_PROTOCOL_STP; 201 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR) 202 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP; 203 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST) 204 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA; 205 206 /* target_port_protocols */ 207 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) 208 identify->target_port_protocols |= SAS_PROTOCOL_SSP; 209 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) 210 identify->target_port_protocols |= SAS_PROTOCOL_STP; 211 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET) 212 identify->target_port_protocols |= SAS_PROTOCOL_SMP; 213 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE) 214 identify->target_port_protocols |= SAS_PROTOCOL_SATA; 215 216 return 0; 217 } 218 219 /** 220 * mpt3sas_transport_done - internal transport layer callback handler. 221 * @ioc: per adapter object 222 * @smid: system request message index 223 * @msix_index: MSIX table index supplied by the OS 224 * @reply: reply message frame(lower 32bit addr) 225 * 226 * Callback handler when sending internal generated transport cmds. 227 * The callback index passed is `ioc->transport_cb_idx` 228 * 229 * Return 1 meaning mf should be freed from _base_interrupt 230 * 0 means the mf is freed from this function. 231 */ 232 u8 233 mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, 234 u32 reply) 235 { 236 MPI2DefaultReply_t *mpi_reply; 237 238 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply); 239 if (ioc->transport_cmds.status == MPT3_CMD_NOT_USED) 240 return 1; 241 if (ioc->transport_cmds.smid != smid) 242 return 1; 243 ioc->transport_cmds.status |= MPT3_CMD_COMPLETE; 244 if (mpi_reply) { 245 memcpy(ioc->transport_cmds.reply, mpi_reply, 246 mpi_reply->MsgLength*4); 247 ioc->transport_cmds.status |= MPT3_CMD_REPLY_VALID; 248 } 249 ioc->transport_cmds.status &= ~MPT3_CMD_PENDING; 250 complete(&ioc->transport_cmds.done); 251 return 1; 252 } 253 254 /* report manufacture request structure */ 255 struct rep_manu_request { 256 u8 smp_frame_type; 257 u8 function; 258 u8 reserved; 259 u8 request_length; 260 }; 261 262 /* report manufacture reply structure */ 263 struct rep_manu_reply { 264 u8 smp_frame_type; /* 0x41 */ 265 u8 function; /* 0x01 */ 266 u8 function_result; 267 u8 response_length; 268 u16 expander_change_count; 269 u8 reserved0[2]; 270 u8 sas_format; 271 u8 reserved2[3]; 272 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN]; 273 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN]; 274 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN]; 275 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN]; 276 u16 component_id; 277 u8 component_revision_id; 278 u8 reserved3; 279 u8 vendor_specific[8]; 280 }; 281 282 /** 283 * transport_expander_report_manufacture - obtain SMP report_manufacture 284 * @ioc: per adapter object 285 * @sas_address: expander sas address 286 * @edev: the sas_expander_device object 287 * 288 * Fills in the sas_expander_device object when SMP port is created. 289 * 290 * Returns 0 for success, non-zero for failure. 291 */ 292 static int 293 _transport_expander_report_manufacture(struct MPT3SAS_ADAPTER *ioc, 294 u64 sas_address, struct sas_expander_device *edev) 295 { 296 Mpi2SmpPassthroughRequest_t *mpi_request; 297 Mpi2SmpPassthroughReply_t *mpi_reply; 298 struct rep_manu_reply *manufacture_reply; 299 struct rep_manu_request *manufacture_request; 300 int rc; 301 u16 smid; 302 u32 ioc_state; 303 unsigned long timeleft; 304 void *psge; 305 u8 issue_reset = 0; 306 void *data_out = NULL; 307 dma_addr_t data_out_dma; 308 dma_addr_t data_in_dma; 309 size_t data_in_sz; 310 size_t data_out_sz; 311 u16 wait_state_count; 312 313 if (ioc->shost_recovery || ioc->pci_error_recovery) { 314 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n", 315 __func__, ioc->name); 316 return -EFAULT; 317 } 318 319 mutex_lock(&ioc->transport_cmds.mutex); 320 321 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) { 322 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", 323 ioc->name, __func__); 324 rc = -EAGAIN; 325 goto out; 326 } 327 ioc->transport_cmds.status = MPT3_CMD_PENDING; 328 329 wait_state_count = 0; 330 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 331 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) { 332 if (wait_state_count++ == 10) { 333 pr_err(MPT3SAS_FMT 334 "%s: failed due to ioc not operational\n", 335 ioc->name, __func__); 336 rc = -EFAULT; 337 goto out; 338 } 339 ssleep(1); 340 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 341 pr_info(MPT3SAS_FMT 342 "%s: waiting for operational state(count=%d)\n", 343 ioc->name, __func__, wait_state_count); 344 } 345 if (wait_state_count) 346 pr_info(MPT3SAS_FMT "%s: ioc is operational\n", 347 ioc->name, __func__); 348 349 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx); 350 if (!smid) { 351 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n", 352 ioc->name, __func__); 353 rc = -EAGAIN; 354 goto out; 355 } 356 357 rc = 0; 358 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid); 359 ioc->transport_cmds.smid = smid; 360 361 data_out_sz = sizeof(struct rep_manu_request); 362 data_in_sz = sizeof(struct rep_manu_reply); 363 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz + data_in_sz, 364 &data_out_dma); 365 366 if (!data_out) { 367 pr_err("failure at %s:%d/%s()!\n", __FILE__, 368 __LINE__, __func__); 369 rc = -ENOMEM; 370 mpt3sas_base_free_smid(ioc, smid); 371 goto out; 372 } 373 374 data_in_dma = data_out_dma + sizeof(struct rep_manu_request); 375 376 manufacture_request = data_out; 377 manufacture_request->smp_frame_type = 0x40; 378 manufacture_request->function = 1; 379 manufacture_request->reserved = 0; 380 manufacture_request->request_length = 0; 381 382 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t)); 383 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH; 384 mpi_request->PhysicalPort = 0xFF; 385 mpi_request->SASAddress = cpu_to_le64(sas_address); 386 mpi_request->RequestDataLength = cpu_to_le16(data_out_sz); 387 psge = &mpi_request->SGL; 388 389 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma, 390 data_in_sz); 391 392 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 393 "report_manufacture - send to sas_addr(0x%016llx)\n", 394 ioc->name, (unsigned long long)sas_address)); 395 init_completion(&ioc->transport_cmds.done); 396 mpt3sas_base_put_smid_default(ioc, smid); 397 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done, 398 10*HZ); 399 400 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) { 401 pr_err(MPT3SAS_FMT "%s: timeout\n", 402 ioc->name, __func__); 403 _debug_dump_mf(mpi_request, 404 sizeof(Mpi2SmpPassthroughRequest_t)/4); 405 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) 406 issue_reset = 1; 407 goto issue_host_reset; 408 } 409 410 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 411 "report_manufacture - complete\n", ioc->name)); 412 413 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) { 414 u8 *tmp; 415 416 mpi_reply = ioc->transport_cmds.reply; 417 418 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 419 "report_manufacture - reply data transfer size(%d)\n", 420 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength))); 421 422 if (le16_to_cpu(mpi_reply->ResponseDataLength) != 423 sizeof(struct rep_manu_reply)) 424 goto out; 425 426 manufacture_reply = data_out + sizeof(struct rep_manu_request); 427 strncpy(edev->vendor_id, manufacture_reply->vendor_id, 428 SAS_EXPANDER_VENDOR_ID_LEN); 429 strncpy(edev->product_id, manufacture_reply->product_id, 430 SAS_EXPANDER_PRODUCT_ID_LEN); 431 strncpy(edev->product_rev, manufacture_reply->product_rev, 432 SAS_EXPANDER_PRODUCT_REV_LEN); 433 edev->level = manufacture_reply->sas_format & 1; 434 if (edev->level) { 435 strncpy(edev->component_vendor_id, 436 manufacture_reply->component_vendor_id, 437 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN); 438 tmp = (u8 *)&manufacture_reply->component_id; 439 edev->component_id = tmp[0] << 8 | tmp[1]; 440 edev->component_revision_id = 441 manufacture_reply->component_revision_id; 442 } 443 } else 444 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 445 "report_manufacture - no reply\n", ioc->name)); 446 447 issue_host_reset: 448 if (issue_reset) 449 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, 450 FORCE_BIG_HAMMER); 451 out: 452 ioc->transport_cmds.status = MPT3_CMD_NOT_USED; 453 if (data_out) 454 pci_free_consistent(ioc->pdev, data_out_sz + data_in_sz, 455 data_out, data_out_dma); 456 457 mutex_unlock(&ioc->transport_cmds.mutex); 458 return rc; 459 } 460 461 462 /** 463 * _transport_delete_port - helper function to removing a port 464 * @ioc: per adapter object 465 * @mpt3sas_port: mpt3sas per port object 466 * 467 * Returns nothing. 468 */ 469 static void 470 _transport_delete_port(struct MPT3SAS_ADAPTER *ioc, 471 struct _sas_port *mpt3sas_port) 472 { 473 u64 sas_address = mpt3sas_port->remote_identify.sas_address; 474 enum sas_device_type device_type = 475 mpt3sas_port->remote_identify.device_type; 476 477 dev_printk(KERN_INFO, &mpt3sas_port->port->dev, 478 "remove: sas_addr(0x%016llx)\n", 479 (unsigned long long) sas_address); 480 481 ioc->logging_level |= MPT_DEBUG_TRANSPORT; 482 if (device_type == SAS_END_DEVICE) 483 mpt3sas_device_remove_by_sas_address(ioc, sas_address); 484 else if (device_type == SAS_EDGE_EXPANDER_DEVICE || 485 device_type == SAS_FANOUT_EXPANDER_DEVICE) 486 mpt3sas_expander_remove(ioc, sas_address); 487 ioc->logging_level &= ~MPT_DEBUG_TRANSPORT; 488 } 489 490 /** 491 * _transport_delete_phy - helper function to removing single phy from port 492 * @ioc: per adapter object 493 * @mpt3sas_port: mpt3sas per port object 494 * @mpt3sas_phy: mpt3sas per phy object 495 * 496 * Returns nothing. 497 */ 498 static void 499 _transport_delete_phy(struct MPT3SAS_ADAPTER *ioc, 500 struct _sas_port *mpt3sas_port, struct _sas_phy *mpt3sas_phy) 501 { 502 u64 sas_address = mpt3sas_port->remote_identify.sas_address; 503 504 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev, 505 "remove: sas_addr(0x%016llx), phy(%d)\n", 506 (unsigned long long) sas_address, mpt3sas_phy->phy_id); 507 508 list_del(&mpt3sas_phy->port_siblings); 509 mpt3sas_port->num_phys--; 510 sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy); 511 mpt3sas_phy->phy_belongs_to_port = 0; 512 } 513 514 /** 515 * _transport_add_phy - helper function to adding single phy to port 516 * @ioc: per adapter object 517 * @mpt3sas_port: mpt3sas per port object 518 * @mpt3sas_phy: mpt3sas per phy object 519 * 520 * Returns nothing. 521 */ 522 static void 523 _transport_add_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_port *mpt3sas_port, 524 struct _sas_phy *mpt3sas_phy) 525 { 526 u64 sas_address = mpt3sas_port->remote_identify.sas_address; 527 528 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev, 529 "add: sas_addr(0x%016llx), phy(%d)\n", (unsigned long long) 530 sas_address, mpt3sas_phy->phy_id); 531 532 list_add_tail(&mpt3sas_phy->port_siblings, &mpt3sas_port->phy_list); 533 mpt3sas_port->num_phys++; 534 sas_port_add_phy(mpt3sas_port->port, mpt3sas_phy->phy); 535 mpt3sas_phy->phy_belongs_to_port = 1; 536 } 537 538 /** 539 * _transport_add_phy_to_an_existing_port - adding new phy to existing port 540 * @ioc: per adapter object 541 * @sas_node: sas node object (either expander or sas host) 542 * @mpt3sas_phy: mpt3sas per phy object 543 * @sas_address: sas address of device/expander were phy needs to be added to 544 * 545 * Returns nothing. 546 */ 547 static void 548 _transport_add_phy_to_an_existing_port(struct MPT3SAS_ADAPTER *ioc, 549 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy, 550 u64 sas_address) 551 { 552 struct _sas_port *mpt3sas_port; 553 struct _sas_phy *phy_srch; 554 555 if (mpt3sas_phy->phy_belongs_to_port == 1) 556 return; 557 558 list_for_each_entry(mpt3sas_port, &sas_node->sas_port_list, 559 port_list) { 560 if (mpt3sas_port->remote_identify.sas_address != 561 sas_address) 562 continue; 563 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list, 564 port_siblings) { 565 if (phy_srch == mpt3sas_phy) 566 return; 567 } 568 _transport_add_phy(ioc, mpt3sas_port, mpt3sas_phy); 569 return; 570 } 571 572 } 573 574 /** 575 * _transport_del_phy_from_an_existing_port - delete phy from existing port 576 * @ioc: per adapter object 577 * @sas_node: sas node object (either expander or sas host) 578 * @mpt3sas_phy: mpt3sas per phy object 579 * 580 * Returns nothing. 581 */ 582 static void 583 _transport_del_phy_from_an_existing_port(struct MPT3SAS_ADAPTER *ioc, 584 struct _sas_node *sas_node, struct _sas_phy *mpt3sas_phy) 585 { 586 struct _sas_port *mpt3sas_port, *next; 587 struct _sas_phy *phy_srch; 588 589 if (mpt3sas_phy->phy_belongs_to_port == 0) 590 return; 591 592 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list, 593 port_list) { 594 list_for_each_entry(phy_srch, &mpt3sas_port->phy_list, 595 port_siblings) { 596 if (phy_srch != mpt3sas_phy) 597 continue; 598 599 if (mpt3sas_port->num_phys == 1) 600 _transport_delete_port(ioc, mpt3sas_port); 601 else 602 _transport_delete_phy(ioc, mpt3sas_port, 603 mpt3sas_phy); 604 return; 605 } 606 } 607 } 608 609 /** 610 * _transport_sanity_check - sanity check when adding a new port 611 * @ioc: per adapter object 612 * @sas_node: sas node object (either expander or sas host) 613 * @sas_address: sas address of device being added 614 * 615 * See the explanation above from _transport_delete_duplicate_port 616 */ 617 static void 618 _transport_sanity_check(struct MPT3SAS_ADAPTER *ioc, struct _sas_node *sas_node, 619 u64 sas_address) 620 { 621 int i; 622 623 for (i = 0; i < sas_node->num_phys; i++) { 624 if (sas_node->phy[i].remote_identify.sas_address != sas_address) 625 continue; 626 if (sas_node->phy[i].phy_belongs_to_port == 1) 627 _transport_del_phy_from_an_existing_port(ioc, sas_node, 628 &sas_node->phy[i]); 629 } 630 } 631 632 /** 633 * mpt3sas_transport_port_add - insert port to the list 634 * @ioc: per adapter object 635 * @handle: handle of attached device 636 * @sas_address: sas address of parent expander or sas host 637 * Context: This function will acquire ioc->sas_node_lock. 638 * 639 * Adding new port object to the sas_node->sas_port_list. 640 * 641 * Returns mpt3sas_port. 642 */ 643 struct _sas_port * 644 mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc, u16 handle, 645 u64 sas_address) 646 { 647 struct _sas_phy *mpt3sas_phy, *next; 648 struct _sas_port *mpt3sas_port; 649 unsigned long flags; 650 struct _sas_node *sas_node; 651 struct sas_rphy *rphy; 652 struct _sas_device *sas_device = NULL; 653 int i; 654 struct sas_port *port; 655 656 mpt3sas_port = kzalloc(sizeof(struct _sas_port), 657 GFP_KERNEL); 658 if (!mpt3sas_port) { 659 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 660 ioc->name, __FILE__, __LINE__, __func__); 661 return NULL; 662 } 663 664 INIT_LIST_HEAD(&mpt3sas_port->port_list); 665 INIT_LIST_HEAD(&mpt3sas_port->phy_list); 666 spin_lock_irqsave(&ioc->sas_node_lock, flags); 667 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address); 668 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 669 670 if (!sas_node) { 671 pr_err(MPT3SAS_FMT 672 "%s: Could not find parent sas_address(0x%016llx)!\n", 673 ioc->name, __func__, (unsigned long long)sas_address); 674 goto out_fail; 675 } 676 677 if ((_transport_set_identify(ioc, handle, 678 &mpt3sas_port->remote_identify))) { 679 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 680 ioc->name, __FILE__, __LINE__, __func__); 681 goto out_fail; 682 } 683 684 if (mpt3sas_port->remote_identify.device_type == SAS_PHY_UNUSED) { 685 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 686 ioc->name, __FILE__, __LINE__, __func__); 687 goto out_fail; 688 } 689 690 _transport_sanity_check(ioc, sas_node, 691 mpt3sas_port->remote_identify.sas_address); 692 693 for (i = 0; i < sas_node->num_phys; i++) { 694 if (sas_node->phy[i].remote_identify.sas_address != 695 mpt3sas_port->remote_identify.sas_address) 696 continue; 697 list_add_tail(&sas_node->phy[i].port_siblings, 698 &mpt3sas_port->phy_list); 699 mpt3sas_port->num_phys++; 700 } 701 702 if (!mpt3sas_port->num_phys) { 703 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 704 ioc->name, __FILE__, __LINE__, __func__); 705 goto out_fail; 706 } 707 708 port = sas_port_alloc_num(sas_node->parent_dev); 709 if ((sas_port_add(port))) { 710 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 711 ioc->name, __FILE__, __LINE__, __func__); 712 goto out_fail; 713 } 714 715 list_for_each_entry(mpt3sas_phy, &mpt3sas_port->phy_list, 716 port_siblings) { 717 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 718 dev_printk(KERN_INFO, &port->dev, 719 "add: handle(0x%04x), sas_addr(0x%016llx), phy(%d)\n", 720 handle, (unsigned long long) 721 mpt3sas_port->remote_identify.sas_address, 722 mpt3sas_phy->phy_id); 723 sas_port_add_phy(port, mpt3sas_phy->phy); 724 mpt3sas_phy->phy_belongs_to_port = 1; 725 } 726 727 mpt3sas_port->port = port; 728 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) 729 rphy = sas_end_device_alloc(port); 730 else 731 rphy = sas_expander_alloc(port, 732 mpt3sas_port->remote_identify.device_type); 733 734 rphy->identify = mpt3sas_port->remote_identify; 735 736 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) { 737 sas_device = mpt3sas_get_sdev_by_addr(ioc, 738 mpt3sas_port->remote_identify.sas_address); 739 if (!sas_device) { 740 dfailprintk(ioc, printk(MPT3SAS_FMT 741 "failure at %s:%d/%s()!\n", 742 ioc->name, __FILE__, __LINE__, __func__)); 743 goto out_fail; 744 } 745 sas_device->pend_sas_rphy_add = 1; 746 } 747 748 if ((sas_rphy_add(rphy))) { 749 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 750 ioc->name, __FILE__, __LINE__, __func__); 751 } 752 753 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) { 754 sas_device->pend_sas_rphy_add = 0; 755 sas_device_put(sas_device); 756 } 757 758 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 759 dev_printk(KERN_INFO, &rphy->dev, 760 "add: handle(0x%04x), sas_addr(0x%016llx)\n", 761 handle, (unsigned long long) 762 mpt3sas_port->remote_identify.sas_address); 763 mpt3sas_port->rphy = rphy; 764 spin_lock_irqsave(&ioc->sas_node_lock, flags); 765 list_add_tail(&mpt3sas_port->port_list, &sas_node->sas_port_list); 766 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 767 768 /* fill in report manufacture */ 769 if (mpt3sas_port->remote_identify.device_type == 770 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER || 771 mpt3sas_port->remote_identify.device_type == 772 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) 773 _transport_expander_report_manufacture(ioc, 774 mpt3sas_port->remote_identify.sas_address, 775 rphy_to_expander_device(rphy)); 776 return mpt3sas_port; 777 778 out_fail: 779 list_for_each_entry_safe(mpt3sas_phy, next, &mpt3sas_port->phy_list, 780 port_siblings) 781 list_del(&mpt3sas_phy->port_siblings); 782 kfree(mpt3sas_port); 783 return NULL; 784 } 785 786 /** 787 * mpt3sas_transport_port_remove - remove port from the list 788 * @ioc: per adapter object 789 * @sas_address: sas address of attached device 790 * @sas_address_parent: sas address of parent expander or sas host 791 * Context: This function will acquire ioc->sas_node_lock. 792 * 793 * Removing object and freeing associated memory from the 794 * ioc->sas_port_list. 795 * 796 * Return nothing. 797 */ 798 void 799 mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address, 800 u64 sas_address_parent) 801 { 802 int i; 803 unsigned long flags; 804 struct _sas_port *mpt3sas_port, *next; 805 struct _sas_node *sas_node; 806 u8 found = 0; 807 struct _sas_phy *mpt3sas_phy, *next_phy; 808 809 spin_lock_irqsave(&ioc->sas_node_lock, flags); 810 sas_node = _transport_sas_node_find_by_sas_address(ioc, 811 sas_address_parent); 812 if (!sas_node) { 813 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 814 return; 815 } 816 list_for_each_entry_safe(mpt3sas_port, next, &sas_node->sas_port_list, 817 port_list) { 818 if (mpt3sas_port->remote_identify.sas_address != sas_address) 819 continue; 820 found = 1; 821 list_del(&mpt3sas_port->port_list); 822 goto out; 823 } 824 out: 825 if (!found) { 826 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 827 return; 828 } 829 830 for (i = 0; i < sas_node->num_phys; i++) { 831 if (sas_node->phy[i].remote_identify.sas_address == sas_address) 832 memset(&sas_node->phy[i].remote_identify, 0 , 833 sizeof(struct sas_identify)); 834 } 835 836 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 837 838 list_for_each_entry_safe(mpt3sas_phy, next_phy, 839 &mpt3sas_port->phy_list, port_siblings) { 840 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 841 dev_printk(KERN_INFO, &mpt3sas_port->port->dev, 842 "remove: sas_addr(0x%016llx), phy(%d)\n", 843 (unsigned long long) 844 mpt3sas_port->remote_identify.sas_address, 845 mpt3sas_phy->phy_id); 846 mpt3sas_phy->phy_belongs_to_port = 0; 847 sas_port_delete_phy(mpt3sas_port->port, mpt3sas_phy->phy); 848 list_del(&mpt3sas_phy->port_siblings); 849 } 850 sas_port_delete(mpt3sas_port->port); 851 kfree(mpt3sas_port); 852 } 853 854 /** 855 * mpt3sas_transport_add_host_phy - report sas_host phy to transport 856 * @ioc: per adapter object 857 * @mpt3sas_phy: mpt3sas per phy object 858 * @phy_pg0: sas phy page 0 859 * @parent_dev: parent device class object 860 * 861 * Returns 0 for success, non-zero for failure. 862 */ 863 int 864 mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy 865 *mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev) 866 { 867 struct sas_phy *phy; 868 int phy_index = mpt3sas_phy->phy_id; 869 870 871 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings); 872 phy = sas_phy_alloc(parent_dev, phy_index); 873 if (!phy) { 874 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 875 ioc->name, __FILE__, __LINE__, __func__); 876 return -1; 877 } 878 if ((_transport_set_identify(ioc, mpt3sas_phy->handle, 879 &mpt3sas_phy->identify))) { 880 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 881 ioc->name, __FILE__, __LINE__, __func__); 882 sas_phy_free(phy); 883 return -1; 884 } 885 phy->identify = mpt3sas_phy->identify; 886 mpt3sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle); 887 if (mpt3sas_phy->attached_handle) 888 _transport_set_identify(ioc, mpt3sas_phy->attached_handle, 889 &mpt3sas_phy->remote_identify); 890 phy->identify.phy_identifier = mpt3sas_phy->phy_id; 891 phy->negotiated_linkrate = _transport_convert_phy_link_rate( 892 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL); 893 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate( 894 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK); 895 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate( 896 phy_pg0.HwLinkRate >> 4); 897 phy->minimum_linkrate = _transport_convert_phy_link_rate( 898 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK); 899 phy->maximum_linkrate = _transport_convert_phy_link_rate( 900 phy_pg0.ProgrammedLinkRate >> 4); 901 902 if ((sas_phy_add(phy))) { 903 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 904 ioc->name, __FILE__, __LINE__, __func__); 905 sas_phy_free(phy); 906 return -1; 907 } 908 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 909 dev_printk(KERN_INFO, &phy->dev, 910 "add: handle(0x%04x), sas_addr(0x%016llx)\n" 911 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n", 912 mpt3sas_phy->handle, (unsigned long long) 913 mpt3sas_phy->identify.sas_address, 914 mpt3sas_phy->attached_handle, 915 (unsigned long long) 916 mpt3sas_phy->remote_identify.sas_address); 917 mpt3sas_phy->phy = phy; 918 return 0; 919 } 920 921 922 /** 923 * mpt3sas_transport_add_expander_phy - report expander phy to transport 924 * @ioc: per adapter object 925 * @mpt3sas_phy: mpt3sas per phy object 926 * @expander_pg1: expander page 1 927 * @parent_dev: parent device class object 928 * 929 * Returns 0 for success, non-zero for failure. 930 */ 931 int 932 mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy 933 *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1, 934 struct device *parent_dev) 935 { 936 struct sas_phy *phy; 937 int phy_index = mpt3sas_phy->phy_id; 938 939 INIT_LIST_HEAD(&mpt3sas_phy->port_siblings); 940 phy = sas_phy_alloc(parent_dev, phy_index); 941 if (!phy) { 942 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 943 ioc->name, __FILE__, __LINE__, __func__); 944 return -1; 945 } 946 if ((_transport_set_identify(ioc, mpt3sas_phy->handle, 947 &mpt3sas_phy->identify))) { 948 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 949 ioc->name, __FILE__, __LINE__, __func__); 950 sas_phy_free(phy); 951 return -1; 952 } 953 phy->identify = mpt3sas_phy->identify; 954 mpt3sas_phy->attached_handle = 955 le16_to_cpu(expander_pg1.AttachedDevHandle); 956 if (mpt3sas_phy->attached_handle) 957 _transport_set_identify(ioc, mpt3sas_phy->attached_handle, 958 &mpt3sas_phy->remote_identify); 959 phy->identify.phy_identifier = mpt3sas_phy->phy_id; 960 phy->negotiated_linkrate = _transport_convert_phy_link_rate( 961 expander_pg1.NegotiatedLinkRate & 962 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL); 963 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate( 964 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK); 965 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate( 966 expander_pg1.HwLinkRate >> 4); 967 phy->minimum_linkrate = _transport_convert_phy_link_rate( 968 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK); 969 phy->maximum_linkrate = _transport_convert_phy_link_rate( 970 expander_pg1.ProgrammedLinkRate >> 4); 971 972 if ((sas_phy_add(phy))) { 973 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 974 ioc->name, __FILE__, __LINE__, __func__); 975 sas_phy_free(phy); 976 return -1; 977 } 978 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 979 dev_printk(KERN_INFO, &phy->dev, 980 "add: handle(0x%04x), sas_addr(0x%016llx)\n" 981 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n", 982 mpt3sas_phy->handle, (unsigned long long) 983 mpt3sas_phy->identify.sas_address, 984 mpt3sas_phy->attached_handle, 985 (unsigned long long) 986 mpt3sas_phy->remote_identify.sas_address); 987 mpt3sas_phy->phy = phy; 988 return 0; 989 } 990 991 /** 992 * mpt3sas_transport_update_links - refreshing phy link changes 993 * @ioc: per adapter object 994 * @sas_address: sas address of parent expander or sas host 995 * @handle: attached device handle 996 * @phy_numberv: phy number 997 * @link_rate: new link rate 998 * 999 * Returns nothing. 1000 */ 1001 void 1002 mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc, 1003 u64 sas_address, u16 handle, u8 phy_number, u8 link_rate) 1004 { 1005 unsigned long flags; 1006 struct _sas_node *sas_node; 1007 struct _sas_phy *mpt3sas_phy; 1008 1009 if (ioc->shost_recovery || ioc->pci_error_recovery) 1010 return; 1011 1012 spin_lock_irqsave(&ioc->sas_node_lock, flags); 1013 sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address); 1014 if (!sas_node) { 1015 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1016 return; 1017 } 1018 1019 mpt3sas_phy = &sas_node->phy[phy_number]; 1020 mpt3sas_phy->attached_handle = handle; 1021 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1022 if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)) { 1023 _transport_set_identify(ioc, handle, 1024 &mpt3sas_phy->remote_identify); 1025 _transport_add_phy_to_an_existing_port(ioc, sas_node, 1026 mpt3sas_phy, mpt3sas_phy->remote_identify.sas_address); 1027 } else 1028 memset(&mpt3sas_phy->remote_identify, 0 , sizeof(struct 1029 sas_identify)); 1030 1031 if (mpt3sas_phy->phy) 1032 mpt3sas_phy->phy->negotiated_linkrate = 1033 _transport_convert_phy_link_rate(link_rate); 1034 1035 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT)) 1036 dev_printk(KERN_INFO, &mpt3sas_phy->phy->dev, 1037 "refresh: parent sas_addr(0x%016llx),\n" 1038 "\tlink_rate(0x%02x), phy(%d)\n" 1039 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n", 1040 (unsigned long long)sas_address, 1041 link_rate, phy_number, handle, (unsigned long long) 1042 mpt3sas_phy->remote_identify.sas_address); 1043 } 1044 1045 static inline void * 1046 phy_to_ioc(struct sas_phy *phy) 1047 { 1048 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); 1049 return shost_priv(shost); 1050 } 1051 1052 static inline void * 1053 rphy_to_ioc(struct sas_rphy *rphy) 1054 { 1055 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent); 1056 return shost_priv(shost); 1057 } 1058 1059 /* report phy error log structure */ 1060 struct phy_error_log_request { 1061 u8 smp_frame_type; /* 0x40 */ 1062 u8 function; /* 0x11 */ 1063 u8 allocated_response_length; 1064 u8 request_length; /* 02 */ 1065 u8 reserved_1[5]; 1066 u8 phy_identifier; 1067 u8 reserved_2[2]; 1068 }; 1069 1070 /* report phy error log reply structure */ 1071 struct phy_error_log_reply { 1072 u8 smp_frame_type; /* 0x41 */ 1073 u8 function; /* 0x11 */ 1074 u8 function_result; 1075 u8 response_length; 1076 __be16 expander_change_count; 1077 u8 reserved_1[3]; 1078 u8 phy_identifier; 1079 u8 reserved_2[2]; 1080 __be32 invalid_dword; 1081 __be32 running_disparity_error; 1082 __be32 loss_of_dword_sync; 1083 __be32 phy_reset_problem; 1084 }; 1085 1086 /** 1087 * _transport_get_expander_phy_error_log - return expander counters 1088 * @ioc: per adapter object 1089 * @phy: The sas phy object 1090 * 1091 * Returns 0 for success, non-zero for failure. 1092 * 1093 */ 1094 static int 1095 _transport_get_expander_phy_error_log(struct MPT3SAS_ADAPTER *ioc, 1096 struct sas_phy *phy) 1097 { 1098 Mpi2SmpPassthroughRequest_t *mpi_request; 1099 Mpi2SmpPassthroughReply_t *mpi_reply; 1100 struct phy_error_log_request *phy_error_log_request; 1101 struct phy_error_log_reply *phy_error_log_reply; 1102 int rc; 1103 u16 smid; 1104 u32 ioc_state; 1105 unsigned long timeleft; 1106 void *psge; 1107 u8 issue_reset = 0; 1108 void *data_out = NULL; 1109 dma_addr_t data_out_dma; 1110 u32 sz; 1111 u16 wait_state_count; 1112 1113 if (ioc->shost_recovery || ioc->pci_error_recovery) { 1114 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n", 1115 __func__, ioc->name); 1116 return -EFAULT; 1117 } 1118 1119 mutex_lock(&ioc->transport_cmds.mutex); 1120 1121 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) { 1122 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", 1123 ioc->name, __func__); 1124 rc = -EAGAIN; 1125 goto out; 1126 } 1127 ioc->transport_cmds.status = MPT3_CMD_PENDING; 1128 1129 wait_state_count = 0; 1130 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 1131 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) { 1132 if (wait_state_count++ == 10) { 1133 pr_err(MPT3SAS_FMT 1134 "%s: failed due to ioc not operational\n", 1135 ioc->name, __func__); 1136 rc = -EFAULT; 1137 goto out; 1138 } 1139 ssleep(1); 1140 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 1141 pr_info(MPT3SAS_FMT 1142 "%s: waiting for operational state(count=%d)\n", 1143 ioc->name, __func__, wait_state_count); 1144 } 1145 if (wait_state_count) 1146 pr_info(MPT3SAS_FMT "%s: ioc is operational\n", 1147 ioc->name, __func__); 1148 1149 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx); 1150 if (!smid) { 1151 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n", 1152 ioc->name, __func__); 1153 rc = -EAGAIN; 1154 goto out; 1155 } 1156 1157 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid); 1158 ioc->transport_cmds.smid = smid; 1159 1160 sz = sizeof(struct phy_error_log_request) + 1161 sizeof(struct phy_error_log_reply); 1162 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma); 1163 if (!data_out) { 1164 pr_err("failure at %s:%d/%s()!\n", __FILE__, 1165 __LINE__, __func__); 1166 rc = -ENOMEM; 1167 mpt3sas_base_free_smid(ioc, smid); 1168 goto out; 1169 } 1170 1171 rc = -EINVAL; 1172 memset(data_out, 0, sz); 1173 phy_error_log_request = data_out; 1174 phy_error_log_request->smp_frame_type = 0x40; 1175 phy_error_log_request->function = 0x11; 1176 phy_error_log_request->request_length = 2; 1177 phy_error_log_request->allocated_response_length = 0; 1178 phy_error_log_request->phy_identifier = phy->number; 1179 1180 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t)); 1181 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH; 1182 mpi_request->PhysicalPort = 0xFF; 1183 mpi_request->VF_ID = 0; /* TODO */ 1184 mpi_request->VP_ID = 0; 1185 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address); 1186 mpi_request->RequestDataLength = 1187 cpu_to_le16(sizeof(struct phy_error_log_request)); 1188 psge = &mpi_request->SGL; 1189 1190 ioc->build_sg(ioc, psge, data_out_dma, 1191 sizeof(struct phy_error_log_request), 1192 data_out_dma + sizeof(struct phy_error_log_request), 1193 sizeof(struct phy_error_log_reply)); 1194 1195 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1196 "phy_error_log - send to sas_addr(0x%016llx), phy(%d)\n", 1197 ioc->name, (unsigned long long)phy->identify.sas_address, 1198 phy->number)); 1199 init_completion(&ioc->transport_cmds.done); 1200 mpt3sas_base_put_smid_default(ioc, smid); 1201 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done, 1202 10*HZ); 1203 1204 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) { 1205 pr_err(MPT3SAS_FMT "%s: timeout\n", 1206 ioc->name, __func__); 1207 _debug_dump_mf(mpi_request, 1208 sizeof(Mpi2SmpPassthroughRequest_t)/4); 1209 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) 1210 issue_reset = 1; 1211 goto issue_host_reset; 1212 } 1213 1214 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1215 "phy_error_log - complete\n", ioc->name)); 1216 1217 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) { 1218 1219 mpi_reply = ioc->transport_cmds.reply; 1220 1221 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1222 "phy_error_log - reply data transfer size(%d)\n", 1223 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength))); 1224 1225 if (le16_to_cpu(mpi_reply->ResponseDataLength) != 1226 sizeof(struct phy_error_log_reply)) 1227 goto out; 1228 1229 phy_error_log_reply = data_out + 1230 sizeof(struct phy_error_log_request); 1231 1232 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1233 "phy_error_log - function_result(%d)\n", 1234 ioc->name, phy_error_log_reply->function_result)); 1235 1236 phy->invalid_dword_count = 1237 be32_to_cpu(phy_error_log_reply->invalid_dword); 1238 phy->running_disparity_error_count = 1239 be32_to_cpu(phy_error_log_reply->running_disparity_error); 1240 phy->loss_of_dword_sync_count = 1241 be32_to_cpu(phy_error_log_reply->loss_of_dword_sync); 1242 phy->phy_reset_problem_count = 1243 be32_to_cpu(phy_error_log_reply->phy_reset_problem); 1244 rc = 0; 1245 } else 1246 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1247 "phy_error_log - no reply\n", ioc->name)); 1248 1249 issue_host_reset: 1250 if (issue_reset) 1251 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, 1252 FORCE_BIG_HAMMER); 1253 out: 1254 ioc->transport_cmds.status = MPT3_CMD_NOT_USED; 1255 if (data_out) 1256 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma); 1257 1258 mutex_unlock(&ioc->transport_cmds.mutex); 1259 return rc; 1260 } 1261 1262 /** 1263 * _transport_get_linkerrors - return phy counters for both hba and expanders 1264 * @phy: The sas phy object 1265 * 1266 * Returns 0 for success, non-zero for failure. 1267 * 1268 */ 1269 static int 1270 _transport_get_linkerrors(struct sas_phy *phy) 1271 { 1272 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy); 1273 unsigned long flags; 1274 Mpi2ConfigReply_t mpi_reply; 1275 Mpi2SasPhyPage1_t phy_pg1; 1276 1277 spin_lock_irqsave(&ioc->sas_node_lock, flags); 1278 if (_transport_sas_node_find_by_sas_address(ioc, 1279 phy->identify.sas_address) == NULL) { 1280 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1281 return -EINVAL; 1282 } 1283 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1284 1285 if (phy->identify.sas_address != ioc->sas_hba.sas_address) 1286 return _transport_get_expander_phy_error_log(ioc, phy); 1287 1288 /* get hba phy error logs */ 1289 if ((mpt3sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1, 1290 phy->number))) { 1291 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1292 ioc->name, __FILE__, __LINE__, __func__); 1293 return -ENXIO; 1294 } 1295 1296 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) 1297 pr_info(MPT3SAS_FMT 1298 "phy(%d), ioc_status (0x%04x), loginfo(0x%08x)\n", 1299 ioc->name, phy->number, 1300 le16_to_cpu(mpi_reply.IOCStatus), 1301 le32_to_cpu(mpi_reply.IOCLogInfo)); 1302 1303 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount); 1304 phy->running_disparity_error_count = 1305 le32_to_cpu(phy_pg1.RunningDisparityErrorCount); 1306 phy->loss_of_dword_sync_count = 1307 le32_to_cpu(phy_pg1.LossDwordSynchCount); 1308 phy->phy_reset_problem_count = 1309 le32_to_cpu(phy_pg1.PhyResetProblemCount); 1310 return 0; 1311 } 1312 1313 /** 1314 * _transport_get_enclosure_identifier - 1315 * @phy: The sas phy object 1316 * 1317 * Obtain the enclosure logical id for an expander. 1318 * Returns 0 for success, non-zero for failure. 1319 */ 1320 static int 1321 _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier) 1322 { 1323 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy); 1324 struct _sas_device *sas_device; 1325 unsigned long flags; 1326 int rc; 1327 1328 spin_lock_irqsave(&ioc->sas_device_lock, flags); 1329 sas_device = __mpt3sas_get_sdev_by_addr(ioc, 1330 rphy->identify.sas_address); 1331 if (sas_device) { 1332 *identifier = sas_device->enclosure_logical_id; 1333 rc = 0; 1334 sas_device_put(sas_device); 1335 } else { 1336 *identifier = 0; 1337 rc = -ENXIO; 1338 } 1339 1340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags); 1341 return rc; 1342 } 1343 1344 /** 1345 * _transport_get_bay_identifier - 1346 * @phy: The sas phy object 1347 * 1348 * Returns the slot id for a device that resides inside an enclosure. 1349 */ 1350 static int 1351 _transport_get_bay_identifier(struct sas_rphy *rphy) 1352 { 1353 struct MPT3SAS_ADAPTER *ioc = rphy_to_ioc(rphy); 1354 struct _sas_device *sas_device; 1355 unsigned long flags; 1356 int rc; 1357 1358 spin_lock_irqsave(&ioc->sas_device_lock, flags); 1359 sas_device = __mpt3sas_get_sdev_by_addr(ioc, 1360 rphy->identify.sas_address); 1361 if (sas_device) { 1362 rc = sas_device->slot; 1363 sas_device_put(sas_device); 1364 } else { 1365 rc = -ENXIO; 1366 } 1367 spin_unlock_irqrestore(&ioc->sas_device_lock, flags); 1368 return rc; 1369 } 1370 1371 /* phy control request structure */ 1372 struct phy_control_request { 1373 u8 smp_frame_type; /* 0x40 */ 1374 u8 function; /* 0x91 */ 1375 u8 allocated_response_length; 1376 u8 request_length; /* 0x09 */ 1377 u16 expander_change_count; 1378 u8 reserved_1[3]; 1379 u8 phy_identifier; 1380 u8 phy_operation; 1381 u8 reserved_2[13]; 1382 u64 attached_device_name; 1383 u8 programmed_min_physical_link_rate; 1384 u8 programmed_max_physical_link_rate; 1385 u8 reserved_3[6]; 1386 }; 1387 1388 /* phy control reply structure */ 1389 struct phy_control_reply { 1390 u8 smp_frame_type; /* 0x41 */ 1391 u8 function; /* 0x11 */ 1392 u8 function_result; 1393 u8 response_length; 1394 }; 1395 1396 #define SMP_PHY_CONTROL_LINK_RESET (0x01) 1397 #define SMP_PHY_CONTROL_HARD_RESET (0x02) 1398 #define SMP_PHY_CONTROL_DISABLE (0x03) 1399 1400 /** 1401 * _transport_expander_phy_control - expander phy control 1402 * @ioc: per adapter object 1403 * @phy: The sas phy object 1404 * 1405 * Returns 0 for success, non-zero for failure. 1406 * 1407 */ 1408 static int 1409 _transport_expander_phy_control(struct MPT3SAS_ADAPTER *ioc, 1410 struct sas_phy *phy, u8 phy_operation) 1411 { 1412 Mpi2SmpPassthroughRequest_t *mpi_request; 1413 Mpi2SmpPassthroughReply_t *mpi_reply; 1414 struct phy_control_request *phy_control_request; 1415 struct phy_control_reply *phy_control_reply; 1416 int rc; 1417 u16 smid; 1418 u32 ioc_state; 1419 unsigned long timeleft; 1420 void *psge; 1421 u8 issue_reset = 0; 1422 void *data_out = NULL; 1423 dma_addr_t data_out_dma; 1424 u32 sz; 1425 u16 wait_state_count; 1426 1427 if (ioc->shost_recovery || ioc->pci_error_recovery) { 1428 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n", 1429 __func__, ioc->name); 1430 return -EFAULT; 1431 } 1432 1433 mutex_lock(&ioc->transport_cmds.mutex); 1434 1435 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) { 1436 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", 1437 ioc->name, __func__); 1438 rc = -EAGAIN; 1439 goto out; 1440 } 1441 ioc->transport_cmds.status = MPT3_CMD_PENDING; 1442 1443 wait_state_count = 0; 1444 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 1445 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) { 1446 if (wait_state_count++ == 10) { 1447 pr_err(MPT3SAS_FMT 1448 "%s: failed due to ioc not operational\n", 1449 ioc->name, __func__); 1450 rc = -EFAULT; 1451 goto out; 1452 } 1453 ssleep(1); 1454 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 1455 pr_info(MPT3SAS_FMT 1456 "%s: waiting for operational state(count=%d)\n", 1457 ioc->name, __func__, wait_state_count); 1458 } 1459 if (wait_state_count) 1460 pr_info(MPT3SAS_FMT "%s: ioc is operational\n", 1461 ioc->name, __func__); 1462 1463 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx); 1464 if (!smid) { 1465 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n", 1466 ioc->name, __func__); 1467 rc = -EAGAIN; 1468 goto out; 1469 } 1470 1471 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid); 1472 ioc->transport_cmds.smid = smid; 1473 1474 sz = sizeof(struct phy_control_request) + 1475 sizeof(struct phy_control_reply); 1476 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma); 1477 if (!data_out) { 1478 pr_err("failure at %s:%d/%s()!\n", __FILE__, 1479 __LINE__, __func__); 1480 rc = -ENOMEM; 1481 mpt3sas_base_free_smid(ioc, smid); 1482 goto out; 1483 } 1484 1485 rc = -EINVAL; 1486 memset(data_out, 0, sz); 1487 phy_control_request = data_out; 1488 phy_control_request->smp_frame_type = 0x40; 1489 phy_control_request->function = 0x91; 1490 phy_control_request->request_length = 9; 1491 phy_control_request->allocated_response_length = 0; 1492 phy_control_request->phy_identifier = phy->number; 1493 phy_control_request->phy_operation = phy_operation; 1494 phy_control_request->programmed_min_physical_link_rate = 1495 phy->minimum_linkrate << 4; 1496 phy_control_request->programmed_max_physical_link_rate = 1497 phy->maximum_linkrate << 4; 1498 1499 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t)); 1500 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH; 1501 mpi_request->PhysicalPort = 0xFF; 1502 mpi_request->VF_ID = 0; /* TODO */ 1503 mpi_request->VP_ID = 0; 1504 mpi_request->SASAddress = cpu_to_le64(phy->identify.sas_address); 1505 mpi_request->RequestDataLength = 1506 cpu_to_le16(sizeof(struct phy_error_log_request)); 1507 psge = &mpi_request->SGL; 1508 1509 ioc->build_sg(ioc, psge, data_out_dma, 1510 sizeof(struct phy_control_request), 1511 data_out_dma + sizeof(struct phy_control_request), 1512 sizeof(struct phy_control_reply)); 1513 1514 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1515 "phy_control - send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", 1516 ioc->name, (unsigned long long)phy->identify.sas_address, 1517 phy->number, phy_operation)); 1518 init_completion(&ioc->transport_cmds.done); 1519 mpt3sas_base_put_smid_default(ioc, smid); 1520 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done, 1521 10*HZ); 1522 1523 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) { 1524 pr_err(MPT3SAS_FMT "%s: timeout\n", 1525 ioc->name, __func__); 1526 _debug_dump_mf(mpi_request, 1527 sizeof(Mpi2SmpPassthroughRequest_t)/4); 1528 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) 1529 issue_reset = 1; 1530 goto issue_host_reset; 1531 } 1532 1533 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1534 "phy_control - complete\n", ioc->name)); 1535 1536 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) { 1537 1538 mpi_reply = ioc->transport_cmds.reply; 1539 1540 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1541 "phy_control - reply data transfer size(%d)\n", 1542 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength))); 1543 1544 if (le16_to_cpu(mpi_reply->ResponseDataLength) != 1545 sizeof(struct phy_control_reply)) 1546 goto out; 1547 1548 phy_control_reply = data_out + 1549 sizeof(struct phy_control_request); 1550 1551 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1552 "phy_control - function_result(%d)\n", 1553 ioc->name, phy_control_reply->function_result)); 1554 1555 rc = 0; 1556 } else 1557 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 1558 "phy_control - no reply\n", ioc->name)); 1559 1560 issue_host_reset: 1561 if (issue_reset) 1562 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, 1563 FORCE_BIG_HAMMER); 1564 out: 1565 ioc->transport_cmds.status = MPT3_CMD_NOT_USED; 1566 if (data_out) 1567 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma); 1568 1569 mutex_unlock(&ioc->transport_cmds.mutex); 1570 return rc; 1571 } 1572 1573 /** 1574 * _transport_phy_reset - 1575 * @phy: The sas phy object 1576 * @hard_reset: 1577 * 1578 * Returns 0 for success, non-zero for failure. 1579 */ 1580 static int 1581 _transport_phy_reset(struct sas_phy *phy, int hard_reset) 1582 { 1583 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy); 1584 Mpi2SasIoUnitControlReply_t mpi_reply; 1585 Mpi2SasIoUnitControlRequest_t mpi_request; 1586 unsigned long flags; 1587 1588 spin_lock_irqsave(&ioc->sas_node_lock, flags); 1589 if (_transport_sas_node_find_by_sas_address(ioc, 1590 phy->identify.sas_address) == NULL) { 1591 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1592 return -EINVAL; 1593 } 1594 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1595 1596 /* handle expander phys */ 1597 if (phy->identify.sas_address != ioc->sas_hba.sas_address) 1598 return _transport_expander_phy_control(ioc, phy, 1599 (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET : 1600 SMP_PHY_CONTROL_LINK_RESET); 1601 1602 /* handle hba phys */ 1603 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t)); 1604 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL; 1605 mpi_request.Operation = hard_reset ? 1606 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET; 1607 mpi_request.PhyNum = phy->number; 1608 1609 if ((mpt3sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) { 1610 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1611 ioc->name, __FILE__, __LINE__, __func__); 1612 return -ENXIO; 1613 } 1614 1615 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) 1616 pr_info(MPT3SAS_FMT 1617 "phy(%d), ioc_status(0x%04x), loginfo(0x%08x)\n", 1618 ioc->name, phy->number, le16_to_cpu(mpi_reply.IOCStatus), 1619 le32_to_cpu(mpi_reply.IOCLogInfo)); 1620 1621 return 0; 1622 } 1623 1624 /** 1625 * _transport_phy_enable - enable/disable phys 1626 * @phy: The sas phy object 1627 * @enable: enable phy when true 1628 * 1629 * Only support sas_host direct attached phys. 1630 * Returns 0 for success, non-zero for failure. 1631 */ 1632 static int 1633 _transport_phy_enable(struct sas_phy *phy, int enable) 1634 { 1635 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy); 1636 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL; 1637 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL; 1638 Mpi2ConfigReply_t mpi_reply; 1639 u16 ioc_status; 1640 u16 sz; 1641 int rc = 0; 1642 unsigned long flags; 1643 int i, discovery_active; 1644 1645 spin_lock_irqsave(&ioc->sas_node_lock, flags); 1646 if (_transport_sas_node_find_by_sas_address(ioc, 1647 phy->identify.sas_address) == NULL) { 1648 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1649 return -EINVAL; 1650 } 1651 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1652 1653 /* handle expander phys */ 1654 if (phy->identify.sas_address != ioc->sas_hba.sas_address) 1655 return _transport_expander_phy_control(ioc, phy, 1656 (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET : 1657 SMP_PHY_CONTROL_DISABLE); 1658 1659 /* handle hba phys */ 1660 1661 /* read sas_iounit page 0 */ 1662 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys * 1663 sizeof(Mpi2SasIOUnit0PhyData_t)); 1664 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL); 1665 if (!sas_iounit_pg0) { 1666 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1667 ioc->name, __FILE__, __LINE__, __func__); 1668 rc = -ENOMEM; 1669 goto out; 1670 } 1671 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply, 1672 sas_iounit_pg0, sz))) { 1673 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1674 ioc->name, __FILE__, __LINE__, __func__); 1675 rc = -ENXIO; 1676 goto out; 1677 } 1678 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & 1679 MPI2_IOCSTATUS_MASK; 1680 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) { 1681 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1682 ioc->name, __FILE__, __LINE__, __func__); 1683 rc = -EIO; 1684 goto out; 1685 } 1686 1687 /* unable to enable/disable phys when when discovery is active */ 1688 for (i = 0, discovery_active = 0; i < ioc->sas_hba.num_phys ; i++) { 1689 if (sas_iounit_pg0->PhyData[i].PortFlags & 1690 MPI2_SASIOUNIT0_PORTFLAGS_DISCOVERY_IN_PROGRESS) { 1691 pr_err(MPT3SAS_FMT "discovery is active on " \ 1692 "port = %d, phy = %d: unable to enable/disable " 1693 "phys, try again later!\n", ioc->name, 1694 sas_iounit_pg0->PhyData[i].Port, i); 1695 discovery_active = 1; 1696 } 1697 } 1698 1699 if (discovery_active) { 1700 rc = -EAGAIN; 1701 goto out; 1702 } 1703 1704 /* read sas_iounit page 1 */ 1705 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys * 1706 sizeof(Mpi2SasIOUnit1PhyData_t)); 1707 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL); 1708 if (!sas_iounit_pg1) { 1709 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1710 ioc->name, __FILE__, __LINE__, __func__); 1711 rc = -ENOMEM; 1712 goto out; 1713 } 1714 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply, 1715 sas_iounit_pg1, sz))) { 1716 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1717 ioc->name, __FILE__, __LINE__, __func__); 1718 rc = -ENXIO; 1719 goto out; 1720 } 1721 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & 1722 MPI2_IOCSTATUS_MASK; 1723 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) { 1724 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1725 ioc->name, __FILE__, __LINE__, __func__); 1726 rc = -EIO; 1727 goto out; 1728 } 1729 1730 /* copy Port/PortFlags/PhyFlags from page 0 */ 1731 for (i = 0; i < ioc->sas_hba.num_phys ; i++) { 1732 sas_iounit_pg1->PhyData[i].Port = 1733 sas_iounit_pg0->PhyData[i].Port; 1734 sas_iounit_pg1->PhyData[i].PortFlags = 1735 (sas_iounit_pg0->PhyData[i].PortFlags & 1736 MPI2_SASIOUNIT0_PORTFLAGS_AUTO_PORT_CONFIG); 1737 sas_iounit_pg1->PhyData[i].PhyFlags = 1738 (sas_iounit_pg0->PhyData[i].PhyFlags & 1739 (MPI2_SASIOUNIT0_PHYFLAGS_ZONING_ENABLED + 1740 MPI2_SASIOUNIT0_PHYFLAGS_PHY_DISABLED)); 1741 } 1742 1743 if (enable) 1744 sas_iounit_pg1->PhyData[phy->number].PhyFlags 1745 &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE; 1746 else 1747 sas_iounit_pg1->PhyData[phy->number].PhyFlags 1748 |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE; 1749 1750 mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz); 1751 1752 /* link reset */ 1753 if (enable) 1754 _transport_phy_reset(phy, 0); 1755 1756 out: 1757 kfree(sas_iounit_pg1); 1758 kfree(sas_iounit_pg0); 1759 return rc; 1760 } 1761 1762 /** 1763 * _transport_phy_speed - set phy min/max link rates 1764 * @phy: The sas phy object 1765 * @rates: rates defined in sas_phy_linkrates 1766 * 1767 * Only support sas_host direct attached phys. 1768 * Returns 0 for success, non-zero for failure. 1769 */ 1770 static int 1771 _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates) 1772 { 1773 struct MPT3SAS_ADAPTER *ioc = phy_to_ioc(phy); 1774 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL; 1775 Mpi2SasPhyPage0_t phy_pg0; 1776 Mpi2ConfigReply_t mpi_reply; 1777 u16 ioc_status; 1778 u16 sz; 1779 int i; 1780 int rc = 0; 1781 unsigned long flags; 1782 1783 spin_lock_irqsave(&ioc->sas_node_lock, flags); 1784 if (_transport_sas_node_find_by_sas_address(ioc, 1785 phy->identify.sas_address) == NULL) { 1786 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1787 return -EINVAL; 1788 } 1789 spin_unlock_irqrestore(&ioc->sas_node_lock, flags); 1790 1791 if (!rates->minimum_linkrate) 1792 rates->minimum_linkrate = phy->minimum_linkrate; 1793 else if (rates->minimum_linkrate < phy->minimum_linkrate_hw) 1794 rates->minimum_linkrate = phy->minimum_linkrate_hw; 1795 1796 if (!rates->maximum_linkrate) 1797 rates->maximum_linkrate = phy->maximum_linkrate; 1798 else if (rates->maximum_linkrate > phy->maximum_linkrate_hw) 1799 rates->maximum_linkrate = phy->maximum_linkrate_hw; 1800 1801 /* handle expander phys */ 1802 if (phy->identify.sas_address != ioc->sas_hba.sas_address) { 1803 phy->minimum_linkrate = rates->minimum_linkrate; 1804 phy->maximum_linkrate = rates->maximum_linkrate; 1805 return _transport_expander_phy_control(ioc, phy, 1806 SMP_PHY_CONTROL_LINK_RESET); 1807 } 1808 1809 /* handle hba phys */ 1810 1811 /* sas_iounit page 1 */ 1812 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys * 1813 sizeof(Mpi2SasIOUnit1PhyData_t)); 1814 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL); 1815 if (!sas_iounit_pg1) { 1816 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1817 ioc->name, __FILE__, __LINE__, __func__); 1818 rc = -ENOMEM; 1819 goto out; 1820 } 1821 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply, 1822 sas_iounit_pg1, sz))) { 1823 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1824 ioc->name, __FILE__, __LINE__, __func__); 1825 rc = -ENXIO; 1826 goto out; 1827 } 1828 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & 1829 MPI2_IOCSTATUS_MASK; 1830 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) { 1831 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1832 ioc->name, __FILE__, __LINE__, __func__); 1833 rc = -EIO; 1834 goto out; 1835 } 1836 1837 for (i = 0; i < ioc->sas_hba.num_phys; i++) { 1838 if (phy->number != i) { 1839 sas_iounit_pg1->PhyData[i].MaxMinLinkRate = 1840 (ioc->sas_hba.phy[i].phy->minimum_linkrate + 1841 (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4)); 1842 } else { 1843 sas_iounit_pg1->PhyData[i].MaxMinLinkRate = 1844 (rates->minimum_linkrate + 1845 (rates->maximum_linkrate << 4)); 1846 } 1847 } 1848 1849 if (mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, 1850 sz)) { 1851 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", 1852 ioc->name, __FILE__, __LINE__, __func__); 1853 rc = -ENXIO; 1854 goto out; 1855 } 1856 1857 /* link reset */ 1858 _transport_phy_reset(phy, 0); 1859 1860 /* read phy page 0, then update the rates in the sas transport phy */ 1861 if (!mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0, 1862 phy->number)) { 1863 phy->minimum_linkrate = _transport_convert_phy_link_rate( 1864 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK); 1865 phy->maximum_linkrate = _transport_convert_phy_link_rate( 1866 phy_pg0.ProgrammedLinkRate >> 4); 1867 phy->negotiated_linkrate = _transport_convert_phy_link_rate( 1868 phy_pg0.NegotiatedLinkRate & 1869 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL); 1870 } 1871 1872 out: 1873 kfree(sas_iounit_pg1); 1874 return rc; 1875 } 1876 1877 /** 1878 * _transport_smp_handler - transport portal for smp passthru 1879 * @shost: shost object 1880 * @rphy: sas transport rphy object 1881 * @req: 1882 * 1883 * This used primarily for smp_utils. 1884 * Example: 1885 * smp_rep_general /sys/class/bsg/expander-5:0 1886 */ 1887 static int 1888 _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy, 1889 struct request *req) 1890 { 1891 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost); 1892 Mpi2SmpPassthroughRequest_t *mpi_request; 1893 Mpi2SmpPassthroughReply_t *mpi_reply; 1894 int rc; 1895 u16 smid; 1896 u32 ioc_state; 1897 unsigned long timeleft; 1898 void *psge; 1899 u8 issue_reset = 0; 1900 dma_addr_t dma_addr_in = 0; 1901 dma_addr_t dma_addr_out = 0; 1902 dma_addr_t pci_dma_in = 0; 1903 dma_addr_t pci_dma_out = 0; 1904 void *pci_addr_in = NULL; 1905 void *pci_addr_out = NULL; 1906 u16 wait_state_count; 1907 struct request *rsp = req->next_rq; 1908 struct bio_vec bvec; 1909 struct bvec_iter iter; 1910 1911 if (!rsp) { 1912 pr_err(MPT3SAS_FMT "%s: the smp response space is missing\n", 1913 ioc->name, __func__); 1914 return -EINVAL; 1915 } 1916 1917 if (ioc->shost_recovery || ioc->pci_error_recovery) { 1918 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n", 1919 __func__, ioc->name); 1920 return -EFAULT; 1921 } 1922 1923 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex); 1924 if (rc) 1925 return rc; 1926 1927 if (ioc->transport_cmds.status != MPT3_CMD_NOT_USED) { 1928 pr_err(MPT3SAS_FMT "%s: transport_cmds in use\n", ioc->name, 1929 __func__); 1930 rc = -EAGAIN; 1931 goto out; 1932 } 1933 ioc->transport_cmds.status = MPT3_CMD_PENDING; 1934 1935 /* Check if the request is split across multiple segments */ 1936 if (bio_multiple_segments(req->bio)) { 1937 u32 offset = 0; 1938 1939 /* Allocate memory and copy the request */ 1940 pci_addr_out = pci_alloc_consistent(ioc->pdev, 1941 blk_rq_bytes(req), &pci_dma_out); 1942 if (!pci_addr_out) { 1943 pr_info(MPT3SAS_FMT "%s(): PCI Addr out = NULL\n", 1944 ioc->name, __func__); 1945 rc = -ENOMEM; 1946 goto out; 1947 } 1948 1949 bio_for_each_segment(bvec, req->bio, iter) { 1950 memcpy(pci_addr_out + offset, 1951 page_address(bvec.bv_page) + bvec.bv_offset, 1952 bvec.bv_len); 1953 offset += bvec.bv_len; 1954 } 1955 } else { 1956 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio), 1957 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL); 1958 if (pci_dma_mapping_error(ioc->pdev, dma_addr_out)) { 1959 pr_info(MPT3SAS_FMT "%s(): DMA Addr out = NULL\n", 1960 ioc->name, __func__); 1961 rc = -ENOMEM; 1962 goto free_pci; 1963 } 1964 } 1965 1966 /* Check if the response needs to be populated across 1967 * multiple segments */ 1968 if (bio_multiple_segments(rsp->bio)) { 1969 pci_addr_in = pci_alloc_consistent(ioc->pdev, blk_rq_bytes(rsp), 1970 &pci_dma_in); 1971 if (!pci_addr_in) { 1972 pr_info(MPT3SAS_FMT "%s(): PCI Addr in = NULL\n", 1973 ioc->name, __func__); 1974 rc = -ENOMEM; 1975 goto unmap; 1976 } 1977 } else { 1978 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio), 1979 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL); 1980 if (pci_dma_mapping_error(ioc->pdev, dma_addr_in)) { 1981 pr_info(MPT3SAS_FMT "%s(): DMA Addr in = NULL\n", 1982 ioc->name, __func__); 1983 rc = -ENOMEM; 1984 goto unmap; 1985 } 1986 } 1987 1988 wait_state_count = 0; 1989 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 1990 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) { 1991 if (wait_state_count++ == 10) { 1992 pr_err(MPT3SAS_FMT 1993 "%s: failed due to ioc not operational\n", 1994 ioc->name, __func__); 1995 rc = -EFAULT; 1996 goto unmap; 1997 } 1998 ssleep(1); 1999 ioc_state = mpt3sas_base_get_iocstate(ioc, 1); 2000 pr_info(MPT3SAS_FMT 2001 "%s: waiting for operational state(count=%d)\n", 2002 ioc->name, __func__, wait_state_count); 2003 } 2004 if (wait_state_count) 2005 pr_info(MPT3SAS_FMT "%s: ioc is operational\n", 2006 ioc->name, __func__); 2007 2008 smid = mpt3sas_base_get_smid(ioc, ioc->transport_cb_idx); 2009 if (!smid) { 2010 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n", 2011 ioc->name, __func__); 2012 rc = -EAGAIN; 2013 goto unmap; 2014 } 2015 2016 rc = 0; 2017 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid); 2018 ioc->transport_cmds.smid = smid; 2019 2020 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t)); 2021 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH; 2022 mpi_request->PhysicalPort = 0xFF; 2023 mpi_request->SASAddress = (rphy) ? 2024 cpu_to_le64(rphy->identify.sas_address) : 2025 cpu_to_le64(ioc->sas_hba.sas_address); 2026 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4); 2027 psge = &mpi_request->SGL; 2028 2029 if (bio_multiple_segments(req->bio)) 2030 ioc->build_sg(ioc, psge, pci_dma_out, (blk_rq_bytes(req) - 4), 2031 pci_dma_in, (blk_rq_bytes(rsp) + 4)); 2032 else 2033 ioc->build_sg(ioc, psge, dma_addr_out, (blk_rq_bytes(req) - 4), 2034 dma_addr_in, (blk_rq_bytes(rsp) + 4)); 2035 2036 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 2037 "%s - sending smp request\n", ioc->name, __func__)); 2038 2039 init_completion(&ioc->transport_cmds.done); 2040 mpt3sas_base_put_smid_default(ioc, smid); 2041 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done, 2042 10*HZ); 2043 2044 if (!(ioc->transport_cmds.status & MPT3_CMD_COMPLETE)) { 2045 pr_err(MPT3SAS_FMT "%s : timeout\n", 2046 __func__, ioc->name); 2047 _debug_dump_mf(mpi_request, 2048 sizeof(Mpi2SmpPassthroughRequest_t)/4); 2049 if (!(ioc->transport_cmds.status & MPT3_CMD_RESET)) 2050 issue_reset = 1; 2051 goto issue_host_reset; 2052 } 2053 2054 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 2055 "%s - complete\n", ioc->name, __func__)); 2056 2057 if (ioc->transport_cmds.status & MPT3_CMD_REPLY_VALID) { 2058 2059 mpi_reply = ioc->transport_cmds.reply; 2060 2061 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 2062 "%s - reply data transfer size(%d)\n", 2063 ioc->name, __func__, 2064 le16_to_cpu(mpi_reply->ResponseDataLength))); 2065 2066 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply)); 2067 req->sense_len = sizeof(*mpi_reply); 2068 req->resid_len = 0; 2069 rsp->resid_len -= 2070 le16_to_cpu(mpi_reply->ResponseDataLength); 2071 2072 /* check if the resp needs to be copied from the allocated 2073 * pci mem */ 2074 if (bio_multiple_segments(rsp->bio)) { 2075 u32 offset = 0; 2076 u32 bytes_to_copy = 2077 le16_to_cpu(mpi_reply->ResponseDataLength); 2078 bio_for_each_segment(bvec, rsp->bio, iter) { 2079 if (bytes_to_copy <= bvec.bv_len) { 2080 memcpy(page_address(bvec.bv_page) + 2081 bvec.bv_offset, pci_addr_in + 2082 offset, bytes_to_copy); 2083 break; 2084 } else { 2085 memcpy(page_address(bvec.bv_page) + 2086 bvec.bv_offset, pci_addr_in + 2087 offset, bvec.bv_len); 2088 bytes_to_copy -= bvec.bv_len; 2089 } 2090 offset += bvec.bv_len; 2091 } 2092 } 2093 } else { 2094 dtransportprintk(ioc, pr_info(MPT3SAS_FMT 2095 "%s - no reply\n", ioc->name, __func__)); 2096 rc = -ENXIO; 2097 } 2098 2099 issue_host_reset: 2100 if (issue_reset) { 2101 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, 2102 FORCE_BIG_HAMMER); 2103 rc = -ETIMEDOUT; 2104 } 2105 2106 unmap: 2107 if (dma_addr_out) 2108 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req), 2109 PCI_DMA_BIDIRECTIONAL); 2110 if (dma_addr_in) 2111 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp), 2112 PCI_DMA_BIDIRECTIONAL); 2113 2114 free_pci: 2115 if (pci_addr_out) 2116 pci_free_consistent(ioc->pdev, blk_rq_bytes(req), pci_addr_out, 2117 pci_dma_out); 2118 2119 if (pci_addr_in) 2120 pci_free_consistent(ioc->pdev, blk_rq_bytes(rsp), pci_addr_in, 2121 pci_dma_in); 2122 2123 out: 2124 ioc->transport_cmds.status = MPT3_CMD_NOT_USED; 2125 mutex_unlock(&ioc->transport_cmds.mutex); 2126 return rc; 2127 } 2128 2129 struct sas_function_template mpt3sas_transport_functions = { 2130 .get_linkerrors = _transport_get_linkerrors, 2131 .get_enclosure_identifier = _transport_get_enclosure_identifier, 2132 .get_bay_identifier = _transport_get_bay_identifier, 2133 .phy_reset = _transport_phy_reset, 2134 .phy_enable = _transport_phy_enable, 2135 .set_phy_speed = _transport_phy_speed, 2136 .smp_handler = _transport_smp_handler, 2137 }; 2138 2139 struct scsi_transport_template *mpt3sas_transport_template; 2140