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