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