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