root/drivers/scsi/hisi_sas/hisi_sas_main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hisi_sas_get_ata_protocol
  2. hisi_sas_sata_done
  3. hisi_sas_get_prog_phy_linkrate_mask
  4. dev_to_hisi_hba
  5. to_hisi_sas_port
  6. hisi_sas_stop_phys
  7. hisi_sas_slot_index_clear
  8. hisi_sas_slot_index_free
  9. hisi_sas_slot_index_set
  10. hisi_sas_slot_index_alloc
  11. hisi_sas_slot_index_init
  12. hisi_sas_slot_task_free
  13. hisi_sas_task_prep_smp
  14. hisi_sas_task_prep_ssp
  15. hisi_sas_task_prep_ata
  16. hisi_sas_task_prep_abort
  17. hisi_sas_dma_unmap
  18. hisi_sas_dma_map
  19. hisi_sas_dif_dma_unmap
  20. hisi_sas_dif_dma_map
  21. hisi_sas_task_prep
  22. hisi_sas_task_exec
  23. hisi_sas_bytes_dmaed
  24. hisi_sas_alloc_dev
  25. hisi_sas_init_device
  26. hisi_sas_dev_found
  27. hisi_sas_slave_configure
  28. hisi_sas_scan_start
  29. hisi_sas_scan_finished
  30. hisi_sas_phyup_work
  31. hisi_sas_linkreset_work
  32. hisi_sas_notify_phy_event
  33. hisi_sas_wait_phyup_timedout
  34. hisi_sas_phy_oob_ready
  35. hisi_sas_phy_init
  36. hisi_sas_phy_enable
  37. hisi_sas_port_notify_formed
  38. hisi_sas_do_release_task
  39. hisi_sas_release_task
  40. hisi_sas_release_tasks
  41. hisi_sas_dereg_device
  42. hisi_sas_dev_gone
  43. hisi_sas_queue_command
  44. hisi_sas_phy_set_linkrate
  45. hisi_sas_control_phy
  46. hisi_sas_task_done
  47. hisi_sas_tmf_timedout
  48. hisi_sas_exec_internal_tmf_task
  49. hisi_sas_fill_ata_reset_cmd
  50. hisi_sas_softreset_ata_disk
  51. hisi_sas_debug_issue_ssp_tmf
  52. hisi_sas_refresh_port_id
  53. hisi_sas_rescan_topology
  54. hisi_sas_reset_init_all_devices
  55. hisi_sas_send_ata_reset_each_phy
  56. hisi_sas_terminate_stp_reject
  57. hisi_sas_controller_reset_prepare
  58. hisi_sas_controller_reset_done
  59. hisi_sas_controller_reset
  60. hisi_sas_abort_task
  61. hisi_sas_abort_task_set
  62. hisi_sas_clear_aca
  63. hisi_sas_debug_I_T_nexus_reset
  64. hisi_sas_I_T_nexus_reset
  65. hisi_sas_lu_reset
  66. hisi_sas_clear_nexus_ha
  67. hisi_sas_query_task
  68. hisi_sas_internal_abort_task_exec
  69. _hisi_sas_internal_task_abort
  70. hisi_sas_internal_task_abort
  71. hisi_sas_port_formed
  72. hisi_sas_write_gpio
  73. hisi_sas_phy_disconnected
  74. hisi_sas_phy_down
  75. hisi_sas_kill_tasklets
  76. hisi_sas_host_reset
  77. hisi_sas_init_mem
  78. hisi_sas_alloc
  79. hisi_sas_free
  80. hisi_sas_rst_work_handler
  81. hisi_sas_sync_rst_work_handler
  82. hisi_sas_get_fw_info
  83. hisi_sas_shost_alloc
  84. hisi_sas_probe
  85. hisi_sas_debugfs_snapshot_cq_reg
  86. hisi_sas_debugfs_snapshot_dq_reg
  87. hisi_sas_debugfs_snapshot_port_reg
  88. hisi_sas_debugfs_snapshot_global_reg
  89. hisi_sas_debugfs_snapshot_axi_reg
  90. hisi_sas_debugfs_snapshot_ras_reg
  91. hisi_sas_debugfs_snapshot_itct_reg
  92. hisi_sas_debugfs_snapshot_iost_reg
  93. hisi_sas_debugfs_to_reg_name
  94. hisi_sas_debugfs_print_reg
  95. hisi_sas_debugfs_global_show
  96. hisi_sas_debugfs_global_open
  97. hisi_sas_debugfs_axi_show
  98. hisi_sas_debugfs_axi_open
  99. hisi_sas_debugfs_ras_show
  100. hisi_sas_debugfs_ras_open
  101. hisi_sas_debugfs_port_show
  102. hisi_sas_debugfs_port_open
  103. hisi_sas_show_row_64
  104. hisi_sas_show_row_32
  105. hisi_sas_cq_show_slot
  106. hisi_sas_debugfs_cq_show
  107. hisi_sas_debugfs_cq_open
  108. hisi_sas_dq_show_slot
  109. hisi_sas_debugfs_dq_show
  110. hisi_sas_debugfs_dq_open
  111. hisi_sas_debugfs_iost_show
  112. hisi_sas_debugfs_iost_open
  113. hisi_sas_debugfs_iost_cache_show
  114. hisi_sas_debugfs_iost_cache_open
  115. hisi_sas_debugfs_itct_show
  116. hisi_sas_debugfs_itct_open
  117. hisi_sas_debugfs_itct_cache_show
  118. hisi_sas_debugfs_itct_cache_open
  119. hisi_sas_debugfs_create_files
  120. hisi_sas_debugfs_snapshot_regs
  121. hisi_sas_debugfs_trigger_dump_write
  122. hisi_sas_debugfs_bist_linkrate_show
  123. hisi_sas_debugfs_bist_linkrate_write
  124. hisi_sas_debugfs_bist_linkrate_open
  125. hisi_sas_debugfs_bist_code_mode_show
  126. hisi_sas_debugfs_bist_code_mode_write
  127. hisi_sas_debugfs_bist_code_mode_open
  128. hisi_sas_debugfs_bist_phy_write
  129. hisi_sas_debugfs_bist_phy_show
  130. hisi_sas_debugfs_bist_phy_open
  131. hisi_sas_debugfs_bist_mode_show
  132. hisi_sas_debugfs_bist_mode_write
  133. hisi_sas_debugfs_bist_mode_open
  134. hisi_sas_debugfs_bist_enable_write
  135. hisi_sas_debugfs_bist_enable_show
  136. hisi_sas_debugfs_bist_enable_open
  137. hisi_sas_debugfs_work_handler
  138. hisi_sas_debugfs_release
  139. hisi_sas_debugfs_alloc
  140. hisi_sas_debugfs_bist_init
  141. hisi_sas_debugfs_init
  142. hisi_sas_debugfs_exit
  143. hisi_sas_remove
  144. hisi_sas_init
  145. hisi_sas_exit

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

/* [<][>][^][v][top][bottom][index][help] */