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