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