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