]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: hisi_sas: Reject setting programmed minimum linkrate > 1.5G
[linux.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 int 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->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
959                 return -EINVAL;
960
961         if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
962                 max = sas_phy->phy->maximum_linkrate;
963                 min = r->minimum_linkrate;
964         } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
965                 max = r->maximum_linkrate;
966                 min = sas_phy->phy->minimum_linkrate;
967         } else
968                 return -EINVAL;
969
970         _r.maximum_linkrate = max;
971         _r.minimum_linkrate = min;
972
973         sas_phy->phy->maximum_linkrate = max;
974         sas_phy->phy->minimum_linkrate = min;
975
976         hisi_hba->hw->phy_disable(hisi_hba, phy_no);
977         msleep(100);
978         hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
979         hisi_hba->hw->phy_start(hisi_hba, phy_no);
980
981         return 0;
982 }
983
984 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
985                                 void *funcdata)
986 {
987         struct sas_ha_struct *sas_ha = sas_phy->ha;
988         struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
989         int phy_no = sas_phy->id;
990
991         switch (func) {
992         case PHY_FUNC_HARD_RESET:
993                 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
994                 break;
995
996         case PHY_FUNC_LINK_RESET:
997                 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
998                 msleep(100);
999                 hisi_hba->hw->phy_start(hisi_hba, phy_no);
1000                 break;
1001
1002         case PHY_FUNC_DISABLE:
1003                 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1004                 break;
1005
1006         case PHY_FUNC_SET_LINK_RATE:
1007                 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1008         case PHY_FUNC_GET_EVENTS:
1009                 if (hisi_hba->hw->get_events) {
1010                         hisi_hba->hw->get_events(hisi_hba, phy_no);
1011                         break;
1012                 }
1013                 /* fallthru */
1014         case PHY_FUNC_RELEASE_SPINUP_HOLD:
1015         default:
1016                 return -EOPNOTSUPP;
1017         }
1018         return 0;
1019 }
1020
1021 static void hisi_sas_task_done(struct sas_task *task)
1022 {
1023         del_timer(&task->slow_task->timer);
1024         complete(&task->slow_task->completion);
1025 }
1026
1027 static void hisi_sas_tmf_timedout(struct timer_list *t)
1028 {
1029         struct sas_task_slow *slow = from_timer(slow, t, timer);
1030         struct sas_task *task = slow->task;
1031         unsigned long flags;
1032         bool is_completed = true;
1033
1034         spin_lock_irqsave(&task->task_state_lock, flags);
1035         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1036                 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1037                 is_completed = false;
1038         }
1039         spin_unlock_irqrestore(&task->task_state_lock, flags);
1040
1041         if (!is_completed)
1042                 complete(&task->slow_task->completion);
1043 }
1044
1045 #define TASK_TIMEOUT 20
1046 #define TASK_RETRY 3
1047 #define INTERNAL_ABORT_TIMEOUT 6
1048 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1049                                            void *parameter, u32 para_len,
1050                                            struct hisi_sas_tmf_task *tmf)
1051 {
1052         struct hisi_sas_device *sas_dev = device->lldd_dev;
1053         struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1054         struct device *dev = hisi_hba->dev;
1055         struct sas_task *task;
1056         int res, retry;
1057
1058         for (retry = 0; retry < TASK_RETRY; retry++) {
1059                 task = sas_alloc_slow_task(GFP_KERNEL);
1060                 if (!task)
1061                         return -ENOMEM;
1062
1063                 task->dev = device;
1064                 task->task_proto = device->tproto;
1065
1066                 if (dev_is_sata(device)) {
1067                         task->ata_task.device_control_reg_update = 1;
1068                         memcpy(&task->ata_task.fis, parameter, para_len);
1069                 } else {
1070                         memcpy(&task->ssp_task, parameter, para_len);
1071                 }
1072                 task->task_done = hisi_sas_task_done;
1073
1074                 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1075                 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1076                 add_timer(&task->slow_task->timer);
1077
1078                 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1079
1080                 if (res) {
1081                         del_timer(&task->slow_task->timer);
1082                         dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1083                                 res);
1084                         goto ex_err;
1085                 }
1086
1087                 wait_for_completion(&task->slow_task->completion);
1088                 res = TMF_RESP_FUNC_FAILED;
1089                 /* Even TMF timed out, return direct. */
1090                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1091                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1092                                 struct hisi_sas_slot *slot = task->lldd_task;
1093
1094                                 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1095                                 if (slot) {
1096                                         struct hisi_sas_cq *cq =
1097                                                &hisi_hba->cq[slot->dlvry_queue];
1098                                         /*
1099                                          * flush tasklet to avoid free'ing task
1100                                          * before using task in IO completion
1101                                          */
1102                                         tasklet_kill(&cq->tasklet);
1103                                         slot->task = NULL;
1104                                 }
1105
1106                                 goto ex_err;
1107                         } else
1108                                 dev_err(dev, "abort tmf: TMF task timeout\n");
1109                 }
1110
1111                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1112                      task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1113                         res = TMF_RESP_FUNC_COMPLETE;
1114                         break;
1115                 }
1116
1117                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1118                         task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1119                         res = TMF_RESP_FUNC_SUCC;
1120                         break;
1121                 }
1122
1123                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1124                       task->task_status.stat == SAS_DATA_UNDERRUN) {
1125                         /* no error, but return the number of bytes of
1126                          * underrun
1127                          */
1128                         dev_warn(dev, "abort tmf: task to dev %016llx "
1129                                  "resp: 0x%x sts 0x%x underrun\n",
1130                                  SAS_ADDR(device->sas_addr),
1131                                  task->task_status.resp,
1132                                  task->task_status.stat);
1133                         res = task->task_status.residual;
1134                         break;
1135                 }
1136
1137                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1138                         task->task_status.stat == SAS_DATA_OVERRUN) {
1139                         dev_warn(dev, "abort tmf: blocked task error\n");
1140                         res = -EMSGSIZE;
1141                         break;
1142                 }
1143
1144                 dev_warn(dev, "abort tmf: task to dev "
1145                          "%016llx resp: 0x%x status 0x%x\n",
1146                          SAS_ADDR(device->sas_addr), task->task_status.resp,
1147                          task->task_status.stat);
1148                 sas_free_task(task);
1149                 task = NULL;
1150         }
1151 ex_err:
1152         if (retry == TASK_RETRY)
1153                 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1154         sas_free_task(task);
1155         return res;
1156 }
1157
1158 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1159                 bool reset, int pmp, u8 *fis)
1160 {
1161         struct ata_taskfile tf;
1162
1163         ata_tf_init(dev, &tf);
1164         if (reset)
1165                 tf.ctl |= ATA_SRST;
1166         else
1167                 tf.ctl &= ~ATA_SRST;
1168         tf.command = ATA_CMD_DEV_RESET;
1169         ata_tf_to_fis(&tf, pmp, 0, fis);
1170 }
1171
1172 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1173 {
1174         u8 fis[20] = {0};
1175         struct ata_port *ap = device->sata_dev.ap;
1176         struct ata_link *link;
1177         int rc = TMF_RESP_FUNC_FAILED;
1178         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1179         struct device *dev = hisi_hba->dev;
1180         int s = sizeof(struct host_to_dev_fis);
1181
1182         ata_for_each_link(link, ap, EDGE) {
1183                 int pmp = sata_srst_pmp(link);
1184
1185                 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1186                 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1187                 if (rc != TMF_RESP_FUNC_COMPLETE)
1188                         break;
1189         }
1190
1191         if (rc == TMF_RESP_FUNC_COMPLETE) {
1192                 ata_for_each_link(link, ap, EDGE) {
1193                         int pmp = sata_srst_pmp(link);
1194
1195                         hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1196                         rc = hisi_sas_exec_internal_tmf_task(device, fis,
1197                                                              s, NULL);
1198                         if (rc != TMF_RESP_FUNC_COMPLETE)
1199                                 dev_err(dev, "ata disk de-reset failed\n");
1200                 }
1201         } else {
1202                 dev_err(dev, "ata disk reset failed\n");
1203         }
1204
1205         if (rc == TMF_RESP_FUNC_COMPLETE)
1206                 hisi_sas_release_task(hisi_hba, device);
1207
1208         return rc;
1209 }
1210
1211 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1212                                 u8 *lun, struct hisi_sas_tmf_task *tmf)
1213 {
1214         struct sas_ssp_task ssp_task;
1215
1216         if (!(device->tproto & SAS_PROTOCOL_SSP))
1217                 return TMF_RESP_FUNC_ESUPP;
1218
1219         memcpy(ssp_task.LUN, lun, 8);
1220
1221         return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1222                                 sizeof(ssp_task), tmf);
1223 }
1224
1225 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1226 {
1227         u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1228         int i;
1229
1230         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1231                 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1232                 struct domain_device *device = sas_dev->sas_device;
1233                 struct asd_sas_port *sas_port;
1234                 struct hisi_sas_port *port;
1235                 struct hisi_sas_phy *phy = NULL;
1236                 struct asd_sas_phy *sas_phy;
1237
1238                 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1239                                 || !device || !device->port)
1240                         continue;
1241
1242                 sas_port = device->port;
1243                 port = to_hisi_sas_port(sas_port);
1244
1245                 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1246                         if (state & BIT(sas_phy->id)) {
1247                                 phy = sas_phy->lldd_phy;
1248                                 break;
1249                         }
1250
1251                 if (phy) {
1252                         port->id = phy->port_id;
1253
1254                         /* Update linkrate of directly attached device. */
1255                         if (!device->parent)
1256                                 device->linkrate = phy->sas_phy.linkrate;
1257
1258                         hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1259                 } else
1260                         port->id = 0xff;
1261         }
1262 }
1263
1264 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1265                               u32 state)
1266 {
1267         struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1268         struct asd_sas_port *_sas_port = NULL;
1269         int phy_no;
1270
1271         for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1272                 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1273                 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1274                 struct asd_sas_port *sas_port = sas_phy->port;
1275                 bool do_port_check = !!(_sas_port != sas_port);
1276
1277                 if (!sas_phy->phy->enabled)
1278                         continue;
1279
1280                 /* Report PHY state change to libsas */
1281                 if (state & BIT(phy_no)) {
1282                         if (do_port_check && sas_port && sas_port->port_dev) {
1283                                 struct domain_device *dev = sas_port->port_dev;
1284
1285                                 _sas_port = sas_port;
1286
1287                                 if (DEV_IS_EXPANDER(dev->dev_type))
1288                                         sas_ha->notify_port_event(sas_phy,
1289                                                         PORTE_BROADCAST_RCVD);
1290                         }
1291                 } else if (old_state & (1 << phy_no))
1292                         /* PHY down but was up before */
1293                         hisi_sas_phy_down(hisi_hba, phy_no, 0);
1294
1295         }
1296 }
1297
1298 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1299 {
1300         struct hisi_sas_device *sas_dev;
1301         struct domain_device *device;
1302         int i;
1303
1304         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1305                 sas_dev = &hisi_hba->devices[i];
1306                 device = sas_dev->sas_device;
1307
1308                 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1309                         continue;
1310
1311                 hisi_sas_init_device(device);
1312         }
1313 }
1314
1315 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1316                                              struct asd_sas_port *sas_port,
1317                                              struct domain_device *device)
1318 {
1319         struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1320         struct ata_port *ap = device->sata_dev.ap;
1321         struct device *dev = hisi_hba->dev;
1322         int s = sizeof(struct host_to_dev_fis);
1323         int rc = TMF_RESP_FUNC_FAILED;
1324         struct asd_sas_phy *sas_phy;
1325         struct ata_link *link;
1326         u8 fis[20] = {0};
1327         u32 state;
1328
1329         state = hisi_hba->hw->get_phys_state(hisi_hba);
1330         list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1331                 if (!(state & BIT(sas_phy->id)))
1332                         continue;
1333
1334                 ata_for_each_link(link, ap, EDGE) {
1335                         int pmp = sata_srst_pmp(link);
1336
1337                         tmf_task.phy_id = sas_phy->id;
1338                         hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1339                         rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1340                                                              &tmf_task);
1341                         if (rc != TMF_RESP_FUNC_COMPLETE) {
1342                                 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1343                                         sas_phy->id, rc);
1344                                 break;
1345                         }
1346                 }
1347         }
1348 }
1349
1350 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1351 {
1352         struct device *dev = hisi_hba->dev;
1353         int port_no, rc, i;
1354
1355         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1356                 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1357                 struct domain_device *device = sas_dev->sas_device;
1358
1359                 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1360                         continue;
1361
1362                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1363                                                   HISI_SAS_INT_ABT_DEV, 0);
1364                 if (rc < 0)
1365                         dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1366         }
1367
1368         for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1369                 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1370                 struct asd_sas_port *sas_port = &port->sas_port;
1371                 struct domain_device *port_dev = sas_port->port_dev;
1372                 struct domain_device *device;
1373
1374                 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1375                         continue;
1376
1377                 /* Try to find a SATA device */
1378                 list_for_each_entry(device, &sas_port->dev_list,
1379                                     dev_list_node) {
1380                         if (dev_is_sata(device)) {
1381                                 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1382                                                                  sas_port,
1383                                                                  device);
1384                                 break;
1385                         }
1386                 }
1387         }
1388 }
1389
1390 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1391 {
1392         struct Scsi_Host *shost = hisi_hba->shost;
1393
1394         down(&hisi_hba->sem);
1395         hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1396
1397         scsi_block_requests(shost);
1398         hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1399
1400         if (timer_pending(&hisi_hba->timer))
1401                 del_timer_sync(&hisi_hba->timer);
1402
1403         set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1404 }
1405 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1406
1407 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1408 {
1409         struct Scsi_Host *shost = hisi_hba->shost;
1410         u32 state;
1411
1412         /* Init and wait for PHYs to come up and all libsas event finished. */
1413         hisi_hba->hw->phys_init(hisi_hba);
1414         msleep(1000);
1415         hisi_sas_refresh_port_id(hisi_hba);
1416         clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1417         up(&hisi_hba->sem);
1418
1419         if (hisi_hba->reject_stp_links_msk)
1420                 hisi_sas_terminate_stp_reject(hisi_hba);
1421         hisi_sas_reset_init_all_devices(hisi_hba);
1422         scsi_unblock_requests(shost);
1423         clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1424
1425         state = hisi_hba->hw->get_phys_state(hisi_hba);
1426         hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state, state);
1427 }
1428 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1429
1430 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1431 {
1432         struct device *dev = hisi_hba->dev;
1433         struct Scsi_Host *shost = hisi_hba->shost;
1434         int rc;
1435
1436         if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
1437                 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1438
1439         if (!hisi_hba->hw->soft_reset)
1440                 return -1;
1441
1442         if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1443                 return -1;
1444
1445         dev_info(dev, "controller resetting...\n");
1446         hisi_sas_controller_reset_prepare(hisi_hba);
1447
1448         rc = hisi_hba->hw->soft_reset(hisi_hba);
1449         if (rc) {
1450                 dev_warn(dev, "controller reset failed (%d)\n", rc);
1451                 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1452                 up(&hisi_hba->sem);
1453                 scsi_unblock_requests(shost);
1454                 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1455                 return rc;
1456         }
1457
1458         hisi_sas_controller_reset_done(hisi_hba);
1459         dev_info(dev, "controller reset complete\n");
1460
1461         return 0;
1462 }
1463
1464 static int hisi_sas_abort_task(struct sas_task *task)
1465 {
1466         struct scsi_lun lun;
1467         struct hisi_sas_tmf_task tmf_task;
1468         struct domain_device *device = task->dev;
1469         struct hisi_sas_device *sas_dev = device->lldd_dev;
1470         struct hisi_hba *hisi_hba;
1471         struct device *dev;
1472         int rc = TMF_RESP_FUNC_FAILED;
1473         unsigned long flags;
1474
1475         if (!sas_dev)
1476                 return TMF_RESP_FUNC_FAILED;
1477
1478         hisi_hba = dev_to_hisi_hba(task->dev);
1479         dev = hisi_hba->dev;
1480
1481         spin_lock_irqsave(&task->task_state_lock, flags);
1482         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1483                 struct hisi_sas_slot *slot = task->lldd_task;
1484                 struct hisi_sas_cq *cq;
1485
1486                 if (slot) {
1487                         /*
1488                          * flush tasklet to avoid free'ing task
1489                          * before using task in IO completion
1490                          */
1491                         cq = &hisi_hba->cq[slot->dlvry_queue];
1492                         tasklet_kill(&cq->tasklet);
1493                 }
1494                 spin_unlock_irqrestore(&task->task_state_lock, flags);
1495                 rc = TMF_RESP_FUNC_COMPLETE;
1496                 goto out;
1497         }
1498         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1499         spin_unlock_irqrestore(&task->task_state_lock, flags);
1500
1501         if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1502                 struct scsi_cmnd *cmnd = task->uldd_task;
1503                 struct hisi_sas_slot *slot = task->lldd_task;
1504                 u16 tag = slot->idx;
1505                 int rc2;
1506
1507                 int_to_scsilun(cmnd->device->lun, &lun);
1508                 tmf_task.tmf = TMF_ABORT_TASK;
1509                 tmf_task.tag_of_task_to_be_managed = tag;
1510
1511                 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1512                                                   &tmf_task);
1513
1514                 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1515                                                    HISI_SAS_INT_ABT_CMD, tag);
1516                 if (rc2 < 0) {
1517                         dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1518                         return TMF_RESP_FUNC_FAILED;
1519                 }
1520
1521                 /*
1522                  * If the TMF finds that the IO is not in the device and also
1523                  * the internal abort does not succeed, then it is safe to
1524                  * free the slot.
1525                  * Note: if the internal abort succeeds then the slot
1526                  * will have already been completed
1527                  */
1528                 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1529                         if (task->lldd_task)
1530                                 hisi_sas_do_release_task(hisi_hba, task, slot);
1531                 }
1532         } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1533                 task->task_proto & SAS_PROTOCOL_STP) {
1534                 if (task->dev->dev_type == SAS_SATA_DEV) {
1535                         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1536                                                 HISI_SAS_INT_ABT_DEV, 0);
1537                         if (rc < 0) {
1538                                 dev_err(dev, "abort task: internal abort failed\n");
1539                                 goto out;
1540                         }
1541                         hisi_sas_dereg_device(hisi_hba, device);
1542                         rc = hisi_sas_softreset_ata_disk(device);
1543                 }
1544         } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1545                 /* SMP */
1546                 struct hisi_sas_slot *slot = task->lldd_task;
1547                 u32 tag = slot->idx;
1548                 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1549
1550                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1551                              HISI_SAS_INT_ABT_CMD, tag);
1552                 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1553                                         task->lldd_task) {
1554                         /*
1555                          * flush tasklet to avoid free'ing task
1556                          * before using task in IO completion
1557                          */
1558                         tasklet_kill(&cq->tasklet);
1559                         slot->task = NULL;
1560                 }
1561         }
1562
1563 out:
1564         if (rc != TMF_RESP_FUNC_COMPLETE)
1565                 dev_notice(dev, "abort task: rc=%d\n", rc);
1566         return rc;
1567 }
1568
1569 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1570 {
1571         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1572         struct device *dev = hisi_hba->dev;
1573         struct hisi_sas_tmf_task tmf_task;
1574         int rc = TMF_RESP_FUNC_FAILED;
1575
1576         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1577                                         HISI_SAS_INT_ABT_DEV, 0);
1578         if (rc < 0) {
1579                 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1580                 return TMF_RESP_FUNC_FAILED;
1581         }
1582         hisi_sas_dereg_device(hisi_hba, device);
1583
1584         tmf_task.tmf = TMF_ABORT_TASK_SET;
1585         rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1586
1587         if (rc == TMF_RESP_FUNC_COMPLETE)
1588                 hisi_sas_release_task(hisi_hba, device);
1589
1590         return rc;
1591 }
1592
1593 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1594 {
1595         int rc = TMF_RESP_FUNC_FAILED;
1596         struct hisi_sas_tmf_task tmf_task;
1597
1598         tmf_task.tmf = TMF_CLEAR_ACA;
1599         rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1600
1601         return rc;
1602 }
1603
1604 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1605 {
1606         struct sas_phy *local_phy = sas_get_local_phy(device);
1607         int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1608                         (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1609         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1610         struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1611         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
1612         struct hisi_sas_phy *phy = container_of(sas_phy,
1613                         struct hisi_sas_phy, sas_phy);
1614         DECLARE_COMPLETION_ONSTACK(phyreset);
1615
1616         if (scsi_is_sas_phy_local(local_phy)) {
1617                 phy->in_reset = 1;
1618                 phy->reset_completion = &phyreset;
1619         }
1620
1621         rc = sas_phy_reset(local_phy, reset_type);
1622         sas_put_local_phy(local_phy);
1623
1624         if (scsi_is_sas_phy_local(local_phy)) {
1625                 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1626                 unsigned long flags;
1627
1628                 spin_lock_irqsave(&phy->lock, flags);
1629                 phy->reset_completion = NULL;
1630                 phy->in_reset = 0;
1631                 spin_unlock_irqrestore(&phy->lock, flags);
1632
1633                 /* report PHY down if timed out */
1634                 if (!ret)
1635                         hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1636         } else
1637                 msleep(2000);
1638
1639         return rc;
1640 }
1641
1642 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1643 {
1644         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1645         struct device *dev = hisi_hba->dev;
1646         int rc = TMF_RESP_FUNC_FAILED;
1647
1648         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1649                                         HISI_SAS_INT_ABT_DEV, 0);
1650         if (rc < 0) {
1651                 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1652                 return TMF_RESP_FUNC_FAILED;
1653         }
1654         hisi_sas_dereg_device(hisi_hba, device);
1655
1656         rc = hisi_sas_debug_I_T_nexus_reset(device);
1657
1658         if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1659                 hisi_sas_release_task(hisi_hba, device);
1660
1661         return rc;
1662 }
1663
1664 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1665 {
1666         struct hisi_sas_device *sas_dev = device->lldd_dev;
1667         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1668         struct device *dev = hisi_hba->dev;
1669         int rc = TMF_RESP_FUNC_FAILED;
1670
1671         if (dev_is_sata(device)) {
1672                 struct sas_phy *phy;
1673
1674                 /* Clear internal IO and then hardreset */
1675                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1676                                                   HISI_SAS_INT_ABT_DEV, 0);
1677                 if (rc < 0) {
1678                         dev_err(dev, "lu_reset: internal abort failed\n");
1679                         goto out;
1680                 }
1681                 hisi_sas_dereg_device(hisi_hba, device);
1682
1683                 phy = sas_get_local_phy(device);
1684
1685                 rc = sas_phy_reset(phy, 1);
1686
1687                 if (rc == 0)
1688                         hisi_sas_release_task(hisi_hba, device);
1689                 sas_put_local_phy(phy);
1690         } else {
1691                 struct hisi_sas_tmf_task tmf_task = { .tmf =  TMF_LU_RESET };
1692
1693                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1694                                                 HISI_SAS_INT_ABT_DEV, 0);
1695                 if (rc < 0) {
1696                         dev_err(dev, "lu_reset: internal abort failed\n");
1697                         goto out;
1698                 }
1699                 hisi_sas_dereg_device(hisi_hba, device);
1700
1701                 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1702                 if (rc == TMF_RESP_FUNC_COMPLETE)
1703                         hisi_sas_release_task(hisi_hba, device);
1704         }
1705 out:
1706         if (rc != TMF_RESP_FUNC_COMPLETE)
1707                 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1708                              sas_dev->device_id, rc);
1709         return rc;
1710 }
1711
1712 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1713 {
1714         struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1715         struct device *dev = hisi_hba->dev;
1716         HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1717         int rc, i;
1718
1719         queue_work(hisi_hba->wq, &r.work);
1720         wait_for_completion(r.completion);
1721         if (!r.done)
1722                 return TMF_RESP_FUNC_FAILED;
1723
1724         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1725                 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1726                 struct domain_device *device = sas_dev->sas_device;
1727
1728                 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1729                     DEV_IS_EXPANDER(device->dev_type))
1730                         continue;
1731
1732                 rc = hisi_sas_debug_I_T_nexus_reset(device);
1733                 if (rc != TMF_RESP_FUNC_COMPLETE)
1734                         dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1735                                  sas_dev->device_id, rc);
1736         }
1737
1738         hisi_sas_release_tasks(hisi_hba);
1739
1740         return TMF_RESP_FUNC_COMPLETE;
1741 }
1742
1743 static int hisi_sas_query_task(struct sas_task *task)
1744 {
1745         struct scsi_lun lun;
1746         struct hisi_sas_tmf_task tmf_task;
1747         int rc = TMF_RESP_FUNC_FAILED;
1748
1749         if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1750                 struct scsi_cmnd *cmnd = task->uldd_task;
1751                 struct domain_device *device = task->dev;
1752                 struct hisi_sas_slot *slot = task->lldd_task;
1753                 u32 tag = slot->idx;
1754
1755                 int_to_scsilun(cmnd->device->lun, &lun);
1756                 tmf_task.tmf = TMF_QUERY_TASK;
1757                 tmf_task.tag_of_task_to_be_managed = tag;
1758
1759                 rc = hisi_sas_debug_issue_ssp_tmf(device,
1760                                                   lun.scsi_lun,
1761                                                   &tmf_task);
1762                 switch (rc) {
1763                 /* The task is still in Lun, release it then */
1764                 case TMF_RESP_FUNC_SUCC:
1765                 /* The task is not in Lun or failed, reset the phy */
1766                 case TMF_RESP_FUNC_FAILED:
1767                 case TMF_RESP_FUNC_COMPLETE:
1768                         break;
1769                 default:
1770                         rc = TMF_RESP_FUNC_FAILED;
1771                         break;
1772                 }
1773         }
1774         return rc;
1775 }
1776
1777 static int
1778 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1779                                   struct sas_task *task, int abort_flag,
1780                                   int task_tag)
1781 {
1782         struct domain_device *device = task->dev;
1783         struct hisi_sas_device *sas_dev = device->lldd_dev;
1784         struct device *dev = hisi_hba->dev;
1785         struct hisi_sas_port *port;
1786         struct hisi_sas_slot *slot;
1787         struct asd_sas_port *sas_port = device->port;
1788         struct hisi_sas_cmd_hdr *cmd_hdr_base;
1789         struct hisi_sas_dq *dq = sas_dev->dq;
1790         int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1791         unsigned long flags, flags_dq = 0;
1792         int wr_q_index;
1793
1794         if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1795                 return -EINVAL;
1796
1797         if (!device->port)
1798                 return -1;
1799
1800         port = to_hisi_sas_port(sas_port);
1801
1802         /* simply get a slot and send abort command */
1803         rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1804         if (rc < 0)
1805                 goto err_out;
1806
1807         slot_idx = rc;
1808         slot = &hisi_hba->slot_info[slot_idx];
1809
1810         spin_lock_irqsave(&dq->lock, flags_dq);
1811         wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1812         if (wr_q_index < 0) {
1813                 spin_unlock_irqrestore(&dq->lock, flags_dq);
1814                 rc = -EAGAIN;
1815                 goto err_out_tag;
1816         }
1817         list_add_tail(&slot->delivery, &dq->list);
1818         spin_unlock_irqrestore(&dq->lock, flags_dq);
1819
1820         dlvry_queue = dq->id;
1821         dlvry_queue_slot = wr_q_index;
1822
1823         slot->n_elem = n_elem;
1824         slot->dlvry_queue = dlvry_queue;
1825         slot->dlvry_queue_slot = dlvry_queue_slot;
1826         cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1827         slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1828         slot->task = task;
1829         slot->port = port;
1830         slot->is_internal = true;
1831         task->lldd_task = slot;
1832
1833         memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1834         memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1835         memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1836
1837         hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1838                                       abort_flag, task_tag);
1839
1840         spin_lock_irqsave(&task->task_state_lock, flags);
1841         task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1842         spin_unlock_irqrestore(&task->task_state_lock, flags);
1843         WRITE_ONCE(slot->ready, 1);
1844         /* send abort command to the chip */
1845         spin_lock_irqsave(&dq->lock, flags);
1846         list_add_tail(&slot->entry, &sas_dev->list);
1847         hisi_hba->hw->start_delivery(dq);
1848         spin_unlock_irqrestore(&dq->lock, flags);
1849
1850         return 0;
1851
1852 err_out_tag:
1853         hisi_sas_slot_index_free(hisi_hba, slot_idx);
1854 err_out:
1855         dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1856
1857         return rc;
1858 }
1859
1860 /**
1861  * hisi_sas_internal_task_abort -- execute an internal
1862  * abort command for single IO command or a device
1863  * @hisi_hba: host controller struct
1864  * @device: domain device
1865  * @abort_flag: mode of operation, device or single IO
1866  * @tag: tag of IO to be aborted (only relevant to single
1867  *       IO mode)
1868  */
1869 static int
1870 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1871                              struct domain_device *device,
1872                              int abort_flag, int tag)
1873 {
1874         struct sas_task *task;
1875         struct hisi_sas_device *sas_dev = device->lldd_dev;
1876         struct device *dev = hisi_hba->dev;
1877         int res;
1878
1879         /*
1880          * The interface is not realized means this HW don't support internal
1881          * abort, or don't need to do internal abort. Then here, we return
1882          * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1883          * the internal abort has been executed and returned CQ.
1884          */
1885         if (!hisi_hba->hw->prep_abort)
1886                 return TMF_RESP_FUNC_FAILED;
1887
1888         task = sas_alloc_slow_task(GFP_KERNEL);
1889         if (!task)
1890                 return -ENOMEM;
1891
1892         task->dev = device;
1893         task->task_proto = device->tproto;
1894         task->task_done = hisi_sas_task_done;
1895         task->slow_task->timer.function = hisi_sas_tmf_timedout;
1896         task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1897         add_timer(&task->slow_task->timer);
1898
1899         res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1900                                                 task, abort_flag, tag);
1901         if (res) {
1902                 del_timer(&task->slow_task->timer);
1903                 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1904                         res);
1905                 goto exit;
1906         }
1907         wait_for_completion(&task->slow_task->completion);
1908         res = TMF_RESP_FUNC_FAILED;
1909
1910         /* Internal abort timed out */
1911         if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1912                 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1913                         struct hisi_sas_slot *slot = task->lldd_task;
1914
1915                         if (slot) {
1916                                 struct hisi_sas_cq *cq =
1917                                         &hisi_hba->cq[slot->dlvry_queue];
1918                                 /*
1919                                  * flush tasklet to avoid free'ing task
1920                                  * before using task in IO completion
1921                                  */
1922                                 tasklet_kill(&cq->tasklet);
1923                                 slot->task = NULL;
1924                         }
1925                         dev_err(dev, "internal task abort: timeout and not done.\n");
1926
1927                         res = -EIO;
1928                         goto exit;
1929                 } else
1930                         dev_err(dev, "internal task abort: timeout.\n");
1931         }
1932
1933         if (task->task_status.resp == SAS_TASK_COMPLETE &&
1934                 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1935                 res = TMF_RESP_FUNC_COMPLETE;
1936                 goto exit;
1937         }
1938
1939         if (task->task_status.resp == SAS_TASK_COMPLETE &&
1940                 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1941                 res = TMF_RESP_FUNC_SUCC;
1942                 goto exit;
1943         }
1944
1945 exit:
1946         dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1947                 "resp: 0x%x sts 0x%x\n",
1948                 SAS_ADDR(device->sas_addr),
1949                 task,
1950                 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1951                 task->task_status.stat);
1952         sas_free_task(task);
1953
1954         return res;
1955 }
1956
1957 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1958 {
1959         hisi_sas_port_notify_formed(sas_phy);
1960 }
1961
1962 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1963                         u8 reg_index, u8 reg_count, u8 *write_data)
1964 {
1965         struct hisi_hba *hisi_hba = sha->lldd_ha;
1966
1967         if (!hisi_hba->hw->write_gpio)
1968                 return -EOPNOTSUPP;
1969
1970         return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1971                                 reg_index, reg_count, write_data);
1972 }
1973
1974 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1975 {
1976         phy->phy_attached = 0;
1977         phy->phy_type = 0;
1978         phy->port = NULL;
1979 }
1980
1981 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1982 {
1983         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1984         struct asd_sas_phy *sas_phy = &phy->sas_phy;
1985         struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1986         struct device *dev = hisi_hba->dev;
1987
1988         if (rdy) {
1989                 /* Phy down but ready */
1990                 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1991                 hisi_sas_port_notify_formed(sas_phy);
1992         } else {
1993                 struct hisi_sas_port *port  = phy->port;
1994
1995                 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
1996                     phy->in_reset) {
1997                         dev_info(dev, "ignore flutter phy%d down\n", phy_no);
1998                         return;
1999                 }
2000                 /* Phy down and not ready */
2001                 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2002                 sas_phy_disconnected(sas_phy);
2003
2004                 if (port) {
2005                         if (phy->phy_type & PORT_TYPE_SAS) {
2006                                 int port_id = port->id;
2007
2008                                 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2009                                                                        port_id))
2010                                         port->port_attached = 0;
2011                         } else if (phy->phy_type & PORT_TYPE_SATA)
2012                                 port->port_attached = 0;
2013                 }
2014                 hisi_sas_phy_disconnected(phy);
2015         }
2016 }
2017 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2018
2019 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
2020 {
2021         int i;
2022
2023         for (i = 0; i < hisi_hba->queue_count; i++) {
2024                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2025
2026                 tasklet_kill(&cq->tasklet);
2027         }
2028 }
2029 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2030
2031 struct scsi_transport_template *hisi_sas_stt;
2032 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2033
2034 static struct sas_domain_function_template hisi_sas_transport_ops = {
2035         .lldd_dev_found         = hisi_sas_dev_found,
2036         .lldd_dev_gone          = hisi_sas_dev_gone,
2037         .lldd_execute_task      = hisi_sas_queue_command,
2038         .lldd_control_phy       = hisi_sas_control_phy,
2039         .lldd_abort_task        = hisi_sas_abort_task,
2040         .lldd_abort_task_set    = hisi_sas_abort_task_set,
2041         .lldd_clear_aca         = hisi_sas_clear_aca,
2042         .lldd_I_T_nexus_reset   = hisi_sas_I_T_nexus_reset,
2043         .lldd_lu_reset          = hisi_sas_lu_reset,
2044         .lldd_query_task        = hisi_sas_query_task,
2045         .lldd_clear_nexus_ha    = hisi_sas_clear_nexus_ha,
2046         .lldd_port_formed       = hisi_sas_port_formed,
2047         .lldd_write_gpio        = hisi_sas_write_gpio,
2048 };
2049
2050 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2051 {
2052         int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2053
2054         for (i = 0; i < hisi_hba->queue_count; i++) {
2055                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2056                 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2057
2058                 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2059                 memset(hisi_hba->cmd_hdr[i], 0, s);
2060                 dq->wr_point = 0;
2061
2062                 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2063                 memset(hisi_hba->complete_hdr[i], 0, s);
2064                 cq->rd_point = 0;
2065         }
2066
2067         s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2068         memset(hisi_hba->initial_fis, 0, s);
2069
2070         s = max_command_entries * sizeof(struct hisi_sas_iost);
2071         memset(hisi_hba->iost, 0, s);
2072
2073         s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2074         memset(hisi_hba->breakpoint, 0, s);
2075
2076         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2077         memset(hisi_hba->sata_breakpoint, 0, s);
2078 }
2079 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2080
2081 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2082 {
2083         struct device *dev = hisi_hba->dev;
2084         int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
2085         int max_command_entries_ru, sz_slot_buf_ru;
2086         int blk_cnt, slots_per_blk;
2087
2088         sema_init(&hisi_hba->sem, 1);
2089         spin_lock_init(&hisi_hba->lock);
2090         for (i = 0; i < hisi_hba->n_phy; i++) {
2091                 hisi_sas_phy_init(hisi_hba, i);
2092                 hisi_hba->port[i].port_attached = 0;
2093                 hisi_hba->port[i].id = -1;
2094         }
2095
2096         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2097                 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2098                 hisi_hba->devices[i].device_id = i;
2099         }
2100
2101         for (i = 0; i < hisi_hba->queue_count; i++) {
2102                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2103                 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2104
2105                 /* Completion queue structure */
2106                 cq->id = i;
2107                 cq->hisi_hba = hisi_hba;
2108
2109                 /* Delivery queue structure */
2110                 spin_lock_init(&dq->lock);
2111                 INIT_LIST_HEAD(&dq->list);
2112                 dq->id = i;
2113                 dq->hisi_hba = hisi_hba;
2114
2115                 /* Delivery queue */
2116                 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2117                 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2118                                                 &hisi_hba->cmd_hdr_dma[i],
2119                                                 GFP_KERNEL);
2120                 if (!hisi_hba->cmd_hdr[i])
2121                         goto err_out;
2122
2123                 /* Completion queue */
2124                 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2125                 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2126                                                 &hisi_hba->complete_hdr_dma[i],
2127                                                 GFP_KERNEL);
2128                 if (!hisi_hba->complete_hdr[i])
2129                         goto err_out;
2130         }
2131
2132         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2133         hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2134                                              GFP_KERNEL);
2135         if (!hisi_hba->itct)
2136                 goto err_out;
2137         memset(hisi_hba->itct, 0, s);
2138
2139         hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2140                                            sizeof(struct hisi_sas_slot),
2141                                            GFP_KERNEL);
2142         if (!hisi_hba->slot_info)
2143                 goto err_out;
2144
2145         /* roundup to avoid overly large block size */
2146         max_command_entries_ru = roundup(max_command_entries, 64);
2147         sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
2148         s = lcm(max_command_entries_ru, sz_slot_buf_ru);
2149         blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2150         slots_per_blk = s / sz_slot_buf_ru;
2151         for (i = 0; i < blk_cnt; i++) {
2152                 struct hisi_sas_slot_buf_table *buf;
2153                 dma_addr_t buf_dma;
2154                 int slot_index = i * slots_per_blk;
2155
2156                 buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
2157                 if (!buf)
2158                         goto err_out;
2159                 memset(buf, 0, s);
2160
2161                 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2162                         struct hisi_sas_slot *slot;
2163
2164                         slot = &hisi_hba->slot_info[slot_index];
2165                         slot->buf = buf;
2166                         slot->buf_dma = buf_dma;
2167                         slot->idx = slot_index;
2168
2169                         buf++;
2170                         buf_dma += sizeof(*buf);
2171                 }
2172         }
2173
2174         s = max_command_entries * sizeof(struct hisi_sas_iost);
2175         hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2176                                              GFP_KERNEL);
2177         if (!hisi_hba->iost)
2178                 goto err_out;
2179
2180         s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2181         hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2182                                                    &hisi_hba->breakpoint_dma,
2183                                                    GFP_KERNEL);
2184         if (!hisi_hba->breakpoint)
2185                 goto err_out;
2186
2187         hisi_hba->slot_index_count = max_command_entries;
2188         s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2189         hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2190         if (!hisi_hba->slot_index_tags)
2191                 goto err_out;
2192
2193         s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2194         hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2195                                                     &hisi_hba->initial_fis_dma,
2196                                                     GFP_KERNEL);
2197         if (!hisi_hba->initial_fis)
2198                 goto err_out;
2199
2200         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2201         hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2202                                         &hisi_hba->sata_breakpoint_dma,
2203                                         GFP_KERNEL);
2204         if (!hisi_hba->sata_breakpoint)
2205                 goto err_out;
2206         hisi_sas_init_mem(hisi_hba);
2207
2208         hisi_sas_slot_index_init(hisi_hba);
2209         hisi_hba->last_slot_index = hisi_hba->hw->max_command_entries -
2210                 HISI_SAS_RESERVED_IPTT_CNT;
2211
2212         hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2213         if (!hisi_hba->wq) {
2214                 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2215                 goto err_out;
2216         }
2217
2218         return 0;
2219 err_out:
2220         return -ENOMEM;
2221 }
2222 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2223
2224 void hisi_sas_free(struct hisi_hba *hisi_hba)
2225 {
2226         if (hisi_hba->wq)
2227                 destroy_workqueue(hisi_hba->wq);
2228 }
2229 EXPORT_SYMBOL_GPL(hisi_sas_free);
2230
2231 void hisi_sas_rst_work_handler(struct work_struct *work)
2232 {
2233         struct hisi_hba *hisi_hba =
2234                 container_of(work, struct hisi_hba, rst_work);
2235
2236         hisi_sas_controller_reset(hisi_hba);
2237 }
2238 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2239
2240 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2241 {
2242         struct hisi_sas_rst *rst =
2243                 container_of(work, struct hisi_sas_rst, work);
2244
2245         if (!hisi_sas_controller_reset(rst->hisi_hba))
2246                 rst->done = true;
2247         complete(rst->completion);
2248 }
2249 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2250
2251 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2252 {
2253         struct device *dev = hisi_hba->dev;
2254         struct platform_device *pdev = hisi_hba->platform_dev;
2255         struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2256         struct clk *refclk;
2257
2258         if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2259                                           SAS_ADDR_SIZE)) {
2260                 dev_err(dev, "could not get property sas-addr\n");
2261                 return -ENOENT;
2262         }
2263
2264         if (np) {
2265                 /*
2266                  * These properties are only required for platform device-based
2267                  * controller with DT firmware.
2268                  */
2269                 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2270                                         "hisilicon,sas-syscon");
2271                 if (IS_ERR(hisi_hba->ctrl)) {
2272                         dev_err(dev, "could not get syscon\n");
2273                         return -ENOENT;
2274                 }
2275
2276                 if (device_property_read_u32(dev, "ctrl-reset-reg",
2277                                              &hisi_hba->ctrl_reset_reg)) {
2278                         dev_err(dev,
2279                                 "could not get property ctrl-reset-reg\n");
2280                         return -ENOENT;
2281                 }
2282
2283                 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2284                                              &hisi_hba->ctrl_reset_sts_reg)) {
2285                         dev_err(dev,
2286                                 "could not get property ctrl-reset-sts-reg\n");
2287                         return -ENOENT;
2288                 }
2289
2290                 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2291                                              &hisi_hba->ctrl_clock_ena_reg)) {
2292                         dev_err(dev,
2293                                 "could not get property ctrl-clock-ena-reg\n");
2294                         return -ENOENT;
2295                 }
2296         }
2297
2298         refclk = devm_clk_get(dev, NULL);
2299         if (IS_ERR(refclk))
2300                 dev_dbg(dev, "no ref clk property\n");
2301         else
2302                 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2303
2304         if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2305                 dev_err(dev, "could not get property phy-count\n");
2306                 return -ENOENT;
2307         }
2308
2309         if (device_property_read_u32(dev, "queue-count",
2310                                      &hisi_hba->queue_count)) {
2311                 dev_err(dev, "could not get property queue-count\n");
2312                 return -ENOENT;
2313         }
2314
2315         return 0;
2316 }
2317 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2318
2319 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2320                                               const struct hisi_sas_hw *hw)
2321 {
2322         struct resource *res;
2323         struct Scsi_Host *shost;
2324         struct hisi_hba *hisi_hba;
2325         struct device *dev = &pdev->dev;
2326
2327         shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2328         if (!shost) {
2329                 dev_err(dev, "scsi host alloc failed\n");
2330                 return NULL;
2331         }
2332         hisi_hba = shost_priv(shost);
2333
2334         INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2335         hisi_hba->hw = hw;
2336         hisi_hba->dev = dev;
2337         hisi_hba->platform_dev = pdev;
2338         hisi_hba->shost = shost;
2339         SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2340
2341         timer_setup(&hisi_hba->timer, NULL, 0);
2342
2343         if (hisi_sas_get_fw_info(hisi_hba) < 0)
2344                 goto err_out;
2345
2346         if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2347             dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2348                 dev_err(dev, "No usable DMA addressing method\n");
2349                 goto err_out;
2350         }
2351
2352         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2353         hisi_hba->regs = devm_ioremap_resource(dev, res);
2354         if (IS_ERR(hisi_hba->regs))
2355                 goto err_out;
2356
2357         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2358         if (res) {
2359                 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2360                 if (IS_ERR(hisi_hba->sgpio_regs))
2361                         goto err_out;
2362         }
2363
2364         if (hisi_sas_alloc(hisi_hba)) {
2365                 hisi_sas_free(hisi_hba);
2366                 goto err_out;
2367         }
2368
2369         return shost;
2370 err_out:
2371         scsi_host_put(shost);
2372         dev_err(dev, "shost alloc failed\n");
2373         return NULL;
2374 }
2375
2376 int hisi_sas_probe(struct platform_device *pdev,
2377                    const struct hisi_sas_hw *hw)
2378 {
2379         struct Scsi_Host *shost;
2380         struct hisi_hba *hisi_hba;
2381         struct device *dev = &pdev->dev;
2382         struct asd_sas_phy **arr_phy;
2383         struct asd_sas_port **arr_port;
2384         struct sas_ha_struct *sha;
2385         int rc, phy_nr, port_nr, i;
2386
2387         shost = hisi_sas_shost_alloc(pdev, hw);
2388         if (!shost)
2389                 return -ENOMEM;
2390
2391         sha = SHOST_TO_SAS_HA(shost);
2392         hisi_hba = shost_priv(shost);
2393         platform_set_drvdata(pdev, sha);
2394
2395         phy_nr = port_nr = hisi_hba->n_phy;
2396
2397         arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2398         arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2399         if (!arr_phy || !arr_port) {
2400                 rc = -ENOMEM;
2401                 goto err_out_ha;
2402         }
2403
2404         sha->sas_phy = arr_phy;
2405         sha->sas_port = arr_port;
2406         sha->lldd_ha = hisi_hba;
2407
2408         shost->transportt = hisi_sas_stt;
2409         shost->max_id = HISI_SAS_MAX_DEVICES;
2410         shost->max_lun = ~0;
2411         shost->max_channel = 1;
2412         shost->max_cmd_len = 16;
2413         if (hisi_hba->hw->slot_index_alloc) {
2414                 shost->can_queue = hisi_hba->hw->max_command_entries;
2415                 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2416         } else {
2417                 shost->can_queue = hisi_hba->hw->max_command_entries -
2418                         HISI_SAS_RESERVED_IPTT_CNT;
2419                 shost->cmd_per_lun = hisi_hba->hw->max_command_entries -
2420                         HISI_SAS_RESERVED_IPTT_CNT;
2421         }
2422
2423         sha->sas_ha_name = DRV_NAME;
2424         sha->dev = hisi_hba->dev;
2425         sha->lldd_module = THIS_MODULE;
2426         sha->sas_addr = &hisi_hba->sas_addr[0];
2427         sha->num_phys = hisi_hba->n_phy;
2428         sha->core.shost = hisi_hba->shost;
2429
2430         for (i = 0; i < hisi_hba->n_phy; i++) {
2431                 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2432                 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2433         }
2434
2435         rc = scsi_add_host(shost, &pdev->dev);
2436         if (rc)
2437                 goto err_out_ha;
2438
2439         rc = sas_register_ha(sha);
2440         if (rc)
2441                 goto err_out_register_ha;
2442
2443         rc = hisi_hba->hw->hw_init(hisi_hba);
2444         if (rc)
2445                 goto err_out_register_ha;
2446
2447         scsi_scan_host(shost);
2448
2449         return 0;
2450
2451 err_out_register_ha:
2452         scsi_remove_host(shost);
2453 err_out_ha:
2454         hisi_sas_free(hisi_hba);
2455         scsi_host_put(shost);
2456         return rc;
2457 }
2458 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2459
2460 struct dentry *hisi_sas_debugfs_dir;
2461
2462 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2463 {
2464         int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2465         int i;
2466
2467         for (i = 0; i < hisi_hba->queue_count; i++)
2468                 memcpy(hisi_hba->debugfs_complete_hdr[i],
2469                        hisi_hba->complete_hdr[i],
2470                        HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2471 }
2472
2473 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2474 {
2475         int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2476         int i;
2477
2478         for (i = 0; i < hisi_hba->queue_count; i++)
2479                 memcpy(hisi_hba->debugfs_cmd_hdr[i],
2480                        hisi_hba->cmd_hdr[i],
2481                        HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2482 }
2483
2484 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2485 {
2486         const struct hisi_sas_debugfs_reg *port =
2487                 hisi_hba->hw->debugfs_reg_port;
2488         int i, phy_cnt;
2489         u32 offset;
2490         u32 *databuf;
2491
2492         for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2493                 databuf = (u32 *)hisi_hba->debugfs_port_reg[phy_cnt];
2494                 for (i = 0; i < port->count; i++, databuf++) {
2495                         offset = port->base_off + 4 * i;
2496                         *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2497                                                        offset);
2498                 }
2499         }
2500 }
2501
2502 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2503 {
2504         u32 *databuf = (u32 *)hisi_hba->debugfs_global_reg;
2505         const struct hisi_sas_debugfs_reg *global =
2506                 hisi_hba->hw->debugfs_reg_global;
2507         int i;
2508
2509         for (i = 0; i < global->count; i++, databuf++)
2510                 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2511 }
2512
2513 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2514 {
2515         void *databuf = hisi_hba->debugfs_itct;
2516         struct hisi_sas_itct *itct;
2517         int i;
2518
2519         itct = hisi_hba->itct;
2520
2521         for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2522                 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2523                 databuf += sizeof(struct hisi_sas_itct);
2524         }
2525 }
2526
2527 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2528 {
2529         int max_command_entries = hisi_hba->hw->max_command_entries;
2530         void *databuf = hisi_hba->debugfs_iost;
2531         struct hisi_sas_iost *iost;
2532         int i;
2533
2534         iost = hisi_hba->iost;
2535
2536         for (i = 0; i < max_command_entries; i++, iost++) {
2537                 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2538                 databuf += sizeof(struct hisi_sas_iost);
2539         }
2540 }
2541
2542 static const char *
2543 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2544                              const struct hisi_sas_debugfs_reg_lu *lu)
2545 {
2546         for (; lu->name; lu++) {
2547                 if (off == lu->off - base_off)
2548                         return lu->name;
2549         }
2550
2551         return NULL;
2552 }
2553
2554 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2555                                        struct seq_file *s)
2556 {
2557         const struct hisi_sas_debugfs_reg *reg = ptr;
2558         int i;
2559
2560         for (i = 0; i < reg->count; i++) {
2561                 int off = i * 4;
2562                 const char *name;
2563
2564                 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2565                                                     reg->lu);
2566
2567                 if (name)
2568                         seq_printf(s, "0x%08x 0x%08x %s\n", off,
2569                                    regs_val[i], name);
2570                 else
2571                         seq_printf(s, "0x%08x 0x%08x\n", off,
2572                                    regs_val[i]);
2573         }
2574 }
2575
2576 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2577 {
2578         struct hisi_hba *hisi_hba = s->private;
2579         const struct hisi_sas_hw *hw = hisi_hba->hw;
2580         const struct hisi_sas_debugfs_reg *reg_global = hw->debugfs_reg_global;
2581
2582         hisi_sas_debugfs_print_reg(hisi_hba->debugfs_global_reg,
2583                                    reg_global, s);
2584
2585         return 0;
2586 }
2587
2588 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2589 {
2590         return single_open(filp, hisi_sas_debugfs_global_show,
2591                            inode->i_private);
2592 }
2593
2594 static const struct file_operations hisi_sas_debugfs_global_fops = {
2595         .open = hisi_sas_debugfs_global_open,
2596         .read = seq_read,
2597         .llseek = seq_lseek,
2598         .release = single_release,
2599         .owner = THIS_MODULE,
2600 };
2601
2602 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2603 {
2604         struct hisi_sas_phy *phy = s->private;
2605         struct hisi_hba *hisi_hba = phy->hisi_hba;
2606         const struct hisi_sas_hw *hw = hisi_hba->hw;
2607         const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2608         u32 *databuf = hisi_hba->debugfs_port_reg[phy->sas_phy.id];
2609
2610         hisi_sas_debugfs_print_reg(databuf, reg_port, s);
2611
2612         return 0;
2613 }
2614
2615 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2616 {
2617         return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2618 }
2619
2620 static const struct file_operations hisi_sas_debugfs_port_fops = {
2621         .open = hisi_sas_debugfs_port_open,
2622         .read = seq_read,
2623         .llseek = seq_lseek,
2624         .release = single_release,
2625         .owner = THIS_MODULE,
2626 };
2627
2628 static int hisi_sas_show_row_64(struct seq_file *s, int index,
2629                                 int sz, __le64 *ptr)
2630 {
2631         int i;
2632
2633         /* completion header size not fixed per HW version */
2634         seq_printf(s, "index %04d:\n\t", index);
2635         for (i = 1; i <= sz / 8; i++, ptr++) {
2636                 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2637                 if (!(i % 2))
2638                         seq_puts(s, "\n\t");
2639         }
2640
2641         seq_puts(s, "\n");
2642
2643         return 0;
2644 }
2645
2646 static int hisi_sas_show_row_32(struct seq_file *s, int index,
2647                                 int sz, __le32 *ptr)
2648 {
2649         int i;
2650
2651         /* completion header size not fixed per HW version */
2652         seq_printf(s, "index %04d:\n\t", index);
2653         for (i = 1; i <= sz / 4; i++, ptr++) {
2654                 if (!(i % 4))
2655                         seq_puts(s, "\n\t");
2656         }
2657         seq_puts(s, "\n");
2658
2659         return 0;
2660 }
2661
2662 static int hisi_sas_cq_show_slot(struct seq_file *s, int slot, void *cq_ptr)
2663 {
2664         struct hisi_sas_cq *cq = cq_ptr;
2665         struct hisi_hba *hisi_hba = cq->hisi_hba;
2666         void *complete_queue = hisi_hba->debugfs_complete_hdr[cq->id];
2667         __le32 *complete_hdr = complete_queue +
2668                         (hisi_hba->hw->complete_hdr_size * slot);
2669
2670         return hisi_sas_show_row_32(s, slot,
2671                                 hisi_hba->hw->complete_hdr_size,
2672                                 complete_hdr);
2673 }
2674
2675 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
2676 {
2677         struct hisi_sas_cq *cq = s->private;
2678         int slot, ret;
2679
2680         for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2681                 ret = hisi_sas_cq_show_slot(s, slot, cq);
2682                 if (ret)
2683                         return ret;
2684         }
2685         return 0;
2686 }
2687
2688 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
2689 {
2690         return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
2691 }
2692
2693 static const struct file_operations hisi_sas_debugfs_cq_fops = {
2694         .open = hisi_sas_debugfs_cq_open,
2695         .read = seq_read,
2696         .llseek = seq_lseek,
2697         .release = single_release,
2698         .owner = THIS_MODULE,
2699 };
2700
2701 static int hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
2702 {
2703         struct hisi_sas_dq *dq = dq_ptr;
2704         struct hisi_hba *hisi_hba = dq->hisi_hba;
2705         void *cmd_queue = hisi_hba->debugfs_cmd_hdr[dq->id];
2706         __le32 *cmd_hdr = cmd_queue +
2707                 sizeof(struct hisi_sas_cmd_hdr) * slot;
2708
2709         return hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr),
2710                                     cmd_hdr);
2711 }
2712
2713 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
2714 {
2715         int slot, ret;
2716
2717         for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2718                 ret = hisi_sas_dq_show_slot(s, slot, s->private);
2719                 if (ret)
2720                         return ret;
2721         }
2722         return 0;
2723 }
2724
2725 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
2726 {
2727         return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
2728 }
2729
2730 static const struct file_operations hisi_sas_debugfs_dq_fops = {
2731         .open = hisi_sas_debugfs_dq_open,
2732         .read = seq_read,
2733         .llseek = seq_lseek,
2734         .release = single_release,
2735         .owner = THIS_MODULE,
2736 };
2737
2738 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
2739 {
2740         struct hisi_hba *hisi_hba = s->private;
2741         struct hisi_sas_iost *debugfs_iost = hisi_hba->debugfs_iost;
2742         int i, ret, max_command_entries = hisi_hba->hw->max_command_entries;
2743         __le64 *iost = &debugfs_iost->qw0;
2744
2745         for (i = 0; i < max_command_entries; i++, debugfs_iost++) {
2746                 ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_iost),
2747                                            iost);
2748                 if (ret)
2749                         return ret;
2750         }
2751
2752         return 0;
2753 }
2754
2755 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
2756 {
2757         return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
2758 }
2759
2760 static const struct file_operations hisi_sas_debugfs_iost_fops = {
2761         .open = hisi_sas_debugfs_iost_open,
2762         .read = seq_read,
2763         .llseek = seq_lseek,
2764         .release = single_release,
2765         .owner = THIS_MODULE,
2766 };
2767
2768 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
2769 {
2770         int i, ret;
2771         struct hisi_hba *hisi_hba = s->private;
2772         struct hisi_sas_itct *debugfs_itct = hisi_hba->debugfs_itct;
2773         __le64 *itct = &debugfs_itct->qw0;
2774
2775         for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, debugfs_itct++) {
2776                 ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_itct),
2777                                            itct);
2778                 if (ret)
2779                         return ret;
2780         }
2781
2782         return 0;
2783 }
2784
2785 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
2786 {
2787         return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
2788 }
2789
2790 static const struct file_operations hisi_sas_debugfs_itct_fops = {
2791         .open = hisi_sas_debugfs_itct_open,
2792         .read = seq_read,
2793         .llseek = seq_lseek,
2794         .release = single_release,
2795         .owner = THIS_MODULE,
2796 };
2797
2798 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
2799 {
2800         struct dentry *dump_dentry;
2801         struct dentry *dentry;
2802         char name[256];
2803         int p;
2804         int c;
2805         int d;
2806
2807         /* Create dump dir inside device dir */
2808         dump_dentry = debugfs_create_dir("dump", hisi_hba->debugfs_dir);
2809         hisi_hba->debugfs_dump_dentry = dump_dentry;
2810
2811         debugfs_create_file("global", 0400, dump_dentry, hisi_hba,
2812                             &hisi_sas_debugfs_global_fops);
2813
2814         /* Create port dir and files */
2815         dentry = debugfs_create_dir("port", dump_dentry);
2816         for (p = 0; p < hisi_hba->n_phy; p++) {
2817                 snprintf(name, 256, "%d", p);
2818
2819                 debugfs_create_file(name, 0400, dentry, &hisi_hba->phy[p],
2820                                     &hisi_sas_debugfs_port_fops);
2821         }
2822
2823         /* Create CQ dir and files */
2824         dentry = debugfs_create_dir("cq", dump_dentry);
2825         for (c = 0; c < hisi_hba->queue_count; c++) {
2826                 snprintf(name, 256, "%d", c);
2827
2828                 debugfs_create_file(name, 0400, dentry, &hisi_hba->cq[c],
2829                                     &hisi_sas_debugfs_cq_fops);
2830         }
2831
2832         /* Create DQ dir and files */
2833         dentry = debugfs_create_dir("dq", dump_dentry);
2834         for (d = 0; d < hisi_hba->queue_count; d++) {
2835                 snprintf(name, 256, "%d", d);
2836
2837                 debugfs_create_file(name, 0400, dentry, &hisi_hba->dq[d],
2838                                     &hisi_sas_debugfs_dq_fops);
2839         }
2840
2841         debugfs_create_file("iost", 0400, dump_dentry, hisi_hba,
2842                             &hisi_sas_debugfs_iost_fops);
2843
2844         debugfs_create_file("itct", 0400, dump_dentry, hisi_hba,
2845                             &hisi_sas_debugfs_itct_fops);
2846
2847         return;
2848 }
2849
2850 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
2851 {
2852         hisi_hba->hw->snapshot_prepare(hisi_hba);
2853
2854         hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
2855         hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
2856         hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
2857         hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
2858         hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
2859         hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
2860
2861         hisi_sas_debugfs_create_files(hisi_hba);
2862
2863         hisi_hba->hw->snapshot_restore(hisi_hba);
2864 }
2865
2866 void hisi_sas_debugfs_work_handler(struct work_struct *work)
2867 {
2868         struct hisi_hba *hisi_hba =
2869                 container_of(work, struct hisi_hba, debugfs_work);
2870
2871         if (hisi_hba->debugfs_snapshot)
2872                 return;
2873         hisi_hba->debugfs_snapshot = true;
2874
2875         hisi_sas_debugfs_snapshot_regs(hisi_hba);
2876 }
2877 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
2878
2879 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
2880 {
2881         int max_command_entries = hisi_hba->hw->max_command_entries;
2882         struct device *dev = hisi_hba->dev;
2883         int p, i, c, d;
2884         size_t sz;
2885
2886         hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
2887                                                    hisi_sas_debugfs_dir);
2888
2889         /* Alloc buffer for global */
2890         sz = hisi_hba->hw->debugfs_reg_global->count * 4;
2891         hisi_hba->debugfs_global_reg =
2892                 devm_kmalloc(dev, sz, GFP_KERNEL);
2893
2894         if (!hisi_hba->debugfs_global_reg)
2895                 goto fail_global;
2896
2897         /* Alloc buffer for port */
2898         sz = hisi_hba->hw->debugfs_reg_port->count * 4;
2899         for (p = 0; p < hisi_hba->n_phy; p++) {
2900                 hisi_hba->debugfs_port_reg[p] =
2901                         devm_kmalloc(dev, sz, GFP_KERNEL);
2902
2903                 if (!hisi_hba->debugfs_port_reg[p])
2904                         goto fail_port;
2905         }
2906
2907         /* Alloc buffer for cq */
2908         sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2909         for (c = 0; c < hisi_hba->queue_count; c++) {
2910                 hisi_hba->debugfs_complete_hdr[c] =
2911                         devm_kmalloc(dev, sz, GFP_KERNEL);
2912
2913                 if (!hisi_hba->debugfs_complete_hdr[c])
2914                         goto fail_cq;
2915         }
2916
2917         /* Alloc buffer for dq */
2918         sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2919         for (d = 0; d < hisi_hba->queue_count; d++) {
2920                 hisi_hba->debugfs_cmd_hdr[d] =
2921                         devm_kmalloc(dev, sz, GFP_KERNEL);
2922
2923                 if (!hisi_hba->debugfs_cmd_hdr[d])
2924                         goto fail_iost_dq;
2925         }
2926
2927         /* Alloc buffer for iost */
2928         sz = max_command_entries * sizeof(struct hisi_sas_iost);
2929
2930         hisi_hba->debugfs_iost = devm_kmalloc(dev, sz, GFP_KERNEL);
2931         if (!hisi_hba->debugfs_iost)
2932                 goto fail_iost_dq;
2933
2934         /* Alloc buffer for itct */
2935         /* New memory allocation must be locate before itct */
2936         sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2937
2938         hisi_hba->debugfs_itct = devm_kmalloc(dev, sz, GFP_KERNEL);
2939         if (!hisi_hba->debugfs_itct)
2940                 goto fail_itct;
2941
2942         return;
2943 fail_itct:
2944         devm_kfree(dev, hisi_hba->debugfs_iost);
2945 fail_iost_dq:
2946         for (i = 0; i < d; i++)
2947                 devm_kfree(dev, hisi_hba->debugfs_cmd_hdr[i]);
2948 fail_cq:
2949         for (i = 0; i < c; i++)
2950                 devm_kfree(dev, hisi_hba->debugfs_complete_hdr[i]);
2951 fail_port:
2952         for (i = 0; i < p; i++)
2953                 devm_kfree(dev, hisi_hba->debugfs_port_reg[i]);
2954         devm_kfree(dev, hisi_hba->debugfs_global_reg);
2955 fail_global:
2956         debugfs_remove_recursive(hisi_hba->debugfs_dir);
2957         dev_dbg(dev, "failed to init debugfs!\n");
2958 }
2959 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
2960
2961 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
2962 {
2963         debugfs_remove_recursive(hisi_hba->debugfs_dir);
2964 }
2965 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
2966
2967 int hisi_sas_remove(struct platform_device *pdev)
2968 {
2969         struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2970         struct hisi_hba *hisi_hba = sha->lldd_ha;
2971         struct Scsi_Host *shost = sha->core.shost;
2972
2973         if (timer_pending(&hisi_hba->timer))
2974                 del_timer(&hisi_hba->timer);
2975
2976         sas_unregister_ha(sha);
2977         sas_remove_host(sha->core.shost);
2978
2979         hisi_sas_free(hisi_hba);
2980         scsi_host_put(shost);
2981         return 0;
2982 }
2983 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2984
2985 bool hisi_sas_debugfs_enable;
2986 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2987 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2988 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
2989
2990 static __init int hisi_sas_init(void)
2991 {
2992         hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2993         if (!hisi_sas_stt)
2994                 return -ENOMEM;
2995
2996         if (hisi_sas_debugfs_enable)
2997                 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2998
2999         return 0;
3000 }
3001
3002 static __exit void hisi_sas_exit(void)
3003 {
3004         sas_release_transport(hisi_sas_stt);
3005
3006         debugfs_remove(hisi_sas_debugfs_dir);
3007 }
3008
3009 module_init(hisi_sas_init);
3010 module_exit(hisi_sas_exit);
3011
3012 MODULE_LICENSE("GPL");
3013 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3014 MODULE_DESCRIPTION("HISILICON SAS controller driver");
3015 MODULE_ALIAS("platform:" DRV_NAME);