1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2015 Linaro Ltd. 4 * Copyright (c) 2015 Hisilicon Limited. 5 */ 6 7 #include "hisi_sas.h" 8 #define DRV_NAME "hisi_sas" 9 10 #define DEV_IS_GONE(dev) \ 11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED)) 12 13 static int hisi_sas_softreset_ata_disk(struct domain_device *device); 14 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, 15 void *funcdata); 16 static void hisi_sas_release_task(struct hisi_hba *hisi_hba, 17 struct domain_device *device); 18 static void hisi_sas_dev_gone(struct domain_device *device); 19 20 struct hisi_sas_internal_abort_data { 21 bool rst_ha_timeout; /* reset the HA for timeout */ 22 }; 23 24 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction) 25 { 26 switch (fis->command) { 27 case ATA_CMD_FPDMA_WRITE: 28 case ATA_CMD_FPDMA_READ: 29 case ATA_CMD_FPDMA_RECV: 30 case ATA_CMD_FPDMA_SEND: 31 case ATA_CMD_NCQ_NON_DATA: 32 return HISI_SAS_SATA_PROTOCOL_FPDMA; 33 34 case ATA_CMD_DOWNLOAD_MICRO: 35 case ATA_CMD_ID_ATA: 36 case ATA_CMD_PMP_READ: 37 case ATA_CMD_READ_LOG_EXT: 38 case ATA_CMD_PIO_READ: 39 case ATA_CMD_PIO_READ_EXT: 40 case ATA_CMD_PMP_WRITE: 41 case ATA_CMD_WRITE_LOG_EXT: 42 case ATA_CMD_PIO_WRITE: 43 case ATA_CMD_PIO_WRITE_EXT: 44 return HISI_SAS_SATA_PROTOCOL_PIO; 45 46 case ATA_CMD_DSM: 47 case ATA_CMD_DOWNLOAD_MICRO_DMA: 48 case ATA_CMD_PMP_READ_DMA: 49 case ATA_CMD_PMP_WRITE_DMA: 50 case ATA_CMD_READ: 51 case ATA_CMD_READ_EXT: 52 case ATA_CMD_READ_LOG_DMA_EXT: 53 case ATA_CMD_READ_STREAM_DMA_EXT: 54 case ATA_CMD_TRUSTED_RCV_DMA: 55 case ATA_CMD_TRUSTED_SND_DMA: 56 case ATA_CMD_WRITE: 57 case ATA_CMD_WRITE_EXT: 58 case ATA_CMD_WRITE_FUA_EXT: 59 case ATA_CMD_WRITE_QUEUED: 60 case ATA_CMD_WRITE_LOG_DMA_EXT: 61 case ATA_CMD_WRITE_STREAM_DMA_EXT: 62 case ATA_CMD_ZAC_MGMT_IN: 63 return HISI_SAS_SATA_PROTOCOL_DMA; 64 65 case ATA_CMD_CHK_POWER: 66 case ATA_CMD_DEV_RESET: 67 case ATA_CMD_EDD: 68 case ATA_CMD_FLUSH: 69 case ATA_CMD_FLUSH_EXT: 70 case ATA_CMD_VERIFY: 71 case ATA_CMD_VERIFY_EXT: 72 case ATA_CMD_SET_FEATURES: 73 case ATA_CMD_STANDBY: 74 case ATA_CMD_STANDBYNOW1: 75 case ATA_CMD_ZAC_MGMT_OUT: 76 return HISI_SAS_SATA_PROTOCOL_NONDATA; 77 78 case ATA_CMD_SET_MAX: 79 switch (fis->features) { 80 case ATA_SET_MAX_PASSWD: 81 case ATA_SET_MAX_LOCK: 82 return HISI_SAS_SATA_PROTOCOL_PIO; 83 84 case ATA_SET_MAX_PASSWD_DMA: 85 case ATA_SET_MAX_UNLOCK_DMA: 86 return HISI_SAS_SATA_PROTOCOL_DMA; 87 88 default: 89 return HISI_SAS_SATA_PROTOCOL_NONDATA; 90 } 91 92 default: 93 { 94 if (direction == DMA_NONE) 95 return HISI_SAS_SATA_PROTOCOL_NONDATA; 96 return HISI_SAS_SATA_PROTOCOL_PIO; 97 } 98 } 99 } 100 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol); 101 102 void hisi_sas_sata_done(struct sas_task *task, 103 struct hisi_sas_slot *slot) 104 { 105 struct task_status_struct *ts = &task->task_status; 106 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf; 107 struct hisi_sas_status_buffer *status_buf = 108 hisi_sas_status_buf_addr_mem(slot); 109 u8 *iu = &status_buf->iu[0]; 110 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu; 111 112 resp->frame_len = sizeof(struct dev_to_host_fis); 113 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis)); 114 115 ts->buf_valid_size = sizeof(*resp); 116 } 117 EXPORT_SYMBOL_GPL(hisi_sas_sata_done); 118 119 /* 120 * This function assumes linkrate mask fits in 8 bits, which it 121 * does for all HW versions supported. 122 */ 123 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max) 124 { 125 u8 rate = 0; 126 int i; 127 128 max -= SAS_LINK_RATE_1_5_GBPS; 129 for (i = 0; i <= max; i++) 130 rate |= 1 << (i * 2); 131 return rate; 132 } 133 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask); 134 135 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device) 136 { 137 return device->port->ha->lldd_ha; 138 } 139 140 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port) 141 { 142 return container_of(sas_port, struct hisi_sas_port, sas_port); 143 } 144 EXPORT_SYMBOL_GPL(to_hisi_sas_port); 145 146 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba) 147 { 148 int phy_no; 149 150 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) 151 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 152 } 153 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys); 154 155 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx) 156 { 157 void *bitmap = hisi_hba->slot_index_tags; 158 159 __clear_bit(slot_idx, bitmap); 160 } 161 162 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx) 163 { 164 if (hisi_hba->hw->slot_index_alloc || 165 slot_idx >= HISI_SAS_UNRESERVED_IPTT) { 166 spin_lock(&hisi_hba->lock); 167 hisi_sas_slot_index_clear(hisi_hba, slot_idx); 168 spin_unlock(&hisi_hba->lock); 169 } 170 } 171 172 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx) 173 { 174 void *bitmap = hisi_hba->slot_index_tags; 175 176 __set_bit(slot_idx, bitmap); 177 } 178 179 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, 180 struct scsi_cmnd *scsi_cmnd) 181 { 182 int index; 183 void *bitmap = hisi_hba->slot_index_tags; 184 185 if (scsi_cmnd) 186 return scsi_cmd_to_rq(scsi_cmnd)->tag; 187 188 spin_lock(&hisi_hba->lock); 189 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count, 190 hisi_hba->last_slot_index + 1); 191 if (index >= hisi_hba->slot_index_count) { 192 index = find_next_zero_bit(bitmap, 193 hisi_hba->slot_index_count, 194 HISI_SAS_UNRESERVED_IPTT); 195 if (index >= hisi_hba->slot_index_count) { 196 spin_unlock(&hisi_hba->lock); 197 return -SAS_QUEUE_FULL; 198 } 199 } 200 hisi_sas_slot_index_set(hisi_hba, index); 201 hisi_hba->last_slot_index = index; 202 spin_unlock(&hisi_hba->lock); 203 204 return index; 205 } 206 207 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, 208 struct hisi_sas_slot *slot) 209 { 210 int device_id = slot->device_id; 211 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id]; 212 213 if (task) { 214 struct device *dev = hisi_hba->dev; 215 216 if (!task->lldd_task) 217 return; 218 219 task->lldd_task = NULL; 220 221 if (!sas_protocol_ata(task->task_proto)) { 222 if (slot->n_elem) 223 dma_unmap_sg(dev, task->scatter, 224 task->num_scatter, 225 task->data_dir); 226 if (slot->n_elem_dif) { 227 struct sas_ssp_task *ssp_task = &task->ssp_task; 228 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd; 229 230 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 231 scsi_prot_sg_count(scsi_cmnd), 232 task->data_dir); 233 } 234 } 235 } 236 237 spin_lock(&sas_dev->lock); 238 list_del_init(&slot->entry); 239 spin_unlock(&sas_dev->lock); 240 241 memset(slot, 0, offsetof(struct hisi_sas_slot, buf)); 242 243 hisi_sas_slot_index_free(hisi_hba, slot->idx); 244 } 245 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); 246 247 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba, 248 struct hisi_sas_slot *slot) 249 { 250 hisi_hba->hw->prep_smp(hisi_hba, slot); 251 } 252 253 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, 254 struct hisi_sas_slot *slot) 255 { 256 hisi_hba->hw->prep_ssp(hisi_hba, slot); 257 } 258 259 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba, 260 struct hisi_sas_slot *slot) 261 { 262 hisi_hba->hw->prep_stp(hisi_hba, slot); 263 } 264 265 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba, 266 struct hisi_sas_slot *slot) 267 { 268 hisi_hba->hw->prep_abort(hisi_hba, slot); 269 } 270 271 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba, 272 struct sas_task *task, int n_elem, 273 int n_elem_req) 274 { 275 struct device *dev = hisi_hba->dev; 276 277 if (!sas_protocol_ata(task->task_proto)) { 278 if (task->num_scatter) { 279 if (n_elem) 280 dma_unmap_sg(dev, task->scatter, 281 task->num_scatter, 282 task->data_dir); 283 } else if (task->task_proto & SAS_PROTOCOL_SMP) { 284 if (n_elem_req) 285 dma_unmap_sg(dev, &task->smp_task.smp_req, 286 1, DMA_TO_DEVICE); 287 } 288 } 289 } 290 291 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba, 292 struct sas_task *task, int *n_elem, 293 int *n_elem_req) 294 { 295 struct device *dev = hisi_hba->dev; 296 int rc; 297 298 if (sas_protocol_ata(task->task_proto)) { 299 *n_elem = task->num_scatter; 300 } else { 301 unsigned int req_len; 302 303 if (task->num_scatter) { 304 *n_elem = dma_map_sg(dev, task->scatter, 305 task->num_scatter, task->data_dir); 306 if (!*n_elem) { 307 rc = -ENOMEM; 308 goto prep_out; 309 } 310 } else if (task->task_proto & SAS_PROTOCOL_SMP) { 311 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req, 312 1, DMA_TO_DEVICE); 313 if (!*n_elem_req) { 314 rc = -ENOMEM; 315 goto prep_out; 316 } 317 req_len = sg_dma_len(&task->smp_task.smp_req); 318 if (req_len & 0x3) { 319 rc = -EINVAL; 320 goto err_out_dma_unmap; 321 } 322 } 323 } 324 325 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) { 326 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n", 327 *n_elem); 328 rc = -EINVAL; 329 goto err_out_dma_unmap; 330 } 331 return 0; 332 333 err_out_dma_unmap: 334 /* It would be better to call dma_unmap_sg() here, but it's messy */ 335 hisi_sas_dma_unmap(hisi_hba, task, *n_elem, 336 *n_elem_req); 337 prep_out: 338 return rc; 339 } 340 341 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba, 342 struct sas_task *task, int n_elem_dif) 343 { 344 struct device *dev = hisi_hba->dev; 345 346 if (n_elem_dif) { 347 struct sas_ssp_task *ssp_task = &task->ssp_task; 348 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd; 349 350 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 351 scsi_prot_sg_count(scsi_cmnd), 352 task->data_dir); 353 } 354 } 355 356 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba, 357 int *n_elem_dif, struct sas_task *task) 358 { 359 struct device *dev = hisi_hba->dev; 360 struct sas_ssp_task *ssp_task; 361 struct scsi_cmnd *scsi_cmnd; 362 int rc; 363 364 if (task->num_scatter) { 365 ssp_task = &task->ssp_task; 366 scsi_cmnd = ssp_task->cmd; 367 368 if (scsi_prot_sg_count(scsi_cmnd)) { 369 *n_elem_dif = dma_map_sg(dev, 370 scsi_prot_sglist(scsi_cmnd), 371 scsi_prot_sg_count(scsi_cmnd), 372 task->data_dir); 373 374 if (!*n_elem_dif) 375 return -ENOMEM; 376 377 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) { 378 dev_err(dev, "task prep: n_elem_dif(%d) too large\n", 379 *n_elem_dif); 380 rc = -EINVAL; 381 goto err_out_dif_dma_unmap; 382 } 383 } 384 } 385 386 return 0; 387 388 err_out_dif_dma_unmap: 389 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 390 scsi_prot_sg_count(scsi_cmnd), task->data_dir); 391 return rc; 392 } 393 394 static 395 void hisi_sas_task_deliver(struct hisi_hba *hisi_hba, 396 struct hisi_sas_slot *slot, 397 struct hisi_sas_dq *dq, 398 struct hisi_sas_device *sas_dev) 399 { 400 struct hisi_sas_cmd_hdr *cmd_hdr_base; 401 int dlvry_queue_slot, dlvry_queue; 402 struct sas_task *task = slot->task; 403 int wr_q_index; 404 405 spin_lock(&dq->lock); 406 wr_q_index = dq->wr_point; 407 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS; 408 list_add_tail(&slot->delivery, &dq->list); 409 spin_unlock(&dq->lock); 410 spin_lock(&sas_dev->lock); 411 list_add_tail(&slot->entry, &sas_dev->list); 412 spin_unlock(&sas_dev->lock); 413 414 dlvry_queue = dq->id; 415 dlvry_queue_slot = wr_q_index; 416 417 slot->device_id = sas_dev->device_id; 418 slot->dlvry_queue = dlvry_queue; 419 slot->dlvry_queue_slot = dlvry_queue_slot; 420 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; 421 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; 422 423 task->lldd_task = slot; 424 425 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); 426 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ); 427 memset(hisi_sas_status_buf_addr_mem(slot), 0, 428 sizeof(struct hisi_sas_err_record)); 429 430 switch (task->task_proto) { 431 case SAS_PROTOCOL_SMP: 432 hisi_sas_task_prep_smp(hisi_hba, slot); 433 break; 434 case SAS_PROTOCOL_SSP: 435 hisi_sas_task_prep_ssp(hisi_hba, slot); 436 break; 437 case SAS_PROTOCOL_SATA: 438 case SAS_PROTOCOL_STP: 439 case SAS_PROTOCOL_STP_ALL: 440 hisi_sas_task_prep_ata(hisi_hba, slot); 441 break; 442 case SAS_PROTOCOL_INTERNAL_ABORT: 443 hisi_sas_task_prep_abort(hisi_hba, slot); 444 break; 445 default: 446 return; 447 } 448 449 WRITE_ONCE(slot->ready, 1); 450 451 spin_lock(&dq->lock); 452 hisi_hba->hw->start_delivery(dq); 453 spin_unlock(&dq->lock); 454 } 455 456 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) 457 { 458 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0; 459 struct domain_device *device = task->dev; 460 struct asd_sas_port *sas_port = device->port; 461 struct hisi_sas_device *sas_dev = device->lldd_dev; 462 bool internal_abort = sas_is_internal_abort(task); 463 struct scsi_cmnd *scmd = NULL; 464 struct hisi_sas_dq *dq = NULL; 465 struct hisi_sas_port *port; 466 struct hisi_hba *hisi_hba; 467 struct hisi_sas_slot *slot; 468 struct device *dev; 469 int rc; 470 471 if (!sas_port) { 472 struct task_status_struct *ts = &task->task_status; 473 474 ts->resp = SAS_TASK_UNDELIVERED; 475 ts->stat = SAS_PHY_DOWN; 476 /* 477 * libsas will use dev->port, should 478 * not call task_done for sata 479 */ 480 if (device->dev_type != SAS_SATA_DEV && !internal_abort) 481 task->task_done(task); 482 return -ECOMM; 483 } 484 485 hisi_hba = dev_to_hisi_hba(device); 486 dev = hisi_hba->dev; 487 488 switch (task->task_proto) { 489 case SAS_PROTOCOL_SSP: 490 case SAS_PROTOCOL_SMP: 491 case SAS_PROTOCOL_SATA: 492 case SAS_PROTOCOL_STP: 493 case SAS_PROTOCOL_STP_ALL: 494 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) { 495 if (!gfpflags_allow_blocking(gfp_flags)) 496 return -EINVAL; 497 498 down(&hisi_hba->sem); 499 up(&hisi_hba->sem); 500 } 501 502 if (DEV_IS_GONE(sas_dev)) { 503 if (sas_dev) 504 dev_info(dev, "task prep: device %d not ready\n", 505 sas_dev->device_id); 506 else 507 dev_info(dev, "task prep: device %016llx not ready\n", 508 SAS_ADDR(device->sas_addr)); 509 510 return -ECOMM; 511 } 512 513 port = to_hisi_sas_port(sas_port); 514 if (!port->port_attached) { 515 dev_info(dev, "task prep: %s port%d not attach device\n", 516 dev_is_sata(device) ? "SATA/STP" : "SAS", 517 device->port->id); 518 519 return -ECOMM; 520 } 521 522 if (task->uldd_task) { 523 struct ata_queued_cmd *qc; 524 525 if (dev_is_sata(device)) { 526 qc = task->uldd_task; 527 scmd = qc->scsicmd; 528 } else { 529 scmd = task->uldd_task; 530 } 531 } 532 533 if (scmd) { 534 unsigned int dq_index; 535 u32 blk_tag; 536 537 blk_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd)); 538 dq_index = blk_mq_unique_tag_to_hwq(blk_tag); 539 dq = &hisi_hba->dq[dq_index]; 540 } else { 541 struct Scsi_Host *shost = hisi_hba->shost; 542 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT]; 543 int queue = qmap->mq_map[raw_smp_processor_id()]; 544 545 dq = &hisi_hba->dq[queue]; 546 } 547 break; 548 case SAS_PROTOCOL_INTERNAL_ABORT: 549 if (!hisi_hba->hw->prep_abort) 550 return TMF_RESP_FUNC_FAILED; 551 552 if (test_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags)) 553 return -EIO; 554 555 hisi_hba = dev_to_hisi_hba(device); 556 557 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) 558 return -EINVAL; 559 560 port = to_hisi_sas_port(sas_port); 561 dq = &hisi_hba->dq[task->abort_task.qid]; 562 break; 563 default: 564 dev_err(hisi_hba->dev, "task prep: unknown/unsupported proto (0x%x)\n", 565 task->task_proto); 566 return -EINVAL; 567 } 568 569 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem, 570 &n_elem_req); 571 if (rc < 0) 572 goto prep_out; 573 574 if (!sas_protocol_ata(task->task_proto)) { 575 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task); 576 if (rc < 0) 577 goto err_out_dma_unmap; 578 } 579 580 if (!internal_abort && hisi_hba->hw->slot_index_alloc) 581 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device); 582 else 583 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd); 584 585 if (rc < 0) 586 goto err_out_dif_dma_unmap; 587 588 slot = &hisi_hba->slot_info[rc]; 589 slot->n_elem = n_elem; 590 slot->n_elem_dif = n_elem_dif; 591 slot->task = task; 592 slot->port = port; 593 594 slot->tmf = task->tmf; 595 slot->is_internal = !!task->tmf || internal_abort; 596 597 /* protect task_prep and start_delivery sequence */ 598 hisi_sas_task_deliver(hisi_hba, slot, dq, sas_dev); 599 600 return 0; 601 602 err_out_dif_dma_unmap: 603 if (!sas_protocol_ata(task->task_proto)) 604 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif); 605 err_out_dma_unmap: 606 hisi_sas_dma_unmap(hisi_hba, task, n_elem, 607 n_elem_req); 608 prep_out: 609 dev_err(dev, "task exec: failed[%d]!\n", rc); 610 return rc; 611 } 612 613 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no, 614 gfp_t gfp_flags) 615 { 616 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 617 struct asd_sas_phy *sas_phy = &phy->sas_phy; 618 619 if (!phy->phy_attached) 620 return; 621 622 sas_notify_phy_event(sas_phy, PHYE_OOB_DONE, gfp_flags); 623 624 if (sas_phy->phy) { 625 struct sas_phy *sphy = sas_phy->phy; 626 627 sphy->negotiated_linkrate = sas_phy->linkrate; 628 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; 629 sphy->maximum_linkrate_hw = 630 hisi_hba->hw->phy_get_max_linkrate(); 631 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) 632 sphy->minimum_linkrate = phy->minimum_linkrate; 633 634 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) 635 sphy->maximum_linkrate = phy->maximum_linkrate; 636 } 637 638 if (phy->phy_type & PORT_TYPE_SAS) { 639 struct sas_identify_frame *id; 640 641 id = (struct sas_identify_frame *)phy->frame_rcvd; 642 id->dev_type = phy->identify.device_type; 643 id->initiator_bits = SAS_PROTOCOL_ALL; 644 id->target_bits = phy->identify.target_port_protocols; 645 } else if (phy->phy_type & PORT_TYPE_SATA) { 646 /* Nothing */ 647 } 648 649 sas_phy->frame_rcvd_size = phy->frame_rcvd_size; 650 sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED, gfp_flags); 651 } 652 653 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) 654 { 655 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 656 struct hisi_sas_device *sas_dev = NULL; 657 int last = hisi_hba->last_dev_id; 658 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES; 659 int i; 660 661 spin_lock(&hisi_hba->lock); 662 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) { 663 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { 664 int queue = i % hisi_hba->queue_count; 665 struct hisi_sas_dq *dq = &hisi_hba->dq[queue]; 666 667 hisi_hba->devices[i].device_id = i; 668 sas_dev = &hisi_hba->devices[i]; 669 sas_dev->dev_status = HISI_SAS_DEV_INIT; 670 sas_dev->dev_type = device->dev_type; 671 sas_dev->hisi_hba = hisi_hba; 672 sas_dev->sas_device = device; 673 sas_dev->dq = dq; 674 spin_lock_init(&sas_dev->lock); 675 INIT_LIST_HEAD(&hisi_hba->devices[i].list); 676 break; 677 } 678 i++; 679 } 680 hisi_hba->last_dev_id = i; 681 spin_unlock(&hisi_hba->lock); 682 683 return sas_dev; 684 } 685 686 static void hisi_sas_tmf_aborted(struct sas_task *task) 687 { 688 struct hisi_sas_slot *slot = task->lldd_task; 689 struct domain_device *device = task->dev; 690 struct hisi_sas_device *sas_dev = device->lldd_dev; 691 struct hisi_hba *hisi_hba = sas_dev->hisi_hba; 692 693 if (slot) { 694 struct hisi_sas_cq *cq = 695 &hisi_hba->cq[slot->dlvry_queue]; 696 /* 697 * sync irq to avoid free'ing task 698 * before using task in IO completion 699 */ 700 synchronize_irq(cq->irq_no); 701 slot->task = NULL; 702 } 703 } 704 705 #define HISI_SAS_DISK_RECOVER_CNT 3 706 static int hisi_sas_init_device(struct domain_device *device) 707 { 708 int rc = TMF_RESP_FUNC_COMPLETE; 709 struct scsi_lun lun; 710 int retry = HISI_SAS_DISK_RECOVER_CNT; 711 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 712 713 switch (device->dev_type) { 714 case SAS_END_DEVICE: 715 int_to_scsilun(0, &lun); 716 717 while (retry-- > 0) { 718 rc = sas_clear_task_set(device, lun.scsi_lun); 719 if (rc == TMF_RESP_FUNC_COMPLETE) { 720 hisi_sas_release_task(hisi_hba, device); 721 break; 722 } 723 } 724 break; 725 case SAS_SATA_DEV: 726 case SAS_SATA_PM: 727 case SAS_SATA_PM_PORT: 728 case SAS_SATA_PENDING: 729 /* 730 * If an expander is swapped when a SATA disk is attached then 731 * we should issue a hard reset to clear previous affiliation 732 * of STP target port, see SPL (chapter 6.19.4). 733 * 734 * However we don't need to issue a hard reset here for these 735 * reasons: 736 * a. When probing the device, libsas/libata already issues a 737 * hard reset in sas_probe_sata() -> ata_sas_async_probe(). 738 * Note that in hisi_sas_debug_I_T_nexus_reset() we take care 739 * to issue a hard reset by checking the dev status (== INIT). 740 * b. When resetting the controller, this is simply unnecessary. 741 */ 742 while (retry-- > 0) { 743 rc = hisi_sas_softreset_ata_disk(device); 744 if (!rc) 745 break; 746 } 747 break; 748 default: 749 break; 750 } 751 752 return rc; 753 } 754 755 int hisi_sas_slave_alloc(struct scsi_device *sdev) 756 { 757 struct domain_device *ddev = sdev_to_domain_dev(sdev); 758 struct hisi_sas_device *sas_dev = ddev->lldd_dev; 759 int rc; 760 761 rc = sas_slave_alloc(sdev); 762 if (rc) 763 return rc; 764 765 rc = hisi_sas_init_device(ddev); 766 if (rc) 767 return rc; 768 sas_dev->dev_status = HISI_SAS_DEV_NORMAL; 769 return 0; 770 } 771 EXPORT_SYMBOL_GPL(hisi_sas_slave_alloc); 772 773 static int hisi_sas_dev_found(struct domain_device *device) 774 { 775 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 776 struct domain_device *parent_dev = device->parent; 777 struct hisi_sas_device *sas_dev; 778 struct device *dev = hisi_hba->dev; 779 int rc; 780 781 if (hisi_hba->hw->alloc_dev) 782 sas_dev = hisi_hba->hw->alloc_dev(device); 783 else 784 sas_dev = hisi_sas_alloc_dev(device); 785 if (!sas_dev) { 786 dev_err(dev, "fail alloc dev: max support %d devices\n", 787 HISI_SAS_MAX_DEVICES); 788 return -EINVAL; 789 } 790 791 device->lldd_dev = sas_dev; 792 hisi_hba->hw->setup_itct(hisi_hba, sas_dev); 793 794 if (parent_dev && dev_is_expander(parent_dev->dev_type)) { 795 int phy_no; 796 u8 phy_num = parent_dev->ex_dev.num_phys; 797 struct ex_phy *phy; 798 799 for (phy_no = 0; phy_no < phy_num; phy_no++) { 800 phy = &parent_dev->ex_dev.ex_phy[phy_no]; 801 if (SAS_ADDR(phy->attached_sas_addr) == 802 SAS_ADDR(device->sas_addr)) 803 break; 804 } 805 806 if (phy_no == phy_num) { 807 dev_info(dev, "dev found: no attached " 808 "dev:%016llx at ex:%016llx\n", 809 SAS_ADDR(device->sas_addr), 810 SAS_ADDR(parent_dev->sas_addr)); 811 rc = -EINVAL; 812 goto err_out; 813 } 814 } 815 816 dev_info(dev, "dev[%d:%x] found\n", 817 sas_dev->device_id, sas_dev->dev_type); 818 819 return 0; 820 821 err_out: 822 hisi_sas_dev_gone(device); 823 return rc; 824 } 825 826 int hisi_sas_slave_configure(struct scsi_device *sdev) 827 { 828 struct domain_device *dev = sdev_to_domain_dev(sdev); 829 int ret = sas_slave_configure(sdev); 830 831 if (ret) 832 return ret; 833 if (!dev_is_sata(dev)) 834 sas_change_queue_depth(sdev, 64); 835 836 return 0; 837 } 838 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure); 839 840 void hisi_sas_scan_start(struct Scsi_Host *shost) 841 { 842 struct hisi_hba *hisi_hba = shost_priv(shost); 843 844 hisi_hba->hw->phys_init(hisi_hba); 845 } 846 EXPORT_SYMBOL_GPL(hisi_sas_scan_start); 847 848 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time) 849 { 850 struct hisi_hba *hisi_hba = shost_priv(shost); 851 struct sas_ha_struct *sha = &hisi_hba->sha; 852 853 /* Wait for PHY up interrupt to occur */ 854 if (time < HZ) 855 return 0; 856 857 sas_drain_work(sha); 858 return 1; 859 } 860 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished); 861 862 static void hisi_sas_phyup_work_common(struct work_struct *work, 863 enum hisi_sas_phy_event event) 864 { 865 struct hisi_sas_phy *phy = 866 container_of(work, typeof(*phy), works[event]); 867 struct hisi_hba *hisi_hba = phy->hisi_hba; 868 struct asd_sas_phy *sas_phy = &phy->sas_phy; 869 int phy_no = sas_phy->id; 870 871 phy->wait_phyup_cnt = 0; 872 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP) 873 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no); 874 hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL); 875 } 876 877 static void hisi_sas_phyup_work(struct work_struct *work) 878 { 879 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP); 880 } 881 882 static void hisi_sas_linkreset_work(struct work_struct *work) 883 { 884 struct hisi_sas_phy *phy = 885 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]); 886 struct asd_sas_phy *sas_phy = &phy->sas_phy; 887 888 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL); 889 } 890 891 static void hisi_sas_phyup_pm_work(struct work_struct *work) 892 { 893 struct hisi_sas_phy *phy = 894 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP_PM]); 895 struct hisi_hba *hisi_hba = phy->hisi_hba; 896 struct device *dev = hisi_hba->dev; 897 898 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP_PM); 899 pm_runtime_put_sync(dev); 900 } 901 902 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = { 903 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work, 904 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work, 905 [HISI_PHYE_PHY_UP_PM] = hisi_sas_phyup_pm_work, 906 }; 907 908 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy, 909 enum hisi_sas_phy_event event) 910 { 911 struct hisi_hba *hisi_hba = phy->hisi_hba; 912 913 if (WARN_ON(event >= HISI_PHYES_NUM)) 914 return false; 915 916 return queue_work(hisi_hba->wq, &phy->works[event]); 917 } 918 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event); 919 920 static void hisi_sas_wait_phyup_timedout(struct timer_list *t) 921 { 922 struct hisi_sas_phy *phy = from_timer(phy, t, timer); 923 struct hisi_hba *hisi_hba = phy->hisi_hba; 924 struct device *dev = hisi_hba->dev; 925 int phy_no = phy->sas_phy.id; 926 927 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no); 928 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET); 929 } 930 931 #define HISI_SAS_WAIT_PHYUP_RETRIES 10 932 933 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no) 934 { 935 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 936 struct device *dev = hisi_hba->dev; 937 unsigned long flags; 938 939 dev_dbg(dev, "phy%d OOB ready\n", phy_no); 940 spin_lock_irqsave(&phy->lock, flags); 941 if (phy->phy_attached) { 942 spin_unlock_irqrestore(&phy->lock, flags); 943 return; 944 } 945 946 if (!timer_pending(&phy->timer)) { 947 if (phy->wait_phyup_cnt < HISI_SAS_WAIT_PHYUP_RETRIES) { 948 phy->wait_phyup_cnt++; 949 phy->timer.expires = jiffies + 950 HISI_SAS_WAIT_PHYUP_TIMEOUT; 951 add_timer(&phy->timer); 952 spin_unlock_irqrestore(&phy->lock, flags); 953 return; 954 } 955 956 dev_warn(dev, "phy%d failed to come up %d times, giving up\n", 957 phy_no, phy->wait_phyup_cnt); 958 phy->wait_phyup_cnt = 0; 959 } 960 spin_unlock_irqrestore(&phy->lock, flags); 961 } 962 963 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready); 964 965 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) 966 { 967 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 968 struct asd_sas_phy *sas_phy = &phy->sas_phy; 969 int i; 970 971 phy->hisi_hba = hisi_hba; 972 phy->port = NULL; 973 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS; 974 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate(); 975 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; 976 sas_phy->class = SAS; 977 sas_phy->iproto = SAS_PROTOCOL_ALL; 978 sas_phy->tproto = 0; 979 sas_phy->type = PHY_TYPE_PHYSICAL; 980 sas_phy->role = PHY_ROLE_INITIATOR; 981 sas_phy->oob_mode = OOB_NOT_CONNECTED; 982 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; 983 sas_phy->id = phy_no; 984 sas_phy->sas_addr = &hisi_hba->sas_addr[0]; 985 sas_phy->frame_rcvd = &phy->frame_rcvd[0]; 986 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; 987 sas_phy->lldd_phy = phy; 988 989 for (i = 0; i < HISI_PHYES_NUM; i++) 990 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]); 991 992 spin_lock_init(&phy->lock); 993 994 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0); 995 } 996 997 /* Wrapper to ensure we track hisi_sas_phy.enable properly */ 998 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable) 999 { 1000 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1001 struct asd_sas_phy *aphy = &phy->sas_phy; 1002 struct sas_phy *sphy = aphy->phy; 1003 unsigned long flags; 1004 1005 spin_lock_irqsave(&phy->lock, flags); 1006 1007 if (enable) { 1008 /* We may have been enabled already; if so, don't touch */ 1009 if (!phy->enable) 1010 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN; 1011 hisi_hba->hw->phy_start(hisi_hba, phy_no); 1012 } else { 1013 sphy->negotiated_linkrate = SAS_PHY_DISABLED; 1014 hisi_hba->hw->phy_disable(hisi_hba, phy_no); 1015 } 1016 phy->enable = enable; 1017 spin_unlock_irqrestore(&phy->lock, flags); 1018 } 1019 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable); 1020 1021 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) 1022 { 1023 struct sas_ha_struct *sas_ha = sas_phy->ha; 1024 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 1025 struct hisi_sas_phy *phy = sas_phy->lldd_phy; 1026 struct asd_sas_port *sas_port = sas_phy->port; 1027 struct hisi_sas_port *port; 1028 unsigned long flags; 1029 1030 if (!sas_port) 1031 return; 1032 1033 port = to_hisi_sas_port(sas_port); 1034 spin_lock_irqsave(&hisi_hba->lock, flags); 1035 port->port_attached = 1; 1036 port->id = phy->port_id; 1037 phy->port = port; 1038 sas_port->lldd_port = port; 1039 spin_unlock_irqrestore(&hisi_hba->lock, flags); 1040 } 1041 1042 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task, 1043 struct hisi_sas_slot *slot) 1044 { 1045 if (task) { 1046 unsigned long flags; 1047 struct task_status_struct *ts; 1048 1049 ts = &task->task_status; 1050 1051 ts->resp = SAS_TASK_COMPLETE; 1052 ts->stat = SAS_ABORTED_TASK; 1053 spin_lock_irqsave(&task->task_state_lock, flags); 1054 task->task_state_flags &= ~SAS_TASK_STATE_PENDING; 1055 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP) 1056 task->task_state_flags |= SAS_TASK_STATE_DONE; 1057 spin_unlock_irqrestore(&task->task_state_lock, flags); 1058 } 1059 1060 hisi_sas_slot_task_free(hisi_hba, task, slot); 1061 } 1062 1063 static void hisi_sas_release_task(struct hisi_hba *hisi_hba, 1064 struct domain_device *device) 1065 { 1066 struct hisi_sas_slot *slot, *slot2; 1067 struct hisi_sas_device *sas_dev = device->lldd_dev; 1068 1069 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry) 1070 hisi_sas_do_release_task(hisi_hba, slot->task, slot); 1071 } 1072 1073 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba) 1074 { 1075 struct hisi_sas_device *sas_dev; 1076 struct domain_device *device; 1077 int i; 1078 1079 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1080 sas_dev = &hisi_hba->devices[i]; 1081 device = sas_dev->sas_device; 1082 1083 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || 1084 !device) 1085 continue; 1086 1087 hisi_sas_release_task(hisi_hba, device); 1088 } 1089 } 1090 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks); 1091 1092 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba, 1093 struct domain_device *device) 1094 { 1095 if (hisi_hba->hw->dereg_device) 1096 hisi_hba->hw->dereg_device(hisi_hba, device); 1097 } 1098 1099 static int 1100 hisi_sas_internal_task_abort_dev(struct hisi_sas_device *sas_dev, 1101 bool rst_ha_timeout) 1102 { 1103 struct hisi_sas_internal_abort_data data = { rst_ha_timeout }; 1104 struct domain_device *device = sas_dev->sas_device; 1105 struct hisi_hba *hisi_hba = sas_dev->hisi_hba; 1106 int i, rc; 1107 1108 for (i = 0; i < hisi_hba->cq_nvecs; i++) { 1109 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 1110 const struct cpumask *mask = cq->irq_mask; 1111 1112 if (mask && !cpumask_intersects(cpu_online_mask, mask)) 1113 continue; 1114 rc = sas_execute_internal_abort_dev(device, i, &data); 1115 if (rc) 1116 return rc; 1117 } 1118 1119 return 0; 1120 } 1121 1122 static void hisi_sas_dev_gone(struct domain_device *device) 1123 { 1124 struct hisi_sas_device *sas_dev = device->lldd_dev; 1125 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1126 struct device *dev = hisi_hba->dev; 1127 int ret = 0; 1128 1129 dev_info(dev, "dev[%d:%x] is gone\n", 1130 sas_dev->device_id, sas_dev->dev_type); 1131 1132 down(&hisi_hba->sem); 1133 if (!test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) { 1134 hisi_sas_internal_task_abort_dev(sas_dev, true); 1135 1136 hisi_sas_dereg_device(hisi_hba, device); 1137 1138 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev); 1139 device->lldd_dev = NULL; 1140 } 1141 1142 if (hisi_hba->hw->free_device) 1143 hisi_hba->hw->free_device(sas_dev); 1144 1145 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */ 1146 if (!ret) 1147 sas_dev->dev_type = SAS_PHY_UNUSED; 1148 sas_dev->sas_device = NULL; 1149 up(&hisi_hba->sem); 1150 } 1151 1152 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no, 1153 struct sas_phy_linkrates *r) 1154 { 1155 struct sas_phy_linkrates _r; 1156 1157 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1158 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1159 enum sas_linkrate min, max; 1160 1161 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS) 1162 return -EINVAL; 1163 1164 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) { 1165 max = sas_phy->phy->maximum_linkrate; 1166 min = r->minimum_linkrate; 1167 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) { 1168 max = r->maximum_linkrate; 1169 min = sas_phy->phy->minimum_linkrate; 1170 } else 1171 return -EINVAL; 1172 1173 _r.maximum_linkrate = max; 1174 _r.minimum_linkrate = min; 1175 1176 sas_phy->phy->maximum_linkrate = max; 1177 sas_phy->phy->minimum_linkrate = min; 1178 1179 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1180 msleep(100); 1181 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r); 1182 hisi_sas_phy_enable(hisi_hba, phy_no, 1); 1183 1184 return 0; 1185 } 1186 1187 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, 1188 void *funcdata) 1189 { 1190 struct hisi_sas_phy *phy = container_of(sas_phy, 1191 struct hisi_sas_phy, sas_phy); 1192 struct sas_ha_struct *sas_ha = sas_phy->ha; 1193 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 1194 struct device *dev = hisi_hba->dev; 1195 DECLARE_COMPLETION_ONSTACK(completion); 1196 int phy_no = sas_phy->id; 1197 u8 sts = phy->phy_attached; 1198 int ret = 0; 1199 1200 down(&hisi_hba->sem); 1201 phy->reset_completion = &completion; 1202 1203 switch (func) { 1204 case PHY_FUNC_HARD_RESET: 1205 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); 1206 break; 1207 1208 case PHY_FUNC_LINK_RESET: 1209 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1210 msleep(100); 1211 hisi_sas_phy_enable(hisi_hba, phy_no, 1); 1212 break; 1213 1214 case PHY_FUNC_DISABLE: 1215 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1216 goto out; 1217 1218 case PHY_FUNC_SET_LINK_RATE: 1219 ret = hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata); 1220 break; 1221 1222 case PHY_FUNC_GET_EVENTS: 1223 if (hisi_hba->hw->get_events) { 1224 hisi_hba->hw->get_events(hisi_hba, phy_no); 1225 goto out; 1226 } 1227 fallthrough; 1228 case PHY_FUNC_RELEASE_SPINUP_HOLD: 1229 default: 1230 ret = -EOPNOTSUPP; 1231 goto out; 1232 } 1233 1234 if (sts && !wait_for_completion_timeout(&completion, 1235 HISI_SAS_WAIT_PHYUP_TIMEOUT)) { 1236 dev_warn(dev, "phy%d wait phyup timed out for func %d\n", 1237 phy_no, func); 1238 if (phy->in_reset) 1239 ret = -ETIMEDOUT; 1240 } 1241 1242 out: 1243 phy->reset_completion = NULL; 1244 1245 up(&hisi_hba->sem); 1246 return ret; 1247 } 1248 1249 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev, 1250 bool reset, int pmp, u8 *fis) 1251 { 1252 struct ata_taskfile tf; 1253 1254 ata_tf_init(dev, &tf); 1255 if (reset) 1256 tf.ctl |= ATA_SRST; 1257 else 1258 tf.ctl &= ~ATA_SRST; 1259 tf.command = ATA_CMD_DEV_RESET; 1260 ata_tf_to_fis(&tf, pmp, 0, fis); 1261 } 1262 1263 static int hisi_sas_softreset_ata_disk(struct domain_device *device) 1264 { 1265 u8 fis[20] = {0}; 1266 struct ata_port *ap = device->sata_dev.ap; 1267 struct ata_link *link; 1268 int rc = TMF_RESP_FUNC_FAILED; 1269 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1270 struct device *dev = hisi_hba->dev; 1271 1272 ata_for_each_link(link, ap, EDGE) { 1273 int pmp = sata_srst_pmp(link); 1274 1275 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis); 1276 rc = sas_execute_ata_cmd(device, fis, -1); 1277 if (rc != TMF_RESP_FUNC_COMPLETE) 1278 break; 1279 } 1280 1281 if (rc == TMF_RESP_FUNC_COMPLETE) { 1282 ata_for_each_link(link, ap, EDGE) { 1283 int pmp = sata_srst_pmp(link); 1284 1285 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis); 1286 rc = sas_execute_ata_cmd(device, fis, -1); 1287 if (rc != TMF_RESP_FUNC_COMPLETE) 1288 dev_err(dev, "ata disk %016llx de-reset failed\n", 1289 SAS_ADDR(device->sas_addr)); 1290 } 1291 } else { 1292 dev_err(dev, "ata disk %016llx reset failed\n", 1293 SAS_ADDR(device->sas_addr)); 1294 } 1295 1296 if (rc == TMF_RESP_FUNC_COMPLETE) 1297 hisi_sas_release_task(hisi_hba, device); 1298 1299 return rc; 1300 } 1301 1302 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba) 1303 { 1304 u32 state = hisi_hba->hw->get_phys_state(hisi_hba); 1305 int i; 1306 1307 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1308 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1309 struct domain_device *device = sas_dev->sas_device; 1310 struct asd_sas_port *sas_port; 1311 struct hisi_sas_port *port; 1312 struct hisi_sas_phy *phy = NULL; 1313 struct asd_sas_phy *sas_phy; 1314 1315 if ((sas_dev->dev_type == SAS_PHY_UNUSED) 1316 || !device || !device->port) 1317 continue; 1318 1319 sas_port = device->port; 1320 port = to_hisi_sas_port(sas_port); 1321 1322 spin_lock(&sas_port->phy_list_lock); 1323 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) 1324 if (state & BIT(sas_phy->id)) { 1325 phy = sas_phy->lldd_phy; 1326 break; 1327 } 1328 spin_unlock(&sas_port->phy_list_lock); 1329 1330 if (phy) { 1331 port->id = phy->port_id; 1332 1333 /* Update linkrate of directly attached device. */ 1334 if (!device->parent) 1335 device->linkrate = phy->sas_phy.linkrate; 1336 1337 hisi_hba->hw->setup_itct(hisi_hba, sas_dev); 1338 } else 1339 port->id = 0xff; 1340 } 1341 } 1342 1343 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state) 1344 { 1345 struct asd_sas_port *_sas_port = NULL; 1346 int phy_no; 1347 1348 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) { 1349 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1350 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1351 struct asd_sas_port *sas_port = sas_phy->port; 1352 bool do_port_check = _sas_port != sas_port; 1353 1354 if (!sas_phy->phy->enabled) 1355 continue; 1356 1357 /* Report PHY state change to libsas */ 1358 if (state & BIT(phy_no)) { 1359 if (do_port_check && sas_port && sas_port->port_dev) { 1360 struct domain_device *dev = sas_port->port_dev; 1361 1362 _sas_port = sas_port; 1363 1364 if (dev_is_expander(dev->dev_type)) 1365 sas_notify_port_event(sas_phy, 1366 PORTE_BROADCAST_RCVD, 1367 GFP_KERNEL); 1368 } 1369 } else { 1370 hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL); 1371 } 1372 } 1373 } 1374 1375 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba) 1376 { 1377 struct hisi_sas_device *sas_dev; 1378 struct domain_device *device; 1379 int i; 1380 1381 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1382 sas_dev = &hisi_hba->devices[i]; 1383 device = sas_dev->sas_device; 1384 1385 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device) 1386 continue; 1387 1388 hisi_sas_init_device(device); 1389 } 1390 } 1391 1392 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba, 1393 struct asd_sas_port *sas_port, 1394 struct domain_device *device) 1395 { 1396 struct ata_port *ap = device->sata_dev.ap; 1397 struct device *dev = hisi_hba->dev; 1398 int rc = TMF_RESP_FUNC_FAILED; 1399 struct ata_link *link; 1400 u8 fis[20] = {0}; 1401 int i; 1402 1403 for (i = 0; i < hisi_hba->n_phy; i++) { 1404 if (!(sas_port->phy_mask & BIT(i))) 1405 continue; 1406 1407 ata_for_each_link(link, ap, EDGE) { 1408 int pmp = sata_srst_pmp(link); 1409 1410 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis); 1411 rc = sas_execute_ata_cmd(device, fis, i); 1412 if (rc != TMF_RESP_FUNC_COMPLETE) { 1413 dev_err(dev, "phy%d ata reset failed rc=%d\n", 1414 i, rc); 1415 break; 1416 } 1417 } 1418 } 1419 } 1420 1421 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba) 1422 { 1423 struct device *dev = hisi_hba->dev; 1424 int port_no, rc, i; 1425 1426 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1427 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1428 struct domain_device *device = sas_dev->sas_device; 1429 1430 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device) 1431 continue; 1432 1433 rc = hisi_sas_internal_task_abort_dev(sas_dev, false); 1434 if (rc < 0) 1435 dev_err(dev, "STP reject: abort dev failed %d\n", rc); 1436 } 1437 1438 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) { 1439 struct hisi_sas_port *port = &hisi_hba->port[port_no]; 1440 struct asd_sas_port *sas_port = &port->sas_port; 1441 struct domain_device *port_dev = sas_port->port_dev; 1442 struct domain_device *device; 1443 1444 if (!port_dev || !dev_is_expander(port_dev->dev_type)) 1445 continue; 1446 1447 /* Try to find a SATA device */ 1448 list_for_each_entry(device, &sas_port->dev_list, 1449 dev_list_node) { 1450 if (dev_is_sata(device)) { 1451 hisi_sas_send_ata_reset_each_phy(hisi_hba, 1452 sas_port, 1453 device); 1454 break; 1455 } 1456 } 1457 } 1458 } 1459 1460 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba) 1461 { 1462 struct Scsi_Host *shost = hisi_hba->shost; 1463 1464 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba); 1465 1466 scsi_block_requests(shost); 1467 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000); 1468 1469 del_timer_sync(&hisi_hba->timer); 1470 1471 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1472 } 1473 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare); 1474 1475 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba) 1476 { 1477 struct Scsi_Host *shost = hisi_hba->shost; 1478 1479 /* Init and wait for PHYs to come up and all libsas event finished. */ 1480 hisi_hba->hw->phys_init(hisi_hba); 1481 msleep(1000); 1482 hisi_sas_refresh_port_id(hisi_hba); 1483 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1484 1485 if (hisi_hba->reject_stp_links_msk) 1486 hisi_sas_terminate_stp_reject(hisi_hba); 1487 hisi_sas_reset_init_all_devices(hisi_hba); 1488 scsi_unblock_requests(shost); 1489 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags); 1490 up(&hisi_hba->sem); 1491 1492 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state); 1493 } 1494 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done); 1495 1496 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba) 1497 { 1498 if (!hisi_hba->hw->soft_reset) 1499 return -1; 1500 1501 down(&hisi_hba->sem); 1502 if (test_and_set_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) { 1503 up(&hisi_hba->sem); 1504 return -1; 1505 } 1506 1507 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct) 1508 hisi_hba->hw->debugfs_snapshot_regs(hisi_hba); 1509 1510 return 0; 1511 } 1512 1513 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba) 1514 { 1515 struct device *dev = hisi_hba->dev; 1516 struct Scsi_Host *shost = hisi_hba->shost; 1517 int rc; 1518 1519 dev_info(dev, "controller resetting...\n"); 1520 hisi_sas_controller_reset_prepare(hisi_hba); 1521 1522 rc = hisi_hba->hw->soft_reset(hisi_hba); 1523 if (rc) { 1524 dev_warn(dev, "controller reset failed (%d)\n", rc); 1525 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1526 up(&hisi_hba->sem); 1527 scsi_unblock_requests(shost); 1528 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags); 1529 return rc; 1530 } 1531 1532 hisi_sas_controller_reset_done(hisi_hba); 1533 clear_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags); 1534 dev_info(dev, "controller reset complete\n"); 1535 1536 return 0; 1537 } 1538 1539 static int hisi_sas_abort_task(struct sas_task *task) 1540 { 1541 struct hisi_sas_internal_abort_data internal_abort_data = { false }; 1542 struct domain_device *device = task->dev; 1543 struct hisi_sas_device *sas_dev = device->lldd_dev; 1544 struct hisi_hba *hisi_hba; 1545 struct device *dev; 1546 int rc = TMF_RESP_FUNC_FAILED; 1547 unsigned long flags; 1548 1549 if (!sas_dev) 1550 return TMF_RESP_FUNC_FAILED; 1551 1552 hisi_hba = dev_to_hisi_hba(task->dev); 1553 dev = hisi_hba->dev; 1554 1555 spin_lock_irqsave(&task->task_state_lock, flags); 1556 if (task->task_state_flags & SAS_TASK_STATE_DONE) { 1557 struct hisi_sas_slot *slot = task->lldd_task; 1558 struct hisi_sas_cq *cq; 1559 1560 if (slot) { 1561 /* 1562 * sync irq to avoid free'ing task 1563 * before using task in IO completion 1564 */ 1565 cq = &hisi_hba->cq[slot->dlvry_queue]; 1566 synchronize_irq(cq->irq_no); 1567 } 1568 spin_unlock_irqrestore(&task->task_state_lock, flags); 1569 rc = TMF_RESP_FUNC_COMPLETE; 1570 goto out; 1571 } 1572 task->task_state_flags |= SAS_TASK_STATE_ABORTED; 1573 spin_unlock_irqrestore(&task->task_state_lock, flags); 1574 1575 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { 1576 struct hisi_sas_slot *slot = task->lldd_task; 1577 u16 tag = slot->idx; 1578 int rc2; 1579 1580 rc = sas_abort_task(task, tag); 1581 rc2 = sas_execute_internal_abort_single(device, tag, 1582 slot->dlvry_queue, &internal_abort_data); 1583 if (rc2 < 0) { 1584 dev_err(dev, "abort task: internal abort (%d)\n", rc2); 1585 return TMF_RESP_FUNC_FAILED; 1586 } 1587 1588 /* 1589 * If the TMF finds that the IO is not in the device and also 1590 * the internal abort does not succeed, then it is safe to 1591 * free the slot. 1592 * Note: if the internal abort succeeds then the slot 1593 * will have already been completed 1594 */ 1595 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) { 1596 if (task->lldd_task) 1597 hisi_sas_do_release_task(hisi_hba, task, slot); 1598 } 1599 } else if (task->task_proto & SAS_PROTOCOL_SATA || 1600 task->task_proto & SAS_PROTOCOL_STP) { 1601 if (task->dev->dev_type == SAS_SATA_DEV) { 1602 rc = hisi_sas_internal_task_abort_dev(sas_dev, false); 1603 if (rc < 0) { 1604 dev_err(dev, "abort task: internal abort failed\n"); 1605 goto out; 1606 } 1607 hisi_sas_dereg_device(hisi_hba, device); 1608 rc = hisi_sas_softreset_ata_disk(device); 1609 } 1610 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) { 1611 /* SMP */ 1612 struct hisi_sas_slot *slot = task->lldd_task; 1613 u32 tag = slot->idx; 1614 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue]; 1615 1616 rc = sas_execute_internal_abort_single(device, 1617 tag, slot->dlvry_queue, 1618 &internal_abort_data); 1619 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) && 1620 task->lldd_task) { 1621 /* 1622 * sync irq to avoid free'ing task 1623 * before using task in IO completion 1624 */ 1625 synchronize_irq(cq->irq_no); 1626 slot->task = NULL; 1627 } 1628 } 1629 1630 out: 1631 if (rc != TMF_RESP_FUNC_COMPLETE) 1632 dev_notice(dev, "abort task: rc=%d\n", rc); 1633 return rc; 1634 } 1635 1636 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun) 1637 { 1638 struct hisi_sas_device *sas_dev = device->lldd_dev; 1639 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1640 struct device *dev = hisi_hba->dev; 1641 int rc; 1642 1643 rc = hisi_sas_internal_task_abort_dev(sas_dev, false); 1644 if (rc < 0) { 1645 dev_err(dev, "abort task set: internal abort rc=%d\n", rc); 1646 return TMF_RESP_FUNC_FAILED; 1647 } 1648 hisi_sas_dereg_device(hisi_hba, device); 1649 1650 rc = sas_abort_task_set(device, lun); 1651 if (rc == TMF_RESP_FUNC_COMPLETE) 1652 hisi_sas_release_task(hisi_hba, device); 1653 1654 return rc; 1655 } 1656 1657 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device) 1658 { 1659 struct sas_phy *local_phy = sas_get_local_phy(device); 1660 struct hisi_sas_device *sas_dev = device->lldd_dev; 1661 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1662 struct sas_ha_struct *sas_ha = &hisi_hba->sha; 1663 int rc, reset_type; 1664 1665 if (!local_phy->enabled) { 1666 sas_put_local_phy(local_phy); 1667 return -ENODEV; 1668 } 1669 1670 if (scsi_is_sas_phy_local(local_phy)) { 1671 struct asd_sas_phy *sas_phy = 1672 sas_ha->sas_phy[local_phy->number]; 1673 struct hisi_sas_phy *phy = 1674 container_of(sas_phy, struct hisi_sas_phy, sas_phy); 1675 unsigned long flags; 1676 1677 spin_lock_irqsave(&phy->lock, flags); 1678 phy->in_reset = 1; 1679 spin_unlock_irqrestore(&phy->lock, flags); 1680 } 1681 1682 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT || 1683 !dev_is_sata(device)) ? true : false; 1684 1685 rc = sas_phy_reset(local_phy, reset_type); 1686 sas_put_local_phy(local_phy); 1687 1688 if (scsi_is_sas_phy_local(local_phy)) { 1689 struct asd_sas_phy *sas_phy = 1690 sas_ha->sas_phy[local_phy->number]; 1691 struct hisi_sas_phy *phy = 1692 container_of(sas_phy, struct hisi_sas_phy, sas_phy); 1693 unsigned long flags; 1694 1695 spin_lock_irqsave(&phy->lock, flags); 1696 phy->in_reset = 0; 1697 spin_unlock_irqrestore(&phy->lock, flags); 1698 1699 /* report PHY down if timed out */ 1700 if (rc == -ETIMEDOUT) 1701 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL); 1702 return rc; 1703 } 1704 1705 if (rc) 1706 return rc; 1707 1708 /* Remote phy */ 1709 if (dev_is_sata(device)) { 1710 rc = sas_ata_wait_after_reset(device, 1711 HISI_SAS_WAIT_PHYUP_TIMEOUT); 1712 } else { 1713 msleep(2000); 1714 } 1715 1716 return rc; 1717 } 1718 1719 static int hisi_sas_I_T_nexus_reset(struct domain_device *device) 1720 { 1721 struct hisi_sas_device *sas_dev = device->lldd_dev; 1722 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1723 struct device *dev = hisi_hba->dev; 1724 int rc; 1725 1726 rc = hisi_sas_internal_task_abort_dev(sas_dev, false); 1727 if (rc < 0) { 1728 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc); 1729 return TMF_RESP_FUNC_FAILED; 1730 } 1731 hisi_sas_dereg_device(hisi_hba, device); 1732 1733 rc = hisi_sas_debug_I_T_nexus_reset(device); 1734 if (rc == TMF_RESP_FUNC_COMPLETE && dev_is_sata(device)) { 1735 struct sas_phy *local_phy; 1736 1737 rc = hisi_sas_softreset_ata_disk(device); 1738 switch (rc) { 1739 case -ECOMM: 1740 rc = -ENODEV; 1741 break; 1742 case TMF_RESP_FUNC_FAILED: 1743 case -EMSGSIZE: 1744 case -EIO: 1745 local_phy = sas_get_local_phy(device); 1746 rc = sas_phy_enable(local_phy, 0); 1747 if (!rc) { 1748 local_phy->enabled = 0; 1749 dev_err(dev, "Disabled local phy of ATA disk %016llx due to softreset fail (%d)\n", 1750 SAS_ADDR(device->sas_addr), rc); 1751 rc = -ENODEV; 1752 } 1753 sas_put_local_phy(local_phy); 1754 break; 1755 default: 1756 break; 1757 } 1758 } 1759 1760 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) 1761 hisi_sas_release_task(hisi_hba, device); 1762 1763 return rc; 1764 } 1765 1766 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun) 1767 { 1768 struct hisi_sas_device *sas_dev = device->lldd_dev; 1769 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1770 struct device *dev = hisi_hba->dev; 1771 int rc = TMF_RESP_FUNC_FAILED; 1772 1773 /* Clear internal IO and then lu reset */ 1774 rc = hisi_sas_internal_task_abort_dev(sas_dev, false); 1775 if (rc < 0) { 1776 dev_err(dev, "lu_reset: internal abort failed\n"); 1777 goto out; 1778 } 1779 hisi_sas_dereg_device(hisi_hba, device); 1780 1781 if (dev_is_sata(device)) { 1782 struct sas_phy *phy; 1783 1784 phy = sas_get_local_phy(device); 1785 1786 rc = sas_phy_reset(phy, true); 1787 1788 if (rc == 0) 1789 hisi_sas_release_task(hisi_hba, device); 1790 sas_put_local_phy(phy); 1791 } else { 1792 rc = sas_lu_reset(device, lun); 1793 if (rc == TMF_RESP_FUNC_COMPLETE) 1794 hisi_sas_release_task(hisi_hba, device); 1795 } 1796 out: 1797 if (rc != TMF_RESP_FUNC_COMPLETE) 1798 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n", 1799 sas_dev->device_id, rc); 1800 return rc; 1801 } 1802 1803 static void hisi_sas_async_I_T_nexus_reset(void *data, async_cookie_t cookie) 1804 { 1805 struct domain_device *device = data; 1806 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1807 int rc; 1808 1809 rc = hisi_sas_debug_I_T_nexus_reset(device); 1810 if (rc != TMF_RESP_FUNC_COMPLETE) 1811 dev_info(hisi_hba->dev, "I_T_nexus reset fail for dev:%016llx rc=%d\n", 1812 SAS_ADDR(device->sas_addr), rc); 1813 } 1814 1815 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha) 1816 { 1817 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 1818 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r); 1819 ASYNC_DOMAIN_EXCLUSIVE(async); 1820 int i; 1821 1822 queue_work(hisi_hba->wq, &r.work); 1823 wait_for_completion(r.completion); 1824 if (!r.done) 1825 return TMF_RESP_FUNC_FAILED; 1826 1827 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1828 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1829 struct domain_device *device = sas_dev->sas_device; 1830 1831 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device || 1832 dev_is_expander(device->dev_type)) 1833 continue; 1834 1835 async_schedule_domain(hisi_sas_async_I_T_nexus_reset, 1836 device, &async); 1837 } 1838 1839 async_synchronize_full_domain(&async); 1840 hisi_sas_release_tasks(hisi_hba); 1841 1842 return TMF_RESP_FUNC_COMPLETE; 1843 } 1844 1845 static int hisi_sas_query_task(struct sas_task *task) 1846 { 1847 int rc = TMF_RESP_FUNC_FAILED; 1848 1849 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { 1850 struct hisi_sas_slot *slot = task->lldd_task; 1851 u32 tag = slot->idx; 1852 1853 rc = sas_query_task(task, tag); 1854 switch (rc) { 1855 /* The task is still in Lun, release it then */ 1856 case TMF_RESP_FUNC_SUCC: 1857 /* The task is not in Lun or failed, reset the phy */ 1858 case TMF_RESP_FUNC_FAILED: 1859 case TMF_RESP_FUNC_COMPLETE: 1860 break; 1861 default: 1862 rc = TMF_RESP_FUNC_FAILED; 1863 break; 1864 } 1865 } 1866 return rc; 1867 } 1868 1869 static bool hisi_sas_internal_abort_timeout(struct sas_task *task, 1870 void *data) 1871 { 1872 struct domain_device *device = task->dev; 1873 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1874 struct hisi_sas_internal_abort_data *timeout = data; 1875 1876 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct) 1877 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work); 1878 1879 if (task->task_state_flags & SAS_TASK_STATE_DONE) { 1880 pr_err("Internal abort: timeout %016llx\n", 1881 SAS_ADDR(device->sas_addr)); 1882 } else { 1883 struct hisi_sas_slot *slot = task->lldd_task; 1884 1885 set_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags); 1886 1887 if (slot) { 1888 struct hisi_sas_cq *cq = 1889 &hisi_hba->cq[slot->dlvry_queue]; 1890 /* 1891 * sync irq to avoid free'ing task 1892 * before using task in IO completion 1893 */ 1894 synchronize_irq(cq->irq_no); 1895 slot->task = NULL; 1896 } 1897 1898 if (timeout->rst_ha_timeout) { 1899 pr_err("Internal abort: timeout and not done %016llx. Queuing reset.\n", 1900 SAS_ADDR(device->sas_addr)); 1901 queue_work(hisi_hba->wq, &hisi_hba->rst_work); 1902 } else { 1903 pr_err("Internal abort: timeout and not done %016llx.\n", 1904 SAS_ADDR(device->sas_addr)); 1905 } 1906 1907 return true; 1908 } 1909 1910 return false; 1911 } 1912 1913 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) 1914 { 1915 hisi_sas_port_notify_formed(sas_phy); 1916 } 1917 1918 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type, 1919 u8 reg_index, u8 reg_count, u8 *write_data) 1920 { 1921 struct hisi_hba *hisi_hba = sha->lldd_ha; 1922 1923 if (!hisi_hba->hw->write_gpio) 1924 return -EOPNOTSUPP; 1925 1926 return hisi_hba->hw->write_gpio(hisi_hba, reg_type, 1927 reg_index, reg_count, write_data); 1928 } 1929 1930 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) 1931 { 1932 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1933 struct sas_phy *sphy = sas_phy->phy; 1934 unsigned long flags; 1935 1936 phy->phy_attached = 0; 1937 phy->phy_type = 0; 1938 phy->port = NULL; 1939 1940 spin_lock_irqsave(&phy->lock, flags); 1941 if (phy->enable) 1942 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN; 1943 else 1944 sphy->negotiated_linkrate = SAS_PHY_DISABLED; 1945 spin_unlock_irqrestore(&phy->lock, flags); 1946 } 1947 1948 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy, 1949 gfp_t gfp_flags) 1950 { 1951 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1952 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1953 struct device *dev = hisi_hba->dev; 1954 1955 if (rdy) { 1956 /* Phy down but ready */ 1957 hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags); 1958 hisi_sas_port_notify_formed(sas_phy); 1959 } else { 1960 struct hisi_sas_port *port = phy->port; 1961 1962 if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags) || 1963 phy->in_reset) { 1964 dev_info(dev, "ignore flutter phy%d down\n", phy_no); 1965 return; 1966 } 1967 /* Phy down and not ready */ 1968 sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags); 1969 sas_phy_disconnected(sas_phy); 1970 1971 if (port) { 1972 if (phy->phy_type & PORT_TYPE_SAS) { 1973 int port_id = port->id; 1974 1975 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, 1976 port_id)) 1977 port->port_attached = 0; 1978 } else if (phy->phy_type & PORT_TYPE_SATA) 1979 port->port_attached = 0; 1980 } 1981 hisi_sas_phy_disconnected(phy); 1982 } 1983 } 1984 EXPORT_SYMBOL_GPL(hisi_sas_phy_down); 1985 1986 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba) 1987 { 1988 int i; 1989 1990 for (i = 0; i < hisi_hba->cq_nvecs; i++) { 1991 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 1992 1993 synchronize_irq(cq->irq_no); 1994 } 1995 } 1996 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs); 1997 1998 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type) 1999 { 2000 struct hisi_hba *hisi_hba = shost_priv(shost); 2001 2002 if (reset_type != SCSI_ADAPTER_RESET) 2003 return -EOPNOTSUPP; 2004 2005 queue_work(hisi_hba->wq, &hisi_hba->rst_work); 2006 2007 return 0; 2008 } 2009 EXPORT_SYMBOL_GPL(hisi_sas_host_reset); 2010 2011 struct scsi_transport_template *hisi_sas_stt; 2012 EXPORT_SYMBOL_GPL(hisi_sas_stt); 2013 2014 static struct sas_domain_function_template hisi_sas_transport_ops = { 2015 .lldd_dev_found = hisi_sas_dev_found, 2016 .lldd_dev_gone = hisi_sas_dev_gone, 2017 .lldd_execute_task = hisi_sas_queue_command, 2018 .lldd_control_phy = hisi_sas_control_phy, 2019 .lldd_abort_task = hisi_sas_abort_task, 2020 .lldd_abort_task_set = hisi_sas_abort_task_set, 2021 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset, 2022 .lldd_lu_reset = hisi_sas_lu_reset, 2023 .lldd_query_task = hisi_sas_query_task, 2024 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha, 2025 .lldd_port_formed = hisi_sas_port_formed, 2026 .lldd_write_gpio = hisi_sas_write_gpio, 2027 .lldd_tmf_aborted = hisi_sas_tmf_aborted, 2028 .lldd_abort_timeout = hisi_sas_internal_abort_timeout, 2029 }; 2030 2031 void hisi_sas_init_mem(struct hisi_hba *hisi_hba) 2032 { 2033 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS; 2034 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint; 2035 2036 for (i = 0; i < hisi_hba->queue_count; i++) { 2037 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2038 struct hisi_sas_dq *dq = &hisi_hba->dq[i]; 2039 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i]; 2040 2041 s = sizeof(struct hisi_sas_cmd_hdr); 2042 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++) 2043 memset(&cmd_hdr[j], 0, s); 2044 2045 dq->wr_point = 0; 2046 2047 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; 2048 memset(hisi_hba->complete_hdr[i], 0, s); 2049 cq->rd_point = 0; 2050 } 2051 2052 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy; 2053 memset(hisi_hba->initial_fis, 0, s); 2054 2055 s = max_command_entries * sizeof(struct hisi_sas_iost); 2056 memset(hisi_hba->iost, 0, s); 2057 2058 s = max_command_entries * sizeof(struct hisi_sas_breakpoint); 2059 memset(hisi_hba->breakpoint, 0, s); 2060 2061 s = sizeof(struct hisi_sas_sata_breakpoint); 2062 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++) 2063 memset(&sata_breakpoint[j], 0, s); 2064 } 2065 EXPORT_SYMBOL_GPL(hisi_sas_init_mem); 2066 2067 int hisi_sas_alloc(struct hisi_hba *hisi_hba) 2068 { 2069 struct device *dev = hisi_hba->dev; 2070 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS; 2071 int max_command_entries_ru, sz_slot_buf_ru; 2072 int blk_cnt, slots_per_blk; 2073 2074 sema_init(&hisi_hba->sem, 1); 2075 spin_lock_init(&hisi_hba->lock); 2076 for (i = 0; i < hisi_hba->n_phy; i++) { 2077 hisi_sas_phy_init(hisi_hba, i); 2078 hisi_hba->port[i].port_attached = 0; 2079 hisi_hba->port[i].id = -1; 2080 } 2081 2082 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 2083 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; 2084 hisi_hba->devices[i].device_id = i; 2085 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT; 2086 } 2087 2088 for (i = 0; i < hisi_hba->queue_count; i++) { 2089 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2090 struct hisi_sas_dq *dq = &hisi_hba->dq[i]; 2091 2092 /* Completion queue structure */ 2093 cq->id = i; 2094 cq->hisi_hba = hisi_hba; 2095 2096 /* Delivery queue structure */ 2097 spin_lock_init(&dq->lock); 2098 INIT_LIST_HEAD(&dq->list); 2099 dq->id = i; 2100 dq->hisi_hba = hisi_hba; 2101 2102 /* Delivery queue */ 2103 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; 2104 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s, 2105 &hisi_hba->cmd_hdr_dma[i], 2106 GFP_KERNEL); 2107 if (!hisi_hba->cmd_hdr[i]) 2108 goto err_out; 2109 2110 /* Completion queue */ 2111 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; 2112 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s, 2113 &hisi_hba->complete_hdr_dma[i], 2114 GFP_KERNEL); 2115 if (!hisi_hba->complete_hdr[i]) 2116 goto err_out; 2117 } 2118 2119 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); 2120 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma, 2121 GFP_KERNEL); 2122 if (!hisi_hba->itct) 2123 goto err_out; 2124 2125 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries, 2126 sizeof(struct hisi_sas_slot), 2127 GFP_KERNEL); 2128 if (!hisi_hba->slot_info) 2129 goto err_out; 2130 2131 /* roundup to avoid overly large block size */ 2132 max_command_entries_ru = roundup(max_command_entries, 64); 2133 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK) 2134 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table); 2135 else 2136 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table); 2137 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64); 2138 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE); 2139 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s; 2140 slots_per_blk = s / sz_slot_buf_ru; 2141 2142 for (i = 0; i < blk_cnt; i++) { 2143 int slot_index = i * slots_per_blk; 2144 dma_addr_t buf_dma; 2145 void *buf; 2146 2147 buf = dmam_alloc_coherent(dev, s, &buf_dma, 2148 GFP_KERNEL); 2149 if (!buf) 2150 goto err_out; 2151 2152 for (j = 0; j < slots_per_blk; j++, slot_index++) { 2153 struct hisi_sas_slot *slot; 2154 2155 slot = &hisi_hba->slot_info[slot_index]; 2156 slot->buf = buf; 2157 slot->buf_dma = buf_dma; 2158 slot->idx = slot_index; 2159 2160 buf += sz_slot_buf_ru; 2161 buf_dma += sz_slot_buf_ru; 2162 } 2163 } 2164 2165 s = max_command_entries * sizeof(struct hisi_sas_iost); 2166 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma, 2167 GFP_KERNEL); 2168 if (!hisi_hba->iost) 2169 goto err_out; 2170 2171 s = max_command_entries * sizeof(struct hisi_sas_breakpoint); 2172 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s, 2173 &hisi_hba->breakpoint_dma, 2174 GFP_KERNEL); 2175 if (!hisi_hba->breakpoint) 2176 goto err_out; 2177 2178 s = hisi_hba->slot_index_count = max_command_entries; 2179 hisi_hba->slot_index_tags = devm_bitmap_zalloc(dev, s, GFP_KERNEL); 2180 if (!hisi_hba->slot_index_tags) 2181 goto err_out; 2182 2183 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; 2184 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s, 2185 &hisi_hba->initial_fis_dma, 2186 GFP_KERNEL); 2187 if (!hisi_hba->initial_fis) 2188 goto err_out; 2189 2190 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint); 2191 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s, 2192 &hisi_hba->sata_breakpoint_dma, 2193 GFP_KERNEL); 2194 if (!hisi_hba->sata_breakpoint) 2195 goto err_out; 2196 2197 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT; 2198 2199 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); 2200 if (!hisi_hba->wq) { 2201 dev_err(dev, "sas_alloc: failed to create workqueue\n"); 2202 goto err_out; 2203 } 2204 2205 return 0; 2206 err_out: 2207 return -ENOMEM; 2208 } 2209 EXPORT_SYMBOL_GPL(hisi_sas_alloc); 2210 2211 void hisi_sas_free(struct hisi_hba *hisi_hba) 2212 { 2213 int i; 2214 2215 for (i = 0; i < hisi_hba->n_phy; i++) { 2216 struct hisi_sas_phy *phy = &hisi_hba->phy[i]; 2217 2218 del_timer_sync(&phy->timer); 2219 } 2220 2221 if (hisi_hba->wq) 2222 destroy_workqueue(hisi_hba->wq); 2223 } 2224 EXPORT_SYMBOL_GPL(hisi_sas_free); 2225 2226 void hisi_sas_rst_work_handler(struct work_struct *work) 2227 { 2228 struct hisi_hba *hisi_hba = 2229 container_of(work, struct hisi_hba, rst_work); 2230 2231 if (hisi_sas_controller_prereset(hisi_hba)) 2232 return; 2233 2234 hisi_sas_controller_reset(hisi_hba); 2235 } 2236 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler); 2237 2238 void hisi_sas_sync_rst_work_handler(struct work_struct *work) 2239 { 2240 struct hisi_sas_rst *rst = 2241 container_of(work, struct hisi_sas_rst, work); 2242 2243 if (hisi_sas_controller_prereset(rst->hisi_hba)) 2244 goto rst_complete; 2245 2246 if (!hisi_sas_controller_reset(rst->hisi_hba)) 2247 rst->done = true; 2248 rst_complete: 2249 complete(rst->completion); 2250 } 2251 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler); 2252 2253 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba) 2254 { 2255 struct device *dev = hisi_hba->dev; 2256 struct platform_device *pdev = hisi_hba->platform_dev; 2257 struct device_node *np = pdev ? pdev->dev.of_node : NULL; 2258 struct clk *refclk; 2259 2260 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr, 2261 SAS_ADDR_SIZE)) { 2262 dev_err(dev, "could not get property sas-addr\n"); 2263 return -ENOENT; 2264 } 2265 2266 if (np) { 2267 /* 2268 * These properties are only required for platform device-based 2269 * controller with DT firmware. 2270 */ 2271 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np, 2272 "hisilicon,sas-syscon"); 2273 if (IS_ERR(hisi_hba->ctrl)) { 2274 dev_err(dev, "could not get syscon\n"); 2275 return -ENOENT; 2276 } 2277 2278 if (device_property_read_u32(dev, "ctrl-reset-reg", 2279 &hisi_hba->ctrl_reset_reg)) { 2280 dev_err(dev, "could not get property ctrl-reset-reg\n"); 2281 return -ENOENT; 2282 } 2283 2284 if (device_property_read_u32(dev, "ctrl-reset-sts-reg", 2285 &hisi_hba->ctrl_reset_sts_reg)) { 2286 dev_err(dev, "could not get property ctrl-reset-sts-reg\n"); 2287 return -ENOENT; 2288 } 2289 2290 if (device_property_read_u32(dev, "ctrl-clock-ena-reg", 2291 &hisi_hba->ctrl_clock_ena_reg)) { 2292 dev_err(dev, "could not get property ctrl-clock-ena-reg\n"); 2293 return -ENOENT; 2294 } 2295 } 2296 2297 refclk = devm_clk_get(dev, NULL); 2298 if (IS_ERR(refclk)) 2299 dev_dbg(dev, "no ref clk property\n"); 2300 else 2301 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000; 2302 2303 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) { 2304 dev_err(dev, "could not get property phy-count\n"); 2305 return -ENOENT; 2306 } 2307 2308 if (device_property_read_u32(dev, "queue-count", 2309 &hisi_hba->queue_count)) { 2310 dev_err(dev, "could not get property queue-count\n"); 2311 return -ENOENT; 2312 } 2313 2314 return 0; 2315 } 2316 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info); 2317 2318 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, 2319 const struct hisi_sas_hw *hw) 2320 { 2321 struct resource *res; 2322 struct Scsi_Host *shost; 2323 struct hisi_hba *hisi_hba; 2324 struct device *dev = &pdev->dev; 2325 int error; 2326 2327 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba)); 2328 if (!shost) { 2329 dev_err(dev, "scsi host alloc failed\n"); 2330 return NULL; 2331 } 2332 hisi_hba = shost_priv(shost); 2333 2334 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler); 2335 hisi_hba->hw = hw; 2336 hisi_hba->dev = dev; 2337 hisi_hba->platform_dev = pdev; 2338 hisi_hba->shost = shost; 2339 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; 2340 2341 timer_setup(&hisi_hba->timer, NULL, 0); 2342 2343 if (hisi_sas_get_fw_info(hisi_hba) < 0) 2344 goto err_out; 2345 2346 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 2347 if (error) { 2348 dev_err(dev, "No usable DMA addressing method\n"); 2349 goto err_out; 2350 } 2351 2352 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0); 2353 if (IS_ERR(hisi_hba->regs)) 2354 goto err_out; 2355 2356 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 2357 if (res) { 2358 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res); 2359 if (IS_ERR(hisi_hba->sgpio_regs)) 2360 goto err_out; 2361 } 2362 2363 if (hisi_sas_alloc(hisi_hba)) { 2364 hisi_sas_free(hisi_hba); 2365 goto err_out; 2366 } 2367 2368 return shost; 2369 err_out: 2370 scsi_host_put(shost); 2371 dev_err(dev, "shost alloc failed\n"); 2372 return NULL; 2373 } 2374 2375 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba) 2376 { 2377 if (hisi_hba->hw->interrupt_preinit) 2378 return hisi_hba->hw->interrupt_preinit(hisi_hba); 2379 return 0; 2380 } 2381 2382 int hisi_sas_probe(struct platform_device *pdev, 2383 const struct hisi_sas_hw *hw) 2384 { 2385 struct Scsi_Host *shost; 2386 struct hisi_hba *hisi_hba; 2387 struct device *dev = &pdev->dev; 2388 struct asd_sas_phy **arr_phy; 2389 struct asd_sas_port **arr_port; 2390 struct sas_ha_struct *sha; 2391 int rc, phy_nr, port_nr, i; 2392 2393 shost = hisi_sas_shost_alloc(pdev, hw); 2394 if (!shost) 2395 return -ENOMEM; 2396 2397 sha = SHOST_TO_SAS_HA(shost); 2398 hisi_hba = shost_priv(shost); 2399 platform_set_drvdata(pdev, sha); 2400 2401 phy_nr = port_nr = hisi_hba->n_phy; 2402 2403 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); 2404 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); 2405 if (!arr_phy || !arr_port) { 2406 rc = -ENOMEM; 2407 goto err_out_ha; 2408 } 2409 2410 sha->sas_phy = arr_phy; 2411 sha->sas_port = arr_port; 2412 sha->lldd_ha = hisi_hba; 2413 2414 shost->transportt = hisi_sas_stt; 2415 shost->max_id = HISI_SAS_MAX_DEVICES; 2416 shost->max_lun = ~0; 2417 shost->max_channel = 1; 2418 shost->max_cmd_len = 16; 2419 if (hisi_hba->hw->slot_index_alloc) { 2420 shost->can_queue = HISI_SAS_MAX_COMMANDS; 2421 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS; 2422 } else { 2423 shost->can_queue = HISI_SAS_UNRESERVED_IPTT; 2424 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT; 2425 } 2426 2427 sha->sas_ha_name = DRV_NAME; 2428 sha->dev = hisi_hba->dev; 2429 sha->lldd_module = THIS_MODULE; 2430 sha->sas_addr = &hisi_hba->sas_addr[0]; 2431 sha->num_phys = hisi_hba->n_phy; 2432 sha->core.shost = hisi_hba->shost; 2433 2434 for (i = 0; i < hisi_hba->n_phy; i++) { 2435 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; 2436 sha->sas_port[i] = &hisi_hba->port[i].sas_port; 2437 } 2438 2439 rc = hisi_sas_interrupt_preinit(hisi_hba); 2440 if (rc) 2441 goto err_out_ha; 2442 2443 rc = scsi_add_host(shost, &pdev->dev); 2444 if (rc) 2445 goto err_out_ha; 2446 2447 rc = sas_register_ha(sha); 2448 if (rc) 2449 goto err_out_register_ha; 2450 2451 rc = hisi_hba->hw->hw_init(hisi_hba); 2452 if (rc) 2453 goto err_out_hw_init; 2454 2455 scsi_scan_host(shost); 2456 2457 return 0; 2458 2459 err_out_hw_init: 2460 sas_unregister_ha(sha); 2461 err_out_register_ha: 2462 scsi_remove_host(shost); 2463 err_out_ha: 2464 hisi_sas_free(hisi_hba); 2465 scsi_host_put(shost); 2466 return rc; 2467 } 2468 EXPORT_SYMBOL_GPL(hisi_sas_probe); 2469 2470 int hisi_sas_remove(struct platform_device *pdev) 2471 { 2472 struct sas_ha_struct *sha = platform_get_drvdata(pdev); 2473 struct hisi_hba *hisi_hba = sha->lldd_ha; 2474 struct Scsi_Host *shost = sha->core.shost; 2475 2476 del_timer_sync(&hisi_hba->timer); 2477 2478 sas_unregister_ha(sha); 2479 sas_remove_host(sha->core.shost); 2480 2481 hisi_sas_free(hisi_hba); 2482 scsi_host_put(shost); 2483 return 0; 2484 } 2485 EXPORT_SYMBOL_GPL(hisi_sas_remove); 2486 2487 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE) 2488 #define DEBUGFS_ENABLE_DEFAULT "enabled" 2489 bool hisi_sas_debugfs_enable = true; 2490 u32 hisi_sas_debugfs_dump_count = 50; 2491 #else 2492 #define DEBUGFS_ENABLE_DEFAULT "disabled" 2493 bool hisi_sas_debugfs_enable; 2494 u32 hisi_sas_debugfs_dump_count = 1; 2495 #endif 2496 2497 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable); 2498 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444); 2499 MODULE_PARM_DESC(hisi_sas_debugfs_enable, 2500 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")"); 2501 2502 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count); 2503 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444); 2504 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow"); 2505 2506 struct dentry *hisi_sas_debugfs_dir; 2507 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir); 2508 2509 static __init int hisi_sas_init(void) 2510 { 2511 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); 2512 if (!hisi_sas_stt) 2513 return -ENOMEM; 2514 2515 if (hisi_sas_debugfs_enable) { 2516 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL); 2517 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) { 2518 pr_info("hisi_sas: Limiting debugfs dump count\n"); 2519 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP; 2520 } 2521 } 2522 2523 return 0; 2524 } 2525 2526 static __exit void hisi_sas_exit(void) 2527 { 2528 sas_release_transport(hisi_sas_stt); 2529 2530 debugfs_remove(hisi_sas_debugfs_dir); 2531 } 2532 2533 module_init(hisi_sas_init); 2534 module_exit(hisi_sas_exit); 2535 2536 MODULE_LICENSE("GPL"); 2537 MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); 2538 MODULE_DESCRIPTION("HISILICON SAS controller driver"); 2539 MODULE_ALIAS("platform:" DRV_NAME); 2540