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); 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_cmnd->request->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 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba) 210 { 211 int i; 212 213 for (i = 0; i < hisi_hba->slot_index_count; ++i) 214 hisi_sas_slot_index_clear(hisi_hba, i); 215 } 216 217 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, 218 struct hisi_sas_slot *slot) 219 { 220 int device_id = slot->device_id; 221 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id]; 222 223 if (task) { 224 struct device *dev = hisi_hba->dev; 225 226 if (!task->lldd_task) 227 return; 228 229 task->lldd_task = NULL; 230 231 if (!sas_protocol_ata(task->task_proto)) { 232 if (slot->n_elem) 233 dma_unmap_sg(dev, task->scatter, 234 task->num_scatter, 235 task->data_dir); 236 if (slot->n_elem_dif) { 237 struct sas_ssp_task *ssp_task = &task->ssp_task; 238 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd; 239 240 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 241 scsi_prot_sg_count(scsi_cmnd), 242 task->data_dir); 243 } 244 } 245 } 246 247 spin_lock(&sas_dev->lock); 248 list_del_init(&slot->entry); 249 spin_unlock(&sas_dev->lock); 250 251 memset(slot, 0, offsetof(struct hisi_sas_slot, buf)); 252 253 hisi_sas_slot_index_free(hisi_hba, slot->idx); 254 } 255 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); 256 257 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba, 258 struct hisi_sas_slot *slot) 259 { 260 hisi_hba->hw->prep_smp(hisi_hba, slot); 261 } 262 263 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, 264 struct hisi_sas_slot *slot) 265 { 266 hisi_hba->hw->prep_ssp(hisi_hba, slot); 267 } 268 269 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba, 270 struct hisi_sas_slot *slot) 271 { 272 hisi_hba->hw->prep_stp(hisi_hba, slot); 273 } 274 275 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba, 276 struct hisi_sas_slot *slot, 277 int device_id, int abort_flag, int tag_to_abort) 278 { 279 hisi_hba->hw->prep_abort(hisi_hba, slot, 280 device_id, abort_flag, tag_to_abort); 281 } 282 283 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba, 284 struct sas_task *task, int n_elem, 285 int n_elem_req) 286 { 287 struct device *dev = hisi_hba->dev; 288 289 if (!sas_protocol_ata(task->task_proto)) { 290 if (task->num_scatter) { 291 if (n_elem) 292 dma_unmap_sg(dev, task->scatter, 293 task->num_scatter, 294 task->data_dir); 295 } else if (task->task_proto & SAS_PROTOCOL_SMP) { 296 if (n_elem_req) 297 dma_unmap_sg(dev, &task->smp_task.smp_req, 298 1, DMA_TO_DEVICE); 299 } 300 } 301 } 302 303 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba, 304 struct sas_task *task, int *n_elem, 305 int *n_elem_req) 306 { 307 struct device *dev = hisi_hba->dev; 308 int rc; 309 310 if (sas_protocol_ata(task->task_proto)) { 311 *n_elem = task->num_scatter; 312 } else { 313 unsigned int req_len; 314 315 if (task->num_scatter) { 316 *n_elem = dma_map_sg(dev, task->scatter, 317 task->num_scatter, task->data_dir); 318 if (!*n_elem) { 319 rc = -ENOMEM; 320 goto prep_out; 321 } 322 } else if (task->task_proto & SAS_PROTOCOL_SMP) { 323 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req, 324 1, DMA_TO_DEVICE); 325 if (!*n_elem_req) { 326 rc = -ENOMEM; 327 goto prep_out; 328 } 329 req_len = sg_dma_len(&task->smp_task.smp_req); 330 if (req_len & 0x3) { 331 rc = -EINVAL; 332 goto err_out_dma_unmap; 333 } 334 } 335 } 336 337 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) { 338 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n", 339 *n_elem); 340 rc = -EINVAL; 341 goto err_out_dma_unmap; 342 } 343 return 0; 344 345 err_out_dma_unmap: 346 /* It would be better to call dma_unmap_sg() here, but it's messy */ 347 hisi_sas_dma_unmap(hisi_hba, task, *n_elem, 348 *n_elem_req); 349 prep_out: 350 return rc; 351 } 352 353 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba, 354 struct sas_task *task, int n_elem_dif) 355 { 356 struct device *dev = hisi_hba->dev; 357 358 if (n_elem_dif) { 359 struct sas_ssp_task *ssp_task = &task->ssp_task; 360 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd; 361 362 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 363 scsi_prot_sg_count(scsi_cmnd), 364 task->data_dir); 365 } 366 } 367 368 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba, 369 int *n_elem_dif, struct sas_task *task) 370 { 371 struct device *dev = hisi_hba->dev; 372 struct sas_ssp_task *ssp_task; 373 struct scsi_cmnd *scsi_cmnd; 374 int rc; 375 376 if (task->num_scatter) { 377 ssp_task = &task->ssp_task; 378 scsi_cmnd = ssp_task->cmd; 379 380 if (scsi_prot_sg_count(scsi_cmnd)) { 381 *n_elem_dif = dma_map_sg(dev, 382 scsi_prot_sglist(scsi_cmnd), 383 scsi_prot_sg_count(scsi_cmnd), 384 task->data_dir); 385 386 if (!*n_elem_dif) 387 return -ENOMEM; 388 389 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) { 390 dev_err(dev, "task prep: n_elem_dif(%d) too large\n", 391 *n_elem_dif); 392 rc = -EINVAL; 393 goto err_out_dif_dma_unmap; 394 } 395 } 396 } 397 398 return 0; 399 400 err_out_dif_dma_unmap: 401 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd), 402 scsi_prot_sg_count(scsi_cmnd), task->data_dir); 403 return rc; 404 } 405 406 static int hisi_sas_task_prep(struct sas_task *task, 407 struct hisi_sas_dq **dq_pointer, 408 bool is_tmf, struct hisi_sas_tmf_task *tmf, 409 int *pass) 410 { 411 struct domain_device *device = task->dev; 412 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 413 struct hisi_sas_device *sas_dev = device->lldd_dev; 414 struct hisi_sas_port *port; 415 struct hisi_sas_slot *slot; 416 struct hisi_sas_cmd_hdr *cmd_hdr_base; 417 struct asd_sas_port *sas_port = device->port; 418 struct device *dev = hisi_hba->dev; 419 int dlvry_queue_slot, dlvry_queue, rc, slot_idx; 420 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0; 421 struct scsi_cmnd *scmd = NULL; 422 struct hisi_sas_dq *dq; 423 unsigned long flags; 424 int wr_q_index; 425 426 if (DEV_IS_GONE(sas_dev)) { 427 if (sas_dev) 428 dev_info(dev, "task prep: device %d not ready\n", 429 sas_dev->device_id); 430 else 431 dev_info(dev, "task prep: device %016llx not ready\n", 432 SAS_ADDR(device->sas_addr)); 433 434 return -ECOMM; 435 } 436 437 if (task->uldd_task) { 438 struct ata_queued_cmd *qc; 439 440 if (dev_is_sata(device)) { 441 qc = task->uldd_task; 442 scmd = qc->scsicmd; 443 } else { 444 scmd = task->uldd_task; 445 } 446 } 447 448 if (scmd && hisi_hba->shost->nr_hw_queues) { 449 unsigned int dq_index; 450 u32 blk_tag; 451 452 blk_tag = blk_mq_unique_tag(scmd->request); 453 dq_index = blk_mq_unique_tag_to_hwq(blk_tag); 454 *dq_pointer = dq = &hisi_hba->dq[dq_index]; 455 } else if (hisi_hba->shost->nr_hw_queues) { 456 struct Scsi_Host *shost = hisi_hba->shost; 457 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT]; 458 int queue = qmap->mq_map[raw_smp_processor_id()]; 459 460 *dq_pointer = dq = &hisi_hba->dq[queue]; 461 } else { 462 *dq_pointer = dq = sas_dev->dq; 463 } 464 465 port = to_hisi_sas_port(sas_port); 466 if (port && !port->port_attached) { 467 dev_info(dev, "task prep: %s port%d not attach device\n", 468 (dev_is_sata(device)) ? 469 "SATA/STP" : "SAS", 470 device->port->id); 471 472 return -ECOMM; 473 } 474 475 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem, 476 &n_elem_req); 477 if (rc < 0) 478 goto prep_out; 479 480 if (!sas_protocol_ata(task->task_proto)) { 481 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task); 482 if (rc < 0) 483 goto err_out_dma_unmap; 484 } 485 486 if (hisi_hba->hw->slot_index_alloc) 487 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device); 488 else 489 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd); 490 491 if (rc < 0) 492 goto err_out_dif_dma_unmap; 493 494 slot_idx = rc; 495 slot = &hisi_hba->slot_info[slot_idx]; 496 497 spin_lock(&dq->lock); 498 wr_q_index = dq->wr_point; 499 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS; 500 list_add_tail(&slot->delivery, &dq->list); 501 spin_unlock(&dq->lock); 502 spin_lock(&sas_dev->lock); 503 list_add_tail(&slot->entry, &sas_dev->list); 504 spin_unlock(&sas_dev->lock); 505 506 dlvry_queue = dq->id; 507 dlvry_queue_slot = wr_q_index; 508 509 slot->device_id = sas_dev->device_id; 510 slot->n_elem = n_elem; 511 slot->n_elem_dif = n_elem_dif; 512 slot->dlvry_queue = dlvry_queue; 513 slot->dlvry_queue_slot = dlvry_queue_slot; 514 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; 515 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; 516 slot->task = task; 517 slot->port = port; 518 slot->tmf = tmf; 519 slot->is_internal = is_tmf; 520 task->lldd_task = slot; 521 522 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); 523 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ); 524 memset(hisi_sas_status_buf_addr_mem(slot), 0, 525 sizeof(struct hisi_sas_err_record)); 526 527 switch (task->task_proto) { 528 case SAS_PROTOCOL_SMP: 529 hisi_sas_task_prep_smp(hisi_hba, slot); 530 break; 531 case SAS_PROTOCOL_SSP: 532 hisi_sas_task_prep_ssp(hisi_hba, slot); 533 break; 534 case SAS_PROTOCOL_SATA: 535 case SAS_PROTOCOL_STP: 536 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: 537 hisi_sas_task_prep_ata(hisi_hba, slot); 538 break; 539 default: 540 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n", 541 task->task_proto); 542 break; 543 } 544 545 spin_lock_irqsave(&task->task_state_lock, flags); 546 task->task_state_flags |= SAS_TASK_AT_INITIATOR; 547 spin_unlock_irqrestore(&task->task_state_lock, flags); 548 549 ++(*pass); 550 WRITE_ONCE(slot->ready, 1); 551 552 return 0; 553 554 err_out_dif_dma_unmap: 555 if (!sas_protocol_ata(task->task_proto)) 556 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif); 557 err_out_dma_unmap: 558 hisi_sas_dma_unmap(hisi_hba, task, n_elem, 559 n_elem_req); 560 prep_out: 561 dev_err(dev, "task prep: failed[%d]!\n", rc); 562 return rc; 563 } 564 565 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags, 566 bool is_tmf, struct hisi_sas_tmf_task *tmf) 567 { 568 u32 rc; 569 u32 pass = 0; 570 struct hisi_hba *hisi_hba; 571 struct device *dev; 572 struct domain_device *device = task->dev; 573 struct asd_sas_port *sas_port = device->port; 574 struct hisi_sas_dq *dq = NULL; 575 576 if (!sas_port) { 577 struct task_status_struct *ts = &task->task_status; 578 579 ts->resp = SAS_TASK_UNDELIVERED; 580 ts->stat = SAS_PHY_DOWN; 581 /* 582 * libsas will use dev->port, should 583 * not call task_done for sata 584 */ 585 if (device->dev_type != SAS_SATA_DEV) 586 task->task_done(task); 587 return -ECOMM; 588 } 589 590 hisi_hba = dev_to_hisi_hba(device); 591 dev = hisi_hba->dev; 592 593 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) { 594 /* 595 * For IOs from upper layer, it may already disable preempt 596 * in the IO path, if disable preempt again in down(), 597 * function schedule() will report schedule_bug(), so check 598 * preemptible() before goto down(). 599 */ 600 if (!preemptible()) 601 return -EINVAL; 602 603 down(&hisi_hba->sem); 604 up(&hisi_hba->sem); 605 } 606 607 /* protect task_prep and start_delivery sequence */ 608 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass); 609 if (rc) 610 dev_err(dev, "task exec: failed[%d]!\n", rc); 611 612 if (likely(pass)) { 613 spin_lock(&dq->lock); 614 hisi_hba->hw->start_delivery(dq); 615 spin_unlock(&dq->lock); 616 } 617 618 return rc; 619 } 620 621 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no) 622 { 623 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 624 struct asd_sas_phy *sas_phy = &phy->sas_phy; 625 struct sas_ha_struct *sas_ha; 626 627 if (!phy->phy_attached) 628 return; 629 630 if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) && 631 !sas_phy->suspended) { 632 dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no); 633 return; 634 } 635 636 sas_ha = &hisi_hba->sha; 637 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE); 638 639 if (sas_phy->phy) { 640 struct sas_phy *sphy = sas_phy->phy; 641 642 sphy->negotiated_linkrate = sas_phy->linkrate; 643 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; 644 sphy->maximum_linkrate_hw = 645 hisi_hba->hw->phy_get_max_linkrate(); 646 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) 647 sphy->minimum_linkrate = phy->minimum_linkrate; 648 649 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) 650 sphy->maximum_linkrate = phy->maximum_linkrate; 651 } 652 653 if (phy->phy_type & PORT_TYPE_SAS) { 654 struct sas_identify_frame *id; 655 656 id = (struct sas_identify_frame *)phy->frame_rcvd; 657 id->dev_type = phy->identify.device_type; 658 id->initiator_bits = SAS_PROTOCOL_ALL; 659 id->target_bits = phy->identify.target_port_protocols; 660 } else if (phy->phy_type & PORT_TYPE_SATA) { 661 /* Nothing */ 662 } 663 664 sas_phy->frame_rcvd_size = phy->frame_rcvd_size; 665 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED); 666 } 667 668 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) 669 { 670 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 671 struct hisi_sas_device *sas_dev = NULL; 672 int last = hisi_hba->last_dev_id; 673 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES; 674 int i; 675 676 spin_lock(&hisi_hba->lock); 677 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) { 678 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { 679 int queue = i % hisi_hba->queue_count; 680 struct hisi_sas_dq *dq = &hisi_hba->dq[queue]; 681 682 hisi_hba->devices[i].device_id = i; 683 sas_dev = &hisi_hba->devices[i]; 684 sas_dev->dev_status = HISI_SAS_DEV_INIT; 685 sas_dev->dev_type = device->dev_type; 686 sas_dev->hisi_hba = hisi_hba; 687 sas_dev->sas_device = device; 688 sas_dev->dq = dq; 689 spin_lock_init(&sas_dev->lock); 690 INIT_LIST_HEAD(&hisi_hba->devices[i].list); 691 break; 692 } 693 i++; 694 } 695 hisi_hba->last_dev_id = i; 696 spin_unlock(&hisi_hba->lock); 697 698 return sas_dev; 699 } 700 701 #define HISI_SAS_DISK_RECOVER_CNT 3 702 static int hisi_sas_init_device(struct domain_device *device) 703 { 704 int rc = TMF_RESP_FUNC_COMPLETE; 705 struct scsi_lun lun; 706 struct hisi_sas_tmf_task tmf_task; 707 int retry = HISI_SAS_DISK_RECOVER_CNT; 708 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 709 struct device *dev = hisi_hba->dev; 710 struct sas_phy *local_phy; 711 712 switch (device->dev_type) { 713 case SAS_END_DEVICE: 714 int_to_scsilun(0, &lun); 715 716 tmf_task.tmf = TMF_CLEAR_TASK_SET; 717 while (retry-- > 0) { 718 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun, 719 &tmf_task); 720 if (rc == TMF_RESP_FUNC_COMPLETE) { 721 hisi_sas_release_task(hisi_hba, device); 722 break; 723 } 724 } 725 break; 726 case SAS_SATA_DEV: 727 case SAS_SATA_PM: 728 case SAS_SATA_PM_PORT: 729 case SAS_SATA_PENDING: 730 /* 731 * send HARD RESET to clear previous affiliation of 732 * STP target port 733 */ 734 local_phy = sas_get_local_phy(device); 735 if (!scsi_is_sas_phy_local(local_phy) && 736 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) { 737 unsigned long deadline = ata_deadline(jiffies, 20000); 738 struct sata_device *sata_dev = &device->sata_dev; 739 struct ata_host *ata_host = sata_dev->ata_host; 740 struct ata_port_operations *ops = ata_host->ops; 741 struct ata_port *ap = sata_dev->ap; 742 struct ata_link *link; 743 unsigned int classes; 744 745 ata_for_each_link(link, ap, EDGE) 746 rc = ops->hardreset(link, &classes, 747 deadline); 748 } 749 sas_put_local_phy(local_phy); 750 if (rc) { 751 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc); 752 return rc; 753 } 754 755 while (retry-- > 0) { 756 rc = hisi_sas_softreset_ata_disk(device); 757 if (!rc) 758 break; 759 } 760 break; 761 default: 762 break; 763 } 764 765 return rc; 766 } 767 768 static int hisi_sas_dev_found(struct domain_device *device) 769 { 770 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 771 struct domain_device *parent_dev = device->parent; 772 struct hisi_sas_device *sas_dev; 773 struct device *dev = hisi_hba->dev; 774 int rc; 775 776 if (hisi_hba->hw->alloc_dev) 777 sas_dev = hisi_hba->hw->alloc_dev(device); 778 else 779 sas_dev = hisi_sas_alloc_dev(device); 780 if (!sas_dev) { 781 dev_err(dev, "fail alloc dev: max support %d devices\n", 782 HISI_SAS_MAX_DEVICES); 783 return -EINVAL; 784 } 785 786 device->lldd_dev = sas_dev; 787 hisi_hba->hw->setup_itct(hisi_hba, sas_dev); 788 789 if (parent_dev && dev_is_expander(parent_dev->dev_type)) { 790 int phy_no; 791 u8 phy_num = parent_dev->ex_dev.num_phys; 792 struct ex_phy *phy; 793 794 for (phy_no = 0; phy_no < phy_num; phy_no++) { 795 phy = &parent_dev->ex_dev.ex_phy[phy_no]; 796 if (SAS_ADDR(phy->attached_sas_addr) == 797 SAS_ADDR(device->sas_addr)) 798 break; 799 } 800 801 if (phy_no == phy_num) { 802 dev_info(dev, "dev found: no attached " 803 "dev:%016llx at ex:%016llx\n", 804 SAS_ADDR(device->sas_addr), 805 SAS_ADDR(parent_dev->sas_addr)); 806 rc = -EINVAL; 807 goto err_out; 808 } 809 } 810 811 dev_info(dev, "dev[%d:%x] found\n", 812 sas_dev->device_id, sas_dev->dev_type); 813 814 rc = hisi_sas_init_device(device); 815 if (rc) 816 goto err_out; 817 sas_dev->dev_status = HISI_SAS_DEV_NORMAL; 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(struct work_struct *work) 862 { 863 struct hisi_sas_phy *phy = 864 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]); 865 struct hisi_hba *hisi_hba = phy->hisi_hba; 866 struct asd_sas_phy *sas_phy = &phy->sas_phy; 867 int phy_no = sas_phy->id; 868 869 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP) 870 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no); 871 hisi_sas_bytes_dmaed(hisi_hba, phy_no); 872 } 873 874 static void hisi_sas_linkreset_work(struct work_struct *work) 875 { 876 struct hisi_sas_phy *phy = 877 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]); 878 struct asd_sas_phy *sas_phy = &phy->sas_phy; 879 880 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL); 881 } 882 883 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = { 884 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work, 885 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work, 886 }; 887 888 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy, 889 enum hisi_sas_phy_event event) 890 { 891 struct hisi_hba *hisi_hba = phy->hisi_hba; 892 893 if (WARN_ON(event >= HISI_PHYES_NUM)) 894 return false; 895 896 return queue_work(hisi_hba->wq, &phy->works[event]); 897 } 898 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event); 899 900 static void hisi_sas_wait_phyup_timedout(struct timer_list *t) 901 { 902 struct hisi_sas_phy *phy = from_timer(phy, t, timer); 903 struct hisi_hba *hisi_hba = phy->hisi_hba; 904 struct device *dev = hisi_hba->dev; 905 int phy_no = phy->sas_phy.id; 906 907 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no); 908 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET); 909 } 910 911 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no) 912 { 913 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 914 struct device *dev = hisi_hba->dev; 915 916 dev_dbg(dev, "phy%d OOB ready\n", phy_no); 917 if (phy->phy_attached) 918 return; 919 920 if (!timer_pending(&phy->timer)) { 921 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ; 922 add_timer(&phy->timer); 923 } 924 } 925 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready); 926 927 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) 928 { 929 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 930 struct asd_sas_phy *sas_phy = &phy->sas_phy; 931 int i; 932 933 phy->hisi_hba = hisi_hba; 934 phy->port = NULL; 935 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS; 936 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate(); 937 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; 938 sas_phy->class = SAS; 939 sas_phy->iproto = SAS_PROTOCOL_ALL; 940 sas_phy->tproto = 0; 941 sas_phy->type = PHY_TYPE_PHYSICAL; 942 sas_phy->role = PHY_ROLE_INITIATOR; 943 sas_phy->oob_mode = OOB_NOT_CONNECTED; 944 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; 945 sas_phy->id = phy_no; 946 sas_phy->sas_addr = &hisi_hba->sas_addr[0]; 947 sas_phy->frame_rcvd = &phy->frame_rcvd[0]; 948 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; 949 sas_phy->lldd_phy = phy; 950 951 for (i = 0; i < HISI_PHYES_NUM; i++) 952 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]); 953 954 spin_lock_init(&phy->lock); 955 956 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0); 957 } 958 959 /* Wrapper to ensure we track hisi_sas_phy.enable properly */ 960 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable) 961 { 962 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 963 struct asd_sas_phy *aphy = &phy->sas_phy; 964 struct sas_phy *sphy = aphy->phy; 965 unsigned long flags; 966 967 spin_lock_irqsave(&phy->lock, flags); 968 969 if (enable) { 970 /* We may have been enabled already; if so, don't touch */ 971 if (!phy->enable) 972 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN; 973 hisi_hba->hw->phy_start(hisi_hba, phy_no); 974 } else { 975 sphy->negotiated_linkrate = SAS_PHY_DISABLED; 976 hisi_hba->hw->phy_disable(hisi_hba, phy_no); 977 } 978 phy->enable = enable; 979 spin_unlock_irqrestore(&phy->lock, flags); 980 } 981 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable); 982 983 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) 984 { 985 struct sas_ha_struct *sas_ha = sas_phy->ha; 986 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 987 struct hisi_sas_phy *phy = sas_phy->lldd_phy; 988 struct asd_sas_port *sas_port = sas_phy->port; 989 struct hisi_sas_port *port; 990 unsigned long flags; 991 992 if (!sas_port) 993 return; 994 995 port = to_hisi_sas_port(sas_port); 996 spin_lock_irqsave(&hisi_hba->lock, flags); 997 port->port_attached = 1; 998 port->id = phy->port_id; 999 phy->port = port; 1000 sas_port->lldd_port = port; 1001 spin_unlock_irqrestore(&hisi_hba->lock, flags); 1002 } 1003 1004 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task, 1005 struct hisi_sas_slot *slot) 1006 { 1007 if (task) { 1008 unsigned long flags; 1009 struct task_status_struct *ts; 1010 1011 ts = &task->task_status; 1012 1013 ts->resp = SAS_TASK_COMPLETE; 1014 ts->stat = SAS_ABORTED_TASK; 1015 spin_lock_irqsave(&task->task_state_lock, flags); 1016 task->task_state_flags &= 1017 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR); 1018 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP) 1019 task->task_state_flags |= SAS_TASK_STATE_DONE; 1020 spin_unlock_irqrestore(&task->task_state_lock, flags); 1021 } 1022 1023 hisi_sas_slot_task_free(hisi_hba, task, slot); 1024 } 1025 1026 static void hisi_sas_release_task(struct hisi_hba *hisi_hba, 1027 struct domain_device *device) 1028 { 1029 struct hisi_sas_slot *slot, *slot2; 1030 struct hisi_sas_device *sas_dev = device->lldd_dev; 1031 1032 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry) 1033 hisi_sas_do_release_task(hisi_hba, slot->task, slot); 1034 } 1035 1036 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba) 1037 { 1038 struct hisi_sas_device *sas_dev; 1039 struct domain_device *device; 1040 int i; 1041 1042 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1043 sas_dev = &hisi_hba->devices[i]; 1044 device = sas_dev->sas_device; 1045 1046 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || 1047 !device) 1048 continue; 1049 1050 hisi_sas_release_task(hisi_hba, device); 1051 } 1052 } 1053 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks); 1054 1055 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba, 1056 struct domain_device *device) 1057 { 1058 if (hisi_hba->hw->dereg_device) 1059 hisi_hba->hw->dereg_device(hisi_hba, device); 1060 } 1061 1062 static void hisi_sas_dev_gone(struct domain_device *device) 1063 { 1064 struct hisi_sas_device *sas_dev = device->lldd_dev; 1065 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1066 struct device *dev = hisi_hba->dev; 1067 int ret = 0; 1068 1069 dev_info(dev, "dev[%d:%x] is gone\n", 1070 sas_dev->device_id, sas_dev->dev_type); 1071 1072 down(&hisi_hba->sem); 1073 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) { 1074 hisi_sas_internal_task_abort(hisi_hba, device, 1075 HISI_SAS_INT_ABT_DEV, 0); 1076 1077 hisi_sas_dereg_device(hisi_hba, device); 1078 1079 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev); 1080 device->lldd_dev = NULL; 1081 } 1082 1083 if (hisi_hba->hw->free_device) 1084 hisi_hba->hw->free_device(sas_dev); 1085 1086 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */ 1087 if (!ret) 1088 sas_dev->dev_type = SAS_PHY_UNUSED; 1089 sas_dev->sas_device = NULL; 1090 up(&hisi_hba->sem); 1091 } 1092 1093 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) 1094 { 1095 return hisi_sas_task_exec(task, gfp_flags, 0, NULL); 1096 } 1097 1098 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no, 1099 struct sas_phy_linkrates *r) 1100 { 1101 struct sas_phy_linkrates _r; 1102 1103 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1104 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1105 enum sas_linkrate min, max; 1106 1107 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS) 1108 return -EINVAL; 1109 1110 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) { 1111 max = sas_phy->phy->maximum_linkrate; 1112 min = r->minimum_linkrate; 1113 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) { 1114 max = r->maximum_linkrate; 1115 min = sas_phy->phy->minimum_linkrate; 1116 } else 1117 return -EINVAL; 1118 1119 _r.maximum_linkrate = max; 1120 _r.minimum_linkrate = min; 1121 1122 sas_phy->phy->maximum_linkrate = max; 1123 sas_phy->phy->minimum_linkrate = min; 1124 1125 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1126 msleep(100); 1127 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r); 1128 hisi_sas_phy_enable(hisi_hba, phy_no, 1); 1129 1130 return 0; 1131 } 1132 1133 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, 1134 void *funcdata) 1135 { 1136 struct sas_ha_struct *sas_ha = sas_phy->ha; 1137 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 1138 int phy_no = sas_phy->id; 1139 1140 switch (func) { 1141 case PHY_FUNC_HARD_RESET: 1142 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); 1143 break; 1144 1145 case PHY_FUNC_LINK_RESET: 1146 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1147 msleep(100); 1148 hisi_sas_phy_enable(hisi_hba, phy_no, 1); 1149 break; 1150 1151 case PHY_FUNC_DISABLE: 1152 hisi_sas_phy_enable(hisi_hba, phy_no, 0); 1153 break; 1154 1155 case PHY_FUNC_SET_LINK_RATE: 1156 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata); 1157 case PHY_FUNC_GET_EVENTS: 1158 if (hisi_hba->hw->get_events) { 1159 hisi_hba->hw->get_events(hisi_hba, phy_no); 1160 break; 1161 } 1162 fallthrough; 1163 case PHY_FUNC_RELEASE_SPINUP_HOLD: 1164 default: 1165 return -EOPNOTSUPP; 1166 } 1167 return 0; 1168 } 1169 1170 static void hisi_sas_task_done(struct sas_task *task) 1171 { 1172 del_timer(&task->slow_task->timer); 1173 complete(&task->slow_task->completion); 1174 } 1175 1176 static void hisi_sas_tmf_timedout(struct timer_list *t) 1177 { 1178 struct sas_task_slow *slow = from_timer(slow, t, timer); 1179 struct sas_task *task = slow->task; 1180 unsigned long flags; 1181 bool is_completed = true; 1182 1183 spin_lock_irqsave(&task->task_state_lock, flags); 1184 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { 1185 task->task_state_flags |= SAS_TASK_STATE_ABORTED; 1186 is_completed = false; 1187 } 1188 spin_unlock_irqrestore(&task->task_state_lock, flags); 1189 1190 if (!is_completed) 1191 complete(&task->slow_task->completion); 1192 } 1193 1194 #define TASK_TIMEOUT 20 1195 #define TASK_RETRY 3 1196 #define INTERNAL_ABORT_TIMEOUT 6 1197 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device, 1198 void *parameter, u32 para_len, 1199 struct hisi_sas_tmf_task *tmf) 1200 { 1201 struct hisi_sas_device *sas_dev = device->lldd_dev; 1202 struct hisi_hba *hisi_hba = sas_dev->hisi_hba; 1203 struct device *dev = hisi_hba->dev; 1204 struct sas_task *task; 1205 int res, retry; 1206 1207 for (retry = 0; retry < TASK_RETRY; retry++) { 1208 task = sas_alloc_slow_task(GFP_KERNEL); 1209 if (!task) 1210 return -ENOMEM; 1211 1212 task->dev = device; 1213 task->task_proto = device->tproto; 1214 1215 if (dev_is_sata(device)) { 1216 task->ata_task.device_control_reg_update = 1; 1217 memcpy(&task->ata_task.fis, parameter, para_len); 1218 } else { 1219 memcpy(&task->ssp_task, parameter, para_len); 1220 } 1221 task->task_done = hisi_sas_task_done; 1222 1223 task->slow_task->timer.function = hisi_sas_tmf_timedout; 1224 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ; 1225 add_timer(&task->slow_task->timer); 1226 1227 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf); 1228 1229 if (res) { 1230 del_timer(&task->slow_task->timer); 1231 dev_err(dev, "abort tmf: executing internal task failed: %d\n", 1232 res); 1233 goto ex_err; 1234 } 1235 1236 wait_for_completion(&task->slow_task->completion); 1237 res = TMF_RESP_FUNC_FAILED; 1238 /* Even TMF timed out, return direct. */ 1239 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 1240 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { 1241 struct hisi_sas_slot *slot = task->lldd_task; 1242 1243 dev_err(dev, "abort tmf: TMF task timeout and not done\n"); 1244 if (slot) { 1245 struct hisi_sas_cq *cq = 1246 &hisi_hba->cq[slot->dlvry_queue]; 1247 /* 1248 * sync irq to avoid free'ing task 1249 * before using task in IO completion 1250 */ 1251 synchronize_irq(cq->irq_no); 1252 slot->task = NULL; 1253 } 1254 1255 goto ex_err; 1256 } else 1257 dev_err(dev, "abort tmf: TMF task timeout\n"); 1258 } 1259 1260 if (task->task_status.resp == SAS_TASK_COMPLETE && 1261 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { 1262 res = TMF_RESP_FUNC_COMPLETE; 1263 break; 1264 } 1265 1266 if (task->task_status.resp == SAS_TASK_COMPLETE && 1267 task->task_status.stat == TMF_RESP_FUNC_SUCC) { 1268 res = TMF_RESP_FUNC_SUCC; 1269 break; 1270 } 1271 1272 if (task->task_status.resp == SAS_TASK_COMPLETE && 1273 task->task_status.stat == SAS_DATA_UNDERRUN) { 1274 /* no error, but return the number of bytes of 1275 * underrun 1276 */ 1277 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n", 1278 SAS_ADDR(device->sas_addr), 1279 task->task_status.resp, 1280 task->task_status.stat); 1281 res = task->task_status.residual; 1282 break; 1283 } 1284 1285 if (task->task_status.resp == SAS_TASK_COMPLETE && 1286 task->task_status.stat == SAS_DATA_OVERRUN) { 1287 dev_warn(dev, "abort tmf: blocked task error\n"); 1288 res = -EMSGSIZE; 1289 break; 1290 } 1291 1292 if (task->task_status.resp == SAS_TASK_COMPLETE && 1293 task->task_status.stat == SAS_OPEN_REJECT) { 1294 dev_warn(dev, "abort tmf: open reject failed\n"); 1295 res = -EIO; 1296 } else { 1297 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n", 1298 SAS_ADDR(device->sas_addr), 1299 task->task_status.resp, 1300 task->task_status.stat); 1301 } 1302 sas_free_task(task); 1303 task = NULL; 1304 } 1305 ex_err: 1306 if (retry == TASK_RETRY) 1307 dev_warn(dev, "abort tmf: executing internal task failed!\n"); 1308 sas_free_task(task); 1309 return res; 1310 } 1311 1312 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev, 1313 bool reset, int pmp, u8 *fis) 1314 { 1315 struct ata_taskfile tf; 1316 1317 ata_tf_init(dev, &tf); 1318 if (reset) 1319 tf.ctl |= ATA_SRST; 1320 else 1321 tf.ctl &= ~ATA_SRST; 1322 tf.command = ATA_CMD_DEV_RESET; 1323 ata_tf_to_fis(&tf, pmp, 0, fis); 1324 } 1325 1326 static int hisi_sas_softreset_ata_disk(struct domain_device *device) 1327 { 1328 u8 fis[20] = {0}; 1329 struct ata_port *ap = device->sata_dev.ap; 1330 struct ata_link *link; 1331 int rc = TMF_RESP_FUNC_FAILED; 1332 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1333 struct device *dev = hisi_hba->dev; 1334 int s = sizeof(struct host_to_dev_fis); 1335 1336 ata_for_each_link(link, ap, EDGE) { 1337 int pmp = sata_srst_pmp(link); 1338 1339 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis); 1340 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL); 1341 if (rc != TMF_RESP_FUNC_COMPLETE) 1342 break; 1343 } 1344 1345 if (rc == TMF_RESP_FUNC_COMPLETE) { 1346 ata_for_each_link(link, ap, EDGE) { 1347 int pmp = sata_srst_pmp(link); 1348 1349 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis); 1350 rc = hisi_sas_exec_internal_tmf_task(device, fis, 1351 s, NULL); 1352 if (rc != TMF_RESP_FUNC_COMPLETE) 1353 dev_err(dev, "ata disk de-reset failed\n"); 1354 } 1355 } else { 1356 dev_err(dev, "ata disk reset failed\n"); 1357 } 1358 1359 if (rc == TMF_RESP_FUNC_COMPLETE) 1360 hisi_sas_release_task(hisi_hba, device); 1361 1362 return rc; 1363 } 1364 1365 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, 1366 u8 *lun, struct hisi_sas_tmf_task *tmf) 1367 { 1368 struct sas_ssp_task ssp_task; 1369 1370 if (!(device->tproto & SAS_PROTOCOL_SSP)) 1371 return TMF_RESP_FUNC_ESUPP; 1372 1373 memcpy(ssp_task.LUN, lun, 8); 1374 1375 return hisi_sas_exec_internal_tmf_task(device, &ssp_task, 1376 sizeof(ssp_task), tmf); 1377 } 1378 1379 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba) 1380 { 1381 u32 state = hisi_hba->hw->get_phys_state(hisi_hba); 1382 int i; 1383 1384 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1385 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1386 struct domain_device *device = sas_dev->sas_device; 1387 struct asd_sas_port *sas_port; 1388 struct hisi_sas_port *port; 1389 struct hisi_sas_phy *phy = NULL; 1390 struct asd_sas_phy *sas_phy; 1391 1392 if ((sas_dev->dev_type == SAS_PHY_UNUSED) 1393 || !device || !device->port) 1394 continue; 1395 1396 sas_port = device->port; 1397 port = to_hisi_sas_port(sas_port); 1398 1399 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) 1400 if (state & BIT(sas_phy->id)) { 1401 phy = sas_phy->lldd_phy; 1402 break; 1403 } 1404 1405 if (phy) { 1406 port->id = phy->port_id; 1407 1408 /* Update linkrate of directly attached device. */ 1409 if (!device->parent) 1410 device->linkrate = phy->sas_phy.linkrate; 1411 1412 hisi_hba->hw->setup_itct(hisi_hba, sas_dev); 1413 } else 1414 port->id = 0xff; 1415 } 1416 } 1417 1418 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state) 1419 { 1420 struct sas_ha_struct *sas_ha = &hisi_hba->sha; 1421 struct asd_sas_port *_sas_port = NULL; 1422 int phy_no; 1423 1424 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) { 1425 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 1426 struct asd_sas_phy *sas_phy = &phy->sas_phy; 1427 struct asd_sas_port *sas_port = sas_phy->port; 1428 bool do_port_check = _sas_port != sas_port; 1429 1430 if (!sas_phy->phy->enabled) 1431 continue; 1432 1433 /* Report PHY state change to libsas */ 1434 if (state & BIT(phy_no)) { 1435 if (do_port_check && sas_port && sas_port->port_dev) { 1436 struct domain_device *dev = sas_port->port_dev; 1437 1438 _sas_port = sas_port; 1439 1440 if (dev_is_expander(dev->dev_type)) 1441 sas_ha->notify_port_event(sas_phy, 1442 PORTE_BROADCAST_RCVD); 1443 } 1444 } else { 1445 hisi_sas_phy_down(hisi_hba, phy_no, 0); 1446 } 1447 } 1448 } 1449 1450 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba) 1451 { 1452 struct hisi_sas_device *sas_dev; 1453 struct domain_device *device; 1454 int i; 1455 1456 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1457 sas_dev = &hisi_hba->devices[i]; 1458 device = sas_dev->sas_device; 1459 1460 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device) 1461 continue; 1462 1463 hisi_sas_init_device(device); 1464 } 1465 } 1466 1467 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba, 1468 struct asd_sas_port *sas_port, 1469 struct domain_device *device) 1470 { 1471 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 }; 1472 struct ata_port *ap = device->sata_dev.ap; 1473 struct device *dev = hisi_hba->dev; 1474 int s = sizeof(struct host_to_dev_fis); 1475 int rc = TMF_RESP_FUNC_FAILED; 1476 struct asd_sas_phy *sas_phy; 1477 struct ata_link *link; 1478 u8 fis[20] = {0}; 1479 u32 state; 1480 1481 state = hisi_hba->hw->get_phys_state(hisi_hba); 1482 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) { 1483 if (!(state & BIT(sas_phy->id))) 1484 continue; 1485 1486 ata_for_each_link(link, ap, EDGE) { 1487 int pmp = sata_srst_pmp(link); 1488 1489 tmf_task.phy_id = sas_phy->id; 1490 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis); 1491 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, 1492 &tmf_task); 1493 if (rc != TMF_RESP_FUNC_COMPLETE) { 1494 dev_err(dev, "phy%d ata reset failed rc=%d\n", 1495 sas_phy->id, rc); 1496 break; 1497 } 1498 } 1499 } 1500 } 1501 1502 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba) 1503 { 1504 struct device *dev = hisi_hba->dev; 1505 int port_no, rc, i; 1506 1507 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1508 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1509 struct domain_device *device = sas_dev->sas_device; 1510 1511 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device) 1512 continue; 1513 1514 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1515 HISI_SAS_INT_ABT_DEV, 0); 1516 if (rc < 0) 1517 dev_err(dev, "STP reject: abort dev failed %d\n", rc); 1518 } 1519 1520 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) { 1521 struct hisi_sas_port *port = &hisi_hba->port[port_no]; 1522 struct asd_sas_port *sas_port = &port->sas_port; 1523 struct domain_device *port_dev = sas_port->port_dev; 1524 struct domain_device *device; 1525 1526 if (!port_dev || !dev_is_expander(port_dev->dev_type)) 1527 continue; 1528 1529 /* Try to find a SATA device */ 1530 list_for_each_entry(device, &sas_port->dev_list, 1531 dev_list_node) { 1532 if (dev_is_sata(device)) { 1533 hisi_sas_send_ata_reset_each_phy(hisi_hba, 1534 sas_port, 1535 device); 1536 break; 1537 } 1538 } 1539 } 1540 } 1541 1542 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba) 1543 { 1544 struct Scsi_Host *shost = hisi_hba->shost; 1545 1546 down(&hisi_hba->sem); 1547 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba); 1548 1549 scsi_block_requests(shost); 1550 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000); 1551 1552 if (timer_pending(&hisi_hba->timer)) 1553 del_timer_sync(&hisi_hba->timer); 1554 1555 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1556 } 1557 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare); 1558 1559 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba) 1560 { 1561 struct Scsi_Host *shost = hisi_hba->shost; 1562 1563 /* Init and wait for PHYs to come up and all libsas event finished. */ 1564 hisi_hba->hw->phys_init(hisi_hba); 1565 msleep(1000); 1566 hisi_sas_refresh_port_id(hisi_hba); 1567 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1568 1569 if (hisi_hba->reject_stp_links_msk) 1570 hisi_sas_terminate_stp_reject(hisi_hba); 1571 hisi_sas_reset_init_all_devices(hisi_hba); 1572 up(&hisi_hba->sem); 1573 scsi_unblock_requests(shost); 1574 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags); 1575 1576 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state); 1577 } 1578 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done); 1579 1580 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba) 1581 { 1582 struct device *dev = hisi_hba->dev; 1583 struct Scsi_Host *shost = hisi_hba->shost; 1584 int rc; 1585 1586 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct) 1587 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work); 1588 1589 if (!hisi_hba->hw->soft_reset) 1590 return -1; 1591 1592 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) 1593 return -1; 1594 1595 dev_info(dev, "controller resetting...\n"); 1596 hisi_sas_controller_reset_prepare(hisi_hba); 1597 1598 rc = hisi_hba->hw->soft_reset(hisi_hba); 1599 if (rc) { 1600 dev_warn(dev, "controller reset failed (%d)\n", rc); 1601 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); 1602 up(&hisi_hba->sem); 1603 scsi_unblock_requests(shost); 1604 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags); 1605 return rc; 1606 } 1607 1608 hisi_sas_controller_reset_done(hisi_hba); 1609 dev_info(dev, "controller reset complete\n"); 1610 1611 return 0; 1612 } 1613 1614 static int hisi_sas_abort_task(struct sas_task *task) 1615 { 1616 struct scsi_lun lun; 1617 struct hisi_sas_tmf_task tmf_task; 1618 struct domain_device *device = task->dev; 1619 struct hisi_sas_device *sas_dev = device->lldd_dev; 1620 struct hisi_hba *hisi_hba; 1621 struct device *dev; 1622 int rc = TMF_RESP_FUNC_FAILED; 1623 unsigned long flags; 1624 1625 if (!sas_dev) 1626 return TMF_RESP_FUNC_FAILED; 1627 1628 hisi_hba = dev_to_hisi_hba(task->dev); 1629 dev = hisi_hba->dev; 1630 1631 spin_lock_irqsave(&task->task_state_lock, flags); 1632 if (task->task_state_flags & SAS_TASK_STATE_DONE) { 1633 struct hisi_sas_slot *slot = task->lldd_task; 1634 struct hisi_sas_cq *cq; 1635 1636 if (slot) { 1637 /* 1638 * sync irq to avoid free'ing task 1639 * before using task in IO completion 1640 */ 1641 cq = &hisi_hba->cq[slot->dlvry_queue]; 1642 synchronize_irq(cq->irq_no); 1643 } 1644 spin_unlock_irqrestore(&task->task_state_lock, flags); 1645 rc = TMF_RESP_FUNC_COMPLETE; 1646 goto out; 1647 } 1648 task->task_state_flags |= SAS_TASK_STATE_ABORTED; 1649 spin_unlock_irqrestore(&task->task_state_lock, flags); 1650 1651 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { 1652 struct scsi_cmnd *cmnd = task->uldd_task; 1653 struct hisi_sas_slot *slot = task->lldd_task; 1654 u16 tag = slot->idx; 1655 int rc2; 1656 1657 int_to_scsilun(cmnd->device->lun, &lun); 1658 tmf_task.tmf = TMF_ABORT_TASK; 1659 tmf_task.tag_of_task_to_be_managed = tag; 1660 1661 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, 1662 &tmf_task); 1663 1664 rc2 = hisi_sas_internal_task_abort(hisi_hba, device, 1665 HISI_SAS_INT_ABT_CMD, tag); 1666 if (rc2 < 0) { 1667 dev_err(dev, "abort task: internal abort (%d)\n", rc2); 1668 return TMF_RESP_FUNC_FAILED; 1669 } 1670 1671 /* 1672 * If the TMF finds that the IO is not in the device and also 1673 * the internal abort does not succeed, then it is safe to 1674 * free the slot. 1675 * Note: if the internal abort succeeds then the slot 1676 * will have already been completed 1677 */ 1678 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) { 1679 if (task->lldd_task) 1680 hisi_sas_do_release_task(hisi_hba, task, slot); 1681 } 1682 } else if (task->task_proto & SAS_PROTOCOL_SATA || 1683 task->task_proto & SAS_PROTOCOL_STP) { 1684 if (task->dev->dev_type == SAS_SATA_DEV) { 1685 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1686 HISI_SAS_INT_ABT_DEV, 1687 0); 1688 if (rc < 0) { 1689 dev_err(dev, "abort task: internal abort failed\n"); 1690 goto out; 1691 } 1692 hisi_sas_dereg_device(hisi_hba, device); 1693 rc = hisi_sas_softreset_ata_disk(device); 1694 } 1695 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) { 1696 /* SMP */ 1697 struct hisi_sas_slot *slot = task->lldd_task; 1698 u32 tag = slot->idx; 1699 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue]; 1700 1701 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1702 HISI_SAS_INT_ABT_CMD, tag); 1703 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) && 1704 task->lldd_task) { 1705 /* 1706 * sync irq to avoid free'ing task 1707 * before using task in IO completion 1708 */ 1709 synchronize_irq(cq->irq_no); 1710 slot->task = NULL; 1711 } 1712 } 1713 1714 out: 1715 if (rc != TMF_RESP_FUNC_COMPLETE) 1716 dev_notice(dev, "abort task: rc=%d\n", rc); 1717 return rc; 1718 } 1719 1720 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun) 1721 { 1722 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1723 struct device *dev = hisi_hba->dev; 1724 struct hisi_sas_tmf_task tmf_task; 1725 int rc; 1726 1727 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1728 HISI_SAS_INT_ABT_DEV, 0); 1729 if (rc < 0) { 1730 dev_err(dev, "abort task set: internal abort rc=%d\n", rc); 1731 return TMF_RESP_FUNC_FAILED; 1732 } 1733 hisi_sas_dereg_device(hisi_hba, device); 1734 1735 tmf_task.tmf = TMF_ABORT_TASK_SET; 1736 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); 1737 1738 if (rc == TMF_RESP_FUNC_COMPLETE) 1739 hisi_sas_release_task(hisi_hba, device); 1740 1741 return rc; 1742 } 1743 1744 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun) 1745 { 1746 struct hisi_sas_tmf_task tmf_task; 1747 int rc; 1748 1749 tmf_task.tmf = TMF_CLEAR_ACA; 1750 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); 1751 1752 return rc; 1753 } 1754 1755 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device) 1756 { 1757 struct sas_phy *local_phy = sas_get_local_phy(device); 1758 struct hisi_sas_device *sas_dev = device->lldd_dev; 1759 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1760 struct sas_ha_struct *sas_ha = &hisi_hba->sha; 1761 DECLARE_COMPLETION_ONSTACK(phyreset); 1762 int rc, reset_type; 1763 1764 if (!local_phy->enabled) { 1765 sas_put_local_phy(local_phy); 1766 return -ENODEV; 1767 } 1768 1769 if (scsi_is_sas_phy_local(local_phy)) { 1770 struct asd_sas_phy *sas_phy = 1771 sas_ha->sas_phy[local_phy->number]; 1772 struct hisi_sas_phy *phy = 1773 container_of(sas_phy, struct hisi_sas_phy, sas_phy); 1774 phy->in_reset = 1; 1775 phy->reset_completion = &phyreset; 1776 } 1777 1778 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT || 1779 !dev_is_sata(device)) ? true : false; 1780 1781 rc = sas_phy_reset(local_phy, reset_type); 1782 sas_put_local_phy(local_phy); 1783 1784 if (scsi_is_sas_phy_local(local_phy)) { 1785 struct asd_sas_phy *sas_phy = 1786 sas_ha->sas_phy[local_phy->number]; 1787 struct hisi_sas_phy *phy = 1788 container_of(sas_phy, struct hisi_sas_phy, sas_phy); 1789 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ); 1790 unsigned long flags; 1791 1792 spin_lock_irqsave(&phy->lock, flags); 1793 phy->reset_completion = NULL; 1794 phy->in_reset = 0; 1795 spin_unlock_irqrestore(&phy->lock, flags); 1796 1797 /* report PHY down if timed out */ 1798 if (!ret) 1799 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0); 1800 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) { 1801 /* 1802 * If in init state, we rely on caller to wait for link to be 1803 * ready; otherwise, except phy reset is fail, delay. 1804 */ 1805 if (!rc) 1806 msleep(2000); 1807 } 1808 1809 return rc; 1810 } 1811 1812 static int hisi_sas_I_T_nexus_reset(struct domain_device *device) 1813 { 1814 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1815 struct device *dev = hisi_hba->dev; 1816 int rc; 1817 1818 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1819 HISI_SAS_INT_ABT_DEV, 0); 1820 if (rc < 0) { 1821 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc); 1822 return TMF_RESP_FUNC_FAILED; 1823 } 1824 hisi_sas_dereg_device(hisi_hba, device); 1825 1826 if (dev_is_sata(device)) { 1827 rc = hisi_sas_softreset_ata_disk(device); 1828 if (rc == TMF_RESP_FUNC_FAILED) 1829 return TMF_RESP_FUNC_FAILED; 1830 } 1831 1832 rc = hisi_sas_debug_I_T_nexus_reset(device); 1833 1834 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV)) 1835 hisi_sas_release_task(hisi_hba, device); 1836 1837 return rc; 1838 } 1839 1840 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun) 1841 { 1842 struct hisi_sas_device *sas_dev = device->lldd_dev; 1843 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); 1844 struct device *dev = hisi_hba->dev; 1845 int rc = TMF_RESP_FUNC_FAILED; 1846 1847 /* Clear internal IO and then lu reset */ 1848 rc = hisi_sas_internal_task_abort(hisi_hba, device, 1849 HISI_SAS_INT_ABT_DEV, 0); 1850 if (rc < 0) { 1851 dev_err(dev, "lu_reset: internal abort failed\n"); 1852 goto out; 1853 } 1854 hisi_sas_dereg_device(hisi_hba, device); 1855 1856 if (dev_is_sata(device)) { 1857 struct sas_phy *phy; 1858 1859 phy = sas_get_local_phy(device); 1860 1861 rc = sas_phy_reset(phy, true); 1862 1863 if (rc == 0) 1864 hisi_sas_release_task(hisi_hba, device); 1865 sas_put_local_phy(phy); 1866 } else { 1867 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET }; 1868 1869 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); 1870 if (rc == TMF_RESP_FUNC_COMPLETE) 1871 hisi_sas_release_task(hisi_hba, device); 1872 } 1873 out: 1874 if (rc != TMF_RESP_FUNC_COMPLETE) 1875 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n", 1876 sas_dev->device_id, rc); 1877 return rc; 1878 } 1879 1880 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha) 1881 { 1882 struct hisi_hba *hisi_hba = sas_ha->lldd_ha; 1883 struct device *dev = hisi_hba->dev; 1884 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r); 1885 int rc, i; 1886 1887 queue_work(hisi_hba->wq, &r.work); 1888 wait_for_completion(r.completion); 1889 if (!r.done) 1890 return TMF_RESP_FUNC_FAILED; 1891 1892 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 1893 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i]; 1894 struct domain_device *device = sas_dev->sas_device; 1895 1896 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device || 1897 dev_is_expander(device->dev_type)) 1898 continue; 1899 1900 rc = hisi_sas_debug_I_T_nexus_reset(device); 1901 if (rc != TMF_RESP_FUNC_COMPLETE) 1902 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n", 1903 sas_dev->device_id, rc); 1904 } 1905 1906 hisi_sas_release_tasks(hisi_hba); 1907 1908 return TMF_RESP_FUNC_COMPLETE; 1909 } 1910 1911 static int hisi_sas_query_task(struct sas_task *task) 1912 { 1913 struct scsi_lun lun; 1914 struct hisi_sas_tmf_task tmf_task; 1915 int rc = TMF_RESP_FUNC_FAILED; 1916 1917 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { 1918 struct scsi_cmnd *cmnd = task->uldd_task; 1919 struct domain_device *device = task->dev; 1920 struct hisi_sas_slot *slot = task->lldd_task; 1921 u32 tag = slot->idx; 1922 1923 int_to_scsilun(cmnd->device->lun, &lun); 1924 tmf_task.tmf = TMF_QUERY_TASK; 1925 tmf_task.tag_of_task_to_be_managed = tag; 1926 1927 rc = hisi_sas_debug_issue_ssp_tmf(device, 1928 lun.scsi_lun, 1929 &tmf_task); 1930 switch (rc) { 1931 /* The task is still in Lun, release it then */ 1932 case TMF_RESP_FUNC_SUCC: 1933 /* The task is not in Lun or failed, reset the phy */ 1934 case TMF_RESP_FUNC_FAILED: 1935 case TMF_RESP_FUNC_COMPLETE: 1936 break; 1937 default: 1938 rc = TMF_RESP_FUNC_FAILED; 1939 break; 1940 } 1941 } 1942 return rc; 1943 } 1944 1945 static int 1946 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id, 1947 struct sas_task *task, int abort_flag, 1948 int task_tag, struct hisi_sas_dq *dq) 1949 { 1950 struct domain_device *device = task->dev; 1951 struct hisi_sas_device *sas_dev = device->lldd_dev; 1952 struct device *dev = hisi_hba->dev; 1953 struct hisi_sas_port *port; 1954 struct hisi_sas_slot *slot; 1955 struct asd_sas_port *sas_port = device->port; 1956 struct hisi_sas_cmd_hdr *cmd_hdr_base; 1957 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; 1958 unsigned long flags; 1959 int wr_q_index; 1960 1961 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) 1962 return -EINVAL; 1963 1964 if (!device->port) 1965 return -1; 1966 1967 port = to_hisi_sas_port(sas_port); 1968 1969 /* simply get a slot and send abort command */ 1970 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL); 1971 if (rc < 0) 1972 goto err_out; 1973 1974 slot_idx = rc; 1975 slot = &hisi_hba->slot_info[slot_idx]; 1976 1977 spin_lock(&dq->lock); 1978 wr_q_index = dq->wr_point; 1979 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS; 1980 list_add_tail(&slot->delivery, &dq->list); 1981 spin_unlock(&dq->lock); 1982 spin_lock(&sas_dev->lock); 1983 list_add_tail(&slot->entry, &sas_dev->list); 1984 spin_unlock(&sas_dev->lock); 1985 1986 dlvry_queue = dq->id; 1987 dlvry_queue_slot = wr_q_index; 1988 1989 slot->device_id = sas_dev->device_id; 1990 slot->n_elem = n_elem; 1991 slot->dlvry_queue = dlvry_queue; 1992 slot->dlvry_queue_slot = dlvry_queue_slot; 1993 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; 1994 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; 1995 slot->task = task; 1996 slot->port = port; 1997 slot->is_internal = true; 1998 task->lldd_task = slot; 1999 2000 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); 2001 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ); 2002 memset(hisi_sas_status_buf_addr_mem(slot), 0, 2003 sizeof(struct hisi_sas_err_record)); 2004 2005 hisi_sas_task_prep_abort(hisi_hba, slot, device_id, 2006 abort_flag, task_tag); 2007 2008 spin_lock_irqsave(&task->task_state_lock, flags); 2009 task->task_state_flags |= SAS_TASK_AT_INITIATOR; 2010 spin_unlock_irqrestore(&task->task_state_lock, flags); 2011 WRITE_ONCE(slot->ready, 1); 2012 /* send abort command to the chip */ 2013 spin_lock(&dq->lock); 2014 hisi_hba->hw->start_delivery(dq); 2015 spin_unlock(&dq->lock); 2016 2017 return 0; 2018 2019 err_out: 2020 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc); 2021 2022 return rc; 2023 } 2024 2025 /** 2026 * _hisi_sas_internal_task_abort -- execute an internal 2027 * abort command for single IO command or a device 2028 * @hisi_hba: host controller struct 2029 * @device: domain device 2030 * @abort_flag: mode of operation, device or single IO 2031 * @tag: tag of IO to be aborted (only relevant to single 2032 * IO mode) 2033 * @dq: delivery queue for this internal abort command 2034 */ 2035 static int 2036 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, 2037 struct domain_device *device, int abort_flag, 2038 int tag, struct hisi_sas_dq *dq) 2039 { 2040 struct sas_task *task; 2041 struct hisi_sas_device *sas_dev = device->lldd_dev; 2042 struct device *dev = hisi_hba->dev; 2043 int res; 2044 2045 /* 2046 * The interface is not realized means this HW don't support internal 2047 * abort, or don't need to do internal abort. Then here, we return 2048 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that 2049 * the internal abort has been executed and returned CQ. 2050 */ 2051 if (!hisi_hba->hw->prep_abort) 2052 return TMF_RESP_FUNC_FAILED; 2053 2054 task = sas_alloc_slow_task(GFP_KERNEL); 2055 if (!task) 2056 return -ENOMEM; 2057 2058 task->dev = device; 2059 task->task_proto = device->tproto; 2060 task->task_done = hisi_sas_task_done; 2061 task->slow_task->timer.function = hisi_sas_tmf_timedout; 2062 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ; 2063 add_timer(&task->slow_task->timer); 2064 2065 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id, 2066 task, abort_flag, tag, dq); 2067 if (res) { 2068 del_timer(&task->slow_task->timer); 2069 dev_err(dev, "internal task abort: executing internal task failed: %d\n", 2070 res); 2071 goto exit; 2072 } 2073 wait_for_completion(&task->slow_task->completion); 2074 res = TMF_RESP_FUNC_FAILED; 2075 2076 /* Internal abort timed out */ 2077 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 2078 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct) 2079 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work); 2080 2081 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { 2082 struct hisi_sas_slot *slot = task->lldd_task; 2083 2084 if (slot) { 2085 struct hisi_sas_cq *cq = 2086 &hisi_hba->cq[slot->dlvry_queue]; 2087 /* 2088 * sync irq to avoid free'ing task 2089 * before using task in IO completion 2090 */ 2091 synchronize_irq(cq->irq_no); 2092 slot->task = NULL; 2093 } 2094 dev_err(dev, "internal task abort: timeout and not done.\n"); 2095 2096 res = -EIO; 2097 goto exit; 2098 } else 2099 dev_err(dev, "internal task abort: timeout.\n"); 2100 } 2101 2102 if (task->task_status.resp == SAS_TASK_COMPLETE && 2103 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { 2104 res = TMF_RESP_FUNC_COMPLETE; 2105 goto exit; 2106 } 2107 2108 if (task->task_status.resp == SAS_TASK_COMPLETE && 2109 task->task_status.stat == TMF_RESP_FUNC_SUCC) { 2110 res = TMF_RESP_FUNC_SUCC; 2111 goto exit; 2112 } 2113 2114 exit: 2115 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n", 2116 SAS_ADDR(device->sas_addr), task, 2117 task->task_status.resp, /* 0 is complete, -1 is undelivered */ 2118 task->task_status.stat); 2119 sas_free_task(task); 2120 2121 return res; 2122 } 2123 2124 static int 2125 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, 2126 struct domain_device *device, 2127 int abort_flag, int tag) 2128 { 2129 struct hisi_sas_slot *slot; 2130 struct device *dev = hisi_hba->dev; 2131 struct hisi_sas_dq *dq; 2132 int i, rc; 2133 2134 switch (abort_flag) { 2135 case HISI_SAS_INT_ABT_CMD: 2136 slot = &hisi_hba->slot_info[tag]; 2137 dq = &hisi_hba->dq[slot->dlvry_queue]; 2138 return _hisi_sas_internal_task_abort(hisi_hba, device, 2139 abort_flag, tag, dq); 2140 case HISI_SAS_INT_ABT_DEV: 2141 for (i = 0; i < hisi_hba->cq_nvecs; i++) { 2142 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2143 const struct cpumask *mask = cq->irq_mask; 2144 2145 if (mask && !cpumask_intersects(cpu_online_mask, mask)) 2146 continue; 2147 dq = &hisi_hba->dq[i]; 2148 rc = _hisi_sas_internal_task_abort(hisi_hba, device, 2149 abort_flag, tag, 2150 dq); 2151 if (rc) 2152 return rc; 2153 } 2154 break; 2155 default: 2156 dev_err(dev, "Unrecognised internal abort flag (%d)\n", 2157 abort_flag); 2158 return -EINVAL; 2159 } 2160 2161 return 0; 2162 } 2163 2164 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) 2165 { 2166 hisi_sas_port_notify_formed(sas_phy); 2167 } 2168 2169 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type, 2170 u8 reg_index, u8 reg_count, u8 *write_data) 2171 { 2172 struct hisi_hba *hisi_hba = sha->lldd_ha; 2173 2174 if (!hisi_hba->hw->write_gpio) 2175 return -EOPNOTSUPP; 2176 2177 return hisi_hba->hw->write_gpio(hisi_hba, reg_type, 2178 reg_index, reg_count, write_data); 2179 } 2180 2181 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) 2182 { 2183 struct asd_sas_phy *sas_phy = &phy->sas_phy; 2184 struct sas_phy *sphy = sas_phy->phy; 2185 unsigned long flags; 2186 2187 phy->phy_attached = 0; 2188 phy->phy_type = 0; 2189 phy->port = NULL; 2190 2191 spin_lock_irqsave(&phy->lock, flags); 2192 if (phy->enable) 2193 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN; 2194 else 2195 sphy->negotiated_linkrate = SAS_PHY_DISABLED; 2196 spin_unlock_irqrestore(&phy->lock, flags); 2197 } 2198 2199 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy) 2200 { 2201 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; 2202 struct asd_sas_phy *sas_phy = &phy->sas_phy; 2203 struct sas_ha_struct *sas_ha = &hisi_hba->sha; 2204 struct device *dev = hisi_hba->dev; 2205 2206 if (rdy) { 2207 /* Phy down but ready */ 2208 hisi_sas_bytes_dmaed(hisi_hba, phy_no); 2209 hisi_sas_port_notify_formed(sas_phy); 2210 } else { 2211 struct hisi_sas_port *port = phy->port; 2212 2213 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) || 2214 phy->in_reset) { 2215 dev_info(dev, "ignore flutter phy%d down\n", phy_no); 2216 return; 2217 } 2218 /* Phy down and not ready */ 2219 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL); 2220 sas_phy_disconnected(sas_phy); 2221 2222 if (port) { 2223 if (phy->phy_type & PORT_TYPE_SAS) { 2224 int port_id = port->id; 2225 2226 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, 2227 port_id)) 2228 port->port_attached = 0; 2229 } else if (phy->phy_type & PORT_TYPE_SATA) 2230 port->port_attached = 0; 2231 } 2232 hisi_sas_phy_disconnected(phy); 2233 } 2234 } 2235 EXPORT_SYMBOL_GPL(hisi_sas_phy_down); 2236 2237 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba) 2238 { 2239 int i; 2240 2241 for (i = 0; i < hisi_hba->cq_nvecs; i++) { 2242 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2243 2244 synchronize_irq(cq->irq_no); 2245 } 2246 } 2247 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs); 2248 2249 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type) 2250 { 2251 struct hisi_hba *hisi_hba = shost_priv(shost); 2252 2253 if (reset_type != SCSI_ADAPTER_RESET) 2254 return -EOPNOTSUPP; 2255 2256 queue_work(hisi_hba->wq, &hisi_hba->rst_work); 2257 2258 return 0; 2259 } 2260 EXPORT_SYMBOL_GPL(hisi_sas_host_reset); 2261 2262 struct scsi_transport_template *hisi_sas_stt; 2263 EXPORT_SYMBOL_GPL(hisi_sas_stt); 2264 2265 static struct sas_domain_function_template hisi_sas_transport_ops = { 2266 .lldd_dev_found = hisi_sas_dev_found, 2267 .lldd_dev_gone = hisi_sas_dev_gone, 2268 .lldd_execute_task = hisi_sas_queue_command, 2269 .lldd_control_phy = hisi_sas_control_phy, 2270 .lldd_abort_task = hisi_sas_abort_task, 2271 .lldd_abort_task_set = hisi_sas_abort_task_set, 2272 .lldd_clear_aca = hisi_sas_clear_aca, 2273 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset, 2274 .lldd_lu_reset = hisi_sas_lu_reset, 2275 .lldd_query_task = hisi_sas_query_task, 2276 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha, 2277 .lldd_port_formed = hisi_sas_port_formed, 2278 .lldd_write_gpio = hisi_sas_write_gpio, 2279 }; 2280 2281 void hisi_sas_init_mem(struct hisi_hba *hisi_hba) 2282 { 2283 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS; 2284 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint; 2285 2286 for (i = 0; i < hisi_hba->queue_count; i++) { 2287 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2288 struct hisi_sas_dq *dq = &hisi_hba->dq[i]; 2289 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i]; 2290 2291 s = sizeof(struct hisi_sas_cmd_hdr); 2292 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++) 2293 memset(&cmd_hdr[j], 0, s); 2294 2295 dq->wr_point = 0; 2296 2297 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; 2298 memset(hisi_hba->complete_hdr[i], 0, s); 2299 cq->rd_point = 0; 2300 } 2301 2302 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy; 2303 memset(hisi_hba->initial_fis, 0, s); 2304 2305 s = max_command_entries * sizeof(struct hisi_sas_iost); 2306 memset(hisi_hba->iost, 0, s); 2307 2308 s = max_command_entries * sizeof(struct hisi_sas_breakpoint); 2309 memset(hisi_hba->breakpoint, 0, s); 2310 2311 s = sizeof(struct hisi_sas_sata_breakpoint); 2312 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++) 2313 memset(&sata_breakpoint[j], 0, s); 2314 } 2315 EXPORT_SYMBOL_GPL(hisi_sas_init_mem); 2316 2317 int hisi_sas_alloc(struct hisi_hba *hisi_hba) 2318 { 2319 struct device *dev = hisi_hba->dev; 2320 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS; 2321 int max_command_entries_ru, sz_slot_buf_ru; 2322 int blk_cnt, slots_per_blk; 2323 2324 sema_init(&hisi_hba->sem, 1); 2325 spin_lock_init(&hisi_hba->lock); 2326 for (i = 0; i < hisi_hba->n_phy; i++) { 2327 hisi_sas_phy_init(hisi_hba, i); 2328 hisi_hba->port[i].port_attached = 0; 2329 hisi_hba->port[i].id = -1; 2330 } 2331 2332 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { 2333 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; 2334 hisi_hba->devices[i].device_id = i; 2335 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT; 2336 } 2337 2338 for (i = 0; i < hisi_hba->queue_count; i++) { 2339 struct hisi_sas_cq *cq = &hisi_hba->cq[i]; 2340 struct hisi_sas_dq *dq = &hisi_hba->dq[i]; 2341 2342 /* Completion queue structure */ 2343 cq->id = i; 2344 cq->hisi_hba = hisi_hba; 2345 2346 /* Delivery queue structure */ 2347 spin_lock_init(&dq->lock); 2348 INIT_LIST_HEAD(&dq->list); 2349 dq->id = i; 2350 dq->hisi_hba = hisi_hba; 2351 2352 /* Delivery queue */ 2353 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; 2354 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s, 2355 &hisi_hba->cmd_hdr_dma[i], 2356 GFP_KERNEL); 2357 if (!hisi_hba->cmd_hdr[i]) 2358 goto err_out; 2359 2360 /* Completion queue */ 2361 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; 2362 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s, 2363 &hisi_hba->complete_hdr_dma[i], 2364 GFP_KERNEL); 2365 if (!hisi_hba->complete_hdr[i]) 2366 goto err_out; 2367 } 2368 2369 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); 2370 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma, 2371 GFP_KERNEL); 2372 if (!hisi_hba->itct) 2373 goto err_out; 2374 2375 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries, 2376 sizeof(struct hisi_sas_slot), 2377 GFP_KERNEL); 2378 if (!hisi_hba->slot_info) 2379 goto err_out; 2380 2381 /* roundup to avoid overly large block size */ 2382 max_command_entries_ru = roundup(max_command_entries, 64); 2383 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK) 2384 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table); 2385 else 2386 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table); 2387 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64); 2388 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE); 2389 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s; 2390 slots_per_blk = s / sz_slot_buf_ru; 2391 2392 for (i = 0; i < blk_cnt; i++) { 2393 int slot_index = i * slots_per_blk; 2394 dma_addr_t buf_dma; 2395 void *buf; 2396 2397 buf = dmam_alloc_coherent(dev, s, &buf_dma, 2398 GFP_KERNEL); 2399 if (!buf) 2400 goto err_out; 2401 2402 for (j = 0; j < slots_per_blk; j++, slot_index++) { 2403 struct hisi_sas_slot *slot; 2404 2405 slot = &hisi_hba->slot_info[slot_index]; 2406 slot->buf = buf; 2407 slot->buf_dma = buf_dma; 2408 slot->idx = slot_index; 2409 2410 buf += sz_slot_buf_ru; 2411 buf_dma += sz_slot_buf_ru; 2412 } 2413 } 2414 2415 s = max_command_entries * sizeof(struct hisi_sas_iost); 2416 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma, 2417 GFP_KERNEL); 2418 if (!hisi_hba->iost) 2419 goto err_out; 2420 2421 s = max_command_entries * sizeof(struct hisi_sas_breakpoint); 2422 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s, 2423 &hisi_hba->breakpoint_dma, 2424 GFP_KERNEL); 2425 if (!hisi_hba->breakpoint) 2426 goto err_out; 2427 2428 hisi_hba->slot_index_count = max_command_entries; 2429 s = hisi_hba->slot_index_count / BITS_PER_BYTE; 2430 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); 2431 if (!hisi_hba->slot_index_tags) 2432 goto err_out; 2433 2434 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; 2435 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s, 2436 &hisi_hba->initial_fis_dma, 2437 GFP_KERNEL); 2438 if (!hisi_hba->initial_fis) 2439 goto err_out; 2440 2441 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint); 2442 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s, 2443 &hisi_hba->sata_breakpoint_dma, 2444 GFP_KERNEL); 2445 if (!hisi_hba->sata_breakpoint) 2446 goto err_out; 2447 2448 hisi_sas_slot_index_init(hisi_hba); 2449 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT; 2450 2451 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); 2452 if (!hisi_hba->wq) { 2453 dev_err(dev, "sas_alloc: failed to create workqueue\n"); 2454 goto err_out; 2455 } 2456 2457 return 0; 2458 err_out: 2459 return -ENOMEM; 2460 } 2461 EXPORT_SYMBOL_GPL(hisi_sas_alloc); 2462 2463 void hisi_sas_free(struct hisi_hba *hisi_hba) 2464 { 2465 int i; 2466 2467 for (i = 0; i < hisi_hba->n_phy; i++) { 2468 struct hisi_sas_phy *phy = &hisi_hba->phy[i]; 2469 2470 del_timer_sync(&phy->timer); 2471 } 2472 2473 if (hisi_hba->wq) 2474 destroy_workqueue(hisi_hba->wq); 2475 } 2476 EXPORT_SYMBOL_GPL(hisi_sas_free); 2477 2478 void hisi_sas_rst_work_handler(struct work_struct *work) 2479 { 2480 struct hisi_hba *hisi_hba = 2481 container_of(work, struct hisi_hba, rst_work); 2482 2483 hisi_sas_controller_reset(hisi_hba); 2484 } 2485 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler); 2486 2487 void hisi_sas_sync_rst_work_handler(struct work_struct *work) 2488 { 2489 struct hisi_sas_rst *rst = 2490 container_of(work, struct hisi_sas_rst, work); 2491 2492 if (!hisi_sas_controller_reset(rst->hisi_hba)) 2493 rst->done = true; 2494 complete(rst->completion); 2495 } 2496 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler); 2497 2498 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba) 2499 { 2500 struct device *dev = hisi_hba->dev; 2501 struct platform_device *pdev = hisi_hba->platform_dev; 2502 struct device_node *np = pdev ? pdev->dev.of_node : NULL; 2503 struct clk *refclk; 2504 2505 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr, 2506 SAS_ADDR_SIZE)) { 2507 dev_err(dev, "could not get property sas-addr\n"); 2508 return -ENOENT; 2509 } 2510 2511 if (np) { 2512 /* 2513 * These properties are only required for platform device-based 2514 * controller with DT firmware. 2515 */ 2516 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np, 2517 "hisilicon,sas-syscon"); 2518 if (IS_ERR(hisi_hba->ctrl)) { 2519 dev_err(dev, "could not get syscon\n"); 2520 return -ENOENT; 2521 } 2522 2523 if (device_property_read_u32(dev, "ctrl-reset-reg", 2524 &hisi_hba->ctrl_reset_reg)) { 2525 dev_err(dev, "could not get property ctrl-reset-reg\n"); 2526 return -ENOENT; 2527 } 2528 2529 if (device_property_read_u32(dev, "ctrl-reset-sts-reg", 2530 &hisi_hba->ctrl_reset_sts_reg)) { 2531 dev_err(dev, "could not get property ctrl-reset-sts-reg\n"); 2532 return -ENOENT; 2533 } 2534 2535 if (device_property_read_u32(dev, "ctrl-clock-ena-reg", 2536 &hisi_hba->ctrl_clock_ena_reg)) { 2537 dev_err(dev, "could not get property ctrl-clock-ena-reg\n"); 2538 return -ENOENT; 2539 } 2540 } 2541 2542 refclk = devm_clk_get(dev, NULL); 2543 if (IS_ERR(refclk)) 2544 dev_dbg(dev, "no ref clk property\n"); 2545 else 2546 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000; 2547 2548 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) { 2549 dev_err(dev, "could not get property phy-count\n"); 2550 return -ENOENT; 2551 } 2552 2553 if (device_property_read_u32(dev, "queue-count", 2554 &hisi_hba->queue_count)) { 2555 dev_err(dev, "could not get property queue-count\n"); 2556 return -ENOENT; 2557 } 2558 2559 return 0; 2560 } 2561 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info); 2562 2563 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, 2564 const struct hisi_sas_hw *hw) 2565 { 2566 struct resource *res; 2567 struct Scsi_Host *shost; 2568 struct hisi_hba *hisi_hba; 2569 struct device *dev = &pdev->dev; 2570 int error; 2571 2572 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba)); 2573 if (!shost) { 2574 dev_err(dev, "scsi host alloc failed\n"); 2575 return NULL; 2576 } 2577 hisi_hba = shost_priv(shost); 2578 2579 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler); 2580 hisi_hba->hw = hw; 2581 hisi_hba->dev = dev; 2582 hisi_hba->platform_dev = pdev; 2583 hisi_hba->shost = shost; 2584 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; 2585 2586 timer_setup(&hisi_hba->timer, NULL, 0); 2587 2588 if (hisi_sas_get_fw_info(hisi_hba) < 0) 2589 goto err_out; 2590 2591 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 2592 if (error) 2593 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 2594 2595 if (error) { 2596 dev_err(dev, "No usable DMA addressing method\n"); 2597 goto err_out; 2598 } 2599 2600 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0); 2601 if (IS_ERR(hisi_hba->regs)) 2602 goto err_out; 2603 2604 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 2605 if (res) { 2606 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res); 2607 if (IS_ERR(hisi_hba->sgpio_regs)) 2608 goto err_out; 2609 } 2610 2611 if (hisi_sas_alloc(hisi_hba)) { 2612 hisi_sas_free(hisi_hba); 2613 goto err_out; 2614 } 2615 2616 return shost; 2617 err_out: 2618 scsi_host_put(shost); 2619 dev_err(dev, "shost alloc failed\n"); 2620 return NULL; 2621 } 2622 2623 int hisi_sas_probe(struct platform_device *pdev, 2624 const struct hisi_sas_hw *hw) 2625 { 2626 struct Scsi_Host *shost; 2627 struct hisi_hba *hisi_hba; 2628 struct device *dev = &pdev->dev; 2629 struct asd_sas_phy **arr_phy; 2630 struct asd_sas_port **arr_port; 2631 struct sas_ha_struct *sha; 2632 int rc, phy_nr, port_nr, i; 2633 2634 shost = hisi_sas_shost_alloc(pdev, hw); 2635 if (!shost) 2636 return -ENOMEM; 2637 2638 sha = SHOST_TO_SAS_HA(shost); 2639 hisi_hba = shost_priv(shost); 2640 platform_set_drvdata(pdev, sha); 2641 2642 phy_nr = port_nr = hisi_hba->n_phy; 2643 2644 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); 2645 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); 2646 if (!arr_phy || !arr_port) { 2647 rc = -ENOMEM; 2648 goto err_out_ha; 2649 } 2650 2651 sha->sas_phy = arr_phy; 2652 sha->sas_port = arr_port; 2653 sha->lldd_ha = hisi_hba; 2654 2655 shost->transportt = hisi_sas_stt; 2656 shost->max_id = HISI_SAS_MAX_DEVICES; 2657 shost->max_lun = ~0; 2658 shost->max_channel = 1; 2659 shost->max_cmd_len = 16; 2660 if (hisi_hba->hw->slot_index_alloc) { 2661 shost->can_queue = HISI_SAS_MAX_COMMANDS; 2662 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS; 2663 } else { 2664 shost->can_queue = HISI_SAS_UNRESERVED_IPTT; 2665 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT; 2666 } 2667 2668 sha->sas_ha_name = DRV_NAME; 2669 sha->dev = hisi_hba->dev; 2670 sha->lldd_module = THIS_MODULE; 2671 sha->sas_addr = &hisi_hba->sas_addr[0]; 2672 sha->num_phys = hisi_hba->n_phy; 2673 sha->core.shost = hisi_hba->shost; 2674 2675 for (i = 0; i < hisi_hba->n_phy; i++) { 2676 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; 2677 sha->sas_port[i] = &hisi_hba->port[i].sas_port; 2678 } 2679 2680 rc = scsi_add_host(shost, &pdev->dev); 2681 if (rc) 2682 goto err_out_ha; 2683 2684 rc = sas_register_ha(sha); 2685 if (rc) 2686 goto err_out_register_ha; 2687 2688 rc = hisi_hba->hw->hw_init(hisi_hba); 2689 if (rc) 2690 goto err_out_register_ha; 2691 2692 scsi_scan_host(shost); 2693 2694 return 0; 2695 2696 err_out_register_ha: 2697 scsi_remove_host(shost); 2698 err_out_ha: 2699 hisi_sas_debugfs_exit(hisi_hba); 2700 hisi_sas_free(hisi_hba); 2701 scsi_host_put(shost); 2702 return rc; 2703 } 2704 EXPORT_SYMBOL_GPL(hisi_sas_probe); 2705 2706 struct dentry *hisi_sas_debugfs_dir; 2707 2708 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba) 2709 { 2710 int queue_entry_size = hisi_hba->hw->complete_hdr_size; 2711 int dump_index = hisi_hba->debugfs_dump_index; 2712 int i; 2713 2714 for (i = 0; i < hisi_hba->queue_count; i++) 2715 memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr, 2716 hisi_hba->complete_hdr[i], 2717 HISI_SAS_QUEUE_SLOTS * queue_entry_size); 2718 } 2719 2720 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba) 2721 { 2722 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr); 2723 int dump_index = hisi_hba->debugfs_dump_index; 2724 int i; 2725 2726 for (i = 0; i < hisi_hba->queue_count; i++) { 2727 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr; 2728 int j; 2729 2730 debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr; 2731 cmd_hdr = hisi_hba->cmd_hdr[i]; 2732 2733 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++) 2734 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j], 2735 queue_entry_size); 2736 } 2737 } 2738 2739 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba) 2740 { 2741 int dump_index = hisi_hba->debugfs_dump_index; 2742 const struct hisi_sas_debugfs_reg *port = 2743 hisi_hba->hw->debugfs_reg_port; 2744 int i, phy_cnt; 2745 u32 offset; 2746 u32 *databuf; 2747 2748 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) { 2749 databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data; 2750 for (i = 0; i < port->count; i++, databuf++) { 2751 offset = port->base_off + 4 * i; 2752 *databuf = port->read_port_reg(hisi_hba, phy_cnt, 2753 offset); 2754 } 2755 } 2756 } 2757 2758 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba) 2759 { 2760 int dump_index = hisi_hba->debugfs_dump_index; 2761 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data; 2762 const struct hisi_sas_hw *hw = hisi_hba->hw; 2763 const struct hisi_sas_debugfs_reg *global = 2764 hw->debugfs_reg_array[DEBUGFS_GLOBAL]; 2765 int i; 2766 2767 for (i = 0; i < global->count; i++, databuf++) 2768 *databuf = global->read_global_reg(hisi_hba, 4 * i); 2769 } 2770 2771 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba) 2772 { 2773 int dump_index = hisi_hba->debugfs_dump_index; 2774 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data; 2775 const struct hisi_sas_hw *hw = hisi_hba->hw; 2776 const struct hisi_sas_debugfs_reg *axi = 2777 hw->debugfs_reg_array[DEBUGFS_AXI]; 2778 int i; 2779 2780 for (i = 0; i < axi->count; i++, databuf++) 2781 *databuf = axi->read_global_reg(hisi_hba, 2782 4 * i + axi->base_off); 2783 } 2784 2785 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba) 2786 { 2787 int dump_index = hisi_hba->debugfs_dump_index; 2788 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data; 2789 const struct hisi_sas_hw *hw = hisi_hba->hw; 2790 const struct hisi_sas_debugfs_reg *ras = 2791 hw->debugfs_reg_array[DEBUGFS_RAS]; 2792 int i; 2793 2794 for (i = 0; i < ras->count; i++, databuf++) 2795 *databuf = ras->read_global_reg(hisi_hba, 2796 4 * i + ras->base_off); 2797 } 2798 2799 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba) 2800 { 2801 int dump_index = hisi_hba->debugfs_dump_index; 2802 void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache; 2803 void *databuf = hisi_hba->debugfs_itct[dump_index].itct; 2804 struct hisi_sas_itct *itct; 2805 int i; 2806 2807 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE, 2808 cachebuf); 2809 2810 itct = hisi_hba->itct; 2811 2812 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) { 2813 memcpy(databuf, itct, sizeof(struct hisi_sas_itct)); 2814 databuf += sizeof(struct hisi_sas_itct); 2815 } 2816 } 2817 2818 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba) 2819 { 2820 int dump_index = hisi_hba->debugfs_dump_index; 2821 int max_command_entries = HISI_SAS_MAX_COMMANDS; 2822 void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache; 2823 void *databuf = hisi_hba->debugfs_iost[dump_index].iost; 2824 struct hisi_sas_iost *iost; 2825 int i; 2826 2827 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE, 2828 cachebuf); 2829 2830 iost = hisi_hba->iost; 2831 2832 for (i = 0; i < max_command_entries; i++, iost++) { 2833 memcpy(databuf, iost, sizeof(struct hisi_sas_iost)); 2834 databuf += sizeof(struct hisi_sas_iost); 2835 } 2836 } 2837 2838 static const char * 2839 hisi_sas_debugfs_to_reg_name(int off, int base_off, 2840 const struct hisi_sas_debugfs_reg_lu *lu) 2841 { 2842 for (; lu->name; lu++) { 2843 if (off == lu->off - base_off) 2844 return lu->name; 2845 } 2846 2847 return NULL; 2848 } 2849 2850 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr, 2851 struct seq_file *s) 2852 { 2853 const struct hisi_sas_debugfs_reg *reg = ptr; 2854 int i; 2855 2856 for (i = 0; i < reg->count; i++) { 2857 int off = i * 4; 2858 const char *name; 2859 2860 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off, 2861 reg->lu); 2862 2863 if (name) 2864 seq_printf(s, "0x%08x 0x%08x %s\n", off, 2865 regs_val[i], name); 2866 else 2867 seq_printf(s, "0x%08x 0x%08x\n", off, 2868 regs_val[i]); 2869 } 2870 } 2871 2872 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p) 2873 { 2874 struct hisi_sas_debugfs_regs *global = s->private; 2875 struct hisi_hba *hisi_hba = global->hisi_hba; 2876 const struct hisi_sas_hw *hw = hisi_hba->hw; 2877 const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL]; 2878 2879 hisi_sas_debugfs_print_reg(global->data, 2880 reg_global, s); 2881 2882 return 0; 2883 } 2884 2885 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp) 2886 { 2887 return single_open(filp, hisi_sas_debugfs_global_show, 2888 inode->i_private); 2889 } 2890 2891 static const struct file_operations hisi_sas_debugfs_global_fops = { 2892 .open = hisi_sas_debugfs_global_open, 2893 .read = seq_read, 2894 .llseek = seq_lseek, 2895 .release = single_release, 2896 .owner = THIS_MODULE, 2897 }; 2898 2899 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p) 2900 { 2901 struct hisi_sas_debugfs_regs *axi = s->private; 2902 struct hisi_hba *hisi_hba = axi->hisi_hba; 2903 const struct hisi_sas_hw *hw = hisi_hba->hw; 2904 const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI]; 2905 2906 hisi_sas_debugfs_print_reg(axi->data, 2907 reg_axi, s); 2908 2909 return 0; 2910 } 2911 2912 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp) 2913 { 2914 return single_open(filp, hisi_sas_debugfs_axi_show, 2915 inode->i_private); 2916 } 2917 2918 static const struct file_operations hisi_sas_debugfs_axi_fops = { 2919 .open = hisi_sas_debugfs_axi_open, 2920 .read = seq_read, 2921 .llseek = seq_lseek, 2922 .release = single_release, 2923 .owner = THIS_MODULE, 2924 }; 2925 2926 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p) 2927 { 2928 struct hisi_sas_debugfs_regs *ras = s->private; 2929 struct hisi_hba *hisi_hba = ras->hisi_hba; 2930 const struct hisi_sas_hw *hw = hisi_hba->hw; 2931 const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS]; 2932 2933 hisi_sas_debugfs_print_reg(ras->data, 2934 reg_ras, s); 2935 2936 return 0; 2937 } 2938 2939 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp) 2940 { 2941 return single_open(filp, hisi_sas_debugfs_ras_show, 2942 inode->i_private); 2943 } 2944 2945 static const struct file_operations hisi_sas_debugfs_ras_fops = { 2946 .open = hisi_sas_debugfs_ras_open, 2947 .read = seq_read, 2948 .llseek = seq_lseek, 2949 .release = single_release, 2950 .owner = THIS_MODULE, 2951 }; 2952 2953 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p) 2954 { 2955 struct hisi_sas_debugfs_port *port = s->private; 2956 struct hisi_sas_phy *phy = port->phy; 2957 struct hisi_hba *hisi_hba = phy->hisi_hba; 2958 const struct hisi_sas_hw *hw = hisi_hba->hw; 2959 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port; 2960 2961 hisi_sas_debugfs_print_reg(port->data, reg_port, s); 2962 2963 return 0; 2964 } 2965 2966 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp) 2967 { 2968 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private); 2969 } 2970 2971 static const struct file_operations hisi_sas_debugfs_port_fops = { 2972 .open = hisi_sas_debugfs_port_open, 2973 .read = seq_read, 2974 .llseek = seq_lseek, 2975 .release = single_release, 2976 .owner = THIS_MODULE, 2977 }; 2978 2979 static void hisi_sas_show_row_64(struct seq_file *s, int index, 2980 int sz, __le64 *ptr) 2981 { 2982 int i; 2983 2984 /* completion header size not fixed per HW version */ 2985 seq_printf(s, "index %04d:\n\t", index); 2986 for (i = 1; i <= sz / 8; i++, ptr++) { 2987 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr)); 2988 if (!(i % 2)) 2989 seq_puts(s, "\n\t"); 2990 } 2991 2992 seq_puts(s, "\n"); 2993 } 2994 2995 static void hisi_sas_show_row_32(struct seq_file *s, int index, 2996 int sz, __le32 *ptr) 2997 { 2998 int i; 2999 3000 /* completion header size not fixed per HW version */ 3001 seq_printf(s, "index %04d:\n\t", index); 3002 for (i = 1; i <= sz / 4; i++, ptr++) { 3003 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr)); 3004 if (!(i % 4)) 3005 seq_puts(s, "\n\t"); 3006 } 3007 seq_puts(s, "\n"); 3008 } 3009 3010 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot, 3011 struct hisi_sas_debugfs_cq *debugfs_cq) 3012 { 3013 struct hisi_sas_cq *cq = debugfs_cq->cq; 3014 struct hisi_hba *hisi_hba = cq->hisi_hba; 3015 __le32 *complete_hdr = debugfs_cq->complete_hdr + 3016 (hisi_hba->hw->complete_hdr_size * slot); 3017 3018 hisi_sas_show_row_32(s, slot, 3019 hisi_hba->hw->complete_hdr_size, 3020 complete_hdr); 3021 } 3022 3023 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p) 3024 { 3025 struct hisi_sas_debugfs_cq *debugfs_cq = s->private; 3026 int slot; 3027 3028 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) { 3029 hisi_sas_cq_show_slot(s, slot, debugfs_cq); 3030 } 3031 return 0; 3032 } 3033 3034 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp) 3035 { 3036 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private); 3037 } 3038 3039 static const struct file_operations hisi_sas_debugfs_cq_fops = { 3040 .open = hisi_sas_debugfs_cq_open, 3041 .read = seq_read, 3042 .llseek = seq_lseek, 3043 .release = single_release, 3044 .owner = THIS_MODULE, 3045 }; 3046 3047 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr) 3048 { 3049 struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr; 3050 void *cmd_queue = debugfs_dq->hdr; 3051 __le32 *cmd_hdr = cmd_queue + 3052 sizeof(struct hisi_sas_cmd_hdr) * slot; 3053 3054 hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr); 3055 } 3056 3057 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p) 3058 { 3059 int slot; 3060 3061 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) { 3062 hisi_sas_dq_show_slot(s, slot, s->private); 3063 } 3064 return 0; 3065 } 3066 3067 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp) 3068 { 3069 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private); 3070 } 3071 3072 static const struct file_operations hisi_sas_debugfs_dq_fops = { 3073 .open = hisi_sas_debugfs_dq_open, 3074 .read = seq_read, 3075 .llseek = seq_lseek, 3076 .release = single_release, 3077 .owner = THIS_MODULE, 3078 }; 3079 3080 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p) 3081 { 3082 struct hisi_sas_debugfs_iost *debugfs_iost = s->private; 3083 struct hisi_sas_iost *iost = debugfs_iost->iost; 3084 int i, max_command_entries = HISI_SAS_MAX_COMMANDS; 3085 3086 for (i = 0; i < max_command_entries; i++, iost++) { 3087 __le64 *data = &iost->qw0; 3088 3089 hisi_sas_show_row_64(s, i, sizeof(*iost), data); 3090 } 3091 3092 return 0; 3093 } 3094 3095 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp) 3096 { 3097 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private); 3098 } 3099 3100 static const struct file_operations hisi_sas_debugfs_iost_fops = { 3101 .open = hisi_sas_debugfs_iost_open, 3102 .read = seq_read, 3103 .llseek = seq_lseek, 3104 .release = single_release, 3105 .owner = THIS_MODULE, 3106 }; 3107 3108 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p) 3109 { 3110 struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private; 3111 struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache; 3112 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4; 3113 int i, tab_idx; 3114 __le64 *iost; 3115 3116 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) { 3117 /* 3118 * Data struct of IOST cache: 3119 * Data[1]: BIT0~15: Table index 3120 * Bit16: Valid mask 3121 * Data[2]~[9]: IOST table 3122 */ 3123 tab_idx = (iost_cache->data[1] & 0xffff); 3124 iost = (__le64 *)iost_cache; 3125 3126 hisi_sas_show_row_64(s, tab_idx, cache_size, iost); 3127 } 3128 3129 return 0; 3130 } 3131 3132 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode, 3133 struct file *filp) 3134 { 3135 return single_open(filp, hisi_sas_debugfs_iost_cache_show, 3136 inode->i_private); 3137 } 3138 3139 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = { 3140 .open = hisi_sas_debugfs_iost_cache_open, 3141 .read = seq_read, 3142 .llseek = seq_lseek, 3143 .release = single_release, 3144 .owner = THIS_MODULE, 3145 }; 3146 3147 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p) 3148 { 3149 int i; 3150 struct hisi_sas_debugfs_itct *debugfs_itct = s->private; 3151 struct hisi_sas_itct *itct = debugfs_itct->itct; 3152 3153 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) { 3154 __le64 *data = &itct->qw0; 3155 3156 hisi_sas_show_row_64(s, i, sizeof(*itct), data); 3157 } 3158 3159 return 0; 3160 } 3161 3162 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp) 3163 { 3164 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private); 3165 } 3166 3167 static const struct file_operations hisi_sas_debugfs_itct_fops = { 3168 .open = hisi_sas_debugfs_itct_open, 3169 .read = seq_read, 3170 .llseek = seq_lseek, 3171 .release = single_release, 3172 .owner = THIS_MODULE, 3173 }; 3174 3175 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p) 3176 { 3177 struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private; 3178 struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache; 3179 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4; 3180 int i, tab_idx; 3181 __le64 *itct; 3182 3183 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) { 3184 /* 3185 * Data struct of ITCT cache: 3186 * Data[1]: BIT0~15: Table index 3187 * Bit16: Valid mask 3188 * Data[2]~[9]: ITCT table 3189 */ 3190 tab_idx = itct_cache->data[1] & 0xffff; 3191 itct = (__le64 *)itct_cache; 3192 3193 hisi_sas_show_row_64(s, tab_idx, cache_size, itct); 3194 } 3195 3196 return 0; 3197 } 3198 3199 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode, 3200 struct file *filp) 3201 { 3202 return single_open(filp, hisi_sas_debugfs_itct_cache_show, 3203 inode->i_private); 3204 } 3205 3206 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = { 3207 .open = hisi_sas_debugfs_itct_cache_open, 3208 .read = seq_read, 3209 .llseek = seq_lseek, 3210 .release = single_release, 3211 .owner = THIS_MODULE, 3212 }; 3213 3214 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba) 3215 { 3216 u64 *debugfs_timestamp; 3217 int dump_index = hisi_hba->debugfs_dump_index; 3218 struct dentry *dump_dentry; 3219 struct dentry *dentry; 3220 char name[256]; 3221 int p; 3222 int c; 3223 int d; 3224 3225 snprintf(name, 256, "%d", dump_index); 3226 3227 dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry); 3228 3229 debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index]; 3230 3231 debugfs_create_u64("timestamp", 0400, dump_dentry, 3232 debugfs_timestamp); 3233 3234 debugfs_create_file("global", 0400, dump_dentry, 3235 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL], 3236 &hisi_sas_debugfs_global_fops); 3237 3238 /* Create port dir and files */ 3239 dentry = debugfs_create_dir("port", dump_dentry); 3240 for (p = 0; p < hisi_hba->n_phy; p++) { 3241 snprintf(name, 256, "%d", p); 3242 3243 debugfs_create_file(name, 0400, dentry, 3244 &hisi_hba->debugfs_port_reg[dump_index][p], 3245 &hisi_sas_debugfs_port_fops); 3246 } 3247 3248 /* Create CQ dir and files */ 3249 dentry = debugfs_create_dir("cq", dump_dentry); 3250 for (c = 0; c < hisi_hba->queue_count; c++) { 3251 snprintf(name, 256, "%d", c); 3252 3253 debugfs_create_file(name, 0400, dentry, 3254 &hisi_hba->debugfs_cq[dump_index][c], 3255 &hisi_sas_debugfs_cq_fops); 3256 } 3257 3258 /* Create DQ dir and files */ 3259 dentry = debugfs_create_dir("dq", dump_dentry); 3260 for (d = 0; d < hisi_hba->queue_count; d++) { 3261 snprintf(name, 256, "%d", d); 3262 3263 debugfs_create_file(name, 0400, dentry, 3264 &hisi_hba->debugfs_dq[dump_index][d], 3265 &hisi_sas_debugfs_dq_fops); 3266 } 3267 3268 debugfs_create_file("iost", 0400, dump_dentry, 3269 &hisi_hba->debugfs_iost[dump_index], 3270 &hisi_sas_debugfs_iost_fops); 3271 3272 debugfs_create_file("iost_cache", 0400, dump_dentry, 3273 &hisi_hba->debugfs_iost_cache[dump_index], 3274 &hisi_sas_debugfs_iost_cache_fops); 3275 3276 debugfs_create_file("itct", 0400, dump_dentry, 3277 &hisi_hba->debugfs_itct[dump_index], 3278 &hisi_sas_debugfs_itct_fops); 3279 3280 debugfs_create_file("itct_cache", 0400, dump_dentry, 3281 &hisi_hba->debugfs_itct_cache[dump_index], 3282 &hisi_sas_debugfs_itct_cache_fops); 3283 3284 debugfs_create_file("axi", 0400, dump_dentry, 3285 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI], 3286 &hisi_sas_debugfs_axi_fops); 3287 3288 debugfs_create_file("ras", 0400, dump_dentry, 3289 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS], 3290 &hisi_sas_debugfs_ras_fops); 3291 3292 return; 3293 } 3294 3295 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba) 3296 { 3297 hisi_hba->hw->snapshot_prepare(hisi_hba); 3298 3299 hisi_sas_debugfs_snapshot_global_reg(hisi_hba); 3300 hisi_sas_debugfs_snapshot_port_reg(hisi_hba); 3301 hisi_sas_debugfs_snapshot_axi_reg(hisi_hba); 3302 hisi_sas_debugfs_snapshot_ras_reg(hisi_hba); 3303 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba); 3304 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba); 3305 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba); 3306 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba); 3307 3308 hisi_sas_debugfs_create_files(hisi_hba); 3309 3310 hisi_hba->hw->snapshot_restore(hisi_hba); 3311 } 3312 3313 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file, 3314 const char __user *user_buf, 3315 size_t count, loff_t *ppos) 3316 { 3317 struct hisi_hba *hisi_hba = file->f_inode->i_private; 3318 char buf[8]; 3319 3320 if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count) 3321 return -EFAULT; 3322 3323 if (count > 8) 3324 return -EFAULT; 3325 3326 if (copy_from_user(buf, user_buf, count)) 3327 return -EFAULT; 3328 3329 if (buf[0] != '1') 3330 return -EFAULT; 3331 3332 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work); 3333 3334 return count; 3335 } 3336 3337 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = { 3338 .write = &hisi_sas_debugfs_trigger_dump_write, 3339 .owner = THIS_MODULE, 3340 }; 3341 3342 enum { 3343 HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0, 3344 HISI_SAS_BIST_LOOPBACK_MODE_SERDES, 3345 HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, 3346 }; 3347 3348 static const struct { 3349 int value; 3350 char *name; 3351 } hisi_sas_debugfs_loop_linkrate[] = { 3352 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" }, 3353 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" }, 3354 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" }, 3355 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" }, 3356 }; 3357 3358 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p) 3359 { 3360 struct hisi_hba *hisi_hba = s->private; 3361 int i; 3362 3363 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) { 3364 int match = (hisi_hba->debugfs_bist_linkrate == 3365 hisi_sas_debugfs_loop_linkrate[i].value); 3366 3367 seq_printf(s, "%s%s%s ", match ? "[" : "", 3368 hisi_sas_debugfs_loop_linkrate[i].name, 3369 match ? "]" : ""); 3370 } 3371 seq_puts(s, "\n"); 3372 3373 return 0; 3374 } 3375 3376 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp, 3377 const char __user *buf, 3378 size_t count, loff_t *ppos) 3379 { 3380 struct seq_file *m = filp->private_data; 3381 struct hisi_hba *hisi_hba = m->private; 3382 char kbuf[16] = {}, *pkbuf; 3383 bool found = false; 3384 int i; 3385 3386 if (hisi_hba->debugfs_bist_enable) 3387 return -EPERM; 3388 3389 if (count >= sizeof(kbuf)) 3390 return -EOVERFLOW; 3391 3392 if (copy_from_user(kbuf, buf, count)) 3393 return -EINVAL; 3394 3395 pkbuf = strstrip(kbuf); 3396 3397 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) { 3398 if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name, 3399 pkbuf, 16)) { 3400 hisi_hba->debugfs_bist_linkrate = 3401 hisi_sas_debugfs_loop_linkrate[i].value; 3402 found = true; 3403 break; 3404 } 3405 } 3406 3407 if (!found) 3408 return -EINVAL; 3409 3410 return count; 3411 } 3412 3413 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode, 3414 struct file *filp) 3415 { 3416 return single_open(filp, hisi_sas_debugfs_bist_linkrate_show, 3417 inode->i_private); 3418 } 3419 3420 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = { 3421 .open = hisi_sas_debugfs_bist_linkrate_open, 3422 .read = seq_read, 3423 .write = hisi_sas_debugfs_bist_linkrate_write, 3424 .llseek = seq_lseek, 3425 .release = single_release, 3426 .owner = THIS_MODULE, 3427 }; 3428 3429 static const struct { 3430 int value; 3431 char *name; 3432 } hisi_sas_debugfs_loop_code_mode[] = { 3433 { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" }, 3434 { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" }, 3435 { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" }, 3436 { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" }, 3437 { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" }, 3438 { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" }, 3439 { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" }, 3440 { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" }, 3441 { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" }, 3442 { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" }, 3443 { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" }, 3444 { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" }, 3445 }; 3446 3447 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p) 3448 { 3449 struct hisi_hba *hisi_hba = s->private; 3450 int i; 3451 3452 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) { 3453 int match = (hisi_hba->debugfs_bist_code_mode == 3454 hisi_sas_debugfs_loop_code_mode[i].value); 3455 3456 seq_printf(s, "%s%s%s ", match ? "[" : "", 3457 hisi_sas_debugfs_loop_code_mode[i].name, 3458 match ? "]" : ""); 3459 } 3460 seq_puts(s, "\n"); 3461 3462 return 0; 3463 } 3464 3465 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp, 3466 const char __user *buf, 3467 size_t count, 3468 loff_t *ppos) 3469 { 3470 struct seq_file *m = filp->private_data; 3471 struct hisi_hba *hisi_hba = m->private; 3472 char kbuf[16] = {}, *pkbuf; 3473 bool found = false; 3474 int i; 3475 3476 if (hisi_hba->debugfs_bist_enable) 3477 return -EPERM; 3478 3479 if (count >= sizeof(kbuf)) 3480 return -EINVAL; 3481 3482 if (copy_from_user(kbuf, buf, count)) 3483 return -EOVERFLOW; 3484 3485 pkbuf = strstrip(kbuf); 3486 3487 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) { 3488 if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name, 3489 pkbuf, 16)) { 3490 hisi_hba->debugfs_bist_code_mode = 3491 hisi_sas_debugfs_loop_code_mode[i].value; 3492 found = true; 3493 break; 3494 } 3495 } 3496 3497 if (!found) 3498 return -EINVAL; 3499 3500 return count; 3501 } 3502 3503 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode, 3504 struct file *filp) 3505 { 3506 return single_open(filp, hisi_sas_debugfs_bist_code_mode_show, 3507 inode->i_private); 3508 } 3509 3510 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = { 3511 .open = hisi_sas_debugfs_bist_code_mode_open, 3512 .read = seq_read, 3513 .write = hisi_sas_debugfs_bist_code_mode_write, 3514 .llseek = seq_lseek, 3515 .release = single_release, 3516 .owner = THIS_MODULE, 3517 }; 3518 3519 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp, 3520 const char __user *buf, 3521 size_t count, loff_t *ppos) 3522 { 3523 struct seq_file *m = filp->private_data; 3524 struct hisi_hba *hisi_hba = m->private; 3525 unsigned int phy_no; 3526 int val; 3527 3528 if (hisi_hba->debugfs_bist_enable) 3529 return -EPERM; 3530 3531 val = kstrtouint_from_user(buf, count, 0, &phy_no); 3532 if (val) 3533 return val; 3534 3535 if (phy_no >= hisi_hba->n_phy) 3536 return -EINVAL; 3537 3538 hisi_hba->debugfs_bist_phy_no = phy_no; 3539 3540 return count; 3541 } 3542 3543 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p) 3544 { 3545 struct hisi_hba *hisi_hba = s->private; 3546 3547 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no); 3548 3549 return 0; 3550 } 3551 3552 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode, 3553 struct file *filp) 3554 { 3555 return single_open(filp, hisi_sas_debugfs_bist_phy_show, 3556 inode->i_private); 3557 } 3558 3559 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = { 3560 .open = hisi_sas_debugfs_bist_phy_open, 3561 .read = seq_read, 3562 .write = hisi_sas_debugfs_bist_phy_write, 3563 .llseek = seq_lseek, 3564 .release = single_release, 3565 .owner = THIS_MODULE, 3566 }; 3567 3568 static const struct { 3569 int value; 3570 char *name; 3571 } hisi_sas_debugfs_loop_modes[] = { 3572 { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" }, 3573 { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" }, 3574 { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" }, 3575 }; 3576 3577 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p) 3578 { 3579 struct hisi_hba *hisi_hba = s->private; 3580 int i; 3581 3582 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) { 3583 int match = (hisi_hba->debugfs_bist_mode == 3584 hisi_sas_debugfs_loop_modes[i].value); 3585 3586 seq_printf(s, "%s%s%s ", match ? "[" : "", 3587 hisi_sas_debugfs_loop_modes[i].name, 3588 match ? "]" : ""); 3589 } 3590 seq_puts(s, "\n"); 3591 3592 return 0; 3593 } 3594 3595 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp, 3596 const char __user *buf, 3597 size_t count, loff_t *ppos) 3598 { 3599 struct seq_file *m = filp->private_data; 3600 struct hisi_hba *hisi_hba = m->private; 3601 char kbuf[16] = {}, *pkbuf; 3602 bool found = false; 3603 int i; 3604 3605 if (hisi_hba->debugfs_bist_enable) 3606 return -EPERM; 3607 3608 if (count >= sizeof(kbuf)) 3609 return -EINVAL; 3610 3611 if (copy_from_user(kbuf, buf, count)) 3612 return -EOVERFLOW; 3613 3614 pkbuf = strstrip(kbuf); 3615 3616 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) { 3617 if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) { 3618 hisi_hba->debugfs_bist_mode = 3619 hisi_sas_debugfs_loop_modes[i].value; 3620 found = true; 3621 break; 3622 } 3623 } 3624 3625 if (!found) 3626 return -EINVAL; 3627 3628 return count; 3629 } 3630 3631 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode, 3632 struct file *filp) 3633 { 3634 return single_open(filp, hisi_sas_debugfs_bist_mode_show, 3635 inode->i_private); 3636 } 3637 3638 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = { 3639 .open = hisi_sas_debugfs_bist_mode_open, 3640 .read = seq_read, 3641 .write = hisi_sas_debugfs_bist_mode_write, 3642 .llseek = seq_lseek, 3643 .release = single_release, 3644 .owner = THIS_MODULE, 3645 }; 3646 3647 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp, 3648 const char __user *buf, 3649 size_t count, loff_t *ppos) 3650 { 3651 struct seq_file *m = filp->private_data; 3652 struct hisi_hba *hisi_hba = m->private; 3653 unsigned int enable; 3654 int val; 3655 3656 val = kstrtouint_from_user(buf, count, 0, &enable); 3657 if (val) 3658 return val; 3659 3660 if (enable > 1) 3661 return -EINVAL; 3662 3663 if (enable == hisi_hba->debugfs_bist_enable) 3664 return count; 3665 3666 if (!hisi_hba->hw->set_bist) 3667 return -EPERM; 3668 3669 val = hisi_hba->hw->set_bist(hisi_hba, enable); 3670 if (val < 0) 3671 return val; 3672 3673 hisi_hba->debugfs_bist_enable = enable; 3674 3675 return count; 3676 } 3677 3678 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p) 3679 { 3680 struct hisi_hba *hisi_hba = s->private; 3681 3682 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable); 3683 3684 return 0; 3685 } 3686 3687 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode, 3688 struct file *filp) 3689 { 3690 return single_open(filp, hisi_sas_debugfs_bist_enable_show, 3691 inode->i_private); 3692 } 3693 3694 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = { 3695 .open = hisi_sas_debugfs_bist_enable_open, 3696 .read = seq_read, 3697 .write = hisi_sas_debugfs_bist_enable_write, 3698 .llseek = seq_lseek, 3699 .release = single_release, 3700 .owner = THIS_MODULE, 3701 }; 3702 3703 static const struct { 3704 char *name; 3705 } hisi_sas_debugfs_ffe_name[FFE_CFG_MAX] = { 3706 { "SAS_1_5_GBPS" }, 3707 { "SAS_3_0_GBPS" }, 3708 { "SAS_6_0_GBPS" }, 3709 { "SAS_12_0_GBPS" }, 3710 { "FFE_RESV" }, 3711 { "SATA_1_5_GBPS" }, 3712 { "SATA_3_0_GBPS" }, 3713 { "SATA_6_0_GBPS" }, 3714 }; 3715 3716 static ssize_t hisi_sas_debugfs_write(struct file *filp, 3717 const char __user *buf, 3718 size_t count, loff_t *ppos) 3719 { 3720 struct seq_file *m = filp->private_data; 3721 u32 *val = m->private; 3722 int res; 3723 3724 res = kstrtouint_from_user(buf, count, 0, val); 3725 if (res) 3726 return res; 3727 3728 return count; 3729 } 3730 3731 static int hisi_sas_debugfs_show(struct seq_file *s, void *p) 3732 { 3733 u32 *val = s->private; 3734 3735 seq_printf(s, "0x%x\n", *val); 3736 3737 return 0; 3738 } 3739 3740 static int hisi_sas_debugfs_open(struct inode *inode, struct file *filp) 3741 { 3742 return single_open(filp, hisi_sas_debugfs_show, 3743 inode->i_private); 3744 } 3745 3746 static const struct file_operations hisi_sas_debugfs_ops = { 3747 .open = hisi_sas_debugfs_open, 3748 .read = seq_read, 3749 .write = hisi_sas_debugfs_write, 3750 .llseek = seq_lseek, 3751 .release = single_release, 3752 .owner = THIS_MODULE, 3753 }; 3754 3755 static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp, 3756 const char __user *buf, 3757 size_t count, loff_t *ppos) 3758 { 3759 struct seq_file *s = filp->private_data; 3760 struct hisi_sas_phy *phy = s->private; 3761 unsigned int set_val; 3762 int res; 3763 3764 res = kstrtouint_from_user(buf, count, 0, &set_val); 3765 if (res) 3766 return res; 3767 3768 if (set_val > 0) 3769 return -EINVAL; 3770 3771 atomic_set(&phy->down_cnt, 0); 3772 3773 return count; 3774 } 3775 3776 static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p) 3777 { 3778 struct hisi_sas_phy *phy = s->private; 3779 3780 seq_printf(s, "%d\n", atomic_read(&phy->down_cnt)); 3781 3782 return 0; 3783 } 3784 3785 static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode, 3786 struct file *filp) 3787 { 3788 return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show, 3789 inode->i_private); 3790 } 3791 3792 static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = { 3793 .open = hisi_sas_debugfs_phy_down_cnt_open, 3794 .read = seq_read, 3795 .write = hisi_sas_debugfs_phy_down_cnt_write, 3796 .llseek = seq_lseek, 3797 .release = single_release, 3798 .owner = THIS_MODULE, 3799 }; 3800 3801 void hisi_sas_debugfs_work_handler(struct work_struct *work) 3802 { 3803 struct hisi_hba *hisi_hba = 3804 container_of(work, struct hisi_hba, debugfs_work); 3805 int debugfs_dump_index = hisi_hba->debugfs_dump_index; 3806 struct device *dev = hisi_hba->dev; 3807 u64 timestamp = local_clock(); 3808 3809 if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) { 3810 dev_warn(dev, "dump count exceeded!\n"); 3811 return; 3812 } 3813 3814 do_div(timestamp, NSEC_PER_MSEC); 3815 hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp; 3816 3817 hisi_sas_debugfs_snapshot_regs(hisi_hba); 3818 hisi_hba->debugfs_dump_index++; 3819 } 3820 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler); 3821 3822 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index) 3823 { 3824 struct device *dev = hisi_hba->dev; 3825 int i; 3826 3827 devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache); 3828 devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache); 3829 devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost); 3830 devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct); 3831 3832 for (i = 0; i < hisi_hba->queue_count; i++) 3833 devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr); 3834 3835 for (i = 0; i < hisi_hba->queue_count; i++) 3836 devm_kfree(dev, 3837 hisi_hba->debugfs_cq[dump_index][i].complete_hdr); 3838 3839 for (i = 0; i < DEBUGFS_REGS_NUM; i++) 3840 devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data); 3841 3842 for (i = 0; i < hisi_hba->n_phy; i++) 3843 devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data); 3844 } 3845 3846 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index) 3847 { 3848 const struct hisi_sas_hw *hw = hisi_hba->hw; 3849 struct device *dev = hisi_hba->dev; 3850 int p, c, d, r, i; 3851 size_t sz; 3852 3853 for (r = 0; r < DEBUGFS_REGS_NUM; r++) { 3854 struct hisi_sas_debugfs_regs *regs = 3855 &hisi_hba->debugfs_regs[dump_index][r]; 3856 3857 sz = hw->debugfs_reg_array[r]->count * 4; 3858 regs->data = devm_kmalloc(dev, sz, GFP_KERNEL); 3859 if (!regs->data) 3860 goto fail; 3861 regs->hisi_hba = hisi_hba; 3862 } 3863 3864 sz = hw->debugfs_reg_port->count * 4; 3865 for (p = 0; p < hisi_hba->n_phy; p++) { 3866 struct hisi_sas_debugfs_port *port = 3867 &hisi_hba->debugfs_port_reg[dump_index][p]; 3868 3869 port->data = devm_kmalloc(dev, sz, GFP_KERNEL); 3870 if (!port->data) 3871 goto fail; 3872 port->phy = &hisi_hba->phy[p]; 3873 } 3874 3875 sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; 3876 for (c = 0; c < hisi_hba->queue_count; c++) { 3877 struct hisi_sas_debugfs_cq *cq = 3878 &hisi_hba->debugfs_cq[dump_index][c]; 3879 3880 cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL); 3881 if (!cq->complete_hdr) 3882 goto fail; 3883 cq->cq = &hisi_hba->cq[c]; 3884 } 3885 3886 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; 3887 for (d = 0; d < hisi_hba->queue_count; d++) { 3888 struct hisi_sas_debugfs_dq *dq = 3889 &hisi_hba->debugfs_dq[dump_index][d]; 3890 3891 dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL); 3892 if (!dq->hdr) 3893 goto fail; 3894 dq->dq = &hisi_hba->dq[d]; 3895 } 3896 3897 sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost); 3898 3899 hisi_hba->debugfs_iost[dump_index].iost = 3900 devm_kmalloc(dev, sz, GFP_KERNEL); 3901 if (!hisi_hba->debugfs_iost[dump_index].iost) 3902 goto fail; 3903 3904 sz = HISI_SAS_IOST_ITCT_CACHE_NUM * 3905 sizeof(struct hisi_sas_iost_itct_cache); 3906 3907 hisi_hba->debugfs_iost_cache[dump_index].cache = 3908 devm_kmalloc(dev, sz, GFP_KERNEL); 3909 if (!hisi_hba->debugfs_iost_cache[dump_index].cache) 3910 goto fail; 3911 3912 sz = HISI_SAS_IOST_ITCT_CACHE_NUM * 3913 sizeof(struct hisi_sas_iost_itct_cache); 3914 3915 hisi_hba->debugfs_itct_cache[dump_index].cache = 3916 devm_kmalloc(dev, sz, GFP_KERNEL); 3917 if (!hisi_hba->debugfs_itct_cache[dump_index].cache) 3918 goto fail; 3919 3920 /* New memory allocation must be locate before itct */ 3921 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); 3922 3923 hisi_hba->debugfs_itct[dump_index].itct = 3924 devm_kmalloc(dev, sz, GFP_KERNEL); 3925 if (!hisi_hba->debugfs_itct[dump_index].itct) 3926 goto fail; 3927 3928 return 0; 3929 fail: 3930 for (i = 0; i < hisi_sas_debugfs_dump_count; i++) 3931 hisi_sas_debugfs_release(hisi_hba, i); 3932 return -ENOMEM; 3933 } 3934 3935 static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba) 3936 { 3937 struct dentry *dir = debugfs_create_dir("phy_down_cnt", 3938 hisi_hba->debugfs_dir); 3939 char name[16]; 3940 int phy_no; 3941 3942 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) { 3943 snprintf(name, 16, "%d", phy_no); 3944 debugfs_create_file(name, 0600, dir, 3945 &hisi_hba->phy[phy_no], 3946 &hisi_sas_debugfs_phy_down_cnt_ops); 3947 } 3948 } 3949 3950 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba) 3951 { 3952 struct dentry *ports_dentry; 3953 int phy_no; 3954 3955 hisi_hba->debugfs_bist_dentry = 3956 debugfs_create_dir("bist", hisi_hba->debugfs_dir); 3957 debugfs_create_file("link_rate", 0600, 3958 hisi_hba->debugfs_bist_dentry, hisi_hba, 3959 &hisi_sas_debugfs_bist_linkrate_ops); 3960 3961 debugfs_create_file("code_mode", 0600, 3962 hisi_hba->debugfs_bist_dentry, hisi_hba, 3963 &hisi_sas_debugfs_bist_code_mode_ops); 3964 3965 debugfs_create_file("fixed_code", 0600, 3966 hisi_hba->debugfs_bist_dentry, 3967 &hisi_hba->debugfs_bist_fixed_code[0], 3968 &hisi_sas_debugfs_ops); 3969 3970 debugfs_create_file("fixed_code_1", 0600, 3971 hisi_hba->debugfs_bist_dentry, 3972 &hisi_hba->debugfs_bist_fixed_code[1], 3973 &hisi_sas_debugfs_ops); 3974 3975 debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry, 3976 hisi_hba, &hisi_sas_debugfs_bist_phy_ops); 3977 3978 debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry, 3979 &hisi_hba->debugfs_bist_cnt); 3980 3981 debugfs_create_file("loopback_mode", 0600, 3982 hisi_hba->debugfs_bist_dentry, 3983 hisi_hba, &hisi_sas_debugfs_bist_mode_ops); 3984 3985 debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry, 3986 hisi_hba, &hisi_sas_debugfs_bist_enable_ops); 3987 3988 ports_dentry = debugfs_create_dir("port", hisi_hba->debugfs_bist_dentry); 3989 3990 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) { 3991 struct dentry *port_dentry; 3992 struct dentry *ffe_dentry; 3993 char name[256]; 3994 int i; 3995 3996 snprintf(name, 256, "%d", phy_no); 3997 port_dentry = debugfs_create_dir(name, ports_dentry); 3998 ffe_dentry = debugfs_create_dir("ffe", port_dentry); 3999 for (i = 0; i < FFE_CFG_MAX; i++) { 4000 if (i == FFE_RESV) 4001 continue; 4002 debugfs_create_file(hisi_sas_debugfs_ffe_name[i].name, 4003 0600, ffe_dentry, 4004 &hisi_hba->debugfs_bist_ffe[phy_no][i], 4005 &hisi_sas_debugfs_ops); 4006 } 4007 } 4008 4009 hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS; 4010 } 4011 4012 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba) 4013 { 4014 struct device *dev = hisi_hba->dev; 4015 int i; 4016 4017 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev), 4018 hisi_sas_debugfs_dir); 4019 debugfs_create_file("trigger_dump", 0200, 4020 hisi_hba->debugfs_dir, 4021 hisi_hba, 4022 &hisi_sas_debugfs_trigger_dump_fops); 4023 4024 /* create bist structures */ 4025 hisi_sas_debugfs_bist_init(hisi_hba); 4026 4027 hisi_hba->debugfs_dump_dentry = 4028 debugfs_create_dir("dump", hisi_hba->debugfs_dir); 4029 4030 hisi_sas_debugfs_phy_down_cnt_init(hisi_hba); 4031 4032 for (i = 0; i < hisi_sas_debugfs_dump_count; i++) { 4033 if (hisi_sas_debugfs_alloc(hisi_hba, i)) { 4034 debugfs_remove_recursive(hisi_hba->debugfs_dir); 4035 dev_dbg(dev, "failed to init debugfs!\n"); 4036 break; 4037 } 4038 } 4039 } 4040 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init); 4041 4042 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba) 4043 { 4044 debugfs_remove_recursive(hisi_hba->debugfs_dir); 4045 } 4046 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit); 4047 4048 int hisi_sas_remove(struct platform_device *pdev) 4049 { 4050 struct sas_ha_struct *sha = platform_get_drvdata(pdev); 4051 struct hisi_hba *hisi_hba = sha->lldd_ha; 4052 struct Scsi_Host *shost = sha->core.shost; 4053 4054 if (timer_pending(&hisi_hba->timer)) 4055 del_timer(&hisi_hba->timer); 4056 4057 sas_unregister_ha(sha); 4058 sas_remove_host(sha->core.shost); 4059 4060 hisi_sas_free(hisi_hba); 4061 scsi_host_put(shost); 4062 return 0; 4063 } 4064 EXPORT_SYMBOL_GPL(hisi_sas_remove); 4065 4066 bool hisi_sas_debugfs_enable; 4067 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable); 4068 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444); 4069 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)"); 4070 4071 u32 hisi_sas_debugfs_dump_count = 1; 4072 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count); 4073 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444); 4074 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow"); 4075 4076 static __init int hisi_sas_init(void) 4077 { 4078 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); 4079 if (!hisi_sas_stt) 4080 return -ENOMEM; 4081 4082 if (hisi_sas_debugfs_enable) { 4083 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL); 4084 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) { 4085 pr_info("hisi_sas: Limiting debugfs dump count\n"); 4086 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP; 4087 } 4088 } 4089 4090 return 0; 4091 } 4092 4093 static __exit void hisi_sas_exit(void) 4094 { 4095 sas_release_transport(hisi_sas_stt); 4096 4097 debugfs_remove(hisi_sas_debugfs_dir); 4098 } 4099 4100 module_init(hisi_sas_init); 4101 module_exit(hisi_sas_exit); 4102 4103 MODULE_LICENSE("GPL"); 4104 MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); 4105 MODULE_DESCRIPTION("HISILICON SAS controller driver"); 4106 MODULE_ALIAS("platform:" DRV_NAME); 4107