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