scsi: hisi_sas: Reset disks when discovered
[linux-2.6-microblaze.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
1 /*
2  * Copyright (c) 2015 Linaro Ltd.
3  * Copyright (c) 2015 Hisilicon Limited.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  */
11
12 #include "hisi_sas.h"
13 #define DRV_NAME "hisi_sas"
14
15 #define DEV_IS_GONE(dev) \
16         ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19                                 u8 *lun, struct hisi_sas_tmf_task *tmf);
20 static int
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22                              struct domain_device *device,
23                              int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26                                 void *funcdata);
27 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
28                                   struct domain_device *device);
29 static void hisi_sas_dev_gone(struct domain_device *device);
30
31 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
32 {
33         switch (fis->command) {
34         case ATA_CMD_FPDMA_WRITE:
35         case ATA_CMD_FPDMA_READ:
36         case ATA_CMD_FPDMA_RECV:
37         case ATA_CMD_FPDMA_SEND:
38         case ATA_CMD_NCQ_NON_DATA:
39                 return HISI_SAS_SATA_PROTOCOL_FPDMA;
40
41         case ATA_CMD_DOWNLOAD_MICRO:
42         case ATA_CMD_ID_ATA:
43         case ATA_CMD_PMP_READ:
44         case ATA_CMD_READ_LOG_EXT:
45         case ATA_CMD_PIO_READ:
46         case ATA_CMD_PIO_READ_EXT:
47         case ATA_CMD_PMP_WRITE:
48         case ATA_CMD_WRITE_LOG_EXT:
49         case ATA_CMD_PIO_WRITE:
50         case ATA_CMD_PIO_WRITE_EXT:
51                 return HISI_SAS_SATA_PROTOCOL_PIO;
52
53         case ATA_CMD_DSM:
54         case ATA_CMD_DOWNLOAD_MICRO_DMA:
55         case ATA_CMD_PMP_READ_DMA:
56         case ATA_CMD_PMP_WRITE_DMA:
57         case ATA_CMD_READ:
58         case ATA_CMD_READ_EXT:
59         case ATA_CMD_READ_LOG_DMA_EXT:
60         case ATA_CMD_READ_STREAM_DMA_EXT:
61         case ATA_CMD_TRUSTED_RCV_DMA:
62         case ATA_CMD_TRUSTED_SND_DMA:
63         case ATA_CMD_WRITE:
64         case ATA_CMD_WRITE_EXT:
65         case ATA_CMD_WRITE_FUA_EXT:
66         case ATA_CMD_WRITE_QUEUED:
67         case ATA_CMD_WRITE_LOG_DMA_EXT:
68         case ATA_CMD_WRITE_STREAM_DMA_EXT:
69         case ATA_CMD_ZAC_MGMT_IN:
70                 return HISI_SAS_SATA_PROTOCOL_DMA;
71
72         case ATA_CMD_CHK_POWER:
73         case ATA_CMD_DEV_RESET:
74         case ATA_CMD_EDD:
75         case ATA_CMD_FLUSH:
76         case ATA_CMD_FLUSH_EXT:
77         case ATA_CMD_VERIFY:
78         case ATA_CMD_VERIFY_EXT:
79         case ATA_CMD_SET_FEATURES:
80         case ATA_CMD_STANDBY:
81         case ATA_CMD_STANDBYNOW1:
82         case ATA_CMD_ZAC_MGMT_OUT:
83                 return HISI_SAS_SATA_PROTOCOL_NONDATA;
84
85         case ATA_CMD_SET_MAX:
86                 switch (fis->features) {
87                 case ATA_SET_MAX_PASSWD:
88                 case ATA_SET_MAX_LOCK:
89                         return HISI_SAS_SATA_PROTOCOL_PIO;
90
91                 case ATA_SET_MAX_PASSWD_DMA:
92                 case ATA_SET_MAX_UNLOCK_DMA:
93                         return HISI_SAS_SATA_PROTOCOL_DMA;
94
95                 default:
96                         return HISI_SAS_SATA_PROTOCOL_NONDATA;
97                 }
98
99         default:
100         {
101                 if (direction == DMA_NONE)
102                         return HISI_SAS_SATA_PROTOCOL_NONDATA;
103                 return HISI_SAS_SATA_PROTOCOL_PIO;
104         }
105         }
106 }
107 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
108
109 void hisi_sas_sata_done(struct sas_task *task,
110                             struct hisi_sas_slot *slot)
111 {
112         struct task_status_struct *ts = &task->task_status;
113         struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
114         struct hisi_sas_status_buffer *status_buf =
115                         hisi_sas_status_buf_addr_mem(slot);
116         u8 *iu = &status_buf->iu[0];
117         struct dev_to_host_fis *d2h =  (struct dev_to_host_fis *)iu;
118
119         resp->frame_len = sizeof(struct dev_to_host_fis);
120         memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
121
122         ts->buf_valid_size = sizeof(*resp);
123 }
124 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
125
126 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
127 {
128         struct ata_queued_cmd *qc = task->uldd_task;
129
130         if (qc) {
131                 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
132                         qc->tf.command == ATA_CMD_FPDMA_READ) {
133                         *tag = qc->tag;
134                         return 1;
135                 }
136         }
137         return 0;
138 }
139 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
140
141 /*
142  * This function assumes linkrate mask fits in 8 bits, which it
143  * does for all HW versions supported.
144  */
145 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
146 {
147         u16 rate = 0;
148         int i;
149
150         max -= SAS_LINK_RATE_1_5_GBPS;
151         for (i = 0; i <= max; i++)
152                 rate |= 1 << (i * 2);
153         return rate;
154 }
155 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
156
157 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
158 {
159         return device->port->ha->lldd_ha;
160 }
161
162 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
163 {
164         return container_of(sas_port, struct hisi_sas_port, sas_port);
165 }
166 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
167
168 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
169 {
170         int phy_no;
171
172         for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
173                 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
174 }
175 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
176
177 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
178 {
179         void *bitmap = hisi_hba->slot_index_tags;
180
181         clear_bit(slot_idx, bitmap);
182 }
183
184 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
185 {
186         hisi_sas_slot_index_clear(hisi_hba, slot_idx);
187 }
188
189 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
190 {
191         void *bitmap = hisi_hba->slot_index_tags;
192
193         set_bit(slot_idx, bitmap);
194 }
195
196 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
197 {
198         unsigned int index;
199         void *bitmap = hisi_hba->slot_index_tags;
200
201         index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
202                         hisi_hba->last_slot_index + 1);
203         if (index >= hisi_hba->slot_index_count) {
204                 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
205                                            0);
206                 if (index >= hisi_hba->slot_index_count)
207                         return -SAS_QUEUE_FULL;
208         }
209         hisi_sas_slot_index_set(hisi_hba, index);
210         *slot_idx = index;
211         hisi_hba->last_slot_index = index;
212
213         return 0;
214 }
215
216 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
217 {
218         int i;
219
220         for (i = 0; i < hisi_hba->slot_index_count; ++i)
221                 hisi_sas_slot_index_clear(hisi_hba, i);
222 }
223
224 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
225                              struct hisi_sas_slot *slot)
226 {
227         struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
228         unsigned long flags;
229
230         if (task) {
231                 struct device *dev = hisi_hba->dev;
232
233                 if (!task->lldd_task)
234                         return;
235
236                 task->lldd_task = NULL;
237
238                 if (!sas_protocol_ata(task->task_proto))
239                         if (slot->n_elem)
240                                 dma_unmap_sg(dev, task->scatter,
241                                              task->num_scatter,
242                                              task->data_dir);
243         }
244
245         if (slot->buf)
246                 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
247
248         spin_lock_irqsave(&dq->lock, flags);
249         list_del_init(&slot->entry);
250         spin_unlock_irqrestore(&dq->lock, flags);
251         slot->buf = NULL;
252         slot->task = NULL;
253         slot->port = NULL;
254         spin_lock_irqsave(&hisi_hba->lock, flags);
255         hisi_sas_slot_index_free(hisi_hba, slot->idx);
256         spin_unlock_irqrestore(&hisi_hba->lock, flags);
257
258         /* slot memory is fully zeroed when it is reused */
259 }
260 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
261
262 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
263                                   struct hisi_sas_slot *slot)
264 {
265         hisi_hba->hw->prep_smp(hisi_hba, slot);
266 }
267
268 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
269                                   struct hisi_sas_slot *slot, int is_tmf,
270                                   struct hisi_sas_tmf_task *tmf)
271 {
272         hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
273 }
274
275 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
276                                   struct hisi_sas_slot *slot)
277 {
278         hisi_hba->hw->prep_stp(hisi_hba, slot);
279 }
280
281 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
282                 struct hisi_sas_slot *slot,
283                 int device_id, int abort_flag, int tag_to_abort)
284 {
285         hisi_hba->hw->prep_abort(hisi_hba, slot,
286                         device_id, abort_flag, tag_to_abort);
287 }
288
289 /*
290  * This function will issue an abort TMF regardless of whether the
291  * task is in the sdev or not. Then it will do the task complete
292  * cleanup and callbacks.
293  */
294 static void hisi_sas_slot_abort(struct work_struct *work)
295 {
296         struct hisi_sas_slot *abort_slot =
297                 container_of(work, struct hisi_sas_slot, abort_slot);
298         struct sas_task *task = abort_slot->task;
299         struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
300         struct scsi_cmnd *cmnd = task->uldd_task;
301         struct hisi_sas_tmf_task tmf_task;
302         struct scsi_lun lun;
303         struct device *dev = hisi_hba->dev;
304         int tag = abort_slot->idx;
305
306         if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
307                 dev_err(dev, "cannot abort slot for non-ssp task\n");
308                 goto out;
309         }
310
311         int_to_scsilun(cmnd->device->lun, &lun);
312         tmf_task.tmf = TMF_ABORT_TASK;
313         tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
314
315         hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
316 out:
317         /* Do cleanup for this task */
318         hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
319         if (task->task_done)
320                 task->task_done(task);
321 }
322
323 static int hisi_sas_task_prep(struct sas_task *task,
324                               struct hisi_sas_dq **dq_pointer,
325                               int is_tmf, struct hisi_sas_tmf_task *tmf,
326                               int *pass)
327 {
328         struct domain_device *device = task->dev;
329         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
330         struct hisi_sas_device *sas_dev = device->lldd_dev;
331         struct hisi_sas_port *port;
332         struct hisi_sas_slot *slot;
333         struct hisi_sas_cmd_hdr *cmd_hdr_base;
334         struct asd_sas_port *sas_port = device->port;
335         struct device *dev = hisi_hba->dev;
336         int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
337         int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
338         unsigned long flags, flags_dq;
339         struct hisi_sas_dq *dq;
340         int wr_q_index;
341
342         if (!sas_port) {
343                 struct task_status_struct *ts = &task->task_status;
344
345                 ts->resp = SAS_TASK_UNDELIVERED;
346                 ts->stat = SAS_PHY_DOWN;
347                 /*
348                  * libsas will use dev->port, should
349                  * not call task_done for sata
350                  */
351                 if (device->dev_type != SAS_SATA_DEV)
352                         task->task_done(task);
353                 return -ECOMM;
354         }
355
356         if (DEV_IS_GONE(sas_dev)) {
357                 if (sas_dev)
358                         dev_info(dev, "task prep: device %d not ready\n",
359                                  sas_dev->device_id);
360                 else
361                         dev_info(dev, "task prep: device %016llx not ready\n",
362                                  SAS_ADDR(device->sas_addr));
363
364                 return -ECOMM;
365         }
366
367         *dq_pointer = dq = sas_dev->dq;
368
369         port = to_hisi_sas_port(sas_port);
370         if (port && !port->port_attached) {
371                 dev_info(dev, "task prep: %s port%d not attach device\n",
372                          (dev_is_sata(device)) ?
373                          "SATA/STP" : "SAS",
374                          device->port->id);
375
376                 return -ECOMM;
377         }
378
379         if (!sas_protocol_ata(task->task_proto)) {
380                 unsigned int req_len, resp_len;
381
382                 if (task->num_scatter) {
383                         n_elem = dma_map_sg(dev, task->scatter,
384                                             task->num_scatter, task->data_dir);
385                         if (!n_elem) {
386                                 rc = -ENOMEM;
387                                 goto prep_out;
388                         }
389                 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
390                         n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
391                                                 1, DMA_TO_DEVICE);
392                         if (!n_elem_req) {
393                                 rc = -ENOMEM;
394                                 goto prep_out;
395                         }
396                         req_len = sg_dma_len(&task->smp_task.smp_req);
397                         if (req_len & 0x3) {
398                                 rc = -EINVAL;
399                                 goto err_out_dma_unmap;
400                         }
401                         n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
402                                                  1, DMA_FROM_DEVICE);
403                         if (!n_elem_resp) {
404                                 rc = -ENOMEM;
405                                 goto err_out_dma_unmap;
406                         }
407                         resp_len = sg_dma_len(&task->smp_task.smp_resp);
408                         if (resp_len & 0x3) {
409                                 rc = -EINVAL;
410                                 goto err_out_dma_unmap;
411                         }
412                 }
413         } else
414                 n_elem = task->num_scatter;
415
416         if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
417                 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
418                         n_elem);
419                 rc = -EINVAL;
420                 goto err_out_dma_unmap;
421         }
422
423         spin_lock_irqsave(&hisi_hba->lock, flags);
424         if (hisi_hba->hw->slot_index_alloc)
425                 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
426                                                     device);
427         else
428                 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
429         spin_unlock_irqrestore(&hisi_hba->lock, flags);
430         if (rc)
431                 goto err_out_dma_unmap;
432
433         slot = &hisi_hba->slot_info[slot_idx];
434         memset(slot, 0, sizeof(struct hisi_sas_slot));
435
436         slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
437                                    GFP_ATOMIC, &slot->buf_dma);
438         if (!slot->buf) {
439                 rc = -ENOMEM;
440                 goto err_out_tag;
441         }
442
443         spin_lock_irqsave(&dq->lock, flags_dq);
444         wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
445         if (wr_q_index < 0) {
446                 spin_unlock_irqrestore(&dq->lock, flags_dq);
447                 goto err_out_buf;
448         }
449
450         list_add_tail(&slot->delivery, &dq->list);
451         spin_unlock_irqrestore(&dq->lock, flags_dq);
452
453         dlvry_queue = dq->id;
454         dlvry_queue_slot = wr_q_index;
455
456         slot->idx = slot_idx;
457         slot->n_elem = n_elem;
458         slot->dlvry_queue = dlvry_queue;
459         slot->dlvry_queue_slot = dlvry_queue_slot;
460         cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
461         slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
462         slot->task = task;
463         slot->port = port;
464         if (is_tmf)
465                 slot->is_internal = true;
466         task->lldd_task = slot;
467         INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
468
469         memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
470         memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
471         memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
472
473         switch (task->task_proto) {
474         case SAS_PROTOCOL_SMP:
475                 hisi_sas_task_prep_smp(hisi_hba, slot);
476                 break;
477         case SAS_PROTOCOL_SSP:
478                 hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
479                 break;
480         case SAS_PROTOCOL_SATA:
481         case SAS_PROTOCOL_STP:
482         case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
483                 hisi_sas_task_prep_ata(hisi_hba, slot);
484                 break;
485         default:
486                 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
487                         task->task_proto);
488                 break;
489         }
490
491         spin_lock_irqsave(&dq->lock, flags);
492         list_add_tail(&slot->entry, &sas_dev->list);
493         spin_unlock_irqrestore(&dq->lock, flags);
494         spin_lock_irqsave(&task->task_state_lock, flags);
495         task->task_state_flags |= SAS_TASK_AT_INITIATOR;
496         spin_unlock_irqrestore(&task->task_state_lock, flags);
497
498         ++(*pass);
499         slot->ready = 1;
500
501         return 0;
502
503 err_out_buf:
504         dma_pool_free(hisi_hba->buffer_pool, slot->buf,
505                       slot->buf_dma);
506 err_out_tag:
507         spin_lock_irqsave(&hisi_hba->lock, flags);
508         hisi_sas_slot_index_free(hisi_hba, slot_idx);
509         spin_unlock_irqrestore(&hisi_hba->lock, flags);
510 err_out_dma_unmap:
511         if (!sas_protocol_ata(task->task_proto)) {
512                 if (task->num_scatter) {
513                         dma_unmap_sg(dev, task->scatter, task->num_scatter,
514                              task->data_dir);
515                 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
516                         if (n_elem_req)
517                                 dma_unmap_sg(dev, &task->smp_task.smp_req,
518                                              1, DMA_TO_DEVICE);
519                         if (n_elem_resp)
520                                 dma_unmap_sg(dev, &task->smp_task.smp_resp,
521                                              1, DMA_FROM_DEVICE);
522                 }
523         }
524 prep_out:
525         dev_err(dev, "task prep: failed[%d]!\n", rc);
526         return rc;
527 }
528
529 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
530                               int is_tmf, struct hisi_sas_tmf_task *tmf)
531 {
532         u32 rc;
533         u32 pass = 0;
534         unsigned long flags;
535         struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
536         struct device *dev = hisi_hba->dev;
537         struct hisi_sas_dq *dq = NULL;
538
539         if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
540                 return -EINVAL;
541
542         /* protect task_prep and start_delivery sequence */
543         rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
544         if (rc)
545                 dev_err(dev, "task exec: failed[%d]!\n", rc);
546
547         if (likely(pass)) {
548                 spin_lock_irqsave(&dq->lock, flags);
549                 hisi_hba->hw->start_delivery(dq);
550                 spin_unlock_irqrestore(&dq->lock, flags);
551         }
552
553         return rc;
554 }
555
556 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
557 {
558         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
559         struct asd_sas_phy *sas_phy = &phy->sas_phy;
560         struct sas_ha_struct *sas_ha;
561
562         if (!phy->phy_attached)
563                 return;
564
565         sas_ha = &hisi_hba->sha;
566         sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
567
568         if (sas_phy->phy) {
569                 struct sas_phy *sphy = sas_phy->phy;
570
571                 sphy->negotiated_linkrate = sas_phy->linkrate;
572                 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
573                 sphy->maximum_linkrate_hw =
574                         hisi_hba->hw->phy_get_max_linkrate();
575                 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
576                         sphy->minimum_linkrate = phy->minimum_linkrate;
577
578                 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
579                         sphy->maximum_linkrate = phy->maximum_linkrate;
580         }
581
582         if (phy->phy_type & PORT_TYPE_SAS) {
583                 struct sas_identify_frame *id;
584
585                 id = (struct sas_identify_frame *)phy->frame_rcvd;
586                 id->dev_type = phy->identify.device_type;
587                 id->initiator_bits = SAS_PROTOCOL_ALL;
588                 id->target_bits = phy->identify.target_port_protocols;
589         } else if (phy->phy_type & PORT_TYPE_SATA) {
590                 /*Nothing*/
591         }
592
593         sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
594         sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
595 }
596
597 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
598 {
599         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
600         struct hisi_sas_device *sas_dev = NULL;
601         unsigned long flags;
602         int last = hisi_hba->last_dev_id;
603         int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
604         int i;
605
606         spin_lock_irqsave(&hisi_hba->lock, flags);
607         for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
608                 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
609                         int queue = i % hisi_hba->queue_count;
610                         struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
611
612                         hisi_hba->devices[i].device_id = i;
613                         sas_dev = &hisi_hba->devices[i];
614                         sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
615                         sas_dev->dev_type = device->dev_type;
616                         sas_dev->hisi_hba = hisi_hba;
617                         sas_dev->sas_device = device;
618                         sas_dev->dq = dq;
619                         INIT_LIST_HEAD(&hisi_hba->devices[i].list);
620                         break;
621                 }
622                 i++;
623         }
624         hisi_hba->last_dev_id = i;
625         spin_unlock_irqrestore(&hisi_hba->lock, flags);
626
627         return sas_dev;
628 }
629
630 #define HISI_SAS_SRST_ATA_DISK_CNT 3
631 static int hisi_sas_init_device(struct domain_device *device)
632 {
633         int rc = TMF_RESP_FUNC_COMPLETE;
634         struct scsi_lun lun;
635         struct hisi_sas_tmf_task tmf_task;
636         int retry = HISI_SAS_SRST_ATA_DISK_CNT;
637         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
638
639         switch (device->dev_type) {
640         case SAS_END_DEVICE:
641                 int_to_scsilun(0, &lun);
642
643                 tmf_task.tmf = TMF_CLEAR_TASK_SET;
644                 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
645                                                   &tmf_task);
646                 if (rc == TMF_RESP_FUNC_COMPLETE)
647                         hisi_sas_release_task(hisi_hba, device);
648                 break;
649         case SAS_SATA_DEV:
650         case SAS_SATA_PM:
651         case SAS_SATA_PM_PORT:
652         case SAS_SATA_PENDING:
653                 while (retry-- > 0) {
654                         rc = hisi_sas_softreset_ata_disk(device);
655                         if (!rc)
656                                 break;
657                 }
658                 break;
659         default:
660                 break;
661         }
662
663         return rc;
664 }
665
666 static int hisi_sas_dev_found(struct domain_device *device)
667 {
668         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
669         struct domain_device *parent_dev = device->parent;
670         struct hisi_sas_device *sas_dev;
671         struct device *dev = hisi_hba->dev;
672         int rc;
673
674         if (hisi_hba->hw->alloc_dev)
675                 sas_dev = hisi_hba->hw->alloc_dev(device);
676         else
677                 sas_dev = hisi_sas_alloc_dev(device);
678         if (!sas_dev) {
679                 dev_err(dev, "fail alloc dev: max support %d devices\n",
680                         HISI_SAS_MAX_DEVICES);
681                 return -EINVAL;
682         }
683
684         device->lldd_dev = sas_dev;
685         hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
686
687         if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
688                 int phy_no;
689                 u8 phy_num = parent_dev->ex_dev.num_phys;
690                 struct ex_phy *phy;
691
692                 for (phy_no = 0; phy_no < phy_num; phy_no++) {
693                         phy = &parent_dev->ex_dev.ex_phy[phy_no];
694                         if (SAS_ADDR(phy->attached_sas_addr) ==
695                                 SAS_ADDR(device->sas_addr))
696                                 break;
697                 }
698
699                 if (phy_no == phy_num) {
700                         dev_info(dev, "dev found: no attached "
701                                  "dev:%016llx at ex:%016llx\n",
702                                  SAS_ADDR(device->sas_addr),
703                                  SAS_ADDR(parent_dev->sas_addr));
704                         rc = -EINVAL;
705                         goto err_out;
706                 }
707         }
708
709         dev_info(dev, "dev[%d:%x] found\n",
710                 sas_dev->device_id, sas_dev->dev_type);
711
712         rc = hisi_sas_init_device(device);
713         if (rc)
714                 goto err_out;
715         return 0;
716
717 err_out:
718         hisi_sas_dev_gone(device);
719         return rc;
720 }
721
722 static int hisi_sas_slave_configure(struct scsi_device *sdev)
723 {
724         struct domain_device *dev = sdev_to_domain_dev(sdev);
725         int ret = sas_slave_configure(sdev);
726
727         if (ret)
728                 return ret;
729         if (!dev_is_sata(dev))
730                 sas_change_queue_depth(sdev, 64);
731
732         return 0;
733 }
734
735 static void hisi_sas_scan_start(struct Scsi_Host *shost)
736 {
737         struct hisi_hba *hisi_hba = shost_priv(shost);
738
739         hisi_hba->hw->phys_init(hisi_hba);
740 }
741
742 static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
743 {
744         struct hisi_hba *hisi_hba = shost_priv(shost);
745         struct sas_ha_struct *sha = &hisi_hba->sha;
746
747         /* Wait for PHY up interrupt to occur */
748         if (time < HZ)
749                 return 0;
750
751         sas_drain_work(sha);
752         return 1;
753 }
754
755 static void hisi_sas_phyup_work(struct work_struct *work)
756 {
757         struct hisi_sas_phy *phy =
758                 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
759         struct hisi_hba *hisi_hba = phy->hisi_hba;
760         struct asd_sas_phy *sas_phy = &phy->sas_phy;
761         int phy_no = sas_phy->id;
762
763         hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
764         hisi_sas_bytes_dmaed(hisi_hba, phy_no);
765 }
766
767 static void hisi_sas_linkreset_work(struct work_struct *work)
768 {
769         struct hisi_sas_phy *phy =
770                 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
771         struct asd_sas_phy *sas_phy = &phy->sas_phy;
772
773         hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
774 }
775
776 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
777         [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
778         [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
779 };
780
781 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
782                                 enum hisi_sas_phy_event event)
783 {
784         struct hisi_hba *hisi_hba = phy->hisi_hba;
785
786         if (WARN_ON(event >= HISI_PHYES_NUM))
787                 return false;
788
789         return queue_work(hisi_hba->wq, &phy->works[event]);
790 }
791 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
792
793 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
794 {
795         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
796         struct asd_sas_phy *sas_phy = &phy->sas_phy;
797         int i;
798
799         phy->hisi_hba = hisi_hba;
800         phy->port = NULL;
801         phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
802         phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
803         sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
804         sas_phy->class = SAS;
805         sas_phy->iproto = SAS_PROTOCOL_ALL;
806         sas_phy->tproto = 0;
807         sas_phy->type = PHY_TYPE_PHYSICAL;
808         sas_phy->role = PHY_ROLE_INITIATOR;
809         sas_phy->oob_mode = OOB_NOT_CONNECTED;
810         sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
811         sas_phy->id = phy_no;
812         sas_phy->sas_addr = &hisi_hba->sas_addr[0];
813         sas_phy->frame_rcvd = &phy->frame_rcvd[0];
814         sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
815         sas_phy->lldd_phy = phy;
816
817         for (i = 0; i < HISI_PHYES_NUM; i++)
818                 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
819 }
820
821 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
822 {
823         struct sas_ha_struct *sas_ha = sas_phy->ha;
824         struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
825         struct hisi_sas_phy *phy = sas_phy->lldd_phy;
826         struct asd_sas_port *sas_port = sas_phy->port;
827         struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
828         unsigned long flags;
829
830         if (!sas_port)
831                 return;
832
833         spin_lock_irqsave(&hisi_hba->lock, flags);
834         port->port_attached = 1;
835         port->id = phy->port_id;
836         phy->port = port;
837         sas_port->lldd_port = port;
838         spin_unlock_irqrestore(&hisi_hba->lock, flags);
839 }
840
841 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
842                                      struct hisi_sas_slot *slot)
843 {
844         if (task) {
845                 unsigned long flags;
846                 struct task_status_struct *ts;
847
848                 ts = &task->task_status;
849
850                 ts->resp = SAS_TASK_COMPLETE;
851                 ts->stat = SAS_ABORTED_TASK;
852                 spin_lock_irqsave(&task->task_state_lock, flags);
853                 task->task_state_flags &=
854                         ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
855                 task->task_state_flags |= SAS_TASK_STATE_DONE;
856                 spin_unlock_irqrestore(&task->task_state_lock, flags);
857         }
858
859         hisi_sas_slot_task_free(hisi_hba, task, slot);
860 }
861
862 /* hisi_hba.lock should be locked */
863 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
864                         struct domain_device *device)
865 {
866         struct hisi_sas_slot *slot, *slot2;
867         struct hisi_sas_device *sas_dev = device->lldd_dev;
868
869         list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
870                 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
871 }
872
873 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
874 {
875         struct hisi_sas_device *sas_dev;
876         struct domain_device *device;
877         int i;
878
879         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
880                 sas_dev = &hisi_hba->devices[i];
881                 device = sas_dev->sas_device;
882
883                 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
884                     !device)
885                         continue;
886
887                 hisi_sas_release_task(hisi_hba, device);
888         }
889 }
890 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
891
892 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
893                                 struct domain_device *device)
894 {
895         if (hisi_hba->hw->dereg_device)
896                 hisi_hba->hw->dereg_device(hisi_hba, device);
897 }
898
899 static void hisi_sas_dev_gone(struct domain_device *device)
900 {
901         struct hisi_sas_device *sas_dev = device->lldd_dev;
902         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
903         struct device *dev = hisi_hba->dev;
904
905         dev_info(dev, "dev[%d:%x] is gone\n",
906                  sas_dev->device_id, sas_dev->dev_type);
907
908         if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
909                 hisi_sas_internal_task_abort(hisi_hba, device,
910                                      HISI_SAS_INT_ABT_DEV, 0);
911
912                 hisi_sas_dereg_device(hisi_hba, device);
913
914                 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
915                 device->lldd_dev = NULL;
916         }
917
918         if (hisi_hba->hw->free_device)
919                 hisi_hba->hw->free_device(sas_dev);
920         sas_dev->dev_type = SAS_PHY_UNUSED;
921 }
922
923 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
924 {
925         return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
926 }
927
928 static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
929                         struct sas_phy_linkrates *r)
930 {
931         struct sas_phy_linkrates _r;
932
933         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
934         struct asd_sas_phy *sas_phy = &phy->sas_phy;
935         enum sas_linkrate min, max;
936
937         if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
938                 max = sas_phy->phy->maximum_linkrate;
939                 min = r->minimum_linkrate;
940         } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
941                 max = r->maximum_linkrate;
942                 min = sas_phy->phy->minimum_linkrate;
943         } else
944                 return;
945
946         _r.maximum_linkrate = max;
947         _r.minimum_linkrate = min;
948
949         hisi_hba->hw->phy_disable(hisi_hba, phy_no);
950         msleep(100);
951         hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
952         hisi_hba->hw->phy_start(hisi_hba, phy_no);
953 }
954
955 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
956                                 void *funcdata)
957 {
958         struct sas_ha_struct *sas_ha = sas_phy->ha;
959         struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
960         int phy_no = sas_phy->id;
961
962         switch (func) {
963         case PHY_FUNC_HARD_RESET:
964                 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
965                 break;
966
967         case PHY_FUNC_LINK_RESET:
968                 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
969                 msleep(100);
970                 hisi_hba->hw->phy_start(hisi_hba, phy_no);
971                 break;
972
973         case PHY_FUNC_DISABLE:
974                 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
975                 break;
976
977         case PHY_FUNC_SET_LINK_RATE:
978                 hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
979                 break;
980         case PHY_FUNC_GET_EVENTS:
981                 if (hisi_hba->hw->get_events) {
982                         hisi_hba->hw->get_events(hisi_hba, phy_no);
983                         break;
984                 }
985                 /* fallthru */
986         case PHY_FUNC_RELEASE_SPINUP_HOLD:
987         default:
988                 return -EOPNOTSUPP;
989         }
990         return 0;
991 }
992
993 static void hisi_sas_task_done(struct sas_task *task)
994 {
995         if (!del_timer(&task->slow_task->timer))
996                 return;
997         complete(&task->slow_task->completion);
998 }
999
1000 static void hisi_sas_tmf_timedout(struct timer_list *t)
1001 {
1002         struct sas_task_slow *slow = from_timer(slow, t, timer);
1003         struct sas_task *task = slow->task;
1004         unsigned long flags;
1005
1006         spin_lock_irqsave(&task->task_state_lock, flags);
1007         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
1008                 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1009         spin_unlock_irqrestore(&task->task_state_lock, flags);
1010
1011         complete(&task->slow_task->completion);
1012 }
1013
1014 #define TASK_TIMEOUT 20
1015 #define TASK_RETRY 3
1016 #define INTERNAL_ABORT_TIMEOUT 6
1017 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1018                                            void *parameter, u32 para_len,
1019                                            struct hisi_sas_tmf_task *tmf)
1020 {
1021         struct hisi_sas_device *sas_dev = device->lldd_dev;
1022         struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1023         struct device *dev = hisi_hba->dev;
1024         struct sas_task *task;
1025         int res, retry;
1026
1027         for (retry = 0; retry < TASK_RETRY; retry++) {
1028                 task = sas_alloc_slow_task(GFP_KERNEL);
1029                 if (!task)
1030                         return -ENOMEM;
1031
1032                 task->dev = device;
1033                 task->task_proto = device->tproto;
1034
1035                 if (dev_is_sata(device)) {
1036                         task->ata_task.device_control_reg_update = 1;
1037                         memcpy(&task->ata_task.fis, parameter, para_len);
1038                 } else {
1039                         memcpy(&task->ssp_task, parameter, para_len);
1040                 }
1041                 task->task_done = hisi_sas_task_done;
1042
1043                 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1044                 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1045                 add_timer(&task->slow_task->timer);
1046
1047                 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1048
1049                 if (res) {
1050                         del_timer(&task->slow_task->timer);
1051                         dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1052                                 res);
1053                         goto ex_err;
1054                 }
1055
1056                 wait_for_completion(&task->slow_task->completion);
1057                 res = TMF_RESP_FUNC_FAILED;
1058                 /* Even TMF timed out, return direct. */
1059                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1060                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1061                                 struct hisi_sas_slot *slot = task->lldd_task;
1062
1063                                 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1064                                 if (slot)
1065                                         slot->task = NULL;
1066
1067                                 goto ex_err;
1068                         } else
1069                                 dev_err(dev, "abort tmf: TMF task timeout\n");
1070                 }
1071
1072                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1073                      task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1074                         res = TMF_RESP_FUNC_COMPLETE;
1075                         break;
1076                 }
1077
1078                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1079                         task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1080                         res = TMF_RESP_FUNC_SUCC;
1081                         break;
1082                 }
1083
1084                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1085                       task->task_status.stat == SAS_DATA_UNDERRUN) {
1086                         /* no error, but return the number of bytes of
1087                          * underrun
1088                          */
1089                         dev_warn(dev, "abort tmf: task to dev %016llx "
1090                                  "resp: 0x%x sts 0x%x underrun\n",
1091                                  SAS_ADDR(device->sas_addr),
1092                                  task->task_status.resp,
1093                                  task->task_status.stat);
1094                         res = task->task_status.residual;
1095                         break;
1096                 }
1097
1098                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1099                         task->task_status.stat == SAS_DATA_OVERRUN) {
1100                         dev_warn(dev, "abort tmf: blocked task error\n");
1101                         res = -EMSGSIZE;
1102                         break;
1103                 }
1104
1105                 dev_warn(dev, "abort tmf: task to dev "
1106                          "%016llx resp: 0x%x status 0x%x\n",
1107                          SAS_ADDR(device->sas_addr), task->task_status.resp,
1108                          task->task_status.stat);
1109                 sas_free_task(task);
1110                 task = NULL;
1111         }
1112 ex_err:
1113         if (retry == TASK_RETRY)
1114                 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1115         sas_free_task(task);
1116         return res;
1117 }
1118
1119 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1120                 bool reset, int pmp, u8 *fis)
1121 {
1122         struct ata_taskfile tf;
1123
1124         ata_tf_init(dev, &tf);
1125         if (reset)
1126                 tf.ctl |= ATA_SRST;
1127         else
1128                 tf.ctl &= ~ATA_SRST;
1129         tf.command = ATA_CMD_DEV_RESET;
1130         ata_tf_to_fis(&tf, pmp, 0, fis);
1131 }
1132
1133 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1134 {
1135         u8 fis[20] = {0};
1136         struct ata_port *ap = device->sata_dev.ap;
1137         struct ata_link *link;
1138         int rc = TMF_RESP_FUNC_FAILED;
1139         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1140         struct device *dev = hisi_hba->dev;
1141         int s = sizeof(struct host_to_dev_fis);
1142
1143         ata_for_each_link(link, ap, EDGE) {
1144                 int pmp = sata_srst_pmp(link);
1145
1146                 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1147                 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1148                 if (rc != TMF_RESP_FUNC_COMPLETE)
1149                         break;
1150         }
1151
1152         if (rc == TMF_RESP_FUNC_COMPLETE) {
1153                 ata_for_each_link(link, ap, EDGE) {
1154                         int pmp = sata_srst_pmp(link);
1155
1156                         hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1157                         rc = hisi_sas_exec_internal_tmf_task(device, fis,
1158                                                              s, NULL);
1159                         if (rc != TMF_RESP_FUNC_COMPLETE)
1160                                 dev_err(dev, "ata disk de-reset failed\n");
1161                 }
1162         } else {
1163                 dev_err(dev, "ata disk reset failed\n");
1164         }
1165
1166         if (rc == TMF_RESP_FUNC_COMPLETE)
1167                 hisi_sas_release_task(hisi_hba, device);
1168
1169         return rc;
1170 }
1171
1172 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1173                                 u8 *lun, struct hisi_sas_tmf_task *tmf)
1174 {
1175         struct sas_ssp_task ssp_task;
1176
1177         if (!(device->tproto & SAS_PROTOCOL_SSP))
1178                 return TMF_RESP_FUNC_ESUPP;
1179
1180         memcpy(ssp_task.LUN, lun, 8);
1181
1182         return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1183                                 sizeof(ssp_task), tmf);
1184 }
1185
1186 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1187 {
1188         u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1189         int i;
1190
1191         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1192                 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1193                 struct domain_device *device = sas_dev->sas_device;
1194                 struct asd_sas_port *sas_port;
1195                 struct hisi_sas_port *port;
1196                 struct hisi_sas_phy *phy = NULL;
1197                 struct asd_sas_phy *sas_phy;
1198
1199                 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1200                                 || !device || !device->port)
1201                         continue;
1202
1203                 sas_port = device->port;
1204                 port = to_hisi_sas_port(sas_port);
1205
1206                 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1207                         if (state & BIT(sas_phy->id)) {
1208                                 phy = sas_phy->lldd_phy;
1209                                 break;
1210                         }
1211
1212                 if (phy) {
1213                         port->id = phy->port_id;
1214
1215                         /* Update linkrate of directly attached device. */
1216                         if (!device->parent)
1217                                 device->linkrate = phy->sas_phy.linkrate;
1218
1219                         hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1220                 } else
1221                         port->id = 0xff;
1222         }
1223 }
1224
1225 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1226                               u32 state)
1227 {
1228         struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1229         struct asd_sas_port *_sas_port = NULL;
1230         int phy_no;
1231
1232         for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1233                 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1234                 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1235                 struct asd_sas_port *sas_port = sas_phy->port;
1236                 bool do_port_check = !!(_sas_port != sas_port);
1237
1238                 if (!sas_phy->phy->enabled)
1239                         continue;
1240
1241                 /* Report PHY state change to libsas */
1242                 if (state & BIT(phy_no)) {
1243                         if (do_port_check && sas_port && sas_port->port_dev) {
1244                                 struct domain_device *dev = sas_port->port_dev;
1245
1246                                 _sas_port = sas_port;
1247
1248                                 if (DEV_IS_EXPANDER(dev->dev_type))
1249                                         sas_ha->notify_port_event(sas_phy,
1250                                                         PORTE_BROADCAST_RCVD);
1251                         }
1252                 } else if (old_state & (1 << phy_no))
1253                         /* PHY down but was up before */
1254                         hisi_sas_phy_down(hisi_hba, phy_no, 0);
1255
1256         }
1257 }
1258
1259 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1260 {
1261         struct device *dev = hisi_hba->dev;
1262         struct Scsi_Host *shost = hisi_hba->shost;
1263         u32 old_state, state;
1264         int rc;
1265
1266         if (!hisi_hba->hw->soft_reset)
1267                 return -1;
1268
1269         if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1270                 return -1;
1271
1272         dev_info(dev, "controller resetting...\n");
1273         old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1274
1275         scsi_block_requests(shost);
1276         if (timer_pending(&hisi_hba->timer))
1277                 del_timer_sync(&hisi_hba->timer);
1278
1279         set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1280         rc = hisi_hba->hw->soft_reset(hisi_hba);
1281         if (rc) {
1282                 dev_warn(dev, "controller reset failed (%d)\n", rc);
1283                 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1284                 scsi_unblock_requests(shost);
1285                 goto out;
1286         }
1287         hisi_sas_release_tasks(hisi_hba);
1288
1289         clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1290
1291         /* Init and wait for PHYs to come up and all libsas event finished. */
1292         hisi_hba->hw->phys_init(hisi_hba);
1293         msleep(1000);
1294         hisi_sas_refresh_port_id(hisi_hba);
1295         scsi_unblock_requests(shost);
1296
1297         state = hisi_hba->hw->get_phys_state(hisi_hba);
1298         hisi_sas_rescan_topology(hisi_hba, old_state, state);
1299         dev_info(dev, "controller reset complete\n");
1300
1301 out:
1302         clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1303
1304         return rc;
1305 }
1306
1307 static int hisi_sas_abort_task(struct sas_task *task)
1308 {
1309         struct scsi_lun lun;
1310         struct hisi_sas_tmf_task tmf_task;
1311         struct domain_device *device = task->dev;
1312         struct hisi_sas_device *sas_dev = device->lldd_dev;
1313         struct hisi_hba *hisi_hba;
1314         struct device *dev;
1315         int rc = TMF_RESP_FUNC_FAILED;
1316         unsigned long flags;
1317
1318         if (!sas_dev)
1319                 return TMF_RESP_FUNC_FAILED;
1320
1321         hisi_hba = dev_to_hisi_hba(task->dev);
1322         dev = hisi_hba->dev;
1323
1324         spin_lock_irqsave(&task->task_state_lock, flags);
1325         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1326                 spin_unlock_irqrestore(&task->task_state_lock, flags);
1327                 rc = TMF_RESP_FUNC_COMPLETE;
1328                 goto out;
1329         }
1330         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1331         spin_unlock_irqrestore(&task->task_state_lock, flags);
1332
1333         sas_dev->dev_status = HISI_SAS_DEV_EH;
1334         if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1335                 struct scsi_cmnd *cmnd = task->uldd_task;
1336                 struct hisi_sas_slot *slot = task->lldd_task;
1337                 u32 tag = slot->idx;
1338                 int rc2;
1339
1340                 int_to_scsilun(cmnd->device->lun, &lun);
1341                 tmf_task.tmf = TMF_ABORT_TASK;
1342                 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1343
1344                 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1345                                                   &tmf_task);
1346
1347                 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1348                                                    HISI_SAS_INT_ABT_CMD, tag);
1349                 if (rc2 < 0) {
1350                         dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1351                         return TMF_RESP_FUNC_FAILED;
1352                 }
1353
1354                 /*
1355                  * If the TMF finds that the IO is not in the device and also
1356                  * the internal abort does not succeed, then it is safe to
1357                  * free the slot.
1358                  * Note: if the internal abort succeeds then the slot
1359                  * will have already been completed
1360                  */
1361                 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1362                         if (task->lldd_task)
1363                                 hisi_sas_do_release_task(hisi_hba, task, slot);
1364                 }
1365         } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1366                 task->task_proto & SAS_PROTOCOL_STP) {
1367                 if (task->dev->dev_type == SAS_SATA_DEV) {
1368                         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1369                                                 HISI_SAS_INT_ABT_DEV, 0);
1370                         if (rc < 0) {
1371                                 dev_err(dev, "abort task: internal abort failed\n");
1372                                 goto out;
1373                         }
1374                         hisi_sas_dereg_device(hisi_hba, device);
1375                         rc = hisi_sas_softreset_ata_disk(device);
1376                 }
1377         } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1378                 /* SMP */
1379                 struct hisi_sas_slot *slot = task->lldd_task;
1380                 u32 tag = slot->idx;
1381
1382                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1383                              HISI_SAS_INT_ABT_CMD, tag);
1384                 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1385                                         task->lldd_task)
1386                         hisi_sas_do_release_task(hisi_hba, task, slot);
1387         }
1388
1389 out:
1390         if (rc != TMF_RESP_FUNC_COMPLETE)
1391                 dev_notice(dev, "abort task: rc=%d\n", rc);
1392         return rc;
1393 }
1394
1395 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1396 {
1397         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1398         struct device *dev = hisi_hba->dev;
1399         struct hisi_sas_tmf_task tmf_task;
1400         int rc = TMF_RESP_FUNC_FAILED;
1401
1402         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1403                                         HISI_SAS_INT_ABT_DEV, 0);
1404         if (rc < 0) {
1405                 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1406                 return TMF_RESP_FUNC_FAILED;
1407         }
1408         hisi_sas_dereg_device(hisi_hba, device);
1409
1410         tmf_task.tmf = TMF_ABORT_TASK_SET;
1411         rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1412
1413         if (rc == TMF_RESP_FUNC_COMPLETE)
1414                 hisi_sas_release_task(hisi_hba, device);
1415
1416         return rc;
1417 }
1418
1419 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1420 {
1421         int rc = TMF_RESP_FUNC_FAILED;
1422         struct hisi_sas_tmf_task tmf_task;
1423
1424         tmf_task.tmf = TMF_CLEAR_ACA;
1425         rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1426
1427         return rc;
1428 }
1429
1430 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1431 {
1432         struct sas_phy *phy = sas_get_local_phy(device);
1433         int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1434                         (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1435         rc = sas_phy_reset(phy, reset_type);
1436         sas_put_local_phy(phy);
1437         msleep(2000);
1438         return rc;
1439 }
1440
1441 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1442 {
1443         struct hisi_sas_device *sas_dev = device->lldd_dev;
1444         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1445         struct device *dev = hisi_hba->dev;
1446         int rc = TMF_RESP_FUNC_FAILED;
1447
1448         if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1449                 return TMF_RESP_FUNC_FAILED;
1450         sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1451
1452         rc = hisi_sas_internal_task_abort(hisi_hba, device,
1453                                         HISI_SAS_INT_ABT_DEV, 0);
1454         if (rc < 0) {
1455                 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1456                 return TMF_RESP_FUNC_FAILED;
1457         }
1458         hisi_sas_dereg_device(hisi_hba, device);
1459
1460         rc = hisi_sas_debug_I_T_nexus_reset(device);
1461
1462         if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1463                 hisi_sas_release_task(hisi_hba, device);
1464
1465         return rc;
1466 }
1467
1468 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1469 {
1470         struct hisi_sas_device *sas_dev = device->lldd_dev;
1471         struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1472         struct device *dev = hisi_hba->dev;
1473         int rc = TMF_RESP_FUNC_FAILED;
1474
1475         sas_dev->dev_status = HISI_SAS_DEV_EH;
1476         if (dev_is_sata(device)) {
1477                 struct sas_phy *phy;
1478
1479                 /* Clear internal IO and then hardreset */
1480                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1481                                                   HISI_SAS_INT_ABT_DEV, 0);
1482                 if (rc < 0) {
1483                         dev_err(dev, "lu_reset: internal abort failed\n");
1484                         goto out;
1485                 }
1486                 hisi_sas_dereg_device(hisi_hba, device);
1487
1488                 phy = sas_get_local_phy(device);
1489
1490                 rc = sas_phy_reset(phy, 1);
1491
1492                 if (rc == 0)
1493                         hisi_sas_release_task(hisi_hba, device);
1494                 sas_put_local_phy(phy);
1495         } else {
1496                 struct hisi_sas_tmf_task tmf_task = { .tmf =  TMF_LU_RESET };
1497
1498                 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1499                                                 HISI_SAS_INT_ABT_DEV, 0);
1500                 if (rc < 0) {
1501                         dev_err(dev, "lu_reset: internal abort failed\n");
1502                         goto out;
1503                 }
1504                 hisi_sas_dereg_device(hisi_hba, device);
1505
1506                 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1507                 if (rc == TMF_RESP_FUNC_COMPLETE)
1508                         hisi_sas_release_task(hisi_hba, device);
1509         }
1510 out:
1511         if (rc != TMF_RESP_FUNC_COMPLETE)
1512                 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1513                              sas_dev->device_id, rc);
1514         return rc;
1515 }
1516
1517 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1518 {
1519         struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1520         HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1521
1522         queue_work(hisi_hba->wq, &r.work);
1523         wait_for_completion(r.completion);
1524         if (r.done)
1525                 return TMF_RESP_FUNC_COMPLETE;
1526
1527         return TMF_RESP_FUNC_FAILED;
1528 }
1529
1530 static int hisi_sas_query_task(struct sas_task *task)
1531 {
1532         struct scsi_lun lun;
1533         struct hisi_sas_tmf_task tmf_task;
1534         int rc = TMF_RESP_FUNC_FAILED;
1535
1536         if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1537                 struct scsi_cmnd *cmnd = task->uldd_task;
1538                 struct domain_device *device = task->dev;
1539                 struct hisi_sas_slot *slot = task->lldd_task;
1540                 u32 tag = slot->idx;
1541
1542                 int_to_scsilun(cmnd->device->lun, &lun);
1543                 tmf_task.tmf = TMF_QUERY_TASK;
1544                 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1545
1546                 rc = hisi_sas_debug_issue_ssp_tmf(device,
1547                                                   lun.scsi_lun,
1548                                                   &tmf_task);
1549                 switch (rc) {
1550                 /* The task is still in Lun, release it then */
1551                 case TMF_RESP_FUNC_SUCC:
1552                 /* The task is not in Lun or failed, reset the phy */
1553                 case TMF_RESP_FUNC_FAILED:
1554                 case TMF_RESP_FUNC_COMPLETE:
1555                         break;
1556                 default:
1557                         rc = TMF_RESP_FUNC_FAILED;
1558                         break;
1559                 }
1560         }
1561         return rc;
1562 }
1563
1564 static int
1565 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1566                                   struct sas_task *task, int abort_flag,
1567                                   int task_tag)
1568 {
1569         struct domain_device *device = task->dev;
1570         struct hisi_sas_device *sas_dev = device->lldd_dev;
1571         struct device *dev = hisi_hba->dev;
1572         struct hisi_sas_port *port;
1573         struct hisi_sas_slot *slot;
1574         struct asd_sas_port *sas_port = device->port;
1575         struct hisi_sas_cmd_hdr *cmd_hdr_base;
1576         struct hisi_sas_dq *dq = sas_dev->dq;
1577         int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1578         unsigned long flags, flags_dq = 0;
1579         int wr_q_index;
1580
1581         if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1582                 return -EINVAL;
1583
1584         if (!device->port)
1585                 return -1;
1586
1587         port = to_hisi_sas_port(sas_port);
1588
1589         /* simply get a slot and send abort command */
1590         spin_lock_irqsave(&hisi_hba->lock, flags);
1591         rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1592         if (rc) {
1593                 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1594                 goto err_out;
1595         }
1596         spin_unlock_irqrestore(&hisi_hba->lock, flags);
1597
1598         slot = &hisi_hba->slot_info[slot_idx];
1599         memset(slot, 0, sizeof(struct hisi_sas_slot));
1600
1601         slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1602                         GFP_ATOMIC, &slot->buf_dma);
1603         if (!slot->buf) {
1604                 rc = -ENOMEM;
1605                 goto err_out_tag;
1606         }
1607
1608         spin_lock_irqsave(&dq->lock, flags_dq);
1609         wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1610         if (wr_q_index < 0) {
1611                 spin_unlock_irqrestore(&dq->lock, flags_dq);
1612                 goto err_out_buf;
1613         }
1614         list_add_tail(&slot->delivery, &dq->list);
1615         spin_unlock_irqrestore(&dq->lock, flags_dq);
1616
1617         dlvry_queue = dq->id;
1618         dlvry_queue_slot = wr_q_index;
1619
1620         slot->idx = slot_idx;
1621         slot->n_elem = n_elem;
1622         slot->dlvry_queue = dlvry_queue;
1623         slot->dlvry_queue_slot = dlvry_queue_slot;
1624         cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1625         slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1626         slot->task = task;
1627         slot->port = port;
1628         slot->is_internal = true;
1629         task->lldd_task = slot;
1630
1631         memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1632         memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1633         memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1634
1635         hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1636                                       abort_flag, task_tag);
1637
1638         spin_lock_irqsave(&task->task_state_lock, flags);
1639         task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1640         spin_unlock_irqrestore(&task->task_state_lock, flags);
1641
1642         slot->ready = 1;
1643         /* send abort command to the chip */
1644         spin_lock_irqsave(&dq->lock, flags);
1645         list_add_tail(&slot->entry, &sas_dev->list);
1646         hisi_hba->hw->start_delivery(dq);
1647         spin_unlock_irqrestore(&dq->lock, flags);
1648
1649         return 0;
1650
1651 err_out_buf:
1652         dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1653                       slot->buf_dma);
1654 err_out_tag:
1655         spin_lock_irqsave(&hisi_hba->lock, flags);
1656         hisi_sas_slot_index_free(hisi_hba, slot_idx);
1657         spin_unlock_irqrestore(&hisi_hba->lock, flags);
1658 err_out:
1659         dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1660
1661         return rc;
1662 }
1663
1664 /**
1665  * hisi_sas_internal_task_abort -- execute an internal
1666  * abort command for single IO command or a device
1667  * @hisi_hba: host controller struct
1668  * @device: domain device
1669  * @abort_flag: mode of operation, device or single IO
1670  * @tag: tag of IO to be aborted (only relevant to single
1671  *       IO mode)
1672  */
1673 static int
1674 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1675                              struct domain_device *device,
1676                              int abort_flag, int tag)
1677 {
1678         struct sas_task *task;
1679         struct hisi_sas_device *sas_dev = device->lldd_dev;
1680         struct device *dev = hisi_hba->dev;
1681         int res;
1682
1683         /*
1684          * The interface is not realized means this HW don't support internal
1685          * abort, or don't need to do internal abort. Then here, we return
1686          * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1687          * the internal abort has been executed and returned CQ.
1688          */
1689         if (!hisi_hba->hw->prep_abort)
1690                 return TMF_RESP_FUNC_FAILED;
1691
1692         task = sas_alloc_slow_task(GFP_KERNEL);
1693         if (!task)
1694                 return -ENOMEM;
1695
1696         task->dev = device;
1697         task->task_proto = device->tproto;
1698         task->task_done = hisi_sas_task_done;
1699         task->slow_task->timer.function = hisi_sas_tmf_timedout;
1700         task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1701         add_timer(&task->slow_task->timer);
1702
1703         res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1704                                                 task, abort_flag, tag);
1705         if (res) {
1706                 del_timer(&task->slow_task->timer);
1707                 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1708                         res);
1709                 goto exit;
1710         }
1711         wait_for_completion(&task->slow_task->completion);
1712         res = TMF_RESP_FUNC_FAILED;
1713
1714         /* Internal abort timed out */
1715         if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1716                 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1717                         struct hisi_sas_slot *slot = task->lldd_task;
1718
1719                         if (slot)
1720                                 slot->task = NULL;
1721                         dev_err(dev, "internal task abort: timeout and not done.\n");
1722                         res = -EIO;
1723                         goto exit;
1724                 } else
1725                         dev_err(dev, "internal task abort: timeout.\n");
1726         }
1727
1728         if (task->task_status.resp == SAS_TASK_COMPLETE &&
1729                 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1730                 res = TMF_RESP_FUNC_COMPLETE;
1731                 goto exit;
1732         }
1733
1734         if (task->task_status.resp == SAS_TASK_COMPLETE &&
1735                 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1736                 res = TMF_RESP_FUNC_SUCC;
1737                 goto exit;
1738         }
1739
1740 exit:
1741         dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1742                 "resp: 0x%x sts 0x%x\n",
1743                 SAS_ADDR(device->sas_addr),
1744                 task,
1745                 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1746                 task->task_status.stat);
1747         sas_free_task(task);
1748
1749         return res;
1750 }
1751
1752 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1753 {
1754         hisi_sas_port_notify_formed(sas_phy);
1755 }
1756
1757 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1758 {
1759 }
1760
1761 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1762                         u8 reg_index, u8 reg_count, u8 *write_data)
1763 {
1764         struct hisi_hba *hisi_hba = sha->lldd_ha;
1765
1766         if (!hisi_hba->hw->write_gpio)
1767                 return -EOPNOTSUPP;
1768
1769         return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1770                                 reg_index, reg_count, write_data);
1771 }
1772
1773 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1774 {
1775         phy->phy_attached = 0;
1776         phy->phy_type = 0;
1777         phy->port = NULL;
1778 }
1779
1780 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1781 {
1782         struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1783         struct asd_sas_phy *sas_phy = &phy->sas_phy;
1784         struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1785
1786         if (rdy) {
1787                 /* Phy down but ready */
1788                 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1789                 hisi_sas_port_notify_formed(sas_phy);
1790         } else {
1791                 struct hisi_sas_port *port  = phy->port;
1792
1793                 /* Phy down and not ready */
1794                 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1795                 sas_phy_disconnected(sas_phy);
1796
1797                 if (port) {
1798                         if (phy->phy_type & PORT_TYPE_SAS) {
1799                                 int port_id = port->id;
1800
1801                                 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1802                                                                        port_id))
1803                                         port->port_attached = 0;
1804                         } else if (phy->phy_type & PORT_TYPE_SATA)
1805                                 port->port_attached = 0;
1806                 }
1807                 hisi_sas_phy_disconnected(phy);
1808         }
1809 }
1810 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1811
1812 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1813 {
1814         int i;
1815
1816         for (i = 0; i < hisi_hba->queue_count; i++) {
1817                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1818
1819                 tasklet_kill(&cq->tasklet);
1820         }
1821 }
1822 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1823
1824 struct scsi_transport_template *hisi_sas_stt;
1825 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1826
1827 static struct device_attribute *host_attrs[] = {
1828         &dev_attr_phy_event_threshold,
1829         NULL,
1830 };
1831
1832 static struct scsi_host_template _hisi_sas_sht = {
1833         .module                 = THIS_MODULE,
1834         .name                   = DRV_NAME,
1835         .queuecommand           = sas_queuecommand,
1836         .target_alloc           = sas_target_alloc,
1837         .slave_configure        = hisi_sas_slave_configure,
1838         .scan_finished          = hisi_sas_scan_finished,
1839         .scan_start             = hisi_sas_scan_start,
1840         .change_queue_depth     = sas_change_queue_depth,
1841         .bios_param             = sas_bios_param,
1842         .can_queue              = 1,
1843         .this_id                = -1,
1844         .sg_tablesize           = SG_ALL,
1845         .max_sectors            = SCSI_DEFAULT_MAX_SECTORS,
1846         .use_clustering         = ENABLE_CLUSTERING,
1847         .eh_device_reset_handler = sas_eh_device_reset_handler,
1848         .eh_target_reset_handler = sas_eh_target_reset_handler,
1849         .target_destroy         = sas_target_destroy,
1850         .ioctl                  = sas_ioctl,
1851         .shost_attrs            = host_attrs,
1852 };
1853 struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht;
1854 EXPORT_SYMBOL_GPL(hisi_sas_sht);
1855
1856 static struct sas_domain_function_template hisi_sas_transport_ops = {
1857         .lldd_dev_found         = hisi_sas_dev_found,
1858         .lldd_dev_gone          = hisi_sas_dev_gone,
1859         .lldd_execute_task      = hisi_sas_queue_command,
1860         .lldd_control_phy       = hisi_sas_control_phy,
1861         .lldd_abort_task        = hisi_sas_abort_task,
1862         .lldd_abort_task_set    = hisi_sas_abort_task_set,
1863         .lldd_clear_aca         = hisi_sas_clear_aca,
1864         .lldd_I_T_nexus_reset   = hisi_sas_I_T_nexus_reset,
1865         .lldd_lu_reset          = hisi_sas_lu_reset,
1866         .lldd_query_task        = hisi_sas_query_task,
1867         .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1868         .lldd_port_formed       = hisi_sas_port_formed,
1869         .lldd_port_deformed = hisi_sas_port_deformed,
1870         .lldd_write_gpio = hisi_sas_write_gpio,
1871 };
1872
1873 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1874 {
1875         int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1876
1877         for (i = 0; i < hisi_hba->queue_count; i++) {
1878                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1879                 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1880
1881                 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1882                 memset(hisi_hba->cmd_hdr[i], 0, s);
1883                 dq->wr_point = 0;
1884
1885                 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1886                 memset(hisi_hba->complete_hdr[i], 0, s);
1887                 cq->rd_point = 0;
1888         }
1889
1890         s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1891         memset(hisi_hba->initial_fis, 0, s);
1892
1893         s = max_command_entries * sizeof(struct hisi_sas_iost);
1894         memset(hisi_hba->iost, 0, s);
1895
1896         s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1897         memset(hisi_hba->breakpoint, 0, s);
1898
1899         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1900         memset(hisi_hba->sata_breakpoint, 0, s);
1901 }
1902 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1903
1904 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1905 {
1906         struct device *dev = hisi_hba->dev;
1907         int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1908
1909         spin_lock_init(&hisi_hba->lock);
1910         for (i = 0; i < hisi_hba->n_phy; i++) {
1911                 hisi_sas_phy_init(hisi_hba, i);
1912                 hisi_hba->port[i].port_attached = 0;
1913                 hisi_hba->port[i].id = -1;
1914         }
1915
1916         for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1917                 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1918                 hisi_hba->devices[i].device_id = i;
1919                 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1920         }
1921
1922         for (i = 0; i < hisi_hba->queue_count; i++) {
1923                 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1924                 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1925
1926                 /* Completion queue structure */
1927                 cq->id = i;
1928                 cq->hisi_hba = hisi_hba;
1929
1930                 /* Delivery queue structure */
1931                 spin_lock_init(&dq->lock);
1932                 INIT_LIST_HEAD(&dq->list);
1933                 dq->id = i;
1934                 dq->hisi_hba = hisi_hba;
1935
1936                 /* Delivery queue */
1937                 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1938                 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1939                                         &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1940                 if (!hisi_hba->cmd_hdr[i])
1941                         goto err_out;
1942
1943                 /* Completion queue */
1944                 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1945                 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1946                                 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1947                 if (!hisi_hba->complete_hdr[i])
1948                         goto err_out;
1949         }
1950
1951         s = sizeof(struct hisi_sas_slot_buf_table);
1952         hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
1953         if (!hisi_hba->buffer_pool)
1954                 goto err_out;
1955
1956         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1957         hisi_hba->itct = dma_zalloc_coherent(dev, s, &hisi_hba->itct_dma,
1958                                             GFP_KERNEL);
1959         if (!hisi_hba->itct)
1960                 goto err_out;
1961
1962         hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
1963                                            sizeof(struct hisi_sas_slot),
1964                                            GFP_KERNEL);
1965         if (!hisi_hba->slot_info)
1966                 goto err_out;
1967
1968         s = max_command_entries * sizeof(struct hisi_sas_iost);
1969         hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1970                                             GFP_KERNEL);
1971         if (!hisi_hba->iost)
1972                 goto err_out;
1973
1974         s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1975         hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1976                                 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1977         if (!hisi_hba->breakpoint)
1978                 goto err_out;
1979
1980         hisi_hba->slot_index_count = max_command_entries;
1981         s = hisi_hba->slot_index_count / BITS_PER_BYTE;
1982         hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1983         if (!hisi_hba->slot_index_tags)
1984                 goto err_out;
1985
1986         s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1987         hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1988                                 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1989         if (!hisi_hba->initial_fis)
1990                 goto err_out;
1991
1992         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1993         hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1994                                 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1995         if (!hisi_hba->sata_breakpoint)
1996                 goto err_out;
1997         hisi_sas_init_mem(hisi_hba);
1998
1999         hisi_sas_slot_index_init(hisi_hba);
2000
2001         hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2002         if (!hisi_hba->wq) {
2003                 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2004                 goto err_out;
2005         }
2006
2007         return 0;
2008 err_out:
2009         return -ENOMEM;
2010 }
2011 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2012
2013 void hisi_sas_free(struct hisi_hba *hisi_hba)
2014 {
2015         struct device *dev = hisi_hba->dev;
2016         int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2017
2018         for (i = 0; i < hisi_hba->queue_count; i++) {
2019                 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2020                 if (hisi_hba->cmd_hdr[i])
2021                         dma_free_coherent(dev, s,
2022                                           hisi_hba->cmd_hdr[i],
2023                                           hisi_hba->cmd_hdr_dma[i]);
2024
2025                 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2026                 if (hisi_hba->complete_hdr[i])
2027                         dma_free_coherent(dev, s,
2028                                           hisi_hba->complete_hdr[i],
2029                                           hisi_hba->complete_hdr_dma[i]);
2030         }
2031
2032         dma_pool_destroy(hisi_hba->buffer_pool);
2033
2034         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2035         if (hisi_hba->itct)
2036                 dma_free_coherent(dev, s,
2037                                   hisi_hba->itct, hisi_hba->itct_dma);
2038
2039         s = max_command_entries * sizeof(struct hisi_sas_iost);
2040         if (hisi_hba->iost)
2041                 dma_free_coherent(dev, s,
2042                                   hisi_hba->iost, hisi_hba->iost_dma);
2043
2044         s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2045         if (hisi_hba->breakpoint)
2046                 dma_free_coherent(dev, s,
2047                                   hisi_hba->breakpoint,
2048                                   hisi_hba->breakpoint_dma);
2049
2050
2051         s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2052         if (hisi_hba->initial_fis)
2053                 dma_free_coherent(dev, s,
2054                                   hisi_hba->initial_fis,
2055                                   hisi_hba->initial_fis_dma);
2056
2057         s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2058         if (hisi_hba->sata_breakpoint)
2059                 dma_free_coherent(dev, s,
2060                                   hisi_hba->sata_breakpoint,
2061                                   hisi_hba->sata_breakpoint_dma);
2062
2063         if (hisi_hba->wq)
2064                 destroy_workqueue(hisi_hba->wq);
2065 }
2066 EXPORT_SYMBOL_GPL(hisi_sas_free);
2067
2068 void hisi_sas_rst_work_handler(struct work_struct *work)
2069 {
2070         struct hisi_hba *hisi_hba =
2071                 container_of(work, struct hisi_hba, rst_work);
2072
2073         hisi_sas_controller_reset(hisi_hba);
2074 }
2075 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2076
2077 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2078 {
2079         struct hisi_sas_rst *rst =
2080                 container_of(work, struct hisi_sas_rst, work);
2081
2082         if (!hisi_sas_controller_reset(rst->hisi_hba))
2083                 rst->done = true;
2084         complete(rst->completion);
2085 }
2086 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2087
2088 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2089 {
2090         struct device *dev = hisi_hba->dev;
2091         struct platform_device *pdev = hisi_hba->platform_dev;
2092         struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2093         struct clk *refclk;
2094
2095         if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2096                                           SAS_ADDR_SIZE)) {
2097                 dev_err(dev, "could not get property sas-addr\n");
2098                 return -ENOENT;
2099         }
2100
2101         if (np) {
2102                 /*
2103                  * These properties are only required for platform device-based
2104                  * controller with DT firmware.
2105                  */
2106                 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2107                                         "hisilicon,sas-syscon");
2108                 if (IS_ERR(hisi_hba->ctrl)) {
2109                         dev_err(dev, "could not get syscon\n");
2110                         return -ENOENT;
2111                 }
2112
2113                 if (device_property_read_u32(dev, "ctrl-reset-reg",
2114                                              &hisi_hba->ctrl_reset_reg)) {
2115                         dev_err(dev,
2116                                 "could not get property ctrl-reset-reg\n");
2117                         return -ENOENT;
2118                 }
2119
2120                 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2121                                              &hisi_hba->ctrl_reset_sts_reg)) {
2122                         dev_err(dev,
2123                                 "could not get property ctrl-reset-sts-reg\n");
2124                         return -ENOENT;
2125                 }
2126
2127                 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2128                                              &hisi_hba->ctrl_clock_ena_reg)) {
2129                         dev_err(dev,
2130                                 "could not get property ctrl-clock-ena-reg\n");
2131                         return -ENOENT;
2132                 }
2133         }
2134
2135         refclk = devm_clk_get(dev, NULL);
2136         if (IS_ERR(refclk))
2137                 dev_dbg(dev, "no ref clk property\n");
2138         else
2139                 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2140
2141         if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2142                 dev_err(dev, "could not get property phy-count\n");
2143                 return -ENOENT;
2144         }
2145
2146         if (device_property_read_u32(dev, "queue-count",
2147                                      &hisi_hba->queue_count)) {
2148                 dev_err(dev, "could not get property queue-count\n");
2149                 return -ENOENT;
2150         }
2151
2152         return 0;
2153 }
2154 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2155
2156 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2157                                               const struct hisi_sas_hw *hw)
2158 {
2159         struct resource *res;
2160         struct Scsi_Host *shost;
2161         struct hisi_hba *hisi_hba;
2162         struct device *dev = &pdev->dev;
2163
2164         shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba));
2165         if (!shost) {
2166                 dev_err(dev, "scsi host alloc failed\n");
2167                 return NULL;
2168         }
2169         hisi_hba = shost_priv(shost);
2170
2171         INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2172         hisi_hba->hw = hw;
2173         hisi_hba->dev = dev;
2174         hisi_hba->platform_dev = pdev;
2175         hisi_hba->shost = shost;
2176         SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2177
2178         timer_setup(&hisi_hba->timer, NULL, 0);
2179
2180         if (hisi_sas_get_fw_info(hisi_hba) < 0)
2181                 goto err_out;
2182
2183         if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2184             dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2185                 dev_err(dev, "No usable DMA addressing method\n");
2186                 goto err_out;
2187         }
2188
2189         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2190         hisi_hba->regs = devm_ioremap_resource(dev, res);
2191         if (IS_ERR(hisi_hba->regs))
2192                 goto err_out;
2193
2194         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2195         if (res) {
2196                 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2197                 if (IS_ERR(hisi_hba->sgpio_regs))
2198                         goto err_out;
2199         }
2200
2201         if (hisi_sas_alloc(hisi_hba, shost)) {
2202                 hisi_sas_free(hisi_hba);
2203                 goto err_out;
2204         }
2205
2206         return shost;
2207 err_out:
2208         scsi_host_put(shost);
2209         dev_err(dev, "shost alloc failed\n");
2210         return NULL;
2211 }
2212
2213 int hisi_sas_probe(struct platform_device *pdev,
2214                          const struct hisi_sas_hw *hw)
2215 {
2216         struct Scsi_Host *shost;
2217         struct hisi_hba *hisi_hba;
2218         struct device *dev = &pdev->dev;
2219         struct asd_sas_phy **arr_phy;
2220         struct asd_sas_port **arr_port;
2221         struct sas_ha_struct *sha;
2222         int rc, phy_nr, port_nr, i;
2223
2224         shost = hisi_sas_shost_alloc(pdev, hw);
2225         if (!shost)
2226                 return -ENOMEM;
2227
2228         sha = SHOST_TO_SAS_HA(shost);
2229         hisi_hba = shost_priv(shost);
2230         platform_set_drvdata(pdev, sha);
2231
2232         phy_nr = port_nr = hisi_hba->n_phy;
2233
2234         arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2235         arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2236         if (!arr_phy || !arr_port) {
2237                 rc = -ENOMEM;
2238                 goto err_out_ha;
2239         }
2240
2241         sha->sas_phy = arr_phy;
2242         sha->sas_port = arr_port;
2243         sha->lldd_ha = hisi_hba;
2244
2245         shost->transportt = hisi_sas_stt;
2246         shost->max_id = HISI_SAS_MAX_DEVICES;
2247         shost->max_lun = ~0;
2248         shost->max_channel = 1;
2249         shost->max_cmd_len = 16;
2250         shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2251         shost->can_queue = hisi_hba->hw->max_command_entries;
2252         shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2253
2254         sha->sas_ha_name = DRV_NAME;
2255         sha->dev = hisi_hba->dev;
2256         sha->lldd_module = THIS_MODULE;
2257         sha->sas_addr = &hisi_hba->sas_addr[0];
2258         sha->num_phys = hisi_hba->n_phy;
2259         sha->core.shost = hisi_hba->shost;
2260
2261         for (i = 0; i < hisi_hba->n_phy; i++) {
2262                 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2263                 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2264         }
2265
2266         rc = scsi_add_host(shost, &pdev->dev);
2267         if (rc)
2268                 goto err_out_ha;
2269
2270         rc = sas_register_ha(sha);
2271         if (rc)
2272                 goto err_out_register_ha;
2273
2274         rc = hisi_hba->hw->hw_init(hisi_hba);
2275         if (rc)
2276                 goto err_out_register_ha;
2277
2278         scsi_scan_host(shost);
2279
2280         return 0;
2281
2282 err_out_register_ha:
2283         scsi_remove_host(shost);
2284 err_out_ha:
2285         hisi_sas_free(hisi_hba);
2286         scsi_host_put(shost);
2287         return rc;
2288 }
2289 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2290
2291 int hisi_sas_remove(struct platform_device *pdev)
2292 {
2293         struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2294         struct hisi_hba *hisi_hba = sha->lldd_ha;
2295         struct Scsi_Host *shost = sha->core.shost;
2296
2297         if (timer_pending(&hisi_hba->timer))
2298                 del_timer(&hisi_hba->timer);
2299
2300         sas_unregister_ha(sha);
2301         sas_remove_host(sha->core.shost);
2302
2303         hisi_sas_free(hisi_hba);
2304         scsi_host_put(shost);
2305         return 0;
2306 }
2307 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2308
2309 static __init int hisi_sas_init(void)
2310 {
2311         hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2312         if (!hisi_sas_stt)
2313                 return -ENOMEM;
2314
2315         return 0;
2316 }
2317
2318 static __exit void hisi_sas_exit(void)
2319 {
2320         sas_release_transport(hisi_sas_stt);
2321 }
2322
2323 module_init(hisi_sas_init);
2324 module_exit(hisi_sas_exit);
2325
2326 MODULE_LICENSE("GPL");
2327 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2328 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2329 MODULE_ALIAS("platform:" DRV_NAME);